Пример #1
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    return(childStatus);
                }
                if (base.m_activeChildIndex != -1)
                {
                    BehaviorTask task = base.m_children[base.m_activeChildIndex];
                    return(task.exec(pAgent));
                }
                int   num  = FrameRandom.Random((ushort)this.m_totalSum);
                float num2 = 0f;

                for (int i = 0; i < base.m_children.Count; i++)
                {
                    int num4 = this.m_weightingMap[i];
                    num2 += num4;
                    if ((num4 > 0) && (num2 >= num))
                    {
                        EBTStatus status2 = base.m_children[i].exec(pAgent);
                        if (status2 == EBTStatus.BT_RUNNING)
                        {
                            base.m_activeChildIndex = i;
                            return(status2);
                        }
                        base.m_activeChildIndex = -1;
                        return(status2);
                    }
                }
                return(EBTStatus.BT_FAILURE);
            }
Пример #2
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                bool      flag = true;
                EBTStatus eBTStatus;

                while (true)
                {
                    eBTStatus = childStatus;
                    if (!flag || eBTStatus == EBTStatus.BT_RUNNING)
                    {
                        int          num          = this.m_set.get_Item(this.m_activeChildIndex);
                        BehaviorTask behaviorTask = this.m_children.get_Item(num);
                        eBTStatus = behaviorTask.exec(pAgent);
                    }
                    flag = false;
                    if (eBTStatus != EBTStatus.BT_SUCCESS)
                    {
                        break;
                    }
                    this.m_activeChildIndex++;
                    if (this.m_activeChildIndex >= this.m_children.get_Count())
                    {
                        return(EBTStatus.BT_SUCCESS);
                    }
                    if (!this.CheckPredicates(pAgent))
                    {
                        return(EBTStatus.BT_FAILURE);
                    }
                }
                return(eBTStatus);
            }
Пример #3
0
 protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
 {
     if (childStatus != EBTStatus.BT_RUNNING)
     {
         return(childStatus);
     }
     if (this.m_activeChildIndex == -1)
     {
         BehaviorTask behaviorTask = this.m_children.get_Item(0);
         EBTStatus    eBTStatus    = behaviorTask.exec(pAgent);
         if (eBTStatus == EBTStatus.BT_SUCCESS)
         {
             this.m_activeChildIndex = 1;
         }
         else if (eBTStatus == EBTStatus.BT_FAILURE)
         {
             this.m_activeChildIndex = 2;
         }
     }
     if (this.m_activeChildIndex != -1)
     {
         BehaviorTask behaviorTask2 = this.m_children.get_Item(this.m_activeChildIndex);
         return(behaviorTask2.exec(pAgent));
     }
     return(EBTStatus.BT_RUNNING);
 }
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                bool      flag = true;
                EBTStatus eBTStatus;

                while (true)
                {
                    eBTStatus = childStatus;
                    if (!flag || eBTStatus == EBTStatus.BT_RUNNING)
                    {
                        int          index        = this.m_set.get_Item(this.m_activeChildIndex);
                        BehaviorTask behaviorTask = this.m_children[index];
                        eBTStatus = behaviorTask.exec(pAgent);
                    }
                    flag = false;
                    if (eBTStatus != EBTStatus.BT_FAILURE)
                    {
                        break;
                    }
                    this.m_activeChildIndex++;
                    if (this.m_activeChildIndex >= this.m_children.Count)
                    {
                        return(EBTStatus.BT_FAILURE);
                    }
                }
                return(eBTStatus);
            }
Пример #5
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    return(childStatus);
                }
                if (this.m_activeChildIndex != -1)
                {
                    BehaviorTask behaviorTask = this.m_children[this.m_activeChildIndex];
                    return(behaviorTask.exec(pAgent));
                }
                int   num  = (int)FrameRandom.Random((uint)((ushort)this.m_totalSum));
                float num2 = 0f;

                for (int i = 0; i < this.m_children.Count; i++)
                {
                    int num3 = this.m_weightingMap.get_Item(i);
                    num2 += (float)num3;
                    if (num3 > 0 && num2 >= (float)num)
                    {
                        BehaviorTask behaviorTask2 = this.m_children[i];
                        EBTStatus    eBTStatus     = behaviorTask2.exec(pAgent);
                        if (eBTStatus == EBTStatus.BT_RUNNING)
                        {
                            this.m_activeChildIndex = i;
                        }
                        else
                        {
                            this.m_activeChildIndex = -1;
                        }
                        return(eBTStatus);
                    }
                }
                return(EBTStatus.BT_FAILURE);
            }
