forked from NotAShelf/rogged
build: move map & rng logic to their own libraries
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I1802469f3baff4576f61accfb5a197d86a6a6964
This commit is contained in:
parent
702b4258e0
commit
26aa295f82
17 changed files with 136 additions and 93 deletions
253
libs/map/map.c
Normal file
253
libs/map/map.c
Normal file
|
|
@ -0,0 +1,253 @@
|
|||
#include "map.h"
|
||||
#include "rng/rng.h"
|
||||
#include "settings.h"
|
||||
#include "utils.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->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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// 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 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);
|
||||
|
||||
// Place stairs at center of last room
|
||||
if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT)) {
|
||||
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;
|
||||
return has_line_of_sight(map, from_x, from_y, to_x, to_y);
|
||||
}
|
||||
|
||||
void calculate_visibility(Map *map, int x, int y) {
|
||||
memset(map->visible, 0, sizeof(map->visible));
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
libs/map/map.h
Normal file
27
libs/map/map.h
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef MAP_H
|
||||
#define MAP_H
|
||||
|
||||
#include "common.h"
|
||||
|
||||
// Check if a tile is walkable floor
|
||||
int is_floor(const Map *map, int x, int y);
|
||||
|
||||
// Get room center coordinates
|
||||
void get_room_center(Room *room, int *cx, int *cy);
|
||||
|
||||
// Generate a new dungeon floor
|
||||
void dungeon_generate(Dungeon *d, Map *map, int floor_num);
|
||||
|
||||
// Initialize map to all walls
|
||||
void map_init(Map *map);
|
||||
|
||||
// Get a random floor tile position
|
||||
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);
|
||||
int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range);
|
||||
|
||||
#endif // MAP_H
|
||||
13
libs/map/utils.c
Normal file
13
libs/map/utils.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include "utils.h"
|
||||
|
||||
int clamp(int value, int min, int max) {
|
||||
if (value < min)
|
||||
return min;
|
||||
if (value > max)
|
||||
return max;
|
||||
return value;
|
||||
}
|
||||
|
||||
int in_bounds(int x, int y, int width, int height) {
|
||||
return x >= 0 && x < width && y >= 0 && y < height;
|
||||
}
|
||||
10
libs/map/utils.h
Normal file
10
libs/map/utils.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef UTILS_H
|
||||
#define UTILS_H
|
||||
|
||||
// Clamp value between min and max
|
||||
int clamp(int value, int min, int max);
|
||||
|
||||
// Check if coordinates are within map bounds
|
||||
int in_bounds(int x, int y, int width, int height);
|
||||
|
||||
#endif // UTILS_H
|
||||
22
libs/rng/rng.c
Normal file
22
libs/rng/rng.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include "rng.h"
|
||||
|
||||
// Linear congruential generator (LCG) state
|
||||
static unsigned int g_seed = 1;
|
||||
|
||||
// LCG parameters (from numerical recipes)
|
||||
#define LCG_A 1664525
|
||||
#define LCG_C 1013904223
|
||||
#define LCG_MOD 4294967294 // 2^32 - 2 (avoid 0)
|
||||
|
||||
void rng_seed(unsigned int seed) {
|
||||
// Ensure seed is never 0
|
||||
g_seed = (seed == 0) ? 1 : seed;
|
||||
}
|
||||
|
||||
int rng_int(int min, int max) {
|
||||
// Generate next value
|
||||
g_seed = (LCG_A * g_seed + LCG_C) % LCG_MOD;
|
||||
|
||||
// Map to [min, max] range
|
||||
return min + (int)((unsigned long long)g_seed % (max - min + 1));
|
||||
}
|
||||
10
libs/rng/rng.h
Normal file
10
libs/rng/rng.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef RNG_H
|
||||
#define RNG_H
|
||||
|
||||
// Seed the RNG with a deterministic value
|
||||
void rng_seed(unsigned int seed);
|
||||
|
||||
// Get a random integer in range [min, max]
|
||||
int rng_int(int min, int max);
|
||||
|
||||
#endif // RNG_H
|
||||
Loading…
Add table
Add a link
Reference in a new issue