1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team |
5 | * Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
7 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * it under the terms of the GNU General Public License as published by |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the Free Software Foundation, either version 3 of the License, or |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * (at your option) any later version. |
11 | * option) any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
16 | * GNU General Public License for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | * and the GNU General Public License along with this program. If not, see |
|
|
20 | * <http://www.gnu.org/licenses/>. |
20 | * |
21 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
23 | */ |
23 | |
24 | |
24 | /* The onion room generator: |
25 | /* The onion room generator: |
… | |
… | |
48 | |
49 | |
49 | */ |
50 | */ |
50 | |
51 | |
51 | |
52 | |
52 | #include <global.h> |
53 | #include <global.h> |
53 | #include <random_map.h> |
54 | #include <rmg.h> |
|
|
55 | #include <rproto.h> |
54 | |
56 | |
55 | #ifndef MIN |
|
|
56 | # define MIN(x,y) (((x)<(y))? (x):(y)) |
|
|
57 | #endif |
|
|
58 | void centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
57 | static void centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
59 | void bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
58 | static void bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
60 | void bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
59 | static void bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers); |
61 | |
60 | |
62 | void draw_onion (char **maze, float *xlocations, float *ylocations, int layers); |
61 | static void draw_onion (char **maze, float *xlocations, float *ylocations, int layers); |
63 | void make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options); |
62 | static void make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options); |
64 | |
63 | |
65 | Maze |
64 | void |
66 | map_gen_onion (int xsize, int ysize, int option, int layers) |
65 | map_gen_onion (layout &maze, int option, int layers) |
67 | { |
66 | { |
68 | int i, j; |
67 | int xsize = maze.w; |
|
|
68 | int ysize = maze.h; |
69 | |
69 | |
70 | Maze maze (xsize, ysize); |
70 | maze.clear (); |
71 | |
71 | |
72 | /* pick some random options if option = 0 */ |
72 | /* pick some random options if option = 0 */ |
73 | if (option == 0) |
73 | if (option == 0) |
74 | { |
74 | { |
75 | switch (rndm (3)) |
75 | switch (rmg_rndm (3)) |
76 | { |
76 | { |
77 | case 0: |
77 | case 0: |
78 | option |= RMOPT_CENTERED; |
78 | option |= RMOPT_CENTERED; |
79 | break; |
79 | break; |
80 | case 1: |
80 | case 1: |
… | |
… | |
83 | case 2: |
83 | case 2: |
84 | option |= RMOPT_BOTTOM_R; |
84 | option |= RMOPT_BOTTOM_R; |
85 | break; |
85 | break; |
86 | } |
86 | } |
87 | |
87 | |
88 | if (rndm (2)) option |= RMOPT_LINEAR; |
88 | if (rmg_rndm (2)) option |= RMOPT_LINEAR; |
89 | if (rndm (2)) option |= RMOPT_IRR_SPACE; |
89 | if (rmg_rndm (2)) option |= RMOPT_IRR_SPACE; |
90 | } |
90 | } |
91 | |
91 | |
92 | /* write the outer walls, if appropriate. */ |
92 | /* write the outer walls, if appropriate. */ |
93 | if (!(option & RMOPT_WALL_OFF)) |
93 | if (!(option & RMOPT_WALL_OFF)) |
94 | { |
94 | maze.border (); |
95 | for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#'; |
|
|
96 | for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#'; |
|
|
97 | }; |
|
|
98 | |
95 | |
99 | if (option & RMOPT_WALLS_ONLY) |
96 | if (option & RMOPT_WALLS_ONLY) |
100 | return maze; |
97 | return; |
101 | |
98 | |
102 | /* pick off the mutually exclusive options */ |
99 | /* pick off the mutually exclusive options */ |
103 | if (option & RMOPT_BOTTOM_R) |
100 | if (option & RMOPT_BOTTOM_R) |
104 | bottom_right_centered_onion (maze, xsize, ysize, option, layers); |
101 | bottom_right_centered_onion (maze, xsize, ysize, option, layers); |
105 | else if (option & RMOPT_BOTTOM_C) |
102 | else if (option & RMOPT_BOTTOM_C) |
106 | bottom_centered_onion (maze, xsize, ysize, option, layers); |
103 | bottom_centered_onion (maze, xsize, ysize, option, layers); |
107 | else if (option & RMOPT_CENTERED) |
104 | else if (option & RMOPT_CENTERED) |
108 | centered_onion (maze, xsize, ysize, option, layers); |
105 | centered_onion (maze, xsize, ysize, option, layers); |
109 | |
|
|
110 | return maze; |
|
|
111 | } |
106 | } |
112 | |
107 | |
113 | void |
108 | static void |
114 | centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
109 | centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
115 | { |
110 | { |
116 | int i, maxlayers; |
111 | int i, maxlayers; |
117 | |
112 | |
118 | maxlayers = (MIN (xsize, ysize) - 2) / 5; |
113 | maxlayers = (min (xsize, ysize) - 2) / 5; |
119 | |
114 | |
120 | if (!maxlayers) |
115 | if (!maxlayers) |
121 | return; /* map too small to onionize */ |
116 | return; /* map too small to onionize */ |
122 | |
117 | |
123 | if (layers > maxlayers) |
118 | if (layers > maxlayers) |
124 | layers = maxlayers; |
119 | layers = maxlayers; |
125 | |
120 | |
126 | if (layers == 0) |
121 | if (layers == 0) |
127 | layers = rndm (maxlayers) + 1; |
122 | layers = rmg_rndm (maxlayers) + 1; |
128 | |
123 | |
129 | float *xlocations = salloc0<float> (2 * layers); |
124 | float *xlocations = salloc0<float> (2 * layers); |
130 | float *ylocations = salloc0<float> (2 * layers); |
125 | float *ylocations = salloc0<float> (2 * layers); |
131 | |
126 | |
132 | /* place all the walls */ |
127 | /* place all the walls */ |
… | |
… | |
142 | for (i = 0; i < 2 * layers; i++) |
137 | for (i = 0; i < 2 * layers; i++) |
143 | { |
138 | { |
144 | float xpitch = 2, ypitch = 2; |
139 | float xpitch = 2, ypitch = 2; |
145 | |
140 | |
146 | if (x_spaces_available > 0) |
141 | if (x_spaces_available > 0) |
147 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
142 | xpitch = 2 + (rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available)) / 3; |
148 | |
143 | |
149 | if (y_spaces_available > 0) |
144 | if (y_spaces_available > 0) |
150 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
145 | ypitch = 2 + (rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available)) / 3; |
151 | |
146 | |
152 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
147 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
153 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
148 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
154 | x_spaces_available -= (int) (xpitch - 2); |
149 | x_spaces_available -= (int) (xpitch - 2); |
155 | y_spaces_available -= (int) (ypitch - 2); |
150 | y_spaces_available -= (int) (ypitch - 2); |
… | |
… | |
180 | |
175 | |
181 | sfree (xlocations, 2 * layers); |
176 | sfree (xlocations, 2 * layers); |
182 | sfree (ylocations, 2 * layers); |
177 | sfree (ylocations, 2 * layers); |
183 | } |
178 | } |
184 | |
179 | |
185 | void |
180 | static void |
186 | bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
181 | bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
187 | { |
182 | { |
188 | int i, maxlayers; |
183 | int i, maxlayers; |
189 | |
184 | |
190 | maxlayers = (MIN (xsize, ysize) - 2) / 5; |
185 | maxlayers = (min (xsize, ysize) - 2) / 5; |
191 | |
186 | |
192 | if (!maxlayers) |
187 | if (!maxlayers) |
193 | return; /* map too small to onionize */ |
188 | return; /* map too small to onionize */ |
194 | |
189 | |
195 | if (layers > maxlayers) |
190 | if (layers > maxlayers) |
196 | layers = maxlayers; |
191 | layers = maxlayers; |
197 | |
192 | |
198 | if (layers == 0) |
193 | if (layers == 0) |
199 | layers = rndm (maxlayers) + 1; |
194 | layers = rmg_rndm (maxlayers) + 1; |
200 | |
195 | |
201 | float *xlocations = salloc0<float> (2 * layers); |
196 | float *xlocations = salloc0<float> (2 * layers); |
202 | float *ylocations = salloc0<float> (2 * layers); |
197 | float *ylocations = salloc0<float> (2 * layers); |
203 | |
198 | |
204 | /* place all the walls */ |
199 | /* place all the walls */ |
… | |
… | |
214 | for (i = 0; i < 2 * layers; i++) |
209 | for (i = 0; i < 2 * layers; i++) |
215 | { |
210 | { |
216 | float xpitch = 2, ypitch = 2; |
211 | float xpitch = 2, ypitch = 2; |
217 | |
212 | |
218 | if (x_spaces_available > 0) |
213 | if (x_spaces_available > 0) |
219 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
214 | xpitch = 2 + (rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available)) / 3; |
220 | |
215 | |
221 | if (y_spaces_available > 0) |
216 | if (y_spaces_available > 0) |
222 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
217 | ypitch = 2 + (rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available)) / 3; |
223 | |
218 | |
224 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
219 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
225 | |
220 | |
226 | if (i < layers) |
221 | if (i < layers) |
227 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
222 | ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; |
… | |
… | |
261 | |
256 | |
262 | sfree (xlocations, 2 * layers); |
257 | sfree (xlocations, 2 * layers); |
263 | sfree (ylocations, 2 * layers); |
258 | sfree (ylocations, 2 * layers); |
264 | } |
259 | } |
265 | |
260 | |
266 | /* draw_boxes: draws the lines in the maze defining the onion layers */ |
261 | /* draw_boxes: draws the lines in the maze defining the onion layers */ |
267 | void |
262 | static void |
268 | draw_onion (char **maze, float *xlocations, float *ylocations, int layers) |
263 | draw_onion (char **maze, float *xlocations, float *ylocations, int layers) |
269 | { |
264 | { |
270 | int i, j, l; |
265 | int i, j, l; |
271 | |
266 | |
272 | for (l = 0; l < layers; l++) |
267 | for (l = 0; l < layers; l++) |
… | |
… | |
292 | } |
287 | } |
293 | |
288 | |
294 | } |
289 | } |
295 | } |
290 | } |
296 | |
291 | |
297 | void |
292 | static void |
298 | make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options) |
293 | make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options) |
299 | { |
294 | { |
300 | int freedoms; /* number of different walls on which we could place a door */ |
295 | int freedoms; /* number of different walls on which we could place a door */ |
301 | int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */ |
296 | int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */ |
302 | int l, x1 = 0, x2, y1 = 0, y2; |
297 | int l, x1 = 0, x2, y1 = 0, y2; |
… | |
… | |
311 | |
306 | |
312 | if (layers <= 0) |
307 | if (layers <= 0) |
313 | return; |
308 | return; |
314 | |
309 | |
315 | /* pick which wall will have a door. */ |
310 | /* pick which wall will have a door. */ |
316 | which_wall = rndm (freedoms) + 1; |
311 | which_wall = rmg_rndm (freedoms) + 1; |
317 | for (l = 0; l < layers; l++) |
312 | for (l = 0; l < layers; l++) |
318 | { |
313 | { |
319 | if (options & RMOPT_LINEAR) |
314 | if (options & RMOPT_LINEAR) |
320 | { /* linear door placement. */ |
315 | { /* linear door placement. */ |
321 | switch (which_wall) |
316 | switch (which_wall) |
… | |
… | |
346 | } |
341 | } |
347 | } |
342 | } |
348 | } |
343 | } |
349 | else |
344 | else |
350 | { /* random door placement. */ |
345 | { /* random door placement. */ |
351 | which_wall = rndm (freedoms) + 1; |
346 | which_wall = rmg_rndm (freedoms) + 1; |
352 | switch (which_wall) |
347 | switch (which_wall) |
353 | { |
348 | { |
354 | case 1: |
349 | case 1: |
355 | { /* left hand wall */ |
350 | { /* left hand wall */ |
356 | x1 = (int) xlocations[l]; |
351 | x1 = (int) xlocations[l]; |
357 | y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1); |
352 | y2 = (int) (ylocations[2 * layers - l - 1] - ylocations[l] - 1); |
358 | if (y2 > 0) |
353 | if (y2 > 0) |
359 | y1 = (int) (ylocations[l] + rndm (y2) + 1); |
354 | y1 = (int) (ylocations[l] + rmg_rndm (y2) + 1); |
360 | else |
355 | else |
361 | y1 = (int) (ylocations[l] + 1); |
356 | y1 = (int) (ylocations[l] + 1); |
362 | break; |
357 | break; |
363 | } |
358 | } |
364 | case 2: |
359 | case 2: |
365 | { /* top wall placement */ |
360 | { /* top wall placement */ |
366 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
361 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
367 | if (x2 > 0) |
362 | if (x2 > 0) |
368 | x1 = (int) (xlocations[l] + rndm (x2) + 1); |
363 | x1 = (int) (xlocations[l] + rmg_rndm (x2) + 1); |
369 | else |
364 | else |
370 | x1 = (int) (xlocations[l] + 1); |
365 | x1 = (int) (xlocations[l] + 1); |
371 | y1 = (int) ylocations[l]; |
366 | y1 = (int) ylocations[l]; |
372 | break; |
367 | break; |
373 | } |
368 | } |
374 | case 3: |
369 | case 3: |
375 | { /* right wall placement */ |
370 | { /* right wall placement */ |
376 | x1 = (int) xlocations[2 * layers - l - 1]; |
371 | x1 = (int) xlocations[2 * layers - l - 1]; |
377 | y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1; |
372 | y2 = (int) ((-ylocations[l] + ylocations[2 * layers - l - 1])) - 1; |
378 | if (y2 > 0) |
373 | if (y2 > 0) |
379 | y1 = (int) (ylocations[l] + rndm (y2) + 1); |
374 | y1 = (int) (ylocations[l] + rmg_rndm (y2) + 1); |
380 | else |
375 | else |
381 | y1 = (int) (ylocations[l] + 1); |
376 | y1 = (int) (ylocations[l] + 1); |
382 | |
377 | |
383 | break; |
378 | break; |
384 | } |
379 | } |
385 | case 4: |
380 | case 4: |
386 | { /* bottom wall placement */ |
381 | { /* bottom wall placement */ |
387 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
382 | x2 = (int) ((-xlocations[l] + xlocations[2 * layers - l - 1])) - 1; |
388 | if (x2 > 0) |
383 | if (x2 > 0) |
389 | x1 = (int) (xlocations[l] + rndm (x2) + 1); |
384 | x1 = (int) (xlocations[l] + rmg_rndm (x2) + 1); |
390 | else |
385 | else |
391 | x1 = (int) (xlocations[l] + 1); |
386 | x1 = (int) (xlocations[l] + 1); |
392 | y1 = (int) ylocations[2 * layers - l - 1]; |
387 | y1 = (int) ylocations[2 * layers - l - 1]; |
393 | break; |
388 | break; |
394 | } |
389 | } |
… | |
… | |
408 | y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2; |
403 | y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2; |
409 | |
404 | |
410 | maze[x1][y1] = 'C'; |
405 | maze[x1][y1] = 'C'; |
411 | } |
406 | } |
412 | |
407 | |
413 | void |
408 | static void |
414 | bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
409 | bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers) |
415 | { |
410 | { |
416 | int i, maxlayers; |
411 | int i, maxlayers; |
417 | |
412 | |
418 | maxlayers = (MIN (xsize, ysize) - 2) / 5; |
413 | maxlayers = (min (xsize, ysize) - 2) / 5; |
419 | |
414 | |
420 | if (!maxlayers) |
415 | if (!maxlayers) |
421 | return; /* map too small to onionize */ |
416 | return; /* map too small to onionize */ |
422 | |
417 | |
423 | if (layers > maxlayers) |
418 | if (layers > maxlayers) |
424 | layers = maxlayers; |
419 | layers = maxlayers; |
425 | |
420 | |
426 | if (layers == 0) |
421 | if (layers == 0) |
427 | layers = rndm (maxlayers) + 1; |
422 | layers = rmg_rndm (maxlayers) + 1; |
428 | |
423 | |
429 | float *xlocations = salloc0<float> (2 * layers); |
424 | float *xlocations = salloc0<float> (2 * layers); |
430 | float *ylocations = salloc0<float> (2 * layers); |
425 | float *ylocations = salloc0<float> (2 * layers); |
431 | |
426 | |
432 | /* place all the walls */ |
427 | /* place all the walls */ |
… | |
… | |
443 | for (i = 0; i < 2 * layers; i++) |
438 | for (i = 0; i < 2 * layers; i++) |
444 | { |
439 | { |
445 | float xpitch = 2, ypitch = 2; |
440 | float xpitch = 2, ypitch = 2; |
446 | |
441 | |
447 | if (x_spaces_available > 0) |
442 | if (x_spaces_available > 0) |
448 | xpitch = 2 + (rndm (x_spaces_available) + rndm (x_spaces_available) + rndm (x_spaces_available)) / 3; |
443 | xpitch = 2 + (rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available) + rmg_rndm (x_spaces_available)) / 3; |
449 | |
444 | |
450 | if (y_spaces_available > 0) |
445 | if (y_spaces_available > 0) |
451 | ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; |
446 | ypitch = 2 + (rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available) + rmg_rndm (y_spaces_available)) / 3; |
452 | |
447 | |
453 | if (i < layers) |
448 | if (i < layers) |
454 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
449 | xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; |
455 | else |
450 | else |
456 | xlocations[i] = xsize - 1; |
451 | xlocations[i] = xsize - 1; |