move FactoryState ownership out of BuildingSystem to Simulation

This commit is contained in:
2026-08-05 06:43:52 +02:00
parent 0edea5d961
commit 46932e4abf
10 changed files with 97 additions and 43 deletions

View File

@@ -23,6 +23,7 @@ bool inputLaneEntryFree(const std::vector<BeltItemSlot>& lane)
} // namespace
BuildingSystem::BuildingSystem(const GameConfig& config,
FactoryState& state,
BeltSystem& belts,
std::function<BuildingId()> allocateBuildingId,
std::function<void(int)> addBuildingBlocks,
@@ -31,6 +32,7 @@ BuildingSystem::BuildingSystem(const GameConfig& config,
std::function<bool(const std::string&)> isItemUnlocked,
std::mt19937& rng)
: m_config(config)
, m_state(state)
, m_belts(belts)
, m_allocateBuildingId(std::move(allocateBuildingId))
, m_addBuildingBlocks(std::move(addBuildingBlocks))

View File

@@ -47,6 +47,7 @@ class BuildingSystem
{
public:
BuildingSystem(const GameConfig& config,
FactoryState& state,
BeltSystem& belts,
std::function<BuildingId()> allocateBuildingId,
std::function<void(int)> addBuildingBlocks,
@@ -285,6 +286,11 @@ private:
QPoint anchor) const;
const GameConfig& m_config;
// The factory's world data — buildings, queued work, tile ownership. Owned by
// Simulation, not by this system (see FactoryState.h).
FactoryState& m_state;
BeltSystem& m_belts;
std::function<BuildingId()> m_allocateBuildingId;
std::function<void(int)> m_addBuildingBlocks;
@@ -293,9 +299,4 @@ private:
std::function<bool(const std::string&)> m_isItemUnlocked;
std::mt19937& m_rng;
int m_asteroidWidth_tiles;
// The factory's world data — buildings, queued work, tile ownership. Held here
// for now; the intent is for Simulation to own it and pass it into the tick
// methods, leaving this system stateless over it (see FactoryState.h).
FactoryState m_state;
};

View File

@@ -31,8 +31,11 @@ struct DeconstructionEntry
// data/behaviour split the ecs/system/ classes already follow, where world data
// arrives as a tick argument instead of being owned by the system.
//
// Owned by BuildingSystem for now. The intent is to hand ownership to Simulation
// and pass this into the tick methods, so the systems become stateless over it.
// Owned by Simulation (and by ArenaSimulation in the balancing tool), not by the
// systems that operate on it. BuildingSystem holds a reference. The remaining step
// is to pass this into the tick methods instead, so the systems become stateless
// over it — that one is gated on the query surface, which today reaches the data
// through BuildingSystem's ~180 const call sites.
struct FactoryState
{
std::vector<Building> buildings;

View File

@@ -94,6 +94,7 @@ void Simulation::reset(unsigned int seed)
m_pendingSchematicChoices.clear();
m_admin.clear();
m_factoryState = FactoryState{};
m_beltSystem = BeltSystem(m_config.world.beltSpeed_tps);
initializeSubsystems();
@@ -105,6 +106,7 @@ void Simulation::initializeSubsystems()
{
m_buildingSystem = std::make_unique<BuildingSystem>(
m_config,
m_factoryState,
m_beltSystem,
[this]() { return allocateBuildingId(); },
[this](int amount) { m_buildingBlocksStock += amount; },

View File

@@ -9,6 +9,7 @@
#include <QPoint>
#include "BeltSystem.h"
#include "FactoryState.h"
#include "EntityAdmin.h"
#include "entt/entity/entity.hpp"
#include "SchematicChoiceOption.h"
@@ -209,6 +210,9 @@ private:
UnlockState m_unlockState;
EntityAdmin m_admin;
// The factory's world data. Owned here, not by BuildingSystem, so the systems
// that operate on it can be handed the same state (see FactoryState.h).
FactoryState m_factoryState;
BeltSystem m_beltSystem;
std::unique_ptr<BuildingSystem> m_buildingSystem;
std::unique_ptr<ShipSystem> m_shipSystem;