DungeonCrawl
Loading...
Searching...
No Matches
map_populator.c File Reference

Implements functionality for populating the generated game map. More...

#include "map_populator.h"
#include "map.h"
#include <stdlib.h>

Go to the source code of this file.

Functions

int is_dead_end (int x, int y)
 Check if a cell is a dead end (is floor and has only one neighboring non-wall cell)
void place_key ()
 Place a key in a dead end that is not the start or exit edge.
int is_close_to_enemy (int x, int y)
 Check if a cell is close to an enemy (based on ENEMY_MIN_DISTANCE)
void place_enemies ()
 Place enemies in random locations on the map.
void place_fountains ()
 Place a mana fountain and a life fountain in random dead ends on the map.
void populate_map ()
 @breif Populates the map with a key, enemies, and fountains

Detailed Description

Implements functionality for populating the generated game map.

Definition in file map_populator.c.

Function Documentation

◆ is_close_to_enemy()

int is_close_to_enemy ( int x,
int y )

Check if a cell is close to an enemy (based on ENEMY_MIN_DISTANCE)

Parameters
xthe x coordinate of the cell
ythe y coordinate of the cell
Returns
1 if the cell is close to an enemy, 0 otherwise

Definition at line 57 of file map_populator.c.

57 {
58 for (int i = -ENEMY_MIN_DISTANCE; i <= ENEMY_MIN_DISTANCE + 1; i++) {
59 for (int j = -ENEMY_MIN_DISTANCE; j <= ENEMY_MIN_DISTANCE + 1; j++) {
60 if (map[x + i][y + j] == GOBLIN || map[x + i][y + j] == START_DOOR) {
61 return 1;
62 }
63 }
64 }
65 return 0;
66}

◆ is_dead_end()

int is_dead_end ( int x,
int y )

Check if a cell is a dead end (is floor and has only one neighboring non-wall cell)

Parameters
xthe x coordinate of the cell
ythe y coordinate of the cell
Returns
1 if the cell is a dead end, 0 otherwise

Definition at line 17 of file map_populator.c.

17 {
18 // Count neighboring non-wall cells
19 int neighbor_count = 0;
20
21 for (int i = 0; i < 4; i++) {
22 int dx = x + directions[i].dx;
23 int dy = y + directions[i].dy;
24
25 if (map[dx][dy] != WALL) {
26 neighbor_count++;
27 }
28 }
29
30 return neighbor_count == 1 && map[x][y] == FLOOR;
31}

◆ place_enemies()

void place_enemies ( )

Place enemies in random locations on the map.

Definition at line 71 of file map_populator.c.

71 {
72 for (int i = 0; i < ENEMY_COUNT; i++) {
73 int x;
74 int y;
75
76 // Place enemies in random locations
77 do {
78 x = rand() % (WIDTH - 2) + 1;
79 y = rand() % (HEIGHT - 2) + 1;
80 } while (map[x][y] != FLOOR || is_close_to_enemy(x, y));
81
82 map[x][y] = GOBLIN;
83 }
84}
int is_close_to_enemy(int x, int y)
Check if a cell is close to an enemy (based on ENEMY_MIN_DISTANCE)

◆ place_fountains()

void place_fountains ( )

Place a mana fountain and a life fountain in random dead ends on the map.

Definition at line 90 of file map_populator.c.

90 {
91 int x;
92 int y;
93
94 // place the life fountain in the first dead end found
95 do {
96 x = rand() % (WIDTH - 2) + 1;
97 y = rand() % (HEIGHT - 2) + 1;
98 } while (!is_dead_end(x, y));
99
100 map[x][y] = LIFE_FOUNTAIN;
101
102 // place the mana fountain in the first dead end found
103 do {
104 x = rand() % (WIDTH - 2) + 1;
105 y = rand() % (HEIGHT - 2) + 1;
106 } while (!is_dead_end(x, y));
107
108 map[x][y] = MANA_FOUNTAIN;
109}
int is_dead_end(int x, int y)
Check if a cell is a dead end (is floor and has only one neighboring non-wall cell)

◆ place_key()

void place_key ( )

Place a key in a dead end that is not the start or exit edge.

Definition at line 37 of file map_populator.c.

37 {
38 int x;
39 int y;
40
41 // place the key in the first dead end found
42 do {
43 x = rand() % (WIDTH - 2) + 1;
44 y = rand() % (HEIGHT - 2) + 1;
45 } while (!is_dead_end(x, y));
46
47 map[x][y] = KEY;
48}

◆ populate_map()

void populate_map ( )

@breif Populates the map with a key, enemies, and fountains

Definition at line 112 of file map_populator.c.

112 {
113 place_key();
116}
void place_enemies()
Place enemies in random locations on the map.
void place_key()
Place a key in a dead end that is not the start or exit edge.
void place_fountains()
Place a mana fountain and a life fountain in random dead ends on the map.