rogged/libs/map/map.c
NotAShelf 514a9560a2
various: add admin build; various layout improvements
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ieaa99fa0a32b42b1e97aada611d809b96a6a6964
2026-06-11 10:39:28 +03:00

840 lines
24 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));
memset(map->door_open_from, 255, sizeof(map->door_open_from));
memset(map->door_anim_timer, 0, sizeof(map->door_anim_timer));
memset(map->door_anim_target, 0, sizeof(map->door_anim_target));
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 || 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) {
*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))
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;
}
}
}
// 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 + 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 = 250;
// Room count varies by floor, but capped at max_rooms
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++;
RoomType type = pick_room_type(floor, room_count);
int w, h;
room_size_for_type(type, &w, &h);
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, type};
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;
}
// A corridor tile is a narrow floor passage with walls on at least 2 sides.
static int is_corridor_tile(const Map *map, int x, int y) {
if (map->tiles[y][x] != TILE_FLOOR)
return 0;
int walls = 0;
if (y > 0 && map->tiles[y - 1][x] == TILE_WALL)
walls++;
if (y < MAP_HEIGHT - 1 && map->tiles[y + 1][x] == TILE_WALL)
walls++;
if (x > 0 && map->tiles[y][x - 1] == TILE_WALL)
walls++;
if (x < MAP_WIDTH - 1 && map->tiles[y][x + 1] == TILE_WALL)
walls++;
return walls >= 2;
}
static int tile_in_any_room(int x, int y, Room *rooms, int room_count) {
for (int i = 0; i < room_count; i++) {
if (x >= rooms[i].x && x < rooms[i].x + rooms[i].w && y >= rooms[i].y && y < rooms[i].y + rooms[i].h)
return 1;
}
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) {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
if (map->tiles[y][x] != TILE_FLOOR)
continue;
if (!is_corridor_tile(map, x, y))
continue;
// Don't place doors inside rooms — corridors are between rooms
if (tile_in_any_room(x, y, rooms, room_count))
continue;
// Corridor must be adjacent to a room floor tile
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 = x + dx[i];
int ny = y + dy[i];
if (!in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT))
continue;
if (map->tiles[ny][nx] != TILE_FLOOR)
continue;
if (tile_in_any_room(nx, ny, rooms, room_count)) {
map->tiles[y][x] = TILE_DOOR_CLOSED;
break;
}
}
}
}
}
// Connect all rooms with corridors
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;
}
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(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;
}
}
}
}
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) {
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);
if (map_validate_layout(map))
break;
}
// 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;
TileType t = map->tiles[y][x];
if ((t == TILE_WALL || t == TILE_DOOR_CLOSED || t == TILE_STATUE) && !(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 || t == TILE_STATUE;
}
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++) {
int max_bright = 0;
int bx = tx * SUB_TILE_RES;
int by = ty * SUB_TILE_RES;
for (int dy = 0; dy < SUB_TILE_RES; dy++) {
for (int dx = 0; dx < SUB_TILE_RES; dx++) {
int v = map->light_map[by + dy][bx + dx];
if (v > max_bright)
max_bright = v;
}
}
if (max_bright > 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;
}