diff --git a/build.zig b/build.zig index 4d6f6b2..9b56898 100644 --- a/build.zig +++ b/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"); diff --git a/src/map.c b/libs/map/map.c similarity index 93% rename from src/map.c rename to libs/map/map.c index 011c06b..9468f1f 100644 --- a/src/map.c +++ b/libs/map/map.c @@ -1,5 +1,5 @@ #include "map.h" -#include "rng.h" +#include "rng/rng.h" #include "settings.h" #include "utils.h" #include @@ -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); diff --git a/src/map.h b/libs/map/map.h similarity index 100% rename from src/map.h rename to libs/map/map.h diff --git a/src/utils.c b/libs/map/utils.c similarity index 100% rename from src/utils.c rename to libs/map/utils.c diff --git a/src/utils.h b/libs/map/utils.h similarity index 100% rename from src/utils.h rename to libs/map/utils.h diff --git a/src/rng.c b/libs/rng/rng.c similarity index 100% rename from src/rng.c rename to libs/rng/rng.c diff --git a/src/rng.h b/libs/rng/rng.h similarity index 100% rename from src/rng.h rename to libs/rng/rng.h diff --git a/src/enemy.c b/src/enemy.c index 57eff34..6832386 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -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 diff --git a/src/items.c b/src/items.c index e5bf212..27f343d 100644 --- a/src/items.c +++ b/src/items.c @@ -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 diff --git a/src/main.c b/src/main.c index 6bd5351..747631e 100644 --- a/src/main.c +++ b/src/main.c @@ -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 #include diff --git a/src/movement.c b/src/movement.c index 0f7fd8f..89e74e1 100644 --- a/src/movement.c +++ b/src/movement.c @@ -1,6 +1,6 @@ #include "movement.h" #include "enemy.h" -#include "map.h" +#include "map/map.h" #include // Check if position is occupied by player