ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.36
Committed: Sat Apr 10 01:54:07 2010 UTC (14 years, 1 month ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-3_0
Changes since 1.35: +20 -34 lines
Log Message:
indent and difficult-support

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