载入中...
搜索中...
未找到
HouseGenerationAlgorithms.cpp
浏览该文件的文档.
2
4const HouseComponent *pick(const std::vector<std::reference_wrapper<const HouseComponent>> &choices,
5 std::mt19937 &rng) {
6 if (choices.empty()) return nullptr;
7 int total = 0;
8 for (const auto &choice : choices) total += choice.get().weight;
9 std::uniform_int_distribution<int> roll(1, total);
10 int target = roll(rng);
11 for (const auto &choice : choices) {
12 target -= choice.get().weight;
13 if (target <= 0) return &choice.get();
14 }
15 return &choices.back().get();
16}
17
18bool has(const std::vector<std::reference_wrapper<const HouseComponent>> &values) { return !values.empty(); }
19
20bool isRotationAllowed(const HouseComponent &component, int rotation) {
21 const int normalized = (rotation % 360 + 360) % 360;
22 return std::any_of(component.rotations.begin(), component.rotations.end(),
23 [&](int allowed) { return (allowed % 360 + 360) % 360 == normalized; });
24}
25
26bool isWindowComponent(const HouseComponent *component) {
27 if (!component) return false;
28 if (component->id.find("window") != std::string::npos) return true;
29 return std::find(component->tags.begin(), component->tags.end(), "window") != component->tags.end();
30}
31
32std::vector<std::reference_wrapper<const HouseComponent>> facadeVariant(
33 const std::vector<std::reference_wrapper<const HouseComponent>> &choices, bool wantWindow) {
34 bool hasWindow = false, hasSolid = false;
35 for (const auto &choice : choices) {
36 if (isWindowComponent(&choice.get()))
37 hasWindow = true;
38 else
39 hasSolid = true;
40 }
41 if (!hasWindow || !hasSolid) return choices;
42 std::vector<std::reference_wrapper<const HouseComponent>> filtered;
43 for (const auto &choice : choices)
44 if (isWindowComponent(&choice.get()) == wantWindow) filtered.push_back(choice);
45 return filtered;
46}
47
48bool isOneOf(const std::string &value, std::initializer_list<const char *> options) {
49 for (const char *option : options)
50 if (value == option) return true;
51 return false;
52}
53
55 if (name == "east") return SocketDirection::East;
56 if (name == "south") return SocketDirection::South;
57 if (name == "west") return SocketDirection::West;
59}
60
62 if (direction == SocketDirection::East) return "east";
63 if (direction == SocketDirection::South) return "south";
64 if (direction == SocketDirection::West) return "west";
65 return "north";
66}
67
69 if (direction == SocketDirection::East) return 90;
70 if (direction == SocketDirection::South) return 180;
71 if (direction == SocketDirection::West) return 270;
72 return 0;
73}
74
75std::vector<uint8_t> footprintMask(const std::string &shape, int width, int depth, int inset) {
76 std::vector<uint8_t> mask(size_t(width * depth), 0);
77 const int minX = inset, minY = inset, maxX = width - 1 - inset, maxY = depth - 1 - inset;
78 if (minX > maxX || minY > maxY) return mask;
79 const int spanX = maxX - minX + 1, spanY = maxY - minY + 1;
80 const int cutX = minX + std::max(1, spanX / 2) - 1;
81 const int cutY = minY + std::max(1, spanY / 2) - 1;
82 const int stemInset = spanX >= 5 ? 1 : 0;
83 for (int y = minY; y <= maxY; ++y)
84 for (int x = minX; x <= maxX; ++x) {
85 bool active = true;
86 if (shape == "l_shape")
87 active = !(x > cutX && y > cutY);
88 else if (shape == "t_shape")
89 active = y <= cutY || (x >= minX + stemInset && x <= maxX - stemInset);
90 if (active) mask[size_t(y * width + x)] = 1;
91 }
92 return mask;
93}
94
95bool active(const std::vector<uint8_t> &mask, int width, int depth, int x, int y) {
96 return x >= 0 && y >= 0 && x < width && y < depth && mask[size_t(y * width + x)] != 0;
97}
98
100bool pointInPolygon(float px, float py, const std::vector<HousePolygonPoint> &poly) {
101 bool inside = false;
102 for (size_t i = 0, j = poly.size() - 1; i < poly.size(); j = i++) {
103 const float xi = poly[i].x, yi = poly[i].y;
104 const float xj = poly[j].x, yj = poly[j].y;
105 if (((yi > py) != (yj > py)) && (px < (xj - xi) * (py - yi) / (yj - yi + 1e-12f) + xi)) inside = !inside;
106 }
107 return inside;
108}
109
111std::vector<uint8_t> polygonMask(const std::vector<HousePolygonPoint> &poly, int width, int depth, int inset) {
112 std::vector<uint8_t> mask(size_t(width * depth), 0);
113 for (int y = inset; y < depth - inset; ++y)
114 for (int x = inset; x < width - inset; ++x)
115 if (pointInPolygon(float(x) + 0.5f, float(y) + 0.5f, poly)) mask[size_t(y * width + x)] = 1;
116 return mask;
117}
118
121 int side = direction == SocketDirection::North ? 0
124 : 3;
125 side = (side + degrees / 90) % 4;
126 return side == 0 ? SocketDirection::North
127 : side == 1 ? SocketDirection::East
128 : side == 2 ? SocketDirection::South
130}
131
132std::vector<std::reference_wrapper<const HouseComponent>> compatibleOnFace(
133 const std::vector<std::reference_wrapper<const HouseComponent>> &choices, SocketDirection face, int rotation) {
134 std::vector<std::reference_wrapper<const HouseComponent>> out;
135 for (const auto &choice : choices) {
136 const auto &c = choice.get();
137 if (!isRotationAllowed(c, rotation)) continue;
138 // Components without sockets are intentionally wildcard-compatible. This preserves
139 // compatibility with small legacy kits while new player kits opt into strict sockets.
140 if (c.sockets.empty()) {
141 out.push_back(choice);
142 continue;
143 }
144 for (const auto &socket : c.sockets) {
145 if (rotate(socket.direction, rotation) == face && !socket.type.empty()) {
146 out.push_back(choice);
147 break;
148 }
149 }
150 }
151 return out;
152}
153
155std::optional<std::pair<int, int>> findInteriorCell(const std::vector<uint8_t> &mask, int width, int depth) {
156 const int dx[] = {0, 1, 0, -1};
157 const int dy[] = {-1, 0, 1, 0};
158 for (int y = 1; y < depth - 1; ++y)
159 for (int x = 1; x < width - 1; ++x) {
160 if (!active(mask, width, depth, x, y)) continue;
161 bool interior = true;
162 for (int side = 0; side < 4; ++side)
163 interior = interior && active(mask, width, depth, x + dx[side], y + dy[side]);
164 if (interior) return std::pair<int, int>{x, y};
165 }
166 return std::nullopt;
167}
168
169void appendRoomRegions(int width, int depth, const std::vector<uint8_t> &mask, const std::string &roomType, int floorZ,
170 std::vector<HouseRoom> &rooms) {
171 std::vector<uint8_t> claimed(mask.size(), 0);
172 for (int y = 0; y < depth; ++y) {
173 for (int x = 0; x < width; ++x) {
174 const size_t origin = size_t(y * width + x);
175 if (!mask[origin] || claimed[origin]) continue;
176 int x1 = x;
177 while (x1 + 1 < width && mask[size_t(y * width + x1 + 1)] && !claimed[size_t(y * width + x1 + 1)]) ++x1;
178 int y1 = y;
179 bool extend = true;
180 while (extend && y1 + 1 < depth) {
181 for (int xx = x; xx <= x1; ++xx) {
182 if (!mask[size_t((y1 + 1) * width + xx)] || claimed[size_t((y1 + 1) * width + xx)]) {
183 extend = false;
184 break;
185 }
186 }
187 if (extend) ++y1;
188 }
189 rooms.push_back({roomType, x, y, floorZ, x1 - x + 1, y1 - y + 1});
190 for (int yy = y; yy <= y1; ++yy)
191 for (int xx = x; xx <= x1; ++xx) claimed[size_t(yy * width + xx)] = 1;
192 }
193 }
194}
195
197eve::Result<void> partitionFloor(int width, int depth, const std::vector<uint8_t> &mask,
198 const std::vector<std::string> &roomTypes, std::mt19937 &rng,
199 const std::vector<std::reference_wrapper<const HouseComponent>> &innerWall,
200 const std::vector<std::reference_wrapper<const HouseComponent>> &innerDoor,
201 std::vector<HouseInstance> &instances, std::vector<HouseRoom> &rooms, int floorZ) {
202 struct Rect {
203 int x0, y0, x1, y1;
204 int area() const { return (x1 - x0 + 1) * (y1 - y0 + 1); }
205 };
206 int activeArea = 0;
207 for (const uint8_t cell : mask) activeArea += cell != 0 ? 1 : 0;
208 const int target = std::max(1, int(roomTypes.size()));
209 if (target > std::max(1, activeArea / 3))
211 "required room count cannot fit the active footprint",
212 "requiredRooms", {}, "housegen.generate"));
213 if (target <= 1) return eve::Result<void>::success();
214
215 // Greedily decompose the mask into non-overlapping, fully active rectangles.
216 std::vector<uint8_t> claimed(mask.size(), 0);
217 std::vector<Rect> rects;
218 for (int y = 0; y < depth; ++y) {
219 for (int x = 0; x < width; ++x) {
220 const size_t origin = size_t(y * width + x);
221 if (!mask[origin] || claimed[origin]) continue;
222 int x1 = x;
223 while (x1 + 1 < width && mask[size_t(y * width + x1 + 1)] && !claimed[size_t(y * width + x1 + 1)]) ++x1;
224 int y1 = y;
225 bool extend = true;
226 while (extend && y1 + 1 < depth) {
227 for (int xx = x; xx <= x1; ++xx)
228 if (!mask[size_t((y1 + 1) * width + xx)] || claimed[size_t((y1 + 1) * width + xx)]) {
229 extend = false;
230 break;
231 }
232 if (extend) ++y1;
233 }
234 rects.push_back({x, y, x1, y1});
235 for (int yy = y; yy <= y1; ++yy)
236 for (int xx = x; xx <= x1; ++xx) claimed[size_t(yy * width + xx)] = 1;
237 }
238 }
239 if (int(rects.size()) > target)
242 "requested room count cannot represent the irregular footprint without outside cells", "requiredRooms", {},
243 "housegen.generate"));
244
245 while (int(rects.size()) < target) {
246 auto splitIt = std::max_element(rects.begin(), rects.end(),
247 [](const Rect &a, const Rect &b) { return a.area() < b.area(); });
248 if (splitIt == rects.end() || splitIt->area() < 2)
250 eve::DiagnosticCode::Unsupported, "required room count cannot fit the active footprint",
251 "requiredRooms", {}, "housegen.generate"));
252 const Rect source = *splitIt;
253 rects.erase(splitIt);
254 if ((source.x1 - source.x0) >= (source.y1 - source.y0) && source.x0 < source.x1) {
255 const int cut = source.x0 + (source.x1 - source.x0) / 2;
256 rects.push_back({source.x0, source.y0, cut, source.y1});
257 rects.push_back({cut + 1, source.y0, source.x1, source.y1});
258 } else {
259 const int cut = source.y0 + (source.y1 - source.y0) / 2;
260 rects.push_back({source.x0, source.y0, source.x1, cut});
261 rects.push_back({source.x0, cut + 1, source.x1, source.y1});
262 }
263 }
264 std::sort(rects.begin(), rects.end(),
265 [](const Rect &a, const Rect &b) { return std::tie(a.y0, a.x0) < std::tie(b.y0, b.x0); });
266
267 std::vector<int> roomAt(size_t(width * depth), -1);
268 for (size_t i = 0; i < rects.size(); ++i) {
269 const Rect &rect = rects[i];
270 rooms.push_back({roomTypes[i], rect.x0, rect.y0, floorZ, rect.x1 - rect.x0 + 1, rect.y1 - rect.y0 + 1});
271 for (int y = rect.y0; y <= rect.y1; ++y)
272 for (int x = rect.x0; x <= rect.x1; ++x) roomAt[size_t(y * width + x)] = int(i);
273 }
274
275 const auto choose = [&](const auto &components, int rotation) -> const HouseComponent * {
276 std::vector<std::reference_wrapper<const HouseComponent>> allowed;
277 for (const auto &component : components)
278 if (isRotationAllowed(component.get(), rotation)) allowed.push_back(component);
279 return pick(allowed, rng);
280 };
281 std::unordered_set<std::string> doorBoundaries;
282 const auto emitBoundary = [&](int x, int y, int rotation, int roomA, int roomB) -> eve::Result<void> {
283 const int lo = std::min(roomA, roomB), hi = std::max(roomA, roomB);
284 const std::string boundary = std::to_string(lo) + ":" + std::to_string(hi) + ":" + std::to_string(rotation);
285 const bool door = doorBoundaries.insert(boundary).second;
286 const HouseComponent *component = choose(door ? innerDoor : innerWall, rotation);
287 if (!component)
290 std::string("no interior ") + (door ? "door" : "wall") +
291 " component allows rotation " + std::to_string(rotation),
292 "rotations", {}, "housegen.generate"));
293 instances.push_back({component->id, x, y, floorZ, rotation});
295 };
296
297 for (int y = 0; y < depth; ++y) {
298 for (int x = 0; x < width; ++x) {
299 const int room = roomAt[size_t(y * width + x)];
300 if (room < 0) continue;
301 if (x + 1 < width) {
302 const int neighbour = roomAt[size_t(y * width + x + 1)];
303 if (neighbour >= 0 && neighbour != room) {
304 auto emitted = emitBoundary(x + 1, y, 90, room, neighbour);
305 if (!emitted.ok()) return emitted;
306 }
307 }
308 if (y + 1 < depth) {
309 const int neighbour = roomAt[size_t((y + 1) * width + x)];
310 if (neighbour >= 0 && neighbour != room) {
311 auto emitted = emitBoundary(x, y + 1, 0, room, neighbour);
312 if (!emitted.ok()) return emitted;
313 }
314 }
315 }
316 }
318}
319
320
321} // namespace eve::housegen::house_detail
double y
double x
LogicalId target
bool & active
int mask
float degrees
Definition CardTypes.cpp:38
std::string name
Definition ECS.cpp:154
ShaderImageInput shape
float area
Definition Grass.cpp:62
std::int32_t c
std::vector< Colorf > px
std::uint32_t width
std::array< float, 4 > rotation
std::set< std::string > choices
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 origin
glm::dvec3 dx
std::set< std::string > values
Object::Ptr value
RoadLaneDirection direction
ScriptStructure out
std::string_view source
float dy
Definition Shadow.cpp:151
const SquirrelValueOptions & options
Cell cell
CommandLogBoundary boundary
Anchor rule, see above.
std::uint32_t depth
std::vector< char > inside
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
std::string directionName(SocketDirection direction)
std::vector< uint8_t > polygonMask(const std::vector< HousePolygonPoint > &poly, int width, int depth, int inset)
Rasterize an arbitrary closed polygon onto the cell grid (cell centers inside).
void appendRoomRegions(int width, int depth, const std::vector< uint8_t > &mask, const std::string &roomType, int floorZ, std::vector< HouseRoom > &rooms)
bool isOneOf(const std::string &value, std::initializer_list< const char * > options)
SocketDirection directionFromName(const std::string &name)
SocketDirection rotate(SocketDirection direction, int degrees)
std::optional< std::pair< int, int > > findInteriorCell(const std::vector< uint8_t > &mask, int width, int depth)
First active cell that is interior (all four orthogonal neighbours active).
std::vector< std::reference_wrapper< const HouseComponent > > compatibleOnFace(const std::vector< std::reference_wrapper< const HouseComponent > > &choices, SocketDirection face, int rotation)
Internal compatibleOnFace stage helper. @cost Component choices times sockets; used for one facade pl...
int directionRotation(SocketDirection direction)
bool pointInPolygon(float px, float py, const std::vector< HousePolygonPoint > &poly)
Ray-casting point-in-polygon test in corner coordinates.
eve::Result< void > partitionFloor(int width, int depth, const std::vector< uint8_t > &mask, const std::vector< std::string > &roomTypes, std::mt19937 &rng, const std::vector< std::reference_wrapper< const HouseComponent > > &innerWall, const std::vector< std::reference_wrapper< const HouseComponent > > &innerDoor, std::vector< HouseInstance > &instances, std::vector< HouseRoom > &rooms, int floorZ)
Partition active cells into exact, rectangular rooms and emit mask-safe boundaries.
bool has(const std::vector< std::reference_wrapper< const HouseComponent > > &values)
bool isWindowComponent(const HouseComponent *component)
std::vector< uint8_t > footprintMask(const std::string &shape, int width, int depth, int inset)
Internal footprintMask stage helper. @cost Linear in plot cells; reuse for one floor emission.
bool isRotationAllowed(const HouseComponent &component, int rotation)
const HouseComponent * pick(const std::vector< std::reference_wrapper< const HouseComponent > > &choices, std::mt19937 &rng)
Borrow a weighted component from library-backed choices. @ownership Borrowed; the session's library o...
std::vector< std::reference_wrapper< const HouseComponent > > facadeVariant(const std::vector< std::reference_wrapper< const HouseComponent > > &choices, bool wantWindow)
Internal facadeVariant stage helper. @cost Linear in component choices; amortized across one facade p...
SocketDirection
组件连接点方向(上下 + 四向)。
Axis-aligned integer rectangle (screen/tile space).
Definition Math.h:46
房屋组件模板(几何 + 连接点 + 权重)。
std::vector< int > rotations
允许的旋转(度)。
std::vector< std::string > tags
int weight
生成权重(越大越常被选中)。