载入中...
搜索中...
未找到
PlacementPatterns.cpp
浏览该文件的文档.
2
3#include <utility>
4
6#include "building/Ghost.h"
10#include "common/Diagnostic.h"
11#include "grid/GridProjection.h"
12
13#include <algorithm>
14#include <cmath>
15#include <limits>
16#include <unordered_set>
17
18namespace eve::building {
19
20using namespace placement_detail;
21
23 PlacementWorld *world, const std::string &buildingId, const std::vector<CornerAddress> &vertices) {
24 if (!world || vertices.size() < 2) {
26 eve::DiagnosticCode::InvalidArgument, "edge path preview requires a world and at least two vertices",
27 buildingId, {}, "building.edge-path"));
28 }
29 const BuildingDefinition *definition = BuildingRegistry::find(buildingId);
30 if (!definition || definition->placementKind != "edge") {
32 eve::DiagnosticCode::InvalidArgument, "edge path preview requires an edge building definition", buildingId,
33 {}, "building.edge-path"));
34 }
35 EdgePathPreview preview;
36 preview.buildingId = buildingId;
37 preview.level = world->getActiveLevel();
38 std::unordered_set<std::string> uniqueEdges;
39 for (size_t i = 1; i < vertices.size(); ++i) {
40 const CornerAddress start = vertices[i - 1];
41 const CornerAddress end = vertices[i];
42 const bool horizontal = start.y == end.y;
43 const bool vertical = start.x == end.x;
44 if (horizontal == vertical) {
46 eve::DiagnosticCode::InvalidArgument, "each edge path segment must be non-empty and axis-aligned",
47 std::to_string(i - 1), {}, "building.edge-path"));
48 }
49 const int length = horizontal ? std::abs(end.x - start.x) : std::abs(end.y - start.y);
50 for (int step = 0; step < length; ++step) {
51 EdgeAddress edge{horizontal ? std::min(start.x, end.x) + step : start.x,
52 vertical ? std::min(start.y, end.y) + step : start.y,
54 const std::string key =
55 std::to_string(edge.x) + ":" + std::to_string(edge.y) + ":" + (horizontal ? "h" : "v");
56 if (!uniqueEdges.insert(key).second) {
58 eve::DiagnosticCode::Conflict, "edge path contains the same canonical edge more than once",
59 "edge_path_duplicate", {}, "building.edge-path"));
60 }
61 const char *direction = horizontal ? "north" : "west";
62 std::string reason;
63 if (!canPlaceEdge(world, buildingId, edge.x, edge.y, direction, 0, &reason)) {
65 eve::DiagnosticCode::Conflict, "edge path preflight rejected without mutation", reason, {},
66 "building.edge-path"));
67 }
68 preview.edges.push_back(edge);
69 }
70 }
71 return eve::Result<EdgePathPreview>::success(std::move(preview));
72}
73
75 const std::vector<EdgeCurvePoint> &controlPoints, int subdivisions) {
76 constexpr int kMaxSubdivisions = 4096;
77 constexpr size_t kMaxRasterEdges = 65536;
78 if (controlPoints.size() != 4 || subdivisions < 2 || subdivisions > kMaxSubdivisions) {
80 eve::DiagnosticCode::InvalidArgument, "cubic edge curve requires four controls and 2..4096 subdivisions",
81 {}, {}, "building.edge-curve"));
82 }
83 for (const EdgeCurvePoint &point : controlPoints) {
84 if (!std::isfinite(point.x) || !std::isfinite(point.y)) {
86 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "edge curve controls must be finite", {},
87 {}, "building.edge-curve"));
88 }
89 }
90
91 auto roundedPoint = [&](int sample) -> eve::Result<CornerAddress> {
92 const double t = static_cast<double>(sample) / static_cast<double>(subdivisions);
93 const double u = 1.0 - t;
94 const double x = u * u * u * controlPoints[0].x + 3.0 * u * u * t * controlPoints[1].x +
95 3.0 * u * t * t * controlPoints[2].x + t * t * t * controlPoints[3].x;
96 const double y = u * u * u * controlPoints[0].y + 3.0 * u * u * t * controlPoints[1].y +
97 3.0 * u * t * t * controlPoints[2].y + t * t * t * controlPoints[3].y;
98 constexpr double kIntMin = static_cast<double>(std::numeric_limits<int>::min()) + 1.0;
99 constexpr double kIntMax = static_cast<double>(std::numeric_limits<int>::max()) - 1.0;
100 if (x < kIntMin || x > kIntMax || y < kIntMin || y > kIntMax) {
102 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "edge curve exceeds grid coordinate range",
103 {}, {}, "building.edge-curve"));
104 }
106 {static_cast<int>(std::lround(x)), static_cast<int>(std::lround(y))});
107 };
108
109 auto first = roundedPoint(0);
110 if (!first.ok()) return eve::Result<std::vector<CornerAddress>>::failure(first.status());
111 std::vector<CornerAddress> vertices;
112 vertices.push_back(std::move(first).takeValue());
113 for (int sample = 1; sample <= subdivisions; ++sample) {
114 auto targetResult = roundedPoint(sample);
115 if (!targetResult.ok()) return eve::Result<std::vector<CornerAddress>>::failure(targetResult.status());
116 const CornerAddress target = std::move(targetResult).takeValue();
118 while (cursor != target) {
119 const int remainingX = target.x - cursor.x;
120 const int remainingY = target.y - cursor.y;
121 if (remainingX != 0 && (remainingY == 0 || std::abs(remainingX) >= std::abs(remainingY)))
122 cursor.x += remainingX > 0 ? 1 : -1;
123 else
124 cursor.y += remainingY > 0 ? 1 : -1;
125 vertices.push_back(cursor);
126 if (vertices.size() - 1 > kMaxRasterEdges) {
128 eve::DiagnosticCode::InvalidArgument, "edge curve rasterization exceeds 65536 unit edges", {}, {},
129 "building.edge-curve"));
130 }
131 }
132 }
133 if (vertices.size() < 2) {
135 eve::DiagnosticCode::InvalidArgument, "edge curve rasterization must cover at least one unit edge", {}, {},
136 "building.edge-curve"));
137 }
138 return eve::Result<std::vector<CornerAddress>>::success(std::move(vertices));
139}
140
142 PlacementWorld *world, const std::string &buildingId, const std::vector<EdgeCurvePoint> &controlPoints,
143 int subdivisions) {
144 auto vertices = sampleEdgeCubicBezier(controlPoints, subdivisions);
145 if (!vertices.ok()) return eve::Result<EdgePathPreview>::failure(vertices.status());
146 return previewEdgePath(world, buildingId, std::move(vertices).takeValue());
147}
148
150 PlacementWorld *world, const std::string &buildingId, const std::vector<EdgeCurvePoint> &controlPoints,
151 int subdivisions) {
152 auto preview = previewEdgeCubicBezier(world, buildingId, controlPoints, subdivisions);
153 if (!preview.ok()) return eve::Result<EdgePathPlacement>::failure(preview.status());
154 return commitEdgePath(world, std::move(preview).takeValue(), &controlPoints, subdivisions);
155}
156
158 const PlacementWorld &world, const std::string &surfaceName, const std::vector<EdgeCurvePoint> &controlPoints,
159 int subdivisions) {
160 auto validated = sampleEdgeCubicBezier(controlPoints, subdivisions);
161 if (!validated.ok()) return eve::Result<EdgeCurveSurface>::failure(validated.status());
162 if (surfaceName.empty())
164 "curve surface provider name is required",
165 {}, {}, "building.edge-curve-surface"));
166 float ox = 0.f, oy = 0.f, xx = 0.f, xy = 0.f, yx = 0.f, yy = 0.f;
167 world.cellToWorldPlane(0, 0, ox, oy);
168 world.cellToWorldPlane(1, 0, xx, xy);
169 world.cellToWorldPlane(0, 1, yx, yy);
170 xx -= ox;
171 xy -= oy;
172 yx -= ox;
173 yy -= oy;
175 result.providerName = surfaceName;
176 result.samples.reserve(static_cast<size_t>(subdivisions + 1));
177 for (int index = 0; index <= subdivisions; ++index) {
178 const double t = static_cast<double>(index) / subdivisions;
179 const double u = 1.0 - t;
180 const double gx = u * u * u * controlPoints[0].x + 3.0 * u * u * t * controlPoints[1].x +
181 3.0 * u * t * t * controlPoints[2].x + t * t * t * controlPoints[3].x;
182 const double gy = u * u * u * controlPoints[0].y + 3.0 * u * u * t * controlPoints[1].y +
183 3.0 * u * t * t * controlPoints[2].y + t * t * t * controlPoints[3].y;
184 auto sampled = sampleSurface(world, surfaceName, ox + static_cast<float>(gx) * xx + static_cast<float>(gy) * yx,
185 oy + static_cast<float>(gx) * xy + static_cast<float>(gy) * yy);
186 if (!sampled.ok()) return eve::Result<EdgeCurveSurface>::failure(sampled.status());
187 const PlacementHit &hit = sampled.value();
188 if (index == 0) {
189 result.surfaceId = hit.surfaceId;
190 result.surfaceRevision = hit.surfaceRevision;
191 } else if (hit.surfaceId != result.surfaceId || hit.surfaceRevision != result.surfaceRevision) {
193 eve::DiagnosticCode::Conflict, "curve crosses a surface identity or revision boundary", hit.surfaceId,
194 {}, "building.edge-curve-surface"));
195 }
196 result.samples.push_back({hit.worldX, hit.worldY, hit.worldZ, hit.normalX, hit.normalY, hit.normalZ});
197 }
199}
200
202 PlacementWorld *world, const std::string &buildingId, const std::vector<EdgeCurvePoint> &controlPoints,
203 int subdivisions, const std::string &surfaceName) {
204 if (!world)
206 eve::Diagnostic::error(eve::DiagnosticCode::InvalidArgument, "curve surface placement requires a world", {},
207 {}, "building.edge-curve-surface"));
208 auto preview = previewEdgeCubicBezier(world, buildingId, controlPoints, subdivisions);
209 if (!preview.ok()) return eve::Result<EdgePathPlacement>::failure(preview.status());
210 auto surface = sampleEdgeCurveSurface(*world, surfaceName, controlPoints, subdivisions);
211 if (!surface.ok()) return eve::Result<EdgePathPlacement>::failure(surface.status());
212 return commitEdgePath(world, std::move(preview).takeValue(), &controlPoints, subdivisions, &surface.value());
213}
214
216 const std::string &buildingId,
217 int startVertexX, int startVertexY,
218 int endVertexX, int endVertexY) {
219 auto path = placeEdgePath(world, buildingId, {{startVertexX, startVertexY}, {endVertexX, endVertexY}});
220 if (!path.ok()) return eve::Result<EdgeLinePlacement>::failure(path.status());
222 result.instanceIds = std::move(path).takeValue().instanceIds;
224}
225
227 PlacementWorld *world, const std::string &buildingId, const std::vector<CornerAddress> &vertices) {
228 auto previewResult = previewEdgePath(world, buildingId, vertices);
229 if (!previewResult.ok()) return eve::Result<EdgePathPlacement>::failure(previewResult.status());
230 return commitEdgePath(world, std::move(previewResult).takeValue());
231}
232
233eve::Result<PlacementSystem::EdgePathPlacement> PlacementSystem::commitEdgePath(
234 PlacementWorld *world, EdgePathPreview preview, const std::vector<EdgeCurvePoint> *curveControls,
235 int curveSubdivisions, const EdgeCurveSurface *curveSurface) {
236 const BuildingDefinition *def = BuildingRegistry::find(preview.buildingId);
237 const std::vector<EdgeAddress> &edges = preview.edges;
238 const EdgeCurveGroupId curveGroupId =
239 curveControls ? EdgeCurveGroupId{world->nextEdgeCurveGroupId_++} : EdgeCurveGroupId{};
240
241 EdgePathPlacement result;
242 result.instanceIds.reserve(edges.size());
243 std::vector<BuildingChangeEvent> events;
244 events.reserve(edges.size());
245 for (const EdgeAddress &edge : edges) {
246 PlacedBuilding placed;
247 placed.instanceId = nextInstanceId(*world);
248 placed.buildingId = preview.buildingId;
249 placed.placementKind = "edge";
250 placed.edge = edge;
251 placed.edgeCurveGroupId = curveGroupId;
252 placed.originCellX = edge.x;
253 placed.originCellY = edge.y;
254 placed.level = preview.level;
255 placed.channel = def->channel;
256 placed.tags = def->tags;
257 float ax = 0.f, ay = 0.f, bx = 0.f, by = 0.f;
258 world->cellToWorldPlane(edge.x, edge.y, ax, ay);
259 world->cellToWorldPlane(edge.x + (edge.axis == EdgeAxis::Horizontal ? 1 : 0),
260 edge.y + (edge.axis == EdgeAxis::Vertical ? 1 : 0), bx, by);
261 placed.worldX = (ax + bx) * 0.5f;
262 placed.worldY = (ay + by) * 0.5f;
263 placed.rotationDeg = edge.axis == EdgeAxis::Horizontal ? 0.f : 90.f;
264
265 auto &channels = world->edgeChannels(edge.axis, placed.level);
266 auto &occupancy = channels[def->channel];
267 const size_t required = edge.axis == EdgeAxis::Horizontal ? size_t(world->width_) * size_t(world->height_ + 1)
268 : size_t(world->width_ + 1) * size_t(world->height_);
269 if (occupancy.size() != required) occupancy.assign(required, 0);
270 const size_t index = edge.axis == EdgeAxis::Horizontal
271 ? size_t(edge.y) * size_t(world->width_) + size_t(edge.x)
272 : size_t(edge.y) * size_t(world->width_ + 1) + size_t(edge.x);
273 occupancy[index] = placed.instanceId;
274 world->buildings()[placed.instanceId] = placed;
275 world->instanceOrder_.push_back(placed.instanceId);
276 result.instanceIds.push_back(placed.instanceId);
277
278 BuildingChangeEvent event;
279 event.action = "place";
280 event.worldId = world->getId();
281 event.buildingId = preview.buildingId;
282 event.instanceId = placed.instanceId;
283 event.cellX = edge.x;
284 event.cellY = edge.y;
285 event.level = placed.level;
286 event.rotationDeg = placed.rotationDeg;
287 event.worldX = placed.worldX;
288 event.worldY = placed.worldY;
289 event.channel = placed.channel;
290 events.push_back(std::move(event));
291 }
292 if (curveControls) {
293 EdgeCurveGroup group;
294 group.id = curveGroupId;
295 group.buildingId = preview.buildingId;
296 group.level = preview.level;
297 group.controlPoints = *curveControls;
298 group.subdivisions = curveSubdivisions;
299 group.instanceIds = result.instanceIds;
300 if (curveSurface) {
301 group.surfaceProviderName = curveSurface->providerName;
302 group.surfaceId = curveSurface->surfaceId;
303 group.surfaceRevision = curveSurface->surfaceRevision;
304 group.surfaceSamples = curveSurface->samples;
305 }
306 world->edgeCurveGroups_.emplace(group.id.value, std::move(group));
307 }
308 if (world->publishEvents_)
309 for (BuildingChangeEvent &event : events) emit(std::move(event));
311}
312
313eve::Result<PlacementSystem::AreaPreview> PlacementSystem::previewArea(PlacementWorld *world,
314 const std::string &buildingId,
315 const std::vector<std::pair<int, int>> &anchors,
316 float rotationDeg) {
317 if (!world || anchors.empty()) {
319 "area preview requires a world and anchors",
320 buildingId, {}, "building.area"));
321 }
322 const BuildingDefinition *def = BuildingRegistry::find(buildingId);
323 if (!def || def->placementKind == "edge") {
325 "area tools require a cell building definition",
326 buildingId, {}, "building.area"));
327 }
328 AreaPreview preview;
329 preview.buildingId = buildingId;
330 preview.level = world->getActiveLevel();
331 preview.rotationDeg = normalizeRotation(buildingId, rotationDeg);
332 std::unordered_set<uint64_t> reserved;
333 for (const auto &[x, y] : anchors) {
334 AreaCellPreview cell{x, y, false, {}};
335 if (!canPlace(world, buildingId, x, y, preview.rotationDeg, 0, &cell.reason)) {
336 ++preview.rejectedCount;
337 preview.cells.push_back(std::move(cell));
338 continue;
339 }
340 bool overlaps = false;
341 std::vector<uint64_t> footprint;
342 foreachFootprintCell(*def, x, y, preview.rotationDeg, [&](int cx, int cy) {
343 const uint64_t key = (uint64_t(uint32_t(cx)) << 32u) | uint32_t(cy);
344 footprint.push_back(key);
345 if (reserved.count(key) != 0) overlaps = true;
346 return true;
347 });
348 if (overlaps) {
349 cell.reason = "area_candidate_overlap";
350 ++preview.rejectedCount;
351 } else {
352 cell.accepted = true;
353 ++preview.acceptedCount;
354 reserved.insert(footprint.begin(), footprint.end());
355 }
356 preview.cells.push_back(std::move(cell));
357 }
358 return eve::Result<AreaPreview>::success(std::move(preview));
359}
360
362 const std::string &buildingId, int minCellX,
363 int minCellY, int maxCellX, int maxCellY,
364 float rotationDeg) {
365 if (minCellX > maxCellX) std::swap(minCellX, maxCellX);
366 if (minCellY > maxCellY) std::swap(minCellY, maxCellY);
367 std::vector<std::pair<int, int>> anchors;
368 for (int y = minCellY; y <= maxCellY; ++y)
369 for (int x = minCellX; x <= maxCellX; ++x) anchors.emplace_back(x, y);
370 return previewArea(world, buildingId, anchors, rotationDeg);
371}
372
374 const std::string &buildingId, int centerCellX,
375 int centerCellY, int radius,
376 float rotationDeg) {
377 if (radius < 0) {
379 "brush radius must be non-negative",
380 std::to_string(radius), {}, "building.area"));
381 }
382 std::vector<std::pair<int, int>> anchors;
383 for (int y = centerCellY - radius; y <= centerCellY + radius; ++y)
384 for (int x = centerCellX - radius; x <= centerCellX + radius; ++x)
385 if ((x - centerCellX) * (x - centerCellX) + (y - centerCellY) * (y - centerCellY) <= radius * radius)
386 anchors.emplace_back(x, y);
387 return previewArea(world, buildingId, anchors, rotationDeg);
388}
389
391 const std::string &buildingId,
392 int minCellX, int minCellY,
393 int maxCellX, int maxCellY,
394 float rotationDeg) {
395 if (minCellX > maxCellX) std::swap(minCellX, maxCellX);
396 if (minCellY > maxCellY) std::swap(minCellY, maxCellY);
397 std::vector<std::pair<int, int>> anchors;
398 for (int x = minCellX; x <= maxCellX; ++x) anchors.emplace_back(x, minCellY);
399 if (maxCellY != minCellY)
400 for (int x = minCellX; x <= maxCellX; ++x) anchors.emplace_back(x, maxCellY);
401 for (int y = minCellY + 1; y < maxCellY; ++y) {
402 anchors.emplace_back(minCellX, y);
403 if (maxCellX != minCellX) anchors.emplace_back(maxCellX, y);
404 }
405 std::sort(anchors.begin(), anchors.end(), [](const auto &lhs, const auto &rhs) {
406 return lhs.second == rhs.second ? lhs.first < rhs.first : lhs.second < rhs.second;
407 });
408 return previewArea(world, buildingId, anchors, rotationDeg);
409}
410
412 const std::string &buildingId,
413 const PatternRequest &request) {
414 const auto failure = [&](std::string message) {
416 eve::DiagnosticCode::InvalidArgument, std::move(message), buildingId, {}, "building.pattern"));
417 };
418 const auto corners = [&]() -> eve::Result<std::vector<CornerAddress>> {
419 std::vector<CornerAddress> result;
420 result.reserve(request.points.size());
421 for (const EdgeCurvePoint &point : request.points) {
422 if (!std::isfinite(point.x) || !std::isfinite(point.y) ||
423 std::abs(point.x - std::round(point.x)) > 0.0001f || std::abs(point.y - std::round(point.y)) > 0.0001f)
424 return eve::Result<std::vector<CornerAddress>>::failure(
426 "discrete pattern points must be finite integer grid vertices", buildingId,
427 {}, "building.pattern"));
428 result.push_back({static_cast<int>(std::lround(point.x)), static_cast<int>(std::lround(point.y))});
429 }
430 return eve::Result<std::vector<CornerAddress>>::success(std::move(result));
431 };
432
433 PatternPreview preview;
434 preview.request = request;
435 switch (request.kind) {
438 if ((request.kind == PatternKind::EdgeLine && request.points.size() != 2) ||
439 (request.kind == PatternKind::EdgePath && request.points.size() < 2))
440 return failure(request.kind == PatternKind::EdgeLine
441 ? "edge-line pattern requires exactly two points"
442 : "edge-path pattern requires at least two points");
443 if (!request.surfaceName.empty())
444 return failure("custom surfaces are currently supported only by cubic curves");
445 auto vertices = corners();
446 if (!vertices.ok()) return eve::Result<PatternPreview>::failure(vertices.status());
447 auto edge = previewEdgePath(world, buildingId, vertices.value());
448 if (!edge.ok()) return eve::Result<PatternPreview>::failure(edge.status());
449 preview.edge = std::move(edge).takeValue();
450 break;
451 }
453 if (request.points.size() != 4) return failure("cubic edge pattern requires exactly four control points");
454 auto edge = previewEdgeCubicBezier(world, buildingId, request.points, request.subdivisions);
455 if (!edge.ok()) return eve::Result<PatternPreview>::failure(edge.status());
456 if (!request.surfaceName.empty()) {
457 auto surface =
458 sampleEdgeCurveSurface(*world, request.surfaceName, request.points, request.subdivisions);
459 if (!surface.ok()) return eve::Result<PatternPreview>::failure(surface.status());
460 }
461 preview.edge = std::move(edge).takeValue();
462 break;
463 }
466 if (request.points.size() != 2) return failure("rectangle pattern requires exactly two corner points");
467 if (!request.surfaceName.empty()) return failure("area patterns do not accept a custom surface name");
468 auto vertices = corners();
469 if (!vertices.ok()) return eve::Result<PatternPreview>::failure(vertices.status());
470 const CornerAddress &a = vertices.value()[0];
471 const CornerAddress &b = vertices.value()[1];
473 ? previewRectangle(world, buildingId, a.x, a.y, b.x, b.y, request.rotationDeg)
474 : previewRectangleOutline(world, buildingId, a.x, a.y, b.x, b.y, request.rotationDeg);
475 if (!area.ok()) return eve::Result<PatternPreview>::failure(area.status());
476 preview.area = std::move(area).takeValue();
477 break;
478 }
480 if (request.points.size() != 1) return failure("circle-brush pattern requires exactly one center point");
481 if (!request.surfaceName.empty()) return failure("area patterns do not accept a custom surface name");
482 auto vertices = corners();
483 if (!vertices.ok()) return eve::Result<PatternPreview>::failure(vertices.status());
484 auto area = previewBrush(world, buildingId, vertices.value()[0].x, vertices.value()[0].y, request.radius,
485 request.rotationDeg);
486 if (!area.ok()) return eve::Result<PatternPreview>::failure(area.status());
487 preview.area = std::move(area).takeValue();
488 break;
489 }
490 }
491 return eve::Result<PatternPreview>::success(std::move(preview));
492}
493
495 const std::string &buildingId,
496 const PatternRequest &request) {
497 auto expanded = previewPattern(world, buildingId, request);
498 if (!expanded.ok()) return eve::Result<PatternPlacement>::failure(expanded.status());
500 result.preview = std::move(expanded).takeValue();
502 auto placed = commitEdgePath(world, result.preview.edge);
503 if (!placed.ok()) return eve::Result<PatternPlacement>::failure(placed.status());
504 result.instanceIds = std::move(placed).takeValue().instanceIds;
505 } else if (request.kind == PatternKind::EdgeCubicBezier) {
506 auto placed = [&]() -> eve::Result<EdgePathPlacement> {
507 if (request.surfaceName.empty())
508 return commitEdgePath(world, result.preview.edge, &result.preview.request.points, request.subdivisions);
509 auto surface = sampleEdgeCurveSurface(*world, request.surfaceName, result.preview.request.points,
510 request.subdivisions);
511 if (!surface.ok()) return eve::Result<EdgePathPlacement>::failure(surface.status());
512 const EdgeCurveSurface frames = std::move(surface).takeValue();
513 return commitEdgePath(world, result.preview.edge, &result.preview.request.points, request.subdivisions,
514 &frames);
515 }();
516 if (!placed.ok()) return eve::Result<PatternPlacement>::failure(placed.status());
517 result.instanceIds = std::move(placed).takeValue().instanceIds;
518 } else {
519 auto placed = commitArea(world, result.preview.area);
520 if (!placed.ok()) return eve::Result<PatternPlacement>::failure(placed.status());
521 result.instanceIds = std::move(placed).takeValue().instanceIds;
522 }
524}
525
526eve::Result<PlacementSystem::AreaPlacement> PlacementSystem::commitArea(PlacementWorld *world, AreaPreview preview) {
527 if (!world || preview.rejectedCount != 0 || preview.cells.empty()) {
529 eve::Diagnostic::error(eve::DiagnosticCode::Conflict, "area commit requires a fully accepted preview",
530 std::to_string(preview.rejectedCount), {}, "building.area"));
531 }
532 const BuildingDefinition *def = BuildingRegistry::find(preview.buildingId);
533 if (!def) {
535 eve::DiagnosticCode::NotFound, "area definition was not found", preview.buildingId, {}, "building.area"));
536 }
537 AreaPlacement placement;
538 placement.preview = std::move(preview);
539 std::vector<BuildingChangeEvent> events;
540 for (const AreaCellPreview &cell : placement.preview.cells) {
541 PlacedBuilding pb;
542 pb.instanceId = nextInstanceId(*world);
543 pb.buildingId = placement.preview.buildingId;
544 pb.originCellX = cell.cellX;
545 pb.originCellY = cell.cellY;
546 pb.level = placement.preview.level;
547 pb.rotationDeg = placement.preview.rotationDeg;
548 pb.channel = def->channel;
549 pb.tags = def->tags;
550 world->cellToWorldPlane(cell.cellX, cell.cellY, pb.worldX, pb.worldY);
551 PlacementQuery supportQuery;
552 supportQuery.cellX = pb.originCellX;
553 supportQuery.cellY = pb.originCellY;
554 supportQuery.level = pb.level;
555 supportQuery.rotationDeg = pb.rotationDeg;
556 pb.supportInstanceIds = collectStructuralSupports(*world, *def, supportQuery);
557 writeOccupancy(*world, *def, pb, pb.instanceId);
558 world->buildings()[pb.instanceId] = pb;
559 world->instanceOrder_.push_back(pb.instanceId);
560 placement.instanceIds.push_back(pb.instanceId);
561 BuildingChangeEvent event;
562 event.action = "place";
563 event.worldId = world->getId();
564 event.buildingId = pb.buildingId;
565 event.instanceId = pb.instanceId;
566 event.cellX = pb.originCellX;
567 event.cellY = pb.originCellY;
568 event.rotationDeg = pb.rotationDeg;
569 event.worldX = pb.worldX;
570 event.worldY = pb.worldY;
571 event.channel = pb.channel;
572 events.push_back(std::move(event));
573 }
574 if (world->publishEvents_)
575 for (BuildingChangeEvent &event : events) emit(std::move(event));
577}
578
580 const std::string &buildingId, int minCellX,
581 int minCellY, int maxCellX, int maxCellY,
582 float rotationDeg) {
583 auto preview = previewRectangle(world, buildingId, minCellX, minCellY, maxCellX, maxCellY, rotationDeg);
584 if (!preview.ok()) return eve::Result<AreaPlacement>::failure(preview.status());
585 return commitArea(world, std::move(preview).takeValue());
586}
587
589 const std::string &buildingId, int centerCellX,
590 int centerCellY, int radius,
591 float rotationDeg) {
592 auto preview = previewBrush(world, buildingId, centerCellX, centerCellY, radius, rotationDeg);
593 if (!preview.ok()) return eve::Result<AreaPlacement>::failure(preview.status());
594 return commitArea(world, std::move(preview).takeValue());
595}
596
598 const std::string &buildingId,
599 int minCellX, int minCellY,
600 int maxCellX, int maxCellY,
601 float rotationDeg) {
602 auto preview = previewRectangleOutline(world, buildingId, minCellX, minCellY, maxCellX, maxCellY, rotationDeg);
603 if (!preview.ok()) return eve::Result<AreaPlacement>::failure(preview.status());
604 return commitArea(world, std::move(preview).takeValue());
605}
606
607
608} // namespace eve::building
double y
double x
LogicalId target
Duration start
std::map< std::string, std::vector< Key >, std::less<> > channels
std::size_t cursor
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
Stable, structured diagnostics shared by engine modules.
SQObjectPtr key
Definition ECS.cpp:155
std::string message
const GltfImportRequest & request
float u
Definition Grass.cpp:233
float area
Definition Grass.cpp:62
Pure cell/world projection helpers for GridConfig.
std::int32_t first
float length
JointPoint2D axis
bool required
std::vector< Edge > edges
Poco::JSON::Object::Ptr result
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
Placement placement
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
std::string path
Definition PlayHost.cpp:111
bool hit
float t
RoadLaneDirection direction
Value::Array corners
size_t end
AbilityActivationGroup group
Cell cell
bool placed
std::array< double, 2 > vertices[3]
float step
Definition TreeMesh.cpp:451
uint32_t index
double oy
double ox
glm::vec3 point
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
T takeValue() &&
Move the value out and remove it from this Result.
Definition Result.h:370
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
static const BuildingDefinition * find(const std::string &id)
Finds .
Definition Building.cpp:557
static eve::Result< EdgeCurveSurface > sampleEdgeCurveSurface(const PlacementWorld &world, const std::string &surfaceName, const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions)
Sample one same-identity custom surface frame at every analytic curve segment.
static eve::Result< PatternPreview > previewPattern(PlacementWorld *world, const std::string &buildingId, const PatternRequest &request)
Expand any built-in placement pattern without mutating the world.
static eve::Result< AreaPlacement > placeBrush(PlacementWorld *world, const std::string &buildingId, int centerCellX, int centerCellY, int radius, float rotationDeg=0.f)
Atomically commit a circular brush; any rejection aborts all cells.
static eve::Result< EdgeLinePlacement > placeEdgeLine(PlacementWorld *world, const std::string &buildingId, int startVertexX, int startVertexY, int endVertexX, int endVertexY)
Atomically place every unit edge between two logical grid vertices.
static eve::Result< PatternPlacement > placePattern(PlacementWorld *world, const std::string &buildingId, const PatternRequest &request)
Validate and atomically commit any built-in placement pattern.
static eve::Result< std::vector< CornerAddress > > sampleEdgeCubicBezier(const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions)
Deterministically rasterize four cubic Bezier controls to adjacent grid vertices.
static eve::Result< AreaPreview > previewRectangle(PlacementWorld *world, const std::string &buildingId, int minCellX, int minCellY, int maxCellX, int maxCellY, float rotationDeg=0.f)
Preview every anchor in an inclusive rectangular area without mutation.
static eve::Result< EdgePathPreview > previewEdgeCubicBezier(PlacementWorld *world, const std::string &buildingId, const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions)
Sample and validate a cubic Bezier edge curve without mutating the world.
static eve::Result< EdgePathPreview > previewEdgePath(PlacementWorld *world, const std::string &buildingId, const std::vector< CornerAddress > &vertices)
Validate and expand a multi-segment orthogonal path without mutation.
static eve::Result< AreaPreview > previewRectangleOutline(PlacementWorld *world, const std::string &buildingId, int minCellX, int minCellY, int maxCellX, int maxCellY, float rotationDeg=0.f)
Preview only the perimeter anchors of an inclusive rectangle.
static eve::Result< EdgePathPlacement > placeEdgeCubicBezier(PlacementWorld *world, const std::string &buildingId, const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions)
Atomically place the deterministic grid-edge rasterization of a cubic Bezier.
static eve::Result< EdgePathPlacement > placeEdgePath(PlacementWorld *world, const std::string &buildingId, const std::vector< CornerAddress > &vertices)
Atomically place every unit edge along a multi-segment orthogonal vertex path.
static eve::Result< AreaPlacement > placeRectangle(PlacementWorld *world, const std::string &buildingId, int minCellX, int minCellY, int maxCellX, int maxCellY, float rotationDeg=0.f)
Atomically commit a previously shaped rectangle; any rejection aborts all cells.
static const std::vector< BuildingChangeEvent > & events()
static eve::Result< EdgePathPlacement > placeEdgeCubicBezierOnSurface(PlacementWorld *world, const std::string &buildingId, const std::vector< EdgeCurvePoint > &controlPoints, int subdivisions, const std::string &surfaceName)
Atomically place a curve whose committed centerline follows a custom surface.
static bool foreachFootprintCell(const BuildingDefinition &def, int originCellX, int originCellY, float rotationDeg, const std::function< bool(int cx, int cy)> &fn)
枚举占地格子(旋转后局部 → 世界格子)。返回 false 若定义未知。
static eve::Result< PlacementHit > sampleSurface(const PlacementWorld &world, const std::string &name, float x, float y)
Sample a named placement surface.
static float normalizeRotation(const std::string &buildingId, float rotationDeg)
规范化旋转角(cardinal → 0/90/180/270;none → 0)。
static eve::Result< AreaPreview > previewBrush(PlacementWorld *world, const std::string &buildingId, int centerCellX, int centerCellY, int radius, float rotationDeg=0.f)
Preview anchors inside an inclusive Euclidean cell-radius brush.
static eve::Result< AreaPlacement > placeRectangleOutline(PlacementWorld *world, const std::string &buildingId, int minCellX, int minCellY, int maxCellX, int maxCellY, float rotationDeg=0.f)
Atomically commit only the perimeter anchors of an inclusive rectangle.
static bool canPlace(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY, float rotationDeg=0.f, int excludeInstanceId=0, std::string *reason=nullptr)
static bool canPlaceEdge(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY, const std::string &direction, int excludeInstanceId=0, std::string *reason=nullptr, int level=std::numeric_limits< int >::min())
Validate an edge-domain placement without mutating the world.
格子型建筑放置世界(脚本可直接操作)。
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
EdgeAxis
Canonical orientation of an edge in logical grid coordinates.
建筑模板(进程级注册表中的定义)。
std::string channel
占地格子尺寸。
std::vector< std::string > tags
Canonical address of one logical grid vertex used by corner objects.
Canonical address of one grid edge.
One cubic Bezier control point in logical grid-vertex space.
Strong world-local identity of one authoritative edge-curve group.
Owning same-identity surface frames sampled along an analytic edge curve.
Atomic result of placing one axis-aligned continuous edge line.
Owning validated edge addresses for one non-mutating path preview.
Atomic placement receipt shared by every built-in pattern kind.
Owning non-mutating expansion of an edge or area pattern.
Owning, renderer-neutral request for one atomic placement pattern.
One sampled point and orthonormal frame on a placement surface.