/** * Constructs a copy of the Position <code>pos</code>. * * @param pos * the Position that is copied. */ public Position(Position pos) { _moves = new List<int>(); foreach (var move in pos._moves) { _moves.Add(move); } }
/** * * @see jbt.execution.core.IBTExecutor#getBehaviourTree() */ /** * Sets the permanent state of a given task. The task is identified by the position it occupies * in the execution behaviour tree, which unambiguously identifies it. * * @param taskPosition * the position of the task whose state must be stored. * @param state * the state of the task, or null if it should be cleared. * @return true if there was a previous state for this task in the BTExecutor, or false * otherwise. */ public bool SetTaskState(Position taskPosition, ITaskState state) { if (state == null) { return _tasksStates.Remove(taskPosition); } if (_tasksStates.ContainsKey(taskPosition)) { _tasksStates.Add(taskPosition, state); return false; } _tasksStates[taskPosition] = state; return true; }
/** * Returns the permanent state of a task. The task is identified by the position it occupies in * the execution behaviour tree, which unambiguously identifies it. * * @param taskPosition * the position of the task whose state must be retrieved. * @return the state of the task, or null if there is no state stored in the BTExecutor for the * task. */ public ITaskState GetTaskState(Position taskPosition) { return _tasksStates.ContainsKey(taskPosition) ? _tasksStates[taskPosition] : null; }
/** * Clears the permanent state of a task. The task is identified by the position it occupies in * the execution behaviour tree, which unambiguously identifies it. * * @param taskPosition * the position of the task whose state must be cleared. * @return true if the BTExecutor contained the state of the task before calling this method, or * false otherwise. */ public bool ClearTaskState(Position taskPosition) { return _tasksStates.Remove(taskPosition); }
/** * Constructs an ExecutionTask with an associated ModelTask and a * BTExecutor. The ModelTask represents the conceptual task that the * ExecutionTask is running, and the BTExecutor is that in charge of the * ExecutionTask. Also, the parent of the ExecutionTask must be provided. * * @param modelTask * the ModelTask this ExecutionTask will run. * @param executor * the BTExecutor managing this task. * @param parent * the parent ExecutionTask, or null in case this is the root of * the tree. */ protected ExecutionTask(ModelTask modelTask, IBTExecutor executor, ExecutionTask parent) { _modelTask = modelTask; _executor = executor as BTExecutor; _listeners = new List<ITaskListener>(); _spawnable = true; _tickable = false; _terminated = false; _status = Status.Uninitialized; _parent = parent; if(_modelTask != null) AlwaysFail = modelTask.AlwaysFail; /* Compute the position of this node. */ if (parent == null) { _position = new Position(); } else { _position = new Position(parent._position); var nextMove = GetMove(); _position.AddMove(nextMove); } }
protected ModelTask(ModelTask guard, string name, params ModelTask[] children) { _guard = guard; _name = name; _children = new List<ModelTask>(); foreach (var modelTask in children) { _children.Add(modelTask); } _position = new Position(); }
/** * This method sets the positions of all tasks below <code>t</code> in the * tree. * * @param t * the task whose descendants will be computed their positions. */ private void RecursiveComputePositions(ModelTask t) { /* * Set the position of all of the children of this task and recursively * compute the position of the rest of the tasks. */ for (var i = 0; i < t._children.Count; i++) { var currentChild = t._children[i]; var currentChildPos = new Position(t.Position); currentChildPos.AddMove(i); currentChild._position = currentChildPos; RecursiveComputePositions(currentChild); } }
/** * This function searches for a ModelTask according to a particular * Position. * <p> * Conceptually, a Position represents a sequence of moves. This method just * applies all moves in <code>moves</code> starting from this ModelTask, and * returns the reached ModelTask, or null in case it does not exist. * * @param moves * the sequence of moves that must be performed to retrieve the * ModelTask. * @return the ModelTask obtained by moving down the tree according to the * sequence of moves <code>moves</code>, or null in case no * ModelTask could be found. */ public ModelTask FindNode(Position moves) { var m = moves.Moves; var currentTask = this; foreach (var currentMove in m) { var children = currentTask.Children; if (currentMove >= children.Count) { return null; } currentTask = children[currentMove]; } return currentTask; }
/** * This method computes the positions of all the tasks of the behaviour tree * whose root is this node. After calling this method, the positions of all * the tasks below this one will be available and accessible through * {@link #getPosition()}. * <p> * It is important to note that, when calling this method, this task is * considered to be the root of the behaviour tree, so its position will be * set to an empty sequence of moves, with no offset, and the positions of * the tasks below it will be computed from it. */ public void ComputePositions() { /* Assume this node is the root of the tree. */ _position = new Position(new List<int>()); /* * Set the position of all of the children of this task and recursively * compute the position of the rest of the tasks. */ for (var i = 0; i < _children.Count; i++) { ModelTask currentChild = _children[i]; var currentChildPos = new Position(_position); currentChildPos.AddMove(i); currentChild._position = currentChildPos; RecursiveComputePositions(currentChild); } }
/** * Adds the moves of a Position to this Position. * * @param position * the position whose moves are going to be added to this * one. * @return this Position. */ public Position AddMoves(Position position) { AddMoves(position.Moves); return this; }