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