Implement BB parameters

This commit is contained in:
Serhii Snitsaruk 2022-10-20 18:26:46 +02:00
parent b451edf49b
commit 172b90ded4
29 changed files with 577 additions and 0 deletions

1
SCsub
View File

@ -9,3 +9,4 @@ env.add_source_files(env.modules_sources, "bt/actions/*.cpp")
env.add_source_files(env.modules_sources, "bt/decorators/*.cpp")
env.add_source_files(env.modules_sources, "bt/conditions/*.cpp")
env.add_source_files(env.modules_sources, "editor/*.cpp")
env.add_source_files(env.modules_sources, "bb_param/*.cpp")

16
bb_param/bb_aabb.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_aabb.h */
#ifndef BB_AABB_H
#define BB_AABB_H
#include "bb_param.h"
#include "core/object.h"
class BBAabb : public BBParam {
GDCLASS(BBAabb, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::AABB; }
};
#endif // BB_AABB_H

16
bb_param/bb_array.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_array.h */
#ifndef BB_ARRAY_H
#define BB_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBArray : public BBParam {
GDCLASS(BBArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::ARRAY; }
};
#endif // BB_ARRAY_H

16
bb_param/bb_basis.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_basis.h */
#ifndef BB_BASIS_H
#define BB_BASIS_H
#include "bb_param.h"
#include "core/object.h"
class BBBasis : public BBParam {
GDCLASS(BBBasis, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::BASIS; }
};
#endif // BB_BASIS_H

16
bb_param/bb_bool.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_bool.h */
#ifndef BB_BOOL_H
#define BB_BOOL_H
#include "bb_param.h"
#include "core/object.h"
class BBBool : public BBParam {
GDCLASS(BBBool, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::BOOL; }
};
#endif // BB_BOOL_H

16
bb_param/bb_byte_array.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_byte_array.h */
#ifndef BB_BYTE_ARRAY_H
#define BB_BYTE_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBByteArray : public BBParam {
GDCLASS(BBByteArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_BYTE_ARRAY; }
};
#endif // BB_BYTE_ARRAY_H

16
bb_param/bb_color.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_color.h */
#ifndef BB_COLOR_H
#define BB_COLOR_H
#include "bb_param.h"
#include "core/object.h"
class BBColor : public BBParam {
GDCLASS(BBColor, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::COLOR; }
};
#endif // BB_COLOR_H

16
bb_param/bb_color_array.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_color_array.h */
#ifndef BB_COLOR_ARRAY_H
#define BB_COLOR_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBColorArray : public BBParam {
GDCLASS(BBColorArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_COLOR_ARRAY; }
};
#endif // BB_COLOR_ARRAY_H

16
bb_param/bb_dictionary.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_dictionary.h */
#ifndef BB_DICTIONARY_H
#define BB_DICTIONARY_H
#include "bb_param.h"
#include "core/object.h"
class BBDictionary : public BBParam {
GDCLASS(BBDictionary, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::DICTIONARY; }
};
#endif // BB_DICTIONARY_H

16
bb_param/bb_float.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_float.h */
#ifndef BB_FLOAT_H
#define BB_FLOAT_H
#include "bb_param.h"
#include "core/object.h"
class BBFloat : public BBParam {
GDCLASS(BBFloat, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::REAL; }
};
#endif // BB_FLOAT_H

16
bb_param/bb_int.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_int.h */
#ifndef BB_INT_H
#define BB_INT_H
#include "bb_param.h"
#include "core/object.h"
class BBInt : public BBParam {
GDCLASS(BBInt, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::INT; }
};
#endif // BB_INT_H

16
bb_param/bb_int_array.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_int_array.h */
#ifndef BB_INT_ARRAY_H
#define BB_INT_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBIntArray : public BBParam {
GDCLASS(BBIntArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_INT_ARRAY; }
};
#endif // BB_INT_ARRAY_H

32
bb_param/bb_node.cpp Normal file
View File

@ -0,0 +1,32 @@
/* bb_node.cpp */
#include "bb_node.h"
#include "core/error_macros.h"
#include "core/variant.h"
#include "scene/main/node.h"
Variant BBNode::get_value(Object *p_agent, const Ref<Blackboard> &p_blackboard, const Variant &p_default) {
ERR_FAIL_COND_V(p_agent == nullptr, Variant());
ERR_FAIL_COND_V(!p_blackboard.is_valid(), Variant());
Variant val;
if (get_value_source() == SAVED_VALUE) {
val = get_saved_value();
} else {
val = p_blackboard->get_var(get_variable(), p_default);
}
if (val.get_type() == Variant::NODE_PATH) {
Node *agent = Object::cast_to<Node>(p_agent);
ERR_FAIL_COND_V_MSG(agent == nullptr, Variant(), "BBNode: p_agent must be a Node.");
return agent->get_node(val);
} else {
Node *node = val;
if (unlikely(node == nullptr && val.get_type() != Variant::NIL)) {
WARN_PRINT("BBNode: Unexpected variant type of a blackboard variable.");
return p_default;
} else {
return node;
}
}
}

19
bb_param/bb_node.h Normal file
View File

@ -0,0 +1,19 @@
/* bb_node.h */
#ifndef BB_NODE_H
#define BB_NODE_H
#include "bb_param.h"
#include "core/object.h"
class BBNode : public BBParam {
GDCLASS(BBNode, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::NODE_PATH; }
public:
virtual Variant get_value(Object *p_agent, const Ref<Blackboard> &p_blackboard, const Variant &p_default = Variant());
};
#endif // BB_NODE_H

51
bb_param/bb_param.cpp Normal file
View File

@ -0,0 +1,51 @@
/* bb_param.cpp */
#include "bb_param.h"
#include "core/class_db.h"
#include "core/error_macros.h"
#include "core/object.h"
#include "core/variant.h"
VARIANT_ENUM_CAST(BBParam::ValueSource);
Variant BBParam::get_value(Object *p_agent, const Ref<Blackboard> &p_blackboard, const Variant &p_default) {
ERR_FAIL_COND_V(p_blackboard.is_valid(), p_default);
if (value_source == SAVED_VALUE) {
return saved_value;
} else {
return p_blackboard->get_var(variable, p_default);
}
}
void BBParam::_get_property_list(List<PropertyInfo> *p_list) const {
if (value_source == ValueSource::SAVED_VALUE) {
p_list->push_back(PropertyInfo(get_type(), "saved_value"));
} else {
p_list->push_back(PropertyInfo(Variant::STRING, "variable"));
}
}
void BBParam::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_value_source", "p_value_source"), &BBParam::set_value_source);
ClassDB::bind_method(D_METHOD("get_value_source"), &BBParam::get_value_source);
ClassDB::bind_method(D_METHOD("set_saved_value", "p_value"), &BBParam::set_saved_value);
ClassDB::bind_method(D_METHOD("get_saved_value"), &BBParam::get_saved_value);
ClassDB::bind_method(D_METHOD("set_variable", "p_variable_name"), &BBParam::set_variable);
ClassDB::bind_method(D_METHOD("get_variable"), &BBParam::get_variable);
ClassDB::bind_method(D_METHOD("get_type"), &BBParam::get_type);
ClassDB::bind_method(D_METHOD("get_value"), &BBParam::get_value);
ADD_PROPERTY(PropertyInfo(Variant::INT, "value_source", PROPERTY_HINT_ENUM, "Saved Value, Blackboard Var"), "set_value_source", "get_value_source");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "variable", PROPERTY_HINT_NONE, "", 0), "set_variable", "get_variable");
ADD_PROPERTY(PropertyInfo(Variant::NIL, "saved_value", PROPERTY_HINT_NONE, "", 0), "set_saved_value", "get_saved_value");
BIND_ENUM_CONSTANT(SAVED_VALUE);
BIND_ENUM_CONSTANT(BLACKBOARD_VAR);
}
BBParam::BBParam() {
value_source = SAVED_VALUE;
variable = "";
saved_value = Variant();
}

57
bb_param/bb_param.h Normal file
View File

@ -0,0 +1,57 @@
/* bb_param.h */
#ifndef BB_PARAM_H
#define BB_PARAM_H
#include "core/object.h"
#include "core/resource.h"
#include "core/variant.h"
#include "modules/limboai/blackboard.h"
class BBParam : public Resource {
GDCLASS(BBParam, Resource);
public:
enum ValueSource : unsigned int {
SAVED_VALUE,
BLACKBOARD_VAR
};
private:
ValueSource value_source;
Variant saved_value;
String variable;
protected:
static void _bind_methods();
virtual Variant::Type get_type() const { return Variant::NIL; }
void _get_property_list(List<PropertyInfo> *p_list) const;
public:
void set_value_source(ValueSource p_value) {
value_source = p_value;
property_list_changed_notify();
emit_changed();
}
ValueSource get_value_source() const { return value_source; }
void set_saved_value(Variant p_value) {
saved_value = p_value;
emit_changed();
}
Variant get_saved_value() const { return saved_value; }
void set_variable(const String &p_value) {
variable = p_value;
emit_changed();
}
String get_variable() const { return variable; }
virtual Variant get_value(Object *p_agent, const Ref<Blackboard> &p_blackboard, const Variant &p_default = Variant());
BBParam();
};
#endif // BB_PARAM_H

16
bb_param/bb_plane.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_plane.h */
#ifndef BB_PLANE_H
#define BB_PLANE_H
#include "bb_param.h"
#include "core/object.h"
class BBPlane : public BBParam {
GDCLASS(BBPlane, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::PLANE; }
};
#endif // BB_PLANE_H

16
bb_param/bb_quat.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_quat.h */
#ifndef BB_QUAT_H
#define BB_QUAT_H
#include "bb_param.h"
#include "core/object.h"
class BBQuat : public BBParam {
GDCLASS(BBQuat, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::QUAT; }
};
#endif // BB_QUAT_H

16
bb_param/bb_real_array.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_real_array.h */
#ifndef BB_REAL_ARRAY_H
#define BB_REAL_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBRealArray : public BBParam {
GDCLASS(BBRealArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_REAL_ARRAY; }
};
#endif // BB_REAL_ARRAY_H

16
bb_param/bb_rect2.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_rect2.h */
#ifndef BB_RECT2_H
#define BB_RECT2_H
#include "bb_param.h"
#include "core/object.h"
class BBRect2 : public BBParam {
GDCLASS(BBRect2, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::RECT2; }
};
#endif // BB_RECT2_H

16
bb_param/bb_string.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_string.h */
#ifndef BB_STRING_H
#define BB_STRING_H
#include "bb_param.h"
#include "core/object.h"
class BBString : public BBParam {
GDCLASS(BBString, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::STRING; }
};
#endif // BB_STRING_H

View File

@ -0,0 +1,16 @@
/* bb_string_array.h */
#ifndef BB_STRING_ARRAY_H
#define BB_STRING_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBStringArray : public BBParam {
GDCLASS(BBStringArray, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_STRING_ARRAY; }
};
#endif // BB_STRING_ARRAY_H

16
bb_param/bb_transform.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_transform.h */
#ifndef BB_TRANSFORM_H
#define BB_TRANSFORM_H
#include "bb_param.h"
#include "core/object.h"
class BBTransform : public BBParam {
GDCLASS(BBTransform, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::TRANSFORM; }
};
#endif // BB_TRANSFORM_H

16
bb_param/bb_transform2d.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_transform2d.h */
#ifndef BB_TRANSFORM2D_H
#define BB_TRANSFORM2D_H
#include "bb_param.h"
#include "core/object.h"
class BBTransform2D : public BBParam {
GDCLASS(BBTransform2D, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::TRANSFORM2D; }
};
#endif // BB_TRANSFORM2D_H

16
bb_param/bb_vector2.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_vector2.h */
#ifndef BB_VECTOR2_H
#define BB_VECTOR2_H
#include "bb_param.h"
#include "core/object.h"
class BBVector2 : public BBParam {
GDCLASS(BBVector2, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::VECTOR2; }
};
#endif // BB_VECTOR2_H

View File

@ -0,0 +1,16 @@
/* bb_vector2_array.h */
#ifndef BB_VECTOR2_ARRAY_H
#define BB_VECTOR2_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBVector2Array : public BBParam {
GDCLASS(BBVector2Array, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_VECTOR2_ARRAY; }
};
#endif // BB_VECTOR2_ARRAY_H

16
bb_param/bb_vector3.h Normal file
View File

@ -0,0 +1,16 @@
/* bb_vector3.h */
#ifndef BB_VECTOR3_H
#define BB_VECTOR3_H
#include "bb_param.h"
#include "core/object.h"
class BBVector3 : public BBParam {
GDCLASS(BBVector3, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::VECTOR3; }
};
#endif // BB_VECTOR3_H

View File

@ -0,0 +1,16 @@
/* bb_vector3_array.h */
#ifndef BB_VECTOR3_ARRAY_H
#define BB_VECTOR3_ARRAY_H
#include "bb_param.h"
#include "core/object.h"
class BBVector3Array : public BBParam {
GDCLASS(BBVector3Array, BBParam);
protected:
virtual Variant::Type get_type() const { return Variant::POOL_VECTOR3_ARRAY; }
};
#endif // BB_VECTOR3_ARRAY_H

View File

@ -4,6 +4,30 @@
#include "core/class_db.h"
#include "bb_param/bb_aabb.h"
#include "bb_param/bb_array.h"
#include "bb_param/bb_basis.h"
#include "bb_param/bb_bool.h"
#include "bb_param/bb_byte_array.h"
#include "bb_param/bb_color.h"
#include "bb_param/bb_color_array.h"
#include "bb_param/bb_dictionary.h"
#include "bb_param/bb_float.h"
#include "bb_param/bb_int.h"
#include "bb_param/bb_int_array.h"
#include "bb_param/bb_param.h"
#include "bb_param/bb_plane.h"
#include "bb_param/bb_quat.h"
#include "bb_param/bb_real_array.h"
#include "bb_param/bb_rect2.h"
#include "bb_param/bb_string.h"
#include "bb_param/bb_string_array.h"
#include "bb_param/bb_transform.h"
#include "bb_param/bb_transform2d.h"
#include "bb_param/bb_vector2.h"
#include "bb_param/bb_vector2_array.h"
#include "bb_param/bb_vector3.h"
#include "bb_param/bb_vector3_array.h"
#include "blackboard.h"
#include "bt/actions/bt_action.h"
#include "bt/actions/bt_console_print.h"
@ -97,6 +121,31 @@ void register_limboai_types() {
ClassDB::register_class<BTCondition>();
ClassDB::register_class<BBParam>();
ClassDB::register_class<BBInt>();
ClassDB::register_class<BBBool>();
ClassDB::register_class<BBFloat>();
ClassDB::register_class<BBString>();
ClassDB::register_class<BBVector2>();
ClassDB::register_class<BBRect2>();
ClassDB::register_class<BBVector3>();
ClassDB::register_class<BBTransform2D>();
ClassDB::register_class<BBPlane>();
ClassDB::register_class<BBQuat>();
ClassDB::register_class<BBAabb>();
ClassDB::register_class<BBBasis>();
ClassDB::register_class<BBTransform>();
ClassDB::register_class<BBColor>();
ClassDB::register_class<BBDictionary>();
ClassDB::register_class<BBArray>();
ClassDB::register_class<BBByteArray>();
ClassDB::register_class<BBIntArray>();
ClassDB::register_class<BBRealArray>();
ClassDB::register_class<BBColorArray>();
ClassDB::register_class<BBStringArray>();
ClassDB::register_class<BBVector2Array>();
ClassDB::register_class<BBVector3Array>();
_limbo_utility = memnew(LimboUtility);
ClassDB::register_class<LimboUtility>();