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

Beehave tree and blackboard improvements #313

Merged
merged 3 commits into from
Feb 17, 2024
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
14 changes: 10 additions & 4 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
import os
import sys

def recursive_glob(rootdir, pattern):
matches = []
for root, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
if filename.endswith(pattern):
matches.append(os.path.join(root, filename))
return matches

env = SConscript("godot-cpp/SConstruct")

# Add those directory manually, so we can skip the godot_cpp directory when including headers in C++ files
Expand All @@ -21,10 +29,8 @@ env.Append(CPPPATH=[env.Dir(d) for d in source_path])

# tweak this if you want to use different folders, or more folders, to store your source code in.
env.Append(CPPPATH=["extension/src/"])
sources = [
Glob("extension/src/*.cpp"),
Glob("extension/src/nodes/*.cpp")
]

sources = recursive_glob('extension/src', '.cpp')

if env["platform"] == "macos":
library = env.SharedLibrary(
Expand Down
Binary file modified addons/beehave/libs/windows/beehave.windows.editor.x86_64.dll
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion addons/gdUnit4/GdUnitRunner.cfg
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"included":{"res://test/":[]},"server_port":31002,"skipped":{},"version":"1.0"}
{"included":{"res://test/beehave_tree_test.gd":[]},"server_port":31002,"skipped":{},"version":"1.0"}
File renamed without changes.
10 changes: 5 additions & 5 deletions examples/actions/SetModulateColor.gd
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
extends ActionLeaf
extends BeehaveAction

@export var modulate_color:Color = Color.WHITE
@export var interpolation_time:float = 3.0

var current_color
var tween

func tick(actor: Node, _blackboard: Blackboard) -> int:
func tick(context: BeehaveContext) -> int:

if current_color != modulate_color and actor.modulate != modulate_color:
if current_color != modulate_color and context.get_actor().modulate != modulate_color:
if tween != null:
tween.stop()
current_color = modulate_color
tween = create_tween()\
.set_ease(Tween.EASE_IN_OUT)\
.set_trans(Tween.TRANS_CUBIC)
tween.tween_property(actor, "modulate", current_color, interpolation_time)\
.finished.connect(_finished.bind(actor))
tween.tween_property(context.get_actor(), "modulate", current_color, interpolation_time)\
.finished.connect(_finished.bind(context.get_actor()))

if current_color != null:
return RUNNING
Expand Down
2 changes: 1 addition & 1 deletion examples/conditions/HasNegativePosition.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
extends ConditionLeaf


func tick(actor: Node, _blackboard: Blackboard) -> int:
func tick(context: BeehaveContext) -> int:
if actor.position.x < 0.0 and actor.position.y < 0.0:
return SUCCESS
else:
Expand Down
2 changes: 1 addition & 1 deletion examples/conditions/HasPositivePosition.gd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class_name HasPositivePosition extends ConditionLeaf


func tick(actor: Node, _blackboard: Blackboard) -> int:
func tick(context: BeehaveContext) -> int:
if actor.position.x > 0.0 and actor.position.y > 0.0:
return SUCCESS
else:
Expand Down
4 changes: 2 additions & 2 deletions examples/random_tree_example/RandomAction.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
## [member weights]. Changes its status every [member reset_duration_msec]
## milliseconds.
@tool
class_name RandomAction extends ActionLeaf
class_name RandomAction extends BeehaveAction


## How often this action changes its return status, in milliseconds.
Expand All @@ -31,7 +31,7 @@ func _get_random_action():
return weights.size() - 1


func tick(actor: Node, blackboard: Blackboard) -> int:
func tick(context: BeehaveContext) -> int:
var step = Time.get_ticks_msec() / reset_duration_msec
if step != last_step:
action = _get_random_action()
Expand Down
File renamed without changes.
22 changes: 22 additions & 0 deletions extension/src/nodes/beehave_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
/**************************************************************************/

#include "beehave_tree.h"
#include "beehave_blackboard.h"
#include <core/class_db.hpp>

using namespace godot;
Expand All @@ -54,6 +55,25 @@ void BeehaveTree::_bind_methods() {
}

void BeehaveTree::_ready() {
if (!actor) {
actor = get_parent();
}
if (!blackboard) {
_internal_blackboard = memnew(BeehaveBlackboard);
blackboard = _internal_blackboard;
}
set_physics_process(enabled && process_thread == ProcessThread::PHYSICS);
set_process(enabled && process_thread == ProcessThread::IDLE);

// Randomize at what frames tick() will happen to avoid stutters
_last_tick = rand() % tick_rate;
}

void BeehaveTree::_exit_tree() {
if (_internal_blackboard) {
memfree(_internal_blackboard);
_internal_blackboard = nullptr;
}
}

void BeehaveTree::_process(double delta) {
Expand All @@ -69,9 +89,11 @@ void BeehaveTree::_physics_process(double delta) {
}

void BeehaveTree::enable() {
enabled = true;
}

void BeehaveTree::disable() {
enabled = false;
}

void BeehaveTree::process_internally(double delta) {
Expand Down
2 changes: 2 additions & 0 deletions extension/src/nodes/beehave_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class BeehaveTree : public Node {
bool enabled;
Node *actor;
BeehaveBlackboard *blackboard;
BeehaveBlackboard *_internal_blackboard = nullptr;
Ref<BeehaveContext> context;
BeehaveTreeNode::TickStatus tick_status;
ProcessThread process_thread = ProcessThread::PHYSICS;
Expand All @@ -69,6 +70,7 @@ class BeehaveTree : public Node {
void _ready();
void _process(double delta);
void _physics_process(double delta);
void _exit_tree();
void enable();
void disable();
BeehaveTreeNode::TickStatus tick();
Expand Down
3 changes: 2 additions & 1 deletion extension/src/nodes/beehave_tree_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ BeehaveTreeNode::TickStatus BeehaveTreeNode::tick(Ref<BeehaveContext> context) {
}

void BeehaveTreeNode::_bind_methods() {
ClassDB::bind_method(D_METHOD("tick"), &BeehaveTreeNode::tick);

BIND_VIRTUAL_METHOD(BeehaveTreeNode, tick);

BIND_ENUM_CONSTANT(SUCCESS);
BIND_ENUM_CONSTANT(FAILURE);
Expand Down
2 changes: 1 addition & 1 deletion extension/src/nodes/beehave_tree_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class BeehaveTreeNode : public Node {
BeehaveTreeNode();
~BeehaveTreeNode();

TickStatus tick(Ref<BeehaveContext> context);
virtual TickStatus tick(Ref<BeehaveContext> context);
};

} //namespace godot
Expand Down
44 changes: 44 additions & 0 deletions extension/src/nodes/decorators/beehave_decorator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**************************************************************************/
/* beehave_decorator.cpp */
/**************************************************************************/
/* This file is part of: */
/* BEEHAVE */
/* https://bitbra.in/beehave */
/**************************************************************************/
/* Copyright (c) 2024-present Beehave Contributors. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "beehave_decorator.h"

using namespace godot;

BeehaveDecorator::BeehaveDecorator() {

}

BeehaveDecorator::~BeehaveDecorator() {

}

void BeehaveDecorator::_bind_methods() {

}
52 changes: 52 additions & 0 deletions extension/src/nodes/decorators/beehave_decorator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**************************************************************************/
/* beehave_decorator.h */
/**************************************************************************/
/* This file is part of: */
/* BEEHAVE */
/* https://bitbra.in/beehave */
/**************************************************************************/
/* Copyright (c) 2024-present Beehave Contributors. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef BEEHAVE_DECORATOR_H
#define BEEHAVE_DECORATOR_H

#include "nodes/beehave_tree_node.h"

namespace godot {

class BeehaveDecorator : public BeehaveTreeNode {
GDCLASS(BeehaveDecorator, BeehaveTreeNode);

protected:
BeehaveTreeNode *running_child;

static void _bind_methods();

public:
BeehaveDecorator();
~BeehaveDecorator();
};

}

#endif//BEEHAVE_DECORATOR_H
44 changes: 44 additions & 0 deletions extension/src/nodes/decorators/beehave_succeeder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**************************************************************************/
/* beehave_succeeder.cpp */
/**************************************************************************/
/* This file is part of: */
/* BEEHAVE */
/* https://bitbra.in/beehave */
/**************************************************************************/
/* Copyright (c) 2024-present Beehave Contributors. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "beehave_succeeder.h"

using namespace godot;

BeehaveSucceeder::BeehaveSucceeder() {

}

BeehaveSucceeder::~BeehaveSucceeder() {

}

void BeehaveSucceeder::_bind_methods() {

}
49 changes: 49 additions & 0 deletions extension/src/nodes/decorators/beehave_succeeder.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**************************************************************************/
/* beehave_succeeder.h */
/**************************************************************************/
/* This file is part of: */
/* BEEHAVE */
/* https://bitbra.in/beehave */
/**************************************************************************/
/* Copyright (c) 2024-present Beehave Contributors. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef BEEHAVE_SUCCEEDER
#define BEEHAVE_SUCCEEDER

#include "nodes/decorators/beehave_decorator.h"

namespace godot {

class BeehaveSucceeder : public BeehaveDecorator {
GDCLASS(BeehaveSucceeder, BeehaveDecorator);

protected:
static void _bind_methods();

public:
BeehaveSucceeder();
~BeehaveSucceeder();
};
}

#endif //BEEHAVE_SUCCEEDER
Empty file.
Loading
Loading