refactor AI system

This commit is contained in:
2026-06-15 09:16:56 +02:00
parent 8451f5a281
commit e8dd73bcb0
67 changed files with 1731 additions and 919 deletions

View File

@@ -1,587 +1,82 @@
#include "AiSystem.h"
#include <optional>
#include <unordered_map>
#include <vector>
#include <limits>
#include <QVector2D>
#include "Building.h"
#include "BuildingSystem.h"
#include "BuildingType.h"
#include "BuildingId.h"
#include "AdvanceBehavior.h"
#include "AttackBehavior.h"
#include "BehaviorKind.h"
#include "DeliverScrapBehavior.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
#include "HealthComponent.h"
#include "HomeReturnBehaviorComponent.h"
#include "HqProxyComponent.h"
#include "ModuleOwnerComponent.h"
#include "MovementIntentComponent.h"
#include "PositionComponent.h"
#include "RallyBehaviorComponent.h"
#include "RepairBehaviorComponent.h"
#include "RepairToolComponent.h"
#include "SalvageBehaviorComponent.h"
#include "SalvageCargoComponent.h"
#include "ScrapSystem.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h"
#include "StationBodyComponent.h"
#include "ThreatResponseBehaviorComponent.h"
#include "RallyBehavior.h"
#include "RepairBehavior.h"
#include "RetreatBehavior.h"
#include "SalvageScrapBehavior.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
// ---------------------------------------------------------------------------
// Shared helpers for repair targeting
// ---------------------------------------------------------------------------
struct RepairableInfo
namespace
{
entt::entity entity;
QVector2D position;
bool isEnemy;
bool isShip;
float hp;
float maxHp;
};
static std::vector<RepairableInfo> buildRepairables(EntityAdmin& admin)
{
std::vector<RepairableInfo> repairables;
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent, HealthComponent>(
[&repairables](entt::entity e, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& f,
const HealthComponent& h)
{
repairables.push_back({e, pos.value, f.isEnemy, true, h.hp, h.maxHp});
});
admin.forEach<StationBodyComponent, PositionComponent, FactionComponent, HealthComponent>(
[&repairables](entt::entity e, const StationBodyComponent& /*sb*/,
const PositionComponent& pos, const FactionComponent& f,
const HealthComponent& h)
{
repairables.push_back({e, pos.value, f.isEnemy, false, h.hp, h.maxHp});
});
return repairables;
}
// ---------------------------------------------------------------------------
// tickHomeReturnBehavior (priority 4)
// ---------------------------------------------------------------------------
void AiSystem::tickHomeReturnBehavior(EntityAdmin& admin)
{
TRACE();
admin.forEach<HomeReturnBehaviorComponent, HealthComponent, MovementIntentComponent>(
[](entt::entity /*e*/, const HomeReturnBehaviorComponent& homeReturnBehavior,
const HealthComponent& h, MovementIntentComponent& intent)
{
if (h.hp / h.maxHp < homeReturnBehavior.retreatHpFraction)
{
if (4 > intent.priority)
{
intent = MovementIntentComponent{4, homeReturnBehavior.homePos};
}
}
});
}
// ---------------------------------------------------------------------------
// tickThreatResponseBehavior (priority 3)
// ---------------------------------------------------------------------------
void AiSystem::tickThreatResponseBehavior(EntityAdmin& admin, const BuildingSystem& buildings)
{
TRACE();
// Snapshot all combatant entities for target acquisition.
struct CombatantInfo
// Records a behavior's score for its owning ship, keeping the highest seen.
// Considered high-priority first, so strict '>' breaks ties toward priority.
template <typename Behavior>
void consider(EntityAdmin& admin, BehaviorKind kind)
{
entt::entity entity;
QVector2D position;
bool isEnemy;
bool isStation;
};
std::vector<CombatantInfo> combatants;
admin.forEach<PositionComponent, FactionComponent, ShipIdentityComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const ShipIdentityComponent& /*si*/)
{
combatants.push_back({e, pos.value, f.isEnemy, false});
});
admin.forEach<PositionComponent, FactionComponent, StationBodyComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const StationBodyComponent& /*sb*/)
{
combatants.push_back({e, pos.value, f.isEnemy, true});
});
admin.forEach<PositionComponent, FactionComponent, HqProxyComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const HqProxyComponent& /*hq*/)
{
combatants.push_back({e, pos.value, f.isEnemy, true});
});
admin.forEach<ThreatResponseBehaviorComponent, PositionComponent, FactionComponent,
SensorRangeComponent, MovementIntentComponent>(
[&](entt::entity e, ThreatResponseBehaviorComponent& threatResponseBehavior,
PositionComponent& pos, FactionComponent& faction,
SensorRangeComponent& sensor, MovementIntentComponent& intent)
{
const float range = sensor.value_tiles;
// Validate current target.
bool targetValid = false;
if (threatResponseBehavior.currentTarget)
admin.forEach<Behavior, SelectedBehaviorComponent>(
[kind](entt::entity /*e*/, const Behavior& behavior,
SelectedBehaviorComponent& selected)
{
const entt::entity t = *threatResponseBehavior.currentTarget;
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
if (behavior.score > selected.bestScore)
{
const float dist =
(admin.get<PositionComponent>(t).value - pos.value).length();
if (dist <= range)
{
targetValid = true;
}
selected.bestScore = behavior.score;
selected.winner = kind;
}
}
if (!targetValid)
{
threatResponseBehavior.currentTarget = std::nullopt;
float bestDist = range;
for (const CombatantInfo& c : combatants)
{
if (c.entity == e) { continue; }
bool isValidTarget = false;
if (!faction.isEnemy)
{
isValidTarget = c.isEnemy;
}
else
{
isValidTarget = !c.isEnemy;
}
if (!isValidTarget) { continue; }
const float dist = (c.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
threatResponseBehavior.currentTarget = c.entity;
}
}
}
if (threatResponseBehavior.currentTarget)
{
const entt::entity t = *threatResponseBehavior.currentTarget;
QVector2D dest = pos.value;
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
{
dest = admin.get<PositionComponent>(t).value;
}
if (3 > intent.priority)
{
intent = MovementIntentComponent{3, dest};
}
}
else
{
if (3 > intent.priority)
{
if (admin.hasAll<RallyBehaviorComponent>(e))
{
intent = MovementIntentComponent{
3, admin.get<RallyBehaviorComponent>(e).rallyPoint};
}
else if (!faction.isEnemy)
{
intent = MovementIntentComponent{
3, QVector2D(pos.value.x() + 1000.0f, pos.value.y())};
}
else
{
intent = MovementIntentComponent{
3, QVector2D(-10000.0f, pos.value.y())};
}
}
}
});
});
}
}
// ---------------------------------------------------------------------------
// tickRepairBehavior (priority 2)
// ---------------------------------------------------------------------------
void AiSystem::tickRepairBehavior(EntityAdmin& admin, BuildingSystem& buildings)
void AiSystem::tick(EntityAdmin& admin, const BuildingSystem& buildings,
const ScrapSystem& scraps)
{
TRACE();
std::vector<RepairableInfo> repairables = buildRepairables(admin);
TRACE();
// Snapshot enemy ships for threat detection.
struct EnemyInfo
{
QVector2D position;
};
std::vector<EnemyInfo> enemies;
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent>(
[&enemies](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& f)
{
if (f.isEnemy)
{
enemies.push_back({pos.value});
}
});
// Phase 1: evaluators score behaviors and set their target data.
m_advanceEvaluator.evaluate(admin);
m_rallyEvaluator.evaluate(admin);
m_retreatEvaluator.evaluate(admin);
m_attackEvaluator.evaluate(admin);
m_repairEvaluator.evaluate(admin);
m_salvageScrapEvaluator.evaluate(admin, scraps);
m_deliverScrapEvaluator.evaluate(admin, buildings);
admin.forEach<RepairBehaviorComponent, PositionComponent,
FactionComponent, SensorRangeComponent, MovementIntentComponent>(
[&](entt::entity e, RepairBehaviorComponent& rb,
PositionComponent& pos, FactionComponent& /*faction*/,
SensorRangeComponent& sensor, MovementIntentComponent& intent)
{
// Flee if enemy nearby.
bool enemyNearby = false;
for (const EnemyInfo& enemy : enemies)
{
if ((enemy.position - pos.value).length() <= sensor.value_tiles)
{
enemyNearby = true;
break;
}
}
if (enemyNearby)
{
if (2 > intent.priority)
{
intent = MovementIntentComponent{
2, QVector2D(-10000.0f, pos.value.y())};
}
return;
}
// Phase 2: pick the highest-scoring behavior per ship.
selectWinningBehaviors(admin);
// Validate current target.
bool targetValid = false;
if (rb.currentTarget)
{
const entt::entity t = *rb.currentTarget;
if (admin.isValid(t) && admin.hasAll<HealthComponent>(t))
{
const HealthComponent& th = admin.get<HealthComponent>(t);
if (th.hp > 0.0f && th.hp < th.maxHp)
{
targetValid = true;
}
}
}
if (!targetValid)
{
rb.currentTarget = std::nullopt;
float bestDist = sensor.value_tiles;
for (const RepairableInfo& r : repairables)
{
if (r.entity == e) { continue; }
if (r.isEnemy) { continue; }
if (r.hp >= r.maxHp) { continue; }
const float dist = (r.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
rb.currentTarget = r.entity;
}
}
}
if (!rb.currentTarget)
{
if (2 > intent.priority)
{
intent = MovementIntentComponent{
2, QVector2D(pos.value.x() + 1000.0f, pos.value.y())};
}
return;
}
const entt::entity target = *rb.currentTarget;
QVector2D targetPos = pos.value;
if (admin.isValid(target) && admin.hasAll<PositionComponent>(target))
{
targetPos = admin.get<PositionComponent>(target).value;
}
if (2 > intent.priority)
{
intent = MovementIntentComponent{2, targetPos};
}
});
// Phase 3: executors run for the winning behavior.
m_advanceExecutor.execute(admin);
m_rallyExecutor.execute(admin);
m_retreatExecutor.execute(admin);
m_attackExecutor.execute(admin);
m_repairExecutor.execute(admin);
m_salvageScrapExecutor.execute(admin);
m_deliverScrapExecutor.execute(admin, buildings);
}
// ---------------------------------------------------------------------------
// tickRepairTools
// ---------------------------------------------------------------------------
void AiSystem::tickRepairTools(EntityAdmin& admin)
void AiSystem::selectWinningBehaviors(EntityAdmin& admin)
{
TRACE();
const std::vector<RepairableInfo> repairables = buildRepairables(admin);
admin.forEach<RepairToolComponent, ModuleOwnerComponent>(
[&](entt::entity /*e*/, RepairToolComponent& rt, const ModuleOwnerComponent& owner)
TRACE();
admin.forEach<SelectedBehaviorComponent>(
[](entt::entity /*e*/, SelectedBehaviorComponent& selected)
{
if (!admin.hasAll<RepairBehaviorComponent>(owner.owner)) { return; }
const RepairBehaviorComponent& rb =
admin.get<RepairBehaviorComponent>(owner.owner);
const PositionComponent& ownerPos =
admin.get<PositionComponent>(owner.owner);
// Try the ship's preferred nav target first.
if (rb.currentTarget)
{
const entt::entity preferred = *rb.currentTarget;
if (admin.isValid(preferred) && admin.hasAll<HealthComponent>(preferred)
&& admin.hasAll<PositionComponent>(preferred))
{
HealthComponent& th = admin.get<HealthComponent>(preferred);
const float dist =
(admin.get<PositionComponent>(preferred).value
- ownerPos.value).length();
if (th.hp > 0.0f && th.hp < th.maxHp && dist <= rt.range_tiles)
{
rt.currentTarget = rb.currentTarget;
th.hp = std::min(th.hp + rt.ratePerTick, th.maxHp);
return;
}
}
}
// Preferred target unavailable; scan for nearest damaged friendly in range.
rt.currentTarget = std::nullopt;
float bestDist = rt.range_tiles;
for (const RepairableInfo& r : repairables)
{
if (r.isEnemy) { continue; }
if (r.hp <= 0.0f || r.hp >= r.maxHp) { continue; }
const float dist = (r.position - ownerPos.value).length();
if (dist < bestDist)
{
bestDist = dist;
rt.currentTarget = r.entity;
}
}
if (!rt.currentTarget) { return; }
HealthComponent& targetHealth =
admin.get<HealthComponent>(*rt.currentTarget);
targetHealth.hp = std::min(targetHealth.hp + rt.ratePerTick, targetHealth.maxHp);
});
}
// ---------------------------------------------------------------------------
// tickSalvageBehavior (priority 1)
// ---------------------------------------------------------------------------
void AiSystem::tickSalvageBehavior(EntityAdmin& admin, ScrapSystem& scraps,
BuildingSystem& buildings)
{
TRACE();
// Snapshot enemy ships for threat detection.
struct EnemyShipPos
{
QVector2D position;
};
std::vector<EnemyShipPos> enemyShips;
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent>(
[&enemyShips](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& f)
{
if (f.isEnemy)
{
enemyShips.push_back({pos.value});
}
});
// Aggregate cargo across all salvage-module children per owning ship.
struct AggregatedCargo
{
int totalCurrent = 0;
int totalCapacity = 0;
};
std::unordered_map<entt::entity, AggregatedCargo> cargoByShip;
admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&](entt::entity /*ce*/, const SalvageCargoComponent& c, const ModuleOwnerComponent& o)
{
AggregatedCargo& agg = cargoByShip[o.owner];
agg.totalCurrent += c.current;
agg.totalCapacity += c.capacity;
});
const std::vector<ScrapInfo> allScrap = scraps.allScrapInfo();
// Tick down per-module collection cooldowns.
admin.forEach<SalvageCargoComponent>(
[](entt::entity /*e*/, SalvageCargoComponent& c)
{
if (c.cooldownTicksRemaining > 0) { --c.cooldownTicksRemaining; }
});
admin.forEach<SalvageBehaviorComponent, PositionComponent,
SensorRangeComponent, MovementIntentComponent>(
[&](entt::entity e, SalvageBehaviorComponent& salvageBehavior,
PositionComponent& pos,
SensorRangeComponent& sensor, MovementIntentComponent& intent)
{
const float collectRange = salvageBehavior.maxCollectionRange_tiles;
const AggregatedCargo& cargoState = cargoByShip[e];
// Assign nearest SalvageBay if needed.
if (salvageBehavior.deliveryBay == kInvalidBuildingId)
{
const Building* bay = buildings.findNearestBuilding(pos.value,
BuildingType::SalvageBay);
if (bay)
{
salvageBehavior.deliveryBay = bay->id;
}
}
const BuildingId bayId = salvageBehavior.deliveryBay;
QVector2D bayPos = pos.value;
if (bayId != kInvalidBuildingId)
{
const Building* bay = buildings.findBuilding(bayId);
if (bay)
{
bayPos = QVector2D(bay->anchor.x() + bay->footprint.width() / 2.0f,
bay->anchor.y() + bay->footprint.height() / 2.0f);
}
}
const bool cargoFull = (cargoState.totalCurrent >= cargoState.totalCapacity
&& cargoState.totalCapacity > 0);
if (cargoFull)
{
if (1 > intent.priority)
{
intent = MovementIntentComponent{1, bayPos};
}
if (bayId != kInvalidBuildingId
&& (pos.value - bayPos).length() <= 1.0f)
{
// Decrement first non-empty salvage child.
bool delivered = false;
admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&](entt::entity /*ce*/, SalvageCargoComponent& c,
const ModuleOwnerComponent& o)
{
if (delivered || o.owner != e || c.current <= 0) { return; }
if (buildings.deliverScrapToSalvageBay(bayId))
{
--c.current;
delivered = true;
}
});
}
return;
}
// Retreat if enemy near and cargo empty.
bool retreating = false;
if (cargoState.totalCurrent == 0)
{
for (const EnemyShipPos& enemy : enemyShips)
{
if ((enemy.position - pos.value).length() <= collectRange)
{
if (1 > intent.priority)
{
intent = MovementIntentComponent{
1, QVector2D(-10000.0f, pos.value.y())};
}
retreating = true;
break;
}
}
}
if (retreating) { return; }
// Per-module independent collection: each ready module collects one scrap.
bool anythingCollected = false;
admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&](entt::entity /*ce*/, SalvageCargoComponent& c,
const ModuleOwnerComponent& o)
{
if (o.owner != e || c.current >= c.capacity
|| c.cooldownTicksRemaining > 0)
{
return;
}
for (const ScrapInfo& si : allScrap)
{
if ((si.position - pos.value).length() > c.collectionRange_tiles) { continue; }
if (scraps.consume(si.entity))
{
++c.current;
c.cooldownTicksRemaining = c.collectionIntervalTicks;
anythingCollected = true;
break;
}
}
});
if (anythingCollected)
{
salvageBehavior.scrapTarget = std::nullopt;
}
// Move toward scrap target or find a new one.
if (salvageBehavior.scrapTarget)
{
if (1 > intent.priority)
{
intent = MovementIntentComponent{1, *salvageBehavior.scrapTarget};
}
}
else
{
float bestDist = sensor.value_tiles;
std::optional<QVector2D> bestPos;
for (const ScrapInfo& si : allScrap)
{
const float dist = (si.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
bestPos = si.position;
}
}
if (bestPos)
{
salvageBehavior.scrapTarget = bestPos;
if (1 > intent.priority)
{
intent = MovementIntentComponent{1, *bestPos};
}
}
else
{
if (1 > intent.priority)
{
intent = MovementIntentComponent{
1, QVector2D(pos.value.x() + 1000.0f, pos.value.y())};
}
}
}
selected.winner = BehaviorKind::None;
selected.bestScore = std::numeric_limits<float>::lowest();
});
// Highest priority first so ties resolve toward the more urgent behavior.
consider<RetreatBehavior>(admin, BehaviorKind::Retreat);
consider<AttackBehavior>(admin, BehaviorKind::Attack);
consider<RepairBehavior>(admin, BehaviorKind::Repair);
consider<SalvageScrapBehavior>(admin, BehaviorKind::SalvageScrap);
consider<DeliverScrapBehavior>(admin, BehaviorKind::DeliverScrap);
consider<RallyBehavior>(admin, BehaviorKind::Rally);
consider<AdvanceBehavior>(admin, BehaviorKind::Advance);
}

