Esempio n. 1
0
        private void doAutoPlanning()
        {
            if (!this.AutoReplan)
            {
                return;
            }

            this.timeTillReplan -= Workspace.Instance.DeltaTime;

            var noPlan = this.m_rootTask == null || this.m_rootTask.GetStatus() != EBTStatus.BT_RUNNING;

            //if (noPlan || timeTillReplan <= 0)
            if (noPlan)
            {
                timeTillReplan += AutoReplanInterval;

                PlannerTask newPlan = this.GeneratePlan();

                if (newPlan != null)
                {
                    if (this.m_rootTask != null)
                    {
                        if (this.m_rootTask.GetStatus() == EBTStatus.BT_RUNNING)
                        {
                            this.m_rootTask.abort(this.agent);
                            raisePlanAborted(this.m_rootTask);
                        }

                        BehaviorTask.DestroyTask(this.m_rootTask);
                    }

                    this.m_rootTask = newPlan;
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Generate a new task for the <paramref name="agent"/> based on the current world state as
        /// described by the <paramref name="agentState"/>.
        /// </summary>
        /// <param name="agent">
        /// The agent for which the task is being generated. This object instance must be of the
        /// same type as the type for which the Task was developed
        /// </param>
        /// <param name="agentState">The current world state required by the planner</param>
        /// <returns></returns>
        private PlannerTask GeneratePlan()
        {
            // If the planner is currently executing a task marked NotInterruptable, do not generate
            // any new plans.
            if (!canInterruptCurrentPlan())
            {
                return(null);
            }

            try
            {
                PlannerTask newPlan = this.BuildPlan(this.m_rootTaskNode);

                if (newPlan == null)
                {
                    return(null);
                }

                if (!newPlan.IsHigherPriority(this.m_rootTask))
                {
                    return(null);
                }

                return(newPlan);
            }

            finally
            {
            }
        }
Esempio n. 3
0
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            Sequence sequence   = (Sequence)node;
            bool     bOk        = false;
            int      childCount = sequence.GetChildrenCount();
            int      i          = 0;

            for (; i < childCount; ++i)
            {
                BehaviorNode childNode = sequence.GetChild(i);
                PlannerTask  childTask = planner.decomposeNode(childNode, depth);

                if (childTask == null)
                {
                    break;
                }

                //clear the log cache so that the next node can log all properites
                LogManager.Instance.PLanningClearCache();
                seqTask.AddChild(childTask);
            }

            if (i == childCount)
            {
                bOk = true;
            }

            return(bOk);
        }
Esempio n. 4
0
        private PlannerTask BuildPlan(Task root)
        {
            LogManager.Instance.PLanningClearCache();

            int depth = this.agent.Variables.Depth;

            PlannerTask rootTask = null;

            using (var currentState = this.agent.Variables.Push(true))
            {
                this.agent.PlanningTop = this.agent.Variables.Top;
                Debug.Check(this.agent.PlanningTop >= 0);

                LogPlanBegin(this.agent, root);

                rootTask = this.decomposeNode(root, 0);

                LogPlanEnd(this.agent, root);

#if !BEHAVIAC_RELEASE
                BehaviorTask.CHECK_BREAKPOINT(this.agent, root, "plan", EActionResult.EAR_all);
#endif

                this.agent.PlanningTop = -1;
            }

            Debug.Check(this.agent.Variables.Depth == depth);

            return(rootTask);
        }
Esempio n. 5
0
 private void raisePlanGenerated(PlannerTask task)
 {
     if (this.PlanGenerated != null)
     {
         this.PlanGenerated(this, task);
     }
 }
Esempio n. 6
0
        private void doAutoPlanning()
        {
            if (!this.AutoReplan)
            {
                return;
            }

            var noPlan = this.m_rootTask == null || this.m_rootTask.GetStatus() != EBTStatus.BT_RUNNING;

            if (noPlan)
            {
                PlannerTask newPlan = this.GeneratePlan();

                if (newPlan != null)
                {
                    if (this.m_rootTask != null)
                    {
                        if (this.m_rootTask.GetStatus() == EBTStatus.BT_RUNNING)
                        {
                            this.m_rootTask.abort(this.agent);
                        }

                        BehaviorTask.DestroyTask(this.m_rootTask);
                    }

                    this.m_rootTask = newPlan;
                }
            }
        }
Esempio n. 7
0
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            Parallel parallel = (Parallel)node;
            bool     bOk      = false;
            //parallel currently is the same with sequence
            int childCount = parallel.GetChildrenCount();
            int i          = 0;

            for (; i < childCount; ++i)
            {
                BehaviorNode childNode = parallel.GetChild(i);
                PlannerTask  childTask = planner.decomposeNode(childNode, depth);

                if (childTask == null)
                {
                    break;
                }

                seqTask.AddChild(childTask);
            }

            if (i == childCount)
            {
                bOk = true;
            }

            return(bOk);
        }
Esempio n. 8
0
 private void raiseTaskSucceeded(PlannerTask task)
 {
     if (task != null && this.TaskSucceeded != null)
     {
         this.TaskSucceeded(this, task);
     }
 }
Esempio n. 9
0
        public static PlannerTask Create(BehaviorNode node, Agent pAgent)
        {
            if (ms_factory == null)
            {
                ms_factory = new Dictionary <Type, TaskCreator>();
                Register <Action>((n, a) => new PlannerTaskAction(n, a));
                Register <Task>((n, a) => new PlannerTaskTask(n, a));
                Register <Method>((n, a) => new PlannerTaskMethod(n, a));
                Register <Sequence>((n, a) => new PlannerTaskSequence(n, a));
                Register <Selector>((n, a) => new PlannerTaskSelector(n, a));
                Register <Parallel>((n, a) => new PlannerTaskParallel(n, a));
                Register <ReferencedBehavior>((n, a) => new PlannerTaskReference(n, a));
                Register <DecoratorLoop>((n, a) => new PlannerTaskLoop(n, a));
                Register <DecoratorIterator>((n, a) => new PlannerTaskIterator(n, a));
            }

            Type type = node.GetType();

            while (!ms_factory.ContainsKey(type))
            {
                type = type.BaseType;
                Debug.Check(type != null);
            }

            if (ms_factory.ContainsKey(type))
            {
                TaskCreator c = ms_factory[type];

                PlannerTask task = c(node, pAgent);

                return(task);
            }

            return(null);
        }
Esempio n. 10
0
        public void AddChild(PlannerTask task)
        {
            if (this.m_children == null)
            {
                this.m_children = new List <BehaviorTask>();
            }

            this.m_children.Add(task);
            task.Parent = this;
        }
Esempio n. 11
0
        private void raisePlanFailed(PlannerTask task)
        {
            if (this.TaskFailed != null)
            {
                this.TaskFailed(this, task);
            }

            if (this.PlanFailed != null)
            {
                this.PlanFailed(this, task);
            }
        }
Esempio n. 12
0
        private void OnDisable()
        {
            if (this.m_rootTask != null)
            {
                if (this.m_rootTask.GetStatus() == EBTStatus.BT_RUNNING)
                {
                    this.m_rootTask.abort(this.agent);
                    BehaviorTask.DestroyTask(this.m_rootTask);
                }

                this.m_rootTask = null;
            }
        }
Esempio n. 13
0
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            bool        bOk       = false;
            Task        task      = (Task)node;
            PlannerTask childTask = planner.decomposeTask((Task)task, depth);

            if (childTask != null)
            {
                seqTask.AddChild(childTask);
                bOk = true;
            }

            return(bOk);
        }
Esempio n. 14
0
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            ReferencedBehavior taskSubTree = (ReferencedBehavior)node;
            bool bOk = false;

            Debug.Check(taskSubTree != null);
            int depth2 = planner.GetAgent().Variables.Depth;

            using (AgentState currentState = planner.GetAgent().Variables.Push(false))
            {
                Agent pAgent = planner.GetAgent();

                string           szTreePath  = taskSubTree.GetReferencedTree(pAgent);
                BehaviorTreeTask subTreeTask = Workspace.Instance.CreateBehaviorTreeTask(szTreePath);

                taskSubTree.SetTaskParams(pAgent, subTreeTask);

                Task task = taskSubTree.RootTaskNode(planner.GetAgent());

                if (task != null)
                {
                    planner.LogPlanReferenceTreeEnter(planner.GetAgent(), taskSubTree);
                    //task.Parent.InstantiatePars(this.LocalVars);

                    BehaviorTreeTask oldCurrentTreeTask = pAgent.ExcutingTreeTask;
                    pAgent.ExcutingTreeTask = subTreeTask;
                    PlannerTask childTask = planner.decomposeNode(task, depth);
                    pAgent.ExcutingTreeTask = oldCurrentTreeTask;

                    if (childTask != null)
                    {
                        //taskSubTree.SetTaskParams(planner.GetAgent(), childTask);
                        PlannerTaskReference subTreeRef = (PlannerTaskReference)seqTask;

                        subTreeRef.SubTreeTask = subTreeTask;
                        seqTask.AddChild(childTask);
                        bOk = true;
                    }

                    //task.Parent.UnInstantiatePars(this.LocalVars);
                    planner.LogPlanReferenceTreeExit(planner.GetAgent(), taskSubTree);
                    Debug.Check(true);
                }
            }

            Debug.Check(planner.GetAgent().Variables.Depth == depth2);
            return(bOk);
        }
