ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.51
Committed: Sun Jan 29 02:47:05 2017 UTC (7 years, 3 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.50: +5 -5 lines
Log Message:
remove eol whitespace

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.24 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.49 *
4 root 1.50 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.33 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
6     * Copyright (©) 1992 Frank Tore Johansen
7 root 1.49 *
8 root 1.29 * 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 root 1.49 *
13 pippijn 1.16 * This program is distributed in the hope that it will be useful,
14     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 root 1.23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 pippijn 1.16 * GNU General Public License for more details.
17 root 1.49 *
18 root 1.29 * 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 root 1.49 *
22 root 1.24 * The authors can be reached via e-mail to <support@deliantra.net>
23 pippijn 1.16 */
24 elmex 1.1
25     #include <global.h>
26 root 1.43 #include <util.h>
27 root 1.44 #include <rmg.h>
28 elmex 1.1 #include <rproto.h>
29    
30     /* Put in the walls and autojoin them. */
31    
32 root 1.38 /* given a maze and a coordinate, tell me which squares up/down/right/left
33 root 1.2 are occupied. */
34 root 1.4 int
35 root 1.38 surround_flag (const layout &maze, int i, int j)
36 root 1.4 {
37 elmex 1.1 /* 1 = wall to left,
38 root 1.4 2 = wall to right,
39     4 = wall above
40     8 = wall below */
41 elmex 1.1 int surround_index = 0;
42 root 1.4
43 root 1.41 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
44 root 1.38 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
45 root 1.41 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
46 root 1.38 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
47 root 1.36
48 elmex 1.1 return surround_index;
49     }
50    
51 root 1.37 /* like surround_flag, but only walls count. */
52 root 1.4 int
53 root 1.38 surround_flag2 (const layout &maze, int i, int j)
54 root 1.4 {
55 elmex 1.1 /* 1 = wall to left,
56 root 1.4 2 = wall to right,
57     4 = wall above
58     8 = wall below */
59 elmex 1.1 int surround_index = 0;
60 root 1.4
61 root 1.41 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
62 root 1.38 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
63 root 1.41 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
64 root 1.38 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
65 root 1.36
66 elmex 1.1 return surround_index;
67     }
68    
69 root 1.38 /* like surround_flag, except it checks a map, not a maze.
70 elmex 1.1 * Since this is part of the random map code, presumption
71     * is that this is not a tiled map.
72     * What is considered blocking and not is somewhat hard coded.
73     */
74 root 1.4 int
75 root 1.37 surround_flag3 (maptile *map, int i, int j)
76 root 1.4 {
77     /*
78 root 1.37 * 1 = blocked to left,
79 root 1.4 * 2 = blocked to right,
80     * 4 = blocked above
81 root 1.51 * 8 = blocked below
82 root 1.4 */
83     int surround_index = 0;
84 elmex 1.1
85 elmex 1.20 // don't forget to update the mapspace!
86 root 1.37 if (i > 0) map->at (i - 1, j ).update ();
87     if (i < map->width - 1) map->at (i + 1, j ).update ();
88     if (j > 0) map->at (i , j - 1).update ();
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;
95 root 1.4
96     return surround_index;
97 elmex 1.1 }
98    
99 root 1.38 /* like surround_flag2, except it checks a map, not a maze. */
100 root 1.31 static int
101 root 1.37 surround_flag4 (maptile *map, int i, int j)
102 root 1.4 {
103 root 1.37 /* 1 = blocked to left,
104 root 1.4 2 = blocked to right,
105     4 = blocked above
106     8 = blocked below */
107 elmex 1.1 int surround_index = 0;
108    
109 root 1.37 if (i > 0 && wall_blocked (map, i - 1, j)) surround_index |= 1;
110     if (i < map->width - 1 && wall_blocked (map, i + 1, j)) surround_index |= 2;
111     if (j > 0 && wall_blocked (map, i, j - 1)) surround_index |= 4;
112     if (j < map->height - 1 && wall_blocked (map, i, j + 1)) surround_index |= 8;
113 elmex 1.1
114     return surround_index;
115     }
116    
117     /* picks the right wall type for this square, to make it look nice,
118 root 1.38 and have everything nicely joined. It uses the maze. */
119 root 1.31 static object *
120 root 1.38 pick_joined_wall (object *the_wall, const layout &maze, int i, int j, random_map_params *RP)
121 root 1.4 {
122 elmex 1.1 int l;
123 root 1.14 char wall_name[1024];
124 root 1.4 archetype *wall_arch = 0;
125 elmex 1.1
126 root 1.22 assign (wall_name, the_wall->arch->archname);
127 elmex 1.1
128     /* conventionally, walls are named like this:
129 root 1.4 wallname_wallcode, where wallcode indicates
130     a joinedness, and wallname is the wall.
131     this code depends on the convention for
132     finding the right wall. */
133 elmex 1.1
134     /* extract the wall name, which is the text up to the leading _ */
135 root 1.4 for (l = 0; l < 64; l++)
136     {
137     if (wall_name[l] == '_')
138     {
139 root 1.40 wall_name[l] = 0;
140 root 1.4 break;
141     }
142     }
143    
144 root 1.40 strcat (wall_name, "_");
145 root 1.39 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
146 root 1.4
147 root 1.5 wall_arch = archetype::find (wall_name);
148 elmex 1.1
149 root 1.32 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
150 elmex 1.1 }
151 root 1.4
152 root 1.42 // checks whether the layout has a "reachable" space at a given point, i.e.
153     // not a wall that is completely surrounded by walls.
154 root 1.41 static bool inline
155     is_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 root 1.43 if (IN_RANGE_EXC (x, 0, maze.w)
164     && IN_RANGE_EXC (y, 0, maze.h)
165 root 1.42 && maze [x][y] != '#')
166     return true;
167 root 1.41 }
168    
169 root 1.42 return false;
170 root 1.41 }
171    
172 root 1.38 /* takes a map and a maze, and puts walls in the map (picked from
173 root 1.30 w_style) at '#' marks. */
174     void
175 root 1.38 make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
176 root 1.30 {
177     object *the_wall;
178    
179     /* get the style map */
180     if (!strcmp (w_style, "none"))
181     return;
182 root 1.35
183 root 1.36 maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
184 root 1.30 if (!style_map)
185     return;
186    
187 root 1.35 maptile *mining_map = m_style && *m_style
188 root 1.36 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
189 root 1.35 : 0;
190    
191 root 1.30 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
192     {
193 root 1.41 // first pass, remove all "unreachable" (in-rock) walls
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    
216 root 1.30 int joinedwalls = 0;
217    
218 root 1.41 assign (RP->wall_name, the_wall->arch->archname);
219     if (char *cp = strchr (RP->wall_name, '_'))
220 root 1.30 {
221     *cp = 0;
222     joinedwalls = 1;
223     }
224    
225 root 1.41 for (int i = 0; i < RP->Xsize; i++)
226     for (int j = 0; j < RP->Ysize; j++)
227     if (clean_maze [i][j] == '#')
228     {
229     object *thiswall = joinedwalls ? pick_joined_wall (the_wall, clean_maze, i, j, RP)
230     : the_wall->arch->instance ();
231    
232     thiswall->move_block = MOVE_ALL;
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);
238     }
239 root 1.30 }
240     }
241 elmex 1.1
242     /* this takes a map, and changes an existing wall to match what's blocked
243     * around it, counting only doors and walls as blocked. If insert_flag is
244     * 1, it will go ahead and insert the wall into the map. If not, it
245     * will only return the wall which would belong there, and doesn't
246     * remove anything. It depends on the
247 root 1.51 * global, previously-set variable, "wall_name"
248 elmex 1.1 */
249 root 1.4 object *
250 root 1.13 retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
251 root 1.4 {
252     int l;
253     object *the_wall = 0;
254     object *new_wall = 0;
255     archetype *wall_arch = 0;
256    
257     /* first find the wall */
258 root 1.41 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above)
259 root 1.4 if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER)
260     break;
261    
262     /* if what we found is a door, don't remove it, set the_wall to NULL to
263 root 1.51 * signal that later.
264 root 1.4 */
265     if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
266     {
267     the_wall = NULL;
268 root 1.51 /* if we're not supposed to insert a new wall where there wasn't one,
269     * we've gotta leave.
270 root 1.4 */
271     if (insert_flag == 0)
272     return 0;
273 elmex 1.1 }
274 root 1.4 else if (the_wall == NULL)
275     return NULL;
276 elmex 1.1
277 root 1.4 /* canonicalize the wall name */
278     for (l = 0; l < 64; l++)
279     {
280     if (RP->wall_name[l] == '_')
281     {
282 root 1.40 RP->wall_name[l] = 0;
283 root 1.4 break;
284 root 1.2 }
285 elmex 1.1 }
286    
287 root 1.40 strcat (RP->wall_name, "_");
288 root 1.39 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
289 root 1.11
290 root 1.5 wall_arch = archetype::find (RP->wall_name);
291 root 1.11
292     if (!wall_arch)
293 root 1.4 {
294 root 1.32 new_wall = wall_arch->instance ();
295 root 1.4 new_wall->x = i;
296     new_wall->y = j;
297 root 1.11
298 root 1.48 if (the_wall->map)
299 root 1.28 the_wall->destroy ();
300 root 1.11
301 root 1.4 the_wall->move_block = MOVE_ALL;
302     insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON);
303 elmex 1.1 }
304 root 1.11
305 root 1.4 return new_wall;
306 elmex 1.1 }