Esempio n. 1
0
    void SimpleForwardPlanner()
    {
        Plan.Clear();  // we clear the plan before we start populating it
        Stack <Task> tasks = new Stack <Task> ();

        tasks.Push(new CaveMonsterCompoundTask());   // HTN ROOT
        while (tasks.Count > 0)
        {
            Task task = tasks.Pop();

            if (task.isCompound())
            {
                // then we can safely cast it

                CompoundTask compoundTask = (CompoundTask)task;
                // not all compound tasks need to find a task
                if (compoundTask.NeedsToFindTask())
                {
                    CompoundTask method = compoundTask.FindTask();
                    // if we found a task
                    if (method != null)
                    {
                        // CODE TO SAVE STATE HERE NOT NEEDED BECAUSE OF PRE CONDITIONS

                        // we push the subtasks into our tasks stack
                        foreach (Task t in method.GetSubtasks())
                        {
                            tasks.Push(t);
                        }
                    }
                    else
                    {
                        // if method == null
                        // CODE TO RESTORE STATE HERE
                    }
                }
                // if don't need to find task, then we just add all subtasks of the compoundtask to tasks
                else
                {
                    foreach (Task t in compoundTask.GetSubtasks())
                    {
                        tasks.Push(t);
                    }
                }
            } // ELSE task is primitive
            else
            {
                // can safely cast
                PrimitiveTask primitiveTask = (PrimitiveTask)task;
                if (primitiveTask.ValidPreconditions())
                {
                    // Also need to Update State but we will do that when we execute the primitive task, not here
                    Plan.Add(primitiveTask);
                }
                else
                {
                    // if preconditions were false
                    // CODE TO RESTORE STATE HERE
                }
            }
        }
        hasPlan = true;
        Plan.Reverse(); // because we built it bottom up
    }