Capture complete immutable world views at simulation boundaries

This commit is contained in:
faumaray committed 2026-12-02 21:36:31 +03:00
1 parent 68f91a236a
commit fd17a2f3ff
6 files changed
+350 -35

No files matched your search

+46 -29
View File
@@ -345,7 +345,47 @@ struct ProductionOrder {
}
};
class World {
namespace detail {
// Logical data and observable counters share one copy operation at publication.
// Execution resources and rebuildable caches belong to World.
struct WorldState {
WorldState(std::uint64_t seed, std::int32_t width, std::int32_t height) noexcept
: seed_(seed), width_(width), height_(height) {}
std::uint64_t seed_;
std::uint64_t tick_ = 0;
std::uint64_t path_searches_ = 0;
std::uint64_t path_nodes_visited_ = 0;
std::uint64_t assignment_path_cache_hits_ = 0;
std::uint64_t jobs_completed_ = 0;
std::uint64_t meals_eaten_ = 0;
bool hunger_enabled_ = false;
std::int32_t width_;
std::int32_t height_;
AgentId next_agent_id_ = 1;
JobId next_job_id_ = 1;
std::uint64_t next_resource_node_id_ = 1;
std::uint64_t next_storage_id_ = 1;
std::vector<std::uint8_t> blocked_;
std::vector<Agent> agents_;
std::vector<WorkJob> jobs_;
std::vector<ResourceNode> resource_nodes_;
std::vector<Storage> storages_;
std::vector<ProductionOrder> production_orders_;
bool scenario_active_ = false;
bool staged_beds_ = false;
bool paused_ = false;
bool scenario_completed_ = false;
WorkPriority work_priority_ = WorkPriority::Balanced;
GridPosition scenario_sawmill_;
std::array<std::uint64_t, colony_player_slots> player_sequences_{};
std::uint64_t command_revision_ = 0;
};
} // namespace detail
class World : private detail::WorldState {
public:
// SerialReference uses one logical worker and executes on the calling thread.
World(std::uint64_t seed, std::int32_t width, std::int32_t height,
@@ -388,6 +428,9 @@ public:
void step(TickProfile* profile = nullptr);
void write_svg(const std::string& path) const;
void write_save(std::ostream& stream) const;
// Called by the owner after every stage has drained. The independent copy
// can be retained by readers on other threads while the owner advances.
[[nodiscard]] std::shared_ptr<const World> capture() const;
[[nodiscard]] static std::unique_ptr<World> read_save(
std::istream& stream, std::size_t workers = min_worker_count, ExecutionMode mode = ExecutionMode::Parallel,
WorkerPoolOptions worker_options = {});
@@ -412,6 +455,8 @@ public:
private:
friend struct WorldSaveCodec;
struct CaptureTag {};
World(CaptureTag, const World& source);
struct PathResult {
std::vector<GridPosition> positions;
std::uint64_t nodes_visited = 0;
@@ -439,39 +484,11 @@ private:
void grow_sites();
[[nodiscard]] bool can_reach_object_cell(GridPosition position) const;
std::uint64_t seed_;
std::uint64_t tick_ = 0;
std::uint64_t path_searches_ = 0;
std::uint64_t path_nodes_visited_ = 0;
std::uint64_t assignment_path_cache_hits_ = 0;
std::uint64_t jobs_completed_ = 0;
std::uint64_t meals_eaten_ = 0;
bool hunger_enabled_ = false;
std::int32_t width_;
std::int32_t height_;
std::size_t worker_count_;
ExecutionMode execution_mode_;
WorkerPoolOptions worker_options_;
std::unique_ptr<WorkerPool> worker_pool_;
AgentId next_agent_id_ = 1;
JobId next_job_id_ = 1;
std::uint64_t next_resource_node_id_ = 1;
std::uint64_t next_storage_id_ = 1;
std::vector<std::uint8_t> blocked_;
std::unordered_map<std::uint64_t, PathResult> assignment_path_cache_;
std::vector<Agent> agents_;
std::vector<WorkJob> jobs_;
std::vector<ResourceNode> resource_nodes_;
std::vector<Storage> storages_;
std::vector<ProductionOrder> production_orders_;
bool scenario_active_ = false;
bool staged_beds_ = false;
bool paused_ = false;
bool scenario_completed_ = false;
WorkPriority work_priority_ = WorkPriority::Balanced;
GridPosition scenario_sawmill_;
std::array<std::uint64_t, colony_player_slots> player_sequences_{};
std::uint64_t command_revision_ = 0;
};
inline constexpr std::int32_t first_colony_map_size = 32;
+13 -5
View File
@@ -282,13 +282,21 @@ void Storage::add_item(ItemType type) {
World::World(std::uint64_t seed, std::int32_t width, std::int32_t height,
std::size_t workers, ExecutionMode mode, WorkerPoolOptions worker_options)
: seed_(seed),
width_(width),
height_(height),
: WorldState(seed, width, height),
worker_count_(checked_worker_count(workers, mode)),
execution_mode_(mode),
worker_options_(std::move(worker_options)),
blocked_(checked_cell_count(width, height), 0) {}
worker_options_(std::move(worker_options)) {
blocked_.resize(checked_cell_count(width, height), 0);
}
World::World(CaptureTag, const World& source)
: WorldState(source),
worker_count_(source.worker_count_),
execution_mode_(source.execution_mode_) {}
std::shared_ptr<const World> World::capture() const {
return std::shared_ptr<const World>(new const World(CaptureTag{}, *this));
}
std::size_t World::cell_index(GridPosition position) const noexcept {
return static_cast<std::size_t>(position.y) * static_cast<std::size_t>(width_) +