Пример #6
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            Debug.Check(this.m_node is DecoratorLoop);
            //DecoratorLoop node = this.m_node as DecoratorLoop;
            Debug.Check(this.m_children.Count == 1);
            BehaviorTask c = this.m_children[0];

            //EBTStatus s = c.exec(pAgent);
            c.exec(pAgent);

            if (this.m_n > 0)
            {
                this.m_n--;

                if (this.m_n == 0)
                {
                    return(EBTStatus.BT_SUCCESS);
                }

                return(EBTStatus.BT_RUNNING);
            }

            if (this.m_n == -1)
            {
                return(EBTStatus.BT_RUNNING);
            }

            Debug.Check(this.m_n == 0);

            return(EBTStatus.BT_SUCCESS);
        }
Пример #7
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            EBTStatus status = childStatus;

            if (childStatus == EBTStatus.BT_RUNNING)
            {
                Debug.Check(this.GetNode() is Task, "node is not an Method");
                Task pTaskNode = (Task)(this.GetNode());

                if (pTaskNode.IsHTN)
                {
#if BEHAVIAC_USE_HTN
                    status = _planner.Update();
#endif//
                }
                else
                {
                    Debug.Check(this.m_children.Count == 1);
                    BehaviorTask c = this.m_children[0];
                    status = c.exec(pAgent);
                }
            }
            else
            {
                Debug.Check(true);
            }

            return(status);
        }
Пример #8
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                bool bFirst = true;

                Debug.Check(this.m_activeChildIndex < this.m_children.Count);

                // Keep going until a child behavior says its running.
                for (; ;)
                {
                    EBTStatus s = childStatus;
                    if (!bFirst || s == EBTStatus.BT_RUNNING)
                    {
                        int          childIndex = this.m_set[this.m_activeChildIndex];
                        BehaviorTask pBehavior  = this.m_children[childIndex];
                        s = pBehavior.exec(pAgent);
                    }

                    bFirst = false;

                    // If the child succeeds, or keeps running, do the same.
                    if (s != EBTStatus.BT_FAILURE)
                    {
                        return(s);
                    }

                    // Hit the end of the array, job done!
                    ++this.m_activeChildIndex;
                    if (this.m_activeChildIndex >= this.m_children.Count)
                    {
                        return(EBTStatus.BT_FAILURE);
                    }
                }
            }
Пример #9
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            Debug.Check(this.m_node is ReferencedBehavior);
            ReferencedBehavior pNode = this.m_node as ReferencedBehavior;

            Debug.Check(pNode != null);

            EBTStatus status = EBTStatus.BT_RUNNING;

            if (pNode.RootTaskNode == null)
            {
                status = this.m_subTree.exec(pAgent);
            }
            else
            {
#if !BEHAVIAC_RELEASE
                if (!_logged)
                {
                    pAgent.LogJumpTree(pNode.ReferencedTree);
                    _logged = true;
                }
#endif
                Debug.Check(this.m_children.Count == 1);
                BehaviorTask c = this.m_children[0];

                status = c.exec(pAgent);
            }

            return(status);
        }
Пример #10
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    return(childStatus);
                }
                if (base.m_activeChildIndex == -1)
                {
                    BehaviorTask task = base.m_children[0];
                    switch (task.exec(pAgent))
                    {
                    case EBTStatus.BT_SUCCESS:
                        base.m_activeChildIndex = 1;
                        break;

                    case EBTStatus.BT_FAILURE:
                        base.m_activeChildIndex = 2;
                        break;
                    }
                }
                if (base.m_activeChildIndex != -1)
                {
                    BehaviorTask task2 = base.m_children[base.m_activeChildIndex];
                    return(task2.exec(pAgent));
                }
                return(EBTStatus.BT_RUNNING);
            }
Пример #11
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.GetNode() is SelectorProbability);
                SelectorProbability pSelectorProbabilityNode = (SelectorProbability)(this.GetNode());

                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    return(childStatus);
                }

                //check if we've already chosen a node to run
                if (this.m_activeChildIndex != CompositeTask.InvalidChildIndex)
                {
                    BehaviorTask pNode = this.m_children[this.m_activeChildIndex];

                    EBTStatus status = pNode.exec(pAgent);

                    return(status);
                }

                Debug.Check(this.m_weightingMap.Count == this.m_children.Count);

                //generate a number between 0 and the sum of the weights
                float chosen = this.m_totalSum * CompositeStochastic.CompositeStochasticTask.GetRandomValue(pSelectorProbabilityNode.m_method, pAgent);

                float sum = 0;

                for (int i = 0; i < this.m_children.Count; ++i)
                {
                    int w = this.m_weightingMap[i];

                    sum += w;

                    if (w > 0 && sum >= chosen)                     //execute this node
                    {
                        BehaviorTask pChild = this.m_children[i];

                        EBTStatus status = pChild.exec(pAgent);

                        if (status == EBTStatus.BT_RUNNING)
                        {
                            this.m_activeChildIndex = i;
                        }
                        else
                        {
                            this.m_activeChildIndex = CompositeTask.InvalidChildIndex;
                        }

                        return(status);
                    }
                }

                return(EBTStatus.BT_FAILURE);
            }
