Ejemplo n.º 1
0
        public EBTStatus resume(Agent pAgent, EBTStatus status)
        {
            BranchTask   prev = null;
            BehaviorTask p    = this.m_currentTask;

            while (p != null)
            {
                BranchTask branch = p as BranchTask;
                if (branch != null)
                {
                    prev = branch;
                    p    = branch.GetCurrentTask();
                }
                else
                {
                    prev = p.GetParent();
                    break;
                }
            }

            if (prev != null)
            {
                //prev is the last interrupted node, to find and let its parent update with 'status' which is the subtree's return status
                Debug.Check(status == EBTStatus.BT_SUCCESS || status == EBTStatus.BT_FAILURE);
                prev.onexit_action(pAgent, status);

                prev.SetReturnStatus(status);
            }

            return(status);
        }
Ejemplo n.º 2
0
        private BranchTask GetTopManageBranchTask()
        {
            BranchTask   tree = null;
            BehaviorTask task = this.m_parent;

            while (task != null)
            {
                if (task is BehaviorTreeTask)
                {
                    //to overwrite the child branch
                    tree = (BranchTask)task;
                    break;
                }
                else if (task.m_node.IsManagingChildrenAsSubTrees())
                {
                    //until it is Parallel/SelectorLoop, it's child is used as tree to store current task
                    break;
                }
                else if (task is BranchTask)
                {
                    //this if must be after BehaviorTreeTask and IsManagingChildrenAsSubTrees
                    tree = (BranchTask)task;
                }
                else
                {
                    Debug.Check(false);
                }

                task = task.m_parent;
            }

            return(tree);
        }
Ejemplo n.º 3
0
        protected EBTStatus resume_branch(Agent pAgent, EBTStatus status)
        {
            Debug.Check(this.m_currentTask != null);
            Debug.Check(status == EBTStatus.BT_SUCCESS || status == EBTStatus.BT_FAILURE);

            BranchTask parent = null;

            if (this.m_currentTask.GetNode().IsManagingChildrenAsSubTrees())
            {
                parent = (BranchTask)this.m_currentTask;
            }
            else
            {
                parent = this.m_currentTask.GetParent();
            }

            //clear it as it ends and the next exec might need to set it
            this.m_currentTask = null;

            if (parent != null)
            {
                EBTStatus s = parent.exec(pAgent, status);

                return(s);
            }

            return(EBTStatus.BT_INVALID);
        }
Ejemplo n.º 4
0
        private bool oneventCurrentNode(Agent pAgent, string eventName)
        {
            Debug.Check(this.m_currentTask != null);

            EBTStatus s = this.m_currentTask.GetStatus();

            Debug.Check(s == EBTStatus.BT_RUNNING && this.m_node.HasEvents());

            bool bGoOn = this.m_currentTask.onevent(pAgent, eventName);

            //give the handling back to parents
            if (bGoOn && this.m_currentTask != null)
            {
                BranchTask parentBranch = this.m_currentTask.GetParent();

                //back track the parents until the branch
                while (parentBranch != null && parentBranch != this)
                {
                    Debug.Check(parentBranch.GetStatus() == EBTStatus.BT_RUNNING);

                    bGoOn = parentBranch.onevent(pAgent, eventName);

                    if (!bGoOn)
                    {
                        return(false);
                    }

                    parentBranch = parentBranch.GetParent();
                }
            }

            return(bGoOn);
        }
Ejemplo n.º 5
0
        protected EBTStatus tickCurrentNode(Agent pAgent)
        {
            EBTStatus status = this.m_currentTask.GetStatus();

            if ((status != EBTStatus.BT_INVALID) && (status != EBTStatus.BT_RUNNING))
            {
                return(status);
            }
            BehaviorTask currentTask = this.m_currentTask;
            EBTStatus    childStatus = this.m_currentTask.exec(pAgent);

            if (childStatus != EBTStatus.BT_RUNNING)
            {
                BranchTask parent = currentTask.GetParent();
                this.SetCurrentTask(null);
                while ((parent != null) && (parent != this))
                {
                    childStatus = parent.update(pAgent, childStatus);
                    if (childStatus == EBTStatus.BT_RUNNING)
                    {
                        return(EBTStatus.BT_RUNNING);
                    }
                    parent.onexit_action(pAgent, childStatus);
                    parent.m_status = childStatus;
                    parent          = parent.GetParent();
                }
            }
            return(childStatus);
        }
