Basic AP (Action Points) system tied to the AI actions, but there's no way to set 'has AP' for the AI?

This commit is contained in:
Zed A. Shaw 2025-12-03 15:24:41 -05:00
parent c78b2ae75e
commit a38bb5b691
8 changed files with 64 additions and 18 deletions

View file

@ -62,7 +62,7 @@
}
]
},
{"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 20, "dead": false},
{"_type": "Combat", "hp": 200, "max_hp": 200, "ap": 0, "ap_delta": 9, "max_ap": 20, "damage": 20, "dead": false},
{"_type": "Sound", "attack": "Marmot_Scream_1", "death": "Creature_Death_1"},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"}
]

View file

@ -6,7 +6,7 @@
"foreground": "enemies/fg:player",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 200, "max_hp": 200, "damage": 10, "dead": false},
{"_type": "Combat", "hp": 200, "max_hp": 200, "ap": 0, "max_ap": 12, "ap_delta": 6, "damage": 10, "dead": false},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "Collision", "has": true},
{"_type": "LightSource", "strength": 35, "radius": 2.0}
@ -18,7 +18,7 @@
"foreground": "enemies/fg:gold_savior",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false},
{"_type": "Combat", "hp": 20, "max_hp": 20, "ap": 0, "max_ap": 12, "ap_delta": 6, "damage": 1, "dead": false},
{"_type": "Collision", "has": true},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"},
@ -33,7 +33,7 @@
"foreground": "enemies/fg:knight",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 1, "dead": false},
{"_type": "Combat", "hp": 20, "max_hp": 20, "ap": 0, "max_ap": 12, "ap_delta": 6,"damage": 1, "dead": false},
{"_type": "Collision", "has": true},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"},
@ -48,7 +48,7 @@
"foreground": "enemies/fg:axe_ranger",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 40, "max_hp": 40, "damage": 10, "dead": false},
{"_type": "Combat", "hp": 40, "max_hp": 40, "ap": 0, "max_ap": 12, "ap_delta": 6,"damage": 10, "dead": false},
{"_type": "Collision", "has": true},
{"_type": "Motion", "dx": 0, "dy": 0, "random": true},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"},
@ -63,7 +63,7 @@
"foreground": "enemies/fg:rat_giant",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 50, "max_hp": 50, "damage": 2, "dead": false},
{"_type": "Combat", "hp": 50, "max_hp": 50, "ap": 0, "max_ap": 12, "ap_delta": 6,"damage": 2, "dead": false},
{"_type": "Collision", "has": true},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"},
@ -78,7 +78,7 @@
"foreground": "enemies/fg:spider_giant",
"background": "color:transparent"
},
{"_type": "Combat", "hp": 20, "max_hp": 20, "damage": 20, "dead": false},
{"_type": "Combat", "hp": 20, "max_hp": 20, "ap": 0, "max_ap": 12, "ap_delta": 6,"damage": 20, "dead": false},
{"_type": "Collision", "has": true},
{"_type": "Motion", "dx": 0, "dy": 0, "random": false},
{"_type": "EnemyConfig", "ai_script": "Enemy::actions", "ai_start_name": "Enemy::initial_state", "ai_goal_name": "Enemy::final_state"},

View file

@ -16,7 +16,16 @@ namespace combat {
int active = 0;
fmt::println("---------- start combatants");
for(auto& [entity, enemy] : $combatants) {
if(enemy.combat->ap < enemy.combat->max_ap) {
// only add up to the max
enemy.combat->ap = std::min(enemy.combat->max_ap, enemy.combat->ap_delta + enemy.combat->ap);
}
fmt::println("--- enemy {} has {} ap", entity, enemy.combat->ap);
// reset action points
enemy.ai->update();
active += enemy.ai->active();
@ -24,15 +33,38 @@ namespace combat {
for(auto& action : enemy.ai->plan.script) {
BattleHostState host_state = not_host;
if(enemy.is_host) {
if(action.cost > enemy.combat->ap) {
host_state = out_of_ap;
} else if(enemy.is_host) {
host_state = $player_requests.contains(action.name) ? agree : disagree;
}
if(host_state == out_of_ap) {
fmt::println("--- enemy CANNOT go: {}-{}={}",
enemy.combat->ap, action.cost,
enemy.combat->ap - action.cost);
break;
} else {
fmt::println("--- enemy can go, {}-{}={}",
enemy.combat->ap, action.cost,
enemy.combat->ap - action.cost);
enemy.combat->ap -= action.cost;
}
fmt::println("--- active enemy {} ap={}, host_state={}",
entity, enemy.combat->ap, int(host_state));
$pending_actions.emplace_back(enemy, action.name, action.cost, host_state);
}
dbc::check(enemy.combat->ap >= 0, "enemy's AP went below 0");
dbc::check(enemy.combat->ap <= enemy.combat->max_ap, "enemy's AP went above max");
}
}
fmt::print("<---- end of enemy setup, sorting");
if($pending_actions.size() > 0) {
std::sort($pending_actions.begin(), $pending_actions.end(),
[](const auto& a, const auto& b) -> bool

View file

@ -11,7 +11,8 @@ namespace combat {
enum class BattleHostState {
not_host = 0,
agree = 1,
disagree = 2
disagree = 2,
out_of_ap = 3
};
struct Combatant {

View file

@ -89,6 +89,10 @@ namespace boss {
if(wants_to == "kill_enemy") {
result.enemy_did = enemy.combat->attack(player_combat);
}
break;
case combat::BattleHostState::out_of_ap:
fmt::println("OUT OF AP {}", wants_to);
break;
}
if(result.player_did > 0) {

View file

@ -102,6 +102,11 @@ namespace components {
int hp;
int max_hp;
int damage;
int ap_delta;
int max_ap;
// everyone starts at 0 but ap_delta is added each round
int ap = 0;
/* NOTE: This is used to _mark_ entities as dead, to detect ones that have just died. Don't make attack automatically set it.*/
bool dead = false;
@ -176,7 +181,7 @@ namespace components {
ENROLL_COMPONENT(EnemyConfig, ai_script, ai_start_name, ai_goal_name);
ENROLL_COMPONENT(Personality, hearing_distance, tough);
ENROLL_COMPONENT(Motion, dx, dy, random);
ENROLL_COMPONENT(Combat, hp, max_hp, damage, dead);
ENROLL_COMPONENT(Combat, hp, max_hp, damage, ap_delta, max_ap, dead);
ENROLL_COMPONENT(Device, config, events);
ENROLL_COMPONENT(Storyboard, image, audio, layout, beats);
ENROLL_COMPONENT(Animation, min_x, min_y,

View file

@ -250,6 +250,7 @@ void System::combat(int attack_id) {
battle.set_all("enemy_found", true);
battle.set_all("in_combat", true);
battle.player_request("kill_enemy");
battle.plan();
}

View file

@ -20,17 +20,17 @@ TEST_CASE("battle operations fantasy", "[combat-battle]") {
DinkyECS::Entity host = 0;
ai::EntityAI host_ai("Host::actions", host_start, host_goal);
components::Combat host_combat{100, 100, 20};
components::Combat host_combat{100, 100, 20, 6, 12};
battle.add_enemy({host, &host_ai, &host_combat, true});
DinkyECS::Entity axe_ranger = 1;
ai::EntityAI axe_ai("Enemy::actions", ai_start, ai_goal);
components::Combat axe_combat{100, 100, 20};
components::Combat axe_combat{100, 100, 20, 8, 12};
battle.add_enemy({axe_ranger, &axe_ai, &axe_combat});
DinkyECS::Entity rat = 2;
ai::EntityAI rat_ai("Enemy::actions", ai_start, ai_goal);
components::Combat rat_combat{10, 10, 2};
components::Combat rat_combat{10, 10, 2, 12, 18};
battle.add_enemy({rat, &rat_ai, &rat_combat});
battle.set_all("enemy_found", true);
@ -42,7 +42,6 @@ TEST_CASE("battle operations fantasy", "[combat-battle]") {
battle.set(host, "have_healing", false);
battle.set(host, "tough_personality", false);
while(host_combat.hp > 0) {
battle.set(host, "health_good", host_combat.hp > 20);
@ -52,14 +51,14 @@ TEST_CASE("battle operations fantasy", "[combat-battle]") {
battle.plan();
while(auto act = battle.next()) {
auto& [enemy, wants_to, cost, host_behavior] = *act;
auto& [enemy, wants_to, cost, enemy_state] = *act;
fmt::println(">>>>> entity: {} wants to {} cost={} and has {} HP and {} damage",
fmt::println(">>>>> entity: {} wants to {} cost={}; has {} HP; {} ap",
enemy.entity, wants_to,
cost, enemy.combat->hp,
enemy.combat->damage);
enemy.combat->ap);
switch(host_behavior) {
switch(enemy_state) {
case BattleHostState::agree:
fmt::println("HOST and PLAYER requests match {}, doing it.",
wants_to);
@ -72,6 +71,10 @@ TEST_CASE("battle operations fantasy", "[combat-battle]") {
if(wants_to == "kill_enemy") {
enemy.combat->attack(host_combat);
}
break;
case BattleHostState::out_of_ap:
fmt::println("ENEMY OUT OF AP");
break;
}
fmt::println("<<<<<<<<<<<<<<<<");