ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/random_map.C
Revision: 1.13
Committed: Sun Dec 31 19:02:24 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.12: +34 -32 lines
Log Message:
reindent, minor changes

File Contents

# User Rev Content
1 root 1.13
2 elmex 1.1 /*
3     CrossFire, A Multiplayer game for X-windows
4    
5     Copyright (C) 2001 Mark Wedel & Crossfire Development Team
6     Copyright (C) 1992 Frank Tore Johansen
7    
8     This program 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 2 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, write to the Free Software
20     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21    
22 root 1.4 The authors can be reached via e-mail at <crossfire@schmorp.de>
23 elmex 1.1 */
24    
25     #include <time.h>
26     #include <stdio.h>
27     #include <global.h>
28     #include <maze_gen.h>
29     #include <room_gen.h>
30     #include <random_map.h>
31     #include <rproto.h>
32     #include <sproto.h>
33    
34 root 1.3 void
35 root 1.13 dump_layout (char **layout, random_map_params *RP)
36 root 1.3 {
37     {
38     int i, j;
39    
40     for (i = 0; i < RP->Xsize; i++)
41     {
42     for (j = 0; j < RP->Ysize; j++)
43     {
44     if (layout[i][j] == 0)
45     layout[i][j] = ' ';
46     printf ("%c", layout[i][j]);
47     if (layout[i][j] == ' ')
48     layout[i][j] = 0;
49     }
50     printf ("\n");
51     }
52     }
53     printf ("\n");
54 elmex 1.1 }
55 root 1.8
56     extern FILE *logfile;
57    
58 root 1.5 maptile *
59 root 1.13 generate_random_map (const char *OutFileName, random_map_params *RP)
60 root 1.3 {
61     char **layout, buf[HUGE_BUF];
62 root 1.5 maptile *theMap;
63 root 1.3 int i;
64    
65     /* pick a random seed, or use the one from the input file */
66     if (RP->random_seed == 0)
67     RP->random_seed = time (0);
68    
69     SRANDOM (RP->random_seed);
70    
71     write_map_parameters_to_string (buf, RP);
72    
73     if (RP->difficulty == 0)
74     {
75     RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */
76 root 1.6
77 root 1.3 if (RP->difficulty_increase > 0.001)
78 root 1.6 RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase);
79    
80     if (RP->difficulty < 1)
81     RP->difficulty = 1;
82 elmex 1.1 }
83 root 1.3 else
84     RP->difficulty_given = 1;
85 elmex 1.1
86 root 1.3 if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
87     RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5;
88 root 1.6
89 root 1.3 if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
90     RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5;
91 elmex 1.1
92 root 1.3 if (RP->expand2x > 0)
93     {
94     RP->Xsize /= 2;
95     RP->Ysize /= 2;
96 elmex 1.1 }
97    
98 root 1.3 layout = layoutgen (RP);
99 elmex 1.1
100     #ifdef RMAP_DEBUG
101 root 1.3 dump_layout (layout, RP);
102 elmex 1.1 #endif
103    
104 root 1.3 /* increment these for the current map */
105     RP->dungeon_level += 1;
106     /* allow constant-difficulty maps. */
107     /* difficulty+=1; */
108 elmex 1.1
109 root 1.3 /* rotate the layout randomly */
110     layout = rotate_layout (layout, RANDOM () % 4, RP);
111 elmex 1.1 #ifdef RMAP_DEBUG
112 root 1.3 dump_layout (layout, RP);
113 elmex 1.1 #endif
114    
115 root 1.3 /* allocate the map and set the floor */
116     theMap = make_map_floor (layout, RP->floorstyle, RP);
117    
118     /* set the name of the map. */
119 root 1.10 theMap->path = OutFileName;
120 root 1.3
121     /* set region */
122     theMap->region = RP->region;
123    
124     /* create walls unless the wallstyle is "none" */
125     if (strcmp (RP->wallstyle, "none"))
126     {
127     make_map_walls (theMap, layout, RP->wallstyle, RP);
128 elmex 1.1
129 root 1.3 /* place doors unless doorstyle or wallstyle is "none" */
130     if (strcmp (RP->doorstyle, "none"))
131     put_doors (theMap, layout, RP->doorstyle, RP);
132 elmex 1.1
133 root 1.3 }
134    
135     /* create exits unless the exitstyle is "none" */
136     if (strcmp (RP->exitstyle, "none"))
137     place_exits (theMap, layout, RP->exitstyle, RP->orientation, RP);
138    
139     place_specials_in_map (theMap, layout, RP);
140    
141     /* create monsters unless the monsterstyle is "none" */
142     if (strcmp (RP->monsterstyle, "none"))
143     place_monsters (theMap, RP->monsterstyle, RP->difficulty, RP);
144    
145     /* treasures needs to have a proper difficulty set for the map. */
146 root 1.10 theMap->difficulty = theMap->estimate_difficulty ();
147 root 1.3
148     /* create treasure unless the treasurestyle is "none" */
149     if (strcmp (RP->treasurestyle, "none"))
150     place_treasure (theMap, layout, RP->treasurestyle, RP->treasureoptions, RP);
151    
152     /* create decor unless the decorstyle is "none" */
153     if (strcmp (RP->decorstyle, "none"))
154     put_decor (theMap, layout, RP->decorstyle, RP->decoroptions, RP);
155    
156     /* generate treasures, etc. */
157 root 1.11 theMap->fix_auto_apply ();
158 root 1.3
159     unblock_exits (theMap, layout, RP);
160    
161     /* free the layout */
162     for (i = 0; i < RP->Xsize; i++)
163     free (layout[i]);
164 root 1.12
165 root 1.3 free (layout);
166    
167 root 1.9 theMap->msg = strdup (buf);
168 root 1.12 theMap->in_memory = MAP_IN_MEMORY;
169 root 1.3
170     return theMap;
171 elmex 1.1 }
172    
173     /* function selects the layout function and gives it whatever
174     arguments it needs. */
175 root 1.3 char **
176 root 1.13 layoutgen (random_map_params *RP)
177 root 1.3 {
178     char **maze = 0;
179     int oxsize = RP->Xsize, oysize = RP->Ysize;
180 elmex 1.1
181 root 1.11 if (RP->symmetry == SYMMETRY_RANDOM)
182     RP->symmetry_used = (RANDOM () % (SYMMETRY_XY)) + 1;
183 root 1.3 else
184     RP->symmetry_used = RP->symmetry;
185    
186 root 1.11 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
187 root 1.3 RP->Ysize = RP->Ysize / 2 + 1;
188 root 1.11 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
189 root 1.3 RP->Xsize = RP->Xsize / 2 + 1;
190    
191     if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
192     RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5;
193     if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
194     RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5;
195     RP->map_layout_style = 0;
196    
197     /* Redo this - there was a lot of redundant code of checking for preset
198     * layout style and then random layout style. Instead, figure out
199     * the numeric layoutstyle, so there is only one area that actually
200     * calls the code to make the maps.
201     */
202     if (strstr (RP->layoutstyle, "onion"))
203 root 1.11 RP->map_layout_style = LAYOUT_ONION;
204 elmex 1.1
205 root 1.3 if (strstr (RP->layoutstyle, "maze"))
206 root 1.11 RP->map_layout_style = LAYOUT_MAZE;
207 elmex 1.1
208 root 1.3 if (strstr (RP->layoutstyle, "spiral"))
209 root 1.11 RP->map_layout_style = LAYOUT_SPIRAL;
210 elmex 1.1
211 root 1.3 if (strstr (RP->layoutstyle, "rogue"))
212 root 1.11 RP->map_layout_style = LAYOUT_ROGUELIKE;
213 elmex 1.1
214 root 1.3 if (strstr (RP->layoutstyle, "snake"))
215 root 1.11 RP->map_layout_style = LAYOUT_SNAKE;
216 elmex 1.1
217 root 1.3 if (strstr (RP->layoutstyle, "squarespiral"))
218 root 1.11 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
219    
220 root 1.3 /* No style found - choose one ranomdly */
221 root 1.11 if (RP->map_layout_style == LAYOUT_NONE)
222     RP->map_layout_style = (RANDOM () % (NROFLAYOUTS - 1)) + 1;
223 elmex 1.1
224 root 1.3 switch (RP->map_layout_style)
225     {
226 root 1.11 case LAYOUT_ONION:
227     maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2);
228     if (!(RANDOM () % 3) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY))
229     roomify_layout (maze, RP);
230     break;
231    
232     case LAYOUT_MAZE:
233     maze = maze_gen (RP->Xsize, RP->Ysize, RANDOM () % 2);
234     if (!(RANDOM () % 2))
235     doorify_layout (maze, RP);
236     break;
237    
238     case LAYOUT_SPIRAL:
239     maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1);
240     if (!(RANDOM () % 2))
241     doorify_layout (maze, RP);
242     break;
243    
244     case LAYOUT_ROGUELIKE:
245     /* Don't put symmetry in rogue maps. There isn't much reason to
246     * do so in the first place (doesn't make it any more interesting),
247     * but more importantly, the symmetry code presumes we are symmetrizing
248     * spirals, or maps with lots of passages - making a symmetric rogue
249     * map fails because its likely that the passages the symmetry process
250     * creates may not connect the rooms.
251     */
252     RP->symmetry_used = SYMMETRY_NONE;
253     RP->Ysize = oysize;
254     RP->Xsize = oxsize;
255     maze = roguelike_layout_gen (RP->Xsize, RP->Ysize, RP->layoutoptions1);
256     /* no doorifying... done already */
257     break;
258    
259     case LAYOUT_SNAKE:
260     maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1);
261     if (RANDOM () % 2)
262     roomify_layout (maze, RP);
263     break;
264    
265     case LAYOUT_SQUARE_SPIRAL:
266     maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1);
267     if (RANDOM () % 2)
268     roomify_layout (maze, RP);
269     break;
270 root 1.3 }
271 root 1.2
272 root 1.3 maze = symmetrize_layout (maze, RP->symmetry_used, RP);
273 root 1.11
274 elmex 1.1 #ifdef RMAP_DEBUG
275 root 1.3 dump_layout (maze, RP);
276 elmex 1.1 #endif
277 root 1.11
278 root 1.3 if (RP->expand2x)
279     {
280     maze = expand2x (maze, RP->Xsize, RP->Ysize);
281     RP->Xsize = RP->Xsize * 2 - 1;
282     RP->Ysize = RP->Ysize * 2 - 1;
283 elmex 1.1 }
284 root 1.11
285 root 1.3 return maze;
286 elmex 1.1 }
287    
288     /* takes a map and makes it symmetric: adjusts Xsize and
289     Ysize to produce a symmetric map. */
290 root 1.3 char **
291 root 1.13 symmetrize_layout (char **maze, int sym, random_map_params *RP)
292 root 1.3 {
293     int i, j;
294 elmex 1.1 char **sym_maze;
295 root 1.3 int Xsize_orig, Ysize_orig;
296    
297 elmex 1.1 Xsize_orig = RP->Xsize;
298     Ysize_orig = RP->Ysize;
299 root 1.3 RP->symmetry_used = sym; /* tell everyone else what sort of symmetry is used. */
300 root 1.11 if (sym == SYMMETRY_NONE)
301 root 1.3 {
302     RP->Xsize = Xsize_orig;
303     RP->Ysize = Ysize_orig;
304     return maze;
305     }
306 elmex 1.1 /* pick new sizes */
307 root 1.11 RP->Xsize = ((sym == SYMMETRY_X || sym == SYMMETRY_XY) ? RP->Xsize * 2 - 3 : RP->Xsize);
308     RP->Ysize = ((sym == SYMMETRY_Y || sym == SYMMETRY_XY) ? RP->Ysize * 2 - 3 : RP->Ysize);
309 elmex 1.1
310 root 1.3 sym_maze = (char **) calloc (sizeof (char *), RP->Xsize);
311     for (i = 0; i < RP->Xsize; i++)
312     sym_maze[i] = (char *) calloc (sizeof (char), RP->Ysize);
313    
314 root 1.11 if (sym == SYMMETRY_X)
315 root 1.3 for (i = 0; i < RP->Xsize / 2 + 1; i++)
316     for (j = 0; j < RP->Ysize; j++)
317     {
318     sym_maze[i][j] = maze[i][j];
319     sym_maze[RP->Xsize - i - 1][j] = maze[i][j];
320     };
321 root 1.11 if (sym == SYMMETRY_Y)
322 root 1.3 for (i = 0; i < RP->Xsize; i++)
323     for (j = 0; j < RP->Ysize / 2 + 1; j++)
324     {
325     sym_maze[i][j] = maze[i][j];
326     sym_maze[i][RP->Ysize - j - 1] = maze[i][j];
327     }
328 root 1.11 if (sym == SYMMETRY_XY)
329 root 1.3 for (i = 0; i < RP->Xsize / 2 + 1; i++)
330     for (j = 0; j < RP->Ysize / 2 + 1; j++)
331     {
332     sym_maze[i][j] = maze[i][j];
333     sym_maze[i][RP->Ysize - j - 1] = maze[i][j];
334     sym_maze[RP->Xsize - i - 1][j] = maze[i][j];
335     sym_maze[RP->Xsize - i - 1][RP->Ysize - j - 1] = maze[i][j];
336     }
337 elmex 1.1 /* delete the old maze */
338 root 1.3 for (i = 0; i < Xsize_orig; i++)
339     free (maze[i]);
340     free (maze);
341 elmex 1.1 /* reconnect disjointed spirals */
342 root 1.11 if (RP->map_layout_style == LAYOUT_SPIRAL)
343 root 1.3 connect_spirals (RP->Xsize, RP->Ysize, sym, sym_maze);
344 elmex 1.1 /* reconnect disjointed nethackmazes: the routine for
345 root 1.3 spirals will do the trick? */
346 root 1.11 if (RP->map_layout_style == LAYOUT_ROGUELIKE)
347 root 1.3 connect_spirals (RP->Xsize, RP->Ysize, sym, sym_maze);
348    
349 elmex 1.1 return sym_maze;
350     }
351    
352    
353     /* takes a map and rotates it. This completes the
354     onion layouts, making them possibly centered on any wall.
355     It'll modify Xsize and Ysize if they're swapped.
356     */
357    
358 root 1.3 char **
359 root 1.13 rotate_layout (char **maze, int rotation, random_map_params *RP)
360 root 1.3 {
361 elmex 1.1 char **new_maze;
362 root 1.3 int i, j;
363 elmex 1.1
364 root 1.3 switch (rotation)
365     {
366 root 1.11 case 0:
367     return maze;
368     break;
369 root 1.13 case 2: /* a reflection */
370 root 1.11 {
371     char *newmaze = (char *) malloc (sizeof (char) * RP->Xsize * RP->Ysize);
372    
373     for (i = 0; i < RP->Xsize; i++)
374 root 1.13 { /* make a copy */
375 root 1.11 for (j = 0; j < RP->Ysize; j++)
376     {
377     newmaze[i * RP->Ysize + j] = maze[i][j];
378     }
379     }
380     for (i = 0; i < RP->Xsize; i++)
381 root 1.13 { /* copy a reflection back */
382 root 1.11 for (j = 0; j < RP->Ysize; j++)
383     {
384     maze[i][j] = newmaze[(RP->Xsize - i - 1) * RP->Ysize + RP->Ysize - j - 1];
385     }
386     }
387     free (newmaze);
388 root 1.3 return maze;
389     break;
390 root 1.11 }
391     case 1:
392     case 3:
393     {
394     int swap;
395     new_maze = (char **) calloc (sizeof (char *), RP->Ysize);
396     for (i = 0; i < RP->Ysize; i++)
397     {
398     new_maze[i] = (char *) calloc (sizeof (char), RP->Xsize);
399     }
400 root 1.13 if (rotation == 1) /* swap x and y */
401 root 1.11 for (i = 0; i < RP->Xsize; i++)
402     for (j = 0; j < RP->Ysize; j++)
403     new_maze[j][i] = maze[i][j];
404 root 1.3
405 root 1.11 if (rotation == 3)
406 root 1.13 { /* swap x and y */
407 root 1.3 for (i = 0; i < RP->Xsize; i++)
408     for (j = 0; j < RP->Ysize; j++)
409 root 1.11 new_maze[j][i] = maze[RP->Xsize - i - 1][RP->Ysize - j - 1];
410     }
411 root 1.3
412 root 1.11 /* delete the old layout */
413     for (i = 0; i < RP->Xsize; i++)
414     free (maze[i]);
415     free (maze);
416    
417     swap = RP->Ysize;
418     RP->Ysize = RP->Xsize;
419     RP->Xsize = swap;
420     return new_maze;
421     break;
422     }
423 elmex 1.1 }
424     return NULL;
425     }
426    
427     /* take a layout and make some rooms in it.
428     --works best on onions.*/
429 root 1.3 void
430 root 1.13 roomify_layout (char **maze, random_map_params *RP)
431 root 1.3 {
432     int tries = RP->Xsize * RP->Ysize / 30;
433 elmex 1.1 int ti;
434    
435 root 1.3 for (ti = 0; ti < tries; ti++)
436     {
437     int dx, dy; /* starting location for looking at creating a door */
438     int cx, cy; /* results of checking on creating walls. */
439    
440     dx = RANDOM () % RP->Xsize;
441     dy = RANDOM () % RP->Ysize;
442     cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */
443     cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */
444     if (cx == -1)
445     {
446     if (cy != -1)
447     make_wall (maze, dx, dy, 1);
448     continue;
449     }
450     if (cy == -1)
451     {
452     make_wall (maze, dx, dy, 0);
453     continue;
454     }
455     if (cx < cy)
456     make_wall (maze, dx, dy, 0);
457     else
458     make_wall (maze, dx, dy, 1);
459 elmex 1.1 }
460     }
461    
462     /* checks the layout to see if I can stick a horizontal(dir = 0) wall
463     (or vertical, dir == 1)
464     here which ends up on other walls sensibly. */
465 root 1.3
466     int
467 root 1.13 can_make_wall (char **maze, int dx, int dy, int dir, random_map_params *RP)
468 root 1.3 {
469 elmex 1.1 int i1;
470 root 1.3 int length = 0;
471 elmex 1.1
472     /* dont make walls if we're on the edge. */
473 root 1.3 if (dx == 0 || dx == (RP->Xsize - 1) || dy == 0 || dy == (RP->Ysize - 1))
474     return -1;
475 elmex 1.1
476     /* don't make walls if we're ON a wall. */
477 root 1.3 if (maze[dx][dy] != 0)
478     return -1;
479 elmex 1.1
480 root 1.3 if (dir == 0) /* horizontal */
481 elmex 1.1 {
482     int y = dy;
483 root 1.3
484     for (i1 = dx - 1; i1 > 0; i1--)
485     {
486     int sindex = surround_flag2 (maze, i1, y, RP);
487    
488     if (sindex == 1)
489     break;
490     if (sindex != 0)
491     return -1; /* can't make horiz. wall here */
492     if (maze[i1][y] != 0)
493     return -1; /* can't make horiz. wall here */
494     length++;
495     }
496    
497     for (i1 = dx + 1; i1 < RP->Xsize - 1; i1++)
498     {
499     int sindex = surround_flag2 (maze, i1, y, RP);
500    
501     if (sindex == 2)
502     break;
503     if (sindex != 0)
504     return -1; /* can't make horiz. wall here */
505     if (maze[i1][y] != 0)
506     return -1; /* can't make horiz. wall here */
507     length++;
508     }
509 elmex 1.1 return length;
510     }
511 root 1.3 else
512     { /* vertical */
513     int x = dx;
514    
515     for (i1 = dy - 1; i1 > 0; i1--)
516     {
517     int sindex = surround_flag2 (maze, x, i1, RP);
518    
519     if (sindex == 4)
520     break;
521     if (sindex != 0)
522     return -1; /* can't make vert. wall here */
523     if (maze[x][i1] != 0)
524     return -1; /* can't make horiz. wall here */
525     length++;
526     }
527    
528     for (i1 = dy + 1; i1 < RP->Ysize - 1; i1++)
529     {
530     int sindex = surround_flag2 (maze, x, i1, RP);
531    
532     if (sindex == 8)
533     break;
534     if (sindex != 0)
535     return -1; /* can't make verti. wall here */
536     if (maze[x][i1] != 0)
537     return -1; /* can't make horiz. wall here */
538     length++;
539     }
540     return length;
541 elmex 1.1 }
542 root 1.3 return -1;
543 elmex 1.1 }
544    
545    
546 root 1.3 int
547     make_wall (char **maze, int x, int y, int dir)
548     {
549     maze[x][y] = 'D'; /* mark a door */
550     switch (dir)
551 elmex 1.1 {
552 root 1.13 case 0: /* horizontal */
553     {
554     int i1;
555 root 1.3
556 root 1.13 for (i1 = x - 1; maze[i1][y] == 0; i1--)
557     maze[i1][y] = '#';
558     for (i1 = x + 1; maze[i1][y] == 0; i1++)
559     maze[i1][y] = '#';
560     break;
561     }
562     case 1: /* vertical */
563     {
564     int i1;
565 root 1.3
566 root 1.13 for (i1 = y - 1; maze[x][i1] == 0; i1--)
567     maze[x][i1] = '#';
568     for (i1 = y + 1; maze[x][i1] == 0; i1++)
569     maze[x][i1] = '#';
570     break;
571     }
572 elmex 1.1 }
573    
574     return 0;
575     }
576    
577     /* puts doors at appropriate locations in a layout. */
578 root 1.3 void
579 root 1.13 doorify_layout (char **maze, random_map_params *RP)
580 root 1.3 {
581     int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */
582 elmex 1.1 char *doorlist_x;
583     char *doorlist_y;
584 root 1.3 int doorlocs = 0; /* # of available doorlocations */
585     int i, j;
586    
587     doorlist_x = (char *) malloc (sizeof (int) * RP->Xsize * RP->Ysize);
588     doorlist_y = (char *) malloc (sizeof (int) * RP->Xsize * RP->Ysize);
589 elmex 1.1
590    
591     /* make a list of possible door locations */
592 root 1.3 for (i = 1; i < RP->Xsize - 1; i++)
593     for (j = 1; j < RP->Ysize - 1; j++)
594     {
595     int sindex = surround_flag (maze, i, j, RP);
596    
597     if (sindex == 3 || sindex == 12) /* these are possible door sindex */
598     {
599     doorlist_x[doorlocs] = i;
600     doorlist_y[doorlocs] = j;
601     doorlocs++;
602     }
603     }
604 root 1.12
605 root 1.3 while (ndoors > 0 && doorlocs > 0)
606     {
607     int di;
608     int sindex;
609    
610     di = RANDOM () % doorlocs;
611     i = doorlist_x[di];
612     j = doorlist_y[di];
613     sindex = surround_flag (maze, i, j, RP);
614     if (sindex == 3 || sindex == 12) /* these are possible door sindex */
615     {
616     maze[i][j] = 'D';
617     ndoors--;
618 elmex 1.1 }
619 root 1.3 /* reduce the size of the list */
620     doorlocs--;
621     doorlist_x[di] = doorlist_x[doorlocs];
622     doorlist_y[di] = doorlist_y[doorlocs];
623 elmex 1.1 }
624 root 1.12
625 root 1.3 free (doorlist_x);
626     free (doorlist_y);
627 elmex 1.1 }
628    
629 root 1.3 void
630 root 1.13 write_map_parameters_to_string (char *buf, random_map_params *RP)
631 root 1.3 {
632 elmex 1.1 char small_buf[256];
633    
634 root 1.3 sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize);
635    
636     if (RP->wallstyle[0])
637     {
638     sprintf (small_buf, "wallstyle %s\n", RP->wallstyle);
639     strcat (buf, small_buf);
640     }
641    
642     if (RP->floorstyle[0])
643     {
644     sprintf (small_buf, "floorstyle %s\n", RP->floorstyle);
645     strcat (buf, small_buf);
646     }
647    
648     if (RP->monsterstyle[0])
649     {
650     sprintf (small_buf, "monsterstyle %s\n", RP->monsterstyle);
651     strcat (buf, small_buf);
652     }
653 elmex 1.1
654 root 1.3 if (RP->treasurestyle[0])
655     {
656     sprintf (small_buf, "treasurestyle %s\n", RP->treasurestyle);
657     strcat (buf, small_buf);
658     }
659 elmex 1.1
660 root 1.3 if (RP->layoutstyle[0])
661     {
662     sprintf (small_buf, "layoutstyle %s\n", RP->layoutstyle);
663     strcat (buf, small_buf);
664     }
665 elmex 1.1
666 root 1.3 if (RP->decorstyle[0])
667     {
668     sprintf (small_buf, "decorstyle %s\n", RP->decorstyle);
669     strcat (buf, small_buf);
670     }
671 elmex 1.1
672 root 1.3 if (RP->doorstyle[0])
673     {
674     sprintf (small_buf, "doorstyle %s\n", RP->doorstyle);
675     strcat (buf, small_buf);
676     }
677 elmex 1.1
678 root 1.3 if (RP->exitstyle[0])
679     {
680     sprintf (small_buf, "exitstyle %s\n", RP->exitstyle);
681     strcat (buf, small_buf);
682     }
683 elmex 1.1
684 root 1.3 if (RP->final_map[0])
685     {
686     sprintf (small_buf, "final_map %s\n", RP->final_map);
687     strcat (buf, small_buf);
688     }
689 elmex 1.1
690 root 1.3 if (RP->exit_on_final_map[0])
691     {
692     sprintf (small_buf, "exit_on_final_map %s\n", RP->exit_on_final_map);
693     strcat (buf, small_buf);
694     }
695 elmex 1.1
696 root 1.3 if (RP->this_map[0])
697     {
698     sprintf (small_buf, "origin_map %s\n", RP->this_map);
699     strcat (buf, small_buf);
700     }
701 elmex 1.1
702 root 1.3 if (RP->expand2x)
703     {
704     sprintf (small_buf, "expand2x %d\n", RP->expand2x);
705     strcat (buf, small_buf);
706     }
707 elmex 1.1
708 root 1.3 if (RP->layoutoptions1)
709     {
710     sprintf (small_buf, "layoutoptions1 %d\n", RP->layoutoptions1);
711     strcat (buf, small_buf);
712     }
713 elmex 1.1
714    
715 root 1.3 if (RP->layoutoptions2)
716     {
717     sprintf (small_buf, "layoutoptions2 %d\n", RP->layoutoptions2);
718     strcat (buf, small_buf);
719     }
720 elmex 1.1
721    
722 root 1.3 if (RP->layoutoptions3)
723     {
724     sprintf (small_buf, "layoutoptions3 %d\n", RP->layoutoptions3);
725     strcat (buf, small_buf);
726     }
727 elmex 1.1
728 root 1.3 if (RP->symmetry)
729     {
730     sprintf (small_buf, "symmetry %d\n", RP->symmetry);
731     strcat (buf, small_buf);
732     }
733 elmex 1.1
734    
735 root 1.3 if (RP->difficulty && RP->difficulty_given)
736     {
737     sprintf (small_buf, "difficulty %d\n", RP->difficulty);
738     strcat (buf, small_buf);
739     }
740 elmex 1.1
741 root 1.3 if (RP->difficulty_increase != 1.0)
742     {
743     sprintf (small_buf, "difficulty_increase %f\n", RP->difficulty_increase);
744     strcat (buf, small_buf);
745     }
746 elmex 1.1
747 root 1.3 sprintf (small_buf, "dungeon_level %d\n", RP->dungeon_level);
748     strcat (buf, small_buf);
749 elmex 1.1
750 root 1.3 if (RP->dungeon_depth)
751     {
752     sprintf (small_buf, "dungeon_depth %d\n", RP->dungeon_depth);
753     strcat (buf, small_buf);
754     }
755 elmex 1.1
756 root 1.3 if (RP->decoroptions)
757     {
758     sprintf (small_buf, "decoroptions %d\n", RP->decoroptions);
759     strcat (buf, small_buf);
760     }
761 elmex 1.1
762 root 1.3 if (RP->orientation)
763     {
764     sprintf (small_buf, "orientation %d\n", RP->orientation);
765     strcat (buf, small_buf);
766     }
767 elmex 1.1
768 root 1.3 if (RP->origin_x)
769     {
770     sprintf (small_buf, "origin_x %d\n", RP->origin_x);
771     strcat (buf, small_buf);
772     }
773 elmex 1.1
774 root 1.3 if (RP->origin_y)
775     {
776     sprintf (small_buf, "origin_y %d\n", RP->origin_y);
777     strcat (buf, small_buf);
778     }
779 root 1.11
780 root 1.3 if (RP->random_seed)
781     {
782     /* Add one so that the next map is a bit different */
783     sprintf (small_buf, "random_seed %d\n", RP->random_seed + 1);
784     strcat (buf, small_buf);
785     }
786 elmex 1.1
787 root 1.3 if (RP->treasureoptions)
788     {
789     sprintf (small_buf, "treasureoptions %d\n", RP->treasureoptions);
790     strcat (buf, small_buf);
791     }
792 elmex 1.1 }
793    
794 root 1.3 void
795     write_parameters_to_string (char *buf,
796     int xsize_n,
797     int ysize_n,
798     char *wallstyle_n,
799     char *floorstyle_n,
800     char *monsterstyle_n,
801     char *treasurestyle_n,
802     char *layoutstyle_n,
803     char *decorstyle_n,
804     char *doorstyle_n,
805     char *exitstyle_n,
806     char *final_map_n,
807     char *exit_on_final_map_n,
808     char *this_map_n,
809     int layoutoptions1_n,
810     int layoutoptions2_n,
811     int layoutoptions3_n,
812     int symmetry_n,
813     int dungeon_depth_n,
814     int dungeon_level_n,
815     int difficulty_n,
816     int difficulty_given_n,
817     int decoroptions_n,
818     int orientation_n,
819     int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase)
820 elmex 1.1 {
821    
822     char small_buf[256];
823    
824 root 1.3 sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n);
825    
826     if (wallstyle_n && wallstyle_n[0])
827     {
828     sprintf (small_buf, "wallstyle %s\n", wallstyle_n);
829     strcat (buf, small_buf);
830     }
831    
832     if (floorstyle_n && floorstyle_n[0])
833     {
834     sprintf (small_buf, "floorstyle %s\n", floorstyle_n);
835     strcat (buf, small_buf);
836     }
837 elmex 1.1
838 root 1.3 if (monsterstyle_n && monsterstyle_n[0])
839     {
840     sprintf (small_buf, "monsterstyle %s\n", monsterstyle_n);
841     strcat (buf, small_buf);
842     }
843 elmex 1.1
844 root 1.3 if (treasurestyle_n && treasurestyle_n[0])
845     {
846     sprintf (small_buf, "treasurestyle %s\n", treasurestyle_n);
847     strcat (buf, small_buf);
848     }
849 elmex 1.1
850 root 1.3 if (layoutstyle_n && layoutstyle_n[0])
851     {
852     sprintf (small_buf, "layoutstyle %s\n", layoutstyle_n);
853     strcat (buf, small_buf);
854     }
855 elmex 1.1
856 root 1.3 if (decorstyle_n && decorstyle_n[0])
857     {
858     sprintf (small_buf, "decorstyle %s\n", decorstyle_n);
859     strcat (buf, small_buf);
860     }
861 elmex 1.1
862 root 1.3 if (doorstyle_n && doorstyle_n[0])
863     {
864     sprintf (small_buf, "doorstyle %s\n", doorstyle_n);
865     strcat (buf, small_buf);
866     }
867 elmex 1.1
868 root 1.3 if (exitstyle_n && exitstyle_n[0])
869     {
870     sprintf (small_buf, "exitstyle %s\n", exitstyle_n);
871     strcat (buf, small_buf);
872     }
873 elmex 1.1
874 root 1.3 if (final_map_n && final_map_n[0])
875     {
876     sprintf (small_buf, "final_map %s\n", final_map_n);
877     strcat (buf, small_buf);
878     }
879 elmex 1.1
880 root 1.3 if (exit_on_final_map_n && exit_on_final_map_n[0])
881     {
882     sprintf (small_buf, "exit_on_final_map %s\n", exit_on_final_map_n);
883     strcat (buf, small_buf);
884     }
885 elmex 1.1
886 root 1.3 if (this_map_n && this_map_n[0])
887     {
888     sprintf (small_buf, "origin_map %s\n", this_map_n);
889     strcat (buf, small_buf);
890     }
891 elmex 1.1
892 root 1.3 if (layoutoptions1_n)
893     {
894     sprintf (small_buf, "layoutoptions1 %d\n", layoutoptions1_n);
895     strcat (buf, small_buf);
896     }
897 elmex 1.1
898 root 1.3 if (layoutoptions2_n)
899     {
900     sprintf (small_buf, "layoutoptions2 %d\n", layoutoptions2_n);
901     strcat (buf, small_buf);
902     }
903 elmex 1.1
904    
905 root 1.3 if (layoutoptions3_n)
906     {
907     sprintf (small_buf, "layoutoptions3 %d\n", layoutoptions3_n);
908     strcat (buf, small_buf);
909     }
910 elmex 1.1
911 root 1.3 if (symmetry_n)
912     {
913     sprintf (small_buf, "symmetry %d\n", symmetry_n);
914     strcat (buf, small_buf);
915     }
916 elmex 1.1
917    
918 root 1.3 if (difficulty_n && difficulty_given_n)
919     {
920     sprintf (small_buf, "difficulty %d\n", difficulty_n);
921     strcat (buf, small_buf);
922     }
923 elmex 1.1
924 root 1.3 if (difficulty_increase > 0.001)
925     {
926     sprintf (small_buf, "difficulty_increase %f\n", difficulty_increase);
927     strcat (buf, small_buf);
928     }
929 elmex 1.1
930 root 1.3 sprintf (small_buf, "dungeon_level %d\n", dungeon_level_n);
931     strcat (buf, small_buf);
932 elmex 1.1
933 root 1.3 if (dungeon_depth_n)
934     {
935     sprintf (small_buf, "dungeon_depth %d\n", dungeon_depth_n);
936     strcat (buf, small_buf);
937     }
938 elmex 1.1
939 root 1.3 if (decoroptions_n)
940     {
941     sprintf (small_buf, "decoroptions %d\n", decoroptions_n);
942     strcat (buf, small_buf);
943     }
944 elmex 1.1
945 root 1.3 if (orientation_n)
946     {
947     sprintf (small_buf, "orientation %d\n", orientation_n);
948     strcat (buf, small_buf);
949     }
950 elmex 1.1
951 root 1.3 if (origin_x_n)
952     {
953     sprintf (small_buf, "origin_x %d\n", origin_x_n);
954     strcat (buf, small_buf);
955     }
956 elmex 1.1
957 root 1.3 if (origin_y_n)
958     {
959     sprintf (small_buf, "origin_y %d\n", origin_y_n);
960     strcat (buf, small_buf);
961     }
962 root 1.11
963 root 1.3 if (random_seed_n)
964     {
965     /* Add one so that the next map is a bit different */
966     sprintf (small_buf, "random_seed %d\n", random_seed_n + 1);
967     strcat (buf, small_buf);
968     }
969 elmex 1.1
970 root 1.3 if (treasureoptions_n)
971     {
972     sprintf (small_buf, "treasureoptions %d\n", treasureoptions_n);
973     strcat (buf, small_buf);
974     }
975 elmex 1.1
976    
977     }
978    
979     /* copy an object with an inventory... i.e., duplicate the inv too. */
980 root 1.3 void
981     copy_object_with_inv (object *src_ob, object *dest_ob)
982     {
983     object *walk, *tmp;
984 elmex 1.1
985 root 1.7 src_ob->copy_to (dest_ob);
986 elmex 1.1
987 root 1.3 for (walk = src_ob->inv; walk != NULL; walk = walk->below)
988     {
989 root 1.7 tmp = object::create ();
990 root 1.13
991 root 1.7 walk->copy_to (tmp);
992 root 1.3 insert_ob_in_ob (tmp, dest_ob);
993     }
994 elmex 1.1 }