ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/room_gen_onion.C
(Generate patch)

Comparing deliantra/server/random_maps/room_gen_onion.C (file contents):
Revision 1.10 by root, Sat Jan 27 02:19:37 2007 UTC vs.
Revision 1.13 by root, Fri Apr 11 21:09:53 2008 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (C) 2001 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2001,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992,2007 Frank Tore Johansen
7 * 7 *
8 * This program is free software; you can redistribute it and/or modify 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 9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or 10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version. 11 * (at your 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 GNU General Public License
19 * along with this program; if not, write to the Free Software 19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * 20 *
22 * The authors can be reached via e-mail at <crossfire@schmorp.de> 21 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 22 */
24
25 23
26/* The onion room generator: 24/* The onion room generator:
27Onion rooms are like this: 25Onion rooms are like this:
28 26
29char **map_gen_onion(int xsize, int ysize, int option, int layers); 27char **map_gen_onion(int xsize, int ysize, int option, int layers);
59#endif 57#endif
60void centered_onion (char **maze, int xsize, int ysize, int option, int layers); 58void centered_onion (char **maze, int xsize, int ysize, int option, int layers);
61void bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers); 59void bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers);
62void bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers); 60void bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers);
63 61
64
65void draw_onion (char **maze, float *xlocations, float *ylocations, int layers); 62void draw_onion (char **maze, float *xlocations, float *ylocations, int layers);
66void make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options); 63void make_doors (char **maze, float *xlocations, float *ylocations, int layers, int options);
67 64
68char ** 65Maze
69map_gen_onion (int xsize, int ysize, int option, int layers) 66map_gen_onion (int xsize, int ysize, int option, int layers)
70{ 67{
71 int i, j; 68 int i, j;
72 69
73 /* allocate that array, set it up */ 70 Maze maze (xsize, ysize);
74 char **maze = (char **) calloc (sizeof (char *), xsize);
75
76 for (i = 0; i < xsize; i++)
77 {
78 maze[i] = (char *) calloc (sizeof (char), ysize);
79 }
80 71
81 /* pick some random options if option = 0 */ 72 /* pick some random options if option = 0 */
82 if (option == 0) 73 if (option == 0)
83 { 74 {
84 switch (rndm (3)) 75 switch (rndm (3))
91 break; 82 break;
92 case 2: 83 case 2:
93 option |= RMOPT_BOTTOM_R; 84 option |= RMOPT_BOTTOM_R;
94 break; 85 break;
95 } 86 }
96 if (rndm (2)) 87
97 option |= RMOPT_LINEAR; 88 if (rndm (2)) option |= RMOPT_LINEAR;
98 if (rndm (2))
99 option |= RMOPT_IRR_SPACE; 89 if (rndm (2)) option |= RMOPT_IRR_SPACE;
100 } 90 }
101 91
102 /* write the outer walls, if appropriate. */ 92 /* write the outer walls, if appropriate. */
103 if (!(option & RMOPT_WALL_OFF)) 93 if (!(option & RMOPT_WALL_OFF))
104 { 94 {
105 for (i = 0; i < xsize; i++)
106 maze[i][0] = maze[i][ysize - 1] = '#'; 95 for (i = 0; i < xsize; i++) maze[i][0] = maze[i][ysize - 1] = '#';
107 for (j = 0; j < ysize; j++)
108 maze[0][j] = maze[xsize - 1][j] = '#'; 96 for (j = 0; j < ysize; j++) maze[0][j] = maze[xsize - 1][j] = '#';
109 }; 97 };
110 98
111 if (option & RMOPT_WALLS_ONLY) 99 if (option & RMOPT_WALLS_ONLY)
112 return maze; 100 return maze;
113 101
124 112
125void 113void
126centered_onion (char **maze, int xsize, int ysize, int option, int layers) 114centered_onion (char **maze, int xsize, int ysize, int option, int layers)
127{ 115{
128 int i, maxlayers; 116 int i, maxlayers;
129 float *xlocations;
130 float *ylocations;
131 117
132 maxlayers = (MIN (xsize, ysize) - 2) / 5; 118 maxlayers = (MIN (xsize, ysize) - 2) / 5;
119
133 if (!maxlayers) 120 if (!maxlayers)
134 return; /* map too small to onionize */ 121 return; /* map too small to onionize */
135 122
136 if (layers > maxlayers) 123 if (layers > maxlayers)
137 layers = maxlayers; 124 layers = maxlayers;
138 125
139 if (layers == 0) 126 if (layers == 0)
140 layers = rndm (maxlayers) + 1; 127 layers = rndm (maxlayers) + 1;
141 128
142 xlocations = (float *) calloc (sizeof (float), 2 * layers); 129 float *xlocations = salloc0<float> (2 * layers);
143 ylocations = (float *) calloc (sizeof (float), 2 * layers); 130 float *ylocations = salloc0<float> (2 * layers);
144 131
145 /* place all the walls */ 132 /* place all the walls */
146 if (option & RMOPT_IRR_SPACE) /* randomly spaced */ 133 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
147 { 134 {
148 int x_spaces_available, y_spaces_available; 135 int x_spaces_available, y_spaces_available;
149 136
150 /* the "extra" spaces available for spacing between layers */ 137 /* the "extra" spaces available for spacing between layers */
151 x_spaces_available = (xsize - 2) - 6 * layers + 1; 138 x_spaces_available = (xsize - 2) - 6 * layers + 1;
152 y_spaces_available = (ysize - 2) - 6 * layers + 1; 139 y_spaces_available = (ysize - 2) - 6 * layers + 1;
153
154 140
155 /* pick an initial random pitch */ 141 /* pick an initial random pitch */
156 for (i = 0; i < 2 * layers; i++) 142 for (i = 0; i < 2 * layers; i++)
157 { 143 {
158 float xpitch = 2, ypitch = 2; 144 float xpitch = 2, ypitch = 2;
168 x_spaces_available -= (int) (xpitch - 2); 154 x_spaces_available -= (int) (xpitch - 2);
169 y_spaces_available -= (int) (ypitch - 2); 155 y_spaces_available -= (int) (ypitch - 2);
170 } 156 }
171 157
172 } 158 }
159
173 if (!(option & RMOPT_IRR_SPACE)) 160 if (!(option & RMOPT_IRR_SPACE))
174 { /* evenly spaced */ 161 { /* evenly spaced */
175 float xpitch, ypitch; /* pitch of the onion layers */ 162 float xpitch, ypitch; /* pitch of the onion layers */
176 163
177 xpitch = (xsize - 2.0) / (2.0 * layers + 1); 164 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
178 ypitch = (ysize - 2.0) / (2.0 * layers + 1); 165 ypitch = (ysize - 2.0) / (2.0 * layers + 1);
166
179 xlocations[0] = xpitch; 167 xlocations[0] = xpitch;
180 ylocations[0] = ypitch; 168 ylocations[0] = ypitch;
169
181 for (i = 1; i < 2 * layers; i++) 170 for (i = 1; i < 2 * layers; i++)
182 { 171 {
183 xlocations[i] = xlocations[i - 1] + xpitch; 172 xlocations[i] = xlocations[i - 1] + xpitch;
184 ylocations[i] = ylocations[i - 1] + ypitch; 173 ylocations[i] = ylocations[i - 1] + ypitch;
185 } 174 }
186 } 175 }
187 176
188 /* draw all the onion boxes. */ 177 /* draw all the onion boxes. */
189
190 draw_onion (maze, xlocations, ylocations, layers); 178 draw_onion (maze, xlocations, ylocations, layers);
191 make_doors (maze, xlocations, ylocations, layers, option); 179 make_doors (maze, xlocations, ylocations, layers, option);
192 180
181 sfree (xlocations, 2 * layers);
182 sfree (ylocations, 2 * layers);
193} 183}
194 184
195void 185void
196bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers) 186bottom_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
197{ 187{
198 int i, maxlayers; 188 int i, maxlayers;
199 float *xlocations;
200 float *ylocations;
201 189
202 maxlayers = (MIN (xsize, ysize) - 2) / 5; 190 maxlayers = (MIN (xsize, ysize) - 2) / 5;
191
203 if (!maxlayers) 192 if (!maxlayers)
204 return; /* map too small to onionize */ 193 return; /* map too small to onionize */
194
205 if (layers > maxlayers) 195 if (layers > maxlayers)
206 layers = maxlayers; 196 layers = maxlayers;
197
207 if (layers == 0) 198 if (layers == 0)
208 layers = rndm (maxlayers) + 1; 199 layers = rndm (maxlayers) + 1;
209 xlocations = (float *) calloc (sizeof (float), 2 * layers);
210 ylocations = (float *) calloc (sizeof (float), 2 * layers);
211 200
201 float *xlocations = salloc0<float> (2 * layers);
202 float *ylocations = salloc0<float> (2 * layers);
212 203
213 /* place all the walls */ 204 /* place all the walls */
214 if (option & RMOPT_IRR_SPACE) /* randomly spaced */ 205 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
215 { 206 {
216 int x_spaces_available, y_spaces_available; 207 int x_spaces_available, y_spaces_available;
217 208
218 /* the "extra" spaces available for spacing between layers */ 209 /* the "extra" spaces available for spacing between layers */
219 x_spaces_available = (xsize - 2) - 6 * layers + 1; 210 x_spaces_available = (xsize - 2) - 6 * layers + 1;
220 y_spaces_available = (ysize - 2) - 3 * layers + 1; 211 y_spaces_available = (ysize - 2) - 3 * layers + 1;
221 212
222
223 /* pick an initial random pitch */ 213 /* pick an initial random pitch */
224 for (i = 0; i < 2 * layers; i++) 214 for (i = 0; i < 2 * layers; i++)
225 { 215 {
226 float xpitch = 2, ypitch = 2; 216 float xpitch = 2, ypitch = 2;
227 217
230 220
231 if (y_spaces_available > 0) 221 if (y_spaces_available > 0)
232 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3; 222 ypitch = 2 + (rndm (y_spaces_available) + rndm (y_spaces_available) + rndm (y_spaces_available)) / 3;
233 223
234 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch; 224 xlocations[i] = ((i > 0) ? xlocations[i - 1] : 0) + xpitch;
225
235 if (i < layers) 226 if (i < layers)
236 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; 227 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
237 else 228 else
238 ylocations[i] = ysize - 1; 229 ylocations[i] = ysize - 1;
239 230
240 x_spaces_available -= (int) (xpitch - 2); 231 x_spaces_available -= (int) (xpitch - 2);
241 y_spaces_available -= (int) (ypitch - 2); 232 y_spaces_available -= (int) (ypitch - 2);
242 } 233 }
243
244 } 234 }
235
245 if (!(option & RMOPT_IRR_SPACE)) 236 if (!(option & RMOPT_IRR_SPACE))
246 { /* evenly spaced */ 237 { /* evenly spaced */
247 float xpitch, ypitch; /* pitch of the onion layers */ 238 float xpitch, ypitch; /* pitch of the onion layers */
248 239
249 xpitch = (xsize - 2.0) / (2.0 * layers + 1); 240 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
250 ypitch = (ysize - 2.0) / (layers + 1); 241 ypitch = (ysize - 2.0) / (layers + 1);
242
251 xlocations[0] = xpitch; 243 xlocations[0] = xpitch;
252 ylocations[0] = ypitch; 244 ylocations[0] = ypitch;
245
253 for (i = 1; i < 2 * layers; i++) 246 for (i = 1; i < 2 * layers; i++)
254 { 247 {
255 xlocations[i] = xlocations[i - 1] + xpitch; 248 xlocations[i] = xlocations[i - 1] + xpitch;
249
256 if (i < layers) 250 if (i < layers)
257 ylocations[i] = ylocations[i - 1] + ypitch; 251 ylocations[i] = ylocations[i - 1] + ypitch;
258 else 252 else
259 ylocations[i] = ysize - 1; 253 ylocations[i] = ysize - 1;
260 } 254 }
263 /* draw all the onion boxes. */ 257 /* draw all the onion boxes. */
264 258
265 draw_onion (maze, xlocations, ylocations, layers); 259 draw_onion (maze, xlocations, ylocations, layers);
266 make_doors (maze, xlocations, ylocations, layers, option); 260 make_doors (maze, xlocations, ylocations, layers, option);
267 261
262 sfree (xlocations, 2 * layers);
263 sfree (ylocations, 2 * layers);
268} 264}
269 265
270
271/* draw_boxes: draws the lines in the maze defining the onion layers */ 266/* draw_boxes: draws the lines in the maze defining the onion layers */
272
273void 267void
274draw_onion (char **maze, float *xlocations, float *ylocations, int layers) 268draw_onion (char **maze, float *xlocations, float *ylocations, int layers)
275{ 269{
276 int i, j, l; 270 int i, j, l;
277 271
306 int freedoms; /* number of different walls on which we could place a door */ 300 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 */ 301 int which_wall; /* left, 1, top, 2, right, 3, bottom 4 */
308 int l, x1 = 0, x2, y1 = 0, y2; 302 int l, x1 = 0, x2, y1 = 0, y2;
309 303
310 freedoms = 4; /* centered */ 304 freedoms = 4; /* centered */
305
311 if (options & RMOPT_BOTTOM_C) 306 if (options & RMOPT_BOTTOM_C)
312 freedoms = 3; 307 freedoms = 3;
308
313 if (options & RMOPT_BOTTOM_R) 309 if (options & RMOPT_BOTTOM_R)
314 freedoms = 2; 310 freedoms = 2;
311
315 if (layers <= 0) 312 if (layers <= 0)
316 return; 313 return;
317 314
318 /* pick which wall will have a door. */ 315 /* pick which wall will have a door. */
319 which_wall = rndm (freedoms) + 1; 316 which_wall = rndm (freedoms) + 1;
396 break; 393 break;
397 } 394 }
398 395
399 } 396 }
400 } 397 }
398
401 if (options & RMOPT_NO_DOORS) 399 if (options & RMOPT_NO_DOORS)
402 maze[x1][y1] = '#'; /* no door. */ 400 maze[x1][y1] = '#'; /* no door. */
403 else 401 else
404 maze[x1][y1] = 'D'; /* write the door */ 402 maze[x1][y1] = 'D'; /* write the door */
405 403
406 } 404 }
407 /* mark the center of the maze with a C */ 405 /* mark the center of the maze with a C */
408 l = layers - 1; 406 l = layers - 1;
409 x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2; 407 x1 = (int) (xlocations[l] + xlocations[2 * layers - l - 1]) / 2;
410 y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2; 408 y1 = (int) (ylocations[l] + ylocations[2 * layers - l - 1]) / 2;
409
411 maze[x1][y1] = 'C'; 410 maze[x1][y1] = 'C';
412
413 /* not needed anymore */
414 free (xlocations);
415 free (ylocations);
416
417} 411}
418 412
419void 413void
420bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers) 414bottom_right_centered_onion (char **maze, int xsize, int ysize, int option, int layers)
421{ 415{
422 int i, maxlayers; 416 int i, maxlayers;
423 float *xlocations;
424 float *ylocations;
425 417
426 maxlayers = (MIN (xsize, ysize) - 2) / 5; 418 maxlayers = (MIN (xsize, ysize) - 2) / 5;
419
427 if (!maxlayers) 420 if (!maxlayers)
428 return; /* map too small to onionize */ 421 return; /* map too small to onionize */
422
429 if (layers > maxlayers) 423 if (layers > maxlayers)
430 layers = maxlayers; 424 layers = maxlayers;
425
431 if (layers == 0) 426 if (layers == 0)
432 layers = rndm (maxlayers) + 1; 427 layers = rndm (maxlayers) + 1;
433 428
434 xlocations = (float *) calloc (sizeof (float), 2 * layers); 429 float *xlocations = salloc0<float> (2 * layers);
435 ylocations = (float *) calloc (sizeof (float), 2 * layers); 430 float *ylocations = salloc0<float> (2 * layers);
436 431
437 /* place all the walls */ 432 /* place all the walls */
438 if (option & RMOPT_IRR_SPACE) /* randomly spaced */ 433 if (option & RMOPT_IRR_SPACE) /* randomly spaced */
439 { 434 {
440 int x_spaces_available, y_spaces_available; 435 int x_spaces_available, y_spaces_available;
462 457
463 if (i < layers) 458 if (i < layers)
464 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch; 459 ylocations[i] = ((i > 0) ? ylocations[i - 1] : 0) + ypitch;
465 else 460 else
466 ylocations[i] = ysize - 1; 461 ylocations[i] = ysize - 1;
462
467 x_spaces_available -= (int) (xpitch - 2); 463 x_spaces_available -= (int) (xpitch - 2);
468 y_spaces_available -= (int) (ypitch - 2); 464 y_spaces_available -= (int) (ypitch - 2);
469 } 465 }
470
471 } 466 }
467
472 if (!(option & RMOPT_IRR_SPACE)) 468 if (!(option & RMOPT_IRR_SPACE))
473 { /* evenly spaced */ 469 { /* evenly spaced */
474 float xpitch, ypitch; /* pitch of the onion layers */ 470 float xpitch, ypitch; /* pitch of the onion layers */
475 471
476 xpitch = (xsize - 2.0) / (2.0 * layers + 1); 472 xpitch = (xsize - 2.0) / (2.0 * layers + 1);
477 ypitch = (ysize - 2.0) / (layers + 1); 473 ypitch = (ysize - 2.0) / (layers + 1);
474
478 xlocations[0] = xpitch; 475 xlocations[0] = xpitch;
479 ylocations[0] = ypitch; 476 ylocations[0] = ypitch;
477
480 for (i = 1; i < 2 * layers; i++) 478 for (i = 1; i < 2 * layers; i++)
481 { 479 {
482 if (i < layers) 480 if (i < layers)
483 xlocations[i] = xlocations[i - 1] + xpitch; 481 xlocations[i] = xlocations[i - 1] + xpitch;
484 else 482 else
485 xlocations[i] = xsize - 1; 483 xlocations[i] = xsize - 1;
484
486 if (i < layers) 485 if (i < layers)
487 ylocations[i] = ylocations[i - 1] + ypitch; 486 ylocations[i] = ylocations[i - 1] + ypitch;
488 else 487 else
489 ylocations[i] = ysize - 1; 488 ylocations[i] = ysize - 1;
490 } 489 }
493 /* draw all the onion boxes. */ 492 /* draw all the onion boxes. */
494 493
495 draw_onion (maze, xlocations, ylocations, layers); 494 draw_onion (maze, xlocations, ylocations, layers);
496 make_doors (maze, xlocations, ylocations, layers, option); 495 make_doors (maze, xlocations, ylocations, layers, option);
497 496
497 sfree (xlocations, 2 * layers);
498 sfree (ylocations, 2 * layers);
498} 499}
500

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines