载入中...
搜索中...
未找到
PlacementWorld.cpp
浏览该文件的文档.
3#include "building/Ghost.h"
5#include "data/DataModule.h"
6#include "data/JsonDocument.h"
8#include "map/TileLayer.h"
9
10#include <Poco/JSON/Object.h>
11
12#include <algorithm>
13#include <cmath>
14#include <memory>
15
16namespace eve::building {
17
18PlacementWorld::PlacementWorld(int width, int height, float cellSize) {
19 assemblyState_ = std::make_unique<AssemblyState>();
20 assemblyState_->epoch = assembly_detail::nextEpoch();
21 width_ = width > 0 ? width : 1;
22 height_ = height > 0 ? height : 1;
23 grid_ = std::make_unique<grid::GridConfig>();
24 grid_->cellW = cellSize > 0.f ? cellSize : 1.f;
25 grid_->cellH = grid_->cellW;
26 occupancy_.assign(size_t(width_) * size_t(height_), 0);
27 terrain_.assign(size_t(width_) * size_t(height_), 0);
28 terrainOverrides_.assign(size_t(width_) * size_t(height_), 0);
30}
31
33
34float PlacementWorld::getCellSize() const { return grid_->cellW; }
35float PlacementWorld::getOriginX() const { return grid_->originX; }
36float PlacementWorld::getOriginY() const { return grid_->originY; }
37const grid::GridConfig &PlacementWorld::getGrid() const { return *grid_; }
39
40void PlacementWorld::destroy() { delete this; }
41
43 if (s > 0.f) {
44 grid_->cellW = s;
45 grid_->cellH = s;
46 }
47}
48
49void PlacementWorld::setOrigin(float x, float y) {
50 grid_->originX = x;
51 grid_->originY = y;
52}
53
54void PlacementWorld::setExtra(const std::string &key, const std::string &value) {
55 extra_[key] = value;
56}
57
58std::string PlacementWorld::getExtra(const std::string &key, const std::string &fallback) const {
59 auto it = extra_.find(key);
60 return it == extra_.end() ? fallback : it->second;
61}
62
63// ---- Grid 配置 ----
64
65void PlacementWorld::setGridLayout(const std::string &layout) {
67}
68
70 return grid::GridConfig::layoutName(grid_->layout);
71}
72
73void PlacementWorld::setGridPlane(const std::string &plane) {
74 grid_->plane = grid::GridConfig::planeFromName(plane);
75}
76
78 return grid::GridConfig::planeName(grid_->plane);
79}
80
81void PlacementWorld::setCellGap(float gapX, float gapY) {
82 grid_->cellGapX = gapX;
83 grid_->cellGapY = gapY;
84}
85
86void PlacementWorld::setHexSideLength(float s) { grid_->hexSideLength = s; }
87
88void PlacementWorld::setStagger(const std::string &axis, const std::string &index) {
89 grid_->staggerAxis = (axis == "x" || axis == "X") ? grid::StaggerAxis::X
91 grid_->staggerIndex = (index == "even" || index == "Even") ? grid::StaggerIndex::Even
93}
94
96 if (!layer) return;
97 const auto &c = *layer->config();
98 switch (c.orientation) {
100 grid_->layout = grid::GridLayout::Rectangle;
101 break;
103 grid_->layout = grid::GridLayout::Isometric;
104 break;
106 grid_->layout = grid::GridLayout::Staggered;
107 break;
109 grid_->layout = grid::GridLayout::Hexagon;
110 break;
111 }
112 grid_->cellW = c.tileW;
113 grid_->cellH = c.tileH;
114 grid_->originX = c.originX;
115 grid_->originY = c.originY;
116 grid_->staggerAxis = c.staggerAxis == map::StaggerAxis::Y ? grid::StaggerAxis::Y
118 grid_->staggerIndex = c.staggerIndex == map::StaggerIndex::Odd ? grid::StaggerIndex::Odd
120 grid_->hexSideLength = c.hexSideLength;
121}
122
123// ---- 坐标换算 ----
124
125int PlacementWorld::worldToCellX(float worldX) const {
126 int cx = 0, cy = 0;
127 grid::worldToCell(*grid_, worldX, 0.f, cx, cy, width_, height_);
128 return cx;
129}
130
131int PlacementWorld::worldToCellY(float worldY) const {
132 int cx = 0, cy = 0;
133 grid::worldToCell(*grid_, 0.f, worldY, cx, cy, width_, height_);
134 return cy;
135}
136
137float PlacementWorld::cellToWorldX(int cellX) const {
138 float px = 0.f, py = 0.f;
139 grid::cellToWorld(*grid_, cellX, 0, px, py);
140 return px;
141}
142
143float PlacementWorld::cellToWorldY(int cellY) const {
144 float px = 0.f, py = 0.f;
145 grid::cellToWorld(*grid_, 0, cellY, px, py);
146 return py;
147}
148
149void PlacementWorld::cellToWorldPlane(int cellX, int cellY, float &px, float &py) const {
150 grid::cellToWorld(*grid_, cellX, cellY, px, py);
151}
152
153void PlacementWorld::cellToWorld3D(int cellX, int cellY, float elevation, float &worldX,
154 float &worldY, float &worldZ) const {
155 float px = 0.f, py = 0.f;
156 grid::cellToWorld(*grid_, cellX, cellY, px, py);
157 if (grid_->plane == grid::GridPlane::XZ) {
158 worldX = px;
159 worldY = elevation;
160 worldZ = py;
161 } else {
162 worldX = px;
163 worldY = py;
164 worldZ = elevation;
165 }
166}
167
168float PlacementWorld::cellToWorld3DX(int cellX, int cellY, float elevation) const {
169 float wx = 0.f, wy = 0.f, wz = 0.f;
170 cellToWorld3D(cellX, cellY, elevation, wx, wy, wz);
171 return wx;
172}
173
174float PlacementWorld::cellToWorld3DY(int cellX, int cellY, float elevation) const {
175 float wx = 0.f, wy = 0.f, wz = 0.f;
176 cellToWorld3D(cellX, cellY, elevation, wx, wy, wz);
177 return wy;
178}
179
180float PlacementWorld::cellToWorld3DZ(int cellX, int cellY, float elevation) const {
181 float wx = 0.f, wy = 0.f, wz = 0.f;
182 cellToWorld3D(cellX, cellY, elevation, wx, wy, wz);
183 return wz;
184}
185
186void PlacementWorld::worldToCell3D(float worldX, float worldY, float worldZ, int &cellX,
187 int &cellY) const {
188 const float px = worldX;
189 const float py = (grid_->plane == grid::GridPlane::XZ) ? worldZ : worldY;
190 grid::worldToCell(*grid_, px, py, cellX, cellY, width_, height_);
191}
192
193int PlacementWorld::worldToCell3DX(float worldX, float worldY, float worldZ) const {
194 int cx = 0, cy = 0;
195 worldToCell3D(worldX, worldY, worldZ, cx, cy);
196 return cx;
197}
198
199int PlacementWorld::worldToCell3DY(float worldX, float worldY, float worldZ) const {
200 int cx = 0, cy = 0;
201 worldToCell3D(worldX, worldY, worldZ, cx, cy);
202 return cy;
203}
204
205// ---- 地形 ----
206
208 std::fill(terrain_.begin(), terrain_.end(), semantic);
209 std::fill(terrainOverrides_.begin(), terrainOverrides_.end(), 1);
210}
211
212void PlacementWorld::setTerrain(int cellX, int cellY, int semantic) {
213 if (!inBounds(cellX, cellY)) return;
214 const size_t idx = size_t(cellY) * size_t(width_) + size_t(cellX);
215 terrain_[idx] = semantic;
216 terrainOverrides_[idx] = 1;
217}
218
219int PlacementWorld::getTerrain(int cellX, int cellY) const {
220 if (!inBounds(cellX, cellY)) return 0;
221 const size_t idx = size_t(cellY) * size_t(width_) + size_t(cellX);
222 if (terrainOverrides_[idx]) return terrain_[idx];
223 if (terrainBound_) return terrainFromGid(cellX, cellY);
224 return terrain_[idx];
225}
226
227int PlacementWorld::terrainFromGid(int cellX, int cellY) const {
228 if (!tileLayer_) return 0;
229 const int gid = tileLayer_->getTile(cellX, cellY);
230 auto it = terrainGidMap_.find(gid);
231 return it == terrainGidMap_.end() ? 0 : it->second;
232}
233
234bool PlacementWorld::inBounds(int cellX, int cellY) const {
235 return cellX >= 0 && cellY >= 0 && cellX < width_ && cellY < height_;
236}
237
238// ---- Tilemap 绑定 ----
239
241 tileLayer_ = layer;
242 terrainBound_ = (layer != nullptr);
244 // 绑定后地形由 GID 懒解析;清掉旧手动地形,避免旧值泄漏。
245 std::fill(terrain_.begin(), terrain_.end(), 0);
246 std::fill(terrainOverrides_.begin(), terrainOverrides_.end(), 0);
247}
248
250 tileLayer_ = nullptr;
251 terrainBound_ = false;
252 std::fill(terrain_.begin(), terrain_.end(), 0);
253 std::fill(terrainOverrides_.begin(), terrainOverrides_.end(), 0);
254}
255
256void PlacementWorld::setTerrainGidMapJson(const std::string &json) {
257 auto *dm = eve::data::DataModule::create();
258 std::string err;
259 std::unique_ptr<data::JsonDocument> doc(dm->decodeJson(json, &err));
260 if (!doc || !doc->isObject()) return;
261 auto obj = doc->object();
262 if (!obj) return;
263 std::vector<std::string> names;
264 obj->getNames(names);
265 for (const auto &n : names) {
266 try {
267 const int gid = std::atoi(n.c_str());
268 const int semantic = obj->getValue<int>(n);
269 if (gid >= 0) terrainGidMap_[gid] = semantic;
270 } catch (...) {
271 }
272 }
273}
274
276 if (gid >= 0) terrainGidMap_[gid] = semantic;
277}
278
279void PlacementWorld::clearTerrainGidMap() { terrainGidMap_.clear(); }
280
282 if (std::isfinite(height) && height > 0.f) floorHeight_ = height;
283}
284
285PlacementWorld::ChannelMap &PlacementWorld::edgeChannels(EdgeAxis axis, int level) {
286 if (level == 0)
287 return axis == EdgeAxis::Horizontal ? horizontalEdgeChannels_ : verticalEdgeChannels_;
288 return axis == EdgeAxis::Horizontal ? horizontalEdgesByLevel_[level]
289 : verticalEdgesByLevel_[level];
290}
291
292const PlacementWorld::ChannelMap *PlacementWorld::findEdgeChannels(EdgeAxis axis,
293 int level) const {
294 if (level == 0)
295 return axis == EdgeAxis::Horizontal ? &horizontalEdgeChannels_ : &verticalEdgeChannels_;
296 const auto &levels =
297 axis == EdgeAxis::Horizontal ? horizontalEdgesByLevel_ : verticalEdgesByLevel_;
298 const auto found = levels.find(level);
299 return found == levels.end() ? nullptr : &found->second;
300}
301
302PlacementWorld::ChannelMap &PlacementWorld::cornerChannels(int level) {
303 return level == 0 ? cornerChannels_ : cornersByLevel_[level];
304}
305
306const PlacementWorld::ChannelMap *PlacementWorld::findCornerChannels(int level) const {
307 if (level == 0) return &cornerChannels_;
308 const auto found = cornersByLevel_.find(level);
309 return found == cornersByLevel_.end() ? nullptr : &found->second;
310}
311
312// ---- 占用查询 ----
313
314std::vector<int> &PlacementWorld::channelOccupancy(const std::string &channel) {
315 return channelOccupancy(channel, activeLevel_);
316}
317
318std::vector<int> &PlacementWorld::channelOccupancy(const std::string &channel, int level) {
319 if (level != 0) {
320 auto &occupancy = cellChannelsByLevel_[level][channel];
321 const size_t required = size_t(width_) * size_t(height_);
322 if (occupancy.size() != required) occupancy.assign(required, 0);
323 return occupancy;
324 }
325 if (channel.empty()) return occupancy_;
326 auto &ch = allChannels_[channel];
327 if (ch.size() != occupancy_.size()) ch.assign(occupancy_.size(), 0);
328 return ch;
329}
330
331int PlacementWorld::getOccupant(int cellX, int cellY) const {
332 return getOccupantInChannel(std::string{}, cellX, cellY);
333}
334
335int PlacementWorld::getOccupantInChannel(const std::string &channel, int cellX, int cellY) const {
336 return getOccupantAtLevel(channel, cellX, cellY, activeLevel_);
337}
338
339int PlacementWorld::getOccupantAtLevel(const std::string &channel, int cellX, int cellY,
340 int level) const {
341 if (!inBounds(cellX, cellY)) return 0;
342 const size_t idx = size_t(cellY) * size_t(width_) + size_t(cellX);
343 if (level != 0) {
344 const auto levelIt = cellChannelsByLevel_.find(level);
345 if (levelIt == cellChannelsByLevel_.end()) return 0;
346 const auto channelIt = levelIt->second.find(channel);
347 return channelIt == levelIt->second.end() || idx >= channelIt->second.size()
348 ? 0
349 : channelIt->second[idx];
350 }
351 if (channel.empty()) return occupancy_[idx];
352 auto it = allChannels_.find(channel);
353 if (it == allChannels_.end() || idx >= it->second.size()) return 0;
354 return it->second[idx];
355}
356
357int PlacementWorld::getAnyOccupant(int cellX, int cellY) const {
358 return getAnyOccupantAtLevel(cellX, cellY, activeLevel_);
359}
360
361int PlacementWorld::getAnyOccupantAtLevel(int cellX, int cellY, int level) const {
362 const int occ = getOccupantAtLevel(std::string{}, cellX, cellY, level);
363 if (occ != 0) return occ;
364 if (level != 0) {
365 const auto levelIt = cellChannelsByLevel_.find(level);
366 if (levelIt == cellChannelsByLevel_.end()) return 0;
367 for (const auto &kv : levelIt->second) {
368 const int value = getOccupantAtLevel(kv.first, cellX, cellY, level);
369 if (value != 0) return value;
370 }
371 return 0;
372 }
373 for (const auto &kv : allChannels_) {
374 const int v = getOccupantAtLevel(kv.first, cellX, cellY, 0);
375 if (v != 0) return v;
376 }
377 return 0;
378}
379
380bool PlacementWorld::isCellEmpty(int cellX, int cellY) const {
381 return getOccupant(cellX, cellY) == 0;
382}
383
384bool PlacementWorld::isCellEmptyInChannel(const std::string &channel, int cellX, int cellY) const {
385 return getOccupantInChannel(channel, cellX, cellY) == 0;
386}
387
388int PlacementWorld::getBuildingCount() const { return int(instanceOrder_.size()); }
389
391 return buildings_.count(instanceId) > 0;
392}
393
395 auto it = buildings_.find(instanceId);
396 return it == buildings_.end() ? std::string{} : it->second.buildingId;
397}
398
400 auto it = buildings_.find(instanceId);
401 return it == buildings_.end() ? 0 : it->second.originCellX;
402}
403
405 auto it = buildings_.find(instanceId);
406 return it == buildings_.end() ? 0 : it->second.originCellY;
407}
408
410 auto it = buildings_.find(instanceId);
411 return it == buildings_.end() ? 0.f : it->second.worldX;
412}
413
415 auto it = buildings_.find(instanceId);
416 if (it == buildings_.end()) return 0.f;
417 const PlacedBuilding &pb = it->second;
418 // 真实世界 Y:XZ 平面 = 高度;XY 平面 = 平面第二轴。
419 return grid_->plane == grid::GridPlane::XZ
420 ? pb.elevation + float(pb.level) * floorHeight_
421 : pb.worldY;
422}
423
425 auto it = buildings_.find(instanceId);
426 if (it == buildings_.end()) return 0.f;
427 const PlacedBuilding &pb = it->second;
428 return grid_->plane == grid::GridPlane::XZ
429 ? pb.worldY
430 : pb.elevation + float(pb.level) * floorHeight_;
431}
432
434 auto it = buildings_.find(instanceId);
435 return it == buildings_.end() ? 0.f : it->second.elevation;
436}
437
439 auto it = buildings_.find(instanceId);
440 return it == buildings_.end() ? 0 : it->second.level;
441}
442
444 const auto found = buildings_.find(instanceId);
445 return found == buildings_.end() ? 0 : int(found->second.supportInstanceIds.size());
446}
447
449 const auto found = buildings_.find(instanceId);
450 if (found == buildings_.end() || index < 0 ||
451 index >= int(found->second.supportInstanceIds.size()))
452 return 0;
453 return found->second.supportInstanceIds[size_t(index)];
454}
455
457 int count = 0;
458 for (const auto &[id, placed] : buildings_) {
459 (void)id;
460 if (std::find(placed.supportInstanceIds.begin(), placed.supportInstanceIds.end(),
461 instanceId) != placed.supportInstanceIds.end())
462 ++count;
463 }
464 return count;
465}
466
467int PlacementWorld::getEdgeOccupant(const std::string &channel, int cellX, int cellY,
468 const std::string &direction) const {
469 return getEdgeOccupantAtLevel(channel, cellX, cellY, direction, activeLevel_);
470}
471
472int PlacementWorld::getEdgeOccupantAtLevel(const std::string &channel, int cellX, int cellY,
473 const std::string &direction, int level) const {
474 auto canonical = PlacementSystem::canonicalEdge(cellX, cellY, direction);
475 if (!canonical.ok()) return 0;
476 const EdgeAddress edge = std::move(canonical).takeValue();
477 const bool inBounds = edge.axis == EdgeAxis::Horizontal
478 ? edge.x >= 0 && edge.x < width_ && edge.y >= 0 && edge.y <= height_
479 : edge.x >= 0 && edge.x <= width_ && edge.y >= 0 && edge.y < height_;
480 if (!inBounds) return 0;
481 const ChannelMap *channels = nullptr;
482 if (level == 0) {
483 channels = edge.axis == EdgeAxis::Horizontal ? &horizontalEdgeChannels_
484 : &verticalEdgeChannels_;
485 } else {
486 const auto &levels = edge.axis == EdgeAxis::Horizontal ? horizontalEdgesByLevel_
487 : verticalEdgesByLevel_;
488 const auto levelIt = levels.find(level);
489 if (levelIt == levels.end()) return 0;
490 channels = &levelIt->second;
491 }
492 const auto it = channels->find(channel);
493 if (it == channels->end()) return 0;
494 const size_t index = edge.axis == EdgeAxis::Horizontal
495 ? size_t(edge.y) * size_t(width_) + size_t(edge.x)
496 : size_t(edge.y) * size_t(width_ + 1) + size_t(edge.x);
497 return index < it->second.size() ? it->second[index] : 0;
498}
499
500bool PlacementWorld::isEdgeEmpty(const std::string &channel, int cellX, int cellY,
501 const std::string &direction) const {
502 return getEdgeOccupant(channel, cellX, cellY, direction) == 0;
503}
504
505int PlacementWorld::getCornerOccupant(const std::string &channel, int vertexX,
506 int vertexY) const {
507 return getCornerOccupantAtLevel(channel, vertexX, vertexY, activeLevel_);
508}
509
510int PlacementWorld::getCornerOccupantAtLevel(const std::string &channel, int vertexX,
511 int vertexY, int level) const {
512 if (vertexX < 0 || vertexX > width_ || vertexY < 0 || vertexY > height_) return 0;
513 const ChannelMap *channels = findCornerChannels(level);
514 if (!channels) return 0;
515 const auto found = channels->find(channel);
516 if (found == channels->end()) return 0;
517 const size_t index = size_t(vertexY) * size_t(width_ + 1) + size_t(vertexX);
518 return index < found->second.size() ? found->second[index] : 0;
519}
520
521int PlacementWorld::getAnyCornerOccupantAtLevel(int vertexX, int vertexY, int level) const {
522 const ChannelMap *channels = findCornerChannels(level);
523 if (!channels) return 0;
524 for (const auto &[channel, occupancy] : *channels) {
525 (void)occupancy;
526 const int occupant = getCornerOccupantAtLevel(channel, vertexX, vertexY, level);
527 if (occupant != 0) return occupant;
528 }
529 return 0;
530}
531
532bool PlacementWorld::isCornerEmpty(const std::string &channel, int vertexX, int vertexY) const {
533 return getCornerOccupant(channel, vertexX, vertexY) == 0;
534}
535
536int PlacementWorld::getFreeOccupant(const std::string &channel, float worldX,
537 float worldY) const {
538 return getFreeOccupantAtLevel(channel, worldX, worldY, activeLevel_);
539}
540
541int PlacementWorld::getFreeOccupantAtLevel(const std::string &channel, float worldX,
542 float worldY, int level) const {
543 for (int instanceId : instanceOrder_) {
544 const auto found = buildings_.find(instanceId);
545 if (found == buildings_.end()) continue;
546 const PlacedBuilding &placed = found->second;
547 if (placed.placementKind != "free" || placed.level != level ||
548 placed.channel != channel)
549 continue;
550 if (PlacementSystem::containsFreePoint(placed, worldX, worldY))
551 return instanceId;
552 }
553 return 0;
554}
555
557 return static_cast<int>(PlacementSystem::edgeConnectionMask(*this, instanceId));
558}
559
563
565 auto it = buildings_.find(instanceId);
566 return it == buildings_.end() ? std::string{} : it->second.surfaceId;
567}
568
570 auto it = buildings_.find(instanceId);
571 return it == buildings_.end() ? 0 : static_cast<int64_t>(it->second.surfaceRevision);
572}
573
575 auto it = buildings_.find(instanceId);
576 return it == buildings_.end() ? 0.f : it->second.surfaceNormalX;
577}
578
580 auto it = buildings_.find(instanceId);
581 return it == buildings_.end() ? 1.f : it->second.surfaceNormalY;
582}
583
585 auto it = buildings_.find(instanceId);
586 return it == buildings_.end() ? 0.f : it->second.surfaceNormalZ;
587}
588
590 auto it = buildings_.find(instanceId);
591 return it == buildings_.end() ? std::string{} : it->second.channel;
592}
593
595 auto it = buildings_.find(instanceId);
596 return it == buildings_.end() ? 0.f : it->second.rotationDeg;
597}
598
599std::string PlacementWorld::getBuildingProp(int instanceId, const std::string &key,
600 const std::string &fallback) const {
601 auto it = buildings_.find(instanceId);
602 return it == buildings_.end() ? fallback : it->second.getProp(key, fallback);
603}
604
605void PlacementWorld::setBuildingProp(int instanceId, const std::string &key,
606 const std::string &value) {
607 auto it = buildings_.find(instanceId);
608 if (it == buildings_.end()) return;
609 it->second.setProp(key, value);
610}
611
612bool PlacementWorld::buildingHasTag(int instanceId, const std::string &tag) const {
613 auto it = buildings_.find(instanceId);
614 return it == buildings_.end() ? false : it->second.hasTag(tag);
615}
616
618 if (index < 0 || index >= int(instanceOrder_.size())) return 0;
619 return instanceOrder_[size_t(index)];
620}
621
622// ---- 便捷操作 ----
623
624bool PlacementWorld::canPlace(const std::string &buildingId, int cellX, int cellY,
625 float rotationDeg) {
626 return PlacementSystem::canPlace(this, buildingId, cellX, cellY, rotationDeg, 0, nullptr);
627}
628
629std::string PlacementWorld::canPlaceReason(const std::string &buildingId, int cellX, int cellY,
630 float rotationDeg) {
631 std::string reason;
632 PlacementSystem::canPlace(this, buildingId, cellX, cellY, rotationDeg, 0, &reason);
633 return reason;
634}
635
636int PlacementWorld::placeAt(const std::string &buildingId, int cellX, int cellY,
637 float rotationDeg) {
638 return PlacementSystem::placeAt(this, buildingId, cellX, cellY, rotationDeg);
639}
640
641int PlacementWorld::placeAtWorld(const std::string &buildingId, float worldX, float worldY,
642 float rotationDeg) {
643 return PlacementSystem::placeAtWorld(this, buildingId, worldX, worldY, rotationDeg);
644}
645
646int PlacementWorld::placeAtWorld3D(const std::string &buildingId, float worldX, float worldY,
647 float worldZ, float rotationDeg) {
648 return PlacementSystem::placeAtWorld3D(this, buildingId, worldX, worldY, worldZ, rotationDeg);
649}
650
652 return PlacementSystem::placeGhost(this, ghost);
653}
654
656 const auto found = edgeCurveGroups_.find(id.value);
657 if (!id || found == edgeCurveGroups_.end()) {
659 eve::DiagnosticCode::NotFound, "edge curve group was not found",
660 std::to_string(id.value), {}, "building.edge-curve-group"));
661 }
663}
664
666 const auto placed = buildings_.find(instanceId);
667 if (placed == buildings_.end() || !placed->second.edgeCurveGroupId) {
669 eve::DiagnosticCode::NotFound, "edge instance is not linked to a curve group",
670 std::to_string(instanceId), {}, "building.edge-curve-group"));
671 }
672 return edgeCurveGroup(placed->second.edgeCurveGroupId);
673}
674
675std::vector<EdgeCurveGroupId> PlacementWorld::edgeCurveGroupIds() const {
676 std::vector<EdgeCurveGroupId> ids;
677 ids.reserve(edgeCurveGroups_.size());
678 for (const auto &[id, group] : edgeCurveGroups_) ids.push_back(group.id);
679 std::sort(ids.begin(), ids.end(), [](EdgeCurveGroupId lhs, EdgeCurveGroupId rhs) {
680 return lhs.value < rhs.value;
681 });
682 return ids;
683}
684
685int PlacementWorld::placeEdge(const std::string &buildingId, int cellX, int cellY,
686 const std::string &direction) {
687 return PlacementSystem::placeEdge(this, buildingId, cellX, cellY, direction);
688}
689
690bool PlacementWorld::canPlaceEdge(const std::string &buildingId, int cellX, int cellY,
691 const std::string &direction) {
692 return PlacementSystem::canPlaceEdge(this, buildingId, cellX, cellY, direction, 0, nullptr);
693}
694
695std::string PlacementWorld::canPlaceEdgeReason(const std::string &buildingId, int cellX, int cellY,
696 const std::string &direction) {
697 std::string reason;
698 PlacementSystem::canPlaceEdge(this, buildingId, cellX, cellY, direction, 0, &reason);
699 return reason;
700}
701
702int PlacementWorld::placeCorner(const std::string &buildingId, int vertexX, int vertexY) {
703 return PlacementSystem::placeCorner(this, buildingId, vertexX, vertexY);
704}
705
706bool PlacementWorld::canPlaceCorner(const std::string &buildingId, int vertexX, int vertexY) {
707 return PlacementSystem::canPlaceCorner(this, buildingId, vertexX, vertexY);
708}
709
710std::string PlacementWorld::canPlaceCornerReason(const std::string &buildingId, int vertexX,
711 int vertexY) {
712 std::string reason;
713 PlacementSystem::canPlaceCorner(this, buildingId, vertexX, vertexY, 0, &reason);
714 return reason;
715}
716
717
718int PlacementWorld::placeFree(const std::string &buildingId, float worldX, float worldY,
719 float elevation, float rotationDeg) {
720 return PlacementSystem::placeFree(this, buildingId, worldX, worldY, elevation, rotationDeg);
721}
722
723bool PlacementWorld::canPlaceFree(const std::string &buildingId, float worldX, float worldY) {
724 return PlacementSystem::canPlaceFree(this, buildingId, worldX, worldY);
725}
726
727std::string PlacementWorld::canPlaceFreeReason(const std::string &buildingId, float worldX,
728 float worldY) {
729 std::string reason;
730 PlacementSystem::canPlaceFree(this, buildingId, worldX, worldY, 0, &reason);
731 return reason;
732}
733
737
738bool PlacementWorld::moveBuilding(int instanceId, int cellX, int cellY, float rotationDeg) {
739 return PlacementSystem::moveBuilding(this, instanceId, cellX, cellY, rotationDeg);
740}
741
743
744std::unique_ptr<PlacementWorld> PlacementWorld::cloneState() const {
745 auto copy = std::make_unique<PlacementWorld>(width_, height_, getCellSize());
746 copy->id_ = id_;
747 *copy->assemblyState_ = *assemblyState_;
748 copy->instanceCounter_ = instanceCounter_;
749 *copy->grid_ = *grid_;
750 copy->snapMode_ = snapMode_;
751 copy->validateRule_ = validateRule_;
752 copy->floorHeight_ = floorHeight_;
753 copy->activeLevel_ = activeLevel_;
754 copy->occupancy_ = occupancy_;
755 copy->terrain_ = terrain_;
756 copy->terrainOverrides_ = terrainOverrides_;
757 copy->allChannels_ = allChannels_;
758 copy->horizontalEdgeChannels_ = horizontalEdgeChannels_;
759 copy->verticalEdgeChannels_ = verticalEdgeChannels_;
760 copy->cornerChannels_ = cornerChannels_;
761 copy->cellChannelsByLevel_ = cellChannelsByLevel_;
762 copy->horizontalEdgesByLevel_ = horizontalEdgesByLevel_;
763 copy->verticalEdgesByLevel_ = verticalEdgesByLevel_;
764 copy->cornersByLevel_ = cornersByLevel_;
765 copy->buildings_ = buildings_;
766 copy->edgeCurveGroups_ = edgeCurveGroups_;
767 copy->nextEdgeCurveGroupId_ = nextEdgeCurveGroupId_;
768 copy->publishEvents_ = false;
769 copy->extra_ = extra_;
770 copy->instanceOrder_ = instanceOrder_;
771 copy->tileLayer_ = tileLayer_;
772 copy->terrainBound_ = terrainBound_;
773 copy->terrainGidMap_ = terrainGidMap_;
774 return copy;
775}
776
778 if (!hasBuilding(instanceId)) return "not_found";
779 return getBuildingDependentCount(instanceId) > 0 ? "support_in_use" : std::string{};
780}
781
785
786void PlacementWorld::swapState(PlacementWorld& candidate) noexcept {
787 using std::swap;
788 swap(id_, candidate.id_);
789 swap(assemblyState_, candidate.assemblyState_);
790 swap(instanceCounter_, candidate.instanceCounter_);
791 swap(width_, candidate.width_);
792 swap(height_, candidate.height_);
793 swap(grid_, candidate.grid_);
794 swap(snapMode_, candidate.snapMode_);
795 swap(validateRule_, candidate.validateRule_);
796 swap(floorHeight_, candidate.floorHeight_);
797 swap(activeLevel_, candidate.activeLevel_);
798 swap(occupancy_, candidate.occupancy_);
799 swap(terrain_, candidate.terrain_);
800 swap(terrainOverrides_, candidate.terrainOverrides_);
801 swap(allChannels_, candidate.allChannels_);
802 swap(horizontalEdgeChannels_, candidate.horizontalEdgeChannels_);
803 swap(verticalEdgeChannels_, candidate.verticalEdgeChannels_);
804 swap(cornerChannels_, candidate.cornerChannels_);
805 swap(cellChannelsByLevel_, candidate.cellChannelsByLevel_);
806 swap(horizontalEdgesByLevel_, candidate.horizontalEdgesByLevel_);
807 swap(verticalEdgesByLevel_, candidate.verticalEdgesByLevel_);
808 swap(cornersByLevel_, candidate.cornersByLevel_);
809 swap(buildings_, candidate.buildings_);
810 swap(edgeCurveGroups_, candidate.edgeCurveGroups_);
811 swap(nextEdgeCurveGroupId_, candidate.nextEdgeCurveGroupId_);
812 swap(extra_, candidate.extra_);
813 swap(instanceOrder_, candidate.instanceOrder_);
814 swap(tileLayer_, candidate.tileLayer_);
815 swap(terrainBound_, candidate.terrainBound_);
816 swap(terrainGidMap_, candidate.terrainGidMap_);
817}
818
819} // namespace eve::building
double y
double x
std::map< std::string, std::vector< Key >, std::less<> > channels
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
SQObjectPtr key
Definition ECS.cpp:155
int instanceId
std::string layout
glm::vec3 n
Definition Grass.cpp:63
Pure cell/world projection helpers for GridConfig.
float v
std::int32_t c
float elevation
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
JointPoint2D axis
bool required
int level
const std::string * tag
TileLayer * layer
int idx
Object::Ptr value
RoadLaneDirection direction
uint32_t semantic
bool found
int count
AbilityActivationGroup group
bool placed
uint32_t index
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
EVENGINE_API_WORLD public API.
Definition Ghost.h:16
static int placeAt(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY, float rotationDeg=0.f)
成功返回 instanceId;失败返回 0。
static int placeFree(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY, float elevation=0.f, float rotationDeg=0.f)
Compatibility-only id projection; prefer placeFreeResult in C++.
static int placeCorner(PlacementWorld *world, const std::string &buildingId, int vertexX, int vertexY)
Compatibility-only id projection; prefer placeCornerResult in C++.
static bool containsFreePoint(const PlacedBuilding &placed, float worldX, float worldY)
Test a world-plane point against a committed circular or oriented-box footprint.
static int placeAtWorld3D(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY, float worldZ, float rotationDeg=0.f)
static int removeBuildingCascade(PlacementWorld *world, int instanceId)
Compatibility-only count projection; prefer removeBuildingCascadeResult in C++.
static void clearBuildings(PlacementWorld *world)
static std::string edgeVariant(const PlacementWorld &world, int instanceId)
Stable visual topology class: isolated/end/straight/corner/tee/cross.
static void ensureBuiltins()
确保内置规则已注册(模块首次使用时自动调用)。
static bool canPlaceFree(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY, int excludeInstanceId=0, std::string *reason=nullptr, int level=std::numeric_limits< int >::min(), float rotationDeg=0.f)
Validate an exact free-domain world-plane anchor and circular or oriented-box footprint.
static bool removeBuilding(PlacementWorld *world, int instanceId)
static bool canPlaceCorner(PlacementWorld *world, const std::string &buildingId, int vertexX, int vertexY, int excludeInstanceId=0, std::string *reason=nullptr, int level=std::numeric_limits< int >::min())
Validate a corner-domain placement at one canonical grid vertex.
static bool moveBuilding(PlacementWorld *world, int instanceId, int cellX, int cellY, float rotationDeg=-1.f)
Compatibility projection of moveBuildingResult; prefer the Result API in C++.
static int placeAtWorld(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY, float rotationDeg=0.f)
先吸附再放置;free/自定义 snap 会保留吸附后的世界坐标。
static uint8_t edgeConnectionMask(const PlacementWorld &world, int instanceId)
Compute compatible neighbouring edge topology.
static int placeGhost(PlacementWorld *world, Ghost *ghost)
static int placeEdge(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY, const std::string &direction)
Place an edge-domain definition, returning the new instance id or zero.
static eve::Result< EdgeAddress > canonicalEdge(int cellX, int cellY, const std::string &direction)
Normalize a cell-relative cardinal direction to a unique edge address.
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.
格子型建筑放置世界(脚本可直接操作)。
float getBuildingSurfaceNormalZ(int instanceId) const
Z component of the unit surface normal captured at placement time.
bool isEdgeEmpty(const std::string &channel, int cellX, int cellY, const std::string &direction) const
Return whether an edge is unoccupied in the requested channel.
int getBuildingSupportCount(int instanceId) const
Number of direct structural supports used by an instance.
std::string canPlaceReason(const std::string &buildingId, int cellX, int cellY, float rotationDeg=0.f)
Can place reason.
std::string canPlaceEdgeReason(const std::string &buildingId, int cellX, int cellY, const std::string &direction)
Stable rejection token for edge placement.
PlacementWorld(int width, int height, float cellSize=32.f)
创建 width×height 的格子世界,cellSize 为像素/格。
bool inBounds(int cellX, int cellY) const
In bounds.
int worldToCellX(float worldX) const
世界像素 ↔ 格子坐标。
int placeFree(const std::string &buildingId, float worldX, float worldY, float elevation=0.f, float rotationDeg=0.f)
Compatibility-only script projection of exact free placement.
float getOriginX() const
Returns the origin x.
void destroy()
释放资源并使其失效。
int getTerrain(int cellX, int cellY) const
Returns the terrain.
std::string getBuildingProp(int instanceId, const std::string &key, const std::string &fallback={}) const
Returns the building prop.
void setCellSize(float s)
Sets the cell size.
bool canPlaceEdge(const std::string &buildingId, int cellX, int cellY, const std::string &direction)
Validate edge placement without mutation.
float getBuildingWorldY(int instanceId) const
Returns the building world y.
int placeCorner(const std::string &buildingId, int vertexX, int vertexY)
Compatibility-only script projection of canonical corner placement.
void clearBuildings()
Clears buildings.
int getBuildingCellY(int instanceId) const
Returns the building cell y.
float getBuildingSurfaceNormalX(int instanceId) const
X component of the unit surface normal captured at placement time.
float getOriginY() const
Returns the origin y.
int getEdgeConnectionMask(int instanceId) const
Six-bit local connection mask for an edge object's compatible neighbours.
int getFreeOccupantAtLevel(const std::string &channel, float worldX, float worldY, int level) const
Query a free-domain footprint at an explicit floor.
std::unordered_map< std::string, std::vector< int > > ChannelMap
void setGridFromLayer(map::TileLayer *layer)
Sets the grid from layer.
std::unique_ptr< PlacementWorld > cloneState() const
Clone complete logical state into an independently owned world candidate.
void setHexSideLength(float s)
Sets the hex side length.
float getBuildingWorldX(int instanceId) const
Returns the building world x.
std::string canPlaceFreeReason(const std::string &buildingId, float worldX, float worldY)
Can place free reason.
bool moveBuilding(int instanceId, int cellX, int cellY, float rotationDeg=-1.f)
Moves building.
int getOccupantAtLevel(const std::string &channel, int cellX, int cellY, int level) const
Query one channel at an explicit discrete floor.
~PlacementWorld()
Placement world.
std::string getGridLayoutName() const
Returns the grid layout name.
int getBuildingLevel(int instanceId) const
Return an instance's authoritative discrete floor.
float getBuildingElevation(int instanceId) const
Returns the building elevation.
int getOccupant(int cellX, int cellY) const
占用查询:格子上建筑实例 / 是否为空。
bool isCellEmptyInChannel(const std::string &channel, int cellX, int cellY) const
已放置建筑实例查询。
int removeBuildingCascade(int instanceId)
Compatibility-only script projection of the canonical Result removal API.
bool hasBuilding(int instanceId) const
True when building.
int getBuildingSupportAt(int instanceId, int index) const
Direct structural support id by stable footprint order, or zero.
void setOrigin(float x, float y)
Sets the origin.
const grid::GridConfig & getGrid() const
Returns the grid.
int getBuildingInstanceAt(int index) const
按插入顺序取实例 id。
int getBuildingCellX(int instanceId) const
Returns the building cell x.
float cellToWorld3DX(int cellX, int cellY, float elevation) const
Cell to world 3 dx.
bool buildingHasTag(int instanceId, const std::string &tag) const
Building has tag.
const std::vector< int > & occupancy() const
Occupancy.
int placeAt(const std::string &buildingId, int cellX, int cellY, float rotationDeg=0.f)
Place at.
int placeEdge(const std::string &buildingId, int cellX, int cellY, const std::string &direction)
Place an edge definition on a north/east/south/west edge; returns instance id or 0.
void setGridLayout(const std::string &layout)
Sets the grid layout.
void setFloorHeight(float height)
Set positive height between discrete floors. Invalid values are ignored.
std::vector< EdgeCurveGroupId > edgeCurveGroupIds() const
Return an owning ascending list of all curve-group identities.
void setExtra(const std::string &key, const std::string &value)
世界级额外属性(键值)。
int getAnyCornerOccupantAtLevel(int vertexX, int vertexY, int level) const
Query the first corner occupant across channels at an explicit floor.
void setCellGap(float gapX, float gapY)
Sets the cell gap.
int getBuildingCount() const
Returns the building count.
bool isCellEmpty(int cellX, int cellY) const
True when cell empty.
void setTerrain(int cellX, int cellY, int semantic)
Sets the terrain.
void setTerrainGid(int gid, int semantic)
Sets the terrain gid.
bool removeBuilding(int instanceId)
Removes building.
float getBuildingRotation(int instanceId) const
Returns the building rotation.
void clearTileLayer()
Clears tile layer.
int64_t getBuildingSurfaceRevision(int instanceId) const
Surface provider revision captured when this instance was placed.
int worldToCell3DX(float worldX, float worldY, float worldZ) const
World to cell 3 dx.
int getEdgeOccupant(const std::string &channel, int cellX, int cellY, const std::string &direction) const
Return the occupant of a canonicalized north/east/south/west cell edge.
bool canPlaceFree(const std::string &buildingId, float worldX, float worldY)
Can place free.
float cellToWorldX(int cellX) const
Cell to world x.
int placeAtWorld3D(const std::string &buildingId, float worldX, float worldY, float worldZ, float rotationDeg=0.f)
Place at world 3 d.
int getEdgeOccupantAtLevel(const std::string &channel, int cellX, int cellY, const std::string &direction, int level) const
Query a canonical edge at an explicit discrete floor.
std::string getGridPlaneName() const
Returns the grid plane name.
bool canPlace(const std::string &buildingId, int cellX, int cellY, float rotationDeg=0.f)
便捷操作(转发 PlacementSystem):放置 / 移除 / 移动。
void swapState(PlacementWorld &candidate) noexcept
Atomically exchange complete logical state with a prepared candidate.
float cellToWorldY(int cellY) const
Cell to world y.
std::string getEdgeVariant(int instanceId) const
Stable topology variant name for an edge instance.
int placeGhost(Ghost *ghost)
Place ghost.
int worldToCellY(float worldY) const
World to cell y.
void cellToWorldPlane(int cellX, int cellY, float &px, float &py) const
Cell to world plane.
eve::Result< EdgeCurveGroup > edgeCurveGroupForInstance(int instanceId) const
Return the owning curve-group snapshot linked by an edge instance.
int getCornerOccupantAtLevel(const std::string &channel, int vertexX, int vertexY, int level) const
Query a corner-domain occupant at an explicit floor.
void setBuildingProp(int instanceId, const std::string &key, const std::string &value)
Sets the building prop.
std::string getBuildingId(int instanceId) const
Returns the building id.
void setGridPlane(const std::string &plane)
Sets the grid plane.
eve::Result< EdgeCurveGroup > edgeCurveGroup(EdgeCurveGroupId id) const
Return an owning curve-group snapshot.
void bindTileLayer(map::TileLayer *layer)
Binds tile layer.
std::string getExtra(const std::string &key, const std::string &fallback={}) const
Returns the extra.
int worldToCell3DY(float worldX, float worldY, float worldZ) const
World to cell 3 dy.
void setStagger(const std::string &axis, const std::string &index)
Sets the stagger.
int placeAtWorld(const std::string &buildingId, float worldX, float worldY, float rotationDeg=0.f)
Place at world.
float getCellSize() const
Returns the cell size.
int getCornerOccupant(const std::string &channel, int vertexX, int vertexY) const
Query a corner-domain occupant at the active floor.
int getAnyOccupant(int cellX, int cellY) const
Returns the any occupant.
void worldToCell3D(float worldX, float worldY, float worldZ, int &cellX, int &cellY) const
World to cell 3 d.
std::string canPlaceCornerReason(const std::string &buildingId, int vertexX, int vertexY)
Stable corner placement rejection token.
std::string canRemoveBuildingReason(int instanceId) const
Stable rejection token for ordinary removal (support_in_use when depended on).
float cellToWorld3DY(int cellX, int cellY, float elevation) const
Cell to world 3 dy.
int getAnyOccupantAtLevel(int cellX, int cellY, int level) const
Query the first occupant across channels at an explicit floor.
int getFreeOccupant(const std::string &channel, float worldX, float worldY) const
Return the first free-domain object whose circular footprint contains the point.
std::string getBuildingSurfaceId(int instanceId) const
Surface provider identity captured when this instance was placed.
std::string getBuildingChannel(int instanceId) const
Returns the building channel.
void setTerrainGidMapJson(const std::string &json)
Sets the terrain gid map json.
float getBuildingWorldZ(int instanceId) const
Returns the building world z.
int getOccupantInChannel(const std::string &channel, int cellX, int cellY) const
Returns the occupant in channel.
bool canPlaceCorner(const std::string &buildingId, int vertexX, int vertexY)
Validate corner placement without mutation.
float getBuildingSurfaceNormalY(int instanceId) const
Y component of the unit surface normal captured at placement time.
float cellToWorld3DZ(int cellX, int cellY, float elevation) const
Cell to world 3 dz.
std::vector< int > & channelOccupancy(const std::string &channel)
Channel occupancy.
void fillTerrain(int semantic)
地形语义(占用检查用)。
void clearTerrainGidMap()
Clears terrain gid map.
void cellToWorld3D(int cellX, int cellY, float elevation, float &worldX, float &worldY, float &worldZ) const
Cell to world 3 d.
bool isCornerEmpty(const std::string &channel, int vertexX, int vertexY) const
Return whether a corner-domain address is empty in one channel.
int getBuildingDependentCount(int instanceId) const
Number of instances that directly depend on this instance.
ECS tile layer entity. Script mutates tile GIDs / tileset / draw; TileRenderSystem batch-draws atlas ...
Definition TileLayer.h:33
int getTile(int tx, int ty)
Definition TileLayer.cpp:72
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
EdgeAxis
Canonical orientation of an edge in logical grid coordinates.
void worldToCell(const GridConfig &cfg, float px, float py, int &cx, int &cy, int mapW, int mapH)
Maps planar coordinates to the nearest cell. Staggered/hex layouts use a bounded nearest-neighbor sea...
void cellToWorld(const GridConfig &cfg, int cx, int cy, float &px, float &py)
Maps a cell to planar coordinates (px, py). On XY plane these are world (x, y); on XZ plane they are ...
Canonical address of one grid edge.
Strong world-local identity of one authoritative edge-curve group.
已放置的建筑实例。
int level
Authoritative discrete floor coordinate used by occupancy and topology.
Grid shape, cell metrics, origin, and placement plane. Pure data; use GridProjection helpers for worl...
Definition GridConfig.h:34
static const char * layoutName(GridLayout l)
Stable string name for a layout enum value.
Definition GridConfig.cpp:5
static const char * planeName(GridPlane p)
Stable string name for a plane enum value.
static GridPlane planeFromName(const std::string &name)
Parse a plane name; unknown names fall back to XY.
static GridLayout layoutFromName(const std::string &name)
Parse a layout name; unknown names fall back to Rectangle.