载入中...
搜索中...
未找到
RoadNetworkEditingCommands.cpp
浏览该文件的文档.
2#include "procgen/editing/RoadNetworkEditTargetInternal.inc"
3
4#include <algorithm>
5#include <cmath>
6#include <utility>
7
8namespace eve::procgen_editing {
9using namespace detail;
10
12 auto add = [&registry](const char* id, const char* name, editing::EditingCommandPlanner planner) {
15 descriptor.ownerModule = "procgen_editing.road";
16 descriptor.displayName = name;
17 descriptor.category = "Road";
18 descriptor.automationAllowed = true;
19 return registry.registerPlannedCommand(std::move(descriptor), std::move(planner));
20 };
24 if (operation.code() == EditorStatus::NoOp)
26 eve::Status(EditorStatus::NoOp, {}));
27 result.operations.push_back(std::move(operation).takeValue());
28 return editing::applied(std::move(result));
29 };
30 auto target = [](editing::IEditableTarget& value) { return dynamic_cast<RoadNetworkEditTarget*>(&value); };
31
32 auto registered = add(
33 "road.node.add.v1", "Add road node",
35 auto* road = target(value);
36 float x = 0.f, y = 0.f, z = 0.f, radius = 6.f;
37 if (!road || !readNumber(request.payload, "x", x) || !readNumber(request.payload, "y", y) ||
38 !readNumber(request.payload, "z", z) ||
39 (field(request.payload, "junctionRadius") && !readNumber(request.payload, "junctionRadius", radius)))
40 return commandError<editing::CommandPlan>("editor.road.command.node-add",
41 "Road node add requires finite x, y and z");
42 return plan(road->makeAddNode(x, y, z, radius));
43 });
44 if (!registered.ok()) return registered;
45
46 registered = add("road.node.move.v1", "Move road node",
48 auto* road = target(value);
49 std::uint32_t id = 0;
50 float x = 0.f, y = 0.f, z = 0.f;
51 if (!road || !readId(request.payload, "id", id) || !readNumber(request.payload, "x", x) ||
52 !readNumber(request.payload, "y", y) || !readNumber(request.payload, "z", z))
53 return commandError<editing::CommandPlan>(
54 "editor.road.command.node-move", "Road node move requires id and finite x, y and z");
55 return plan(road->makeMoveNode(id, x, y, z));
56 });
57 if (!registered.ok()) return registered;
58
59 registered =
60 add("road.node.connection.set.v1", "Set custom road connection",
62 auto* road = target(value);
63 std::uint32_t id = 0;
65 const auto* encoded = field(request.payload, "connection");
66 if (!road || !readId(request.payload, "id", id) || !encoded || !decodeConnection(*encoded, connection))
67 return commandError<editing::CommandPlan>("editor.road.command.connection",
68 "custom connection requires id and valid asset data");
69 return plan(road->makeSetNodeConnection(id, std::move(connection)));
70 });
71 if (!registered.ok()) return registered;
72
73 registered = add(
74 "road.node.radius.set.v1", "Set road junction radius",
76 auto* road = target(value);
77 std::uint32_t id = 0;
78 float radius = 0.f;
79 if (!road || !readId(request.payload, "id", id) || !readNumber(request.payload, "junctionRadius", radius))
80 return commandError<editing::CommandPlan>("editor.road.command.node-radius",
81 "Road node radius requires id and radius");
82 return plan(road->makeSetNodeJunctionRadius(id, radius));
83 });
84 if (!registered.ok()) return registered;
85
86 registered = add(
87 "road.node.junction-control.set.v1", "Set road junction control",
89 auto* road = target(value);
90 std::uint32_t id = 0;
91 int control = 0;
92 if (!road || !readId(request.payload, "id", id) ||
93 !readSmallInt(request.payload, "junctionControl", 0, 3, control))
94 return commandError<editing::CommandPlan>(
95 "editor.road.command.node-control",
96 "Road junction control requires id and junctionControl in [0,3]");
97 return plan(road->makeSetNodeJunctionControl(id, static_cast<procgen::road::RoadJunctionControl>(control)));
98 });
99 if (!registered.ok()) return registered;
100
101 registered =
102 add("road.node.merge.v1", "Merge nearby road nodes",
104 auto* road = target(value);
105 std::uint32_t keepNodeId = 0, removeNodeId = 0;
106 float maxDistance = 1.f;
107 if (!road || !readId(request.payload, "keepNodeId", keepNodeId) ||
108 !readId(request.payload, "removeNodeId", removeNodeId) ||
109 (field(request.payload, "maxDistance") && !readNumber(request.payload, "maxDistance", maxDistance)))
110 return commandError<editing::CommandPlan>("editor.road.command.node-merge",
111 "Road node merge requires two node ids");
112 return plan(road->makeMergeNodes(keepNodeId, removeNodeId, maxDistance));
113 });
114 if (!registered.ok()) return registered;
115
116 registered =
117 add("road.node.merge-and-connect.v1", "Merge road nodes and connect turns",
119 auto* road = target(value);
120 std::uint32_t keepNodeId = 0, removeNodeId = 0;
121 float maxDistance = 1.f;
122 if (!road || !readId(request.payload, "keepNodeId", keepNodeId) ||
123 !readId(request.payload, "removeNodeId", removeNodeId) ||
124 (field(request.payload, "maxDistance") && !readNumber(request.payload, "maxDistance", maxDistance)))
125 return commandError<editing::CommandPlan>("editor.road.command.node-merge-connect",
126 "Road node merge and connect requires two node ids");
127 return plan(road->makeMergeNodesAndConnect(keepNodeId, removeNodeId, maxDistance));
128 });
129 if (!registered.ok()) return registered;
130
131 registered = add("road.node.remove.v1", "Remove road node",
133 auto* road = target(value);
134 std::uint32_t id = 0;
135 if (!road || !readId(request.payload, "id", id))
136 return commandError<editing::CommandPlan>("editor.road.command.node-remove",
137 "Road node remove requires id");
138 return plan(road->makeRemoveNode(id));
139 });
140 if (!registered.ok()) return registered;
141
142 registered = add("road.node.connect-all-turns.v1", "Connect all road turns",
144 auto* road = target(value);
145 std::uint32_t id = 0;
146 if (!road || !readId(request.payload, "id", id))
147 return commandError<editing::CommandPlan>("editor.road.command.connect-turns",
148 "Connect all turns requires a node id");
149 return plan(road->makeConnectAllTurns(id));
150 });
151 if (!registered.ok()) return registered;
152
153 registered =
154 add("road.edge.add.v1", "Add road edge",
156 auto* road = target(value);
157 std::uint32_t from = 0, to = 0, ignored = 0;
158 int forward = 2, backward = 0;
159 std::vector<procgen::road::RoadControlPoint> points;
160 EditorValue::Object pointPayload{{"id", std::int64_t{1}}};
161 if (const auto* encoded = field(request.payload, "points")) pointPayload["points"] = *encoded;
162 if (const auto* profiles = field(request.payload, "pointProfiles"))
163 pointPayload["pointProfiles"] = *profiles;
164 if (!road || !readId(request.payload, "from", from) || !readId(request.payload, "to", to) ||
165 !decodeEdge(EditorValue(std::move(pointPayload)), ignored, points) ||
166 (field(request.payload, "lanesForward") &&
167 !readSmallInt(request.payload, "lanesForward", 0, 8, forward)) ||
168 (field(request.payload, "lanesBackward") &&
169 !readSmallInt(request.payload, "lanesBackward", 0, 8, backward)))
170 return commandError<editing::CommandPlan>(
171 "editor.road.command.edge-add", "Road edge add requires endpoints and at least two points");
172 return plan(road->makeAddEdge(from, to, std::move(points), forward, backward));
173 });
174 if (!registered.ok()) return registered;
175
176 registered =
177 add("road.edge.add-and-connect.v1", "Add road edge and connect endpoints",
179 auto* road = target(value);
180 std::uint32_t from = 0, to = 0, ignored = 0;
181 int forward = 2, backward = 0;
182 std::vector<procgen::road::RoadControlPoint> points;
183 EditorValue::Object pointPayload{{"id", std::int64_t{1}}};
184 if (const auto* encoded = field(request.payload, "points")) pointPayload["points"] = *encoded;
185 if (const auto* profiles = field(request.payload, "pointProfiles"))
186 pointPayload["pointProfiles"] = *profiles;
187 if (!road || !readId(request.payload, "from", from) || !readId(request.payload, "to", to) ||
188 !decodeEdge(EditorValue(std::move(pointPayload)), ignored, points) ||
189 (field(request.payload, "lanesForward") &&
190 !readSmallInt(request.payload, "lanesForward", 0, 8, forward)) ||
191 (field(request.payload, "lanesBackward") &&
192 !readSmallInt(request.payload, "lanesBackward", 0, 8, backward)))
193 return commandError<editing::CommandPlan>(
194 "editor.road.command.edge-add-connect",
195 "Road edge add and connect requires endpoints and at least two points");
197 if (const auto* encodedStyle = field(request.payload, "style")) {
198 const auto* styleObject = encodedStyle->getIf<EditorValue::Object>();
199 if (!styleObject)
200 return commandError<editing::CommandPlan>("editor.road.command.edge-add-connect-style",
201 "Road edge style is invalid");
202 EditorValue::Object stylePayload = *styleObject;
203 stylePayload["id"] = std::int64_t{1};
204 if (!decodeStyle(EditorValue(std::move(stylePayload)), ignored, style))
205 return commandError<editing::CommandPlan>("editor.road.command.edge-add-connect-style",
206 "Road edge style is invalid");
207 }
208 return plan(road->makeAddEdgeAndConnect(from, to, std::move(points), forward, backward, style));
209 });
210 if (!registered.ok()) return registered;
211
212 registered = add(
213 "road.edge.split.v1", "Split road edge",
215 auto* road = target(value);
216 std::uint32_t id = 0;
217 int index = 0;
218 float radius = 6.f;
219 if (!road || !readId(request.payload, "id", id) ||
220 !readSmallInt(request.payload, "controlPointIndex", 1, 4095, index) ||
221 (field(request.payload, "junctionRadius") && !readNumber(request.payload, "junctionRadius", radius)))
222 return commandError<editing::CommandPlan>(
223 "editor.road.command.edge-split", "Road split requires edge id and interior control-point index");
224 return plan(road->makeSplitEdge(id, static_cast<std::size_t>(index), radius));
225 });
226 if (!registered.ok()) return registered;
227
228 registered = add(
229 "road.edge.split-at-position.v1", "Split road edge at world position",
231 auto* road = target(value);
232 std::uint32_t id = 0;
234 float maxDistance = 2.f, radius = 6.f;
235 if (!road || !readId(request.payload, "id", id) || !readNumber(request.payload, "x", position.x) ||
236 !readNumber(request.payload, "y", position.y) || !readNumber(request.payload, "z", position.z) ||
237 (field(request.payload, "maxDistance") && !readNumber(request.payload, "maxDistance", maxDistance)) ||
238 (field(request.payload, "junctionRadius") && !readNumber(request.payload, "junctionRadius", radius)))
239 return commandError<editing::CommandPlan>(
240 "editor.road.command.edge-split-position",
241 "Road position split requires edge id and a finite world position");
242 return plan(road->makeSplitEdgeAtPosition(id, position, maxDistance, radius));
243 });
244 if (!registered.ok()) return registered;
245
246 registered = add(
247 "road.node.connect-at-position.v1", "Connect road node at world position",
249 auto* road = target(value);
250 std::uint32_t sourceNodeId = 0, edgeId = 0;
252 float maxDistance = 2.f, radius = 6.f;
253 int forward = 1, backward = 0;
254 if (!road || !readId(request.payload, "sourceNodeId", sourceNodeId) ||
255 !readId(request.payload, "edgeId", edgeId) || !readNumber(request.payload, "x", position.x) ||
256 !readNumber(request.payload, "y", position.y) || !readNumber(request.payload, "z", position.z) ||
257 (field(request.payload, "maxDistance") && !readNumber(request.payload, "maxDistance", maxDistance)) ||
258 (field(request.payload, "junctionRadius") && !readNumber(request.payload, "junctionRadius", radius)) ||
259 (field(request.payload, "lanesForward") &&
260 !readSmallInt(request.payload, "lanesForward", 0, 8, forward)) ||
261 (field(request.payload, "lanesBackward") &&
262 !readSmallInt(request.payload, "lanesBackward", 0, 8, backward)))
263 return commandError<editing::CommandPlan>(
264 "editor.road.command.node-connect-position",
265 "Road branch connection requires source node, edge and world position");
266 return plan(road->makeConnectNodeAtPosition(sourceNodeId, edgeId, position, maxDistance, radius, forward,
267 backward));
268 });
269 if (!registered.ok()) return registered;
270
271 registered = add(
272 "road.edge.connect-intersection.v1", "Connect crossing road edges",
274 auto* road = target(value);
275 std::uint32_t firstEdgeId = 0, secondEdgeId = 0;
276 float maximumHeightDelta = 0.5f, radius = 6.f;
277 if (!road || !readId(request.payload, "firstEdgeId", firstEdgeId) ||
278 !readId(request.payload, "secondEdgeId", secondEdgeId) ||
279 (field(request.payload, "maximumHeightDelta") &&
280 !readNumber(request.payload, "maximumHeightDelta", maximumHeightDelta)) ||
281 (field(request.payload, "junctionRadius") && !readNumber(request.payload, "junctionRadius", radius)))
282 return commandError<editing::CommandPlan>(
283 "editor.road.command.edge-intersection",
284 "Road intersection requires two edge ids and finite tolerances");
285 return plan(road->makeConnectEdgeIntersection(firstEdgeId, secondEdgeId, maximumHeightDelta, radius));
286 });
287 if (!registered.ok()) return registered;
288
289 registered = add("road.edge.lanes.set.v1", "Set road edge lanes",
291 auto* road = target(value);
292 std::uint32_t id = 0;
293 int forward = 0, backward = 0;
294 if (!road || !readId(request.payload, "id", id) ||
295 !readSmallInt(request.payload, "lanesForward", 0, 8, forward) ||
296 !readSmallInt(request.payload, "lanesBackward", 0, 8, backward))
297 return commandError<editing::CommandPlan>("editor.road.command.edge-lanes",
298 "Road lane edit requires id and lane counts");
299 return plan(road->makeSetEdgeLaneCounts(id, forward, backward));
300 });
301 if (!registered.ok()) return registered;
302
303 registered = add("road.edge.style.set.v1", "Set road edge style",
305 auto* road = target(value);
306 std::uint32_t edgeId = 0;
308 if (!road || !decodeStyle(request.payload, edgeId, style))
309 return commandError<editing::CommandPlan>(
310 "editor.road.command.edge-style", "Road edge style requires a complete style payload");
311 return plan(road->makeSetEdgeStyle(edgeId, style));
312 });
313 if (!registered.ok()) return registered;
314
315 registered = add("road.edge.control-points.set.v1", "Set road edge control points",
317 auto* road = target(value);
318 std::uint32_t edgeId = 0;
319 std::vector<procgen::road::RoadControlPoint> points;
320 if (!road || !decodeEdge(request.payload, edgeId, points))
321 return commandError<editing::CommandPlan>(
322 "editor.road.command.edge-control-points",
323 "Road edge control points require id and at least two finite points");
324 return plan(road->makeSetEdgeControlPoints(edgeId, std::move(points)));
325 });
326 if (!registered.ok()) return registered;
327
328 registered = add("road.edge.reverse.v1", "Reverse road edge",
330 auto* road = target(value);
331 std::uint32_t edgeId = 0;
332 if (!road || !readId(request.payload, "id", edgeId))
333 return commandError<editing::CommandPlan>("editor.road.command.edge-reverse",
334 "Road edge reverse requires an id");
335 return plan(road->makeReverseEdge(edgeId));
336 });
337 if (!registered.ok()) return registered;
338
339 registered = add("road.edge.reconnect-endpoint.v1", "Reconnect road edge endpoint",
341 auto* road = target(value);
342 std::uint32_t edgeId = 0, nodeId = 0;
343 const auto* endpointValue = field(request.payload, "fromEndpoint");
344 const auto* fromEndpoint = endpointValue ? endpointValue->getIf<bool>() : nullptr;
345 if (!road || !fromEndpoint || !readId(request.payload, "id", edgeId) ||
346 !readId(request.payload, "nodeId", nodeId))
347 return commandError<editing::CommandPlan>(
348 "editor.road.command.edge-reconnect",
349 "Road edge reconnect requires id, fromEndpoint, and nodeId");
350 return plan(road->makeReconnectEdgeEndpoint(edgeId, *fromEndpoint, nodeId));
351 });
352 if (!registered.ok()) return registered;
353
354 registered = add("road.edge.reconnect-endpoint-and-connect.v1", "Reconnect and connect road edge endpoint",
356 auto* road = target(value);
357 std::uint32_t edgeId = 0, nodeId = 0;
358 const auto* endpointValue = field(request.payload, "fromEndpoint");
359 const auto* fromEndpoint = endpointValue ? endpointValue->getIf<bool>() : nullptr;
360 if (!road || !fromEndpoint || !readId(request.payload, "id", edgeId) ||
361 !readId(request.payload, "nodeId", nodeId))
362 return commandError<editing::CommandPlan>(
363 "editor.road.command.edge-reconnect-connect",
364 "Road edge reconnect-and-connect requires id, fromEndpoint, and nodeId");
365 return plan(road->makeReconnectEdgeEndpointAndConnect(edgeId, *fromEndpoint, nodeId));
366 });
367 if (!registered.ok()) return registered;
368
369 registered =
370 add("road.edge.snap-endpoint.v1", "Snap road edge endpoint to nearest node",
372 auto* road = target(value);
373 std::uint32_t edgeId = 0;
374 float maxDistance = 0.f;
375 bool connectTurns = true;
376 const auto* endpointValue = field(request.payload, "fromEndpoint");
377 const auto* fromEndpoint = endpointValue ? endpointValue->getIf<bool>() : nullptr;
378 const auto* connectValue = field(request.payload, "connectTurns");
379 const auto* connect = connectValue ? connectValue->getIf<bool>() : nullptr;
380 if (!road || !fromEndpoint || (connectValue && !connect) || !readId(request.payload, "id", edgeId) ||
381 !readNumber(request.payload, "maxDistance", maxDistance))
382 return commandError<editing::CommandPlan>(
383 "editor.road.command.edge-snap",
384 "Road edge snap requires id, fromEndpoint, and a finite maxDistance");
385 if (connect) connectTurns = *connect;
386 return plan(road->makeSnapEdgeEndpoint(edgeId, *fromEndpoint, maxDistance, connectTurns));
387 });
388 if (!registered.ok()) return registered;
389
390 registered = add("road.edge.detach-endpoint.v1", "Detach road edge endpoint",
392 auto* road = target(value);
393 std::uint32_t edgeId = 0;
394 const auto* endpointValue = field(request.payload, "fromEndpoint");
395 const auto* fromEndpoint = endpointValue ? endpointValue->getIf<bool>() : nullptr;
396 if (!road || !fromEndpoint || !readId(request.payload, "id", edgeId))
397 return commandError<editing::CommandPlan>("editor.road.command.edge-detach",
398 "Road edge detach requires id and fromEndpoint");
399 return plan(road->makeDetachEdgeEndpoint(edgeId, *fromEndpoint));
400 });
401 if (!registered.ok()) return registered;
402
403 registered = add("road.edge.remove.v1", "Remove road edge",
405 auto* road = target(value);
406 std::uint32_t id = 0;
407 if (!road || !readId(request.payload, "id", id))
408 return commandError<editing::CommandPlan>("editor.road.command.edge-remove",
409 "Road edge remove requires id");
410 return plan(road->makeRemoveEdge(id));
411 });
412 if (!registered.ok()) return registered;
413
414 auto laneLinkCommand = [target, plan](bool addLink, editing::IEditableTarget& value,
416 auto* road = target(value);
418 if (!road || !decodeLink(request.payload, link))
419 return commandError<editing::CommandPlan>("editor.road.command.lane-link",
420 "Road lane-link command requires a complete directed link");
421 return plan(addLink ? road->makeAddLaneLink(link) : road->makeRemoveLaneLink(link));
422 };
423 registered = add("road.lane-link.add.v1", "Add road lane link",
425 return laneLinkCommand(true, value, request);
426 });
427 if (!registered.ok()) return registered;
428 registered = add("road.lane-link.remove.v1", "Remove road lane link",
430 return laneLinkCommand(false, value, request);
431 });
432 if (!registered.ok()) return registered;
433 auto laneLinkBlockCommand = [target, plan](bool block, editing::IEditableTarget& value,
435 auto* road = target(value);
437 if (!road || !decodeLink(request.payload, link))
438 return commandError<editing::CommandPlan>("editor.road.command.lane-link-block",
439 "Road lane-link block command requires a complete directed link");
440 return plan(block ? road->makeBlockLaneLink(link) : road->makeUnblockLaneLink(link));
441 };
442 registered = add("road.lane-link.block.v1", "Block road lane link",
443 [laneLinkBlockCommand](editing::IEditableTarget& value, const editing::CommandRequest& request) {
444 return laneLinkBlockCommand(true, value, request);
445 });
446 if (!registered.ok()) return registered;
447 return add("road.lane-link.unblock.v1", "Unblock road lane link",
448 [laneLinkBlockCommand](editing::IEditableTarget& value, const editing::CommandRequest& request) {
449 return laneLinkBlockCommand(false, value, request);
450 });
451}
452
453} // namespace eve::procgen_editing
double z
double y
double x
LogicalId target
ActionParameterOperation operation
std::string descriptor
std::string nodeId
std::string name
Definition ECS.cpp:154
const GltfImportRequest & request
PhysicsJointLink link
std::array< float, 3 > position
std::string to
std::string from
std::string id
Poco::JSON::Object::Ptr result
double radius
Definition MeleeHit.cpp:42
std::shared_ptr< const std::vector< glm::vec2 > > points
const eve::script::BindingContractRegistry & registry
Object::Ptr value
int detail
std::string sourceNodeId
Definition UISystem.cpp:153
uint32_t index
V forward
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Structured status and zero or more diagnostics for an operation.
Definition Status.h:70
IEditableTarget public API.
Lower-layer registration boundary implemented by an editor or runtime editing host.
std::map< std::string, Value > Object
Transaction target that edits stable RoadNetwork node/edge ids.
std::function< Result< CommandPlan >(IEditableTarget &, const CommandRequest &)> EditingCommandPlanner
Side-effect-free planner invoked with the resolved target.
Result< T > applied(T value, std::vector< Diagnostic > diagnostics={})
Construct an Applied result with an owning payload and optional diagnostics.
StrongId< CommandIdTag > CommandId
Definition EditingIds.h:58
RoadJunctionControl
Authoritative traffic-control policy applied to every approach of one junction node.
Definition RoadTypes.h:232
bool readSmallInt(const EditorValue &value, const char *name, int minimum, int maximum, int &output)
editing::Result< void > registerRoadNetworkEditingCommands(editing::IEditingCommandRegistry &registry)
Register stable road topology commands with an existing editing host.
CommandPlan public API.
CommandRequest public API.
Domain-owned metadata for one planned editing command.
Owning custom junction asset snapshot; all geometry/ports share one node-local transform.
Definition RoadTypes.h:266
One authored control point on a road centerline (world space, Y-up).
Definition RoadTypes.h:19
One legal lane-to-lane connection inside a junction.
Definition RoadTypes.h:315
Owning cross-section, width policy and structural authoring for one road edge.
Definition RoadTypes.h:182