载入中...
搜索中...
未找到
PlacementMutation.cpp
浏览该文件的文档.
2
4#include "building/Ghost.h"
8#include "common/Diagnostic.h"
10
11#include <algorithm>
12#include <cmath>
13#include <limits>
14#include <unordered_set>
15
16namespace eve::building {
17
18using namespace placement_detail;
19
20void PlacementSystem::writeOccupancy(PlacementWorld &world, const BuildingDefinition &def, const PlacedBuilding &placed,
21 int instanceId) {
22 std::vector<int> &occ = world.channelOccupancy(def.channel, placed.level);
23 foreachFootprintCell(def, placed.originCellX, placed.originCellY, placed.rotationDeg, [&](int cx, int cy) {
24 if (world.inBounds(cx, cy)) {
25 const size_t idx = size_t(cy) * size_t(world.getWidth()) + size_t(cx);
26 if (idx < occ.size()) occ[idx] = instanceId;
27 }
28 return true;
29 });
30}
31
32void PlacementSystem::clearOccupancy(PlacementWorld &world, int instanceId) {
33 // 默认通道("")占用存于 occupancy_,附加通道存于 allChannels_,两者都要清。
34 for (int &v : world.occupancy()) {
35 if (v == instanceId) v = 0;
36 }
37 for (auto &kv : world.allChannels()) {
38 for (int &v : kv.second) {
39 if (v == instanceId) v = 0;
40 }
41 }
42 for (auto &kv : world.horizontalEdgeChannels_) {
43 for (int &v : kv.second)
44 if (v == instanceId) v = 0;
45 }
46 for (auto &kv : world.verticalEdgeChannels_) {
47 for (int &v : kv.second)
48 if (v == instanceId) v = 0;
49 }
50 for (auto &kv : world.cornerChannels_) {
51 for (int &v : kv.second)
52 if (v == instanceId) v = 0;
53 }
54 for (auto &level : world.cellChannelsByLevel_)
55 for (auto &channel : level.second)
56 for (int &value : channel.second)
57 if (value == instanceId) value = 0;
58 for (auto &levels : {&world.horizontalEdgesByLevel_, &world.verticalEdgesByLevel_})
59 for (auto &level : *levels)
60 for (auto &channel : level.second)
61 for (int &value : channel.second)
62 if (value == instanceId) value = 0;
63 for (auto &level : world.cornersByLevel_)
64 for (auto &channel : level.second)
65 for (int &value : channel.second)
66 if (value == instanceId) value = 0;
67}
68
69int PlacementSystem::placeAt(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY,
70 float rotationDeg) {
71 ensureBuiltins();
72 if (!world) return 0;
73 const BuildingDefinition *def = BuildingRegistry::find(buildingId);
74 if (!def) return 0;
75 if (def->placementKind != "cell") return 0;
76 const float rot = normalizeRotation(buildingId, rotationDeg);
77 if (!canPlace(world, buildingId, cellX, cellY, rot, 0, nullptr)) return 0;
78
80 pb.instanceId = nextInstanceId(*world);
81 pb.buildingId = buildingId;
82 pb.originCellX = cellX;
83 pb.originCellY = cellY;
84 pb.level = world->getActiveLevel();
85 world->cellToWorldPlane(cellX, cellY, pb.worldX, pb.worldY);
86 pb.elevation = 0.f;
87 pb.rotationDeg = rot;
88 pb.channel = def->channel;
89 pb.tags = def->tags;
90 PlacementQuery supportQuery;
91 supportQuery.cellX = pb.originCellX;
92 supportQuery.cellY = pb.originCellY;
93 supportQuery.level = pb.level;
94 supportQuery.rotationDeg = pb.rotationDeg;
95 pb.supportInstanceIds = collectStructuralSupports(*world, *def, supportQuery);
96
97 writeOccupancy(*world, *def, pb, pb.instanceId);
98 world->buildings()[pb.instanceId] = pb;
99 world->instanceOrder_.push_back(pb.instanceId);
100
102 ev.action = "place";
103 ev.worldId = world->getId();
104 ev.buildingId = buildingId;
105 ev.instanceId = pb.instanceId;
106 ev.cellX = cellX;
107 ev.cellY = cellY;
108 ev.rotationDeg = rot;
109 ev.worldX = pb.worldX;
110 ev.worldY = pb.worldY;
111 ev.elevation = pb.elevation;
112 ev.channel = pb.channel;
113 if (world->publishEvents_) emit(std::move(ev));
114 return pb.instanceId;
115}
116
117int PlacementSystem::placeAtWorld(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY,
118 float rotationDeg) {
119 ensureBuiltins();
120 if (!world) return 0;
121 const SnapResult s = snap(*world, buildingId, worldX, worldY);
122 const int id = placeAt(world, buildingId, s.cellX, s.cellY, rotationDeg);
123 if (id <= 0) return 0;
124 // Preserve free / custom snap world pose (cell placement alone snaps to cell origin).
125 auto it = world->buildings().find(id);
126 if (it != world->buildings().end()) {
127 it->second.worldX = s.worldX;
128 it->second.worldY = s.worldY;
129 }
130 return id;
131}
132
133int PlacementSystem::placeAtWorld3D(PlacementWorld *world, const std::string &buildingId, float worldX, float worldY,
134 float worldZ, float rotationDeg) {
135 ensureBuiltins();
136 if (!world) return 0;
137 const SnapResult s = snap3D(*world, buildingId, worldX, worldY, worldZ);
138 const int id = placeAt(world, buildingId, s.cellX, s.cellY, rotationDeg);
139 if (id <= 0) return 0;
140 auto it = world->buildings().find(id);
141 if (it != world->buildings().end()) {
142 it->second.worldX = s.worldX;
143 it->second.worldY = s.worldY;
144 it->second.elevation = s.elevation;
145 }
146 return id;
147}
148
149int PlacementSystem::placeGhost(PlacementWorld *world, Ghost *ghost) {
150 if (!world || !ghost) return 0;
151 if (!ghost->validate(world)) return 0;
152 const BuildingDefinition *def = BuildingRegistry::find(ghost->getBuildingId());
153 if (!def) return 0;
154 if (def->placementKind == "edge") {
155 const char *direction = ghost->edge_.axis == EdgeAxis::Horizontal ? "north" : "west";
156 return placeEdge(world, ghost->getBuildingId(), ghost->edge_.x, ghost->edge_.y, direction);
157 }
158 if (def->placementKind == "corner") {
159 if (ghost->placementKind_ != "corner") return 0;
160 return placeCorner(world, ghost->getBuildingId(), ghost->corner_.x, ghost->corner_.y);
161 }
162 if (def->placementKind == "free") {
163 if (ghost->placementKind_ != "free") return 0;
164 if (!ghost->surfaceId_.empty()) {
165 SurfacePatch patch;
166 if (world->getGrid().plane == grid::GridPlane::XZ) {
167 patch.anchor.worldX = ghost->worldX_;
168 patch.anchor.worldY = ghost->elevation_;
169 patch.anchor.worldZ = ghost->worldY_;
170 } else {
171 patch.anchor.worldX = ghost->worldX_;
172 patch.anchor.worldY = ghost->worldY_;
173 patch.anchor.worldZ = ghost->elevation_;
174 }
175 patch.anchor.surfaceId = ghost->surfaceId_;
176 patch.anchor.surfaceRevision = ghost->surfaceRevision_;
177 patch.anchor.normalX = ghost->surfaceNormalX_;
178 patch.anchor.normalY = ghost->surfaceNormalY_;
179 patch.anchor.normalZ = ghost->surfaceNormalZ_;
180 patch.anchor.tangentX = ghost->surfaceTangentX_;
181 patch.anchor.tangentY = ghost->surfaceTangentY_;
182 patch.anchor.tangentZ = ghost->surfaceTangentZ_;
183 patch.samples.resize(static_cast<size_t>(std::max(ghost->surfaceSampleCount_, 0)));
184 patch.maxSlopeDegrees = ghost->surfaceMaxSlopeDegrees_;
185 patch.heightDelta = ghost->surfaceHeightDelta_;
186 auto result = placeFreeSurfaceResult(world, ghost->getBuildingId(), patch, ghost->rotationDeg_);
187 return result.ok() ? std::move(result).takeValue().instanceId : 0;
188 }
189 return placeFree(world, ghost->getBuildingId(), ghost->worldX_, ghost->worldY_, ghost->elevation_,
190 ghost->rotationDeg_);
191 }
192
194 placed.instanceId = nextInstanceId(*world);
195 placed.buildingId = ghost->getBuildingId();
196 placed.originCellX = ghost->getCellX();
197 placed.originCellY = ghost->getCellY();
198 placed.level = world->getActiveLevel();
199 placed.worldX = ghost->getWorldX();
200 placed.worldY = ghost->getWorldY();
201 placed.elevation = ghost->getElevation();
202 placed.rotationDeg = ghost->getRotationDeg();
203 placed.channel = def->channel;
204 placed.tags = def->tags;
205 placed.surfaceId = ghost->surfaceId_;
206 placed.surfaceRevision = ghost->surfaceRevision_;
207 placed.surfaceNormalX = ghost->surfaceNormalX_;
208 placed.surfaceNormalY = ghost->surfaceNormalY_;
209 placed.surfaceNormalZ = ghost->surfaceNormalZ_;
210 placed.surfaceTangentX = ghost->surfaceTangentX_;
211 placed.surfaceTangentY = ghost->surfaceTangentY_;
212 placed.surfaceTangentZ = ghost->surfaceTangentZ_;
213 placed.surfaceSampleCount = ghost->surfaceSampleCount_;
214 placed.surfaceMaxSlopeDegrees = ghost->surfaceMaxSlopeDegrees_;
215 placed.surfaceHeightDelta = ghost->surfaceHeightDelta_;
216 PlacementQuery supportQuery;
217 supportQuery.cellX = placed.originCellX;
218 supportQuery.cellY = placed.originCellY;
219 supportQuery.level = placed.level;
220 supportQuery.rotationDeg = placed.rotationDeg;
221 placed.supportInstanceIds = collectStructuralSupports(*world, *def, supportQuery);
222
223 writeOccupancy(*world, *def, placed, placed.instanceId);
224 world->buildings()[placed.instanceId] = placed;
225 world->instanceOrder_.push_back(placed.instanceId);
226
228 event.action = "place";
229 event.worldId = world->getId();
230 event.buildingId = placed.buildingId;
231 event.instanceId = placed.instanceId;
232 event.cellX = placed.originCellX;
233 event.cellY = placed.originCellY;
234 event.rotationDeg = placed.rotationDeg;
235 event.worldX = placed.worldX;
236 event.worldY = placed.worldY;
237 event.elevation = placed.elevation;
238 event.channel = placed.channel;
239 if (world->publishEvents_) emit(std::move(event));
240 return placed.instanceId;
241}
242
243
244} // namespace eve::building
const std::string & s
float cx
Definition CardTypes.cpp:36
float cy
Definition CardTypes.cpp:37
Stable, structured diagnostics shared by engine modules.
int instanceId
Pure cell/world projection helpers for GridConfig.
float v
std::int32_t second
std::string id
Poco::JSON::Object::Ptr result
int level
Object::Ptr value
RoadLaneDirection direction
bool placed
EVENGINE_API_WORLD public API.
Definition Ghost.h:16
float getElevation() const
Returns the elevation.
Definition Ghost.h:45
int getCellY() const
Returns the cell y.
Definition Ghost.h:34
float getWorldY() const
Returns the world y.
Definition Ghost.h:41
int getCellX() const
Returns the cell x.
Definition Ghost.h:32
bool validate(PlacementWorld *world)
对当前姿态做校验,写入 valid_/reason_。
Definition Ghost.cpp:270
float getWorldX() const
Returns the world x.
Definition Ghost.h:39
float getRotationDeg() const
Returns the rotation deg.
Definition Ghost.h:50
std::string getBuildingId() const
Returns the building id.
Definition Ghost.h:27
static bool foreachFootprintCell(const BuildingDefinition &def, int originCellX, int originCellY, float rotationDeg, const std::function< bool(int cx, int cy)> &fn)
枚举占地格子(旋转后局部 → 世界格子)。返回 false 若定义未知。
格子型建筑放置世界(脚本可直接操作)。
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
一次成功放置变更的事件(供脚本 poll / C++ hook)。
std::string action
place / remove / move / rotate
建筑模板(进程级注册表中的定义)。
std::string placementKind
Placement domain: cell (default), edge, corner, or free.
std::string channel
占地格子尺寸。
std::vector< std::string > tags
已放置的建筑实例。
std::vector< std::string > tags
int level
Authoritative discrete floor coordinate used by occupancy and topology.
std::vector< int > supportInstanceIds
Derived support links rebuilt from authoritative level/address data.
校验上下文:传给可插拔规则。
int level
Discrete floor whose occupancy and adjacency are queried.
Surface samples and continuity metrics for one rotated building footprint.
std::vector< SurfacePatchSample > samples