ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.29
Committed: Mon Oct 12 14:00:58 2009 UTC (14 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81
Changes since 1.28: +7 -6 lines
Log Message:
clarify license

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     /* takes a map and a layout, and puts walls in the map (picked from
137 root 1.2 w_style) at '#' marks. */
138 elmex 1.1
139 root 1.4 void
140 root 1.13 make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP)
141 root 1.4 {
142 root 1.14 char styledirname[1024];
143     char stylefilepath[1024];
144 root 1.7 maptile *style_map = 0;
145 root 1.4 object *the_wall;
146    
147     /* get the style map */
148     if (!strcmp (w_style, "none"))
149     return;
150     sprintf (styledirname, "%s", "/styles/wallstyles");
151     sprintf (stylefilepath, "%s/%s", styledirname, w_style);
152     style_map = find_style (styledirname, w_style, -1);
153 root 1.17 if (!style_map)
154 root 1.4 return;
155    
156     /* fill up the map with the given floor style */
157 root 1.26 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
158 root 1.4 {
159     int i, j;
160     char *cp;
161     int joinedwalls = 0;
162     object *thiswall;
163    
164 root 1.22 sprintf (RP->wall_name, "%s", &the_wall->arch->archname);
165 root 1.4 if ((cp = strchr (RP->wall_name, '_')) != NULL)
166     {
167     *cp = 0;
168     joinedwalls = 1;
169 root 1.2 }
170    
171 root 1.4 for (i = 0; i < RP->Xsize; i++)
172     for (j = 0; j < RP->Ysize; j++)
173     {
174     if (layout[i][j] == '#')
175     {
176     if (joinedwalls)
177     thiswall = pick_joined_wall (the_wall, layout, i, j, RP);
178     else
179     thiswall = arch_to_object (the_wall->arch);
180     thiswall->x = i;
181     thiswall->y = j;
182     thiswall->move_block = MOVE_ALL;
183     insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
184     }
185     }
186 elmex 1.1 }
187     }
188    
189    
190     /* picks the right wall type for this square, to make it look nice,
191 root 1.2 and have everything nicely joined. It uses the layout. */
192 elmex 1.1
193 root 1.4 object *
194 root 1.13 pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP)
195 root 1.4 {
196 elmex 1.1 /* 1 = wall to left,
197 root 1.4 2 = wall to right,
198     4 = wall above
199     8 = wall below */
200     int surround_index = 0;
201 elmex 1.1 int l;
202 root 1.14 char wall_name[1024];
203 root 1.4 archetype *wall_arch = 0;
204 elmex 1.1
205 root 1.22 assign (wall_name, the_wall->arch->archname);
206 elmex 1.1
207     /* conventionally, walls are named like this:
208 root 1.4 wallname_wallcode, where wallcode indicates
209     a joinedness, and wallname is the wall.
210     this code depends on the convention for
211     finding the right wall. */
212 elmex 1.1
213     /* extract the wall name, which is the text up to the leading _ */
214 root 1.4 for (l = 0; l < 64; l++)
215     {
216     if (wall_name[l] == '_')
217     {
218     wall_name[l] = 0;
219     break;
220     }
221     }
222    
223     surround_index = surround_flag2 (layout, i, j, RP);
224    
225     switch (surround_index)
226     {
227 root 1.13 case 0:
228     strcat (wall_name, "_0");
229     break;
230     case 1:
231     strcat (wall_name, "_1_3");
232     break;
233     case 2:
234     strcat (wall_name, "_1_4");
235     break;
236     case 3:
237     strcat (wall_name, "_2_1_2");
238     break;
239     case 4:
240     strcat (wall_name, "_1_2");
241     break;
242     case 5:
243     strcat (wall_name, "_2_2_4");
244     break;
245     case 6:
246     strcat (wall_name, "_2_2_1");
247     break;
248     case 7:
249     strcat (wall_name, "_3_1");
250     break;
251     case 8:
252     strcat (wall_name, "_1_1");
253     break;
254     case 9:
255     strcat (wall_name, "_2_2_3");
256     break;
257     case 10:
258     strcat (wall_name, "_2_2_2");
259     break;
260     case 11:
261     strcat (wall_name, "_3_3");
262     break;
263     case 12:
264     strcat (wall_name, "_2_1_1");
265     break;
266     case 13:
267     strcat (wall_name, "_3_4");
268     break;
269     case 14:
270     strcat (wall_name, "_3_2");
271     break;
272     case 15:
273     strcat (wall_name, "_4");
274     break;
275 root 1.4 }
276 root 1.5 wall_arch = archetype::find (wall_name);
277 elmex 1.1
278 root 1.13 return wall_arch ? arch_to_object (wall_arch) : arch_to_object (the_wall->arch);
279 elmex 1.1 }
280 root 1.4
281 elmex 1.1
282     /* this takes a map, and changes an existing wall to match what's blocked
283     * around it, counting only doors and walls as blocked. If insert_flag is
284     * 1, it will go ahead and insert the wall into the map. If not, it
285     * will only return the wall which would belong there, and doesn't
286     * remove anything. It depends on the
287     * global, previously-set variable, "wall_name"
288     */
289    
290 root 1.4 object *
291 root 1.13 retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
292 root 1.4 {
293     /* 1 = wall to left,
294     * 2 = wall to right,
295     * 4 = wall above
296     * 8 = wall below
297     */
298     int surround_index = 0;
299     int l;
300     object *the_wall = 0;
301     object *new_wall = 0;
302     archetype *wall_arch = 0;
303    
304     /* first find the wall */
305 root 1.10 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above)
306 root 1.4 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
307     break;
308    
309    
310     /* if what we found is a door, don't remove it, set the_wall to NULL to
311     * signal that later.
312     */
313     if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
314     {
315     the_wall = NULL;
316     /* if we're not supposed to insert a new wall where there wasn't one,
317     * we've gotta leave.
318     */
319     if (insert_flag == 0)
320     return 0;
321 elmex 1.1 }
322 root 1.4 else if (the_wall == NULL)
323     return NULL;
324 elmex 1.1
325 root 1.4 /* canonicalize the wall name */
326     for (l = 0; l < 64; l++)
327     {
328     if (RP->wall_name[l] == '_')
329     {
330     RP->wall_name[l] = 0;
331     break;
332 root 1.2 }
333 elmex 1.1 }
334    
335 root 1.4 surround_index = surround_flag4 (the_map, i, j, RP);
336     /* This would be a lot cleaner to just us a lookup table,
337     * eg, wall_suffix[surround_index]
338     */
339     switch (surround_index)
340     {
341 root 1.13 case 0:
342     strcat (RP->wall_name, "_0");
343     break;
344     case 1:
345     strcat (RP->wall_name, "_1_3");
346     break;
347     case 2:
348     strcat (RP->wall_name, "_1_4");
349     break;
350     case 3:
351     strcat (RP->wall_name, "_2_1_2");
352     break;
353     case 4:
354     strcat (RP->wall_name, "_1_2");
355     break;
356     case 5:
357     strcat (RP->wall_name, "_2_2_4");
358     break;
359     case 6:
360     strcat (RP->wall_name, "_2_2_1");
361     break;
362     case 7:
363     strcat (RP->wall_name, "_3_1");
364     break;
365     case 8:
366     strcat (RP->wall_name, "_1_1");
367     break;
368     case 9:
369     strcat (RP->wall_name, "_2_2_3");
370     break;
371     case 10:
372     strcat (RP->wall_name, "_2_2_2");
373     break;
374     case 11:
375     strcat (RP->wall_name, "_3_3");
376     break;
377     case 12:
378     strcat (RP->wall_name, "_2_1_1");
379     break;
380     case 13:
381     strcat (RP->wall_name, "_3_4");
382     break;
383     case 14:
384     strcat (RP->wall_name, "_3_2");
385     break;
386     case 15:
387     strcat (RP->wall_name, "_4");
388     break;
389 elmex 1.1 }
390 root 1.11
391 root 1.5 wall_arch = archetype::find (RP->wall_name);
392 root 1.11
393     if (!wall_arch)
394 root 1.4 {
395     new_wall = arch_to_object (wall_arch);
396     new_wall->x = i;
397     new_wall->y = j;
398 root 1.11
399 root 1.4 if (the_wall && the_wall->map)
400 root 1.28 the_wall->destroy ();
401 root 1.11
402 root 1.4 the_wall->move_block = MOVE_ALL;
403     insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
404 elmex 1.1 }
405 root 1.11
406 root 1.4 return new_wall;
407 elmex 1.1 }