载入中...
搜索中...
未找到
ArchSpaceImport.cpp
浏览该文件的文档.
2
4
5#include <cmath>
6#include <queue>
7#include <utility>
8#include <vector>
9
10namespace eve::archspace {
11namespace {
12
13constexpr double kEps = 1e-8;
14
16bool isFloor(ImportCellKind kind) { return kind == ImportCellKind::Floor; }
17
18} // namespace
19
20eve::Result<void> importCellGrid(Document& document, const std::string& levelId, const CellGridImport& grid,
21 const std::string& idPrefix) {
22 const Node* level = document.find(levelId);
23 if (!level || level->kind != NodeKind::Level)
25 "ArchSpace import requires an existing level",
26 "levelId", {}, "archspace.import"));
27 if (idPrefix.empty() || idPrefix.size() > 64)
29 "ArchSpace import idPrefix is invalid", "idPrefix", {},
30 "archspace.import"));
31 if (grid.width <= 0 || grid.height <= 0 || grid.width > 4096 || grid.height > 4096)
33 "ArchSpace import grid size is out of range", "size",
34 {}, "archspace.import"));
35 if (!std::isfinite(grid.cellSize) || grid.cellSize <= kEps || !std::isfinite(grid.originX) ||
36 !std::isfinite(grid.originZ))
38 "ArchSpace import cellSize/origin must be finite",
39 "cellSize", {}, "archspace.import"));
40 const std::size_t expected = static_cast<std::size_t>(grid.width) * static_cast<std::size_t>(grid.height);
41 if (grid.cells.size() != expected)
43 "ArchSpace import cells size must equal width*height",
44 "cells", {}, "archspace.import"));
45
46 Document candidate = document;
47 if (candidate.topology() == TopologyKind::Architectural) {
49 if (!topology.ok()) return topology;
50 }
51
52 const int W = grid.width;
53 const int H = grid.height;
54 std::vector<int> labels(expected, -1);
55 int regionCount = 0;
56 std::vector<std::string> regionIds;
57
58 auto indexOf = [W](int x, int z) { return z * W + x; };
59
60 for (int z = 0; z < H; ++z) {
61 for (int x = 0; x < W; ++x) {
62 const int idx = indexOf(x, z);
63 if (!isFloor(grid.cells[static_cast<std::size_t>(idx)]) || labels[static_cast<std::size_t>(idx)] >= 0)
64 continue;
65 const int label = regionCount++;
66 int minX = x, maxX = x, minZ = z, maxZ = z;
67 std::queue<std::pair<int, int>> q;
68 q.push({x, z});
69 labels[static_cast<std::size_t>(idx)] = label;
70 while (!q.empty()) {
71 const auto [cx, cz] = q.front();
72 q.pop();
73 minX = std::min(minX, cx);
74 maxX = std::max(maxX, cx);
75 minZ = std::min(minZ, cz);
76 maxZ = std::max(maxZ, cz);
77 static constexpr int kDx[4] = {1, -1, 0, 0};
78 static constexpr int kDz[4] = {0, 0, 1, -1};
79 for (int d = 0; d < 4; ++d) {
80 const int nx = cx + kDx[d];
81 const int nz = cz + kDz[d];
82 if (nx < 0 || nz < 0 || nx >= W || nz >= H) continue;
83 const int nidx = indexOf(nx, nz);
84 if (labels[static_cast<std::size_t>(nidx)] >= 0) continue;
85 if (!isFloor(grid.cells[static_cast<std::size_t>(nidx)])) continue;
86 labels[static_cast<std::size_t>(nidx)] = label;
87 q.push({nx, nz});
88 }
89 }
90 const std::string regionId = idPrefix + ".region." + std::to_string(label);
91 regionIds.push_back(regionId);
92 const double x0 = grid.originX + static_cast<double>(minX) * grid.cellSize;
93 const double z0 = grid.originZ + static_cast<double>(minZ) * grid.cellSize;
94 const double x1 = grid.originX + static_cast<double>(maxX + 1) * grid.cellSize;
95 const double z1 = grid.originZ + static_cast<double>(maxZ + 1) * grid.cellSize;
96 auto created =
97 candidate.createRegion(levelId, regionId, regionId, {{x0, z0}, {x1, z0}, {x1, z1}, {x0, z1}});
98 if (!created.ok()) return created;
99 }
100 }
101
102 int portalCount = 0;
103 for (int z = 0; z < H; ++z) {
104 for (int x = 0; x < W; ++x) {
105 const int idx = indexOf(x, z);
106 if (grid.cells[static_cast<std::size_t>(idx)] != ImportCellKind::Portal) continue;
107 int first = -1;
108 int second = -1;
109 static constexpr int kDx[4] = {1, -1, 0, 0};
110 static constexpr int kDz[4] = {0, 0, 1, -1};
111 for (int d = 0; d < 4; ++d) {
112 const int nx = x + kDx[d];
113 const int nz = z + kDz[d];
114 if (nx < 0 || nz < 0 || nx >= W || nz >= H) continue;
115 const int nlabel = labels[static_cast<std::size_t>(indexOf(nx, nz))];
116 if (nlabel < 0) continue;
117 if (first < 0) {
118 first = nlabel;
119 } else if (nlabel != first && second < 0) {
120 second = nlabel;
121 }
122 }
123 if (first < 0 || second < 0) continue;
124 const int a = std::min(first, second);
125 const int b = std::max(first, second);
126 const std::string portalId = idPrefix + ".portal." + std::to_string(a) + "." + std::to_string(b) + "." +
127 std::to_string(portalCount++);
128 const Vec3 position{grid.originX + (static_cast<double>(x) + 0.5) * grid.cellSize, 0.0,
129 grid.originZ + (static_cast<double>(z) + 0.5) * grid.cellSize};
130 auto created = candidate.createPortal(levelId, portalId, portalId, regionIds[static_cast<std::size_t>(a)],
131 regionIds[static_cast<std::size_t>(b)], kPassageWalk, position);
132 if (!created.ok()) return created;
133 }
134 }
135
136 document = std::move(candidate);
138}
139
140} // namespace eve::archspace
double z
double x
float cx
Definition CardTypes.cpp:36
std::array< double, 10 > q
std::int32_t regionCount
std::int32_t second
std::int32_t first
std::array< float, 3 > position
std::string label
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
Topology topology
int idx
float d
int created
std::string kind
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
static Status success(StatusCode code=StatusCode::Ok)
Construct a successful status with an explicit non-error outcome.
Definition Status.cpp:19
Authoritative ArchSpace document (Pascal-like node graph).
eve::Result< void > createRegion(const std::string &levelId, const std::string &regionId, std::string regionName, std::vector< Vec2 > polygon)
Create a Region (Zone) without perimeter walls — dungeon rooms / cave chambers.
const Node * find(const std::string &id) const
Borrow a node by id. @ownership Non-owning pointer into this document's node map; do not free....
eve::Result< void > createPortal(const std::string &levelId, const std::string &portalId, std::string portalName, std::string regionAId, std::string regionBId, PassageMask passageMask, Vec3 position)
Create a level-hosted portal connecting two regions.
eve::Result< void > setTopology(TopologyKind topology)
Set topology dialect.
TopologyKind topology() const noexcept
Topology dialect for authoring helpers and SpaceQuery.
ImportCellKind
Raster cell kinds accepted by dungeon/cave grid import (procgen-facing).
eve::Result< void > importCellGrid(Document &document, const std::string &levelId, const CellGridImport &grid, const std::string &idPrefix)
Import a dungeon/cave occupancy grid into an existing level.
constexpr PassageMask kPassageWalk
Default walkable mask.
Flat row-major cell grid in level XZ (x grows with column, z with row).
One authored architectural node. Only fields for the active kind are meaningful.
Metres in world/level space.