limboai/bt/tasks/composites/bt_parallel.cpp

62 lines
2.3 KiB
C++
Raw Permalink Normal View History

/**
* bt_parallel.cpp
* =============================================================================
* 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.
* =============================================================================
*/
2022-08-28 10:54:34 +00:00
#include "bt_parallel.h"
2023-07-20 16:35:36 +00:00
2022-08-28 10:54:34 +00:00
void BTParallel::_enter() {
for (int i = 0; i < get_child_count(); i++) {
get_child(i)->abort();
2022-08-28 10:54:34 +00:00
}
}
BT::Status BTParallel::_tick(double p_delta) {
2022-08-28 10:54:34 +00:00
int num_succeeded = 0;
int num_failed = 0;
BT::Status return_status = RUNNING;
2022-08-28 10:54:34 +00:00
for (int i = 0; i < get_child_count(); i++) {
Status status = BT::FRESH;
2022-08-28 10:54:34 +00:00
Ref<BTTask> child = get_child(i);
2023-07-28 22:35:47 +00:00
if (!repeat && (child->get_status() == FAILURE || child->get_status() == SUCCESS)) {
2022-08-28 10:54:34 +00:00
status = child->get_status();
} else {
status = child->execute(p_delta);
}
if (status == FAILURE) {
num_failed += 1;
if (num_failed >= num_failures_required && return_status == RUNNING) {
return_status = FAILURE;
}
} else if (status == SUCCESS) {
num_succeeded += 1;
if (num_succeeded >= num_successes_required && return_status == RUNNING) {
return_status = SUCCESS;
}
}
}
if (!repeat && (num_failed + num_succeeded) == get_child_count() && return_status == RUNNING) {
return_status = FAILURE;
}
2022-08-28 10:54:34 +00:00
return return_status;
}
void BTParallel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_num_successes_required"), &BTParallel::get_num_successes_required);
ClassDB::bind_method(D_METHOD("set_num_successes_required", "value"), &BTParallel::set_num_successes_required);
2022-08-28 10:54:34 +00:00
ClassDB::bind_method(D_METHOD("get_num_failures_required"), &BTParallel::get_num_failures_required);
ClassDB::bind_method(D_METHOD("set_num_failures_required", "value"), &BTParallel::set_num_failures_required);
2022-08-28 10:54:34 +00:00
ClassDB::bind_method(D_METHOD("get_repeat"), &BTParallel::get_repeat);
ClassDB::bind_method(D_METHOD("set_repeat", "enable"), &BTParallel::set_repeat);
2022-08-28 10:54:34 +00:00
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_successes_required"), "set_num_successes_required", "get_num_successes_required");
ADD_PROPERTY(PropertyInfo(Variant::INT, "num_failures_required"), "set_num_failures_required", "get_num_failures_required");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "repeat"), "set_repeat", "get_repeat");
}