ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/wall.C
Revision: 1.52
Committed: Sat Nov 17 23:40:02 2018 UTC (5 years, 6 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.51: +1 -0 lines
Log Message:
copyright update 2018

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.52 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.50 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.33 * Copyright (©) 2002 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.49 *
9 root 1.29 * 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 root 1.49 *
14 pippijn 1.16 * This program is distributed in the hope that it will be useful,
15     * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 root 1.23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 pippijn 1.16 * GNU General Public License for more details.
18 root 1.49 *
19 root 1.29 * 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 root 1.49 *
23 root 1.24 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.16 */
25 elmex 1.1
26     #include <global.h>
27 root 1.43 #include <util.h>
28 root 1.44 #include <rmg.h>
29 elmex 1.1 #include <rproto.h>
30    
31     /* Put in the walls and autojoin them. */
32    
33 root 1.38 /* given a maze and a coordinate, tell me which squares up/down/right/left
34 root 1.2 are occupied. */
35 root 1.4 int
36 root 1.38 surround_flag (const layout &maze, int i, int j)
37 root 1.4 {
38 elmex 1.1 /* 1 = wall to left,
39 root 1.4 2 = wall to right,
40     4 = wall above
41     8 = wall below */
42 elmex 1.1 int surround_index = 0;
43 root 1.4
44 root 1.41 if (i > 0 && maze[i - 1][j] != 0) surround_index |= 1;
45 root 1.38 if (i < maze.w - 1 && maze[i + 1][j] != 0) surround_index |= 2;
46 root 1.41 if (j > 0 && maze[i][j - 1] != 0) surround_index |= 4;
47 root 1.38 if (j < maze.h - 1 && maze[i][j + 1] != 0) surround_index |= 8;
48 root 1.36
49 elmex 1.1 return surround_index;
50     }
51    
52 root 1.37 /* like surround_flag, but only walls count. */
53 root 1.4 int
54 root 1.38 surround_flag2 (const layout &maze, int i, int j)
55 root 1.4 {
56 elmex 1.1 /* 1 = wall to left,
57 root 1.4 2 = wall to right,
58     4 = wall above
59     8 = wall below */
60 elmex 1.1 int surround_index = 0;
61 root 1.4
62 root 1.41 if (i > 0 && maze[i - 1][j] == '#') surround_index |= 1;
63 root 1.38 if (i < maze.w - 1 && maze[i + 1][j] == '#') surround_index |= 2;
64 root 1.41 if (j > 0 && maze[i][j - 1] == '#') surround_index |= 4;
65 root 1.38 if (j < maze.h - 1 && maze[i][j + 1] == '#') surround_index |= 8;
66 root 1.36
67 elmex 1.1 return surround_index;
68     }
69    
70 root 1.38 /* like surround_flag, except it checks a map, not a maze.
71 elmex 1.1 * 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 root 1.4 int
76 root 1.37 surround_flag3 (maptile *map, int i, int j)
77 root 1.4 {
78     /*
79 root 1.37 * 1 = blocked to left,
80 root 1.4 * 2 = blocked to right,
81     * 4 = blocked above
82 root 1.51 * 8 = blocked below
83 root 1.4 */
84     int surround_index = 0;
85 elmex 1.1
86 elmex 1.20 // don't forget to update the mapspace!
87 root 1.37 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 root 1.4
97     return surround_index;
98 elmex 1.1 }
99    
100 root 1.38 /* like surround_flag2, except it checks a map, not a maze. */
101 root 1.31 static int
102 root 1.37 surround_flag4 (maptile *map, int i, int j)
103 root 1.4 {
104 root 1.37 /* 1 = blocked to left,
105 root 1.4 2 = blocked to right,
106     4 = blocked above
107     8 = blocked below */
108 elmex 1.1 int surround_index = 0;
109    
110 root 1.37 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 elmex 1.1
115     return surround_index;
116     }
117    
118     /* picks the right wall type for this square, to make it look nice,
119 root 1.38 and have everything nicely joined. It uses the maze. */
120 root 1.31 static object *
121 root 1.38 pick_joined_wall (object *the_wall, const layout &maze, int i, int j, random_map_params *RP)
122 root 1.4 {
123 elmex 1.1 int l;
124 root 1.14 char wall_name[1024];
125 root 1.4 archetype *wall_arch = 0;
126 elmex 1.1
127 root 1.22 assign (wall_name, the_wall->arch->archname);
128 elmex 1.1
129     /* conventionally, walls are named like this:
130 root 1.4 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 elmex 1.1
135     /* extract the wall name, which is the text up to the leading _ */
136 root 1.4 for (l = 0; l < 64; l++)
137     {
138     if (wall_name[l] == '_')
139     {
140 root 1.40 wall_name[l] = 0;
141 root 1.4 break;
142     }
143     }
144    
145 root 1.40 strcat (wall_name, "_");
146 root 1.39 strcat (wall_name, wall_suffix [surround_flag2 (maze, i, j)]);
147 root 1.4
148 root 1.5 wall_arch = archetype::find (wall_name);
149 elmex 1.1
150 root 1.32 return wall_arch ? wall_arch->instance () : the_wall->arch->instance ();
151 elmex 1.1 }
152 root 1.4
153 root 1.42 // 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 root 1.41 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 root 1.43 if (IN_RANGE_EXC (x, 0, maze.w)
165     && IN_RANGE_EXC (y, 0, maze.h)
166 root 1.42 && maze [x][y] != '#')
167     return true;
168 root 1.41 }
169    
170 root 1.42 return false;
171 root 1.41 }
172    
173 root 1.38 /* takes a map and a maze, and puts walls in the map (picked from
174 root 1.30 w_style) at '#' marks. */
175     void
176 root 1.38 make_map_walls (maptile *map, layout &maze, const char *w_style, const char *m_style, random_map_params *RP)
177 root 1.30 {
178     object *the_wall;
179    
180     /* get the style map */
181     if (!strcmp (w_style, "none"))
182     return;
183 root 1.35
184 root 1.36 maptile *style_map = find_style ("/styles/wallstyles", w_style, RP->difficulty);
185 root 1.30 if (!style_map)
186     return;
187    
188 root 1.35 maptile *mining_map = m_style && *m_style
189 root 1.36 ? find_style ("/styles/miningstyles", m_style, RP->difficulty)
190 root 1.35 : 0;
191    
192 root 1.30 if ((the_wall = style_map->pick_random_object (rmg_rndm)))
193     {
194 root 1.41 // 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 root 1.30 int joinedwalls = 0;
218    
219 root 1.41 assign (RP->wall_name, the_wall->arch->archname);
220     if (char *cp = strchr (RP->wall_name, '_'))
221 root 1.30 {
222     *cp = 0;
223     joinedwalls = 1;
224     }
225    
226 root 1.41 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 root 1.30 }
241     }
242 elmex 1.1
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 root 1.51 * global, previously-set variable, "wall_name"
249 elmex 1.1 */
250 root 1.4 object *
251 root 1.13 retrofit_joined_wall (maptile *the_map, int i, int j, int insert_flag, random_map_params *RP)
252 root 1.4 {
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 root 1.41 for (the_wall = GET_MAP_OB (the_map, i, j); the_wall; the_wall = the_wall->above)
260 root 1.4 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 root 1.51 * signal that later.
265 root 1.4 */
266     if (the_wall && (the_wall->type == DOOR || the_wall->type == LOCKED_DOOR))
267     {
268     the_wall = NULL;
269 root 1.51 /* if we're not supposed to insert a new wall where there wasn't one,
270     * we've gotta leave.
271 root 1.4 */
272     if (insert_flag == 0)
273     return 0;
274 elmex 1.1 }
275 root 1.4 else if (the_wall == NULL)
276     return NULL;
277 elmex 1.1
278 root 1.4 /* canonicalize the wall name */
279     for (l = 0; l < 64; l++)
280     {
281     if (RP->wall_name[l] == '_')
282     {
283 root 1.40 RP->wall_name[l] = 0;
284 root 1.4 break;
285 root 1.2 }
286 elmex 1.1 }
287    
288 root 1.40 strcat (RP->wall_name, "_");
289 root 1.39 strcat (RP->wall_name, wall_suffix [surround_flag4 (the_map, i, j)]);
290 root 1.11
291 root 1.5 wall_arch = archetype::find (RP->wall_name);
292 root 1.11
293     if (!wall_arch)
294 root 1.4 {
295 root 1.32 new_wall = wall_arch->instance ();
296 root 1.4 new_wall->x = i;
297     new_wall->y = j;
298 root 1.11
299 root 1.48 if (the_wall->map)
300 root 1.28 the_wall->destroy ();
301 root 1.11
302 root 1.4 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 elmex 1.1 }
305 root 1.11
306 root 1.4 return new_wall;
307 elmex 1.1 }