From 4dfe52ae729776dfd08408b926a6b33f37c5ec44 Mon Sep 17 00:00:00 2001 From: Squirrel Modeller Date: Thu, 9 Apr 2026 14:11:46 +0000 Subject: [PATCH 1/3] movement: generalize; use vectors (#16) Generalized movement, so that all entities move the same. Reviewed-on: https://git.frzn.dev/NotAShelf/rogged/pulls/16 Reviewed-by: raf Co-authored-by: Squirrel Modeller Co-committed-by: Squirrel Modeller --- build.zig | 1 + src/common.h | 8 ++++-- src/enemy.c | 49 +++++++++++++++-------------------- src/main.c | 69 ++++++++++++++++++++++++++++---------------------- src/movement.c | 29 +++++++++++++++++++++ src/movement.h | 17 +++++++++++++ src/player.c | 31 +++++------------------ src/player.h | 4 +-- src/render.c | 11 ++++---- 9 files changed, 127 insertions(+), 92 deletions(-) create mode 100644 src/movement.c create mode 100644 src/movement.h diff --git a/build.zig b/build.zig index 7109941..4d6f6b2 100644 --- a/build.zig +++ b/build.zig @@ -25,6 +25,7 @@ pub fn build(b: *std.Build) void { "src/main.c", "src/map.c", "src/player.c", + "src/movement.c", "src/render.c", "src/rng.c", "src/settings.c", diff --git a/src/common.h b/src/common.h index 61ab044..52fe274 100644 --- a/src/common.h +++ b/src/common.h @@ -4,6 +4,10 @@ #include "settings.h" #include +typedef struct { + int x, y; +} Vec2; + // Tile types typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS } TileType; @@ -57,7 +61,7 @@ typedef struct { // Player typedef struct { - int x, y; + Vec2 position; int hp, max_hp; int attack; int defense; @@ -83,7 +87,7 @@ typedef enum { ENEMY_GOBLIN, ENEMY_SKELETON, ENEMY_ORC } EnemyType; // Enemy typedef struct { - int x, y; + Vec2 position; int hp; int max_hp; int attack; diff --git a/src/enemy.c b/src/enemy.c index 9f72670..a9d4909 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -2,6 +2,7 @@ #include "combat.h" #include "common.h" #include "map.h" +#include "movement.h" #include "rng.h" #include @@ -29,7 +30,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { get_random_floor_tile(map, &ex, &ey, 50); // Don't spawn on player position - if (ex == p->x && ey == p->y) { + if (ex == p->position.x && ey == p->position.y) { continue; } @@ -41,8 +42,8 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { // Create enemy Enemy e; memset(&e, 0, sizeof(Enemy)); - e.x = ex; - e.y = ey; + e.position.x = ex; + e.position.y = ey; e.alive = 1; e.type = rng_int(ENEMY_GOBLIN, max_type); e.effect_count = 0; @@ -127,7 +128,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { // Check if position has an enemy int is_enemy_at(const 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) { + if (enemies[i].alive && enemies[i].position.x == x && enemies[i].position.y == y) { return 1; } } @@ -136,45 +137,37 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) { // 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; + int dx = p->position.x - e->position.x; + int dy = p->position.y - e->position.y; return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1); } -// Check if position is occupied by player -static int is_player_at(Player *p, int x, int y) { - return (p->x == x && p->y == y); -} // 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) + if (p->position.x > e->position.x) dx = 1; - else if (p->x < e->x) + else if (p->position.x < e->position.x) dx = -1; - if (p->y > e->y) + if (p->position.y > e->position.y) dy = 1; - else if (p->y < e->y) + else if (p->position.y < e->position.y) dy = -1; - // Try horizontal first, then vertical - int new_x = e->x + dx; - int new_y = e->y; + Vec2 dir = {dx, 0}; + if (dx != 0) { + MoveResult r = try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false); + if (r == MOVE_RESULT_MOVED) + return; + } - if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y) && - !is_player_at(p, 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) && - !is_player_at(p, new_x, new_y)) { - e->x = new_x; - e->y = new_y; - } + dir.x = 0; + dir.y = dy; + if (dy != 0) { + try_move_entity(&e->position, dir, map, p, all_enemies, enemy_count, false); } } diff --git a/src/main.c b/src/main.c index d450d75..d90487b 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "enemy.h" #include "items.h" #include "map.h" +#include "movement.h" #include "player.h" #include "raylib.h" #include "render.h" @@ -117,8 +118,8 @@ static void init_floor(GameState *gs, int floor_num) { gs->floors_reached = 1; } else { // Move player to new floor position - gs->player.x = start_x; - gs->player.y = start_y; + gs->player.position.x = start_x; + gs->player.position.y = start_y; } gs->player.floor = floor_num; @@ -137,7 +138,8 @@ static void tick_all_effects(GameState *gs) { // Player effects int player_effect_dmg = combat_tick_effects(&gs->player); if (player_effect_dmg > 0) { - spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, player_effect_dmg, 0); + spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, player_effect_dmg, + 0); gs->screen_shake = SHAKE_EFFECT_DURATION; } @@ -155,7 +157,7 @@ static void tick_all_effects(GameState *gs) { continue; int enemy_effect_dmg = combat_tick_enemy_effects(e); if (enemy_effect_dmg > 0) { - spawn_floating_text(gs, e->x * TILE_SIZE + 8, e->y * TILE_SIZE, enemy_effect_dmg, 0); + spawn_floating_text(gs, e->position.x * TILE_SIZE + 8, e->position.y * TILE_SIZE, enemy_effect_dmg, 0); } if (!e->alive) { add_log(gs, "Enemy died from effects!"); @@ -173,7 +175,7 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { return; // Check if stepped on stairs - if (gs->map.tiles[gs->player.y][gs->player.x] == TILE_STAIRS) { + if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) { gs->awaiting_descend = 1; gs->last_message = "Descend to next floor? (Y/N)"; gs->message_timer = 120; @@ -182,8 +184,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { // combat feedback - player attacked an enemy this turn if (attacked_enemy != NULL) { - int ex = attacked_enemy->x * TILE_SIZE + 8; - int ey = attacked_enemy->y * TILE_SIZE; + int ex = attacked_enemy->position.x * TILE_SIZE + 8; + int ey = attacked_enemy->position.y * TILE_SIZE; if (combat_was_dodged()) { spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE); @@ -224,8 +226,8 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION; gs->damage_taken += combat_get_last_damage(); gs->times_hit++; - spawn_floating_text(gs, gs->player.x * TILE_SIZE + 8, gs->player.y * TILE_SIZE, combat_get_last_damage(), - combat_was_critical()); + spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, + combat_get_last_damage(), combat_was_critical()); } // Set message and check game over @@ -388,7 +390,7 @@ static int handle_movement_input(GameState *gs) { // Check for manual item pickup (G key) if (IsKeyPressed(KEY_G)) { - Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.x, gs->player.y); + Item *item = get_item_at_floor(gs->items, gs->item_count, gs->player.position.x, gs->player.position.y); if (item != NULL) { if (player_pickup(&gs->player, item)) { gs->items_collected++; @@ -417,38 +419,45 @@ static int handle_movement_input(GameState *gs) { } } - // Movement: use IsKeyDown for held-key repeat - int dx = 0, dy = 0; - 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) + Vec2 direction = {0, 0}; + if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) + direction.y = -1; + else if (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) + direction.y = 1; + else if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) + direction.x = -1; + else if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) + direction.x = 1; + + if (direction.x == 0 && direction.y == 0) return 0; // Reset combat event before player acts combat_reset_event(); - int new_x = gs->player.x + dx; - int new_y = gs->player.y + dy; + + int new_x = gs->player.position.x + direction.x; + int new_y = gs->player.position.y + direction.y; + + Enemy *target = NULL; int action = 0; - // Attack enemy at target tile, or move into it - Enemy *target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y); - if (target != NULL) { - player_attack(&gs->player, target); + MoveResult result = + try_move_entity(&gs->player.position, direction, &gs->map, &gs->player, gs->enemies, gs->enemy_count, true); + if (result == MOVE_RESULT_MOVED) { + player_on_move(&gs->player); action = 1; - } else { - action = player_move(&gs->player, dx, dy, &gs->map); + } else if (result == MOVE_RESULT_BLOCKED_ENEMY) { + target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y); + if (target != NULL) { + player_attack(&gs->player, target); + action = 1; + } } if (action) - post_action(gs, target); // target is NULL on move, enemy ptr on attack + post_action(gs, target); return action; } diff --git a/src/movement.c b/src/movement.c new file mode 100644 index 0000000..0f7fd8f --- /dev/null +++ b/src/movement.c @@ -0,0 +1,29 @@ +#include "movement.h" +#include "enemy.h" +#include "map.h" +#include + +// Check if position is occupied by player +static int is_player_at(Player *p, int x, int y) { + return (p->position.x == x && p->position.y == y); +} + +MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count, + bool moving_is_player) { + int new_x = p->x + direction.x; + int new_y = p->y + direction.y; + + if (!is_floor(map, new_x, new_y)) + return MOVE_RESULT_BLOCKED_WALL; + + if (is_enemy_at(enemies, enemy_count, new_x, new_y)) + return MOVE_RESULT_BLOCKED_ENEMY; + if (!moving_is_player) { + if (is_player_at(player, new_x, new_y)) + return MOVE_RESULT_BLOCKED_PLAYER; + } + + p->x = new_x; + p->y = new_y; + return MOVE_RESULT_MOVED; +} diff --git a/src/movement.h b/src/movement.h new file mode 100644 index 0000000..e73a9a0 --- /dev/null +++ b/src/movement.h @@ -0,0 +1,17 @@ +#ifndef MOVEMENT_H +#define MOVEMENT_H + +#include "common.h" + +typedef enum { + MOVE_RESULT_MOVED, + MOVE_RESULT_BLOCKED_WALL, + MOVE_RESULT_BLOCKED_PLAYER, + MOVE_RESULT_BLOCKED_ENEMY +} MoveResult; + +// Attempts to move entity in a given direction. Returns outcome of action. +MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count, + bool moving_is_player); + +#endif // MOVEMENT_H diff --git a/src/player.c b/src/player.c index 3bb4c4d..ecc9967 100644 --- a/src/player.c +++ b/src/player.c @@ -2,15 +2,12 @@ #include "combat.h" #include "common.h" #include "items.h" -#include "map.h" #include "settings.h" -#include "utils.h" -#include #include void player_init(Player *p, int x, int y) { - p->x = x; - p->y = y; + p->position.x = x; + p->position.y = y; p->hp = PLAYER_BASE_HP; p->max_hp = PLAYER_BASE_HP; p->attack = PLAYER_BASE_ATTACK; @@ -43,36 +40,20 @@ Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y) { if (count > MAX_ENEMIES) count = MAX_ENEMIES; for (int i = 0; i < count; i++) { - if (enemies[i].alive && enemies[i].x == x && enemies[i].y == y) + if (enemies[i].alive && enemies[i].position.x == x && enemies[i].position.y == y) return &enemies[i]; } return NULL; } -int player_move(Player *p, int dx, int dy, Map *map) { - 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; - - // Move player - p->x = new_x; - p->y = new_y; +void player_on_move(Player *p) { p->step_count += 1; - // Regen suppressed while poisoned, bleeding, or burning if (p->step_count % REGEN_STEP_INTERVAL == 0 && p->hp < p->max_hp && !combat_has_effect(p->effects, p->effect_count, EFFECT_POISON) && !combat_has_effect(p->effects, p->effect_count, EFFECT_BLEED) && !combat_has_effect(p->effects, p->effect_count, EFFECT_BURN)) { p->hp += 1; } - return 1; } void player_attack(Player *p, Enemy *e) { @@ -228,8 +209,8 @@ int player_drop_item(Player *p, int inv_index, Item *items, int item_count) { if (items[i].picked_up) { // Place dropped item at this position items[i] = *item; - items[i].x = p->x; - items[i].y = p->y; + items[i].x = p->position.x; + items[i].y = p->position.y; items[i].picked_up = 0; // Remove from inventory player_remove_inventory_item(p, inv_index); diff --git a/src/player.h b/src/player.h index ca91abf..290806a 100644 --- a/src/player.h +++ b/src/player.h @@ -6,8 +6,8 @@ // Initialize player at position void player_init(Player *p, int x, int y); -// Move player to (x+dx, y+dy); returns 1 if moved, 0 if blocked -int player_move(Player *p, int dx, int dy, Map *map); +// Apply status effects, healing, etc +void player_on_move(Player *p); // Find a living enemy at tile (x, y); returns NULL if none Enemy *player_find_enemy_at(Enemy *enemies, int count, int x, int y); diff --git a/src/render.c b/src/render.c index 3c24e15..99267a8 100644 --- a/src/render.c +++ b/src/render.c @@ -30,7 +30,8 @@ void render_map(const Map *map) { } void render_player(const Player *p) { - Rectangle rect = {(float)(p->x * TILE_SIZE), (float)(p->y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; + Rectangle rect = {(float)(p->position.x * TILE_SIZE), (float)(p->position.y * TILE_SIZE), (float)TILE_SIZE, + (float)TILE_SIZE}; DrawRectangleRec(rect, BLUE); } @@ -39,8 +40,8 @@ void render_enemies(const Enemy *enemies, int count) { 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}; + Rectangle rect = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE), + (float)TILE_SIZE, (float)TILE_SIZE}; // Different colors based on enemy type Color enemy_color; @@ -72,8 +73,8 @@ void render_enemies(const Enemy *enemies, int count) { bar_color = (Color){200, 180, 40, 255}; // yellow else bar_color = (Color){200, 60, 60, 255}; // red - Rectangle hp_bar = {(float)(enemies[i].x * TILE_SIZE), (float)(enemies[i].y * TILE_SIZE - 4), (float)hp_pixels, - 3}; + Rectangle hp_bar = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE - 4), + (float)hp_pixels, 3}; DrawRectangleRec(hp_bar, bar_color); } } From f85d28e932ec2b10df98baa7611f32197453e12e Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 9 Apr 2026 09:35:52 +0300 Subject: [PATCH 2/3] various: implement fog of war; make enemy AI slightly more intelligent Signed-off-by: NotAShelf Change-Id: I3e22dbc5e10690871255980c52a24c226a6a6964 --- src/common.h | 2 ++ src/enemy.c | 47 +++++++++++++++++++++++++++-------- src/main.c | 11 +++++++-- src/map.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/map.h | 6 +++++ src/render.c | 31 ++++++++++++++++++------ src/render.h | 4 +-- src/settings.h | 5 ++++ 8 files changed, 151 insertions(+), 21 deletions(-) diff --git a/src/common.h b/src/common.h index 52fe274..b8dd19f 100644 --- a/src/common.h +++ b/src/common.h @@ -34,6 +34,8 @@ typedef struct { TileType tiles[MAP_HEIGHT][MAP_WIDTH]; Room rooms[MAX_ROOMS]; int room_count; + unsigned char visible[MAP_HEIGHT][MAP_WIDTH]; + unsigned char remembered[MAP_HEIGHT][MAP_WIDTH]; } Map; // Dungeon diff --git a/src/enemy.c b/src/enemy.c index a9d4909..33308c5 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -4,6 +4,7 @@ #include "map.h" #include "movement.h" #include "rng.h" +#include "settings.h" #include // Forward declaration @@ -135,11 +136,9 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) { return 0; } -// Check if enemy can see player (adjacent) -static int can_see_player(Enemy *e, Player *p) { - int dx = p->position.x - e->position.x; - int dy = p->position.y - e->position.y; - return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1); +// Check if enemy can see player (within view range and line of sight) +static int can_see_player(Enemy *e, Player *p, Map *map) { + return can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, ENEMY_VIEW_RANGE); } @@ -171,7 +170,27 @@ static void enemy_move_toward_player(Enemy *e, Player *p, Map *map, Enemy *all_e } } -// Perform a single action for an enemy (attack if adjacent, otherwise move) +// Move enemy in a random direction (patrol) +static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) { + if (rng_int(0, 100) > ENEMY_PATROL_MOVE_CHANCE) + return; + + int dx = rng_int(-1, 1); + int dy = rng_int(-1, 1); + + if (dx == 0 && dy == 0) + return; + + int new_x = e->position.x + dx; + int new_y = e->position.y + dy; + + if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->position.x = new_x; + e->position.y = new_y; + } +} + +// Perform a single action for an enemy (attack if visible, otherwise patrol) void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) { if (!e->alive) return; @@ -180,14 +199,22 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun if (combat_has_effect(e->effects, e->effect_count, EFFECT_STUN)) return; - // Check if adjacent to player - attack - if (can_see_player(e, p)) { + int can_see = can_see_player(e, p, map); + + // Attack if adjacent to player + if (can_see && can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, 1)) { combat_enemy_attack(e, p); return; } - // Otherwise, move toward player - enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + // Move toward player if visible + if (can_see) { + enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + return; + } + + // Player not visible - patrol randomly + enemy_patrol(e, map, all_enemies, enemy_count); } void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) { diff --git a/src/main.c b/src/main.c index d90487b..ed3cb51 100644 --- a/src/main.c +++ b/src/main.c @@ -123,6 +123,9 @@ static void init_floor(GameState *gs, int floor_num) { } gs->player.floor = floor_num; + // Calculate initial visibility + calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); + // Spawn enemies enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num); @@ -217,6 +220,9 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { } } + // Update visibility based on player's new position + calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); + // Enemy turns - uses speed/cooldown system enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); @@ -248,6 +254,7 @@ static int handle_stun_turn(GameState *gs) { if (gs->game_over) return 1; enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); + calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); if (gs->player.hp <= 0) gs->game_over = 1; gs->last_message = "You are stunned!"; @@ -550,8 +557,8 @@ static void game_loop(void) { cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y}; BeginMode2D(cam); render_map(&gs.map); - render_items(gs.items, gs.item_count); - render_enemies(gs.enemies, gs.enemy_count); + render_items(gs.items, gs.item_count, gs.map.visible); + render_enemies(gs.enemies, gs.enemy_count, gs.map.visible); render_player(&gs.player); EndMode2D(); diff --git a/src/map.c b/src/map.c index 24016c9..a29889b 100644 --- a/src/map.c +++ b/src/map.c @@ -1,6 +1,8 @@ #include "map.h" #include "rng.h" +#include "settings.h" #include "utils.h" +#include #include void map_init(Map *map) { @@ -186,3 +188,67 @@ void dungeon_generate(Dungeon *d, Map *map, int 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; + } + } + } + } +} diff --git a/src/map.h b/src/map.h index 3cf6337..84f1119 100644 --- a/src/map.h +++ b/src/map.h @@ -18,4 +18,10 @@ 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 diff --git a/src/render.c b/src/render.c index 99267a8..430b3e3 100644 --- a/src/render.c +++ b/src/render.c @@ -11,18 +11,31 @@ void render_map(const 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}; + int visible = map->visible[y][x]; + int remembered = map->remembered[y][x]; + + if (!visible && !remembered) { + DrawRectangleRec(rect, (Color){5, 5, 10, 255}); + continue; + } + + Color wall_color = visible ? DARKGRAY : (Color){25, 25, 30, 255}; + Color floor_color = visible ? BLACK : (Color){15, 15, 20, 255}; + Color stairs_color = visible ? (Color){100, 100, 100, 255} : (Color){40, 40, 45, 255}; switch (map->tiles[y][x]) { case TILE_WALL: - DrawRectangleRec(rect, DARKGRAY); + DrawRectangleRec(rect, wall_color); break; case TILE_FLOOR: - DrawRectangleRec(rect, BLACK); + DrawRectangleRec(rect, floor_color); 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); + DrawRectangleRec(rect, stairs_color); + if (visible) + DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE); + else + DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, (Color){60, 60, 65, 255}); break; } } @@ -35,10 +48,12 @@ void render_player(const Player *p) { DrawRectangleRec(rect, BLUE); } -void render_enemies(const Enemy *enemies, int count) { +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]) { for (int i = 0; i < count; i++) { if (!enemies[i].alive) continue; + if (!visible[enemies[i].position.y][enemies[i].position.x]) + continue; Rectangle rect = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; @@ -80,10 +95,12 @@ void render_enemies(const Enemy *enemies, int count) { } } -void render_items(const Item *items, int count) { +void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]) { for (int i = 0; i < count; i++) { if (items[i].picked_up) continue; + if (!visible[items[i].y][items[i].x]) + continue; Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; diff --git a/src/render.h b/src/render.h index 016291f..d934ffc 100644 --- a/src/render.h +++ b/src/render.h @@ -80,10 +80,10 @@ void render_map(const Map *map); void render_player(const Player *p); // Render all enemies -void render_enemies(const Enemy *enemies, int count); +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); // Render all items -void render_items(const Item *items, int count); +void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); // Render UI overlay void render_ui(const Player *p); diff --git a/src/settings.h b/src/settings.h index 8b8ac54..1b96056 100644 --- a/src/settings.h +++ b/src/settings.h @@ -63,4 +63,9 @@ // Message timer #define MESSAGE_TIMER_DURATION 60 +// Visibility / Fog of War +#define PLAYER_VIEW_RANGE 8 +#define ENEMY_VIEW_RANGE 6 +#define ENEMY_PATROL_MOVE_CHANCE 30 + #endif // SETTINGS_H From 71343311ebd827066fadafb1d4a7870dc706869d Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 9 Apr 2026 12:31:52 +0300 Subject: [PATCH 3/3] enemy: add alert memory; vision variance based on type Signed-off-by: NotAShelf Change-Id: I2f5c7cac72c8772e5871b99026d106b46a6a6964 --- src/common.h | 5 +++ src/enemy.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++-- src/map.c | 2 ++ 3 files changed, 90 insertions(+), 3 deletions(-) diff --git a/src/common.h b/src/common.h index b8dd19f..474e133 100644 --- a/src/common.h +++ b/src/common.h @@ -104,6 +104,11 @@ typedef struct { int status_chance; int crit_chance; // crit chance percentage (0-100) int crit_mult; // crit damage multiplier percentage (e.g. 150 = 1.5x) + // vision + int vision_range; + int alert; // 1 = aware of player, searching + int last_known_x; // last position where enemy saw player + int last_known_y; // status effects StatusEffect effects[MAX_EFFECTS]; int effect_count; diff --git a/src/enemy.c b/src/enemy.c index 33308c5..57eff34 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -70,6 +70,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = 0; e.resistance[DMG_FIRE] = -25; e.resistance[DMG_POISON] = 50; + e.vision_range = 7; break; case ENEMY_SKELETON: e.max_hp = ENEMY_BASE_HP + floor + 2; @@ -87,6 +88,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = 50; e.resistance[DMG_FIRE] = 25; e.resistance[DMG_POISON] = 75; + e.vision_range = 6; break; case ENEMY_ORC: e.max_hp = ENEMY_BASE_HP + floor + 4; @@ -104,6 +106,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.resistance[DMG_PIERCE] = -25; e.resistance[DMG_FIRE] = 0; e.resistance[DMG_POISON] = 0; + e.vision_range = 5; break; default: e.max_hp = ENEMY_BASE_HP; @@ -117,6 +120,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { e.crit_chance = ENEMY_CRIT_CHANCE; e.crit_mult = ENEMY_CRIT_MULT; memset(e.resistance, 0, sizeof(e.resistance)); + e.vision_range = ENEMY_VIEW_RANGE; break; } e.cooldown = e.speed; @@ -138,7 +142,7 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) { // Check if enemy can see player (within view range and line of sight) static int can_see_player(Enemy *e, Player *p, Map *map) { - return can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, ENEMY_VIEW_RANGE); + return can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, e->vision_range); } @@ -190,7 +194,68 @@ static void enemy_patrol(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count } } -// Perform a single action for an enemy (attack if visible, otherwise patrol) +// Move enemy toward last known player position +static void enemy_move_to_last_known(Enemy *e, Map *map, Enemy *all_enemies, int enemy_count) { + int dx = 0, dy = 0; + + if (e->last_known_x > e->position.x) + dx = 1; + else if (e->last_known_x < e->position.x) + dx = -1; + + if (e->last_known_y > e->position.y) + dy = 1; + else if (e->last_known_y < e->position.y) + dy = -1; + + int new_x = e->position.x + dx; + int new_y = e->position.y; + + if (dx != 0 && is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->position.x = new_x; + } else if (dy != 0) { + new_x = e->position.x; + new_y = e->position.y + dy; + if (is_floor(map, new_x, new_y) && !is_enemy_at(all_enemies, enemy_count, new_x, new_y)) { + e->position.x = new_x; + e->position.y = new_y; + } + } + + if (e->position.x == e->last_known_x && e->position.y == e->last_known_y) + e->alert = 0; +} + +// Check if position is within alert radius of another enemy +static int is_nearby_enemy(const Enemy *enemies, int count, int x, int y, int radius) { + for (int i = 0; i < count; i++) { + if (!enemies[i].alive) + continue; + int dx = enemies[i].position.x - x; + int dy = enemies[i].position.y - y; + if (dx * dx + dy * dy <= radius * radius) + return 1; + } + return 0; +} + +// Propagate alert to nearby enemies +static void propagate_alert(Enemy *trigger_enemy, Enemy *all_enemies, int enemy_count) { + for (int i = 0; i < enemy_count; i++) { + Enemy *e = &all_enemies[i]; + if (!e->alive || e == trigger_enemy) + continue; + if (e->alert) + continue; + if (is_nearby_enemy(all_enemies, enemy_count, e->position.x, e->position.y, 5)) { + e->alert = 1; + e->last_known_x = trigger_enemy->last_known_x; + e->last_known_y = trigger_enemy->last_known_y; + } + } +} + +// Perform a single action for an enemy (attack if visible, otherwise patrol or search) void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) { if (!e->alive) return; @@ -201,19 +266,34 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun int can_see = can_see_player(e, p, map); + // If we can see the player, update alert state and last known position + if (can_see) { + e->alert = 1; + e->last_known_x = p->position.x; + e->last_known_y = p->position.y; + } + // Attack if adjacent to player if (can_see && can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, 1)) { combat_enemy_attack(e, p); + propagate_alert(e, all_enemies, enemy_count); return; } // Move toward player if visible if (can_see) { enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + propagate_alert(e, all_enemies, enemy_count); return; } - // Player not visible - patrol randomly + // If alert but can't see player, move toward last known position + if (e->alert) { + enemy_move_to_last_known(e, map, all_enemies, enemy_count); + return; + } + + // Not alert - patrol randomly enemy_patrol(e, map, all_enemies, enemy_count); } diff --git a/src/map.c b/src/map.c index a29889b..b9d5bbc 100644 --- a/src/map.c +++ b/src/map.c @@ -12,6 +12,8 @@ void map_init(Map *map) { map->tiles[y][x] = TILE_WALL; } } + memset(map->visible, 0, sizeof(map->visible)); + memset(map->remembered, 0, sizeof(map->remembered)); map->room_count = 0; }