5#if defined(__EMSCRIPTEN__)
12 "material Catalog JSON codec is unavailable on the Web build",
"codec",
13 {},
"pixelworld.catalog-codec");
30#include <Poco/Dynamic/Var.h>
31#include <Poco/JSON/Array.h>
32#include <Poco/JSON/Object.h>
33#include <Poco/JSON/Parser.h>
34#include <Poco/JSON/Stringifier.h>
41#include <unordered_set>
46using Object = Poco::JSON::Object;
47using Array = Poco::JSON::Array;
54bool exactFields(
const Object&
object, std::initializer_list<std::string_view> allowed) {
55 std::unordered_set<std::string> names;
56 for (
const auto name : allowed) names.emplace(
name);
57 if (
object.
size() != names.size())
return false;
59 if (!names.contains(
name)) return false;
60 for (
const auto&
name : names)
90 static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>);
91 if (!
object.
has(
key) ||
object.isNull(
key))
return false;
93 const auto wide =
object.get(
key).convert<std::int64_t>();
94 if constexpr (std::is_unsigned_v<T>) {
95 if (wide < 0 || std::uint64_t(wide) > std::numeric_limits<T>::max())
return false;
96 }
else if (wide < std::numeric_limits<T>::min() ||
97 wide > std::numeric_limits<T>::max()) {
100 result =
static_cast<T
>(wide);
107Object::Ptr materialJson(
const MaterialDefinition&
material) {
114 out->set(
"thermalConductivity",
material.thermalConductivity);
116 out->set(
"ignitionTemperature",
material.ignitionTemperature);
117 out->set(
"meltingTemperature",
material.meltingTemperature);
118 out->set(
"boilingTemperature",
material.boilingTemperature);
119 out->set(
"defaultLifetime",
material.defaultLifetime);
121 out->set(
"defaultTemperature",
material.defaultTemperature);
122 out->set(
"blastResistance",
material.blastResistance);
126 out->set(
"tags", tags);
134 root->set(
"schema",
"eve.pixelworld.material-catalog");
135 root->set(
"version", 1);
136 Array::Ptr materials(
new Array);
138 root->set(
"materials", materials);
140 for (
const auto& reaction : catalog.
reactions()) {
142 value->set(
"id", reaction.id);
143 value->set(
"first", std::uint16_t(reaction.first));
144 value->set(
"second", std::uint16_t(reaction.second));
145 value->set(
"firstResult", std::uint16_t(reaction.firstResult));
146 value->set(
"secondResult", std::uint16_t(reaction.secondResult));
147 value->set(
"minimumTemperature", reaction.minimumTemperature);
148 value->set(
"heatDelta", reaction.heatDelta);
149 value->set(
"priority", reaction.priority);
153 Array::Ptr phases(
new Array);
157 value->set(
"source", std::uint16_t(
phase.source));
158 value->set(
"result", std::uint16_t(
phase.result));
163 value->set(
"temperatureDelta",
phase.temperatureDelta);
167 root->set(
"phaseRules", phases);
168 std::ostringstream
text;
169 Poco::JSON::Stringifier::stringify(Poco::Dynamic::Var(
root),
text, 2, -1);
176 root = Poco::JSON::Parser().parse(std::string(json)).extract<Object::Ptr>();
178 return malformed(
"material Catalog JSON is malformed");
180 if (!
root || !exactFields(*
root, {
"schema",
"version",
"materials",
"reactions",
"phaseRules"}))
181 return malformed(
"root contains missing or unknown fields");
182 if (
root->optValue<std::string>(
"schema",
"") !=
"eve.pixelworld.material-catalog")
183 return malformed(
"unknown material Catalog schema",
"schema");
186 return malformed(
"unknown material Catalog version",
"version");
187 Array::Ptr materials;
191 materials =
root->getArray(
"materials");
193 phases =
root->getArray(
"phaseRules");
195 return malformed(
"material Catalog arrays are invalid");
197 if (!materials || !
reactions || !phases || materials->size() > 65535U ||
198 reactions->size() > 1'000'000U || phases->size() > 1'000'000U)
199 return malformed(
"material Catalog arrays are missing or exceed their budgets");
206 value = materials->getObject(
unsigned(
index));
208 return malformed(
"material entry is not an object",
209 "materials[" + std::to_string(
index) +
"]");
211 if (!
value || !exactFields(*
value, {
"id",
"name",
"state",
"density",
"viscosity",
212 "thermalConductivity",
"heatCapacity",
"ignitionTemperature",
213 "meltingTemperature",
"boilingTemperature",
"defaultLifetime",
214 "flammable",
"defaultTemperature",
"blastResistance",
215 "displayRgba",
"tags"}))
216 return malformed(
"material contains missing or unknown fields",
217 "materials[" + std::to_string(
index) +
"]");
219 std::uint16_t
id = 0;
220 if (!readNumber(*
value,
"id",
id) || !readNumber(*
value,
"density",
material.density) ||
222 !readNumber(*
value,
"thermalConductivity",
material.thermalConductivity) ||
223 !readNumber(*
value,
"heatCapacity",
material.heatCapacity) ||
224 !readNumber(*
value,
"ignitionTemperature",
material.ignitionTemperature) ||
225 !readNumber(*
value,
"meltingTemperature",
material.meltingTemperature) ||
226 !readNumber(*
value,
"boilingTemperature",
material.boilingTemperature) ||
227 !readNumber(*
value,
"defaultLifetime",
material.defaultLifetime) ||
228 !readNumber(*
value,
"defaultTemperature",
material.defaultTemperature) ||
229 !readNumber(*
value,
"blastResistance",
material.blastResistance) ||
231 return malformed(
"material contains an invalid number",
232 "materials[" + std::to_string(
index) +
"]");
235 if (!parseState(
value->optValue<std::string>(
"state",
""),
material.state))
236 return malformed(
"material state is unknown",
237 "materials[" + std::to_string(
index) +
"].state");
238 if (!
value->has(
"flammable") || !
value->has(
"tags"))
239 return malformed(
"material fields are missing",
240 "materials[" + std::to_string(
index) +
"]");
243 Array::Ptr
tags =
value->getArray(
"tags");
244 if (!
tags ||
tags->size() > 1024U)
throw std::runtime_error(
"tags");
248 return malformed(
"material tags or flammable value are invalid",
249 "materials[" + std::to_string(
index) +
"]");
254 std::vector<MaterialReactionRule> reactionRules;
255 reactionRules.reserve(
reactions->size());
261 return malformed(
"reaction entry is not an object",
262 "reactions[" + std::to_string(
index) +
"]");
264 if (!
value || !exactFields(*
value, {
"id",
"first",
"second",
"firstResult",
"secondResult",
265 "minimumTemperature",
"heatDelta",
"priority"}))
266 return malformed(
"reaction contains missing or unknown fields",
267 "reactions[" + std::to_string(
index) +
"]");
269 std::uint16_t
first = 0,
second = 0, firstResult = 0, secondResult = 0;
270 rule.
id =
value->optValue<std::string>(
"id",
"");
272 !readNumber(*
value,
"firstResult", firstResult) ||
273 !readNumber(*
value,
"secondResult", secondResult) ||
277 return malformed(
"reaction contains an invalid number",
278 "reactions[" + std::to_string(
index) +
"]");
283 reactionRules.push_back(std::move(rule));
286 std::vector<MaterialPhaseRule> phaseRules;
287 phaseRules.reserve(phases->size());
293 return malformed(
"phase rule entry is not an object",
294 "phaseRules[" + std::to_string(
index) +
"]");
296 if (!
value || !exactFields(*
value, {
"id",
"source",
"result",
"direction",
"threshold",
297 "temperatureDelta",
"priority"}))
298 return malformed(
"phase rule contains missing or unknown fields",
299 "phaseRules[" + std::to_string(
index) +
"]");
302 rule.
id =
value->optValue<std::string>(
"id",
"");
303 const std::string
direction =
value->optValue<std::string>(
"direction",
"");
306 else return malformed(
"phase direction is unknown",
307 "phaseRules[" + std::to_string(
index) +
"].direction");
312 return malformed(
"phase rule contains an invalid number",
313 "phaseRules[" + std::to_string(
index) +
"]");
316 phaseRules.push_back(std::move(rule));
319 std::move(phaseRules));
std::map< unsigned, Colour > definitions
std::array< uint32_t, 16 > material
RoadLaneDirection direction
Battle::Reactions reactions
A structured explanation of a failed, degraded, or noteworthy result.
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
Owning immutable material and reaction table used by one PixelWorld.
std::span< const MaterialReactionRule > reactions() const noexcept
Borrow immutable canonical-priority reaction rules.
std::span< const MaterialDefinition > definitions() const noexcept
Borrow immutable definitions until this catalog is destroyed.
std::span< const MaterialPhaseRule > phaseRules() const noexcept
Borrow immutable canonical-priority phase rules.
static eve::Result< MaterialCatalog > create(std::vector< MaterialDefinition > definitions, std::vector< MaterialReactionRule > reactions={}, std::vector< MaterialPhaseRule > phaseRules={})
Validate and own a complete catalog candidate transactionally.
bool has(const std::vector< std::reference_wrapper< const HouseComponent > > &values)
std::string stateName(OrderState state)
Returns the stable lowercase name of a state.
ForceVector add(ForceVector a, ForceVector b)
eve::Result< double > readNumber(const eve::Value::Object &object, std::string_view name)
Reads a required finite number.
eve::Result< MaterialCatalog > decodeMaterialCatalogJson(std::string_view json)
Decode and validate schema eve.pixelworld.material-catalog version 1.
MaterialId
Compact stable material identifier stored in each authoritative cell.
MaterialState
Broad movement class for one pixel material.
eve::Result< std::string > encodeMaterialCatalogJson(const MaterialCatalog &catalog)
Encode schema eve.pixelworld.material-catalog version 1 canonically.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Immutable validated properties for one catalog material.
Data-driven phase transition evaluated after heat diffusion.
std::int16_t temperatureDelta
TemperatureDirection direction
Deterministically ordered binary material reaction.
std::int16_t minimumTemperature