rogged/libs/map/map.c
NotAShelf 00b3798ae0
various: sub-tile lighting; nicer visibility calculations
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I0f0a0c12db76cc8e0f4c8ccc72ca4b826a6a6964
2026-05-11 20:20:55 +03:00

444 lines
12 KiB
C

#include "map.h"
#include "rng/rng.h"
#include "settings.h"
#include "utils.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void map_init(Map *map) {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
map->tiles[y][x] = TILE_WALL;
}
}
memset(map->light_map, 0, sizeof(map->light_map));
memset(map->remembered, 0, sizeof(map->remembered));
map->room_count = 0;
}
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;
}
void get_room_center(Room *room, int *cx, int *cy) {
*cx = room->x + room->w / 2;
*cy = room->y + room->h / 2;
}
// Carve a room into the map
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;
}
}
}
}
// Carve a horizontal corridor
static void carve_h_corridor(Map *map, int x1, int x2, int y) {
int start = (x1 < x2) ? x1 : x2;
int end = (x1 < x2) ? x2 : x1;
for (int x = start; x <= end; x++) {
if (in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) {
map->tiles[y][x] = TILE_FLOOR;
}
}
}
// Carve a vertical corridor
static void carve_v_corridor(Map *map, int x, int y1, int y2) {
int start = (y1 < y2) ? y1 : y2;
int end = (y1 < y2) ? y2 : y1;
for (int y = start; y <= end; y++) {
if (in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) {
map->tiles[y][x] = TILE_FLOOR;
}
}
}
// Check if a room overlaps with existing rooms
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)) {
return 1;
}
}
return 0;
}
// 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;
// Room count varies by floor, but capped at max_rooms
int target_rooms = 5 + (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);
// 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);
Room new_room = {x, y, w, h};
// Check for overlap
if (!room_overlaps(rooms, room_count, &new_room)) {
rooms[room_count] = new_room;
carve_room(map, &new_room);
room_count++;
}
}
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++) {
int cx1, cy1, cx2, cy2;
get_room_center(&rooms[i], &cx1, &cy1);
get_room_center(&rooms[i + 1], &cx2, &cy2);
// 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);
}
}
// Place doors after all corridors are carved
place_doors(map, rooms, room_count);
}
// Place stairs in the last room (furthest from start)
static void place_stairs(Map *map, Room *rooms, int room_count) {
if (room_count > 0) {
Room *last_room = &rooms[room_count - 1];
int cx, cy;
get_room_center(last_room, &cx, &cy);
// 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;
}
}
}
// Get a random floor tile (for player/enemy spawn)
void get_random_floor_tile(Map *map, 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) {
*x = tx;
*y = ty;
return;
}
}
// Fallback: search from top-left
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) {
*x = tx;
*y = ty;
return;
}
}
}
}
void dungeon_generate(Dungeon *d, Map *map, int floor_num) {
// Initialize map to all walls
map_init(map);
// 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);
// Store dungeon state
d->current_floor = floor_num;
d->room_count = map->room_count;
memcpy(d->rooms, map->rooms, sizeof(Room) * map->room_count);
}
int is_in_view_range(int x, int y, int view_x, int view_y, int range) {
int dx = x - view_x;
int dy = y - view_y;
return (dx * dx + dy * dy) <= (range * range);
}
static int trace_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) {
int dx = abs(x2 - x1);
int dy = abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx - dy;
int x = x1;
int y = y1;
while (1) {
if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT))
return 0;
if (x == x2 && y == y2)
return 1;
if (map->tiles[y][x] == TILE_WALL && !(x == x1 && y == y1))
return 0;
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += sx;
}
if (e2 < dx) {
err += dx;
y += sy;
}
}
}
int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) {
if (!in_bounds(x1, y1, MAP_WIDTH, MAP_HEIGHT) || !in_bounds(x2, y2, MAP_WIDTH, MAP_HEIGHT))
return 0;
return trace_line_of_sight(map, x1, y1, x2, y2);
}
int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range) {
if (!is_in_view_range(to_x, to_y, from_x, from_y, range))
return 0;
if (!has_line_of_sight(map, from_x, from_y, to_x, to_y))
return 0;
return tile_brightness(map, to_x, to_y) > LIGHT_SIGHT_THRESHOLD;
}
static int is_solid(const Map *map, int sub_x, int sub_y) {
int map_w = MAP_WIDTH;
int map_h = MAP_HEIGHT;
int tx = sub_x / SUB_TILE_RES;
int ty = sub_y / SUB_TILE_RES;
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;
}
static float smoothstep_light(float edge0, float edge1, float x) {
float t = (x - edge0) / (edge1 - edge0);
if (t < 0.0f)
return 0.0f;
if (t > 1.0f)
return 1.0f;
return t * t * (3.0f - 2.0f * t);
}
static int trace_sub_los(const Map *map, int sx, int sy, int tx, int ty) {
int dx = abs(tx - sx);
int dy = abs(ty - sy);
int step_x = (sx < tx) ? 1 : -1;
int step_y = (sy < ty) ? 1 : -1;
int err = dx - dy;
int x = sx, y = sy;
while (1) {
if (x == tx && y == ty)
return 1;
if (is_solid(map, x, y) && !(x == sx && y == sy))
return 0;
int e2 = 2 * err;
if (e2 > -dy) {
err -= dy;
x += step_x;
}
if (e2 < dx) {
err += dx;
y += step_y;
}
}
}
void compute_lighting(Map *map, const LightSource *sources, int num_sources) {
memset(map->light_map, 0, sizeof(map->light_map));
int map_sub_w = MAP_WIDTH * SUB_TILE_RES;
int map_sub_h = MAP_HEIGHT * SUB_TILE_RES;
for (int si = 0; si < num_sources; si++) {
int cx = sources[si].x * SUB_TILE_RES + SUB_TILE_RES / 2;
int cy = sources[si].y * SUB_TILE_RES + SUB_TILE_RES / 2;
int range_sub = sources[si].range * SUB_TILE_RES;
int intensity = sources[si].intensity;
for (int dy = -range_sub; dy <= range_sub; dy++) {
for (int dx = -range_sub; dx <= range_sub; dx++) {
int sub_x = cx + dx;
int sub_y = cy + dy;
if (sub_x < 0 || sub_x >= map_sub_w || sub_y < 0 || sub_y >= map_sub_h)
continue;
int dist_sq = dx * dx + dy * dy;
if (dist_sq > range_sub * range_sub)
continue;
if (!trace_sub_los(map, cx, cy, sub_x, sub_y))
continue;
float dist = sqrtf((float)dist_sq);
float t = dist / (float)range_sub;
float brightness = 1.0f - smoothstep_light(0.35f, 1.0f, t);
int val = (int)(brightness * (float)intensity);
if (val > map->light_map[sub_y][sub_x])
map->light_map[sub_y][sub_x] = (unsigned char)val;
}
}
int src_x = sources[si].x;
int src_y = sources[si].y;
int src_range = sources[si].range;
int src_intensity = sources[si].intensity;
for (int ty = 0; ty < MAP_HEIGHT; ty++) {
for (int tx = 0; tx < MAP_WIDTH; tx++) {
if (map->tiles[ty][tx] != TILE_WALL && map->tiles[ty][tx] != TILE_DOOR_CLOSED)
continue;
if (!is_in_view_range(tx, ty, src_x, src_y, src_range))
continue;
if (!has_line_of_sight(map, src_x, src_y, tx, ty))
continue;
int dx = tx - src_x;
int dy = ty - src_y;
float t = sqrtf((float)(dx * dx + dy * dy)) / (float)src_range;
float fb = 1.0f - smoothstep_light(0.35f, 1.0f, t);
int val = (int)(fb * (float)src_intensity);
int base_x = tx * SUB_TILE_RES;
int base_y = ty * SUB_TILE_RES;
for (int cy2 = 0; cy2 < SUB_TILE_RES; cy2++) {
for (int cx2 = 0; cx2 < SUB_TILE_RES; cx2++) {
if (val > map->light_map[base_y + cy2][base_x + cx2])
map->light_map[base_y + cy2][base_x + cx2] = (unsigned char)val;
}
}
}
}
}
for (int ty = 0; ty < MAP_HEIGHT; ty++) {
for (int tx = 0; tx < MAP_WIDTH; tx++) {
if (tile_brightness(map, tx, ty) > LIGHT_SIGHT_THRESHOLD)
map->remembered[ty][tx] = 1;
}
}
}
int tile_brightness(const Map *map, int tx, int ty) {
int sum = 0;
int base_x = tx * SUB_TILE_RES;
int base_y = ty * SUB_TILE_RES;
for (int dy = 0; dy < SUB_TILE_RES; dy++) {
for (int dx = 0; dx < SUB_TILE_RES; dx++) {
sum += map->light_map[base_y + dy][base_x + dx];
}
}
return sum / (SUB_TILE_RES * SUB_TILE_RES);
}
int is_tile_revealed(const Map *map, int tx, int ty) {
return tile_brightness(map, tx, ty) > LIGHT_SIGHT_THRESHOLD;
}