View File

@@ -1,15 +1,52 @@
#pragma once
#include "AdvanceEvaluator.h"
#include "AdvanceExecutor.h"
#include "AttackEvaluator.h"
#include "AttackExecutor.h"
#include "DeliverScrapEvaluator.h"
#include "DeliverScrapExecutor.h"
#include "RallyEvaluator.h"
#include "RallyExecutor.h"
#include "RepairEvaluator.h"
#include "RepairExecutor.h"
#include "RetreatEvaluator.h"
#include "RetreatExecutor.h"
#include "SalvageScrapEvaluator.h"
#include "SalvageScrapExecutor.h"
class BuildingSystem;
class EntityAdmin;
class ScrapSystem;
// Orchestrates ship-behavior decision-making in three batched phases:
// 1. evaluators score each behavior and set its target data,
// 2. selectWinningBehaviors picks the highest-scoring behavior per ship,
// 3. executors run for the winning behavior, setting movement intent and
// preferred module targets.
// All world mutation (collection, healing, damage) is left to the module
// systems (SalvagerSystem, RepairSystem, CombatSystem).
class AiSystem
{
public:
void tickHomeReturnBehavior(EntityAdmin& admin);
void tickThreatResponseBehavior(EntityAdmin& admin, const BuildingSystem& buildings);
void tickRepairBehavior(EntityAdmin& admin, BuildingSystem& buildings);
void tickRepairTools(EntityAdmin& admin);
void tickSalvageBehavior(EntityAdmin& admin, ScrapSystem& scraps, BuildingSystem& buildings);
void tick(EntityAdmin& admin, const BuildingSystem& buildings, const ScrapSystem& scraps);
private:
void selectWinningBehaviors(EntityAdmin& admin);
AdvanceEvaluator m_advanceEvaluator;
RallyEvaluator m_rallyEvaluator;
RetreatEvaluator m_retreatEvaluator;
AttackEvaluator m_attackEvaluator;
RepairEvaluator m_repairEvaluator;
SalvageScrapEvaluator m_salvageScrapEvaluator;
DeliverScrapEvaluator m_deliverScrapEvaluator;
AdvanceExecutor m_advanceExecutor;
RallyExecutor m_rallyExecutor;
RetreatExecutor m_retreatExecutor;
AttackExecutor m_attackExecutor;
RepairExecutor m_repairExecutor;
SalvageScrapExecutor m_salvageScrapExecutor;
DeliverScrapExecutor m_deliverScrapExecutor;
};

