Exercise replay against the modern custody state and migration
This commit is contained in:
commit
3c7cc3ce2c
23 files changed
+2615
-93
No files matched your search
@@ -15,7 +15,7 @@
|
||||
|
||||
namespace settlement {
|
||||
|
||||
inline constexpr std::uint32_t checkpoint_version = 2;
|
||||
inline constexpr std::uint32_t checkpoint_version = 3;
|
||||
inline constexpr std::string_view checkpoint_signature = "STHCHKPT";
|
||||
inline constexpr std::size_t max_checkpoint_body_bytes = 128 * 1024 * 1024;
|
||||
inline constexpr std::size_t max_checkpoint_metadata_bytes = 64 * 1024;
|
||||
@@ -47,10 +47,10 @@ struct WorldCheckpoint {
|
||||
RulesManifest rules;
|
||||
};
|
||||
|
||||
// Both schemas store magic, schema, metadata length and body length, then world UUID,
|
||||
// All schemas store magic, schema, metadata length and body length, then world UUID,
|
||||
// city, tick, rules fingerprint and the sorted complete package closure. Each
|
||||
// package stores its ID, exact version and SHA-256. The body uses World's save
|
||||
// codec: schema 1 accepts legacy bodies 1–13; schema 2 requires body 14.
|
||||
// codec: schema 1 accepts bodies 1–13; schema 2 requires 14; schema 3 requires 15.
|
||||
// A final SHA-256 covers every preceding byte. Integers are little-endian.
|
||||
// The caller holds a stable World for the entire synchronous encode operation.
|
||||
[[nodiscard]] std::expected<std::string, CheckpointDiagnostic> encode_checkpoint(
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace settlement {
|
||||
|
||||
enum class ItemType : std::uint8_t;
|
||||
using StackId = std::uint64_t;
|
||||
using ReservationId = std::uint64_t;
|
||||
using TransferId = std::uint64_t;
|
||||
|
||||
// Recipe quantities are not property and never carry an identity.
|
||||
struct ItemAmount {
|
||||
ItemType type{};
|
||||
std::uint32_t count = 0;
|
||||
bool operator==(const ItemAmount&) const = default;
|
||||
};
|
||||
enum class OwnerKind : std::uint8_t { None, Storage, Resident, Production };
|
||||
struct StockOwner {
|
||||
OwnerKind kind = OwnerKind::None;
|
||||
std::uint64_t id = 0;
|
||||
bool operator==(const StockOwner&) const = default;
|
||||
};
|
||||
enum class ReservationKind : std::uint8_t { Item, Capacity, Worksite, Harvest, Tending };
|
||||
struct Reservation {
|
||||
ReservationId id = 0;
|
||||
ReservationKind kind = ReservationKind::Item;
|
||||
StockOwner owner{};
|
||||
StackId stack = 0;
|
||||
ItemType item{};
|
||||
std::uint32_t count = 0;
|
||||
ReservationId backing = 0;
|
||||
std::uint64_t origin_storage = 0;
|
||||
bool operator==(const Reservation&) const = default;
|
||||
};
|
||||
enum class TransferKind : std::uint8_t { Production, Harvest, Tending, Meal, Return };
|
||||
enum class CustodyStatus : std::uint8_t {
|
||||
Accepted, Conflict, Stale, Future, Invalid, Busy, Capacity, Exhausted, Overflow, NoDestination
|
||||
};
|
||||
enum class CustodyOperation : std::uint8_t { Admit, Pickup, Deliver, Complete, Cancel, Return, Harvest, Eat, Tend };
|
||||
enum class CustodyKeyKind : std::uint8_t { Admission, Operation };
|
||||
struct CustodyKey {
|
||||
CustodyKeyKind kind = CustodyKeyKind::Admission;
|
||||
std::uint64_t id = 0; // AgentId for admission, TransferId for operation.
|
||||
std::uint64_t ordinal = 0;
|
||||
bool operator==(const CustodyKey&) const = default;
|
||||
};
|
||||
struct CustodyLine {
|
||||
std::uint64_t storage = 0;
|
||||
ItemType item{};
|
||||
std::uint32_t count = 0;
|
||||
bool operator==(const CustodyLine&) const = default;
|
||||
};
|
||||
inline constexpr std::size_t custody_material_limit = 5;
|
||||
inline constexpr std::size_t custody_reservation_limit = 8;
|
||||
inline constexpr std::size_t custody_receipt_limit = 4096;
|
||||
struct CustodyRequest {
|
||||
CustodyKey key{};
|
||||
CustodyOperation operation = CustodyOperation::Admit;
|
||||
TransferKind kind = TransferKind::Production;
|
||||
std::uint64_t production = 0;
|
||||
std::uint64_t source = 0;
|
||||
std::uint64_t destination = 0;
|
||||
std::array<CustodyLine, custody_material_limit> lines{};
|
||||
std::uint8_t line_count = 0;
|
||||
bool operator==(const CustodyRequest&) const = default;
|
||||
};
|
||||
struct CustodyEffect {
|
||||
StockOwner owner{};
|
||||
StackId stack = 0;
|
||||
ItemType item{};
|
||||
std::uint32_t count = 0;
|
||||
bool operator==(const CustodyEffect&) const = default;
|
||||
};
|
||||
struct CustodyResult {
|
||||
CustodyStatus status = CustodyStatus::Invalid;
|
||||
TransferId transfer = 0;
|
||||
std::array<ReservationId, custody_reservation_limit> reservations{};
|
||||
std::array<CustodyEffect, 11> effects{};
|
||||
std::uint8_t reservation_count = 0;
|
||||
std::uint8_t effect_count = 0;
|
||||
bool terminal = false;
|
||||
bool operator==(const CustodyResult&) const = default;
|
||||
};
|
||||
struct CustodyReceipt {
|
||||
CustodyRequest request{};
|
||||
CustodyResult result{};
|
||||
};
|
||||
struct TransferExecution {
|
||||
TransferId id = 0;
|
||||
TransferKind kind = TransferKind::Production;
|
||||
std::uint64_t production = 0;
|
||||
std::uint64_t source = 0;
|
||||
std::uint64_t destination = 0;
|
||||
std::uint64_t next_operation = 1;
|
||||
std::array<Reservation, custody_reservation_limit> reservations{};
|
||||
std::uint8_t reservation_count = 0;
|
||||
CustodyStatus blocked = CustodyStatus::Accepted;
|
||||
};
|
||||
struct ItemBalance {
|
||||
std::uint64_t opening = 0;
|
||||
std::uint64_t grant = 0;
|
||||
std::uint64_t harvest = 0;
|
||||
std::uint64_t recipe_output = 0;
|
||||
std::uint64_t recipe_input = 0;
|
||||
std::uint64_t meal = 0;
|
||||
std::uint64_t installation = 0;
|
||||
std::uint64_t demolition = 0;
|
||||
};
|
||||
|
||||
static_assert(sizeof(CustodyRequest) <= 1024 && sizeof(CustodyResult) <= 1024);
|
||||
|
||||
} // namespace settlement
|
||||
@@ -13,6 +13,7 @@
|
||||
#include <vector>
|
||||
|
||||
#include "settlement/core/worker_pool.hpp"
|
||||
#include "settlement/core/custody.hpp"
|
||||
|
||||
namespace settlement {
|
||||
|
||||
@@ -22,7 +23,12 @@ using ResourceNodeId = std::uint64_t;
|
||||
using StorageId = std::uint64_t;
|
||||
using ProductionId = std::uint64_t;
|
||||
|
||||
enum class WorldSaveFormat : std::uint32_t { Legacy13 = 13, Current14 = 14 };
|
||||
enum class WorldSaveFormat : std::uint32_t { Legacy13 = 13, Current14 = 14, Current15 = 15 };
|
||||
|
||||
class UnsupportedLegacyState : public std::runtime_error {
|
||||
public:
|
||||
UnsupportedLegacyState() : std::runtime_error("state has no exact legacy representation") {}
|
||||
};
|
||||
|
||||
// Raised only by a complete production-ID preflight, before any domain effects.
|
||||
class ProductionCapacityError : public std::overflow_error {
|
||||
@@ -47,7 +53,8 @@ enum class AgentState : std::uint8_t {
|
||||
GoingToEat,
|
||||
Eating,
|
||||
GoingToSleep,
|
||||
Sleeping
|
||||
Sleeping,
|
||||
Returning
|
||||
};
|
||||
|
||||
enum class JobState : std::uint8_t {
|
||||
@@ -237,9 +244,13 @@ public:
|
||||
void add(ItemType type, std::uint32_t count = 1) noexcept;
|
||||
void remove(ItemType type, std::uint32_t count = 1) noexcept;
|
||||
[[nodiscard]] ItemType first_item() const noexcept;
|
||||
|
||||
[[nodiscard]] StackId stack_id(ItemType type) const noexcept;
|
||||
private:
|
||||
friend class World;
|
||||
friend struct WorldSaveCodec;
|
||||
void set_stack_id(ItemType type, StackId id) noexcept;
|
||||
std::array<std::uint32_t, item_type_count> counts_{};
|
||||
std::array<StackId, item_type_count> stack_ids_{};
|
||||
};
|
||||
|
||||
struct Agent {
|
||||
@@ -260,6 +271,9 @@ struct Agent {
|
||||
bool resting_on_ground = false;
|
||||
Profession profession = Profession::Generalist;
|
||||
|
||||
std::uint64_t next_admission = 1;
|
||||
std::optional<TransferExecution> execution;
|
||||
|
||||
[[nodiscard]] bool hungry() const noexcept { return hunger >= hunger_threshold; }
|
||||
[[nodiscard]] bool tired() const noexcept { return fatigue >= fatigue_threshold; }
|
||||
};
|
||||
@@ -292,6 +306,7 @@ struct ItemStack {
|
||||
ItemType type = ItemType::None;
|
||||
std::uint32_t count = 0;
|
||||
std::uint32_t reserved = 0;
|
||||
StackId id = 0;
|
||||
};
|
||||
|
||||
struct Storage {
|
||||
@@ -351,6 +366,8 @@ struct ProductionOrder {
|
||||
AgentId bed_owner = 0;
|
||||
BedBase bed_base = BedBase::Wood;
|
||||
BedStage bed_stage = BedStage::Legacy;
|
||||
Inventory input;
|
||||
bool suspended = false;
|
||||
|
||||
[[nodiscard]] ItemType bed_material() const noexcept {
|
||||
if (bed_stage == BedStage::Legacy) return ItemType::Plank;
|
||||
@@ -382,6 +399,12 @@ struct WorldState {
|
||||
std::uint64_t next_resource_node_id_ = 1;
|
||||
std::uint64_t next_storage_id_ = 1;
|
||||
ProductionId next_production_id_ = 1;
|
||||
StackId next_stack_id_ = 1;
|
||||
ReservationId next_reservation_id_ = 1;
|
||||
TransferId next_transfer_id_ = 1;
|
||||
std::array<ItemBalance, item_type_count> item_balances_{};
|
||||
std::vector<std::shared_ptr<const CustodyReceipt>> custody_receipts_;
|
||||
std::size_t custody_receipt_cursor_ = 0;
|
||||
std::vector<std::uint8_t> blocked_;
|
||||
std::vector<Agent> agents_;
|
||||
std::vector<WorkJob> jobs_;
|
||||
@@ -417,12 +440,19 @@ public:
|
||||
StorageKind kind = StorageKind::Materials);
|
||||
void stock_storage(std::uint64_t id, ItemType type, std::uint32_t count);
|
||||
void enable_hunger() noexcept;
|
||||
[[nodiscard]] CommandStatus cancel_production(ProductionId id);
|
||||
[[nodiscard]] CommandStatus resume_production(ProductionId id);
|
||||
[[nodiscard]] bool legacy_representable() const noexcept;
|
||||
void validate_custody() const;
|
||||
[[nodiscard]] CustodyResult custody_request(const CustodyRequest& request);
|
||||
[[nodiscard]] const auto& item_balances() const noexcept { return item_balances_; }
|
||||
[[nodiscard]] const auto& custody_receipts() const noexcept { return custody_receipts_; }
|
||||
void queue_production(GridPosition position, RecipeId recipe);
|
||||
[[nodiscard]] bool can_place_cabin(GridPosition position) const noexcept;
|
||||
[[nodiscard]] bool can_place_room(const RoomPlan& plan) const noexcept;
|
||||
[[nodiscard]] std::array<ItemStack, 2> construction_materials(RecipeId recipe, BedBase base) const;
|
||||
[[nodiscard]] std::array<ItemAmount, 2> construction_materials(RecipeId recipe, BedBase base) const;
|
||||
[[nodiscard]] RecipeId room_construction_recipe(RecipeId part) const noexcept;
|
||||
[[nodiscard]] std::array<ItemStack, 2> carpentry_materials(RecipeId recipe) const;
|
||||
[[nodiscard]] std::array<ItemAmount, 2> carpentry_materials(RecipeId recipe) const;
|
||||
[[nodiscard]] std::optional<GridPosition> carpenter_workshop() const noexcept {
|
||||
if (!scenario_active_) return std::nullopt;
|
||||
return scenario_sawmill_;
|
||||
@@ -475,6 +505,20 @@ public:
|
||||
|
||||
private:
|
||||
friend struct WorldSaveCodec;
|
||||
void migrate_legacy_custody();
|
||||
[[nodiscard]] bool admit_custody(Agent& agent, TransferKind kind, ProductionId production,
|
||||
ResourceNodeId source, StorageId destination, const std::vector<MaterialReservation>& materials);
|
||||
void finish_custody(Agent& agent) noexcept;
|
||||
void advance_return(Agent& agent);
|
||||
[[nodiscard]] bool pickup_custody(Agent& agent, Storage& source,
|
||||
const std::vector<MaterialReservation>& materials, bool reserve_output);
|
||||
[[nodiscard]] bool deliver_custody(Agent& agent, Inventory& destination, StockOwner owner);
|
||||
[[nodiscard]] bool deposit_custody(Agent& agent, Storage& destination, ItemType type, std::uint32_t count);
|
||||
void record_custody(std::shared_ptr<const CustodyReceipt> receipt) noexcept;
|
||||
[[nodiscard]] std::shared_ptr<const CustodyReceipt> prepare_custody(
|
||||
Agent& agent, CustodyOperation operation, CustodyStatus status = CustodyStatus::Accepted,
|
||||
StorageId source = 0, StorageId destination = 0);
|
||||
void commit_custody(Agent& agent, std::shared_ptr<const CustodyReceipt> receipt) noexcept;
|
||||
struct CaptureTag {};
|
||||
World(CaptureTag, const World& source);
|
||||
struct PathResult {
|
||||
|
||||
Reference in new issue
Block a user