ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/exit.C
Revision: 1.57
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.56: +1 -0 lines
Log Message:
copyright update 2018

File Contents

# User Rev Content
1 elmex 1.1 /*
2 root 1.28 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.54 *
4 root 1.57 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 root 1.55 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 root 1.38 * Copyright (©) 2001 Mark Wedel & Crossfire Development Team
7     * Copyright (©) 1992 Frank Tore Johansen
8 root 1.54 *
9 root 1.34 * 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.54 *
14 pippijn 1.23 * 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.27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 pippijn 1.23 * GNU General Public License for more details.
18 root 1.54 *
19 root 1.34 * 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.54 *
23 root 1.28 * The authors can be reached via e-mail to <support@deliantra.net>
24 pippijn 1.23 */
25 elmex 1.1
26     #include <global.h>
27 root 1.51 #include <rmg.h>
28 elmex 1.1 #include <sproto.h>
29     #include <rproto.h>
30    
31 root 1.45 /* find a character in the maze. fx and fy are pointers to
32 elmex 1.1 where to find the char. fx,fy = -1 if not found. */
33 root 1.35 static void
34 root 1.48 find_in_layout (int mode, char target, int &fx, int &fy, layout &maze)
35 root 1.4 {
36 elmex 1.1 int M;
37 root 1.4 int i, j;
38    
39 root 1.48 fx = -1;
40     fy = -1;
41 elmex 1.1
42     /* if a starting point isn't given, pick one */
43 root 1.4 if (mode < 1 || mode > 4)
44 root 1.30 M = rmg_rndm (4) + 1;
45 root 1.4 else
46     M = mode;
47 elmex 1.1
48     /* four different search starting points and methods so that
49     we can do something different for symmetrical maps instead of
50     the same damned thing every time. */
51 root 1.4 switch (M)
52     {
53 root 1.16 case 1:
54 root 1.30 /* search from top left down/right */
55 root 1.48 for (i = 1; i < maze.w; i++)
56     for (j = 1; j < maze.h; j++)
57 root 1.30 {
58 root 1.45 if (maze[i][j] == target)
59 root 1.30 {
60 root 1.48 fx = i;
61     fy = j;
62 root 1.30 return;
63     }
64     }
65     break;
66    
67 root 1.16 case 2:
68 root 1.30 /* Search from top right down/left */
69 root 1.48 for (i = maze.w - 2; i > 0; i--)
70     for (j = 1; j < maze.h - 1; j++)
71 root 1.30 {
72 root 1.45 if (maze[i][j] == target)
73 root 1.30 {
74 root 1.48 fx = i;
75     fy = j;
76 root 1.30 return;
77     }
78     }
79     break;
80    
81 root 1.16 case 3:
82 root 1.49 /* search from bottom-right up-left */
83     for (i = maze.w - 2; i > 0; i--)
84 root 1.48 for (j = maze.h - 2; j > 0; j--)
85 root 1.30 {
86 root 1.45 if (maze[i][j] == target)
87 root 1.30 {
88 root 1.48 fx = i;
89     fy = j;
90 root 1.30 return;
91     }
92     }
93     break;
94 root 1.49
95 root 1.16 case 4:
96 root 1.49 /* search from bottom-left up-right */
97     for (i = 1; i < maze.w - 1; i++)
98 root 1.48 for (j = maze.h - 2; j > 0; j--)
99 root 1.30 {
100 root 1.45 if (maze[i][j] == target)
101 root 1.30 {
102 root 1.48 fx = i;
103     fy = j;
104 root 1.30 return;
105     }
106     }
107     break;
108 root 1.4 }
109 elmex 1.1 }
110    
111 root 1.49 point
112     layout::find (char target, int mode)
113     {
114     int x, y;
115    
116     find_in_layout (mode, target, x, y, *this);
117    
118     return point (x, y);
119     }
120    
121 elmex 1.1 /* orientation: 0 means random,
122     1 means descending dungeon
123     2 means ascending dungeon
124     3 means rightward
125     4 means leftward
126     5 means northward
127     6 means southward
128     */
129 root 1.4 void
130 root 1.48 place_exits (maptile *map, layout &maze, const char *exitstyle, int orientation, random_map_params *RP)
131 root 1.4 {
132 root 1.16 maptile *style_map_down = 0; /* harder maze */
133     maptile *style_map_up = 0; /* easier maze */
134 elmex 1.1 object *the_exit_down; /* harder maze */
135     object *the_exit_up; /* easier maze */
136 root 1.4 int cx = -1, cy = -1; /* location of a map center */
137     int upx = -1, upy = -1; /* location of up exit */
138     int downx = -1, downy = -1;
139     int final_map_exit = 1;
140    
141 root 1.46 if (const char *eofm = RP->get_str ("exit_on_final_map", 0))
142     if (strstr (eofm, "no"))
143 root 1.16 final_map_exit = 0;
144 elmex 1.1
145 root 1.16 if (!orientation)
146 root 1.30 orientation = rmg_rndm (6) + 1;
147 elmex 1.1
148 root 1.4 switch (orientation)
149 elmex 1.1 {
150 root 1.15 case 1:
151     {
152 root 1.41 style_map_up = find_style ("/styles/exitstyles/up" , exitstyle, RP->difficulty);
153     style_map_down = find_style ("/styles/exitstyles/down", exitstyle, RP->difficulty);
154 root 1.15 break;
155     }
156    
157     case 2:
158     {
159 root 1.41 style_map_up = find_style ("/styles/exitstyles/down", exitstyle, RP->difficulty);
160     style_map_down = find_style ("/styles/exitstyles/up" , exitstyle, RP->difficulty);
161 root 1.15 break;
162     }
163    
164     default:
165     {
166 root 1.40 style_map_up =
167 root 1.41 style_map_down = find_style ("/styles/exitstyles/generic", exitstyle, RP->difficulty);
168 root 1.15 break;
169     }
170 elmex 1.1 }
171 root 1.15
172 root 1.30 the_exit_up = style_map_up
173     ? style_map_up->pick_random_object (rmg_rndm)->clone ()
174     : archetype::get (shstr_exit);
175 elmex 1.1
176 root 1.46 const char *final_map = RP->get_str ("final_map", 0);
177    
178 elmex 1.1 /* we need a down exit only if we're recursing. */
179 root 1.46 if (RP->dungeon_level < RP->dungeon_depth || final_map)
180 root 1.30 the_exit_down = style_map_down
181     ? style_map_down->pick_random_object (rmg_rndm)->clone ()
182     : archetype::get (shstr_exit);
183 root 1.4 else
184     the_exit_down = 0;
185 elmex 1.1
186     /* set up the up exit */
187 root 1.46 the_exit_up->stats.hp = RP->get_iv ("origin_x", -1);
188     the_exit_up->stats.sp = RP->get_iv ("origin_y", -1);
189     the_exit_up->slaying = RP->get_str ("origin_map", 0);
190 elmex 1.1
191     /* figure out where to put the entrance */
192     /* begin a logical block */
193     {
194     /* First, look for a '<' char */
195 root 1.48 find_in_layout (0, '<', upx, upy, maze);
196 elmex 1.1
197     /* next, look for a C, the map center. */
198 root 1.48 find_in_layout (0, 'C', cx, cy, maze);
199 root 1.4
200 elmex 1.1 /* if we didn't find an up, find an empty place far from the center */
201 root 1.4 if (upx == -1 && cx != -1)
202     {
203     if (cx > RP->Xsize / 2)
204     upx = 1;
205 elmex 1.1 else
206 root 1.4 upx = RP->Xsize - 2;
207 root 1.17
208 root 1.4 if (cy > RP->Ysize / 2)
209     upy = 1;
210     else
211     upy = RP->Ysize - 2;
212 root 1.17
213 root 1.4 /* find an empty place far from the center */
214 root 1.49 if (upx == 1 && upy == 1) find_in_layout (1, 0, upx, upy, maze);
215     else if (upx > 1 && upy == 1) find_in_layout (2, 0, upx, upy, maze);
216     else if (upx > 1 && upy > 1) find_in_layout (3, 0, upx, upy, maze);
217 root 1.50 else if (upx == 1 && upy > 1) find_in_layout (4, 0, upx, upy, maze);
218 root 1.4 }
219    
220 elmex 1.1 /* no indication of where to place the exit, so just place it. */
221 root 1.4 if (upx == -1)
222 root 1.48 find_in_layout (0, 0, upx, upy, maze);
223 elmex 1.1
224     the_exit_up->x = upx;
225     the_exit_up->y = upy;
226    
227     /* surround the exits with notices that this is a random map. */
228 root 1.17 for (int j = 1; j < 9; j++)
229 root 1.56 if (!wall_blocked (map, the_exit_up->x + DIRX (j), the_exit_up->y + DIRY (j)))
230 root 1.47 {
231     object *random_sign = archetype::get (shstr_sign);
232     random_sign->msg = format ("This is a random map.\nLevel: %d of %d.\n", RP->dungeon_level - 1, RP->dungeon_depth);
233 root 1.56 map->insert (random_sign, the_exit_up->x + DIRX (j), the_exit_up->y + DIRY (j), 0, 0);
234 root 1.47 }
235 root 1.17
236 elmex 1.1 /* Block the exit so things don't get dumped on top of it. */
237     the_exit_up->move_block = MOVE_ALL;
238    
239 root 1.4 insert_ob_in_map (the_exit_up, map, NULL, 0);
240     maze[the_exit_up->x][the_exit_up->y] = '<';
241 elmex 1.1
242     /* set the starting x,y for this map */
243 root 1.11 map->enter_x = the_exit_up->x;
244     map->enter_y = the_exit_up->y;
245 elmex 1.1
246     /* first, look for a '>' character */
247 root 1.48 find_in_layout (0, '>', downx, downy, maze);
248 root 1.30
249 elmex 1.1 /* if no > is found use C */
250 root 1.4 if (downx == -1)
251     {
252     downx = cx;
253     downy = cy;
254     };
255    
256 elmex 1.1 /* make the other exit far away from this one if
257     there's no center. */
258 root 1.4 if (downx == -1)
259     {
260     if (upx > RP->Xsize / 2)
261     downx = 1;
262 elmex 1.1 else
263 root 1.4 downx = RP->Xsize - 2;
264 root 1.17
265 root 1.4 if (upy > RP->Ysize / 2)
266     downy = 1;
267     else
268     downy = RP->Ysize - 2;
269 root 1.17
270 root 1.4 /* find an empty place far from the entrance */
271 root 1.49 if (downx == 1 && downy == 1) find_in_layout (1, 0, downx, downy, maze);
272     else if (downx > 1 && downy == 1) find_in_layout (2, 0, downx, downy, maze);
273     else if (downx > 1 && downy > 1) find_in_layout (3, 0, downx, downy, maze);
274 root 1.50 else if (downx == 1 && downy > 1) find_in_layout (4, 0, downx, downy, maze);
275 root 1.17 }
276 root 1.4
277 elmex 1.1 /* no indication of where to place the down exit, so just place it */
278 root 1.4 if (downx == -1)
279 root 1.48 find_in_layout (0, 0, downx, downy, maze);
280 root 1.16
281 root 1.4 if (the_exit_down)
282     {
283 root 1.42 int i = rmg_find_free_spot (the_exit_down, map, downx, downy, 1, SIZEOFFREE1 + 1);
284 root 1.49
285 root 1.56 the_exit_down->x = downx + DIRX (i);
286     the_exit_down->y = downy + DIRY (i);
287 root 1.49
288 root 1.46 RP->set ("origin_x", (IV)the_exit_down->x);
289     RP->set ("origin_y", (IV)the_exit_down->y);
290 root 1.43
291 root 1.49 the_exit_down->msg = RP->as_shstr ();
292     the_exit_down->slaying = shstr_random_map_exit;
293 root 1.16
294 root 1.4 /* the identifier for making a random map. */
295 root 1.46 if (RP->dungeon_level >= RP->dungeon_depth && final_map)
296 root 1.4 {
297 root 1.49 the_exit_down->msg = 0;
298     the_exit_down->slaying = final_map;
299 root 1.4
300 root 1.49 if (final_map_exit)
301     if (maptile *new_map = maptile::find_sync (final_map))
302     {
303     object *the_exit_back = the_exit_up->arch->instance ();
304 root 1.4
305 root 1.49 new_map->load_sync ();
306 root 1.19
307 root 1.49 for (object *tmp = new_map->at (new_map->enter_x, new_map->enter_y).bot; tmp; tmp = tmp->above)
308     /* Remove exit back to previous random map. There should only be one
309     * which is why we break out. To try to process more than one
310     * would require keeping a 'next' pointer, ad free_object kills tmp, which
311     * breaks the for loop.
312     */
313     if (tmp->type == EXIT && EXIT_PATH (tmp).starts_with ("?random/"))
314     {
315     tmp->destroy ();
316     break;
317     }
318    
319     /* setup the exit back */
320     the_exit_back->slaying = map->path;
321     the_exit_back->stats.hp = the_exit_down->x;
322     the_exit_back->stats.sp = the_exit_down->y;
323     the_exit_back->x = new_map->enter_x;
324     the_exit_back->y = new_map->enter_y;
325 root 1.4
326 root 1.49 insert_ob_in_map (the_exit_back, new_map, NULL, 0);
327 root 1.4 }
328     }
329 elmex 1.1
330 root 1.4 /* Block the exit so things don't get dumped on top of it. */
331     the_exit_down->move_block = MOVE_ALL;
332     insert_ob_in_map (the_exit_down, map, NULL, 0);
333     maze[the_exit_down->x][the_exit_down->y] = '>';
334 elmex 1.1 }
335 root 1.4 }
336 elmex 1.1 }
337    
338     /* this function unblocks the exits. We blocked them to
339     keep things from being dumped on them during the other
340     phases of random map generation. */
341 root 1.4 void
342 root 1.48 unblock_exits (maptile *map, layout &maze)
343 root 1.4 {
344     int i = 0, j = 0;
345 elmex 1.1 object *walk;
346    
347 root 1.48 for (i = 0; i < maze.w; i++)
348     for (j = 0; j < maze.h; j++)
349 root 1.4 if (maze[i][j] == '>' || maze[i][j] == '<')
350     {
351 root 1.10 for (walk = GET_MAP_OB (map, i, j); walk != NULL; walk = walk->above)
352 root 1.4 {
353     if (walk->move_block == MOVE_ALL && walk->type != LOCKED_DOOR)
354     {
355 elmex 1.26 walk->move_block = 0;
356 root 1.4 update_object (walk, UP_OBJ_CHANGE);
357     }
358     }
359 elmex 1.1 }
360     }
361 root 1.17