Пример #12
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            Debug.Check(this.m_node is Method);
            //Method pNode = this.m_node as Method;

            Debug.Check(this.m_children.Count == 1);
            BehaviorTask c = this.m_children[0];

            EBTStatus s = c.exec(pAgent);

            return(s);
        }
Пример #13
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            Debug.Check(this.m_node is DecoratorIterator);
            //DecoratorIterator pNode = this.m_node as DecoratorIterator;

            Debug.Check(this.m_children.Count == 1);
            BehaviorTask c = this.m_children[0];

            EBTStatus s = c.exec(pAgent);

            return(s);
        }
Пример #14
0
 protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
 {
     for (int i = 0; i < this.m_children.Count; i++)
     {
         BehaviorTask behaviorTask = this.m_children[i];
         EBTStatus    eBTStatus    = behaviorTask.exec(pAgent);
         if (eBTStatus == EBTStatus.BT_FAILURE)
         {
             return(eBTStatus);
         }
     }
     return(EBTStatus.BT_SUCCESS);
 }
Пример #15
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(childStatus != EBTStatus.BT_INVALID);
                Debug.Check(this.m_children.Count == 3);

                EBTStatus conditionResult = EBTStatus.BT_INVALID;

                if (childStatus == EBTStatus.BT_SUCCESS || childStatus == EBTStatus.BT_FAILURE)
                {
                    // if the condition returned running then ended with childStatus
                    conditionResult = childStatus;
                }

                if (this.m_activeChildIndex == CompositeTask.InvalidChildIndex)
                {
                    BehaviorTask pCondition = this.m_children[0];

                    if (conditionResult == EBTStatus.BT_INVALID)
                    {
                        // condition has not been checked
                        conditionResult = pCondition.exec(pAgent);
                    }

                    if (conditionResult == EBTStatus.BT_SUCCESS)
                    {
                        // if
                        this.m_activeChildIndex = 1;
                    }
                    else if (conditionResult == EBTStatus.BT_FAILURE)
                    {
                        // else
                        this.m_activeChildIndex = 2;
                    }
                }
                else
                {
                    return(childStatus);
                }

                if (this.m_activeChildIndex != CompositeTask.InvalidChildIndex)
                {
                    BehaviorTask pBehavior = this.m_children[this.m_activeChildIndex];
                    EBTStatus    s         = pBehavior.exec(pAgent);

                    return(s);
                }

                return(EBTStatus.BT_RUNNING);
            }
Пример #16
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.m_children.Count == 3);

                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    Debug.Check(this.m_activeChildIndex != CompositeTask.InvalidChildIndex);

                    return(childStatus);
                }

                if (this.m_activeChildIndex == CompositeTask.InvalidChildIndex)
                {
                    BehaviorTask pCondition = this.m_children[0];

                    EBTStatus conditionResult = pCondition.exec(pAgent);

                    //Debug.Check (conditionResult == EBTStatus.BT_SUCCESS || conditionResult == EBTStatus.BT_FAILURE,
                    //	"conditionResult should be either EBTStatus.BT_SUCCESS of EBTStatus.BT_FAILURE");

                    if (conditionResult == EBTStatus.BT_SUCCESS)
                    {
                        //BehaviorTask pIf = this.m_children[1];

                        this.m_activeChildIndex = 1;
                    }
                    else if (conditionResult == EBTStatus.BT_FAILURE)
                    {
                        //BehaviorTask pElse = this.m_children[2];

                        this.m_activeChildIndex = 2;
                    }
                }

                if (this.m_activeChildIndex != CompositeTask.InvalidChildIndex)
                {
                    BehaviorTask pBehavior = this.m_children[this.m_activeChildIndex];
                    EBTStatus    s         = pBehavior.exec(pAgent);

                    return(s);
                }

                return(EBTStatus.BT_RUNNING);
            }
Пример #17
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                //Debug.Check(this.m_children.Count == 2);
                for (int i = 0; i < this.m_children.Count; ++i)
                {
                    BehaviorTask pBehavior = this.m_children[i];
                    EBTStatus    s         = pBehavior.exec(pAgent);

                    // If the child succeeds, succeeds
                    if (s == EBTStatus.BT_SUCCESS)
                    {
                        return(s);
                    }

                    Debug.Check(s == EBTStatus.BT_FAILURE);
                }

                return(EBTStatus.BT_FAILURE);
            }
Пример #18
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            Debug.Check(this.GetNode() is Task, "node is not an Method");
            Task pTaskNode = (Task)(this.GetNode());

            if (pTaskNode.IsHTN)
            {
                EBTStatus status = _planner.Update();

                return(status);
            }
            else
            {
                Debug.Check(this.m_children.Count == 1);
                BehaviorTask c      = this.m_children[0];
                EBTStatus    status = c.exec(pAgent);

                return(status);
            }
        }
