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.36 by root, Sat Apr 10 01:54:07 2010 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,2009,2010 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 Mark Wedel & Crossfire Development Team 5 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6 * Copyright (©) 1992 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) surround_index |= 1; 43 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
43 if (i < RP->Xsize - 1 && layout[i + 1][j] != 0) surround_index |= 2; 44 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
44 if (j > 0 && layout[i][j - 1] != 0) surround_index |= 4; 45 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
45 if (j < RP->Ysize - 1 && layout[i][j + 1] != 0) surround_index |= 8; 46 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
46 47
47 return surround_index; 48 return surround_index;
48} 49}
49 50
50
51/* like surround_flag, but only walls count. 51/* like surround_flag, but only walls count. */
52 */
53
54int 52int
55surround_flag2 (char **layout, int i, int j, random_map_params *RP) 53surround_flag2 (const layout &maze, int i, int j)
56{ 54{
57 /* 1 = wall to left, 55 /* 1 = wall to left,
58 2 = wall to right, 56 2 = wall to right,
59 4 = wall above 57 4 = wall above
60 8 = wall below */ 58 8 = wall below */
61 int surround_index = 0; 59 int surround_index = 0;
62 60
63 if (i > 0 && layout[i - 1][j] == '#') surround_index |= 1; 61 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
64 if (i < RP->Xsize - 1 && layout[i + 1][j] == '#') surround_index |= 2; 62 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
65 if (j > 0 && layout[i][j - 1] == '#') surround_index |= 4; 63 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
66 if (j < RP->Ysize - 1 && layout[i][j + 1] == '#') surround_index |= 8; 64 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
67 65
68 return surround_index; 66 return surround_index;
69} 67}
70 68
71
72/* like surround_flag, except it checks a map, not a layout. 69/* like surround_flag, except it checks a map, not a maze.
73 * Since this is part of the random map code, presumption 70 * Since this is part of the random map code, presumption
74 * is that this is not a tiled map. 71 * is that this is not a tiled map.
75 * What is considered blocking and not is somewhat hard coded. 72 * What is considered blocking and not is somewhat hard coded.
76 */ 73 */
77int 74int
78surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP) 75surround_flag3 (maptile *map, int i, int j)
79{ 76{
80 /* 77 /*
81 * 1 = blocked to left, 78 * 1 = blocked to left,
82 * 2 = blocked to right, 79 * 2 = blocked to right,
83 * 4 = blocked above 80 * 4 = blocked above
84 * 8 = blocked below 81 * 8 = blocked below
85 */ 82 */
86
87 int surround_index = 0; 83 int surround_index = 0;
88 84
89 // don't forget to update the mapspace! 85 // don't forget to update the mapspace!
90 if (i > 0) map->at (i - 1, j ).update (); 86 if (i > 0) map->at (i - 1, j ).update ();
91 if (i < RP->Xsize - 1) map->at (i + 1, j ).update (); 87 if (i < map->width - 1) map->at (i + 1, j ).update ();
92 if (j > 0) map->at (i , j - 1).update (); 88 if (j > 0) map->at (i , j - 1).update ();
93 if (j < RP->Ysize - 1) map->at (i , j + 1).update (); 89 if (j < map->height - 1) map->at (i , j + 1).update ();
94 90
95 if (i > 0 && (GET_MAP_MOVE_BLOCK (map, i - 1, j) & MOVE_WALK)) surround_index |= 1; 91 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; 92 if (i < map->width - 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; 93 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; 94 if (j < map->height - 1 && (GET_MAP_MOVE_BLOCK (map, i, j + 1) & MOVE_WALK)) surround_index |= 8;
99 95
100 return surround_index; 96 return surround_index;
101} 97}
102 98
103/* like surround_flag2, except it checks a map, not a layout. */ 99/* like surround_flag2, except it checks a map, not a maze. */
104static int 100static int
105surround_flag4 (maptile *map, int i, int j, random_map_params *RP) 101surround_flag4 (maptile *map, int i, int j)
106{ 102{
107 /* 1 = blocked to left, 103 /* 1 = blocked to left,
108 2 = blocked to right, 104 2 = blocked to right,
109 4 = blocked above 105 4 = blocked above
110 8 = blocked below */ 106 8 = blocked below */
111 int surround_index = 0; 107 int surround_index = 0;
112 108
113 if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1; 109 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; 110 if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
115 if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4; 111 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; 112 if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
117 113
118 return surround_index; 114 return surround_index;
119} 115}
120 116
121/* 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,
122 and have everything nicely joined. It uses the layout. */ 118 and have everything nicely joined. It uses the maze. */
123static object * 119static object *
124pick_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)
125{ 121{
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; 122 int l;
132 char wall_name[1024]; 123 char wall_name[1024];
133 archetype *wall_arch = 0; 124 archetype *wall_arch = 0;
134 125
135 assign (wall_name, the_wall->arch->archname); 126 assign (wall_name, the_wall->arch->archname);
148 wall_name[l] = 0; 139 wall_name[l] = 0;
149 break; 140 break;
150 } 141 }
151 } 142 }
152 143
153 surround_index = surround_flag2 (layout, i, j, RP);
154
155 switch (surround_index)
156 {
157 case 0:
158 strcat (wall_name, "_0"); 144 strcat (wall_name, "_");
159 break; 145 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
160 case 1: 146
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); 147 wall_arch = archetype::find (wall_name);
207 148
208 return wall_arch ? wall_arch->instance () : the_wall->arch->instance (); 149 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
209} 150}
210 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
211/* 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
212 w_style) at '#' marks. */ 173 w_style) at '#' marks. */
213void 174void
214make_map_walls (maptile *map, char **layout, const char *w_style, const char *m_style, random_map_params *RP) 175make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
215{ 176{
216 object *the_wall; 177 object *the_wall;
217 178
218 /* get the style map */ 179 /* get the style map */
219 if (!strcmp (w_style, "none")) 180 if (!strcmp (w_style, "none"))
227 ? find_style ("/styles/miningstyles", m_style, RP->difficulty) 188 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
228 : 0; 189 : 0;
229 190
230 if ((the_wall = style_map->pick_random_object (rmg_rndm))) 191 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
231 { 192 {
232 int i, j; 193 // first pass, remove all "unreachable" (in-rock) walls
233 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
234 int joinedwalls = 0; 216 int joinedwalls = 0;
235 object *thiswall;
236 217
237 sprintf (RP->wall_name, "%s", &the_wall->arch->archname); 218 assign (RP->wall_name, the_wall->arch->archname);
238 if ((cp = strchr (RP->wall_name, '_')) != NULL) 219 if (char *cp = strchr (RP->wall_name, '_'))
239 { 220 {
240 *cp = 0; 221 *cp = 0;
241 joinedwalls = 1; 222 joinedwalls = 1;
242 } 223 }
243 224
244 for (i = 0; i < RP->Xsize; i++) 225 for (int i = 0; i < RP->Xsize; i++)
245 for (j = 0; j < RP->Ysize; j++) 226 for (int j = 0; j < RP->Ysize; j++)
246 {
247 if (layout[i][j] == '#') 227 if (clean_maze [i][j] == '#')
248 { 228 {
249 if (joinedwalls)
250 thiswall = pick_joined_wall (the_wall, layout, i, j, RP); 229 object *thiswall = joinedwalls ? pick_joined_wall (the_wall, clean_maze, i, j, RP)
251 else 230 : the_wall->arch->instance ();
252 thiswall = the_wall->arch->instance ();
253 231
254 thiswall->move_block = MOVE_ALL; 232 thiswall->move_block = MOVE_ALL;
255 map->insert (thiswall, i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON); 233 map->insert (thiswall, i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
256 234
257 if (mining_map) 235 if (mining_map)
258 if (object *vein = mining_map->at (rmg_rndm (mining_map->width), rmg_rndm (mining_map->height)).bot) 236 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); 237 map->insert (vein->clone (), i, j, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
260 } 238 }
261 }
262 } 239 }
263} 240}
264 241
265/* 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
266 * 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
270 * global, previously-set variable, "wall_name" 247 * global, previously-set variable, "wall_name"
271 */ 248 */
272object * 249object *
273retrofit_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)
274{ 251{
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; 252 int l;
282 object *the_wall = 0; 253 object *the_wall = 0;
283 object *new_wall = 0; 254 object *new_wall = 0;
284 archetype *wall_arch = 0; 255 archetype *wall_arch = 0;
285 256
286 /* first find the wall */ 257 /* first find the wall */
287 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)
288 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)
289 break; 260 break;
290
291 261
292 /* 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
293 * signal that later. 263 * signal that later.
294 */ 264 */
295 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))
312 RP->wall_name[l] = 0; 282 RP->wall_name[l] = 0;
313 break; 283 break;
314 } 284 }
315 } 285 }
316 286
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"); 287 strcat (RP->wall_name, "_");
325 break; 288 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
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 289
373 wall_arch = archetype::find (RP->wall_name); 290 wall_arch = archetype::find (RP->wall_name);
374 291
375 if (!wall_arch) 292 if (!wall_arch)
376 { 293 {
377 new_wall = wall_arch->instance (); 294 new_wall = wall_arch->instance ();
378 new_wall->x = i; 295 new_wall->x = i;
379 new_wall->y = j; 296 new_wall->y = j;
380 297
381 if (the_wall && the_wall->map) 298 if (the_wall->map)
382 the_wall->destroy (); 299 the_wall->destroy ();
383 300
384 the_wall->move_block = MOVE_ALL; 301 the_wall->move_block = MOVE_ALL;
385 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);
386 } 303 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines