Fix BTProbabilitySelector showing probability for comments

This commit is contained in:
Serhii Snitsaruk 2023-09-26 16:06:28 +02:00
parent dd978b636c
commit b5a44c0807
4 changed files with 27 additions and 3 deletions

View File

@ -16,15 +16,20 @@
#include "core/error/error_macros.h"
double BTProbabilitySelector::get_weight(int p_index) const {
ERR_FAIL_INDEX_V(p_index, get_child_count(), 0.0);
ERR_FAIL_COND_V(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()), 0.0);
return _get_weight(p_index);
}
void BTProbabilitySelector::set_weight(int p_index, double p_weight) {
ERR_FAIL_INDEX(p_index, get_child_count());
ERR_FAIL_COND(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()));
_set_weight(p_index, p_weight);
}
double BTProbabilitySelector::get_probability(int p_index) const {
ERR_FAIL_INDEX_V(p_index, get_child_count(), 0.0);
ERR_FAIL_COND_V(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()), 0.0);
double total = _get_total_weight();
return total == 0.0 ? 0.0 : _get_weight(p_index) / total;
}
@ -33,6 +38,7 @@ void BTProbabilitySelector::set_probability(int p_index, double p_probability) {
ERR_FAIL_INDEX(p_index, get_child_count());
ERR_FAIL_COND(p_probability < 0.0);
ERR_FAIL_COND(p_probability >= 1.0);
ERR_FAIL_COND(get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static()));
double others_total = _get_total_weight() - _get_weight(p_index);
double others_probability = 1.0 - p_probability;
@ -44,6 +50,11 @@ void BTProbabilitySelector::set_probability(int p_index, double p_probability) {
}
}
bool BTProbabilitySelector::has_probability(int p_index) const {
ERR_FAIL_INDEX_V(p_index, get_child_count(), false);
return !get_child(p_index)->is_class_ptr(BTComment::get_class_ptr_static());
}
void BTProbabilitySelector::set_abort_on_failure(bool p_abort_on_failure) {
abort_on_failure = p_abort_on_failure;
emit_changed();
@ -115,6 +126,7 @@ void BTProbabilitySelector::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_total_weight"), &BTProbabilitySelector::get_total_weight);
ClassDB::bind_method(D_METHOD("get_probability", "p_index"), &BTProbabilitySelector::get_probability);
ClassDB::bind_method(D_METHOD("set_probability", "p_index", "p_probability"), &BTProbabilitySelector::set_probability);
ClassDB::bind_method(D_METHOD("has_probability", "p_index"), &BTProbabilitySelector::has_probability);
ClassDB::bind_method(D_METHOD("get_abort_on_failure"), &BTProbabilitySelector::get_abort_on_failure);
ClassDB::bind_method(D_METHOD("set_abort_on_failure", "p_value"), &BTProbabilitySelector::set_abort_on_failure);

View File

@ -12,6 +12,7 @@
#ifndef BT_PROBABILITY_SELECTOR_H
#define BT_PROBABILITY_SELECTOR_H
#include "modules/limboai/bt/tasks/bt_comment.h"
#include "modules/limboai/bt/tasks/bt_composite.h"
#include "core/core_string_names.h"
@ -37,7 +38,9 @@ private:
_FORCE_INLINE_ double _get_total_weight() const {
double total = 0.0;
for (int i = 0; i < get_child_count(); i++) {
total += _get_weight(i);
if (!get_child(i)->is_class_ptr(BTComment::get_class_ptr_static())) {
total += _get_weight(i);
}
}
return total;
}
@ -57,6 +60,8 @@ public:
double get_probability(int p_index) const;
void set_probability(int p_index, double p_probability);
bool has_probability(int p_index) const;
void set_abort_on_failure(bool p_abort_on_failure);
bool get_abort_on_failure() const;
};

View File

@ -34,6 +34,13 @@
Returns the child task's weight within the weighted probability selection algorithm.
</description>
</method>
<method name="has_probability" qualifiers="const">
<return type="bool" />
<param index="0" name="p_index" type="int" />
<description>
Returns whether the child task at index [param p_index] participates within the weighted probability selection algorithm and has a probability assigned to it. Returns [code]false[/code] for [BTComment] tasks.
</description>
</method>
<method name="set_probability">
<return type="void" />
<param index="0" name="p_index" type="int" />

View File

@ -38,7 +38,7 @@ void TaskTree::_update_item(TreeItem *p_item) {
if (p_item->get_parent()) {
Ref<BTProbabilitySelector> sel = p_item->get_parent()->get_metadata(0);
if (sel.is_valid()) {
if (sel.is_valid() && sel->has_probability(p_item->get_index())) {
p_item->set_custom_draw(0, this, SNAME("_draw_probability"));
p_item->set_cell_mode(0, TreeItem::CELL_MODE_CUSTOM);
}
@ -236,7 +236,7 @@ double TaskTree::get_selected_probability_percent() const {
bool TaskTree::selected_has_probability() const {
bool result = false;
Ref<BTTask> selected = get_selected();
if (selected.is_valid()) {
if (selected.is_valid() && !selected->is_class_ptr(BTComment::get_class_ptr_static())) {
Ref<BTProbabilitySelector> probability_selector = selected->get_parent();
result = probability_selector.is_valid();
}