-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added tests for weighted random composites
- Loading branch information
Showing
11 changed files
with
300 additions
and
68 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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
3 changes: 3 additions & 0 deletions
3
test/randomized_composites/runtime_changes/RuntimeChangesTestScene.gd
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,3 @@ | ||
extends Node2D | ||
|
||
@onready var sequence_random: SequenceRandomComposite = %SequenceRandom |
34 changes: 34 additions & 0 deletions
34
test/randomized_composites/runtime_changes/RuntimeChangesTestScene.tscn
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,34 @@ | ||
[gd_scene load_steps=5 format=3 uid="uid://dhhw4ej2jbyha"] | ||
|
||
[ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="1_10c1m"] | ||
[ext_resource type="Script" path="res://test/randomized_composites/runtime_changes/RuntimeChangesTestScene.gd" id="1_folsk"] | ||
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence_random.gd" id="2_k8ytk"] | ||
[ext_resource type="Script" path="res://test/actions/mock_action.gd" id="3_kqvkq"] | ||
|
||
[node name="RuntimeChangesTestScene" type="Node2D"] | ||
script = ExtResource("1_folsk") | ||
|
||
[node name="BeehaveTree" type="Node" parent="."] | ||
script = ExtResource("1_10c1m") | ||
|
||
[node name="SequenceRandom" type="Node" parent="BeehaveTree"] | ||
unique_name_in_owner = true | ||
script = ExtResource("2_k8ytk") | ||
random_seed = 12345 | ||
use_weights = true | ||
Weights/Idle = 1 | ||
Weights/Run = 1 | ||
"Weights/Attack Meele" = 1 | ||
"Weights/Attack Ranged" = 1 | ||
|
||
[node name="Idle" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kqvkq") | ||
|
||
[node name="Run" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kqvkq") | ||
|
||
[node name="Attack Meele" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kqvkq") | ||
|
||
[node name="Attack Ranged" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kqvkq") |
105 changes: 105 additions & 0 deletions
105
test/randomized_composites/runtime_changes/runtime_changes_test.gd
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,105 @@ | ||
# GdUnit generated TestSuite | ||
class_name RuntimeChangesTest | ||
extends GdUnitTestSuite | ||
@warning_ignore("unused_parameter") | ||
@warning_ignore("return_value_discarded") | ||
|
||
|
||
const __source = "res://test/randomized_composites/runtime_changes/RuntimeChangesTestScene.tscn" | ||
const __mock_action = "res://test/actions/mock_action.gd" | ||
|
||
func create_scene() -> Node2D: | ||
var scene = auto_free(load(__source).instantiate()) | ||
return scene | ||
|
||
|
||
func create_new_action(): | ||
var new_action = auto_free(load(__mock_action).new()) | ||
new_action.name = "New Attack" | ||
return new_action | ||
|
||
|
||
func test_add_child() -> void: | ||
var scene = create_scene() | ||
var runner := scene_runner(scene) | ||
|
||
runner.set_time_factor(100.0) | ||
|
||
var weights_before = scene.sequence_random._weights.duplicate() | ||
|
||
runner.simulate_frames(10) | ||
|
||
var new_action = create_new_action() | ||
scene.sequence_random.add_child(new_action) | ||
|
||
# Weights should have a new key with the added child. | ||
assert_dict(scene.sequence_random._weights)\ | ||
.contains_key_value(new_action.name, 1) | ||
|
||
# All other children's weights should be the same. | ||
for node in weights_before.keys(): | ||
assert_dict(scene.sequence_random._weights)\ | ||
.contains_key_value(node, weights_before[node]) | ||
|
||
runner.simulate_frames(10) # Everything should work fine afterwards. | ||
|
||
|
||
func test_remove_child() -> void: | ||
var scene = create_scene() | ||
var runner := scene_runner(scene) | ||
|
||
runner.set_time_factor(100.0) | ||
|
||
var weights_before: Dictionary = scene.sequence_random._weights.duplicate() | ||
|
||
runner.simulate_frames(10) | ||
|
||
var removed_action = runner.find_child(weights_before.keys()[0]) | ||
scene.sequence_random.remove_child(removed_action) | ||
|
||
# Weights should not have that action anymore. | ||
assert_dict(scene.sequence_random._weights)\ | ||
.not_contains_keys([removed_action.name]) | ||
|
||
# All other children's weights should be the same. | ||
var other_children = weights_before.keys()\ | ||
.filter(func(k): return k != removed_action.name) | ||
for node in other_children: | ||
assert_dict(scene.sequence_random._weights)\ | ||
.contains_key_value(node, weights_before[node]) | ||
|
||
removed_action.queue_free() | ||
|
||
runner.simulate_frames(10) # Everything should work fine afterwards. | ||
|
||
|
||
func test_rename_child() -> void: | ||
var scene = create_scene() | ||
var runner := scene_runner(scene) | ||
|
||
runner.set_time_factor(100.0) | ||
|
||
var weights_before: Dictionary = scene.sequence_random._weights.duplicate() | ||
|
||
runner.simulate_frames(10) | ||
|
||
var renamed_action = runner.find_child(weights_before.keys()[0]) | ||
var previous_name = renamed_action.name | ||
renamed_action.name = "Renamed Action" | ||
|
||
# Weights should not have the old action name anymore. | ||
assert_dict(scene.sequence_random._weights)\ | ||
.not_contains_keys([previous_name]) | ||
|
||
# Weights should have the new name with the default weight. | ||
assert_dict(scene.sequence_random._weights)\ | ||
.contains_key_value(renamed_action.name, 1) | ||
|
||
# All other children's weights should be the same. | ||
var other_children = weights_before.keys()\ | ||
.filter(func(k): return k != previous_name) | ||
for node in other_children: | ||
assert_dict(scene.sequence_random._weights)\ | ||
.contains_key_value(node, weights_before[node]) | ||
|
||
runner.simulate_frames(10) # Everything should work fine afterwards. |
30 changes: 30 additions & 0 deletions
30
test/randomized_composites/weighted_sampling/WeightedSamplingTestScene.gd
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,30 @@ | ||
extends Node2D | ||
|
||
signal done | ||
|
||
# How many iterations should the test run. | ||
@export var test_sample_count: int = 1_000 | ||
|
||
@onready var sequence_random: SequenceRandomComposite = %SequenceRandom | ||
|
||
var reset_count: int = 0 | ||
var sample_count: Dictionary = {} | ||
|
||
|
||
func _on_sequence_reset(new_order: Array[Node]): | ||
reset_count += 1 | ||
var first = new_order[0] | ||
if not sample_count.has(first.name): | ||
sample_count[first.name] = 0 | ||
sample_count[first.name] += 1 | ||
|
||
if reset_count >= test_sample_count: | ||
done.emit() | ||
|
||
|
||
func get_final_results() -> Dictionary: | ||
var final_results = {} | ||
for node in sample_count.keys(): | ||
var perc = float(sample_count[node]) / float(reset_count) * 100.0 | ||
final_results[node] = perc | ||
return final_results |
36 changes: 36 additions & 0 deletions
36
test/randomized_composites/weighted_sampling/WeightedSamplingTestScene.tscn
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,36 @@ | ||
[gd_scene load_steps=5 format=3 uid="uid://dkaaauniwk8vr"] | ||
|
||
[ext_resource type="Script" path="res://test/randomized_composites/weighted_sampling/WeightedSamplingTestScene.gd" id="1_jp21r"] | ||
[ext_resource type="Script" path="res://addons/beehave/nodes/beehave_tree.gd" id="1_lxyfw"] | ||
[ext_resource type="Script" path="res://addons/beehave/nodes/composites/sequence_random.gd" id="2_rx8pw"] | ||
[ext_resource type="Script" path="res://test/actions/mock_action.gd" id="3_kgdbt"] | ||
|
||
[node name="WeightedSamplingTestScene" type="Node2D"] | ||
script = ExtResource("1_jp21r") | ||
|
||
[node name="BeehaveTree" type="Node" parent="."] | ||
script = ExtResource("1_lxyfw") | ||
|
||
[node name="SequenceRandom" type="Node" parent="BeehaveTree"] | ||
unique_name_in_owner = true | ||
script = ExtResource("2_rx8pw") | ||
random_seed = 12345 | ||
use_weights = true | ||
Weights/Idle = 1 | ||
Weights/Run = 1 | ||
"Weights/Attack Meele" = 1 | ||
"Weights/Attack Ranged" = 1 | ||
|
||
[node name="Idle" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kgdbt") | ||
|
||
[node name="Run" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kgdbt") | ||
|
||
[node name="Attack Meele" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kgdbt") | ||
|
||
[node name="Attack Ranged" type="Node" parent="BeehaveTree/SequenceRandom"] | ||
script = ExtResource("3_kgdbt") | ||
|
||
[connection signal="reset" from="BeehaveTree/SequenceRandom" to="." method="_on_sequence_reset"] |
Oops, something went wrong.