public BehaviorReturnCode Behave(object agent, Blackboard blackboard) { try { if (!started) { Enter(agent, blackboard); started = true; } BehaviorReturnCode returnCode = Update(agent, blackboard); if (returnCode != BehaviorReturnCode.Running) { started = false; Exit(agent, blackboard); } return(returnCode); } catch (Exception e) { //Output error HandleException(e); //This means that all nodes will return FAILURE if they throw an unhandled exception ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
public BehaviorReturnCode Stop() { if (!stopped) { if (debug) { Debug.Log("Stop"); } BehaviorReturnCode code = aiMovement.Stop(); if (code == BehaviorReturnCode.Running) { return(BehaviorReturnCode.Running); } else if (code == BehaviorReturnCode.Success) { stopped = true; return(code); } return(code); } else { return(BehaviorReturnCode.Success); } }
/// <summary> /// perform the behavior /// </summary> public BehaviorReturnCode Behave(TreeContext context) { try { switch (_Root.Behave(context)) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; break; case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; break; case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; break; default: ReturnCode = BehaviorReturnCode.Running; break; } } catch (Exception e) { #if DEBUG Console.Error.WriteLine(e.ToString()); #endif ReturnCode = BehaviorReturnCode.Failure; } return(this.ReturnCode); }
/// <summary> /// /// </summary> /// <returns></returns> public override BehaviorReturnCode Run() { if (_children.Count == 0) { return(BehaviorReturnCode.BT_SUCCESS); } if (_currentPosition == -1) { Init(); } BehaviorTreeNode currentRunningNode = _children[_currentPosition]; BehaviorReturnCode status = BehaviorReturnCode.BT_FAILURE; while ((status = currentRunningNode.Run()) == BehaviorReturnCode.BT_SUCCESS) { _currentPosition++; // Fallan todos los hijos if (_currentPosition == _children.Count) { _currentPosition = -1; return(status); } currentRunningNode = _children[_currentPosition]; } if (status == BehaviorReturnCode.BT_FAILURE) { _currentPosition = -1; } return(status); }
public BehaviorReturnCode Behave(Entity entity) { try { switch (_action(entity)) { case BehaviorReturnCode.Success: return(ReturnCode = BehaviorReturnCode.Success); case BehaviorReturnCode.Failure: return(ReturnCode = BehaviorReturnCode.Failure); case BehaviorReturnCode.Running: return(ReturnCode = BehaviorReturnCode.Running); default: return(ReturnCode = BehaviorReturnCode.Failure); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
/// <summary> /// perform the behavior /// </summary> public BehaviorReturnCode Behave() { try { switch (_Root.Behave()) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); default: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); } } catch (Exception e) { Debug.LogError(e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { try { switch (_Behaviors[_index(entity)].Behave(entity)) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); default: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { try { if (_counter < _max_count) { _counter++; ReturnCode = BehaviorReturnCode.Running; return(BehaviorReturnCode.Running); } else { _counter = 0; ReturnCode = _behavior.Behave(entity); return(ReturnCode); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(BehaviorReturnCode.Failure); } }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { try { switch (_bool(entity)) { case true: ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case false: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); default: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
public BehaviorReturnCode Behave(TreeContext context) { context.OnAboutToCall(this); this.ReturnCode = this.OnBehave(context); context.OnCalled(this, this.ReturnCode); return(this.ReturnCode); }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { //_Random = new Random(DateTime.Now.Millisecond); // try { switch (_Behaviors[UnityEngine.Random.Range(0, _Behaviors.Length)].Behave(entity)) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); default: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
/// <summary> /// perform the behavior /// </summary> public BehaviorReturnCode Behave() { try { switch (_Root.Behave()) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); default: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); } } catch (Exception e) { #if DEBUG Console.Error.WriteLine(e.ToString()); #endif ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); } }
/// <summary> /// perform the behavior /// </summary> public BehaviorReturnCode Behave() { try { switch (b_Root.Behave()) { case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; case BehaviorReturnCode.Success: ReturnCode = BehaviorReturnCode.Success; return ReturnCode; case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return ReturnCode; default: ReturnCode = BehaviorReturnCode.Running; return ReturnCode; } } catch (Exception e) { #if DEBUG Console.Error.WriteLine(e.ToString()); #endif ReturnCode = BehaviorReturnCode.Failure; return ReturnCode; } }
/// <summary> /// /// </summary> /// <param name="code"></param> private void setChildrenStatus(BehaviorReturnCode code) { for (int i = 0; i < _childrenStatus.Count; i++) { _childrenStatus[i] = code; } }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { for (; _LastBehavior < _Behaviors.Length; _LastBehavior++) { try{ switch (_Behaviors[_LastBehavior].Behave(entity)) { case BehaviorReturnCode.Failure: continue; case BehaviorReturnCode.Success: _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Success; return(ReturnCode); case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; return(ReturnCode); default: continue; } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); continue; } } _LastBehavior = 0; ReturnCode = BehaviorReturnCode.Failure; return(ReturnCode); }
protected override BehaviorReturnCode Update(object agent, Blackboard blackboard) { BehaviorReturnCode returnedCode = _Behavior.Behave(agent, blackboard); switch (returnedCode) { case BehaviorReturnCode.Success: counter++; if (counter == times) { ReturnCode = BehaviorReturnCode.Success; } else { ReturnCode = BehaviorReturnCode.Running; } break; case BehaviorReturnCode.Running: ReturnCode = BehaviorReturnCode.Running; break; case BehaviorReturnCode.Failure: ReturnCode = BehaviorReturnCode.Failure; break; } return(ReturnCode); }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { try { _TimeElapsed += _ElapsedTimeFunction(); if (_TimeElapsed >= _WaitTime) { _TimeElapsed = 0; ReturnCode = _Behavior.Behave(entity); return(ReturnCode); } else { ReturnCode = BehaviorReturnCode.Running; return(BehaviorReturnCode.Running); } } catch (Exception e) { Debug.Log("oopsie..." + e.ToString()); ReturnCode = BehaviorReturnCode.Failure; return(BehaviorReturnCode.Failure); } }
// <summary> /// /// </summary> /// <returns></returns> public override BehaviorReturnCode Run() { if (_children.Count == 0) { return(BehaviorReturnCode.BT_SUCCESS); } if (_currentPosition == -1) { Init(); } System.Random random = new System.Random(DateTime.Now.Millisecond); int randomNumber = 0; BehaviorReturnCode status = BehaviorReturnCode.BT_FAILURE; if (_currentPosition == -1) { randomNumber = random.Next(0, _children.Count); } else { randomNumber = _currentPosition; } BehaviorTreeNode currentRunningNode = _children[randomNumber]; status = currentRunningNode.Run(); if (status == BehaviorReturnCode.BT_SUCCESS || status == BehaviorReturnCode.BT_FAILURE) { _currentPosition = -1; } return(status); }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { ReturnCode = _Behavior.Behave(entity); if (ReturnCode == BehaviorReturnCode.Failure) { ReturnCode = BehaviorReturnCode.Success; } return(ReturnCode); }
/// <summary> /// Initializes a new instance of the <see cref="NavigationPathExistNode"/> class. /// </summary> /// <param name="positionFunc">A function that returns the desired location.</param> /// <param name="hasPathKey">Blackboard Property key for storing if there is a possible navigation path.</param> /// <param name="failureCode">The code that will return if failed.</param> public NavigationPathExistNode( Func <BasicEntity, Coord> positionFunc, string hasPathKey = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Failure) { this.Function = positionFunc; this.HasPathKey = hasPathKey; this.FailureCode = failureCode; }
/// <summary> /// Initializes a new instance of the <see cref="NavigationAtLocationNode"/> class. /// </summary> /// <param name="positionFunc">A function that returns the desired location.</param> /// <param name="atLocationKey">Blackboard Property key for storing if we are at that location.</param> /// <param name="failureCode">The code that will return if failed.</param> public NavigationAtLocationNode( Func <BasicEntity, Coord> positionFunc, string atLocationKey = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Failure) { this.Function = positionFunc; this.AtLocationKey = atLocationKey; this.FailureCode = failureCode; }
public void OnCalled(BehaviorComponent called, BehaviorReturnCode result) { this.AddToCalled(called); // Check if we just called the root node, so are done with the tree. if (!this.isInvokingFinalizer && this.calling.Count > 0 && this.calling[0] == called) { this.ProcessFinalizers(this.calling[0]); } }
/// <summary> /// Initializes a new instance of the <see cref="EnvironmentQueryNode"/> class. /// </summary> /// <param name="radiusFunc">A function that returns the entities fov radius.</param> /// <param name="evaluator">A function that evaluates if the entity should be counted.</param> /// <param name="selector">A function that evaluates all the found entities in the radius.</param> /// <param name="fieldOfView">Whether it should only count entities that are visible.</param> /// <param name="spottedKey">Blackboard Property key for storing if we spotted something.</param> /// <param name="spottedCoordKey">Blackboard Property key for storing where we spotted something.</param> /// <param name="failureCode">The code that will return if failed.</param> public EnvironmentQueryNode( Func <BasicEntity, int> radiusFunc, Func <BasicEntity, bool> evaluator = null, Func <List <BasicEntity>, List <BasicEntity> > selector = null, bool fieldOfView = true, string spottedKey = null, string spottedCoordKey = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Running) : base(radiusFunc, evaluator, selector, fieldOfView, spottedKey, spottedCoordKey, failureCode) { }
/// <summary> /// Initializes a new instance of the <see cref="RandomDecoratorNode"/> class. /// </summary> /// <param name="child">The child.</param> /// <param name="probability">The probability that it will execute.</param> /// <param name="function">The random function.</param> /// <param name="failureCode">The code that will return if it wont execute.</param> public RandomDecoratorNode( BehaviorTreeNode child, float probability, Func <Blackboard, float> function = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Failure) { this.Child = child ?? throw new ArgumentNullException(nameof(child)); this.Probability = probability; this.Function = function ?? RandomProbability; this.FailureCode = failureCode; }
/// <summary> /// Initializes a new instance of the <see cref="TimerNode"/> class. /// </summary> /// <param name="child">The child.</param> /// <param name="sleepTime">The amount of time in seconds that we will wait.</param> /// <param name="durationKey">Blackboard Property key for storing the current duration.</param> /// <param name="failureCode">The code that will return when the duration is hit.</param> public TimerNode( BehaviorTreeNode child, float sleepTime, string durationKey = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Running) { this.Child = child ?? throw new ArgumentNullException(nameof(child)); this.SleepTime = sleepTime; this.DurationKey = durationKey ?? Guid.ToString(); this.FailureCode = failureCode; }
/// <summary> /// Run the child until it fails. /// </summary> /// <returns>Success if the child fails or Running if not</returns> public override BehaviorReturnCode Run() { BehaviorReturnCode status = _children[0].Run(); if (status != BehaviorReturnCode.BT_FAILURE) { Init(); return(BehaviorReturnCode.BT_RUNNING); } return(BehaviorReturnCode.BT_SUCCESS); }
/// <summary> /// performs the given behavior /// </summary> /// <returns>the behaviors return code</returns> public BehaviorReturnCode Behave(Entity entity) { ReturnCode = _Behavior.Behave(entity); if (ReturnCode == BehaviorReturnCode.Failure) { return(BehaviorReturnCode.Failure); } else { ReturnCode = BehaviorReturnCode.Running; return(BehaviorReturnCode.Running); } }
public BehaviorReturnCode DodgeJump() { BehaviorReturnCode toReturn = aiMovement.DodgeJump(); if (toReturn.Equals(BehaviorReturnCode.Running)) { onDodge = true; } else { onDodge = false; } return(toReturn); }
public override BehaviorReturnCode Run() { BehaviorReturnCode status = _children[0].Run(); if (status == BehaviorReturnCode.BT_FAILURE) { return(BehaviorReturnCode.BT_SUCCESS); } else if (status == BehaviorReturnCode.BT_SUCCESS) { return(BehaviorReturnCode.BT_FAILURE); } return(status); }
public void selector_1() { _log.enterScope(); UtilityVector vector = new UtilityVector(0, 1, 0, 2); BehaviorAction action = new BehaviorAction(delegate() { return(BehaviorReturnCode.Success); }); UtilityPair pair = new UtilityPair(vector, action); UtilitySelector sel = new UtilitySelector(delegate(){ return(new UtilityVector(0, 1, 1, 2)); }, pair, pair); BehaviorReturnCode result = sel.Behave(); Verify.VerifyNotEquals("basic vector compare", true, result, BehaviorReturnCode.Failure); _log.exitScope(); }
/// <summary> /// Initializes a new instance of the <see cref="RetryNode"/> class. /// </summary> /// <param name="child">The child node.</param> /// <param name="maxAttempts">The max attempts this node will be executed.</param> /// <param name="attemptKey">The blackboard property key that will contain the how many attempts have passed.</param> /// <param name="failureCode">The code that will return if exceed max attempts.</param> public RetryNode( BehaviorTreeNode child, int maxAttempts, string attemptKey = null, BehaviorReturnCode failureCode = BehaviorReturnCode.Failure) { if (maxAttempts <= 0) { throw new ArgumentOutOfRangeException(nameof(maxAttempts)); } this.Child = child ?? throw new ArgumentNullException(nameof(child)); this.MaxAttempts = maxAttempts; this.AttemptKey = attemptKey; this.FailureCode = failureCode; }