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