1
0
Fork 0
forked from NotAShelf/rogged

various: add admin build; various layout improvements

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ieaa99fa0a32b42b1e97aada611d809b96a6a6964
This commit is contained in:
raf 2026-05-11 20:35:13 +03:00
commit 514a9560a2
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
9 changed files with 856 additions and 201 deletions

View file

@ -25,7 +25,8 @@ int is_floor(const Map *map, int x, int y) {
if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT))
return 0;
return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS || map->tiles[y][x] == TILE_DOOR_OPEN ||
map->tiles[y][x] == TILE_DOOR_RUINED || map->tiles[y][x] == TILE_DOOR_CLOSED;
map->tiles[y][x] == TILE_DOOR_RUINED || map->tiles[y][x] == TILE_DOOR_CLOSED ||
map->tiles[y][x] == TILE_RUBBLE || map->tiles[y][x] == TILE_SHALLOW_WATER;
}
void get_room_center(Room *room, int *cx, int *cy) {
@ -37,9 +38,25 @@ void get_room_center(Room *room, int *cx, int *cy) {
static void carve_room(Map *map, Room *room) {
for (int y = room->y; y < room->y + room->h; y++) {
for (int x = room->x; x < room->x + room->w; x++) {
if (in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) {
map->tiles[y][x] = TILE_FLOOR;
if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT))
continue;
int local_x = x - room->x;
int local_y = y - room->y;
int carved = 1;
if ((room->type == ROOM_SHRINE || room->type == ROOM_VAULT) &&
((local_x == 0 && local_y == 0) || (local_x == room->w - 1 && local_y == 0) ||
(local_x == 0 && local_y == room->h - 1) || (local_x == room->w - 1 && local_y == room->h - 1))) {
carved = 0;
}
if (room->type == ROOM_CRYPT &&
((local_x == 0 || local_x == room->w - 1) && (local_y == 0 || local_y == room->h - 1)))
carved = 0;
if (carved)
map->tiles[y][x] = TILE_FLOOR;
}
}
}
@ -71,39 +88,125 @@ static int room_overlaps(Room *rooms, int count, Room *new_room) {
// Add padding to prevent rooms from touching
for (int i = 0; i < count; i++) {
Room *r = &rooms[i];
if (!(new_room->x > r->x + r->w || new_room->x + new_room->w < r->x || new_room->y > r->y + r->h ||
new_room->y + new_room->h < r->y)) {
if (!(new_room->x > r->x + r->w + 2 || new_room->x + new_room->w + 2 < r->x || new_room->y > r->y + r->h + 2 ||
new_room->y + new_room->h + 2 < r->y)) {
return 1;
}
}
return 0;
}
static RoomType pick_room_type(int floor, int room_index) {
if (room_index == 0)
return ROOM_START;
int roll = rng_int(0, 99);
if (floor <= 1) {
if (roll < 35)
return ROOM_GUARD;
if (roll < 55)
return ROOM_ARMORY;
if (roll < 75)
return ROOM_SHRINE;
return ROOM_CRYPT;
}
if (floor == 2) {
if (roll < 40)
return ROOM_CISTERN;
if (roll < 60)
return ROOM_GUARD;
if (roll < 80)
return ROOM_SHRINE;
return ROOM_CRYPT;
}
if (floor == 3) {
if (roll < 35)
return ROOM_CRYPT;
if (roll < 60)
return ROOM_LIBRARY;
if (roll < 80)
return ROOM_CISTERN;
return ROOM_VAULT;
}
if (floor == 4) {
if (roll < 35)
return ROOM_FORGE;
if (roll < 55)
return ROOM_ARMORY;
if (roll < 75)
return ROOM_CRYPT;
return ROOM_VAULT;
}
if (roll < 30)
return ROOM_VAULT;
if (roll < 55)
return ROOM_CRYPT;
if (roll < 75)
return ROOM_FORGE;
return ROOM_SHRINE;
}
static void room_size_for_type(RoomType type, int *w, int *h) {
switch (type) {
case ROOM_START:
*w = rng_int(7, 10);
*h = rng_int(6, 8);
break;
case ROOM_GUARD:
case ROOM_ARMORY:
*w = rng_int(6, 11);
*h = rng_int(5, 8);
break;
case ROOM_SHRINE:
case ROOM_VAULT:
*w = rng_int(7, 11);
*h = rng_int(7, 11);
break;
case ROOM_CISTERN:
*w = rng_int(8, 14);
*h = rng_int(6, 10);
break;
case ROOM_CRYPT:
case ROOM_LIBRARY:
*w = rng_int(8, 13);
*h = rng_int(5, 9);
break;
case ROOM_FORGE:
*w = rng_int(7, 12);
*h = rng_int(6, 9);
break;
}
}
// Generate rooms for this floor
static int generate_rooms(Map *map, Room *rooms, int floor) {
int room_count = 0;
int attempts = 0;
int max_attempts = 100;
int max_attempts = 250;
// Room count varies by floor, but capped at max_rooms
int target_rooms = 5 + (floor % 3) + rng_int(0, 3);
int target_rooms = 8 + (floor % 3) + rng_int(0, 3);
if (target_rooms > MAX_ROOMS)
target_rooms = MAX_ROOMS;
while (room_count < target_rooms && attempts < max_attempts) {
attempts++;
// Random room dimensions
int w = rng_int(5, 12);
int h = rng_int(5, 10);
RoomType type = pick_room_type(floor, room_count);
int w, h;
room_size_for_type(type, &w, &h);
// Random position (within map bounds with 1-tile border)
int x = rng_int(2, MAP_WIDTH - w - 2);
int y = rng_int(2, MAP_HEIGHT - h - 2);
int x, y;
if (room_count == 0) {
x = rng_int(3, 8);
y = rng_int(3, MAP_HEIGHT - h - 4);
} else {
x = rng_int(2, MAP_WIDTH - w - 3);
y = rng_int(2, MAP_HEIGHT - h - 3);
}
Room new_room = {x, y, w, h};
Room new_room = {x, y, w, h, type};
// Check for overlap
if (!room_overlaps(rooms, room_count, &new_room)) {
rooms[room_count] = new_room;
carve_room(map, &new_room);
@ -155,6 +258,103 @@ static int tile_in_any_room(int x, int y, Room *rooms, int room_count) {
return 0;
}
static int tile_in_room(const Room *room, int x, int y) {
return x >= room->x && x < room->x + room->w && y >= room->y && y < room->y + room->h;
}
Room *map_room_at(Map *map, int x, int y) {
for (int i = 0; i < map->room_count; i++) {
if (tile_in_room(&map->rooms[i], x, y))
return &map->rooms[i];
}
return NULL;
}
static int protected_room_tile(const Room *room, int x, int y) {
int cx, cy;
get_room_center((Room *)room, &cx, &cy);
return (abs(x - cx) <= 1 && abs(y - cy) <= 1) || x == room->x || y == room->y || x == room->x + room->w - 1 ||
y == room->y + room->h - 1;
}
static void set_room_tile(Map *map, const Room *room, int x, int y, TileType tile) {
if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT) || !tile_in_room(room, x, y) || protected_room_tile(room, x, y))
return;
if (map->tiles[y][x] == TILE_FLOOR)
map->tiles[y][x] = tile;
}
static void decorate_room(Map *map, const Room *room, int floor) {
int cx, cy;
get_room_center((Room *)room, &cx, &cy);
switch (room->type) {
case ROOM_START:
if (room->w >= 8 && room->h >= 6) {
set_room_tile(map, room, room->x + 2, room->y + 2, TILE_RUBBLE);
set_room_tile(map, room, room->x + room->w - 3, room->y + room->h - 3, TILE_RUBBLE);
}
break;
case ROOM_GUARD:
for (int y = room->y + 2; y < room->y + room->h - 2; y += 3) {
set_room_tile(map, room, room->x + 2, y, TILE_STATUE);
set_room_tile(map, room, room->x + room->w - 3, y, TILE_STATUE);
}
break;
case ROOM_SHRINE:
set_room_tile(map, room, cx - 2, cy, TILE_STATUE);
set_room_tile(map, room, cx + 2, cy, TILE_STATUE);
set_room_tile(map, room, cx, cy - 2, TILE_RUBBLE);
set_room_tile(map, room, cx, cy + 2, TILE_RUBBLE);
break;
case ROOM_CISTERN:
for (int y = cy - 1; y <= cy + 1; y++) {
for (int x = cx - 2; x <= cx + 2; x++) {
if ((x + y + floor) % 5 != 0)
set_room_tile(map, room, x, y, TILE_SHALLOW_WATER);
}
}
break;
case ROOM_ARMORY:
for (int x = room->x + 2; x < room->x + room->w - 2; x += 3) {
set_room_tile(map, room, x, room->y + 2, TILE_RUBBLE);
set_room_tile(map, room, x, room->y + room->h - 3, TILE_RUBBLE);
}
break;
case ROOM_CRYPT:
for (int x = room->x + 2; x < room->x + room->w - 2; x += 3) {
set_room_tile(map, room, x, cy - 1, TILE_STATUE);
set_room_tile(map, room, x, cy + 1, TILE_RUBBLE);
}
break;
case ROOM_LIBRARY:
for (int y = room->y + 2; y < room->y + room->h - 2; y += 2) {
set_room_tile(map, room, room->x + 2, y, TILE_STATUE);
set_room_tile(map, room, room->x + room->w - 3, y, TILE_RUBBLE);
}
break;
case ROOM_FORGE:
for (int y = cy - 1; y <= cy + 1; y++) {
set_room_tile(map, room, cx - 2, y, TILE_RUBBLE);
set_room_tile(map, room, cx + 2, y, TILE_RUBBLE);
}
set_room_tile(map, room, cx, cy - 2, TILE_STATUE);
break;
case ROOM_VAULT:
set_room_tile(map, room, cx - 2, cy - 2, TILE_STATUE);
set_room_tile(map, room, cx + 2, cy - 2, TILE_STATUE);
set_room_tile(map, room, cx - 2, cy + 2, TILE_STATUE);
set_room_tile(map, room, cx + 2, cy + 2, TILE_STATUE);
break;
}
}
static void decorate_rooms(Map *map, Room *rooms, int room_count, int floor) {
for (int i = 0; i < room_count; i++) {
decorate_room(map, &rooms[i], floor);
}
}
// Place doors at corridor-room junctions.
// Doors sit on corridor tiles, not room tiles, so they occupy the actual doorway.
static void place_doors(Map *map, Room *rooms, int room_count) {
@ -187,23 +387,56 @@ static void place_doors(Map *map, Room *rooms, int room_count) {
}
// Connect all rooms with corridors
static void connect_rooms(Map *map, Room *rooms, int room_count) {
for (int i = 0; i < room_count - 1; i++) {
int cx1, cy1, cx2, cy2;
get_room_center(&rooms[i], &cx1, &cy1);
get_room_center(&rooms[i + 1], &cx2, &cy2);
static int room_distance2(Room *a, Room *b) {
int ax, ay, bx, by;
get_room_center(a, &ax, &ay);
get_room_center(b, &bx, &by);
int dx = ax - bx;
int dy = ay - by;
return dx * dx + dy * dy;
}
// Carve L-shaped corridor between rooms
if (rng_int(0, 1) == 0) {
carve_h_corridor(map, cx1, cx2, cy1);
carve_v_corridor(map, cx2, cy1, cy2);
} else {
carve_v_corridor(map, cx1, cy1, cy2);
carve_h_corridor(map, cx1, cx2, cy2);
static void carve_connection(Map *map, Room *a, Room *b) {
int cx1, cy1, cx2, cy2;
get_room_center(a, &cx1, &cy1);
get_room_center(b, &cx2, &cy2);
if (rng_int(0, 2) == 0) {
int mid_x = (cx1 + cx2) / 2 + rng_int(-3, 3);
carve_h_corridor(map, cx1, mid_x, cy1);
carve_v_corridor(map, mid_x, cy1, cy2);
carve_h_corridor(map, mid_x, cx2, cy2);
} else if (rng_int(0, 1) == 0) {
carve_h_corridor(map, cx1, cx2, cy1);
carve_v_corridor(map, cx2, cy1, cy2);
} else {
carve_v_corridor(map, cx1, cy1, cy2);
carve_h_corridor(map, cx1, cx2, cy2);
}
}
static void connect_rooms(Map *map, Room *rooms, int room_count) {
for (int i = 1; i < room_count; i++) {
int nearest = 0;
int best = room_distance2(&rooms[i], &rooms[0]);
for (int j = 1; j < i; j++) {
int dist = room_distance2(&rooms[i], &rooms[j]);
if (dist < best) {
best = dist;
nearest = j;
}
}
carve_connection(map, &rooms[i], &rooms[nearest]);
}
for (int i = 0; i < room_count; i++) {
if (rng_int(0, 99) >= 28)
continue;
int j = rng_int(0, room_count - 1);
if (i != j)
carve_connection(map, &rooms[i], &rooms[j]);
}
// Place doors after all corridors are carved
place_doors(map, rooms, room_count);
}
@ -284,18 +517,121 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts) {
}
}
int get_room_floor_tile(const Map *map, const Room *room, int *x, int *y) {
*x = -1;
*y = -1;
int cx = room->x + room->w / 2;
int cy = room->y + room->h / 2;
if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT) && map->tiles[cy][cx] == TILE_FLOOR) {
*x = cx;
*y = cy;
return 1;
}
for (int radius = 1; radius < room->w + room->h; radius++) {
for (int yy = cy - radius; yy <= cy + radius; yy++) {
for (int xx = cx - radius; xx <= cx + radius; xx++) {
if (!tile_in_room(room, xx, yy) || !in_bounds(xx, yy, MAP_WIDTH, MAP_HEIGHT))
continue;
if (map->tiles[yy][xx] == TILE_FLOOR) {
*x = xx;
*y = yy;
return 1;
}
}
}
}
return 0;
}
int get_random_floor_tile_excluding_room(Map *map, const Room *excluded, int *x, int *y, int attempts) {
*x = -1;
*y = -1;
for (int i = 0; i < attempts; i++) {
int tx = rng_int(1, MAP_WIDTH - 2);
int ty = rng_int(1, MAP_HEIGHT - 2);
if (map->tiles[ty][tx] == TILE_FLOOR && (excluded == NULL || !tile_in_room(excluded, tx, ty))) {
*x = tx;
*y = ty;
return 1;
}
}
for (int ty = 1; ty < MAP_HEIGHT - 1; ty++) {
for (int tx = 1; tx < MAP_WIDTH - 1; tx++) {
if (map->tiles[ty][tx] == TILE_FLOOR && (excluded == NULL || !tile_in_room(excluded, tx, ty))) {
*x = tx;
*y = ty;
return 1;
}
}
}
return 0;
}
int map_validate_layout(const Map *map) {
if (map->room_count <= 0)
return 0;
int sx, sy;
if (!get_room_floor_tile(map, &map->rooms[0], &sx, &sy))
return 0;
unsigned char visited[MAP_HEIGHT][MAP_WIDTH];
Vec2 queue[MAP_HEIGHT * MAP_WIDTH];
memset(visited, 0, sizeof(visited));
int head = 0;
int tail = 0;
queue[tail++] = (Vec2){sx, sy};
visited[sy][sx] = 1;
while (head < tail) {
Vec2 p = queue[head++];
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
for (int i = 0; i < 4; i++) {
int nx = p.x + dx[i];
int ny = p.y + dy[i];
if (!in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) || visited[ny][nx] || !is_floor(map, nx, ny))
continue;
visited[ny][nx] = 1;
queue[tail++] = (Vec2){nx, ny};
}
}
int stairs = 0;
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (is_floor(map, x, y) && !visited[y][x])
return 0;
if (map->tiles[y][x] == TILE_STAIRS) {
if (!visited[y][x])
return 0;
stairs++;
}
}
}
return stairs == 1;
}
void dungeon_generate(Dungeon *d, Map *map, int floor_num) {
// Initialize map to all walls
map_init(map);
for (int attempt = 0; attempt < 4; attempt++) {
map_init(map);
map->room_count = generate_rooms(map, map->rooms, floor_num);
connect_rooms(map, map->rooms, map->room_count);
decorate_rooms(map, map->rooms, map->room_count, floor_num);
place_stairs(map, map->rooms, map->room_count);
// Generate rooms
map->room_count = generate_rooms(map, map->rooms, floor_num);
// Connect rooms with corridors
connect_rooms(map, map->rooms, map->room_count);
// Place stairs in last room
place_stairs(map, map->rooms, map->room_count);
if (map_validate_layout(map))
break;
}
// Store dungeon state
d->current_floor = floor_num;
@ -326,7 +662,7 @@ static int trace_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) {
return 1;
TileType t = map->tiles[y][x];
if ((t == TILE_WALL || t == TILE_DOOR_CLOSED) && !(x == x1 && y == y1))
if ((t == TILE_WALL || t == TILE_DOOR_CLOSED || t == TILE_STATUE) && !(x == x1 && y == y1))
return 0;
int e2 = 2 * err;
@ -363,7 +699,7 @@ static int is_solid(const Map *map, int sub_x, int sub_y) {
if (tx < 0 || tx >= map_w || ty < 0 || ty >= map_h)
return 1;
TileType t = map->tiles[ty][tx];
return t == TILE_WALL || t == TILE_DOOR_CLOSED;
return t == TILE_WALL || t == TILE_DOOR_CLOSED || t == TILE_STATUE;
}
static float smoothstep_light(float edge0, float edge1, float x) {

View file

@ -17,6 +17,10 @@ void map_init(Map *map);
// Get a random floor tile position
void get_random_floor_tile(Map *map, int *x, int *y, int attempts);
int get_room_floor_tile(const Map *map, const Room *room, int *x, int *y);
int get_random_floor_tile_excluding_room(Map *map, const Room *excluded, int *x, int *y, int attempts);
Room *map_room_at(Map *map, int x, int y);
int map_validate_layout(const Map *map);
// Visibility / Fog of War
int is_in_view_range(int x, int y, int view_x, int view_y, int range);