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

Comparing deliantra/server/random_maps/random_map.C (file contents):
Revision 1.20 by pippijn, Mon Jan 15 21:06:19 2007 UTC vs.
Revision 1.28 by root, Sun Mar 18 03:05:40 2007 UTC

29#include <room_gen.h> 29#include <room_gen.h>
30#include <random_map.h> 30#include <random_map.h>
31#include <rproto.h> 31#include <rproto.h>
32#include <sproto.h> 32#include <sproto.h>
33 33
34#define CEDE coroapi::cede (); rndm.seed (RP->random_seed + __LINE__);
35
34void 36void
35dump_layout (char **layout, random_map_params *RP) 37dump_layout (char **layout, random_map_params *RP)
36{ 38{
37 { 39 {
38 int i, j; 40 int i, j;
57maptile::generate_random_map (random_map_params *RP) 59maptile::generate_random_map (random_map_params *RP)
58{ 60{
59 char **layout, buf[16384]; 61 char **layout, buf[16384];
60 int i; 62 int i;
61 63
64 RP->Xsize = RP->xsize;
65 RP->Ysize = RP->ysize;
66
62 /* pick a random seed, or use the one from the input file */ 67 /* pick a random seed, or use the one from the input file */
63 SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0)); 68 RP->random_seed = RP->random_seed
69 ? RP->random_seed + RP->dungeon_level
70 : time (0);
71 CEDE;
64 72
65 write_map_parameters_to_string (buf, RP); 73 write_map_parameters_to_string (buf, RP);
66 74
67 if (RP->difficulty == 0) 75 if (RP->difficulty == 0)
68 { 76 {
76 } 84 }
77 else 85 else
78 RP->difficulty_given = 1; 86 RP->difficulty_given = 1;
79 87
80 if (RP->Xsize < MIN_RANDOM_MAP_SIZE) 88 if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
81 RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; 89 RP->Xsize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5;
82 90
83 if (RP->Ysize < MIN_RANDOM_MAP_SIZE) 91 if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
84 RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; 92 RP->Ysize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5;
93
94 if (RP->symmetry == SYMMETRY_RANDOM)
95 RP->symmetry_used = rndm (SYMMETRY_XY) + 1;
96 else
97 RP->symmetry_used = RP->symmetry;
98
99 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
100 RP->Ysize = RP->Ysize / 2 + 1;
101
102 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
103 RP->Xsize = RP->Xsize / 2 + 1;
85 104
86 if (RP->expand2x > 0) 105 if (RP->expand2x > 0)
87 { 106 {
88 RP->Xsize /= 2; 107 RP->Xsize /= 2;
89 RP->Ysize /= 2; 108 RP->Ysize /= 2;
90 } 109 }
91 110
92 layout = layoutgen (RP);
93
94#ifdef RMAP_DEBUG
95 dump_layout (layout, RP);
96#endif
97
98 /* increment these for the current map */
99 RP->dungeon_level += 1;
100 /* allow constant-difficulty maps. */
101 /* difficulty+=1; */
102
103 /* rotate the layout randomly */
104 layout = rotate_layout (layout, RANDOM () % 4, RP);
105#ifdef RMAP_DEBUG
106 dump_layout (layout, RP);
107#endif
108
109 /* allocate the map and set the floor */
110 make_map_floor (layout, RP->floorstyle, RP);
111
112 /* set region */
113 region = RP->region;
114
115 coroapi::cede ();
116
117 /* create walls unless the wallstyle is "none" */
118 if (strcmp (RP->wallstyle, "none"))
119 {
120 make_map_walls (this, layout, RP->wallstyle, RP);
121
122 /* place doors unless doorstyle or wallstyle is "none" */
123 if (strcmp (RP->doorstyle, "none"))
124 put_doors (this, layout, RP->doorstyle, RP);
125
126 }
127
128 coroapi::cede ();
129
130 /* create exits unless the exitstyle is "none" */
131 if (strcmp (RP->exitstyle, "none"))
132 place_exits (this, layout, RP->exitstyle, RP->orientation, RP);
133
134 coroapi::cede ();
135
136 place_specials_in_map (this, layout, RP);
137
138 coroapi::cede ();
139
140 /* create monsters unless the monsterstyle is "none" */
141 if (strcmp (RP->monsterstyle, "none"))
142 place_monsters (this, RP->monsterstyle, RP->difficulty, RP);
143
144 coroapi::cede ();
145
146 /* treasures needs to have a proper difficulty set for the map. */
147 difficulty = estimate_difficulty ();
148
149 coroapi::cede ();
150
151 /* create treasure unless the treasurestyle is "none" */
152 if (strcmp (RP->treasurestyle, "none"))
153 place_treasure (this, layout, RP->treasurestyle, RP->treasureoptions, RP);
154
155 coroapi::cede ();
156
157 /* create decor unless the decorstyle is "none" */
158 if (strcmp (RP->decorstyle, "none"))
159 put_decor (this, layout, RP->decorstyle, RP->decoroptions, RP);
160
161 coroapi::cede ();
162
163 /* generate treasures, etc. */
164 fix_auto_apply ();
165
166 coroapi::cede ();
167 unblock_exits (this, layout, RP);
168
169 /* free the layout */
170 for (i = 0; i < RP->Xsize; i++)
171 free (layout[i]);
172
173 free (layout);
174
175 msg = strdup (buf);
176 in_memory = MAP_IN_MEMORY;
177
178 return 1;
179}
180
181/* function selects the layout function and gives it whatever
182 arguments it needs. */
183char **
184layoutgen (random_map_params *RP)
185{
186 char **maze = 0;
187 int oxsize = RP->Xsize, oysize = RP->Ysize;
188
189 if (RP->symmetry == SYMMETRY_RANDOM)
190 RP->symmetry_used = (RANDOM () % (SYMMETRY_XY)) + 1;
191 else
192 RP->symmetry_used = RP->symmetry;
193
194 if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY)
195 RP->Ysize = RP->Ysize / 2 + 1;
196 if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY)
197 RP->Xsize = RP->Xsize / 2 + 1;
198
199 if (RP->Xsize < MIN_RANDOM_MAP_SIZE)
200 RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5;
201 if (RP->Ysize < MIN_RANDOM_MAP_SIZE)
202 RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5;
203 RP->map_layout_style = 0; 111 RP->map_layout_style = LAYOUT_NONE;
204 112
205 /* Redo this - there was a lot of redundant code of checking for preset 113 /* Redo this - there was a lot of redundant code of checking for preset
206 * layout style and then random layout style. Instead, figure out 114 * layout style and then random layout style. Instead, figure out
207 * the numeric layoutstyle, so there is only one area that actually 115 * the numeric layoutstyle, so there is only one area that actually
208 * calls the code to make the maps. 116 * calls the code to make the maps.
223 RP->map_layout_style = LAYOUT_SNAKE; 131 RP->map_layout_style = LAYOUT_SNAKE;
224 132
225 if (strstr (RP->layoutstyle, "squarespiral")) 133 if (strstr (RP->layoutstyle, "squarespiral"))
226 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; 134 RP->map_layout_style = LAYOUT_SQUARE_SPIRAL;
227 135
228 /* No style found - choose one ranomdly */ 136 /* No style found - choose one randomly */
229 if (RP->map_layout_style == LAYOUT_NONE) 137 if (RP->map_layout_style == LAYOUT_NONE)
230 RP->map_layout_style = (RANDOM () % (NROFLAYOUTS - 1)) + 1; 138 RP->map_layout_style = rndm (NROFLAYOUTS - 1) + 1;
139
140 layout = layoutgen (RP);
141
142#ifdef RMAP_DEBUG
143 dump_layout (layout, RP);
144#endif
145
146 /* increment these for the current map */
147 RP->dungeon_level += 1;
148 /* allow constant-difficulty maps. */
149 /* difficulty+=1; */
150
151 /* rotate the layout randomly */
152 layout = rotate_layout (layout, rndm (4), RP);
153#ifdef RMAP_DEBUG
154 dump_layout (layout, RP);
155#endif
156
157 /* allocate the map and set the floor */
158 make_map_floor (layout, RP->floorstyle, RP);
159
160 /* set region */
161 default_region = RP->region;
162
163 CEDE;
164
165 /* create walls unless the wallstyle is "none" */
166 if (strcmp (RP->wallstyle, "none"))
167 {
168 make_map_walls (this, layout, RP->wallstyle, RP);
169
170 /* place doors unless doorstyle or wallstyle is "none" */
171 if (strcmp (RP->doorstyle, "none"))
172 put_doors (this, layout, RP->doorstyle, RP);
173 }
174
175 CEDE;
176
177 /* create exits unless the exitstyle is "none" */
178 if (strcmp (RP->exitstyle, "none"))
179 place_exits (this, layout, RP->exitstyle, RP->orientation, RP);
180
181 CEDE;
182
183 place_specials_in_map (this, layout, RP);
184
185 CEDE;
186
187 /* create monsters unless the monsterstyle is "none" */
188 if (strcmp (RP->monsterstyle, "none"))
189 place_monsters (this, RP->monsterstyle, RP->difficulty, RP);
190
191 CEDE;
192
193 /* treasures needs to have a proper difficulty set for the map. */
194 difficulty = estimate_difficulty ();
195
196 CEDE;
197
198 /* create treasure unless the treasurestyle is "none" */
199 if (strcmp (RP->treasurestyle, "none"))
200 place_treasure (this, layout, RP->treasurestyle, RP->treasureoptions, RP);
201
202 CEDE;
203
204 /* create decor unless the decorstyle is "none" */
205 if (strcmp (RP->decorstyle, "none"))
206 put_decor (this, layout, RP->decorstyle, RP->decoroptions, RP);
207
208 CEDE;
209
210 /* generate treasures, etc. */
211 fix_auto_apply ();
212
213 CEDE;
214
215 unblock_exits (this, layout, RP);
216
217 /* free the layout */
218 for (i = 0; i < RP->Xsize; i++)
219 free (layout[i]);
220
221 free (layout);
222
223 msg = strdup (buf);
224 in_memory = MAP_IN_MEMORY;
225
226 CEDE;
227
228 return 1;
229}
230
231/* function selects the layout function and gives it whatever
232 arguments it needs. */
233char **
234layoutgen (random_map_params *RP)
235{
236 char **maze = 0;
237 int oxsize = RP->Xsize, oysize = RP->Ysize;
231 238
232 switch (RP->map_layout_style) 239 switch (RP->map_layout_style)
233 { 240 {
234 case LAYOUT_ONION: 241 case LAYOUT_ONION:
235 maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); 242 maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2);
236 if (!(RANDOM () % 3) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY)) 243 if (!(rndm (3)) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY))
237 roomify_layout (maze, RP); 244 roomify_layout (maze, RP);
238 break; 245 break;
239 246
240 case LAYOUT_MAZE: 247 case LAYOUT_MAZE:
241 maze = maze_gen (RP->Xsize, RP->Ysize, RANDOM () % 2); 248 maze = maze_gen (RP->Xsize, RP->Ysize, rndm (2));
242 if (!(RANDOM () % 2)) 249 if (!(rndm (2)))
243 doorify_layout (maze, RP); 250 doorify_layout (maze, RP);
244 break; 251 break;
245 252
246 case LAYOUT_SPIRAL: 253 case LAYOUT_SPIRAL:
247 maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); 254 maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1);
248 if (!(RANDOM () % 2)) 255 if (!(rndm (2)))
249 doorify_layout (maze, RP); 256 doorify_layout (maze, RP);
250 break; 257 break;
251 258
252 case LAYOUT_ROGUELIKE: 259 case LAYOUT_ROGUELIKE:
253 /* Don't put symmetry in rogue maps. There isn't much reason to 260 /* Don't put symmetry in rogue maps. There isn't much reason to
264 /* no doorifying... done already */ 271 /* no doorifying... done already */
265 break; 272 break;
266 273
267 case LAYOUT_SNAKE: 274 case LAYOUT_SNAKE:
268 maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); 275 maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1);
269 if (RANDOM () % 2) 276 if (rndm (2))
270 roomify_layout (maze, RP); 277 roomify_layout (maze, RP);
271 break; 278 break;
272 279
273 case LAYOUT_SQUARE_SPIRAL: 280 case LAYOUT_SQUARE_SPIRAL:
274 maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); 281 maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1);
275 if (RANDOM () % 2) 282 if (rndm (2))
276 roomify_layout (maze, RP); 283 roomify_layout (maze, RP);
277 break; 284 break;
278 } 285 }
279 286
280 maze = symmetrize_layout (maze, RP->symmetry_used, RP); 287 maze = symmetrize_layout (maze, RP->symmetry_used, RP);
443 for (ti = 0; ti < tries; ti++) 450 for (ti = 0; ti < tries; ti++)
444 { 451 {
445 int dx, dy; /* starting location for looking at creating a door */ 452 int dx, dy; /* starting location for looking at creating a door */
446 int cx, cy; /* results of checking on creating walls. */ 453 int cx, cy; /* results of checking on creating walls. */
447 454
448 dx = RANDOM () % RP->Xsize; 455 dx = rndm (RP->Xsize);
449 dy = RANDOM () % RP->Ysize; 456 dy = rndm (RP->Ysize);
457
450 cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */ 458 cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */
451 cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */ 459 cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */
452 if (cx == -1) 460 if (cx == -1)
453 { 461 {
454 if (cy != -1) 462 if (cy != -1)
455 make_wall (maze, dx, dy, 1); 463 make_wall (maze, dx, dy, 1);
464
456 continue; 465 continue;
457 } 466 }
467
458 if (cy == -1) 468 if (cy == -1)
459 { 469 {
460 make_wall (maze, dx, dy, 0); 470 make_wall (maze, dx, dy, 0);
461 continue; 471 continue;
462 } 472 }
473
463 if (cx < cy) 474 if (cx < cy)
464 make_wall (maze, dx, dy, 0); 475 make_wall (maze, dx, dy, 0);
465 else 476 else
466 make_wall (maze, dx, dy, 1); 477 make_wall (maze, dx, dy, 1);
467 } 478 }
613 while (ndoors > 0 && doorlocs > 0) 624 while (ndoors > 0 && doorlocs > 0)
614 { 625 {
615 int di; 626 int di;
616 int sindex; 627 int sindex;
617 628
618 di = RANDOM () % doorlocs; 629 di = rndm (doorlocs);
619 i = doorlist_x[di]; 630 i = doorlist_x[di];
620 j = doorlist_y[di]; 631 j = doorlist_y[di];
621 sindex = surround_flag (maze, i, j, RP); 632 sindex = surround_flag (maze, i, j, RP);
633
622 if (sindex == 3 || sindex == 12) /* these are possible door sindex */ 634 if (sindex == 3 || sindex == 12) /* these are possible door sindex */
623 { 635 {
624 maze[i][j] = 'D'; 636 maze[i][j] = 'D';
625 ndoors--; 637 ndoors--;
626 } 638 }
639
627 /* reduce the size of the list */ 640 /* reduce the size of the list */
628 doorlocs--; 641 doorlocs--;
629 doorlist_x[di] = doorlist_x[doorlocs]; 642 doorlist_x[di] = doorlist_x[doorlocs];
630 doorlist_y[di] = doorlist_y[doorlocs]; 643 doorlist_y[di] = doorlist_y[doorlocs];
631 } 644 }
637void 650void
638write_map_parameters_to_string (char *buf, random_map_params *RP) 651write_map_parameters_to_string (char *buf, random_map_params *RP)
639{ 652{
640 char small_buf[16384]; 653 char small_buf[16384];
641 654
642 sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); 655 sprintf (buf, "xsize %d\nysize %d\n", RP->xsize, RP->ysize);
643 656
644 if (RP->wallstyle[0]) 657 if (RP->wallstyle[0])
645 { 658 {
646 sprintf (small_buf, "wallstyle %s\n", RP->wallstyle); 659 sprintf (small_buf, "wallstyle %s\n", RP->wallstyle);
647 strcat (buf, small_buf); 660 strcat (buf, small_buf);
788 strcat (buf, small_buf); 801 strcat (buf, small_buf);
789 } 802 }
790 803
791 if (RP->random_seed) 804 if (RP->random_seed)
792 { 805 {
793 sprintf (small_buf, "random_seed %d\n", RP->random_seed); 806 sprintf (small_buf, "random_seed %u\n", RP->random_seed);
794 strcat (buf, small_buf); 807 strcat (buf, small_buf);
795 } 808 }
796 809
797 if (RP->custom) 810 if (RP->custom)
798 { 811 {
803 816
804void 817void
805write_parameters_to_string (char *buf, 818write_parameters_to_string (char *buf,
806 int xsize_n, 819 int xsize_n,
807 int ysize_n, 820 int ysize_n,
808 char *wallstyle_n, 821 const char *wallstyle_n,
809 char *floorstyle_n, 822 const char *floorstyle_n,
810 char *monsterstyle_n, 823 const char *monsterstyle_n,
811 char *treasurestyle_n, 824 const char *treasurestyle_n,
812 char *layoutstyle_n, 825 const char *layoutstyle_n,
813 char *decorstyle_n, 826 const char *decorstyle_n,
814 char *doorstyle_n, 827 const char *doorstyle_n,
815 char *exitstyle_n, 828 const char *exitstyle_n,
816 char *final_map_n, 829 const char *final_map_n,
817 char *exit_on_final_map_n, 830 const char *exit_on_final_map_n,
818 char *this_map_n, 831 const char *this_map_n,
819 int layoutoptions1_n, 832 int layoutoptions1_n,
820 int layoutoptions2_n, 833 int layoutoptions2_n,
821 int layoutoptions3_n, 834 int layoutoptions3_n,
822 int symmetry_n, 835 int symmetry_n,
823 int dungeon_depth_n, 836 int dungeon_depth_n,
824 int dungeon_level_n, 837 int dungeon_level_n,
825 int difficulty_n, 838 int difficulty_n,
826 int difficulty_given_n, 839 int difficulty_given_n,
827 int decoroptions_n, 840 int decoroptions_n,
828 int orientation_n, 841 int orientation_n,
829 int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) 842 int origin_x_n,
843 int origin_y_n,
844 uint32_t random_seed_n,
845 int treasureoptions_n,
846 float difficulty_increase)
830{ 847{
831 char small_buf[16384]; 848 char small_buf[16384];
832 849
833 sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); 850 sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n);
834 851
970 } 987 }
971 988
972 if (random_seed_n) 989 if (random_seed_n)
973 { 990 {
974 /* Add one so that the next map is a bit different */ 991 /* Add one so that the next map is a bit different */
975 sprintf (small_buf, "random_seed %d\n", random_seed_n + 1); 992 sprintf (small_buf, "random_seed %u\n", random_seed_n + 1);
976 strcat (buf, small_buf); 993 strcat (buf, small_buf);
977 } 994 }
978 995
979 if (treasureoptions_n) 996 if (treasureoptions_n)
980 { 997 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines