Preserve production order identities and preflight whole placement batches
This commit is contained in:
1 parent
4ba64f7e62
commit
2a3ec924de
16 files changed
+661
-88
No files matched your search
+67
-40
@@ -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);
|
||||
}
|
||||
@@ -690,18 +717,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;
|
||||
}
|
||||
@@ -709,19 +731,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]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -732,9 +758,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;
|
||||
@@ -769,12 +793,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);
|
||||
}
|
||||
@@ -841,19 +865,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;
|
||||
}
|
||||
@@ -870,8 +897,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) {
|
||||
@@ -880,7 +907,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) {
|
||||
@@ -889,7 +916,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);
|
||||
@@ -1419,7 +1446,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;
|
||||
@@ -1552,7 +1579,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);
|
||||
@@ -1583,7 +1610,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)) {
|
||||
@@ -1592,7 +1619,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;
|
||||
@@ -1618,7 +1645,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;
|
||||
@@ -1694,7 +1721,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,
|
||||
@@ -1741,12 +1768,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);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user