Make bt nodes execute subnodes immediately (and not across several updates) whenever possible
This commit is contained in:
parent
14b18197bf
commit
d826940b4c
3 changed files with 15 additions and 17 deletions
|
|
@ -39,10 +39,7 @@ namespace psemek::bt
|
||||||
{
|
{
|
||||||
if (condition_result_)
|
if (condition_result_)
|
||||||
{
|
{
|
||||||
if (*condition_result_)
|
return current_node()->update(dt, args...);
|
||||||
return true_branch_->update(dt, args...);
|
|
||||||
else
|
|
||||||
return false_branch_->update(dt, args...);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -51,11 +48,8 @@ namespace psemek::bt
|
||||||
if (auto finished = std::get_if<typename node_type::finished>(&result))
|
if (auto finished = std::get_if<typename node_type::finished>(&result))
|
||||||
{
|
{
|
||||||
condition_result_ = finished->result;
|
condition_result_ = finished->result;
|
||||||
if (*condition_result_)
|
current_node()->start(args...);
|
||||||
true_branch_->start(args...);
|
return current_node()->update(Time{}, args...);
|
||||||
else
|
|
||||||
false_branch_->start(args...);
|
|
||||||
return running{};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -71,10 +65,7 @@ namespace psemek::bt
|
||||||
{
|
{
|
||||||
if (condition_result_)
|
if (condition_result_)
|
||||||
{
|
{
|
||||||
if (*condition_result_)
|
return current_node()->event(event, args...);
|
||||||
return true_branch_->event(event, args...);
|
|
||||||
else
|
|
||||||
return false_branch_->event(event, args...);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return condition_->event(event, args...);
|
return condition_->event(event, args...);
|
||||||
|
|
@ -85,6 +76,11 @@ namespace psemek::bt
|
||||||
node_ptr<tree_type> true_branch_;
|
node_ptr<tree_type> true_branch_;
|
||||||
node_ptr<tree_type> false_branch_;
|
node_ptr<tree_type> false_branch_;
|
||||||
std::optional<bool> condition_result_;
|
std::optional<bool> condition_result_;
|
||||||
|
|
||||||
|
node<tree_type> * current_node()
|
||||||
|
{
|
||||||
|
return *condition_result_ ? true_branch_.get() : false_branch_.get();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename Tree>
|
template <typename Tree>
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ namespace psemek::bt
|
||||||
|
|
||||||
status update(Time dt, Args ... args) override
|
status update(Time dt, Args ... args) override
|
||||||
{
|
{
|
||||||
if (current_index_ < children_.size())
|
while (current_index_ < children_.size())
|
||||||
{
|
{
|
||||||
if (!current_started_)
|
if (!current_started_)
|
||||||
{
|
{
|
||||||
|
|
@ -41,13 +41,14 @@ namespace psemek::bt
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = children_[current_index_]->update(dt, args...);
|
auto result = children_[current_index_]->update(dt, args...);
|
||||||
|
dt = Time{};
|
||||||
if (auto f = std::get_if<finished>(&result))
|
if (auto f = std::get_if<finished>(&result))
|
||||||
{
|
{
|
||||||
if (f->result)
|
if (f->result)
|
||||||
return finished{true};
|
return finished{true};
|
||||||
++current_index_;
|
++current_index_;
|
||||||
current_started_ = false;
|
current_started_ = false;
|
||||||
return running{};
|
continue;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ namespace psemek::bt
|
||||||
|
|
||||||
status update(Time dt, Args ... args) override
|
status update(Time dt, Args ... args) override
|
||||||
{
|
{
|
||||||
if (current_index_ < children_.size())
|
while (current_index_ < children_.size())
|
||||||
{
|
{
|
||||||
if (!current_started_)
|
if (!current_started_)
|
||||||
{
|
{
|
||||||
|
|
@ -41,13 +41,14 @@ namespace psemek::bt
|
||||||
}
|
}
|
||||||
|
|
||||||
auto result = children_[current_index_]->update(dt, args...);
|
auto result = children_[current_index_]->update(dt, args...);
|
||||||
|
dt = Time{};
|
||||||
if (auto f = std::get_if<finished>(&result))
|
if (auto f = std::get_if<finished>(&result))
|
||||||
{
|
{
|
||||||
if (!f->result)
|
if (!f->result)
|
||||||
return finished{false};
|
return finished{false};
|
||||||
++current_index_;
|
++current_index_;
|
||||||
current_started_ = false;
|
current_started_ = false;
|
||||||
return running{};
|
continue;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return result;
|
return result;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue