private Predator createPredator (boolean parallel) { Predator predator = new Predator(parallel ? badlogicTextureRegion : greenFishTextureRegion, this); predator.setPosition(MathUtils.random(stage.getWidth()), MathUtils.random(stage.getHeight()), Align.center); predator.setMaxLinearSpeed(100); predator.setMaxLinearAcceleration(600); final Pursue<Vector2> pursueSB = new Pursue<Vector2>(predator, null, .5f); predator.setSteeringBehavior(pursueSB); testTable.addActor(predator); BranchTask<Predator> branch = parallel ? new Parallel<Predator>() : new Sequence<Predator>(); branch.addChild(new SelectTargetTask()); branch.addChild(new PursueTask()); predator.btree = new BehaviorTree<Predator>(branch, predator); return predator; }
private Predator createPredator (boolean join) { Predator predator = new Predator(join ? badlogicTextureRegion : greenFishTextureRegion, this); predator.setPosition(MathUtils.random(stage.getWidth()), MathUtils.random(stage.getHeight()), Align.center); predator.setMaxLinearSpeed(100); predator.setMaxLinearAcceleration(600); final Pursue<Vector2> pursueSB = new Pursue<Vector2>(predator, null, .5f); predator.setSteeringBehavior(pursueSB); testTable.addActor(predator); BranchTask<Predator> branch = join ? new Parallel<Predator>(Orchestrator.Join) : new Parallel<Predator>(Orchestrator.Resume); branch.addChild(new SpinAroundTask()); branch.addChild(new SelectTargetTask()); branch.addChild(new PursueTask()); predator.btree = new BehaviorTree<Predator>(branch, predator); return predator; }
@Override public Actor createActor (Skin skin) { // Create the semaphore NonBlockingSemaphoreRepository.clear(); NonBlockingSemaphoreRepository.addSemaphore("dogSemaphore", 1); Reader reader = null; try { // Parse Buddy's tree reader = Gdx.files.internal("data/dogSemaphore.tree").reader(); BehaviorTreeParser<Dog> parser = new BehaviorTreeParser<Dog>(BehaviorTreeParser.DEBUG_HIGH); BehaviorTree<Dog> buddyTree = parser.parse(reader, new Dog("Buddy")); // Clone Buddy's tree for Snoopy BehaviorTree<Dog> snoopyTree = (BehaviorTree<Dog>)buddyTree.cloneTask(); snoopyTree.setObject(new Dog("Snoopy")); // Create split pane BehaviorTreeViewer<?> buddyTreeViewer = createTreeViewer(buddyTree.getObject().name, buddyTree, false, skin); BehaviorTreeViewer<?> snoopyTreeViewer = createTreeViewer(snoopyTree.getObject().name, snoopyTree, false, skin); return new SplitPane(new ScrollPane(buddyTreeViewer, skin), new ScrollPane(snoopyTreeViewer, skin), true, skin, "default-horizontal"); } finally { StreamUtils.closeQuietly(reader); } }
@SuppressWarnings("rawtypes") private BehaviorTreeLibrary (FileHandleResolver resolver, AssetManager assetManager, int parseDebugLevel) { this.resolver = resolver; // this.assetManager = assetManager; this.repository = new ObjectMap<String, BehaviorTree<?>>(); this.parser = new BehaviorTreeParser(parseDebugLevel); }
/** Creates the {@link BehaviorTree} for the specified reference and blackboard object. * @param treeReference the tree identifier, typically a path * @param blackboard the blackboard object (it can be {@code null}). * @return the tree cloned from the archetype. * @throws SerializationException if the reference cannot be successfully parsed. * @throws TaskCloneException if the archetype cannot be successfully parsed. */ @SuppressWarnings("unchecked") public <T> BehaviorTree<T> createBehaviorTree (String treeReference, T blackboard) { BehaviorTree<T> bt = (BehaviorTree<T>)retrieveArchetypeTree(treeReference).cloneTask(); bt.setObject(blackboard); return bt; }
/** Retrieves the archetype tree from the library. If the library doesn't contain the archetype tree it is loaded and added to * the library. * @param treeReference the tree identifier, typically a path * @return the archetype tree. * @throws SerializationException if the reference cannot be successfully parsed. */ protected BehaviorTree<?> retrieveArchetypeTree (String treeReference) { BehaviorTree<?> archetypeTree = repository.get(treeReference); if (archetypeTree == null) { // if (assetManager != null) { // // TODO: fix me!!! // // archetypeTree = assetManager.load(name, BehaviorTree.class, null); // repository.put(treeReference, archetypeTree); // return null; // } archetypeTree = parser.parse(resolver.resolve(treeReference), null); registerArchetypeTree(treeReference, archetypeTree); } return archetypeTree; }
/** Registers the {@link BehaviorTree} archetypeTree with the specified reference. Existing archetypes in the repository with * the same treeReference will be replaced. * @param treeReference the tree identifier, typically a path. * @param archetypeTree the archetype tree. * @throws IllegalArgumentException if the archetypeTree is null */ public void registerArchetypeTree (String treeReference, BehaviorTree<?> archetypeTree) { if (archetypeTree == null) { throw new IllegalArgumentException("The registered archetype must not be null."); } repository.put(treeReference, archetypeTree); }
private static AIEditor createDogBTEditor() { AIEditor editor = new AIEditor(); // add default task classes to task drawer with default tags editor.addDefaultTaskClasses(); editor.setUpdateStrategy(new AIEditor.BehaviorTreeStepStrategy() { @Override public boolean shouldStep (@SuppressWarnings("rawtypes") BehaviorTree behaviorTree, float v) { return true; } }); // Add dog-specific tasks editor.addTaskClass("dog", AlreadyCriedForHumanDeathCondition.class); editor.addTaskClass("dog", CalculatePathToHumanTask.class); editor.addTaskClass("dog", CalculatePathToStickTask.class); editor.addTaskClass("dog", FollowPathTask.class); editor.addTaskClass("dog", GiveStickTask.class); editor.addTaskClass("dog", HumanWantToPlayCondition.class); editor.addTaskClass("dog", IsHumanDeadCondition.class); editor.addTaskClass("dog", IsHumanInRangeCondition.class); editor.addTaskClass("dog", LieDownTask.class); editor.addTaskClass("dog", PissTask.class); editor.addTaskClass("dog", PickUpStickTask.class); editor.addTaskClass("dog", SetAlreadyCriedForHumanDeathTask.class); editor.addTaskClass("dog", SetThrowButtonTask.class); editor.addTaskClass("dog", SitTask.class); editor.addTaskClass("dog", SpinAroundTask.class); editor.addTaskClass("dog", SpinAroundToFaceHumanTask.class); editor.addTaskClass("dog", StandTask.class); editor.addTaskClass("dog", StickThrownCondition.class); editor.addTaskClass("dog", WanderTask.class); editor.addTaskClass("dog", WhineTask.class); return editor; }
public void save () { Array<BehaviorTree.Listener<E>> listeners = tree.listeners; tree.listeners = null; IntArray taskSteps = new IntArray(); fill(taskSteps, (TaskNode)displayTree.getNodes().get(0)); KryoUtils.save(new SaveObject<E>(tree, step, taskSteps)); tree.listeners = listeners; }
public void load () { @SuppressWarnings("unchecked") SaveObject<E> saveObject = KryoUtils.load(SaveObject.class); BehaviorTree<E> oldTree = tree; tree = saveObject.tree; tree.listeners = oldTree.listeners; step = saveObject.step; updateStepLabel(); rebuildDisplayTree(saveObject.taskSteps); }
protected BehaviorTreeViewer<?> createTreeViewer (String name, BehaviorTree<?> tree, boolean loadAndSave, Skin skin) { @SuppressWarnings({"rawtypes", "unchecked"}) BehaviorTreeViewer<?> btv = new BehaviorTreeViewer(tree, loadAndSave, skin); btv.setName(name); treeViewers.add(btv); return btv; }
@Override public Actor createActor (Skin skin) { Reader reader = null; try { reader = Gdx.files.internal("data/dog.tree").reader(); BehaviorTreeParser<Dog> parser = new BehaviorTreeParser<Dog>(BehaviorTreeParser.DEBUG_HIGH); BehaviorTree<Dog> tree = parser.parse(reader, new Dog("Buddy")); treeViewer = createTreeViewer(tree.getObject().name, tree, true, skin); return new ScrollPane(treeViewer, skin); } finally { StreamUtils.closeQuietly(reader); } }
@Override public Actor createActor (Skin skin) { BehaviorTreeLibraryManager libraryManager = BehaviorTreeLibraryManager.getInstance(); libraryManager.setLibrary(new BehaviorTreeLibrary(BehaviorTreeParser.DEBUG_HIGH)); String name = lazy ? "data/dogIncludeLazy.tree" : "data/dogInclude.tree"; BehaviorTree<Dog> tree = libraryManager.createBehaviorTree(name, new Dog("Buddy")); BehaviorTreeViewer<?> treeViewer = createTreeViewer(tree.getObject().name, tree, true, skin); return new ScrollPane(treeViewer, skin); }
@Override public Actor createActor (Skin skin) { BehaviorTreeLibraryManager libraryManager = BehaviorTreeLibraryManager.getInstance(); BehaviorTreeLibrary library = new BehaviorTreeLibrary(BehaviorTreeParser.DEBUG_HIGH); registerDogBehavior(library); libraryManager.setLibrary(library); BehaviorTree<Dog> tree = libraryManager.createBehaviorTree("dog", new Dog("Buddy")); BehaviorTreeViewer<?> treeViewer = createTreeViewer(tree.getObject().name, tree, true, skin); return new ScrollPane(treeViewer, skin); }
private void registerDogBehavior (BehaviorTreeLibrary library) { Include<Dog> include = new Include<Dog>(); include.lazy = lazy; include.subtree = "dog.actual"; BehaviorTree<Dog> includeBehavior = new BehaviorTree<Dog>(include); library.registerArchetypeTree("dog", includeBehavior); BehaviorTree<Dog> actualBehavior = new BehaviorTree<Dog>(createDogBehavior()); library.registerArchetypeTree("dog.actual", actualBehavior); }
/** * retrieve pool by tree reference, create it if not already exists. * @param treeReference * @return existing or newly created pool. */ protected Pool<BehaviorTree> getPool(final String treeReference){ Pool<BehaviorTree> treePool = pools.get(treeReference); if(treePool == null){ treePool = new Pool<BehaviorTree>(){ @Override protected BehaviorTree newObject() { return newBehaviorTree(treeReference); } }; pools.put(treeReference, treePool); } return treePool; }
@SuppressWarnings("unchecked") @Override public <T> BehaviorTree<T> createBehaviorTree(String treeReference, T blackboard) { Pool<BehaviorTree> pool = getPool(treeReference); BehaviorTree<T> tree = (BehaviorTree<T>)pool.obtain(); tree.setObject(blackboard); return tree; }
/** * Clear pool for a tree reference. * @param treeReference */ public void clear(String treeReference){ Pool<BehaviorTree> treePool = pools.get(treeReference); if(treePool != null){ treePool.clear(); } }
/** * clear all pools. */ public void clear(){ for(Entry<String, Pool<BehaviorTree>> entry : pools.entries()){ entry.value.clear(); } pools.clear(); }
@Override public BehaviorTree loadSync (AssetManager manager, String fileName, FileHandle file, BehaviorTreeParameter parameter) { BehaviorTree bundle = this.behaviorTree; this.behaviorTree = null; return bundle; }
protected BehaviorTree<E> createBehaviorTree (Task<E> root, E object) { if (debugLevel > BehaviorTreeParser.DEBUG_LOW) printTree(root, 0); return new BehaviorTree<E>(root, object); }
public BehaviorTree<Blackboard> getBt() { return bt; }
public void setBt(BehaviorTree<Blackboard> bt) { this.bt = bt; }
public Dog (String name, BehaviorTree<Dog> btree) { this.name = name; this.brainLog = name + " brain"; this.behaviorTree = btree; if (btree != null) btree.setObject(this); }
public BehaviorTree<Dog> getBehaviorTree () { return behaviorTree; }
public void setBehaviorTree (BehaviorTree<Dog> behaviorTree) { this.behaviorTree = behaviorTree; }
public BehaviorTreeViewer (BehaviorTree<E> tree, Skin skin) { this(tree, true, skin); }
public BehaviorTree<E> getBehaviorTree () { return tree; }
SaveObject (BehaviorTree<T> tree, int step, IntArray taskSteps) { this.tree = tree; this.step = step; this.taskSteps = taskSteps; }
/** * Dispose behavior tree obtain by this library. * @param treeReference the tree identifier. * @param behaviorTree the tree to dispose. */ public void disposeBehaviorTree(String treeReference, BehaviorTree<?> behaviorTree){ if(Task.TASK_CLONER != null){ Task.TASK_CLONER.freeTask(behaviorTree); } }
@Override public void disposeBehaviorTree(final String treeReference, BehaviorTree<?> behaviorTree) { Pool<BehaviorTree> pool = getPool(treeReference); pool.free(behaviorTree); }
public void initTest() { Sequence<Blackboard> seq = new Sequence<>(); seq.addChild((new RandomTest()).setDebug("1")); seq.addChild((new RandomTest()).setDebug("2")); seq.addChild((new RandomTest()).setDebug("3")); bt = new BehaviorTree<Blackboard>(seq); bt.setObject(agent.getBlackboard()); }
/** Creates the {@link BehaviorTree} for the specified reference. * @param treeReference the tree identifier, typically a path * @return the tree cloned from the archetype. * @throws SerializationException if the reference cannot be successfully parsed. * @throws TaskCloneException if the archetype cannot be successfully parsed. */ public <T> BehaviorTree<T> createBehaviorTree (String treeReference) { return createBehaviorTree(treeReference, null); }
/** Parses the given string. * @param string the string to parse * @param object the blackboard object. It can be {@code null}. * @return the behavior tree * @throws SerializationException if the string cannot be successfully parsed. */ public BehaviorTree<E> parse (String string, E object) { btReader.parse(string); return createBehaviorTree(btReader.root, object); }