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