1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game for X-windows |
3 | |
3 | * |
4 | Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
5 | Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
5 | * Copyright (C) 2001 Mark Wedel & Crossfire Development Team |
6 | Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (C) 1992 Frank Tore Johansen |
7 | |
7 | * |
8 | This program is free software; you can redistribute it and/or modify |
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 |
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 2 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, write to the Free Software |
20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | |
21 | * |
22 | The authors can be reached via e-mail at <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail at <crossfire@schmorp.de> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | #include <time.h> |
25 | #include <time.h> |
26 | #include <stdio.h> |
26 | #include <stdio.h> |
27 | #include <global.h> |
27 | #include <global.h> |
28 | #include <maze_gen.h> |
28 | #include <maze_gen.h> |
… | |
… | |
51 | } |
51 | } |
52 | } |
52 | } |
53 | printf ("\n"); |
53 | printf ("\n"); |
54 | } |
54 | } |
55 | |
55 | |
56 | maptile * |
56 | bool |
57 | generate_random_map (const char *OutFileName, random_map_params *RP) |
57 | maptile::generate_random_map (random_map_params *RP) |
58 | { |
58 | { |
59 | char **layout, buf[HUGE_BUF]; |
59 | char **layout, buf[16384]; |
60 | maptile *theMap; |
|
|
61 | int i; |
60 | int i; |
62 | |
61 | |
63 | /* pick a random seed, or use the one from the input file */ |
62 | /* pick a random seed, or use the one from the input file */ |
64 | SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0)); |
63 | SRANDOM (RP->random_seed ? RP->random_seed + RP->dungeon_level : time (0)); |
65 | |
64 | |
… | |
… | |
77 | } |
76 | } |
78 | else |
77 | else |
79 | RP->difficulty_given = 1; |
78 | RP->difficulty_given = 1; |
80 | |
79 | |
81 | if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
80 | if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
82 | RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; |
81 | RP->Xsize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5; |
83 | |
82 | |
84 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
83 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
85 | RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 25 + 5; |
84 | RP->Ysize = MIN_RANDOM_MAP_SIZE + rndm (25) + 5; |
86 | |
85 | |
87 | if (RP->expand2x > 0) |
86 | if (RP->expand2x > 0) |
88 | { |
87 | { |
89 | RP->Xsize /= 2; |
88 | RP->Xsize /= 2; |
90 | RP->Ysize /= 2; |
89 | RP->Ysize /= 2; |
… | |
… | |
100 | RP->dungeon_level += 1; |
99 | RP->dungeon_level += 1; |
101 | /* allow constant-difficulty maps. */ |
100 | /* allow constant-difficulty maps. */ |
102 | /* difficulty+=1; */ |
101 | /* difficulty+=1; */ |
103 | |
102 | |
104 | /* rotate the layout randomly */ |
103 | /* rotate the layout randomly */ |
105 | layout = rotate_layout (layout, RANDOM () % 4, RP); |
104 | layout = rotate_layout (layout, rndm (4), RP); |
106 | #ifdef RMAP_DEBUG |
105 | #ifdef RMAP_DEBUG |
107 | dump_layout (layout, RP); |
106 | dump_layout (layout, RP); |
108 | #endif |
107 | #endif |
109 | |
108 | |
110 | /* allocate the map and set the floor */ |
109 | /* allocate the map and set the floor */ |
111 | theMap = make_map_floor (layout, RP->floorstyle, RP); |
110 | make_map_floor (layout, RP->floorstyle, RP); |
112 | |
|
|
113 | /* set the name of the map. */ |
|
|
114 | theMap->path = OutFileName; |
|
|
115 | |
111 | |
116 | /* set region */ |
112 | /* set region */ |
117 | theMap->region = RP->region; |
113 | region = RP->region; |
118 | |
114 | |
119 | coroapi::cede (); |
115 | coroapi::cede (); |
|
|
116 | |
120 | /* create walls unless the wallstyle is "none" */ |
117 | /* create walls unless the wallstyle is "none" */ |
121 | if (strcmp (RP->wallstyle, "none")) |
118 | if (strcmp (RP->wallstyle, "none")) |
122 | { |
119 | { |
123 | make_map_walls (theMap, layout, RP->wallstyle, RP); |
120 | make_map_walls (this, layout, RP->wallstyle, RP); |
124 | |
121 | |
125 | /* place doors unless doorstyle or wallstyle is "none" */ |
122 | /* place doors unless doorstyle or wallstyle is "none" */ |
126 | if (strcmp (RP->doorstyle, "none")) |
123 | if (strcmp (RP->doorstyle, "none")) |
127 | put_doors (theMap, layout, RP->doorstyle, RP); |
124 | put_doors (this, layout, RP->doorstyle, RP); |
128 | |
125 | |
129 | } |
126 | } |
130 | |
127 | |
131 | coroapi::cede (); |
128 | coroapi::cede (); |
|
|
129 | |
132 | /* create exits unless the exitstyle is "none" */ |
130 | /* create exits unless the exitstyle is "none" */ |
133 | if (strcmp (RP->exitstyle, "none")) |
131 | if (strcmp (RP->exitstyle, "none")) |
134 | place_exits (theMap, layout, RP->exitstyle, RP->orientation, RP); |
132 | place_exits (this, layout, RP->exitstyle, RP->orientation, RP); |
135 | |
133 | |
136 | coroapi::cede (); |
134 | coroapi::cede (); |
|
|
135 | |
137 | place_specials_in_map (theMap, layout, RP); |
136 | place_specials_in_map (this, layout, RP); |
138 | |
137 | |
139 | coroapi::cede (); |
138 | coroapi::cede (); |
|
|
139 | |
140 | /* create monsters unless the monsterstyle is "none" */ |
140 | /* create monsters unless the monsterstyle is "none" */ |
141 | if (strcmp (RP->monsterstyle, "none")) |
141 | if (strcmp (RP->monsterstyle, "none")) |
142 | place_monsters (theMap, RP->monsterstyle, RP->difficulty, RP); |
142 | place_monsters (this, RP->monsterstyle, RP->difficulty, RP); |
143 | |
143 | |
144 | coroapi::cede (); |
144 | coroapi::cede (); |
|
|
145 | |
145 | /* treasures needs to have a proper difficulty set for the map. */ |
146 | /* treasures needs to have a proper difficulty set for the map. */ |
146 | theMap->difficulty = theMap->estimate_difficulty (); |
147 | difficulty = estimate_difficulty (); |
147 | |
148 | |
148 | coroapi::cede (); |
149 | coroapi::cede (); |
|
|
150 | |
149 | /* create treasure unless the treasurestyle is "none" */ |
151 | /* create treasure unless the treasurestyle is "none" */ |
150 | if (strcmp (RP->treasurestyle, "none")) |
152 | if (strcmp (RP->treasurestyle, "none")) |
151 | place_treasure (theMap, layout, RP->treasurestyle, RP->treasureoptions, RP); |
153 | place_treasure (this, layout, RP->treasurestyle, RP->treasureoptions, RP); |
152 | |
154 | |
153 | coroapi::cede (); |
155 | coroapi::cede (); |
|
|
156 | |
154 | /* create decor unless the decorstyle is "none" */ |
157 | /* create decor unless the decorstyle is "none" */ |
155 | if (strcmp (RP->decorstyle, "none")) |
158 | if (strcmp (RP->decorstyle, "none")) |
156 | put_decor (theMap, layout, RP->decorstyle, RP->decoroptions, RP); |
159 | put_decor (this, layout, RP->decorstyle, RP->decoroptions, RP); |
157 | |
160 | |
158 | coroapi::cede (); |
161 | coroapi::cede (); |
|
|
162 | |
159 | /* generate treasures, etc. */ |
163 | /* generate treasures, etc. */ |
160 | theMap->fix_auto_apply (); |
164 | fix_auto_apply (); |
161 | |
165 | |
162 | coroapi::cede (); |
166 | coroapi::cede (); |
163 | unblock_exits (theMap, layout, RP); |
167 | unblock_exits (this, layout, RP); |
164 | |
168 | |
165 | /* free the layout */ |
169 | /* free the layout */ |
166 | for (i = 0; i < RP->Xsize; i++) |
170 | for (i = 0; i < RP->Xsize; i++) |
167 | free (layout[i]); |
171 | free (layout[i]); |
168 | |
172 | |
169 | free (layout); |
173 | free (layout); |
170 | |
174 | |
171 | theMap->msg = strdup (buf); |
175 | msg = strdup (buf); |
172 | theMap->in_memory = MAP_IN_MEMORY; |
176 | in_memory = MAP_IN_MEMORY; |
173 | |
177 | |
174 | return theMap; |
178 | return 1; |
175 | } |
179 | } |
176 | |
180 | |
177 | /* function selects the layout function and gives it whatever |
181 | /* function selects the layout function and gives it whatever |
178 | arguments it needs. */ |
182 | arguments it needs. */ |
179 | char ** |
183 | char ** |
… | |
… | |
191 | RP->Ysize = RP->Ysize / 2 + 1; |
195 | RP->Ysize = RP->Ysize / 2 + 1; |
192 | if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
196 | if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
193 | RP->Xsize = RP->Xsize / 2 + 1; |
197 | RP->Xsize = RP->Xsize / 2 + 1; |
194 | |
198 | |
195 | if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
199 | if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
196 | RP->Xsize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5; |
200 | RP->Xsize = MIN_RANDOM_MAP_SIZE + rndm (5); |
197 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
201 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
198 | RP->Ysize = MIN_RANDOM_MAP_SIZE + RANDOM () % 5; |
202 | RP->Ysize = MIN_RANDOM_MAP_SIZE + rndm (5); |
199 | RP->map_layout_style = 0; |
203 | RP->map_layout_style = 0; |
200 | |
204 | |
201 | /* Redo this - there was a lot of redundant code of checking for preset |
205 | /* Redo this - there was a lot of redundant code of checking for preset |
202 | * layout style and then random layout style. Instead, figure out |
206 | * layout style and then random layout style. Instead, figure out |
203 | * the numeric layoutstyle, so there is only one area that actually |
207 | * the numeric layoutstyle, so there is only one area that actually |
… | |
… | |
227 | |
231 | |
228 | switch (RP->map_layout_style) |
232 | switch (RP->map_layout_style) |
229 | { |
233 | { |
230 | case LAYOUT_ONION: |
234 | case LAYOUT_ONION: |
231 | maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); |
235 | maze = map_gen_onion (RP->Xsize, RP->Ysize, RP->layoutoptions1, RP->layoutoptions2); |
232 | if (!(RANDOM () % 3) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY)) |
236 | if (!(rndm (3)) && !(RP->layoutoptions1 & RMOPT_WALLS_ONLY)) |
233 | roomify_layout (maze, RP); |
237 | roomify_layout (maze, RP); |
234 | break; |
238 | break; |
235 | |
239 | |
236 | case LAYOUT_MAZE: |
240 | case LAYOUT_MAZE: |
237 | maze = maze_gen (RP->Xsize, RP->Ysize, RANDOM () % 2); |
241 | maze = maze_gen (RP->Xsize, RP->Ysize, rndm (2)); |
238 | if (!(RANDOM () % 2)) |
242 | if (!(rndm (2))) |
239 | doorify_layout (maze, RP); |
243 | doorify_layout (maze, RP); |
240 | break; |
244 | break; |
241 | |
245 | |
242 | case LAYOUT_SPIRAL: |
246 | case LAYOUT_SPIRAL: |
243 | maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
247 | maze = map_gen_spiral (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
244 | if (!(RANDOM () % 2)) |
248 | if (!(rndm (2))) |
245 | doorify_layout (maze, RP); |
249 | doorify_layout (maze, RP); |
246 | break; |
250 | break; |
247 | |
251 | |
248 | case LAYOUT_ROGUELIKE: |
252 | case LAYOUT_ROGUELIKE: |
249 | /* Don't put symmetry in rogue maps. There isn't much reason to |
253 | /* Don't put symmetry in rogue maps. There isn't much reason to |
… | |
… | |
260 | /* no doorifying... done already */ |
264 | /* no doorifying... done already */ |
261 | break; |
265 | break; |
262 | |
266 | |
263 | case LAYOUT_SNAKE: |
267 | case LAYOUT_SNAKE: |
264 | maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
268 | maze = make_snake_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
265 | if (RANDOM () % 2) |
269 | if (rndm (2)) |
266 | roomify_layout (maze, RP); |
270 | roomify_layout (maze, RP); |
267 | break; |
271 | break; |
268 | |
272 | |
269 | case LAYOUT_SQUARE_SPIRAL: |
273 | case LAYOUT_SQUARE_SPIRAL: |
270 | maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
274 | maze = make_square_spiral_layout (RP->Xsize, RP->Ysize, RP->layoutoptions1); |
271 | if (RANDOM () % 2) |
275 | if (rndm (2)) |
272 | roomify_layout (maze, RP); |
276 | roomify_layout (maze, RP); |
273 | break; |
277 | break; |
274 | } |
278 | } |
275 | |
279 | |
276 | maze = symmetrize_layout (maze, RP->symmetry_used, RP); |
280 | maze = symmetrize_layout (maze, RP->symmetry_used, RP); |
… | |
… | |
631 | } |
635 | } |
632 | |
636 | |
633 | void |
637 | void |
634 | write_map_parameters_to_string (char *buf, random_map_params *RP) |
638 | write_map_parameters_to_string (char *buf, random_map_params *RP) |
635 | { |
639 | { |
636 | char small_buf[2048]; |
640 | char small_buf[16384]; |
637 | |
641 | |
638 | sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); |
642 | sprintf (buf, "xsize %d\nysize %d\n", RP->Xsize, RP->Ysize); |
639 | |
643 | |
640 | if (RP->wallstyle[0]) |
644 | if (RP->wallstyle[0]) |
641 | { |
645 | { |
… | |
… | |
785 | } |
789 | } |
786 | |
790 | |
787 | if (RP->random_seed) |
791 | if (RP->random_seed) |
788 | { |
792 | { |
789 | sprintf (small_buf, "random_seed %d\n", RP->random_seed); |
793 | sprintf (small_buf, "random_seed %d\n", RP->random_seed); |
|
|
794 | strcat (buf, small_buf); |
|
|
795 | } |
|
|
796 | |
|
|
797 | if (RP->custom) |
|
|
798 | { |
|
|
799 | sprintf (small_buf, "custom %s\n", RP->custom); |
790 | strcat (buf, small_buf); |
800 | strcat (buf, small_buf); |
791 | } |
801 | } |
792 | } |
802 | } |
793 | |
803 | |
794 | void |
804 | void |
… | |
… | |
816 | int difficulty_given_n, |
826 | int difficulty_given_n, |
817 | int decoroptions_n, |
827 | int decoroptions_n, |
818 | int orientation_n, |
828 | int orientation_n, |
819 | int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) |
829 | int origin_x_n, int origin_y_n, int random_seed_n, int treasureoptions_n, float difficulty_increase) |
820 | { |
830 | { |
821 | char small_buf[2048]; |
831 | char small_buf[16384]; |
822 | |
832 | |
823 | sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); |
833 | sprintf (buf, "xsize %d\nysize %d\n", xsize_n, ysize_n); |
824 | |
834 | |
825 | if (wallstyle_n && wallstyle_n[0]) |
835 | if (wallstyle_n && wallstyle_n[0]) |
826 | { |
836 | { |