1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2017,2018 Marc Alexander Lehmann / 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 |
6 |
* Copyright (©) 2001 Mark Wedel & Crossfire Development Team |
7 |
* Copyright (©) 1992 Frank Tore Johansen |
8 |
* |
9 |
* Deliantra is free software: you can redistribute it and/or modify it under |
10 |
* the terms of the Affero GNU General Public License as published by the |
11 |
* Free Software Foundation, either version 3 of the License, or (at your |
12 |
* option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the Affero GNU General Public License |
20 |
* and the GNU General Public License along with this program. If not, see |
21 |
* <http://www.gnu.org/licenses/>. |
22 |
* |
23 |
* The authors can be reached via e-mail to <support@deliantra.net> |
24 |
*/ |
25 |
|
26 |
#include <time.h> |
27 |
#include <stdio.h> |
28 |
#include <global.h> |
29 |
#include <rmg.h> |
30 |
#include <rproto.h> |
31 |
#include <sproto.h> |
32 |
|
33 |
#define CEDE coroapi::cede_to_tick () |
34 |
|
35 |
random_map_params::random_map_params () |
36 |
{ |
37 |
hv = newHV (); |
38 |
} |
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 |
|
114 |
ecb_noinline SV * |
115 |
random_map_params::opt_sv (const char *option) const |
116 |
{ |
117 |
SV **he = hv_fetch (hv, option, strlen (option), 0); |
118 |
|
119 |
return he ? *he : 0; |
120 |
} |
121 |
|
122 |
ecb_noinline const_utf8_string |
123 |
random_map_params::get_str (const char *option, const_utf8_string fallback) const |
124 |
{ |
125 |
SV *sv = opt_sv (option); |
126 |
return sv ? cfSvPVutf8_nolen (sv) : fallback; |
127 |
} |
128 |
|
129 |
ecb_noinline IV |
130 |
random_map_params::get_iv (const char *option, IV fallback) const |
131 |
{ |
132 |
SV *sv = opt_sv (option); |
133 |
return sv ? SvIV (sv) : fallback; |
134 |
} |
135 |
|
136 |
ecb_noinline UV |
137 |
random_map_params::get_uv (const char *option, UV fallback) const |
138 |
{ |
139 |
SV *sv = opt_sv (option); |
140 |
return sv ? SvUV (sv) : fallback; |
141 |
} |
142 |
|
143 |
ecb_noinline NV |
144 |
random_map_params::get_nv (const char *option, NV fallback) const |
145 |
{ |
146 |
SV *sv = opt_sv (option); |
147 |
return sv ? SvNV (sv) : fallback; |
148 |
} |
149 |
|
150 |
ecb_noinline void |
151 |
random_map_params::set (const char *option, SV *value) const |
152 |
{ |
153 |
int len = strlen (option); |
154 |
|
155 |
if (value) |
156 |
hv_store (hv, option, len, value, 0); |
157 |
else |
158 |
hv_delete (hv, option, len, G_DISCARD); |
159 |
} |
160 |
|
161 |
ecb_noinline void |
162 |
random_map_params::set (const char *option, const_utf8_string value) const |
163 |
{ |
164 |
set (option, value && *value ? newSVpvn_utf8 (value, strlen (value), 1) : 0); |
165 |
} |
166 |
|
167 |
void |
168 |
random_map_params::set (const char *option, IV value) const |
169 |
{ |
170 |
set (option, newSViv (value)); |
171 |
} |
172 |
|
173 |
void |
174 |
random_map_params::set (const char *option, UV value) const |
175 |
{ |
176 |
set (option, newSVuv (value)); |
177 |
} |
178 |
|
179 |
void |
180 |
random_map_params::set (const char *option, NV value) const |
181 |
{ |
182 |
set (option, newSVnv (value)); |
183 |
} |
184 |
|
185 |
bool |
186 |
maptile::generate_random_map (random_map_params *RP) |
187 |
{ |
188 |
RP->Xsize = RP->xsize; |
189 |
RP->Ysize = RP->ysize; |
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 |
|
196 |
/* pick a random seed, or use the one from the input file */ |
197 |
random_seed = random_seed |
198 |
? random_seed + RP->dungeon_level |
199 |
: server_tick; |
200 |
|
201 |
// we run "single-threaded" |
202 |
rmg_rndm.seed (random_seed); |
203 |
|
204 |
shstr buf = RP->as_shstr (); |
205 |
|
206 |
if (RP->difficulty == 0) |
207 |
{ |
208 |
RP->difficulty = RP->dungeon_level; /* use this instead of a map difficulty */ |
209 |
|
210 |
if (RP->difficulty_increase > 0.001f) |
211 |
RP->difficulty = RP->dungeon_level * RP->difficulty_increase; |
212 |
|
213 |
if (RP->difficulty < 1) |
214 |
RP->difficulty = 1; |
215 |
} |
216 |
else |
217 |
RP->difficulty_given = 1; |
218 |
|
219 |
if (RP->Xsize < MIN_RANDOM_MAP_SIZE) |
220 |
RP->Xsize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
221 |
|
222 |
if (RP->Ysize < MIN_RANDOM_MAP_SIZE) |
223 |
RP->Ysize = MIN_RANDOM_MAP_SIZE + rmg_rndm (25) + 5; |
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 |
|
230 |
if (symmetry == SYMMETRY_RANDOM) |
231 |
RP->symmetry_used = rmg_rndm (SYMMETRY_XY) + 1; |
232 |
else |
233 |
RP->symmetry_used = symmetry; |
234 |
|
235 |
if (RP->symmetry_used == SYMMETRY_Y || RP->symmetry_used == SYMMETRY_XY) |
236 |
RP->Ysize = RP->Ysize / 2 + 1; |
237 |
|
238 |
if (RP->symmetry_used == SYMMETRY_X || RP->symmetry_used == SYMMETRY_XY) |
239 |
RP->Xsize = RP->Xsize / 2 + 1; |
240 |
|
241 |
if (expand2x) |
242 |
{ |
243 |
RP->Xsize /= 2; |
244 |
RP->Ysize /= 2; |
245 |
} |
246 |
|
247 |
const char *layoutstyle = RP->get_str ("layoutstyle", ""); |
248 |
|
249 |
if (strstr (layoutstyle, "onion")) |
250 |
RP->map_layout_style = LAYOUT_ONION; |
251 |
else if (strstr (layoutstyle, "maze")) |
252 |
RP->map_layout_style = LAYOUT_MAZE; |
253 |
else if (strstr (layoutstyle, "spiral")) |
254 |
RP->map_layout_style = LAYOUT_SPIRAL; |
255 |
else if (strstr (layoutstyle, "rogue")) |
256 |
RP->map_layout_style = LAYOUT_ROGUELIKE; |
257 |
else if (strstr (layoutstyle, "snake")) |
258 |
RP->map_layout_style = LAYOUT_SNAKE; |
259 |
else if (strstr (layoutstyle, "squarespiral")) |
260 |
RP->map_layout_style = LAYOUT_SQUARE_SPIRAL; |
261 |
else if (strstr (layoutstyle, "cave")) |
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 |
268 |
RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 1) + 1; /* No style found - choose one randomly */ |
269 |
|
270 |
layout maze (RP->Xsize, RP->Ysize); |
271 |
maze.generate (RP); |
272 |
|
273 |
if (RP->get_iv ("rotate", 1)) |
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 |
283 |
#endif |
284 |
|
285 |
/* increment these for the current map */ |
286 |
++RP->dungeon_level; |
287 |
|
288 |
// need to patch RP becasue following code doesn't use the layout object |
289 |
RP->Xsize = maze.w; |
290 |
RP->Ysize = maze.h; |
291 |
|
292 |
/* allocate the map and set the floor */ |
293 |
make_map_floor (maze, RP->get_str ("floorstyle", ""), RP); |
294 |
|
295 |
/* set region */ |
296 |
default_region = region::find (RP->get_str ("region", 0)); |
297 |
|
298 |
CEDE; |
299 |
|
300 |
place_specials_in_map (this, maze, RP); |
301 |
|
302 |
CEDE; |
303 |
|
304 |
const char *wallstyle = RP->get_str ("wallstyle", ""); |
305 |
|
306 |
/* create walls unless the wallstyle is "none" */ |
307 |
if (strcmp (wallstyle, "none")) |
308 |
{ |
309 |
make_map_walls (this, maze, wallstyle, RP->get_str ("miningstyle", ""), RP); |
310 |
|
311 |
const char *doorstyle = RP->get_str ("doorstyle", ""); |
312 |
|
313 |
/* place doors unless doorstyle or wallstyle is "none" */ |
314 |
if (strcmp (doorstyle, "none")) |
315 |
put_doors (this, maze, doorstyle, RP); |
316 |
} |
317 |
|
318 |
CEDE; |
319 |
|
320 |
const char *exitstyle = RP->get_str ("exitstyle", ""); |
321 |
|
322 |
/* create exits unless the exitstyle is "none" */ |
323 |
if (strcmp (exitstyle, "none")) |
324 |
place_exits (this, maze, exitstyle, RP->get_iv ("orientation", 0), RP); |
325 |
|
326 |
CEDE; |
327 |
|
328 |
const char *monsterstyle = RP->get_str ("monsterstyle", ""); |
329 |
|
330 |
/* create monsters unless the monsterstyle is "none" */ |
331 |
if (strcmp (monsterstyle, "none")) |
332 |
place_monsters (this, monsterstyle, RP->difficulty, RP); |
333 |
|
334 |
CEDE; |
335 |
|
336 |
/* treasures needs to have a proper difficulty set for the map. */ |
337 |
difficulty = estimate_difficulty (); |
338 |
|
339 |
CEDE; |
340 |
|
341 |
const char *treasurestyle = RP->get_str ("treasurestyle", ""); |
342 |
|
343 |
/* create treasure unless the treasurestyle is "none" */ |
344 |
place_treasure (this, maze, treasurestyle, RP->get_iv ("treasureoptions"), RP); |
345 |
|
346 |
CEDE; |
347 |
|
348 |
const char *decorstyle = RP->get_str ("decorstyle", ""); |
349 |
|
350 |
/* create decor unless the decorstyle is "none" */ |
351 |
if (strcmp (decorstyle, "none")) |
352 |
put_decor (this, maze, decorstyle, RP->get_iv ("decoroptions"), RP); |
353 |
|
354 |
CEDE; |
355 |
|
356 |
/* generate treasures, etc. */ |
357 |
fix_auto_apply (); |
358 |
|
359 |
CEDE; |
360 |
|
361 |
unblock_exits (this, maze); |
362 |
|
363 |
msg = buf; |
364 |
state = MAP_INACTIVE; // this is probably a lie, but currently works with ext/map-random.ext |
365 |
|
366 |
CEDE; |
367 |
|
368 |
return 1; |
369 |
} |
370 |
|