载入中...
搜索中...
未找到
BuildingAssemblyValidation.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <cmath>
3#include <set>
7
8namespace eve::building {
9using namespace assembly_detail;
10Value AssemblyService::admissionStamp(const PlacementWorld& w) {
11 auto sequence = [](const auto& values) {
13 for (auto item : values) a.emplace_back(item);
14 return Value(std::move(a));
15 };
16 auto channels = [&](const auto& values) {
18 for (const auto& [key, items] : values) a[key] = sequence(items);
19 return Value(std::move(a));
20 };
21 auto levels = [&](const auto& values) {
23 for (const auto& [key, items] : values) a[std::to_string(key)] = channels(items);
24 return Value(std::move(a));
25 };
26 Value::Object supports;
27 for (const auto& [id, p] : w.buildings_) supports[std::to_string(id)] = sequence(p.supportInstanceIds);
28 return Value::Object{{"cells", sequence(w.occupancy_)},
29 {"channels", channels(w.allChannels_)},
30 {"horizontal", channels(w.horizontalEdgeChannels_)},
31 {"vertical", channels(w.verticalEdgeChannels_)},
32 {"corners", channels(w.cornerChannels_)},
33 {"floors", levels(w.cellChannelsByLevel_)},
34 {"horizontalFloors", levels(w.horizontalEdgesByLevel_)},
35 {"verticalFloors", levels(w.verticalEdgesByLevel_)},
36 {"cornerFloors", levels(w.cornersByLevel_)},
37 {"supports", std::move(supports)}};
38}
39Result<void> AssemblyService::validateFinal(PlacementWorld& w) {
40 auto grid = checkGrid(w);
41 if (!grid.ok()) return grid;
42 w.occupancy_.assign(size_t(w.width_) * size_t(w.height_), 0);
43 w.allChannels_.clear();
44 w.horizontalEdgeChannels_.clear();
45 w.verticalEdgeChannels_.clear();
46 w.cornerChannels_.clear();
47 w.cellChannelsByLevel_.clear();
48 w.horizontalEdgesByLevel_.clear();
49 w.verticalEdgesByLevel_.clear();
50 w.cornersByLevel_.clear();
51 std::set<int> ids;
52 // Install the complete final occupancy first; adjacency/support validation below sees
53 // the same state regardless of member insertion order.
54 for (int id : w.instanceOrder_) {
55 if (!ids.insert(id).second || !w.buildings_.contains(id)) return fail<void>("Invalid instance order");
56 auto& p = w.buildings_.at(id);
57 const auto* d = BuildingRegistry::find(p.buildingId);
58 if (!d || p.instanceId != id || id <= 0 || d->placementKind != p.placementKind || d->channel != p.channel ||
59 !std::isfinite(p.worldX) || !std::isfinite(p.worldY) || !std::isfinite(p.rotationDeg) ||
60 !std::isfinite(p.elevation) || std::abs(double(p.originCellX)) > 10000000 ||
61 std::abs(double(p.originCellY)) > 10000000 || std::abs(double(p.level)) > 10000000 || d->footprintW <= 0 ||
62 d->footprintH <= 0 || d->footprintW > 4096 || d->footprintH > 4096)
63 return fail<void>("Invalid instance or incompatible definition: " + p.buildingId);
64 bool valid = true;
65 auto write = [&](std::vector<int>& channel, size_t size, size_t index) {
66 if (channel.size() != size) channel.assign(size, 0);
67 if (index >= size || channel[index] != 0)
68 valid = false;
69 else
70 channel[index] = id;
71 };
72 if (p.placementKind == "cell") {
73 PlacementSystem::foreachFootprintCell(*d, p.originCellX, p.originCellY, p.rotationDeg, [&](int x, int y) {
74 if (!w.inBounds(x, y))
75 valid = false;
76 else
77 write(w.channelOccupancy(d->channel, p.level), size_t(w.width_) * w.height_,
78 size_t(y) * w.width_ + x);
79 return valid;
80 });
81 } else if (p.placementKind == "edge") {
82 const bool h = p.edge.axis == EdgeAxis::Horizontal;
83 const int width = w.width_ + (h ? 0 : 1), height = w.height_ + (h ? 1 : 0);
84 if (p.edge.x < 0 || p.edge.y < 0 || p.edge.x >= width || p.edge.y >= height)
85 valid = false;
86 else
87 write(w.edgeChannels(p.edge.axis, p.level)[d->channel], size_t(width) * height,
88 size_t(p.edge.y) * width + p.edge.x);
89 } else if (p.placementKind == "corner") {
90 if (p.corner.x < 0 || p.corner.y < 0 || p.corner.x > w.width_ || p.corner.y > w.height_)
91 valid = false;
92 else
93 write(w.cornerChannels(p.level)[d->channel], size_t(w.width_ + 1) * (w.height_ + 1),
94 size_t(p.corner.y) * (w.width_ + 1) + p.corner.x);
95 } else if (p.placementKind == "free") {
97 } else
98 valid = false;
99 if (!valid) return fail<void>("Final occupancy conflicts or exceeds world bounds", DiagnosticCode::Conflict);
100 }
101 if (ids.size() != w.buildings_.size()) return fail<void>("Unordered instance in world");
102 for (int id : w.instanceOrder_) {
103 auto& p = w.buildings_.at(id);
104 const auto& d = *BuildingRegistry::find(p.buildingId);
105 std::string reason;
106 bool valid = false;
107 if (p.placementKind == "cell")
108 valid = PlacementSystem::canPlaceElev(&w, p.buildingId, p.originCellX, p.originCellY, p.elevation,
109 p.rotationDeg, id, &reason, p.level);
110 else if (p.placementKind == "edge")
111 valid = PlacementSystem::canPlaceEdge(&w, p.buildingId, p.edge.x, p.edge.y,
112 p.edge.axis == EdgeAxis::Horizontal ? "north" : "west", id, &reason,
113 p.level);
114 else if (p.placementKind == "corner")
115 valid = PlacementSystem::canPlaceCorner(&w, p.buildingId, p.corner.x, p.corner.y, id, &reason, p.level);
116 else
117 valid = PlacementSystem::canPlaceFree(&w, p.buildingId, p.worldX, p.worldY, id, &reason, p.level,
118 p.rotationDeg);
119 PlacementQuery query;
120 query.buildingId = p.buildingId;
121 query.cellX = p.originCellX;
122 query.cellY = p.originCellY;
123 query.level = p.level;
124 query.rotationDeg = p.rotationDeg;
125 query.excludeInstanceId = id;
126 if (!valid || !PlacementSystem::checkStructuralSupport(w, d, query, &reason))
127 return fail<void>("Final state rejected for " + std::to_string(id) + ": " + reason,
129 p.supportInstanceIds = PlacementSystem::collectStructuralSupports(w, d, query);
130 }
131 std::set<int> owned;
132 for (const auto& [id, a] : w.assemblyState_->records) {
133 if (id.isNil() || a.id != id || a.blueprint.isNil() || a.members.empty())
134 return fail<void>("Invalid house record");
135 for (const auto& [key, member] : a.members)
136 if (!key || key >= a.nextPartKey || !w.buildings_.contains(member) || !owned.insert(member).second)
137 return fail<void>("Invalid or multiply owned house member");
138 }
139 for (auto& [id, p] : w.buildings_) p.edgeCurveGroupId = {};
140 for (const auto& [id, g] : w.edgeCurveGroups_) {
141 if (!id || g.id.value != id || g.instanceIds.empty()) return fail<void>("Invalid curve identity");
142 std::vector<BuildingInstanceSnapshot> parts;
143 for (int member : g.instanceIds) {
144 auto it = w.buildings_.find(member);
145 if (it == w.buildings_.end() || it->second.edgeCurveGroupId || it->second.placementKind != "edge" ||
146 it->second.buildingId != g.buildingId || it->second.level != g.level)
147 return fail<void>("Invalid curve member");
148 it->second.edgeCurveGroupId = g.id;
149 parts.push_back(codec::snapshot(it->second));
150 }
152 if (!parsed.ok()) return fail<void>("Invalid curve geometry");
153 }
154 return Result<void>::success();
155}
156} // namespace eve::building
double y
double x
float w
Definition AnimClip.cpp:738
std::map< std::string, std::vector< Key >, std::less<> > channels
std::vector< eve::artifact::PartView > parts
ActiveSource owned
glm::vec4 p[6]
SQObjectPtr key
Definition ECS.cpp:155
tensor::Graph g
Definition GpuGraph.cpp:7
std::int32_t second
int h
std::uint32_t height
std::uint32_t width
std::function< void(b3JointId, const Numbers &)> write
std::string id
bool valid
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
std::unique_ptr< gpgpu::Sequence > sequence
Definition OnnxGpgpu.cpp:43
std::set< std::string > values
float d
float size
Definition TreeMesh.cpp:158
float(ui::Theme::* member)[4]
std::map< std::string, std::span< const std::uint8_t > > members
uint32_t index
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
std::map< std::string, Value > Object
Definition Value.h:34
std::vector< Value > Array
Definition Value.h:33
static const BuildingDefinition * find(const std::string &id)
Finds .
Definition Building.cpp:557
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 foreachFootprintCell(const BuildingDefinition &def, int originCellX, int originCellY, float rotationDeg, const std::function< bool(int cx, int cy)> &fn)
枚举占地格子(旋转后局部 → 世界格子)。返回 false 若定义未知。
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 canPlaceElev(PlacementWorld *world, const std::string &buildingId, int cellX, int cellY, float elevation, float rotationDeg=0.f, int excludeInstanceId=0, std::string *reason=nullptr, int level=std::numeric_limits< int >::min())
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.
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
Result< T > fail(std::string message, DiagnosticCode code=DiagnosticCode::InvalidArgument)
Result< void > checkGrid(const PlacementWorld &world)
Result< EdgeCurvePayload > parseEdgeCurve(const Value &value)
Decode one complete curve with structured failure and independent ownership.
Value edgeCurveValue(const building::EdgeCurveGroup &group, const std::vector< BuildingInstanceSnapshot > &instances)
Encode one complete curve group in linear time; no world mutation.
BuildingInstanceSnapshot snapshot(const building::PlacedBuilding &placed)
Copy a runtime instance into its canonical owning persistence record.
void applyDefinitionFreeFootprint(PlacedBuilding &placed, const BuildingDefinition &definition, const PlacementWorld &world)
建筑放置模块入口:定义 / 放置世界 / 鬼影 / 变更事件的脚本绑定点。 设计文档:docs/dev/建筑放置系统设计.md
Definition Building.cpp:10
I * query()
Queries .
Definition Capability.h:88
std::variant< std::monostate, std::int64_t, double, std::string, bool > Value
Definition Database.h:27
WidgetDesc grid(int columns, std::vector< WidgetDesc > children, std::string id)
Fixed-column grid; children are placed in source order.
Definition Widget.cpp:878