837 lines
52 KiB
C++
837 lines
52 KiB
C++
#include "settlement/core/world.hpp"
|
|
#include "settlement/core/inventory_transaction.hpp"
|
|
#include <Void/Math/Numeric.hpp>
|
|
#include <algorithm>
|
|
#include <limits>
|
|
#include <set>
|
|
#include <map>
|
|
#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;
|
|
const auto fail = [&](CustodyStatus status) {
|
|
receipt->result.status = status;
|
|
commit_custody(agent, receipt);
|
|
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 fail(CustodyStatus::Invalid);
|
|
slots -= claim.count;
|
|
claim = {};
|
|
}
|
|
}
|
|
if (planned.total_items() + slots + count > planned.capacity) return fail(CustodyStatus::Capacity);
|
|
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 fail(CustodyStatus::Overflow);
|
|
target->count = *amount;
|
|
id = target->id;
|
|
} else {
|
|
id = inventory.stack_id(type);
|
|
if (count < inventory.item_count(type)) {
|
|
if (!room(next, 1)) return fail(CustodyStatus::Exhausted);
|
|
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<JobId, const WorkJob*> work_jobs;
|
|
std::set<JobId> assigned_jobs;
|
|
for (const auto& job : jobs_) require(work_jobs.emplace(job.id, &job).second);
|
|
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::unordered_map<TransferId, std::uint64_t> active_ordinals;
|
|
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 &&
|
|
actor.state != AgentState::Gathering && actor.state != AgentState::FetchingMaterials &&
|
|
(actor.state != AgentState::Working || actor.current_job != 0));
|
|
require(actor.storage_id == 0 && actor.haul_route.empty());
|
|
if (actor.state == AgentState::Working || actor.state == AgentState::Travelling) {
|
|
const auto job = work_jobs.find(actor.current_job);
|
|
require(job != work_jobs.end() && job->second->state == JobState::InProgress &&
|
|
assigned_jobs.insert(actor.current_job).second && actor.work_remaining > 0 && actor.work_remaining <= job->second->work_required);
|
|
if (actor.state == AgentState::Working) require(actor.position == job->second->position && actor.route_cursor == actor.route.size());
|
|
else require(actor.route_cursor < actor.route.size() && actor.route.back() == job->second->position);
|
|
} else require(actor.current_job == 0 && actor.work_remaining == 0 &&
|
|
(actor.state == AgentState::Idle || actor.state == AgentState::GoingToSleep || actor.state == AgentState::Sleeping));
|
|
continue;
|
|
}
|
|
const auto& execution = *actor.execution;
|
|
require(actor.current_job == 0 && !actor.resting_on_ground);
|
|
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);
|
|
active_ordinals.emplace(execution.id, execution.next_operation);
|
|
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.haul_route.empty());
|
|
require((actor.state == AgentState::Returning && !actor.storage_id && !execution.destination && !execution.reservation_count) ||
|
|
(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 && actor.haul_route.empty());
|
|
if (actor.state == AgentState::GoingToEat) {
|
|
const auto* pantry = find_storage(actor.storage_id);
|
|
require(pantry && pantry->kind == StorageKind::Pantry && actor.inventory.empty() && actor.work_remaining == 0);
|
|
require(actor.route.empty() ? actor.position == pantry->position : actor.route.back() == pantry->position);
|
|
} else require(!actor.storage_id && actor.route.empty() && actor.route_cursor == 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.source != 0) == (actor.state != AgentState::Hauling));
|
|
if (execution.kind == TransferKind::Tending) require(actor.state != AgentState::Hauling && !actor.storage_id && !execution.destination && actor.haul_route.empty());
|
|
else require(execution.destination != 0 && actor.storage_id == execution.destination);
|
|
} else {
|
|
require(actor.state == AgentState::Travelling || actor.state == AgentState::Working ||
|
|
actor.state == AgentState::FetchingMaterials || actor.state == AgentState::Hauling);
|
|
if (execution.production) {
|
|
const auto* order = find_production(execution.production);
|
|
require(order && !actor.storage_id);
|
|
require(is_carpentry_recipe(order->recipe) || actor.haul_route.empty());
|
|
}
|
|
else require(actor.state == AgentState::Hauling && is_carpentry_item(actor.inventory.first_item()));
|
|
}
|
|
std::array<unsigned, 5> claim_kinds{};
|
|
std::array<std::uint64_t, item_type_count> resident_claims{};
|
|
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);
|
|
++claim_kinds[static_cast<std::size_t>(claim.kind)];
|
|
if (claim.kind == ReservationKind::Item) {
|
|
if (claim.owner.kind == OwnerKind::Resident && claim.item > ItemType::None && claim.item <= ItemType::Loom)
|
|
resident_claims[static_cast<std::size_t>(claim.item) - 1] += claim.count;
|
|
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;
|
|
require((claim.kind == ReservationKind::Tending) == (execution.kind == TransferKind::Tending));
|
|
}
|
|
}
|
|
require(claim_kinds[static_cast<std::size_t>(ReservationKind::Worksite)] == (execution.production ? 1U : 0U));
|
|
require(claim_kinds[static_cast<std::size_t>(ReservationKind::Capacity)] == (execution.destination ? 1U : 0U));
|
|
require(claim_kinds[static_cast<std::size_t>(ReservationKind::Harvest)] ==
|
|
(execution.kind == TransferKind::Harvest && execution.source ? 1U : 0U));
|
|
require(claim_kinds[static_cast<std::size_t>(ReservationKind::Tending)] == (execution.kind == TransferKind::Tending ? 1U : 0U));
|
|
if (execution.kind == TransferKind::Harvest || execution.kind == TransferKind::Tending ||
|
|
(execution.kind == TransferKind::Production && !execution.production)) require(claim_kinds[0] == 0);
|
|
if (execution.kind == TransferKind::Meal) {
|
|
require(claim_kinds[0] == 1);
|
|
for (const auto& claim : execution.reservations) if (claim.id && claim.kind == ReservationKind::Item) {
|
|
require(claim.item == ItemType::Berries && claim.count == 1 && claim.owner ==
|
|
(actor.state == AgentState::Eating ? StockOwner{OwnerKind::Resident, actor.id} : StockOwner{OwnerKind::Storage, actor.storage_id}));
|
|
}
|
|
}
|
|
if (actor.state == AgentState::Eating || (execution.kind == TransferKind::Return && actor.state == AgentState::Hauling)) {
|
|
for (std::size_t i = 0; i < item_type_count; ++i) require(resident_claims[i] == actor.inventory.item_count(static_cast<ItemType>(i + 1)));
|
|
}
|
|
if (execution.source) {
|
|
const auto* node = find_resource_node(execution.source);
|
|
require(node && actor.inventory.empty() && (actor.state == AgentState::Gathering || actor.state == AgentState::Travelling));
|
|
require(execution.kind != TransferKind::Tending || node->needs_tending());
|
|
require(execution.kind != TransferKind::Harvest || (node->quantity && execution.destination && actor.storage_id == execution.destination));
|
|
if (actor.state == AgentState::Travelling) require(actor.work_remaining > 0 && actor.route_cursor < actor.route.size() && actor.route.back() == node->position);
|
|
else require(actor.position == node->position && actor.route_cursor == actor.route.size());
|
|
if (execution.kind == TransferKind::Harvest) {
|
|
const auto* destination = find_storage(execution.destination);
|
|
require(destination && destination->accepts(node->type));
|
|
require(actor.haul_route.empty() ? node->position == destination->position : actor.haul_route.back() == destination->position);
|
|
}
|
|
}
|
|
if (actor.state == AgentState::Hauling && !execution.production) {
|
|
require(actor.storage_id == execution.destination && execution.destination && actor.work_remaining == 0);
|
|
std::uint64_t carried = 0;
|
|
for (std::size_t i = 1; i <= item_type_count; ++i) carried += actor.inventory.item_count(static_cast<ItemType>(i));
|
|
require(carried == 1 && actor.haul_route.empty());
|
|
const auto cargo = actor.inventory.first_item();
|
|
const bool resource = cargo == ItemType::Wood || cargo == ItemType::Stone || cargo == ItemType::Berries || cargo == ItemType::Flax;
|
|
require(resource || is_carpentry_item(cargo));
|
|
if (execution.kind == TransferKind::Harvest) require(resource);
|
|
const auto* destination = find_storage(execution.destination);
|
|
require(destination && destination->accepts(cargo));
|
|
require(actor.route.empty() ? actor.position == destination->position : actor.route.back() == destination->position);
|
|
}
|
|
}
|
|
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& job : jobs_) require((job.state == JobState::InProgress) == assigned_jobs.contains(job.id));
|
|
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;
|
|
require(order.materials.size() <= custody_material_limit && order.collected_stops <= order.delivery_stops.size());
|
|
const bool delivery = delivers_materials(order.recipe);
|
|
const auto destination = is_construction(order.recipe) ? 0 : order.materials.front().storage_id;
|
|
require(actor->execution->destination == destination);
|
|
if (destination) {
|
|
ReservationId backing = 0;
|
|
for (const auto& claim : actor->execution->reservations) if (claim.id && claim.kind == ReservationKind::Item &&
|
|
claim.owner == StockOwner{OwnerKind::Storage, destination}) { backing = claim.id; break; }
|
|
for (const auto& claim : actor->execution->reservations) if (claim.id && claim.kind == ReservationKind::Capacity) require(claim.backing == backing);
|
|
}
|
|
require(delivery || order.delivery_stops.empty());
|
|
// Delivered recipes never use the generic Travelling transition:
|
|
// that transition enters Working without performing any pickup.
|
|
if (delivery) {
|
|
require(actor->state == AgentState::FetchingMaterials || actor->state == AgentState::Hauling || actor->state == AgentState::Working);
|
|
if (actor->state == AgentState::Working) require(order.delivery_stops.empty() && order.collected_stops == 0 && actor->position == order.position);
|
|
else {
|
|
require(!order.delivery_stops.empty() && actor->work_remaining > 0);
|
|
require(actor->state == AgentState::FetchingMaterials ? order.collected_stops == 0 : order.collected_stops > 0);
|
|
auto target = order.position;
|
|
if (order.collected_stops < order.delivery_stops.size()) {
|
|
const auto* store = find_storage(order.delivery_stops[order.collected_stops].storage_id);
|
|
require(store != nullptr); target = store->position;
|
|
}
|
|
require(actor->route.empty() ? actor->position == target : actor->route.back() == target);
|
|
}
|
|
} else {
|
|
require(order.collected_stops == 0 && (actor->state == AgentState::Travelling || actor->state == AgentState::Working));
|
|
if (actor->state == AgentState::Travelling) require(actor->work_remaining > 0 && actor->route_cursor < actor->route.size() && actor->route.back() == order.position);
|
|
else require(actor->position == order.position);
|
|
}
|
|
std::array<std::uint64_t, item_type_count> required{}, planned{}, claimed{}, cargo{};
|
|
const auto need = [&](ItemType item, std::uint32_t count) { if (count) required[static_cast<std::size_t>(item) - 1] += count; };
|
|
if (order.recipe == RecipeId::SawPlank) need(ItemType::Wood, 1);
|
|
else if (order.recipe == RecipeId::CutStoneBlock) need(ItemType::Stone, 1);
|
|
else if (order.recipe == RecipeId::WeaveCloth) need(ItemType::Flax, 1);
|
|
else if (order.recipe == RecipeId::BuildBed) need(order.bed_material(), 1);
|
|
else for (const auto& item : is_carpentry_recipe(order.recipe) ? carpentry_materials(order.recipe) :
|
|
construction_materials(order.recipe, order.bed_base)) need(item.type, item.count);
|
|
StorageId previous_stop = 0;
|
|
for (const auto& stop : order.delivery_stops) {
|
|
require(stop.storage_id > previous_stop && find_storage(stop.storage_id)); previous_stop = stop.storage_id;
|
|
require(std::any_of(order.materials.begin(), order.materials.end(), [&](const auto& m) { return m.storage_id == stop.storage_id; }));
|
|
}
|
|
for (const auto& line : order.materials) {
|
|
require(line.type > ItemType::None && line.type <= ItemType::Loom && line.count && find_storage(line.storage_id));
|
|
planned[static_cast<std::size_t>(line.type) - 1] += line.count;
|
|
StockOwner expected{OwnerKind::Storage, line.storage_id};
|
|
if (delivery) {
|
|
if (actor->state == AgentState::Working) expected = {OwnerKind::Production, order.id};
|
|
else {
|
|
const auto stop = std::find_if(order.delivery_stops.begin(), order.delivery_stops.end(), [&](const auto& e) { return e.storage_id == line.storage_id; });
|
|
require(stop != order.delivery_stops.end());
|
|
if (static_cast<std::size_t>(stop - order.delivery_stops.begin()) < order.collected_stops) expected = {OwnerKind::Resident, actor->id};
|
|
}
|
|
}
|
|
std::uint64_t line_claims = 0;
|
|
for (const auto& claim : actor->execution->reservations) if (claim.id && claim.kind == ReservationKind::Item &&
|
|
claim.item == line.type && claim.origin_storage == line.storage_id) { require(claim.owner == expected); line_claims += claim.count; }
|
|
require(line_claims == line.count);
|
|
if (expected.kind == OwnerKind::Resident) cargo[static_cast<std::size_t>(line.type) - 1] += line.count;
|
|
}
|
|
for (const auto& claim : actor->execution->reservations) if (claim.id && claim.kind == ReservationKind::Item)
|
|
claimed[static_cast<std::size_t>(claim.item) - 1] += claim.count;
|
|
require(planned == required && claimed == required);
|
|
for (std::size_t i = 0; i < item_type_count; ++i) require(cargo[i] == actor->inventory.item_count(static_cast<ItemType>(i + 1)));
|
|
if (actor->state == AgentState::Working) require(actor->route_cursor == actor->route.size() && order.delivery_stops.empty());
|
|
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;
|
|
std::map<std::pair<CustodyKeyKind, std::uint64_t>, std::uint64_t> last_ordinals;
|
|
TransferId last_admission = 0;
|
|
ReservationId last_issued_reservation = 0;
|
|
std::set<TransferId> terminal_transfers;
|
|
for (std::size_t receipt_index = 0; receipt_index < custody_receipts_.size(); ++receipt_index) {
|
|
const auto& receipt = custody_receipts_[(receipt_index + custody_receipt_cursor_) % custody_receipts_.size()];
|
|
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);
|
|
auto& last_ordinal = last_ordinals[{request.key.kind, request.key.id}];
|
|
require(last_ordinal == 0 || (last_ordinal != maximum && request.key.ordinal == last_ordinal + 1));
|
|
last_ordinal = request.key.ordinal;
|
|
require(!terminal_transfers.contains(result.transfer));
|
|
if (result.terminal) {
|
|
require(result.status == CustodyStatus::Accepted && !active_ordinals.contains(result.transfer));
|
|
require(request.operation == CustodyOperation::Complete || request.operation == CustodyOperation::Eat ||
|
|
request.operation == CustodyOperation::Tend || request.operation == CustodyOperation::Cancel ||
|
|
request.operation == CustodyOperation::Return ||
|
|
(request.operation == CustodyOperation::Deliver && request.destination != 0));
|
|
terminal_transfers.insert(result.transfer);
|
|
}
|
|
require(request.source < (((request.operation == CustodyOperation::Admit &&
|
|
(request.kind == TransferKind::Harvest || request.kind == TransferKind::Tending)) ||
|
|
request.operation == CustodyOperation::Harvest || request.operation == CustodyOperation::Tend) ? next_resource_node_id_ : next_storage_id_));
|
|
if (request.key.kind == CustodyKeyKind::Admission) {
|
|
require(result.transfer > last_admission && !result.terminal);
|
|
last_admission = result.transfer;
|
|
require(result.status == CustodyStatus::Accepted && 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 && result.transfer == request.key.id);
|
|
const auto active = active_ordinals.find(request.key.id);
|
|
if (active != active_ordinals.end()) require(request.key.ordinal < active->second);
|
|
}
|
|
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);
|
|
}
|
|
if (request.operation == CustodyOperation::Admit) {
|
|
require(result.effect_count == 0);
|
|
require(result.reservation_count == request.line_count + static_cast<unsigned>(request.production != 0) +
|
|
static_cast<unsigned>(request.source != 0) + static_cast<unsigned>(request.destination != 0));
|
|
switch (request.kind) {
|
|
case TransferKind::Production: require(request.production && !request.source && request.line_count); break;
|
|
case TransferKind::Harvest: require(!request.production && request.source && request.destination && !request.line_count); break;
|
|
case TransferKind::Tending: require(!request.production && request.source && !request.destination && !request.line_count); break;
|
|
case TransferKind::Meal:
|
|
require(!request.production && !request.source && !request.destination && request.line_count == 1 &&
|
|
request.lines[0].item == ItemType::Berries && request.lines[0].count == 1); break;
|
|
case TransferKind::Return: require(false); break;
|
|
}
|
|
} else {
|
|
switch (request.operation) {
|
|
case CustodyOperation::Pickup:
|
|
require((request.kind == TransferKind::Production || request.kind == TransferKind::Meal) &&
|
|
request.source && !request.destination && !request.line_count &&
|
|
(request.production != 0) == (request.kind == TransferKind::Production)); break;
|
|
case CustodyOperation::Deliver:
|
|
require(!request.source && (request.kind == TransferKind::Production || request.kind == TransferKind::Harvest));
|
|
if (request.destination) require(!request.production && request.line_count == 1 && request.lines[0].storage == request.destination);
|
|
else require(request.kind == TransferKind::Production && request.production && !request.line_count);
|
|
break;
|
|
case CustodyOperation::Complete:
|
|
case CustodyOperation::Cancel:
|
|
require(request.kind == TransferKind::Production && request.production && !request.source && !request.destination && !request.line_count); break;
|
|
case CustodyOperation::Return:
|
|
require(request.kind == TransferKind::Return && !request.production && !request.source && request.destination &&
|
|
request.line_count == 1 && request.lines[0].storage == request.destination); break;
|
|
case CustodyOperation::Harvest:
|
|
case CustodyOperation::Tend:
|
|
require(request.kind == (request.operation == CustodyOperation::Harvest ? TransferKind::Harvest : TransferKind::Tending) &&
|
|
!request.production && request.source && !request.destination && !request.line_count); break;
|
|
case CustodyOperation::Eat:
|
|
require(request.kind == TransferKind::Meal && !request.production && !request.source && !request.destination && !request.line_count); break;
|
|
case CustodyOperation::Admit: require(false); break;
|
|
}
|
|
}
|
|
require(result.status <= CustodyStatus::NoDestination && result.transfer && result.transfer < next_transfer_id_ &&
|
|
result.reservation_count <= custody_reservation_limit && result.effect_count <= item_type_count);
|
|
// Only admissions issue claims, in allocator order. Check their retained
|
|
// history without resolving already released claims to live objects.
|
|
require(request.operation == CustodyOperation::Admit || result.reservation_count == 0);
|
|
for (std::size_t i = 0; i < custody_reservation_limit; ++i) {
|
|
if (i >= result.reservation_count) { require(result.reservations[i] == 0); continue; }
|
|
require(result.reservations[i] > last_issued_reservation && result.reservations[i] < next_reservation_id_);
|
|
last_issued_reservation = result.reservations[i];
|
|
}
|
|
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);
|
|
// Effects are final outcomes, not per-material deltas. Distinct
|
|
// owners/stack IDs may legitimately share an item type. Zero counts
|
|
// describe consumption of a historical stack and remain valid.
|
|
for (std::size_t prior = 0; prior < i; ++prior) {
|
|
const auto& other = result.effects[prior];
|
|
require(effect.owner != other.owner || effect.stack != other.stack || effect.item != other.item);
|
|
}
|
|
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
|