From c74fa4e3e416683eaf768ed9e203d526948b6320 Mon Sep 17 00:00:00 2001 From: Germain AUBERT Date: Mon, 2 Jan 2017 13:36:33 +0100 Subject: [PATCH] implements Poolable interface for stateful tasks fix libgdx/gdx-ai#12 --- .../src/com/badlogic/gdx/ai/btree/BehaviorTree.java | 9 +++++++++ gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java | 6 ++++++ gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java | 6 ++++++ .../src/com/badlogic/gdx/ai/btree/LoopDecorator.java | 6 ++++++ .../gdx/ai/btree/SingleRunningChildBranch.java | 8 ++++++++ gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java | 11 ++++++++++- .../gdx/ai/btree/branch/DynamicGuardSelector.java | 5 +++++ .../com/badlogic/gdx/ai/btree/branch/Parallel.java | 9 +++++++++ .../com/badlogic/gdx/ai/btree/decorator/Include.java | 7 +++++++ .../com/badlogic/gdx/ai/btree/decorator/Random.java | 6 ++++++ .../com/badlogic/gdx/ai/btree/decorator/Repeat.java | 7 +++++++ .../gdx/ai/btree/decorator/SemaphoreGuard.java | 8 ++++++++ gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java | 8 ++++++++ .../com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java | 7 +++++++ .../com/badlogic/gdx/ai/tests/btree/dog/CareTask.java | 6 ++++++ .../com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java | 5 +++++ .../com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java | 6 ++++++ 17 files changed, 119 insertions(+), 1 deletion(-) diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java index f396c06d..aab24e7b 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/BehaviorTree.java @@ -169,6 +169,15 @@ public void notifyChildAdded (Task task, int index) { listener.childAdded(task, index); } } + + @Override + public void reset() { + removeListeners(); + this.rootTask = null; + this.object = null; + this.listeners = null; + super.reset(); + } private static final class GuardEvaluator extends Task { diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java index 2247292c..adef60c5 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/BranchTask.java @@ -70,5 +70,11 @@ protected Task copyTo (Task task) { return task; } + + @Override + public void reset() { + children.clear(); + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java index 11d5107e..f38b18c3 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/Decorator.java @@ -98,5 +98,11 @@ protected Task copyTo (Task task) { return task; } + + @Override + public void reset() { + child = null; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/LoopDecorator.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/LoopDecorator.java index b9f0bd46..0e60a68e 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/LoopDecorator.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/LoopDecorator.java @@ -65,5 +65,11 @@ public void childRunning (Task runningTask, Task reporter) { super.childRunning(runningTask, reporter); loop = false; } + + @Override + public void reset() { + loop = false; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/SingleRunningChildBranch.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/SingleRunningChildBranch.java index 21806d17..0c4ce74b 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/SingleRunningChildBranch.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/SingleRunningChildBranch.java @@ -129,5 +129,13 @@ protected Task[] createRandomChildren () { System.arraycopy(children.items, 0, rndChildren, 0, children.size); return rndChildren; } + + @Override + public void reset() { + this.currentChildIndex = 0; + this.runningChild = null; + this.randomChildren = null; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java index 66b547d1..d49a48f9 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/Task.java @@ -17,6 +17,7 @@ package com.badlogic.gdx.ai.btree; import com.badlogic.gdx.ai.btree.annotation.TaskConstraint; +import com.badlogic.gdx.utils.Pool.Poolable; import com.badlogic.gdx.utils.reflect.ClassReflection; import com.badlogic.gdx.utils.reflect.ReflectionException; @@ -28,7 +29,7 @@ * @author implicit-invocation * @author davebaol */ @TaskConstraint -public abstract class Task { +public abstract class Task implements Poolable { /** The enumeration of the values that a task's status can have. * @@ -277,5 +278,13 @@ public Task cloneTask () { * @return the given task for chaining * @throws TaskCloneException if the task cannot be successfully copied. */ protected abstract Task copyTo (Task task); + + @Override + public void reset() { + control = null; + guard = null; + status = Status.FRESH; + tree = null; + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/DynamicGuardSelector.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/DynamicGuardSelector.java index 0803dabd..d8065242 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/DynamicGuardSelector.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/DynamicGuardSelector.java @@ -113,4 +113,9 @@ protected Task copyTo (Task task) { return super.copyTo(task); } + @Override + public void reset() { + runningChild = null; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/Parallel.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/Parallel.java index 4f5f14fb..af49a85e 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/Parallel.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/branch/Parallel.java @@ -141,6 +141,15 @@ protected Task copyTo (Task task) { return super.copyTo(task); } + + @Override + public void reset() { + policy = Policy.Sequence; + noRunningTasks = true; + lastResult = null; + currentChildIndex = 0; + super.reset(); + } /** The enumeration of the policies supported by the {@link Parallel} task. */ public enum Policy { diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Include.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Include.java index ff83a03d..7a1dffd4 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Include.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Include.java @@ -101,4 +101,11 @@ protected Task copyTo (Task task) { private Task createSubtreeRootTask () { return BehaviorTreeLibraryManager.getInstance().createRootTask(subtree); } + + @Override + public void reset() { + lazy = false; + subtree = null; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Random.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Random.java index cb72689e..c8576d15 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Random.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Random.java @@ -115,4 +115,10 @@ protected Task copyTo (Task task) { return super.copyTo(task); } + @Override + public void reset() { + this.p = 0; + this.success = ConstantFloatDistribution.ZERO_POINT_FIVE; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Repeat.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Repeat.java index 4ded3e86..3c3874ff 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Repeat.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/Repeat.java @@ -97,4 +97,11 @@ protected Task copyTo (Task task) { return super.copyTo(task); } + + @Override + public void reset() { + count = 0; + times = null; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/SemaphoreGuard.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/SemaphoreGuard.java index f6706b07..58084060 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/SemaphoreGuard.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/decorator/SemaphoreGuard.java @@ -125,4 +125,12 @@ protected Task copyTo (Task task) { return super.copyTo(task); } + + @Override + public void reset() { + name = null; + semaphore = null; + semaphoreAcquired = false; + super.reset(); + } } diff --git a/gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java b/gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java index 9ef8dda8..687f7b96 100644 --- a/gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java +++ b/gdx-ai/src/com/badlogic/gdx/ai/btree/leaf/Wait.java @@ -83,4 +83,12 @@ protected Task copyTo (Task task) { return task; } + @Override + public void reset() { + seconds = ConstantFloatDistribution.ZERO; + startTime = 0; + timeout = 0; + super.reset(); + } + } diff --git a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java index 31bb10f8..cdd58108 100644 --- a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java +++ b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/BarkTask.java @@ -53,4 +53,11 @@ protected Task copyTo (Task task) { return task; } + @Override + public void reset() { + times = ConstantIntegerDistribution.ONE; + t = 0; + super.reset(); + } + } diff --git a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/CareTask.java b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/CareTask.java index 201a47e7..f7363439 100644 --- a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/CareTask.java +++ b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/CareTask.java @@ -44,4 +44,10 @@ protected Task copyTo (Task task) { return task; } + @Override + public void reset() { + urgentProb = 0.8f; + super.reset(); + } + } diff --git a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java index bbfd28e4..350fcc3f 100644 --- a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java +++ b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/MarkTask.java @@ -46,4 +46,9 @@ protected Task copyTo (Task task) { return task; } + @Override + public void reset() { + i = 0; + super.reset(); + } } diff --git a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java index 0d9a5970..2735a409 100644 --- a/tests/src/com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java +++ b/tests/src/com/badlogic/gdx/ai/tests/btree/dog/WalkTask.java @@ -49,5 +49,11 @@ public void end () { protected Task copyTo (Task task) { return task; } + + @Override + public void reset() { + i = 0; + super.reset(); + } }