5#if defined(__EMSCRIPTEN__)
12 "generation request JSON codec is unavailable on the Web build",
"codec",
13 {},
"pixelworld.generation-codec");
19 const PixelWorldGenerationRequest&) {
31#include <Poco/Dynamic/Var.h>
32#include <Poco/JSON/Array.h>
33#include <Poco/JSON/Object.h>
34#include <Poco/JSON/Parser.h>
35#include <Poco/JSON/Stringifier.h>
41#include <unordered_set>
46using Object = Poco::JSON::Object;
47using Array = Poco::JSON::Array;
50 std::string
path =
"document") {
53 "pixelworld.generation-codec"));
56bool exactFields(
const Object&
object, std::initializer_list<std::string_view> allowed) {
57 std::unordered_set<std::string> names;
58 for (
const auto name : allowed) names.emplace(
name);
59 if (
object.
size() != names.size())
return false;
61 if (!names.contains(
name)) return false;
62 for (
const auto&
name : names)
69 static_assert(std::is_integral_v<T> && !std::is_same_v<T, bool>);
70 if (!
object.
has(
key) ||
object.isNull(
key))
return false;
72 const auto wide =
object.get(
key).convert<std::int64_t>();
73 if constexpr (std::is_unsigned_v<T>) {
74 if (wide < 0 || std::uint64_t(wide) > std::numeric_limits<T>::max())
return false;
75 }
else if (wide < std::numeric_limits<T>::min() ||
76 wide > std::numeric_limits<T>::max()) {
79 result =
static_cast<T
>(wide);
86bool readSeed(
const Object&
object, std::uint64_t&
seed) {
87 if (!
object.
has(
"seed") ||
object.isNull(
"seed"))
return false;
89 const std::string
text =
object.getValue<std::string>(
"seed");
93 return parsed.ec == std::errc{} && parsed.ptr ==
end;
99Object::Ptr cellJson(
const PixelCell&
cell) {
101 out->set(
"material", std::uint16_t(
cell.material));
102 out->set(
"temperature",
cell.temperature);
103 out->set(
"lifetime",
cell.lifetime);
104 out->set(
"thermalRemainder",
cell.thermalRemainder);
113 root->set(
"schema",
"eve.pixelworld.generation-request");
123 root->set(
"terrainAmplitude",
request.terrainAmplitude);
126 Array::Ptr stamps(
new Array);
138 root->set(
"stamps", stamps);
139 std::ostringstream
text;
140 Poco::JSON::Stringifier::stringify(Poco::Dynamic::Var(
root),
text, 2, -1);
145 std::string_view json) {
148 root = Poco::JSON::Parser().parse(std::string(json)).extract<Object::Ptr>();
150 return malformed(
"generation request JSON is malformed");
152 if (!
root || !exactFields(*
root, {
"schema",
"version",
"seed",
"region",
"surfaceY",
153 "terrainAmplitude",
"waterLevel",
"caveThreshold",
"stamps"}))
154 return malformed(
"root contains missing or unknown fields");
155 if (
root->optValue<std::string>(
"schema",
"") !=
"eve.pixelworld.generation-request")
156 return malformed(
"unknown generation request schema",
"schema");
159 if (!readNumber(*
root,
"version",
request.schemaVersion) ||
161 return malformed(
"unknown generation request version",
"version");
162 if (!readSeed(*
root,
request.seed))
return malformed(
"seed is not a canonical uint64",
"seed");
167 stamps =
root->getArray(
"stamps");
169 return malformed(
"region or stamps has an invalid type");
171 if (!
region || !exactFields(*
region, {
"minX",
"minY",
"maxX",
"maxY"}) || !stamps ||
172 stamps->size() > 65'536U)
173 return malformed(
"region fields or stamp budget are invalid");
178 !readNumber(*
root,
"surfaceY",
request.surfaceY) ||
179 !readNumber(*
root,
"terrainAmplitude",
request.terrainAmplitude) ||
180 !readNumber(*
root,
"waterLevel",
request.waterLevel) ||
181 !readNumber(*
root,
"caveThreshold",
request.caveThreshold))
182 return malformed(
"generation request contains an invalid number");
184 std::uint64_t totalCells = 0;
185 request.stamps.reserve(stamps->size());
193 return malformed(
"stamp is not an object",
"stamps[" + std::to_string(
index) +
"]");
195 if (!
value || !exactFields(*
value, {
"originX",
"originY",
"width",
"height",
"cells"}) ||
197 return malformed(
"stamp contains missing or unknown fields",
198 "stamps[" + std::to_string(
index) +
"]");
200 if (!readNumber(*
value,
"originX",
stamp.originX) ||
201 !readNumber(*
value,
"originY",
stamp.originY) ||
204 std::uint64_t(
stamp.width) * std::uint64_t(
stamp.height) !=
cells->size())
205 return malformed(
"stamp dimensions are invalid",
"stamps[" + std::to_string(
index) +
"]");
206 totalCells +=
cells->size();
207 if (totalCells > 4'194'304ULL)
return malformed(
"stamp cell budget is exceeded",
"stamps");
209 for (std::size_t cellIndex = 0; cellIndex <
cells->size(); ++cellIndex) {
212 item =
cells->getObject(
unsigned(cellIndex));
214 return malformed(
"stamp cell is not an object",
"stamps.cells");
218 if (!item || !exactFields(*item, {
"material",
"temperature",
"lifetime",
"thermalRemainder"}) ||
219 !readNumber(*item,
"material",
material) ||
220 !readNumber(*item,
"temperature",
cell.temperature) ||
221 !readNumber(*item,
"lifetime",
cell.lifetime) ||
222 !readNumber(*item,
"thermalRemainder",
cell.thermalRemainder))
223 return malformed(
"stamp cell contains invalid fields",
"stamps.cells");
const GltfImportRequest & request
std::array< uint32_t, 16 > material
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
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.
bool has(const std::vector< std::reference_wrapper< const HouseComponent > > &values)
eve::Result< double > readNumber(const eve::Value::Object &object, std::string_view name)
Reads a required finite number.
eve::Result< std::string > encodePixelWorldGenerationRequestJson(const PixelWorldGenerationRequest &request)
Encode schema eve.pixelworld.generation-request version 1 canonically.
MaterialId
Compact stable material identifier stored in each authoritative cell.
eve::Result< PixelWorldGenerationRequest > decodePixelWorldGenerationRequestJson(std::string_view json)
Decode schema eve.pixelworld.generation-request version 1.
enum EVENGINE_API_FOUNDATION Array
OT_ARRAY (editable via Runtime array APIs / ReflectedPropertyModel).
Compact authoritative state for one simulated world pixel.
Owning row-major material stamp applied after terrain and cave generation.
Versioned bounded request for deterministic biome and cave generation.
static constexpr std::uint32_t kSchemaVersion