various: sub-tile lighting; nicer visibility calculations

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I0f0a0c12db76cc8e0f4c8ccc72ca4b826a6a6964
This commit is contained in:
raf 2026-04-28 15:57:17 +03:00
commit 00b3798ae0
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
8 changed files with 206 additions and 47 deletions

View file

@ -2,18 +2,18 @@
#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) {
// Fill entire map with walls
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
map->tiles[y][x] = TILE_WALL;
}
}
memset(map->visible, 0, sizeof(map->visible));
memset(map->light_map, 0, sizeof(map->light_map));
memset(map->remembered, 0, sizeof(map->remembered));
map->room_count = 0;
}
@ -300,20 +300,145 @@ int has_line_of_sight(const Map *map, int x1, int y1, int x2, int 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;
return has_line_of_sight(map, from_x, from_y, to_x, to_y);
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;
}
void calculate_visibility(Map *map, int x, int y) {
memset(map->visible, 0, sizeof(map->visible));
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;
}
for (int ty = 0; ty < MAP_HEIGHT; ty++) {
for (int tx = 0; tx < MAP_WIDTH; tx++) {
if (is_in_view_range(tx, ty, x, y, PLAYER_VIEW_RANGE)) {
if (has_line_of_sight(map, x, y, tx, ty)) {
map->visible[ty][tx] = 1;
map->remembered[ty][tx] = 1;
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;
}

View file

@ -21,7 +21,9 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts);
// Visibility / Fog of War
int is_in_view_range(int x, int y, int view_x, int view_y, int range);
int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2);
void calculate_visibility(Map *map, int x, int y);
void compute_lighting(Map *map, const LightSource *sources, int num_sources);
int tile_brightness(const Map *map, int tx, int ty);
int is_tile_revealed(const Map *map, int tx, int ty);
int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range);
#endif // MAP_H