Connect legacy admission to the validated custody state
This commit is contained in:
commit
c183de1952
8 files changed
+445
-25
No files matched your search
+122
-8
@@ -4,6 +4,7 @@
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <tuple>
|
||||
|
||||
namespace settlement {
|
||||
@@ -257,26 +258,31 @@ bool World::deposit_custody(Agent& agent, Storage& destination, ItemType type, s
|
||||
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 false;
|
||||
if (slots < claim.count) return fail(CustodyStatus::Invalid);
|
||||
slots -= claim.count;
|
||||
claim = {};
|
||||
}
|
||||
}
|
||||
if (planned.total_items() + slots + count > planned.capacity) return false;
|
||||
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 false;
|
||||
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 false;
|
||||
if (!room(next, 1)) return fail(CustodyStatus::Exhausted);
|
||||
id = next++;
|
||||
}
|
||||
planned.items.push_back({type, count, 0, id});
|
||||
@@ -448,6 +454,9 @@ void World::validate_custody() const {
|
||||
}
|
||||
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_) {
|
||||
@@ -455,6 +464,7 @@ void World::validate_custody() const {
|
||||
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;
|
||||
@@ -463,13 +473,26 @@ void World::validate_custody() const {
|
||||
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::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);
|
||||
@@ -488,12 +511,17 @@ void World::validate_custody() const {
|
||||
if (execution.production) require(find_production(execution.production) != nullptr);
|
||||
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);
|
||||
@@ -526,14 +554,43 @@ void World::validate_custody() const {
|
||||
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::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::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);
|
||||
}
|
||||
}
|
||||
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) {
|
||||
@@ -541,6 +598,45 @@ void World::validate_custody() const {
|
||||
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);
|
||||
require(delivery || order.delivery_stops.empty());
|
||||
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) {
|
||||
@@ -571,7 +667,11 @@ void World::validate_custody() const {
|
||||
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_) {
|
||||
std::map<std::pair<CustodyKeyKind, std::uint64_t>, std::uint64_t> last_ordinals;
|
||||
TransferId last_admission = 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;
|
||||
@@ -579,11 +679,25 @@ void World::validate_custody() const {
|
||||
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); 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(request.key.id < next_agent_id_ && request.operation == CustodyOperation::Admit);
|
||||
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);
|
||||
} 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{});
|
||||
|
||||
+31
-8
@@ -429,12 +429,15 @@ AgentId World::spawn_agent(GridPosition position) {
|
||||
if (!is_walkable(position)) {
|
||||
throw std::invalid_argument("agent spawn cell is not walkable");
|
||||
}
|
||||
const AgentId id = next_agent_id_++;
|
||||
const AgentId id = next_agent_id_;
|
||||
const auto successor = Void::Numeric::checked_add(id, AgentId{1});
|
||||
if (id == 0 || !successor) throw std::overflow_error("resident identities exhausted");
|
||||
Agent agent;
|
||||
agent.id = id;
|
||||
agent.position = position;
|
||||
if (hunger_enabled_) agent.hunger = initial_hunger(id);
|
||||
agents_.push_back(std::move(agent));
|
||||
next_agent_id_ = *successor;
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -445,8 +448,11 @@ JobId World::add_job(GridPosition position, std::uint32_t work_required) {
|
||||
if (work_required == 0) {
|
||||
throw std::invalid_argument("work duration must be greater than zero");
|
||||
}
|
||||
const JobId id = next_job_id_++;
|
||||
const JobId id = next_job_id_;
|
||||
const auto successor = Void::Numeric::checked_add(id, JobId{1});
|
||||
if (id == 0 || !successor) throw std::overflow_error("job identities exhausted");
|
||||
jobs_.push_back(WorkJob{id, position, work_required, work_required, JobState::Available});
|
||||
next_job_id_ = *successor;
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -1544,9 +1550,13 @@ void World::finish_meal_step(Agent& agent) {
|
||||
auto& sink = item_balances_[static_cast<std::size_t>(ItemType::Berries) - 1].meal;
|
||||
const auto amount = Void::Numeric::checked_add(sink, std::uint64_t{1});
|
||||
const auto meals = Void::Numeric::checked_add(meals_eaten_, std::uint64_t{1});
|
||||
auto receipt = prepare_custody(agent, CustodyOperation::Eat,
|
||||
amount && meals ? CustodyStatus::Accepted : CustodyStatus::Overflow);
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(agent, CustodyOperation::Eat,
|
||||
amount && meals ? CustodyStatus::Accepted : CustodyStatus::Overflow));
|
||||
if (!receipt) return;
|
||||
receipt->result.terminal = amount && meals;
|
||||
receipt->result.effects[0] = {{OwnerKind::Resident, agent.id}, agent.inventory.stack_id(ItemType::Berries), ItemType::Berries,
|
||||
amount && meals ? 0U : 1U};
|
||||
receipt->result.effect_count = 1;
|
||||
commit_custody(agent, receipt);
|
||||
if (!amount || !meals) return;
|
||||
sink = *amount;
|
||||
@@ -1690,11 +1700,23 @@ void World::finish_production(Agent& agent) {
|
||||
if (!receipt) return;
|
||||
if (status == CustodyStatus::Accepted) {
|
||||
receipt->result.terminal = !carpentry;
|
||||
for (const auto& material : order.materials) {
|
||||
const auto& original_store = storage_by_id(material.storage_id);
|
||||
StackId consumed_id = 0;
|
||||
std::uint32_t remaining = 0;
|
||||
StockOwner owner{OwnerKind::Production, order.id};
|
||||
if (delivered) { consumed_id = order.input.stack_id(material.type); remaining = input.item_count(material.type); }
|
||||
else {
|
||||
owner = {OwnerKind::Storage, material.storage_id};
|
||||
for (const auto& stack : original_store.items) if (stack.type == material.type) consumed_id = stack.id;
|
||||
for (const auto& store : stores) if (store.id == material.storage_id) remaining = store.item_count(material.type);
|
||||
}
|
||||
receipt->result.effects[receipt->result.effect_count++] = {owner, consumed_id, material.type, remaining};
|
||||
}
|
||||
if (output_id) {
|
||||
receipt->result.effects[0] = {carpentry ? StockOwner{OwnerKind::Resident, agent.id} :
|
||||
receipt->result.effects[receipt->result.effect_count++] = {carpentry ? StockOwner{OwnerKind::Resident, agent.id} :
|
||||
StockOwner{OwnerKind::Storage, stores.front().id}, output_id, recipe.output,
|
||||
carpentry ? inventory.item_count(recipe.output) : stores.front().item_count(recipe.output)};
|
||||
receipt->result.effect_count = 1;
|
||||
}
|
||||
}
|
||||
commit_custody(agent, receipt);
|
||||
@@ -1867,8 +1889,9 @@ void World::step(TickProfile* profile) {
|
||||
ResourceNode& node = resource_node_by_id(agent.resource_node_id);
|
||||
if (agent.storage_id == 0) {
|
||||
const auto growth = Void::Numeric::checked_add(tick_, growing_time(node.site));
|
||||
auto receipt = prepare_custody(agent, CustodyOperation::Tend, growth ? CustodyStatus::Accepted : CustodyStatus::Overflow);
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(agent, CustodyOperation::Tend, growth ? CustodyStatus::Accepted : CustodyStatus::Overflow, node.id));
|
||||
if (!receipt) continue;
|
||||
receipt->result.terminal = growth.has_value();
|
||||
commit_custody(agent, receipt);
|
||||
if (!growth) continue;
|
||||
node.growth_at = *growth;
|
||||
@@ -1884,7 +1907,7 @@ void World::step(TickProfile* profile) {
|
||||
const auto amount = Void::Numeric::checked_add(balance, std::uint64_t{1});
|
||||
const auto next = Void::Numeric::checked_add(next_stack_id_, std::uint64_t{1});
|
||||
auto receipt = std::const_pointer_cast<CustodyReceipt>(prepare_custody(agent, CustodyOperation::Harvest,
|
||||
!next ? CustodyStatus::Exhausted : !amount ? CustodyStatus::Overflow : CustodyStatus::Accepted));
|
||||
!next ? CustodyStatus::Exhausted : !amount ? CustodyStatus::Overflow : CustodyStatus::Accepted, node.id));
|
||||
if (!receipt) continue;
|
||||
if (next && amount) {
|
||||
receipt->result.effects[0] = {{OwnerKind::Resident, agent.id}, next_stack_id_, node.type, 1};
|
||||
|
||||
@@ -22,6 +22,16 @@ constexpr std::uint32_t save_version = 15;
|
||||
constexpr std::uint32_t last_legacy_footer_version = 8;
|
||||
constexpr std::size_t max_records = 1'000'000;
|
||||
constexpr std::size_t max_save_bytes = 128 * 1024 * 1024;
|
||||
// The wire limit is independent of decoded C++ layout: an old byte-light agent
|
||||
// now includes an execution/claim array. Cover every bounded top-level pool,
|
||||
// nested vectors (maximum element expansion <4x wire), and the receipt ring.
|
||||
// This is an accounting ceiling, never a reserve() request from untrusted input.
|
||||
constexpr std::size_t record_pool_bytes = sizeof(Agent) + sizeof(WorkJob) +
|
||||
sizeof(ResourceNode) + sizeof(Storage) + sizeof(ProductionOrder);
|
||||
static_assert(record_pool_bytes <= (std::numeric_limits<std::size_t>::max() -
|
||||
4 * max_save_bytes - custody_receipt_limit * (sizeof(CustodyReceipt) + 32)) / max_records);
|
||||
constexpr std::size_t max_decode_allocation_bytes = max_records * record_pool_bytes +
|
||||
4 * max_save_bytes + custody_receipt_limit * (sizeof(CustodyReceipt) + 32);
|
||||
|
||||
void require_save(bool condition, const char* error) {
|
||||
if (!condition) throw std::runtime_error(error);
|
||||
@@ -87,7 +97,7 @@ public:
|
||||
}
|
||||
|
||||
void allocate(std::size_t count, std::size_t size) {
|
||||
require_save(count <= max_save_bytes / size &&
|
||||
require_save(count <= max_decode_allocation_bytes / size &&
|
||||
allocations_.consume_field(count * size).has_value(), "Save allocation budget exceeded.");
|
||||
}
|
||||
|
||||
@@ -109,7 +119,7 @@ private:
|
||||
std::istream& stream_;
|
||||
std::size_t bytes_ = 0;
|
||||
// Legacy footers cover the signature and version, which are read first.
|
||||
Void::Binary::DecodeBudget allocations_{{max_save_bytes, max_save_bytes, 64}};
|
||||
Void::Binary::DecodeBudget allocations_{{max_decode_allocation_bytes, max_decode_allocation_bytes, 64}};
|
||||
bool legacy_footer_ = true;
|
||||
std::uint64_t legacy_digest_ = Void::Hash::fnv1a64_offset_basis;
|
||||
};
|
||||
@@ -467,7 +477,7 @@ void result_fields(Archive& archive, Result& result) {
|
||||
archive.field(effect.item);
|
||||
archive.field(effect.count);
|
||||
require_save(effect.owner.kind != OwnerKind::None && effect.stack != 0 &&
|
||||
effect.item > ItemType::None && effect.item <= ItemType::Loom && effect.count != 0,
|
||||
effect.item > ItemType::None && effect.item <= ItemType::Loom,
|
||||
"Invalid custody result effect.");
|
||||
}
|
||||
for (std::size_t i = result.effect_count; i < result.effects.size(); ++i) {
|
||||
@@ -634,8 +644,8 @@ void WorldSaveCodec::validate(const World& world, bool modern) {
|
||||
|
||||
const auto storage_index = [&](StorageId id) { return record_index(world.storages_, id); };
|
||||
const auto node_index = [&](ResourceNodeId id) { return record_index(world.resource_nodes_, id); };
|
||||
std::vector<ItemCounts> reserved_items(world.storages_.size());
|
||||
std::vector<ItemCounts> expected_cargo(world.production_orders_.size());
|
||||
std::vector<ItemCounts> reserved_items(modern ? 0 : world.storages_.size());
|
||||
std::vector<ItemCounts> expected_cargo(modern ? 0 : world.production_orders_.size());
|
||||
std::vector<AgentId> bed_owners;
|
||||
for (std::size_t i = 0; i < world.production_orders_.size(); ++i) {
|
||||
const auto& order = world.production_orders_[i];
|
||||
|
||||
Reference in new issue
Block a user