File: wall.c

package info (click to toggle)
crossfire 1.75.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 24,168 kB
  • sloc: ansic: 83,169; sh: 4,659; perl: 1,736; lex: 1,443; makefile: 1,199; python: 43
file content (372 lines) | stat: -rw-r--r-- 10,199 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Crossfire -- cooperative multi-player graphical RPG and adventure game
 *
 * Copyright (c) 1999-2013 Mark Wedel and the Crossfire Development Team
 * Copyright (c) 1992 Frank Tore Johansen
 *
 * Crossfire is free software and comes with ABSOLUTELY NO WARRANTY. You are
 * welcome to redistribute it under certain conditions. For details, please
 * see COPYING and LICENSE.
 *
 * The authors can be reached via e-mail at <crossfire@metalforge.org>.
 */

#include "global.h"

#include <string.h>

#include "random_map.h"
#include "rproto.h"
#include <assert.h>

/**
 * Given a layout and a coordinate, tell me which squares up/down/right/left
 * are occupied.
 * @param layout
 * @param i
 * @param j
 * spot to look.
 * @param RP
 * map parameters.
 * @return
 * combination of:
 * - 1 = something on left.
 * - 2 = something on right.
 * - 4 = something on above.
 * - 8 = something on below.
 * @todo
 * merge with surround_flag2() and friends, check if such a function doesn't exist in other files.
 */
int surround_flag(char **layout, int i, int j, RMParms *RP)
{
    int surround_index = 0;

    if ((i > 0) && layout[i-1][j] != 0) {
        surround_index |= 1;
    }
    if ((i < RP->Xsize-1) && layout[i+1][j] != 0) {
        surround_index |= 2;
    }
    if ((j > 0) && layout[i][j-1] != 0) {
        surround_index |= 4;
    }
    if ((j < RP->Ysize-1) && layout[i][j+1] != 0) {
        surround_index |= 8;
    }
    return surround_index;
}

/**
 * Given a layout and a coordinate, tell me which squares up/down/right/left
 * are occupied by walls.
 * @param layout
 * @param i
 * @param j
 * spot to look.
 * @param RP
 * map parameters.
 * @return
 * combination of:
 * - 1 = wall on left.
 * - 2 = wall on right.
 * - 4 = wall on above.
 * - 8 = wall on below.
 * @todo
 * merge with surround_flag() and friends, check if such a function doesn't exist in other files.
 */
int surround_flag2(char **layout, int i, int j, RMParms *RP)
{
    int surround_index = 0;

    if ((i > 0) && layout[i-1][j] == '#') {
        surround_index |= 1;
    }
    if ((i < RP->Xsize-1) && layout[i+1][j] == '#') {
        surround_index |= 2;
    }
    if ((j > 0) && layout[i][j-1] == '#') {
        surround_index |= 4;
    }
    if ((j < RP->Ysize-1) && layout[i][j+1] == '#') {
        surround_index |= 8;
    }
    return surround_index;
}

/**
 * Check a map for blocked spots.
 * Since this is part of the random map code, presumption
 * is that this is not a tiled map.
 * What is considered blocking and not is somewhat hard coded.
 * @param map
 * @param i
 * @param j
 * spot to look.
 * @param RP
 * map parameters.
 * @return
 * combination of:
 * - 1 = blocked on left.
 * - 2 = blocked on right.
 * - 4 = blocked on above.
 * - 8 = blocked on below.
 */
int surround_flag3(mapstruct *map, int i, int j, RMParms *RP)
{
    int surround_index = 0;

    if ((i > 0) && (GET_MAP_MOVE_BLOCK(map, i-1, j)&~MOVE_BLOCK_DEFAULT)) {
        surround_index |= 1;
    }
    if ((i < RP->Xsize-1) && (GET_MAP_MOVE_BLOCK(map, i+1, j)&~MOVE_BLOCK_DEFAULT)) {
        surround_index |= 2;
    }
    if ((j > 0) && (GET_MAP_MOVE_BLOCK(map, i, j-1)&~MOVE_BLOCK_DEFAULT)) {
        surround_index |= 4;
    }
    if ((j < RP->Ysize-1) && (GET_MAP_MOVE_BLOCK(map, i, j+1)&~MOVE_BLOCK_DEFAULT)) {
        surround_index |= 8;
    }

    return surround_index;
}

