2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:29:17 +01:00
|
|
|
#include "gsai_priority.h"
|
2023-01-13 21:13:57 +01:00
|
|
|
|
|
|
|
float GSAIPriority::get_zero_threshold() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return zero_threshold;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPriority::set_zero_threshold(const float val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
zero_threshold = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int GSAIPriority::get__last_selected_index() const {
|
2023-01-13 21:35:07 +01:00
|
|
|
return _last_selected_index;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPriority::set__last_selected_index(const int val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
_last_selected_index = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Array GSAIPriority::get__behaviors() {
|
2023-01-13 21:35:07 +01:00
|
|
|
return _behaviors;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSAIPriority::set__behaviors(const Array &val) {
|
2023-01-13 21:35:07 +01:00
|
|
|
_behaviors = val;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
// Container for multiple behaviors that returns the result of the first child;
|
|
|
|
// behavior with non-zero acceleration.;
|
|
|
|
// @category - Combination behaviors;
|
|
|
|
// If a behavior's acceleration is lower than this threshold, the container;
|
|
|
|
// considers it has an acceleration of zero.;
|
|
|
|
float zero_threshold = 0.0;
|
|
|
|
// The index of the last behavior the container prioritized.;
|
|
|
|
int _last_selected_index = 0;
|
|
|
|
Array _behaviors = Array();
|
|
|
|
// Appends a steering behavior as a child of this container.;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIPriority::add_behavior(const GSAISteeringBehavior &behavior) {
|
|
|
|
_behaviors.append(behavior);
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
// Returns the behavior at the position in the pool referred to by `index`, or;
|
|
|
|
// `null` if no behavior was found.;
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAISteeringBehavior GSAIPriority::get_behavior(const int index) {
|
|
|
|
if (_behaviors.size() > index) {
|
|
|
|
return _behaviors[index];
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()));
|
|
|
|
return null;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIPriority::remove_behavior(const int index) {
|
|
|
|
if (_behaviors.size() > index) {
|
|
|
|
_behaviors.remove(index);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
printerr("Tried to get index " + str(index) + " in array of size " + str(_behaviors.size()));
|
|
|
|
return;
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
int GSAIPriority::get_behaviour_count() {
|
|
|
|
return _behaviors.size();
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
void GSAIPriority::_calculate_steering(const GSAITargetAcceleration &accel) {
|
|
|
|
float threshold_squared = zero_threshold * zero_threshold;
|
|
|
|
_last_selected_index = -1;
|
|
|
|
int size = _behaviors.size();
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (size > 0) {
|
|
|
|
for (int i = 0; i < size; ++i) { //i in range(size)
|
|
|
|
_last_selected_index = i;
|
|
|
|
GSAISteeringBehavior *behavior = _behaviors[i];
|
|
|
|
behavior.calculate_steering(accel);
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
if (accel.get_magnitude_squared() > threshold_squared) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
else {
|
|
|
|
accel.set_zero();
|
|
|
|
}
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPriority::GSAIPriority() {
|
|
|
|
zero_threshold = 0.0;
|
|
|
|
_last_selected_index = 0;
|
|
|
|
_behaviors = Array();
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
GSAIPriority::~GSAIPriority() {
|
2023-01-13 21:13:57 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
static void GSAIPriority::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_zero_threshold"), &GSAIPriority::get_zero_threshold);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_zero_threshold", "value"), &GSAIPriority::set_zero_threshold);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::REAL, "zero_threshold"), "set_zero_threshold", "get_zero_threshold");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get__last_selected_index"), &GSAIPriority::get__last_selected_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("set__last_selected_index", "value"), &GSAIPriority::set__last_selected_index);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "_last_selected_index"), "set__last_selected_index", "get__last_selected_index");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get__behaviors"), &GSAIPriority::get__behaviors);
|
|
|
|
ClassDB::bind_method(D_METHOD("set__behaviors", "value"), &GSAIPriority::set__behaviors);
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "_behaviors"), "set__behaviors", "get__behaviors");
|
2023-01-13 21:13:57 +01:00
|
|
|
|
2023-01-13 21:35:07 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("add_behavior", "behavior"), &GSAIPriority::add_behavior);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_behavior", "index"), &GSAIPriority::get_behavior);
|
|
|
|
ClassDB::bind_method(D_METHOD("remove_behavior", "index"), &GSAIPriority::remove_behavior);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_behaviour_count"), &GSAIPriority::get_behaviour_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("_calculate_steering", "accel"), &GSAIPriority::_calculate_steering);
|
|
|
|
}
|