ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.37
Committed: Fri Jul 2 03:40:14 2010 UTC (13 years, 10 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.36: +33 -38 lines
Log Message:
refactoring (untested)

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