Esempio n. 15
0
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            DecoratorIterator pForEach = (DecoratorIterator)node;
            bool bOk        = false;
            int  childCount = pForEach.GetChildrenCount();

            Debug.Check(childCount == 1);
            BehaviorNode childNode = pForEach.GetChild(0);

            bool bGoOn = true;
            int  count = 0;
            int  index = 0;

            while (bGoOn)
            {
                int depth2 = planner.GetAgent().Variables.Depth;
                using (AgentState currentState = planner.GetAgent().Variables.Push(false))
                {
                    bGoOn = pForEach.IterateIt(planner.GetAgent(), index, ref count);

                    if (bGoOn)
                    {
                        planner.LogPlanForEachBegin(planner.GetAgent(), pForEach, index, count);
                        PlannerTask childTask = planner.decomposeNode(childNode, depth);
                        planner.LogPlanForEachEnd(planner.GetAgent(), pForEach, index, count, childTask != null ? "success" : "failure");

                        if (childTask != null)
                        {
                            Debug.Check(seqTask is PlannerTaskIterator);
                            PlannerTaskIterator pForEachTask = seqTask as PlannerTaskIterator;
                            pForEachTask.Index = index;

                            seqTask.AddChild(childTask);
                            bOk = true;
                            break;
                        }

                        index++;
                    }
                }

                Debug.Check(planner.GetAgent().Variables.Depth == depth2);
            }

            return(bOk);
        }
Esempio n. 16
0
        public override bool decompose(BehaviorNode branch, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            bool bOk        = false;
            int  childCount = branch.GetChildrenCount();

            Debug.Check(childCount == 1);
            BehaviorNode childNode = branch.GetChild(0);
            PlannerTask  childTask = planner.decomposeNode(childNode, depth);

            if (childTask != null)
            {
                seqTask.AddChild(childTask);
                bOk = true;
            }

            return(bOk);
        }
Esempio n. 17
0
        //~Selector()
        //{
        //}

#if BEHAVIAC_USE_HTN
        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            Selector sel = (Selector)node;

            bool bOk        = false;
            int  childCount = sel.GetChildrenCount();
            int  i          = 0;

            for (; i < childCount; ++i)
            {
                BehaviorNode childNode = sel.GetChild(i);
                PlannerTask  childTask = planner.decomposeNode(childNode, depth);

                if (childTask != null)
                {
                    seqTask.AddChild(childTask);
                    bOk = true;
                    break;
                }
            }

            return(bOk);
        }
