载入中...
搜索中...
未找到
ModuleAssembly.cpp
浏览该文件的文档.
2#include <algorithm>
3#include <cmath>
4#include <limits>
5#include <set>
6#include <simplesquirrel/simplesquirrel.hpp>
7#include <utility>
9#include "common/Value.h"
11
12// ModuleAssembly.cpp
13namespace eve::procgen {
14namespace {
15
16Result<void> reject(DiagnosticCode code, std::string message, std::string path) {
18 Diagnostic::error(code, std::move(message), std::move(path), {}, "procgen.moduleAssembly"));
19}
20
21struct Voxel {
22 int x;
23 int z;
24 int level;
25};
26
27int normalizedTurn(int turn) { return ((turn % 4) + 4) % 4; }
28
29Voxel rotateCell(int x, int z, int level, int width, int depth, int turn) {
30 switch (normalizedTurn(turn)) {
31 case 1: return {depth - 1 - z, x, level};
32 case 2: return {width - 1 - x, depth - 1 - z, level};
33 case 3: return {z, width - 1 - x, level};
34 default: return {x, z, level};
35 }
36}
37
38ModuleFacing rotateFacing(ModuleFacing facing, int turn) {
40 return static_cast<ModuleFacing>((static_cast<int>(facing) + normalizedTurn(turn)) % 4);
41}
42
43Voxel facingStep(ModuleFacing facing) {
44 switch (facing) {
45 case ModuleFacing::North: return {0, -1, 0};
46 case ModuleFacing::East: return {1, 0, 0};
47 case ModuleFacing::South: return {0, 1, 0};
48 case ModuleFacing::West: return {-1, 0, 0};
49 case ModuleFacing::Up: return {0, 0, 1};
50 case ModuleFacing::Down: return {0, 0, -1};
51 }
52 return {};
53}
54
58 return static_cast<ModuleFacing>((static_cast<int>(facing) + 2) % 4);
59}
60
61const ModuleDefinition* findDefinition(const std::vector<ModuleDefinition>& definitions, const std::string& id) {
62 const auto it =
63 std::find_if(definitions.begin(), definitions.end(), [&](const auto& item) { return item.id == id; });
64 return it == definitions.end() ? nullptr : &*it;
65}
66
67std::vector<Voxel> occupiedVoxels(const ModuleDefinition& definition, const ModulePlacement& placement) {
68 std::vector<Voxel> result;
69 result.reserve(static_cast<size_t>(definition.widthCells) * definition.depthCells * definition.heightLevels);
70 for (int level = 0; level < definition.heightLevels; ++level)
71 for (int z = 0; z < definition.depthCells; ++z)
72 for (int x = 0; x < definition.widthCells; ++x) {
73 const Voxel local =
74 rotateCell(x, z, level, definition.widthCells, definition.depthCells, placement.quarterTurn);
75 result.push_back({placement.cellX + local.x, placement.cellZ + local.z, placement.level + local.level});
76 }
77 return result;
78}
79
80struct WorldConnector {
81 int x;
82 int z;
83 int level;
85 const std::string* tag;
86 const std::string* accepts;
87};
88
89std::vector<WorldConnector> worldConnectors(const ModuleDefinition& definition, const ModulePlacement& placement) {
90 std::vector<WorldConnector> result;
91 result.reserve(definition.connectors.size());
92 for (const auto& connector : definition.connectors) {
93 const Voxel local = rotateCell(connector.cellX, connector.cellZ, connector.levelOffset, definition.widthCells,
94 definition.depthCells, placement.quarterTurn);
95 result.push_back({placement.cellX + local.x, placement.cellZ + local.z, placement.level + local.level,
96 rotateFacing(connector.facing, placement.quarterTurn), &connector.tag, &connector.accepts});
97 }
98 return result;
99}
100
101bool compatible(const WorldConnector& a, const WorldConnector& b) {
102 return *a.accepts == *b.tag && *b.accepts == *a.tag;
103}
104
105bool exact(const Value::Object& object, std::initializer_list<const char*> fields) {
106 std::set<std::string> expected;
107 for (const char* field : fields) expected.emplace(field);
108 if (object.size() != expected.size()) return false;
109 for (const auto& [key, value] : object) {
110 (void)value;
111 if (!expected.contains(key)) return false;
112 }
113 return true;
114}
115
116const double* number(const Value& value) { return value.getIf<double>(); }
117
118bool integer(const Value& value, int& output) {
119 const auto* encoded = value.getIf<int64_t>();
120 if (!encoded || *encoded < std::numeric_limits<int>::min() || *encoded > std::numeric_limits<int>::max())
121 return false;
122 output = static_cast<int>(*encoded);
123 return true;
124}
125
126int facingFromName(const std::string& name) {
127 static constexpr std::array<const char*, 6> names{"north", "east", "south", "west", "up", "down"};
128 const auto found = std::find(names.begin(), names.end(), name);
129 return found == names.end() ? -1 : static_cast<int>(found - names.begin());
130}
131
132} // namespace
133
134ModuleAssemblyPlan::ModuleAssemblyPlan(ModuleAssemblyConstraints constraints) : constraints_(std::move(constraints)) {}
135
136Result<void> ModuleAssemblyPlan::configure(float cellSize, float floorHeight, int minCellX, int maxCellX, int minCellZ,
137 int maxCellZ, int maxLevels, bool bounded, bool requireConnection) {
138 if (!definitions_.empty() || !placements_.empty())
140 "assembly constraints must be configured before definitions or placements", "constraints");
141 if (!std::isfinite(cellSize) || cellSize <= 0.f || !std::isfinite(floorHeight) || floorHeight <= 0.f ||
142 maxLevels <= 0 || (bounded && (minCellX > maxCellX || minCellZ > maxCellZ)))
143 return reject(DiagnosticCode::InvalidArgument, "assembly dimensions and bounds must be valid", "constraints");
144 constraints_.cellSize = cellSize;
145 constraints_.floorHeight = floorHeight;
146 constraints_.minCellX = minCellX;
147 constraints_.maxCellX = maxCellX;
148 constraints_.minCellZ = minCellZ;
149 constraints_.maxCellZ = maxCellZ;
150 constraints_.maxLevels = maxLevels;
151 constraints_.bounded = bounded;
152 constraints_.requireConnection = requireConnection;
154}
155
156Result<void> ModuleAssemblyPlan::setAllowedQuarterTurns(bool turn0, bool turn90, bool turn180, bool turn270) {
157 if (!definitions_.empty() || !placements_.empty())
159 "allowed rotations must be configured before definitions or placements", "constraints");
160 if (!turn0 && !turn90 && !turn180 && !turn270)
161 return reject(DiagnosticCode::InvalidArgument, "at least one module rotation must be allowed", "constraints");
162 constraints_.allowedQuarterTurns = {turn0, turn90, turn180, turn270};
164}
165
167 if (!definitions_.empty() || !placements_.empty())
169 "minimum support must be configured before definitions or placements", "constraints");
170 if (!std::isfinite(ratio) || ratio < 0.f || ratio > 1.f)
171 return reject(DiagnosticCode::InvalidArgument, "minimum support ratio must be between zero and one",
172 "constraints.minimumSupportRatio");
173 constraints_.minimumSupportRatio = ratio;
175}
176
178 if (!definitions_.empty() || !placements_.empty())
179 return reject(DiagnosticCode::PreconditionViolation, "JSON configuration requires an empty plan", "config");
180 if (json.size() > 1024U * 1024U)
181 return reject(DiagnosticCode::InvalidArgument, "assembly configuration exceeds 1 MiB", "config");
182 auto parsed = Value::fromJson(json);
183 if (!parsed) return Result<void>::failure(parsed.status());
184 const auto* root = parsed.value().getIf<Value::Object>();
185 if (!root || !exact(*root, {"schema", "version", "unknownFields", "constraints", "modules"}))
186 return reject(DiagnosticCode::InvalidArgument, "assembly configuration root fields are invalid", "config");
187 const auto *schema = root->at("schema").getIf<std::string>(),
188 *unknown = root->at("unknownFields").getIf<std::string>();
189 const auto* version = root->at("version").getIf<int64_t>();
190 const auto* constraints = root->at("constraints").getIf<Value::Object>();
191 const auto* modules = root->at("modules").getIf<Value::Array>();
192 if (!schema || *schema != "eve.procgen.module-assembly" || !version || *version != 1 || !unknown ||
193 *unknown != "reject" || !constraints || !modules || modules->empty() || modules->size() > 4096 ||
194 !exact(*constraints, {"cellSize", "floorHeight", "bounds", "maxLevels", "allowedQuarterTurns",
195 "requireConnection", "minimumSupportRatio"}))
196 return reject(DiagnosticCode::InvalidArgument, "assembly configuration schema is invalid", "config");
197
198 const auto *cellSize = number(constraints->at("cellSize")), *floorHeight = number(constraints->at("floorHeight")),
199 *support = number(constraints->at("minimumSupportRatio"));
200 const auto* bounds = constraints->at("bounds").getIf<Value::Object>();
201 const auto* turns = constraints->at("allowedQuarterTurns").getIf<Value::Array>();
202 const auto* requireConnection = constraints->at("requireConnection").getIf<bool>();
203 int maxLevels = 0;
204 if (!cellSize || !floorHeight || !support || !bounds || !turns || turns->size() != 4 || !requireConnection ||
205 !integer(constraints->at("maxLevels"), maxLevels) ||
206 !exact(*bounds, {"enabled", "minCellX", "maxCellX", "minCellZ", "maxCellZ"}))
207 return reject(DiagnosticCode::InvalidArgument, "assembly constraint fields are invalid", "constraints");
208 const auto* bounded = bounds->at("enabled").getIf<bool>();
209 int minX = 0, maxX = 0, minZ = 0, maxZ = 0;
210 std::array<bool, 4> allowed{};
211 for (size_t i = 0; i < allowed.size(); ++i) {
212 const auto* enabled = (*turns)[i].getIf<bool>();
213 if (!enabled)
214 return reject(DiagnosticCode::InvalidArgument, "rotation entries must be booleans", "constraints");
215 allowed[i] = *enabled;
216 }
217 if (!bounded || !integer(bounds->at("minCellX"), minX) || !integer(bounds->at("maxCellX"), maxX) ||
218 !integer(bounds->at("minCellZ"), minZ) || !integer(bounds->at("maxCellZ"), maxZ))
219 return reject(DiagnosticCode::InvalidArgument, "assembly bounds are invalid", "constraints.bounds");
220
221 ModuleAssemblyPlan candidate;
222 auto configured = candidate.configure(float(*cellSize), float(*floorHeight), minX, maxX, minZ, maxZ, maxLevels,
223 *bounded, *requireConnection);
224 if (!configured) return configured;
225 auto rotations = candidate.setAllowedQuarterTurns(allowed[0], allowed[1], allowed[2], allowed[3]);
226 if (!rotations) return rotations;
227 auto supported = candidate.setMinimumSupportRatio(float(*support));
228 if (!supported) return supported;
229 for (const Value& encodedModule : *modules) {
230 const auto* module = encodedModule.getIf<Value::Object>();
231 if (!module || !exact(*module, {"id", "widthCells", "depthCells", "heightLevels", "connectors"}))
232 return reject(DiagnosticCode::InvalidArgument, "module fields are invalid", "modules");
233 const auto* id = module->at("id").getIf<std::string>();
234 const auto* connectors = module->at("connectors").getIf<Value::Array>();
235 int width = 0, depth = 0, height = 0;
236 if (!id || !connectors || connectors->size() > 16384 || !integer(module->at("widthCells"), width) ||
237 !integer(module->at("depthCells"), depth) || !integer(module->at("heightLevels"), height))
238 return reject(DiagnosticCode::InvalidArgument, "module values are invalid", "modules");
239 auto registered = candidate.registerVolumeModuleType(*id, width, depth, height);
240 if (!registered) return registered;
241 for (const Value& encodedConnector : *connectors) {
242 const auto* connector = encodedConnector.getIf<Value::Object>();
243 if (!connector || !exact(*connector, {"cellX", "cellZ", "level", "facing", "tag", "accepts"}))
244 return reject(DiagnosticCode::InvalidArgument, "connector fields are invalid", *id);
245 int x = 0, z = 0, level = 0;
246 const auto *facing = connector->at("facing").getIf<std::string>(),
247 *tag = connector->at("tag").getIf<std::string>(),
248 *accepts = connector->at("accepts").getIf<std::string>();
249 if (!facing || !tag || !accepts || !integer(connector->at("cellX"), x) ||
250 !integer(connector->at("cellZ"), z) || !integer(connector->at("level"), level))
251 return reject(DiagnosticCode::InvalidArgument, "connector values are invalid", *id);
252 auto added = candidate.addVolumeConnector(*id, x, z, level, facingFromName(*facing), *tag, *accepts);
253 if (!added) return added;
254 }
255 }
256 *this = std::move(candidate);
258}
259
260Result<void> ModuleAssemblyPlan::registerModuleType(std::string id, int widthCells, int depthCells) {
261 return registerModule({std::move(id), widthCells, depthCells, {}});
262}
263
264Result<void> ModuleAssemblyPlan::registerVolumeModuleType(std::string id, int widthCells, int depthCells,
265 int heightLevels) {
266 ModuleDefinition definition{std::move(id), widthCells, depthCells, {}};
267 definition.heightLevels = heightLevels;
268 return registerModule(std::move(definition));
269}
270
271Result<void> ModuleAssemblyPlan::addConnector(const std::string& moduleId, int cellX, int cellZ, int facing,
272 std::string tag, std::string accepts) {
273 return addVolumeConnector(moduleId, cellX, cellZ, 0, facing, std::move(tag), std::move(accepts));
274}
275
276Result<void> ModuleAssemblyPlan::addVolumeConnector(const std::string& moduleId, int cellX, int cellZ, int levelOffset,
277 int facing, std::string tag, std::string accepts) {
278 auto definition =
279 std::find_if(definitions_.begin(), definitions_.end(), [&](const auto& item) { return item.id == moduleId; });
280 if (definition == definitions_.end())
281 return reject(DiagnosticCode::NotFound, "module definition is not registered", moduleId);
282 if (std::any_of(placements_.begin(), placements_.end(),
283 [&](const auto& placement) { return placement.moduleId == moduleId; }))
285 "connectors cannot change after the module has been placed", moduleId);
286 if (facing < 0 || facing > 5 || cellX < 0 || cellX >= definition->widthCells || cellZ < 0 ||
287 cellZ >= definition->depthCells || levelOffset < 0 || levelOffset >= definition->heightLevels || tag.empty() ||
288 accepts.empty())
289 return reject(DiagnosticCode::InvalidArgument, "connector fields are outside the module contract", moduleId);
290 definition->connectors.push_back(
291 {cellX, cellZ, static_cast<ModuleFacing>(facing), std::move(tag), std::move(accepts), levelOffset});
293}
294
296 if (definition.id.empty()) return reject(DiagnosticCode::InvalidArgument, "module id must not be empty", "id");
297 if (definition.widthCells <= 0 || definition.depthCells <= 0 || definition.heightLevels <= 0)
298 return reject(DiagnosticCode::InvalidArgument, "module volume dimensions must be positive", definition.id);
299 if (findDefinition(definitions_, definition.id))
300 return reject(DiagnosticCode::AlreadyExists, "module id is already registered", definition.id);
301 for (const auto& connector : definition.connectors) {
302 if (connector.cellX < 0 || connector.cellX >= definition.widthCells || connector.cellZ < 0 ||
303 connector.cellZ >= definition.depthCells || connector.levelOffset < 0 ||
304 connector.levelOffset >= definition.heightLevels || connector.tag.empty() || connector.accepts.empty())
305 return reject(DiagnosticCode::InvalidArgument, "connector must reference a volume cell and typed tags",
306 definition.id);
307 }
308 definitions_.push_back(std::move(definition));
310}
311
313 const ModuleDefinition* definition = findDefinition(definitions_, placement.moduleId);
314 if (!definition) return reject(DiagnosticCode::NotFound, "module definition is not registered", placement.moduleId);
315 placement.quarterTurn = normalizedTurn(placement.quarterTurn);
316 if (!constraints_.allowedQuarterTurns[static_cast<size_t>(placement.quarterTurn)])
317 return reject(DiagnosticCode::PreconditionViolation, "module rotation is disabled by the assembly constraints",
318 placement.moduleId);
319 if (placement.level < 0 || int64_t(placement.level) + definition->heightLevels > constraints_.maxLevels)
320 return reject(DiagnosticCode::PreconditionViolation, "module level is outside the configured range",
321 placement.moduleId);
322
323 const int width = placement.quarterTurn % 2 ? definition->depthCells : definition->widthCells;
324 const int depth = placement.quarterTurn % 2 ? definition->widthCells : definition->depthCells;
325 if (int64_t(placement.cellX) + width - 1 > std::numeric_limits<int>::max() ||
326 int64_t(placement.cellZ) + depth - 1 > std::numeric_limits<int>::max())
327 return reject(DiagnosticCode::InvalidArgument, "module coordinates overflow the grid range",
328 placement.moduleId);
329
330 const auto newVoxels = occupiedVoxels(*definition, placement);
331 std::vector<WorldConnector> existingConnectors;
332 std::vector<Voxel> existingVoxels;
333 bool matchedConnection = false;
334 for (const auto& existing : placements_) {
335 const auto* existingDefinition = findDefinition(definitions_, existing.moduleId);
336 const auto occupied = occupiedVoxels(*existingDefinition, existing);
337 {
338 for (const Voxel& candidate : newVoxels)
339 if (std::any_of(occupied.begin(), occupied.end(), [&](Voxel cell) {
340 return cell.x == candidate.x && cell.z == candidate.z && cell.level == candidate.level;
341 }))
342 return reject(DiagnosticCode::Conflict, "module volume overlaps an existing placement",
343 placement.moduleId);
344 auto connectors = worldConnectors(*existingDefinition, existing);
345 existingConnectors.insert(existingConnectors.end(), connectors.begin(), connectors.end());
346 existingVoxels.insert(existingVoxels.end(), occupied.begin(), occupied.end());
347 }
348 }
349 if (constraints_.bounded)
350 for (const Voxel& cell : newVoxels)
351 if (cell.x < constraints_.minCellX || cell.x > constraints_.maxCellX || cell.z < constraints_.minCellZ ||
352 cell.z > constraints_.maxCellZ)
353 return reject(DiagnosticCode::PreconditionViolation, "module footprint crosses assembly bounds",
354 placement.moduleId);
355
356 if (placement.level > 0 && constraints_.minimumSupportRatio > 0.f) {
357 const size_t bottomCellCount = size_t(definition->widthCells) * definition->depthCells;
358 size_t supportedCellCount = 0;
359 for (const Voxel& cell : newVoxels) {
360 if (cell.level != placement.level) continue;
361 if (std::any_of(existingVoxels.begin(), existingVoxels.end(), [&](Voxel other) {
362 return other.x == cell.x && other.z == cell.z && other.level == cell.level - 1;
363 }))
364 ++supportedCellCount;
365 }
366 const size_t requiredSupport =
367 static_cast<size_t>(std::ceil(double(constraints_.minimumSupportRatio) * double(bottomCellCount)));
368 if (supportedCellCount < requiredSupport)
370 "elevated module does not meet the configured vertical support ratio", placement.moduleId);
371 }
372
373 const auto connectors = worldConnectors(*definition, placement);
374 for (const Voxel& cell : newVoxels) {
375 for (int direction = 0; direction < 6; ++direction) {
376 const auto facing = static_cast<ModuleFacing>(direction);
377 const Voxel step = facingStep(facing);
378 const int64_t neighborX = int64_t(cell.x) + step.x;
379 const int64_t neighborZ = int64_t(cell.z) + step.z;
380 const int64_t neighborLevel = int64_t(cell.level) + step.level;
381 bool neighborOccupied = false;
382 neighborOccupied = std::any_of(existingVoxels.begin(), existingVoxels.end(), [&](Voxel other) {
383 return other.x == neighborX && other.z == neighborZ && other.level == neighborLevel;
384 });
385 if (!neighborOccupied) continue;
386 const auto own = std::find_if(connectors.begin(), connectors.end(), [&](const auto& connector) {
387 return connector.x == cell.x && connector.z == cell.z && connector.level == cell.level &&
388 connector.facing == facing;
389 });
390 const auto other =
391 std::find_if(existingConnectors.begin(), existingConnectors.end(), [&](const auto& connector) {
392 return connector.x == neighborX && connector.z == neighborZ && connector.level == neighborLevel &&
393 connector.facing == opposite(facing);
394 });
395 if (own == connectors.end() || other == existingConnectors.end() || !compatible(*own, *other))
396 return reject(DiagnosticCode::Conflict, "touching module edges do not expose compatible connectors",
397 placement.moduleId);
398 matchedConnection = true;
399 }
400 }
401 if (constraints_.requireConnection && !placements_.empty() && !matchedConnection)
402 return reject(DiagnosticCode::PreconditionViolation, "module is disconnected from the existing assembly",
403 placement.moduleId);
404 placements_.push_back(std::move(placement));
406}
407
409 return index >= 0 && static_cast<size_t>(index) < placements_.size()
410 ? placements_[static_cast<size_t>(index)].moduleId
411 : std::string{};
412}
413
414float ModuleAssemblyPlan::getPlacementX(int index) const noexcept {
415 return index >= 0 && static_cast<size_t>(index) < placements_.size()
416 ? float(placements_[static_cast<size_t>(index)].cellX) * constraints_.cellSize
417 : 0.f;
418}
419
420float ModuleAssemblyPlan::getPlacementY(int index) const noexcept {
421 return index >= 0 && static_cast<size_t>(index) < placements_.size()
422 ? float(placements_[static_cast<size_t>(index)].level) * constraints_.floorHeight
423 : 0.f;
424}
425
426float ModuleAssemblyPlan::getPlacementZ(int index) const noexcept {
427 return index >= 0 && static_cast<size_t>(index) < placements_.size()
428 ? float(placements_[static_cast<size_t>(index)].cellZ) * constraints_.cellSize
429 : 0.f;
430}
431
433 return index >= 0 && static_cast<size_t>(index) < placements_.size()
434 ? float(normalizedTurn(placements_[static_cast<size_t>(index)].quarterTurn)) * 90.f
435 : 0.f;
436}
437
438} // namespace eve::procgen
439
440// ModuleAssemblyScript.cpp
441namespace eve::procgen {
442void exposeModuleAssembly(ssq::Table& table) {
443 auto vm = table.getHandle();
444 auto assembly = table.addClass<ModuleAssemblyPlan>(
445 "ProcgenModuleAssemblyPlan", std::function<ModuleAssemblyPlan*()>([] { return new ModuleAssemblyPlan(); }),
446 true);
447 assembly.addFunc(
448 "configure", [vm](ModuleAssemblyPlan* value, float cellSize, float floorHeight, int minCellX, int maxCellX,
449 int minCellZ, int maxCellZ, int maxLevels, bool bounded, bool requireConnection) {
450 return eve::script::projectResult(vm, value->configure(cellSize, floorHeight, minCellX, maxCellX, minCellZ,
451 maxCellZ, maxLevels, bounded, requireConnection));
452 });
453 assembly.addFunc(
454 "setAllowedQuarterTurns", [vm](ModuleAssemblyPlan* value, bool turn0, bool turn90, bool turn180, bool turn270) {
455 return eve::script::projectResult(vm, value->setAllowedQuarterTurns(turn0, turn90, turn180, turn270));
456 });
457 assembly.addFunc("setMinimumSupportRatio", [vm](ModuleAssemblyPlan* value, float ratio) {
458 return eve::script::projectResult(vm, value->setMinimumSupportRatio(ratio));
459 });
460 assembly.addFunc("applyConfigJson", [vm](ModuleAssemblyPlan* value, const std::string& json) {
461 return eve::script::projectResult(vm, value->applyConfigJson(json));
462 });
463 assembly.addFunc("registerModule",
464 [vm](ModuleAssemblyPlan* value, const std::string& id, int widthCells, int depthCells) {
465 return eve::script::projectResult(vm, value->registerModuleType(id, widthCells, depthCells));
466 });
467 assembly.addFunc("registerVolumeModule", [vm](ModuleAssemblyPlan* value, const std::string& id, int widthCells,
468 int depthCells, int heightLevels) {
470 value->registerVolumeModuleType(id, widthCells, depthCells, heightLevels));
471 });
472 assembly.addFunc("addConnector", [vm](ModuleAssemblyPlan* value, const std::string& id, int cellX, int cellZ,
473 int facing, const std::string& tag, const std::string& accepts) {
474 return eve::script::projectResult(vm, value->addConnector(id, cellX, cellZ, facing, tag, accepts));
475 });
476 assembly.addFunc("addVolumeConnector",
477 [vm](ModuleAssemblyPlan* value, const std::string& id, int cellX, int cellZ, int levelOffset,
478 int facing, const std::string& tag, const std::string& accepts) {
480 vm, value->addVolumeConnector(id, cellX, cellZ, levelOffset, facing, tag, accepts));
481 });
482 assembly.addFunc("place", [vm](ModuleAssemblyPlan* value, const std::string& id, int cellX, int cellZ, int level,
483 int quarterTurn) {
484 return eve::script::projectResult(vm, value->place({id, cellX, cellZ, level, quarterTurn}));
485 });
486 assembly.addFunc("getPlacementCount", [](ModuleAssemblyPlan* value) { return value->getPlacementCount(); });
487 assembly.addFunc("getPlacementModule",
488 [](ModuleAssemblyPlan* value, int index) { return value->getPlacementModule(index); });
489 assembly.addFunc("getPlacementX", [](ModuleAssemblyPlan* value, int index) { return value->getPlacementX(index); });
490 assembly.addFunc("getPlacementY", [](ModuleAssemblyPlan* value, int index) { return value->getPlacementY(index); });
491 assembly.addFunc("getPlacementZ", [](ModuleAssemblyPlan* value, int index) { return value->getPlacementZ(index); });
492 assembly.addFunc("getPlacementYawDegrees",
493 [](ModuleAssemblyPlan* value, int index) { return value->getPlacementYawDegrees(index); });
494}
495} // namespace eve::procgen
double z
double x
int root
Definition AnimSmr.cpp:119
std::string output
HSQUIRRELVM vm
Definition ECS.cpp:38
SQObjectPtr key
Definition ECS.cpp:155
std::string name
Definition ECS.cpp:154
std::vector< std::string > fields
Definition ECS.cpp:42
std::string message
DiagnosticCode code
std::uint32_t height
std::uint32_t width
float ratio
std::map< unsigned, Colour > definitions
std::string local
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
const std::string * accepts
ModuleFacing facing
int level
const std::string * tag
Placement placement
std::string path
Definition PlayHost.cpp:111
Object::Ptr value
RoadLaneDirection direction
double number
bool found
bool occupied
Definition Shadow.cpp:155
The single Squirrel projection for common Result, Status and Value.
Cell cell
TacticalUnit::TurnResources turn
Json object
float step
Definition TreeMesh.cpp:451
float size
Definition TreeMesh.cpp:158
std::uint32_t depth
uint32_t index
int turns
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.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
The canonical owning dynamic value used by data-facing protocols.
Definition Value.h:31
static Result< Value > fromJson(std::string_view json)
Parse one strict JSON value into an owning Value.
Definition Value.cpp:177
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
Deterministic owner of a validated modular assembly plan.
Result< void > registerVolumeModuleType(std::string id, int widthCells, int depthCells, int heightLevels)
Register a three-dimensional module volume using scalar script-friendly fields.
std::string getPlacementModule(int index) const
Return one accepted placement's module id, or empty text for an invalid index.
const ModuleAssemblyConstraints & constraints() const noexcept
Return the immutable constraint configuration.
Result< void > place(ModulePlacement placement)
Validate and atomically append one placement. @cost Linear in existing occupied cells and connectors.
Result< void > applyConfigJson(const std::string &json)
Atomically replace an empty plan's constraints and module definitions from strict JSON....
Result< void > setAllowedQuarterTurns(bool turn0, bool turn90, bool turn180, bool turn270)
Configure the four permitted quarter-turn rotations before registration or placement.
Result< void > addConnector(const std::string &moduleId, int cellX, int cellZ, int facing, std::string tag, std::string accepts)
Append a typed connector to a registered, not-yet-placed module.
float getPlacementY(int index) const noexcept
Return one placement's world Y coordinate, or zero for an invalid index.
Result< void > registerModuleType(std::string id, int widthCells, int depthCells)
Register a module footprint using scalar script-friendly fields.
ModuleAssemblyPlan()=default
Construct an empty plan with default constraints.
float getPlacementX(int index) const noexcept
Return one placement's world X coordinate, or zero for an invalid index.
float getPlacementZ(int index) const noexcept
Return one placement's world Z coordinate, or zero for an invalid index.
Result< void > configure(float cellSize, float floorHeight, int minCellX, int maxCellX, int minCellZ, int maxCellZ, int maxLevels, bool bounded, bool requireConnection)
Configure an empty plan before definitions or placements are added.
Result< void > addVolumeConnector(const std::string &moduleId, int cellX, int cellZ, int levelOffset, int facing, std::string tag, std::string accepts)
Append a typed connector to one face of a cell in a module volume.
float getPlacementYawDegrees(int index) const noexcept
Return one placement's yaw in degrees, or zero for an invalid index.
Result< void > registerModule(ModuleDefinition definition)
Register one module definition before it is placed.
Result< void > setMinimumSupportRatio(float ratio)
Require this fraction of an elevated module's bottom cells to be supported from below.
const Value * field(const Value &value, const char *key)
Borrow an optional object field.
constexpr HexDirection opposite(HexDirection d) noexcept
The direction opposite to d.
Definition HexMetrics.h:65
ModuleFacing
Horizontal or vertical face used by grid-module connection constraints.
void exposeModuleAssembly(ssq::Table &table)
Bind the VM-owned assembly planner synchronously on the VM owner thread.
ssq::Table projectResult(HSQUIRRELVM vm, Result< void > &&result)
Consume and project a void native Result using the common schema.
int64_t integer(const RuntimeTensor &v, size_t i=0)
Integer.
DiagnosticCode
Stable machine-readable diagnostic codes.
Definition Diagnostic.h:47
bool enabled
Configurable limits shared by automatic modular-map generators.
Immutable footprint and connection contract for one modular asset.
World-grid placement accepted by a ModuleAssemblyPlan.
glm::vec4 bounds