14#include <unordered_set>
18using namespace placement_detail;
20bool PlacementSystem::checkBoundsAndOccupancy(
const PlacementWorld &world,
const BuildingDefinition &def,
21 const PlacementQuery &
q,
bool checkOccupancy, std::string *reason) {
24 if (!world.inBounds(cx, cy)) {
25 if (reason) *reason =
"out_of_bounds";
30 const int occ = world.getOccupantAtLevel(def.channel,
cx,
cy,
q.level);
31 if (occ != 0 && occ !=
q.excludeInstanceId) {
32 if (reason) *reason =
"occupied";
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;
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";
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";
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;
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);
80 auto isFootprint = [&](
int x,
int y) {
81 return std::find(
cells.begin(),
cells.end(), std::make_pair(
x,
y)) !=
cells.end();
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};
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;
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);
104 if (
const BuildingDefinition *odef = BuildingRegistry::find(pb.buildingId)) {
105 hit = odef->hasTag(def.requireAdjacentTag);
108 if (
hit) tagOk =
true;
112 if (needTerrain && !terrainOk) {
113 if (world.getTerrain(
nx,
ny) == def.requireAdjacentTerrain) terrainOk =
true;
115 if (tagOk && terrainOk)
return true;
119 if (needTag && !tagOk) {
120 if (reason) *reason =
"adjacency_tag";
123 if (needTerrain && !terrainOk) {
124 if (reason) *reason =
"adjacency_terrain";
130bool PlacementSystem::runValidate(
const PlacementWorld &world,
const BuildingDefinition &def,
const PlacementQuery &
q,
131 std::string *reason) {
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");
140 if (it == rules.end() || !it->second) {
141 if (reason) *reason =
"validate_rejected";
144 return it->second(world,
q, reason);
147bool PlacementSystem::canPlaceElev(
PlacementWorld *world,
const std::string &buildingId,
int cellX,
int cellY,
148 float elevation,
float rotationDeg,
int excludeInstanceId, std::string *reason,
151 if (definition && definition->placementKind !=
"cell") {
153 if (definition->placementKind ==
"edge")
154 *reason =
"edge_requires_edge_address";
155 else if (definition->placementKind ==
"corner")
156 *reason =
"corner_requires_corner_address";
158 *reason =
"free_requires_free_address";
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);
170 q.excludeInstanceId = excludeInstanceId;
171 return canPlaceQuery(world,
q, reason);
175 if (
q.surfaceSampleCount <= 0)
return true;
177 if (reason) *reason =
"surface_slope";
181 if (reason) *reason =
"surface_height_delta";
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";
194 const int instanceId = world.getAnyCornerOccupantAtLevel(
q.cellX,
q.cellY,
q.level - 1);
197 if (reason) *reason =
"support_missing";
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";
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()) {
213 }
else if (!def.supportTag.empty()) {
214 const PlacedBuilding &placed = found->second;
215 const BuildingDefinition *support = BuildingRegistry::find(placed.buildingId);
218 supported = supported && (placed.hasTag(def.supportTag) || (support && support->hasTag(def.supportTag)));
220 if (!supported && reason) *reason =
"support_missing";
226std::vector<int> PlacementSystem::collectStructuralSupports(
const PlacementWorld &world,
const BuildingDefinition &def,
227 const PlacementQuery &
q) {
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);
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);
243bool PlacementSystem::hasStructuralDependents(
const PlacementWorld &world,
int instanceId) {
244 for (
const auto &[
id,
placed] : world.buildings_) {
247 placed.supportInstanceIds.end())
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;
259 std::unordered_set<uint64_t> replacementCells;
260 if (replacement.placementKind !=
"edge") {
261 foreachFootprintCell(replacement,
source.originCellX,
source.originCellY,
source.rotationDeg,
263 replacementCells.insert((uint64_t(uint32_t(x)) << 32U) | uint32_t(y));
268 for (
const auto &[candidateId, dependent] : world.buildings_) {
270 if (std::find(dependent.supportInstanceIds.begin(), dependent.supportInstanceIds.end(),
instanceId) ==
271 dependent.supportInstanceIds.end())
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;
281 if (dependentDef->supportMode !=
"cell_below" || replacement.placementKind !=
"cell")
return false;
283 foreachFootprintCell(*dependentDef, dependent.originCellX, dependent.originCellY, dependent.rotationDeg,
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);
295bool PlacementSystem::canPlaceQuery(PlacementWorld *world,
const PlacementQuery &
q, std::string *reason) {
298 if (reason) *reason =
"no_world";
301 const BuildingDefinition *def = BuildingRegistry::find(
q.buildingId);
303 if (reason) *reason =
"unknown_building";
306 return runValidate(*world, *def,
q, reason);
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);
Stable, structured diagnostics shared by engine modules.
Pure cell/world projection helpers for GridConfig.
std::array< double, 10 > q
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
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
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.