Can now run the full AI for all combatants and then sort by the action costs to make the action queue.

This commit is contained in:
Zed A. Shaw 2025-11-27 12:46:14 -05:00
parent d244106981
commit b48df3f4db
10 changed files with 104 additions and 59 deletions

View file

@ -10,18 +10,30 @@ namespace combat {
int active = 0;
for(auto& [entity, enemy] : combatants) {
enemy.ai.update();
active += enemy.ai.active();
enemy.ai->update();
active += enemy.ai->active();
if(enemy.ai.active()) {
if(enemy.ai.wants_to("kill_enemy")) {
pending_actions.emplace_back(enemy, BattleAction::ATTACK);
} else if(enemy.ai.wants_to("run_away")) {
pending_actions.emplace_back(enemy, BattleAction::ESCAPE);
if(enemy.ai->active()) {
for(auto& action : enemy.ai->plan.script) {
if(enemy.ai->wants_to("kill_enemy")) {
pending_actions.emplace_back(enemy, action, BattleAction::ATTACK);
} else if(enemy.ai->wants_to("run_away")) {
pending_actions.emplace_back(enemy, action, BattleAction::ESCAPE);
} else {
pending_actions.emplace_back(enemy, action, BattleAction::OTHER);
}
}
}
}
if(pending_actions.size() > 0) {
std::sort(pending_actions.begin(), pending_actions.end(),
[](const auto& a, const auto& b) -> bool
{
return a.wants_to.cost > b.wants_to.cost;
});
}
return active > 0;
}
@ -36,25 +48,25 @@ namespace combat {
void BattleEngine::dump() {
for(auto& [entity, enemy] : combatants) {
fmt::println("\n\n###### ENTITY #{}", entity);
enemy.ai.dump();
enemy.ai->dump();
}
}
void BattleEngine::set(DinkyECS::Entity entity, const std::string& state, bool setting) {
dbc::check(combatants.contains(entity), "invalid combatant given to BattleEngine");
auto& action = combatants.at(entity);
action.ai.set_state(state, setting);
action.ai->set_state(state, setting);
}
void BattleEngine::set_all(const std::string& state, bool setting) {
for(auto& [ent, action] : combatants) {
action.ai.set_state(state, setting);
action.ai->set_state(state, setting);
}
}
void BattleEngine::queue(DinkyECS::Entity entity, BattleAction action) {
Combatant& BattleEngine::get_enemy(DinkyECS::Entity entity) {
dbc::check(combatants.contains(entity), "invalid combatant given to BattleEngine");
auto& enemy = combatants.at(entity);
pending_actions.emplace_back(enemy, action);
return combatants.at(entity);
}
}