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