map: expand tile system with doors

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I5704e8f954f6f935954c46ef8af40b836a6a6964
This commit is contained in:
raf 2026-04-28 15:32:15 +03:00
commit 2f5c959500
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
3 changed files with 176 additions and 11 deletions

View file

@ -2,6 +2,7 @@
#include "rng/rng.h"
#include "settings.h"
#include "utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -20,7 +21,8 @@ void map_init(Map *map) {
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;
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;
}
void get_room_center(Room *room, int *cx, int *cy) {
@ -109,6 +111,32 @@ static int generate_rooms(Map *map, Room *rooms, int floor) {
return room_count;
}
// Check if a tile is at a room boundary (adjacent to wall but inside room)
static int is_room_boundary(Map *map, int x, int y) {
// Must be floor
if (map->tiles[y][x] != TILE_FLOOR)
return 0;
// Must have at least one adjacent wall
if (in_bounds(x - 1, y, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y][x - 1] == TILE_WALL)
return 1;
if (in_bounds(x + 1, y, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y][x + 1] == TILE_WALL)
return 1;
if (in_bounds(x, y - 1, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y - 1][x] == TILE_WALL)
return 1;
if (in_bounds(x, y + 1, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y + 1][x] == TILE_WALL)
return 1;
return 0;
}
// Place doors at corridor-room junctions
// DISABLED: Door placement removed per user request
static void place_doors(Map *map, Room *rooms, int room_count) {
(void)map;
(void)rooms;
(void)room_count;
// No-op: doors disabled
}
// 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++) {
@ -125,6 +153,9 @@ static void connect_rooms(Map *map, Room *rooms, int room_count) {
carve_h_corridor(map, cx1, cx2, cy2);
}
}
// Place doors after all corridors are carved
place_doors(map, rooms, room_count);
}
// Place stairs in the last room (furthest from start)
@ -134,8 +165,43 @@ static void place_stairs(Map *map, Room *rooms, int room_count) {
int cx, cy;
get_room_center(last_room, &cx, &cy);
// Place stairs at center of last room
// Ensure stairs are placed on a floor tile, not a wall
if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT) && map->tiles[cy][cx] == TILE_FLOOR) {
map->tiles[cy][cx] = TILE_STAIRS;
return;
}
// 3x3 fallback
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int nx = cx + dx;
int ny = cy + dy;
if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_FLOOR) {
map->tiles[ny][nx] = TILE_STAIRS;
return;
}
}
}
// Expanded fallback: scan the room for any floor tile
for (int dy = 0; dy < last_room->h; dy++) {
for (int dx = 0; dx < last_room->w; dx++) {
int nx = last_room->x + dx;
int ny = last_room->y + dy;
if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_FLOOR) {
map->tiles[ny][nx] = TILE_STAIRS;
return;
}
}
}
// Final fallback: force the center tile to stairs regardless of type
fprintf(stderr, "Warning: No floor tile found for stairs at room center (%d, %d). Forcing stairs placement.\n", cx,
cy);
if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT)) {
if (map->tiles[cy][cx] == TILE_WALL) {
map->tiles[cy][cx] = TILE_FLOOR;
}
map->tiles[cy][cx] = TILE_STAIRS;
}
}