载入中...
搜索中...
未找到
HouseGenerationFloor.cpp
浏览该文件的文档.
1#include <utility>
3
5eve::Result<void> State::floor(const procgen::Grid2D &grid, const std::vector<std::string> &roomTypes) {
6 const auto foundation = library_.byCategory("foundation", r.style);
7 const auto floor = library_.byCategory("floor", r.style);
8 const auto wall = library_.byCategory("wall", r.style);
9 const auto door = library_.byCategory("door", r.style);
10 const auto roof = library_.byCategory("roof", r.style);
11 const auto stairs = library_.byCategory("stairs", r.style);
12 const auto innerWall = library_.byCategory("interior_wall", r.style);
13 const auto innerDoor = library_.byCategory("interior_door", r.style);
14 const bool partitionsInteriors = roomTypes.size() > 1;
15 const auto entranceDirection = directionFromName(generated.entranceSide);
16 std::vector<uint8_t> mask(grid.cells().size());
17 std::transform(grid.cells().begin(), grid.cells().end(), mask.begin(),
18 [](auto cell) { return uint8_t(cell != 0); });
19 // Any lower-floor cell not covered by this floor becomes a roof terrace/canopy at the
20 // current level. Thus every floor cell is covered by either another floor or a roof.
21 if (!previousMask.empty()) {
22 for (int y = 0; y < r.depth; ++y)
23 for (int x = 0; x < r.width; ++x) {
24 if (active(previousMask, r.width, r.depth, x, y) && !active(mask, r.width, r.depth, x, y))
25 generated.instances.push_back({pick(roof, rng)->id, x, y, z, 0});
26 }
27 }
28 int minX = r.width, minY = r.depth, maxX = -1, maxY = -1;
29 for (int y = 0; y < r.depth; ++y)
30 for (int x = 0; x < r.width; ++x)
31 if (active(mask, r.width, r.depth, x, y)) {
32 minX = std::min(minX, x);
33 minY = std::min(minY, y);
34 maxX = std::max(maxX, x);
35 maxY = std::max(maxY, y);
36 // The stair column replaces foundation+floor on every level it passes through.
37 if (stairwell && x == stairwell->first && y == stairwell->second) {
38 generated.instances.push_back({pick(stairs, rng)->id, x, y, z, 0});
39 continue;
40 }
41 if (z == 0) generated.instances.push_back({pick(foundation, rng)->id, x, y, z, 0});
42 generated.instances.push_back({pick(floor, rng)->id, x, y, z, 0});
43 }
44 if (maxX < minX || maxY < minY)
46 eve::DiagnosticCode::Failed, "footprint collapsed after inset", {}, {}, "housegen.generate"));
47 if (!partitionsInteriors) appendRoomRegions(r.width, r.depth, mask, roomTypes.front(), z, generated.rooms);
48
49 using Face = std::tuple<int, int, SocketDirection>;
50 std::vector<Face> faces;
51 std::vector<Face> entranceCandidates;
52 std::unordered_map<int, int> facesPerCell;
53 for (int y = 0; y < r.depth; ++y)
54 for (int x = 0; x < r.width; ++x) {
55 if (!active(mask, r.width, r.depth, x, y)) continue;
58 const int dx[] = {0, 1, 0, -1};
59 const int dy[] = {-1, 0, 1, 0};
60 for (int side = 0; side < 4; ++side)
61 if (!active(mask, r.width, r.depth, x + dx[side], y + dy[side])) {
62 Face face{x, y, directions[side]};
63 faces.push_back(face);
64 ++facesPerCell[y * r.width + x];
65 if (z == 0 && directions[side] == entranceDirection) entranceCandidates.push_back(face);
66 }
67 }
68 std::sort(entranceCandidates.begin(), entranceCandidates.end(), [](const Face &a, const Face &b) {
69 return std::tie(std::get<1>(a), std::get<0>(a)) < std::tie(std::get<1>(b), std::get<0>(b));
70 });
71 Face entranceFace{-1, -1, entranceDirection};
72 if (z == 0 && !entranceCandidates.empty()) entranceFace = entranceCandidates[entranceCandidates.size() / 2];
73 for (const auto &faceData : faces) {
74 const auto [x, y, face] = faceData;
75 const bool isEntrance = z == 0 && faceData == entranceFace;
76 const int rotation = directionRotation(face);
77 auto choices = compatibleOnFace(isEntrance ? door : wall, face, rotation);
78 if (!isEntrance) {
79 const bool corner = facesPerCell[y * r.width + x] > 1;
80 const bool besideEntrance =
81 z == 0 && std::abs(x - std::get<0>(entranceFace)) + std::abs(y - std::get<1>(entranceFace)) <= 1;
82 const int facadeAxis = (face == SocketDirection::North || face == SocketDirection::South) ? x : y;
83 const bool rhythmicWindow = ((facadeAxis + int(r.seed & 1u)) & 1) == 0;
84 choices = facadeVariant(choices, !corner && !besideEntrance && rhythmicWindow);
85 }
86 const auto *selected = pick(choices, rng);
87 if (!selected) {
90 "no socket-compatible " + std::string(isEntrance ? "door" : "wall") + " component", {}, {},
91 "housegen.generate"));
92 }
93 generated.instances.push_back({selected->id, x, y, z, rotation});
94 }
95 if (partitionsInteriors) {
96 auto partitioned = partitionFloor(r.width, r.depth, mask, roomTypes, rng, innerWall, innerDoor,
98 if (!partitioned.ok()) return eve::Result<void>::failure(partitioned.status());
99 }
100 if (z + 1 == r.floors) {
101 for (int y = 0; y < r.depth; ++y)
102 for (int x = 0; x < r.width; ++x)
103 if (active(mask, r.width, r.depth, x, y))
104 generated.instances.push_back({pick(roof, rng)->id, x, y, z + 1, 0});
105 }
106 previousMask = std::move(mask);
107 ++z;
108 planned.reset();
110}
111} // namespace eve::housegen::house_detail
double y
double x
bool & active
std::string selected
int mask
std::array< float, 4 > rotation
std::set< std::string > choices
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
glm::dvec3 dx
size_t directions
float dy
Definition Shadow.cpp:151
Cell cell
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::vector< std::reference_wrapper< const HouseComponent > > byCategory(std::string_view category, std::string_view style={}) const
按分类(可选风格)查询组件。
std::vector< HouseRoom > rooms
Definition HouseLayout.h:39
std::vector< HouseInstance > instances
组件实例 / 房间 / 诊断信息。
Definition HouseLayout.h:38
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
Definition Grid2D.h:37
void appendRoomRegions(int width, int depth, const std::vector< uint8_t > &mask, const std::string &roomType, int floorZ, std::vector< HouseRoom > &rooms)
SocketDirection directionFromName(const std::string &name)
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)
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.
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
组件连接点方向(上下 + 四向)。
int width
占地尺寸(格)与层数。
std::optional< std::pair< int, int > > stairwell
eve::Result< void > floor(const procgen::Grid2D &grid, const std::vector< std::string > &rooms)
std::optional< procgen::Grid2D > planned