1 | |
|
|
2 | /* |
1 | /* |
3 | * static char *room_gen_onion_c = |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
4 | * "$Id: room_gen_onion.C,v 1.3 2006/09/10 16:06:37 root Exp $"; |
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team |
|
|
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
|
|
7 | * |
|
|
8 | * Deliantra is free software: you can redistribute it and/or modify |
|
|
9 | * it under the terms of the GNU General Public License as published by |
|
|
10 | * the Free Software Foundation, either version 3 of the License, or |
|
|
11 | * (at your option) any later version. |
|
|
12 | * |
|
|
13 | * This program is distributed in the hope that it will be useful, |
|
|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
16 | * GNU General Public License for more details. |
|
|
17 | * |
|
|
18 | * You should have received a copy of the GNU General Public License |
|
|
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
5 | */ |
22 | */ |
6 | |
|
|
7 | /* |
|
|
8 | CrossFire, A Multiplayer game for X-windows |
|
|
9 | |
|
|
10 | Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
|
|
11 | Copyright (C) 1992 Frank Tore Johansen |
|
|
12 | |
|
|
13 | This program is free software; you can redistribute it and/or modify |
|
|
14 | it under the terms of the GNU General Public License as published by |
|
|
15 | the Free Software Foundation; either version 2 of the License, or |
|
|
16 | (at your option) any later version. |
|
|
17 | |
|
|
18 | This program is distributed in the hope that it will be useful, |
|
|
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
21 | GNU General Public License for more details. |
|
|
22 | |
|
|
23 | You should have received a copy of the GNU General Public License |
|
|
24 | along with this program; if not, write to the Free Software |
|
|
25 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
|
26 | |
|
|
27 | The authors can be reached via e-mail at crossfire-devel@real-time.com |
|
|
28 | */ |
|
|
29 | |
|
|
30 | |
23 | |
31 | /* The onion room generator: |
24 | /* The onion room generator: |
32 | Onion rooms are like this: |
25 | Onion rooms are like this: |
33 | |
26 | |
34 | char **map_gen_onion(int xsize, int ysize, int option, int layers); |
27 | char **map_gen_onion(int xsize, int ysize, int option, int layers); |
… | |
… | |
84 | } |
77 | } |
85 | |
78 | |
86 | /* pick some random options if option = 0 */ |
79 | /* pick some random options if option = 0 */ |
87 | if (option == 0) |
80 | if (option == 0) |
88 | { |
81 | { |
89 | switch (RANDOM () % 3) |
82 | switch (rndm (3)) |
90 | { |
83 | { |
91 | case 0: |
84 | case 0: |
92 | option |= OPT_CENTERED; |
85 | option |= RMOPT_CENTERED; |
93 | break; |
86 | break; |
94 | case 1: |
87 | case 1: |
95 | option |= OPT_BOTTOM_C; |
88 | option |= RMOPT_BOTTOM_C; |
96 | break; |
89 | break; |
97 | case 2: |
90 | case 2: |
98 | option |= OPT_BOTTOM_R; |
91 | option |= RMOPT_BOTTOM_R; |
99 | break; |
92 | break; |
100 | } |
93 | } |
101 | if (RANDOM () % 2) |
94 | if (rndm (2)) |
102 | option |= OPT_LINEAR; |
95 | option |= RMOPT_LINEAR; |
103 | if (RANDOM () % 2) |
96 | if (rndm (2)) |
104 | option |= OPT_IRR_SPACE; |
97 | option |= RMOPT_IRR_SPACE; |
105 | } |
98 | } |
106 | |
99 | |
107 | /* write the outer walls, if appropriate. */ |
100 | /* write the outer walls, if appropriate. */ |
108 | if (!(option & OPT_WALL_OFF)) |
101 | if (!(option & RMOPT_WALL_OFF)) |
109 | { |
102 | { |
110 | for (i = 0; i < xsize; i++) |
103 | for (i = 0; i < xsize; i++) |
111 | maze[i][0] = maze[i][ysize - 1] = '#'; |
104 | maze[i][0] = maze[i][ysize - 1] = '#'; |
112 | for (j = 0; j < ysize; j++) |
105 | for (j = 0; j < ysize; j++) |
113 | maze[0][j] = maze[xsize - 1][j] = '#'; |
106 | maze[0][j] = maze[xsize - 1][j] = '#'; |
114 | }; |
107 | }; |
115 | |
108 | |
116 | if (option & OPT_WALLS_ONLY) |
109 | if (option & RMOPT_WALLS_ONLY) |
117 | return maze; |
110 | return maze; |
118 | |
111 | |
119 | /* pick off the mutually exclusive options */ |
112 | /* pick off the mutually exclusive options */ |
120 | if (option & OPT_BOTTOM_R) |
113 | if (option & RMOPT_BOTTOM_R) |
121 | bottom_right_centered_onion (maze, xsize, ysize, option, layers); |
114 | bottom_right_centered_onion (maze, xsize, ysize, option, layers); |
122 | else if (option & OPT_BOTTOM_C) |
115 | else if (option & RMOPT_BOTTOM_C) |
123 | bottom_centered_onion (maze, xsize, ysize, option, layers); |
116 | bottom_centered_onion (maze, xsize, ysize, option, layers); |
124 | else if (option & OPT_CENTERED) |
117 | else if (option & RMOPT_CENTERED) |
125 | centered_onion (maze, xsize, ysize, option, layers); |
118 | centered_onion (maze, xsize, ysize, option, layers); |
126 | |
119 | |
127 | return maze; |
120 | return maze; |
128 | } |
121 | } |
129 | |
122 | |
130 | void |
123 | void |
131 | centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
124 | centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
|
|
125 | { |
|
|
126 | int i, maxlayers; |
|
|
127 | float *xlocations; |
|
|
128 | float *ylocations; |
|
|
129 | |
|
|
130 | maxlayers = (MIN (xsize, ysize) - 2) / 5; |
|
|
131 | if (!maxlayers) |
|
|
132 | return; /* map too small to onionize */ |
|
|
133 | |
|
|
134 | if (layers > maxlayers) |
|
|
135 | layers = maxlayers; |
|
|
136 | |
|
|
137 | if (layers == 0) |
|
|
138 | layers = rndm (maxlayers) + 1; |
|
|
139 | |
|
|
140 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
|
|
141 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
|
|
142 | |
|
|
143 | /* place all the walls */ |
|
|
144 | if (option & RMOPT_IRR_SPACE) /* randomly spaced */ |
|
|
145 | { |
|
|
146 | int x_spaces_available, y_spaces_available; |
|
|
147 | |
|
|
148 | /* the "extra" spaces available for spacing between layers */ |
|
|
149 | x_spaces_available = (xsize - 2) - 6 * layers + 1; |
|
|
150 | y_spaces_available = (ysize - 2) - 6 * layers + 1; |
|
|
151 | |
|
|
152 | |
|
|
153 | /* pick an initial random pitch */ |
|
|
154 | for (i = 0; i < 2 * layers; i++) |
|
|
155 | { |
|
|
156 | float xpitch = 2, ypitch = 2; |
|
|
157 | |
|
|
158 | if (x_spaces_available > 0) |
|
|
159 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
|
|
160 | |
|
|
161 | if (y_spaces_available > 0) |
|
|
162 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
|
|
163 | |
|
|
164 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
|
|
165 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
|
|
166 | x_spaces_available -= (int) (xpitch - 2); |
|
|
167 | y_spaces_available -= (int) (ypitch - 2); |
|
|
168 | } |
|
|
169 | |
|
|
170 | } |
|
|
171 | if (!(option & RMOPT_IRR_SPACE)) |
|
|
172 | { /* evenly spaced */ |
|
|
173 | float xpitch, ypitch; /* pitch of the onion layers */ |
|
|
174 | |
|
|
175 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
|
|
176 | ypitch = (ysize - 2.0) / (2.0 * layers + 1); |
|
|
177 | xlocations[0] = xpitch; |
|
|
178 | ylocations[0] = ypitch; |
|
|
179 | for (i = 1; i < 2 * layers; i++) |
|
|
180 | { |
|
|
181 | xlocations[i] = xlocations[i - 1] + xpitch; |
|
|
182 | ylocations[i] = ylocations[i - 1] + ypitch; |
|
|
183 | } |
|
|
184 | } |
|
|
185 | |
|
|
186 | /* draw all the onion boxes. */ |
|
|
187 | |
|
|
188 | draw_onion (maze, xlocations, ylocations, layers); |
|
|
189 | make_doors (maze, xlocations, ylocations, layers, option); |
|
|
190 | |
|
|
191 | } |
|
|
192 | |
|
|
193 | void |
|
|
194 | bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
132 | { |
195 | { |
133 | int i, maxlayers; |
196 | int i, maxlayers; |
134 | float *xlocations; |
197 | float *xlocations; |
135 | float *ylocations; |
198 | float *ylocations; |
136 | |
199 | |
… | |
… | |
138 | if (!maxlayers) |
201 | if (!maxlayers) |
139 | return; /* map too small to onionize */ |
202 | return; /* map too small to onionize */ |
140 | if (layers > maxlayers) |
203 | if (layers > maxlayers) |
141 | layers = maxlayers; |
204 | layers = maxlayers; |
142 | if (layers == 0) |
205 | if (layers == 0) |
143 | layers = (RANDOM () % maxlayers) + 1; |
206 | layers = rndm (maxlayers) + 1; |
144 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
207 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
145 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
208 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
146 | |
209 | |
147 | |
210 | |
148 | /* place all the walls */ |
211 | /* place all the walls */ |
149 | if (option & OPT_IRR_SPACE) /* randomly spaced */ |
212 | if (option & RMOPT_IRR_SPACE) /* randomly spaced */ |
150 | { |
213 | { |
151 | int x_spaces_available, y_spaces_available; |
214 | int x_spaces_available, y_spaces_available; |
152 | |
215 | |
153 | /* the "extra" spaces available for spacing between layers */ |
216 | /* the "extra" spaces available for spacing between layers */ |
154 | x_spaces_available = (xsize - 2) - 6 * layers + 1; |
217 | x_spaces_available = (xsize - 2) - 6 * layers + 1; |
155 | y_spaces_available = (ysize - 2) - 6 * layers + 1; |
218 | y_spaces_available = (ysize - 2) - 3 * layers + 1; |
156 | |
219 | |
157 | |
220 | |
158 | /* pick an initial random pitch */ |
221 | /* pick an initial random pitch */ |
159 | for (i = 0; i < 2 * layers; i++) |
222 | for (i = 0; i < 2 * layers; i++) |
160 | { |
223 | { |
161 | float xpitch = 2, ypitch = 2; |
224 | float xpitch = 2, ypitch = 2; |
162 | |
225 | |
163 | if (x_spaces_available > 0) |
226 | if (x_spaces_available > 0) |
164 | xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3; |
227 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
165 | |
228 | |
166 | if (y_spaces_available > 0) |
229 | if (y_spaces_available > 0) |
167 | ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3; |
230 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
|
|
231 | |
168 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
232 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
|
|
233 | if (i < layers) |
169 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
234 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
|
|
235 | else |
|
|
236 | ylocations[i] = ysize - 1; |
|
|
237 | |
170 | x_spaces_available -= (int) (xpitch - 2); |
238 | x_spaces_available -= (int) (xpitch - 2); |
171 | y_spaces_available -= (int) (ypitch - 2); |
239 | y_spaces_available -= (int) (ypitch - 2); |
172 | } |
240 | } |
173 | |
241 | |
174 | } |
242 | } |
175 | if (!(option & OPT_IRR_SPACE)) |
243 | if (!(option & RMOPT_IRR_SPACE)) |
176 | { /* evenly spaced */ |
244 | { /* evenly spaced */ |
177 | float xpitch, ypitch; /* pitch of the onion layers */ |
245 | float xpitch, ypitch; /* pitch of the onion layers */ |
178 | |
246 | |
179 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
247 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
180 | ypitch = (ysize - 2.0) / (2.0 * layers + 1); |
248 | ypitch = (ysize - 2.0) / (layers + 1); |
181 | xlocations[0] = xpitch; |
249 | xlocations[0] = xpitch; |
182 | ylocations[0] = ypitch; |
250 | ylocations[0] = ypitch; |
183 | for (i = 1; i < 2 * layers; i++) |
251 | for (i = 1; i < 2 * layers; i++) |
184 | { |
252 | { |
185 | xlocations[i] = xlocations[i - 1] + xpitch; |
253 | xlocations[i] = xlocations[i - 1] + xpitch; |
|
|
254 | if (i < layers) |
186 | ylocations[i] = ylocations[i - 1] + ypitch; |
255 | ylocations[i] = ylocations[i - 1] + ypitch; |
|
|
256 | else |
|
|
257 | ylocations[i] = ysize - 1; |
187 | } |
258 | } |
188 | } |
259 | } |
189 | |
260 | |
190 | /* draw all the onion boxes. */ |
261 | /* draw all the onion boxes. */ |
191 | |
262 | |
192 | draw_onion (maze, xlocations, ylocations, layers); |
263 | draw_onion (maze, xlocations, ylocations, layers); |
193 | make_doors (maze, xlocations, ylocations, layers, option); |
264 | make_doors (maze, xlocations, ylocations, layers, option); |
194 | |
265 | |
195 | } |
266 | } |
196 | |
267 | |
|
|
268 | |
|
|
269 | /* draw_boxes: draws the lines in the maze defining the onion layers */ |
|
|
270 | |
197 | void |
271 | void |
|
|
272 | draw_onion (char **maze, float *xlocations, float *ylocations, int layers) |
|
|
273 | { |
|
|
274 | int i, j, l; |
|
|
275 | |
|
|
276 | for (l = 0; l < layers; l++) |
|
|
277 | { |
|
|
278 | int x1, x2, y1, y2; |
|
|
279 | |
|
|
280 | /* horizontal segments */ |
|
|
281 | y1 = (int) ylocations[l]; |
|
|
282 | y2 = (int) ylocations[2 * layers - l - 1]; |
|
|
283 | for (i = (int) xlocations[l]; i <= (int) xlocations[2 * layers - l - 1]; i++) |
|
|
284 | { |
|
|
285 | maze[i][y1] = '#'; |
|
|
286 | maze[i][y2] = '#'; |
|
|
287 | } |
|
|
288 | |
|
|
289 | /* vertical segments */ |
|
|
290 | x1 = (int) xlocations[l]; |
|
|
291 | x2 = (int) xlocations[2 * layers - l - 1]; |
|
|
292 | for (j = (int) ylocations[l]; j <= (int) ylocations[2 * layers - l - 1]; j++) |
|
|
293 | { |
|
|
294 | maze[x1][j] = '#'; |
|
|
295 | maze[x2][j] = '#'; |
|
|
296 | } |
|
|
297 | |
|
|
298 | } |
|
|
299 | } |
|
|
300 | |
|
|
301 | void |
|
|
302 | make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options) |
|
|
303 | { |
|
|
304 | int freedoms; /* number of different walls on which we could place a door */ |
|
|
305 | int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */ |
|
|
306 | int l, x1 = 0, x2, y1 = 0, y2; |
|
|
307 | |
|
|
308 | freedoms = 4; /* centered */ |
|
|
309 | if (options & RMOPT_BOTTOM_C) |
|
|
310 | freedoms = 3; |
|
|
311 | if (options & RMOPT_BOTTOM_R) |
|
|
312 | freedoms = 2; |
|
|
313 | if (layers <= 0) |
|
|
314 | return; |
|
|
315 | |
|
|
316 | /* pick which wall will have a door. */ |
|
|
317 | which_wall = rndm (freedoms) + 1; |
|
|
318 | for (l = 0; l < layers; l++) |
|
|
319 | { |
|
|
320 | if (options & RMOPT_LINEAR) |
|
|
321 | { /* linear door placement. */ |
|
|
322 | switch (which_wall) |
|
|
323 | { |
|
|
324 | case 1: |
|
|
325 | { /* left hand wall */ |
|
|
326 | x1 = (int) xlocations[l]; |
|
|
327 | y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2); |
|
|
328 | break; |
|
|
329 | } |
|
|
330 | case 2: |
|
|
331 | { /* top wall placement */ |
|
|
332 | x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2); |
|
|
333 | y1 = (int) ylocations[l]; |
|
|
334 | break; |
|
|
335 | } |
|
|
336 | case 3: |
|
|
337 | { /* right wall placement */ |
|
|
338 | x1 = (int) xlocations[2 * layers - l - 1]; |
|
|
339 | y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2); |
|
|
340 | break; |
|
|
341 | } |
|
|
342 | case 4: |
|
|
343 | { /* bottom wall placement */ |
|
|
344 | x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2); |
|
|
345 | y1 = (int) ylocations[2 * layers - l - 1]; |
|
|
346 | break; |
|
|
347 | } |
|
|
348 | } |
|
|
349 | } |
|
|
350 | else |
|
|
351 | { /* random door placement. */ |
|
|
352 | which_wall = rndm (freedoms) + 1; |
|
|
353 | switch (which_wall) |
|
|
354 | { |
|
|
355 | case 1: |
|
|
356 | { /* left hand wall */ |
|
|
357 | x1 = (int) xlocations[l]; |
|
|
358 | y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1); |
|
|
359 | if (y2 > 0) |
|
|
360 | y1 = (int) (ylocations[l] + rndm (y2) + 1); |
|
|
361 | else |
|
|
362 | y1 = (int) (ylocations[l] + 1); |
|
|
363 | break; |
|
|
364 | } |
|
|
365 | case 2: |
|
|
366 | { /* top wall placement */ |
|
|
367 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
|
|
368 | if (x2 > 0) |
|
|
369 | x1 = (int) (xlocations[l] + rndm (x2) + 1); |
|
|
370 | else |
|
|
371 | x1 = (int) (xlocations[l] + 1); |
|
|
372 | y1 = (int) ylocations[l]; |
|
|
373 | break; |
|
|
374 | } |
|
|
375 | case 3: |
|
|
376 | { /* right wall placement */ |
|
|
377 | x1 = (int) xlocations[2 * layers - l - 1]; |
|
|
378 | y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1; |
|
|
379 | if (y2 > 0) |
|
|
380 | y1 = (int) (ylocations[l] + rndm (y2) + 1); |
|
|
381 | else |
|
|
382 | y1 = (int) (ylocations[l] + 1); |
|
|
383 | |
|
|
384 | break; |
|
|
385 | } |
|
|
386 | case 4: |
|
|
387 | { /* bottom wall placement */ |
|
|
388 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
|
|
389 | if (x2 > 0) |
|
|
390 | x1 = (int) (xlocations[l] + rndm (x2) + 1); |
|
|
391 | else |
|
|
392 | x1 = (int) (xlocations[l] + 1); |
|
|
393 | y1 = (int) ylocations[2 * layers - l - 1]; |
|
|
394 | break; |
|
|
395 | } |
|
|
396 | |
|
|
397 | } |
|
|
398 | } |
|
|
399 | if (options & RMOPT_NO_DOORS) |
|
|
400 | maze[x1][y1] = '#'; /* no door. */ |
|
|
401 | else |
|
|
402 | maze[x1][y1] = 'D'; /* write the door */ |
|
|
403 | |
|
|
404 | } |
|
|
405 | /* mark the center of the maze with a C */ |
|
|
406 | l = layers - 1; |
|
|
407 | x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2; |
|
|
408 | y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2; |
|
|
409 | maze[x1][y1] = 'C'; |
|
|
410 | |
|
|
411 | /* not needed anymore */ |
|
|
412 | free (xlocations); |
|
|
413 | free (ylocations); |
|
|
414 | |
|
|
415 | } |
|
|
416 | |
|
|
417 | void |
198 | bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
418 | bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
199 | { |
419 | { |
200 | int i, maxlayers; |
420 | int i, maxlayers; |
201 | float *xlocations; |
421 | float *xlocations; |
202 | float *ylocations; |
422 | float *ylocations; |
203 | |
423 | |
… | |
… | |
205 | if (!maxlayers) |
425 | if (!maxlayers) |
206 | return; /* map too small to onionize */ |
426 | return; /* map too small to onionize */ |
207 | if (layers > maxlayers) |
427 | if (layers > maxlayers) |
208 | layers = maxlayers; |
428 | layers = maxlayers; |
209 | if (layers == 0) |
429 | if (layers == 0) |
210 | layers = (RANDOM () % maxlayers) + 1; |
430 | layers = rndm (maxlayers) + 1; |
|
|
431 | |
211 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
432 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
212 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
433 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
213 | |
434 | |
214 | |
|
|
215 | /* place all the walls */ |
435 | /* place all the walls */ |
216 | if (option & OPT_IRR_SPACE) /* randomly spaced */ |
436 | if (option & RMOPT_IRR_SPACE) /* randomly spaced */ |
217 | { |
437 | { |
218 | int x_spaces_available, y_spaces_available; |
438 | int x_spaces_available, y_spaces_available; |
219 | |
439 | |
220 | /* the "extra" spaces available for spacing between layers */ |
440 | /* the "extra" spaces available for spacing between layers */ |
221 | x_spaces_available = (xsize - 2) - 6 * layers + 1; |
441 | x_spaces_available = (xsize - 2) - 3 * layers + 1; |
222 | y_spaces_available = (ysize - 2) - 3 * layers + 1; |
442 | y_spaces_available = (ysize - 2) - 3 * layers + 1; |
223 | |
443 | |
224 | |
444 | |
225 | /* pick an initial random pitch */ |
445 | /* pick an initial random pitch */ |
226 | for (i = 0; i < 2 * layers; i++) |
446 | for (i = 0; i < 2 * layers; i++) |
227 | { |
447 | { |
228 | float xpitch = 2, ypitch = 2; |
448 | float xpitch = 2, ypitch = 2; |
229 | |
449 | |
230 | if (x_spaces_available > 0) |
450 | if (x_spaces_available > 0) |
231 | xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3; |
451 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
232 | |
452 | |
233 | if (y_spaces_available > 0) |
453 | if (y_spaces_available > 0) |
234 | ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3; |
454 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
|
|
455 | |
|
|
456 | if (i < layers) |
235 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
457 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
|
|
458 | else |
|
|
459 | xlocations[i] = xsize - 1; |
|
|
460 | |
236 | if (i < layers) |
461 | if (i < layers) |
237 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
462 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
238 | else |
463 | else |
239 | ylocations[i] = ysize - 1; |
464 | ylocations[i] = ysize - 1; |
240 | x_spaces_available -= (int) (xpitch - 2); |
465 | x_spaces_available -= (int) (xpitch - 2); |
241 | y_spaces_available -= (int) (ypitch - 2); |
466 | y_spaces_available -= (int) (ypitch - 2); |
242 | } |
467 | } |
243 | |
468 | |
244 | } |
469 | } |
245 | if (!(option & OPT_IRR_SPACE)) |
470 | if (!(option & RMOPT_IRR_SPACE)) |
246 | { /* evenly spaced */ |
471 | { /* evenly spaced */ |
247 | float xpitch, ypitch; /* pitch of the onion layers */ |
472 | float xpitch, ypitch; /* pitch of the onion layers */ |
248 | |
473 | |
249 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
474 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
250 | ypitch = (ysize - 2.0) / (layers + 1); |
475 | ypitch = (ysize - 2.0) / (layers + 1); |
251 | xlocations[0] = xpitch; |
476 | xlocations[0] = xpitch; |
252 | ylocations[0] = ypitch; |
477 | ylocations[0] = ypitch; |
253 | for (i = 1; i < 2 * layers; i++) |
478 | for (i = 1; i < 2 * layers; i++) |
254 | { |
479 | { |
|
|
480 | if (i < layers) |
255 | xlocations[i] = xlocations[i - 1] + xpitch; |
481 | xlocations[i] = xlocations[i - 1] + xpitch; |
|
|
482 | else |
|
|
483 | xlocations[i] = xsize - 1; |
256 | if (i < layers) |
484 | if (i < layers) |
257 | ylocations[i] = ylocations[i - 1] + ypitch; |
485 | ylocations[i] = ylocations[i - 1] + ypitch; |
258 | else |
486 | else |
259 | ylocations[i] = ysize - 1; |
487 | ylocations[i] = ysize - 1; |
260 | } |
488 | } |
… | |
… | |
264 | |
492 | |
265 | draw_onion (maze, xlocations, ylocations, layers); |
493 | draw_onion (maze, xlocations, ylocations, layers); |
266 | make_doors (maze, xlocations, ylocations, layers, option); |
494 | make_doors (maze, xlocations, ylocations, layers, option); |
267 | |
495 | |
268 | } |
496 | } |
269 | |
|
|
270 | |
|
|
271 | /* draw_boxes: draws the lines in the maze defining the onion layers */ |
|
|
272 | |
|
|
273 | void |
|
|
274 | draw_onion (char **maze, float *xlocations, float *ylocations, int layers) |
|
|
275 | { |
|
|
276 | int i, j, l; |
|
|
277 | |
|
|
278 | for (l = 0; l < layers; l++) |
|
|
279 | { |
|
|
280 | int x1, x2, y1, y2; |
|
|
281 | |
|
|
282 | /* horizontal segments */ |
|
|
283 | y1 = (int) ylocations[l]; |
|
|
284 | y2 = (int) ylocations[2 * layers - l - 1]; |
|
|
285 | for (i = (int) xlocations[l]; i <= (int) xlocations[2 * layers - l - 1]; i++) |
|
|
286 | { |
|
|
287 | maze[i][y1] = '#'; |
|
|
288 | maze[i][y2] = '#'; |
|
|
289 | } |
|
|
290 | |
|
|
291 | /* vertical segments */ |
|
|
292 | x1 = (int) xlocations[l]; |
|
|
293 | x2 = (int) xlocations[2 * layers - l - 1]; |
|
|
294 | for (j = (int) ylocations[l]; j <= (int) ylocations[2 * layers - l - 1]; j++) |
|
|
295 | { |
|
|
296 | maze[x1][j] = '#'; |
|
|
297 | maze[x2][j] = '#'; |
|
|
298 | } |
|
|
299 | |
|
|
300 | } |
|
|
301 | } |
|
|
302 | |
|
|
303 | void |
|
|
304 | make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options) |
|
|
305 | { |
|
|
306 | int freedoms; /* number of different walls on which we could place a door */ |
|
|
307 | int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */ |
|
|
308 | int l, x1 = 0, x2, y1 = 0, y2; |
|
|
309 | |
|
|
310 | freedoms = 4; /* centered */ |
|
|
311 | if (options & OPT_BOTTOM_C) |
|
|
312 | freedoms = 3; |
|
|
313 | if (options & OPT_BOTTOM_R) |
|
|
314 | freedoms = 2; |
|
|
315 | if (layers <= 0) |
|
|
316 | return; |
|
|
317 | /* pick which wall will have a door. */ |
|
|
318 | which_wall = RANDOM () % freedoms + 1; |
|
|
319 | for (l = 0; l < layers; l++) |
|
|
320 | { |
|
|
321 | if (options & OPT_LINEAR) |
|
|
322 | { /* linear door placement. */ |
|
|
323 | switch (which_wall) |
|
|
324 | { |
|
|
325 | case 1: |
|
|
326 | { /* left hand wall */ |
|
|
327 | x1 = (int) xlocations[l]; |
|
|
328 | y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2); |
|
|
329 | break; |
|
|
330 | } |
|
|
331 | case 2: |
|
|
332 | { /* top wall placement */ |
|
|
333 | x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2); |
|
|
334 | y1 = (int) ylocations[l]; |
|
|
335 | break; |
|
|
336 | } |
|
|
337 | case 3: |
|
|
338 | { /* right wall placement */ |
|
|
339 | x1 = (int) xlocations[2 * layers - l - 1]; |
|
|
340 | y1 = (int) ((ylocations[l] + ylocations[2 * layers - l - 1]) / 2); |
|
|
341 | break; |
|
|
342 | } |
|
|
343 | case 4: |
|
|
344 | { /* bottom wall placement */ |
|
|
345 | x1 = (int) ((xlocations[l] + xlocations[2 * layers - l - 1]) / 2); |
|
|
346 | y1 = (int) ylocations[2 * layers - l - 1]; |
|
|
347 | break; |
|
|
348 | } |
|
|
349 | } |
|
|
350 | } |
|
|
351 | else |
|
|
352 | { /* random door placement. */ |
|
|
353 | which_wall = RANDOM () % freedoms + 1; |
|
|
354 | switch (which_wall) |
|
|
355 | { |
|
|
356 | case 1: |
|
|
357 | { /* left hand wall */ |
|
|
358 | x1 = (int) xlocations[l]; |
|
|
359 | y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1); |
|
|
360 | if (y2 > 0) |
|
|
361 | y1 = (int) (ylocations[l] + RANDOM () % y2 + 1); |
|
|
362 | else |
|
|
363 | y1 = (int) (ylocations[l] + 1); |
|
|
364 | break; |
|
|
365 | } |
|
|
366 | case 2: |
|
|
367 | { /* top wall placement */ |
|
|
368 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
|
|
369 | if (x2 > 0) |
|
|
370 | x1 = (int) (xlocations[l] + RANDOM () % x2 + 1); |
|
|
371 | else |
|
|
372 | x1 = (int) (xlocations[l] + 1); |
|
|
373 | y1 = (int) ylocations[l]; |
|
|
374 | break; |
|
|
375 | } |
|
|
376 | case 3: |
|
|
377 | { /* right wall placement */ |
|
|
378 | x1 = (int) xlocations[2 * layers - l - 1]; |
|
|
379 | y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1; |
|
|
380 | if (y2 > 0) |
|
|
381 | y1 = (int) (ylocations[l] + RANDOM () % y2 + 1); |
|
|
382 | else |
|
|
383 | y1 = (int) (ylocations[l] + 1); |
|
|
384 | |
|
|
385 | break; |
|
|
386 | } |
|
|
387 | case 4: |
|
|
388 | { /* bottom wall placement */ |
|
|
389 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
|
|
390 | if (x2 > 0) |
|
|
391 | x1 = (int) (xlocations[l] + RANDOM () % x2 + 1); |
|
|
392 | else |
|
|
393 | x1 = (int) (xlocations[l] + 1); |
|
|
394 | y1 = (int) ylocations[2 * layers - l - 1]; |
|
|
395 | break; |
|
|
396 | } |
|
|
397 | |
|
|
398 | } |
|
|
399 | } |
|
|
400 | if (options & OPT_NO_DOORS) |
|
|
401 | maze[x1][y1] = '#'; /* no door. */ |
|
|
402 | else |
|
|
403 | maze[x1][y1] = 'D'; /* write the door */ |
|
|
404 | |
|
|
405 | } |
|
|
406 | /* mark the center of the maze with a C */ |
|
|
407 | l = layers - 1; |
|
|
408 | x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2; |
|
|
409 | y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2; |
|
|
410 | maze[x1][y1] = 'C'; |
|
|
411 | |
|
|
412 | /* not needed anymore */ |
|
|
413 | free (xlocations); |
|
|
414 | free (ylocations); |
|
|
415 | |
|
|
416 | } |
|
|
417 | |
|
|
418 | void |
|
|
419 | bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
|
|
420 | { |
|
|
421 | int i, maxlayers; |
|
|
422 | float *xlocations; |
|
|
423 | float *ylocations; |
|
|
424 | |
|
|
425 | maxlayers = (MIN (xsize, ysize) - 2) / 5; |
|
|
426 | if (!maxlayers) |
|
|
427 | return; /* map too small to onionize */ |
|
|
428 | if (layers > maxlayers) |
|
|
429 | layers = maxlayers; |
|
|
430 | if (layers == 0) |
|
|
431 | layers = (RANDOM () % maxlayers) + 1; |
|
|
432 | xlocations = (float *) calloc (sizeof (float), 2 * layers); |
|
|
433 | ylocations = (float *) calloc (sizeof (float), 2 * layers); |
|
|
434 | |
|
|
435 | |
|
|
436 | /* place all the walls */ |
|
|
437 | if (option & OPT_IRR_SPACE) /* randomly spaced */ |
|
|
438 | { |
|
|
439 | int x_spaces_available, y_spaces_available; |
|
|
440 | |
|
|
441 | /* the "extra" spaces available for spacing between layers */ |
|
|
442 | x_spaces_available = (xsize - 2) - 3 * layers + 1; |
|
|
443 | y_spaces_available = (ysize - 2) - 3 * layers + 1; |
|
|
444 | |
|
|
445 | |
|
|
446 | /* pick an initial random pitch */ |
|
|
447 | for (i = 0; i < 2 * layers; i++) |
|
|
448 | { |
|
|
449 | float xpitch = 2, ypitch = 2; |
|
|
450 | |
|
|
451 | if (x_spaces_available > 0) |
|
|
452 | xpitch = 2 + (RANDOM () % x_spaces_available + RANDOM () % x_spaces_available + RANDOM () % x_spaces_available) / 3; |
|
|
453 | |
|
|
454 | if (y_spaces_available > 0) |
|
|
455 | ypitch = 2 + (RANDOM () % y_spaces_available + RANDOM () % y_spaces_available + RANDOM () % y_spaces_available) / 3; |
|
|
456 | if (i < layers) |
|
|
457 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
|
|
458 | else |
|
|
459 | xlocations[i] = xsize - 1; |
|
|
460 | |
|
|
461 | if (i < layers) |
|
|
462 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
|
|
463 | else |
|
|
464 | ylocations[i] = ysize - 1; |
|
|
465 | x_spaces_available -= (int) (xpitch - 2); |
|
|
466 | y_spaces_available -= (int) (ypitch - 2); |
|
|
467 | } |
|
|
468 | |
|
|
469 | } |
|
|
470 | if (!(option & OPT_IRR_SPACE)) |
|
|
471 | { /* evenly spaced */ |
|
|
472 | float xpitch, ypitch; /* pitch of the onion layers */ |
|
|
473 | |
|
|
474 | xpitch = (xsize - 2.0) / (2.0 * layers + 1); |
|
|
475 | ypitch = (ysize - 2.0) / (layers + 1); |
|
|
476 | xlocations[0] = xpitch; |
|
|
477 | ylocations[0] = ypitch; |
|
|
478 | for (i = 1; i < 2 * layers; i++) |
|
|
479 | { |
|
|
480 | if (i < layers) |
|
|
481 | xlocations[i] = xlocations[i - 1] + xpitch; |
|
|
482 | else |
|
|
483 | xlocations[i] = xsize - 1; |
|
|
484 | if (i < layers) |
|
|
485 | ylocations[i] = ylocations[i - 1] + ypitch; |
|
|
486 | else |
|
|
487 | ylocations[i] = ysize - 1; |
|
|
488 | } |
|
|
489 | } |
|
|
490 | |
|
|
491 | /* draw all the onion boxes. */ |
|
|
492 | |
|
|
493 | draw_onion (maze, xlocations, ylocations, layers); |
|
|
494 | make_doors (maze, xlocations, ylocations, layers, option); |
|
|
495 | |
|
|
496 | } |
|
|