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
dd9b7835a6
11 changed files with 68 additions and 31 deletions
72
build.zig
72
build.zig
|
|
@ -4,6 +4,56 @@ pub fn build(b: *std.Build) void {
|
|||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
const c_flags = [_][]const u8{
|
||||
"-std=c99",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-O2",
|
||||
};
|
||||
|
||||
// RNG library
|
||||
const rng_lib = b.addLibrary(.{
|
||||
.name = "rng",
|
||||
.root_module = b.createModule(.{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.link_libc = true,
|
||||
}),
|
||||
});
|
||||
rng_lib.addCSourceFiles(.{
|
||||
.files = &[_][]const u8{"libs/rng/rng.c"},
|
||||
.flags = &c_flags,
|
||||
});
|
||||
rng_lib.addIncludePath(b.path("libs/rng"));
|
||||
|
||||
// Map library
|
||||
const map_lib = b.addLibrary(.{
|
||||
.name = "map",
|
||||
.root_module = b.createModule(.{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
.link_libc = true,
|
||||
}),
|
||||
});
|
||||
map_lib.addCSourceFiles(.{
|
||||
.files = &[_][]const u8{
|
||||
"libs/map/map.c",
|
||||
"libs/map/utils.c",
|
||||
},
|
||||
.flags = &c_flags,
|
||||
});
|
||||
// map.h includes common.h and settings.h which live in src/
|
||||
map_lib.addIncludePath(b.path("src"));
|
||||
// map.c includes rng/rng.h via libs/ root
|
||||
map_lib.addIncludePath(b.path("libs"));
|
||||
// utils.h is co-located with map.c
|
||||
map_lib.addIncludePath(b.path("libs/map"));
|
||||
map_lib.linkLibrary(rng_lib);
|
||||
// map.c doesn't call raylib, but common.h (via map.h) includes raylib.h;
|
||||
// add the include path without linking the library into the static archive
|
||||
const raylib_include = std.mem.trim(u8, b.run(&.{ "pkg-config", "--variable=includedir", "raylib" }), "\n ");
|
||||
map_lib.addSystemIncludePath(.{ .cwd_relative = raylib_include });
|
||||
|
||||
// Zig combat library
|
||||
const combat_lib = b.addLibrary(.{
|
||||
.name = "combat",
|
||||
|
|
@ -14,29 +64,22 @@ pub fn build(b: *std.Build) void {
|
|||
.link_libc = true,
|
||||
}),
|
||||
});
|
||||
// common.h and settings.h live in src/; rng.h exposed bare from libs/rng
|
||||
combat_lib.addIncludePath(b.path("src"));
|
||||
combat_lib.addIncludePath(b.path("libs/rng"));
|
||||
combat_lib.linkLibrary(rng_lib);
|
||||
combat_lib.linkSystemLibrary("raylib");
|
||||
|
||||
// C sources (everything except combat, which is now Zig)
|
||||
// C sources remaining in src/
|
||||
const c_sources = [_][]const u8{
|
||||
"src/audio.c",
|
||||
"src/enemy.c",
|
||||
"src/items.c",
|
||||
"src/main.c",
|
||||
"src/map.c",
|
||||
"src/player.c",
|
||||
"src/movement.c",
|
||||
"src/player.c",
|
||||
"src/render.c",
|
||||
"src/rng.c",
|
||||
"src/settings.c",
|
||||
"src/utils.c",
|
||||
};
|
||||
|
||||
const c_flags = [_][]const u8{
|
||||
"-std=c99",
|
||||
"-Wall",
|
||||
"-Wextra",
|
||||
"-O2",
|
||||
};
|
||||
|
||||
// Main executable
|
||||
|
|
@ -54,7 +97,12 @@ pub fn build(b: *std.Build) void {
|
|||
.flags = &c_flags,
|
||||
});
|
||||
|
||||
// src/ for own headers; libs/ so "rng/rng.h" and "map/map.h" resolve
|
||||
exe.addIncludePath(b.path("src"));
|
||||
exe.addIncludePath(b.path("libs"));
|
||||
|
||||
exe.linkLibrary(rng_lib);
|
||||
exe.linkLibrary(map_lib);
|
||||
exe.linkLibrary(combat_lib);
|
||||
exe.linkSystemLibrary("raylib");
|
||||
exe.linkSystemLibrary("m");
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "map.h"
|
||||
#include "rng.h"
|
||||
#include "rng/rng.h"
|
||||
#include "settings.h"
|
||||
#include "utils.h"
|
||||
#include <stdlib.h>
|
||||
|
|
@ -80,7 +80,6 @@ static int generate_rooms(Map *map, Room *rooms, int floor) {
|
|||
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;
|
||||
|
|
@ -88,17 +87,14 @@ static int generate_rooms(Map *map, Room *rooms, int floor) {
|
|||
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);
|
||||
|
|
@ -116,7 +112,6 @@ static void connect_rooms(Map *map, Room *rooms, int room_count) {
|
|||
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);
|
||||
|
|
@ -134,7 +129,6 @@ 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
|
||||
if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT)) {
|
||||
map->tiles[cy][cx] = TILE_STAIRS;
|
||||
}
|
||||
|
|
@ -170,19 +164,14 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts) {
|
|||
}
|
||||
|
||||
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);
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
#include "enemy.h"
|
||||
#include "combat.h"
|
||||
#include "common.h"
|
||||
#include "map.h"
|
||||
#include "map/map.h"
|
||||
#include "movement.h"
|
||||
#include "rng.h"
|
||||
#include "rng/rng.h"
|
||||
#include "settings.h"
|
||||
#include <string.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "common.h"
|
||||
#include "map.h"
|
||||
#include "rng.h"
|
||||
#include "map/map.h"
|
||||
#include "rng/rng.h"
|
||||
#include "settings.h"
|
||||
#include <stddef.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@
|
|||
#include "common.h"
|
||||
#include "enemy.h"
|
||||
#include "items.h"
|
||||
#include "map.h"
|
||||
#include "map/map.h"
|
||||
#include "movement.h"
|
||||
#include "player.h"
|
||||
#include "raylib.h"
|
||||
#include "render.h"
|
||||
#include "rng.h"
|
||||
#include "rng/rng.h"
|
||||
#include "settings.h"
|
||||
#include <ctype.h>
|
||||
#include <stddef.h>
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
#include "movement.h"
|
||||
#include "enemy.h"
|
||||
#include "map.h"
|
||||
#include "map/map.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
// Check if position is occupied by player
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue