ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.32
Committed: Sun Nov 29 17:41:07 2009 UTC (14 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_92, rel-2_93
Changes since 1.31: +3 -3 lines
Log Message:
-instance, some los fiddling

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 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)
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 return surround_index;
51 }
52
53
54 /* like surround_flag, but only walls count.
55 */
56
57 int
58 surround_flag2 (char **layout, int i, int j, random_map_params *RP)
59 {
60 /* 1 = wall to left,
61 2 = wall to right,
62 4 = wall above
63 8 = wall below */
64 int surround_index = 0;
65
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 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 int
84 surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP)
85 {
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
95 // don't forget to update the mapspace!
96 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
101 if ((i > 0) && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK))
102 surround_index |= 1;
103 if ((i < RP->Xsize - 1) && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK))
104 surround_index |= 2;
105 if ((j > 0) && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK))
106 surround_index |= 4;
107 if ((j < RP->Ysize - 1) && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK))
108 surround_index |= 8;
109
110 return surround_index;
111 }
112
113 /* like surround_flag2, except it checks a map, not a layout. */
114 static int
115 surround_flag4 (maptile *map, int i, int j, random_map_params *RP)
116 {
117 /* 1 = blocked to left,
118 2 = blocked to right,
119 4 = blocked above
120 8 = blocked below */
121 int surround_index = 0;
122
123 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
132 return surround_index;
133 }
134
135 /* picks the right wall type for this square, to make it look nice,
136 and have everything nicely joined. It uses the layout. */
137 static object *
138 pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP)
139 {
140 /* 1 = wall to left,
141 2 = wall to right,
142 4 = wall above
143 8 = wall below */
144 int surround_index = 0;
145 int l;
146 char wall_name[1024];
147 archetype *wall_arch = 0;
148
149 assign (wall_name, the_wall->arch->archname);
150
151 /* conventionally, walls are named like this:
152 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
157 /* extract the wall name, which is the text up to the leading _ */
158 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 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 }
220 wall_arch = archetype::find (wall_name);
221
222 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
223 }
224
225 /* 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 thiswall = the_wall->arch->instance ();
268
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
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 object *
286 retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
287 {
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 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above)
301 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 }
317 else if (the_wall == NULL)
318 return NULL;
319
320 /* 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 }
328 }
329
330 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 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 }
385
386 wall_arch = archetype::find (RP->wall_name);
387
388 if (!wall_arch)
389 {
390 new_wall = wall_arch->instance ();
391 new_wall->x = i;
392 new_wall->y = j;
393
394 if (the_wall && the_wall->map)
395 the_wall->destroy ();
396
397 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 }
400
401 return new_wall;
402 }