Rework the source so that battle is in its own thing to work on.

master
Zed A. Shaw 10 months ago
parent e6a8a8b338
commit ca328e10dc
  1. 39
      battle.cpp
  2. 25
      battle.hpp
  3. 5
      meson.build
  4. 36
      rituals.cpp
  5. 19
      rituals.hpp
  6. 1
      systems.cpp
  7. 1
      tests/battle.cpp

@ -0,0 +1,39 @@
#include "rituals.hpp"
#include "battle.hpp"
namespace combat {
void BattleEngine::add_enemy(BattleAction enemy) {
combatants.try_emplace(enemy.entity, enemy);
}
bool BattleEngine::plan() {
int active = 0;
for(auto& [entity, enemy] : combatants) {
enemy.ai.set_state("enemy_found", true);
enemy.ai.set_state("in_combat", true);
enemy.ai.update();
active += enemy.ai.active();
// yes, copy it out of the combatants list
pending_actions.push_back(enemy);
}
return active > 0;
}
std::optional<BattleAction> 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();
}
}
}

@ -0,0 +1,25 @@
#pragma once
#include "rituals.hpp"
#include "config.hpp"
#include "dinkyecs.hpp"
#include <optional>
#include "components.hpp"
namespace combat {
struct BattleAction {
DinkyECS::Entity entity;
ai::EntityAI &ai;
components::Combat &combat;
};
struct BattleEngine {
std::unordered_map<DinkyECS::Entity, BattleAction> combatants;
std::vector<BattleAction> pending_actions;
void add_enemy(BattleAction ba);
bool plan();
std::optional<BattleAction> next();
void dump();
};
}

@ -84,6 +84,7 @@ sources = [
'animation.cpp', 'animation.cpp',
'animation.cpp', 'animation.cpp',
'autowalker.cpp', 'autowalker.cpp',
'battle.cpp',
'boss_fight_ui.cpp', 'boss_fight_ui.cpp',
'camera.cpp', 'camera.cpp',
'combat.cpp', 'combat.cpp',
@ -126,7 +127,9 @@ sources = [
executable('runtests', sources + [ executable('runtests', sources + [
'tests/ai.cpp', 'tests/ai.cpp',
'tests/animation.cpp',
'tests/base.cpp', 'tests/base.cpp',
'tests/battle.cpp',
'tests/components.cpp', 'tests/components.cpp',
'tests/config.cpp', 'tests/config.cpp',
'tests/dbc.cpp', 'tests/dbc.cpp',
@ -142,10 +145,8 @@ executable('runtests', sources + [
'tests/matrix.cpp', 'tests/matrix.cpp',
'tests/pathing.cpp', 'tests/pathing.cpp',
'tests/rituals.cpp', 'tests/rituals.cpp',
'tests/combat.cpp',
'tests/sound.cpp', 'tests/sound.cpp',
'tests/spatialmap.cpp', 'tests/spatialmap.cpp',
'tests/animation.cpp',
'tests/stats.cpp', 'tests/stats.cpp',
'tests/textures.cpp', 'tests/textures.cpp',
'tests/tilemap.cpp', 'tests/tilemap.cpp',

@ -3,42 +3,6 @@
#include "ai.hpp" #include "ai.hpp"
namespace combat { namespace combat {
void BattleEngine::add_enemy(BattleAction enemy) {
combatants.try_emplace(enemy.entity, enemy);
}
bool BattleEngine::plan() {
int active = 0;
for(auto& [entity, enemy] : combatants) {
enemy.ai.set_state("enemy_found", true);
enemy.ai.set_state("in_combat", true);
enemy.ai.update();
active += enemy.ai.active();
// yes, copy it out of the combatants list
pending_actions.push_back(enemy);
}
return active > 0;
}
std::optional<BattleAction> 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();
}
}
RitualEngine::RitualEngine(std::string config_path) : RitualEngine::RitualEngine(std::string config_path) :
$config(config_path) $config(config_path)
{ {

@ -2,29 +2,10 @@
#include "goap.hpp" #include "goap.hpp"
#include "ai.hpp" #include "ai.hpp"
#include "config.hpp" #include "config.hpp"
#include <functional>
#include "dinkyecs.hpp" #include "dinkyecs.hpp"
#include <optional>
#include "components.hpp" #include "components.hpp"
namespace combat { namespace combat {
struct BattleAction {
DinkyECS::Entity entity;
ai::EntityAI &ai;
components::Combat &combat;
};
struct BattleEngine {
std::unordered_map<DinkyECS::Entity, BattleAction> combatants;
std::vector<BattleAction> pending_actions;
void add_enemy(BattleAction ba);
bool plan();
std::optional<BattleAction> next();
void dump();
};
struct RitualAI { struct RitualAI {
std::string script; std::string script;
ai::State start; ai::State start;

@ -13,6 +13,7 @@
#include "ai_debug.hpp" #include "ai_debug.hpp"
#include "shiterator.hpp" #include "shiterator.hpp"
#include "rituals.hpp" #include "rituals.hpp"
#include "battle.hpp"
#include <iostream> #include <iostream>
using std::string; using std::string;

@ -1,6 +1,7 @@
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <iostream> #include <iostream>
#include "rituals.hpp" #include "rituals.hpp"
#include "battle.hpp"
#include "fsm.hpp" #include "fsm.hpp"
#include "dinkyecs.hpp" #include "dinkyecs.hpp"