Preserve production order identities and preflight whole placement batches

This commit is contained in:
faumaray committed 2026-12-02 22:30:07 +03:00
1 parent 4ba64f7e62
commit 2a3ec924de
16 files changed
+661 -88

No files matched your search

+42 -19
View File
@@ -18,7 +18,7 @@ namespace settlement {
namespace {
constexpr std::string_view signature = "SETTLERS";
constexpr std::uint32_t save_version = 13;
constexpr std::uint32_t save_version = 14;
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;
@@ -231,12 +231,13 @@ struct WorldSaveCodec {
archive.field(world.next_job_id_);
archive.field(world.next_resource_node_id_);
archive.field(world.next_storage_id_);
if (version >= 14) archive.field(world.next_production_id_);
const auto cells = world.blocked_.size();
for (auto& cell : world.blocked_) archive.field(cell);
// Agent links precede their pools in legacy saves. Raw positions stay
// loader-owned until the complete, ID-ordered pools are available.
std::vector<std::array<std::size_t, 2>> agent_links;
std::vector<std::array<std::size_t, 3>> agent_links;
sequence(archive, world.agents_, max_records, [&](auto& agent) {
archive.field(agent.id);
position(archive, agent.position);
@@ -245,8 +246,11 @@ struct WorldSaveCodec {
route(archive, agent.route, cells);
offset(archive, agent.route_cursor);
archive.field(agent.work_remaining);
if constexpr (Archive::reading) {
std::array<std::size_t, 2> links{no_entity_index, no_entity_index};
if (version >= 14) {
archive.field(agent.resource_node_id);
archive.field(agent.storage_id);
} else if constexpr (Archive::reading) {
std::array<std::size_t, 3> links{no_entity_index, no_entity_index, no_entity_index};
index(archive, links[0]);
index(archive, links[1]);
agent_links.push_back(links);
@@ -264,7 +268,9 @@ struct WorldSaveCodec {
}
}
route(archive, agent.haul_route, cells);
index(archive, agent.production_index);
if (version >= 14) archive.field(agent.production_id);
else if constexpr (Archive::reading) index(archive, agent_links.back()[2]);
else reference(archive, agent.production_id, world.production_orders_);
archive.field(agent.hunger);
if (version >= 4) archive.field(agent.fatigue);
if (version >= 5) flag(archive, agent.resting_on_ground);
@@ -315,6 +321,8 @@ struct WorldSaveCodec {
}
}
sequence(archive, world.production_orders_, max_records, [&](auto& order) {
if (version >= 14) archive.field(order.id);
else if constexpr (Archive::reading) order.id = world.production_orders_.size();
position(archive, order.position);
archive.field(order.recipe);
require_save(version >= 2 || order.recipe <= RecipeId::BuildCabin, "Unknown recipe for save version.");
@@ -325,13 +333,15 @@ struct WorldSaveCodec {
require_save(version >= 13 || order.recipe <= RecipeId::CraftWoodenDoor, "Unknown recipe for save version.");
archive.field(order.state);
sequence(archive, order.materials, item_types, [&](auto& material) {
reference(archive, material.storage_id, world.storages_);
if (version >= 14) archive.field(material.storage_id);
else reference(archive, material.storage_id, world.storages_);
archive.field(material.type);
require_save(material.type <= max_item, "Unknown material for save version.");
archive.field(material.count);
});
sequence(archive, order.delivery_stops, item_types, [&](auto& stop) {
reference(archive, stop.storage_id, world.storages_);
if (version >= 14) archive.field(stop.storage_id);
else reference(archive, stop.storage_id, world.storages_);
route(archive, stop.route_to_next, cells);
});
offset(archive, order.collected_stops);
@@ -341,6 +351,14 @@ struct WorldSaveCodec {
archive.field(order.bed_stage);
}
});
if constexpr (Archive::reading) {
if (version < 14) {
world.next_production_id_ = world.production_orders_.size() + 1;
for (std::size_t i = 0; i < agent_links.size(); ++i) {
world.agents_[i].production_id = reference_id(world.production_orders_, agent_links[i][2]);
}
}
}
if (version >= 2) {
flag(archive, world.scenario_active_);
flag(archive, world.paused_);
@@ -376,6 +394,7 @@ void WorldSaveCodec::validate(const World& world) {
check_ids(world.jobs_, world.next_job_id_);
check_ids(world.resource_nodes_, world.next_resource_node_id_);
check_ids(world.storages_, world.next_storage_id_);
check_ids(world.production_orders_, world.next_production_id_);
invalid(world.work_priority_ <= WorkPriority::BuildingFirst);
std::uint64_t revision = 0;
for (const auto sequence : world.player_sequences_) {
@@ -540,12 +559,13 @@ void WorldSaveCodec::validate(const World& world) {
valid_route(agent.route);
valid_route(agent.haul_route);
const bool has_job = agent.current_job != 0;
const bool has_order = agent.production_index != no_entity_index;
const bool has_order = agent.production_id != 0;
const auto* production = world.find_production(agent.production_id);
invalid(!has_order || production != nullptr);
const bool has_source = agent.resource_node_id != 0;
const bool has_storage = agent.storage_id != 0;
invalid(!has_storage || world.find_storage(agent.storage_id) != nullptr);
const bool carpentry = has_order && agent.production_index < world.production_orders_.size() &&
is_carpentry_recipe(world.production_orders_[agent.production_index].recipe);
const bool carpentry = has_order && is_carpentry_recipe(production->recipe);
invalid(agent.haul_route.empty() || has_source || carpentry);
if (has_job) {
invalid(!has_order && !has_storage && !has_source && agent.inventory.empty() &&
@@ -557,11 +577,11 @@ void WorldSaveCodec::validate(const World& world) {
invalid(claimed == 0);
claimed = 1;
} else if (has_order) {
invalid(!has_storage && !has_source && agent.production_index < world.production_orders_.size() &&
agent.work_remaining > 0);
const auto& order = world.production_orders_[agent.production_index];
invalid(order.state == JobState::InProgress && order_claims[agent.production_index] == 0);
order_claims[agent.production_index] = 1;
invalid(!has_storage && !has_source && agent.work_remaining > 0);
const auto order_index = static_cast<std::size_t>(production - world.production_orders_.data());
const auto& order = *production;
invalid(order.state == JobState::InProgress && order_claims[order_index] == 0);
order_claims[order_index] = 1;
if (!order.delivery_stops.empty()) {
invalid(agent.state == (order.collected_stops == 0 ? FetchingMaterials : Hauling));
} else {
@@ -579,7 +599,7 @@ void WorldSaveCodec::validate(const World& world) {
if (collected) ++reserved_slots[storage];
}
for (std::size_t i = 0; i < item_types; ++i) {
invalid(agent.inventory.item_count(static_cast<ItemType>(i + 1)) == expected_cargo[agent.production_index][i]);
invalid(agent.inventory.item_count(static_cast<ItemType>(i + 1)) == expected_cargo[order_index][i]);
}
} else if (has_source) {
invalid(world.find_resource_node(agent.resource_node_id) != nullptr &&
@@ -654,14 +674,17 @@ void WorldSaveCodec::validate(const World& world) {
}
}
void World::write_save(std::ostream& stream) const {
void World::write_save(std::ostream& stream, WorldSaveFormat format) const {
const auto version = static_cast<std::uint32_t>(format);
require_save(format == WorldSaveFormat::Legacy13 || format == WorldSaveFormat::Current14,
"Unsupported save output format.");
SaveWriter archive(stream);
for (char letter : signature) archive.field(letter);
archive.field(save_version);
archive.field(version);
archive.field(seed_);
archive.field(width_);
archive.field(height_);
WorldSaveCodec::body(archive, *this, save_version);
WorldSaveCodec::body(archive, *this, version);
}
std::unique_ptr<World> World::read_save(std::istream& stream, std::size_t workers, ExecutionMode mode,