1 |
elmex |
1.1 |
/* |
2 |
root |
1.24 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
pippijn |
1.16 |
* |
4 |
root |
1.34 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 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 |
pippijn |
1.16 |
* |
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 |
pippijn |
1.16 |
* |
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 |
root |
1.23 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
pippijn |
1.16 |
* GNU General Public License for more details. |
17 |
|
|
* |
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.23 |
* |
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 |
|
|
#include <random_map.h> |
27 |
|
|
#include <rproto.h> |
28 |
|
|
|
29 |
|
|
/* Put in the walls and autojoin them. */ |
30 |
|
|
|
31 |
root |
1.38 |
/* given a maze and a coordinate, tell me which squares up/down/right/left |
32 |
root |
1.2 |
are occupied. */ |
33 |
root |
1.4 |
int |
34 |
root |
1.38 |
surround_flag (const layout &maze, int i, int j) |
35 |
root |
1.4 |
{ |
36 |
elmex |
1.1 |
/* 1 = wall to left, |
37 |
root |
1.4 |
2 = wall to right, |
38 |
|
|
4 = wall above |
39 |
|
|
8 = wall below */ |
40 |
elmex |
1.1 |
int surround_index = 0; |
41 |
root |
1.4 |
|
42 |
root |
1.41 |
if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1; |
43 |
root |
1.38 |
if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2; |
44 |
root |
1.41 |
if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4; |
45 |
root |
1.38 |
if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8; |
46 |
root |
1.36 |
|
47 |
elmex |
1.1 |
return surround_index; |
48 |
|
|
} |
49 |
|
|
|
50 |
root |
1.37 |
/* like surround_flag, but only walls count. */ |
51 |
root |
1.4 |
int |
52 |
root |
1.38 |
surround_flag2 (const layout &maze, int i, int j) |
53 |
root |
1.4 |
{ |
54 |
elmex |
1.1 |
/* 1 = wall to left, |
55 |
root |
1.4 |
2 = wall to right, |
56 |
|
|
4 = wall above |
57 |
|
|
8 = wall below */ |
58 |
elmex |
1.1 |
int surround_index = 0; |
59 |
root |
1.4 |
|
60 |
root |
1.41 |
if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1; |
61 |
root |
1.38 |
if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2; |
62 |
root |
1.41 |
if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4; |
63 |
root |
1.38 |
if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8; |
64 |
root |
1.36 |
|
65 |
elmex |
1.1 |
return surround_index; |
66 |
|
|
} |
67 |
|
|
|
68 |
root |
1.38 |
/* like surround_flag, except it checks a map, not a maze. |
69 |
elmex |
1.1 |
* 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 |
root |
1.4 |
int |
74 |
root |
1.37 |
surround_flag3 (maptile *map, int i, int j) |
75 |
root |
1.4 |
{ |
76 |
|
|
/* |
77 |
root |
1.37 |
* 1 = blocked to left, |
78 |
root |
1.4 |
* 2 = blocked to right, |
79 |
|
|
* 4 = blocked above |
80 |
|
|
* 8 = blocked below |
81 |
|
|
*/ |
82 |
|
|
int surround_index = 0; |
83 |
elmex |
1.1 |
|
84 |
elmex |
1.20 |
// don't forget to update the mapspace! |
85 |
root |
1.37 |
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 |
root |
1.4 |
|
95 |
|
|
return surround_index; |
96 |
elmex |
1.1 |
} |
97 |
|
|
|
98 |
root |
1.38 |
/* like surround_flag2, except it checks a map, not a maze. */ |
99 |
root |
1.31 |
static int |
100 |
root |
1.37 |
surround_flag4 (maptile *map, int i, int j) |
101 |
root |
1.4 |
{ |
102 |
root |
1.37 |
/* 1 = blocked to left, |
103 |
root |
1.4 |
2 = blocked to right, |
104 |
|
|
4 = blocked above |
105 |
|
|
8 = blocked below */ |
106 |
elmex |
1.1 |
int surround_index = 0; |
107 |
|
|
|
108 |
root |
1.37 |
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 |
elmex |
1.1 |
|
113 |
|
|
return surround_index; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* picks the right wall type for this square, to make it look nice, |
117 |
root |
1.38 |
and have everything nicely joined. It uses the maze. */ |
118 |
root |
1.31 |
static object * |
119 |
root |
1.38 |
pick_joined_wall (object *the_wall, const layout &maze, int i, int j, random_map_params *RP) |
120 |
root |
1.4 |
{ |
121 |
elmex |
1.1 |
/* 1 = wall to left, |
122 |
root |
1.4 |
2 = wall to right, |
123 |
|
|
4 = wall above |
124 |
|
|
8 = wall below */ |
125 |
|
|
int surround_index = 0; |
126 |
elmex |
1.1 |
int l; |
127 |
root |
1.14 |
char wall_name[1024]; |
128 |
root |
1.4 |
archetype *wall_arch = 0; |
129 |
elmex |
1.1 |
|
130 |
root |
1.22 |
assign (wall_name, the_wall->arch->archname); |
131 |
elmex |
1.1 |
|
132 |
|
|
/* conventionally, walls are named like this: |
133 |
root |
1.4 |
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 |
elmex |
1.1 |
|
138 |
|
|
/* extract the wall name, which is the text up to the leading _ */ |
139 |
root |
1.4 |
for (l = 0; l < 64; l++) |
140 |
|
|
{ |
141 |
|
|
if (wall_name[l] == '_') |
142 |
|
|
{ |
143 |
root |
1.40 |
wall_name[l] = 0; |
144 |
root |
1.4 |
break; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
root |
1.40 |
strcat (wall_name, "_"); |
149 |
root |
1.39 |
strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]); |
150 |
root |
1.4 |
|
151 |
root |
1.5 |
wall_arch = archetype::find (wall_name); |
152 |
elmex |
1.1 |
|
153 |
root |
1.32 |
return wall_arch ? wall_arch->instance () : the_wall->arch->instance (); |
154 |
elmex |
1.1 |
} |
155 |
root |
1.4 |
|
156 |
root |
1.41 |
// checks whether the layout has a "reachable" wall at a givne point, i.e. |
157 |
|
|
// a wall that is not completely surrounded by walls. |
158 |
|
|
static bool inline |
159 |
|
|
is_visible (layout &maze, int i, int j) |
160 |
|
|
{ |
161 |
|
|
int cnt = 0; |
162 |
|
|
|
163 |
|
|
for (int dx = -1; dx <= 1; ++dx) |
164 |
|
|
for (int dy = -1; dy <= 1; ++dy) |
165 |
|
|
{ |
166 |
|
|
int x = i + dx; |
167 |
|
|
int y = j + dy; |
168 |
|
|
|
169 |
|
|
cnt += x < 0 || x >= maze.w |
170 |
|
|
|| y < 0 || y >= maze.h |
171 |
|
|
|| maze [x][y] == '#'; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
return cnt != 9; |
175 |
|
|
} |
176 |
|
|
|
177 |
root |
1.38 |
/* takes a map and a maze, and puts walls in the map (picked from |
178 |
root |
1.30 |
w_style) at '#' marks. */ |
179 |
|
|
void |
180 |
root |
1.38 |
make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP) |
181 |
root |
1.30 |
{ |
182 |
|
|
object *the_wall; |
183 |
|
|
|
184 |
|
|
/* get the style map */ |
185 |
|
|
if (!strcmp (w_style, "none")) |
186 |
|
|
return; |
187 |
root |
1.35 |
|
188 |
root |
1.36 |
maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty); |
189 |
root |
1.30 |
if (!style_map) |
190 |
|
|
return; |
191 |
|
|
|
192 |
root |
1.35 |
maptile *mining_map = m_style && *m_style |
193 |
root |
1.36 |
? find_style ("/styles/miningstyles", m_style, RP->difficulty) |
194 |
root |
1.35 |
: 0; |
195 |
|
|
|
196 |
root |
1.30 |
if ((the_wall = style_map->pick_random_object (rmg_rndm))) |
197 |
|
|
{ |
198 |
root |
1.41 |
// 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 |
root |
1.30 |
int joinedwalls = 0; |
222 |
|
|
|
223 |
root |
1.41 |
assign (RP->wall_name, the_wall->arch->archname); |
224 |
|
|
if (char *cp = strchr (RP->wall_name, '_')) |
225 |
root |
1.30 |
{ |
226 |
|
|
*cp = 0; |
227 |
|
|
joinedwalls = 1; |
228 |
|
|
} |
229 |
|
|
|
230 |
root |
1.41 |
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 |
root |
1.30 |
} |
245 |
|
|
} |
246 |
elmex |
1.1 |
|
247 |
|
|
/* this takes a map, and changes an existing wall to match what's blocked |
248 |
|
|
* around it, counting only doors and walls as blocked. If insert_flag is |
249 |
|
|
* 1, it will go ahead and insert the wall into the map. If not, it |
250 |
|
|
* will only return the wall which would belong there, and doesn't |
251 |
|
|
* remove anything. It depends on the |
252 |
|
|
* global, previously-set variable, "wall_name" |
253 |
|
|
*/ |
254 |
root |
1.4 |
object * |
255 |
root |
1.13 |
retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP) |
256 |
root |
1.4 |
{ |
257 |
|
|
/* 1 = wall to left, |
258 |
|
|
* 2 = wall to right, |
259 |
|
|
* 4 = wall above |
260 |
|
|
* 8 = wall below |
261 |
|
|
*/ |
262 |
|
|
int surround_index = 0; |
263 |
|
|
int l; |
264 |
|
|
object *the_wall = 0; |
265 |
|
|
object *new_wall = 0; |
266 |
|
|
archetype *wall_arch = 0; |
267 |
|
|
|
268 |
|
|
/* first find the wall */ |
269 |
root |
1.41 |
for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above) |
270 |
root |
1.4 |
if ((the_wall->move_type & MOVE_WALK) && the_wall->type != EXIT && the_wall->type != TELEPORTER) |
271 |
|
|
break; |
272 |
|
|
|
273 |
|
|
/* if what we found is a door, don't remove it, set the_wall to NULL to |
274 |
|
|
* signal that later. |
275 |
|
|
*/ |
276 |
|
|
if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR)) |
277 |
|
|
{ |
278 |
|
|
the_wall = NULL; |
279 |
|
|
/* if we're not supposed to insert a new wall where there wasn't one, |
280 |
|
|
* we've gotta leave. |
281 |
|
|
*/ |
282 |
|
|
if (insert_flag == 0) |
283 |
|
|
return 0; |
284 |
elmex |
1.1 |
} |
285 |
root |
1.4 |
else if (the_wall == NULL) |
286 |
|
|
return NULL; |
287 |
elmex |
1.1 |
|
288 |
root |
1.4 |
/* canonicalize the wall name */ |
289 |
|
|
for (l = 0; l < 64; l++) |
290 |
|
|
{ |
291 |
|
|
if (RP->wall_name[l] == '_') |
292 |
|
|
{ |
293 |
root |
1.40 |
RP->wall_name[l] = 0; |
294 |
root |
1.4 |
break; |
295 |
root |
1.2 |
} |
296 |
elmex |
1.1 |
} |
297 |
|
|
|
298 |
root |
1.40 |
strcat (RP->wall_name, "_"); |
299 |
root |
1.39 |
strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]); |
300 |
root |
1.11 |
|
301 |
root |
1.5 |
wall_arch = archetype::find (RP->wall_name); |
302 |
root |
1.11 |
|
303 |
|
|
if (!wall_arch) |
304 |
root |
1.4 |
{ |
305 |
root |
1.32 |
new_wall = wall_arch->instance (); |
306 |
root |
1.4 |
new_wall->x = i; |
307 |
|
|
new_wall->y = j; |
308 |
root |
1.11 |
|
309 |
root |
1.4 |
if (the_wall && the_wall->map) |
310 |
root |
1.28 |
the_wall->destroy (); |
311 |
root |
1.11 |
|
312 |
root |
1.4 |
the_wall->move_block = MOVE_ALL; |
313 |
|
|
insert_ob_in_map (new_wall, the_map, new_wall, INS_NO_MERGE | INS_NO_WALK_ON); |
314 |
elmex |
1.1 |
} |
315 |
root |
1.11 |
|
316 |
root |
1.4 |
return new_wall; |
317 |
elmex |
1.1 |
} |