Now have the basics of the turn based battle engine with AI rebellion working.

This commit is contained in:
Zed A. Shaw 2025-12-01 00:14:08 -05:00
parent f3b20f30c5
commit c78b2ae75e
8 changed files with 114 additions and 73 deletions

View file

@ -3,34 +3,41 @@
namespace combat {
void BattleEngine::add_enemy(Combatant enemy) {
combatants.try_emplace(enemy.entity, enemy);
$combatants.try_emplace(enemy.entity, enemy);
}
void BattleEngine::player_request(const std::string& request) {
$player_requests.emplace(request);
}
bool BattleEngine::plan() {
dbc::check($player_requests.size() > 0, "Calling plan without any player reqeusts queued.");
using enum BattleHostState;
int active = 0;
for(auto& [entity, enemy] : combatants) {
for(auto& [entity, enemy] : $combatants) {
enemy.ai->update();
active += enemy.ai->active();
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);
BattleHostState host_state = not_host;
if(enemy.is_host) {
host_state = $player_requests.contains(action.name) ? agree : disagree;
}
$pending_actions.emplace_back(enemy, action.name, action.cost, host_state);
}
}
}
if(pending_actions.size() > 0) {
std::sort(pending_actions.begin(), pending_actions.end(),
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 a.cost > b.cost;
});
}
@ -38,35 +45,35 @@ namespace combat {
}
std::optional<BattleResult> BattleEngine::next() {
if(pending_actions.size() == 0) return std::nullopt;
if($pending_actions.size() == 0) return std::nullopt;
auto ba = pending_actions.back();
pending_actions.pop_back();
auto ba = $pending_actions.back();
$pending_actions.pop_back();
return std::make_optional(ba);
}
void BattleEngine::dump() {
for(auto& [entity, enemy] : combatants) {
for(auto& [entity, enemy] : $combatants) {
fmt::println("\n\n###### ENTITY #{}", entity);
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);
dbc::check($combatants.contains(entity), "invalid combatant given to BattleEngine");
auto& action = $combatants.at(entity);
action.ai->set_state(state, setting);
}
void BattleEngine::set_all(const std::string& state, bool setting) {
for(auto& [ent, action] : combatants) {
for(auto& [ent, action] : $combatants) {
action.ai->set_state(state, setting);
}
}
Combatant& BattleEngine::get_enemy(DinkyECS::Entity entity) {
dbc::check(combatants.contains(entity), "invalid combatant given to BattleEngine");
dbc::check($combatants.contains(entity), "invalid combatant given to BattleEngine");
return combatants.at(entity);
return $combatants.at(entity);
}
}