Esempio n. 18
0
        //~ReferencedBehavior()
        //{
        //}

        public override bool decompose(BehaviorNode node, PlannerTaskComplex seqTask, int depth, Planner planner)
        {
            ReferencedBehavior taskSubTree = (ReferencedBehavior)node;
            bool bOk = false;

            Debug.Check(taskSubTree != null);
            int depth2 = planner.GetAgent().Variables.Depth;

            using (AgentState currentState = planner.GetAgent().Variables.Push(false))
            {
                //planner.agent.Variables.Log(planner.agent, true);
                taskSubTree.SetTaskParams(planner.GetAgent());

                Task task = taskSubTree.RootTaskNode(planner.GetAgent());

                if (task != null)
                {
                    planner.LogPlanReferenceTreeEnter(planner.GetAgent(), taskSubTree);
                    task.Parent.InstantiatePars(planner.GetAgent());

                    PlannerTask childTask = planner.decomposeNode(task, depth);

                    if (childTask != null)
                    {
                        seqTask.AddChild(childTask);
                        bOk = true;
                    }

                    task.Parent.UnInstantiatePars(planner.GetAgent());
                    planner.LogPlanReferenceTreeExit(planner.GetAgent(), taskSubTree);
                    Debug.Check(true);
                }
            }

            Debug.Check(planner.GetAgent().Variables.Depth == depth2);
            return(bOk);
        }
Esempio n. 19
0
        //private PlannerTask decomposeTask(Task task, int depth) {
        //because called form other node , so change the private to public
        public PlannerTask decomposeTask(Task task, int depth)
        {
            var methodsCount = task.GetChildrenCount();

            if (methodsCount == 0)
            {
                return(null);
            }

            int         depth1     = this.agent.Variables.Depth;
            PlannerTask methodTask = null;

            for (int i = 0; i < methodsCount; i++)
            {
                BehaviorNode method = task.GetChild(i);
                Debug.Check(method is Method);
                int depth2 = this.agent.Variables.Depth;
                using (var currentState = this.agent.Variables.Push(false))
                {
                    LogPlanMethodBegin(this.agent, method);
                    methodTask = this.decomposeNode(method, depth + 1);
                    LogPlanMethodEnd(this.agent, method, methodTask != null ? "success" : "failure");

                    if (methodTask != null)
                    {
                        // succeeded
                        break;
                    }
                }

                Debug.Check(this.agent.Variables.Depth == depth2);
            }

            Debug.Check(this.agent.Variables.Depth == depth1);
            return(methodTask);
        }
Esempio n. 20
0
 public bool IsHigherPriority(PlannerTask other)
 {
     return(true);
 }
Esempio n. 21
0
        public void RemoveChild(PlannerTask childTask)
        {
            Debug.Check(this.m_children.Count > 0 && this.m_children[this.m_children.Count - 1] == childTask);

            this.m_children.Remove(childTask);
        }
Esempio n. 22
0
        public PlannerTask decomposeNode(BehaviorNode node, int depth)
        {
            try
            {
                // Ensure that the planner does not get stuck in an infinite loop
                if (depth >= 256)
                {
                    Debug.LogError("Exceeded task nesting depth. Does the graph contain an invalid cycle?");
                    return(null);
                }

                LogPlanNodeBegin(this.agent, node);

                int         depth1    = this.agent.Variables.Depth;
                PlannerTask taskAdded = null;

                bool isPreconditionOk = node.CheckPreconditions(this.agent, false);

                if (isPreconditionOk)
                {
                    bool bOk = true;
                    taskAdded = PlannerTask.Create(node, this.agent);

                    if (node is Action)
                    {
                        //nothing to do for action
                        Debug.Check(true);
                    }
                    else
                    {
                        Debug.Check(taskAdded is PlannerTaskComplex);
                        PlannerTaskComplex seqTask = taskAdded as PlannerTaskComplex;

                        bOk = this.decomposeComplex(node, seqTask, depth);
                    }

                    if (bOk)
                    {
                        node.ApplyEffects(this.agent, Effector.EPhase.E_SUCCESS);
                    }
                    else
                    {
                        BehaviorTask.DestroyTask(taskAdded);
                        taskAdded = null;
                    }
                }
                else
                {
                    //precondition failed
                    LogPlanNodePreconditionFailed(this.agent, node);
                }

                LogPlanNodeEnd(this.agent, node, taskAdded != null ? "success" : "failure");

                Debug.Check(this.agent.Variables.Depth == depth1);

                return(taskAdded);
            }
            catch (Exception ex)
            {
                Debug.Check(false, ex.Message);
            }

            return(null);
        }