20 const auto it =
object.find(std::string(
name));
21 return it ==
object.end() ? nullptr : &it->second;
24Result<std::string> optionalString(
const Value::Object&
object, std::string_view
name, std::string fallback) {
29Result<float> optionalFloat(
const Value::Object&
object, std::string_view
name,
float fallback) {
32 if (!parsed)
return fail<float>(parsed.status());
36Result<int> optionalInt(
const Value::Object&
object, std::string_view
name,
int fallback) {
39 if (!parsed)
return fail<int>(parsed.status());
40 const double number = parsed.value();
41 if (!std::isfinite(
number) ||
number <
static_cast<double>(std::numeric_limits<int>::min()) ||
42 number >
static_cast<double>(std::numeric_limits<int>::max())) {
49Result<std::vector<std::string>> optionalStringArray(
const Value::Object&
object, std::string_view
name) {
50 std::vector<std::string>
out;
52 if (!
value)
return Result<std::vector<std::string>>::success(std::move(
out));
55 return Result<std::vector<std::string>>::failure(
58 for (
const Value& entry : *array) {
59 const auto*
text = entry.getIf<std::string>();
61 return Result<std::vector<std::string>>::failure(
66 return Result<std::vector<std::string>>::success(std::move(
out));
69Result<CompartmentFunctionDef> parseFunction(
const Value::Object&
object) {
70 CompartmentFunctionDef def;
72 if (!
id)
return fail<CompartmentFunctionDef>(
id.
status());
73 if (
id.
value().empty()) {
77 def.id = std::move(
id).takeValue();
79 auto display = optionalString(
object,
"name", def.id);
80 if (!display)
return fail<CompartmentFunctionDef>(display.status());
81 def.displayName = std::move(display).takeValue();
83 auto powerDraw = optionalFloat(
object,
"powerDraw", 0.f);
84 if (!powerDraw)
return fail<CompartmentFunctionDef>(powerDraw.status());
85 def.powerDraw = powerDraw.value();
87 auto powerProvide = optionalFloat(
object,
"powerProvide", 0.f);
88 if (!powerProvide)
return fail<CompartmentFunctionDef>(powerProvide.status());
89 def.powerProvide = powerProvide.value();
91 auto thrustBonus = optionalFloat(
object,
"thrustBonus", 0.f);
92 if (!thrustBonus)
return fail<CompartmentFunctionDef>(thrustBonus.status());
93 def.thrustBonus = thrustBonus.value();
95 auto hangar = optionalInt(
object,
"hangarCapacity", 0);
96 if (!hangar)
return fail<CompartmentFunctionDef>(hangar.status());
97 def.hangarCapacity = hangar.value();
99 auto weapons = optionalInt(
object,
"weaponBayCount", 0);
100 if (!weapons)
return fail<CompartmentFunctionDef>(weapons.status());
101 def.weaponBayCount = weapons.value();
103 auto crew = optionalInt(
object,
"crewCapacity", 0);
104 if (!crew)
return fail<CompartmentFunctionDef>(crew.status());
105 def.crewCapacity = crew.value();
107 auto sail = optionalFloat(
object,
"sailPower", 0.f);
108 if (!sail)
return fail<CompartmentFunctionDef>(sail.status());
109 def.sailPower = sail.value();
111 auto tags = optionalStringArray(
object,
"tags");
112 if (!tags)
return fail<CompartmentFunctionDef>(
tags.status());
113 def.tags = std::move(tags).takeValue();
119 auto deck = optionalInt(
object,
"deck", 0);
120 if (!deck)
return fail<LayoutSeed>(deck.status());
121 seed.deck = deck.value();
122 auto row = optionalInt(
object,
"row", 0);
123 if (!row)
return fail<LayoutSeed>(
row.status());
125 auto col = optionalInt(
object,
"col", 0);
126 if (!col)
return fail<LayoutSeed>(col.status());
127 seed.col = col.value();
132 if (!functionId)
return fail<LayoutSeed>(functionId.status());
133 seed.functionId = std::move(functionId).takeValue();
134 if (
seed.functionId.empty()) {
136 "layout function id must be non-empty",
"warship.layout"));
141Result<WarshipDefinition> parseHull(
const Value::Object&
object) {
142 WarshipDefinition def;
144 if (!
id)
return fail<WarshipDefinition>(
id.
status());
145 if (
id.
value().empty()) {
149 def.id = std::move(
id).takeValue();
153 def.category = std::move(
category).takeValue();
156 if (!mobility)
return fail<WarshipDefinition>(mobility.status());
157 def.mobility = std::move(mobility).takeValue();
159 auto length = optionalFloat(
object,
"length", def.length);
160 if (!
length)
return fail<WarshipDefinition>(
length.status());
161 def.length =
length.value();
162 auto beam = optionalFloat(
object,
"beam", def.beam);
163 if (!beam)
return fail<WarshipDefinition>(beam.status());
164 def.beam = beam.value();
165 auto maxSpeed = optionalFloat(
object,
"maxSpeed", def.maxSpeed);
166 if (!maxSpeed)
return fail<WarshipDefinition>(maxSpeed.status());
167 def.maxSpeed = maxSpeed.value();
168 auto accel = optionalFloat(
object,
"accel", def.accel);
169 if (!accel)
return fail<WarshipDefinition>(accel.status());
170 def.accel = accel.value();
171 auto turnRate = optionalFloat(
object,
"turnRate", def.turnRate);
172 if (!turnRate)
return fail<WarshipDefinition>(turnRate.status());
173 def.turnRate = turnRate.value();
174 auto maxHealth = optionalFloat(
object,
"maxHealth", def.maxHealth);
175 if (!maxHealth)
return fail<WarshipDefinition>(maxHealth.status());
176 def.maxHealth = maxHealth.value();
178 if (def.length <= 0.f || def.beam <= 0.f || def.maxSpeed < 0.f || def.accel < 0.f || def.turnRate < 0.f ||
179 def.maxHealth < 0.f) {
184 if (
const Value* gridValue =
field(
object,
"grid")) {
190 auto cols = optionalInt(*gridObject,
"cols", 4);
191 if (!
cols)
return fail<WarshipDefinition>(
cols.status());
192 auto rows = optionalInt(*gridObject,
"rows", 2);
193 if (!
rows)
return fail<WarshipDefinition>(
rows.status());
194 auto decks = optionalInt(*gridObject,
"decks", 1);
195 if (!decks)
return fail<WarshipDefinition>(decks.status());
196 if (
cols.value() <= 0 ||
rows.value() <= 0 || decks.value() <= 0) {
200 def.grid.cols =
cols.value();
201 def.grid.rows =
rows.value();
202 def.grid.decks = decks.value();
209 if (
const Value* layoutValue =
field(
object,
"defaultLayout")) {
215 for (
const Value& entry : *array) {
221 auto seed = parseSeed(*seedObject);
222 if (!
seed)
return fail<WarshipDefinition>(
seed.status());
223 if (def.grid.indexOf(
seed.value().deck,
seed.value().row,
seed.value().col) < 0) {
227 def.defaultLayout.push_back(std::move(
seed).takeValue());
231 auto tags = optionalStringArray(
object,
"tags");
232 if (!tags)
return fail<WarshipDefinition>(
tags.status());
233 def.tags = std::move(tags).takeValue();
239std::unordered_map<std::string, WarshipDefinition>& WarshipRegistry::hullTable() {
240 static std::unordered_map<std::string, WarshipDefinition>
table;
244std::unordered_map<std::string, CompartmentFunctionDef>& WarshipRegistry::functionTable() {
245 static std::unordered_map<std::string, CompartmentFunctionDef>
table;
251 if (!parsed)
return fail<int>(parsed.status());
252 Value root = std::move(parsed).takeValue();
256 auto def = parseHull(
object);
259 hullTable()[parsed.
id] = std::move(parsed);
265 for (
const Value& entry : *array) {
269 "hull array entries must be objects",
"warship.hull"));
271 if (
auto ok = accept(*
object); !
ok)
return fail<int>(
ok.status());
280 if (
auto ok = accept(*
object); !
ok)
return fail<int>(
ok.status());
286 if (!parsed)
return fail<int>(parsed.status());
287 Value root = std::move(parsed).takeValue();
291 auto def = parseFunction(
object);
294 functionTable()[parsed.
id] = std::move(parsed);
300 for (
const Value& entry : *array) {
306 if (
auto ok = accept(*
object); !
ok)
return fail<int>(
ok.status());
313 "function JSON must be an object or array",
"warship.function"));
315 if (
auto ok = accept(*
object); !
ok)
return fail<int>(
ok.status());
332 auto it = hullTable().find(
id);
333 if (it == hullTable().
end())
return nullptr;
338 auto it = functionTable().find(
id);
339 if (it == functionTable().
end())
return nullptr;
Stable, structured diagnostics shared by engine modules.
wgpu::PopErrorScopeStatus status
Owning, renderer-independent dynamic values.
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.
The canonical owning dynamic value used by data-facing protocols.
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
std::map< std::string, Value > Object
std::vector< Value > Array
static void clearFunctions()
static const CompartmentFunctionDef * findFunction(const std::string &id)
Lookup a compartment function. @ownership Borrowed; valid until the next clear / replace of that id....
static Result< int > loadHullsFromJson(std::string_view json)
static bool hasHull(const std::string &id)
static int functionCount()
static bool hasFunction(const std::string &id)
static const WarshipDefinition * findHull(const std::string &id)
Lookup a hull template. @ownership Borrowed; valid until the next clear / replace of that id....
static Result< int > loadFunctionsFromJson(std::string_view json)
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
std::unordered_map< std::string, SkillDefinition > & table()
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Warship module entry (eve.Warship): hull/function catalogues, spawn, compartment remodel,...
constexpr const char * kCategoryWarship
Documented hull category conventions (free string; not an enum).
constexpr const char * kMobilitySurface
Built-in mobility model names used by hull definitions.
Result< float > readFiniteFloatValue(const Value &value)
Convert an Integer or Double to a finite Float after validating its source range.
Result< std::string > readStringMember(const Value::Object &object, std::string_view name)
Read a required string field, including an empty string, as an owning value.
Result< double > readNumericDoubleValue(const Value &value)
Read an Int64 or Double as a double without imposing a finite-value policy.
Compartment function catalogue entry.
Hull template registered through Warship::registerHullsFromJson.