/**
 * Check a map for spots with walls.
 * Since this is part of the random map code, presumption
 * is that this is not a tiled map.
 * What is considered blocking and not is somewhat hard coded.
 * @param map
 * @param i
 * @param j
 * spot to look.
 * @param RP
 * map parameters.
 * @return
 * combination of:
 * - 1 = blocked on left.
 * - 2 = blocked on right.
 * - 4 = blocked on above.
 * - 8 = blocked on below.
 */
int surround_flag4(mapstruct *map, int i, int j, RMParms *RP)
{
    int surround_index = 0;

    if ((i > 0) && wall_blocked(map, i-1, j)) {
        surround_index |= 1;
    }
    if ((i < RP->Xsize-1) && wall_blocked(map, i+1, j)) {
        surround_index |= 2;
    }
    if ((j > 0) && wall_blocked(map, i, j-1)) {
        surround_index |= 4;
    }
    if ((j < RP->Ysize-1) && wall_blocked(map, i, j+1)) {
        surround_index |= 8;
    }

    return surround_index;
}

/**
 * takes a map and a layout, and puts walls in the map (picked from
 * w_style) at '#' marks.
 * @param map
 * map where walls will be put.
 * @param layout
 * layout containing walls and such.
 * @param w_style
 * wall style. Must not be NULL, can be "none".
 * @param RP
 * map parameters.
 */
void make_map_walls(mapstruct *map, char **layout, char *w_style, RMParms *RP)
{
    mapstruct *style_map = NULL;
    object *the_wall;

    /* get the style map */
    if (!strcmp(w_style, "none")) {
        return;
    }
    const char *styledirname = "/styles/wallstyles";
    style_map = find_style(styledirname, w_style, -1);
    if (style_map == NULL) {
        return;
    }

    /* fill up the map with the given floor style */
    if ((the_wall = pick_random_object(style_map)) != NULL) {
        int i, j;
        char *cp;
        int joinedwalls = 0;
        object *thiswall;

        strlcpy(RP->wall_name, the_wall->arch->name, sizeof(RP->wall_name));
        if ((cp = strchr(RP->wall_name, '_')) != NULL) {
            *cp = 0;
            joinedwalls = 1;
        }

        for (i = 0; i < RP->Xsize; i++)
            for (j = 0; j < RP->Ysize; j++) {
                if (layout[i][j] == '#') {
                    if (joinedwalls) {
                        thiswall = pick_joined_wall(the_wall, layout, i, j, RP);
                    } else {
                        thiswall = arch_to_object(the_wall->arch);
                    }
                    thiswall->move_block = MOVE_ALL;
                    thiswall->move_allow = 0;
                    object_insert_in_map_at(thiswall, map, thiswall, INS_NO_MERGE|INS_NO_WALK_ON, i, j);
                }
            }
    }
}

/**
 * Suffix to add to a base wall archetype name to get the joined wall based on the
 * value returned by surround_flag2().
 */
static const char* wall_join[16] = {
    "_0", "_1_3", "_1_4", "_2_1_2",
    "_1_2", "_2_2_4", "_2_2_1", "_3_1",
    "_1_1", "_2_2_3", "_2_2_2", "_3_3",
    "_2_1_1", "_3_4", "_3_2", "_4"
};

