From 1324a320e24e9af3bb9270816b0d32ca373f741a Mon Sep 17 00:00:00 2001 From: mlangkabel Date: Tue, 16 Jun 2026 22:17:09 +0200 Subject: [PATCH] fix issue where ships cancel their attacks and advance if on low health in balancing target --- src/lib/ecs/component/BehaviorScores.h | 4 ++-- src/lib/ecs/system/ai/AttackEvaluator.cpp | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/lib/ecs/component/BehaviorScores.h b/src/lib/ecs/component/BehaviorScores.h index 09d3f7f..cfeb2ba 100644 --- a/src/lib/ecs/component/BehaviorScores.h +++ b/src/lib/ecs/component/BehaviorScores.h @@ -16,7 +16,7 @@ namespace BehaviorScores constexpr float kAttack = 0.60f; // healthy and target in sensor range constexpr float kRetreat = 0.90f; - // Health fraction at/below which a ship is considered "low HP" — used by the - // Attack evaluator (do not attack when low) and the Retreat evaluator. + // Health fraction below which a ship is considered "low HP" — used by the + // Retreat evaluator to trigger retreat (which outscores attack). constexpr float kLowHpFraction = 0.3f; } diff --git a/src/lib/ecs/system/ai/AttackEvaluator.cpp b/src/lib/ecs/system/ai/AttackEvaluator.cpp index af8f1de..ef6dc93 100644 --- a/src/lib/ecs/system/ai/AttackEvaluator.cpp +++ b/src/lib/ecs/system/ai/AttackEvaluator.cpp @@ -11,7 +11,6 @@ #include "BehaviorTargeting.h" #include "EntityAdmin.h" #include "FactionComponent.h" -#include "HealthComponent.h" #include "ModuleOwnerComponent.h" #include "PositionComponent.h" #include "SensorRangeComponent.h" @@ -55,10 +54,9 @@ void AttackEvaluator::evaluate(EntityAdmin& admin) // Pass C: per-ship target selection. admin.forEach( + SensorRangeComponent>( [&](entt::entity e, AttackBehavior& attack, const PositionComponent& pos, - const FactionComponent& faction, const SensorRangeComponent& sensor, - const HealthComponent& health) + const FactionComponent& faction, const SensorRangeComponent& sensor) { const float sensorRange_tiles = sensor.value_tiles; @@ -135,13 +133,14 @@ void AttackEvaluator::evaluate(EntityAdmin& admin) } } - if (!keptCurrent) { attack.currentTarget = bestTarget; } + if (!keptCurrent) + { + attack.currentTarget = bestTarget; + } - const bool healthy = - (health.maxHp > 0.0f) - && (health.hp / health.maxHp >= BehaviorScores::kLowHpFraction); - attack.score = (healthy && attack.currentTarget) + attack.score = attack.currentTarget ? BehaviorScores::kAttack : BehaviorScores::kInactive; }); } +