ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.42
Committed: Fri Jul 2 17:41:24 2010 UTC (13 years, 11 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.41: +7 -8 lines
Log Message:
i was drunk

File Contents

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