Make bt nodes execute subnodes immediately (and not across several updates) whenever possible

This commit is contained in:
Nikita Lisitsa 2025-07-08 20:21:59 +03:00
parent 14b18197bf
commit d826940b4c
3 changed files with 15 additions and 17 deletions

View file

@ -39,10 +39,7 @@ namespace psemek::bt
{
if (condition_result_)
{
if (*condition_result_)
return true_branch_->update(dt, args...);
else
return false_branch_->update(dt, args...);
return current_node()->update(dt, args...);
}
else
{
@ -51,11 +48,8 @@ namespace psemek::bt
if (auto finished = std::get_if<typename node_type::finished>(&result))
{
condition_result_ = finished->result;
if (*condition_result_)
true_branch_->start(args...);
else
false_branch_->start(args...);
return running{};
current_node()->start(args...);
return current_node()->update(Time{}, args...);
}
return result;
@ -71,10 +65,7 @@ namespace psemek::bt
{
if (condition_result_)
{
if (*condition_result_)
return true_branch_->event(event, args...);
else
return false_branch_->event(event, args...);
return current_node()->event(event, args...);
}
else
return condition_->event(event, args...);
@ -85,6 +76,11 @@ namespace psemek::bt
node_ptr<tree_type> true_branch_;
node_ptr<tree_type> false_branch_;
std::optional<bool> condition_result_;
node<tree_type> * current_node()
{
return *condition_result_ ? true_branch_.get() : false_branch_.get();
}
};
template <typename Tree>

View file

@ -32,7 +32,7 @@ namespace psemek::bt
status update(Time dt, Args ... args) override
{
if (current_index_ < children_.size())
while (current_index_ < children_.size())
{
if (!current_started_)
{
@ -41,13 +41,14 @@ namespace psemek::bt
}
auto result = children_[current_index_]->update(dt, args...);
dt = Time{};
if (auto f = std::get_if<finished>(&result))
{
if (f->result)
return finished{true};
++current_index_;
current_started_ = false;
return running{};
continue;
}
else
return result;

View file

@ -32,7 +32,7 @@ namespace psemek::bt
status update(Time dt, Args ... args) override
{
if (current_index_ < children_.size())
while (current_index_ < children_.size())
{
if (!current_started_)
{
@ -41,13 +41,14 @@ namespace psemek::bt
}
auto result = children_[current_index_]->update(dt, args...);
dt = Time{};
if (auto f = std::get_if<finished>(&result))
{
if (!f->result)
return finished{false};
++current_index_;
current_started_ = false;
return running{};
continue;
}
else
return result;