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.1 by elmex, Sun Aug 13 17:16:03 2006 UTC vs.
Revision 1.2 by root, Tue Aug 29 08:01:36 2006 UTC

1/* 1/*
2 * static char *room_gen_onion_c = 2 * static char *room_gen_onion_c =
3 * "$Id: room_gen_onion.C,v 1.1 2006/08/13 17:16:03 elmex Exp $"; 3 * "$Id: room_gen_onion.C,v 1.2 2006/08/29 08:01:36 root Exp $";
4 */ 4 */
5 5
6/* 6/*
7 CrossFire, A Multiplayer game for X-windows 7 CrossFire, A Multiplayer game for X-windows
8 8
78 maze[i] = (char *) calloc(sizeof(char),ysize); 78 maze[i] = (char *) calloc(sizeof(char),ysize);
79 } 79 }
80 80
81 /* pick some random options if option = 0 */ 81 /* pick some random options if option = 0 */
82 if(option == 0) { 82 if(option == 0) {
83 switch(RANDOM()%3) { 83 switch(RANDOM()%3) {
84 case 0: 84 case 0:
85 option |= OPT_CENTERED; 85 option |= OPT_CENTERED;
86 break; 86 break;
87 case 1: 87 case 1:
88 option |= OPT_BOTTOM_C; 88 option |= OPT_BOTTOM_C;
89 break; 89 break;
90 case 2: 90 case 2:
91 option |= OPT_BOTTOM_R; 91 option |= OPT_BOTTOM_R;
92 break; 92 break;
93 } 93 }
94 if(RANDOM()%2) option |=OPT_LINEAR; 94 if(RANDOM()%2) option |=OPT_LINEAR;
95 if(RANDOM()%2) option |=OPT_IRR_SPACE; 95 if(RANDOM()%2) option |=OPT_IRR_SPACE;
96 } 96 }
97 97
98 /* write the outer walls, if appropriate. */ 98 /* write the outer walls, if appropriate. */
99 if(!(option & OPT_WALL_OFF )) { 99 if(!(option & OPT_WALL_OFF )) {
100 for(i=0;i<xsize;i++) 100 for(i=0;i<xsize;i++)
101 maze[i][0] = maze[i][ysize-1] = '#'; 101 maze[i][0] = maze[i][ysize-1] = '#';
102 for(j=0;j<ysize;j++) 102 for(j=0;j<ysize;j++)
103 maze[0][j] = maze[xsize-1][j] = '#'; 103 maze[0][j] = maze[xsize-1][j] = '#';
104 }; 104 };
105 105
106 if(option & OPT_WALLS_ONLY) return maze; 106 if(option & OPT_WALLS_ONLY) return maze;
107 107
108 /* pick off the mutually exclusive options */ 108 /* pick off the mutually exclusive options */
129 ylocations = (float *) calloc(sizeof(float),2 * layers); 129 ylocations = (float *) calloc(sizeof(float),2 * layers);
130 130
131 131
132 /* place all the walls */ 132 /* place all the walls */
133 if(option & OPT_IRR_SPACE) /* randomly spaced */ { 133 if(option & OPT_IRR_SPACE) /* randomly spaced */ {
134 int x_spaces_available, y_spaces_available; 134 int x_spaces_available, y_spaces_available;
135 /* the "extra" spaces available for spacing between layers */ 135 /* the "extra" spaces available for spacing between layers */
136 x_spaces_available = (xsize -2) - 6*layers +1; 136 x_spaces_available = (xsize -2) - 6*layers +1;
137 y_spaces_available = (ysize -2) - 6*layers +1; 137 y_spaces_available = (ysize -2) - 6*layers +1;
138 138
139 139
140 /* pick an initial random pitch */ 140 /* pick an initial random pitch */
141 for(i=0;i<2*layers;i++) { 141 for(i=0;i<2*layers;i++) {
142 float xpitch=2,ypitch=2; 142 float xpitch=2,ypitch=2;
143 if(x_spaces_available>0) 143 if(x_spaces_available>0)
144 xpitch = 2 + (RANDOM()%x_spaces_available + 144 xpitch = 2 + (RANDOM()%x_spaces_available +
145 RANDOM()%x_spaces_available + 145 RANDOM()%x_spaces_available +
146 RANDOM()%x_spaces_available)/3; 146 RANDOM()%x_spaces_available)/3;
147 147
148 if(y_spaces_available>0) 148 if(y_spaces_available>0)
149 ypitch = 2 + (RANDOM()%y_spaces_available + 149 ypitch = 2 + (RANDOM()%y_spaces_available +
150 RANDOM()%y_spaces_available + 150 RANDOM()%y_spaces_available +
151 RANDOM()%y_spaces_available)/3; 151 RANDOM()%y_spaces_available)/3;
152 xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch; 152 xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch;
153 ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch; 153 ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch;
154 x_spaces_available-=(int) (xpitch -2); 154 x_spaces_available-=(int) (xpitch -2);
155 y_spaces_available-=(int) (ypitch -2); 155 y_spaces_available-=(int) (ypitch -2);
156 } 156 }
157 157
158 } 158 }
159 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */ 159 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */
160 float xpitch, ypitch; /* pitch of the onion layers */ 160 float xpitch, ypitch; /* pitch of the onion layers */
161 xpitch = (xsize-2.0)/(2.0*layers+1); 161 xpitch = (xsize-2.0)/(2.0*layers+1);
162 ypitch = (ysize-2.0)/(2.0*layers+1); 162 ypitch = (ysize-2.0)/(2.0*layers+1);
163 xlocations[0] = xpitch; 163 xlocations[0] = xpitch;
164 ylocations[0] = ypitch; 164 ylocations[0] = ypitch;
165 for(i=1;i<2*layers;i++) { 165 for(i=1;i<2*layers;i++) {
166 xlocations[i] = xlocations[i-1] + xpitch; 166 xlocations[i] = xlocations[i-1] + xpitch;
167 ylocations[i] = ylocations[i-1] + ypitch; 167 ylocations[i] = ylocations[i-1] + ypitch;
168 } 168 }
169 } 169 }
170 170
171 /* draw all the onion boxes. */ 171 /* draw all the onion boxes. */
172 172
188 ylocations = (float *) calloc(sizeof(float),2 * layers); 188 ylocations = (float *) calloc(sizeof(float),2 * layers);
189 189
190 190
191 /* place all the walls */ 191 /* place all the walls */
192 if(option & OPT_IRR_SPACE) /* randomly spaced */ { 192 if(option & OPT_IRR_SPACE) /* randomly spaced */ {
193 int x_spaces_available, y_spaces_available; 193 int x_spaces_available, y_spaces_available;
194 /* the "extra" spaces available for spacing between layers */ 194 /* the "extra" spaces available for spacing between layers */
195 x_spaces_available = (xsize -2) - 6*layers +1; 195 x_spaces_available = (xsize -2) - 6*layers +1;
196 y_spaces_available = (ysize -2) - 3*layers +1; 196 y_spaces_available = (ysize -2) - 3*layers +1;
197 197
198 198
199 /* pick an initial random pitch */ 199 /* pick an initial random pitch */
200 for(i=0;i<2*layers;i++) { 200 for(i=0;i<2*layers;i++) {
201 float xpitch=2,ypitch=2; 201 float xpitch=2,ypitch=2;
202 if(x_spaces_available>0) 202 if(x_spaces_available>0)
203 xpitch = 2 + (RANDOM()%x_spaces_available + 203 xpitch = 2 + (RANDOM()%x_spaces_available +
204 RANDOM()%x_spaces_available + 204 RANDOM()%x_spaces_available +
205 RANDOM()%x_spaces_available)/3; 205 RANDOM()%x_spaces_available)/3;
206 206
207 if(y_spaces_available>0) 207 if(y_spaces_available>0)
208 ypitch = 2 + (RANDOM()%y_spaces_available + 208 ypitch = 2 + (RANDOM()%y_spaces_available +
209 RANDOM()%y_spaces_available + 209 RANDOM()%y_spaces_available +
210 RANDOM()%y_spaces_available)/3; 210 RANDOM()%y_spaces_available)/3;
211 xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch; 211 xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch;
212 if(i < layers) ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch; 212 if(i < layers) ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch;
213 else ylocations[i] = ysize-1; 213 else ylocations[i] = ysize-1;
214 x_spaces_available-=(int)(xpitch -2); 214 x_spaces_available-=(int)(xpitch -2);
215 y_spaces_available-=(int)(ypitch -2); 215 y_spaces_available-=(int)(ypitch -2);
216 } 216 }
217 217
218 } 218 }
219 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */ 219 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */
220 float xpitch, ypitch; /* pitch of the onion layers */ 220 float xpitch, ypitch; /* pitch of the onion layers */
221 xpitch = (xsize-2.0)/(2.0*layers+1); 221 xpitch = (xsize-2.0)/(2.0*layers+1);
222 ypitch = (ysize-2.0)/(layers+1); 222 ypitch = (ysize-2.0)/(layers+1);
223 xlocations[0] = xpitch; 223 xlocations[0] = xpitch;
224 ylocations[0] = ypitch; 224 ylocations[0] = ypitch;
225 for(i=1;i<2*layers;i++) { 225 for(i=1;i<2*layers;i++) {
226 xlocations[i] = xlocations[i-1] + xpitch; 226 xlocations[i] = xlocations[i-1] + xpitch;
227 if(i < layers) ylocations[i] = ylocations[i-1] + ypitch; 227 if(i < layers) ylocations[i] = ylocations[i-1] + ypitch;
228 else ylocations[i]=ysize-1; 228 else ylocations[i]=ysize-1;
229 } 229 }
230 } 230 }
231 231
232 /* draw all the onion boxes. */ 232 /* draw all the onion boxes. */
233 233
241 241
242void draw_onion(char **maze,float *xlocations,float *ylocations, int layers) { 242void draw_onion(char **maze,float *xlocations,float *ylocations, int layers) {
243 int i,j,l; 243 int i,j,l;
244 244
245 for(l=0;l<layers;l++) { 245 for(l=0;l<layers;l++) {
246 int x1,x2,y1,y2; 246 int x1,x2,y1,y2;
247 /* horizontal segments */ 247 /* horizontal segments */
248 y1 = (int)ylocations[l]; 248 y1 = (int)ylocations[l];
249 y2 = (int)ylocations[2*layers -l-1]; 249 y2 = (int)ylocations[2*layers -l-1];
250 for(i=(int)xlocations[l];i<=(int)xlocations[2*layers -l -1];i++) { 250 for(i=(int)xlocations[l];i<=(int)xlocations[2*layers -l -1];i++) {
251 maze[i][y1] = '#'; 251 maze[i][y1] = '#';
252 maze[i][y2] = '#'; 252 maze[i][y2] = '#';
253 } 253 }
254 254
255 /* vertical segments */ 255 /* vertical segments */
256 x1 = (int)xlocations[l]; 256 x1 = (int)xlocations[l];
257 x2 = (int)xlocations[2*layers-l-1]; 257 x2 = (int)xlocations[2*layers-l-1];
258 for(j=(int)ylocations[l];j<=(int)ylocations[2*layers -l -1];j++) { 258 for(j=(int)ylocations[l];j<=(int)ylocations[2*layers -l -1];j++) {
259 maze[x1][j] = '#'; 259 maze[x1][j] = '#';
260 maze[x2][j] = '#'; 260 maze[x2][j] = '#';
261 } 261 }
262 262
263 } 263 }
264} 264}
265 265
266void make_doors(char **maze, float *xlocations,float *ylocations,int layers,int options) { 266void make_doors(char **maze, float *xlocations,float *ylocations,int layers,int options) {
267 int freedoms; /* number of different walls on which we could place a door */ 267 int freedoms; /* number of different walls on which we could place a door */
272 if(options & OPT_BOTTOM_R) freedoms=2; 272 if(options & OPT_BOTTOM_R) freedoms=2;
273 if(layers<= 0) return; 273 if(layers<= 0) return;
274 /* pick which wall will have a door. */ 274 /* pick which wall will have a door. */
275 which_wall = RANDOM() %freedoms + 1; 275 which_wall = RANDOM() %freedoms + 1;
276 for(l=0;l<layers;l++) { 276 for(l=0;l<layers;l++) {
277 if(options & OPT_LINEAR) { /* linear door placement. */ 277 if(options & OPT_LINEAR) { /* linear door placement. */
278 switch(which_wall) { 278 switch(which_wall) {
279 case 1: { /* left hand wall */ 279 case 1: { /* left hand wall */
280 x1 = (int)xlocations[l]; 280 x1 = (int)xlocations[l];
281 y1 = (int)( (ylocations[l] + ylocations[2*layers-l-1])/2); 281 y1 = (int)( (ylocations[l] + ylocations[2*layers-l-1])/2);
282 break; 282 break;
283 } 283 }
284 case 2: { /* top wall placement */ 284 case 2: { /* top wall placement */
285 x1 = (int)( (xlocations[l] + xlocations[2*layers-l-1])/2); 285 x1 = (int)( (xlocations[l] + xlocations[2*layers-l-1])/2);
286 y1 = (int)ylocations[l]; 286 y1 = (int)ylocations[l];
287 break; 287 break;
288 } 288 }
289 case 3: { /* right wall placement */ 289 case 3: { /* right wall placement */
290 x1 = (int)xlocations[2*layers-l-1]; 290 x1 = (int)xlocations[2*layers-l-1];
291 y1 = (int)( (ylocations[l] + ylocations[2*layers-l-1])/2); 291 y1 = (int)( (ylocations[l] + ylocations[2*layers-l-1])/2);
292 break; 292 break;
293 } 293 }
294 case 4: { /* bottom wall placement */ 294 case 4: { /* bottom wall placement */
295 x1 = (int)( (xlocations[l] + xlocations[2*layers-l-1])/2); 295 x1 = (int)( (xlocations[l] + xlocations[2*layers-l-1])/2);
296 y1 = (int)ylocations[2*layers -l -1]; 296 y1 = (int)ylocations[2*layers -l -1];
297 break; 297 break;
298 } 298 }
299 } 299 }
300 } 300 }
301 else { /* random door placement. */ 301 else { /* random door placement. */
302 which_wall=RANDOM()%freedoms + 1; 302 which_wall=RANDOM()%freedoms + 1;
303 switch(which_wall) { 303 switch(which_wall) {
304 case 1: { /* left hand wall */ 304 case 1: { /* left hand wall */
305 x1 = (int)xlocations[l]; 305 x1 = (int)xlocations[l];
306 y2 = (int) (ylocations[2*layers-l-1]-ylocations[l]-1); 306 y2 = (int) (ylocations[2*layers-l-1]-ylocations[l]-1);
307 if(y2 > 0) y1 = (int) (ylocations[l]+RANDOM() %y2 + 1); 307 if(y2 > 0) y1 = (int) (ylocations[l]+RANDOM() %y2 + 1);
308 else y1 = (int) (ylocations[l]+1); 308 else y1 = (int) (ylocations[l]+1);
309 break; 309 break;
310 } 310 }
311 case 2: { /* top wall placement */ 311 case 2: { /* top wall placement */
312 x2 = (int)( (-xlocations[l] + xlocations[2*layers-l-1]))-1; 312 x2 = (int)( (-xlocations[l] + xlocations[2*layers-l-1]))-1;
313 if (x2 > 0) x1 = (int) (xlocations[l]+RANDOM()%x2 + 1); 313 if (x2 > 0) x1 = (int) (xlocations[l]+RANDOM()%x2 + 1);
314 else x1 = (int) (xlocations[l]+1); 314 else x1 = (int) (xlocations[l]+1);
315 y1 = (int)ylocations[l]; 315 y1 = (int)ylocations[l];
316 break; 316 break;
317 } 317 }
318 case 3: { /* right wall placement */ 318 case 3: { /* right wall placement */
319 x1 = (int)xlocations[2*layers-l-1]; 319 x1 = (int)xlocations[2*layers-l-1];
320 y2 = (int)( (-ylocations[l] + ylocations[2*layers-l-1]))-1; 320 y2 = (int)( (-ylocations[l] + ylocations[2*layers-l-1]))-1;
321 if(y2 > 0) y1 = (int) (ylocations[l]+RANDOM() %y2 + 1); 321 if(y2 > 0) y1 = (int) (ylocations[l]+RANDOM() %y2 + 1);
322 else y1 = (int) (ylocations[l]+1); 322 else y1 = (int) (ylocations[l]+1);
323 323
324 break; 324 break;
325 } 325 }
326 case 4: { /* bottom wall placement */ 326 case 4: { /* bottom wall placement */
327 x2 = (int)( (-xlocations[l] + xlocations[2*layers-l-1]))-1; 327 x2 = (int)( (-xlocations[l] + xlocations[2*layers-l-1]))-1;
328 if (x2 > 0) x1 = (int) (xlocations[l]+RANDOM()%x2 + 1); 328 if (x2 > 0) x1 = (int) (xlocations[l]+RANDOM()%x2 + 1);
329 else x1 = (int) (xlocations[l]+1); 329 else x1 = (int) (xlocations[l]+1);
330 y1 = (int)ylocations[2*layers-l-1]; 330 y1 = (int)ylocations[2*layers-l-1];
331 break; 331 break;
332 } 332 }
333 333
334 } 334 }
335 } 335 }
336 if(options & OPT_NO_DOORS) 336 if(options & OPT_NO_DOORS)
337 maze[x1][y1] = '#'; /* no door. */ 337 maze[x1][y1] = '#'; /* no door. */
338 else 338 else
339 maze[x1][y1] = 'D'; /* write the door */ 339 maze[x1][y1] = 'D'; /* write the door */
340 340
341 } 341 }
364 ylocations = (float *) calloc(sizeof(float),2 * layers); 364 ylocations = (float *) calloc(sizeof(float),2 * layers);
365 365
366 366
367 /* place all the walls */ 367 /* place all the walls */
368 if(option & OPT_IRR_SPACE) /* randomly spaced */ { 368 if(option & OPT_IRR_SPACE) /* randomly spaced */ {
369 int x_spaces_available, y_spaces_available; 369 int x_spaces_available, y_spaces_available;
370 /* the "extra" spaces available for spacing between layers */ 370 /* the "extra" spaces available for spacing between layers */
371 x_spaces_available = (xsize -2) - 3*layers +1; 371 x_spaces_available = (xsize -2) - 3*layers +1;
372 y_spaces_available = (ysize -2) - 3*layers +1; 372 y_spaces_available = (ysize -2) - 3*layers +1;
373 373
374 374
375 /* pick an initial random pitch */ 375 /* pick an initial random pitch */
376 for(i=0;i<2*layers;i++) { 376 for(i=0;i<2*layers;i++) {
377 float xpitch=2,ypitch=2; 377 float xpitch=2,ypitch=2;
378 if(x_spaces_available>0) 378 if(x_spaces_available>0)
379 xpitch = 2 + (RANDOM()%x_spaces_available + 379 xpitch = 2 + (RANDOM()%x_spaces_available +
380 RANDOM()%x_spaces_available + 380 RANDOM()%x_spaces_available +
381 RANDOM()%x_spaces_available)/3; 381 RANDOM()%x_spaces_available)/3;
382 382
383 if(y_spaces_available>0) 383 if(y_spaces_available>0)
384 ypitch = 2 + (RANDOM()%y_spaces_available + 384 ypitch = 2 + (RANDOM()%y_spaces_available +
385 RANDOM()%y_spaces_available + 385 RANDOM()%y_spaces_available +
386 RANDOM()%y_spaces_available)/3; 386 RANDOM()%y_spaces_available)/3;
387 if(i < layers) xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch; 387 if(i < layers) xlocations[i] = ( (i>0)?xlocations[i-1]:0) + xpitch;
388 else xlocations[i] = xsize-1; 388 else xlocations[i] = xsize-1;
389 389
390 if(i < layers) ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch; 390 if(i < layers) ylocations[i] = ( (i>0)?ylocations[i-1]:0) + ypitch;
391 else ylocations[i] = ysize-1; 391 else ylocations[i] = ysize-1;
392 x_spaces_available-=(int)(xpitch -2); 392 x_spaces_available-=(int)(xpitch -2);
393 y_spaces_available-=(int)(ypitch -2); 393 y_spaces_available-=(int)(ypitch -2);
394 } 394 }
395 395
396 } 396 }
397 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */ 397 if(!(option&OPT_IRR_SPACE)){ /* evenly spaced */
398 float xpitch, ypitch; /* pitch of the onion layers */ 398 float xpitch, ypitch; /* pitch of the onion layers */
399 xpitch = (xsize-2.0)/(2.0*layers+1); 399 xpitch = (xsize-2.0)/(2.0*layers+1);
400 ypitch = (ysize-2.0)/(layers+1); 400 ypitch = (ysize-2.0)/(layers+1);
401 xlocations[0] = xpitch; 401 xlocations[0] = xpitch;
402 ylocations[0] = ypitch; 402 ylocations[0] = ypitch;
403 for(i=1;i<2*layers;i++) { 403 for(i=1;i<2*layers;i++) {
404 if(i < layers) xlocations[i] = xlocations[i-1] + xpitch; 404 if(i < layers) xlocations[i] = xlocations[i-1] + xpitch;
405 else xlocations[i]=xsize-1; 405 else xlocations[i]=xsize-1;
406 if(i < layers) ylocations[i] = ylocations[i-1] + ypitch; 406 if(i < layers) ylocations[i] = ylocations[i-1] + ypitch;
407 else ylocations[i]=ysize-1; 407 else ylocations[i]=ysize-1;
408 } 408 }
409 } 409 }
410 410
411 /* draw all the onion boxes. */ 411 /* draw all the onion boxes. */
412 412

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines