219 lines
11 KiB
C++
219 lines
11 KiB
C++
#pragma once
|
|
|
|
#include "settlement/core/world.hpp"
|
|
#include <algorithm>
|
|
#include <map>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <type_traits>
|
|
|
|
namespace replay_diagnostics {
|
|
using namespace settlement;
|
|
|
|
struct Context {
|
|
std::uint64_t attempt = 0;
|
|
std::size_t workers = 1;
|
|
std::string schedule = "uncontrolled";
|
|
std::string load = "uninterrupted";
|
|
std::size_t capacity = 4096;
|
|
};
|
|
|
|
inline std::string canonical(const World& world) {
|
|
std::ostringstream stream(std::ios::binary);
|
|
world.write_save(stream, WorldSaveFormat::Current15);
|
|
return stream.str();
|
|
}
|
|
|
|
template<class T> std::string value(T item) {
|
|
if constexpr (std::is_enum_v<T>) return std::to_string(static_cast<unsigned>(item));
|
|
else return std::to_string(item);
|
|
}
|
|
inline std::string value(GridPosition p) { return "(" + value(p.x) + "," + value(p.y) + ")"; }
|
|
inline std::string value(StockOwner owner) { return value(owner.kind) + ":" + value(owner.id); }
|
|
inline std::string value(const std::vector<GridPosition>& route) {
|
|
std::string text = "[";
|
|
for (auto p : route) text += value(p) + ",";
|
|
return text + "]";
|
|
}
|
|
|
|
// Diagnostic projection of public domain objects, never a save parser or an
|
|
// alternate equality oracle. Unknown/private fields retain the byte fallback.
|
|
// Prefer visible stock/job changes, then durable identities, retained results
|
|
// and accounting. A secondary balance change must not hide the changed stock.
|
|
using Facts = std::map<std::pair<unsigned, std::string>, std::string>;
|
|
inline Facts facts(const World& world) {
|
|
Facts out;
|
|
const auto put = [&](const std::string& owner, const char* field, const auto& item, unsigned priority = 0) {
|
|
out[{priority, owner + " field=" + field}] = value(item);
|
|
};
|
|
for (const auto& a : world.agents()) {
|
|
const auto key = "agent id=" + value(a.id);
|
|
#define AGENT_FIELD(field) put(key, #field, a.field)
|
|
AGENT_FIELD(position); AGENT_FIELD(state); AGENT_FIELD(current_job);
|
|
AGENT_FIELD(route); AGENT_FIELD(route_cursor); AGENT_FIELD(work_remaining);
|
|
AGENT_FIELD(resource_node_id); AGENT_FIELD(storage_id); AGENT_FIELD(haul_route);
|
|
AGENT_FIELD(production_id); AGENT_FIELD(hunger); AGENT_FIELD(fatigue);
|
|
AGENT_FIELD(resting_on_ground); AGENT_FIELD(profession);
|
|
#undef AGENT_FIELD
|
|
put(key, "next_admission", a.next_admission, 1);
|
|
put(key, "transfer_id", a.execution ? a.execution->id : 0, 1);
|
|
for (std::size_t item = 1; item <= item_type_count; ++item) {
|
|
const auto stock = "stock owner=agent:" + value(a.id) + " item=" + value(item);
|
|
put(stock, "count",
|
|
a.inventory.item_count(static_cast<ItemType>(item)));
|
|
put(stock, "stack_id", a.inventory.stack_id(static_cast<ItemType>(item)), 1);
|
|
}
|
|
if (a.execution) {
|
|
const auto& transfer = *a.execution;
|
|
const auto transfer_key = "transfer id=" + value(transfer.id) + " owner=agent:" + value(a.id);
|
|
put(transfer_key, "kind", transfer.kind, 1);
|
|
put(transfer_key, "production", transfer.production, 1);
|
|
put(transfer_key, "source", transfer.source, 1);
|
|
put(transfer_key, "destination", transfer.destination, 1);
|
|
put(transfer_key, "next_operation", transfer.next_operation, 1);
|
|
put(transfer_key, "blocked", transfer.blocked, 1);
|
|
put(transfer_key, "reservation_count", transfer.reservation_count, 1);
|
|
for (const auto& claim : transfer.reservations) {
|
|
if (!claim.id) continue;
|
|
const auto claim_key = "reservation id=" + value(claim.id) + " transfer=" + value(transfer.id);
|
|
put(claim_key, "kind", claim.kind, 1);
|
|
put(claim_key, "owner", claim.owner, 1);
|
|
put(claim_key, "stack_id", claim.stack, 1);
|
|
put(claim_key, "item", claim.item, 1);
|
|
put(claim_key, "count", claim.count, 1);
|
|
put(claim_key, "backing", claim.backing, 1);
|
|
put(claim_key, "origin_storage", claim.origin_storage, 1);
|
|
}
|
|
}
|
|
}
|
|
for (const auto& j : world.jobs()) {
|
|
const auto key = "job id=" + value(j.id);
|
|
put(key, "position", j.position); put(key, "state", j.state);
|
|
put(key, "work_required", j.work_required); put(key, "work_remaining", j.work_remaining);
|
|
}
|
|
for (const auto& p : world.production_orders()) {
|
|
const auto key = "production id=" + value(p.id);
|
|
put(key, "position", p.position); put(key, "recipe", p.recipe); put(key, "state", p.state);
|
|
put(key, "collected_stops", p.collected_stops); put(key, "bed_owner", p.bed_owner);
|
|
put(key, "bed_base", p.bed_base); put(key, "bed_stage", p.bed_stage);
|
|
put(key, "suspended", p.suspended, 1);
|
|
for (std::size_t item = 1; item <= item_type_count; ++item) {
|
|
const auto stock = "stock owner=production:" + value(p.id) + " item=" + value(item);
|
|
put(stock, "count", p.input.item_count(static_cast<ItemType>(item)));
|
|
put(stock, "stack_id", p.input.stack_id(static_cast<ItemType>(item)), 1);
|
|
}
|
|
for (const auto& m : p.materials) {
|
|
put(key + " material owner=storage:" + value(m.storage_id) + " item=" + value(m.type),
|
|
"count", m.count);
|
|
}
|
|
for (std::size_t i = 0; i < p.delivery_stops.size(); ++i) {
|
|
const auto stop = key + " stop=" + value(i);
|
|
put(stop, "storage_id", p.delivery_stops[i].storage_id);
|
|
put(stop, "route_to_next", p.delivery_stops[i].route_to_next);
|
|
}
|
|
}
|
|
for (const auto& s : world.storages()) {
|
|
const auto key = "storage id=" + value(s.id);
|
|
put(key, "position", s.position); put(key, "capacity", s.capacity);
|
|
put(key, "reserved_slots", s.reserved_slots); put(key, "kind", s.kind);
|
|
for (const auto& stack : s.items) {
|
|
const auto stock = "stock owner=storage:" + value(s.id) + " item=" + value(stack.type);
|
|
put(stock, "count", stack.count); put(stock, "reserved", stack.reserved);
|
|
put(stock, "stack_id", stack.id, 1);
|
|
}
|
|
}
|
|
// Keys identify logical results independently of the ring's backing slots.
|
|
for (const auto& receipt : world.custody_receipts()) {
|
|
const auto& request = receipt->request;
|
|
const auto& result = receipt->result;
|
|
const auto key = "custody receipt kind=" + value(request.key.kind) + " id=" + value(request.key.id) +
|
|
" ordinal=" + value(request.key.ordinal);
|
|
put(key, "operation", request.operation, 2);
|
|
put(key, "kind", request.kind, 2);
|
|
put(key, "production", request.production, 2);
|
|
put(key, "source", request.source, 2);
|
|
put(key, "destination", request.destination, 2);
|
|
put(key, "line_count", request.line_count, 2);
|
|
for (std::size_t i = 0; i < request.line_count; ++i) {
|
|
const auto line_key = key + " line=" + value(i);
|
|
put(line_key, "storage", request.lines[i].storage, 2);
|
|
put(line_key, "item", request.lines[i].item, 2);
|
|
put(line_key, "count", request.lines[i].count, 2);
|
|
}
|
|
put(key, "status", result.status, 2);
|
|
put(key, "transfer_id", result.transfer, 2);
|
|
put(key, "terminal", result.terminal, 2);
|
|
put(key, "reservation_count", result.reservation_count, 2);
|
|
put(key, "effect_count", result.effect_count, 2);
|
|
for (std::size_t i = 0; i < result.reservation_count; ++i) {
|
|
put(key + " reservation=" + value(i), "id", result.reservations[i], 2);
|
|
}
|
|
for (std::size_t i = 0; i < result.effect_count; ++i) {
|
|
const auto effect_key = key + " effect=" + value(i);
|
|
const auto& effect = result.effects[i];
|
|
put(effect_key, "owner", effect.owner, 2);
|
|
put(effect_key, "stack_id", effect.stack, 2);
|
|
put(effect_key, "item", effect.item, 2);
|
|
put(effect_key, "count", effect.count, 2);
|
|
}
|
|
}
|
|
for (std::size_t item = 1; item <= item_type_count; ++item) {
|
|
const auto key = "balance item=" + value(item);
|
|
const auto& balance = world.item_balances()[item - 1];
|
|
put(key, "opening", balance.opening, 3);
|
|
put(key, "grant", balance.grant, 3);
|
|
put(key, "harvest", balance.harvest, 3);
|
|
put(key, "recipe_output", balance.recipe_output, 3);
|
|
put(key, "recipe_input", balance.recipe_input, 3);
|
|
put(key, "meal", balance.meal, 3);
|
|
put(key, "installation", balance.installation, 3);
|
|
put(key, "demolition", balance.demolition, 3);
|
|
}
|
|
for (std::uint8_t player = 1; player <= colony_player_slots; ++player) {
|
|
put("command player=" + value(player), "next_sequence", world.next_command_sequence(player));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
inline std::string context(const World& world, const Context& c) {
|
|
return "replay divergence tick=" + value(world.tick()) + " attempt=" + value(c.attempt) +
|
|
" seed=" + value(world.seed()) + " workers=" + value(c.workers) + " schedule=" + c.schedule +
|
|
" load=" + c.load + " capacity=" + value(c.capacity) + " ";
|
|
}
|
|
|
|
inline std::string difference(const World& expected, const World& actual, const Context& c = {}) {
|
|
const auto left = canonical(expected), right = canonical(actual);
|
|
if (left == right) return {};
|
|
const auto a = facts(expected), b = facts(actual);
|
|
auto ai = a.begin(), bi = b.begin();
|
|
while (ai != a.end() || bi != b.end()) {
|
|
if (bi == b.end() || (ai != a.end() && ai->first < bi->first)) {
|
|
return context(expected, c) + ai->first.second + " expected=" + ai->second + " actual=<absent>";
|
|
}
|
|
if (ai == a.end() || bi->first < ai->first) {
|
|
return context(expected, c) + bi->first.second + " expected=<absent> actual=" + bi->second;
|
|
}
|
|
if (ai->second != bi->second) {
|
|
return context(expected, c) + ai->first.second + " expected=" + ai->second + " actual=" + bi->second;
|
|
}
|
|
++ai; ++bi;
|
|
}
|
|
const auto mismatch = std::mismatch(left.begin(), left.end(), right.begin(), right.end());
|
|
const auto offset = static_cast<std::size_t>(mismatch.first - left.begin());
|
|
const auto byte = [offset](const std::string& bytes) {
|
|
return offset < bytes.size() ? value(static_cast<unsigned char>(bytes[offset])) : "<end>";
|
|
};
|
|
return context(expected, c) + "byte=" + value(offset) + " expected=" + byte(left) + " actual=" + byte(right);
|
|
}
|
|
|
|
inline std::string receipt_difference(const World& world, const PlayerCommand& command,
|
|
CommandReceipt expected, CommandReceipt actual, const Context& c) {
|
|
if (expected.status == actual.status && expected.tick == actual.tick && expected.revision == actual.revision) return {};
|
|
const auto receipt = [](CommandReceipt r) {
|
|
return "{status=" + value(r.status) + ",tick=" + value(r.tick) + ",revision=" + value(r.revision) + "}";
|
|
};
|
|
return context(world, c) + "command player=" + value(command.player) + " sequence=" + value(command.sequence) +
|
|
" field=receipt expected=" + receipt(expected) + " actual=" + receipt(actual);
|
|
}
|
|
} // namespace replay_diagnostics
|