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.31 by root, Fri Nov 6 13:03:34 2009 UTC vs.
Revision 1.49 by root, Mon Oct 29 23:55:54 2012 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * Copyright (©) 2002,2007 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992,2007 Frank Tore Johansen 6 * Copyright (©) 1992 Frank Tore Johansen
7 * 7 *
8 * Deliantra is free software: you can redistribute it and/or modify it under 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 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 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * option) any later version. 11 * option) any later version.
12 * 12 *
13 * 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,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details. 16 * GNU General Public License for more details.
17 * 17 *
18 * You should have received a copy of the Affero GNU General Public License 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 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>. 20 * <http://www.gnu.org/licenses/>.
21 * 21 *
22 * The authors can be reached via e-mail to <support@deliantra.net> 22 * The authors can be reached via e-mail to <support@deliantra.net>
23 */ 23 */
24 24
25#include <global.h> 25#include <global.h>
26#include <util.h>
26#include <random_map.h> 27#include <rmg.h>
27#include <rproto.h> 28#include <rproto.h>
28 29
29/* Put in the walls and autojoin them. */ 30/* Put in the walls and autojoin them. */
30 31
31/* 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
32 are occupied. */ 33 are occupied. */
33int 34int
34surround_flag (char **layout, int i, int j, random_map_params *RP) 35surround_flag (const layout &maze, int i, int j)
35{ 36{
36 /* 1 = wall to left, 37 /* 1 = wall to left,
37 2 = wall to right, 38 2 = wall to right,
38 4 = wall above 39 4 = wall above
39 8 = wall below */ 40 8 = wall below */
40 int surround_index = 0; 41 int surround_index = 0;
41 42
42 if ((i > 0) && layout[i - 1][j] != 0) 43 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
43 surround_index |= 1; 44 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
44 if ((i < RP->Xsize - 1) && layout[i + 1][j] != 0) 45 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
45 surround_index |= 2; 46 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
46 if ((j > 0) && layout[i][j - 1] != 0) 47
47 surround_index |= 4;
48 if ((j < RP->Ysize - 1) && layout[i][j + 1] != 0)
49 surround_index |= 8;
50 return surround_index; 48 return surround_index;
51} 49}
52 50
53
54/* like surround_flag, but only walls count. 51/* like surround_flag, but only walls count. */
55 */
56
57int 52int
58surround_flag2 (char **layout, int i, int j, random_map_params *RP) 53surround_flag2 (const layout &maze, int i, int j)
59{ 54{
60 /* 1 = wall to left, 55 /* 1 = wall to left,
61 2 = wall to right, 56 2 = wall to right,
62 4 = wall above 57 4 = wall above
63 8 = wall below */ 58 8 = wall below */
64 int surround_index = 0; 59 int surround_index = 0;
65 60
66 if ((i > 0) && layout[i - 1][j] == '#') 61 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
67 surround_index |= 1; 62 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
68 if ((i < RP->Xsize - 1) && layout[i + 1][j] == '#') 63 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
69 surround_index |= 2; 64 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
70 if ((j > 0) && layout[i][j - 1] == '#') 65
71 surround_index |= 4;
72 if ((j < RP->Ysize - 1) && layout[i][j + 1] == '#')
73 surround_index |= 8;
74 return surround_index; 66 return surround_index;
75} 67}
76 68
77
78/* like surround_flag, except it checks a map, not a layout. 69/* like surround_flag, except it checks a map, not a maze.
79 * Since this is part of the random map code, presumption 70 * Since this is part of the random map code, presumption
80 * is that this is not a tiled map. 71 * is that this is not a tiled map.
81 * What is considered blocking and not is somewhat hard coded. 72 * What is considered blocking and not is somewhat hard coded.
82 */ 73 */
83int 74int
84surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP) 75surround_flag3 (maptile *map, int i, int j)
85{ 76{
86 /* 77 /*
87 * 1 = blocked to left, 78 * 1 = blocked to left,
88 * 2 = blocked to right, 79 * 2 = blocked to right,
89 * 4 = blocked above 80 * 4 = blocked above
90 * 8 = blocked below 81 * 8 = blocked below
91 */ 82 */
92
93 int surround_index = 0; 83 int surround_index = 0;
94 84
95 // don't forget to update the mapspace! 85 // don't forget to update the mapspace!
96 if (i > 0) map->at (i - 1, j ).update (); 86 if (i > 0) map->at (i - 1, j ).update ();
97 if (i < RP->Xsize - 1) map->at (i + 1, j ).update (); 87 if (i < map->width - 1) map->at (i + 1, j ).update ();
98 if (j > 0) map->at (i , j - 1).update (); 88 if (j > 0) map->at (i , j - 1).update ();
99 if (j < RP->Ysize - 1) map->at (i , j + 1).update (); 89 if (j < map->height - 1) map->at (i , j + 1).update ();
100 90
101 if ((i > 0) && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) 91 if (i > 0 && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) surround_index |= 1;
102 surround_index |= 1;
103 if ((i < RP->Xsize - 1) && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK)) 92 if (i < map->width - 1 && (GET_MAP_MOVE_BLOCK (map, i + 1, j) & MOVE_WALK)) surround_index |= 2;
104 surround_index |= 2;
105 if ((j > 0) && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK)) 93 if (j > 0 && (GET_MAP_MOVE_BLOCK (map, i, j - 1) & MOVE_WALK)) surround_index |= 4;
106 surround_index |= 4;
107 if ((j < RP->Ysize - 1) && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) 94 if (j < map->height - 1 && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) surround_index |= 8;
108 surround_index |= 8;
109 95
110 return surround_index; 96 return surround_index;
111} 97}
112 98
113/* like surround_flag2, except it checks a map, not a layout. */ 99/* like surround_flag2, except it checks a map, not a maze. */
114static int 100static int
115surround_flag4 (maptile *map, int i, int j, random_map_params *RP) 101surround_flag4 (maptile *map, int i, int j)
116{ 102{
117 /* 1 = blocked to left, 103 /* 1 = blocked to left,
118 2 = blocked to right, 104 2 = blocked to right,
119 4 = blocked above 105 4 = blocked above
120 8 = blocked below */ 106 8 = blocked below */
121 int surround_index = 0; 107 int surround_index = 0;
122 108
123 if ((i > 0) && wall_blocked (map, i - 1, j)) 109 if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1;
124 surround_index |= 1; 110 if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
125 if ((i < RP->Xsize - 1) && wall_blocked (map, i + 1, j)) 111 if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4;
126 surround_index |= 2; 112 if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
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 113
132 return surround_index; 114 return surround_index;
133} 115}
134 116
135/* 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,
136 and have everything nicely joined. It uses the layout. */ 118 and have everything nicely joined. It uses the maze. */
137static object * 119static object *
138pick_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)
139{ 121{
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; 122 int l;
146 char wall_name[1024]; 123 char wall_name[1024];
147 archetype *wall_arch = 0; 124 archetype *wall_arch = 0;
148 125
149 assign (wall_name, the_wall->arch->archname); 126 assign (wall_name, the_wall->arch->archname);
162 wall_name[l] = 0; 139 wall_name[l] = 0;
163 break; 140 break;
164 } 141 }
165 } 142 }
166 143
167 surround_index = surround_flag2 (layout, i, j, RP);
168
169 switch (surround_index)
170 {
171 case 0:
172 strcat (wall_name, "_0"); 144 strcat (wall_name, "_");
173 break; 145 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
174 case 1: 146
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); 147 wall_arch = archetype::find (wall_name);
221 148
222 return wall_arch ? arch_to_object (wall_arch) : arch_to_object (the_wall->arch); 149 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
223} 150}
224 151
152// checks whether the layout has a "reachable" space at a given point, i.e.
153// not a wall that is completely surrounded by walls.
154static bool inline
155is_visible (layout &maze, int i, int j)
156{
157 for (int dx = -1; dx <= 1; ++dx)
158 for (int dy = -1; dy <= 1; ++dy)
159 {
160 int x = i + dx;
161 int y = j + dy;
162
163 if (IN_RANGE_EXC (x, 0, maze.w)
164 && IN_RANGE_EXC (y, 0, maze.h)
165 && maze [x][y] != '#')
166 return true;
167 }
168
169 return false;
170}
171
225/* takes a map and a layout, and puts walls in the map (picked from 172/* takes a map and a maze, and puts walls in the map (picked from
226 w_style) at '#' marks. */ 173 w_style) at '#' marks. */
227void 174void
228make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP) 175make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
229{ 176{
230 char styledirname[1024];
231 char stylefilepath[1024];
232 maptile *style_map = 0;
233 object *the_wall; 177 object *the_wall;
234 178
235 /* get the style map */ 179 /* get the style map */
236 if (!strcmp (w_style, "none")) 180 if (!strcmp (w_style, "none"))
237 return; 181 return;
238 sprintf (styledirname, "%s", "/styles/wallstyles"); 182
239 sprintf (stylefilepath, "%s/%s", styledirname, w_style); 183 maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
240 style_map = find_style (styledirname, w_style, -1);
241 if (!style_map) 184 if (!style_map)
242 return; 185 return;
243 186
244 /* fill up the map with the given floor style */ 187 maptile *mining_map = m_style && *m_style
188 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
189 : 0;
190
245 if ((the_wall = style_map->pick_random_object (rmg_rndm))) 191 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
246 { 192 {
247 int i, j; 193 // first pass, remove all "unreachable" (in-rock) walls
248 char *cp; 194 // and replace them by "blocked" spaces.
195
196 layout clean_maze (maze);
197
198 if (archetype *blocked = archetype::find ("blocked"))
199 for (int x = 0; x < maze.w; ++x)
200 for (int y = 0; y < maze.h; ++y)
201 if (maze [x][y] == '#' && !is_visible (maze, x, y))
202 {
203 clean_maze [x][y] = 'X';
204
205 // erase everything on this space ad replace it by blocked
206 mapspace &ms = map->at (x, y);
207
208 while (ms.top)
209 ms.top->destroy ();
210
211 map->insert (blocked->instance (), x, y, 0, INS_NO_MERGE | INS_NO_WALK_ON);
212 }
213
214 // second pass, add in walls
215
249 int joinedwalls = 0; 216 int joinedwalls = 0;
250 object *thiswall;
251 217
252 sprintf (RP->wall_name, "%s", &the_wall->arch->archname); 218 assign (RP->wall_name, the_wall->arch->archname);
253 if ((cp = strchr (RP->wall_name, '_')) != NULL) 219 if (char *cp = strchr (RP->wall_name, '_'))
254 { 220 {
255 *cp = 0; 221 *cp = 0;
256 joinedwalls = 1; 222 joinedwalls = 1;
257 } 223 }
258 224
259 for (i = 0; i < RP->Xsize; i++) 225 for (int i = 0; i < RP->Xsize; i++)
260 for (j = 0; j < RP->Ysize; j++) 226 for (int j = 0; j < RP->Ysize; j++)
261 {
262 if (layout[i][j] == '#') 227 if (clean_maze [i][j] == '#')
263 { 228 {
264 if (joinedwalls)
265 thiswall = pick_joined_wall (the_wall, layout, i, j, RP); 229 object *thiswall = joinedwalls ? pick_joined_wall (the_wall, clean_maze, i, j, RP)
266 else 230 : the_wall->arch->instance ();
267 thiswall = arch_to_object (the_wall->arch);
268 231
269 thiswall->x = i;
270 thiswall->y = j;
271 thiswall->move_block = MOVE_ALL; 232 thiswall->move_block = MOVE_ALL;
272 insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON); 233 map->insert (thiswall, i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
234
235 if (mining_map)
236 if (object *vein = mining_map->at (rmg_rndm (mining_map->width), rmg_rndm (mining_map->height)).bot)
237 map->insert (vein->clone (), i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
273 } 238 }
274 }
275 } 239 }
276} 240}
277 241
278/* this takes a map, and changes an existing wall to match what's blocked 242/* 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 243 * around it, counting only doors and walls as blocked. If insert_flag is
283 * global, previously-set variable, "wall_name" 247 * global, previously-set variable, "wall_name"
284 */ 248 */
285object * 249object *
286retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP) 250retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
287{ 251{
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; 252 int l;
295 object *the_wall = 0; 253 object *the_wall = 0;
296 object *new_wall = 0; 254 object *new_wall = 0;
297 archetype *wall_arch = 0; 255 archetype *wall_arch = 0;
298 256
299 /* first find the wall */ 257 /* first find the wall */
300 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall != NULL; the_wall = the_wall->above) 258 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above)
301 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER) 259 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
302 break; 260 break;
303
304 261
305 /* if what we found is a door, don't remove it, set the_wall to NULL to 262 /* if what we found is a door, don't remove it, set the_wall to NULL to
306 * signal that later. 263 * signal that later.
307 */ 264 */
308 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) 265 if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
325 RP->wall_name[l] = 0; 282 RP->wall_name[l] = 0;
326 break; 283 break;
327 } 284 }
328 } 285 }
329 286
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"); 287 strcat (RP->wall_name, "_");
338 break; 288 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
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 289
386 wall_arch = archetype::find (RP->wall_name); 290 wall_arch = archetype::find (RP->wall_name);
387 291
388 if (!wall_arch) 292 if (!wall_arch)
389 { 293 {
390 new_wall = arch_to_object (wall_arch); 294 new_wall = wall_arch->instance ();
391 new_wall->x = i; 295 new_wall->x = i;
392 new_wall->y = j; 296 new_wall->y = j;
393 297
394 if (the_wall && the_wall->map) 298 if (the_wall->map)
395 the_wall->destroy (); 299 the_wall->destroy ();
396 300
397 the_wall->move_block = MOVE_ALL; 301 the_wall->move_block = MOVE_ALL;
398 insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON); 302 insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
399 } 303 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines