载入中...
搜索中...
未找到
HouseGeneration.cpp
浏览该文件的文档.
2
4
5namespace eve::housegen {
6namespace {
7Result<void> invalidStage(std::string message) {
9 Diagnostic::error(DiagnosticCode::InvalidArgument, std::move(message), {}, {}, "housegen.stages"));
10}
11} // namespace
12
15HouseGeneration::HouseGeneration(HouseGeneration&&) noexcept = default;
16HouseGeneration& HouseGeneration::operator=(HouseGeneration&&) noexcept = default;
17
18Result<void> house_detail::State::plan() {
19 if (planned) return Result<void>::success();
20 if (!isPolygon && z > 0 && inset == 0 && r.width > 4 && r.depth > 4 && (rng() & 3u) == 0u) inset = 1;
21 auto mask = makeMask(inset);
22 if (!previousMask.empty()) {
23 for (size_t i = 0; i < mask.size(); ++i) mask[i] = uint8_t(mask[i] && previousMask[i]);
24 }
25 procgen::Grid2D grid;
26 grid.resize(r.width, r.depth);
27 for (int y = 0; y < r.depth; ++y)
28 for (int x = 0; x < r.width; ++x) grid.setCell(x, y, mask[size_t(y * r.width + x)]);
29 planned = std::move(grid);
30 return Result<void>::success();
31}
32
34 auto candidate = std::make_unique<house_detail::State>();
35 candidate->r = request;
36 candidate->library_ = library;
37 auto result = candidate->configure();
38 if (!result.ok()) return result;
39 state_ = std::move(candidate);
40 return Result<void>::success();
41}
42
43int HouseGeneration::floorCount() const noexcept { return state_ ? state_->z : 0; }
44
46 if (!state_ || state_->z >= state_->r.floors)
47 return Result<procgen::Grid2D>::failure(invalidStage("no next house floor").status());
48 auto result = state_->plan();
49 if (!result.ok()) return Result<procgen::Grid2D>::failure(result.status());
50 return Result<procgen::Grid2D>::success(*state_->planned);
51}
52
53Result<void> HouseGeneration::emitFloor(const procgen::Grid2D& grid, const std::vector<std::string>& rooms) {
54 if (!state_ || !state_->planned || state_->z >= state_->r.floors)
55 return invalidStage("prepare nextFloorGrid before emitting a floor");
56 const auto& r = state_->r;
57 if (grid.getWidth() != r.width || grid.getHeight() != r.depth || rooms.empty() ||
58 std::any_of(rooms.begin(), rooms.end(), [](const auto& name) { return name.empty(); }))
59 return invalidStage("floor grid dimensions and nonempty room types must match the request");
60 if (std::none_of(grid.cells().begin(), grid.cells().end(), [](auto cell) { return cell != 0; }))
61 return invalidStage("house floor must contain occupied cells");
62 if (!state_->previousMask.empty()) {
63 for (size_t i = 0; i < grid.cells().size(); ++i)
64 if (grid.cells()[i] && !state_->previousMask[i]) return invalidStage("upper floor lacks direct support");
65 }
66 if (rooms.size() > 1 && (state_->library_.byCategory("interior_wall", r.style).empty() ||
67 state_->library_.byCategory("interior_door", r.style).empty()))
68 return invalidStage("multi-room floor requires interior wall and door components");
69 auto candidate = std::make_unique<house_detail::State>(*state_);
70 if (candidate->z == 0) {
71 std::transform(grid.cells().begin(), grid.cells().end(), candidate->baseMask.begin(),
72 [](auto cell) { return uint8_t(cell != 0); });
73 if (candidate->stairwell) {
74 candidate->stairwell = house_detail::findInteriorCell(candidate->baseMask, r.width, r.depth);
75 if (!candidate->stairwell) return invalidStage("edited floor has no interior stair cell");
76 }
77 } else if (candidate->stairwell) {
78 auto [x, y] = *candidate->stairwell;
79 if (!grid.getCell(x, y)) return invalidStage("upper floor removes the stair column");
80 }
81 auto result = candidate->floor(grid, rooms);
82 if (!result.ok()) return result;
83 state_ = std::move(candidate);
84 return Result<void>::success();
85}
86
88 if (!state_ || state_->z != state_->r.floors)
89 return Result<HouseLayout>::failure(invalidStage("emit all requested floors before finishing").status());
90 auto candidate = std::make_unique<house_detail::State>(*state_);
91 auto result = candidate->finish();
92 if (!result.ok()) return Result<HouseLayout>::failure(result.status());
93 return Result<HouseLayout>::success(std::move(candidate->generated));
94}
95} // namespace eve::housegen
double z
double y
double x
int mask
std::string name
Definition ECS.cpp:154
std::string message
const GltfImportRequest & request
wgpu::PopErrorScopeStatus status
float u
Definition Grass.cpp:233
double r
Poco::JSON::Object::Ptr result
Cell cell
float size
Definition TreeMesh.cpp:158
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
房屋组件注册表(按 id 索引,支持分类/风格查询)。
Result< void > emitFloor(const procgen::Grid2D &grid, const std::vector< std::string > &roomTypes)
Emit a caller-edited, same-size floor with caller-selected room types. Upper floors must be supported...
Result< procgen::Grid2D > nextFloorGrid()
Prepare the next default mask and return an independent GridGraph-compatible copy....
Result< HouseLayout > finish() const
Validate and return an independent complete layout; never publishes ECS entities. @cost Linear in emi...
Result< void > configure(const HouseRequest &request, const HouseComponentLibrary &library)
Validate the request and copy the library; success resets all intermediate floors....
~HouseGeneration()
Release staging data without publishing entities.
HouseGeneration()
Construct an inexpensive empty session.
int floorCount() const noexcept
Number of successfully emitted floors; zero for empty or moved-from sessions.
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
Definition Grid2D.h:37
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).
一次房屋生成请求(参数集)。