View File

@@ -1,9 +1,26 @@
SET(HDRS
${HDRS}
${CMAKE_CURRENT_SOURCE_DIR}/ai/AdvanceEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/AdvanceExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/BehaviorTargeting.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RallyEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RallyExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RepairEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RepairExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RetreatEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/RetreatExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/SalvageScrapEvaluator.h
${CMAKE_CURRENT_SOURCE_DIR}/ai/SalvageScrapExecutor.h
${CMAKE_CURRENT_SOURCE_DIR}/AiSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/CombatSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/DynamicBodySystem.h
${CMAKE_CURRENT_SOURCE_DIR}/MovementIntentSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/RepairSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/SalvagerSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/ScrapSystem.h
${CMAKE_CURRENT_SOURCE_DIR}/ShipSystem.h
PARENT_SCOPE
@@ -11,10 +28,27 @@ SET(HDRS
SET(SRCS
${SRCS}
${CMAKE_CURRENT_SOURCE_DIR}/ai/AdvanceEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/AdvanceExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/AttackExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/BehaviorTargeting.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/DeliverScrapExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RallyEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RallyExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RepairEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RepairExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RetreatEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/RetreatExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/SalvageScrapEvaluator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ai/SalvageScrapExecutor.cpp
${CMAKE_CURRENT_SOURCE_DIR}/AiSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/CombatSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/DynamicBodySystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/MovementIntentSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/RepairSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/SalvagerSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ScrapSystem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ShipSystem.cpp
PARENT_SCOPE
@@ -23,5 +57,6 @@ SET(SRCS
set(LIB_INCLUDE_PATH
${LIB_INCLUDE_PATH}
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/ai
PARENT_SCOPE
)

View File

@@ -7,7 +7,6 @@
#include "PositionComponent.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h"
#include "ThreatResponseBehaviorComponent.h"
#include "tracing.h"
#include "WeaponComponent.h"
@@ -25,14 +24,11 @@ void CombatSystem::tick(Tick currentTick,
{
TRACE();
// All weapons (ships and stations) are child entities linked via ModuleOwnerComponent.
// AttackExecutor has already set each weapon's preferred (in-range) target; here we
// validate it, fall back to nearest-target acquisition, and fire.
admin.forEach<WeaponComponent, ModuleOwnerComponent>(
[&](entt::entity /*e*/, WeaponComponent& weapon, const ModuleOwnerComponent& owner)
{
if (admin.hasAll<ThreatResponseBehaviorComponent>(owner.owner))
{
weapon.currentTarget =
admin.get<ThreatResponseBehaviorComponent>(owner.owner).currentTarget;
}
const PositionComponent& pos = admin.get<PositionComponent>(owner.owner);
const FactionComponent& faction = admin.get<FactionComponent>(owner.owner);
resolveWeapon(owner.owner, weapon, pos, faction, currentTick, admin, outWeaponFiredEvents);

View File

@@ -29,7 +29,7 @@ void MovementIntentSystem::tick(EntityAdmin& admin)
[](entt::entity /*e*/, const PositionComponent& pos, const FacingComponent& facing,
DynamicBodyComponent& body, const MovementIntentComponent& intent)
{
if (intent.priority == 0)
if (!intent.active)
{
// No movement intent: brake using available thrust.
const float linearBraking = std::min(body.velocity_tpt.length(),

View File

@@ -0,0 +1,70 @@
#include "RepairSystem.h"
#include <algorithm>
#include <optional>
#include <vector>
#include <QVector2D>
#include "BehaviorTargeting.h"
#include "EntityAdmin.h"
#include "HealthComponent.h"
#include "ModuleOwnerComponent.h"
#include "PositionComponent.h"
#include "RepairToolComponent.h"
#include "tracing.h"
RepairSystem::RepairSystem(EntityAdmin& admin)
: m_admin(admin)
{
}
void RepairSystem::tick()
{
TRACE();
const std::vector<RepairableInfo> repairables = buildRepairables(m_admin);
m_admin.forEach<RepairToolComponent, ModuleOwnerComponent>(
[&](entt::entity /*re*/, RepairToolComponent& tool, const ModuleOwnerComponent& owner)
{
if (!m_admin.hasAll<PositionComponent>(owner.owner)) { return; }
const QVector2D ownerPos = m_admin.get<PositionComponent>(owner.owner).value;
// Honour the executor-set target if it is still valid and in range.
if (tool.currentTarget)
{
const entt::entity t = *tool.currentTarget;
if (m_admin.isValid(t) && m_admin.hasAll<HealthComponent, PositionComponent>(t))
{
HealthComponent& th = m_admin.get<HealthComponent>(t);
const float dist =
(m_admin.get<PositionComponent>(t).value - ownerPos).length();
if (th.hp > 0.0f && th.hp < th.maxHp && dist <= tool.range_tiles)
{
th.hp = std::min(th.hp + tool.ratePerTick, th.maxHp);
return;
}
}
}
// Fallback: heal the nearest damaged friendly within tool range.
tool.currentTarget = std::nullopt;
float bestDist = tool.range_tiles;
for (const RepairableInfo& r : repairables)
{
if (r.isEnemy) { continue; }
if (r.hp <= 0.0f || r.hp >= r.maxHp) { continue; }
const float dist = (r.position - ownerPos).length();
if (dist < bestDist)
{
bestDist = dist;
tool.currentTarget = r.entity;
}
}
if (!tool.currentTarget) { return; }
HealthComponent& targetHealth = m_admin.get<HealthComponent>(*tool.currentTarget);
targetHealth.hp = std::min(targetHealth.hp + tool.ratePerTick, targetHealth.maxHp);
});
}

View File

@@ -0,0 +1,17 @@
#pragma once
class EntityAdmin;
// World-mutation system for repair modules: validates each tool's target (set by
// RepairExecutor), falls back to the nearest damaged friendly in range, and
// applies healing. Runs every tick, independent of behavior selection.
class RepairSystem
{
public:
explicit RepairSystem(EntityAdmin& admin);
void tick();
private:
EntityAdmin& m_admin;
};

View File

@@ -0,0 +1,79 @@
#include "SalvagerSystem.h"
#include <vector>
#include <QVector2D>
#include "Building.h"
#include "BuildingSystem.h"
#include "DeliverScrapBehavior.h"
#include "EntityAdmin.h"
#include "ModuleOwnerComponent.h"
#include "PositionComponent.h"
#include "SalvageCargoComponent.h"
#include "ScrapSystem.h"
#include "tracing.h"
SalvagerSystem::SalvagerSystem(EntityAdmin& admin)
: m_admin(admin)
{
}
void SalvagerSystem::tick(ScrapSystem& scraps, BuildingSystem& buildings)
{
TRACE();
const std::vector<ScrapInfo> allScrap = scraps.allScrapInfo();
// Tick down per-module collection cooldowns.
m_admin.forEach<SalvageCargoComponent>(
[](entt::entity /*e*/, SalvageCargoComponent& c)
{
if (c.cooldownTicksRemaining > 0) { --c.cooldownTicksRemaining; }
});
// Collection: each ready, in-range module collects one scrap.
m_admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&](entt::entity /*ce*/, SalvageCargoComponent& c, const ModuleOwnerComponent& o)
{
if (c.current >= c.capacity || c.cooldownTicksRemaining > 0) { return; }
if (!m_admin.hasAll<PositionComponent>(o.owner)) { return; }
const QVector2D ownerPos = m_admin.get<PositionComponent>(o.owner).value;
for (const ScrapInfo& si : allScrap)
{
if ((si.position - ownerPos).length() > c.collectionRange_tiles) { continue; }
if (scraps.consume(si.entity))
{
++c.current;
c.cooldownTicksRemaining = c.collectionIntervalTicks;
break;
}
}
});
// Delivery: a ship at its assigned bay hands over one unit of cargo per tick.
m_admin.forEach<DeliverScrapBehavior, PositionComponent>(
[&](entt::entity ship, const DeliverScrapBehavior& deliver, const PositionComponent& pos)
{
if (deliver.deliveryBay == kInvalidBuildingId) { return; }
const Building* bay = buildings.findBuilding(deliver.deliveryBay);
if (!bay) { return; }
const QVector2D bayCenter(bay->anchor.x() + bay->footprint.width() / 2.0f,
bay->anchor.y() + bay->footprint.height() / 2.0f);
if ((pos.value - bayCenter).length() > 1.0f) { return; }
// Decrement the first non-empty salvage child belonging to this ship.
bool delivered = false;
m_admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&](entt::entity /*ce*/, SalvageCargoComponent& c, const ModuleOwnerComponent& o)
{
if (delivered || o.owner != ship || c.current <= 0) { return; }
if (buildings.deliverScrapToSalvageBay(deliver.deliveryBay))
{
--c.current;
delivered = true;
}
});
});
}

View File

@@ -0,0 +1,19 @@
#pragma once
class BuildingSystem;
class EntityAdmin;
class ScrapSystem;
// World-mutation system for salvage modules: collects scrap into cargo and
// delivers full cargo at a SalvageBay. Runs every tick, independent of which
// behavior the AiSystem selected.
class SalvagerSystem
{
public:
explicit SalvagerSystem(EntityAdmin& admin);
void tick(ScrapSystem& scraps, BuildingSystem& buildings);
private:
EntityAdmin& m_admin;
};

View File

@@ -6,6 +6,10 @@
#include <utility>
#include <vector>
#include "AdvanceBehavior.h"
#include "AttackBehavior.h"
#include "BehaviorScores.h"
#include "DeliverScrapBehavior.h"
#include "DynamicBodyComponent.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
@@ -13,14 +17,15 @@
#include "ModuleOwnerComponent.h"
#include "ModulesConfig.h"
#include "MovementIntentComponent.h"
#include "RallyBehaviorComponent.h"
#include "RepairBehaviorComponent.h"
#include "RallyBehavior.h"
#include "RepairBehavior.h"
#include "RepairToolComponent.h"
#include "SalvageBehaviorComponent.h"
#include "RetreatBehavior.h"
#include "SalvageCargoComponent.h"
#include "SalvageScrapBehavior.h"
#include "SelectedBehaviorComponent.h"
#include "SensorRangeComponent.h"
#include "Tick.h"
#include "ThreatResponseBehaviorComponent.h"
#include "tracing.h"
#include "WeaponComponent.h"
@@ -321,15 +326,30 @@ entt::entity ShipSystem::spawn(const std::string& schematicId, int level,
// --- Pass 3: attach behavior components based on capability presence -----
// Baseline: every ship can always fall back to advancing, and needs a slot
// for the per-tick behavior selection result.
m_admin.addComponent<AdvanceBehavior>(entity, AdvanceBehavior{});
m_admin.addComponent<SelectedBehaviorComponent>(entity, SelectedBehaviorComponent{});
// Player ships retreat to the rally point when threatened or badly damaged
// (disabled by the balancing tool to keep arena fights symmetric).
if (!isEnemy && m_retreatEnabled)
{
RetreatBehavior retreat;
retreat.retreatHpFraction = BehaviorScores::kLowHpFraction;
retreat.retreatPoint = m_rallyPoint;
m_admin.addComponent<RetreatBehavior>(entity, retreat);
}
if (!weaponChildren.empty())
{
m_admin.addComponent<ThreatResponseBehaviorComponent>(
entity, ThreatResponseBehaviorComponent{});
m_admin.addComponent<AttackBehavior>(entity, AttackBehavior{});
if (!isEnemy)
{
m_admin.addComponent<RallyBehaviorComponent>(
entity, RallyBehaviorComponent{m_rallyPoint});
RallyBehavior rally;
rally.rallyPoint = m_rallyPoint;
m_admin.addComponent<RallyBehavior>(entity, rally);
}
}
@@ -342,11 +362,14 @@ entt::entity ShipSystem::spawn(const std::string& schematicId, int level,
if (r > maxCollRange) { maxCollRange = r; }
}
SalvageBehaviorComponent sb;
sb.scrapTarget = std::nullopt;
sb.deliveryBay = kInvalidBuildingId;
sb.maxCollectionRange_tiles = maxCollRange;
m_admin.addComponent<SalvageBehaviorComponent>(entity, sb);
SalvageScrapBehavior salvage;
salvage.scrapTarget = std::nullopt;
salvage.maxCollectionRange_tiles = maxCollRange;
m_admin.addComponent<SalvageScrapBehavior>(entity, salvage);
DeliverScrapBehavior deliver;
deliver.deliveryBay = kInvalidBuildingId;
m_admin.addComponent<DeliverScrapBehavior>(entity, deliver);
}
if (!repairChildren.empty())
@@ -358,10 +381,10 @@ entt::entity ShipSystem::spawn(const std::string& schematicId, int level,
if (r > maxRepairRange) { maxRepairRange = r; }
}
RepairBehaviorComponent rb;
rb.currentTarget = std::nullopt;
rb.maxRepairRange_tiles = maxRepairRange;
m_admin.addComponent<RepairBehaviorComponent>(entity, rb);
RepairBehavior repair;
repair.currentTarget = std::nullopt;
repair.maxRepairRange_tiles = maxRepairRange;
m_admin.addComponent<RepairBehavior>(entity, repair);
}
return entity;
@@ -385,7 +408,7 @@ void ShipSystem::clearMovementIntents()
m_admin.forEach<MovementIntentComponent>(
[](entt::entity /*e*/, MovementIntentComponent& i)
{
i = MovementIntentComponent{0, QVector2D(0.0f, 0.0f)};
i = MovementIntentComponent{false, QVector2D(0.0f, 0.0f)};
});
}
@@ -394,12 +417,17 @@ void ShipSystem::setRallyPoint(QVector2D point)
m_rallyPoint = point;
}
void ShipSystem::setRetreatEnabled(bool enabled)
{
m_retreatEnabled = enabled;
}
void ShipSystem::triggerRallyDeparture()
{
TRACE();
std::vector<entt::entity> toRemove;
m_admin.forEach<RallyBehaviorComponent, FactionComponent>(
[&toRemove](entt::entity e, const RallyBehaviorComponent& /*rb*/,
m_admin.forEach<RallyBehavior, FactionComponent>(
[&toRemove](entt::entity e, const RallyBehavior& /*rb*/,
const FactionComponent& f)
{
if (!f.isEnemy)
@@ -409,6 +437,6 @@ void ShipSystem::triggerRallyDeparture()
});
for (entt::entity e : toRemove)
{
m_admin.removeComponent<RallyBehaviorComponent>(e);
m_admin.removeComponent<RallyBehavior>(e);
}
}

View File

@@ -24,7 +24,7 @@ public:
const std::map<std::string, int>& moduleLevelOverrides = {});
void despawn(entt::entity entity);
// Reset all movement intents to priority 0 before behavior systems run.
// Reset all movement intents to inactive before behavior systems run.
void clearMovementIntents();
// Set the rally point that newly spawned player combat ships will loiter at.
@@ -33,6 +33,11 @@ public:
// Release all gathered player combat ships to advance toward the enemy.
void triggerRallyDeparture();
// Controls whether newly spawned player ships receive a RetreatBehavior. The
// balancing tool disables this so arena fights stay symmetric and aggressive
// (REQ-BAL-SIM-AI); the main game keeps it enabled (REQ-SHP-RETREAT).
void setRetreatEnabled(bool enabled);
private:
const ShipDef* findShipDef(const std::string& schematicId) const;
const ModuleDef* findModuleDef(const std::string& id) const;
@@ -40,4 +45,5 @@ private:
const GameConfig& m_config;
EntityAdmin& m_admin;
QVector2D m_rallyPoint;
bool m_retreatEnabled = true;
};

View File

@@ -0,0 +1,16 @@
#include "AdvanceEvaluator.h"
#include "AdvanceBehavior.h"
#include "BehaviorScores.h"
#include "EntityAdmin.h"
#include "tracing.h"
void AdvanceEvaluator::evaluate(EntityAdmin& admin)
{
TRACE();
admin.forEach<AdvanceBehavior>(
[](entt::entity /*e*/, AdvanceBehavior& advance)
{
advance.score = BehaviorScores::kAdvance;
});
}

View File

@@ -0,0 +1,11 @@
#pragma once
class EntityAdmin;
// Baseline fallback: gives every ship a constant low score so there is always a
// winning behavior. The actual movement direction is decided by AdvanceExecutor.
class AdvanceEvaluator
{
public:
void evaluate(EntityAdmin& admin);
};

View File

@@ -0,0 +1,30 @@
#include "AdvanceExecutor.h"
#include <QVector2D>
#include "AdvanceBehavior.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
#include "MovementIntentComponent.h"
#include "PositionComponent.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void AdvanceExecutor::execute(EntityAdmin& admin)
{
TRACE();
admin.forEach<AdvanceBehavior, SelectedBehaviorComponent, PositionComponent,
FactionComponent, MovementIntentComponent>(
[](entt::entity /*e*/, const AdvanceBehavior& /*advance*/,
const SelectedBehaviorComponent& selected, const PositionComponent& pos,
const FactionComponent& faction, MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::Advance) { return; }
const QVector2D target = faction.isEnemy
? QVector2D(-10000.0f, pos.value.y())
: QVector2D(pos.value.x() + 1000.0f, pos.value.y());
intent = MovementIntentComponent{true, target};
});
}

View File

@@ -0,0 +1,11 @@
#pragma once
class EntityAdmin;
// Moves a ship toward the opposing side when Advance is the winning behavior:
// player ships advance toward +x (the enemy), enemy ships toward -x (the base).
class AdvanceExecutor
{
public:
void execute(EntityAdmin& admin);
};

View File

@@ -0,0 +1,71 @@
#include "AttackEvaluator.h"
#include <vector>
#include <QVector2D>
#include "AttackBehavior.h"
#include "BehaviorScores.h"
#include "BehaviorTargeting.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "SensorRangeComponent.h"
#include "tracing.h"
void AttackEvaluator::evaluate(EntityAdmin& admin)
{
TRACE();
const std::vector<CombatantInfo> combatants = buildCombatants(admin);
admin.forEach<AttackBehavior, PositionComponent, FactionComponent,
SensorRangeComponent, HealthComponent>(
[&](entt::entity e, AttackBehavior& attack, const PositionComponent& pos,
const FactionComponent& faction, const SensorRangeComponent& sensor,
const HealthComponent& health)
{
const float range = sensor.value_tiles;
// Validate current target: still valid, still in range.
bool targetValid = false;
if (attack.currentTarget)
{
const entt::entity t = *attack.currentTarget;
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
{
const float dist =
(admin.get<PositionComponent>(t).value - pos.value).length();
if (dist <= range) { targetValid = true; }
}
}
// Acquire nearest valid target if needed.
if (!targetValid)
{
attack.currentTarget = std::nullopt;
float bestDist = range;
for (const CombatantInfo& c : combatants)
{
if (c.entity == e) { continue; }
const bool isValidTarget =
faction.isEnemy ? !c.isEnemy : c.isEnemy;
if (!isValidTarget) { continue; }
const float dist = (c.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
attack.currentTarget = c.entity;
}
}
}
const bool healthy =
(health.maxHp > 0.0f)
&& (health.hp / health.maxHp >= BehaviorScores::kLowHpFraction);
attack.score = (healthy && attack.currentTarget)
? BehaviorScores::kAttack
: BehaviorScores::kInactive;
});
}

View File

@@ -0,0 +1,11 @@
#pragma once
class EntityAdmin;
// Acquires/validates a combat target for ships with weapons. Scores high only
// when the ship's health is not low and a valid target is within sensor range.
class AttackEvaluator
{
public:
void evaluate(EntityAdmin& admin);
};

View File

@@ -0,0 +1,61 @@
#include "AttackExecutor.h"
#include "AttackBehavior.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "ModuleOwnerComponent.h"
#include "MovementIntentComponent.h"
#include "PositionComponent.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
#include "WeaponComponent.h"
void AttackExecutor::execute(EntityAdmin& admin)
{
TRACE();
// Ships: move toward the behavior target.
admin.forEach<AttackBehavior, SelectedBehaviorComponent, PositionComponent,
MovementIntentComponent>(
[&](entt::entity /*e*/, const AttackBehavior& attack,
const SelectedBehaviorComponent& selected, const PositionComponent& pos,
MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::Attack) { return; }
if (!attack.currentTarget) { return; }
const entt::entity t = *attack.currentTarget;
QVector2D dest = pos.value;
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
{
dest = admin.get<PositionComponent>(t).value;
}
intent = MovementIntentComponent{true, dest};
});
// Weapons: assign the behavior target only if it is within this weapon's range.
admin.forEach<WeaponComponent, ModuleOwnerComponent>(
[&](entt::entity /*we*/, WeaponComponent& weapon, const ModuleOwnerComponent& owner)
{
if (!admin.hasAll<AttackBehavior, SelectedBehaviorComponent>(owner.owner))
{
return;
}
const SelectedBehaviorComponent& selected =
admin.get<SelectedBehaviorComponent>(owner.owner);
if (selected.winner != BehaviorKind::Attack) { return; }
const AttackBehavior& attack = admin.get<AttackBehavior>(owner.owner);
if (!attack.currentTarget) { return; }
const entt::entity t = *attack.currentTarget;
if (!admin.isValid(t) || !admin.hasAll<PositionComponent>(t)) { return; }
const QVector2D ownerPos = admin.get<PositionComponent>(owner.owner).value;
const float dist = (admin.get<PositionComponent>(t).value - ownerPos).length();
if (dist <= weapon.range_tiles)
{
weapon.currentTarget = t;
}
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
// When Attack wins, moves the ship toward its target and assigns that target to
// each weapon that has it in range. Weapons whose range excludes the target are
// left untouched so CombatSystem can keep/acquire a closer target (no thrash).
class AttackExecutor
{
public:
void execute(EntityAdmin& admin);
};

View File

@@ -0,0 +1,81 @@
#include "BehaviorTargeting.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
#include "HealthComponent.h"
#include "HqProxyComponent.h"
#include "ModuleOwnerComponent.h"
#include "PositionComponent.h"
#include "SalvageCargoComponent.h"
#include "ShipIdentityComponent.h"
#include "StationBodyComponent.h"
std::vector<RepairableInfo> buildRepairables(EntityAdmin& admin)
{
std::vector<RepairableInfo> repairables;
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent, HealthComponent>(
[&repairables](entt::entity e, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& f,
const HealthComponent& h)
{
repairables.push_back({e, pos.value, f.isEnemy, true, h.hp, h.maxHp});
});
admin.forEach<StationBodyComponent, PositionComponent, FactionComponent, HealthComponent>(
[&repairables](entt::entity e, const StationBodyComponent& /*sb*/,
const PositionComponent& pos, const FactionComponent& f,
const HealthComponent& h)
{
repairables.push_back({e, pos.value, f.isEnemy, false, h.hp, h.maxHp});
});
return repairables;
}
std::vector<CombatantInfo> buildCombatants(EntityAdmin& admin)
{
std::vector<CombatantInfo> combatants;
admin.forEach<PositionComponent, FactionComponent, ShipIdentityComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const ShipIdentityComponent& /*si*/)
{
combatants.push_back({e, pos.value, f.isEnemy, false});
});
admin.forEach<PositionComponent, FactionComponent, StationBodyComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const StationBodyComponent& /*sb*/)
{
combatants.push_back({e, pos.value, f.isEnemy, true});
});
admin.forEach<PositionComponent, FactionComponent, HqProxyComponent>(
[&combatants](entt::entity e, const PositionComponent& pos,
const FactionComponent& f, const HqProxyComponent& /*hq*/)
{
combatants.push_back({e, pos.value, f.isEnemy, true});
});
return combatants;
}
std::unordered_map<entt::entity, CargoState> buildCargoByShip(EntityAdmin& admin)
{
std::unordered_map<entt::entity, CargoState> cargoByShip;
admin.forEach<SalvageCargoComponent, ModuleOwnerComponent>(
[&cargoByShip](entt::entity /*ce*/, const SalvageCargoComponent& c,
const ModuleOwnerComponent& o)
{
CargoState& agg = cargoByShip[o.owner];
agg.current += c.current;
agg.capacity += c.capacity;
});
return cargoByShip;
}
bool isCargoFull(const CargoState& cargo)
{
return cargo.capacity > 0 && cargo.current >= cargo.capacity;
}

View File

@@ -0,0 +1,49 @@
#pragma once
#include <unordered_map>
#include <vector>
#include <QVector2D>
#include "entt/entity/entity.hpp"
class EntityAdmin;
// Shared, per-call target snapshots used by behavior evaluators and the repair
// system. Each caller builds its own snapshot (no cross-system caching).
struct RepairableInfo
{
entt::entity entity;
QVector2D position;
bool isEnemy;
bool isShip;
float hp;
float maxHp;
};
struct CombatantInfo
{
entt::entity entity;
QVector2D position;
bool isEnemy;
bool isStation;
};
struct CargoState
{
int current = 0;
int capacity = 0;
};
// All ships and stations with health — candidates for repair targeting.
std::vector<RepairableInfo> buildRepairables(EntityAdmin& admin);
// All ships, stations, and the HQ proxy — candidates for attack targeting.
std::vector<CombatantInfo> buildCombatants(EntityAdmin& admin);
// Aggregated salvage cargo per owning ship, summed across its salvage modules.
std::unordered_map<entt::entity, CargoState> buildCargoByShip(EntityAdmin& admin);
// True when the ship's aggregated cargo is at capacity (and it has any capacity).
bool isCargoFull(const CargoState& cargo);

View File

@@ -0,0 +1,43 @@
#include "DeliverScrapEvaluator.h"
#include <unordered_map>
#include "BehaviorScores.h"
#include "BehaviorTargeting.h"
#include "Building.h"
#include "BuildingSystem.h"
#include "BuildingType.h"
#include "DeliverScrapBehavior.h"
#include "EntityAdmin.h"
#include "PositionComponent.h"
#include "tracing.h"
void DeliverScrapEvaluator::evaluate(EntityAdmin& admin, const BuildingSystem& buildings)
{
TRACE();
const std::unordered_map<entt::entity, CargoState> cargoByShip = buildCargoByShip(admin);
admin.forEach<DeliverScrapBehavior, PositionComponent>(
[&](entt::entity e, DeliverScrapBehavior& deliver, const PositionComponent& pos)
{
const std::unordered_map<entt::entity, CargoState>::const_iterator it =
cargoByShip.find(e);
const bool cargoFull = (it != cargoByShip.end()) && isCargoFull(it->second);
if (!cargoFull)
{
deliver.score = BehaviorScores::kInactive;
return;
}
// Assign nearest SalvageBay if not yet assigned.
if (deliver.deliveryBay == kInvalidBuildingId)
{
const Building* bay =
buildings.findNearestBuilding(pos.value, BuildingType::SalvageBay);
if (bay) { deliver.deliveryBay = bay->id; }
}
deliver.score = BehaviorScores::kDeliver;
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
class BuildingSystem;
// Scores high only when the ship's cargo is full, and assigns the nearest
// SalvageBay as the delivery destination.
class DeliverScrapEvaluator
{
public:
void evaluate(EntityAdmin& admin, const BuildingSystem& buildings);
};

View File

@@ -0,0 +1,38 @@
#include "DeliverScrapExecutor.h"
#include <QVector2D>
#include "BehaviorKind.h"
#include "Building.h"
#include "BuildingSystem.h"
#include "DeliverScrapBehavior.h"
#include "EntityAdmin.h"
#include "MovementIntentComponent.h"
#include "PositionComponent.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void DeliverScrapExecutor::execute(EntityAdmin& admin, const BuildingSystem& buildings)
{
TRACE();
admin.forEach<DeliverScrapBehavior, SelectedBehaviorComponent, PositionComponent,
MovementIntentComponent>(
[&](entt::entity /*e*/, const DeliverScrapBehavior& deliver,
const SelectedBehaviorComponent& selected, const PositionComponent& pos,
MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::DeliverScrap) { return; }
QVector2D dest = pos.value;
if (deliver.deliveryBay != kInvalidBuildingId)
{
const Building* bay = buildings.findBuilding(deliver.deliveryBay);
if (bay)
{
dest = QVector2D(bay->anchor.x() + bay->footprint.width() / 2.0f,
bay->anchor.y() + bay->footprint.height() / 2.0f);
}
}
intent = MovementIntentComponent{true, dest};
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
class BuildingSystem;
// Moves a ship toward its delivery bay when DeliverScrap is the winning
// behavior. Never decrements cargo — SalvagerSystem performs the delivery.
class DeliverScrapExecutor
{
public:
void execute(EntityAdmin& admin, const BuildingSystem& buildings);
};

View File

@@ -0,0 +1,16 @@
#include "RallyEvaluator.h"
#include "BehaviorScores.h"
#include "EntityAdmin.h"
#include "RallyBehavior.h"
#include "tracing.h"
void RallyEvaluator::evaluate(EntityAdmin& admin)
{
TRACE();
admin.forEach<RallyBehavior>(
[](entt::entity /*e*/, RallyBehavior& rally)
{
rally.score = BehaviorScores::kRally;
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
// Scores the rally behavior so player combat ships gather at the rally point
// until an enemy appears (Attack outscores it) or the departure timer removes
// the RallyBehavior component.
class RallyEvaluator
{
public:
void evaluate(EntityAdmin& admin);
};

View File

@@ -0,0 +1,20 @@
#include "RallyExecutor.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "MovementIntentComponent.h"
#include "RallyBehavior.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void RallyExecutor::execute(EntityAdmin& admin)
{
TRACE();
admin.forEach<RallyBehavior, SelectedBehaviorComponent, MovementIntentComponent>(
[](entt::entity /*e*/, const RallyBehavior& rally,
const SelectedBehaviorComponent& selected, MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::Rally) { return; }
intent = MovementIntentComponent{true, rally.rallyPoint};
});
}

View File

@@ -0,0 +1,10 @@
#pragma once
class EntityAdmin;
// Moves a ship to its rally point when Rally is the winning behavior.
class RallyExecutor
{
public:
void execute(EntityAdmin& admin);
};

View File

@@ -0,0 +1,58 @@
#include "RepairEvaluator.h"
#include <vector>
#include "BehaviorScores.h"
#include "BehaviorTargeting.h"
#include "EntityAdmin.h"
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "RepairBehavior.h"
#include "SensorRangeComponent.h"
#include "tracing.h"
void RepairEvaluator::evaluate(EntityAdmin& admin)
{
TRACE();
const std::vector<RepairableInfo> repairables = buildRepairables(admin);
admin.forEach<RepairBehavior, PositionComponent, SensorRangeComponent>(
[&](entt::entity e, RepairBehavior& repair, const PositionComponent& pos,
const SensorRangeComponent& sensor)
{
// Validate current target: alive and still damaged.
bool targetValid = false;
if (repair.currentTarget)
{
const entt::entity t = *repair.currentTarget;
if (admin.isValid(t) && admin.hasAll<HealthComponent>(t))
{
const HealthComponent& th = admin.get<HealthComponent>(t);
if (th.hp > 0.0f && th.hp < th.maxHp) { targetValid = true; }
}
}
// Acquire nearest damaged friendly within sensor range.
if (!targetValid)
{
repair.currentTarget = std::nullopt;
float bestDist = sensor.value_tiles;
for (const RepairableInfo& r : repairables)
{
if (r.entity == e) { continue; }
if (r.isEnemy) { continue; }
if (r.hp <= 0.0f || r.hp >= r.maxHp) { continue; }
const float dist = (r.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
repair.currentTarget = r.entity;
}
}
}
repair.score = repair.currentTarget
? BehaviorScores::kRepair
: BehaviorScores::kInactive;
});
}

View File

@@ -0,0 +1,11 @@
#pragma once
class EntityAdmin;
// Picks the nearest damaged friendly within sensor range as the repair target.
// Scores high when such a target exists.
class RepairEvaluator
{
public:
void evaluate(EntityAdmin& admin);
};

View File

@@ -0,0 +1,61 @@
#include "RepairExecutor.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "ModuleOwnerComponent.h"
#include "MovementIntentComponent.h"
#include "PositionComponent.h"
#include "RepairBehavior.h"
#include "RepairToolComponent.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void RepairExecutor::execute(EntityAdmin& admin)
{
TRACE();
// Ships: move toward the repair target.
admin.forEach<RepairBehavior, SelectedBehaviorComponent, PositionComponent,
MovementIntentComponent>(
[&](entt::entity /*e*/, const RepairBehavior& repair,
const SelectedBehaviorComponent& selected, const PositionComponent& pos,
MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::Repair) { return; }
if (!repair.currentTarget) { return; }
const entt::entity t = *repair.currentTarget;
QVector2D dest = pos.value;
if (admin.isValid(t) && admin.hasAll<PositionComponent>(t))
{
dest = admin.get<PositionComponent>(t).value;
}
intent = MovementIntentComponent{true, dest};
});
// Repair tools: prefer the behavior target if it is within tool range.
admin.forEach<RepairToolComponent, ModuleOwnerComponent>(
[&](entt::entity /*re*/, RepairToolComponent& tool, const ModuleOwnerComponent& owner)
{
if (!admin.hasAll<RepairBehavior, SelectedBehaviorComponent>(owner.owner))
{
return;
}
const SelectedBehaviorComponent& selected =
admin.get<SelectedBehaviorComponent>(owner.owner);
if (selected.winner != BehaviorKind::Repair) { return; }
const RepairBehavior& repair = admin.get<RepairBehavior>(owner.owner);
if (!repair.currentTarget) { return; }
const entt::entity t = *repair.currentTarget;
if (!admin.isValid(t) || !admin.hasAll<PositionComponent>(t)) { return; }
const QVector2D ownerPos = admin.get<PositionComponent>(owner.owner).value;
const float dist = (admin.get<PositionComponent>(t).value - ownerPos).length();
if (dist <= tool.range_tiles)
{
tool.currentTarget = t;
}
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
// When Repair wins, moves the ship toward its target and assigns that target to
// each repair tool that has it in range. RepairSystem applies the healing and
// does fallback acquisition for tools whose preferred target is out of range.
class RepairExecutor
{
public:
void execute(EntityAdmin& admin);
};

View File

@@ -0,0 +1,56 @@
#include "RetreatEvaluator.h"
#include <vector>
#include <QVector2D>
#include "AttackBehavior.h"
#include "BehaviorScores.h"
#include "EntityAdmin.h"
#include "FactionComponent.h"
#include "HealthComponent.h"
#include "PositionComponent.h"
#include "RetreatBehavior.h"
#include "SensorRangeComponent.h"
#include "ShipIdentityComponent.h"
#include "tracing.h"
void RetreatEvaluator::evaluate(EntityAdmin& admin)
{
TRACE();
// Snapshot enemy ship positions for threat detection.
std::vector<QVector2D> enemyShips;
admin.forEach<ShipIdentityComponent, PositionComponent, FactionComponent>(
[&enemyShips](entt::entity /*e*/, const ShipIdentityComponent& /*si*/,
const PositionComponent& pos, const FactionComponent& f)
{
if (f.isEnemy) { enemyShips.push_back(pos.value); }
});
admin.forEach<RetreatBehavior, PositionComponent, HealthComponent, SensorRangeComponent>(
[&](entt::entity e, RetreatBehavior& retreat, const PositionComponent& pos,
const HealthComponent& health, const SensorRangeComponent& sensor)
{
const bool lowHp = (health.maxHp > 0.0f)
&& (health.hp / health.maxHp < retreat.retreatHpFraction);
bool threatened = false;
const bool hasWeapons = admin.hasAll<AttackBehavior>(e);
if (!hasWeapons)
{
for (const QVector2D& enemy : enemyShips)
{
if ((enemy - pos.value).length() <= sensor.value_tiles)
{
threatened = true;
break;
}
}
}
retreat.score = (lowHp || threatened)
? BehaviorScores::kRetreat
: BehaviorScores::kInactive;
});
}

View File

@@ -0,0 +1,12 @@
#pragma once
class EntityAdmin;
// Scores high (above all task behaviors) when the ship's health is below its
// retreat threshold, or when an enemy ship is within sensor range and the ship
// has no weapons to fight back with.
class RetreatEvaluator
{
public:
void evaluate(EntityAdmin& admin);
};

View File

@@ -0,0 +1,20 @@
#include "RetreatExecutor.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "MovementIntentComponent.h"
#include "RetreatBehavior.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void RetreatExecutor::execute(EntityAdmin& admin)
{
TRACE();
admin.forEach<RetreatBehavior, SelectedBehaviorComponent, MovementIntentComponent>(
[](entt::entity /*e*/, const RetreatBehavior& retreat,
const SelectedBehaviorComponent& selected, MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::Retreat) { return; }
intent = MovementIntentComponent{true, retreat.retreatPoint};
});
}

View File

@@ -0,0 +1,10 @@
#pragma once
class EntityAdmin;
// Moves a ship to its retreat point (the rally point) when Retreat wins.
class RetreatExecutor
{
public:
void execute(EntityAdmin& admin);
};

View File

@@ -0,0 +1,55 @@
#include "SalvageScrapEvaluator.h"
#include <optional>
#include <unordered_map>
#include <vector>
#include <QVector2D>
#include "BehaviorScores.h"
#include "BehaviorTargeting.h"
#include "EntityAdmin.h"
#include "PositionComponent.h"
#include "SalvageScrapBehavior.h"
#include "ScrapSystem.h"
#include "SensorRangeComponent.h"
#include "tracing.h"
void SalvageScrapEvaluator::evaluate(EntityAdmin& admin, const ScrapSystem& scraps)
{
TRACE();
const std::unordered_map<entt::entity, CargoState> cargoByShip = buildCargoByShip(admin);
const std::vector<ScrapInfo> allScrap = scraps.allScrapInfo();
admin.forEach<SalvageScrapBehavior, PositionComponent, SensorRangeComponent>(
[&](entt::entity e, SalvageScrapBehavior& salvage, const PositionComponent& pos,
const SensorRangeComponent& sensor)
{
const std::unordered_map<entt::entity, CargoState>::const_iterator it =
cargoByShip.find(e);
const bool cargoFull = (it != cargoByShip.end()) && isCargoFull(it->second);
if (cargoFull)
{
salvage.scrapTarget = std::nullopt;
salvage.score = BehaviorScores::kInactive;
return;
}
// Find nearest scrap within sensor range.
float bestDist = sensor.value_tiles;
std::optional<QVector2D> bestPos;
for (const ScrapInfo& si : allScrap)
{
const float dist = (si.position - pos.value).length();
if (dist < bestDist)
{
bestDist = dist;
bestPos = si.position;
}
}
salvage.scrapTarget = bestPos;
salvage.score = bestPos ? BehaviorScores::kSalvage : BehaviorScores::kInactive;
});
}

View File

@@ -0,0 +1,13 @@
#pragma once
class EntityAdmin;
class ScrapSystem;
// When cargo is not full, finds the nearest scrap within sensor range and sets
// it as the target, scoring high. Scores inactive when cargo is full or no scrap
// is in range (Advance then handles roaming).
class SalvageScrapEvaluator
{
public:
void evaluate(EntityAdmin& admin, const ScrapSystem& scraps);
};

View File

@@ -0,0 +1,21 @@
#include "SalvageScrapExecutor.h"
#include "BehaviorKind.h"
#include "EntityAdmin.h"
#include "MovementIntentComponent.h"
#include "SalvageScrapBehavior.h"
#include "SelectedBehaviorComponent.h"
#include "tracing.h"
void SalvageScrapExecutor::execute(EntityAdmin& admin)
{
TRACE();
admin.forEach<SalvageScrapBehavior, SelectedBehaviorComponent, MovementIntentComponent>(
[](entt::entity /*e*/, const SalvageScrapBehavior& salvage,
const SelectedBehaviorComponent& selected, MovementIntentComponent& intent)
{
if (selected.winner != BehaviorKind::SalvageScrap) { return; }
if (!salvage.scrapTarget) { return; }
intent = MovementIntentComponent{true, *salvage.scrapTarget};
});
}

View File

@@ -0,0 +1,10 @@
#pragma once
class EntityAdmin;
// Moves a ship toward its scrap target when SalvageScrap is the winning behavior.
class SalvageScrapExecutor
{
public:
void execute(EntityAdmin& admin);
};