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.23 by root, Sun Jul 1 05:00:19 2007 UTC vs.
Revision 1.44 by root, Sun Aug 22 20:23:07 2010 UTC

1/* 1/*
2 * This file is part of Crossfire TRT, the Roguelike Realtime MORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 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 * Crossfire TRT is free software: you can redistribute it and/or modify 8 * Deliantra is free software: you can redistribute it and/or modify it under
9 * 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
10 * the Free Software Foundation, either version 3 of the License, or 10 * Free Software Foundation, either version 3 of the License, or (at your
11 * (at your 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 GNU General Public License 18 * You should have received a copy of the Affero GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 19 * and the GNU General Public License along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
20 * 21 *
21 * The authors can be reached via e-mail to <crossfire@schmorp.de> 22 * The authors can be reached via e-mail to <support@deliantra.net>
22 */ 23 */
23 24
24#include <global.h> 25#include <global.h>
26#include <util.h>
25#include <random_map.h> 27#include <rmg.h>
26#include <rproto.h> 28#include <rproto.h>
27 29
28/* Put in the walls and autojoin them. */ 30/* Put in the walls and autojoin them. */
29 31
30
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. */
33
34int 34int
35surround_flag (char **layout, int i, int j, random_map_params *RP) 35surround_flag (const layout &maze, int i, int j)
36{ 36{
37 /* 1 = wall to left, 37 /* 1 = wall to left,
38 2 = wall to right, 38 2 = wall to right,
39 4 = wall above 39 4 = wall above
40 8 = wall below */ 40 8 = wall below */
41 int surround_index = 0; 41 int surround_index = 0;
42 42
43 if ((i > 0) && layout[i - 1][j] != 0) 43 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
44 surround_index |= 1; 44 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
45 if ((i < RP->Xsize - 1) && layout[i + 1][j] != 0) 45 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
46 surround_index |= 2; 46 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
47 if ((j > 0) && layout[i][j - 1] != 0) 47
48 surround_index |= 4;
49 if ((j < RP->Ysize - 1) && layout[i][j + 1] != 0)
50 surround_index |= 8;
51 return surround_index; 48 return surround_index;
52} 49}
53 50
54
55/* like surround_flag, but only walls count. 51/* like surround_flag, but only walls count. */
56 */
57
58int 52int
59surround_flag2 (char **layout, int i, int j, random_map_params *RP) 53surround_flag2 (const layout &maze, int i, int j)
60{ 54{
61 /* 1 = wall to left, 55 /* 1 = wall to left,
62 2 = wall to right, 56 2 = wall to right,
63 4 = wall above 57 4 = wall above
64 8 = wall below */ 58 8 = wall below */
65 int surround_index = 0; 59 int surround_index = 0;
66 60
67 if ((i > 0) && layout[i - 1][j] == '#') 61 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
68 surround_index |= 1; 62 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
69 if ((i < RP->Xsize - 1) && layout[i + 1][j] == '#') 63 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
70 surround_index |= 2; 64 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
71 if ((j > 0) && layout[i][j - 1] == '#') 65
72 surround_index |= 4;
73 if ((j < RP->Ysize - 1) && layout[i][j + 1] == '#')
74 surround_index |= 8;
75 return surround_index; 66 return surround_index;
76} 67}
77 68
78
79/* like surround_flag, except it checks a map, not a layout. 69/* like surround_flag, except it checks a map, not a maze.
80 * Since this is part of the random map code, presumption 70 * Since this is part of the random map code, presumption
81 * is that this is not a tiled map. 71 * is that this is not a tiled map.
82 * What is considered blocking and not is somewhat hard coded. 72 * What is considered blocking and not is somewhat hard coded.
83 */ 73 */
84int 74int
85surround_flag3 (maptile *map, sint16 i, sint16 j, random_map_params *RP) 75surround_flag3 (maptile *map, int i, int j)
86{ 76{
87 /* 77 /*
88 * 1 = blocked to left, 78 * 1 = blocked to left,
89 * 2 = blocked to right, 79 * 2 = blocked to right,
90 * 4 = blocked above 80 * 4 = blocked above
91 * 8 = blocked below 81 * 8 = blocked below
92 */ 82 */
93
94 int surround_index = 0; 83 int surround_index = 0;
95 84
96 // don't forget to update the mapspace! 85 // don't forget to update the mapspace!
97 if (i > 0) map->at (i - 1, j ).update (); 86 if (i > 0) map->at (i - 1, j ).update ();
98 if (i < RP->Xsize - 1) map->at (i + 1, j ).update (); 87 if (i < map->width - 1) map->at (i + 1, j ).update ();
99 if (j > 0) map->at (i , j - 1).update (); 88 if (j > 0) map->at (i , j - 1).update ();
100 if (j < RP->Ysize - 1) map->at (i , j + 1).update (); 89 if (j < map->height - 1) map->at (i , j + 1).update ();
101 90
102 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;
103 surround_index |= 1;
104 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;
105 surround_index |= 2;
106 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;
107 surround_index |= 4;
108 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;
109 surround_index |= 8;
110 95
111 return surround_index; 96 return surround_index;
112} 97}
113 98
114/* like surround_flag2, except it checks a map, not a layout. */ 99/* like surround_flag2, except it checks a map, not a maze. */
115 100static int
116int
117surround_flag4 (maptile *map, int i, int j, random_map_params *RP) 101surround_flag4 (maptile *map, int i, int j)
118{ 102{
119 /* 1 = blocked to left, 103 /* 1 = blocked to left,
120 2 = blocked to right, 104 2 = blocked to right,
121 4 = blocked above 105 4 = blocked above
122 8 = blocked below */ 106 8 = blocked below */
123 int surround_index = 0; 107 int surround_index = 0;
124 108
125 if ((i > 0) && wall_blocked (map, i - 1, j)) 109 if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1;
126 surround_index |= 1; 110 if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
127 if ((i < RP->Xsize - 1) && wall_blocked (map, i + 1, j)) 111 if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4;
128 surround_index |= 2; 112 if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
129 if ((j > 0) && wall_blocked (map, i, j - 1))
130 surround_index |= 4;
131 if ((j < RP->Ysize - 1) && wall_blocked (map, i, j + 1))
132 surround_index |= 8;
133 113
134 return surround_index; 114 return surround_index;
135} 115}
136 116
137/* takes a map and a layout, and puts walls in the map (picked from
138 w_style) at '#' marks. */
139
140void
141make_map_walls (maptile *map, char **layout, char *w_style, random_map_params *RP)
142{
143 char styledirname[1024];
144 char stylefilepath[1024];
145 maptile *style_map = 0;
146 object *the_wall;
147
148 /* get the style map */
149 if (!strcmp (w_style, "none"))
150 return;
151 sprintf (styledirname, "%s", "/styles/wallstyles");
152 sprintf (stylefilepath, "%s/%s", styledirname, w_style);
153 style_map = find_style (styledirname, w_style, -1);
154 if (!style_map)
155 return;
156
157 /* fill up the map with the given floor style */
158 if ((the_wall = style_map->pick_random_object ()))
159 {
160 int i, j;
161 char *cp;
162 int joinedwalls = 0;
163 object *thiswall;
164
165 sprintf (RP->wall_name, "%s", &the_wall->arch->archname);
166 if ((cp = strchr (RP->wall_name, '_')) != NULL)
167 {
168 *cp = 0;
169 joinedwalls = 1;
170 }
171
172 for (i = 0; i < RP->Xsize; i++)
173 for (j = 0; j < RP->Ysize; j++)
174 {
175 if (layout[i][j] == '#')
176 {
177 if (joinedwalls)
178 thiswall = pick_joined_wall (the_wall, layout, i, j, RP);
179 else
180 thiswall = arch_to_object (the_wall->arch);
181 thiswall->x = i;
182 thiswall->y = j;
183 thiswall->move_block = MOVE_ALL;
184 insert_ob_in_map (thiswall, map, thiswall, INS_NO_MERGE | INS_NO_WALK_ON);
185 }
186 }
187 }
188}
189
190
191/* 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,
192 and have everything nicely joined. It uses the layout. */ 118 and have everything nicely joined. It uses the maze. */
193 119static object *
194object *
195pick_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)
196{ 121{
197 /* 1 = wall to left, 122 /* 1 = wall to left,
198 2 = wall to right, 123 2 = wall to right,
199 4 = wall above 124 4 = wall above
200 8 = wall below */ 125 8 = wall below */
219 wall_name[l] = 0; 144 wall_name[l] = 0;
220 break; 145 break;
221 } 146 }
222 } 147 }
223 148
224 surround_index = surround_flag2 (layout, i, j, RP);
225
226 switch (surround_index)
227 {
228 case 0:
229 strcat (wall_name, "_0"); 149 strcat (wall_name, "_");
230 break; 150 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
231 case 1: 151
232 strcat (wall_name, "_1_3");
233 break;
234 case 2:
235 strcat (wall_name, "_1_4");
236 break;
237 case 3:
238 strcat (wall_name, "_2_1_2");
239 break;
240 case 4:
241 strcat (wall_name, "_1_2");
242 break;
243 case 5:
244 strcat (wall_name, "_2_2_4");
245 break;
246 case 6:
247 strcat (wall_name, "_2_2_1");
248 break;
249 case 7:
250 strcat (wall_name, "_3_1");
251 break;
252 case 8:
253 strcat (wall_name, "_1_1");
254 break;
255 case 9:
256 strcat (wall_name, "_2_2_3");
257 break;
258 case 10:
259 strcat (wall_name, "_2_2_2");
260 break;
261 case 11:
262 strcat (wall_name, "_3_3");
263 break;
264 case 12:
265 strcat (wall_name, "_2_1_1");
266 break;
267 case 13:
268 strcat (wall_name, "_3_4");
269 break;
270 case 14:
271 strcat (wall_name, "_3_2");
272 break;
273 case 15:
274 strcat (wall_name, "_4");
275 break;
276 }
277 wall_arch = archetype::find (wall_name); 152 wall_arch = archetype::find (wall_name);
278 153
279 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 ();
280} 155}
281 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}
282 246
283/* 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
284 * 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
285 * 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
286 * 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
287 * remove anything. It depends on the 251 * remove anything. It depends on the
288 * global, previously-set variable, "wall_name" 252 * global, previously-set variable, "wall_name"
289 */ 253 */
290
291object * 254object *
292retrofit_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)
293{ 256{
294 /* 1 = wall to left, 257 /* 1 = wall to left,
295 * 2 = wall to right, 258 * 2 = wall to right,
301 object *the_wall = 0; 264 object *the_wall = 0;
302 object *new_wall = 0; 265 object *new_wall = 0;
303 archetype *wall_arch = 0; 266 archetype *wall_arch = 0;
304 267
305 /* first find the wall */ 268 /* first find the wall */
306 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)
307 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)
308 break; 271 break;
309
310 272
311 /* 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
312 * signal that later. 274 * signal that later.
313 */ 275 */
314 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))
331 RP->wall_name[l] = 0; 293 RP->wall_name[l] = 0;
332 break; 294 break;
333 } 295 }
334 } 296 }
335 297
336 surround_index = surround_flag4 (the_map, i, j, RP);
337 /* This would be a lot cleaner to just us a lookup table,
338 * eg, wall_suffix[surround_index]
339 */
340 switch (surround_index)
341 {
342 case 0:
343 strcat (RP->wall_name, "_0"); 298 strcat (RP->wall_name, "_");
344 break; 299 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
345 case 1:
346 strcat (RP->wall_name, "_1_3");
347 break;
348 case 2:
349 strcat (RP->wall_name, "_1_4");
350 break;
351 case 3:
352 strcat (RP->wall_name, "_2_1_2");
353 break;
354 case 4:
355 strcat (RP->wall_name, "_1_2");
356 break;
357 case 5:
358 strcat (RP->wall_name, "_2_2_4");
359 break;
360 case 6:
361 strcat (RP->wall_name, "_2_2_1");
362 break;
363 case 7:
364 strcat (RP->wall_name, "_3_1");
365 break;
366 case 8:
367 strcat (RP->wall_name, "_1_1");
368 break;
369 case 9:
370 strcat (RP->wall_name, "_2_2_3");
371 break;
372 case 10:
373 strcat (RP->wall_name, "_2_2_2");
374 break;
375 case 11:
376 strcat (RP->wall_name, "_3_3");
377 break;
378 case 12:
379 strcat (RP->wall_name, "_2_1_1");
380 break;
381 case 13:
382 strcat (RP->wall_name, "_3_4");
383 break;
384 case 14:
385 strcat (RP->wall_name, "_3_2");
386 break;
387 case 15:
388 strcat (RP->wall_name, "_4");
389 break;
390 }
391 300
392 wall_arch = archetype::find (RP->wall_name); 301 wall_arch = archetype::find (RP->wall_name);
393 302
394 if (!wall_arch) 303 if (!wall_arch)
395 { 304 {
396 new_wall = arch_to_object (wall_arch); 305 new_wall = wall_arch->instance ();
397 new_wall->x = i; 306 new_wall->x = i;
398 new_wall->y = j; 307 new_wall->y = j;
399 308
400 if (the_wall && the_wall->map) 309 if (the_wall && the_wall->map)
401 {
402 the_wall->remove ();
403 the_wall->destroy (); 310 the_wall->destroy ();
404 }
405 311
406 the_wall->move_block = MOVE_ALL; 312 the_wall->move_block = MOVE_ALL;
407 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);
408 } 314 }
409 315

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines