diff --git a/modules/steering_ai/SCsub b/modules/steering_ai/SCsub index 054c5bb56..adfacc610 100644 --- a/modules/steering_ai/SCsub +++ b/modules/steering_ai/SCsub @@ -16,6 +16,9 @@ sources = [ "gsai_group_behavior.cpp", "gsai_agent_location.cpp", "proximities/gsai_proximity.cpp", + + "proximities/gsai_radius_proximity.cpp", + "proximities/gsai_infinite_proximity.cpp", ] diff --git a/modules/steering_ai/config.py b/modules/steering_ai/config.py index 7db54cf7b..6fb7c43ee 100644 --- a/modules/steering_ai/config.py +++ b/modules/steering_ai/config.py @@ -15,6 +15,9 @@ def get_doc_classes(): "GSAIGroupBehavior", "GSAIAgentLocation", "GSAIProximity", + + "GSAIRadiusProximity", + "GSAIInfiniteProximity", ] def get_doc_path(): diff --git a/modules/steering_ai/proximities/gsai_infinite_proximity.cpp b/modules/steering_ai/proximities/gsai_infinite_proximity.cpp index 35df87c27..cdf264920 100644 --- a/modules/steering_ai/proximities/gsai_infinite_proximity.cpp +++ b/modules/steering_ai/proximities/gsai_infinite_proximity.cpp @@ -1,24 +1,24 @@ #include "gsai_infinite_proximity.h" -// Determines any agent that is in the specified list as being neighbors with the; -// owner agent, regardless of distance.; -// @category - Proximities; -// Returns a number of neighbors based on a `callback` function.; -//; -// `_find_neighbors` calls `callback` for each agent in the `agents` array and; -// adds one to the count if its `callback` returns true.; -// @tags - virtual; +#include "../gsai_steering_agent.h" +#include "scene/main/scene_tree.h" -int GSAIInfiniteProximity::_find_neighbors(const FuncRef &callback) { +int GSAIInfiniteProximity::_find_neighbors(Ref callback) { int neighbor_count = 0; int agent_count = agents.size(); + Variant arg; + const Variant *argptr[1]; + argptr[0] = &arg; + Variant::CallError err; - for (int i = 0; i < agent_count; ++i) { //i in range(agent_count) - GSAISteeringAgent *current_agent = agents[i] as GSAISteeringAgent; + for (int i = 0; i < agent_count; ++i) { + Ref current_agent = agents[i]; + + ERR_CONTINUE(!current_agent.is_valid()); if (current_agent != agent) { - if (callback.call_func(current_agent)) { + if (callback->call_func(argptr, 1, err)) { neighbor_count += 1; } } @@ -26,7 +26,6 @@ int GSAIInfiniteProximity::_find_neighbors(const FuncRef &callback) { return neighbor_count; } -} GSAIInfiniteProximity::GSAIInfiniteProximity() { } @@ -34,6 +33,5 @@ GSAIInfiniteProximity::GSAIInfiniteProximity() { GSAIInfiniteProximity::~GSAIInfiniteProximity() { } -static void GSAIInfiniteProximity::_bind_methods() { - ClassDB::bind_method(D_METHOD("_find_neighbors", "callback"), &GSAIInfiniteProximity::_find_neighbors); +void GSAIInfiniteProximity::_bind_methods() { } diff --git a/modules/steering_ai/proximities/gsai_infinite_proximity.h b/modules/steering_ai/proximities/gsai_infinite_proximity.h index e891af3a0..91af4b1a7 100644 --- a/modules/steering_ai/proximities/gsai_infinite_proximity.h +++ b/modules/steering_ai/proximities/gsai_infinite_proximity.h @@ -1,11 +1,16 @@ #ifndef GSAI_INFINITE_PROXIMITY_H #define GSAI_INFINITE_PROXIMITY_H +#include "core/object/func_ref.h" +#include "core/object/reference.h" + +#include "gsai_proximity.h" + class GSAIInfiniteProximity : public GSAIProximity { GDCLASS(GSAIInfiniteProximity, GSAIProximity); public: - int _find_neighbors(const FuncRef &callback); + int _find_neighbors(Ref callback); GSAIInfiniteProximity(); ~GSAIInfiniteProximity(); diff --git a/modules/steering_ai/proximities/gsai_radius_proximity.cpp b/modules/steering_ai/proximities/gsai_radius_proximity.cpp index f334b63ff..aaa59c11f 100644 --- a/modules/steering_ai/proximities/gsai_radius_proximity.cpp +++ b/modules/steering_ai/proximities/gsai_radius_proximity.cpp @@ -1,6 +1,9 @@ #include "gsai_radius_proximity.h" +#include "../gsai_steering_agent.h" +#include "scene/main/scene_tree.h" + float GSAIRadiusProximity::get_radius() const { return radius; } @@ -9,84 +12,62 @@ void GSAIRadiusProximity::set_radius(const float val) { radius = val; } -int GSAIRadiusProximity::get__last_frame() const { - return _last_frame; -} +int GSAIRadiusProximity::_find_neighbors(Ref callback) { + ERR_FAIL_COND_V(!agent.is_valid(), 0); -void GSAIRadiusProximity::set__last_frame(const int val) { - _last_frame = val; -} - -SceneTree GSAIRadiusProximity::get_ *_scene_tree() { - return *_scene_tree; -} - -void GSAIRadiusProximity::set_ *_scene_tree(const SceneTree &val) { - *_scene_tree = val; -} - -// Determines any agent that is in the specified list as being neighbors with the owner agent if; -// they lie within the specified radius.; -// @category - Proximities; -// The radius around the owning agent to find neighbors in; -float radius = 0.0; -int _last_frame = 0; -SceneTree *_scene_tree; - -void GSAIRadiusProximity::_init() { - _scene_tree = Engine.get_main_loop(); -} - -// Returns a number of neighbors based on a `callback` function.; -//; -// `_find_neighbors` calls `callback` for each agent in the `agents` array that lie within; -// the radius around the owning agent and adds one to the count if its `callback` returns true.; -// @tags - virtual; - -int GSAIRadiusProximity::_find_neighbors(const FuncRef &callback) { int agent_count = agents.size(); int neighbor_count = 0; - int current_frame = ; + int current_frame; + Variant arg; + const Variant *argptr[1]; + argptr[0] = &arg; + Variant::CallError err; - if (_scene_tree) { - current_frame = _scene_tree.get_frame(); - } + SceneTree *scene_tree = SceneTree::get_singleton(); - else { + if (scene_tree) { + current_frame = scene_tree->get_frame(); + } else { current_frame = -_last_frame; } if (current_frame != _last_frame) { _last_frame = current_frame; - Vector3 owner_position = agent.position; + Vector3 owner_position = agent->get_position(); - for (int i = 0; i < agent_count; ++i) { //i in range(agent_count) - GSAISteeringAgent *current_agent = agents[i] as GSAISteeringAgent; + for (int i = 0; i < agent_count; ++i) { + Ref current_agent = agents[i]; + + ERR_CONTINUE(!current_agent.is_valid()); if (current_agent != agent) { - float distance_squared = owner_position.distance_squared_to(current_agent.position); - float range_to = radius + current_agent.bounding_radius; + float distance_squared = owner_position.distance_squared_to(current_agent->get_position()); + float range_to = radius + current_agent->get_bounding_radius(); + + arg = current_agent.get_ref_ptr(); if (distance_squared < range_to * range_to) { - if (callback.call_func(current_agent)) { - current_agent.is_tagged = true; + if (callback->call_func(argptr, 1, err)) { + current_agent->set_is_tagged(true); neighbor_count += 1; continue; } } } - current_agent.is_tagged = false; + current_agent->set_is_tagged(false); } - } + } else { + for (int i = 0; i < agent_count; ++i) { + Ref current_agent = agents[i]; - else { - for (int i = 0; i < agent_count; ++i) { //i in range(agent_count) - GSAISteeringAgent *current_agent = agents[i] as GSAISteeringAgent; + ERR_CONTINUE(!current_agent.is_valid()); - if (current_agent != agent && current_agent.is_tagged) { - if (callback.call_func(current_agent)) { + if (current_agent != agent && current_agent->get_is_tagged()) { + arg = current_agent.get_ref_ptr(); + + if (callback->call_func(argptr, 1, err)) { neighbor_count += 1; } } @@ -95,30 +76,17 @@ int GSAIRadiusProximity::_find_neighbors(const FuncRef &callback) { return neighbor_count; } -} GSAIRadiusProximity::GSAIRadiusProximity() { radius = 0.0; _last_frame = 0; - *_scene_tree; } GSAIRadiusProximity::~GSAIRadiusProximity() { } -static void GSAIRadiusProximity::_bind_methods() { +void GSAIRadiusProximity::_bind_methods() { ClassDB::bind_method(D_METHOD("get_radius"), &GSAIRadiusProximity::get_radius); ClassDB::bind_method(D_METHOD("set_radius", "value"), &GSAIRadiusProximity::set_radius); ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius"), "set_radius", "get_radius"); - - ClassDB::bind_method(D_METHOD("get__last_frame"), &GSAIRadiusProximity::get__last_frame); - ClassDB::bind_method(D_METHOD("set__last_frame", "value"), &GSAIRadiusProximity::set__last_frame); - ADD_PROPERTY(PropertyInfo(Variant::INT, "_last_frame"), "set__last_frame", "get__last_frame"); - - ClassDB::bind_method(D_METHOD("get_*_scene_tree"), &GSAIRadiusProximity::get_ * _scene_tree); - ClassDB::bind_method(D_METHOD("set_*_scene_tree", "value"), &GSAIRadiusProximity::set_ * _scene_tree); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "*_scene_tree", PROPERTY_HINT_RESOURCE_TYPE, "SceneTree"), "set_*_scene_tree", "get_*_scene_tree"); - - ClassDB::bind_method(D_METHOD("_init"), &GSAIRadiusProximity::_init); - ClassDB::bind_method(D_METHOD("_find_neighbors", "callback"), &GSAIRadiusProximity::_find_neighbors); } diff --git a/modules/steering_ai/proximities/gsai_radius_proximity.h b/modules/steering_ai/proximities/gsai_radius_proximity.h index 14e03d75a..32a4d9f0b 100644 --- a/modules/steering_ai/proximities/gsai_radius_proximity.h +++ b/modules/steering_ai/proximities/gsai_radius_proximity.h @@ -1,6 +1,13 @@ #ifndef GSAI_RADIUS_PROXIMITY_H #define GSAI_RADIUS_PROXIMITY_H +#include "core/object/func_ref.h" +#include "core/object/reference.h" + +#include "gsai_proximity.h" + +class SceneTree; + class GSAIRadiusProximity : public GSAIProximity { GDCLASS(GSAIRadiusProximity, GSAIProximity); @@ -8,14 +15,7 @@ public: float get_radius() const; void set_radius(const float val); - int get__last_frame() const; - void set__last_frame(const int val); - - SceneTree get_ *_scene_tree(); - void set_ *_scene_tree(const SceneTree &val); - - void _init(); - int _find_neighbors(const FuncRef &callback); + int _find_neighbors(Ref callback); GSAIRadiusProximity(); ~GSAIRadiusProximity(); @@ -27,9 +27,8 @@ protected: // they lie within the specified radius. // @category - Proximities // The radius around the owning agent to find neighbors in - float radius = 0.0; - int _last_frame = 0; - SceneTree *_scene_tree; + float radius; + int _last_frame; // Returns a number of neighbors based on a `callback` function. // // `_find_neighbors` calls `callback` for each agent in the `agents` array that lie within diff --git a/modules/steering_ai/register_types.cpp b/modules/steering_ai/register_types.cpp index 0c3b2a41d..97906772e 100644 --- a/modules/steering_ai/register_types.cpp +++ b/modules/steering_ai/register_types.cpp @@ -35,6 +35,9 @@ SOFTWARE. #include "gsai_target_acceleration.h" #include "proximities/gsai_proximity.h" +#include "proximities/gsai_radius_proximity.h" +#include "proximities/gsai_infinite_proximity.h" + static GSAIUtils *gs_ai_utils = NULL; void register_steering_ai_types() { @@ -49,6 +52,9 @@ void register_steering_ai_types() { ClassDB::register_class(); ClassDB::register_class(); ClassDB::register_class(); + + ClassDB::register_class(); + ClassDB::register_class(); } void unregister_steering_ai_types() {