private static void appendFieldString (StringBuilder sb, Task<?> task, TaskAttribute ann, Field field) { // prefer name from annotation if there is one String name = ann.name(); if (name == null || name.length() == 0) name = field.getName(); Object value; try { field.setAccessible(true); value = field.get(task); } catch (ReflectionException e) { Gdx.app.error("", "Failed to get field", e); return; } sb.append(name).append(":"); Class<?> fieldType = field.getType(); if (fieldType.isEnum() || fieldType == String.class) { sb.append('\"').append(value).append('\"'); } else if (Distribution.class.isAssignableFrom(fieldType)) { sb.append('\"').append(DAs.toString((Distribution)value)).append('\"'); } else sb.append(value); }
private static Task<Dog> createDogBehavior () { Selector<Dog> selector = new Selector<Dog>(); Parallel<Dog> parallel = new Parallel<Dog>(); selector.addChild(parallel); CareTask care = new CareTask(); care.urgentProb = 0.8f; parallel.addChild(care); parallel.addChild(new AlwaysFail<Dog>(new RestTask())); Sequence<Dog> sequence = new Sequence<Dog>(); selector.addChild(sequence); BarkTask bark1 = new BarkTask(); bark1.times = new TriangularIntegerDistribution(1, 5, 2); sequence.addChild(bark1); sequence.addChild(new WalkTask()); sequence.addChild(new BarkTask()); sequence.addChild(new MarkTask()); return selector; }
final Task<T> createTask(StructBHTNode node) { Task<T> ret = createTask(node.type, node.key, node.args); if(ret == null) { CCLog.error(TAG, "createTask fail "); } if(node.children != null) { int len = node.children.length; for(int i = 0; i < len; ++i) { Task<T> child = createTask(node.children[i]); ret.addChild(child); } } return ret; }
protected static <E> void printTree (Task<E> task, int indent) { for (int i = 0; i < indent; i++) System.out.print(' '); if (task.getGuard() != null) { System.out.println("Guard"); indent = indent + 2; printTree(task.getGuard(), indent); for (int i = 0; i < indent; i++) System.out.print(' '); } System.out.println(task.getClass().getSimpleName()); for (int i = 0; i < task.getChildCount(); i++) { printTree(task.getChild(i), indent + 2); } }
private void setField (Field field, Task<E> task, Object value) { field.setAccessible(true); Object valueObject = castValue(field, value); try { field.set(task, valueObject); } catch (ReflectionException e) { throw new GdxRuntimeException(e); } }
private StackedTask<E> createStackedTask (String name, Task<E> task) { Metadata metadata = findMetadata(task.getClass()); if (metadata == null) throw new GdxRuntimeException(name + ": @TaskConstraint annotation not found in '" + task.getClass().getSimpleName() + "' class hierarchy"); return new StackedTask<E>(lineNumber, name, task, metadata); }
void initCurrentTree(Task<E> rootTask, int startIndent) { currentDepth = -1; step = 1; currentTreeStartIndent = startIndent; this.currentTree.init(rootTask); prevTask = null; }
@Override public void childSuccess (Task<E> runningTask) { if (count > 0) count--; if (count == 0) { super.childSuccess(runningTask); loop = false; } else loop = true; }
@Override protected Task<E> copyTo (Task<E> task) { Repeat<E> repeat = (Repeat<E>)task; repeat.times = times; // no need to clone since it is immutable return super.copyTo(task); }
@Override protected Task<E> copyTo (Task<E> task) { Random<E> random = (Random<E>)task; random.success = success; // no need to clone since it is immutable return super.copyTo(task); }
/** Returns a clone of the referenced subtree if this {@code Import} is eager; otherwise returns a clone of itself. */ @Override public Task<E> cloneTask () { if (lazy) return super.cloneTask(); // Non lazy include is grafted at clone-time return createSubtreeRootTask(); }
/** Copies this {@code Include} to the given task. A {@link TaskCloneException} is thrown if this {@code Include} is eager. * @param task the task to be filled * @return the given task for chaining * @throws TaskCloneException if this {@code Include} is eager. */ @Override protected Task<E> copyTo (Task<E> task) { if (!lazy) throw new TaskCloneException("A non-lazy " + getClass().getSimpleName() + " should never be copied."); Include<E> include = (Include<E>)task; include.subtree = subtree; include.lazy = lazy; return task; }
@Override protected Task<E> copyTo (Task<E> task) { SemaphoreGuard<E> semaphoreGuard = (SemaphoreGuard<E>)task; semaphoreGuard.name = name; semaphoreGuard.semaphore = null; semaphoreGuard.semaphoreAcquired = false; return super.copyTo(task); }
@Override public void childFail (Task<E> runningTask) { super.childFail(runningTask); if (++currentChildIndex < children.size) { run(); // Run next child } else { fail(); // All children processed, return failure status } }
@Override public void childSuccess (Task<E> runningTask) { super.childSuccess(runningTask); if (++currentChildIndex < children.size) { run(); // Run next child } else { success(); // All children processed, return success status } }
@Override public void run () { noRunningTasks = true; lastResult = null; for (currentChildIndex = 0; currentChildIndex < children.size; currentChildIndex++) { Task<E> child = children.get(currentChildIndex); if (child.getStatus() == Status.RUNNING) { child.run(); } else { child.setControl(this); child.start(); if (child.checkGuard(this)) child.run(); else child.fail(); } if (lastResult != null) { // Current child has finished either with success or fail cancelRunningChildren(noRunningTasks ? currentChildIndex + 1 : 0); if (lastResult) success(); else fail(); return; } } running(); }
@Override protected Task<E> copyTo (Task<E> task) { Parallel<E> parallel = (Parallel<E>)task; parallel.policy = policy; // no need to clone since it is immutable return super.copyTo(task); }
@Override public void run () { // Check guards Task<E> childToRun = null; for (int i = 0, n = children.size; i < n; i++) { Task<E> child = children.get(i); if (child.checkGuard(this)) { childToRun = child; break; } } if (runningChild != null && runningChild != childToRun) { runningChild.cancel(); runningChild = null; } if (childToRun == null) { fail(); } else { if (runningChild == null) { runningChild = childToRun; runningChild.setControl(this); runningChild.start(); } runningChild.run(); } }
@Override protected Task<E> copyTo (Task<E> task) { DynamicGuardSelector<E> branch = (DynamicGuardSelector<E>)task; branch.runningChild = null; return super.copyTo(task); }
@Override public Status execute () { DogCharacter dog = getObject(); updateAnimation(dog); if (getStatus() == Task.Status.RUNNING && !dog.isSteering()) { return Status.SUCCEEDED; } return Status.RUNNING; }
@Override protected Task<Dog> copyTo (Task<Dog> task) { BarkTask bark = (BarkTask)task; bark.times = times; return task; }
@Override protected Task<Dog> copyTo (Task<Dog> task) { CareTask care = (CareTask)task; care.urgentProb = urgentProb; return task; }
public TaskNode (Task<?> task, BehaviorTreeViewer<?> btViewer, int step, Skin skin) { super(new View(task, skin)); ((View)getActor()).taskNode = this; this.task = task; this.btViewer = btViewer; this.step = step; updateStatus(null, step); }
private void updateStatus (Task.Status previousStatus, int step) { this.step = step; Task.Status status = task.getStatus(); if (status != previousStatus) { View view = (View)getActor(); view.status.setText(status == Task.Status.FRESH ? "" : status.name()); } }
public View (Task<?> task, Skin skin) { super(skin); this.name = new Label(task.getClass().getSimpleName(), skin); this.status = new Label("", skin); add(name); add(status).padLeft(10); StringBuilder attrs = new StringBuilder(task.getClass().getSimpleName()).append('\n'); addListener(new TextTooltip(appendTaskAttributes(attrs, task).toString(), skin)); }
private static StringBuilder appendTaskAttributes (StringBuilder attrs, Task<?> task) { Class<?> aClass = task.getClass(); Field[] fields = ClassReflection.getFields(aClass); for (Field f : fields) { Annotation a = f.getDeclaredAnnotation(TaskAttribute.class); if (a == null) continue; TaskAttribute annotation = a.getAnnotation(TaskAttribute.class); attrs.append('\n'); appendFieldString(attrs, task, annotation, f); } return attrs; }
private int addToTree (Tree displayTree, TaskNode parentNode, Task<E> task, IntArray taskSteps, int taskStepIndex) { TaskNode node = new TaskNode(task, this, taskSteps == null ? step - 1 : taskSteps.get(taskStepIndex), getSkin()); taskNodes.put(task, node); if (parentNode == null) { displayTree.add(node); } else { parentNode.add(node); } taskStepIndex++; for (int i = 0; i < task.getChildCount(); i++) { Task<E> child = task.getChild(i); taskStepIndex = addToTree(displayTree, node, child, taskSteps, taskStepIndex); } return taskStepIndex; }
private void setField (Field field, Task<E> task, Object value) { field.setAccessible(true); Object valueObject = castValue(field, value); if (valueObject == null) throwAttributeTypeException(getCurrentTask().name, field.getName(), field.getType().getSimpleName()); try { field.set(task, valueObject); } catch (ReflectionException e) { throw new GdxRuntimeException(e); } }
/** Creates a parallel task with the given orchestrator, policy and children * @param policy the policy * @param orchestrator the orchestrator * @param tasks the children */ public Parallel (Policy policy, Orchestrator orchestrator, Array<Task<E>> tasks) { super(tasks); this.policy = policy; this.orchestrator = orchestrator; noRunningTasks = true; }