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

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 Frank Tore Johansen
7 *
8 * 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 *
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 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * 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 *
22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */
24
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 are occupied. */
33 int
34 surround_flag (char **layout, int i, int j, random_map_params *RP)
35 {
36 /* 1 = wall to left,
37 2 = wall to right,
38 4 = wall above
39 8 = wall below */
40 int surround_index = 0;
41
42 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 return surround_index;
48 }
49
50
51 /* like surround_flag, but only walls count.
52 */
53
54 int
55 surround_flag2 (char **layout, int i, int j, random_map_params *RP)
56 {
57 /* 1 = wall to left,
58 2 = wall to right,
59 4 = wall above
60 8 = wall below */
61 int surround_index = 0;
62
63 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 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 int
78 surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP)
79 {
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
89 // don't forget to update the mapspace!
90 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
95 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
100 return surround_index;
101 }
102
103 /* like surround_flag2, except it checks a map, not a layout. */
104 static int
105 surround_flag4 (maptile *map, int i, int j, random_map_params *RP)
106 {
107 /* 1 = blocked to left,
108 2 = blocked to right,
109 4 = blocked above
110 8 = blocked below */
111 int surround_index = 0;
112
113 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
118 return surround_index;
119 }
120
121 /* picks the right wall type for this square, to make it look nice,
122 and have everything nicely joined. It uses the layout. */
123 static object *
124 pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP)
125 {
126 /* 1 = wall to left,
127 2 = wall to right,
128 4 = wall above
129 8 = wall below */
130 int surround_index = 0;
131 int l;
132 char wall_name[1024];
133 archetype *wall_arch = 0;
134
135 assign (wall_name, the_wall->arch->archname);
136
137 /* conventionally, walls are named like this:
138 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
143 /* extract the wall name, which is the text up to the leading _ */
144 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 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 }
206 wall_arch = archetype::find (wall_name);
207
208 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
209 }
210
211 /* takes a map and a layout, and puts walls in the map (picked from
212 w_style) at '#' marks. */
213 void
214 make_map_walls (maptile *map, char **layout, const char *w_style, const char *m_style, random_map_params *RP)
215 {
216 object *the_wall;
217
218 /* get the style map */
219 if (!strcmp (w_style, "none"))
220 return;
221
222 maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
223 if (!style_map)
224 return;
225
226 maptile *mining_map = m_style && *m_style
227 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
228 : 0;
229
230 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 thiswall = the_wall->arch->instance ();
253
254 thiswall->move_block = MOVE_ALL;
255 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 }
261 }
262 }
263 }
264
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 object *
273 retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
274 {
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 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above)
288 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 }
304 else if (the_wall == NULL)
305 return NULL;
306
307 /* 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 }
315 }
316
317 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 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 }
372
373 wall_arch = archetype::find (RP->wall_name);
374
375 if (!wall_arch)
376 {
377 new_wall = wall_arch->instance ();
378 new_wall->x = i;
379 new_wall->y = j;
380
381 if (the_wall && the_wall->map)
382 the_wall->destroy ();
383
384 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 }
387
388 return new_wall;
389 }