… | |
… | |
14 | int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */ |
14 | int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */ |
15 | |
15 | |
16 | int rtype; /* circle or rectangular */ |
16 | int rtype; /* circle or rectangular */ |
17 | } Room; |
17 | } Room; |
18 | |
18 | |
19 | static int roguelike_place_room (Room * Rooms, int xsize, int ysize, int nrooms); |
19 | static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms); |
20 | static void roguelike_make_rooms (Room * Rooms, char **maze, int options); |
20 | static void roguelike_make_rooms (Room *rooms, char **maze, int options); |
21 | static void roguelike_link_rooms (Room * Rooms, char **maze, int xsize, int ysize); |
21 | static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); |
22 | |
|
|
23 | |
22 | |
24 | int |
23 | int |
25 | surround_check (char **layout, int i, int j, int Xsize, int Ysize) |
24 | surround_check (char **layout, int i, int j, int Xsize, int Ysize) |
26 | { |
25 | { |
27 | /* 1 = wall to left, |
26 | /* 1 = wall to left, |
28 | 2 = wall to right, |
27 | 2 = wall to right, |
29 | 4 = wall above |
28 | 4 = wall above |
30 | 8 = wall below */ |
29 | 8 = wall below */ |
31 | int surround_index = 0; |
30 | int surround_index = 0; |
32 | |
31 | |
33 | if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) |
32 | if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) surround_index |= 1; |
34 | surround_index += 1; |
|
|
35 | if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) |
33 | if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) surround_index |= 2; |
36 | surround_index += 2; |
|
|
37 | if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) |
34 | if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) surround_index |= 4; |
38 | surround_index += 4; |
|
|
39 | if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) |
35 | if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) surround_index |= 8; |
40 | surround_index += 8; |
36 | |
41 | return surround_index; |
37 | return surround_index; |
42 | } |
38 | } |
43 | |
|
|
44 | |
39 | |
45 | /* actually make the layout: we work by a reduction process: |
40 | /* actually make the layout: we work by a reduction process: |
46 | * first we make everything a wall, then we remove areas to make rooms |
41 | * first we make everything a wall, then we remove areas to make rooms |
47 | */ |
42 | */ |
48 | |
43 | void |
49 | char ** |
44 | roguelike_layout_gen (Layout maze, int options) |
50 | roguelike_layout_gen (int xsize, int ysize, int options) |
|
|
51 | { |
45 | { |
52 | int i, j; |
46 | int i, j; |
53 | Room *Rooms = 0; |
|
|
54 | Room *walk; |
47 | Room *walk; |
55 | int nrooms = 0; |
48 | int nrooms = 0; |
56 | int tries = 0; |
49 | int tries = 0; |
57 | |
50 | |
58 | /* allocate that array, write walls everywhere up */ |
51 | int xsize = maze->w; |
59 | char **maze = (char **) malloc (sizeof (char *) * xsize); |
52 | int ysize = maze->h; |
60 | |
|
|
61 | for (i = 0; i < xsize; i++) |
|
|
62 | { |
|
|
63 | maze[i] = (char *) malloc (sizeof (char) * ysize); |
|
|
64 | for (j = 0; j < ysize; j++) |
|
|
65 | maze[i][j] = '#'; |
|
|
66 | } |
|
|
67 | |
53 | |
68 | /* minimum room size is basically 5x5: if xsize/ysize is |
54 | /* minimum room size is basically 5x5: if xsize/ysize is |
69 | less than 3x that then hollow things out, stick in |
55 | less than 3x that then hollow things out, stick in |
70 | a stairsup and stairs down, and exit */ |
56 | a stairsup and stairs down, and exit */ |
71 | |
|
|
72 | if (xsize < 11 || ysize < 11) |
57 | if (xsize < 11 || ysize < 11) |
73 | { |
58 | { |
74 | for (i = 1; i < xsize - 1; i++) |
59 | maze->clear (); |
75 | for (j = 1; j < ysize - 1; j++) |
60 | maze->border (); |
76 | maze[i][j] = 0; |
61 | |
77 | maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; |
62 | maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
78 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
63 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
|
|
64 | |
79 | return maze; |
65 | return; |
80 | } |
66 | } |
|
|
67 | |
|
|
68 | maze->clear ('#'); |
81 | |
69 | |
82 | /* decide on the number of rooms */ |
70 | /* decide on the number of rooms */ |
83 | nrooms = RANDOM () % 10 + 6; |
71 | nrooms = rmg_rndm (10) + 6; |
84 | Rooms = (Room *) calloc (nrooms + 1, sizeof (Room)); |
72 | Room *rooms = salloc0<Room> (nrooms + 1); |
85 | |
73 | |
86 | /* actually place the rooms */ |
74 | /* actually place the rooms */ |
87 | i = 0; |
75 | i = 0; |
88 | while (tries < 450 && i < nrooms) |
76 | while (tries < 450 && i < nrooms) |
89 | { |
77 | { |
90 | /* try to place the room */ |
78 | /* try to place the room */ |
91 | if (!roguelike_place_room (Rooms, xsize, ysize, nrooms)) |
79 | if (!roguelike_place_room (rooms, xsize, ysize, nrooms)) |
92 | tries++; |
80 | tries++; |
93 | else |
81 | else |
94 | i++; |
82 | i++; |
95 | } |
83 | } |
96 | |
84 | |
97 | if (i == 0) |
85 | if (i == 0) /* no can do! */ |
98 | { /* no can do! */ |
86 | { |
99 | for (i = 1; i < xsize - 1; i++) |
87 | maze->clear (); |
100 | for (j = 1; j < ysize - 1; j++) |
88 | maze->border (); |
101 | maze[i][j] = 0; |
89 | |
102 | maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; |
90 | maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
103 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
91 | maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
104 | free (Rooms); |
92 | |
|
|
93 | sfree (rooms, nrooms + 1); |
105 | return maze; |
94 | return; |
106 | } |
95 | } |
107 | |
|
|
108 | |
96 | |
109 | /* erase the areas occupied by the rooms */ |
97 | /* erase the areas occupied by the rooms */ |
110 | roguelike_make_rooms (Rooms, maze, options); |
98 | roguelike_make_rooms (rooms, maze, options); |
111 | |
99 | |
112 | roguelike_link_rooms (Rooms, maze, xsize, ysize); |
100 | roguelike_link_rooms (rooms, maze, xsize, ysize); |
113 | |
101 | |
114 | /* put in the stairs */ |
102 | /* put in the stairs */ |
115 | |
103 | |
116 | maze[Rooms->x][Rooms->y] = '<'; |
104 | maze[rooms->x][rooms->y] = '<'; |
|
|
105 | |
117 | /* get the last one */ |
106 | /* get the last one */ |
118 | for (walk = Rooms; walk->x != 0; walk++); |
107 | for (walk = rooms; walk->x != 0; walk++) |
|
|
108 | ; |
|
|
109 | |
119 | /* back up one */ |
110 | /* back up one */ |
120 | walk--; |
111 | walk--; |
|
|
112 | |
121 | if (walk == Rooms) |
113 | if (walk == rooms) |
122 | { |
114 | { |
123 | /* In this case, there is only a single room. We don't want to |
115 | /* In this case, there is only a single room. We don't want to |
124 | * clobber are up exit (above) with a down exit, so put the |
116 | * clobber are up exit (above) with a down exit, so put the |
125 | * other exit one space up/down, depending which is a space |
117 | * other exit one space up/down, depending which is a space |
126 | * and not a wall. |
118 | * and not a wall. |
… | |
… | |
137 | for (i = 0; i < xsize; i++) |
129 | for (i = 0; i < xsize; i++) |
138 | for (j = 0; j < ysize; j++) |
130 | for (j = 0; j < ysize; j++) |
139 | { |
131 | { |
140 | if (maze[i][j] == '.') |
132 | if (maze[i][j] == '.') |
141 | maze[i][j] = 0; |
133 | maze[i][j] = 0; |
|
|
134 | |
142 | if (maze[i][j] == 'D') |
135 | if (maze[i][j] == 'D') |
143 | { /* remove bad door. */ |
136 | { /* remove bad door. */ |
144 | int si = surround_check (maze, i, j, xsize, ysize); |
137 | int si = surround_check (maze, i, j, xsize, ysize); |
145 | |
138 | |
146 | if (si != 3 && si != 12) |
139 | if (si != 3 && si != 12) |
… | |
… | |
151 | j = 0; |
144 | j = 0; |
152 | } |
145 | } |
153 | } |
146 | } |
154 | } |
147 | } |
155 | |
148 | |
156 | free (Rooms); |
149 | sfree (rooms, nrooms + 1); |
157 | return maze; |
|
|
158 | } |
150 | } |
159 | |
|
|
160 | |
|
|
161 | |
151 | |
162 | static int |
152 | static int |
163 | roguelike_place_room (Room * Rooms, int xsize, int ysize, int nrooms) |
153 | roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms) |
164 | { |
154 | { |
165 | |
|
|
166 | int tx, ty; /* trial center locations */ |
155 | int tx, ty; /* trial center locations */ |
167 | int sx, sy; /* trial sizes */ |
156 | int sx, sy; /* trial sizes */ |
168 | int ax, ay; /* min coords of rect */ |
157 | int ax, ay; /* min coords of rect */ |
169 | int zx, zy; /* max coords of rect */ |
158 | int zx, zy; /* max coords of rect */ |
170 | int x_basesize; |
159 | int x_basesize; |
171 | int y_basesize; |
160 | int y_basesize; |
172 | Room *walk; |
161 | Room *walk; |
173 | |
162 | |
174 | /* decide on the base x and y sizes */ |
163 | /* decide on the base x and y sizes */ |
175 | |
|
|
176 | x_basesize = xsize / isqrt (nrooms); |
164 | x_basesize = xsize / isqrt (nrooms); |
177 | y_basesize = ysize / isqrt (nrooms); |
165 | y_basesize = ysize / isqrt (nrooms); |
178 | |
166 | |
179 | |
167 | tx = rmg_rndm (xsize); |
180 | tx = RANDOM () % xsize; |
168 | ty = rmg_rndm (ysize); |
181 | ty = RANDOM () % ysize; |
|
|
182 | |
169 | |
183 | /* generate a distribution of sizes centered about basesize */ |
170 | /* generate a distribution of sizes centered about basesize */ |
184 | sx = (RANDOM () % x_basesize) + (RANDOM () % x_basesize) + (RANDOM () % x_basesize); |
171 | sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); |
185 | sy = (RANDOM () % y_basesize) + (RANDOM () % y_basesize) + (RANDOM () % y_basesize); |
172 | sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); |
186 | sy = (int) (sy * .5); /* renormalize */ |
173 | sy = (int) (sy * .5); /* renormalize */ |
187 | |
174 | |
188 | /* find the corners */ |
175 | /* find the corners */ |
189 | ax = tx - sx / 2; |
176 | ax = tx - sx / 2; |
190 | zx = tx + sx / 2 + sx % 2; |
177 | zx = tx + sx / 2 + sx % 2; |
191 | |
178 | |
192 | ay = ty - sy / 2; |
179 | ay = ty - sy / 2; |
193 | zy = ty + sy / 2 + sy % 2; |
180 | zy = ty + sy / 2 + sy % 2; |
194 | |
181 | |
195 | /* check to see if it's in the map */ |
182 | /* check to see if it's in the map */ |
196 | if (zx > xsize - 1 || ax < 1) |
183 | if (zx > xsize - 1 || ax < 1) return 0; |
197 | return 0; |
|
|
198 | if (zy > ysize - 1 || ay < 1) |
184 | if (zy > ysize - 1 || ay < 1) return 0; |
199 | return 0; |
|
|
200 | |
185 | |
201 | /* no small fish */ |
186 | /* no small fish */ |
202 | if (sx < 3 || sy < 3) |
187 | if (sx < 3 || sy < 3) |
203 | return 0; |
188 | return 0; |
204 | |
189 | |
205 | /* check overlap with existing rooms */ |
190 | /* check overlap with existing rooms */ |
206 | for (walk = Rooms; walk->x != 0; walk++) |
191 | for (walk = rooms; walk->x != 0; walk++) |
207 | { |
192 | { |
208 | int dx = abs (tx - walk->x); |
193 | int dx = abs (tx - walk->x); |
209 | int dy = abs (ty - walk->y); |
194 | int dy = abs (ty - walk->y); |
210 | |
195 | |
211 | if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2)) |
196 | if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2)) |
… | |
… | |
213 | } |
198 | } |
214 | |
199 | |
215 | /* if we've got here, presumably the room is OK. */ |
200 | /* if we've got here, presumably the room is OK. */ |
216 | |
201 | |
217 | /* get a pointer to the first free room */ |
202 | /* get a pointer to the first free room */ |
218 | for (walk = Rooms; walk->x != 0; walk++); |
203 | for (walk = rooms; walk->x != 0; walk++) |
|
|
204 | ; |
|
|
205 | |
219 | walk->x = tx; |
206 | walk->x = tx; |
220 | walk->y = ty; |
207 | walk->y = ty; |
221 | walk->sx = sx; |
208 | walk->sx = sx; |
222 | walk->sy = sy; |
209 | walk->sy = sy; |
223 | walk->ax = ax; |
210 | walk->ax = ax; |
224 | walk->ay = ay; |
211 | walk->ay = ay; |
225 | walk->zx = zx; |
212 | walk->zx = zx; |
226 | walk->zy = zy; |
213 | walk->zy = zy; |
|
|
214 | |
227 | return 1; /* success */ |
215 | return 1; /* success */ |
228 | |
|
|
229 | } |
216 | } |
230 | |
|
|
231 | |
217 | |
232 | /* write all the rooms into the maze */ |
218 | /* write all the rooms into the maze */ |
233 | static void |
219 | static void |
234 | roguelike_make_rooms (Room * Rooms, char **maze, int options) |
220 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
235 | { |
221 | { |
236 | int making_circle = 0; |
222 | int making_circle = 0; |
237 | int i, j; |
223 | int i, j; |
238 | int R; |
224 | int R; |
239 | Room *walk; |
225 | Room *walk; |
240 | |
226 | |
241 | for (walk = Rooms; walk->x != 0; walk++) |
227 | for (walk = rooms; walk->x != 0; walk++) |
242 | { |
228 | { |
243 | /* first decide what shape to make */ |
229 | /* first decide what shape to make */ |
244 | switch (options) |
230 | switch (options) |
245 | { |
231 | { |
246 | case 1: |
232 | case 1: |
247 | making_circle = 0; |
233 | making_circle = 0; |
248 | break; |
234 | break; |
249 | case 2: |
235 | case 2: |
250 | making_circle = 1; |
236 | making_circle = 1; |
251 | break; |
237 | break; |
252 | default: |
238 | default: |
253 | making_circle = ((RANDOM () % 3 == 0) ? 1 : 0); |
239 | making_circle = ((rmg_rndm (3) == 0) ? 1 : 0); |
254 | break; |
240 | break; |
255 | } |
241 | } |
256 | |
242 | |
257 | if (walk->sx < walk->sy) |
243 | if (walk->sx < walk->sy) |
258 | R = walk->sx / 2; |
244 | R = walk->sx / 2; |
259 | else |
245 | else |
260 | R = walk->sy / 2; |
246 | R = walk->sy / 2; |
261 | |
247 | |
262 | /* enscribe a rectangle */ |
248 | /* enscribe a rectangle */ |
263 | for (i = walk->ax; i < walk->zx; i++) |
249 | for (i = walk->ax; i < walk->zx; i++) |
264 | for (j = walk->ay; j < walk->zy; j++) |
250 | for (j = walk->ay; j < walk->zy; j++) |
265 | { |
|
|
266 | if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) |
251 | if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) |
267 | maze[i][j] = '.'; |
252 | maze[i][j] = '.'; |
268 | } |
|
|
269 | } |
253 | } |
270 | } |
254 | } |
271 | |
|
|
272 | |
|
|
273 | |
255 | |
274 | static void |
256 | static void |
275 | roguelike_link_rooms (Room * Rooms, char **maze, int xsize, int ysize) |
257 | roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) |
276 | { |
258 | { |
277 | Room *walk; |
259 | Room *walk; |
278 | int i, j; |
260 | int i, j; |
279 | |
261 | |
280 | /* link each room to the previous room */ |
262 | /* link each room to the previous room */ |
281 | if (Rooms[1].x == 0) |
263 | if (rooms[1].x == 0) |
282 | return; /* only 1 room */ |
264 | return; /* only 1 room */ |
283 | |
265 | |
284 | for (walk = Rooms + 1; walk->x != 0; walk++) |
266 | for (walk = rooms + 1; walk->x != 0; walk++) |
285 | { |
267 | { |
286 | int x1 = walk->x; |
268 | int x1 = walk->x; |
287 | int y1 = walk->y; |
269 | int y1 = walk->y; |
288 | int x2 = (walk - 1)->x; |
270 | int x2 = (walk - 1)->x; |
289 | int y2 = (walk - 1)->y; |
271 | int y2 = (walk - 1)->y; |
290 | int in_wall = 0; |
272 | int in_wall = 0; |
291 | |
273 | |
292 | if (RANDOM () % 2) |
274 | if (rmg_rndm (2)) |
293 | { /* connect in x direction first */ |
275 | { /* connect in x direction first */ |
294 | /* horizontal connect */ |
276 | /* horizontal connect */ |
295 | /* swap (x1,y1) (x2,y2) if necessary */ |
277 | /* swap (x1,y1) (x2,y2) if necessary */ |
296 | |
278 | |
297 | if (x2 < x1) |
279 | if (x2 < x1) |
… | |
… | |
398 | |
380 | |
399 | } |
381 | } |
400 | |
382 | |
401 | } |
383 | } |
402 | } |
384 | } |
|
|
385 | |