Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repeater decorator + tests #235

Merged
merged 2 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions addons/beehave/icons/repeater.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions addons/beehave/nodes/decorators/repeater.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
## The repeater will execute its child until it returns `SUCCESS` a certain amount of times.
## When the number of maximum ticks is reached, it will return a `SUCCESS` status code.
## If the child returns `FAILURE`, the repeater will return `FAILURE` immediately.
@tool
@icon("../../icons/repeater.svg")
class_name RepeaterDecorator extends Decorator

@export var repetitions: int = 1
var current_count: int = 0


func before_run(actor: Node, blackboard: Blackboard):
current_count = 0


func tick(actor: Node, blackboard: Blackboard) -> int:
var child = get_child(0)

if current_count < repetitions:
if running_child == null:
child.before_run(actor, blackboard)

var response = child.tick(actor, blackboard)

if can_send_message(blackboard):
BeehaveDebuggerMessages.process_tick(child.get_instance_id(), response)

if child is ConditionLeaf:
blackboard.set_value("last_condition", child, str(actor.get_instance_id()))
blackboard.set_value("last_condition_status", response, str(actor.get_instance_id()))

if response == RUNNING:
running_child = child
if child is ActionLeaf:
blackboard.set_value("running_action", child, str(actor.get_instance_id()))
return RUNNING

current_count += 1
child.after_run(actor, blackboard)

if running_child != null:
running_child = null

if response == FAILURE:
return FAILURE

if current_count >= repetitions:
return SUCCESS

return RUNNING
else:
return SUCCESS


func get_class_name() -> Array[StringName]:
var classes := super()
classes.push_back(&"LimiterDecorator")
return classes
98 changes: 98 additions & 0 deletions test/nodes/decorators/repeater_test.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# GdUnit generated TestSuite
class_name RepeaterTest
extends GdUnitTestSuite
@warning_ignore("unused_parameter")
@warning_ignore("return_value_discarded")


# TestSuite generated from
const __source = "res://addons/beehave/nodes/decorators/repeater.gd"
const __action = "res://test/actions/mock_action.gd"
const __tree = "res://addons/beehave/nodes/beehave_tree.gd"
const __blackboard = "res://addons/beehave/blackboard.gd"

var tree: BeehaveTree
var action: MockAction
var repeater: RepeaterDecorator


func before_test() -> void:
tree = auto_free(load(__tree).new())
action = auto_free(load(__action).new())
repeater = auto_free(load(__source).new())

# action setup
action.running_frame_count = 3 # runs for 3 frames
action.started_running.connect(_on_action_started)
action.stopped_running.connect(_on_action_ended)

var actor = auto_free(Node2D.new())
var blackboard = auto_free(load(__blackboard).new())

tree.add_child(repeater)
repeater.add_child(action)

tree.actor = actor
tree.blackboard = blackboard


func after_test():
# resets blackboard
tree.blackboard.set_value("started", 0)
tree.blackboard.set_value("ended", 0)


func test_repetitions(count: int, test_parameters: Array = [[2], [0]]) -> void:
repeater.repetitions = count
action.final_result = BeehaveNode.SUCCESS

var frames_to_run = count * (action.running_frame_count + 1)

# It should return `RUNNING` every frame but the last one.
for i in range(frames_to_run - 1):
assert_that(tree.tick()).is_equal(BeehaveNode.RUNNING)

assert_that(tree.tick()).is_equal(BeehaveNode.SUCCESS)

var times_started = tree.blackboard.get_value("started", 0)
var times_ended = tree.blackboard.get_value("ended", 0)

assert_int(times_started).is_equal(count)
assert_int(times_ended).is_equal(count)


func test_failure():
repeater.repetitions = 2
action.final_result = BeehaveNode.SUCCESS

for i in range(action.running_frame_count + 1):
assert_that(tree.tick()).is_equal(BeehaveNode.RUNNING)

# it should have started and ended normally
var times_started = tree.blackboard.get_value("started", 0)
var times_ended = tree.blackboard.get_value("ended", 0)

assert_int(times_started).is_equal(1)
assert_int(times_ended).is_equal(1)

action.final_result = BeehaveNode.FAILURE

for i in range(action.running_frame_count):
assert_that(tree.tick()).is_equal(BeehaveNode.RUNNING)
assert_that(tree.tick()).is_equal(BeehaveNode.FAILURE)

times_started = tree.blackboard.get_value("started", 0)
times_ended = tree.blackboard.get_value("ended", 0)

assert_int(times_started).is_equal(2)
assert_int(times_ended).is_equal(2)


func _on_action_started(actor, blackboard):
var started = blackboard.get_value("started", 0)
blackboard.set_value("started", started + 1)


func _on_action_ended(actor, blackboard):
var ended = blackboard.get_value("ended", 0)
blackboard.set_value("ended", ended + 1)