Debian Codegraph @

random_maps/wall.c

raw symbols SCIP
1 /*
2 * Crossfire -- cooperative multi-player graphical RPG and adventure game
3 *
4 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
5 * Copyright (c) 1992 Frank Tore Johansen
6 *
7 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
8 * welcome to redistribute it under certain conditions. For details, please
9 * see COPYING and LICENSE.
10 *
11 * The authors can be reached via e-mail at <[email protected]>.
12 */
13
14 #include "global.h"
15
16 #include <string.h>
17
18 #include "random_map.h"
19 #include "rproto.h"
20 #include <assert.h>
21
22 /**
23 * Given a layout and a coordinate, tell me which squares up/down/right/left
24 * are occupied.
25 * @param layout
26 * @param i
27 * @param j
28 * spot to look.
29 * @param RP
30 * map parameters.
31 * @return
32 * combination of:
33 * - 1 = something on left.
34 * - 2 = something on right.
35 * - 4 = something on above.
36 * - 8 = something on below.
37 * @todo
38 * merge with surround_flag2() and friends, check if such a function doesn't exist in other files.
39 */
40 int surround_flag(char **layout, int i, int j, RMParms *RP)
41 {
42 int surround_index = 0;
43
44 if ((i > 0) && layout[i-1][j] != 0) {
45 surround_index |= 1;
46 }
47 if ((i < RP->Xsize-1) && layout[i+1][j] != 0) {
48 surround_index |= 2;
49 }
50 if ((j > 0) && layout[i][j-1] != 0) {
51 surround_index |= 4;
52 }
53 if ((j < RP->Ysize-1) && layout[i][j+1] != 0) {
54 surround_index |= 8;
55 }
56 return surround_index;
57 }
58
59 /**
60 * Given a layout and a coordinate, tell me which squares up/down/right/left
61 * are occupied by walls.
62 * @param layout
63 * @param i
64 * @param j
65 * spot to look.
66 * @param RP
67 * map parameters.
68 * @return
69 * combination of:
70 * - 1 = wall on left.
71 * - 2 = wall on right.
72 * - 4 = wall on above.
73 * - 8 = wall on below.
74 * @todo
75 * merge with surround_flag() and friends, check if such a function doesn't exist in other files.
76 */
77 int surround_flag2(char **layout, int i, int j, RMParms *RP)
78 {
79 int surround_index = 0;
80
81 if ((i > 0) && layout[i-1][j] == '#') {
82 surround_index |= 1;
83 }
84 if ((i < RP->Xsize-1) && layout[i+1][j] == '#') {
85 surround_index |= 2;
86 }
87 if ((j > 0) && layout[i][j-1] == '#') {
88 surround_index |= 4;
89 }
90 if ((j < RP->Ysize-1) && layout[i][j+1] == '#') {
91 surround_index |= 8;
92 }
93 return surround_index;
94 }
95
96 /**
97 * Check a map for blocked spots.
98 * Since this is part of the random map code, presumption
99 * is that this is not a tiled map.
100 * What is considered blocking and not is somewhat hard coded.
101 * @param map
102 * @param i
103 * @param j
104 * spot to look.
105 * @param RP
106 * map parameters.
107 * @return
108 * combination of:
109 * - 1 = blocked on left.
110 * - 2 = blocked on right.
111 * - 4 = blocked on above.
112 * - 8 = blocked on below.
113 */
114 int surround_flag3(mapstruct *map, int i, int j, RMParms *RP)
115 {
116 int surround_index = 0;
117
118 if ((i > 0) && (GET_MAP_MOVE_BLOCK(map, i-1, j)&~MOVE_BLOCK_DEFAULT)) {
119 surround_index |= 1;
120 }
121 if ((i < RP->Xsize-1) && (GET_MAP_MOVE_BLOCK(map, i+1, j)&~MOVE_BLOCK_DEFAULT)) {
122 surround_index |= 2;
123 }
124 if ((j > 0) && (GET_MAP_MOVE_BLOCK(map, i, j-1)&~MOVE_BLOCK_DEFAULT)) {
125 surround_index |= 4;
126 }
127 if ((j < RP->Ysize-1) && (GET_MAP_MOVE_BLOCK(map, i, j+1)&~MOVE_BLOCK_DEFAULT)) {
128 surround_index |= 8;
129 }
130
131 return surround_index;
132 }
133
134 /**
135 * Check a map for spots with walls.
136 * Since this is part of the random map code, presumption
137 * is that this is not a tiled map.
138 * What is considered blocking and not is somewhat hard coded.
139 * @param map
140 * @param i
141 * @param j
142 * spot to look.
143 * @param RP
144 * map parameters.
145 * @return
146 * combination of:
147 * - 1 = blocked on left.
148 * - 2 = blocked on right.
149 * - 4 = blocked on above.
150 * - 8 = blocked on below.
151 */
152 int surround_flag4(mapstruct *map, int i, int j, RMParms *RP)
153 {
154 int surround_index = 0;
155
156 if ((i > 0) && wall_blocked(map, i-1, j)) {
157 surround_index |= 1;
158 }
159 if ((i < RP->Xsize-1) && wall_blocked(map, i+1, j)) {
160 surround_index |= 2;
161 }
162 if ((j > 0) && wall_blocked(map, i, j-1)) {
163 surround_index |= 4;
164 }
165 if ((j < RP->Ysize-1) && wall_blocked(map, i, j+1)) {
166 surround_index |= 8;
167 }
168
169 return surround_index;
170 }
171
172 /**
173 * takes a map and a layout, and puts walls in the map (picked from
174 * w_style) at '#' marks.
175 * @param map
176 * map where walls will be put.
177 * @param layout
178 * layout containing walls and such.
179 * @param w_style
180 * wall style. Must not be NULL, can be "none".
181 * @param RP
182 * map parameters.
183 */
184 void make_map_walls(mapstruct *map, char **layout, char *w_style, RMParms *RP)
185 {
186 mapstruct *style_map = NULL;
187 object *the_wall;
188
189 /* get the style map */
190 if (!strcmp(w_style, "none")) {
191 return;
192 }
193 const char *styledirname = "/styles/wallstyles";
194 style_map = find_style(styledirname, w_style, -1);
195 if (style_map == NULL) {
196 return;
197 }
198
199 /* fill up the map with the given floor style */
200 if ((the_wall = pick_random_object(style_map)) != NULL) {
201 int i, j;
202 char *cp;
203 int joinedwalls = 0;
204 object *thiswall;
205
206 strlcpy(RP->wall_name, the_wall->arch->name, sizeof(RP->wall_name));
207 if ((cp = strchr(RP->wall_name, '_')) != NULL) {
208 *cp = 0;
209 joinedwalls = 1;
210 }
211
212 for (i = 0; i < RP->Xsize; i++)
213 for (j = 0; j < RP->Ysize; j++) {
214 if (layout[i][j] == '#') {
215 if (joinedwalls) {
216 thiswall = pick_joined_wall(the_wall, layout, i, j, RP);
217 } else {
218 thiswall = arch_to_object(the_wall->arch);
219 }
220 thiswall->move_block = MOVE_ALL;
221 thiswall->move_allow = 0;
222 object_insert_in_map_at(thiswall, map, thiswall, INS_NO_MERGE|INS_NO_WALK_ON, i, j);
223 }
224 }
225 }
226 }
227
228 /**
229 * Suffix to add to a base wall archetype name to get the joined wall based on the
230 * value returned by surround_flag2().
231 */
232 static const char* wall_join[16] = {
233 "_0", "_1_3", "_1_4", "_2_1_2",
234 "_1_2", "_2_2_4", "_2_2_1", "_3_1",
235 "_1_1", "_2_2_3", "_2_2_2", "_3_3",
236 "_2_1_1", "_3_4", "_3_2", "_4"
237 };
238
239 /**
240 * Picks the right wall type for this square, to make it look nice,
241 * and have everything nicely joined. It uses the layout.
242 * @param the_wall
243 * wall we want to insert.
244 * @param layout
245 * @param i
246 * @param j
247 * where to insert.
248 * @param RP
249 * map parameters.
250 * @return
251 * correct wall archetype to fit on the square.
252 * @todo
253 * check if there isn't an equivalent function in the building code, merge?
254 */
255 object *pick_joined_wall(object *the_wall, char **layout, int i, int j, RMParms *RP)
256 {
257 /* 1 = wall to left,
258 2 = wall to right,
259 4 = wall above
260 8 = wall below */
261 int surround_index = 0;
262 int l;
263 char wall_name[64];
264 archetype *wall_arch = NULL;
265
266 strncpy(wall_name, the_wall->arch->name, sizeof(wall_name));
267
268 /* conventionally, walls are named like this:
269 wallname_wallcode, where wallcode indicates
270 a joinedness, and wallname is the wall.
271 this code depends on the convention for
272 finding the right wall. */
273
274 /* extract the wall name, which is the text up to the leading _ */
275 for (l = 0; l < 64; l++) {
276 if (wall_name[l] == '_') {
277 wall_name[l] = 0;
278 break;
279 }
280 }
281
282 surround_index = surround_flag2(layout, i, j, RP);
283 assert(surround_index >= 0 && surround_index < 16);
284 strcat(wall_name, wall_join[surround_index]);
285
286 wall_arch = try_find_archetype(wall_name);
287 if (wall_arch) {
288 return arch_to_object(wall_arch);
289 } else {
290 return arch_to_object(the_wall->arch);
291 }
292 }
293
294 /**
295 * this takes a map, and changes an existing wall to match what's blocked
296 * around it, counting only doors and walls as blocked. If insert_flag is
297 * 1, . If not, it
298 * will only return the wall which would belong there, and doesn't
299 * remove anything. It depends on the
300 * global, previously-set variable, "wall_name"
301 * @param the_map
302 * @param i
303 * @param j
304 * where to look.
305 * @param insert_flag
306 * if 1, insert the correct wall into the map, else don't insert.
307 * @param RP
308 * map parameters.
309 * @return
310 * correct wall for spot.
311 * @todo
312 * merge with pick_joined_wall()?
313 */
314 object *retrofit_joined_wall(mapstruct *the_map, int i, int j, int insert_flag, RMParms *RP)
315 {
316 /* 1 = wall to left,
317 * 2 = wall to right,
318 * 4 = wall above
319 * 8 = wall below
320 */
321 int surround_index = 0;
322 int l;
323 object *the_wall = NULL;
324 object *new_wall = NULL;
325 archetype *wall_arch = NULL;
326
327 /* first find the wall */
328 FOR_MAP_PREPARE(the_map, i, j, tmp)
329 if ((tmp->move_type&MOVE_WALK) && tmp->type != EXIT && tmp->type != TELEPORTER) {
330 the_wall = tmp;
331 break;
332 }
333 FOR_MAP_FINISH();
334
335 /* if what we found is a door, don't remove it, set the_wall to NULL to
336 * signal that later.
337 */
338 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) {
339 the_wall = NULL;
340 /* if we're not supposed to insert a new wall where there wasn't one,
341 * we've gotta leave.
342 */
343 if (insert_flag == 0) {
344 return NULL;
345 }
346 } else if (the_wall == NULL) {
347 return NULL;
348 }
349
350 /* canonicalize the wall name */
351 for (l = 0; l < 64; l++) {
352 if (RP->wall_name[l] == '_') {
353 RP->wall_name[l] = 0;
354 break;
355 }
356 }
357
358 surround_index = surround_flag4(the_map, i, j, RP);
359 assert(surround_index >= 0 && surround_index < 16);
360 strcat(RP->wall_name, wall_join[surround_index]);
361 wall_arch = try_find_archetype(RP->wall_name);
362 if (wall_arch != NULL) {
363 new_wall = arch_to_object(wall_arch);
364 if (the_wall && the_wall->map) {
365 object_remove(the_wall);
366 object_free_drop_inventory(the_wall);
367 }
368 the_wall->move_block = MOVE_ALL;
369 object_insert_in_map_at(new_wall, the_map, new_wall, INS_NO_MERGE|INS_NO_WALK_ON, i, j);
370 }
371 return new_wall;
372 }