예제 #1
0
파일: Parallel.cs 프로젝트: zoe27/behaviac
            //~ParallelTask()
            //{
            //    this.m_children.Clear();
            //}

            protected override bool onenter(Agent pAgent)
            {
                Debug.Check(this.m_activeChildIndex == CompositeTask.InvalidChildIndex);

                // reset the status cache of the children
                for (int i = 0; i < this.m_children.Count; ++i)
                {
                    BehaviorTask pChild = this.m_children[i];

                    pChild.reset(pAgent);
                }

                return(true);
            }
예제 #2
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);
            }