17int visited[WIDTH][HEIGHT];
33 for (
int i = n - 1; i > 0; i--) {
34 int j = rand() % (i + 1);
48 return x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT;
71 return map[x][y + 1] == FLOOR;
73 return map[x][y - 1] == FLOOR;
75 return map[x + 1][y] == FLOOR;
77 return map[x - 1][y] == FLOOR;
79 log_msg(ERROR,
"map_generator",
"Invalid exit edge: %d", exit_e);
95 for (
int i = 0; i < 4; i++) {
96 shuffled_dirs[i] = directions[i];
101 for (
int i = 0; i < 4; i++) {
102 int nx = x + shuffled_dirs[i].dx * 2;
103 int ny = y + shuffled_dirs[i].dy * 2;
107 int wall_x = x + shuffled_dirs[i].dx;
108 int wall_y = y + shuffled_dirs[i].dy;
111 map[wall_x][wall_y] = FLOOR;
126 for (
int i = 0; i < 4; i++) {
127 int dx = x + directions[i].dx;
128 int dy = y + directions[i].dy;
130 if (map[dx][dy] == FLOOR) {
132 neighbor_directions[i] = 1;
144 int max_attempts = num_loops * 10;
146 while (count < num_loops && max_attempts > 0) {
148 int x = 1 + rand() % (WIDTH - 2);
149 int y = 1 + rand() % (HEIGHT - 2);
152 if (map[x][y] == WALL) {
153 int neighbor_directions[] = {0, 0, 0, 0};
158 if ((floor_count == 2) &&
159 ((neighbor_directions[TOP] && neighbor_directions[BOTTOM]) ||
160 (neighbor_directions[LEFT] && neighbor_directions[RIGHT]))) {
176 exit_edge = start_edge;
177 while (exit_edge == start_edge) {
178 exit_edge = rand() % 4;
184 exit_x = 1 + 2 * (rand() % ((WIDTH - 2) / 2));
188 exit_x = 1 + 2 * (rand() % ((WIDTH - 2) / 2));
193 exit_y = 1 + 2 * (rand() % ((HEIGHT - 2) / 2));
197 exit_y = 1 + 2 * (rand() % ((HEIGHT - 2) / 2));
200 log_msg(ERROR,
"map_generator",
"Invalid exit edge: %d", exit_edge);
205 map[exit_x][exit_y] = EXIT_DOOR;
213 for (
int y = 0; y < HEIGHT; y++) {
214 for (
int x = 0; x < WIDTH; x++) {
216 revealed_map[x][y] = HIDDEN;
231 if (start_x % 2 == 0) {
234 if (start_y % 2 == 0) {
242 int num_loops = (WIDTH * HEIGHT) / 100 + 1;
253 switch (start_edge) {
255 *start_x = 3 + 2 * (rand() % ((WIDTH - 5) / 2));
257 map[*start_x][0] = START_DOOR;
260 *start_x = WIDTH - 2;
261 *start_y = 3 + 2 * (rand() % ((HEIGHT - 5) / 2));
262 map[WIDTH - 1][*start_y] = START_DOOR;
265 *start_x = 3 + 2 * (rand() % ((WIDTH - 5) / 2));
266 *start_y = HEIGHT - 2;
267 map[*start_x][HEIGHT - 1] = START_DOOR;
271 *start_y = 3 + 2 * (rand() % ((HEIGHT - 5) / 2));
272 map[0][*start_y] = START_DOOR;
275 log_msg(ERROR,
"map_generator",
"Invalid start edge: %d", start_edge);
288 *start_edge = BOTTOM;
290 *start_y = HEIGHT - 2;
291 map[*start_x][HEIGHT - 1] = START_DOOR;
297 map[0][*start_y] = START_DOOR;
303 map[*start_x][0] = START_DOOR;
307 *start_x = WIDTH - 2;
309 map[WIDTH - 1][*start_y] = START_DOOR;
312 log_msg(ERROR,
"map_generator",
"Invalid exit edge: %d", exit_edge);
319 unsigned int seed = (
unsigned int) time(NULL);
322 seed ^= (uintptr_t) &stack_var;
331 if (!exit_x && !exit_y) {
335 start_edge = rand() % 4;
void log_msg(const log_level_t level, const char *module, const char *format,...)
Logs a formatted message with a specified log level and module.
Header file for logging functionality of the game.
Exposes common types and constants for the games map.
int validate_exit_position(int exit_e, int x, int y)
Check if the exit position is adjacent to a floor cell.
int is_in_bounds(int x, int y)
Check if cell is within bounds of the map.
int is_valid_cell(int x, int y)
Check if a cell is a valid cell to visit (in bounds and not visited)
void add_loops(int num_loops)
Add loops to the map by knocking down some walls.
int check_neighboring_floors(int x, int y, int neighbor_directions[4])
Count neighboring floor cells.
void initialize_map()
Initialize the map with walls.
void shuffle(vector2d_t *dir, int n)
Shuffle array using Fisher-Yates algorithm.
void place_exit(int start_edge)
Place the exit on a random edge of the map, ensuring there's a path to it.
void carve_passages(int x, int y)
Recursive backtracking algorithm to generate map (based on dfs)
void make_exit_into_start(int *start_edge, int *start_x, int *start_y)
Set the start position on the map based on the previous exit position.
void generate_maze(int start_x, int start_y)
Generate a new maze using recursive backtracking.
void generate_map()
Generate the map and populate it with keys, enemies, and the exit.
void set_start_position(int start_edge, int *start_x, int *start_y)
Set the start position on the map.
Exposes functions for generating the game map.
void set_player_start_pos(const int player_x, const int player_y)
Sets the starting position of the player.
Defines and manages functions for map exploration, player movement, and map interactions in map mode.
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.
2-dimensional vector struct