Пример #19
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.m_activeChildIndex < this.m_children.Count);

                SequenceStochastic node = this.m_node as SequenceStochastic;

                EBTStatus s = childStatus;

                // Keep going until a child behavior says its running.
                for (; ;)
                {
                    if (s == EBTStatus.BT_RUNNING)
                    {
                        int          childIndex = this.m_set[this.m_activeChildIndex];
                        BehaviorTask pBehavior  = this.m_children[childIndex];

                        if (node.CheckIfInterrupted(pAgent))
                        {
                            return(EBTStatus.BT_FAILURE);
                        }

                        s = pBehavior.exec(pAgent);
                    }

                    // If the child fails, or keeps running, do the same.
                    if (s != EBTStatus.BT_SUCCESS)
                    {
                        return(s);
                    }

                    // Hit the end of the array, job done!
                    ++this.m_activeChildIndex;

                    if (this.m_activeChildIndex >= this.m_children.Count)
                    {
                        return(EBTStatus.BT_SUCCESS);
                    }

                    s = childStatus;
                }
            }
Пример #20
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.m_activeChildIndex < this.m_children.Count);

                bool bFirst = true;

                // Keep going until a child behavior says its running.
                for (; ;)
                {
                    EBTStatus s = childStatus;
                    if (!bFirst || s == EBTStatus.BT_RUNNING)
                    {
                        Debug.Check(this.m_status == EBTStatus.BT_INVALID ||
                                    this.m_status == EBTStatus.BT_RUNNING);

                        BehaviorTask pBehavior = this.m_children[this.m_activeChildIndex];
                        s = pBehavior.exec(pAgent);
                    }

                    bFirst = false;

                    // If the child fails, or keeps running, do the same.
                    if (s != EBTStatus.BT_SUCCESS)
                    {
                        return(s);
                    }

                    // Hit the end of the array, job done!
                    ++this.m_activeChildIndex;
                    if (this.m_activeChildIndex >= this.m_children.Count)
                    {
                        return(EBTStatus.BT_SUCCESS);
                    }

                    if (!this.CheckPredicates(pAgent))
                    {
                        return(EBTStatus.BT_FAILURE);
                    }
                }
            }
Пример #21
0
            private EBTStatus UpdateFSM(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.m_node != null);
                Debug.Check(this.m_currentNodeId != -1);

                EBTStatus status = childStatus;
                bool      bLoop  = true;

                while (bLoop)
                {
                    BehaviorTask currentState = this.GetChildById(this.m_currentNodeId);
                    currentState.exec(pAgent);

                    if (currentState is State.StateTask)
                    {
                        State.StateTask pStateTask = (State.StateTask)currentState;

                        if (pStateTask.IsEndState)
                        {
                            return(EBTStatus.BT_SUCCESS);
                        }
                    }

                    int nextStateId = currentState.GetNextStateId();

                    if (nextStateId == -1)
                    {
                        //if not transitioned, don't go on next state, to exit
                        bLoop = false;
                    }
                    else
                    {
                        //if transitioned, go on next state
                        this.m_currentNodeId = nextStateId;
                    }
                }

                return(status);
            }
Пример #22
0
        protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
        {
            BehaviorTask pParent = this.GetParent();

            Debug.Check(pParent is SelectorLoop.SelectorLoopTask);

            if (this.m_bIsUpdatePrecondition)
            {
                BehaviorTask precond = this.PreconditionNode;
                EBTStatus    s       = precond.exec(pAgent, childStatus);

                return(s);
            }
            else
            {
                BehaviorTask action = this.ActionNode;
                EBTStatus    s      = action.exec(pAgent, childStatus);

                return(s);
            }

            //return EBTStatus.BT_RUNNING;
        }
Пример #23
0
        public EBTStatus SequenceUpdate(Agent pAgent, EBTStatus childStatus, ref int activeChildIndex, List <BehaviorTask> children)
        {
            EBTStatus s = childStatus;

            for (; ;)
            {
                Debug.Check(activeChildIndex < children.Count);

                if (s == EBTStatus.BT_RUNNING)
                {
                    BehaviorTask pBehavior = children[activeChildIndex];

                    if (this.CheckIfInterrupted(pAgent))
                    {
                        return(EBTStatus.BT_FAILURE);
                    }

                    s = pBehavior.exec(pAgent);
                }

                // If the child fails, or keeps running, do the same.
                if (s != EBTStatus.BT_SUCCESS)
                {
                    return(s);
                }

                // Hit the end of the array, job done!
                ++activeChildIndex;

                if (activeChildIndex >= children.Count)
                {
                    return(EBTStatus.BT_SUCCESS);
                }

                s = EBTStatus.BT_RUNNING;
            }
        }
