2023-07-21 09:50:06 +00:00
|
|
|
/**
|
|
|
|
* bt_task.h
|
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2023 Serhii Snitsaruk
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style
|
|
|
|
* license that can be found in the LICENSE file or at
|
|
|
|
* https://opensource.org/licenses/MIT.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
2022-08-28 10:54:34 +00:00
|
|
|
|
|
|
|
#ifndef BTTASK_H
|
|
|
|
#define BTTASK_H
|
|
|
|
|
2024-01-10 21:45:42 +00:00
|
|
|
#include "../../blackboard/blackboard.h"
|
|
|
|
#include "../../util/limbo_compat.h"
|
|
|
|
#include "../../util/limbo_string_names.h"
|
|
|
|
#include "../../util/limbo_task_db.h"
|
2023-07-20 16:35:36 +00:00
|
|
|
|
2024-01-10 21:45:42 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2024-01-06 23:47:46 +00:00
|
|
|
#include "core/config/engine.h"
|
|
|
|
#include "core/error/error_macros.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/io/resource.h"
|
2024-01-06 23:47:46 +00:00
|
|
|
#include "core/math/math_funcs.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/object/object.h"
|
|
|
|
#include "core/object/ref_counted.h"
|
2024-01-06 23:47:46 +00:00
|
|
|
#include "core/os/memory.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/string/ustring.h"
|
|
|
|
#include "core/templates/vector.h"
|
2023-11-22 12:05:55 +00:00
|
|
|
#include "core/typedefs.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/array.h"
|
2023-09-16 16:05:09 +00:00
|
|
|
#include "core/variant/binder_common.h"
|
2022-12-15 07:26:52 +00:00
|
|
|
#include "core/variant/dictionary.h"
|
2022-08-28 10:54:34 +00:00
|
|
|
#include "scene/resources/texture.h"
|
2024-01-10 21:45:42 +00:00
|
|
|
#endif // ! LIMBOAI_MODULE
|
2024-01-06 20:04:34 +00:00
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
2024-01-06 23:47:46 +00:00
|
|
|
#include <godot_cpp/classes/engine.hpp>
|
2024-01-06 20:04:34 +00:00
|
|
|
#include <godot_cpp/classes/resource.hpp>
|
|
|
|
#include <godot_cpp/core/object.hpp>
|
|
|
|
#include <godot_cpp/templates/vector.hpp>
|
|
|
|
|
|
|
|
using namespace godot;
|
2024-01-10 21:45:42 +00:00
|
|
|
#endif // ! LIMBOAI_GDEXTENSION
|
2022-08-28 10:54:34 +00:00
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
/**
|
|
|
|
* Base class for BTTask.
|
|
|
|
* Note: In order to properly return Status in the _tick virtual method (GDVIRTUAL1R...)
|
|
|
|
* we must do VARIANT_ENUM_CAST for Status enum before the actual BTTask class declaration.
|
|
|
|
*/
|
|
|
|
class BT : public Resource {
|
|
|
|
GDCLASS(BT, Resource);
|
2022-08-28 10:54:34 +00:00
|
|
|
|
|
|
|
public:
|
2023-09-19 11:43:26 +00:00
|
|
|
enum Status {
|
2022-08-28 10:54:34 +00:00
|
|
|
FRESH,
|
|
|
|
RUNNING,
|
|
|
|
FAILURE,
|
|
|
|
SUCCESS,
|
|
|
|
};
|
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
};
|
|
|
|
|
|
|
|
VARIANT_ENUM_CAST(BT::Status)
|
|
|
|
|
|
|
|
class BTTask : public BT {
|
|
|
|
GDCLASS(BTTask, BT);
|
|
|
|
|
2022-08-28 10:54:34 +00:00
|
|
|
private:
|
2022-08-30 16:48:49 +00:00
|
|
|
friend class BehaviorTree;
|
|
|
|
|
2023-07-20 18:10:02 +00:00
|
|
|
// Avoid namespace pollution in derived classes.
|
|
|
|
struct Data {
|
2023-11-22 12:05:55 +00:00
|
|
|
int index = -1;
|
2023-07-20 18:10:02 +00:00
|
|
|
String custom_name;
|
2023-11-22 12:05:55 +00:00
|
|
|
Node *agent = nullptr;
|
2023-07-20 18:10:02 +00:00
|
|
|
Ref<Blackboard> blackboard;
|
2023-11-22 12:05:55 +00:00
|
|
|
BTTask *parent = nullptr;
|
2023-07-20 18:10:02 +00:00
|
|
|
Vector<Ref<BTTask>> children;
|
2023-11-22 12:05:55 +00:00
|
|
|
Status status = FRESH;
|
|
|
|
double elapsed = 0.0;
|
2023-07-20 18:10:02 +00:00
|
|
|
} data;
|
2022-08-28 10:54:34 +00:00
|
|
|
|
|
|
|
Array _get_children() const;
|
|
|
|
void _set_children(Array children);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2024-01-06 20:04:34 +00:00
|
|
|
virtual String _generate_name();
|
2022-08-31 15:05:25 +00:00
|
|
|
virtual void _setup() {}
|
|
|
|
virtual void _enter() {}
|
|
|
|
virtual void _exit() {}
|
2023-09-19 11:43:26 +00:00
|
|
|
virtual Status _tick(double p_delta) { return FAILURE; }
|
2022-08-28 10:54:34 +00:00
|
|
|
|
2024-01-06 20:04:34 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-12-15 07:26:52 +00:00
|
|
|
GDVIRTUAL0RC(String, _generate_name);
|
|
|
|
GDVIRTUAL0(_setup);
|
|
|
|
GDVIRTUAL0(_enter);
|
|
|
|
GDVIRTUAL0(_exit);
|
2023-09-19 11:43:26 +00:00
|
|
|
GDVIRTUAL1R(Status, _tick, double);
|
2023-08-15 15:05:30 +00:00
|
|
|
GDVIRTUAL0RC(PackedStringArray, _get_configuration_warning);
|
2024-01-06 20:04:34 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
2022-12-15 07:26:52 +00:00
|
|
|
|
2022-08-28 10:54:34 +00:00
|
|
|
public:
|
2024-01-06 20:04:34 +00:00
|
|
|
// TODO: GDExtension doesn't have this method hmm...
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_MODULE
|
2022-12-19 09:29:46 +00:00
|
|
|
virtual bool editor_can_reload_from_file() override { return false; }
|
2024-01-06 20:04:34 +00:00
|
|
|
#endif // LIMBOAI_MODULE
|
2022-12-19 09:29:46 +00:00
|
|
|
|
2023-11-22 12:41:28 +00:00
|
|
|
_FORCE_INLINE_ Node *get_agent() const { return data.agent; }
|
2023-07-20 18:10:02 +00:00
|
|
|
void set_agent(Node *p_agent) { data.agent = p_agent; }
|
2022-12-17 07:33:18 +00:00
|
|
|
|
2023-07-20 18:10:02 +00:00
|
|
|
String get_custom_name() const { return data.custom_name; }
|
2022-08-28 10:54:34 +00:00
|
|
|
void set_custom_name(const String &p_name);
|
2024-01-06 20:04:34 +00:00
|
|
|
String get_task_name();
|
2022-08-28 10:54:34 +00:00
|
|
|
|
2022-12-17 07:33:18 +00:00
|
|
|
Ref<BTTask> get_root() const;
|
|
|
|
|
2022-08-28 10:54:34 +00:00
|
|
|
virtual Ref<BTTask> clone() const;
|
2022-12-17 07:33:18 +00:00
|
|
|
virtual void initialize(Node *p_agent, const Ref<Blackboard> &p_blackboard);
|
2024-01-06 20:04:34 +00:00
|
|
|
virtual PackedStringArray get_configuration_warnings();
|
2022-12-15 07:26:52 +00:00
|
|
|
|
2023-09-19 11:43:26 +00:00
|
|
|
Status execute(double p_delta);
|
2023-10-26 14:20:33 +00:00
|
|
|
void abort();
|
2022-12-17 07:33:18 +00:00
|
|
|
|
2023-11-22 12:41:28 +00:00
|
|
|
_FORCE_INLINE_ Ref<BTTask> get_parent() const { return Ref<BTTask>(data.parent); }
|
|
|
|
_FORCE_INLINE_ bool is_root() const { return data.parent == nullptr; }
|
|
|
|
_FORCE_INLINE_ Ref<Blackboard> get_blackboard() const { return data.blackboard; }
|
|
|
|
_FORCE_INLINE_ Status get_status() const { return data.status; }
|
|
|
|
_FORCE_INLINE_ double get_elapsed_time() const { return data.elapsed; };
|
|
|
|
|
|
|
|
_FORCE_INLINE_ Ref<BTTask> get_child(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, data.children.size(), nullptr);
|
|
|
|
return data.children.get(p_idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
_FORCE_INLINE_ int get_child_count() const { return data.children.size(); }
|
2023-08-19 10:19:58 +00:00
|
|
|
int get_child_count_excluding_comments() const;
|
2023-11-22 12:41:28 +00:00
|
|
|
|
2022-08-28 10:54:34 +00:00
|
|
|
void add_child(Ref<BTTask> p_child);
|
|
|
|
void add_child_at_index(Ref<BTTask> p_child, int p_idx);
|
|
|
|
void remove_child(Ref<BTTask> p_child);
|
2022-09-21 14:13:17 +00:00
|
|
|
void remove_child_at_index(int p_idx);
|
2023-11-22 12:41:28 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ bool has_child(const Ref<BTTask> &p_child) const { return data.children.find(p_child) != -1; }
|
2023-11-22 12:05:55 +00:00
|
|
|
_FORCE_INLINE_ int get_index() const { return data.index; }
|
2023-11-22 12:41:28 +00:00
|
|
|
|
|
|
|
bool is_descendant_of(const Ref<BTTask> &p_task) const;
|
2022-08-28 10:54:34 +00:00
|
|
|
Ref<BTTask> next_sibling() const;
|
2022-12-17 07:33:18 +00:00
|
|
|
|
2024-01-06 20:04:34 +00:00
|
|
|
void print_tree(int p_initial_tabs = 0);
|
2022-08-28 10:54:34 +00:00
|
|
|
|
|
|
|
BTTask();
|
2022-08-30 16:48:49 +00:00
|
|
|
~BTTask();
|
2022-08-28 10:54:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BTTASK_H
|