2024-01-23 09:33:57 +00:00
|
|
|
/**
|
2024-01-23 19:02:23 +00:00
|
|
|
* blackboard_plan.h
|
2024-01-23 09:33:57 +00:00
|
|
|
* =============================================================================
|
|
|
|
* Copyright 2021-2024 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.
|
|
|
|
* =============================================================================
|
|
|
|
*/
|
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
#ifndef BLACKBOARD_PLAN_H
|
|
|
|
#define BLACKBOARD_PLAN_H
|
2024-01-23 09:33:57 +00:00
|
|
|
|
|
|
|
#include "bb_variable.h"
|
|
|
|
#include "blackboard.h"
|
|
|
|
|
2024-01-25 16:59:38 +00:00
|
|
|
#ifdef LIMBOAI_MODULE
|
|
|
|
#include "core/io/resource.h"
|
|
|
|
#endif // LIMBOAI_MODULE
|
|
|
|
|
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
#include <godot_cpp/classes/resource.hpp>
|
|
|
|
using namespace godot;
|
|
|
|
#endif // LIMBOAI_GDEXTENSION
|
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
class BlackboardPlan : public Resource {
|
|
|
|
GDCLASS(BlackboardPlan, Resource);
|
2024-01-23 09:33:57 +00:00
|
|
|
|
|
|
|
private:
|
2024-03-04 15:55:08 +00:00
|
|
|
List<Pair<StringName, BBVariable>> var_list;
|
|
|
|
HashMap<StringName, BBVariable> var_map;
|
2024-01-23 16:54:20 +00:00
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
// When base is not null, the plan is considered to be derived from the base plan.
|
2024-01-24 22:11:09 +00:00
|
|
|
// A derived plan can only have variables that exist in the base plan,
|
|
|
|
// and only the values can be different in those variables.
|
2024-11-10 18:08:56 +00:00
|
|
|
// The derived plan is synced with the base plan to maintain consistency.
|
2024-01-23 19:02:23 +00:00
|
|
|
Ref<BlackboardPlan> base;
|
2024-01-23 09:33:57 +00:00
|
|
|
|
2024-05-13 21:21:55 +00:00
|
|
|
// Mapping between variables in this plan and their parent scope names.
|
|
|
|
// Used for linking variables to their parent scope counterparts upon Blackboard creation/population.
|
|
|
|
HashMap<StringName, StringName> parent_scope_mapping;
|
2024-11-10 18:08:56 +00:00
|
|
|
// Fetcher function for the parent scope plan. Function should return a Ref<BlackboardPlan>.
|
|
|
|
// Used in the inspector: enables mapping feature when set.
|
2024-05-15 09:38:53 +00:00
|
|
|
Callable parent_scope_plan_provider;
|
2024-05-13 21:21:55 +00:00
|
|
|
|
2024-11-10 18:08:56 +00:00
|
|
|
// Bindings to properties in the scene to which this plan belongs.
|
|
|
|
HashMap<StringName, NodePath> property_bindings;
|
|
|
|
bool property_binding_enabled = false;
|
|
|
|
|
2024-03-06 19:17:23 +00:00
|
|
|
// If true, NodePath variables will be prefetched, so that the vars will contain node pointers instead (upon BB creation/population).
|
|
|
|
bool prefetch_nodepath_vars = true;
|
|
|
|
|
2024-11-15 17:50:42 +00:00
|
|
|
_FORCE_INLINE_ bool _is_var_hidden(const String &p_name, const BBVariable &p_var) const { return p_var.get_type() == Variant::NIL || (is_derived() && p_name.begins_with("_")); }
|
|
|
|
|
2024-01-23 14:31:56 +00:00
|
|
|
protected:
|
2024-03-02 15:06:32 +00:00
|
|
|
static void _bind_methods();
|
2024-01-25 16:59:38 +00:00
|
|
|
|
2024-01-23 14:31:56 +00:00
|
|
|
bool _set(const StringName &p_name, const Variant &p_value);
|
|
|
|
bool _get(const StringName &p_name, Variant &r_ret) const;
|
|
|
|
void _get_property_list(List<PropertyInfo> *p_list) const;
|
2024-01-23 16:54:20 +00:00
|
|
|
bool _property_can_revert(const StringName &p_name) const;
|
|
|
|
bool _property_get_revert(const StringName &p_name, Variant &r_property) const;
|
2024-01-23 14:31:56 +00:00
|
|
|
|
2024-09-05 11:39:47 +00:00
|
|
|
#ifdef LIMBOAI_GDEXTENSION
|
|
|
|
String _to_string() const { return "<" + get_class() + "#" + itos(get_instance_id()) + ">"; }
|
|
|
|
#endif
|
|
|
|
|
2024-01-23 09:33:57 +00:00
|
|
|
public:
|
2024-01-23 19:02:23 +00:00
|
|
|
void set_base_plan(const Ref<BlackboardPlan> &p_base);
|
|
|
|
Ref<BlackboardPlan> get_base_plan() const { return base; }
|
2024-01-23 14:31:56 +00:00
|
|
|
|
2024-05-15 09:38:53 +00:00
|
|
|
void set_parent_scope_plan_provider(const Callable &p_parent_scope_plan_provider);
|
|
|
|
Callable get_parent_scope_plan_provider() const { return parent_scope_plan_provider; }
|
|
|
|
|
2024-05-15 11:13:27 +00:00
|
|
|
bool is_mapping_enabled() const { return parent_scope_plan_provider.is_valid() && (parent_scope_plan_provider.call() != Ref<BlackboardPlan>()); }
|
2024-05-13 21:21:55 +00:00
|
|
|
bool has_mapping(const StringName &p_name) const;
|
|
|
|
|
2024-11-10 18:08:56 +00:00
|
|
|
bool has_property_binding(const StringName &p_name) const { return property_bindings.has(p_name); }
|
|
|
|
void set_property_binding(const StringName &p_name, const NodePath &p_path);
|
|
|
|
NodePath get_property_binding(const StringName &p_name) const { return property_bindings.has(p_name) ? property_bindings[p_name] : NodePath(); }
|
|
|
|
|
2024-03-06 19:17:23 +00:00
|
|
|
void set_prefetch_nodepath_vars(bool p_enable);
|
|
|
|
bool is_prefetching_nodepath_vars() const;
|
|
|
|
|
2024-03-04 15:55:08 +00:00
|
|
|
void add_var(const StringName &p_name, const BBVariable &p_var);
|
|
|
|
void remove_var(const StringName &p_name);
|
|
|
|
BBVariable get_var(const StringName &p_name);
|
|
|
|
Pair<StringName, BBVariable> get_var_by_index(int p_index);
|
2024-03-06 19:17:23 +00:00
|
|
|
_FORCE_INLINE_ bool has_var(const StringName &p_name) { return var_map.has(p_name); }
|
|
|
|
_FORCE_INLINE_ bool is_empty() const { return var_map.is_empty(); }
|
2024-01-25 16:59:38 +00:00
|
|
|
int get_var_count() const { return var_map.size(); }
|
2024-01-24 22:11:09 +00:00
|
|
|
|
2024-05-31 09:57:27 +00:00
|
|
|
TypedArray<StringName> list_vars() const;
|
2024-03-04 15:55:08 +00:00
|
|
|
StringName get_var_name(const BBVariable &p_var) const;
|
|
|
|
bool is_valid_var_name(const StringName &p_name) const;
|
|
|
|
void rename_var(const StringName &p_name, const StringName &p_new_name);
|
2024-01-25 21:01:14 +00:00
|
|
|
void move_var(int p_index, int p_new_index);
|
2024-01-23 09:33:57 +00:00
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
void sync_with_base_plan();
|
2024-03-08 14:33:28 +00:00
|
|
|
_FORCE_INLINE_ bool is_derived() const { return base.is_valid(); }
|
2024-09-15 12:16:54 +00:00
|
|
|
_FORCE_INLINE_ bool is_derived_var_changed(const StringName &p_name) const { return base.is_valid() && var_map.has(p_name) && var_map[p_name].is_value_changed(); }
|
2024-01-23 16:54:20 +00:00
|
|
|
|
2024-09-15 12:16:54 +00:00
|
|
|
Ref<Blackboard> create_blackboard(Node *p_prefetch_root, const Ref<Blackboard> &p_parent_scope = Ref<Blackboard>(), Node *p_prefetch_root_for_base_plan = nullptr);
|
|
|
|
void populate_blackboard(const Ref<Blackboard> &p_blackboard, bool overwrite, Node *p_prefetch_root, Node *p_prefetch_root_for_base_plan = nullptr);
|
2024-01-23 09:33:57 +00:00
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
BlackboardPlan();
|
2024-01-23 09:33:57 +00:00
|
|
|
};
|
|
|
|
|
2024-01-23 19:02:23 +00:00
|
|
|
#endif // BLACKBOARD_PLAN_H
|