Track transfers, reservations and item owners through colony work
This commit is contained in:
1 parent
4f42488d54
commit
6c6f4e7857
4 files changed
+972
-72
No files matched your search
@@ -0,0 +1,606 @@
|
||||
#include "settlement/core/world.hpp"
|
||||
#include "settlement/core/inventory_transaction.hpp"
|
||||
#include <Void/Math/Numeric.hpp>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <tuple>
|
||||
|
||||
namespace settlement {
|
||||
namespace {
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
bool room(std::uint64_t next, std::uint64_t count) noexcept {
|
||||
return next != 0 && count <= maximum - next;
|
||||
}
|
||||
auto stock(Storage& storage, ItemType item) {
|
||||
return std::find_if(storage.items.begin(), storage.items.end(), [item](const auto& s) { return s.type == item; });
|
||||
}
|
||||
void retarget(TransferExecution& execution, StockOwner from, StackId old_id,
|
||||
StockOwner to, StackId new_id) noexcept {
|
||||
for (auto& claim : execution.reservations) {
|
||||
if (claim.id && claim.kind == ReservationKind::Item && claim.owner == from && claim.stack == old_id) {
|
||||
claim.owner = to;
|
||||
claim.stack = new_id;
|
||||
}
|
||||
}
|
||||
}
|
||||
CustodyRequest operation_request(const Agent& agent, CustodyOperation operation,
|
||||
StorageId source, StorageId destination) {
|
||||
CustodyRequest request;
|
||||
request.key = {CustodyKeyKind::Operation, agent.execution->id, agent.execution->next_operation};
|
||||
request.operation = operation;
|
||||
request.kind = agent.execution->kind;
|
||||
request.production = agent.execution->production;
|
||||
request.source = source;
|
||||
request.destination = destination;
|
||||
return request;
|
||||
}
|
||||
void effects(CustodyResult& result, const Inventory& inventory, StockOwner owner) {
|
||||
for (std::size_t i = 1; i <= item_type_count; ++i) {
|
||||
const auto item = static_cast<ItemType>(i);
|
||||
if (inventory.item_count(item)) result.effects[result.effect_count++] =
|
||||
{owner, inventory.stack_id(item), item, inventory.item_count(item)};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
StackId Inventory::stack_id(ItemType type) const noexcept {
|
||||
const auto i = static_cast<std::size_t>(type);
|
||||
return i > 0 && i <= stack_ids_.size() ? stack_ids_[i - 1] : 0;
|
||||
}
|
||||
void Inventory::set_stack_id(ItemType type, StackId id) noexcept {
|
||||
const auto i = static_cast<std::size_t>(type);
|
||||
if (i > 0 && i <= stack_ids_.size()) stack_ids_[i - 1] = id;
|
||||
}
|
||||
|
||||
void World::record_custody(std::shared_ptr<const CustodyReceipt> receipt) noexcept {
|
||||
if (custody_receipts_.size() < custody_receipt_limit) custody_receipts_.push_back(std::move(receipt));
|
||||
else {
|
||||
custody_receipts_[custody_receipt_cursor_] = std::move(receipt);
|
||||
custody_receipt_cursor_ = (custody_receipt_cursor_ + 1) % custody_receipt_limit;
|
||||
}
|
||||
}
|
||||
std::shared_ptr<const CustodyReceipt> World::prepare_custody(Agent& agent, CustodyOperation operation,
|
||||
CustodyStatus status, StorageId source, StorageId destination) {
|
||||
if (!agent.execution || !room(agent.execution->next_operation, 1)) return {};
|
||||
custody_receipts_.reserve(custody_receipt_limit);
|
||||
auto receipt = std::make_shared<CustodyReceipt>();
|
||||
receipt->request = operation_request(agent, operation, source, destination);
|
||||
receipt->result.status = status;
|
||||
receipt->result.transfer = agent.execution->id;
|
||||
return receipt;
|
||||
}
|
||||
void World::commit_custody(Agent& agent, std::shared_ptr<const CustodyReceipt> receipt) noexcept {
|
||||
agent.execution->blocked = receipt->result.status;
|
||||
++agent.execution->next_operation;
|
||||
record_custody(std::move(receipt));
|
||||
}
|
||||
CustodyResult World::custody_request(const CustodyRequest& request) {
|
||||
// The scheduler issues operations. This endpoint recovers their exact result;
|
||||
// it never admits unissued work through an out-of-order retry.
|
||||
for (const auto& receipt : custody_receipts_) {
|
||||
if (receipt->request.key == request.key) {
|
||||
return receipt->request == request ? receipt->result : CustodyResult{CustodyStatus::Conflict};
|
||||
}
|
||||
}
|
||||
if (request.key.id == 0 || request.key.ordinal == 0) return {CustodyStatus::Invalid};
|
||||
if (request.key.kind == CustodyKeyKind::Admission) {
|
||||
const auto actor = std::find_if(agents_.begin(), agents_.end(), [&](const auto& a) { return a.id == request.key.id; });
|
||||
if (actor == agents_.end()) return {CustodyStatus::Invalid};
|
||||
return {request.key.ordinal < actor->next_admission ? CustodyStatus::Stale : CustodyStatus::Future};
|
||||
}
|
||||
if (request.key.kind != CustodyKeyKind::Operation) return {CustodyStatus::Invalid};
|
||||
for (const auto& agent : agents_) {
|
||||
if (agent.execution && agent.execution->id == request.key.id) {
|
||||
return {request.key.ordinal < agent.execution->next_operation ? CustodyStatus::Stale : CustodyStatus::Future};
|
||||
}
|
||||
}
|
||||
return {request.key.id < next_transfer_id_ ? CustodyStatus::Stale : CustodyStatus::Future};
|
||||
}
|
||||
|
||||
bool World::admit_custody(Agent& agent, TransferKind kind, ProductionId production,
|
||||
ResourceNodeId source, StorageId destination, const std::vector<MaterialReservation>& materials) {
|
||||
if (agent.execution || !room(next_transfer_id_, 1) || !room(agent.next_admission, 1) ||
|
||||
materials.size() > custody_material_limit) return false;
|
||||
TransferExecution execution;
|
||||
execution.id = next_transfer_id_;
|
||||
execution.kind = kind;
|
||||
execution.production = production;
|
||||
execution.source = source;
|
||||
execution.destination = destination;
|
||||
auto next = next_reservation_id_;
|
||||
const auto add = [&](Reservation claim) {
|
||||
if (!room(next, 1) || execution.reservation_count == custody_reservation_limit) return false;
|
||||
claim.id = next++;
|
||||
execution.reservations[execution.reservation_count++] = claim;
|
||||
return true;
|
||||
};
|
||||
CustodyRequest request;
|
||||
request.key = {CustodyKeyKind::Admission, agent.id, agent.next_admission};
|
||||
request.kind = kind;
|
||||
request.production = production;
|
||||
request.source = source;
|
||||
request.destination = destination;
|
||||
for (const auto& material : materials) {
|
||||
auto& store = storage_by_id(material.storage_id);
|
||||
const auto stack = stock(store, material.type);
|
||||
if (material.count == 0 || stack == store.items.end() || stack->id == 0 ||
|
||||
stack->reserved > stack->count || material.count > stack->count - stack->reserved) return false;
|
||||
if (!add({0, ReservationKind::Item, {OwnerKind::Storage, store.id}, stack->id,
|
||||
material.type, material.count, 0, store.id})) return false;
|
||||
request.lines[request.line_count++] = {store.id, material.type, material.count};
|
||||
}
|
||||
if (production && !add({0, ReservationKind::Worksite, {OwnerKind::Production, production}, 0, {}, 1})) return false;
|
||||
if (source && !add({0, kind == TransferKind::Tending ? ReservationKind::Tending : ReservationKind::Harvest,
|
||||
{}, 0, {}, 1})) return false;
|
||||
if (destination) {
|
||||
auto& store = storage_by_id(destination);
|
||||
ReservationId backing = 0;
|
||||
if (production) {
|
||||
// Every production output replaces claimed input in this exact store.
|
||||
for (const auto& claim : execution.reservations) {
|
||||
if (claim.id && claim.kind == ReservationKind::Item && claim.origin_storage == destination) {
|
||||
backing = claim.id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!backing && store.total_items() + store.reserved_slots >= store.capacity) return false;
|
||||
if (!add({0, ReservationKind::Capacity, {OwnerKind::Storage, destination}, 0, {}, 1, backing})) return false;
|
||||
}
|
||||
custody_receipts_.reserve(custody_receipt_limit);
|
||||
auto receipt = std::make_shared<CustodyReceipt>();
|
||||
receipt->request = request;
|
||||
receipt->result = {CustodyStatus::Accepted, execution.id};
|
||||
receipt->result.reservation_count = execution.reservation_count;
|
||||
for (std::size_t i = 0; i < execution.reservation_count; ++i) receipt->result.reservations[i] = execution.reservations[i].id;
|
||||
// The caller has already allocated routes and material vectors. No operation
|
||||
// below can throw; its existing cache increments follow immediately.
|
||||
agent.execution = execution;
|
||||
++agent.next_admission;
|
||||
++next_transfer_id_;
|
||||
next_reservation_id_ = next;
|
||||
record_custody(std::move(receipt));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool World::pickup_custody(Agent& agent, Storage& source,
|
||||
const std::vector<MaterialReservation>& materials, bool reserve_output) {
|
||||
Storage planned_source = source;
|
||||
Inventory planned_inventory = agent.inventory;
|
||||
auto execution = *agent.execution;
|
||||
auto next = next_stack_id_;
|
||||
custody_receipts_.reserve(custody_receipt_limit);
|
||||
auto receipt = std::make_shared<CustodyReceipt>();
|
||||
receipt->request = operation_request(agent, CustodyOperation::Pickup, source.id, 0);
|
||||
receipt->result = {CustodyStatus::Accepted, execution.id};
|
||||
const auto fail = [&](CustodyStatus status) {
|
||||
receipt->result.status = status;
|
||||
commit_custody(agent, receipt);
|
||||
return false;
|
||||
};
|
||||
if (!room(execution.next_operation, 1)) return false;
|
||||
if (!withdraw_reserved_materials(planned_source, planned_inventory, materials, reserve_output)) return fail(CustodyStatus::Invalid);
|
||||
for (const auto& stack : source.items) {
|
||||
const auto remaining = planned_source.item_count(stack.type);
|
||||
if (remaining == stack.count) continue;
|
||||
auto id = agent.inventory.stack_id(stack.type);
|
||||
if (!id) {
|
||||
if (remaining) {
|
||||
if (!room(next, 1)) return fail(CustodyStatus::Exhausted);
|
||||
id = next++;
|
||||
} else id = stack.id;
|
||||
}
|
||||
if (!id) return fail(CustodyStatus::Invalid);
|
||||
planned_inventory.set_stack_id(stack.type, id);
|
||||
retarget(execution, {OwnerKind::Storage, source.id}, stack.id, {OwnerKind::Resident, agent.id}, id);
|
||||
if (execution.kind == TransferKind::Meal) for (auto& claim : execution.reservations) {
|
||||
if (claim.kind == ReservationKind::Item) claim.origin_storage = 0;
|
||||
}
|
||||
}
|
||||
if (reserve_output) {
|
||||
for (auto& claim : execution.reservations) {
|
||||
if (claim.id && claim.kind == ReservationKind::Capacity && claim.owner.id == source.id) claim.backing = 0;
|
||||
}
|
||||
}
|
||||
effects(receipt->result, planned_inventory, {OwnerKind::Resident, agent.id});
|
||||
source.items.swap(planned_source.items);
|
||||
source.reserved_slots = planned_source.reserved_slots;
|
||||
agent.inventory = planned_inventory;
|
||||
agent.execution = execution;
|
||||
next_stack_id_ = next;
|
||||
commit_custody(agent, receipt);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool World::deliver_custody(Agent& agent, Inventory& destination, StockOwner owner) {
|
||||
auto planned = destination;
|
||||
auto execution = *agent.execution;
|
||||
custody_receipts_.reserve(custody_receipt_limit);
|
||||
auto receipt = std::make_shared<CustodyReceipt>();
|
||||
receipt->request = operation_request(agent, CustodyOperation::Deliver, 0, 0);
|
||||
receipt->result = {CustodyStatus::Accepted, execution.id};
|
||||
if (!room(execution.next_operation, 1)) return false;
|
||||
for (std::size_t i = 1; i <= item_type_count; ++i) {
|
||||
const auto type = static_cast<ItemType>(i);
|
||||
const auto count = agent.inventory.item_count(type);
|
||||
if (!count) continue;
|
||||
if (!Void::Numeric::checked_add(planned.item_count(type), count)) {
|
||||
receipt->result.status = CustodyStatus::Overflow;
|
||||
commit_custody(agent, receipt);
|
||||
return false;
|
||||
}
|
||||
auto id = planned.stack_id(type);
|
||||
if (!id) id = agent.inventory.stack_id(type);
|
||||
planned.add(type, count);
|
||||
planned.set_stack_id(type, id);
|
||||
retarget(execution, {OwnerKind::Resident, agent.id}, agent.inventory.stack_id(type), owner, id);
|
||||
}
|
||||
effects(receipt->result, planned, owner);
|
||||
destination = planned;
|
||||
agent.inventory = {};
|
||||
agent.execution = execution;
|
||||
commit_custody(agent, receipt);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool World::deposit_custody(Agent& agent, Storage& destination, ItemType type, std::uint32_t count) {
|
||||
if (!count || count > agent.inventory.item_count(type) || !destination.accepts(type)) return false;
|
||||
auto planned = destination;
|
||||
auto inventory = agent.inventory;
|
||||
auto next = next_stack_id_;
|
||||
auto execution = *agent.execution;
|
||||
custody_receipts_.reserve(custody_receipt_limit);
|
||||
auto receipt = std::make_shared<CustodyReceipt>();
|
||||
receipt->request = operation_request(agent, execution.kind == TransferKind::Return ? CustodyOperation::Return : CustodyOperation::Deliver, 0, destination.id);
|
||||
receipt->request.lines[0] = {destination.id, type, count};
|
||||
receipt->request.line_count = 1;
|
||||
receipt->result = {CustodyStatus::Accepted, execution.id};
|
||||
if (!room(execution.next_operation, 1)) return false;
|
||||
auto slots = planned.reserved_slots;
|
||||
for (auto& claim : execution.reservations) {
|
||||
if (claim.id && claim.kind == ReservationKind::Capacity && claim.owner.id == destination.id && !claim.backing) {
|
||||
if (slots < claim.count) return false;
|
||||
slots -= claim.count;
|
||||
claim = {};
|
||||
}
|
||||
}
|
||||
if (planned.total_items() + slots + count > planned.capacity) return false;
|
||||
auto target = stock(planned, type);
|
||||
StackId id = 0;
|
||||
if (target != planned.items.end()) {
|
||||
const auto amount = Void::Numeric::checked_add(target->count, count);
|
||||
if (!amount) return false;
|
||||
target->count = *amount;
|
||||
id = target->id;
|
||||
} else {
|
||||
id = inventory.stack_id(type);
|
||||
if (count < inventory.item_count(type)) {
|
||||
if (!room(next, 1)) return false;
|
||||
id = next++;
|
||||
}
|
||||
planned.items.push_back({type, count, 0, id});
|
||||
std::sort(planned.items.begin(), planned.items.end(), [](const auto& a, const auto& b) { return a.type < b.type; });
|
||||
}
|
||||
inventory.remove(type, count);
|
||||
receipt->result.effects[0] = {{OwnerKind::Storage, destination.id}, id, type, planned.item_count(type)};
|
||||
receipt->result.effect_count = 1;
|
||||
receipt->result.terminal = inventory.empty();
|
||||
for (auto& claim : execution.reservations) {
|
||||
if (claim.id && claim.kind == ReservationKind::Item && claim.owner == StockOwner{OwnerKind::Resident, agent.id} && claim.item == type) {
|
||||
if (claim.count <= count) claim = {};
|
||||
else claim.count -= count;
|
||||
}
|
||||
}
|
||||
destination.items.swap(planned.items);
|
||||
destination.reserved_slots = slots;
|
||||
agent.inventory = inventory;
|
||||
agent.execution = execution;
|
||||
next_stack_id_ = next;
|
||||
commit_custody(agent, receipt);
|
||||
return true;
|
||||
}
|
||||
|
||||
void World::finish_custody(Agent& agent) noexcept { agent.execution.reset(); }
|
||||
|
||||
bool World::legacy_representable() const noexcept {
|
||||
for (const auto& order : production_orders_) if (order.suspended) return false;
|
||||
for (const auto& agent : agents_) {
|
||||
if (agent.state == AgentState::Returning || (agent.execution && agent.execution->kind == TransferKind::Return && agent.state != AgentState::Hauling)) return false;
|
||||
if ((agent.state == AgentState::Working || agent.state == AgentState::Gathering || agent.state == AgentState::Eating) && agent.work_remaining == 0) return false;
|
||||
if (agent.production_id == 0 && !agent.inventory.empty()) {
|
||||
const auto type = agent.inventory.first_item();
|
||||
if (agent.state == AgentState::Eating) {
|
||||
if (type != ItemType::Berries || agent.inventory.item_count(type) != 1) return false;
|
||||
} else if (agent.state != AgentState::Hauling || agent.storage_id == 0 ||
|
||||
type == ItemType::Plank || type == ItemType::StoneBlock || type == ItemType::Cloth ||
|
||||
agent.inventory.item_count(type) != 1) return false;
|
||||
for (std::size_t i = 1; i <= item_type_count; ++i) {
|
||||
if (static_cast<ItemType>(i) != type && agent.inventory.item_count(static_cast<ItemType>(i))) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
CommandStatus World::cancel_production(ProductionId id) {
|
||||
auto it = std::find_if(production_orders_.begin(), production_orders_.end(), [id](const auto& p) { return p.id == id; });
|
||||
if (it == production_orders_.end()) return CommandStatus::InvalidTarget;
|
||||
if (it->state == JobState::Completed || it->suspended) return CommandStatus::Busy;
|
||||
auto actor = std::find_if(agents_.begin(), agents_.end(), [id](const auto& a) { return a.production_id == id; });
|
||||
const bool retain = it->recipe == RecipeId::WeaveCloth ||
|
||||
(it->recipe == RecipeId::BuildBed && (it->bed_stage == BedStage::Bedding || it->bed_stage == BedStage::Pillow));
|
||||
if (actor != agents_.end()) {
|
||||
if (!actor->execution) return CommandStatus::Busy;
|
||||
auto inventory = actor->inventory;
|
||||
for (std::size_t i = 1; i <= item_type_count; ++i) {
|
||||
const auto item = static_cast<ItemType>(i);
|
||||
const auto count = it->input.item_count(item);
|
||||
if (!Void::Numeric::checked_add(inventory.item_count(item), count)) return CommandStatus::Busy;
|
||||
if (count) {
|
||||
if (!inventory.stack_id(item)) inventory.set_stack_id(item, it->input.stack_id(item));
|
||||
inventory.add(item, count);
|
||||
}
|
||||
}
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(*actor, CustodyOperation::Cancel));
|
||||
if (!receipt) return CommandStatus::Busy;
|
||||
effects(receipt->result, inventory, {OwnerKind::Resident, actor->id});
|
||||
receipt->result.terminal = inventory.empty();
|
||||
for (auto& claim : actor->execution->reservations) {
|
||||
if (!claim.id) continue;
|
||||
if (claim.kind == ReservationKind::Item && claim.owner.kind == OwnerKind::Storage) {
|
||||
auto& store = storage_by_id(claim.owner.id);
|
||||
stock(store, claim.item)->reserved -= claim.count;
|
||||
} else if (claim.kind == ReservationKind::Capacity && !claim.backing) {
|
||||
storage_by_id(claim.owner.id).reserved_slots -= claim.count;
|
||||
}
|
||||
claim = {};
|
||||
}
|
||||
actor->execution->reservation_count = 0;
|
||||
actor->execution->kind = TransferKind::Return;
|
||||
actor->execution->production = 0;
|
||||
actor->execution->source = 0;
|
||||
actor->execution->destination = 0;
|
||||
actor->inventory = inventory;
|
||||
actor->production_id = 0;
|
||||
actor->storage_id = 0;
|
||||
actor->work_remaining = 0;
|
||||
actor->route.clear();
|
||||
actor->route_cursor = 0;
|
||||
actor->haul_route.clear();
|
||||
actor->state = inventory.empty() ? AgentState::Idle : AgentState::Returning;
|
||||
commit_custody(*actor, receipt);
|
||||
if (inventory.empty()) finish_custody(*actor);
|
||||
}
|
||||
if (retain) {
|
||||
it->input = {};
|
||||
it->materials.clear();
|
||||
it->delivery_stops.clear();
|
||||
it->collected_stops = 0;
|
||||
it->state = JobState::Available;
|
||||
it->suspended = true;
|
||||
} else production_orders_.erase(it);
|
||||
return CommandStatus::Accepted;
|
||||
}
|
||||
CommandStatus World::resume_production(ProductionId id) {
|
||||
auto it = std::find_if(production_orders_.begin(), production_orders_.end(), [id](const auto& p) { return p.id == id; });
|
||||
if (it == production_orders_.end()) return CommandStatus::InvalidTarget;
|
||||
if (!it->suspended || it->state != JobState::Available) return CommandStatus::Busy;
|
||||
it->suspended = false;
|
||||
return CommandStatus::Accepted;
|
||||
}
|
||||
void World::advance_return(Agent& agent) {
|
||||
if (agent.route_cursor < agent.route.size()) return;
|
||||
agent.route.clear();
|
||||
agent.route_cursor = 0;
|
||||
const auto type = agent.inventory.first_item();
|
||||
if (type == ItemType::None) { agent.state = AgentState::Idle; finish_custody(agent); return; }
|
||||
for (auto& store : storages_) {
|
||||
if (!store.accepts(type) || store.total_items() + store.reserved_slots >= store.capacity) continue;
|
||||
auto path = assignment_path(agent.position, store.position);
|
||||
if (!path.reachable) continue;
|
||||
if (!path.positions.empty()) {
|
||||
agent.route = std::move(path.positions);
|
||||
agent.execution->blocked = CustodyStatus::Accepted;
|
||||
return;
|
||||
}
|
||||
const auto free = store.capacity - store.total_items() - store.reserved_slots;
|
||||
const auto count = static_cast<std::uint32_t>(std::min<std::uint64_t>(free, agent.inventory.item_count(type)));
|
||||
if (!deposit_custody(agent, store, type, count)) return;
|
||||
if (agent.inventory.empty()) { agent.state = AgentState::Idle; finish_custody(agent); }
|
||||
return;
|
||||
}
|
||||
agent.execution->blocked = CustodyStatus::NoDestination;
|
||||
}
|
||||
|
||||
void World::validate_custody() const {
|
||||
const auto require = [](bool condition) { if (!condition) throw std::runtime_error("inconsistent custody state"); };
|
||||
require(next_stack_id_ && next_reservation_id_ && next_transfer_id_);
|
||||
struct Physical { StockOwner owner; ItemType item; std::uint32_t count; std::uint64_t claimed = 0; };
|
||||
std::unordered_map<StackId, Physical> physical;
|
||||
std::unordered_map<StorageId, std::array<std::uint64_t, item_type_count>> reserved;
|
||||
std::unordered_map<StorageId, std::uint64_t> slots;
|
||||
std::unordered_map<ResourceNodeId, std::uint64_t> workers;
|
||||
std::array<std::uint64_t, item_type_count> live{};
|
||||
const auto add = [&](StockOwner owner, ItemType item, std::uint32_t count, StackId id) {
|
||||
require((count == 0) == (id == 0));
|
||||
if (!count) return;
|
||||
require(item > ItemType::None && item <= ItemType::Loom && id < next_stack_id_);
|
||||
require(physical.emplace(id, Physical{owner, item, count}).second);
|
||||
auto& total = live[static_cast<std::size_t>(item) - 1];
|
||||
const auto amount = Void::Numeric::checked_add(total, std::uint64_t{count});
|
||||
require(amount.has_value()); total = *amount;
|
||||
};
|
||||
const auto add_inventory = [&](const Inventory& inventory, StockOwner owner) {
|
||||
for (std::size_t i = 1; i <= item_type_count; ++i) {
|
||||
const auto item = static_cast<ItemType>(i);
|
||||
add(owner, item, inventory.item_count(item), inventory.stack_id(item));
|
||||
}
|
||||
};
|
||||
for (const auto& store : storages_) {
|
||||
auto previous = ItemType::None;
|
||||
for (const auto& stack : store.items) {
|
||||
require(stack.type > previous && stack.count && store.accepts(stack.type) && stack.reserved <= stack.count);
|
||||
previous = stack.type;
|
||||
add({OwnerKind::Storage, store.id}, stack.type, stack.count, stack.id);
|
||||
}
|
||||
require(store.total_items() + store.reserved_slots <= store.capacity);
|
||||
}
|
||||
for (const auto& actor : agents_) add_inventory(actor.inventory, {OwnerKind::Resident, actor.id});
|
||||
for (const auto& order : production_orders_) add_inventory(order.input, {OwnerKind::Production, order.id});
|
||||
std::unordered_map<AgentId, const Agent*> actors;
|
||||
std::unordered_map<ProductionId, const Agent*> producers;
|
||||
for (const auto& actor : agents_) {
|
||||
require(actors.emplace(actor.id, &actor).second);
|
||||
if (actor.production_id) require(producers.emplace(actor.production_id, &actor).second);
|
||||
}
|
||||
std::set<TransferId> transfers;
|
||||
std::set<ReservationId> reservation_ids;
|
||||
std::set<ProductionId> work_sites;
|
||||
std::set<ReservationId> backed;
|
||||
for (const auto& actor : agents_) {
|
||||
require(actor.next_admission != 0 && actor.route_cursor <= actor.route.size());
|
||||
if (!actor.execution) {
|
||||
require(actor.inventory.empty() && actor.production_id == 0 && actor.resource_node_id == 0 &&
|
||||
actor.state != AgentState::Returning && actor.state != AgentState::Hauling &&
|
||||
actor.state != AgentState::Eating && actor.state != AgentState::GoingToEat);
|
||||
continue;
|
||||
}
|
||||
const auto& execution = *actor.execution;
|
||||
require(execution.id && execution.id < next_transfer_id_ && transfers.insert(execution.id).second &&
|
||||
execution.kind <= TransferKind::Return && execution.next_operation && actor.next_admission >= 2 &&
|
||||
execution.reservation_count <= custody_reservation_limit && execution.blocked <= CustodyStatus::NoDestination);
|
||||
require(execution.production == actor.production_id && execution.source == actor.resource_node_id);
|
||||
if (execution.kind == TransferKind::Return) {
|
||||
require(!actor.inventory.empty() && !actor.production_id && !actor.resource_node_id && actor.work_remaining == 0);
|
||||
require((actor.state == AgentState::Returning && !actor.storage_id) ||
|
||||
(actor.state == AgentState::Hauling && actor.storage_id && find_storage(actor.storage_id) &&
|
||||
execution.destination == actor.storage_id));
|
||||
} else if (execution.kind == TransferKind::Meal) {
|
||||
require(actor.state == AgentState::GoingToEat || actor.state == AgentState::Eating);
|
||||
require(execution.production == 0 && execution.source == 0 && execution.destination == 0);
|
||||
} else if (execution.kind == TransferKind::Harvest || execution.kind == TransferKind::Tending) {
|
||||
require(actor.state == AgentState::Travelling || actor.state == AgentState::Gathering || actor.state == AgentState::Hauling);
|
||||
require(execution.production == 0 && (execution.kind != TransferKind::Tending || actor.storage_id == 0));
|
||||
} else {
|
||||
require(actor.state == AgentState::Travelling || actor.state == AgentState::Working ||
|
||||
actor.state == AgentState::FetchingMaterials || actor.state == AgentState::Hauling);
|
||||
if (execution.production) require(find_production(execution.production) != nullptr);
|
||||
else require(actor.state == AgentState::Hauling && is_carpentry_item(actor.inventory.first_item()));
|
||||
}
|
||||
for (std::size_t index = 0; index < custody_reservation_limit; ++index) {
|
||||
const auto& claim = execution.reservations[index];
|
||||
if (claim.id == 0) { require(claim == Reservation{}); continue; }
|
||||
require(index < execution.reservation_count && claim.id < next_reservation_id_ &&
|
||||
reservation_ids.insert(claim.id).second && claim.kind <= ReservationKind::Tending && claim.count);
|
||||
if (claim.kind == ReservationKind::Item) {
|
||||
auto entry = physical.find(claim.stack);
|
||||
require(entry != physical.end() && entry->second.owner == claim.owner && entry->second.item == claim.item);
|
||||
require(!claim.backing);
|
||||
if (claim.origin_storage) require(find_storage(claim.origin_storage) != nullptr);
|
||||
else require(claim.owner.kind == OwnerKind::Resident &&
|
||||
(execution.kind == TransferKind::Meal || (execution.kind == TransferKind::Return && actor.state == AgentState::Hauling)));
|
||||
require(claim.owner.kind != OwnerKind::Resident || claim.owner.id == actor.id);
|
||||
require(claim.owner.kind != OwnerKind::Production || claim.owner.id == actor.production_id);
|
||||
auto& amount = entry->second.claimed;
|
||||
require(claim.count <= entry->second.count - amount);
|
||||
amount += claim.count;
|
||||
if (claim.owner.kind == OwnerKind::Storage) {
|
||||
require(claim.owner.id == claim.origin_storage);
|
||||
reserved[claim.owner.id][static_cast<std::size_t>(claim.item) - 1] += claim.count;
|
||||
}
|
||||
} else if (claim.kind == ReservationKind::Capacity) {
|
||||
require(claim.owner.kind == OwnerKind::Storage && claim.owner.id == execution.destination &&
|
||||
find_storage(claim.owner.id) && !claim.stack && claim.item == ItemType::None && !claim.origin_storage && claim.count == 1);
|
||||
if (claim.backing) {
|
||||
const auto input = std::find_if(execution.reservations.begin(), execution.reservations.end(),
|
||||
[&](const auto& c) { return c.id == claim.backing; });
|
||||
require(input != execution.reservations.end() && input->kind == ReservationKind::Item &&
|
||||
input->owner == claim.owner && input->count >= claim.count && backed.insert(claim.backing).second);
|
||||
} else slots[claim.owner.id] += claim.count;
|
||||
} else if (claim.kind == ReservationKind::Worksite) {
|
||||
require(claim.owner == StockOwner{OwnerKind::Production, execution.production} && execution.production &&
|
||||
!claim.stack && claim.item == ItemType::None && claim.count == 1 && !claim.backing && !claim.origin_storage);
|
||||
require(work_sites.insert(execution.production).second);
|
||||
} else {
|
||||
require(claim.owner == StockOwner{} && !claim.stack && claim.item == ItemType::None &&
|
||||
claim.count == 1 && !claim.backing && !claim.origin_storage && execution.source && find_resource_node(execution.source));
|
||||
workers[execution.source] += claim.count;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto& store : storages_) {
|
||||
require(slots[store.id] == store.reserved_slots);
|
||||
for (const auto& stack : store.items) require(reserved[store.id][static_cast<std::size_t>(stack.type) - 1] == stack.reserved);
|
||||
}
|
||||
for (const auto& node : resource_nodes_) require(workers[node.id] == node.reserved_workers);
|
||||
for (const auto& order : production_orders_) {
|
||||
require(order.state <= JobState::Completed);
|
||||
if (order.state == JobState::InProgress) {
|
||||
require(!order.suspended && work_sites.contains(order.id) && !order.materials.empty());
|
||||
const auto found_actor = producers.find(order.id);
|
||||
require(found_actor != producers.end());
|
||||
const auto actor = found_actor->second;
|
||||
if (delivers_materials(order.recipe) && actor->state == AgentState::Working) {
|
||||
std::array<std::uint32_t, item_type_count> expected{};
|
||||
for (const auto& line : order.materials) {
|
||||
require(line.type > ItemType::None && line.type <= ItemType::Loom && line.count);
|
||||
const auto i = static_cast<std::size_t>(line.type) - 1;
|
||||
const auto amount = Void::Numeric::checked_add(expected[i], line.count);
|
||||
require(amount.has_value()); expected[i] = *amount;
|
||||
}
|
||||
for (std::size_t i = 0; i < item_type_count; ++i) require(order.input.item_count(static_cast<ItemType>(i + 1)) == expected[i]);
|
||||
require(actor->inventory.empty());
|
||||
} else require(order.input.empty());
|
||||
} else require(order.input.empty() && order.materials.empty() && order.delivery_stops.empty());
|
||||
if (order.suspended) require(order.state == JobState::Available &&
|
||||
(order.recipe == RecipeId::WeaveCloth || (order.recipe == RecipeId::BuildBed &&
|
||||
(order.bed_stage == BedStage::Bedding || order.bed_stage == BedStage::Pillow))));
|
||||
}
|
||||
for (std::size_t i = 0; i < item_type_count; ++i) {
|
||||
const auto& b = item_balances_[i];
|
||||
std::uint64_t in = 0, out = live[i];
|
||||
for (const auto value : {b.opening, b.grant, b.harvest, b.recipe_output}) {
|
||||
const auto amount = Void::Numeric::checked_add(in, value); require(amount.has_value()); in = *amount;
|
||||
}
|
||||
for (const auto value : {b.recipe_input, b.meal, b.installation, b.demolition}) {
|
||||
const auto amount = Void::Numeric::checked_add(out, value); require(amount.has_value()); out = *amount;
|
||||
}
|
||||
require(in == out);
|
||||
}
|
||||
require(custody_receipts_.size() <= custody_receipt_limit &&
|
||||
(custody_receipts_.size() == custody_receipt_limit ? custody_receipt_cursor_ < custody_receipt_limit : custody_receipt_cursor_ == 0));
|
||||
std::set<std::tuple<CustodyKeyKind, std::uint64_t, std::uint64_t>> keys;
|
||||
for (const auto& receipt : custody_receipts_) {
|
||||
require(static_cast<bool>(receipt));
|
||||
const auto& request = receipt->request;
|
||||
const auto& result = receipt->result;
|
||||
require(request.key.kind <= CustodyKeyKind::Operation && request.key.id && request.key.ordinal &&
|
||||
request.kind <= TransferKind::Return && request.operation <= CustodyOperation::Tend &&
|
||||
request.line_count <= custody_material_limit && request.production < next_production_id_ &&
|
||||
request.destination < next_storage_id_ && keys.emplace(request.key.kind, request.key.id, request.key.ordinal).second);
|
||||
if (request.key.kind == CustodyKeyKind::Admission) {
|
||||
require(request.key.id < next_agent_id_ && request.operation == CustodyOperation::Admit);
|
||||
const auto actor = actors.find(request.key.id);
|
||||
if (actor != actors.end()) require(request.key.ordinal < actor->second->next_admission);
|
||||
} else require(request.key.id < next_transfer_id_ && request.operation != CustodyOperation::Admit);
|
||||
for (std::size_t i = 0; i < custody_material_limit; ++i) {
|
||||
const auto& line = request.lines[i];
|
||||
if (i >= request.line_count) require(line == CustodyLine{});
|
||||
else require(line.storage && line.storage < next_storage_id_ && line.item > ItemType::None && line.item <= ItemType::Loom && line.count);
|
||||
}
|
||||
require(result.status <= CustodyStatus::NoDestination && result.transfer && result.transfer < next_transfer_id_ &&
|
||||
result.reservation_count <= custody_reservation_limit && result.effect_count <= item_type_count);
|
||||
for (std::size_t i = 0; i < custody_reservation_limit; ++i) require(i < result.reservation_count ?
|
||||
result.reservations[i] > 0 && result.reservations[i] < next_reservation_id_ : result.reservations[i] == 0);
|
||||
for (std::size_t i = 0; i < item_type_count; ++i) {
|
||||
const auto& effect = result.effects[i];
|
||||
if (i >= result.effect_count) { require(effect == CustodyEffect{}); continue; }
|
||||
require(effect.stack && effect.stack < next_stack_id_ && effect.item > ItemType::None && effect.item <= ItemType::Loom && effect.owner.id);
|
||||
if (effect.owner.kind == OwnerKind::Storage) require(effect.owner.id < next_storage_id_);
|
||||
else if (effect.owner.kind == OwnerKind::Resident) require(effect.owner.id < next_agent_id_);
|
||||
else { require(effect.owner.kind == OwnerKind::Production); require(effect.owner.id < next_production_id_); }
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace settlement
|
||||
Reference in new issue
Block a user