DungeonCrawl
Loading...
Searching...
No Matches
map_populator.c
Go to the documentation of this file.
1
5#include "map_populator.h"
6
7#include "map.h"
8
9#include <stdlib.h>
10
17int is_dead_end(int x, int y) {
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}
32
33
37void place_key() {
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}
49
50
57int is_close_to_enemy(int x, int y) {
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}
67
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}
85
86
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}
110
111
113 place_key();
116}
Exposes common types and constants for the games map.
void place_enemies()
Place enemies in random locations on the map.
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_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.
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 populate_map()
@breif Populates the map with a key, enemies, and fountains
Exposes functions for populating the generated map with a key, enemies and fountains.