Пример #24
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Parallel node  = (Parallel)base.GetNode();
                bool     flag  = false;
                bool     flag2 = false;
                bool     flag3 = false;
                bool     flag4 = true;
                bool     flag5 = true;
                bool     flag6 = node.m_childFinishPolicy == CHILDFINISH_POLICY.CHILDFINISH_LOOP;

                for (int i = 0; i < base.m_children.Count; i++)
                {
                    BehaviorTask task   = base.m_children[i];
                    EBTStatus    status = task.GetStatus();
                    if ((flag6 || (status == EBTStatus.BT_RUNNING)) || (status == EBTStatus.BT_INVALID))
                    {
                        EBTStatus status2 = task.exec(pAgent);
                        switch (status2)
                        {
                        case EBTStatus.BT_FAILURE:
                        {
                            flag2 = true;
                            flag5 = false;
                            continue;
                        }

                        case EBTStatus.BT_SUCCESS:
                        {
                            flag  = true;
                            flag4 = false;
                            continue;
                        }
                        }
                        if (status2 == EBTStatus.BT_RUNNING)
                        {
                            flag3 = true;
                            flag4 = false;
                            flag5 = false;
                        }
                    }
                    else if (status == EBTStatus.BT_SUCCESS)
                    {
                        flag  = true;
                        flag4 = false;
                    }
                    else
                    {
                        flag2 = true;
                        flag5 = false;
                    }
                }
                EBTStatus status3 = !flag3 ? EBTStatus.BT_FAILURE : EBTStatus.BT_RUNNING;

                if (((node.m_failPolicy == FAILURE_POLICY.FAIL_ON_ALL) && flag4) || ((node.m_failPolicy == FAILURE_POLICY.FAIL_ON_ONE) && flag2))
                {
                    status3 = EBTStatus.BT_FAILURE;
                }
                else if (((node.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ALL) && flag5) || ((node.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ONE) && flag))
                {
                    status3 = EBTStatus.BT_SUCCESS;
                }
                if ((node.m_exitPolicy == EXIT_POLICY.EXIT_ABORT_RUNNINGSIBLINGS) && ((status3 == EBTStatus.BT_FAILURE) || (status3 == EBTStatus.BT_SUCCESS)))
                {
                    for (int j = 0; j < base.m_children.Count; j++)
                    {
                        BehaviorTask task2 = base.m_children[j];
                        if (task2.GetStatus() == EBTStatus.BT_RUNNING)
                        {
                            task2.abort(pAgent);
                        }
                    }
                }
                return(status3);
            }
Пример #25
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                int idx = -1;

                if (childStatus != EBTStatus.BT_RUNNING)
                {
                    Debug.Check(this.m_activeChildIndex != CompositeTask.InvalidChildIndex);

                    if (childStatus == EBTStatus.BT_SUCCESS)
                    {
                        return(EBTStatus.BT_SUCCESS);
                    }
                    else if (childStatus == EBTStatus.BT_FAILURE)
                    {
                        //the next for starts from (idx + 1), so that it starts from next one after this failed one
                        idx = this.m_activeChildIndex;
                    }
                    else
                    {
                        Debug.Check(false);
                    }
                }

                //checking the preconditions and take the first action tree
                int index = (int)-1;

                for (int i = (idx + 1); i < this.m_children.Count; ++i)
                {
                    Debug.Check(this.m_children[i] is WithPreconditionTask);
                    WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[i];

                    BehaviorTask pre = pSubTree.PreconditionNode;

                    EBTStatus status = pre.exec(pAgent);

                    if (status == EBTStatus.BT_SUCCESS)
                    {
                        index = i;
                        break;
                    }
                }

                //clean up the current ticking action tree
                if (index != (int)-1)
                {
                    if (this.m_activeChildIndex != CompositeTask.InvalidChildIndex &&
                        this.m_activeChildIndex != index)
                    {
                        WithPreconditionTask pCurrentSubTree = (WithPreconditionTask)this.m_children[this.m_activeChildIndex];
                        BehaviorTask         action          = pCurrentSubTree.ActionNode;
                        action.abort(pAgent);

                        //don't set it here
                        //this.m_activeChildIndex = index;
                    }

                    for (int i = index; i < this.m_children.Count; ++i)
                    {
                        WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[i];

                        if (i > index)
                        {
                            BehaviorTask pre    = pSubTree.PreconditionNode;
                            EBTStatus    status = pre.exec(pAgent);

                            //to search for the first one whose precondition is success
                            if (status != EBTStatus.BT_SUCCESS)
                            {
                                continue;
                            }
                        }

                        BehaviorTask action = pSubTree.ActionNode;
                        EBTStatus    s      = action.exec(pAgent);

                        if (s == EBTStatus.BT_RUNNING)
                        {
                            this.m_activeChildIndex = i;
                        }
                        else
                        {
                            if (s == EBTStatus.BT_FAILURE)
                            {
                                //THE ACTION failed, to try the next one
                                continue;
                            }
                        }

                        Debug.Check(s == EBTStatus.BT_RUNNING || s == EBTStatus.BT_SUCCESS);

                        return(s);
                    }
                }

                return(EBTStatus.BT_FAILURE);
            }
Пример #26
0
            private EBTStatus UpdateFSM(Agent pAgent, EBTStatus childStatus)
            {
                Debug.Check(this.m_node != null);
                Debug.Check(this.m_currentNodeId != -1);

#if !BEHAVIAC_RELEASE
                int kMaxCount = 10;
                Dictionary <int, int> state_update_count = new Dictionary <int, int>();
#endif//#if !BEHAVIAC_RELEASE

                EBTStatus status = childStatus;
                bool      bLoop  = true;

                while (bLoop)
                {
                    BehaviorTask currentState = this.GetChildById(this.m_currentNodeId);
                    currentState.exec(pAgent);

                    if (currentState is State.StateTask)
                    {
                        State.StateTask pStateTask = (State.StateTask)currentState;

                        if (pStateTask.IsEndState)
                        {
                            return(EBTStatus.BT_SUCCESS);
                        }
                    }

                    int nextStateId = currentState.GetNextStateId();

                    if (nextStateId == -1)
                    {
                        //if not transitioned, don't go on next state, to exit
                        bLoop = false;
                    }
                    else
                    {
#if !BEHAVIAC_RELEASE
                        if (state_update_count.ContainsKey(this.m_currentNodeId))
                        {
                            state_update_count[this.m_currentNodeId]++;
                        }
                        else
                        {
                            state_update_count.Add(this.m_currentNodeId, 1);
                        }

                        if (state_update_count[this.m_currentNodeId] > kMaxCount)
                        {
                            string treeName = BehaviorTask.GetParentTreeName(pAgent, this.GetNode());
                            Debug.LogError(string.Format("{0} might be updating an FSM('{1}') endlessly, possibly a dead loop, please redesign it!\n", pAgent.GetName(), treeName));
                            Debug.Check(false);
                        }
#endif

                        //if transitioned, go on next state
                        this.m_currentNodeId = nextStateId;
                    }
                }

                return(status);
            }
Пример #27
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                int idx = -1;

                for (int i = 0; i < this.m_children.Count; ++i)
                {
                    WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[i];
                    Debug.Check(pSubTree is WithPreconditionTask);

                    EBTStatus returnStatus = pSubTree.GetReturnStatus();
                    if (returnStatus != EBTStatus.BT_INVALID)
                    {
                        pSubTree.SetReturnStatus(EBTStatus.BT_INVALID);

                        if (returnStatus == EBTStatus.BT_SUCCESS)
                        {
                            return(EBTStatus.BT_SUCCESS);
                        }
                        else if (returnStatus == EBTStatus.BT_FAILURE)
                        {
                            idx = i;
                            break;
                        }
                    }
                }

                //checking the preconditions and take the first action tree
                int index = (int)-1;

                for (int i = (idx + 1); i < this.m_children.Count; ++i)
                {
                    WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[i];
                    Debug.Check(pSubTree is WithPreconditionTask);

                    BehaviorTask pPrecondTree = pSubTree.PreconditionNode();

                    EBTStatus status = pPrecondTree.exec(pAgent);

                    if (status == EBTStatus.BT_SUCCESS)
                    {
                        index = i;
                        break;
                    }
                }

                //clean up the current ticking action tree
                if (index != (int)-1)
                {
                    if (this.m_activeChildIndex != CompositeTask.InvalidChildIndex && this.m_activeChildIndex != index)
                    {
                        WithPreconditionTask pCurrentSubTree = (WithPreconditionTask)this.m_children[this.m_activeChildIndex];
                        Debug.Check(pCurrentSubTree is WithPreconditionTask);
                        BehaviorTask pCurrentActionTree = pCurrentSubTree.Action();

                        WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[index];
                        Debug.Check(pSubTree is WithPreconditionTask);

                        BehaviorTask pActionTree = pSubTree.Action();

                        Debug.Check(pCurrentActionTree != pActionTree);

                        pCurrentActionTree.abort(pAgent);

                        pCurrentSubTree.abort(pAgent);

                        this.m_activeChildIndex = index;
                    }

                    for (int i = index; i < this.m_children.Count; ++i)
                    {
                        WithPreconditionTask pSubTree = (WithPreconditionTask)this.m_children[i];
                        Debug.Check(pSubTree is WithPreconditionTask);

                        if (i > index)
                        {
                            BehaviorTask pPreconditionTree = pSubTree.PreconditionNode();

                            EBTStatus status = pPreconditionTree.exec(pAgent);

                            //to search for the first one whose precondition is success
                            if (status != EBTStatus.BT_SUCCESS)
                            {
                                continue;
                            }
                        }

                        BehaviorTask pActionTree = pSubTree.Action();

                        EBTStatus s = pActionTree.exec(pAgent);

                        if (s == EBTStatus.BT_RUNNING)
                        {
                            this.m_activeChildIndex = index;
                        }
                        else
                        {
                            pActionTree.reset(pAgent);

                            if (s == EBTStatus.BT_FAILURE || s == EBTStatus.BT_INVALID)
                            {
                                //THE ACTION failed, to try the next one
                                continue;
                            }
                        }

                        Debug.Check(s == EBTStatus.BT_RUNNING || s == EBTStatus.BT_SUCCESS);

                        return(s);
                    }
                }

                return(EBTStatus.BT_FAILURE);
            }
Пример #28
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                int num = -1;

                for (int i = 0; i < this.m_children.get_Count(); i++)
                {
                    WithPreconditionTask withPreconditionTask = (WithPreconditionTask)this.m_children.get_Item(i);
                    BehaviorTask         behaviorTask         = withPreconditionTask.PreconditionNode();
                    EBTStatus            eBTStatus            = behaviorTask.exec(pAgent);
                    if (eBTStatus == EBTStatus.BT_SUCCESS)
                    {
                        num = i;
                        break;
                    }
                }
                if (num != -1)
                {
                    if (this.m_activeChildIndex != -1)
                    {
                        WithPreconditionTask withPreconditionTask2 = (WithPreconditionTask)this.m_children.get_Item(this.m_activeChildIndex);
                        BehaviorTask         behaviorTask2         = withPreconditionTask2.Action();
                        WithPreconditionTask withPreconditionTask3 = (WithPreconditionTask)this.m_children.get_Item(num);
                        BehaviorTask         behaviorTask3         = withPreconditionTask3.Action();
                        if (behaviorTask2 != behaviorTask3)
                        {
                            behaviorTask2.abort(pAgent);
                            withPreconditionTask2.abort(pAgent);
                            this.m_activeChildIndex = num;
                        }
                    }
                    for (int j = 0; j < this.m_children.get_Count(); j++)
                    {
                        WithPreconditionTask withPreconditionTask4 = (WithPreconditionTask)this.m_children.get_Item(j);
                        EBTStatus            eBTStatus2            = withPreconditionTask4.exec(pAgent);
                        if (j >= num)
                        {
                            if (j > num)
                            {
                                BehaviorTask behaviorTask4 = withPreconditionTask4.PreconditionNode();
                                EBTStatus    eBTStatus3    = behaviorTask4.exec(pAgent);
                                if (eBTStatus3 != EBTStatus.BT_SUCCESS)
                                {
                                    goto IL_158;
                                }
                            }
                            BehaviorTask behaviorTask5 = withPreconditionTask4.Action();
                            EBTStatus    eBTStatus4    = behaviorTask5.exec(pAgent);
                            if (eBTStatus4 == EBTStatus.BT_RUNNING)
                            {
                                this.m_activeChildIndex = num;
                            }
                            else if (eBTStatus4 == EBTStatus.BT_FAILURE || eBTStatus4 == EBTStatus.BT_INVALID)
                            {
                                goto IL_158;
                            }
                            return(eBTStatus4);
                        }
                        IL_158 :;
                    }
                }
                return(EBTStatus.BT_FAILURE);
            }
Пример #29
0
        public EBTStatus ParallelUpdate(Agent pAgent, List <BehaviorTask> children)
        {
            bool sawSuccess    = false;
            bool sawFail       = false;
            bool sawRunning    = false;
            bool sawAllFails   = true;
            bool sawAllSuccess = true;

            bool bLoop = (this.m_childFinishPolicy == CHILDFINISH_POLICY.CHILDFINISH_LOOP);

            // go through all m_children
            for (int i = 0; i < children.Count; ++i)
            {
                BehaviorTask pChild = children[i];

                EBTStatus treeStatus = pChild.GetStatus();

                if (bLoop || (treeStatus == EBTStatus.BT_RUNNING || treeStatus == EBTStatus.BT_INVALID))
                {
                    EBTStatus status = pChild.exec(pAgent);

                    if (status == EBTStatus.BT_FAILURE)
                    {
                        sawFail       = true;
                        sawAllSuccess = false;
                    }
                    else if (status == EBTStatus.BT_SUCCESS)
                    {
                        sawSuccess  = true;
                        sawAllFails = false;
                    }
                    else if (status == EBTStatus.BT_RUNNING)
                    {
                        sawRunning    = true;
                        sawAllFails   = false;
                        sawAllSuccess = false;
                    }
                }
                else if (treeStatus == EBTStatus.BT_SUCCESS)
                {
                    sawSuccess  = true;
                    sawAllFails = false;
                }
                else
                {
                    Debug.Check(treeStatus == EBTStatus.BT_FAILURE);

                    sawFail       = true;
                    sawAllSuccess = false;
                }
            }

            EBTStatus result = sawRunning ? EBTStatus.BT_RUNNING : EBTStatus.BT_FAILURE;

            if ((this.m_failPolicy == FAILURE_POLICY.FAIL_ON_ALL && sawAllFails) ||
                (this.m_failPolicy == FAILURE_POLICY.FAIL_ON_ONE && sawFail))
            {
                result = EBTStatus.BT_FAILURE;
            }
            else if ((this.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ALL && sawAllSuccess) ||
                     (this.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ONE && sawSuccess))
            {
                result = EBTStatus.BT_SUCCESS;
            }

            //else if (m_failPolicy == FAIL_ON_ALL && m_succeedPolicy == SUCCEED_ON_ALL && sawRunning)
            //{
            //  return EBTStatus.BT_RUNNING;
            //}

            if (this.m_exitPolicy == EXIT_POLICY.EXIT_ABORT_RUNNINGSIBLINGS && (result == EBTStatus.BT_FAILURE || result == EBTStatus.BT_SUCCESS))
            {
                for (int i = 0; i < children.Count; ++i)
                {
                    BehaviorTask pChild = children[i];
                    //Debug.Check(BehaviorTreeTask.DynamicCast(pChild));
                    EBTStatus treeStatus = pChild.GetStatus();

                    if (treeStatus == EBTStatus.BT_RUNNING)
                    {
                        pChild.abort(pAgent);
                    }
                }
            }

            return(result);
        }
Пример #30
0
            protected override EBTStatus update(Agent pAgent, EBTStatus childStatus)
            {
                Parallel parallel = (Parallel)base.GetNode();
                bool     flag     = false;
                bool     flag2    = false;
                bool     flag3    = false;
                bool     flag4    = true;
                bool     flag5    = true;
                bool     flag6    = parallel.m_childFinishPolicy == CHILDFINISH_POLICY.CHILDFINISH_LOOP;

                for (int i = 0; i < this.m_children.get_Count(); i++)
                {
                    BehaviorTask behaviorTask = this.m_children.get_Item(i);
                    EBTStatus    status       = behaviorTask.GetStatus();
                    if (flag6 || status == EBTStatus.BT_RUNNING || status == EBTStatus.BT_INVALID)
                    {
                        EBTStatus eBTStatus = behaviorTask.exec(pAgent);
                        if (eBTStatus == EBTStatus.BT_FAILURE)
                        {
                            flag2 = true;
                            flag5 = false;
                        }
                        else if (eBTStatus == EBTStatus.BT_SUCCESS)
                        {
                            flag  = true;
                            flag4 = false;
                        }
                        else if (eBTStatus == EBTStatus.BT_RUNNING)
                        {
                            flag3 = true;
                            flag4 = false;
                            flag5 = false;
                        }
                    }
                    else if (status == EBTStatus.BT_SUCCESS)
                    {
                        flag  = true;
                        flag4 = false;
                    }
                    else
                    {
                        flag2 = true;
                        flag5 = false;
                    }
                }
                EBTStatus eBTStatus2 = (!flag3) ? EBTStatus.BT_FAILURE : EBTStatus.BT_RUNNING;

                if ((parallel.m_failPolicy == FAILURE_POLICY.FAIL_ON_ALL && flag4) || (parallel.m_failPolicy == FAILURE_POLICY.FAIL_ON_ONE && flag2))
                {
                    eBTStatus2 = EBTStatus.BT_FAILURE;
                }
                else if ((parallel.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ALL && flag5) || (parallel.m_succeedPolicy == SUCCESS_POLICY.SUCCEED_ON_ONE && flag))
                {
                    eBTStatus2 = EBTStatus.BT_SUCCESS;
                }
                if (parallel.m_exitPolicy == EXIT_POLICY.EXIT_ABORT_RUNNINGSIBLINGS && (eBTStatus2 == EBTStatus.BT_FAILURE || eBTStatus2 == EBTStatus.BT_SUCCESS))
                {
                    for (int j = 0; j < this.m_children.get_Count(); j++)
                    {
                        BehaviorTask behaviorTask2 = this.m_children.get_Item(j);
                        EBTStatus    status2       = behaviorTask2.GetStatus();
                        if (status2 == EBTStatus.BT_RUNNING)
                        {
                            behaviorTask2.abort(pAgent);
                        }
                    }
                }
                return(eBTStatus2);
            }