Integrate durable production orders and checkpoint schema 2 into M01

# Conflicts:
#	CMakeLists.txt
This commit is contained in:
faumaray committed 2026-12-02 22:30:29 +03:00
commit a8cd7bb415
16 files changed
+661 -88

No files matched your search

+67 -40
View File
@@ -322,6 +322,29 @@ const ResourceNode* World::find_resource_node(ResourceNodeId id) const noexcept
return found == resource_nodes_.end() || found->id != id ? nullptr : &*found;
}
const ProductionOrder* World::find_production(ProductionId id) const noexcept {
const auto found = record_position(production_orders_, id);
return found == production_orders_.end() || found->id != id ? nullptr : &*found;
}
ProductionOrder& World::production_by_id(ProductionId id) {
return require_record(production_orders_, id);
}
// Replace with a restorable checked sequence when Void/Library#7 is available:
// https://git.faumaray.com/Void/Library/issues/7
bool World::reserve_production(std::size_t count) {
if (!Void::Numeric::checked_add(next_production_id_, static_cast<std::uint64_t>(count))) return false;
production_orders_.reserve(production_orders_.size() + count);
return true;
}
void World::append_production(ProductionOrder order) {
order.id = next_production_id_;
production_orders_.push_back(std::move(order));
++next_production_id_;
}
Storage& World::storage_by_id(StorageId id) {
return require_record(storages_, id);
}
@@ -381,12 +404,12 @@ void World::set_blocked(GridPosition position, bool blocked) {
}
}
for (const Agent& agent : agents_) {
if (agent.production_index != no_entity_index &&
is_carpentry_recipe(production_orders_[agent.production_index].recipe) &&
if (agent.production_id != 0 &&
is_carpentry_recipe(production_by_id(agent.production_id).recipe) &&
std::find(agent.haul_route.begin(), agent.haul_route.end(), position) != agent.haul_route.end()) {
throw std::logic_error("cannot block the carpenter's delivery route");
}
if (agent.production_index == no_entity_index &&
if (agent.production_id == 0 &&
agent.state != AgentState::GoingToEat && agent.state != AgentState::GoingToSleep &&
!is_carpentry_item(agent.inventory.first_item())) continue;
const auto remaining = agent.route.begin() + static_cast<std::ptrdiff_t>(agent.route_cursor);
@@ -499,10 +522,11 @@ void World::queue_production(GridPosition position, RecipeId recipe) {
throw std::invalid_argument("a building occupies the production site");
}
}
if (!reserve_production(1)) throw ProductionCapacityError{};
ProductionOrder order{};
order.position = position;
order.recipe = recipe;
production_orders_.push_back(std::move(order));
append_production(std::move(order));
}
bool World::can_place_cabin(GridPosition position) const noexcept {
@@ -563,7 +587,8 @@ std::array<ItemStack, 2> World::carpentry_materials(RecipeId recipe) const {
return is_carpentry_recipe(recipe) ? recipe_for(recipe).ingredients : std::array<ItemStack, 2>{};
}
std::size_t World::missing_carpentry_orders(ItemType product, std::size_t new_installations) const {
std::size_t World::missing_carpentry_orders(ItemType product, std::size_t new_installations,
StorageId excluded_storage) const {
const auto craft = recipe_for_carpentry_item(product);
std::uint64_t waiting_installations = new_installations, supply = 0;
for (const auto& order : production_orders_) {
@@ -574,11 +599,12 @@ std::size_t World::missing_carpentry_orders(ItemType product, std::size_t new_in
}
}
for (const auto& agent : agents_) {
if (agent.state == AgentState::Hauling && agent.production_index == no_entity_index) {
if (agent.state == AgentState::Hauling && agent.production_id == 0) {
supply += agent.inventory.item_count(product);
}
}
for (const auto& storage : storages_) {
if (storage.id == excluded_storage) continue;
const auto available = storage.available_item_count(product);
if (available != 0 && find_path(scenario_sawmill_, storage.position).reachable) supply += available;
}
@@ -600,8 +626,9 @@ void World::queue_cabin(GridPosition position, GridPosition sawmill, GridPositio
throw std::invalid_argument("the cabin site is disconnected from the workshops");
}
const Recipe& cabin = recipe_for(RecipeId::BuildCabin);
production_orders_.reserve(production_orders_.size() + 1 +
cabin.ingredients[0].count + cabin.ingredients[1].count);
if (!reserve_production(1 + cabin.ingredients[0].count + cabin.ingredients[1].count)) {
throw ProductionCapacityError{};
}
for (std::uint32_t i = 0; i < cabin.ingredients[0].count; ++i) {
queue_production(sawmill, RecipeId::SawPlank);
}
@@ -691,18 +718,13 @@ CommandStatus World::edit_object(GridPosition position, const GridPosition* dest
return CommandStatus::InvalidTarget;
}
const auto at_position = [position](const auto& item) { return item.position == position; };
const auto shift_index = [](std::size_t& index, std::size_t removed) {
if (index != no_entity_index && index > removed) --index;
};
const auto order = std::find_if(production_orders_.begin(), production_orders_.end(), [&](const auto& entry) {
return is_production_object(entry.recipe) && !is_finished_floor(entry) && at_position(entry);
});
if (order != production_orders_.end()) {
if (destination) order->position = *destination;
else {
const auto removed = static_cast<std::size_t>(order - production_orders_.begin());
production_orders_.erase(order);
for (auto& agent : agents_) shift_index(agent.production_index, removed);
}
return CommandStatus::Accepted;
}
@@ -710,19 +732,23 @@ CommandStatus World::edit_object(GridPosition position, const GridPosition* dest
if (storage != storages_.end()) {
if (destination) storage->position = *destination;
else {
std::array<std::uint8_t, std::size(carpentry_products)> lost_products{};
for (std::size_t i = 0; i < std::size(carpentry_products); ++i) {
lost_products[i] = storage->item_count(carpentry_products[i]) != 0;
}
storages_.erase(storage);
std::array<std::size_t, std::size(carpentry_products)> replacements{};
std::size_t total = 0;
if (scenario_active_) {
for (std::size_t i = 0; i < std::size(carpentry_products); ++i) {
if (!lost_products[i]) continue;
for (std::size_t i = 0; i < replacements.size(); ++i) {
const auto product = carpentry_products[i];
const auto missing = missing_carpentry_orders(product);
for (std::size_t j = 0; j < missing; ++j) {
queue_production(scenario_sawmill_, recipe_for_carpentry_item(product));
}
if (storage->item_count(product) == 0) continue;
replacements[i] = missing_carpentry_orders(product, 0, storage->id);
total += replacements[i];
}
}
// Demand is evaluated against the post-removal inventory, without
// losing stock or issuing any order until the entire batch fits.
if (!reserve_production(total)) return CommandStatus::Busy;
storages_.erase(storage);
for (std::size_t i = 0; i < replacements.size(); ++i) {
for (std::size_t j = 0; j < replacements[i]; ++j) {
queue_production(scenario_sawmill_, recipe_for_carpentry_item(carpentry_products[i]));
}
}
}
@@ -733,9 +759,7 @@ CommandStatus World::edit_object(GridPosition position, const GridPosition* dest
const auto floor = std::find_if(production_orders_.begin(), production_orders_.end(),
[&](const auto& entry) { return is_finished_floor(entry) && at_position(entry); });
if (floor == production_orders_.end() || destination) return CommandStatus::InvalidTarget;
const auto removed = static_cast<std::size_t>(floor - production_orders_.begin());
production_orders_.erase(floor);
for (auto& agent : agents_) shift_index(agent.production_index, removed);
return CommandStatus::Accepted;
}
if (destination) source->position = *destination;
@@ -770,12 +794,12 @@ CommandReceipt World::apply_command(const PlayerCommand& command) {
!can_place_bed(command.cell) || !find_path(scenario_sawmill_, command.cell).reachable) {
return receipt(CommandStatus::InvalidTarget);
}
if (!reserve_production(staged_beds_ ? 1 : 2)) return receipt(CommandStatus::Busy);
if (staged_beds_) {
queue_production(command.cell, RecipeId::BuildBed);
production_orders_.back().bed_base = static_cast<BedBase>(command.value);
production_orders_.back().bed_stage = BedStage::Foundation;
} else {
production_orders_.reserve(production_orders_.size() + 2);
queue_production(scenario_sawmill_, RecipeId::SawPlank);
queue_production(command.cell, RecipeId::BuildBed);
}
@@ -842,19 +866,22 @@ CommandReceipt World::apply_command(const PlayerCommand& command) {
}
if (scenario_active_) {
const auto missing = missing_carpentry_orders(ItemType::Loom, 1);
production_orders_.reserve(production_orders_.size() + 1 + missing);
if (!reserve_production(1 + missing)) return receipt(CommandStatus::Busy);
queue_production(command.cell, RecipeId::InstallLoom);
for (std::size_t i = 0; i < missing; ++i) queue_production(scenario_sawmill_, RecipeId::CraftLoom);
} else {
if (!reserve_production(1)) return receipt(CommandStatus::Busy);
queue_production(command.cell, RecipeId::WeaveCloth);
}
break;
case CommandKind::CraftDoor:
if (!reserve_production(1)) return receipt(CommandStatus::Busy);
queue_production(scenario_sawmill_, RecipeId::CraftWoodenDoor);
break;
case CommandKind::CraftFurniture: {
const auto product = static_cast<ItemType>(command.value);
if (!is_carpentry_item(product) || product == ItemType::WoodenDoor) return receipt(CommandStatus::InvalidTarget);
if (!reserve_production(1)) return receipt(CommandStatus::Busy);
queue_production(scenario_sawmill_, recipe_for_carpentry_item(product));
break;
}
@@ -871,8 +898,8 @@ CommandReceipt World::apply_command(const PlayerCommand& command) {
const auto fixture = furnishings ? room_construction_recipe(furnishings->back().recipe) : RecipeId::BuildFloor;
const auto fixture_item = furnishings ? recipe_for(fixture).ingredients.front().type : ItemType::None;
const auto fixture_orders = furnishings ? missing_carpentry_orders(fixture_item, 1) : 0;
production_orders_.reserve(production_orders_.size() + width * height +
(furnishings ? furnishings->size() : 0) + (legacy_bed ? 1U : 0U) + door_orders + fixture_orders);
if (!reserve_production(width * height + (furnishings ? furnishings->size() : 0) +
(legacy_bed ? 1U : 0U) + door_orders + fixture_orders)) return receipt(CommandStatus::Busy);
if (legacy_bed) queue_production(scenario_sawmill_, RecipeId::SawPlank);
if (furnishings) {
for (const auto& part : *furnishings) {
@@ -881,7 +908,7 @@ CommandReceipt World::apply_command(const PlayerCommand& command) {
order.recipe = room_construction_recipe(part.recipe);
order.bed_base = part.bed_base;
if (order.recipe == RecipeId::BuildBed && staged_beds_) order.bed_stage = BedStage::Foundation;
production_orders_.push_back(std::move(order));
append_production(std::move(order));
}
}
for (int y = plan.first.y; y <= plan.last.y; ++y) {
@@ -890,7 +917,7 @@ CommandReceipt World::apply_command(const PlayerCommand& command) {
ProductionOrder order{};
order.position = cell;
order.recipe = room_construction_recipe(plan.recipe_at(cell));
production_orders_.push_back(std::move(order));
append_production(std::move(order));
}
}
for (std::size_t i = 0; i < door_orders; ++i) queue_production(scenario_sawmill_, RecipeId::CraftWoodenDoor);
@@ -1420,7 +1447,7 @@ void World::assign_production_tasks(bool weaving) {
order.materials = std::move(materials);
order.delivery_stops = std::move(stops);
order.state = JobState::InProgress;
agent.production_index = index;
agent.production_id = order.id;
agent.route = std::move(path.positions);
agent.route_cursor = 0;
agent.work_remaining = recipe.ticks;
@@ -1553,7 +1580,7 @@ void World::finish_rest_step(Agent& agent) {
}
void World::advance_construction_delivery(Agent& agent) {
ProductionOrder& order = production_orders_[agent.production_index];
ProductionOrder& order = production_by_id(agent.production_id);
if (order.collected_stops == order.delivery_stops.size()) {
for (const auto& material : order.materials) {
agent.inventory.remove(material.type, material.count);
@@ -1584,7 +1611,7 @@ void World::advance_construction_delivery(Agent& agent) {
}
void World::finish_production(Agent& agent) {
ProductionOrder& order = production_orders_[agent.production_index];
ProductionOrder& order = production_by_id(agent.production_id);
const Recipe recipe = recipe_for(order);
assert(!order.materials.empty());
if (is_carpentry_recipe(order.recipe)) {
@@ -1593,7 +1620,7 @@ void World::finish_production(Agent& agent) {
agent.route = std::move(agent.haul_route);
agent.haul_route.clear();
agent.route_cursor = 0;
agent.production_index = no_entity_index;
agent.production_id = 0;
agent.state = AgentState::Hauling;
order.materials.clear();
order.state = JobState::Completed;
@@ -1619,7 +1646,7 @@ void World::finish_production(Agent& agent) {
} else {
order.state = JobState::Completed;
}
agent.production_index = no_entity_index;
agent.production_id = 0;
agent.state = AgentState::Idle;
agent.route.clear();
agent.route_cursor = 0;
@@ -1695,7 +1722,7 @@ void World::step(TickProfile* profile) {
if (agent.state != AgentState::Working) continue;
if (agent.work_remaining > 0 && can_work(agent)) --agent.work_remaining;
if (agent.work_remaining != 0) continue;
if (agent.production_index != no_entity_index) continue;
if (agent.production_id != 0) continue;
const auto job = std::lower_bound(
jobs_.begin(), jobs_.end(), agent.current_job,
@@ -1742,12 +1769,12 @@ void World::step(TickProfile* profile) {
finish_rest_step(agent);
continue;
}
if (agent.production_index != no_entity_index && agent.route_cursor == agent.route.size() &&
if (agent.production_id != 0 && agent.route_cursor == agent.route.size() &&
(agent.state == AgentState::FetchingMaterials || agent.state == AgentState::Hauling)) {
advance_construction_delivery(agent);
continue;
}
if (agent.state == AgentState::Working && agent.production_index != no_entity_index &&
if (agent.state == AgentState::Working && agent.production_id != 0 &&
agent.work_remaining == 0) {
finish_production(agent);
}