载入中...
搜索中...
未找到
PlacementValidation.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
20bool PlacementSystem::checkBoundsAndOccupancy(const PlacementWorld &world, const BuildingDefinition &def,
21 const PlacementQuery &q, bool checkOccupancy, std::string *reason) {
22 bool ok = true;
23 foreachFootprintCell(def, q.cellX, q.cellY, q.rotationDeg, [&](int cx, int cy) {
24 if (!world.inBounds(cx, cy)) {
25 if (reason) *reason = "out_of_bounds";
26 ok = false;
27 return false;
28 }
29 if (checkOccupancy) {
30 const int occ = world.getOccupantAtLevel(def.channel, cx, cy, q.level);
31 if (occ != 0 && occ != q.excludeInstanceId) {
32 if (reason) *reason = "occupied";
33 ok = false;
34 return false;
35 }
36 }
37 return true;
38 });
39 return ok;
40}
41
42bool PlacementSystem::checkTerrain(const PlacementWorld &world, const BuildingDefinition &def, const PlacementQuery &q,
43 std::string *reason) {
44 if (def.requireTerrain.empty() && def.forbidTerrain.empty()) return true;
45 bool ok = true;
46 foreachFootprintCell(def, q.cellX, q.cellY, q.rotationDeg, [&](int cx, int cy) {
47 const int sem = world.getTerrain(cx, cy);
48 if (!def.forbidTerrain.empty()) {
49 if (std::find(def.forbidTerrain.begin(), def.forbidTerrain.end(), sem) != def.forbidTerrain.end()) {
50 if (reason) *reason = "terrain_forbidden";
51 ok = false;
52 return false;
53 }
54 }
55 if (!def.requireTerrain.empty()) {
56 if (std::find(def.requireTerrain.begin(), def.requireTerrain.end(), sem) == def.requireTerrain.end()) {
57 if (reason) *reason = "terrain_mismatch";
58 ok = false;
59 return false;
60 }
61 }
62 return true;
63 });
64 return ok;
65}
66
67bool PlacementSystem::checkAdjacency(const PlacementWorld &world, const BuildingDefinition &def,
68 const PlacementQuery &q, std::string *reason) {
69 const bool needTag = !def.requireAdjacentTag.empty();
70 const bool needTerrain = def.requireAdjacentTerrain >= 0;
71 if (!needTag && !needTerrain) return true;
72
73 // Collect footprint cells + expand by 1 (4-neigh).
74 std::vector<std::pair<int, int>> cells;
75 foreachFootprintCell(def, q.cellX, q.cellY, q.rotationDeg, [&](int cx, int cy) {
76 cells.emplace_back(cx, cy);
77 return true;
78 });
79
80 auto isFootprint = [&](int x, int y) {
81 return std::find(cells.begin(), cells.end(), std::make_pair(x, y)) != cells.end();
82 };
83
84 bool tagOk = !needTag;
85 bool terrainOk = !needTerrain;
86 static const int dx[4] = {1, -1, 0, 0};
87 static const int dy[4] = {0, 0, 1, -1};
88
89 for (const auto &c : cells) {
90 for (int i = 0; i < 4; ++i) {
91 const int nx = c.first + dx[i];
92 const int ny = c.second + dy[i];
93 if (isFootprint(nx, ny)) continue;
94 if (!world.inBounds(nx, ny)) continue;
95
96 if (needTag && !tagOk) {
97 const int occ = world.getAnyOccupantAtLevel(nx, ny, q.level);
98 if (occ != 0 && occ != q.excludeInstanceId) {
99 auto it = world.buildings().find(occ);
100 if (it != world.buildings().end()) {
101 const PlacedBuilding &pb = it->second;
102 bool hit = pb.hasTag(def.requireAdjacentTag);
103 if (!hit) {
104 if (const BuildingDefinition *odef = BuildingRegistry::find(pb.buildingId)) {
105 hit = odef->hasTag(def.requireAdjacentTag);
106 }
107 }
108 if (hit) tagOk = true;
109 }
110 }
111 }
112 if (needTerrain && !terrainOk) {
113 if (world.getTerrain(nx, ny) == def.requireAdjacentTerrain) terrainOk = true;
114 }
115 if (tagOk && terrainOk) return true;
116 }
117 }
118
119 if (needTag && !tagOk) {
120 if (reason) *reason = "adjacency_tag";
121 return false;
122 }
123 if (needTerrain && !terrainOk) {
124 if (reason) *reason = "adjacency_terrain";
125 return false;
126 }
127 return true;
128}
129
130bool PlacementSystem::runValidate(const PlacementWorld &world, const BuildingDefinition &def, const PlacementQuery &q,
131 std::string *reason) {
132 ensureBuiltins();
133 std::string rule = def.validateRule.empty() ? world.getValidateRule() : def.validateRule;
134 if (rule.empty()) rule = "default";
135 auto &rules = validateRules();
136 auto it = rules.find(rule);
137 if (it == rules.end() || !it->second) {
138 it = rules.find("default");
139 }
140 if (it == rules.end() || !it->second) {
141 if (reason) *reason = "validate_rejected";
142 return false;
143 }
144 return it->second(world, q, reason);
145}
146
147bool PlacementSystem::canPlaceElev(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY,
148 float elevation, float rotationDeg, int excludeInstanceId, std::string *reason,
149 int level) {
150 const BuildingDefinition *definition = BuildingRegistry::find(buildingId);
151 if (definition && definition->placementKind != "cell") {
152 if (reason) {
153 if (definition->placementKind == "edge")
154 *reason = "edge_requires_edge_address";
155 else if (definition->placementKind == "corner")
156 *reason = "corner_requires_corner_address";
157 else
158 *reason = "free_requires_free_address";
159 }
160 return false;
161 }
163 q.buildingId = buildingId;
164 q.cellX = cellX;
165 q.cellY = cellY;
166 q.level = level == std::numeric_limits<int>::min() && world ? world->getActiveLevel() : level;
167 q.rotationDeg = normalizeRotation(buildingId, rotationDeg);
168 if (world) world->cellToWorldPlane(cellX, cellY, q.worldX, q.worldY);
169 q.elevation = elevation;
170 q.excludeInstanceId = excludeInstanceId;
171 return canPlaceQuery(world, q, reason);
172}
173
174bool PlacementSystem::checkSurfacePatch(const BuildingDefinition &def, const PlacementQuery &q, std::string *reason) {
175 if (q.surfaceSampleCount <= 0) return true;
176 if (q.surfaceMaxSlopeDegrees > def.maxSurfaceSlopeDegrees + 1e-4f) {
177 if (reason) *reason = "surface_slope";
178 return false;
179 }
180 if (def.maxSurfaceHeightDelta >= 0.f && q.surfaceHeightDelta > def.maxSurfaceHeightDelta + 1e-4f) {
181 if (reason) *reason = "surface_height_delta";
182 return false;
183 }
184 return true;
185}
186
187bool PlacementSystem::checkStructuralSupport(const PlacementWorld &world, const BuildingDefinition &def,
188 const PlacementQuery &q, std::string *reason) {
189 if (def.supportMode == "corner_below") {
190 if (def.placementKind != "corner") {
191 if (reason) *reason = "support_mode_mismatch";
192 return false;
193 }
194 const int instanceId = world.getAnyCornerOccupantAtLevel(q.cellX, q.cellY, q.level - 1);
195 const auto found = world.buildings().find(instanceId);
196 if (instanceId == 0 || found == world.buildings().end()) {
197 if (reason) *reason = "support_missing";
198 return false;
199 }
200 if (def.supportTag.empty()) return true;
201 const BuildingDefinition *support = BuildingRegistry::find(found->second.buildingId);
202 const bool tagged = found->second.hasTag(def.supportTag) || (support && support->hasTag(def.supportTag));
203 if (!tagged && reason) *reason = "support_missing";
204 return tagged;
205 }
206 if (def.supportMode != "cell_below") return true;
207 bool supported = true;
208 foreachFootprintCell(def, q.cellX, q.cellY, q.rotationDeg, [&](int cellX, int cellY) {
209 const int instanceId = world.getAnyOccupantAtLevel(cellX, cellY, q.level - 1);
210 const auto found = world.buildings().find(instanceId);
211 if (instanceId == 0 || found == world.buildings().end()) {
212 supported = false;
213 } else if (!def.supportTag.empty()) {
214 const PlacedBuilding &placed = found->second;
215 const BuildingDefinition *support = BuildingRegistry::find(placed.buildingId);
216 // Footprint iteration visits every cell even after a callback returns false.
217 // A later tagged support must not erase an earlier missing/invalid support.
218 supported = supported && (placed.hasTag(def.supportTag) || (support && support->hasTag(def.supportTag)));
219 }
220 if (!supported && reason) *reason = "support_missing";
221 return supported;
222 });
223 return supported;
224}
225
226std::vector<int> PlacementSystem::collectStructuralSupports(const PlacementWorld &world, const BuildingDefinition &def,
227 const PlacementQuery &q) {
228 std::vector<int> result;
229 if (def.supportMode == "corner_below") {
230 const int support = world.getAnyCornerOccupantAtLevel(q.cellX, q.cellY, q.level - 1);
231 if (support != 0) result.push_back(support);
232 return result;
233 }
234 if (def.supportMode != "cell_below") return result;
235 foreachFootprintCell(def, q.cellX, q.cellY, q.rotationDeg, [&](int cellX, int cellY) {
236 const int support = world.getAnyOccupantAtLevel(cellX, cellY, q.level - 1);
237 if (support != 0 && std::find(result.begin(), result.end(), support) == result.end()) result.push_back(support);
238 return true;
239 });
240 return result;
241}
242
243bool PlacementSystem::hasStructuralDependents(const PlacementWorld &world, int instanceId) {
244 for (const auto &[id, placed] : world.buildings_) {
245 (void)id;
246 if (std::find(placed.supportInstanceIds.begin(), placed.supportInstanceIds.end(), instanceId) !=
247 placed.supportInstanceIds.end())
248 return true;
249 }
250 return false;
251}
252
253bool PlacementSystem::replacementPreservesStructuralDependents(const PlacementWorld &world, int instanceId,
254 const BuildingDefinition &replacement) {
255 const auto sourceIt = world.buildings_.find(instanceId);
256 if (sourceIt == world.buildings_.end()) return false;
257 const PlacedBuilding &source = sourceIt->second;
258
259 std::unordered_set<uint64_t> replacementCells;
260 if (replacement.placementKind != "edge") {
261 foreachFootprintCell(replacement, source.originCellX, source.originCellY, source.rotationDeg,
262 [&](int x, int y) {
263 replacementCells.insert((uint64_t(uint32_t(x)) << 32U) | uint32_t(y));
264 return true;
265 });
266 }
267
268 for (const auto &[candidateId, dependent] : world.buildings_) {
269 (void)candidateId;
270 if (std::find(dependent.supportInstanceIds.begin(), dependent.supportInstanceIds.end(), instanceId) ==
271 dependent.supportInstanceIds.end())
272 continue;
273 const BuildingDefinition *dependentDef = BuildingRegistry::find(dependent.buildingId);
274 if (!dependentDef) return false;
275 const bool tagMatches = dependentDef->supportTag.empty() || replacement.hasTag(dependentDef->supportTag);
276 if (!tagMatches) return false;
277 if (dependentDef->supportMode == "corner_below") {
278 if (replacement.placementKind != "corner" || source.corner != dependent.corner) return false;
279 continue;
280 }
281 if (dependentDef->supportMode != "cell_below" || replacement.placementKind != "cell") return false;
282 bool covered = true;
283 foreachFootprintCell(*dependentDef, dependent.originCellX, dependent.originCellY, dependent.rotationDeg,
284 [&](int x, int y) {
285 if (world.getAnyOccupantAtLevel(x, y, dependent.level - 1) != instanceId) return true;
286 const uint64_t key = (uint64_t(uint32_t(x)) << 32U) | uint32_t(y);
287 covered = replacementCells.contains(key);
288 return covered;
289 });
290 if (!covered) return false;
291 }
292 return true;
293}
294
295bool PlacementSystem::canPlaceQuery(PlacementWorld *world, const PlacementQuery &q, std::string *reason) {
296 ensureBuiltins();
297 if (!world) {
298 if (reason) *reason = "no_world";
299 return false;
300 }
301 const BuildingDefinition *def = BuildingRegistry::find(q.buildingId);
302 if (!def) {
303 if (reason) *reason = "unknown_building";
304 return false;
305 }
306 return runValidate(*world, *def, q, reason);
307}
308
309bool PlacementSystem::canPlace(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY,
310 float rotationDeg, int excludeInstanceId, std::string *reason) {
311 return canPlaceElev(world, buildingId, cellX, cellY, 0.f, rotationDeg, excludeInstanceId, reason);
312}
313
314
315} // namespace eve::building
double y
double x
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.
std::array< double, 10 > q
std::int32_t c
float elevation
Poco::JSON::Object::Ptr result
int level
glm::dvec3 dx
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
bool hit
bool found
std::string_view source
float dy
Definition Shadow.cpp:151
bool placed
int covered
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
建筑模板(进程级注册表中的定义)。
float maxSurfaceHeightDelta
Maximum elevation range across a sampled footprint; negative disables it.
float maxSurfaceSlopeDegrees
Maximum angle between a footprint normal and the grid-plane up axis.
校验上下文:传给可插拔规则。