9#include <unordered_set>
39constexpr int kDecorBase = 100;
40constexpr int kFloorMaxVariant = 32;
43constexpr int kE = 1 << 0;
44constexpr int kS = 1 << 1;
45constexpr int kW = 1 << 2;
46constexpr int kN = 1 << 3;
47constexpr int kSE = 1 << 4;
48constexpr int kSW = 1 << 5;
49constexpr int kNW = 1 << 6;
50constexpr int kNE = 1 << 7;
61int clampInt(
int v,
int lo,
int hi) {
return std::max(lo, std::min(hi,
v)); }
63std::vector<std::string> assetPool(
const Params &
params,
const std::string &role,
const std::string &fallback) {
64 std::vector<std::string>
result;
65 std::stringstream
input(
params.getString(
"assets." + role, fallback));
67 while (std::getline(
input, item,
',')) {
68 const auto first = item.find_first_not_of(
" \t");
69 const auto last = item.find_last_not_of(
" \t");
75std::string pickAsset(
const std::vector<std::string> &pool, std::mt19937 &rng) {
76 if (pool.empty())
return {};
77 return pool[size_t(rng()) % pool.size()];
81void autotileWalls(Grid2D &
out) {
82 const int w =
out.getWidth();
83 const int h =
out.getHeight();
84 for (
int y = 0;
y <
h; ++
y) {
85 for (
int x = 0;
x <
w; ++
x) {
88 if (
x + 1 <
w && isWalkable(uint32_t(
out.getCell(
x + 1,
y))))
mask |= kE;
89 if (
y + 1 <
h && isWalkable(uint32_t(
out.getCell(
x,
y + 1))))
mask |= kS;
90 if (
x - 1 >= 0 && isWalkable(uint32_t(
out.getCell(
x - 1,
y))))
mask |= kW;
91 if (
y - 1 >= 0 && isWalkable(uint32_t(
out.getCell(
x,
y - 1))))
mask |= kN;
92 if (
x + 1 <
w &&
y + 1 <
h && isWalkable(uint32_t(
out.getCell(
x + 1,
y + 1))))
mask |= kSE;
93 if (
x - 1 >= 0 &&
y + 1 <
h && isWalkable(uint32_t(
out.getCell(
x - 1,
y + 1))))
mask |= kSW;
94 if (
x - 1 >= 0 &&
y - 1 >= 0 && isWalkable(uint32_t(
out.getCell(
x - 1,
y - 1))))
mask |= kNW;
95 if (
x + 1 <
w &&
y - 1 >= 0 && isWalkable(uint32_t(
out.getCell(
x + 1,
y - 1))))
mask |= kNE;
102int floorVariant(
int x,
int y,
const std::string &
pattern,
int variants, uint32_t salt) {
103 const int v = std::max(1, variants);
106 return 1 + ((
x +
y) %
v);
110 return 1 + (int((
x * 73856093u +
y * 19349663u + salt) & 0x7FFFFFFFu) %
v);
114void carveRect(Grid2D &
out,
const Rect &
r, uint32_t
semantic) {
115 for (
int y =
r.y;
y <
r.y +
r.h; ++
y)
119bool rectsOverlap(
const Rect &
a,
const Rect &
b,
int pad) {
123void carveCorridor(Grid2D &
out,
int ax,
int ay,
int bx,
int by,
int width,
const std::string &style,
125 auto carve = [&](
int x,
int y) {
128 if (style ==
"diagonal") {
131 while (
x != bx ||
y != by) {
134 if (
x != bx &&
y != by && (rng() & 1u)) {
135 x += (bx >
x) ? 1 : -1;
136 }
else if (
x != bx) {
137 x += (bx >
x) ? 1 : -1;
139 y += (by >
y) ? 1 : -1;
150 if (style ==
"straight") {
152 }
else if (rng() & 1u) {
157 const int x0 = std::min(ax, bx);
158 const int x1 = std::max(ax, bx);
159 for (
int x = x0;
x <= x1; ++
x)
162 const int y0 = std::min(ay, by);
163 const int y1 = std::max(ay, by);
164 for (
int y = y0;
y <= y1; ++
y)
168bool genRoguelike(
const Params &
params, Grid2D &
out, std::string &
error) {
169 const int w =
params.getWidth();
170 const int h =
params.getHeight();
171 if (
w < 9 ||
h < 9) {
172 error =
"level.roguelike: size must be at least 9x9";
177 const int roomCount = clampInt(
params.getInt(
"roomCount", 9), 1, 256);
178 const int roomMin = clampInt(
params.getInt(
"roomMin", 4), 2,
w);
179 const int roomMax = clampInt(
params.getInt(
"roomMax", 8), roomMin,
w);
180 const int padding = clampInt(
params.getInt(
"padding", 1), 0, 4);
181 const int spacing = clampInt(
params.getInt(
"spacing", 2), 0, 8);
182 const int clusterGapMin = clampInt(
params.getInt(
"clusterGapMin",
spacing + 1), 1, 8);
183 const int clusterGapMax = clampInt(
params.getInt(
"clusterGapMax",
spacing + 3), clusterGapMin, 12);
184 const int clusterBranchBias = clampInt(
params.getInt(
"clusterBranchBias", 0), 0, 4);
185 const int corridorW = clampInt(
params.getInt(
"corridorWidth", 1), 1, 3);
186 const int stairCount = clampInt(
params.getInt(
"stairCount", 1), 0, 4);
187 const int stairSideMask = clampInt(
params.getInt(
"stairSideMask", 15), 1, 15);
188 const std::string
layout =
params.getString(
"layoutStyle",
"grid");
189 const std::string style =
params.getString(
"corridorStyle",
"l");
190 const std::string connections =
params.getString(
"connectionStyle",
"sequential");
191 const std::string
pattern =
params.getString(
"floorPattern",
"brick");
192 const int variants = clampInt(
params.getInt(
"floorVariants", 4), 1, kFloorMaxVariant);
193 const float decorDensity = std::clamp(
params.getFloat(
"decorDensity", 0.05f), 0.f, 1.f);
194 const std::string decorSet =
params.getString(
"decorSet",
"mixed");
195 const bool doAutotile =
params.getInt(
"autotile", 1) != 0;
200 std::mt19937 rng(
seed);
205 const int cols = std::max(1,
int(std::sqrt(
double(roomCount))));
206 const int rows = std::max(1, (roomCount +
cols - 1) /
cols);
207 std::vector<Rect> rooms;
208 std::vector<size_t> roomParents;
209 std::uniform_int_distribution<int> dim(roomMin, roomMax);
210 std::uniform_int_distribution<int> jx(0, 0), jy(0, 0);
212 if (
layout ==
"clustered") {
213 const int firstW = std::min(dim(rng),
w - padding * 2);
214 const int firstH = std::min(dim(rng),
h - padding * 2);
215 rooms.push_back({(
w - firstW) / 2, (
h - firstH) / 2, firstW, firstH});
216 roomParents.push_back(0);
217 std::uniform_int_distribution<int> jitter(-1, 1);
218 std::uniform_int_distribution<int> clusterGap(clusterGapMin, clusterGapMax);
219 const int maxAttempts = roomCount * 160;
220 for (
int attempt = 0; attempt < maxAttempts && int(rooms.size()) < roomCount; ++attempt) {
221 size_t parentIndex = size_t(rng()) % rooms.size();
226 parentIndex = std::min(parentIndex,
size_t(rng()) % rooms.size());
227 const Rect &
parent = rooms[parentIndex];
228 const int rw = std::min(dim(rng),
w - padding * 2);
229 const int rh = std::min(dim(rng),
h - padding * 2);
230 const int gap = clusterGap(rng);
231 Rect cand{0, 0, rw, rh};
232 switch (rng() % 4u) {
235 cand.y =
parent.y +
parent.h / 2 - rh / 2 + jitter(rng);
239 cand.y =
parent.y +
parent.h / 2 - rh / 2 + jitter(rng);
242 cand.x =
parent.x +
parent.w / 2 - rw / 2 + jitter(rng);
246 cand.x =
parent.x +
parent.w / 2 - rw / 2 + jitter(rng);
250 if (cand.x < padding || cand.y < padding || cand.x + cand.w >
w - padding || cand.y + cand.h >
h - padding)
253 for (
const Rect &other : rooms) {
254 if (rectsOverlap(cand, other,
spacing)) {
260 rooms.push_back(cand);
261 roomParents.push_back(parentIndex);
265 for (
int r = 0;
r <
rows; ++
r) {
266 for (
int c = 0;
c <
cols; ++
c) {
267 if (
int(rooms.size()) >= roomCount)
break;
268 const int sx = padding + int((
long(
c) * (
w - 2 * padding)) /
cols);
269 const int ex = padding + int((
long(
c + 1) * (
w - 2 * padding)) /
cols);
270 const int sy = padding + int((
long(
r) * (
h - 2 * padding)) /
rows);
271 const int ey = padding + int((
long(
r + 1) * (
h - 2 * padding)) /
rows);
272 const int slotW = std::max(roomMin, ex -
sx);
273 const int slotH = std::max(roomMin, ey -
sy);
274 if (slotW < roomMin || slotH < roomMin)
continue;
275 for (
int attempt = 0; attempt < 24; ++attempt) {
276 const int rw = std::min(dim(rng), slotW);
277 const int rh = std::min(dim(rng), slotH);
278 std::uniform_int_distribution<int>
ox(
sx,
sx + slotW - rw);
279 std::uniform_int_distribution<int>
oy(
sy,
sy + slotH - rh);
280 const Rect cand{
ox(rng),
oy(rng), rw, rh};
282 for (
const Rect &other : rooms) {
283 if (rectsOverlap(cand, other,
spacing)) {
289 roomParents.push_back(rooms.empty() ? 0 : rooms.
size() - 1);
290 rooms.push_back(cand);
299 const int rw = std::max(roomMin,
w / 3);
300 const int rh = std::max(roomMin,
h / 3);
301 rooms.push_back({(
w - rw) / 2, (
h - rh) / 2, rw, rh});
302 roomParents.push_back(0);
306 for (
const Rect &
r : rooms) carveRect(
out,
r, Semantic::
Floor);
310 for (
size_t i = 1; i < rooms.size(); ++i) {
312 if (connections ==
"growth" && i < roomParents.size()) {
314 }
else if (connections ==
"nearest") {
315 int bestDistance =
w +
h + 1;
316 const int bx = rooms[i].x + rooms[i].w / 2;
317 const int by = rooms[i].y + rooms[i].h / 2;
318 for (
size_t candidate = 0; candidate < i; ++candidate) {
319 const int ax = rooms[candidate].x + rooms[candidate].w / 2;
320 const int ay = rooms[candidate].y + rooms[candidate].h / 2;
321 const int distance = std::abs(ax - bx) + std::abs(ay - by);
330 const int bx = rooms[i].x + rooms[i].w / 2;
331 const int by = rooms[i].y + rooms[i].h / 2;
332 carveCorridor(
out, ax, ay, bx, by, corridorW, style, rng);
336 if (doAutotile) autotileWalls(
out);
339 const uint32_t salt =
seed * 2654435761u;
340 for (
int y = 0;
y <
h; ++
y) {
341 for (
int x = 0;
x <
w; ++
x) {
342 const uint32_t
c = uint32_t(
out.getCell(
x,
y));
352 std::vector<std::pair<int, int>> floorCells;
353 floorCells.reserve(
size_t(
w *
h));
354 for (
int y = 1;
y <
h - 1; ++
y)
355 for (
int x = 1;
x <
w - 1; ++
x)
356 if (isWalkable(uint32_t(
out.getCell(
x,
y)))) floorCells.emplace_back(
x,
y);
358 std::vector<std::pair<int, int>> decorTiles;
359 if (!floorCells.empty() && decorDensity > 0.f) {
360 const size_t count = size_t(
float(floorCells.size()) * decorDensity);
361 std::uniform_int_distribution<size_t>
pick(0, floorCells.size() - 1);
362 std::uniform_int_distribution<int>
kind(0, 2);
363 for (
size_t i = 0; i <
count; ++i) {
364 const auto [
dx,
dy] = floorCells[
pick(rng)];
365 if (
out.getDetail(
dx,
dy) >= kDecorBase)
continue;
367 bool nearMarker =
false;
368 for (
int k = 0; k <
out.getObjectCount(); ++k) {
369 if (
int(
out.getObjectX(k)) ==
dx &&
int(
out.getObjectY(k)) ==
dy) {
374 if (nearMarker)
continue;
376 decorTiles.emplace_back(
dx,
dy);
383 const auto containers = assetPool(
params,
"container",
"container");
384 const auto treasure = assetPool(
params,
"treasure",
"treasure");
386 const auto tables = assetPool(
params,
"table",
"table");
387 const auto seating = assetPool(
params,
"seating",
"seat");
388 const auto beds = assetPool(
params,
"bed",
"bed");
389 const auto shelves = assetPool(
params,
"shelf",
"shelf");
390 const auto lights = assetPool(
params,
"light",
"light");
391 const auto wallLights = assetPool(
params,
"wallLight",
params.getString(
"assets.light",
"light"));
392 const auto wallShelves = assetPool(
params,
"wallShelf",
params.getString(
"assets.shelf",
"shelf"));
393 const auto banners = assetPool(
params,
"banner",
"banner");
394 const auto weapons = assetPool(
params,
"weapon",
"weapon_display");
395 const auto traps = assetPool(
params,
"trap",
"trap");
396 const auto food = assetPool(
params,
"food",
"food");
397 const auto tavern = assetPool(
params,
"tavern",
"tavern_prop");
398 const auto clutter = assetPool(
params,
"clutter",
"clutter");
399 const auto stairsAssets = assetPool(
params,
"stairs",
"stairs");
400 const float propDensity = std::clamp(
params.getFloat(
"propDensity", 0.16f), 0.f, 1.f);
401 const float corridorLightDensity = std::clamp(
params.getFloat(
"corridorLightDensity", 0.035f), 0.f, 0.25f);
403 int objectSerial = 0;
404 int minimumRoomProps = std::numeric_limits<int>::max();
405 auto cellKey = [
w](
int x,
int y) {
return y *
w +
x; };
406 auto place = [&](
const std::string &role,
const std::vector<std::string> &pool,
double x,
double y,
float rotation,
407 int flags,
float ow = 1.f,
float oh = 1.f) {
408 const int cellX = clampInt(
int(std::round(
x)), 0,
w - 1);
409 const int cellY = clampInt(
int(std::round(
y)), 0,
h - 1);
410 if (pool.empty() || !isWalkable(uint32_t(
out.getCell(cellX, cellY))))
return false;
411 if ((
flags & 1) &&
occupied.count(cellKey(cellX, cellY)))
return false;
412 out.addAssetObject(role + std::to_string(objectSerial++), role, pickAsset(pool, rng),
static_cast<float>(
x),
418 if (decorSet !=
"none") {
419 for (
size_t roomIndex = 0; roomIndex < rooms.size(); ++roomIndex) {
420 const Rect &
r = rooms[roomIndex];
421 if (
r.w < 3 ||
r.h < 3)
continue;
424 const int cx =
r.x +
r.w / 2,
cy =
r.y +
r.h / 2;
428 const int theme = roomIndex == 0 ? 2 : int((roomIndex +
seed) % 7);
429 static constexpr const char *kThemeNames[] = {
"storage",
"quarters",
"dining",
"armory",
430 "treasury",
"shrine",
"tavern"};
431 out.addAssetObject(
"room" + std::to_string(roomIndex),
"room", kThemeNames[
theme],
float(
r.x),
float(
r.y),
432 float(
r.w),
float(
r.h), 0.f, 32);
433 const int propsBeforeRoom = objectSerial;
436 place(
"light", wallLights,
left,
cy, 90.f, 2 | 4);
437 if (
r.w >= 6) place(
"light", wallLights,
right,
cy, 270.f, 2 | 4);
438 if ((rng() % 3u) != 0) place(
"banner", banners,
left,
cy - 1, 90.f, 2);
439 if (propDensity >= 0.45f &&
r.w >= 5 && (roomIndex % 2) == 0)
440 place(
"banner", banners,
cx + (
r.w >= 7 ? 1 : 0),
bottom, 0.f, 2);
441 if (propDensity >= 0.6f &&
r.w >= 6 && (roomIndex % 2) != 0)
442 place(
"light", wallLights,
cx,
bottom, 0.f, 2 | 4);
444 if (decorSet ==
"pillars") {
449 if (decorSet ==
"treasure" ||
theme == 4) {
450 place(
"container", containers,
right,
cy, 270.f, 1);
451 place(
"treasure", treasure,
right - 1,
cy, 0.f, 16);
452 place(
"treasure", treasure,
right - 1.35f,
cy + 0.45f, 35.f, 16);
453 place(
"treasure", treasure,
right - 0.65f,
cy - 0.45f, 320.f, 16);
456 }
else if (
theme == 0) {
457 place(
"container", containers,
right,
top + 1, 270.f, 1);
458 place(
"container", containers,
right,
bottom - 1, 270.f, 1);
459 place(
"container", containers,
right - 0.75f,
top + 1.35f, 245.f, 16);
460 place(
"container", containers,
right - 0.55f,
bottom - 1.25f, 300.f, 16);
461 place(
"shelf", wallShelves,
cx,
bottom, 0.f, 1 | 2);
462 if (
r.h >= 6) place(
"shelf", wallShelves,
left,
cy + 1, 90.f, 1 | 2);
463 if (
r.w >= 6) place(
"container", containers,
left + 2,
bottom, 0.f, 1);
464 place(
"clutter", clutter,
left + 1,
bottom, 0.f, 16);
465 }
else if (
theme == 1) {
466 place(
"bed", beds,
right,
cy, 270.f, 1, 1.f, 2.f);
467 if (
r.h >= 6) place(
"bed", beds,
right,
cy - 2, 270.f, 1, 1.f, 2.f);
468 place(
"container", containers,
right - 1,
top, 180.f, 1);
470 place(
"seating", seating,
left + 1,
bottom - 1, 45.f, 1);
471 }
else if (
theme == 2) {
472 place(
"table", tables,
cx,
cy, (rng() & 1u) ? 0.f : 90.f, 1, 2.f, 1.f);
473 place(
"seating", seating,
cx - 1,
cy, 270.f, 1);
474 place(
"seating", seating,
cx + 1,
cy, 90.f, 1);
475 place(
"seating", seating,
cx,
cy - 1, 0.f, 1);
476 place(
"seating", seating,
cx,
cy + 1, 180.f, 1);
477 place(
"food", food,
cx,
cy, 0.f, 16);
478 place(
"food", food,
cx - 0.38f,
cy + 0.18f, 25.f, 16);
479 place(
"food", food,
cx + 0.38f,
cy - 0.18f, 205.f, 16);
481 place(
"table", tables,
cx - 2,
cy, 0.f, 1, 2.f, 1.f);
482 place(
"seating", seating,
cx - 2,
cy - 1, 0.f, 1);
483 place(
"seating", seating,
cx - 2,
cy + 1, 180.f, 1);
484 place(
"food", food,
cx - 2,
cy, 70.f, 16);
486 }
else if (
theme == 3) {
487 place(
"weapon", weapons,
left,
cy, 90.f, 2);
488 place(
"weapon", weapons,
cx,
bottom, 0.f, 2);
491 place(
"container", containers,
right,
top + 1, 270.f, 1);
492 if ((rng() & 1u) != 0) place(
"trap", traps,
cx - 1,
cy, 0.f, 8);
493 }
else if (
theme == 5) {
494 place(
"table", tables,
cx,
cy, 0.f, 1, 2.f, 1.f);
495 place(
"light",
lights,
cx - 0.45f,
cy, 0.f, 4);
496 place(
"light",
lights,
cx + 0.45f,
cy, 0.f, 4);
497 place(
"food", food,
cx,
cy, 0.f, 16);
498 place(
"seating", seating,
cx,
bottom - 1, 180.f, 1);
500 place(
"seating", seating,
cx - 2,
bottom - 1, 180.f, 1);
501 place(
"seating", seating,
cx + 2,
bottom - 1, 180.f, 1);
504 place(
"tavern", tavern,
right,
cy, 270.f, 1);
505 place(
"table", tables,
cx,
cy, 0.f, 1, 2.f, 1.f);
506 place(
"seating", seating,
cx - 1,
cy, 270.f, 1);
507 place(
"seating", seating,
cx + 1,
cy, 90.f, 1);
508 place(
"seating", seating,
cx,
cy + 1, 180.f, 1);
509 place(
"food", food,
cx,
cy, 0.f, 16);
510 place(
"food", food,
cx - 0.4f,
cy, 20.f, 16);
511 place(
"food", food,
cx + 0.4f,
cy, 340.f, 16);
512 place(
"container", containers,
right,
bottom - 1, 270.f, 1);
513 place(
"tavern", tavern,
right - 0.7f,
bottom - 1.2f, 250.f, 16);
520 place(
"container", containers,
right,
bottom - 1, 270.f, 1);
521 place(
"container", containers,
right - 0.65f,
bottom - 1.3f, 245.f, 16);
522 if (
r.w >= 6 &&
theme != 2) place(
"seating", seating,
left + 1,
top + 1, 45.f, 1);
527 const int clutterAttempts = clampInt(
int(std::ceil(propDensity * 1.5f)), 0, 2);
528 const std::pair<int, int> clutterSpots[] = {
530 for (
int i = 0; i < clutterAttempts; ++i) {
531 const auto &spot = clutterSpots[(size_t(i) + roomIndex) % 4];
532 place(
"clutter", clutter, spot.first, spot.second,
float((i +
int(roomIndex)) % 4) * 90.f, 16);
538 if (propDensity >= 0.35f) {
541 const int roomArea =
r.w *
r.h;
542 const int targetProps = clampInt(4 +
int(std::ceil(propDensity *
float(roomArea) * 0.28f)), 4, 16);
543 const std::pair<int, int> fillerSpots[] = {{
left + 1,
top + 1}, {
right - 1,
top + 1},
546 for (
int attempt = 0; attempt < 24 && objectSerial - propsBeforeRoom < targetProps; ++attempt) {
547 const auto &spot = fillerSpots[(size_t(attempt) + roomIndex) % 6];
548 if ((attempt & 1) == 0)
549 place(
"container", containers, spot.first, spot.second,
550 float((attempt +
int(roomIndex)) % 4) * 90.f, 1);
552 place(
"container", containers,
float(spot.first) + 0.35f,
float(spot.second) - 0.25f,
553 float((attempt * 37) % 360), 16);
556 minimumRoomProps = std::min(minimumRoomProps, objectSerial - propsBeforeRoom);
562 const uint32_t lightThreshold = uint32_t(corridorLightDensity * 1000.f);
563 for (
int y = 1;
y <
h - 1; ++
y) {
564 for (
int x = 1;
x <
w - 1; ++
x) {
566 const uint32_t
hash = uint32_t(
x) * 73856093u ^ uint32_t(
y) * 19349663u ^
seed;
567 if ((
hash % 1000u) >= lightThreshold)
continue;
569 place(
"light", wallLights,
float(
x),
float(
y), 90.f, 2 | 4);
571 place(
"light", wallLights,
float(
x),
float(
y), 270.f, 2 | 4);
573 place(
"light", wallLights,
float(
x),
float(
y), 180.f, 2 | 4);
575 place(
"light", wallLights,
float(
x),
float(
y), 0.f, 2 | 4);
583 struct StairCandidate {
588 std::vector<StairCandidate> stairCandidates;
589 for (
const Rect &
r : rooms) {
590 for (
int x =
r.x + 1;
x <
r.x +
r.w - 1; ++
x) {
592 stairCandidates.push_back({
x,
r.y, 180.f, 1});
594 stairCandidates.push_back({
x,
r.y +
r.h - 1, 0.f, 2});
596 for (
int y =
r.y + 1;
y <
r.y +
r.h - 1; ++
y) {
598 stairCandidates.push_back({
r.x,
y, 270.f, 4});
600 stairCandidates.push_back({
r.x +
r.w - 1,
y, 90.f, 8});
603 std::vector<size_t> selectedStairs;
604 for (
int entrance = 0; entrance < stairCount && selectedStairs.size() < stairCandidates.size(); ++entrance) {
605 size_t best = size_t(
seed) % stairCandidates.size();
606 int bestDistance = -1;
607 if (!selectedStairs.empty()) {
608 for (
size_t candidate = 0; candidate < stairCandidates.size(); ++candidate) {
609 bool duplicate =
false;
611 for (
size_t chosen : selectedStairs) {
612 if (stairCandidates[candidate].
x == stairCandidates[chosen].
x &&
613 stairCandidates[candidate].
y == stairCandidates[chosen].
y) {
617 nearest = std::min(nearest, std::abs(stairCandidates[candidate].
x - stairCandidates[chosen].
x) +
618 std::abs(stairCandidates[candidate].
y - stairCandidates[chosen].
y));
620 if (!duplicate && nearest > bestDistance) {
621 bestDistance = nearest;
625 if (bestDistance < 0)
break;
627 selectedStairs.push_back(
best);
628 const StairCandidate &stairs = stairCandidates[
best];
629 out.addAssetObject(
"stairs" + std::to_string(entrance),
"stairs", pickAsset(stairsAssets, rng),
float(stairs.x),
630 float(stairs.y), 1.f, 1.f, stairs.rotation, 64);
631 occupied.insert(cellKey(stairs.x, stairs.y));
634 std::vector<std::pair<int, int>> walkable;
635 for (
int y = 0;
y <
h; ++
y)
636 for (
int x = 0;
x <
w; ++
x)
637 if (isWalkable(uint32_t(
out.getCell(
x,
y))) && !
occupied.count(cellKey(
x,
y))) walkable.emplace_back(
x,
y);
638 if (!walkable.empty()) {
639 auto pick = [&](uint32_t
s) {
return walkable[(
seed * 1664525u +
s * 1013904223u) % walkable.size()]; };
641 out.addObjectAt(
"spawn",
"spawn",
float(
a.first),
float(
a.second));
645 out.setMeta(
"algorithm",
"level.roguelike");
646 out.setMeta(
"seed", std::to_string(
seed));
647 out.setMeta(
"rooms", std::to_string(rooms.size()));
649 out.setMeta(
"decorTiles", std::to_string(decorTiles.size()));
650 out.setMeta(
"corridorStyle", style);
652 out.setMeta(
"connectionStyle", connections);
653 out.setMeta(
"assetPack",
params.getString(
"assetPack",
"semantic-default"));
654 out.setMeta(
"placedProps", std::to_string(objectSerial));
655 out.setMeta(
"minimumRoomProps",
656 std::to_string(minimumRoomProps == std::numeric_limits<int>::max() ? 0 : minimumRoomProps));
657 out.setMeta(
"stairs", std::to_string(selectedStairs.size()));
660 static const char *architectureRoles[] = {
661 "wall",
"wallCorner",
"wallJunction",
"wallDoor",
"wallWindow",
"wallHalf",
"wallBroken",
662 "wallScaffold",
"floor",
"floorBroken",
"floorDirt",
"floorWood",
"floorGrate",
"floorFoundation",
663 "ceiling",
"stairs",
"stairsRail",
"door",
"barrier"};
664 for (
const char *role : architectureRoles) {
665 const std::string
key = std::string(
"assets.") + role;
674 auto descriptor = GeneratorDescriptor::grid(
"level.roguelike",
"Roguelike Level",
"Dungeon", 9, 9);
675 descriptor.params.push_back(ParamDescriptor::integer(
"roomCount",
"Room Count", 9, 1, 256));
676 descriptor.params.push_back(ParamDescriptor::integer(
"roomMin",
"Minimum Room Size", 4, 2, 128));
677 descriptor.params.push_back(ParamDescriptor::integer(
"roomMax",
"Maximum Room Size", 8, 2, 256));
678 descriptor.params.push_back(ParamDescriptor::integer(
"padding",
"Room Padding", 1, 0, 4));
679 descriptor.params.push_back(ParamDescriptor::integer(
"spacing",
"Room Spacing", 2, 0, 8));
680 descriptor.params.push_back(ParamDescriptor::integer(
"clusterGapMin",
"Cluster Minimum Gap", 2, 1, 8));
681 descriptor.params.push_back(ParamDescriptor::integer(
"clusterGapMax",
"Cluster Maximum Gap", 4, 1, 12));
682 descriptor.params.push_back(ParamDescriptor::integer(
"clusterBranchBias",
"Cluster Branch Bias", 0, 0, 4));
683 descriptor.params.push_back(ParamDescriptor::integer(
"corridorWidth",
"Corridor Width", 1, 1, 3));
684 descriptor.params.push_back(ParamDescriptor::integer(
"stairCount",
"Stair Count", 1, 0, 4));
685 descriptor.params.push_back(ParamDescriptor::integer(
"stairSideMask",
"Stair Side Mask", 15, 1, 15));
686 descriptor.params.push_back(ParamDescriptor::choice(
"layoutStyle",
"Room Layout",
"grid", {
"grid",
"clustered"}));
687 descriptor.params.push_back(ParamDescriptor::choice(
"connectionStyle",
"Room Connections",
"sequential",
688 {
"sequential",
"nearest",
"growth"}));
690 ParamDescriptor::choice(
"corridorStyle",
"Corridor Style",
"l", {
"l",
"straight",
"diagonal"}));
691 descriptor.params.push_back(ParamDescriptor::choice(
"floorPattern",
"Floor Pattern",
"brick",
692 {
"brick",
"checker",
"plank",
"plain",
"cobble"}));
693 descriptor.params.push_back(ParamDescriptor::integer(
"floorVariants",
"Floor Variants", 4, 1, 15));
695 ParamDescriptor::floating(
"decorDensity",
"Decoration Density", 0.05f, 0.f, 1.f, 0.01f));
697 ParamDescriptor::choice(
"decorSet",
"Decoration Set",
"mixed", {
"mixed",
"pillars",
"treasure",
"none"}));
698 descriptor.params.push_back(ParamDescriptor::floating(
"propDensity",
"Prop Density", 0.16f, 0.f, 1.f, 0.01f));
700 ParamDescriptor::floating(
"corridorLightDensity",
"Corridor Light Density", 0.035f, 0.f, 0.25f, 0.005f));
701 descriptor.params.push_back(ParamDescriptor::boolean(
"autotile",
"Autotile",
true));
707 return grid.getWidth() > 0 && grid.getHeight() > 0;
711 if (grid.getWidth() <= 0 || grid.getHeight() <= 0)
713 "autotile grid dimensions must be positive", {}, {},
714 "procgen.gridGraph"));
715 constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
716 constexpr int dy[] = {-1, 0, 1, 0, -1, 1, 1, -1};
717 for (
int y = 0;
y < grid.getHeight(); ++
y) {
718 for (
int x = 0;
x < grid.getWidth(); ++
x) {
720 if (uint32_t(grid.getCell(
x,
y)) != Semantic::Empty)
724 grid.setDetail(
x,
y,
mask);
731 std::random_device rd;
732 const uint32_t
v = rd();
733 return v == 0 ? 1u :
v;
753WfcPreset makeDungeonPreset() {
763 auto allow = [&](
int a,
int b) {
764 p.tiles[size_t(
a)].compat |= (1ull <<
b);
765 p.tiles[size_t(
b)].compat |= (1ull <<
a);
779WfcPreset makeCavePreset() {
786 auto allow = [&](
int a,
int b) {
787 p.tiles[size_t(
a)].compat |= (1ull <<
b);
788 p.tiles[size_t(
b)].compat |= (1ull <<
a);
796WfcPreset makeTerrainPreset() {
805 auto allow = [&](
int a,
int b) {
806 p.tiles[size_t(
a)].compat |= (1ull <<
b);
807 p.tiles[size_t(
b)].compat |= (1ull <<
a);
809 for (
int i = 0; i < 6; ++i) {
811 if (i + 1 < 6) allow(i, i + 1);
816const WfcPreset &presetByName(
const std::string &
name) {
817 static const WfcPreset dungeon = makeDungeonPreset();
818 static const WfcPreset cave = makeCavePreset();
819 static const WfcPreset
terrain = makeTerrainPreset();
820 if (
name ==
"cave")
return cave;
825bool genWfcSimple(
const Params &
params, Grid2D &
out, std::string &
error) {
826 const int w =
params.getWidth();
827 const int h =
params.getHeight();
828 if (
w < 4 ||
h < 4) {
829 error =
"wfc.simple: size must be at least 4x4";
832 if (
w > 256 ||
h > 256) {
833 error =
"wfc.simple: size capped at 256x256";
838 if (presetName !=
"dungeon" && presetName !=
"cave" && presetName !=
"terrain") {
839 error =
"wfc.simple: unknown preset '" +
presetName +
"' (use dungeon|cave|terrain)";
842 const WfcPreset &preset = presetByName(presetName);
843 const int tileCount = int(preset.tiles.size());
844 if (tileCount <= 0 || tileCount > 63) {
845 error =
"wfc.simple: invalid tile set";
849 const int maxAttempts = std::max(1,
params.getInt(
"maxAttempts", 32));
850 const uint64_t fullMask = (tileCount >= 63) ? ~0ull : ((1ull << tileCount) - 1ull);
852 std::mt19937 rng(
params.getSeed());
854 auto trySolve = [&](Grid2D &
grid) ->
bool {
855 std::vector<uint64_t> wave(
size_t(
w *
h), fullMask);
856 auto idx = [&](
int x,
int y) {
return size_t(
y *
w +
x); };
857 auto countBits = [](uint64_t
m) {
866 auto pickTile = [&](uint64_t
mask) ->
int {
869 for (
int t = 0;
t < tileCount; ++
t) {
872 if (
n <= 0)
return -1;
873 std::uniform_int_distribution<int> dist(0,
n - 1);
878 if (presetName !=
"terrain") {
879 for (
int x = 0;
x <
w; ++
x) {
880 wave[
idx(
x, 0)] = 1ull << 0;
881 wave[
idx(
x,
h - 1)] = 1ull << 0;
883 for (
int y = 0;
y <
h; ++
y) {
884 wave[
idx(0,
y)] = 1ull << 0;
885 wave[
idx(
w - 1,
y)] = 1ull << 0;
889 auto propagate = [&](
int sx,
int sy) ->
bool {
890 std::vector<std::pair<int, int>> stack;
891 stack.emplace_back(
sx,
sy);
892 while (!stack.empty()) {
893 const auto [
cx,
cy] = stack.back();
895 const uint64_t self = wave[
idx(
cx,
cy)];
896 if (self == 0)
return false;
898 uint64_t allowedNeighbor = 0;
899 for (
int t = 0;
t < tileCount; ++
t) {
900 if (self & (1ull <<
t)) allowedNeighbor |= preset.tiles[size_t(
t)].compat;
903 const int dirs[4][2] = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
904 for (
const auto &
d : dirs) {
905 const int nx =
cx +
d[0];
906 const int ny =
cy +
d[1];
907 if (
nx < 0 || ny < 0 || nx >=
w ||
ny >=
h)
continue;
908 uint64_t &nb = wave[
idx(
nx,
ny)];
909 const uint64_t before = nb;
910 nb &= allowedNeighbor;
911 if (nb == 0)
return false;
912 if (nb != before) stack.emplace_back(
nx,
ny);
919 if (presetName !=
"terrain") {
920 for (
int x = 0;
x <
w; ++
x) {
921 if (!propagate(
x, 0) || !propagate(
x,
h - 1))
return false;
923 for (
int y = 0;
y <
h; ++
y) {
924 if (!propagate(0,
y) || !propagate(
w - 1,
y))
return false;
929 int bestX = -1, bestY = -1;
930 int bestEntropy = std::numeric_limits<int>::max();
931 bool anyUncollapsed =
false;
932 for (
int y = 0;
y <
h; ++
y) {
933 for (
int x = 0;
x <
w; ++
x) {
934 const int e = countBits(wave[
idx(
x,
y)]);
935 if (e == 0)
return false;
936 if (e == 1)
continue;
937 anyUncollapsed =
true;
939 const int salted = e * 1000 + int(rng() % 1000u);
940 if (salted < bestEntropy) {
941 bestEntropy = salted;
947 if (!anyUncollapsed)
break;
948 if (bestX < 0)
return false;
950 const int chosen = pickTile(wave[
idx(bestX, bestY)]);
951 if (chosen < 0)
return false;
952 wave[
idx(bestX, bestY)] = 1ull << chosen;
953 if (!propagate(bestX, bestY))
return false;
957 for (
int y = 0;
y <
h; ++
y) {
958 for (
int x = 0;
x <
w; ++
x) {
959 const uint64_t
m = wave[
idx(
x,
y)];
961 for (
int t = 0;
t < tileCount; ++
t) {
962 if (
m & (1ull <<
t)) {
967 grid.setCell(
x,
y,
int(preset.tiles[
size_t(tile)].semantic));
974 for (
int attempt = 0; attempt < maxAttempts; ++attempt) {
976 if (attempt > 0) (void)rng();
978 if (trySolve(candidate)) {
979 out = std::move(candidate);
985 error =
"wfc.simple: failed to collapse (try different seed/preset or raise maxAttempts)";
989 out.setMeta(
"algorithm",
"wfc.simple");
990 out.setMeta(
"preset", presetName);
993 std::vector<std::pair<int, int>> walkable;
994 for (
int y = 0;
y <
h; ++
y) {
995 for (
int x = 0;
x <
w; ++
x) {
996 const uint32_t
c = uint32_t(
out.getCell(
x,
y));
999 walkable.emplace_back(
x,
y);
1003 if (!walkable.empty()) {
1004 std::uniform_int_distribution<size_t>
pick(0, walkable.size() - 1);
1005 auto a = walkable[
pick(rng)];
1006 auto b = walkable[
pick(rng)];
1007 if (
a ==
b && walkable.size() > 1)
b = walkable[(
pick(rng) + 1) % walkable.size()];
1009 out.addObjectAt(
"spawn",
"spawn",
float(
a.first), float(
a.second));
1010 out.addObjectAt(
"stairs",
"stairs",
float(
b.first), float(
b.second));
std::vector< eve::ProcgenProbeDesc > lights
const ColourScope * parent
std::array< float, 4 > rotation
const eve::script::BindingContractRegistry & registry
RoadLaneDirection direction
std::vector< WfcTile > tiles
const SquirrelValueOptions & options
std::map< Cell, int > best
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.
static Result success(T value)
Construct a successful result owning value.
static Result failure(Status status)
Construct a failed result from a structured status.
Registry for executable generators and their reflection metadata.
Intermediate 2D generation result. cells store semantic ids (see Semantic.h), not tile GIDs — convert...
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...
constexpr uint32_t Corridor
double sample(const Heightmap &map, double u, double v)
Sample.
void registerWfcSimple(GeneratorRegistry ®istry)
void registerRoguelikeGenerator(GeneratorRegistry ®istry)
Register the "level.roguelike" algorithm (idempotent).
eve::Result< void > autotileOccupiedGridInPlace(Grid2D &grid)
Write an eight-neighbour occupancy mask into every non-empty cell's detail value.
bool autotileGridInPlace(Grid2D &grid)
Post-process any generated Grid2D: fill each wall cell's detail with an 8-bit neighbour mask describi...
uint32_t randomSeedValue()
Produce a fresh seed suitable for regenerating a level (never 0).
std::string presetName(UiThemeBasePreset preset)
Preset name.
WidgetDesc grid(int columns, std::vector< WidgetDesc > children, std::string id)
Fixed-column grid; children are placed in source order.
static ParamDescriptor choice(std::string key, std::string label, std::string defaultValue, std::vector< std::string > choices)
Construct a finite-choice string descriptor.
static ParamDescriptor integer(std::string key, std::string label, int defaultValue, int minimum, int maximum, int step=1)
Construct a bounded integer descriptor.
static RecipeDescriptor grid(std::string id, std::string displayName, std::string category, int minimumWidth, int minimumHeight, int maximumWidth=4096, int maximumHeight=4096)
Create a schema with common seed, width and height parameters.