various: implement fog of war; make enemy AI slightly more intelligent
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I3e22dbc5e10690871255980c52a24c226a6a6964
This commit is contained in:
parent
4dfe52ae72
commit
f85d28e932
8 changed files with 151 additions and 21 deletions
|
|
@ -34,6 +34,8 @@ typedef struct {
|
||||||
TileType tiles[MAP_HEIGHT][MAP_WIDTH];
|
TileType tiles[MAP_HEIGHT][MAP_WIDTH];
|
||||||
Room rooms[MAX_ROOMS];
|
Room rooms[MAX_ROOMS];
|
||||||
int room_count;
|
int room_count;
|
||||||
|
unsigned char visible[MAP_HEIGHT][MAP_WIDTH];
|
||||||
|
unsigned char remembered[MAP_HEIGHT][MAP_WIDTH];
|
||||||
} Map;
|
} Map;
|
||||||
|
|
||||||
// Dungeon
|
// Dungeon
|
||||||
|
|
|
||||||
45
src/enemy.c
45
src/enemy.c
|
|
@ -4,6 +4,7 @@
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "movement.h"
|
#include "movement.h"
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
|
#include "settings.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// Forward declaration
|
// Forward declaration
|
||||||
|
|
@ -135,11 +136,9 @@ int is_enemy_at(const Enemy *enemies, int count, int x, int y) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if enemy can see player (adjacent)
|
// Check if enemy can see player (within view range and line of sight)
|
||||||
static int can_see_player(Enemy *e, Player *p) {
|
static int can_see_player(Enemy *e, Player *p, Map *map) {
|
||||||
int dx = p->position.x - e->position.x;
|
return can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, ENEMY_VIEW_RANGE);
|
||||||
int dy = p->position.y - e->position.y;
|
|
||||||
return (dx >= -1 && dx <= 1 && dy >= -1 && dy <= 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -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) {
|
void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_count) {
|
||||||
if (!e->alive)
|
if (!e->alive)
|
||||||
return;
|
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))
|
if (combat_has_effect(e->effects, e->effect_count, EFFECT_STUN))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// Check if adjacent to player - attack
|
int can_see = can_see_player(e, p, map);
|
||||||
if (can_see_player(e, p)) {
|
|
||||||
|
// 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);
|
combat_enemy_attack(e, p);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, move toward player
|
// Move toward player if visible
|
||||||
|
if (can_see) {
|
||||||
enemy_move_toward_player(e, p, map, all_enemies, enemy_count);
|
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) {
|
void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) {
|
||||||
|
|
|
||||||
11
src/main.c
11
src/main.c
|
|
@ -123,6 +123,9 @@ static void init_floor(GameState *gs, int floor_num) {
|
||||||
}
|
}
|
||||||
gs->player.floor = floor_num;
|
gs->player.floor = floor_num;
|
||||||
|
|
||||||
|
// Calculate initial visibility
|
||||||
|
calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y);
|
||||||
|
|
||||||
// Spawn enemies
|
// Spawn enemies
|
||||||
enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num);
|
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 turns - uses speed/cooldown system
|
||||||
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
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)
|
if (gs->game_over)
|
||||||
return 1;
|
return 1;
|
||||||
enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map);
|
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)
|
if (gs->player.hp <= 0)
|
||||||
gs->game_over = 1;
|
gs->game_over = 1;
|
||||||
gs->last_message = "You are stunned!";
|
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};
|
cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y};
|
||||||
BeginMode2D(cam);
|
BeginMode2D(cam);
|
||||||
render_map(&gs.map);
|
render_map(&gs.map);
|
||||||
render_items(gs.items, gs.item_count);
|
render_items(gs.items, gs.item_count, gs.map.visible);
|
||||||
render_enemies(gs.enemies, gs.enemy_count);
|
render_enemies(gs.enemies, gs.enemy_count, gs.map.visible);
|
||||||
render_player(&gs.player);
|
render_player(&gs.player);
|
||||||
EndMode2D();
|
EndMode2D();
|
||||||
|
|
||||||
|
|
|
||||||
66
src/map.c
66
src/map.c
|
|
@ -1,6 +1,8 @@
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "rng.h"
|
#include "rng.h"
|
||||||
|
#include "settings.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
void map_init(Map *map) {
|
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;
|
d->room_count = map->room_count;
|
||||||
memcpy(d->rooms, map->rooms, sizeof(Room) * 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,4 +18,10 @@ void map_init(Map *map);
|
||||||
// Get a random floor tile position
|
// Get a random floor tile position
|
||||||
void get_random_floor_tile(Map *map, int *x, int *y, int attempts);
|
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
|
#endif // MAP_H
|
||||||
|
|
|
||||||
29
src/render.c
29
src/render.c
|
|
@ -11,18 +11,31 @@ void render_map(const Map *map) {
|
||||||
for (int y = 0; y < MAP_HEIGHT; y++) {
|
for (int y = 0; y < MAP_HEIGHT; y++) {
|
||||||
for (int x = 0; x < MAP_WIDTH; x++) {
|
for (int x = 0; x < MAP_WIDTH; x++) {
|
||||||
Rectangle rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE};
|
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]) {
|
switch (map->tiles[y][x]) {
|
||||||
case TILE_WALL:
|
case TILE_WALL:
|
||||||
DrawRectangleRec(rect, DARKGRAY);
|
DrawRectangleRec(rect, wall_color);
|
||||||
break;
|
break;
|
||||||
case TILE_FLOOR:
|
case TILE_FLOOR:
|
||||||
DrawRectangleRec(rect, BLACK);
|
DrawRectangleRec(rect, floor_color);
|
||||||
break;
|
break;
|
||||||
case TILE_STAIRS:
|
case TILE_STAIRS:
|
||||||
DrawRectangleRec(rect, (Color){100, 100, 100, 255});
|
DrawRectangleRec(rect, stairs_color);
|
||||||
// Draw stairs marker
|
if (visible)
|
||||||
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE);
|
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;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -35,10 +48,12 @@ void render_player(const Player *p) {
|
||||||
DrawRectangleRec(rect, BLUE);
|
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++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (!enemies[i].alive)
|
if (!enemies[i].alive)
|
||||||
continue;
|
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),
|
Rectangle rect = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE),
|
||||||
(float)TILE_SIZE, (float)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++) {
|
for (int i = 0; i < count; i++) {
|
||||||
if (items[i].picked_up)
|
if (items[i].picked_up)
|
||||||
continue;
|
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,
|
Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE,
|
||||||
(float)TILE_SIZE};
|
(float)TILE_SIZE};
|
||||||
|
|
|
||||||
|
|
@ -80,10 +80,10 @@ void render_map(const Map *map);
|
||||||
void render_player(const Player *p);
|
void render_player(const Player *p);
|
||||||
|
|
||||||
// Render all enemies
|
// 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
|
// 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
|
// Render UI overlay
|
||||||
void render_ui(const Player *p);
|
void render_ui(const Player *p);
|
||||||
|
|
|
||||||
|
|
@ -63,4 +63,9 @@
|
||||||
// Message timer
|
// Message timer
|
||||||
#define MESSAGE_TIMER_DURATION 60
|
#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
|
#endif // SETTINGS_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue