Files
stoneheart_at_home/engine/src/colony_profile.cpp
T

135 lines
6.4 KiB
C++

#include "settlement/core/colony_profile.hpp"
#include <Void/Data/StrictJson.hpp>
#include <algorithm>
#include <array>
#include <exception>
#include <utility>
namespace settlement {
namespace {
RulesDiagnostic unsupported(std::string package, std::string message) {
return {std::move(package), "builtin/colony-profile", "profile", std::move(message), 0};
}
std::expected<RulesManifest, RulesDiagnostic> resolve_profile(
ColonyProfile profile, std::span<const RulePackage> catalog) {
if (profile != ColonyProfile::Default && profile != ColonyProfile::PlankBeds) {
return std::unexpected{unsupported({}, "Unknown colony profile.")};
}
const std::array selected{std::string{profile == ColonyProfile::Default ?
"settlement.colony" : "settlement.plankbeds"}};
return resolve_rules(catalog, selected);
}
std::expected<BedConstructionMode, RulesDiagnostic> plank_bed_mode(const RulePackage& package) {
namespace json = Void::Json::Strict;
const auto definitions = package.definitions();
if (definitions.size() != 1 || definitions.front().id != "settlement.plankbeds:bed" ||
definitions.front().kind != "recipe") {
return std::unexpected{unsupported(package.identity().id, "Plank-bed recipe is missing.")};
}
const auto parsed = json::parse(definitions.front().canonical_data,
{.maximumDepth = 4, .allowDecimals = false});
const auto* fields = parsed ? std::get_if<json::Value::Object>(&parsed->value) : nullptr;
if (!fields || fields->size() != 5) {
return std::unexpected{unsupported(package.identity().id, "Invalid plank-bed recipe data.")};
}
const auto matches = [&]<class T>(std::string_view key, const T& expected) {
const auto entry = fields->find(key);
if (entry == fields->end()) return false;
const auto* value = std::get_if<T>(&entry->second.value);
return value && *value == expected;
};
// This finite profile binds typed content to the already implemented recipe.
// General recipe-data execution is scheduled separately in S19-W02.
if (!matches("assembly", std::string{"plank"}) ||
!matches("input", std::string{"settlement.colony:board"}) ||
!matches("input_count", std::int64_t{1}) ||
!matches("saw_work_ticks", std::int64_t{12}) ||
!matches("work_ticks", std::int64_t{14})) {
return std::unexpected{unsupported(package.identity().id, "Plank-bed recipe is not implemented.")};
}
return BedConstructionMode::Plank;
}
CheckpointDiagnostic checkpoint_error(const RulesDiagnostic& error) {
return {CheckpointError::RulesMismatch, error.field, error.package, error.message};
}
} // namespace
ColonyRules::ColonyRules(ColonyProfile profile, BedConstructionMode beds, RulesManifest manifest)
: profile_(profile), bed_mode_(beds), manifest_(std::move(manifest)) {}
std::expected<ColonyRules, RulesDiagnostic> compile_colony_rules(const RulesManifest& manifest) {
const auto catalog = builtin_rule_packages();
if (!catalog) return std::unexpected{catalog.error()};
for (const auto& identity : manifest.packages()) {
const auto installed = std::find_if(catalog->begin(), catalog->end(),
[&](const auto& package) { return package.identity().id == identity.id; });
if (installed == catalog->end() || installed->identity() != identity) {
return std::unexpected{unsupported(identity.id, "Package has no matching compiled colony implementation.")};
}
}
const auto default_manifest = resolve_profile(ColonyProfile::Default, *catalog);
if (!default_manifest) return std::unexpected{default_manifest.error()};
if (manifest == *default_manifest) {
return ColonyRules{ColonyProfile::Default, BedConstructionMode::Staged, manifest};
}
const auto plank_manifest = resolve_profile(ColonyProfile::PlankBeds, *catalog);
if (!plank_manifest) return std::unexpected{plank_manifest.error()};
if (manifest != *plank_manifest) {
return std::unexpected{unsupported({}, "Selected package closure is not an implemented colony profile.")};
}
const auto package = std::find_if(catalog->begin(), catalog->end(),
[](const auto& entry) { return entry.identity().id == "settlement.plankbeds"; });
const auto mode = plank_bed_mode(*package);
if (!mode) return std::unexpected{mode.error()};
return ColonyRules{ColonyProfile::PlankBeds, *mode, manifest};
}
std::expected<ColonyRules, RulesDiagnostic> colony_rules(ColonyProfile profile) {
const auto catalog = builtin_rule_packages();
if (!catalog) return std::unexpected{catalog.error()};
const auto manifest = resolve_profile(profile, *catalog);
if (!manifest) return std::unexpected{manifest.error()};
return compile_colony_rules(*manifest);
}
std::expected<WorldCheckpoint, CheckpointDiagnostic> make_colony_checkpoint(
ColonyProfile profile, std::size_t workers, ExecutionMode mode) {
const auto rules = colony_rules(profile);
if (!rules) return std::unexpected{checkpoint_error(rules.error())};
const auto world_id = Void::Uuid::try_v4();
if (!world_id) {
return std::unexpected{CheckpointDiagnostic{
CheckpointError::ResourceFailure, "world_id", {}, "Could not create a world identity."}};
}
try {
return WorldCheckpoint{make_first_colony(workers, rules->bed_mode(), mode),
*world_id, 1, rules->manifest()};
} catch (const std::exception& error) {
return std::unexpected{CheckpointDiagnostic{
CheckpointError::ResourceFailure, "world", {}, error.what()}};
}
}
std::expected<void, CheckpointDiagnostic> validate_colony_checkpoint(const WorldCheckpoint& candidate) {
if (!candidate.world || candidate.world_id.is_nil() || candidate.city == 0) {
return std::unexpected{CheckpointDiagnostic{
CheckpointError::InvalidIdentity, "world", {}, "A colony needs an owned world and nonzero identities."}};
}
const auto rules = compile_colony_rules(candidate.rules);
if (!rules) return std::unexpected{checkpoint_error(rules.error())};
if (rules->profile() == ColonyProfile::PlankBeds && candidate.world->staged_beds()) {
return std::unexpected{CheckpointDiagnostic{CheckpointError::RulesMismatch, "world.staged_beds",
"settlement.plankbeds", "Saved bed assembly does not match the selected plank-bed package."}};
}
return {};
}
} // namespace settlement