-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(composites): new selector random node
Selects the first node to return success and shuffles ever time it's run. Optimized for performance.
- Loading branch information
Showing
6 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Assets/FluidBehaviorTree/Runtime/TaskParents/Composites/SelectorRandom.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using Adnc.FluidBT.Tasks; | ||
using Random = System.Random; | ||
|
||
namespace Adnc.FluidBT.TaskParents.Composites { | ||
/// <summary> | ||
/// Randomly selects a child node with a shuffle algorithm | ||
/// </summary> | ||
public class SelectorRandom : CompositeBase { | ||
private bool _init; | ||
|
||
protected override TaskStatus OnUpdate () { | ||
if (!_init) { | ||
ShuffleChildren(); | ||
_init = true; | ||
} | ||
|
||
for (var i = ChildIndex; i < Children.Count; i++) { | ||
var child = Children[ChildIndex]; | ||
|
||
switch (child.Update()) { | ||
case TaskStatus.Success: | ||
return TaskStatus.Success; | ||
case TaskStatus.Continue: | ||
return TaskStatus.Continue; | ||
} | ||
|
||
ChildIndex++; | ||
} | ||
|
||
return TaskStatus.Failure; | ||
} | ||
|
||
public override void Reset () { | ||
base.Reset(); | ||
|
||
ShuffleChildren(); | ||
} | ||
|
||
private void ShuffleChildren () { | ||
var rng = new Random(); | ||
var n = Children.Count; | ||
while (n > 1) { | ||
n--; | ||
var k = rng.Next(n + 1); | ||
var value = Children[k]; | ||
Children[k] = Children[n]; | ||
Children[n] = value; | ||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/FluidBehaviorTree/Runtime/TaskParents/Composites/SelectorRandom.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Assets/FluidBehaviorTree/Tests/Editor/TaskParents/Composites/SelectorRandomTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using Adnc.FluidBT.Tasks; | ||
using Adnc.FluidBT.Testing; | ||
using NUnit.Framework; | ||
|
||
namespace Adnc.FluidBT.TaskParents.Composites.Editors.Tests { | ||
public class SelectorRandomTest { | ||
public class UpdateMethod { | ||
[Test] | ||
public void It_should_return_success_if_a_child_returns_success () { | ||
var child = A.TaskStub().WithUpdateStatus(TaskStatus.Success).Build(); | ||
var selectorRandom = new SelectorRandom(); | ||
selectorRandom.AddChild(child); | ||
|
||
Assert.AreEqual(TaskStatus.Success, selectorRandom.Update()); | ||
} | ||
|
||
[Test] | ||
public void It_should_return_continue_if_a_child_returns_continue () { | ||
var child = A.TaskStub().WithUpdateStatus(TaskStatus.Continue).Build(); | ||
var selectorRandom = new SelectorRandom(); | ||
selectorRandom.AddChild(child); | ||
|
||
Assert.AreEqual(TaskStatus.Continue, selectorRandom.Update()); | ||
} | ||
|
||
[Test] | ||
public void It_should_return_failure_if_empty () { | ||
var selectorRandom = new SelectorRandom(); | ||
|
||
Assert.AreEqual(TaskStatus.Failure, selectorRandom.Update()); | ||
|
||
} | ||
|
||
[Test] | ||
public void It_should_return_failure_if_child_returns_failure () { | ||
var child = A.TaskStub().WithUpdateStatus(TaskStatus.Failure).Build(); | ||
var selectorRandom = new SelectorRandom(); | ||
selectorRandom.AddChild(child); | ||
|
||
Assert.AreEqual(TaskStatus.Failure, selectorRandom.Update()); | ||
|
||
} | ||
} | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
Assets/FluidBehaviorTree/Tests/Editor/TaskParents/Composites/SelectorRandomTest.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.