1
0
Fork 0
forked from NotAShelf/rogged

initial commit

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ie3b66d17f6f660c9b9a719210bd86f9f6a6a6964
This commit is contained in:
raf 2026-03-17 23:34:37 +03:00
commit b381e2efbd
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
29 changed files with 1633 additions and 0 deletions

15
.editorconfig Normal file
View file

@ -0,0 +1,15 @@
root = true
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
[*.c]
ident_style = space
ident_size = 4
[Makefile*]
ident_style = tab
ident_size = 4

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
# ignore build artifacts
result
build
obj
roguelike

31
Makefile Normal file
View file

@ -0,0 +1,31 @@
# Makefile for Roguelike Game
# Requires raylib, pkg-config
CC := cc
CFLAGS := -Wall -Wextra -O2 -std=c99 -Isrc
LDFLAGS := -lraylib -lm -lpthread -ldl -lrt
TARGET := roguelike
SRCDIR := src
OBJDIR := obj
SOURCES := $(wildcard $(SRCDIR)/*.c)
OBJECTS := $(patsubst $(SRCDIR)/%.c,$(OBJDIR)/%.o,$(SOURCES))
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $^ -o $@ $(LDFLAGS)
$(OBJDIR)/%.o: $(SRCDIR)/%.c
@mkdir -p $(dir $@)
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -rf $(OBJDIR) $(TARGET)
# Alias for development
dev: all
./$(TARGET)

27
flake.lock generated Normal file
View file

@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1773646010,
"narHash": "sha256-iYrs97hS7p5u4lQzuNWzuALGIOdkPXvjz7bviiBjUu8=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "5b2c2d84341b2afb5647081c1386a80d7a8d8605",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

17
flake.nix Normal file
View file

@ -0,0 +1,17 @@
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable";
outputs = {nixpkgs, ...}: let
systems = ["x86_64-linux" "aarch64-linux"];
forEachSystem = nixpkgs.lib.genAttrs systems;
pkgsForEach = nixpkgs.legacyPackages;
in {
packages = forEachSystem (system: {
default = pkgsForEach.${system}.callPackage ./nix/package.nix {};
});
devShells = forEachSystem (system: {
default = pkgsForEach.${system}.callPackage ./nix/shell.nix {};
});
};
}

12
nix/package.nix Normal file
View file

@ -0,0 +1,12 @@
{stdenv}:
stdenv.mkDerivation (finalAttrs: {
pname = "sample-c-cpp";
version = "0.0.1";
src = builtins.path {
path = ../.;
name = finalAttrs.pname;
};
makeFlags = ["PREFIX=$(out)"];
})

16
nix/shell.nix Normal file
View file

@ -0,0 +1,16 @@
{
mkShell,
clang-tools,
raylib,
gnumake,
pkg-config,
}:
mkShell {
strictDeps = true;
buildInputs = [
clang-tools
raylib
gnumake
pkg-config
];
}

90
src/audio.c Normal file
View file

@ -0,0 +1,90 @@
#include "audio.h"
#include "raylib.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846 // xd
#endif
#define SAMPLE_RATE 44100
#define DURATION 0.1
// Generate a simple sine wave tone
static void play_tone(float frequency, float duration, float volume) {
static float samples[SAMPLE_RATE];
int sample_count = (int)(SAMPLE_RATE * duration);
if (sample_count > SAMPLE_RATE)
sample_count = SAMPLE_RATE;
// Generate sine wave
for (int i = 0; i < sample_count; i++) {
float t = (float)i / SAMPLE_RATE;
samples[i] = sinf(2.0f * M_PI * frequency * t) * volume;
// Apply simple envelope (fade in/out)
float envelope = 1.0f;
int fade_samples = SAMPLE_RATE / 20; // 50ms fade
if (i < fade_samples) {
envelope = (float)i / fade_samples;
} else if (i > sample_count - fade_samples) {
envelope = (float)(sample_count - i) / fade_samples;
}
samples[i] *= envelope;
}
// Create wave from samples
Wave wave = {.frameCount = (unsigned int)sample_count,
.sampleRate = SAMPLE_RATE,
.sampleSize = 32,
.channels = 1,
.data = samples};
Sound sound = LoadSoundFromWave(wave);
PlaySound(sound);
UnloadSound(sound);
}
void audio_init(void) {
// Initialize audio device
InitAudioDevice();
}
void audio_close(void) {
// Close audio device
CloseAudioDevice();
}
void audio_play_move(void) {
// Low blip for movement
play_tone(200.0f, 0.05f, 0.3f);
}
void audio_play_attack(void) {
// Mid-range hit sound
play_tone(400.0f, 0.1f, 0.5f);
}
void audio_play_item_pickup(void) {
// High-pitched pickup sound
play_tone(800.0f, 0.15f, 0.4f);
}
void audio_play_enemy_death(void) {
// Descending death sound
play_tone(300.0f, 0.1f, 0.5f);
play_tone(150.0f, 0.15f, 0.4f);
}
void audio_play_player_damage(void) {
// Harsh damage sound
play_tone(150.0f, 0.1f, 0.6f);
play_tone(100.0f, 0.1f, 0.4f);
}
void audio_play_stairs(void) {
// Ascending stairs sound
play_tone(400.0f, 0.1f, 0.3f);
play_tone(600.0f, 0.1f, 0.3f);
play_tone(800.0f, 0.15f, 0.3f);
}

28
src/audio.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef AUDIO_H
#define AUDIO_H
// Initialize audio system
void audio_init(void);
// Close audio system
void audio_close(void);
// Play movement sound
void audio_play_move(void);
// Play attack sound
void audio_play_attack(void);
// Play item pickup sound
void audio_play_item_pickup(void);
// Play enemy death sound
void audio_play_enemy_death(void);
// Play player damage sound
void audio_play_player_damage(void);
// Play stairs/level change sound
void audio_play_stairs(void);
#endif // AUDIO_H

71
src/combat.c Normal file
View file

@ -0,0 +1,71 @@
#include "combat.h"
#include "common.h"
#include <stddef.h>
// Track combat events for feedback
typedef struct {
const char *message;
int damage;
int is_player_damage;
} CombatEvent;
static CombatEvent last_event = {NULL, 0, 0};
const char *combat_get_last_message(void) { return last_event.message; }
int combat_get_last_damage(void) { return last_event.damage; }
int combat_was_player_damage(void) { return last_event.is_player_damage; }
void combat_player_attack(Player *p, Enemy *e) {
if (e == NULL || !e->alive)
return;
// Deal damage
int damage = p->attack;
e->hp -= damage;
// Set combat event
last_event.damage = damage;
last_event.is_player_damage = 0;
// Check if enemy died
if (e->hp <= 0) {
e->hp = 0;
e->alive = 0;
last_event.message = "Enemy killed!";
} else {
last_event.message = "You hit the enemy";
}
}
void combat_enemy_attack(Enemy *e, Player *p) {
if (e == NULL || !e->alive)
return;
if (p == NULL)
return;
// Deal damage reduced by defense (minimum 1 damage)
int damage = e->attack - p->defense;
if (damage < 1)
damage = 1;
p->hp -= damage;
// Set combat event
last_event.damage = damage;
last_event.is_player_damage = 1;
// Check if player died
if (p->hp <= 0) {
p->hp = 0;
last_event.message = "You died!";
} else {
last_event.message = "Enemy attacks!";
}
}
void combat_reset_event(void) {
last_event.message = NULL;
last_event.damage = 0;
last_event.is_player_damage = 0;
}

24
src/combat.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef COMBAT_H
#define COMBAT_H
#include "common.h"
// Get last combat message
const char *combat_get_last_message(void);
// Get last damage amount
int combat_get_last_damage(void);
// Was last damage to player?
int combat_was_player_damage(void);
// Reset combat event
void combat_reset_event(void);
// Player attacks enemy
void combat_player_attack(Player *p, Enemy *e);
// Enemy attacks player
void combat_enemy_attack(Enemy *e, Player *p);
#endif // COMBAT_H

63
src/common.h Normal file
View file

@ -0,0 +1,63 @@
#ifndef COMMON_H
#define COMMON_H
#include "settings.h"
// Tile types
typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS } TileType;
// Room
typedef struct {
int x, y, w, h;
} Room;
// Map
typedef struct {
TileType tiles[MAP_HEIGHT][MAP_WIDTH];
Room rooms[MAX_ROOMS];
int room_count;
} Map;
// Dungeon
typedef struct {
int current_floor;
Room rooms[MAX_ROOMS];
int room_count;
} Dungeon;
// Item types
typedef enum { ITEM_POTION, ITEM_WEAPON, ITEM_ARMOR } ItemType;
// Item
typedef struct {
int x, y;
ItemType type;
int power;
int floor;
int picked_up;
} Item;
// Player
typedef struct {
int x, y;
int hp, max_hp;
int attack;
int defense;
int floor;
Item inventory[MAX_INVENTORY];
int inventory_count;
} Player;
// Enemy types
typedef enum { ENEMY_GOBLIN, ENEMY_SKELETON, ENEMY_ORC } EnemyType;
// Enemy
typedef struct {
int x, y;
int hp;
int attack;
int alive;
EnemyType type;
} Enemy;
#endif // COMMON_H

139
src/enemy.c Normal file
View file

@ -0,0 +1,139 @@
#include "enemy.h"
#include "combat.h"
#include "common.h"
#include "map.h"
#include "rng.h"
// Forward declaration
int is_enemy_at(Enemy *enemies, int count, int x, int y);
void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) {
*count = 0;
// Number of enemies scales with floor
int num_enemies = 3 + floor + rng_int(0, 2);
if (num_enemies > MAX_ENEMIES)
num_enemies = MAX_ENEMIES;
// Enemy types available for this floor
int max_type = 1;
if (floor >= 2)
max_type = 2;
if (floor >= 4)
max_type = 3;
for (int i = 0; i < num_enemies; i++) {
// Find random floor position
int ex, ey;
get_random_floor_tile(map, &ex, &ey, 50);
// Don't spawn on player position
if (ex == p->x && ey == p->y) {
continue;
}
// Don't spawn on other enemies
if (is_enemy_at(enemies, *count, ex, ey)) {
continue;
}
// Create enemy
Enemy e;
e.x = ex;
e.y = ey;
e.alive = 1;
e.type = rng_int(ENEMY_GOBLIN, max_type);
// Stats based on type and floor
switch (e.type) {
case ENEMY_GOBLIN:
e.hp = ENEMY_BASE_HP + floor;
e.attack = ENEMY_BASE_ATTACK;
break;
case ENEMY_SKELETON:
e.hp = ENEMY_BASE_HP + floor + 2;
e.attack = ENEMY_BASE_ATTACK + 1;
break;
case ENEMY_ORC:
e.hp = ENEMY_BASE_HP + floor + 4;
e.attack = ENEMY_BASE_ATTACK + 2;
break;
default:
e.hp = ENEMY_BASE_HP;
e.attack = ENEMY_BASE_ATTACK;
break;
}
enemies[i] = e;
(*count)++;
}
}
// Check if position has an enemy
int is_enemy_at(Enemy *enemies, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
return 1;
}
}
return 0;
}
// Check if enemy can see player (adjacent)
static int can_see_player(Enemy *e, Player *p) {
int dx = p->x - e->x;
int dy = p->y - e->y;
return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1);
}
// Move enemy toward player
static void enemy_move_toward_player(Enemy *e, Player *p, Map *map,
Enemy *all_enemies, int enemy_count) {
int dx = 0, dy = 0;
if (p->x > e->x)
dx = 1;
else if (p->x < e->x)
dx = -1;
if (p->y > e->y)
dy = 1;
else if (p->y < e->y)
dy = -1;
// Try horizontal first, then vertical
int new_x = e->x + dx;
int new_y = e->y;
if (dx != 0 && is_floor(map, new_x, new_y) &&
!is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
e->x = new_x;
} else if (dy != 0) {
new_x = e->x;
new_y = e->y + dy;
if (is_floor(map, new_x, new_y) &&
!is_enemy_at(all_enemies, enemy_count, new_x, new_y)) {
e->x = new_x;
e->y = new_y;
}
}
}
void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) {
for (int i = 0; i < count; i++) {
Enemy *e = &enemies[i];
if (!e->alive)
continue;
// Check if adjacent to player - attack
if (can_see_player(e, p)) {
// Use combat system
combat_enemy_attack(e, p);
continue;
}
// Otherwise, move toward player
enemy_move_toward_player(e, p, map, enemies, count);
}
}

15
src/enemy.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef ENEMY_H
#define ENEMY_H
#include "common.h"
// Spawn enemies for a floor
void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor);
// Update all enemy AI
void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map);
// Check if position has an enemy
int is_enemy_at(Enemy *enemies, int count, int x, int y);
#endif // ENEMY_H

126
src/items.c Normal file
View file

@ -0,0 +1,126 @@
#include "common.h"
#include "map.h"
#include "rng.h"
#include <stddef.h>
void item_spawn(Item items[], int *count, Map *map, int floor) {
*count = 0;
// Number of items scales with floor
int num_items = 2 + rng_int(0, 3);
if (num_items > MAX_ITEMS)
num_items = MAX_ITEMS;
for (int i = 0; i < num_items; i++) {
// Find random floor position
int ix, iy;
get_random_floor_tile(map, &ix, &iy, 50);
// Don't spawn on other items
int occupied = 0;
for (int j = 0; j < *count; j++) {
if (items[j].x == ix && items[j].y == iy) {
occupied = 1;
break;
}
}
if (occupied)
continue;
// Create item
Item item;
item.x = ix;
item.y = iy;
item.floor = floor;
item.picked_up = 0;
// Item type distribution
int type_roll = rng_int(0, 99);
if (type_roll < 50) {
// 50% chance for potion
item.type = ITEM_POTION;
item.power = 5 + rng_int(0, floor * 2); // healing: 5 + 0-2*floor
} else if (type_roll < 80) {
// 30% chance for weapon
item.type = ITEM_WEAPON;
item.power = 1 + rng_int(0, floor); // attack bonus: 1 + 0-floor
} else {
// 20% chance for armor
item.type = ITEM_ARMOR;
item.power = 1 + rng_int(0, floor / 2); // defense bonus
}
items[i] = item;
(*count)++;
}
}
// Get item name for display
const char *item_get_name(Item *i) {
if (i == NULL)
return "";
switch (i->type) {
case ITEM_POTION:
return "Potion";
case ITEM_WEAPON:
return "Weapon";
case ITEM_ARMOR:
return "Armor";
default:
return "Unknown";
}
}
// Get item description
const char *item_get_description(Item *i) {
if (i == NULL)
return "";
switch (i->type) {
case ITEM_POTION:
return "Heals HP";
case ITEM_WEAPON:
return "+Attack";
case ITEM_ARMOR:
return "+Defense";
default:
return "";
}
}
// Get item power value
int item_get_power(Item *i) {
if (i == NULL)
return 0;
return i->power;
}
void item_use(Player *p, Item *i) {
if (p == NULL || i == NULL)
return;
switch (i->type) {
case ITEM_POTION:
// Heal player
p->hp += i->power;
if (p->hp > p->max_hp) {
p->hp = p->max_hp;
}
break;
case ITEM_WEAPON:
// Increase attack
p->attack += i->power;
break;
case ITEM_ARMOR:
// Increase defense
p->defense += i->power;
break;
default:
break;
}
}

14
src/items.h Normal file
View file

@ -0,0 +1,14 @@
#ifndef ITEMS_H
#define ITEMS_H
#include "common.h"
// Item functions - types already defined in common.h
// Spawn items for a floor
void item_spawn(Item items[], int *count, Map *map, int floor);
// Use an item
void item_use(Player *p, Item *i);
#endif // ITEMS_H

247
src/main.c Normal file
View file

@ -0,0 +1,247 @@
#include "audio.h"
#include "combat.h"
#include "common.h"
#include "enemy.h"
#include "items.h"
#include "map.h"
#include "player.h"
#include "raylib.h"
#include "render.h"
#include "rng.h"
#include "settings.h"
#include <stddef.h>
// Global game state
static Player player;
static Map map;
static Dungeon dungeon;
static Enemy enemies[MAX_ENEMIES];
static int enemy_count;
static Item items[MAX_ITEMS];
static int item_count;
static int game_over = 0;
static int game_won = 0;
static const char *last_message = NULL;
static int message_timer = 0;
// Turn counter for enemy movement (enemies move every other turn)
static int turn_count = 0;
// Initialize a new floor
static void init_floor(int floor_num) {
// Generate dungeon
dungeon_generate(&dungeon, &map, floor_num);
// Seed rng for this floor's content
rng_seed(floor_num * 54321);
// Find spawn position
int start_x, start_y;
get_random_floor_tile(&map, &start_x, &start_y, 100);
// Initialize player position if first floor
if (floor_num == 1) {
player_init(&player, start_x, start_y);
} else {
// Move player to new floor position
player.x = start_x;
player.y = start_y;
}
player.floor = floor_num;
// Spawn enemies
enemy_spawn(enemies, &enemy_count, &map, &player, floor_num);
// Spawn items
item_spawn(items, &item_count, &map, floor_num);
// Reset turn counter
turn_count = 0;
}
// Handle player input - returns: 0=continue, -1=quit
static int handle_input(void) {
int dx = 0, dy = 0;
// Check for quit first (always works)
if (IsKeyPressed(KEY_Q)) {
return -1;
}
// Check for restart (works during game over)
if (IsKeyPressed(KEY_R) && game_over) {
game_over = 0;
game_won = 0;
init_floor(1);
return 0;
}
// Check for item usage (U key)
if (IsKeyPressed(KEY_U) && !game_over) {
if (player.inventory_count > 0) {
if (player_use_first_item(&player)) {
last_message = "Used item!";
message_timer = 60;
audio_play_item_pickup();
return 1; // consume a turn
}
}
}
// Movement, use iskeydown for held key repeat, with delay
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) {
dy = -1;
} else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) {
dy = 1;
} else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) {
dx = -1;
} else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) {
dx = 1;
}
if (dx != 0 || dy != 0) {
// Reset combat message
combat_reset_event();
// Player action
int action = player_move(&player, dx, dy, &map, enemies, enemy_count, items,
item_count);
if (action) {
// Increment turn counter
turn_count++;
// Check if stepped on stairs
if (map.tiles[player.y][player.x] == TILE_STAIRS) {
// Go to next floor
if (player.floor < NUM_FLOORS) {
audio_play_stairs();
init_floor(player.floor + 1);
last_message = "Descended to next floor!";
message_timer = 60;
} else {
// Won the game
game_won = 1;
game_over = 1;
}
}
// Check if killed enemy
if (combat_get_last_message() != NULL && !combat_was_player_damage()) {
// Check if enemy died
for (int i = 0; i < enemy_count; i++) {
if (!enemies[i].alive) {
audio_play_enemy_death();
}
}
}
// Enemy turn - only every other turn for fairness
if (turn_count % 2 == 0) {
enemy_update_all(enemies, enemy_count, &player, &map);
}
// Check if player took damage
if (combat_was_player_damage() && combat_get_last_damage() > 0) {
audio_play_player_damage();
}
// Set message
last_message = combat_get_last_message();
message_timer = 60;
// Check game over
if (player.hp <= 0) {
game_over = 1;
}
}
}
return 0;
}
// Main game loop
static void game_loop(void) {
// Initialize first floor
rng_seed(12345);
init_floor(1);
// Disable esc to exit
SetExitKey(0);
while (!WindowShouldClose()) {
// Handle input
if (!game_over) {
int quit = handle_input();
if (quit == -1)
break;
} else {
// Even during game over, check for q/r
if (IsKeyPressed(KEY_Q))
break;
if (IsKeyPressed(KEY_R)) {
game_over = 0;
game_won = 0;
init_floor(1);
}
}
// Update message timer
if (message_timer > 0)
message_timer--;
// Render
BeginDrawing();
ClearBackground(BLACK);
// Draw game elements
render_map(&map);
render_items(items, item_count);
render_enemies(enemies, enemy_count);
render_player(&player);
render_ui(&player);
// Draw message if any
if (last_message != NULL && message_timer > 0) {
render_message(last_message);
}
// Draw game over screen
if (game_over) {
render_game_over();
if (game_won) {
// Draw win message
const char *win_msg = "YOU WIN! ESCAPED THE DUNGEON!";
int msg_w = MeasureText(win_msg, 30);
DrawText(win_msg, (SCREEN_WIDTH - msg_w) / 2, SCREEN_HEIGHT / 2 - 80,
30, GOLD);
}
}
EndDrawing();
// small delay for key repeat control
WaitTime(0.08);
}
}
int main(void) {
// Initialize audio
audio_init();
// Initialize window
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT + 60, "Roguelike");
SetTargetFPS(60);
// Run game
game_loop();
// Cleanup
CloseWindow();
audio_close();
return 0;
}

188
src/map.c Normal file
View file

@ -0,0 +1,188 @@
#include "map.h"
#include "rng.h"
#include "utils.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;
}
}
map->room_count = 0;
}
int is_floor(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) {
// Seed RNG with floor number for deterministic generation
rng_seed(floor_num * 12345);
// 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);
}

21
src/map.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef MAP_H
#define MAP_H
#include "common.h"
// Check if a tile is walkable floor
int is_floor(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);
#endif // MAP_H

142
src/player.c Normal file
View file

@ -0,0 +1,142 @@
#include "player.h"
#include "common.h"
#include "map.h"
#include "utils.h"
#include "combat.h"
#include "items.h"
#include <stdlib.h>
void player_init(Player* p, int x, int y) {
p->x = x;
p->y = y;
p->hp = PLAYER_BASE_HP;
p->max_hp = PLAYER_BASE_HP;
p->attack = PLAYER_BASE_ATTACK;
p->defense = 0;
p->floor = 1;
p->inventory_count = 0;
// Initialize inventory to empty
for (int i = 0; i < MAX_INVENTORY; i++) {
p->inventory[i].picked_up = 1; // mark as invalid
}
}
// Check if position has an enemy
static Enemy* get_enemy_at(Enemy* enemies, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) {
return &enemies[i];
}
}
return NULL;
}
// Check if position has an item
static Item* get_item_at(Item* items, int count, int x, int y) {
for (int i = 0; i < count; i++) {
if (!items[i].picked_up && items[i].x == x && items[i].y == y) {
return &items[i];
}
}
return NULL;
}
int player_move(Player* p, int dx, int dy, Map* map, Enemy* enemies, int enemy_count, Item* items, int item_count) {
int new_x = p->x + dx;
int new_y = p->y + dy;
// Check bounds
if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT)) {
return 0;
}
// Check if walkable
if (!is_floor(map, new_x, new_y)) {
return 0;
}
// Check for enemy at target position
Enemy* enemy = get_enemy_at(enemies, enemy_count, new_x, new_y);
if (enemy != NULL) {
// Attack the enemy
player_attack(p, enemy);
return 1;
}
// Check for item at target position
Item* item = get_item_at(items, item_count, new_x, new_y);
if (item != NULL) {
// Pick up the item
player_pickup(p, item);
}
// Move player
p->x = new_x;
p->y = new_y;
return 1;
}
void player_attack(Player* p, Enemy* e) {
// Use combat system
combat_player_attack(p, e);
}
void player_pickup(Player* p, Item* i) {
if (p->inventory_count >= MAX_INVENTORY) {
return; // inventory full
}
if (i->picked_up) {
return; // already picked up
}
i->picked_up = 1;
p->inventory[p->inventory_count] = *i; // copy item to inventory
p->inventory_count++;
}
void player_use_item(Player* p, Item* i) {
if (p == NULL || i == NULL) return;
if (i->picked_up) return; // invalid item
// Apply item effect
item_use(p, i);
// Mark item as used (remove from inventory)
i->picked_up = 1;
}
int player_use_first_item(Player* p) {
if (p == NULL || p->inventory_count == 0) return 0;
// Find first valid item in inventory
for (int i = 0; i < MAX_INVENTORY; i++) {
if (!p->inventory[i].picked_up) {
Item* item = &p->inventory[i];
// Apply item effect
item_use(p, item);
// Remove from inventory (shift remaining items)
for (int j = i; j < MAX_INVENTORY - 1; j++) {
p->inventory[j] = p->inventory[j + 1];
}
p->inventory_count--;
// Mark last slot as invalid
p->inventory[MAX_INVENTORY - 1].picked_up = 1;
return 1;
}
}
return 0;
}
Item* player_get_inventory_item(Player* p, int index) {
if (p == NULL) return NULL;
if (index < 0 || index >= MAX_INVENTORY) return NULL;
if (p->inventory[index].picked_up) return NULL; // invalid/empty
return &p->inventory[index];
}

28
src/player.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef PLAYER_H
#define PLAYER_H
#include "common.h"
// Initialize player at position
void player_init(Player *p, int x, int y);
// Move player, return 1 if moved/attacked, 0 if blocked
int player_move(Player *p, int dx, int dy, Map *map, Enemy *enemies,
int enemy_count, Item *items, int item_count);
// Player attacks enemy (deal damage)
void player_attack(Player *p, Enemy *e);
// Pick up item
void player_pickup(Player *p, Item *i);
// Use item
void player_use_item(Player *p, Item *i);
// Use first available item in inventory, return 1 if used
int player_use_first_item(Player *p);
// Get item at inventory index, returns NULL if invalid
Item *player_get_inventory_item(Player *p, int index);
#endif // PLAYER_H

202
src/render.c Normal file
View file

@ -0,0 +1,202 @@
#include "render.h"
#include "common.h"
#include "raylib.h"
#include "settings.h"
#include <stddef.h>
#include <stdio.h>
void render_map(Map *map) {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Rectangle rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE),
(float)TILE_SIZE, (float)TILE_SIZE};
switch (map->tiles[y][x]) {
case TILE_WALL:
DrawRectangleRec(rect, DARKGRAY);
break;
case TILE_FLOOR:
DrawRectangleRec(rect, BLACK);
break;
case TILE_STAIRS:
DrawRectangleRec(rect, (Color){100, 100, 100, 255});
// Draw stairs marker
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE);
break;
}
}
}
}
void render_player(Player *p) {
Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE),
(float)TILE_SIZE, (float)TILE_SIZE};
DrawRectangleRec(rect, BLUE);
}
void render_enemies(Enemy *enemies, int count) {
for (int i = 0; i < count; i++) {
if (!enemies[i].alive)
continue;
Rectangle rect = {(float)(enemies[i].x * TILE_SIZE),
(float)(enemies[i].y * TILE_SIZE), (float)TILE_SIZE,
(float)TILE_SIZE};
// Different colors based on enemy type
Color enemy_color;
switch (enemies[i].type) {
case ENEMY_GOBLIN:
enemy_color = (Color){150, 50, 50, 255}; // dark red
break;
case ENEMY_SKELETON:
enemy_color = (Color){200, 200, 200, 255}; // light gray
break;
case ENEMY_ORC:
enemy_color = (Color){50, 150, 50, 255}; // dark green
break;
default:
enemy_color = RED;
break;
}
DrawRectangleRec(rect, enemy_color);
// Draw hp bar above enemy
int hp_percent =
(enemies[i].hp * TILE_SIZE) / 10; // FIXME: assuming max 10 hp, for now
if (hp_percent > 0) {
Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE),
(float)(enemies[i].y * TILE_SIZE - 4),
(float)hp_percent, 3};
DrawRectangleRec(hp_bar, GREEN);
}
}
}
void render_items(Item *items, int count) {
for (int i = 0; i < count; i++) {
if (items[i].picked_up)
continue;
Rectangle rect = {(float)(items[i].x * TILE_SIZE),
(float)(items[i].y * TILE_SIZE), (float)TILE_SIZE,
(float)TILE_SIZE};
// Different colors based on item type
Color item_color;
switch (items[i].type) {
case ITEM_POTION:
item_color = (Color){255, 100, 100, 255}; // red/pink
break;
case ITEM_WEAPON:
item_color = (Color){255, 255, 100, 255}; // yellow
break;
case ITEM_ARMOR:
item_color = (Color){100, 100, 255, 255}; // blue
break;
default:
item_color = GREEN;
break;
}
DrawRectangleRec(rect, item_color);
}
}
void render_ui(Player *p) {
// UI background bar at bottom of screen
Rectangle ui_bg = {0, (float)(MAP_HEIGHT * TILE_SIZE), (float)SCREEN_WIDTH,
60};
DrawRectangleRec(ui_bg, (Color){30, 30, 30, 255});
// Draw dividing line
DrawLine(0, MAP_HEIGHT * TILE_SIZE, SCREEN_WIDTH, MAP_HEIGHT * TILE_SIZE,
GRAY);
// Player hp
char hp_text[32];
snprintf(hp_text, sizeof(hp_text), "HP: %d/%d", p->hp, p->max_hp);
DrawText(hp_text, 10, MAP_HEIGHT * TILE_SIZE + 10, 20, RED);
// Player attack
char atk_text[32];
snprintf(atk_text, sizeof(atk_text), "ATK: %d", p->attack);
DrawText(atk_text, 150, MAP_HEIGHT * TILE_SIZE + 10, 20, YELLOW);
// Floor number
char floor_text[32];
snprintf(floor_text, sizeof(floor_text), "Floor: %d", p->floor);
DrawText(floor_text, 280, MAP_HEIGHT * TILE_SIZE + 10, 20, WHITE);
// Defense stat
char def_text[32];
snprintf(def_text, sizeof(def_text), "DEF: %d", p->defense);
DrawText(def_text, 420, MAP_HEIGHT * TILE_SIZE + 10, 20, BLUE);
// Inventory count
char inv_text[32];
snprintf(inv_text, sizeof(inv_text), "Inv: %d/%d", p->inventory_count,
MAX_INVENTORY);
DrawText(inv_text, 530, MAP_HEIGHT * TILE_SIZE + 10, 16, GREEN);
// Show first item in inventory if any
if (p->inventory_count > 0 && !p->inventory[0].picked_up) {
const char *item_name = "";
switch (p->inventory[0].type) {
case ITEM_POTION:
item_name = "Potion";
break;
case ITEM_WEAPON:
item_name = "Weapon";
break;
case ITEM_ARMOR:
item_name = "Armor";
break;
default:
item_name = "?";
break;
}
char first_item[48];
snprintf(first_item, sizeof(first_item), "[%s +%d]", item_name,
p->inventory[0].power);
DrawText(first_item, 10, MAP_HEIGHT * TILE_SIZE + 35, 14, LIGHTGRAY);
}
// Controls hint
DrawText("WASD: Move | U: Use Item | Q: Quit", 280,
MAP_HEIGHT * TILE_SIZE + 35, 14, GRAY);
}
void render_game_over(void) {
// Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 200});
// Game over text
const char *title = "GAME OVER";
int title_width = MeasureText(title, 60);
DrawText(title, (SCREEN_WIDTH - title_width) / 2, SCREEN_HEIGHT / 2 - 30, 60,
RED);
const char *subtitle = "Press R to restart or Q to quit";
int sub_width = MeasureText(subtitle, 20);
DrawText(subtitle, (SCREEN_WIDTH - sub_width) / 2, SCREEN_HEIGHT / 2 + 40, 20,
WHITE);
}
void render_message(const char *message) {
if (message == NULL)
return;
// Draw message box
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2 - 150),
(float)(SCREEN_HEIGHT / 2 - 30), 300, 60};
DrawRectangleRec(msg_bg, (Color){50, 50, 50, 230});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width,
(int)msg_bg.height, WHITE);
int msg_width = MeasureText(message, 20);
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20,
WHITE);
}

27
src/render.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef RENDER_H
#define RENDER_H
#include "common.h"
// Render the map tiles
void render_map(Map *map);
// Render the player
void render_player(Player *p);
// Render all enemies
void render_enemies(Enemy *enemies, int count);
// Render all items
void render_items(Item *items, int count);
// Render UI overlay
void render_ui(Player *p);
// Render game over screen
void render_game_over(void);
// Render a message popup
void render_message(const char *message);
#endif // RENDER_H

22
src/rng.c Normal file
View 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
src/rng.h Normal file
View 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

2
src/settings.c Normal file
View file

@ -0,0 +1,2 @@
// All constants are defined in settings.h
#include "settings.h"

28
src/settings.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef SETTINGS_H
#define SETTINGS_H
// Core Constants
#define TILE_SIZE 16
#define MAP_WIDTH 64
#define MAP_HEIGHT 48
#define SCREEN_WIDTH (MAP_WIDTH * TILE_SIZE)
#define SCREEN_HEIGHT (MAP_HEIGHT * TILE_SIZE)
// Game Limits
#define MAX_ENEMIES 64
#define MAX_ITEMS 128
#define MAX_ROOMS 25
// Player Stats
#define PLAYER_BASE_HP 20
#define PLAYER_BASE_ATTACK 5
// Enemy Stats
#define ENEMY_BASE_HP 5
#define ENEMY_BASE_ATTACK 2
// Progression
#define NUM_FLOORS 5
#define MAX_INVENTORY 10
#endif // SETTINGS_H

13
src/utils.c Normal file
View 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
src/utils.h Normal file
View 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