1 |
elmex |
1.1 |
/* |
2 |
root |
1.24 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
pippijn |
1.16 |
* |
4 |
root |
1.25 |
* Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.23 |
* Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team |
6 |
|
|
* Copyright (©) 1992,2007 Frank Tore Johansen |
7 |
pippijn |
1.16 |
* |
8 |
root |
1.24 |
* Deliantra is free software: you can redistribute it and/or modify |
9 |
pippijn |
1.16 |
* it under the terms of the GNU General Public License as published by |
10 |
root |
1.23 |
* the Free Software Foundation, either version 3 of the License, or |
11 |
pippijn |
1.16 |
* (at your option) any later version. |
12 |
|
|
* |
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.23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
pippijn |
1.16 |
* GNU General Public License for more details. |
17 |
|
|
* |
18 |
|
|
* You should have received a copy of the GNU General Public License |
19 |
root |
1.23 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 |
|
|
* |
21 |
root |
1.24 |
* The authors can be reached via e-mail to <support@deliantra.net> |
22 |
pippijn |
1.16 |
*/ |
23 |
elmex |
1.1 |
|
24 |
|
|
#include <global.h> |
25 |
|
|
#include <random_map.h> |
26 |
|
|
#include <rproto.h> |
27 |
|
|
|
28 |
|
|
/* Put in the walls and autojoin them. */ |
29 |
|
|
|
30 |
|
|
/* given a layout and a coordinate, tell me which squares up/down/right/left |
31 |
root |
1.2 |
are occupied. */ |
32 |
root |
1.4 |
int |
33 |
root |
1.13 |
surround_flag (char **layout, int i, int j, random_map_params *RP) |
34 |
root |
1.4 |
{ |
35 |
elmex |
1.1 |
/* 1 = wall to left, |
36 |
root |
1.4 |
2 = wall to right, |
37 |
|
|
4 = wall above |
38 |
|
|
8 = wall below */ |
39 |
elmex |
1.1 |
int surround_index = 0; |
40 |
root |
1.4 |
|
41 |
|
|
if ((i > 0) && layout[i - 1][j] != 0) |
42 |
|
|
surround_index |= 1; |
43 |
|
|
if ((i < RP->Xsize - 1) && layout[i + 1][j] != 0) |
44 |
|
|
surround_index |= 2; |
45 |
|
|
if ((j > 0) && layout[i][j - 1] != 0) |
46 |
|
|
surround_index |= 4; |
47 |
|
|
if ((j < RP->Ysize - 1) && layout[i][j + 1] != 0) |
48 |
|
|
surround_index |= 8; |
49 |
elmex |
1.1 |
return surround_index; |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
|
53 |
|
|
/* like surround_flag, but only walls count. |
54 |
root |
1.2 |
*/ |
55 |
elmex |
1.1 |
|
56 |
root |
1.4 |
int |
57 |
root |
1.13 |
surround_flag2 (char **layout, int i, int j, random_map_params *RP) |
58 |
root |
1.4 |
{ |
59 |
elmex |
1.1 |
/* 1 = wall to left, |
60 |
root |
1.4 |
2 = wall to right, |
61 |
|
|
4 = wall above |
62 |
|
|
8 = wall below */ |
63 |
elmex |
1.1 |
int surround_index = 0; |
64 |
root |
1.4 |
|
65 |
|
|
if ((i > 0) && layout[i - 1][j] == '#') |
66 |
|
|
surround_index |= 1; |
67 |
|
|
if ((i < RP->Xsize - 1) && layout[i + 1][j] == '#') |
68 |
|
|
surround_index |= 2; |
69 |
|
|
if ((j > 0) && layout[i][j - 1] == '#') |
70 |
|
|
surround_index |= 4; |
71 |
|
|
if ((j < RP->Ysize - 1) && layout[i][j + 1] == '#') |
72 |
|
|
surround_index |= 8; |
73 |
elmex |
1.1 |
return surround_index; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/* like surround_flag, except it checks a map, not a layout. |
78 |
|
|
* Since this is part of the random map code, presumption |
79 |
|
|
* is that this is not a tiled map. |
80 |
|
|
* What is considered blocking and not is somewhat hard coded. |
81 |
|
|
*/ |
82 |
root |
1.4 |
int |
83 |
root |
1.13 |
surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP) |
84 |
root |
1.4 |
{ |
85 |
|
|
/* |
86 |
|
|
* 1 = blocked to left, |
87 |
|
|
* 2 = blocked to right, |
88 |
|
|
* 4 = blocked above |
89 |
|
|
* 8 = blocked below |
90 |
|
|
*/ |
91 |
|
|
|
92 |
|
|
int surround_index = 0; |
93 |
elmex |
1.1 |
|
94 |
elmex |
1.20 |
// don't forget to update the mapspace! |
95 |
root |
1.21 |
if (i > 0) map->at (i - 1, j ).update (); |
96 |
|
|
if (i < RP->Xsize - 1) map->at (i + 1, j ).update (); |
97 |
|
|
if (j > 0) map->at (i , j - 1).update (); |
98 |
|
|
if (j < RP->Ysize - 1) map->at (i , j + 1).update (); |
99 |
elmex |
1.20 |
|
100 |
|
|
if ((i > 0) && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) |
101 |
root |
1.4 |
surround_index |= 1; |
102 |
elmex |
1.20 |
if ((i < RP->Xsize - 1) && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK)) |
103 |
root |
1.4 |
surround_index |= 2; |
104 |
elmex |
1.20 |
if ((j > 0) && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK)) |
105 |
root |
1.4 |
surround_index |= 4; |
106 |
elmex |
1.20 |
if ((j < RP->Ysize - 1) && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) |
107 |
root |
1.4 |
surround_index |= 8; |
108 |
|
|
|
109 |
|
|
return surround_index; |
110 |
elmex |
1.1 |
} |
111 |
|
|
|
112 |
|
|
/* like surround_flag2, except it checks a map, not a layout. */ |
113 |
|
|
|
114 |
root |
1.4 |
int |
115 |
root |
1.13 |
surround_flag4 (maptile *map, int i, int j, random_map_params *RP) |
116 |
root |
1.4 |
{ |
117 |
elmex |
1.1 |
/* 1 = blocked to left, |
118 |
root |
1.4 |
2 = blocked to right, |
119 |
|
|
4 = blocked above |
120 |
|
|
8 = blocked below */ |
121 |
elmex |
1.1 |
int surround_index = 0; |
122 |
|
|
|
123 |
root |
1.4 |
if ((i > 0) && wall_blocked (map, i - 1, j)) |
124 |
|
|
surround_index |= 1; |
125 |
|
|
if ((i < RP->Xsize - 1) && wall_blocked (map, i + 1, j)) |
126 |
|
|
surround_index |= 2; |
127 |
|
|
if ((j > 0) && wall_blocked (map, i, j - 1)) |
128 |
|
|
surround_index |= 4; |
129 |
|
|
if ((j < RP->Ysize - 1) && wall_blocked (map, i, j + 1)) |
130 |
|
|
surround_index |= 8; |
131 |
elmex |
1.1 |
|
132 |
|
|
return surround_index; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/* takes a map and a layout, and puts walls in the map (picked from |
136 |
root |
1.2 |
w_style) at '#' marks. */ |
137 |
elmex |
1.1 |
|
138 |
root |
1.4 |
void |
139 |
root |
1.13 |
make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP) |
140 |
root |
1.4 |
{ |
141 |
root |
1.14 |
char styledirname[1024]; |
142 |
|
|
char stylefilepath[1024]; |
143 |
root |
1.7 |
maptile *style_map = 0; |
144 |
root |
1.4 |
object *the_wall; |
145 |
|
|
|
146 |
|
|
/* get the style map */ |
147 |
|
|
if (!strcmp (w_style, "none")) |
148 |
|
|
return; |
149 |
|
|
sprintf (styledirname, "%s", "/styles/wallstyles"); |
150 |
|
|
sprintf (stylefilepath, "%s/%s", styledirname, w_style); |
151 |
|
|
style_map = find_style (styledirname, w_style, -1); |
152 |
root |
1.17 |
if (!style_map) |
153 |
root |
1.4 |
return; |
154 |
|
|
|
155 |
|
|
/* fill up the map with the given floor style */ |
156 |
root |
1.26 |
if ((the_wall = style_map->pick_random_object (rmg_rndm))) |
157 |
root |
1.4 |
{ |
158 |
|
|
int i, j; |
159 |
|
|
char *cp; |
160 |
|
|
int joinedwalls = 0; |
161 |
|
|
object *thiswall; |
162 |
|
|
|
163 |
root |
1.22 |
sprintf (RP->wall_name, "%s", &the_wall->arch->archname); |
164 |
root |
1.4 |
if ((cp = strchr (RP->wall_name, '_')) != NULL) |
165 |
|
|
{ |
166 |
|
|
*cp = 0; |
167 |
|
|
joinedwalls = 1; |
168 |
root |
1.2 |
} |
169 |
|
|
|
170 |
root |
1.4 |
for (i = 0; i < RP->Xsize; i++) |
171 |
|
|
for (j = 0; j < RP->Ysize; j++) |
172 |
|
|
{ |
173 |
|
|
if (layout[i][j] == '#') |
174 |
|
|
{ |
175 |
|
|
if (joinedwalls) |
176 |
|
|
thiswall = pick_joined_wall (the_wall, layout, i, j, RP); |
177 |
|
|
else |
178 |
|
|
thiswall = arch_to_object (the_wall->arch); |
179 |
|
|
thiswall->x = i; |
180 |
|
|
thiswall->y = j; |
181 |
|
|
thiswall->move_block = MOVE_ALL; |
182 |
|
|
insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON); |
183 |
|
|
} |
184 |
|
|
} |
185 |
elmex |
1.1 |
} |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
|
189 |
|
|
/* picks the right wall type for this square, to make it look nice, |
190 |
root |
1.2 |
and have everything nicely joined. It uses the layout. */ |
191 |
elmex |
1.1 |
|
192 |
root |
1.4 |
object * |
193 |
root |
1.13 |
pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP) |
194 |
root |
1.4 |
{ |
195 |
elmex |
1.1 |
/* 1 = wall to left, |
196 |
root |
1.4 |
2 = wall to right, |
197 |
|
|
4 = wall above |
198 |
|
|
8 = wall below */ |
199 |
|
|
int surround_index = 0; |
200 |
elmex |
1.1 |
int l; |
201 |
root |
1.14 |
char wall_name[1024]; |
202 |
root |
1.4 |
archetype *wall_arch = 0; |
203 |
elmex |
1.1 |
|
204 |
root |
1.22 |
assign (wall_name, the_wall->arch->archname); |
205 |
elmex |
1.1 |
|
206 |
|
|
/* conventionally, walls are named like this: |
207 |
root |
1.4 |
wallname_wallcode, where wallcode indicates |
208 |
|
|
a joinedness, and wallname is the wall. |
209 |
|
|
this code depends on the convention for |
210 |
|
|
finding the right wall. */ |
211 |
elmex |
1.1 |
|
212 |
|
|
/* extract the wall name, which is the text up to the leading _ */ |
213 |
root |
1.4 |
for (l = 0; l < 64; l++) |
214 |
|
|
{ |
215 |
|
|
if (wall_name[l] == '_') |
216 |
|
|
{ |
217 |
|
|
wall_name[l] = 0; |
218 |
|
|
break; |
219 |
|
|
} |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
surround_index = surround_flag2 (layout, i, j, RP); |
223 |
|
|
|
224 |
|
|
switch (surround_index) |
225 |
|
|
{ |
226 |
root |
1.13 |
case 0: |
227 |
|
|
strcat (wall_name, "_0"); |
228 |
|
|
break; |
229 |
|
|
case 1: |
230 |
|
|
strcat (wall_name, "_1_3"); |
231 |
|
|
break; |
232 |
|
|
case 2: |
233 |
|
|
strcat (wall_name, "_1_4"); |
234 |
|
|
break; |
235 |
|
|
case 3: |
236 |
|
|
strcat (wall_name, "_2_1_2"); |
237 |
|
|
break; |
238 |
|
|
case 4: |
239 |
|
|
strcat (wall_name, "_1_2"); |
240 |
|
|
break; |
241 |
|
|
case 5: |
242 |
|
|
strcat (wall_name, "_2_2_4"); |
243 |
|
|
break; |
244 |
|
|
case 6: |
245 |
|
|
strcat (wall_name, "_2_2_1"); |
246 |
|
|
break; |
247 |
|
|
case 7: |
248 |
|
|
strcat (wall_name, "_3_1"); |
249 |
|
|
break; |
250 |
|
|
case 8: |
251 |
|
|
strcat (wall_name, "_1_1"); |
252 |
|
|
break; |
253 |
|
|
case 9: |
254 |
|
|
strcat (wall_name, "_2_2_3"); |
255 |
|
|
break; |
256 |
|
|
case 10: |
257 |
|
|
strcat (wall_name, "_2_2_2"); |
258 |
|
|
break; |
259 |
|
|
case 11: |
260 |
|
|
strcat (wall_name, "_3_3"); |
261 |
|
|
break; |
262 |
|
|
case 12: |
263 |
|
|
strcat (wall_name, "_2_1_1"); |
264 |
|
|
break; |
265 |
|
|
case 13: |
266 |
|
|
strcat (wall_name, "_3_4"); |
267 |
|
|
break; |
268 |
|
|
case 14: |
269 |
|
|
strcat (wall_name, "_3_2"); |
270 |
|
|
break; |
271 |
|
|
case 15: |
272 |
|
|
strcat (wall_name, "_4"); |
273 |
|
|
break; |
274 |
root |
1.4 |
} |
275 |
root |
1.5 |
wall_arch = archetype::find (wall_name); |
276 |
elmex |
1.1 |
|
277 |
root |
1.13 |
return wall_arch ? arch_to_object (wall_arch) : arch_to_object (the_wall->arch); |
278 |
elmex |
1.1 |
} |
279 |
root |
1.4 |
|
280 |
elmex |
1.1 |
|
281 |
|
|
/* this takes a map, and changes an existing wall to match what's blocked |
282 |
|
|
* around it, counting only doors and walls as blocked. If insert_flag is |
283 |
|
|
* 1, it will go ahead and insert the wall into the map. If not, it |
284 |
|
|
* will only return the wall which would belong there, and doesn't |
285 |
|
|
* remove anything. It depends on the |
286 |
|
|
* global, previously-set variable, "wall_name" |
287 |
|
|
*/ |
288 |
|
|
|
289 |
root |
1.4 |
object * |
290 |
root |
1.13 |
retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP) |
291 |
root |
1.4 |
{ |
292 |
|
|
/* 1 = wall to left, |
293 |
|
|
* 2 = wall to right, |
294 |
|
|
* 4 = wall above |
295 |
|
|
* 8 = wall below |
296 |
|
|
*/ |
297 |
|
|
int surround_index = 0; |
298 |
|
|
int l; |
299 |
|
|
object *the_wall = 0; |
300 |
|
|
object *new_wall = 0; |
301 |
|
|
archetype *wall_arch = 0; |
302 |
|
|
|
303 |
|
|
/* first find the wall */ |
304 |
root |
1.10 |
for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above) |
305 |
root |
1.4 |
if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER) |
306 |
|
|
break; |
307 |
|
|
|
308 |
|
|
|
309 |
|
|
/* if what we found is a door, don't remove it, set the_wall to NULL to |
310 |
|
|
* signal that later. |
311 |
|
|
*/ |
312 |
|
|
if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) |
313 |
|
|
{ |
314 |
|
|
the_wall = NULL; |
315 |
|
|
/* if we're not supposed to insert a new wall where there wasn't one, |
316 |
|
|
* we've gotta leave. |
317 |
|
|
*/ |
318 |
|
|
if (insert_flag == 0) |
319 |
|
|
return 0; |
320 |
elmex |
1.1 |
} |
321 |
root |
1.4 |
else if (the_wall == NULL) |
322 |
|
|
return NULL; |
323 |
elmex |
1.1 |
|
324 |
root |
1.4 |
/* canonicalize the wall name */ |
325 |
|
|
for (l = 0; l < 64; l++) |
326 |
|
|
{ |
327 |
|
|
if (RP->wall_name[l] == '_') |
328 |
|
|
{ |
329 |
|
|
RP->wall_name[l] = 0; |
330 |
|
|
break; |
331 |
root |
1.2 |
} |
332 |
elmex |
1.1 |
} |
333 |
|
|
|
334 |
root |
1.4 |
surround_index = surround_flag4 (the_map, i, j, RP); |
335 |
|
|
/* This would be a lot cleaner to just us a lookup table, |
336 |
|
|
* eg, wall_suffix[surround_index] |
337 |
|
|
*/ |
338 |
|
|
switch (surround_index) |
339 |
|
|
{ |
340 |
root |
1.13 |
case 0: |
341 |
|
|
strcat (RP->wall_name, "_0"); |
342 |
|
|
break; |
343 |
|
|
case 1: |
344 |
|
|
strcat (RP->wall_name, "_1_3"); |
345 |
|
|
break; |
346 |
|
|
case 2: |
347 |
|
|
strcat (RP->wall_name, "_1_4"); |
348 |
|
|
break; |
349 |
|
|
case 3: |
350 |
|
|
strcat (RP->wall_name, "_2_1_2"); |
351 |
|
|
break; |
352 |
|
|
case 4: |
353 |
|
|
strcat (RP->wall_name, "_1_2"); |
354 |
|
|
break; |
355 |
|
|
case 5: |
356 |
|
|
strcat (RP->wall_name, "_2_2_4"); |
357 |
|
|
break; |
358 |
|
|
case 6: |
359 |
|
|
strcat (RP->wall_name, "_2_2_1"); |
360 |
|
|
break; |
361 |
|
|
case 7: |
362 |
|
|
strcat (RP->wall_name, "_3_1"); |
363 |
|
|
break; |
364 |
|
|
case 8: |
365 |
|
|
strcat (RP->wall_name, "_1_1"); |
366 |
|
|
break; |
367 |
|
|
case 9: |
368 |
|
|
strcat (RP->wall_name, "_2_2_3"); |
369 |
|
|
break; |
370 |
|
|
case 10: |
371 |
|
|
strcat (RP->wall_name, "_2_2_2"); |
372 |
|
|
break; |
373 |
|
|
case 11: |
374 |
|
|
strcat (RP->wall_name, "_3_3"); |
375 |
|
|
break; |
376 |
|
|
case 12: |
377 |
|
|
strcat (RP->wall_name, "_2_1_1"); |
378 |
|
|
break; |
379 |
|
|
case 13: |
380 |
|
|
strcat (RP->wall_name, "_3_4"); |
381 |
|
|
break; |
382 |
|
|
case 14: |
383 |
|
|
strcat (RP->wall_name, "_3_2"); |
384 |
|
|
break; |
385 |
|
|
case 15: |
386 |
|
|
strcat (RP->wall_name, "_4"); |
387 |
|
|
break; |
388 |
elmex |
1.1 |
} |
389 |
root |
1.11 |
|
390 |
root |
1.5 |
wall_arch = archetype::find (RP->wall_name); |
391 |
root |
1.11 |
|
392 |
|
|
if (!wall_arch) |
393 |
root |
1.4 |
{ |
394 |
|
|
new_wall = arch_to_object (wall_arch); |
395 |
|
|
new_wall->x = i; |
396 |
|
|
new_wall->y = j; |
397 |
root |
1.11 |
|
398 |
root |
1.4 |
if (the_wall && the_wall->map) |
399 |
|
|
{ |
400 |
root |
1.8 |
the_wall->remove (); |
401 |
root |
1.9 |
the_wall->destroy (); |
402 |
root |
1.2 |
} |
403 |
root |
1.11 |
|
404 |
root |
1.4 |
the_wall->move_block = MOVE_ALL; |
405 |
|
|
insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON); |
406 |
elmex |
1.1 |
} |
407 |
root |
1.11 |
|
408 |
root |
1.4 |
return new_wall; |
409 |
elmex |
1.1 |
} |