Ejemplo n.º 6
0
        public EBTStatus exec(Agent pAgent)
        {
            bool flag;

            if (this.m_status == EBTStatus.BT_RUNNING)
            {
                flag = true;
            }
            else
            {
                this.m_status = EBTStatus.BT_INVALID;
                flag          = this.onenter_action(pAgent);
                bool flag2 = this.isContinueTicking();
                if (flag2)
                {
                    BranchTask parentBranch = this.GetParentBranch();
                    if (parentBranch != null && parentBranch != this)
                    {
                        parentBranch.SetCurrentTask(this);
                    }
                }
            }
            if (flag)
            {
                EBTStatus returnStatus = this.GetReturnStatus();
                if (returnStatus == EBTStatus.BT_INVALID)
                {
                    this.m_status = this.update(pAgent, EBTStatus.BT_RUNNING);
                }
                else
                {
                    this.m_status = returnStatus;
                }
                if (this.m_status != EBTStatus.BT_RUNNING)
                {
                    bool flag3 = this.isContinueTicking();
                    if (flag3)
                    {
                        BranchTask parentBranch2 = this.GetParentBranch();
                        if (parentBranch2 != null && parentBranch2 != this)
                        {
                            parentBranch2.SetCurrentTask(null);
                        }
                    }
                    this.onexit_action(pAgent, this.m_status);
                }
            }
            else
            {
                this.m_status = EBTStatus.BT_FAILURE;
            }
            EBTStatus status = this.m_status;

            if (this.m_status != EBTStatus.BT_RUNNING && this.NeedRestart())
            {
                this.m_status = EBTStatus.BT_INVALID;
                this.SetReturnStatus(EBTStatus.BT_INVALID);
            }
            return(status);
        }
Ejemplo n.º 7
0
 protected BehaviorTask()
 {
     this.m_status      = EBTStatus.BT_INVALID;
     this.m_node        = null;
     this.m_attachments = null;
     this.m_parent      = null;
 }
Ejemplo n.º 8
0
        private EBTStatus execCurrentTask(Agent pAgent)
        {
            Debug.Check(this.m_currentTask != null && this.m_currentTask.GetStatus() == EBTStatus.BT_RUNNING);

            //this.m_currentTask could be cleared in ::tick, to remember it
            EBTStatus status = this.m_currentTask.exec(pAgent);

            //give the handling back to parents
            if (status != EBTStatus.BT_RUNNING)
            {
                Debug.Check(status == EBTStatus.BT_SUCCESS || status == EBTStatus.BT_FAILURE);
                Debug.Check(this.m_currentTask.m_status == status);

                BranchTask parentBranch = this.m_currentTask.GetParent();

                this.m_currentTask = null;

                //back track the parents until the branch
                while (parentBranch != null && parentBranch != this)
                {
                    status = parentBranch.exec(pAgent, status);

                    if (status == EBTStatus.BT_RUNNING)
                    {
                        return(EBTStatus.BT_RUNNING);
                    }

                    Debug.Check(parentBranch.m_status == status);

                    parentBranch = parentBranch.GetParent();
                }
            }

            return(status);
        }
Ejemplo n.º 9
0
        public EBTStatus resume(Agent pAgent, EBTStatus status)
        {
            BranchTask   parent      = null;
            BehaviorTask currentTask = base.m_currentTask;

            while (currentTask != null)
            {
                BranchTask task3 = currentTask as BranchTask;
                if (task3 != null)
                {
                    parent      = task3;
                    currentTask = task3.GetCurrentTask();
                }
                else
                {
                    parent = currentTask.GetParent();
                    break;
                }
            }
            if (parent != null)
            {
                parent.onexit_action(pAgent, status);
                parent.SetReturnStatus(status);
            }
            return(status);
        }
Ejemplo n.º 10
0
 public virtual void Clear()
 {
     this.m_status = EBTStatus.BT_INVALID;
     this.m_parent = null;
     this.m_id     = -1;
     this.FreeAttachments();
     this.m_node = null;
 }
Ejemplo n.º 11
0
        private bool CheckParentUpdatePreconditions(Agent pAgent)
        {
            bool bValid = true;

            if (this.m_bHasManagingParent)
            {
                bool           bHasManagingParent = false;
                const int      kMaxParentsCount   = 512;
                int            parentsCount       = 0;
                BehaviorTask[] parents            = new BehaviorTask[kMaxParentsCount];

                BranchTask parentBranch = this.GetParent();

                parents[parentsCount++] = this;

                //back track the parents until the managing branch
                while (parentBranch != null)
                {
                    Debug.Check(parentsCount < kMaxParentsCount, "weird tree!");

                    parents[parentsCount++] = parentBranch;

                    if (parentBranch.GetCurrentTask() == this)
                    {
                        //Debug.Check(parentBranch->GetNode()->IsManagingChildrenAsSubTrees());

                        bHasManagingParent = true;
                        break;
                    }

                    parentBranch = parentBranch.GetParent();
                }

                if (bHasManagingParent)
                {
                    for (int i = parentsCount - 1; i >= 0; --i)
                    {
                        BehaviorTask pb = parents[i];

                        bValid = pb.CheckPreconditions(pAgent, true);

                        if (!bValid)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                bValid = this.CheckPreconditions(pAgent, true);
            }

            return(bValid);
        }
Ejemplo n.º 12
0
 public BranchTask GetParentBranch()
 {
     for (BehaviorTask task = this.m_parent; task != null; task = task.m_parent)
     {
         BranchTask task2 = task as BranchTask;
         if ((task2 != null) && task2.isContinueTicking())
         {
             return(task2);
         }
     }
     return(null);
 }
Ejemplo n.º 13
0
 public BranchTask GetParentBranch()
 {
     for (BehaviorTask parent = this.m_parent; parent != null; parent = parent.m_parent)
     {
         BranchTask branchTask = parent as BranchTask;
         if (branchTask != null && branchTask.isContinueTicking())
         {
             return(branchTask);
         }
     }
     return(null);
 }
Ejemplo n.º 14
0
        protected EBTStatus tickCurrentNode(Agent pAgent)
        {
            Debug.Check(this.m_currentTask != null);

            EBTStatus s = this.m_currentTask.GetStatus();

            if (s == EBTStatus.BT_INVALID || s == EBTStatus.BT_RUNNING)
            {
                //this.m_currentTask could be cleared in ::tick, to remember it
                BehaviorTask currentTask = this.m_currentTask;
                EBTStatus    status      = this.m_currentTask.exec(pAgent);

                //give the handling back to parents
                if (status != EBTStatus.BT_RUNNING)
                {
                    Debug.Check(currentTask.m_status == EBTStatus.BT_SUCCESS ||
                                currentTask.m_status == EBTStatus.BT_FAILURE ||
                                (currentTask.m_status == EBTStatus.BT_INVALID && currentTask.NeedRestart()));

                    BranchTask parentBranch = currentTask.GetParent();

                    this.SetCurrentTask(null);

                    //back track the parents until the branch
                    while (parentBranch != null && parentBranch != this)
                    {
                        status = parentBranch.update(pAgent, status);
                        if (status == EBTStatus.BT_RUNNING)
                        {
                            return(EBTStatus.BT_RUNNING);
                        }

                        parentBranch.onexit_action(pAgent, status);

                        parentBranch.m_status = status;

                        Debug.Check(parentBranch.m_currentTask == null);

                        parentBranch = parentBranch.GetParent();
                    }
                }

                return(status);
            }

            return(s);
        }
Ejemplo n.º 15
0
        /**
         * a branch is a node whose isContinueTicking returns true
         *
         * BehaviorTreeTask, DecoratorTask, ParallelTask, SelectorLoopTask, etc.
         */
        public BranchTask GetParentBranch()
        {
            BehaviorTask pTopNode = this.m_parent;

            while (pTopNode != null)
            {
                BranchTask pBranch = pTopNode as BranchTask;
                if (pBranch != null && pBranch.isContinueTicking())
                {
                    return(pBranch);
                }

                pTopNode = pTopNode.m_parent;
            }

            return(null);
        }
Ejemplo n.º 16
0
        private bool oneventCurrentNode(Agent pAgent, string eventName)
        {
            EBTStatus status = this.m_currentTask.GetStatus();
            bool      flag   = this.m_currentTask.onevent(pAgent, eventName);

            if (flag)
            {
                for (BranchTask task = this.m_currentTask.GetParent(); (task != null) && (task != this); task = task.GetParent())
                {
                    flag = task.onevent(pAgent, eventName);
                    if (!flag)
                    {
                        return(false);
                    }
                }
            }
            return(flag);
        }
Ejemplo n.º 17
0
        private bool oneventCurrentNode(Agent pAgent, string eventName)
        {
            EBTStatus status = this.m_currentTask.GetStatus();
            bool      flag   = this.m_currentTask.onevent(pAgent, eventName);

            if (flag)
            {
                BranchTask parent = this.m_currentTask.GetParent();
                while (parent != null && parent != this)
                {
                    flag = parent.onevent(pAgent, eventName);
                    if (!flag)
                    {
                        return(false);
                    }
                    parent = parent.GetParent();
                }
            }
            return(flag);
        }
Ejemplo n.º 18
0
        public EBTStatus resume(Agent pAgent, EBTStatus status)
        {
            BranchTask branchTask = null;
            BranchTask branchTask2;

            for (BehaviorTask currentTask = this.m_currentTask; currentTask != null; currentTask = branchTask2.GetCurrentTask())
            {
                branchTask2 = (currentTask as BranchTask);
                if (branchTask2 == null)
                {
                    branchTask = currentTask.GetParent();
                    break;
                }
                branchTask = branchTask2;
            }
            if (branchTask != null)
            {
                branchTask.onexit_action(pAgent, status);
                branchTask.SetReturnStatus(status);
            }
            return(status);
        }
Ejemplo n.º 19
0
 public void SetParent(BranchTask parent)
 {
     this.m_parent = parent;
 }
        public virtual void Clear()
        {
            this.m_status = EBTStatus.BT_INVALID;
            this.m_parent = null;

            if (this.m_attachments != null)
            {
                this.m_attachments.Clear();
                this.m_attachments = null;
            }

            this.m_node = null;
        }
Ejemplo n.º 21
0
        public EBTStatus exec(Agent pAgent, EBTStatus childStatus)
        {
#if !BEHAVIAC_RELEASE
            Debug.Check(this.m_node == null || this.m_node.IsValid(pAgent, this),
                        string.Format("Agent In BT:{0} while the Agent used for: {1}", this.m_node.GetAgentType(), pAgent.GetClassTypeName()));
#endif//#if !BEHAVIAC_RELEASE
            bool bEnterResult = false;

            if (this.m_status == EBTStatus.BT_RUNNING)
            {
                bEnterResult = true;
            }
            else
            {
                //reset it to invalid when it was success/failure
                this.m_status = EBTStatus.BT_INVALID;
                bEnterResult  = this.onenter_action(pAgent);
            }

            if (bEnterResult)
            {
#if !BEHAVIAC_RELEASE
                if (Config.IsLoggingOrSocketing)
                {
                    string btStr = BehaviorTask.GetTickInfo(pAgent, this, "update");

                    //empty btStr is for internal BehaviorTreeTask
                    if (!string.IsNullOrEmpty(btStr))
                    {
                        LogManager.Instance.Log(pAgent, btStr, EActionResult.EAR_none, LogMode.ELM_tick);
                    }
                }
#endif
                bool bValid = this.CheckParentUpdatePreconditions(pAgent);

                if (bValid)
                {
                    this.m_status = this.update_current(pAgent, childStatus);
                }
                else
                {
                    this.m_status = EBTStatus.BT_FAILURE;

                    if (this.GetCurrentTask() != null)
                    {
                        this.update_current(pAgent, EBTStatus.BT_FAILURE);
                    }
                }

                if (this.m_status != EBTStatus.BT_RUNNING)
                {
                    //clear it

                    this.onexit_action(pAgent, this.m_status);

                    //this node is possibly ticked by its parent or by the topBranch who records it as currrent node
                    //so, we can't here reset the topBranch's current node
                }
                else
                {
                    BranchTask tree = this.GetTopManageBranchTask();

                    if (tree != null)
                    {
                        tree.SetCurrentTask(this);
                    }
                }
            }
            else
            {
                this.m_status = EBTStatus.BT_FAILURE;
            }

            return(this.m_status);
        }
Ejemplo n.º 22
0
 public void SetParent(BranchTask parent)
 {
     this.m_parent = parent;
 }
Ejemplo n.º 23
0
        public virtual void Clear()
        {
            this.m_status = EBTStatus.BT_INVALID;
            this.m_parent = null;
            this.m_id = -1;

            this.m_node = null;
        }
Ejemplo n.º 24
0
 protected BehaviorTask()
 {
     m_status = EBTStatus.BT_INVALID;
     m_node = null;
     m_parent = null;
     m_bHasManagingParent = false;
 }
Ejemplo n.º 25
0
        public EBTStatus exec(Agent pAgent)
        {
#if !BEHAVIAC_RELEASE
            Debug.Check(this.m_node == null || this.m_node.IsValid(pAgent, this),
                        string.Format("Agent In BT:{0} while the Agent used for: {1}", this.m_node.GetAgentType(), pAgent.GetClassTypeName()));
#endif//#if !BEHAVIAC_RELEASE
            bool bEnterResult = false;
            if (this.m_status == EBTStatus.BT_RUNNING)
            {
                bEnterResult = true;
            }
            else
            {
                //reset it to invalid when it was success/failure
                this.m_status = EBTStatus.BT_INVALID;

                bEnterResult = this.onenter_action(pAgent);

                //for continue ticking task, to set it as the cached current task
                bool bIsContinueTicking = this.isContinueTicking();
                if (bIsContinueTicking)
                {
                    BranchTask pBranch = this.GetParentBranch();

                    if (pBranch != null && pBranch != this)
                    {
                        //if 'this' is a tree, don't set it into it parent's current node
                        Debug.Check(!(this is BehaviorTreeTask));

                        pBranch.SetCurrentTask(this);
                    }
                }
            }

            if (bEnterResult)
            {
#if !BEHAVIAC_RELEASE
                if (Config.IsLoggingOrSocketing)
                {
                    string btStr = BehaviorTask.GetTickInfo(pAgent, this, "update");
                    //empty btStr is for internal BehaviorTreeTask
                    if (!string.IsNullOrEmpty(btStr))
                    {
                        LogManager.Log(pAgent, btStr, EActionResult.EAR_none, LogMode.ELM_tick);
                    }
                }
#endif
                EBTStatus returnStatus = this.GetReturnStatus();
                if (returnStatus == EBTStatus.BT_INVALID)
                {
                    this.m_status = this.update(pAgent, EBTStatus.BT_RUNNING);
                }
                else
                {
                    this.m_status = returnStatus;
                }

                if (this.m_status != EBTStatus.BT_RUNNING)
                {
                    //clear it
                    bool bIsContinueTicking = this.isContinueTicking();
                    if (bIsContinueTicking)
                    {
                        BranchTask pBranch = this.GetParentBranch();

                        if (pBranch != null && pBranch != this)
                        {
                            //if 'this' is a tree, don't set it into it parent's current node
                            Debug.Check(!(this is BehaviorTreeTask));

                            pBranch.SetCurrentTask(null);
                        }
                    }


                    this.onexit_action(pAgent, this.m_status);

                    //this node is possibly ticked by its parent or by the topBranch who records it as currrent node
                    //so, we can't here reset the topBranch's current node
                }
            }
            else
            {
                this.m_status = EBTStatus.BT_FAILURE;
            }

            EBTStatus currentStatus = this.m_status;
            if (this.m_status != EBTStatus.BT_RUNNING && this.NeedRestart())
            {
                //reset it to invalid when it needs restarting
                //don't need to reset the sub tree
                this.m_status = EBTStatus.BT_INVALID;
                this.SetReturnStatus(EBTStatus.BT_INVALID);
            }

            return(currentStatus);
        }
Ejemplo n.º 26
0
 protected BehaviorTask()
 {
     m_status = EBTStatus.BT_INVALID;
     m_node = null;
     m_attachments = null;
     m_parent = null;
 }