Compare commits

..

4 commits

18 changed files with 122 additions and 266 deletions

Binary file not shown.

View file

@ -4,55 +4,8 @@ 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"));
// Zig combat library. This must be compiled as an object and linked
// directly to bypassing the archive step, or it yields a corrupt
// archive that forces the user to clear the cache each time.
const combat_obj = b.addObject(.{
// Zig combat library
const combat_lib = b.addLibrary(.{
.name = "combat",
.root_module = b.createModule(.{
.root_source_file = b.path("libs/combat/combat.zig"),
@ -61,20 +14,29 @@ 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_obj.addIncludePath(b.path("src"));
combat_obj.addIncludePath(b.path("libs/rng"));
combat_lib.addIncludePath(b.path("src"));
combat_lib.linkSystemLibrary("raylib");
// C sources remaining in src/
// C sources (everything except combat, which is now Zig)
const c_sources = [_][]const u8{
"src/audio.c",
"src/enemy.c",
"src/items.c",
"src/main.c",
"src/movement.c",
"src/map.c",
"src/player.c",
"src/movement.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
@ -92,13 +54,8 @@ 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.addObject(combat_obj);
exe.linkLibrary(combat_lib);
exe.linkSystemLibrary("raylib");
exe.linkSystemLibrary("m");
exe.linkSystemLibrary("pthread");

View file

@ -1,4 +1,6 @@
#include "audio.h"
#include "raylib.h"
#include "common.h"
#include <math.h>
#include <stddef.h>

View file

@ -1,6 +1,6 @@
#ifndef AUDIO_H
#define AUDIO_H
#include "game_state.h"
#include "common.h"
// Initialize audio system
void audio_init(void);

View file

@ -2,7 +2,7 @@
#define COMMON_H
#include "settings.h"
#include <stdbool.h>
#include <raylib.h>
typedef struct {
int x, y;
@ -114,5 +114,66 @@ typedef struct {
int effect_count;
} Enemy;
// Floating damage text
typedef enum { LABEL_NONE = 0, LABEL_DODGE, LABEL_BLOCK, LABEL_CRIT, LABEL_SLAIN, LABEL_PROC } FloatingLabel;
typedef struct {
int x, y;
int value;
int lifetime; // frames remaining
int is_critical;
FloatingLabel label; // label type instead of string
StatusEffectType effect_type; // used to pick color for proc labels
} FloatingText;
// AudioAssets
typedef struct {
Sound attack1, attack2, attack3;
Sound pickup;
Sound staircase;
Sound dodge1, dodge2, dodge3;
Sound crit;
} AudioAssets;
// GameState - encapsulates all game state for testability and save/load
typedef struct {
Player player;
Map map;
Dungeon dungeon;
Enemy enemies[MAX_ENEMIES];
int enemy_count;
Item items[MAX_ITEMS];
int item_count;
int game_over;
int game_won;
const char *last_message;
int message_timer;
int turn_count;
int awaiting_descend; // 0 = normal, 1 = waiting for Y/N
int show_inventory; // 0 = hidden, 1 = show overlay
int inv_selected; // currently selected inventory index
// action log
char action_log[5][128];
int log_count;
int log_head;
// visual effects
FloatingText floating_texts[8];
int floating_count;
int screen_shake; // frames of screen shake remaining
int shake_x;
int shake_y;
AudioAssets sounds;
// Statistics
int total_kills;
int items_collected;
int damage_dealt;
int damage_taken;
int crits_landed;
int times_hit;
int potions_used;
int floors_reached;
int final_score;
} GameState;
#endif // COMMON_H

View file

@ -1,9 +1,9 @@
#include "enemy.h"
#include "combat.h"
#include "common.h"
#include "map/map.h"
#include "map.h"
#include "movement.h"
#include "rng/rng.h"
#include "rng.h"
#include "settings.h"
#include <string.h>

View file

@ -1,70 +0,0 @@
#ifndef GAME_STATE_H
#define GAME_STATE_H
#include "common.h"
#include <raylib.h>
// Floating damage text
typedef enum { LABEL_NONE = 0, LABEL_DODGE, LABEL_BLOCK, LABEL_CRIT, LABEL_SLAIN, LABEL_PROC } FloatingLabel;
typedef struct {
int x, y;
int value;
int lifetime; // frames remaining
int is_critical;
FloatingLabel label; // label type instead of string
StatusEffectType effect_type; // used to pick color for proc labels
} FloatingText;
// AudioAssets
typedef struct {
Sound attack1, attack2, attack3;
Sound pickup;
Sound staircase;
Sound dodge1, dodge2, dodge3;
Sound crit;
} AudioAssets;
// GameState - encapsulates all game state for testability and save/load
typedef struct {
Player player;
Map map;
Dungeon dungeon;
Enemy enemies[MAX_ENEMIES];
int enemy_count;
Item items[MAX_ITEMS];
int item_count;
int game_over;
int game_won;
const char *last_message;
int message_timer;
int turn_count;
int awaiting_descend; // 0 = normal, 1 = waiting for Y/N
int show_inventory; // 0 = hidden, 1 = show overlay
int inv_selected; // currently selected inventory index
// action log
char action_log[5][128];
int log_count;
int log_head;
// visual effects
FloatingText floating_texts[8];
int floating_count;
int screen_shake; // frames of screen shake remaining
int shake_x;
int shake_y;
AudioAssets sounds;
// Statistics
int total_kills;
int items_collected;
int damage_dealt;
int damage_taken;
int crits_landed;
int times_hit;
int potions_used;
int floors_reached;
int final_score;
// Seed for this run
unsigned int run_seed;
} GameState;
#endif // GAME_STATE_H

View file

@ -1,6 +1,6 @@
#include "common.h"
#include "map/map.h"
#include "rng/rng.h"
#include "map.h"
#include "rng.h"
#include "settings.h"
#include <stddef.h>

View file

@ -1,20 +1,18 @@
#include "audio.h"
#include "combat.h"
#include "game_state.h"
#include "common.h"
#include "enemy.h"
#include "items.h"
#include "map/map.h"
#include "map.h"
#include "movement.h"
#include "player.h"
#include "raylib.h"
#include "render.h"
#include "rng/rng.h"
#include "rng.h"
#include "settings.h"
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// Add message to action log
static void add_log(GameState *gs, const char *msg) {
@ -104,14 +102,11 @@ static void update_effects(GameState *gs) {
// Initialize a new floor
static void init_floor(GameState *gs, int floor_num) {
// Seed RNG with run seed combined with floor number for deterministic generation
rng_seed(gs->run_seed + floor_num * 54321);
// Generate dungeon
dungeon_generate(&gs->dungeon, &gs->map, floor_num);
// Seed rng for this floor's content
rng_seed(gs->run_seed + floor_num * 98765);
rng_seed(floor_num * 54321);
// Find spawn position
int start_x, start_y;
@ -483,13 +478,7 @@ static int handle_input(GameState *gs) {
// Check for restart (works during game over)
if (IsKeyPressed(KEY_R) && gs->game_over) {
memset(gs, 0, sizeof(GameState));
// Generate a new random seed for the new run
gs->run_seed = (unsigned int)time(NULL);
init_floor(gs, 1);
// Update window title with new seed
char title[128];
snprintf(title, sizeof(title), "Roguelike - Seed: %u", gs->run_seed);
SetWindowTitle(title);
return 0;
}
@ -519,16 +508,16 @@ void load_audio_assets(GameState *gs) {
}
// Main game loop
static void game_loop(unsigned int run_seed) {
static void game_loop(void) {
GameState gs;
memset(&gs, 0, sizeof(GameState));
gs.run_seed = run_seed;
// load external assets
// sound
load_audio_assets(&gs);
// font
Font fontTTF = LoadFontEx("./assets/fonts/spartan_500.ttf", 36, NULL, 0);
Font fontTTF = LoadFontEx("./assets/fonts/Tomorrow_Night.ttf", 24, NULL, 0);
// Initialize first floor
rng_seed(12345);
init_floor(&gs, 1);
// Disable esc to exit
@ -548,16 +537,10 @@ static void game_loop(unsigned int run_seed) {
break;
if (IsKeyPressed(KEY_R)) {
memset(&gs, 0, sizeof(GameState));
// Generate a new random seed for the new run
gs.run_seed = (unsigned int)time(NULL);
gs.game_over = 0;
gs.game_won = 0;
load_audio_assets(&gs);
init_floor(&gs, 1);
// Update window title with new seed
char title[128];
snprintf(title, sizeof(title), "Roguelike - Seed: %u", gs.run_seed);
SetWindowTitle(title);
}
}
@ -600,9 +583,6 @@ static void game_loop(unsigned int run_seed) {
render_message(gs.last_message, &fontTTF);
}
// Draw persistent seed display in top right
render_seed_display(gs.run_seed);
// Draw game over screen
if (gs.game_over) {
// Compute final score
@ -613,7 +593,7 @@ static void game_loop(unsigned int run_seed) {
}
render_end_screen(gs.game_won, gs.total_kills, gs.items_collected, gs.damage_dealt, gs.damage_taken,
gs.crits_landed, gs.times_hit, gs.potions_used, gs.floors_reached, gs.turn_count,
gs.final_score, gs.run_seed, &fontTTF);
gs.final_score, &fontTTF);
}
EndDrawing();
@ -623,67 +603,17 @@ static void game_loop(unsigned int run_seed) {
}
}
// Check if a string is a valid unsigned integer
static int is_valid_uint(const char *str) {
if (str == NULL || *str == '\0')
return 0;
// Check for optional leading +
if (*str == '+')
str++;
// Must have at least one digit
if (*str == '\0')
return 0;
// All characters must be digits
for (const char *p = str; *p != '\0'; p++) {
if (!isdigit((unsigned char)*p))
return 0;
}
return 1;
}
int main(int argc, char **argv) {
// Parse command-line arguments
unsigned int run_seed = 0;
int seed_provided = 0;
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--seed") == 0) {
if (i + 1 >= argc) {
fprintf(stderr, "Error: --seed requires a value\n");
fprintf(stderr, "Usage: %s [--seed <number>]\n", argv[0]);
return 1;
}
const char *seed_str = argv[i + 1];
if (!is_valid_uint(seed_str)) {
fprintf(stderr, "Error: Invalid seed value: %s\n", seed_str);
fprintf(stderr, "Seed must be a non-negative integer\n");
return 1;
}
run_seed = (unsigned int)strtoul(seed_str, NULL, 10);
seed_provided = 1;
i++; // Skip the value
}
}
// If no seed provided, generate random seed from time
if (!seed_provided) {
run_seed = (unsigned int)time(NULL);
}
printf("Starting game with seed: %u\n", run_seed);
int main(void) {
// Initialize audio
audio_init();
// Initialize random number generator
SetRandomSeed(88435);
// Initialize window with seed in title
char title[128];
snprintf(title, sizeof(title), "Roguelike - Seed: %u", run_seed);
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, title);
// Initialize window
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, "Roguelike");
SetTargetFPS(60);
// Run game
game_loop(run_seed);
game_loop();
// Cleanup
CloseWindow();

View file

@ -1,5 +1,5 @@
#include "map.h"
#include "rng/rng.h"
#include "rng.h"
#include "settings.h"
#include "utils.h"
#include <stdlib.h>
@ -170,6 +170,9 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts) {
}
void dungeon_generate(Dungeon *d, Map *map, int floor_num) {
// Seed RNG with floor number for deterministic generation
rng_seed(floor_num * 12345);
// Initialize map to all walls
map_init(map);

View file

@ -1,6 +1,6 @@
#include "movement.h"
#include "enemy.h"
#include "map/map.h"
#include "map.h"
#include <stdbool.h>
// Check if position is occupied by player

View file

@ -1,5 +1,7 @@
#include "render.h"
#include "common.h"
#include "items.h"
#include "raylib.h"
#include "settings.h"
#include <stddef.h>
#include <stdio.h>
@ -168,7 +170,6 @@ void render_ui(const Player *p, Font *font) {
int bar_height = 16;
// HP Label, above bar
// Vector2 hp_width = MeasureTextEx(*font, "HP", BIG_FONT, NAR_CHAR_SPACE);
DrawTextEx(*font, "HP", (Vector2){bar_x, bar_y - 17}, BIG_FONT, NAR_CHAR_SPACE, text_dim);
// HP Bar background
@ -352,8 +353,7 @@ void render_action_log(const char log[5][128], int count, int head, Font *font)
} else {
text_color = (Color){120, 110, 100, 200}; // oldest: dim
}
DrawTextEx(*font, log[idx], (Vector2){text_x, text_start_y + i * line_height}, NORM_FONT, SMALL_CHAR_SPACE,
text_color);
DrawTextEx(*font, log[idx], (Vector2){text_x, text_start_y + i * line_height}, 10, NAR_CHAR_SPACE, text_color);
}
}
}
@ -369,7 +369,7 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
// Title
const char *title = "INVENTORY";
// int title_w = MeasureText(title, 24);
int title_w = MeasureText(title, 24);
Vector2 t_w = MeasureTextEx(*font, title, 30, NORM_CHAR_SPACE);
DrawTextEx(*font, title, (Vector2){overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10}, HUGE_FONT,
NORM_CHAR_SPACE, WHITE);
@ -403,17 +403,17 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255}
: (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255}
: (Color){140, 140, 255, 255};
DrawTextEx(*font, name, (Vector2){overlay.x + 45, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, name_color);
DrawTextEx(*font, name, (Vector2){overlay.x + 45, y_pos + 4}, 14, NORM_CHAR_SPACE, name_color);
}
// Power
snprintf(slot_text, sizeof(slot_text), "+%d", item->power);
DrawTextEx(*font, slot_text, (Vector2){overlay.x + 150, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, YELLOW);
DrawTextEx(*font, slot_text, (Vector2){overlay.x + 150, y_pos + 4}, 14, NORM_CHAR_SPACE, YELLOW);
// Action
if (item->type == ITEM_POTION) {
DrawTextEx(*font, "[U]se", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GREEN);
DrawTextEx(*font, "[U]se", (Vector2){overlay.x + 200, y_pos + 4}, 14, NORM_CHAR_SPACE, GREEN);
} else {
DrawTextEx(*font, "[E]quip [D]rop", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GOLD);
DrawTextEx(*font, "[E]quip [D]rop", (Vector2){overlay.x + 200, y_pos + 4}, 14, NORM_CHAR_SPACE, GOLD);
}
} else {
// Empty slot
@ -425,9 +425,9 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
// Instructions at bottom
const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close";
Vector2 hint_w = MeasureTextEx(*font, hint, SMALL_FONT, NAR_CHAR_SPACE);
DrawTextEx(*font, hint, (Vector2){overlay.x + (overlay.width - hint_w.x) / 2.0f, overlay.y + overlay.height - 22},
SMALL_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255});
Vector2 hint_w = MeasureTextEx(*font, hint, NORM_FONT, NAR_CHAR_SPACE);
DrawTextEx(*font, hint, (Vector2){overlay.x + (overlay.width - hint_w.x) / 2, overlay.y + overlay.height - 22},
NORM_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255});
}
static Color label_color(FloatingText *ft, int alpha) {
@ -515,7 +515,7 @@ void render_floating_texts(FloatingText *texts, int count, int shake_x, int shak
}
void render_end_screen(int is_victory, int kills, int items, int damage_dealt, int damage_taken, int crits,
int times_hit, int potions, int floors, int turns, int score, unsigned int seed, Font *font) {
int times_hit, int potions, int floors, int turns, int score, Font *font) {
// Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 210});
@ -532,7 +532,7 @@ void render_end_screen(int is_victory, int kills, int items, int damage_dealt, i
int box_x = SCREEN_WIDTH / 2 - 200;
int box_y = 110;
int box_w = 400;
int box_h = 350;
int box_h = 320;
DrawRectangle(box_x, box_y, box_w, box_h, (Color){20, 20, 20, 240});
DrawRectangleLines(box_x, box_y, box_w, box_h, (Color){100, 100, 100, 255});
@ -598,14 +598,7 @@ void render_end_screen(int is_victory, int kills, int items, int damage_dealt, i
DrawTextEx(*font, "SCORE:", (Vector2){col1_x, row_y}, 22, NORM_CHAR_SPACE, GOLD);
snprintf(line, sizeof(line), "%d", score);
DrawTextEx(*font, line, (Vector2){col1_x + 90, col2_row_y}, 22, NORM_CHAR_SPACE, GOLD);
row_y += 35;
// Seed display
DrawTextEx(*font, "SEED:", (Vector2){col1_x, row_y}, 18, SMALL_CHAR_SPACE, label_color);
snprintf(line, sizeof(line), "%u", seed);
DrawTextEx(*font, line, (Vector2){col1_x + 60, row_y}, 18, SMALL_CHAR_SPACE, END_SEED);
// Instructions
if (is_victory) {
const char *subtitle = "Press R to play again or Q to quit";
int sub_width = MeasureText(subtitle, 20);
@ -654,8 +647,8 @@ void render_message(const char *message, Font *font) {
longest_line_width = current_line_width;
// Measure full message
Vector2 total_msg_width = MeasureTextEx(*font, message, font_size, NORM_CHAR_SPACE);
int box_width = total_msg_width.x + (padding_x * 2);
int total_msg_width = MeasureText(message, font_size);
int box_width = total_msg_width + (padding_x * 2);
// If message is too long, use wrapped width
if (box_width > max_box_width) {
@ -679,7 +672,7 @@ void render_message(const char *message, Font *font) {
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
// Draw text centered
int text_x = (SCREEN_WIDTH - total_msg_width.x) / 2;
int text_x = (SCREEN_WIDTH - total_msg_width) / 2;
int text_y = (SCREEN_HEIGHT - font_size) / 2;
// For wrapped text, draw at box center with padding
@ -690,18 +683,3 @@ void render_message(const char *message, Font *font) {
DrawTextEx(*font, message, (Vector2){text_x, text_y}, font_size, NORM_CHAR_SPACE, WHITE);
}
void render_seed_display(unsigned int seed) {
char seed_text[64];
snprintf(seed_text, sizeof(seed_text), "Seed: %u", seed);
const int font_size = 14;
int text_width = MeasureText(seed_text, font_size);
// Position at top right with padding
int x = SCREEN_WIDTH - text_width - 10;
int y = 5;
// Draw with non-obstructive dim text color
DrawText(seed_text, x, y, font_size, TEXT_DIM);
}

View file

@ -1,8 +1,7 @@
#ifndef RENDER_H
#define RENDER_H
#include "game_state.h"
#include "common.h"
// HUD colors
#define HUD_BG (Color){25, 20, 15, 255}
@ -69,7 +68,6 @@
#define END_OVERLAY (Color){0, 0, 0, 210}
#define END_BOX_BG (Color){20, 20, 20, 240}
#define END_BOX_BORDER (Color){100, 100, 100, 255}
#define END_SEED (Color){150, 200, 255, 255}
// Portrait placeholder
// FIXME: remove when player sprites are available
@ -101,12 +99,9 @@ void render_floating_texts(FloatingText *texts, int count, int shake_x, int shak
// Render end screen (victory or death) with stats breakdown
void render_end_screen(int is_victory, int kills, int items, int damage_dealt, int damage_taken, int crits,
int times_hit, int potions, int floors, int turns, int score, unsigned int seed, Font *font);
int times_hit, int potions, int floors, int turns, int score, Font *font);
// Render a message popup
void render_message(const char *message, Font *font);
// Render seed display at top right of screen
void render_seed_display(unsigned int seed);
#endif // RENDER_H