… | |
… | |
38 | #include "rproto.h" |
38 | #include "rproto.h" |
39 | |
39 | |
40 | /* These are some helper functions which help with |
40 | /* These are some helper functions which help with |
41 | manipulating a centered onion and turning it into |
41 | manipulating a centered onion and turning it into |
42 | a square spiral */ |
42 | a square spiral */ |
43 | |
|
|
44 | /* this starts from within a centered onion layer (or between two layers), |
|
|
45 | and looks up until it finds a wall, and then looks right until it |
|
|
46 | finds a vertical wall, i.e., the corner. It sets cx and cy to that. |
|
|
47 | it also starts from cx and cy. */ |
|
|
48 | |
|
|
49 | void |
|
|
50 | find_top_left_corner (char **maze, int *cx, int *cy) |
|
|
51 | { |
|
|
52 | --(*cy); |
|
|
53 | |
|
|
54 | /* find the top wall. */ |
|
|
55 | while (maze[*cx][*cy] == 0) |
|
|
56 | --(*cy); |
|
|
57 | |
|
|
58 | /* proceed right until a corner is detected */ |
|
|
59 | while (maze[*cx][*cy + 1] == 0) |
|
|
60 | ++(*cx); |
|
|
61 | |
|
|
62 | /* cx and cy should now be the top-right corner of the onion layer */ |
|
|
63 | } |
|
|
64 | |
|
|
65 | void |
43 | void |
66 | make_square_spiral_layout (Layout maze, int options) |
44 | make_square_spiral_layout (Layout maze, int options) |
67 | { |
45 | { |
68 | int i, j; |
|
|
69 | int cx, cy; |
|
|
70 | int tx, ty; |
|
|
71 | |
|
|
72 | /* generate and allocate a doorless, centered onion */ |
46 | /* generate and allocate a doorless, centered onion */ |
73 | map_gen_onion (maze, RMOPT_CENTERED | RMOPT_NO_DOORS, 0); |
47 | map_gen_onion (maze, RMOPT_CENTERED | RMOPT_NO_DOORS, 0); |
74 | |
48 | |
75 | int xsize = maze->w; |
49 | int xsize = maze->w; |
76 | int ysize = maze->h; |
50 | int ysize = maze->h; |
77 | |
51 | |
78 | /* find the layout center. */ |
52 | /* find the layout center. */ |
79 | cx = 0; |
53 | int cx = 0; |
80 | cy = 0; |
54 | int cy = 0; |
81 | for (i = 0; i < xsize; i++) |
55 | for (int i = 0; i < xsize; i++) |
82 | for (j = 0; j < ysize; j++) |
56 | for (int j = 0; j < ysize; j++) |
83 | if (maze[i][j] == 'C') |
57 | if (maze[i][j] == 'C') |
84 | { |
58 | { |
85 | cx = i; |
59 | cx = i; |
86 | cy = j; |
60 | cy = j; |
|
|
61 | break; |
87 | } |
62 | } |
88 | |
63 | |
89 | tx = cx; |
64 | int tx = cx; |
90 | ty = cy; |
65 | int ty = cy; |
91 | for (;;) |
66 | for (;;) |
92 | { |
67 | { |
93 | find_top_left_corner (maze, &tx, &ty); |
68 | /* this starts from within a centered onion layer (or between two layers), |
|
|
69 | and looks up until it finds a wall, and then looks right until it |
|
|
70 | finds a vertical wall, i.e., the corner. It sets tx and ty to that. |
|
|
71 | it also starts from tx and ty. */ |
|
|
72 | --ty; |
94 | |
73 | |
|
|
74 | /* find the top wall. */ |
|
|
75 | while (maze[tx][ty] != '#') |
|
|
76 | --ty; |
|
|
77 | |
|
|
78 | /* proceed right until a corner is detected */ |
|
|
79 | while (maze[tx][ty + 1] != '#') |
|
|
80 | ++tx; |
|
|
81 | |
|
|
82 | /* tx and ty should now be the top-right corner of the onion layer */ |
95 | if (ty < 2 || tx < 2 || tx > xsize - 2 || ty > ysize - 2) |
83 | if (ty < 2 || tx < 2 || tx > xsize - 2 || ty > ysize - 2) |
96 | break; |
84 | break; |
97 | |
85 | |
98 | make_wall (maze, tx, ty - 1, 1); /* make a vertical wall with a door */ |
86 | make_wall (maze, tx, ty - 1, 1); /* make a vertical wall with a door */ |
99 | |
87 | |
100 | maze[tx][ty - 1] = '#'; /* convert the door that make_wall puts here to a wall */ |
88 | maze[tx][ty - 1] = '#'; /* convert the door that make_wall puts here to a wall */ |
101 | maze[tx - 1][ty] = 'D'; /* make a doorway out of this layer */ |
89 | maze[tx - 1][ty] = 'D'; /* make a doorway out of this layer */ |
102 | |
90 | |
103 | /* walk left until we find the top-left corner */ |
91 | /* walk left until we find the top-left corner */ |
104 | while ((tx > 2) && maze[tx - 1, ty]) |
92 | while (tx > 2 && maze[tx - 1][ty]) |
105 | tx--; |
93 | --tx; |
106 | |
94 | |
107 | make_wall (maze, tx - 1, ty, 0); /* make a horizontal wall with a door */ |
95 | make_wall (maze, tx - 1, ty, 0); /* make a horizontal wall with a door */ |
108 | |
96 | |
109 | /* walk down until we find the bottom-left corner */ |
97 | /* walk down until we find the bottom-left corner */ |
110 | while (((ty + 1) < ysize) && maze[tx][ty + 1]) |
98 | while (ty + 1 < ysize && maze[tx][ty + 1]) |
111 | ty++; |
99 | ++ty; |
112 | |
100 | |
113 | make_wall (maze, tx, ty + 1, 1); /* make a vertical wall with a door */ |
101 | make_wall (maze, tx, ty + 1, 1); /* make a vertical wall with a door */ |
114 | |
102 | |
115 | /* walk rightuntil we find the bottom-right corner */ |
103 | /* walk rightuntil we find the bottom-right corner */ |
116 | while (((tx + 1) < xsize) && maze[tx + 1][ty]) |
104 | while (tx + 1 < xsize && maze[tx + 1][ty]) |
117 | tx++; |
105 | ++tx; |
118 | |
106 | |
119 | make_wall (maze, tx + 1, ty, 0); /* make a horizontal wall with a door */ |
107 | make_wall (maze, tx + 1, ty, 0); /* make a horizontal wall with a door */ |
120 | tx++; /* set up for next layer. */ |
108 | ++tx; /* set up for next layer. */ |
121 | } |
109 | } |
122 | |
110 | |
123 | /* place the exits. */ |
111 | /* place the exits. */ |
124 | |
112 | |
125 | if (rndm (2)) |
113 | if (rndm (2)) |