ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/deliantra/server/random_maps/wall.C
Revision: 1.30
Committed: Fri Nov 6 12:49:19 2009 UTC (16 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.29: +52 -56 lines
Log Message:
make effectively static symbols actually static, part 1

File Contents

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