depend on the registry instead of DebrisSystem in the AI path

getAllDebrisInfo and collectOne only ever touched EntityAdmin — DebrisSystem
holds nothing else — so they become free functions over the registry. That lets
AiSystem, SalvagerSystem and SalvageScrapEvaluator drop their DebrisSystem&
parameters entirely; SalvagerSystem already held the admin, and the other two
were handed it alongside.

No system in lib/ecs/system takes another system now. Every tick signature names
the data it works on: the registry, the factory state, or both.

DebrisSystem keeps spawn, tickDespawn and consume — the first two are genuine
tick behaviour rather than lookups.

Verified with a golden-checksum capture before and after — all four sample ticks
identical.

Co-Authored-By: Claude <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YHcUerKAZKWNvSKJxYKbnG
This commit is contained in:
2026-08-05 07:16:36 +02:00
parent a86ba3428a
commit 72d85d681c
16 changed files with 50 additions and 46 deletions

View File

@@ -324,9 +324,9 @@ void ArenaSimulation::tick()
// Ship behavior systems (tick step 7): evaluate, select winner, execute. // Ship behavior systems (tick step 7): evaluate, select winner, execute.
// Module + combat systems emit their tool beams into a shared buffer. // Module + combat systems emit their tool beams into a shared buffer.
m_shipSystem->clearMovementIntents(); m_shipSystem->clearMovementIntents();
m_aiSystem->tick(m_admin, m_factoryState, *m_debrisSystem); m_aiSystem->tick(m_admin, m_factoryState);
std::vector<BeamFiredEvent> beamFiredEvents; std::vector<BeamFiredEvent> beamFiredEvents;
m_salvagerSystem->tick(m_currentTick, *m_debrisSystem, m_factoryState, beamFiredEvents); m_salvagerSystem->tick(m_currentTick, m_factoryState, beamFiredEvents);
m_repairSystem->tick(m_currentTick, beamFiredEvents); m_repairSystem->tick(m_currentTick, beamFiredEvents);
// Combat resolution (tick step 8). // Combat resolution (tick step 8).

View File

