--- deliantra/server/random_maps/maze_gen.C 2006/09/10 16:06:37 1.3 +++ deliantra/server/random_maps/maze_gen.C 2008/04/11 21:09:53 1.8 @@ -1,5 +1,4 @@ - /* peterm@langmuir.eecs.berkeley.edu: this function generates a random blocked maze with the property that there is only one path from one spot to any other, and there is always a path from one spot to any other. @@ -16,17 +15,10 @@ reasonable mazes: a straightforward recursive random walk maze generator would generate a map with a trivial circle-the-outer-wall solution */ -#include #include -/*#include */ -#include -#include - - -/* this include solely, and only, is needed for the definition of RANDOM */ - - +#include "random_map.h" +#include "rproto.h" /* global variables that everyone needs: don't want to pass them in as parameters every time. */ @@ -43,25 +35,16 @@ ** maze. option is a flag for either a sparse or a full maze. Sparse mazes have sizable rooms. option = 1, full, 0, sparse.*/ -char ** +Maze maze_gen (int xsize, int ysize, int option) { int i, j; - /* allocate that array, set it up */ - char **maze = (char **) calloc (sizeof (char *), xsize); - - for (i = 0; i < xsize; i++) - { - maze[i] = (char *) calloc (sizeof (char), ysize); - } + Maze maze (xsize, ysize); /* write the outer walls */ - for (i = 0; i < xsize; i++) - maze[i][0] = maze[i][ysize - 1] = '#'; - for (j = 0; j < ysize; j++) - maze[0][j] = maze[xsize - 1][j] = '#'; - + for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#'; + for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#'; /* find how many free wall spots there are */ wall_free_size = 2 * (xsize - 4) + 2 * (ysize - 4); @@ -77,6 +60,7 @@ while (wall_free_size > 0) { pop_wall_point (&i, &j); + if (option) fill_maze_full (maze, i, j, xsize, ysize); else @@ -91,11 +75,8 @@ return maze; } - - /* the free wall points are those outer points which aren't corners or near corners, and don't have a maze wall growing out of them already. */ - void make_wall_free_list (int xsize, int ysize) { @@ -105,9 +86,9 @@ /*allocate it */ if (wall_free_size < 0) return; - wall_x_list = (int *) calloc (sizeof (int), wall_free_size); - wall_y_list = (int *) calloc (sizeof (int), wall_free_size); + wall_x_list = (int *)calloc (sizeof (int), wall_free_size); + wall_y_list = (int *)calloc (sizeof (int), wall_free_size); /* top and bottom wall */ for (i = 2; i < xsize - 2; i++) @@ -132,14 +113,11 @@ } } - - /* randomly returns one of the elements from the wall point list */ - void pop_wall_point (int *x, int *y) { - int index = RANDOM () % wall_free_size; + int index = rndm (wall_free_size); *x = wall_x_list[index]; *y = wall_y_list[index]; @@ -149,12 +127,9 @@ wall_free_size--; } - - /* find free point: randomly look for a square adjacent to this one where we can place a new block without closing a path. We may only look up, down, right, or left. */ - int find_free_point (char **maze, int *x, int *y, int xc, int yc, int xsize, int ysize) { @@ -227,39 +202,39 @@ /* choose a random direction */ if (count > 1) - count = RANDOM () % count; + count = rndm (count); else count = 0; + switch (dirlist[count]) { - case 1: /* up */ - { - *y = yc + 1; - *x = xc; - break; - }; - case 2: /* down */ - { - *y = yc - 1; - *x = xc; - break; - }; - case 3: /* right */ - { - *y = yc; - *x = xc + 1; - break; - } - case 4: /* left */ - { - *x = xc - 1; - *y = yc; - break; - } - default: /* ??? */ - { - return -1; - } + case 1: /* up */ + { + *y = yc + 1; + *x = xc; + break; + }; + case 2: /* down */ + { + *y = yc - 1; + *x = xc; + break; + }; + case 3: /* right */ + { + *y = yc; + *x = xc + 1; + break; + } + case 4: /* left */ + { + *x = xc - 1; + *y = yc; + break; + } + default: /* ??? */ + return -1; + } return 1; } @@ -276,7 +251,7 @@ maze[x][y] = '#'; /* decide if we're going to pick from the wall_free_list */ - if (RANDOM () % 4 && wall_free_size > 0) + if (rndm (4) && wall_free_size > 0) { pop_wall_point (&xc, &yc); fill_maze_full (maze, xc, yc, xsize, ysize); @@ -289,10 +264,8 @@ } } - /* recursive routine which will fill much of the maze, but will leave some free spots (possibly large) toward the center.*/ - void fill_maze_sparse (char **maze, int x, int y, int xsize, int ysize) { @@ -302,7 +275,7 @@ maze[x][y] = '#'; /* decide if we're going to pick from the wall_free_list */ - if (RANDOM () % 4 && wall_free_size > 0) + if (rndm (4) && wall_free_size > 0) { pop_wall_point (&xc, &yc); fill_maze_sparse (maze, xc, yc, xsize, ysize); @@ -310,7 +283,5 @@ /* change the if to a while for a complete maze. */ if (find_free_point (maze, &xc, &yc, x, y, xsize, ysize) != -1) - { - fill_maze_sparse (maze, xc, yc, xsize, ysize); - } + fill_maze_sparse (maze, xc, yc, xsize, ysize); }