raycaster/battle.cpp

79 lines
2.2 KiB
C++

#include "rituals.hpp"
#include "battle.hpp"
namespace combat {
void BattleEngine::add_enemy(Combatant 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) {
enemy.ai->update();
active += enemy.ai->active();
if(enemy.ai->active()) {
for(auto& action : enemy.ai->plan.script) {
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(),
[](const auto& a, const auto& b) -> bool
{
return a.cost > b.cost;
});
}
return active > 0;
}
std::optional<BattleResult> BattleEngine::next() {
if($pending_actions.size() == 0) return std::nullopt;
auto ba = $pending_actions.back();
$pending_actions.pop_back();
return std::make_optional(ba);
}
void BattleEngine::dump() {
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);
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);
}
}
Combatant& BattleEngine::get_enemy(DinkyECS::Entity entity) {
dbc::check($combatants.contains(entity), "invalid combatant given to BattleEngine");
return $combatants.at(entity);
}
}