Exemplo n.º 1
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns>Result of action</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            if (AlreadyUpdated)
            {
                AlreadyUpdated = false;
                return(ProcessResult(this.Result));
            }

            BehaviorResult result = BehaviorResult.Failure;// by default failure

            // execute handler and get result back
            if (_Handler != null)
            {
                result = _Handler(this, status.Parameters);
            }

            status.ChangePosture(this.ChangePosture);

            // if action needs to run next frame store it's reference
            if (result == BehaviorResult.Running)
            {
                status.RunningActions.Add(this, status.Parameters);
            }
            else
            {
                status.RunningActions.Remove(this);
            }
            return(ProcessResult(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns>Result</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            if (Priority == PriorityType.HighestPriority || RunningChildIndex < 0)
            {
                RunningChildIndex = 0;
            }
            BehaviorResult result = BehaviorResult.Failure;

            for (int i = RunningChildIndex; i < ChildCount; i++)
            {
                if (status.IsInterrupted)
                {
                    break;
                }
                BehaviorContainer node = this[i];
                status.Parameters = node.Parameters;
                node.Execute(status);
                result = node.Result;
                if (result == BehaviorResult.Running)
                {
                    RunningChildIndex = i;
                    break;
                }
                else
                {
                    RunningChildIndex = -1;
                }
                if (result == BehaviorResult.Success)
                {
                    ResetNext(i, status);
                    break;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Reset behavior. For internal use. when a branch with higher priority executed, let nodes in previous branch reset (internal use)
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public virtual void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running && LastUpdateId != status.UpdateId)
     {
         Result = BehaviorResult.Failure;
     }
 }
Exemplo n.º 4
0
        // update action manually when action is running
        internal void UpdateImmediately(BehaviorTreeStatus status)
        {
            try
            {
                // execute handler and get result back
                if (this._Handler != null)
                {
                    this.Result = ProcessResult(this._Handler(this, status.Parameters));
                }
                else
                {
                    this.Result = ProcessResult(BehaviorResult.Failure);// by default failure
                }
                this.AlreadyUpdated = true;

                // we do not remove action from RunningActions here because this method called inside a foreach of RunningActions
                // and we can not modify list inside foreach
            }
            catch (Exception ex)
            {
                status.Exception = ex;
                this.Result      = BehaviorResult.Failure;
                return;
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status od BehaviorTree</param>
        /// <returns></returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (Child != null)
            {
                if (_Handler != null)
                {
                    if (Result == BehaviorResult.Running)// continue execution of child
                    {
                        result = TraceChild(status);
                    }
                    else if (_Handler(this, status.Parameters))
                    {
                        result = TraceChild(status);
                    }
                }
                else
                {
                    result = TraceChild(status);
                }
            }

            if (NeverFail && result == BehaviorResult.Failure)
            {
                result = BehaviorResult.Success;
            }
            return(result);
        }
Exemplo n.º 6
0
 /// <summary>
 /// Behave
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 /// <returns>Result of action</returns>
 protected override BehaviorResult Behave(BehaviorTreeStatus status)
 {
     if (status.Tree != null)
     {
         status.Tree.ChangeState(DestinationState);
     }
     return(BehaviorResult.Success);
 }
Exemplo n.º 7
0
 private void ResetNext(int index, BehaviorTreeStatus status)
 {
     for (int i = index + 1; i < ChildCount; i++)
     {
         BehaviorContainer node = this[i];
         node.Behavior.ResetBehavior(status);
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// Reset behavior
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running && status.UpdateId != LastUpdateId)
     {
         _LoopCounter = 0;
     }
     base.ResetBehavior(status);
 }
Exemplo n.º 9
0
 /// <summary>
 /// Reset behavior. For internal use. when a branch with higher priority executed, let nodes in previous branch reset
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running)
     {
         if (LastUpdateId != status.UpdateId)
         {
             ResetChildrenExecution();
         }
     }
     base.ResetBehavior(status);
 }
Exemplo n.º 10
0
 /// <summary>
 /// Reset behavior
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running)
     {
         if (Child != null)
         {
             Child.Behavior.ResetBehavior(status);
         }
     }
     base.ResetBehavior(status);
 }
Exemplo n.º 11
0
        /// <summary>
        /// Trace Child - subclasses should implement this method
        /// </summary>
        /// <param name="status">Status od BehaviorTree</param>
        /// <returns></returns>
        protected virtual BehaviorResult TraceChild(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (Child != null)
            {
                status.Parameters = Child.Parameters;
                Child.Execute(status);
                result = Child.Result;
            }
            return(result);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Reset behavior. For internal use. when a branch with higher priority executed, let nodes in previous branch reset
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (_Lock && Result == BehaviorResult.Running)
     {
         if (LastUpdateId != status.UpdateId)
         {
             AccessKey.Unlock();
             _Lock = false;
         }
     }
     base.ResetBehavior(status);
 }
Exemplo n.º 13
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns>esult</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (RunningChildIndex < 0)
            {
                RunningChildIndex = 0;
            }
            for (int i = RunningChildIndex; i < ChildCount; i++)
            {
                if (status.IsInterrupted)
                {
                    break;
                }
                BehaviorContainer node = this[i];
                status.Parameters = node.Parameters;
                node.Execute(status);
                result = node.Result;
                if (result == BehaviorResult.Running)
                {
                    RunningChildIndex = i;
                    break;
                }
                else
                {
                    RunningChildIndex = -1;
                }
                if (result == BehaviorResult.Failure)
                {
                    break;
                }
            }
            if (result == BehaviorResult.Success) // cause loop next update and begin from child 0
            {
                _LoopCounter++;
                if (LoopCount > 0 && _LoopCounter >= LoopCount)
                {
                    result       = BehaviorResult.Success;
                    _LoopCounter = 0;
                }
                else
                {
                    result = BehaviorResult.Running;
                }
                RunningChildIndex = 0;
            }
            else if (result == BehaviorResult.Failure)
            {
                _LoopCounter = 0;
            }
            return(result);
        }
Exemplo n.º 14
0
 /// <summary>
 /// Reset behavior. For internal use. when a branch with higher priority executed, let nodes in previous branch reset
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running)
     {
         if (LastUpdateId != status.UpdateId)
         {
             if (status.RunningActions.Remove(this))
             {
                 OnReset();
                 base.ResetBehavior(status);
             }
         }
     }
 }
Exemplo n.º 15
0
 /// <summary>
 /// handle execution of behavior and call appropriate events
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 /// <returns>Result of execution</returns>
 public BehaviorResult Execute(BehaviorTreeStatus status)
 {
     LastUpdateId = status.UpdateId;
     try
     {
         Result = Behave(status);// let subclass behave
     }
     catch (Exception e)
     {
         status.Exception = e;                      // store exception
         Result           = BehaviorResult.Failure; // set result to failure
     }
     return(Result);
 }
Exemplo n.º 16
0
        /// <summary>
        /// iterate throw children and evaluate conditions
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns></returns>
        private BehaviorResult CheckConditions(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Success;

            for (int i = 0; i < ChildCount; i++)
            {
                BehaviorContainer node = this[i];
                status.Parameters = node.Parameters;

                if (node.Behavior.Type == BehaviorType.Condition)
                {
                    node.Execute(status);
                    BehaviorResult r = node.Result;
                    if (r == BehaviorResult.Failure)
                    {
                        if (BreakOnConditionFailure && node.Behavior.Type == BehaviorType.Condition)
                        {
                            result = BehaviorResult.Failure;
                            break;
                        }
                        else
                        {
                            _FailureCount++;
                        }
                    }
                    // check failure policity
                    if ((FailurePolicy == AI.FailurePolicy.FailOnOne && _FailureCount > 0) || (FailurePolicy == AI.FailurePolicy.FailOnAll && _FailureCount == ChildCount))
                    {
                        result = BehaviorResult.Failure;
                        break;
                    }
                    // check success policity
                    if (SuccessPolicy == AI.SuccessPolicy.SucceedOnOne && r == BehaviorResult.Success)
                    {
                        result = BehaviorResult.Success;
                        break;
                    }

                    // diable these lines because : conditions returns success or failure as result
                    // if result of this node is running or result of any previous node is running, set result to running
                    //if (r == BehaviorResult.Running || result != BehaviorResult.Running)
                    //result = r;
                }
            }
            return(result);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns></returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (_Handler != null)
            {
                bool b = _Handler(this, status.Parameters);
                if (Reverse)
                {
                    result = b ? BehaviorResult.Failure : BehaviorResult.Success;
                }
                else
                {
                    result = b ? BehaviorResult.Success : BehaviorResult.Failure;
                }
            }
            return(result);
        }
Exemplo n.º 18
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status od BehaviorTree</param>
        /// <returns>Result</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            if (RunningChildIndex < 0)
            {
                RunningChildIndex = GetRandomIndex();// pick random node
            }
            BehaviorResult    result = BehaviorResult.Failure;
            BehaviorContainer node   = this[RunningChildIndex];

            status.Parameters = node.Parameters;
            node.Execute(status);
            result = node.Result;
            if (result != BehaviorResult.Running)
            {
                RunningChildIndex = -1;
            }
            return(result);
        }
Exemplo n.º 19
0
 /// <summary>
 /// Reset behavior. For internal use. when a branch with higher priority executed, let nodes in previous branch reset
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 public override void ResetBehavior(BehaviorTreeStatus status)
 {
     if (Result == BehaviorResult.Running)
     {
         if (LastUpdateId != status.UpdateId)
         {
             RunningChildIndex = -1;
         }
         foreach (var child in this)
         {
             if (child != null)
             {
                 child.Behavior.ResetBehavior(status);
             }
         }
     }
     base.ResetBehavior(status);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status of BehaviorTree</param>
        /// <returns>Result</returns>
        //protected override BehaviorResult Behave(BehaviorStatus status)
        //{
        //    BehaviorResult result = BehaviorResult.Failure;
        //    if (Child != null)
        //    {
        //        if (AccessKey != null)
        //        {
        //            if (!_Lock)// if do not access to key
        //                _Lock = AccessKey.Lock();// try to lock key
        //            if (_Lock)// if success, execute child
        //            {
        //                status.Parameters = Child.Parameters;
        //                result = Child.Behavior.Trace(status);
        //            }
        //            if (_Lock && result != BehaviorResult.Running)// if finish job, unlock key
        //            {
        //                AccessKey.Unlock();
        //                _Lock = false;
        //            }
        //        }
        //    }
        //    if (NeverFail && result == BehaviorResult.Failure)
        //        result = BehaviorResult.Success;
        //    return result;
        //}

        protected override BehaviorResult TraceChild(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (AccessKey != null)
            {
                if (!_Lock)                   // if do not access to key
                {
                    _Lock = AccessKey.Lock(); // try to lock key
                }
                if (_Lock)                    // if success, execute child
                {
                    result = base.TraceChild(status);
                }
                if (_Lock && result != BehaviorResult.Running)// if finish job, unlock key
                {
                    AccessKey.Unlock();
                    _Lock = false;
                }
            }
            return(result);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">Status od BehaviorTree</param>
        /// <returns>Result</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            BehaviorResult result = BehaviorResult.Failure;

            if (RunningChildIndex < 0)
            {
                RunningChildIndex = 0;
            }
            for (int i = RunningChildIndex; i < ChildCount; i++)
            {
                if (status.IsInterrupted)
                {
                    break;
                }
                BehaviorContainer node = this[i];
                status.Parameters = node.Parameters;
                node.Execute(status);
                result = node.Result;
                if (result == BehaviorResult.Running)
                {
                    RunningChildIndex = i;
                    break;
                }
                else
                {
                    RunningChildIndex = -1;
                }
                if (result == BehaviorResult.Failure)
                {
                    break;
                }
            }
            if (result != BehaviorResult.Running)
            {
                RunningChildIndex = -1;
            }
            return(result);
        }
Exemplo n.º 22
0
        /// <summary>
        /// Behave
        /// </summary>
        /// <param name="status">status of BehaviorTree</param>
        /// <returns>Result</returns>
        protected override BehaviorResult Behave(BehaviorTreeStatus status)
        {
            _FailureCount = 0;
            _SuccessCount = 0;
            CreateChildrenExecution();                      // make sure the  _ChildrenResults array is valid
            BehaviorResult result = BehaviorResult.Running; // by default running

            // iterate throw children an execute them
            for (int i = 0; i < ChildCount; i++)
            {
                if (status.IsInterrupted)
                {
                    ResetChildrenExecution();
                    break;
                }
                BehaviorContainer node = this[i];
                status.Parameters = node.Parameters;

                if (_ChildrenExecution[i])
                {
                    if (node.Behavior.Concurrency == ConcurrencyMode.UntilFailure && node.Behavior.Result == BehaviorResult.Failure)
                    {
                        _FailureCount++;
                        if ((FailurePolicy == AI.FailurePolicy.FailOnOne && _FailureCount > 0) || (FailurePolicy == AI.FailurePolicy.FailOnAll && _FailureCount == ChildCount))
                        {
                            result = BehaviorResult.Failure;
                            break;
                        }
                        continue;
                    }
                    else if (node.Behavior.Concurrency == ConcurrencyMode.UntilSuccess && node.Behavior.Result == BehaviorResult.Success)
                    {
                        _SuccessCount++;
                        if (SuccessPolicy == AI.SuccessPolicy.SucceedOnAll)
                        {
                            if (_SuccessCount == ChildCount)
                            {
                                result = BehaviorResult.Success;
                                break;
                            }
                        }
                        continue;
                    }
                }

                // if this node executed first ignore it
                //if (FirstConditions && node.Behavior.Type == BehaviorType.Condition) continue;

                node.Execute(status);// execute child node
                BehaviorResult childResult = node.Result;
                if (childResult == BehaviorResult.Failure)
                {
                    if (BreakOnConditionFailure && node.Behavior.Type == BehaviorType.Condition)
                    {
                        result = BehaviorResult.Failure;
                        break;
                    }
                    else
                    {
                        _FailureCount++;
                    }
                }
                else if (childResult == BehaviorResult.Success)
                {
                    _SuccessCount++;
                }
                // check failure policity
                if ((FailurePolicy == AI.FailurePolicy.FailOnOne && _FailureCount > 0) || (FailurePolicy == AI.FailurePolicy.FailOnAll && _FailureCount == ChildCount))
                {
                    result = BehaviorResult.Failure;
                    break;
                }
                // check success policity
                if (SuccessPolicy == AI.SuccessPolicy.SucceedOnOne)
                {
                    if (childResult == BehaviorResult.Success)
                    {
                        result = BehaviorResult.Success;
                        break;
                    }
                }
                else if (SuccessPolicy == AI.SuccessPolicy.SucceedOnAll)
                {
                    if (_SuccessCount == ChildCount)
                    {
                        result = BehaviorResult.Success;
                        break;
                    }
                }

                //// if result of this node is running or result of any previous node is running, set result to running
                //if (childResult == BehaviorResult.Running || result != BehaviorResult.Running)
                //    result = childResult;

                _ChildrenExecution[i] = true;
            }


            if (result != BehaviorResult.Running)
            {
                ResetChildrenExecution();
            }
            return(result);
        }
Exemplo n.º 23
0
 public void Execute(BehaviorTreeStatus status)
 {
     status.RegisterForExecution(this); // register in execution sequence
     Result = Behavior.Execute(status);
 }
Exemplo n.º 24
0
 /// <summary>
 /// Let subclass implement behaveior
 /// </summary>
 /// <param name="status">Status of BehaviorTre</param>
 /// <returns>Result of behavior</returns>
 protected abstract BehaviorResult Behave(BehaviorTreeStatus status);
Exemplo n.º 25
0
 /// <summary>
 /// for internal use when behavior tree let action continue (when result is BehaviorResult.Running)
 /// </summary>
 /// <param name="status">Status of BehaviorTree</param>
 /// <returns>Result</returns>
 internal BehaviorResult Continue(BehaviorTreeStatus status)
 {
     return(Behave(status));
 }