@@ -340,7 +340,7 @@ void ArenaView::drawBuildings(QPainter& painter)
void ArenaView::drawDebris(QPainter& painter) void ArenaView::drawDebris(QPainter& painter)
{ {
const float r = getTilePx() * 0.2f; const float r = getTilePx() * 0.2f;
for (const DebrisInfo& debris : m_sim->getDebrisSystem().getAllDebrisInfo()) for (const DebrisInfo& debris : getAllDebrisInfo(m_sim->getAdmin()))
{ {
const QPointF center = worldToWidget(debris.position); const QPointF center = worldToWidget(debris.position);
painter.setBrush(QColor(128, 110, 90)); painter.setBrush(QColor(128, 110, 90));

View File

@@ -43,8 +43,7 @@ AiSystem::AiSystem(const GameConfig& config)
{ {
} }
void AiSystem::tick(EntityAdmin& admin, const FactoryState& state, void AiSystem::tick(EntityAdmin& admin, const FactoryState& state)
const DebrisSystem& debris)
{ {
TRACE(); TRACE();
@@ -55,7 +54,7 @@ void AiSystem::tick(EntityAdmin& admin, const FactoryState& state,
m_retreatEvaluator.evaluate(admin); m_retreatEvaluator.evaluate(admin);
m_attackEvaluator.evaluate(admin); m_attackEvaluator.evaluate(admin);
m_repairEvaluator.evaluate(admin); m_repairEvaluator.evaluate(admin);
m_salvageScrapEvaluator.evaluate(admin, debris); m_salvageScrapEvaluator.evaluate(admin);
m_deliverScrapEvaluator.evaluate(admin, state); m_deliverScrapEvaluator.evaluate(admin, state);
// Phase 2: pick the highest-scoring behavior per ship. // Phase 2: pick the highest-scoring behavior per ship.

View File

@@ -20,7 +20,6 @@
#include "StandbyExecutor.h" #include "StandbyExecutor.h"
class EntityAdmin; class EntityAdmin;
class DebrisSystem;
struct GameConfig; struct GameConfig;
// Orchestrates ship-behavior decision-making in three batched phases: // Orchestrates ship-behavior decision-making in three batched phases:
@@ -35,7 +34,7 @@ class AiSystem
public: public:
explicit AiSystem(const GameConfig& config); explicit AiSystem(const GameConfig& config);
void tick(EntityAdmin& admin, const FactoryState& state, const DebrisSystem& debris); void tick(EntityAdmin& admin, const FactoryState& state);
private: private:
void selectWinningBehaviors(EntityAdmin& admin); void selectWinningBehaviors(EntityAdmin& admin);

View File

@@ -46,13 +46,13 @@ std::optional<int> DebrisSystem::consume(entt::entity entity)
return amount; return amount;
} }
bool DebrisSystem::collectOne(entt::entity entity) bool collectOne(EntityAdmin& admin, entt::entity entity)
{ {
if (!m_admin.isValid(entity) || !m_admin.hasAll<DebrisComponent>(entity)) if (!admin.isValid(entity) || !admin.hasAll<DebrisComponent>(entity))
{ {
return false; return false;
} }
DebrisComponent& data = m_admin.get<DebrisComponent>(entity); DebrisComponent& data = admin.get<DebrisComponent>(entity);
if (data.amount <= 0) if (data.amount <= 0)
{ {
return false; return false;
@@ -60,18 +60,18 @@ bool DebrisSystem::collectOne(entt::entity entity)
--data.amount; --data.amount;
if (data.amount <= 0) if (data.amount <= 0)
{ {
m_admin.destroy(entity); admin.destroy(entity);
} }
return true; return true;
} }
std::vector<DebrisInfo> DebrisSystem::getAllDebrisInfo() const std::vector<DebrisInfo> getAllDebrisInfo(const EntityAdmin& admin)
{ {
std::vector<DebrisInfo> result; std::vector<DebrisInfo> result;
m_admin.forEach<DebrisComponent>( admin.forEach<DebrisComponent>(
[&result, this](entt::entity e, const DebrisComponent& sd) [&result, &admin](entt::entity e, const DebrisComponent& sd)
{ {
result.push_back(DebrisInfo{e, m_admin.get<PositionComponent>(e).value, sd.amount}); result.push_back(DebrisInfo{e, admin.get<PositionComponent>(e).value, sd.amount});
}); });
return result; return result;
} }

View File

@@ -38,9 +38,17 @@ public:
// false if the entity is invalid or already empty (REQ-SHP-SALVAGE). // false if the entity is invalid or already empty (REQ-SHP-SALVAGE).
bool collectOne(entt::entity entity); bool collectOne(entt::entity entity);
// Lightweight snapshot for callers that need to iterate all debris.
std::vector<DebrisInfo> getAllDebrisInfo() const;
private: private:
EntityAdmin& m_admin; EntityAdmin& m_admin;
}; };
// Debris state read and changed straight off the registry — no system needed.
// Lightweight snapshot for callers that need to iterate all debris.
std::vector<DebrisInfo> getAllDebrisInfo(const EntityAdmin& admin);
// Collects a single scrap unit from the debris: decrements its amount by one,
// destroying the entity once depleted. Returns true if a scrap was collected,
// false if the entity is invalid or already empty (REQ-SHP-SALVAGE).
bool collectOne(EntityAdmin& admin, entt::entity entity);

View File

@@ -24,14 +24,14 @@ SalvagerSystem::SalvagerSystem(EntityAdmin& admin)
{ {
} }
void SalvagerSystem::tick(Tick currentTick, DebrisSystem& debris, FactoryState& state, void SalvagerSystem::tick(Tick currentTick, FactoryState& state,
std::vector<BeamFiredEvent>& outBeamFiredEvents) std::vector<BeamFiredEvent>& outBeamFiredEvents)
{ {
TRACE(); TRACE();
// Apply collections whose mid-beam delay has elapsed (cycles started earlier). // Apply collections whose mid-beam delay has elapsed (cycles started earlier).
applyPendingCollections(currentTick, debris); applyPendingCollections(currentTick);
const std::vector<DebrisInfo> allDebris = debris.getAllDebrisInfo(); const std::vector<DebrisInfo> allDebris = getAllDebrisInfo(m_admin);
// Tick down per-module collection cooldowns. // Tick down per-module collection cooldowns.
m_admin.forEach<SalvagerComponent>( m_admin.forEach<SalvagerComponent>(
@@ -108,7 +108,7 @@ void SalvagerSystem::tick(Tick currentTick, DebrisSystem& debris, FactoryState&
}); });
} }
void SalvagerSystem::applyPendingCollections(Tick currentTick, DebrisSystem& debris) void SalvagerSystem::applyPendingCollections(Tick currentTick)
{ {
std::vector<PendingCollection>::iterator it = m_pendingCollections.begin(); std::vector<PendingCollection>::iterator it = m_pendingCollections.begin();
while (it != m_pendingCollections.end()) while (it != m_pendingCollections.end())
@@ -118,7 +118,7 @@ void SalvagerSystem::applyPendingCollections(Tick currentTick, DebrisSystem& deb
if (m_admin.isValid(it->ship) && m_admin.hasAll<CargoComponent>(it->ship)) if (m_admin.isValid(it->ship) && m_admin.hasAll<CargoComponent>(it->ship))
{ {
CargoComponent& cargo = m_admin.get<CargoComponent>(it->ship); CargoComponent& cargo = m_admin.get<CargoComponent>(it->ship);
if (cargo.current < cargo.maxCapacity && debris.collectOne(it->debris)) if (cargo.current < cargo.maxCapacity && collectOne(m_admin, it->debris))
{ {
++cargo.current; ++cargo.current;
} }

View File

@@ -10,7 +10,6 @@
#include "entt/entity/entity.hpp" #include "entt/entity/entity.hpp"
class EntityAdmin; class EntityAdmin;
class DebrisSystem;
// World-mutation system for salvage modules: each module runs a collection cycle // World-mutation system for salvage modules: each module runs a collection cycle
// on its own cooldown. When a cycle starts it emits a salvage beam toward an // on its own cooldown. When a cycle starts it emits a salvage beam toward an
@@ -22,7 +21,7 @@ class SalvagerSystem
public: public:
explicit SalvagerSystem(EntityAdmin& admin); explicit SalvagerSystem(EntityAdmin& admin);
void tick(Tick currentTick, DebrisSystem& debris, FactoryState& state, void tick(Tick currentTick, FactoryState& state,
std::vector<BeamFiredEvent>& outBeamFiredEvents); std::vector<BeamFiredEvent>& outBeamFiredEvents);
private: private:
@@ -33,7 +32,7 @@ private:
Tick appliesAt; Tick appliesAt;
}; };
void applyPendingCollections(Tick currentTick, DebrisSystem& debris); void applyPendingCollections(Tick currentTick);
EntityAdmin& m_admin; EntityAdmin& m_admin;
std::vector<PendingCollection> m_pendingCollections; std::vector<PendingCollection> m_pendingCollections;

View File

@@ -15,11 +15,11 @@
#include "SensorRangeComponent.h" #include "SensorRangeComponent.h"
#include "tracing.h" #include "tracing.h"
void SalvageScrapEvaluator::evaluate(EntityAdmin& admin, const DebrisSystem& debris) void SalvageScrapEvaluator::evaluate(EntityAdmin& admin)
{ {
TRACE(); TRACE();
const std::unordered_map<entt::entity, CargoState> cargoByShip = buildCargoByShip(admin); const std::unordered_map<entt::entity, CargoState> cargoByShip = buildCargoByShip(admin);
const std::vector<DebrisInfo> allDebris = debris.getAllDebrisInfo(); const std::vector<DebrisInfo> allDebris = getAllDebrisInfo(admin);
admin.forEach<SalvageScrapBehavior, PositionComponent, SensorRangeComponent>( admin.forEach<SalvageScrapBehavior, PositionComponent, SensorRangeComponent>(
[&](entt::entity e, SalvageScrapBehavior& salvage, const PositionComponent& pos, [&](entt::entity e, SalvageScrapBehavior& salvage, const PositionComponent& pos,

View File

@@ -1,7 +1,6 @@
#pragma once #pragma once
class EntityAdmin; class EntityAdmin;
class DebrisSystem;
// When cargo is not full, finds the nearest debris within sensor range and sets // When cargo is not full, finds the nearest debris within sensor range and sets
// it as the target, scoring high. Scores inactive when cargo is full or no debris // it as the target, scoring high. Scores inactive when cargo is full or no debris
@@ -9,5 +8,5 @@ class DebrisSystem;
class SalvageScrapEvaluator class SalvageScrapEvaluator
{ {
public: public:
void evaluate(EntityAdmin& admin, const DebrisSystem& debris); void evaluate(EntityAdmin& admin);
}; };

View File

@@ -268,10 +268,10 @@ void Simulation::tick()
m_shipSystem->clearMovementIntents(); m_shipSystem->clearMovementIntents();
// Score-based behavior selection: evaluate, select winner, execute (sets // Score-based behavior selection: evaluate, select winner, execute (sets
// movement intent + preferred module targets only — no world mutation). // movement intent + preferred module targets only — no world mutation).
m_aiSystem->tick(m_admin, m_factoryState, *m_debrisSystem); m_aiSystem->tick(m_admin, m_factoryState);
// Module systems perform the world mutation (collection/delivery, healing). // Module systems perform the world mutation (collection/delivery, healing).
// Each emits its tool beams and applies its own delayed (mid-beam) effects. // Each emits its tool beams and applies its own delayed (mid-beam) effects.
m_salvagerSystem->tick(m_currentTick, *m_debrisSystem, m_factoryState, m_beamFiredEvents); m_salvagerSystem->tick(m_currentTick, m_factoryState, m_beamFiredEvents);
m_repairSystem->tick(m_currentTick, m_beamFiredEvents); m_repairSystem->tick(m_currentTick, m_beamFiredEvents);
// Step 8: combat resolution // Step 8: combat resolution

View File

@@ -101,14 +101,14 @@ struct Fixture
void decide() void decide()
{ {
ships.clearMovementIntents(); ships.clearMovementIntents();
ai.tick(admin, state, scraps); ai.tick(admin, state);
} }
// World mutation: collection/delivery and healing. // World mutation: collection/delivery and healing.
void runModules() void runModules()
{ {
beamEvents.clear(); beamEvents.clear();
salvager.tick(tick, scraps, state, beamEvents); salvager.tick(tick, state, beamEvents);
repair.tick(tick, beamEvents); repair.tick(tick, beamEvents);
} }
@@ -141,7 +141,7 @@ struct Fixture
void salvageTick() void salvageTick()
{ {
beamEvents.clear(); beamEvents.clear();
salvager.tick(tick, scraps, state, beamEvents); salvager.tick(tick, state, beamEvents);
++tick; ++tick;
} }

View File

@@ -415,7 +415,7 @@ TEST_CASE("CombatSystem: scrap is spawned on ship death", "[combat]")
sim.tick(); sim.tick();
const std::vector<DebrisInfo> scraps = sim.getDebrisSystem().getAllDebrisInfo(); const std::vector<DebrisInfo> scraps = getAllDebrisInfo(sim.getAdmin());
REQUIRE(scraps.size() == 1); REQUIRE(scraps.size() == 1);
CHECK(sim.getAdmin().get<DebrisComponent>(scraps[0].entity).amount == 59); CHECK(sim.getAdmin().get<DebrisComponent>(scraps[0].entity).amount == 59);
} }

View File

@@ -116,16 +116,16 @@ TEST_CASE("DebrisSystem: collectOne depletes one scrap and keeps the debris unti
const entt::entity e = ss.spawn(QVector2D(0.0f, 0.0f), 3, 100); const entt::entity e = ss.spawn(QVector2D(0.0f, 0.0f), 3, 100);
REQUIRE(ss.collectOne(e)); REQUIRE(collectOne(admin, e));
REQUIRE(admin.isValid(e)); REQUIRE(admin.isValid(e));
REQUIRE(admin.get<DebrisComponent>(e).amount == 2); REQUIRE(admin.get<DebrisComponent>(e).amount == 2);
REQUIRE(ss.collectOne(e)); REQUIRE(collectOne(admin, e));
REQUIRE(admin.isValid(e)); REQUIRE(admin.isValid(e));
REQUIRE(admin.get<DebrisComponent>(e).amount == 1); REQUIRE(admin.get<DebrisComponent>(e).amount == 1);
// Final unit collected: the debris is removed once depleted. // Final unit collected: the debris is removed once depleted.
REQUIRE(ss.collectOne(e)); REQUIRE(collectOne(admin, e));
REQUIRE_FALSE(admin.isValid(e)); REQUIRE_FALSE(admin.isValid(e));
} }
@@ -134,7 +134,7 @@ TEST_CASE("DebrisSystem: collectOne returns false for an invalid entity", "[debr
EntityAdmin admin; EntityAdmin admin;
DebrisSystem ss(admin); DebrisSystem ss(admin);
REQUIRE_FALSE(ss.collectOne(entt::null)); REQUIRE_FALSE(collectOne(admin, entt::null));
} }
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -149,7 +149,7 @@ TEST_CASE("DebrisSystem: getAllDebrisInfo returns all spawned debris", "[debris]
ss.spawn(QVector2D(1.0f, 2.0f), 3, 100); ss.spawn(QVector2D(1.0f, 2.0f), 3, 100);
ss.spawn(QVector2D(4.0f, 5.0f), 6, 200); ss.spawn(QVector2D(4.0f, 5.0f), 6, 200);
const std::vector<DebrisInfo> info = ss.getAllDebrisInfo(); const std::vector<DebrisInfo> info = getAllDebrisInfo(admin);
REQUIRE(info.size() == 2); REQUIRE(info.size() == 2);
} }
@@ -161,7 +161,7 @@ TEST_CASE("DebrisSystem: getAllDebrisInfo reports each debris entry.s remaining
const entt::entity a = ss.spawn(QVector2D(1.0f, 2.0f), 3, 100); const entt::entity a = ss.spawn(QVector2D(1.0f, 2.0f), 3, 100);
const entt::entity b = ss.spawn(QVector2D(4.0f, 5.0f), 6, 200); const entt::entity b = ss.spawn(QVector2D(4.0f, 5.0f), 6, 200);
const std::vector<DebrisInfo> info = ss.getAllDebrisInfo(); const std::vector<DebrisInfo> info = getAllDebrisInfo(admin);
REQUIRE(info.size() == 2); REQUIRE(info.size() == 2);
for (const DebrisInfo& i : info) for (const DebrisInfo& i : info)
{ {

View File

@@ -367,7 +367,7 @@ int FieldSelectionPanel::selectedDebrisScrapTotal() const
{ {
// Sum the remaining scrap across the still-living selected debris (REQ-UI-DEBRIS-PANEL). // Sum the remaining scrap across the still-living selected debris (REQ-UI-DEBRIS-PANEL).
int total = 0; int total = 0;
for (const DebrisInfo& info : m_sim->getDebrisSystem().getAllDebrisInfo()) for (const DebrisInfo& info : getAllDebrisInfo(m_sim->getAdmin()))
{ {
if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), info.entity) if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), info.entity)
!= m_selectedDebris.end()) != m_selectedDebris.end())

View File

@@ -777,7 +777,7 @@ void GameWorldView::pruneDespawnedDebris()
if (m_selectedDebris.empty()) { return; } if (m_selectedDebris.empty()) { return; }
std::vector<entt::entity> live; std::vector<entt::entity> live;
for (const DebrisInfo& info : m_sim->getDebrisSystem().getAllDebrisInfo()) for (const DebrisInfo& info : getAllDebrisInfo(m_sim->getAdmin()))
{ {
if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), info.entity) if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), info.entity)
!= m_selectedDebris.end()) != m_selectedDebris.end())
@@ -1510,7 +1510,7 @@ void GameWorldView::drawSelectionHighlights(QPainter& painter)
if (!m_selectedDebris.empty()) if (!m_selectedDebris.empty())
{ {
const qreal outlineRadius = static_cast<qreal>(getTilePx() * 0.2f) + 3.0; const qreal outlineRadius = static_cast<qreal>(getTilePx() * 0.2f) + 3.0;
for (const DebrisInfo& debris : m_sim->getDebrisSystem().getAllDebrisInfo()) for (const DebrisInfo& debris : getAllDebrisInfo(m_sim->getAdmin()))
{ {
if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), debris.entity) if (std::find(m_selectedDebris.begin(), m_selectedDebris.end(), debris.entity)
== m_selectedDebris.end()) { continue; } == m_selectedDebris.end()) { continue; }
@@ -1654,7 +1654,7 @@ void GameWorldView::drawBeltItems(QPainter& painter)
void GameWorldView::drawDebris(QPainter& painter) void GameWorldView::drawDebris(QPainter& painter)
{ {
const float r = getTilePx() * 0.2f; const float r = getTilePx() * 0.2f;
for (const DebrisInfo& debris : m_sim->getDebrisSystem().getAllDebrisInfo()) for (const DebrisInfo& debris : getAllDebrisInfo(m_sim->getAdmin()))
{ {
const QPointF center = worldToWidget(debris.position); const QPointF center = worldToWidget(debris.position);
painter.setBrush(QColor(128, 110, 90)); painter.setBrush(QColor(128, 110, 90));