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 (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * Copyright (©) 1992 Frank Tore Johansen |
7 | * |
8 | * |
8 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * Deliantra is free software: you can redistribute it and/or modify it under |
9 | * the terms of the Affero GNU General Public License as published by the |
10 | * the terms of the Affero GNU General Public License as published by the |
10 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * Free Software Foundation, either version 3 of the License, or (at your |
11 | * option) any later version. |
12 | * option) any later version. |
12 | * |
13 | * |
13 | * This program is distributed in the hope that it will be useful, |
14 | * This program is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | * GNU General Public License for more details. |
17 | * GNU General Public License for more details. |
17 | * |
18 | * |
18 | * You should have received a copy of the Affero GNU General Public License |
19 | * You should have received a copy of the Affero GNU General Public License |
19 | * and the GNU General Public License along with this program. If not, see |
20 | * and the GNU General Public License along with this program. If not, see |
20 | * <http://www.gnu.org/licenses/>. |
21 | * <http://www.gnu.org/licenses/>. |
21 | * |
22 | * |
22 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | * The authors can be reached via e-mail to <support@deliantra.net> |
23 | */ |
24 | */ |
24 | |
25 | |
25 | #include <time.h> |
26 | #include <time.h> |
26 | #include <stdio.h> |
27 | #include <stdio.h> |
27 | #include <global.h> |
28 | #include <global.h> |
28 | #include <random_map.h> |
29 | #include <rmg.h> |
29 | #include <rproto.h> |
30 | #include <rproto.h> |
30 | #include <sproto.h> |
31 | #include <sproto.h> |
31 | |
32 | |
32 | #define CEDE coroapi::cede_to_tick () |
33 | #define CEDE coroapi::cede_to_tick () |
33 | |
34 | |
34 | static void symmetrize_layout (Layout maze, random_map_params *RP); |
35 | random_map_params::random_map_params () |
35 | static void rotate_layout (Layout maze, int rotation); |
36 | { |
|
|
37 | hv = newHV (); |
|
|
38 | } |
36 | |
39 | |
|
|
40 | random_map_params::random_map_params (random_map_params *RP) |
|
|
41 | { |
|
|
42 | *this = *RP; |
|
|
43 | |
|
|
44 | HV *copy = newHV (); |
|
|
45 | |
|
|
46 | hv_iterinit (hv); |
|
|
47 | |
|
|
48 | // does not work for utf-8 keys |
|
|
49 | while (HE *he = hv_iternext (hv)) |
|
|
50 | { |
|
|
51 | STRLEN klen; const char *key = HePV (he, klen); |
|
|
52 | hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he)); |
|
|
53 | } |
|
|
54 | |
|
|
55 | hv = copy; |
|
|
56 | } |
|
|
57 | |
|
|
58 | random_map_params::random_map_params (HV *hv) |
|
|
59 | { |
|
|
60 | this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv); |
|
|
61 | |
|
|
62 | assign (wall_name, get_str ("wall_name")); |
|
|
63 | |
|
|
64 | xsize = get_iv ("xsize"); |
|
|
65 | ysize = get_iv ("ysize"); |
|
|
66 | layoutoptions1 = get_iv ("layoutoptions1"); |
|
|
67 | layoutoptions2 = get_iv ("layoutoptions2"); |
|
|
68 | layoutoptions3 = get_iv ("layoutoptions3"); |
|
|
69 | difficulty = get_iv ("difficulty"); |
|
|
70 | difficulty_given = get_iv ("difficulty_given"); |
|
|
71 | difficulty_increase = get_nv ("difficulty_increase"); |
|
|
72 | dungeon_level = get_iv ("dungeon_level"); |
|
|
73 | dungeon_depth = get_iv ("dungeon_depth"); |
|
|
74 | total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy |
|
|
75 | symmetry_used = get_iv ("symmetry_used"); |
|
|
76 | } |
|
|
77 | |
|
|
78 | random_map_params::~random_map_params () |
|
|
79 | { |
|
|
80 | SvREFCNT_dec (hv); |
|
|
81 | } |
|
|
82 | |
|
|
83 | shstr_tmp |
|
|
84 | random_map_params::as_shstr () const |
|
|
85 | { |
|
|
86 | set ("xsize" , xsize); |
|
|
87 | set ("ysize" , ysize); |
|
|
88 | set ("layoutoptions1" , layoutoptions1); |
|
|
89 | set ("layoutoptions2" , layoutoptions2); |
|
|
90 | set ("layoutoptions3" , layoutoptions3); |
|
|
91 | set ("dungeon_depth" , dungeon_depth); |
|
|
92 | set ("difficulty" , difficulty && difficulty_given ? difficulty : 0); |
|
|
93 | set ("difficulty_increase", difficulty_increase); |
|
|
94 | set ("dungeon_level" , dungeon_level); |
|
|
95 | |
|
|
96 | dynbuf_text buf; |
|
|
97 | hv_iterinit (hv); |
|
|
98 | |
|
|
99 | // does not work for utf-8 keys |
|
|
100 | while (HE *he = hv_iternext (hv)) |
|
|
101 | { |
|
|
102 | STRLEN klen; const char *key = HePV (he, klen); |
|
|
103 | STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen); |
|
|
104 | |
|
|
105 | buf.fadd (key, klen); |
|
|
106 | buf << ' '; |
|
|
107 | buf.fadd (value, vlen); |
|
|
108 | buf << '\n'; |
|
|
109 | } |
|
|
110 | |
|
|
111 | return shstr (buf); |
|
|
112 | } |
|
|
113 | |
37 | noinline SV * |
114 | ecb_noinline SV * |
38 | random_map_params::get_sv (const char *option) const |
115 | random_map_params::opt_sv (const char *option) const |
39 | { |
116 | { |
40 | SV **he = hv_fetch (hv, option, strlen (option), 0); |
117 | SV **he = hv_fetch (hv, option, strlen (option), 0); |
41 | |
118 | |
42 | return he ? *he : 0; |
119 | return he ? *he : 0; |
43 | } |
120 | } |
44 | |
121 | |
45 | noinline const_utf8_string |
122 | ecb_noinline const_utf8_string |
46 | random_map_params::get_str (const char *option, const_utf8_string fallback) const |
123 | random_map_params::get_str (const char *option, const_utf8_string fallback) const |
47 | { |
124 | { |
48 | SV *sv = get_sv (option); |
125 | SV *sv = opt_sv (option); |
49 | return sv ? cfSvPVutf8_nolen (sv) : fallback; |
126 | return sv ? cfSvPVutf8_nolen (sv) : fallback; |
50 | } |
127 | } |
51 | |
128 | |
52 | noinline IV |
129 | ecb_noinline IV |
53 | random_map_params::get_iv (const char *option, IV fallback) const |
130 | random_map_params::get_iv (const char *option, IV fallback) const |
54 | { |
131 | { |
55 | SV *sv = get_sv (option); |
132 | SV *sv = opt_sv (option); |
56 | return sv ? SvIV (sv) : fallback; |
133 | return sv ? SvIV (sv) : fallback; |
57 | } |
134 | } |
58 | |
135 | |
59 | noinline UV |
136 | ecb_noinline UV |
60 | random_map_params::get_uv (const char *option, UV fallback) const |
137 | random_map_params::get_uv (const char *option, UV fallback) const |
61 | { |
138 | { |
62 | SV *sv = get_sv (option); |
139 | SV *sv = opt_sv (option); |
63 | return sv ? SvUV (sv) : fallback; |
140 | return sv ? SvUV (sv) : fallback; |
64 | } |
141 | } |
65 | |
142 | |
66 | noinline NV |
143 | ecb_noinline NV |
67 | random_map_params::get_nv (const char *option, NV fallback) const |
144 | random_map_params::get_nv (const char *option, NV fallback) const |
68 | { |
145 | { |
69 | SV *sv = get_sv (option); |
146 | SV *sv = opt_sv (option); |
70 | return sv ? SvNV (sv) : fallback; |
147 | return sv ? SvNV (sv) : fallback; |
71 | } |
148 | } |
72 | |
149 | |
73 | noinline void |
150 | ecb_noinline void |
74 | random_map_params::set (const char *option, SV *value) const |
151 | random_map_params::set (const char *option, SV *value) const |
75 | { |
152 | { |
76 | int len = strlen (option); |
153 | int len = strlen (option); |
77 | |
154 | |
78 | if (value) |
155 | if (value) |
79 | hv_store (hv, option, len, value, 0); |
156 | hv_store (hv, option, len, value, 0); |
80 | else |
157 | else |
81 | hv_delete (hv, option, len, G_DISCARD); |
158 | hv_delete (hv, option, len, G_DISCARD); |
82 | } |
159 | } |
83 | |
160 | |
84 | noinline void |
161 | ecb_noinline void |
85 | random_map_params::set (const char *option, const_utf8_string value) const |
162 | random_map_params::set (const char *option, const_utf8_string value) const |
86 | { |
163 | { |
87 | set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); |
164 | set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); |
88 | } |
165 | } |
89 | |
166 | |
… | |
… | |
103 | random_map_params::set (const char *option, NV value) const |
180 | random_map_params::set (const char *option, NV value) const |
104 | { |
181 | { |
105 | set (option, newSVnv (value)); |
182 | set (option, newSVnv (value)); |
106 | } |
183 | } |
107 | |
184 | |
108 | void |
|
|
109 | random_map_params::hv_clone () |
|
|
110 | { |
|
|
111 | HV *copy = newHV (); |
|
|
112 | |
|
|
113 | hv_iterinit (hv); |
|
|
114 | |
|
|
115 | // does not work for utf-8 keys |
|
|
116 | while (HE *he = hv_iternext (hv)) |
|
|
117 | { |
|
|
118 | STRLEN klen; const char *key = HePV (he, klen); |
|
|
119 | hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he)); |
|
|
120 | } |
|
|
121 | |
|
|
122 | SvREFCNT_dec (hv); |
|
|
123 | hv = copy; |
|
|
124 | } |
|
|
125 | |
|
|
126 | shstr_tmp |
|
|
127 | random_map_params::as_shstr () const |
|
|
128 | { |
|
|
129 | set ("xsize" , xsize); |
|
|
130 | set ("ysize" , ysize); |
|
|
131 | set ("monsterstyle" , monsterstyle); |
|
|
132 | set ("exit_on_final_map" , exit_on_final_map); |
|
|
133 | set ("layoutstyle" , layoutstyle); |
|
|
134 | set ("doorstyle" , doorstyle); |
|
|
135 | set ("final_map" , final_map); |
|
|
136 | set ("this_map" , this_map); |
|
|
137 | set ("expand2x" , expand2x); |
|
|
138 | set ("layoutoptions1" , layoutoptions1); |
|
|
139 | set ("layoutoptions2" , layoutoptions2); |
|
|
140 | set ("layoutoptions3" , layoutoptions3); |
|
|
141 | set ("symmetry" , symmetry); |
|
|
142 | set ("dungeon_depth" , dungeon_depth); |
|
|
143 | set ("orientation" , orientation); |
|
|
144 | set ("origin_x" , origin_x); |
|
|
145 | set ("origin_y" , origin_y); |
|
|
146 | set ("random_seed" , (UV)random_seed); |
|
|
147 | set ("difficulty" , difficulty && difficulty_given ? difficulty : 0); |
|
|
148 | set ("difficulty_increase", difficulty_increase); |
|
|
149 | set ("dungeon_level" , dungeon_level); |
|
|
150 | |
|
|
151 | dynbuf_text buf; |
|
|
152 | hv_iterinit (hv); |
|
|
153 | |
|
|
154 | // does not work for utf-8 keys |
|
|
155 | while (HE *he = hv_iternext (hv)) |
|
|
156 | { |
|
|
157 | STRLEN klen; const char *key = HePV (he, klen); |
|
|
158 | STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen); |
|
|
159 | |
|
|
160 | buf.fadd (key, klen); |
|
|
161 | buf << ' '; |
|
|
162 | buf.fadd (value, vlen); |
|
|
163 | buf << '\n'; |
|
|
164 | } |
|
|
165 | |
|
|
166 | return shstr (buf); |
|
|
167 | } |
|
|
168 | |
|
|
169 | random_map_params::~random_map_params () |
|
|
170 | { |
|
|
171 | SvREFCNT_dec (hv); |
|
|
172 | } |
|
|
173 | |
|
|
174 | void |
|
|
175 | Layout::print () |
|
|
176 | { |
|
|
177 | for (int j = 0; j < ptr->h; j++) |
|
|
178 | { |
|
|
179 | for (int i = 0; i < ptr->w; i++) |
|
|
180 | putc (ptr->col[i][j] ? ptr->col[i][j] : ' ', stdout); |
|
|
181 | |
|
|
182 | putc ('\n', stdout); |
|
|
183 | } |
|
|
184 | |
|
|
185 | putc ('\n', stdout); |
|
|
186 | } |
|
|
187 | |
|
|
188 | /* takes a map and makes it symmetric: adjusts Xsize and |
|
|
189 | * Ysize to produce a symmetric map. |
|
|
190 | */ |
|
|
191 | static void |
|
|
192 | symmetrize_layout (Layout layout, random_map_params *RP) |
|
|
193 | { |
|
|
194 | if (RP->symmetry_used == SYMMETRY_NONE) |
|
|
195 | return; |
|
|
196 | |
|
|
197 | Layout sym_layout ( |
|
|
198 | RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY ? layout->w * 2 - 3 : layout->w, |
|
|
199 | RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY ? layout->h * 2 - 3 : layout->h |
|
|
200 | ); |
|
|
201 | |
|
|
202 | if (RP->symmetry_used == SYMMETRY_X) |
|
|
203 | for (int i = 0; i < sym_layout->w / 2 + 1; i++) |
|
|
204 | for (int j = 0; j < sym_layout->h; j++) |
|
|
205 | { |
|
|
206 | sym_layout[i ][j] = |
|
|
207 | sym_layout[sym_layout->w - i - 1][j] = layout[i][j]; |
|
|
208 | } |
|
|
209 | |
|
|
210 | if (RP->symmetry_used == SYMMETRY_Y) |
|
|
211 | for (int i = 0; i < sym_layout->w; i++) |
|
|
212 | for (int j = 0; j < sym_layout->h / 2 + 1; j++) |
|
|
213 | { |
|
|
214 | sym_layout[i][j ] = |
|
|
215 | sym_layout[i][sym_layout->h - j - 1] = layout[i][j]; |
|
|
216 | } |
|
|
217 | |
|
|
218 | if (RP->symmetry_used == SYMMETRY_XY) |
|
|
219 | for (int i = 0; i < sym_layout->w / 2 + 1; i++) |
|
|
220 | for (int j = 0; j < sym_layout->h / 2 + 1; j++) |
|
|
221 | { |
|
|
222 | sym_layout[i ][j ] = |
|
|
223 | sym_layout[i ][sym_layout->h - j - 1] = |
|
|
224 | sym_layout[sym_layout->w - i - 1][j ] = |
|
|
225 | sym_layout[sym_layout->w - i - 1][sym_layout->h - j - 1] = layout[i][j]; |
|
|
226 | } |
|
|
227 | |
|
|
228 | layout.swap (sym_layout); |
|
|
229 | sym_layout.free (); |
|
|
230 | |
|
|
231 | /* reconnect disjointed spirals */ |
|
|
232 | /* reconnect disjointed nethacklayouts: the routine for |
|
|
233 | spirals will do the trick? */ |
|
|
234 | if (RP->map_layout_style == LAYOUT_SPIRAL |
|
|
235 | || RP->map_layout_style == LAYOUT_ROGUELIKE) |
|
|
236 | connect_spirals (layout->w, layout->h, RP->symmetry_used, layout); |
|
|
237 | } |
|
|
238 | |
|
|
239 | /* takes a map and rotates it. This completes the |
|
|
240 | onion layouts, making them possibly centered on any wall. |
|
|
241 | It'll modify Xsize and Ysize if they're swapped. |
|
|
242 | */ |
|
|
243 | static void |
|
|
244 | rotate_layout (Layout layout, int rotation) |
|
|
245 | { |
|
|
246 | int w = layout->w; |
|
|
247 | int h = layout->h; |
|
|
248 | |
|
|
249 | switch (rotation) |
|
|
250 | { |
|
|
251 | case 2: /* a reflection */ |
|
|
252 | { |
|
|
253 | Layout new_layout (w, h); |
|
|
254 | |
|
|
255 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
|
|
256 | for (int j = 0; j < h; j++) |
|
|
257 | new_layout[i][j] = layout[w - i - 1][h - j - 1]; |
|
|
258 | |
|
|
259 | layout.swap (new_layout); |
|
|
260 | new_layout.free (); |
|
|
261 | } |
|
|
262 | break; |
|
|
263 | |
|
|
264 | case 1: |
|
|
265 | case 3: |
|
|
266 | { |
|
|
267 | Layout new_layout (h, w); |
|
|
268 | |
|
|
269 | if (rotation == 1) /* swap x and y */ |
|
|
270 | for (int i = 0; i < w; i++) |
|
|
271 | for (int j = 0; j < h; j++) |
|
|
272 | new_layout[j][i] = layout[i][j]; |
|
|
273 | |
|
|
274 | if (rotation == 3) /* swap x and y */ |
|
|
275 | for (int i = 0; i < w; i++) |
|
|
276 | for (int j = 0; j < h; j++) |
|
|
277 | new_layout[j][i] = layout[w - i - 1][h - j - 1]; |
|
|
278 | |
|
|
279 | layout.swap (new_layout); |
|
|
280 | new_layout.free (); |
|
|
281 | } |
|
|
282 | break; |
|
|
283 | } |
|
|
284 | } |
|
|
285 | |
|
|
286 | /* checks the layout to see if I can stick a horizontal(dir = 0) wall |
|
|
287 | (or vertical, dir == 1) |
|
|
288 | here which ends up on other walls sensibly. */ |
|
|
289 | static int |
|
|
290 | can_make_wall (char **maze, int dx, int dy, int dir, random_map_params *RP) |
|
|
291 | { |
|
|
292 | int i1; |
|
|
293 | int length = 0; |
|
|
294 | |
|
|
295 | /* dont make walls if we're on the edge. */ |
|
|
296 | if (dx == 0 || dx == (RP->Xsize - 1) || dy == 0 || dy == (RP->Ysize - 1)) |
|
|
297 | return -1; |
|
|
298 | |
|
|
299 | /* don't make walls if we're ON a wall. */ |
|
|
300 | if (maze[dx][dy] != 0) |
|
|
301 | return -1; |
|
|
302 | |
|
|
303 | if (dir == 0) /* horizontal */ |
|
|
304 | { |
|
|
305 | int y = dy; |
|
|
306 | |
|
|
307 | for (i1 = dx - 1; i1 > 0; i1--) |
|
|
308 | { |
|
|
309 | int sindex = surround_flag2 (maze, i1, y, RP); |
|
|
310 | |
|
|
311 | if (sindex == 1) |
|
|
312 | break; |
|
|
313 | if (sindex != 0) |
|
|
314 | return -1; /* can't make horiz. wall here */ |
|
|
315 | if (maze[i1][y] != 0) |
|
|
316 | return -1; /* can't make horiz. wall here */ |
|
|
317 | length++; |
|
|
318 | } |
|
|
319 | |
|
|
320 | for (i1 = dx + 1; i1 < RP->Xsize - 1; i1++) |
|
|
321 | { |
|
|
322 | int sindex = surround_flag2 (maze, i1, y, RP); |
|
|
323 | |
|
|
324 | if (sindex == 2) |
|
|
325 | break; |
|
|
326 | if (sindex != 0) |
|
|
327 | return -1; /* can't make horiz. wall here */ |
|
|
328 | if (maze[i1][y] != 0) |
|
|
329 | return -1; /* can't make horiz. wall here */ |
|
|
330 | length++; |
|
|
331 | } |
|
|
332 | return length; |
|
|
333 | } |
|
|
334 | else |
|
|
335 | { /* vertical */ |
|
|
336 | int x = dx; |
|
|
337 | |
|
|
338 | for (i1 = dy - 1; i1 > 0; i1--) |
|
|
339 | { |
|
|
340 | int sindex = surround_flag2 (maze, x, i1, RP); |
|
|
341 | |
|
|
342 | if (sindex == 4) |
|
|
343 | break; |
|
|
344 | if (sindex != 0) |
|
|
345 | return -1; /* can't make vert. wall here */ |
|
|
346 | if (maze[x][i1] != 0) |
|
|
347 | return -1; /* can't make horiz. wall here */ |
|
|
348 | length++; |
|
|
349 | } |
|
|
350 | |
|
|
351 | for (i1 = dy + 1; i1 < RP->Ysize - 1; i1++) |
|
|
352 | { |
|
|
353 | int sindex = surround_flag2 (maze, x, i1, RP); |
|
|
354 | |
|
|
355 | if (sindex == 8) |
|
|
356 | break; |
|
|
357 | if (sindex != 0) |
|
|
358 | return -1; /* can't make verti. wall here */ |
|
|
359 | if (maze[x][i1] != 0) |
|
|
360 | return -1; /* can't make horiz. wall here */ |
|
|
361 | length++; |
|
|
362 | } |
|
|
363 | |
|
|
364 | return length; |
|
|
365 | } |
|
|
366 | |
|
|
367 | return -1; |
|
|
368 | } |
|
|
369 | |
|
|
370 | /* take a layout and make some rooms in it. |
|
|
371 | --works best on onions.*/ |
|
|
372 | static void |
|
|
373 | roomify_layout (char **maze, random_map_params *RP) |
|
|
374 | { |
|
|
375 | int tries = RP->Xsize * RP->Ysize / 30; |
|
|
376 | |
|
|
377 | for (int ti = 0; ti < tries; ti++) |
|
|
378 | { |
|
|
379 | /* starting location for looking at creating a door */ |
|
|
380 | int dx = rmg_rndm (RP->Xsize); |
|
|
381 | int dy = rmg_rndm (RP->Ysize); |
|
|
382 | |
|
|
383 | /* results of checking on creating walls. */ |
|
|
384 | int cx = can_make_wall (maze, dx, dy, 0, RP); /* horizontal */ |
|
|
385 | int cy = can_make_wall (maze, dx, dy, 1, RP); /* vertical */ |
|
|
386 | |
|
|
387 | if (cx == -1) |
|
|
388 | { |
|
|
389 | if (cy != -1) |
|
|
390 | make_wall (maze, dx, dy, 1); |
|
|
391 | |
|
|
392 | continue; |
|
|
393 | } |
|
|
394 | |
|
|
395 | if (cy == -1) |
|
|
396 | { |
|
|
397 | make_wall (maze, dx, dy, 0); |
|
|
398 | continue; |
|
|
399 | } |
|
|
400 | |
|
|
401 | if (cx < cy) |
|
|
402 | make_wall (maze, dx, dy, 0); |
|
|
403 | else |
|
|
404 | make_wall (maze, dx, dy, 1); |
|
|
405 | } |
|
|
406 | } |
|
|
407 | |
|
|
408 | int |
|
|
409 | make_wall (char **maze, int x, int y, int dir) |
|
|
410 | { |
|
|
411 | maze[x][y] = 'D'; /* mark a door */ |
|
|
412 | |
|
|
413 | switch (dir) |
|
|
414 | { |
|
|
415 | case 0: /* horizontal */ |
|
|
416 | { |
|
|
417 | for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#'; |
|
|
418 | for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#'; |
|
|
419 | break; |
|
|
420 | } |
|
|
421 | case 1: /* vertical */ |
|
|
422 | { |
|
|
423 | for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#'; |
|
|
424 | for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#'; |
|
|
425 | break; |
|
|
426 | } |
|
|
427 | } |
|
|
428 | |
|
|
429 | return 0; |
|
|
430 | } |
|
|
431 | |
|
|
432 | /* puts doors at appropriate locations in a layout. */ |
|
|
433 | static void |
|
|
434 | doorify_layout (char **maze, random_map_params *RP) |
|
|
435 | { |
|
|
436 | int ndoors = RP->Xsize * RP->Ysize / 60; /* reasonable number of doors. */ |
|
|
437 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
438 | |
|
|
439 | uint16 *doorlist_x = salloc<uint16> (RP->Xsize * RP->Ysize); |
|
|
440 | uint16 *doorlist_y = salloc<uint16> (RP->Xsize * RP->Ysize); |
|
|
441 | |
|
|
442 | /* make a list of possible door locations */ |
|
|
443 | for (int i = 1; i < RP->Xsize - 1; i++) |
|
|
444 | for (int j = 1; j < RP->Ysize - 1; j++) |
|
|
445 | { |
|
|
446 | int sindex = surround_flag (maze, i, j, RP); |
|
|
447 | |
|
|
448 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
449 | { |
|
|
450 | doorlist_x[doorlocs] = i; |
|
|
451 | doorlist_y[doorlocs] = j; |
|
|
452 | doorlocs++; |
|
|
453 | } |
|
|
454 | } |
|
|
455 | |
|
|
456 | while (ndoors > 0 && doorlocs > 0) |
|
|
457 | { |
|
|
458 | int di = rmg_rndm (doorlocs); |
|
|
459 | int i = doorlist_x[di]; |
|
|
460 | int j = doorlist_y[di]; |
|
|
461 | int sindex = surround_flag (maze, i, j, RP); |
|
|
462 | |
|
|
463 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
464 | { |
|
|
465 | maze[i][j] = 'D'; |
|
|
466 | ndoors--; |
|
|
467 | } |
|
|
468 | |
|
|
469 | /* reduce the size of the list */ |
|
|
470 | doorlocs--; |
|
|
471 | doorlist_x[di] = doorlist_x[doorlocs]; |
|
|
472 | doorlist_y[di] = doorlist_y[doorlocs]; |
|
|
473 | } |
|
|
474 | |
|
|
475 | sfree (doorlist_x, RP->Xsize * RP->Ysize); |
|
|
476 | sfree (doorlist_y, RP->Xsize * RP->Ysize); |
|
|
477 | } |
|
|
478 | |
|
|
479 | ///////////////////////////////////////////////////////////////////////////// |
|
|
480 | |
|
|
481 | LayoutData::LayoutData (int w, int h) |
|
|
482 | : w(w), h(h) |
|
|
483 | { |
|
|
484 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
|
|
485 | |
|
|
486 | col = (char **)salloc<char> (size); |
|
|
487 | |
|
|
488 | char *data = (char *)(col + w); |
|
|
489 | |
|
|
490 | for (int x = w; x--; ) |
|
|
491 | col [x] = data + x * h; |
|
|
492 | } |
|
|
493 | |
|
|
494 | LayoutData::~LayoutData () |
|
|
495 | { |
|
|
496 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
|
|
497 | |
|
|
498 | sfree ((char *)col, size); |
|
|
499 | } |
|
|
500 | |
|
|
501 | void LayoutData::clear (char fill) |
|
|
502 | { |
|
|
503 | memset (col [0], fill, w * h); |
|
|
504 | } |
|
|
505 | |
|
|
506 | void LayoutData::border (char fill) |
|
|
507 | { |
|
|
508 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
|
|
509 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
|
|
510 | } |
|
|
511 | |
|
|
512 | /* function selects the layout function and gives it whatever |
|
|
513 | arguments it needs. */ |
|
|
514 | static Layout |
|
|
515 | layoutgen (random_map_params *RP) |
|
|
516 | { |
|
|
517 | Layout layout (RP); |
|
|
518 | |
|
|
519 | switch (RP->map_layout_style) |
|
|
520 | { |
|
|
521 | case LAYOUT_ONION: |
|
|
522 | map_gen_onion (layout, RP->layoutoptions1, RP->layoutoptions2); |
|
|
523 | |
|
|
524 | if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF))) |
|
|
525 | roomify_layout (layout, RP); |
|
|
526 | |
|
|
527 | break; |
|
|
528 | |
|
|
529 | case LAYOUT_MAZE: |
|
|
530 | maze_gen (layout, RP->get_iv ("maze_type", rmg_rndm (4))); |
|
|
531 | |
|
|
532 | if (!(rmg_rndm (2))) |
|
|
533 | doorify_layout (layout, RP); |
|
|
534 | |
|
|
535 | break; |
|
|
536 | |
|
|
537 | case LAYOUT_SPIRAL: |
|
|
538 | map_gen_spiral (layout, RP->layoutoptions1); |
|
|
539 | |
|
|
540 | if (!(rmg_rndm (2))) |
|
|
541 | doorify_layout (layout, RP); |
|
|
542 | |
|
|
543 | break; |
|
|
544 | |
|
|
545 | case LAYOUT_ROGUELIKE: |
|
|
546 | /* Don't put symmetry in rogue maps. There isn't much reason to |
|
|
547 | * do so in the first place (doesn't make it any more interesting), |
|
|
548 | * but more importantly, the symmetry code presumes we are symmetrizing |
|
|
549 | * spirals, or maps with lots of passages - making a symmetric rogue |
|
|
550 | * map fails because its likely that the passages the symmetry process |
|
|
551 | * creates may not connect the rooms. |
|
|
552 | */ |
|
|
553 | RP->symmetry_used = SYMMETRY_NONE; |
|
|
554 | roguelike_layout_gen (layout, RP->layoutoptions1); |
|
|
555 | /* no doorifying... done already */ |
|
|
556 | break; |
|
|
557 | |
|
|
558 | case LAYOUT_SNAKE: |
|
|
559 | make_snake_layout (layout, RP->layoutoptions1); |
|
|
560 | |
|
|
561 | if (rmg_rndm (2)) |
|
|
562 | roomify_layout (layout, RP); |
|
|
563 | |
|
|
564 | break; |
|
|
565 | |
|
|
566 | case LAYOUT_SQUARE_SPIRAL: |
|
|
567 | make_square_spiral_layout (layout, RP->layoutoptions1); |
|
|
568 | |
|
|
569 | if (rmg_rndm (2)) |
|
|
570 | roomify_layout (layout, RP); |
|
|
571 | |
|
|
572 | break; |
|
|
573 | |
|
|
574 | default: |
|
|
575 | abort (); |
|
|
576 | } |
|
|
577 | |
|
|
578 | /* rotate the layout randomly */ |
|
|
579 | rotate_layout (layout, rmg_rndm (4)); |
|
|
580 | |
|
|
581 | symmetrize_layout (layout, RP); |
|
|
582 | |
|
|
583 | #ifdef RMAP_DEBUG |
|
|
584 | dump_layout (layout); |
|
|
585 | #endif |
|
|
586 | |
|
|
587 | if (RP->expand2x) |
|
|
588 | expand2x (layout); |
|
|
589 | |
|
|
590 | return layout; |
|
|
591 | } |
|
|
592 | |
|
|
593 | bool |
185 | bool |
594 | maptile::generate_random_map (random_map_params *RP) |
186 | maptile::generate_random_map (random_map_params *RP) |
595 | { |
187 | { |
596 | RP->Xsize = RP->xsize; |
188 | RP->Xsize = RP->xsize; |
597 | RP->Ysize = RP->ysize; |
189 | RP->Ysize = RP->ysize; |
598 | |
190 | |
|
|
191 | max_it (RP->dungeon_level, 1); |
|
|
192 | |
|
|
193 | IV expand2x = RP->get_iv ("expand2x"); |
|
|
194 | UV random_seed = RP->get_uv ("random_seed"); |
|
|
195 | |
599 | /* pick a random seed, or use the one from the input file */ |
196 | /* pick a random seed, or use the one from the input file */ |
600 | RP->random_seed = RP->random_seed |
197 | random_seed = random_seed |
601 | ? RP->random_seed + RP->dungeon_level |
198 | ? random_seed + RP->dungeon_level |
602 | : time (0); |
199 | : server_tick; |
603 | |
200 | |
604 | // we run "single-threaded" |
201 | // we run "single-threaded" |
605 | rmg_rndm.seed (RP->random_seed); |
202 | rmg_rndm.seed (random_seed); |
606 | |
203 | |
607 | shstr buf = RP->as_shstr (); |
204 | shstr buf = RP->as_shstr (); |
608 | |
205 | |
609 | if (RP->difficulty == 0) |
206 | if (RP->difficulty == 0) |
610 | { |
207 | { |
611 | RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ |
208 | RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ |
612 | |
209 | |
613 | if (RP->difficulty_increase > 0.001) |
210 | if (RP->difficulty_increase > 0.001f) |
614 | RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); |
211 | RP->difficulty = RP->dungeon_level * RP->difficulty_increase; |
615 | |
212 | |
616 | if (RP->difficulty < 1) |
213 | if (RP->difficulty < 1) |
617 | RP->difficulty = 1; |
214 | RP->difficulty = 1; |
618 | } |
215 | } |
619 | else |
216 | else |
… | |
… | |
623 | RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
220 | RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
624 | |
221 | |
625 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
222 | if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
626 | RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
223 | RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
627 | |
224 | |
|
|
225 | min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); |
|
|
226 | min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); |
|
|
227 | |
|
|
228 | int symmetry = RP->get_iv ("symmetry", SYMMETRY_NONE); |
|
|
229 | |
628 | if (RP->symmetry == SYMMETRY_RANDOM) |
230 | if (symmetry == SYMMETRY_RANDOM) |
629 | RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; |
231 | RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; |
630 | else |
232 | else |
631 | RP->symmetry_used = RP->symmetry; |
233 | RP->symmetry_used = symmetry; |
632 | |
234 | |
633 | if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) |
235 | if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) |
634 | RP->Ysize = RP->Ysize / 2 + 1; |
236 | RP->Ysize = RP->Ysize / 2 + 1; |
635 | |
237 | |
636 | if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
238 | if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
637 | RP->Xsize = RP->Xsize / 2 + 1; |
239 | RP->Xsize = RP->Xsize / 2 + 1; |
638 | |
240 | |
639 | if (RP->expand2x > 0) |
241 | if (expand2x) |
640 | { |
242 | { |
641 | RP->Xsize /= 2; |
243 | RP->Xsize /= 2; |
642 | RP->Ysize /= 2; |
244 | RP->Ysize /= 2; |
643 | } |
245 | } |
644 | |
246 | |
645 | RP->map_layout_style = LAYOUT_NONE; |
247 | const char *layoutstyle = RP->get_str ("layoutstyle", ""); |
646 | |
248 | |
647 | /* Redo this - there was a lot of redundant code of checking for preset |
|
|
648 | * layout style and then random layout style. Instead, figure out |
|
|
649 | * the numeric layoutstyle, so there is only one area that actually |
|
|
650 | * calls the code to make the maps. |
|
|
651 | */ |
|
|
652 | if (strstr (RP->layoutstyle, "onion")) |
249 | if (strstr (layoutstyle, "onion")) |
653 | RP->map_layout_style = LAYOUT_ONION; |
250 | RP->map_layout_style = LAYOUT_ONION; |
654 | else if (strstr (RP->layoutstyle, "maze")) |
251 | else if (strstr (layoutstyle, "maze")) |
655 | RP->map_layout_style = LAYOUT_MAZE; |
252 | RP->map_layout_style = LAYOUT_MAZE; |
656 | else if (strstr (RP->layoutstyle, "spiral")) |
253 | else if (strstr (layoutstyle, "spiral")) |
657 | RP->map_layout_style = LAYOUT_SPIRAL; |
254 | RP->map_layout_style = LAYOUT_SPIRAL; |
658 | else if (strstr (RP->layoutstyle, "rogue")) |
255 | else if (strstr (layoutstyle, "rogue")) |
659 | RP->map_layout_style = LAYOUT_ROGUELIKE; |
256 | RP->map_layout_style = LAYOUT_ROGUELIKE; |
660 | else if (strstr (RP->layoutstyle, "snake")) |
257 | else if (strstr (layoutstyle, "snake")) |
661 | RP->map_layout_style = LAYOUT_SNAKE; |
258 | RP->map_layout_style = LAYOUT_SNAKE; |
662 | else if (strstr (RP->layoutstyle, "squarespiral")) |
259 | else if (strstr (layoutstyle, "squarespiral")) |
663 | RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; |
260 | RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; |
|
|
261 | else if (strstr (layoutstyle, "cave")) |
664 | else if (RP->map_layout_style == LAYOUT_NONE) |
262 | RP->map_layout_style = LAYOUT_CAVE; |
|
|
263 | else if (strstr (layoutstyle, "castle")) |
|
|
264 | RP->map_layout_style = LAYOUT_CASTLE; |
|
|
265 | else if (strstr (layoutstyle, "multiple")) |
|
|
266 | RP->map_layout_style = LAYOUT_MULTIPLE; |
|
|
267 | else |
665 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ |
268 | RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ |
666 | else |
|
|
667 | abort (); |
|
|
668 | |
269 | |
669 | Layout layout = layoutgen (RP); |
270 | layout maze (RP->Xsize, RP->Ysize); |
|
|
271 | maze.generate (RP); |
670 | |
272 | |
671 | #ifdef RMAP_DEBUG |
273 | if (RP->get_iv ("rotate", 1)) |
672 | dump_layout (layout); |
274 | maze.rotate (rmg_rndm (4)); |
|
|
275 | |
|
|
276 | maze.symmetrize (RP->symmetry_used); |
|
|
277 | |
|
|
278 | if (expand2x) |
|
|
279 | maze.expand2x (); |
|
|
280 | |
|
|
281 | #if 0 |
|
|
282 | maze.print ();//D |
673 | #endif |
283 | #endif |
674 | |
284 | |
675 | /* increment these for the current map */ |
285 | /* increment these for the current map */ |
676 | ++RP->dungeon_level; |
286 | ++RP->dungeon_level; |
677 | |
287 | |
678 | // need to patch RP becasue following code doesn't use the Layout object |
288 | // need to patch RP becasue following code doesn't use the layout object |
679 | RP->Xsize = layout->w; |
289 | RP->Xsize = maze.w; |
680 | RP->Ysize = layout->h; |
290 | RP->Ysize = maze.h; |
681 | |
291 | |
682 | /* allocate the map and set the floor */ |
292 | /* allocate the map and set the floor */ |
683 | make_map_floor (layout, RP->get_str ("floorstyle", ""), RP); |
293 | make_map_floor (maze, RP->get_str ("floorstyle", ""), RP); |
684 | |
294 | |
685 | /* set region */ |
295 | /* set region */ |
686 | default_region = RP->region; |
296 | default_region = region::find (RP->get_str ("region", 0)); |
687 | |
297 | |
688 | CEDE; |
298 | CEDE; |
689 | |
299 | |
690 | place_specials_in_map (this, layout, RP); |
300 | place_specials_in_map (this, maze, RP); |
691 | |
301 | |
692 | CEDE; |
302 | CEDE; |
693 | |
303 | |
694 | const char *wallstyle = RP->get_str ("wallstyle", 0); |
304 | const char *wallstyle = RP->get_str ("wallstyle", ""); |
695 | |
305 | |
696 | /* create walls unless the wallstyle is "none" */ |
306 | /* create walls unless the wallstyle is "none" */ |
697 | if (strcmp (wallstyle, "none")) |
307 | if (strcmp (wallstyle, "none")) |
698 | { |
308 | { |
699 | make_map_walls (this, layout, wallstyle, RP->get_str ("miningstyle", ""), RP); |
309 | make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); |
|
|
310 | |
|
|
311 | const char *doorstyle = RP->get_str ("doorstyle", ""); |
700 | |
312 | |
701 | /* place doors unless doorstyle or wallstyle is "none" */ |
313 | /* place doors unless doorstyle or wallstyle is "none" */ |
702 | if (strcmp (RP->doorstyle, "none")) |
314 | if (strcmp (doorstyle, "none")) |
703 | put_doors (this, layout, RP->doorstyle, RP); |
315 | put_doors (this, maze, doorstyle, RP); |
704 | } |
316 | } |
705 | |
317 | |
706 | CEDE; |
318 | CEDE; |
707 | |
319 | |
708 | const char *exitstyle = RP->get_str ("exitstyle", ""); |
320 | const char *exitstyle = RP->get_str ("exitstyle", ""); |
709 | |
321 | |
710 | /* create exits unless the exitstyle is "none" */ |
322 | /* create exits unless the exitstyle is "none" */ |
711 | if (strcmp (exitstyle, "none")) |
323 | if (strcmp (exitstyle, "none")) |
712 | place_exits (this, layout, exitstyle, RP->orientation, RP); |
324 | place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP); |
713 | |
325 | |
714 | CEDE; |
326 | CEDE; |
|
|
327 | |
|
|
328 | const char *monsterstyle = RP->get_str ("monsterstyle", ""); |
715 | |
329 | |
716 | /* create monsters unless the monsterstyle is "none" */ |
330 | /* create monsters unless the monsterstyle is "none" */ |
717 | if (strcmp (RP->monsterstyle, "none")) |
331 | if (strcmp (monsterstyle, "none")) |
718 | place_monsters (this, RP->monsterstyle, RP->difficulty, RP); |
332 | place_monsters (this, monsterstyle, RP->difficulty, RP); |
719 | |
333 | |
720 | CEDE; |
334 | CEDE; |
721 | |
335 | |
722 | /* treasures needs to have a proper difficulty set for the map. */ |
336 | /* treasures needs to have a proper difficulty set for the map. */ |
723 | difficulty = estimate_difficulty (); |
337 | difficulty = estimate_difficulty (); |
… | |
… | |
725 | CEDE; |
339 | CEDE; |
726 | |
340 | |
727 | const char *treasurestyle = RP->get_str ("treasurestyle", ""); |
341 | const char *treasurestyle = RP->get_str ("treasurestyle", ""); |
728 | |
342 | |
729 | /* create treasure unless the treasurestyle is "none" */ |
343 | /* create treasure unless the treasurestyle is "none" */ |
730 | place_treasure (this, layout, treasurestyle, RP->get_iv ("treasureoptions"), RP); |
344 | place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); |
731 | |
345 | |
732 | CEDE; |
346 | CEDE; |
733 | |
347 | |
734 | const char *decorstyle = RP->get_str ("treasurestyle", ""); |
348 | const char *decorstyle = RP->get_str ("decorstyle", ""); |
735 | |
349 | |
736 | /* create decor unless the decorstyle is "none" */ |
350 | /* create decor unless the decorstyle is "none" */ |
737 | if (strcmp (decorstyle, "none")) |
351 | if (strcmp (decorstyle, "none")) |
738 | put_decor (this, layout, decorstyle, RP->get_iv ("decoroptions"), RP); |
352 | put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); |
739 | |
353 | |
740 | CEDE; |
354 | CEDE; |
741 | |
355 | |
742 | /* generate treasures, etc. */ |
356 | /* generate treasures, etc. */ |
743 | fix_auto_apply (); |
357 | fix_auto_apply (); |
744 | |
358 | |
745 | CEDE; |
359 | CEDE; |
746 | |
360 | |
747 | unblock_exits (this, layout, RP); |
361 | unblock_exits (this, maze); |
748 | |
362 | |
749 | msg = buf; |
363 | msg = buf; |
750 | in_memory = MAP_ACTIVE; |
364 | state = MAP_INACTIVE; // this is probably a lie, but currently works with ext/map-random.ext |
751 | |
365 | |
752 | CEDE; |
366 | CEDE; |
753 | |
367 | |
754 | return 1; |
368 | return 1; |
755 | } |
369 | } |