ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
(Generate patch)

Comparing deliantra/server/random_maps/wall.C (file contents):
Revision 1.15 by pippijn, Sat Jan 6 14:42:30 2007 UTC vs.
Revision 1.43 by root, Fri Jul 2 17:43:16 2010 UTC

1
2/* 1/*
3 CrossFire, A Multiplayer game for X-windows 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
4 3 *
5 Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 Copyright (C) 2002 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7 Copyright (C) 1992 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
8 7 *
9 This program is free software; you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
10 it under the terms of the GNU General Public License as published by 9 * the terms of the Affero GNU General Public License as published by the
11 the Free Software Foundation; either version 2 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
12 (at your option) any later version. 11 * option) any later version.
13 12 *
14 This program is distributed in the hope that it will be useful, 13 * This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details. 16 * GNU General Public License for more details.
18 17 *
19 You should have received a copy of the GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
20 along with this program; if not, write to the Free Software 19 * and the GNU General Public License along with this program. If not, see
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 20 * <http://www.gnu.org/licenses/>.
22 21 *
23 The authors can be reached via e-mail at <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
24*/ 23 */
25 24
26#include <global.h> 25#include <global.h>
26#include <util.h>
27#include <random_map.h> 27#include <random_map.h>
28#include <rproto.h> 28#include <rproto.h>
29 29
30/* Put in the walls and autojoin them. */ 30/* Put in the walls and autojoin them. */
31 31
32
33/* given a layout and a coordinate, tell me which squares up/down/right/left 32/* given a maze and a coordinate, tell me which squares up/down/right/left
34 are occupied. */ 33 are occupied. */
35
36int 34int
37surround_flag (char **layout, int i, int j, random_map_params *RP) 35surround_flag (const layout &maze, int i, int j)
38{ 36{
39 /* 1 = wall to left, 37 /* 1 = wall to left,
40 2 = wall to right, 38 2 = wall to right,
41 4 = wall above 39 4 = wall above
42 8 = wall below */ 40 8 = wall below */
43 int surround_index = 0; 41 int surround_index = 0;
44 42
45 if ((i > 0) && layout[i - 1][j] != 0) 43 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
46 surround_index |= 1; 44 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
47 if ((i < RP->Xsize - 1) && layout[i + 1][j] != 0) 45 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
48 surround_index |= 2; 46 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
49 if ((j > 0) && layout[i][j - 1] != 0) 47
50 surround_index |= 4;
51 if ((j < RP->Ysize - 1) && layout[i][j + 1] != 0)
52 surround_index |= 8;
53 return surround_index; 48 return surround_index;
54} 49}
55 50
56
57/* like surround_flag, but only walls count. 51/* like surround_flag, but only walls count. */
58 */
59
60int 52int
61surround_flag2 (char **layout, int i, int j, random_map_params *RP) 53surround_flag2 (const layout &maze, int i, int j)
62{ 54{
63 /* 1 = wall to left, 55 /* 1 = wall to left,
64 2 = wall to right, 56 2 = wall to right,
65 4 = wall above 57 4 = wall above
66 8 = wall below */ 58 8 = wall below */
67 int surround_index = 0; 59 int surround_index = 0;
68 60
69 if ((i > 0) && layout[i - 1][j] == '#') 61 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
70 surround_index |= 1; 62 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
71 if ((i < RP->Xsize - 1) && layout[i + 1][j] == '#') 63 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
72 surround_index |= 2; 64 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
73 if ((j > 0) && layout[i][j - 1] == '#') 65
74 surround_index |= 4;
75 if ((j < RP->Ysize - 1) && layout[i][j + 1] == '#')
76 surround_index |= 8;
77 return surround_index; 66 return surround_index;
78} 67}
79 68
80
81/* like surround_flag, except it checks a map, not a layout. 69/* like surround_flag, except it checks a map, not a maze.
82 * Since this is part of the random map code, presumption 70 * Since this is part of the random map code, presumption
83 * is that this is not a tiled map. 71 * is that this is not a tiled map.
84 * What is considered blocking and not is somewhat hard coded. 72 * What is considered blocking and not is somewhat hard coded.
85 */ 73 */
86int 74int
87surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP) 75surround_flag3 (maptile *map, int i, int j)
88{ 76{
89 /* 77 /*
90 * 1 = blocked to left, 78 * 1 = blocked to left,
91 * 2 = blocked to right, 79 * 2 = blocked to right,
92 * 4 = blocked above 80 * 4 = blocked above
93 * 8 = blocked below 81 * 8 = blocked below
94 */ 82 */
95
96 int surround_index = 0;
97
98 if ((i > 0) && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & ~MOVE_BLOCK_DEFAULT))
99 surround_index |= 1; 83 int surround_index = 0;
100 if ((i < RP->Xsize - 1) && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & ~MOVE_BLOCK_DEFAULT)) 84
101 surround_index |= 2; 85 // don't forget to update the mapspace!
102 if ((j > 0) && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & ~MOVE_BLOCK_DEFAULT)) 86 if (i > 0) map->at (i - 1, j ).update ();
103 surround_index |= 4; 87 if (i < map->width - 1) map->at (i + 1, j ).update ();
104 if ((j < RP->Ysize - 1) && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & ~MOVE_BLOCK_DEFAULT)) 88 if (j > 0) map->at (i , j - 1).update ();
105 surround_index |= 8; 89 if (j < map->height - 1) map->at (i , j + 1).update ();
90
91 if (i > 0 && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) surround_index |= 1;
92 if (i < map->width - 1 && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK)) surround_index |= 2;
93 if (j > 0 && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK)) surround_index |= 4;
94 if (j < map->height - 1 && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) surround_index |= 8;
106 95
107 return surround_index; 96 return surround_index;
108} 97}
109 98
110/* like surround_flag2, except it checks a map, not a layout. */ 99/* like surround_flag2, except it checks a map, not a maze. */
111 100static int
112int
113surround_flag4 (maptile *map, int i, int j, random_map_params *RP) 101surround_flag4 (maptile *map, int i, int j)
114{ 102{
115 /* 1 = blocked to left, 103 /* 1 = blocked to left,
116 2 = blocked to right, 104 2 = blocked to right,
117 4 = blocked above 105 4 = blocked above
118 8 = blocked below */ 106 8 = blocked below */
119 int surround_index = 0; 107 int surround_index = 0;
120 108
121 if ((i > 0) && wall_blocked (map, i - 1, j)) 109 if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1;
122 surround_index |= 1; 110 if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
123 if ((i < RP->Xsize - 1) && wall_blocked (map, i + 1, j)) 111 if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4;
124 surround_index |= 2; 112 if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
125 if ((j > 0) && wall_blocked (map, i, j - 1))
126 surround_index |= 4;
127 if ((j < RP->Ysize - 1) && wall_blocked (map, i, j + 1))
128 surround_index |= 8;
129 113
130 return surround_index; 114 return surround_index;
131} 115}
132 116
133/* takes a map and a layout, and puts walls in the map (picked from
134 w_style) at '#' marks. */
135
136void
137make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP)
138{
139 char styledirname[1024];
140 char stylefilepath[1024];
141 maptile *style_map = 0;
142 object *the_wall;
143
144 /* get the style map */
145 if (!strcmp (w_style, "none"))
146 return;
147 sprintf (styledirname, "%s", "/styles/wallstyles");
148 sprintf (stylefilepath, "%s/%s", styledirname, w_style);
149 style_map = find_style (styledirname, w_style, -1);
150 if (style_map == 0)
151 return;
152
153 /* fill up the map with the given floor style */
154 if ((the_wall = pick_random_object (style_map)) != NULL)
155 {
156 int i, j;
157 char *cp;
158 int joinedwalls = 0;
159 object *thiswall;
160
161 sprintf (RP->wall_name, "%s", &the_wall->arch->name);
162 if ((cp = strchr (RP->wall_name, '_')) != NULL)
163 {
164 *cp = 0;
165 joinedwalls = 1;
166 }
167
168 for (i = 0; i < RP->Xsize; i++)
169 for (j = 0; j < RP->Ysize; j++)
170 {
171 if (layout[i][j] == '#')
172 {
173 if (joinedwalls)
174 thiswall = pick_joined_wall (the_wall, layout, i, j, RP);
175 else
176 thiswall = arch_to_object (the_wall->arch);
177 thiswall->x = i;
178 thiswall->y = j;
179 thiswall->move_block = MOVE_ALL;
180 insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
181 }
182 }
183 }
184}
185
186
187/* picks the right wall type for this square, to make it look nice, 117/* picks the right wall type for this square, to make it look nice,
188 and have everything nicely joined. It uses the layout. */ 118 and have everything nicely joined. It uses the maze. */
189 119static object *
190object *
191pick_joined_wall (object *the_wall, char **layout, int i, int j, random_map_params *RP) 120pick_joined_wall (object *the_wall, const layout &maze, int i, int j, random_map_params *RP)
192{ 121{
193 /* 1 = wall to left, 122 /* 1 = wall to left,
194 2 = wall to right, 123 2 = wall to right,
195 4 = wall above 124 4 = wall above
196 8 = wall below */ 125 8 = wall below */
197 int surround_index = 0; 126 int surround_index = 0;
198 int l; 127 int l;
199 char wall_name[1024]; 128 char wall_name[1024];
200 archetype *wall_arch = 0; 129 archetype *wall_arch = 0;
201 130
202 strcpy (wall_name, the_wall->arch->name); 131 assign (wall_name, the_wall->arch->archname);
203 132
204 /* conventionally, walls are named like this: 133 /* conventionally, walls are named like this:
205 wallname_wallcode, where wallcode indicates 134 wallname_wallcode, where wallcode indicates
206 a joinedness, and wallname is the wall. 135 a joinedness, and wallname is the wall.
207 this code depends on the convention for 136 this code depends on the convention for
215 wall_name[l] = 0; 144 wall_name[l] = 0;
216 break; 145 break;
217 } 146 }
218 } 147 }
219 148
220 surround_index = surround_flag2 (layout, i, j, RP);
221
222 switch (surround_index)
223 {
224 case 0:
225 strcat (wall_name, "_0"); 149 strcat (wall_name, "_");
226 break; 150 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
227 case 1: 151
228 strcat (wall_name, "_1_3");
229 break;
230 case 2:
231 strcat (wall_name, "_1_4");
232 break;
233 case 3:
234 strcat (wall_name, "_2_1_2");
235 break;
236 case 4:
237 strcat (wall_name, "_1_2");
238 break;
239 case 5:
240 strcat (wall_name, "_2_2_4");
241 break;
242 case 6:
243 strcat (wall_name, "_2_2_1");
244 break;
245 case 7:
246 strcat (wall_name, "_3_1");
247 break;
248 case 8:
249 strcat (wall_name, "_1_1");
250 break;
251 case 9:
252 strcat (wall_name, "_2_2_3");
253 break;
254 case 10:
255 strcat (wall_name, "_2_2_2");
256 break;
257 case 11:
258 strcat (wall_name, "_3_3");
259 break;
260 case 12:
261 strcat (wall_name, "_2_1_1");
262 break;
263 case 13:
264 strcat (wall_name, "_3_4");
265 break;
266 case 14:
267 strcat (wall_name, "_3_2");
268 break;
269 case 15:
270 strcat (wall_name, "_4");
271 break;
272 }
273 wall_arch = archetype::find (wall_name); 152 wall_arch = archetype::find (wall_name);
274 153
275 return wall_arch ? arch_to_object (wall_arch) : arch_to_object (the_wall->arch); 154 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
276} 155}
277 156
157// checks whether the layout has a "reachable" space at a given point, i.e.
158// not a wall that is completely surrounded by walls.
159static bool inline
160is_visible (layout &maze, int i, int j)
161{
162 for (int dx = -1; dx <= 1; ++dx)
163 for (int dy = -1; dy <= 1; ++dy)
164 {
165 int x = i + dx;
166 int y = j + dy;
167
168 if (IN_RANGE_EXC (x, 0, maze.w)
169 && IN_RANGE_EXC (y, 0, maze.h)
170 && maze [x][y] != '#')
171 return true;
172 }
173
174 return false;
175}
176
177/* takes a map and a maze, and puts walls in the map (picked from
178 w_style) at '#' marks. */
179void
180make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
181{
182 object *the_wall;
183
184 /* get the style map */
185 if (!strcmp (w_style, "none"))
186 return;
187
188 maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
189 if (!style_map)
190 return;
191
192 maptile *mining_map = m_style && *m_style
193 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
194 : 0;
195
196 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
197 {
198 // first pass, remove all "unreachable" (in-rock) walls
199 // and replace them by "blocked" spaces.
200
201 layout clean_maze (maze);
202
203 if (archetype *blocked = archetype::find ("blocked"))
204 for (int x = 0; x < maze.w; ++x)
205 for (int y = 0; y < maze.h; ++y)
206 if (maze [x][y] == '#' && !is_visible (maze, x, y))
207 {
208 clean_maze [x][y] = 'X';
209
210 // erase everything on this space ad replace it by blocked
211 mapspace &ms = map->at (x, y);
212
213 while (ms.top)
214 ms.top->destroy ();
215
216 map->insert (blocked->instance (), x, y, 0, INS_NO_MERGE | INS_NO_WALK_ON);
217 }
218
219 // second pass, add in walls
220
221 int joinedwalls = 0;
222
223 assign (RP->wall_name, the_wall->arch->archname);
224 if (char *cp = strchr (RP->wall_name, '_'))
225 {
226 *cp = 0;
227 joinedwalls = 1;
228 }
229
230 for (int i = 0; i < RP->Xsize; i++)
231 for (int j = 0; j < RP->Ysize; j++)
232 if (clean_maze [i][j] == '#')
233 {
234 object *thiswall = joinedwalls ? pick_joined_wall (the_wall, clean_maze, i, j, RP)
235 : the_wall->arch->instance ();
236
237 thiswall->move_block = MOVE_ALL;
238 map->insert (thiswall, i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
239
240 if (mining_map)
241 if (object *vein = mining_map->at (rmg_rndm (mining_map->width), rmg_rndm (mining_map->height)).bot)
242 map->insert (vein->clone (), i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
243 }
244 }
245}
278 246
279/* this takes a map, and changes an existing wall to match what's blocked 247/* this takes a map, and changes an existing wall to match what's blocked
280 * around it, counting only doors and walls as blocked. If insert_flag is 248 * around it, counting only doors and walls as blocked. If insert_flag is
281 * 1, it will go ahead and insert the wall into the map. If not, it 249 * 1, it will go ahead and insert the wall into the map. If not, it
282 * will only return the wall which would belong there, and doesn't 250 * will only return the wall which would belong there, and doesn't
283 * remove anything. It depends on the 251 * remove anything. It depends on the
284 * global, previously-set variable, "wall_name" 252 * global, previously-set variable, "wall_name"
285 */ 253 */
286
287object * 254object *
288retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP) 255retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
289{ 256{
290 /* 1 = wall to left, 257 /* 1 = wall to left,
291 * 2 = wall to right, 258 * 2 = wall to right,
297 object *the_wall = 0; 264 object *the_wall = 0;
298 object *new_wall = 0; 265 object *new_wall = 0;
299 archetype *wall_arch = 0; 266 archetype *wall_arch = 0;
300 267
301 /* first find the wall */ 268 /* first find the wall */
302 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above) 269 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above)
303 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER) 270 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
304 break; 271 break;
305
306 272
307 /* if what we found is a door, don't remove it, set the_wall to NULL to 273 /* if what we found is a door, don't remove it, set the_wall to NULL to
308 * signal that later. 274 * signal that later.
309 */ 275 */
310 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) 276 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
327 RP->wall_name[l] = 0; 293 RP->wall_name[l] = 0;
328 break; 294 break;
329 } 295 }
330 } 296 }
331 297
332 surround_index = surround_flag4 (the_map, i, j, RP);
333 /* This would be a lot cleaner to just us a lookup table,
334 * eg, wall_suffix[surround_index]
335 */
336 switch (surround_index)
337 {
338 case 0:
339 strcat (RP->wall_name, "_0"); 298 strcat (RP->wall_name, "_");
340 break; 299 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
341 case 1:
342 strcat (RP->wall_name, "_1_3");
343 break;
344 case 2:
345 strcat (RP->wall_name, "_1_4");
346 break;
347 case 3:
348 strcat (RP->wall_name, "_2_1_2");
349 break;
350 case 4:
351 strcat (RP->wall_name, "_1_2");
352 break;
353 case 5:
354 strcat (RP->wall_name, "_2_2_4");
355 break;
356 case 6:
357 strcat (RP->wall_name, "_2_2_1");
358 break;
359 case 7:
360 strcat (RP->wall_name, "_3_1");
361 break;
362 case 8:
363 strcat (RP->wall_name, "_1_1");
364 break;
365 case 9:
366 strcat (RP->wall_name, "_2_2_3");
367 break;
368 case 10:
369 strcat (RP->wall_name, "_2_2_2");
370 break;
371 case 11:
372 strcat (RP->wall_name, "_3_3");
373 break;
374 case 12:
375 strcat (RP->wall_name, "_2_1_1");
376 break;
377 case 13:
378 strcat (RP->wall_name, "_3_4");
379 break;
380 case 14:
381 strcat (RP->wall_name, "_3_2");
382 break;
383 case 15:
384 strcat (RP->wall_name, "_4");
385 break;
386 }
387 300
388 wall_arch = archetype::find (RP->wall_name); 301 wall_arch = archetype::find (RP->wall_name);
389 302
390 if (!wall_arch) 303 if (!wall_arch)
391 { 304 {
392 new_wall = arch_to_object (wall_arch); 305 new_wall = wall_arch->instance ();
393 new_wall->x = i; 306 new_wall->x = i;
394 new_wall->y = j; 307 new_wall->y = j;
395 308
396 if (the_wall && the_wall->map) 309 if (the_wall && the_wall->map)
397 {
398 the_wall->remove ();
399 the_wall->destroy (); 310 the_wall->destroy ();
400 }
401 311
402 the_wall->move_block = MOVE_ALL; 312 the_wall->move_block = MOVE_ALL;
403 insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON); 313 insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
404 } 314 }
405 315

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines