From f534c8d2c4cd8c8692cd7d831afb3041cc584c2b Mon Sep 17 00:00:00 2001 From: Jacob Janzen Date: Mon, 12 Feb 2024 12:51:08 -0600 Subject: refactor --- cavegen.c | 86 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 44 insertions(+), 42 deletions(-) (limited to 'cavegen.c') diff --git a/cavegen.c b/cavegen.c index 11a88cb..9c3a4af 100644 --- a/cavegen.c +++ b/cavegen.c @@ -3,10 +3,7 @@ #include #include #include -#include -#define HEIGHT 100 -#define WIDTH 180 #define MAX_STEPS 200 #define NUM_WALKERS 100 @@ -18,60 +15,51 @@ enum direction { NUM_DIRS, }; -void create_cave(bool **map, struct point **open, int *open_tiles) +void create_cave( + enum tile_type **map, struct point **open_tiles, int *num_open_tiles, + struct point *up, struct point *down +) { - srand(time(NULL)); - *map = malloc(sizeof(bool) * HEIGHT * WIDTH); + // create a map consisting entirely of walls + *map = malloc(sizeof(enum tile_type) * HEIGHT * WIDTH); for (int i = 0; i < HEIGHT; ++i) { for (int j = 0; j < WIDTH; ++j) { - (*map)[i * WIDTH + j] = false; + (*map)[i * WIDTH + j] = WALL; } } - int start_x = WIDTH / 2; - int start_y = HEIGHT / 2; - *open_tiles = 1; - *open = malloc(sizeof(struct point) * HEIGHT * WIDTH); - struct point p = {.x = start_x, .y = start_y}; - (*open)[0] = p; + // start in the middle of the screen + int start_x = WIDTH / 2; + int start_y = HEIGHT / 2; + + // make the starting point GROUND + (*map)[start_y * WIDTH + start_x] = GROUND; + *open_tiles = malloc(sizeof(struct point) * HEIGHT * WIDTH); + *num_open_tiles = 1; + (*open_tiles)[0].x = start_x; + (*open_tiles)[0].y = start_y; for (int i = 0; i < NUM_WALKERS; ++i) { - struct point curr_point = (*open)[rand() % *open_tiles]; - int x_pos = curr_point.x; - int y_pos = curr_point.y; + // get a random open point + struct point curr_point = (*open_tiles)[rand() % *num_open_tiles]; + + int x_pos = curr_point.x; + int y_pos = curr_point.y; + // iterate until the walk exits the array or MAX_STEPS is reached for (int j = 1; j < MAX_STEPS - 1 && x_pos < WIDTH - 1 && x_pos >= 1 && y_pos < HEIGHT - 1 && y_pos >= 1; ++j) { - if (!((*map)[y_pos * WIDTH + x_pos])) { - (*map)[y_pos * WIDTH + x_pos] = true; - struct point p = {.x = x_pos, .y = y_pos}; - (*open)[(*open_tiles)++] = p; - } - enum direction dir = rand() % NUM_DIRS; - switch (dir) { - case UP : --y_pos; break; - case LEFT : --x_pos; break; - case RIGHT : ++x_pos; break; - case DOWN : ++y_pos; break; - default : exit(EXIT_FAILURE); // should not occur + // add new open point if the current point is still a wall + if ((*map)[y_pos * WIDTH + x_pos] == WALL) { + (*open_tiles)[*num_open_tiles].x = x_pos; + (*open_tiles)[*num_open_tiles].y = y_pos; + ++(*num_open_tiles); } - } - } - for (int i = 0; i < NUM_WALKERS; ++i) { - struct point curr_point = (*open)[rand() % *open_tiles]; - int x_pos = curr_point.x; - int y_pos = curr_point.y; + (*map)[y_pos * WIDTH + x_pos] = GROUND; // assign ground - for (int j = 1; j < MAX_STEPS - 1 && x_pos < WIDTH - 1 && x_pos >= 1 && - y_pos < HEIGHT - 1 && y_pos >= 1; - ++j) { - if (!((*map)[y_pos * WIDTH + x_pos])) { - (*map)[y_pos * WIDTH + x_pos] = true; - struct point p = {.x = x_pos, .y = y_pos}; - (*open)[(*open_tiles)++] = p; - } + // move in a random direction enum direction dir = rand() % NUM_DIRS; switch (dir) { case UP : --y_pos; break; @@ -82,4 +70,18 @@ void create_cave(bool **map, struct point **open, int *open_tiles) } } } + + // assign the up stair and remove from open tiles + int in = rand() % *num_open_tiles; + *up = (*open_tiles)[in]; + (*open_tiles)[in] = (*open_tiles)[*num_open_tiles - 1]; + --(*num_open_tiles); + (*map)[up->y * WIDTH + up->x] = UP_STAIR; + + // assign the down stair and remove from open tiles + in = rand() % *num_open_tiles; + *down = (*open_tiles)[in]; + (*open_tiles)[in] = (*open_tiles)[*num_open_tiles - 1]; + --(*num_open_tiles); + (*map)[down->y * WIDTH + down->x] = DOWN_STAIR; } -- cgit v1.2.3