1 |
elmex |
1.1 |
/* |
2 |
root |
1.31 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
pippijn |
1.20 |
* |
4 |
root |
1.47 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.46 |
* Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
6 |
|
|
* Copyright (©) 1992 Frank Tore Johansen |
7 |
pippijn |
1.20 |
* |
8 |
root |
1.42 |
* 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 |
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
11 |
|
|
* option) any later version. |
12 |
pippijn |
1.20 |
* |
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 |
root |
1.30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
pippijn |
1.20 |
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
root |
1.42 |
* 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 |
|
|
* <http://www.gnu.org/licenses/>. |
21 |
root |
1.30 |
* |
22 |
root |
1.31 |
* The authors can be reached via e-mail to <support@deliantra.net> |
23 |
pippijn |
1.20 |
*/ |
24 |
elmex |
1.1 |
|
25 |
|
|
#include <time.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <global.h> |
28 |
|
|
#include <random_map.h> |
29 |
|
|
#include <rproto.h> |
30 |
|
|
#include <sproto.h> |
31 |
|
|
|
32 |
root |
1.39 |
#define CEDE coroapi::cede_to_tick () |
33 |
root |
1.22 |
|
34 |
root |
1.63 |
random_map_params::random_map_params () |
35 |
|
|
{ |
36 |
|
|
hv = newHV (); |
37 |
|
|
} |
38 |
|
|
|
39 |
root |
1.64 |
random_map_params::random_map_params (random_map_params *RP) |
40 |
|
|
{ |
41 |
|
|
*this = *RP; |
42 |
|
|
|
43 |
|
|
HV *copy = newHV (); |
44 |
|
|
|
45 |
|
|
hv_iterinit (hv); |
46 |
|
|
|
47 |
|
|
// does not work for utf-8 keys |
48 |
|
|
while (HE *he = hv_iternext (hv)) |
49 |
|
|
{ |
50 |
|
|
STRLEN klen; const char *key = HePV (he, klen); |
51 |
|
|
hv_store (copy, key, klen, newSVsv (HeVAL (he)), HeHASH (he)); |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
hv = copy; |
55 |
|
|
} |
56 |
|
|
|
57 |
root |
1.63 |
random_map_params::random_map_params (HV *hv) |
58 |
|
|
{ |
59 |
|
|
this->hv = (HV *)SvREFCNT_inc_NN ((SV *)hv); |
60 |
|
|
|
61 |
|
|
assign (wall_name, get_str ("wall_name")); |
62 |
|
|
|
63 |
|
|
xsize = get_iv ("xsize"); |
64 |
|
|
ysize = get_iv ("ysize"); |
65 |
|
|
expand2x = get_iv ("expand2x"); |
66 |
|
|
layoutoptions1 = get_iv ("layoutoptions1"); |
67 |
|
|
layoutoptions2 = get_iv ("layoutoptions2"); |
68 |
|
|
layoutoptions3 = get_iv ("layoutoptions3"); |
69 |
|
|
symmetry = get_iv ("symmetry"); |
70 |
|
|
difficulty = get_iv ("difficulty"); |
71 |
|
|
difficulty_given = get_iv ("difficulty_given"); |
72 |
|
|
difficulty_increase = get_nv ("difficulty_increase"); |
73 |
|
|
dungeon_level = get_iv ("dungeon_level"); |
74 |
|
|
dungeon_depth = get_iv ("dungeon_depth"); |
75 |
|
|
orientation = get_iv ("orientation"); |
76 |
|
|
random_seed = get_uv ("random_seed"); |
77 |
|
|
total_map_hp = get_nv ("total_map_hp"); // actually val64, but I am too lazy |
78 |
|
|
symmetry_used = get_iv ("symmetry_used"); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
random_map_params::~random_map_params () |
82 |
|
|
{ |
83 |
|
|
SvREFCNT_dec (hv); |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
shstr_tmp |
87 |
|
|
random_map_params::as_shstr () const |
88 |
|
|
{ |
89 |
|
|
set ("xsize" , xsize); |
90 |
|
|
set ("ysize" , ysize); |
91 |
|
|
set ("layoutoptions1" , layoutoptions1); |
92 |
|
|
set ("layoutoptions2" , layoutoptions2); |
93 |
|
|
set ("layoutoptions3" , layoutoptions3); |
94 |
|
|
set ("symmetry" , symmetry); |
95 |
|
|
set ("dungeon_depth" , dungeon_depth); |
96 |
|
|
set ("orientation" , orientation); |
97 |
|
|
set ("random_seed" , (UV)random_seed); |
98 |
|
|
set ("difficulty" , difficulty && difficulty_given ? difficulty : 0); |
99 |
|
|
set ("difficulty_increase", difficulty_increase); |
100 |
|
|
set ("dungeon_level" , dungeon_level); |
101 |
|
|
|
102 |
|
|
dynbuf_text buf; |
103 |
|
|
hv_iterinit (hv); |
104 |
|
|
|
105 |
|
|
// does not work for utf-8 keys |
106 |
|
|
while (HE *he = hv_iternext (hv)) |
107 |
|
|
{ |
108 |
|
|
STRLEN klen; const char *key = HePV (he, klen); |
109 |
|
|
STRLEN vlen; const char *value = SvPVutf8 (HeVAL (he), vlen); |
110 |
|
|
|
111 |
|
|
buf.fadd (key, klen); |
112 |
|
|
buf << ' '; |
113 |
|
|
buf.fadd (value, vlen); |
114 |
|
|
buf << '\n'; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
return shstr (buf); |
118 |
|
|
} |
119 |
|
|
|
120 |
root |
1.55 |
noinline SV * |
121 |
|
|
random_map_params::get_sv (const char *option) const |
122 |
root |
1.51 |
{ |
123 |
|
|
SV **he = hv_fetch (hv, option, strlen (option), 0); |
124 |
|
|
|
125 |
root |
1.55 |
return he ? *he : 0; |
126 |
root |
1.51 |
} |
127 |
|
|
|
128 |
root |
1.55 |
noinline const_utf8_string |
129 |
|
|
random_map_params::get_str (const char *option, const_utf8_string fallback) const |
130 |
|
|
{ |
131 |
|
|
SV *sv = get_sv (option); |
132 |
|
|
return sv ? cfSvPVutf8_nolen (sv) : fallback; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
noinline IV |
136 |
root |
1.51 |
random_map_params::get_iv (const char *option, IV fallback) const |
137 |
|
|
{ |
138 |
root |
1.55 |
SV *sv = get_sv (option); |
139 |
|
|
return sv ? SvIV (sv) : fallback; |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
noinline UV |
143 |
|
|
random_map_params::get_uv (const char *option, UV fallback) const |
144 |
|
|
{ |
145 |
|
|
SV *sv = get_sv (option); |
146 |
|
|
return sv ? SvUV (sv) : fallback; |
147 |
|
|
} |
148 |
|
|
|
149 |
|
|
noinline NV |
150 |
|
|
random_map_params::get_nv (const char *option, NV fallback) const |
151 |
|
|
{ |
152 |
|
|
SV *sv = get_sv (option); |
153 |
|
|
return sv ? SvNV (sv) : fallback; |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
noinline void |
157 |
|
|
random_map_params::set (const char *option, SV *value) const |
158 |
|
|
{ |
159 |
|
|
int len = strlen (option); |
160 |
|
|
|
161 |
|
|
if (value) |
162 |
|
|
hv_store (hv, option, len, value, 0); |
163 |
|
|
else |
164 |
|
|
hv_delete (hv, option, len, G_DISCARD); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
noinline void |
168 |
|
|
random_map_params::set (const char *option, const_utf8_string value) const |
169 |
|
|
{ |
170 |
|
|
set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
void |
174 |
|
|
random_map_params::set (const char *option, IV value) const |
175 |
|
|
{ |
176 |
|
|
set (option, newSViv (value)); |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
void |
180 |
|
|
random_map_params::set (const char *option, UV value) const |
181 |
|
|
{ |
182 |
|
|
set (option, newSVuv (value)); |
183 |
|
|
} |
184 |
|
|
|
185 |
|
|
void |
186 |
|
|
random_map_params::set (const char *option, NV value) const |
187 |
|
|
{ |
188 |
|
|
set (option, newSVnv (value)); |
189 |
|
|
} |
190 |
|
|
|
191 |
root |
1.43 |
bool |
192 |
|
|
maptile::generate_random_map (random_map_params *RP) |
193 |
|
|
{ |
194 |
|
|
RP->Xsize = RP->xsize; |
195 |
|
|
RP->Ysize = RP->ysize; |
196 |
|
|
|
197 |
|
|
/* pick a random seed, or use the one from the input file */ |
198 |
|
|
RP->random_seed = RP->random_seed |
199 |
|
|
? RP->random_seed + RP->dungeon_level |
200 |
root |
1.66 |
: pticks; |
201 |
root |
1.43 |
|
202 |
|
|
// we run "single-threaded" |
203 |
|
|
rmg_rndm.seed (RP->random_seed); |
204 |
|
|
|
205 |
root |
1.55 |
shstr buf = RP->as_shstr (); |
206 |
root |
1.43 |
|
207 |
|
|
if (RP->difficulty == 0) |
208 |
|
|
{ |
209 |
|
|
RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ |
210 |
|
|
|
211 |
|
|
if (RP->difficulty_increase > 0.001) |
212 |
|
|
RP->difficulty = (int) ((float) RP->dungeon_level * RP->difficulty_increase); |
213 |
|
|
|
214 |
|
|
if (RP->difficulty < 1) |
215 |
|
|
RP->difficulty = 1; |
216 |
|
|
} |
217 |
|
|
else |
218 |
|
|
RP->difficulty_given = 1; |
219 |
|
|
|
220 |
|
|
if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
221 |
|
|
RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
222 |
|
|
|
223 |
|
|
if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
224 |
|
|
RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
225 |
|
|
|
226 |
root |
1.58 |
min_it (RP->Xsize, MAX_RANDOM_MAP_SIZE); |
227 |
|
|
min_it (RP->Ysize, MAX_RANDOM_MAP_SIZE); |
228 |
|
|
|
229 |
root |
1.43 |
if (RP->symmetry == SYMMETRY_RANDOM) |
230 |
|
|
RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; |
231 |
|
|
else |
232 |
|
|
RP->symmetry_used = RP->symmetry; |
233 |
|
|
|
234 |
|
|
if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) |
235 |
|
|
RP->Ysize = RP->Ysize / 2 + 1; |
236 |
|
|
|
237 |
|
|
if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
238 |
|
|
RP->Xsize = RP->Xsize / 2 + 1; |
239 |
|
|
|
240 |
|
|
if (RP->expand2x > 0) |
241 |
|
|
{ |
242 |
|
|
RP->Xsize /= 2; |
243 |
|
|
RP->Ysize /= 2; |
244 |
|
|
} |
245 |
|
|
|
246 |
root |
1.62 |
const char *layoutstyle = RP->get_str ("layoutstyle", ""); |
247 |
|
|
|
248 |
|
|
if (strstr (layoutstyle, "onion")) |
249 |
root |
1.43 |
RP->map_layout_style = LAYOUT_ONION; |
250 |
root |
1.62 |
else if (strstr (layoutstyle, "maze")) |
251 |
root |
1.43 |
RP->map_layout_style = LAYOUT_MAZE; |
252 |
root |
1.62 |
else if (strstr (layoutstyle, "spiral")) |
253 |
root |
1.43 |
RP->map_layout_style = LAYOUT_SPIRAL; |
254 |
root |
1.62 |
else if (strstr (layoutstyle, "rogue")) |
255 |
root |
1.43 |
RP->map_layout_style = LAYOUT_ROGUELIKE; |
256 |
root |
1.62 |
else if (strstr (layoutstyle, "snake")) |
257 |
root |
1.43 |
RP->map_layout_style = LAYOUT_SNAKE; |
258 |
root |
1.62 |
else if (strstr (layoutstyle, "squarespiral")) |
259 |
root |
1.43 |
RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; |
260 |
root |
1.62 |
else if (strstr (layoutstyle, "cave")) |
261 |
root |
1.57 |
RP->map_layout_style = LAYOUT_CAVE; |
262 |
root |
1.64 |
else if (strstr (layoutstyle, "multiple")) |
263 |
|
|
RP->map_layout_style = LAYOUT_MULTIPLE; |
264 |
root |
1.43 |
else |
265 |
root |
1.59 |
RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ |
266 |
root |
1.43 |
|
267 |
root |
1.61 |
layout maze (RP->Xsize, RP->Ysize); |
268 |
|
|
maze.generate (RP); |
269 |
root |
1.64 |
maze.rotate (rmg_rndm (4)); |
270 |
|
|
maze.symmetrize (RP->symmetry_used); |
271 |
|
|
|
272 |
|
|
if (RP->expand2x) |
273 |
|
|
maze.expand2x (); |
274 |
|
|
|
275 |
root |
1.65 |
#if 0 |
276 |
root |
1.64 |
maze.print ();//D |
277 |
|
|
#endif |
278 |
root |
1.43 |
|
279 |
|
|
/* increment these for the current map */ |
280 |
|
|
++RP->dungeon_level; |
281 |
|
|
|
282 |
root |
1.61 |
// need to patch RP becasue following code doesn't use the layout object |
283 |
|
|
RP->Xsize = maze.w; |
284 |
|
|
RP->Ysize = maze.h; |
285 |
root |
1.43 |
|
286 |
|
|
/* allocate the map and set the floor */ |
287 |
root |
1.61 |
make_map_floor (maze, RP->get_str ("floorstyle", ""), RP); |
288 |
root |
1.43 |
|
289 |
|
|
/* set region */ |
290 |
root |
1.63 |
default_region = region::find (RP->get_str ("region", 0)); |
291 |
root |
1.43 |
|
292 |
|
|
CEDE; |
293 |
|
|
|
294 |
root |
1.61 |
place_specials_in_map (this, maze, RP); |
295 |
root |
1.43 |
|
296 |
|
|
CEDE; |
297 |
|
|
|
298 |
root |
1.62 |
const char *wallstyle = RP->get_str ("wallstyle", ""); |
299 |
root |
1.51 |
|
300 |
root |
1.43 |
/* create walls unless the wallstyle is "none" */ |
301 |
root |
1.51 |
if (strcmp (wallstyle, "none")) |
302 |
root |
1.43 |
{ |
303 |
root |
1.61 |
make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); |
304 |
root |
1.43 |
|
305 |
root |
1.62 |
const char *doorstyle = RP->get_str ("doorstyle", ""); |
306 |
|
|
|
307 |
root |
1.43 |
/* place doors unless doorstyle or wallstyle is "none" */ |
308 |
root |
1.62 |
if (strcmp (doorstyle, "none")) |
309 |
|
|
put_doors (this, maze, doorstyle, RP); |
310 |
root |
1.43 |
} |
311 |
|
|
|
312 |
|
|
CEDE; |
313 |
|
|
|
314 |
root |
1.51 |
const char *exitstyle = RP->get_str ("exitstyle", ""); |
315 |
|
|
|
316 |
root |
1.43 |
/* create exits unless the exitstyle is "none" */ |
317 |
root |
1.51 |
if (strcmp (exitstyle, "none")) |
318 |
root |
1.61 |
place_exits (this, maze, exitstyle, RP->orientation, RP); |
319 |
root |
1.43 |
|
320 |
|
|
CEDE; |
321 |
|
|
|
322 |
root |
1.62 |
const char *monsterstyle = RP->get_str ("monsterstyle", ""); |
323 |
|
|
|
324 |
root |
1.43 |
/* create monsters unless the monsterstyle is "none" */ |
325 |
root |
1.62 |
if (strcmp (monsterstyle, "none")) |
326 |
|
|
place_monsters (this, monsterstyle, RP->difficulty, RP); |
327 |
root |
1.43 |
|
328 |
|
|
CEDE; |
329 |
|
|
|
330 |
|
|
/* treasures needs to have a proper difficulty set for the map. */ |
331 |
|
|
difficulty = estimate_difficulty (); |
332 |
|
|
|
333 |
|
|
CEDE; |
334 |
|
|
|
335 |
root |
1.55 |
const char *treasurestyle = RP->get_str ("treasurestyle", ""); |
336 |
|
|
|
337 |
root |
1.43 |
/* create treasure unless the treasurestyle is "none" */ |
338 |
root |
1.61 |
place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); |
339 |
root |
1.43 |
|
340 |
|
|
CEDE; |
341 |
|
|
|
342 |
root |
1.55 |
const char *decorstyle = RP->get_str ("treasurestyle", ""); |
343 |
|
|
|
344 |
root |
1.43 |
/* create decor unless the decorstyle is "none" */ |
345 |
root |
1.55 |
if (strcmp (decorstyle, "none")) |
346 |
root |
1.61 |
put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); |
347 |
root |
1.43 |
|
348 |
|
|
CEDE; |
349 |
|
|
|
350 |
|
|
/* generate treasures, etc. */ |
351 |
|
|
fix_auto_apply (); |
352 |
|
|
|
353 |
|
|
CEDE; |
354 |
|
|
|
355 |
root |
1.61 |
unblock_exits (this, maze, RP); |
356 |
root |
1.43 |
|
357 |
root |
1.51 |
msg = buf; |
358 |
root |
1.43 |
in_memory = MAP_ACTIVE; |
359 |
|
|
|
360 |
|
|
CEDE; |
361 |
|
|
|
362 |
|
|
return 1; |
363 |
|
|
} |
364 |
|
|
|