/**
 * Picks the right wall type for this square, to make it look nice,
 * and have everything nicely joined.  It uses the layout.
 * @param the_wall
 * wall we want to insert.
 * @param layout
 * @param i
 * @param j
 * where to insert.
 * @param RP
 * map parameters.
 * @return
 * correct wall archetype to fit on the square.
 * @todo
 * check if there isn't an equivalent function in the building code, merge?
 */
object *pick_joined_wall(object *the_wall, char **layout, int i, int j, RMParms *RP)
{
    /* 1 = wall to left,
       2 = wall to right,
       4 = wall above
       8 = wall below */
    int surround_index = 0;
    int l;
    char wall_name[64];
    archetype *wall_arch = NULL;

    strncpy(wall_name, the_wall->arch->name, sizeof(wall_name));

    /* conventionally, walls are named like this:
     wallname_wallcode, where wallcode indicates
     a joinedness, and wallname is the wall.
     this code depends on the convention for
     finding the right wall. */

    /* extract the wall name, which is the text up to the leading _ */
    for (l = 0; l < 64; l++) {
        if (wall_name[l] == '_') {
            wall_name[l] = 0;
            break;
        }
    }

    surround_index = surround_flag2(layout, i, j, RP);
    assert(surround_index >= 0 && surround_index < 16);
    strcat(wall_name, wall_join[surround_index]);

    wall_arch = try_find_archetype(wall_name);
    if (wall_arch) {
        return arch_to_object(wall_arch);
    } else {
        return arch_to_object(the_wall->arch);
    }
}

/**
 * this takes a map, and changes an existing wall to match what's blocked
 * around it, counting only doors and walls as blocked.  If insert_flag is
 * 1, .  If not, it
 * will only return the wall which would belong there, and doesn't
 * remove anything.  It depends on the
 * global, previously-set variable, "wall_name"
 * @param the_map
 * @param i
 * @param j
 * where to look.
 * @param insert_flag
 * if 1, insert the correct wall into the map, else don't insert.
 * @param RP
 * map parameters.
 * @return
 * correct wall for spot.
 * @todo
 * merge with pick_joined_wall()?
 */
object *retrofit_joined_wall(mapstruct *the_map, int i, int j, int insert_flag, RMParms *RP)
{
    /* 1 = wall to left,
     * 2 = wall to right,
     * 4 = wall above
     * 8 = wall below
     */
    int surround_index = 0;
    int l;
    object *the_wall = NULL;
    object *new_wall = NULL;
    archetype *wall_arch = NULL;

    /* first find the wall */
    FOR_MAP_PREPARE(the_map, i, j, tmp)
    if ((tmp->move_type&MOVE_WALK) && tmp->type != EXIT && tmp->type != TELEPORTER) {
        the_wall = tmp;
        break;
    }
    FOR_MAP_FINISH();

    /* if what we found is a door, don't remove it, set the_wall to NULL to
     * signal that later.
     */
    if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) {
        the_wall = NULL;
        /* if we're not supposed to insert a new wall where there wasn't one,
         * we've gotta leave.
         */
        if (insert_flag == 0) {
            return NULL;
        }
    } else if (the_wall == NULL) {
        return NULL;
    }

    /* canonicalize the wall name */
    for (l = 0; l < 64; l++) {
        if (RP->wall_name[l] == '_') {
            RP->wall_name[l] = 0;
            break;
        }
    }

    surround_index = surround_flag4(the_map, i, j, RP);
    assert(surround_index >= 0 && surround_index < 16);
    strcat(RP->wall_name, wall_join[surround_index]);
    wall_arch = try_find_archetype(RP->wall_name);
    if (wall_arch != NULL) {
        new_wall = arch_to_object(wall_arch);
        if (the_wall && the_wall->map) {
            object_remove(the_wall);
            object_free_drop_inventory(the_wall);
        }
        the_wall->move_block = MOVE_ALL;
        object_insert_in_map_at(new_wall, the_map, new_wall, INS_NO_MERGE|INS_NO_WALK_ON, i, j);
    }
    return new_wall;
}