Esempio n. 1
0
        /**
         * Creates a BTExecutor that handles the execution of a behaviour tree. The behaviour tree is
         * represented by a ModelTask (the root of the tree).
         * <p>
         * A new empty context for the tree is created. This context is passed to the root of the tree,
         * and, in general, it will be shared by all the nodes in the tree (it will be passed down the
         * hierarchy of the tree). Note however that, depending on the semantics of the tree itself,
         * some nodes may not use the context context.
         *
         * @param modelBT
         *            the root of the behaviour tree to run.
         */
        public BTExecutor(ModelTask modelBT)
        {
            if (modelBT == null)
            {
                throw new ArgumentException("The input ModelTask cannot be null");
            }

            _modelBT = modelBT;
            _modelBT.ComputePositions();
            _context = new BasicContext();
            _tickableTasks = new List<ExecutionTask>();
            _openTasks = new List<ExecutionTask>();
            _currentOpenInsertions = new List<ExecutionTask>();
            _currentOpenRemovals = new List<ExecutionTask>();
            _currentTickableInsertions = new List<ExecutionTask>();
            _currentTickableRemovals = new List<ExecutionTask>();
            _interrupters = new Dictionary<ModelInterrupter, ExecutionInterrupter>();
            _tasksStates = new Dictionary<Position, ITaskState>();
        }
        /**
         * This method first retrieve from the context the tree (ModelTask) that is
         * going to be emulated by this task. Then, it creates its corresponding
         * executor and finally spawns it. If the tree cannot be found in the
         * context, does nothing.
         *
         * @see jbt.execution.core.ExecutionTask#internalSpawn()
         */
        protected override void InternalSpawn()
        {
            /* Retrieve the tree to run from the context. */
            treeToRun = Context.GetBT(
                ((ModelSubtreeLookup)ModelTask).TreeName);

            if (treeToRun == null)
            {
                treeRetrieved = false;
                /*
             * Must request to be inserted into the list of tickable nodes,
             * since no tree has been retrieved and as a result it must be the
             * task the one continuin the work.
             */
                Executor.RequestInsertionIntoList(BTExecutor.BTExecutorList.Tickable, this);
                //			System.err.println("Could not retrieve tree "
                //					+ ((ModelSubtreeLookup) this.getModelTask()).getTreeName()
                //					+ " from the context. Check if the context has been properly initialized.");
            }
            else
            {
                treeRetrieved = true;
                /* Compute positions for the retrieved tree. */
                treeToRun.ComputePositions();

                executionTree = treeToRun.CreateExecutor(Executor, this);
                executionTree.AddTaskListener(this);
                executionTree.Spawn(Context);
            }
        }