|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice) |
|
|
6 | * |
|
|
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
|
|
8 | * the terms of the Affero GNU General Public License as published by the |
|
|
9 | * Free Software Foundation, either version 3 of the License, or (at your |
|
|
10 | * option) any later version. |
|
|
11 | * |
|
|
12 | * This program is distributed in the hope that it will be useful, |
|
|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
15 | * GNU General Public License for more details. |
|
|
16 | * |
|
|
17 | * You should have received a copy of the Affero GNU General Public License |
|
|
18 | * and the GNU General Public License along with this program. If not, see |
|
|
19 | * <http://www.gnu.org/licenses/>. |
|
|
20 | * |
|
|
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
|
|
22 | */ |
1 | |
23 | |
2 | /* generate a rogue/nethack-like layout */ |
24 | /* generate a rogue/nethack-like maze */ |
3 | #include <global.h> |
25 | #include <global.h> |
4 | #include <random_map.h> |
|
|
5 | #include <math.h> |
26 | #include <rmg.h> |
|
|
27 | #include <rproto.h> |
6 | |
28 | |
7 | typedef struct |
29 | typedef struct |
8 | { |
30 | { |
9 | int x; |
31 | int x; |
10 | int y; /* coordinates of room centers */ |
32 | int y; /* coordinates of room centers */ |
… | |
… | |
16 | int rtype; /* circle or rectangular */ |
38 | int rtype; /* circle or rectangular */ |
17 | } Room; |
39 | } Room; |
18 | |
40 | |
19 | static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms); |
41 | static int roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms); |
20 | static void roguelike_make_rooms (Room *rooms, char **maze, int options); |
42 | static void roguelike_make_rooms (Room *rooms, char **maze, int options); |
21 | static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); |
43 | static void roguelike_link_rooms (Room *rooms, layout &maze); |
22 | |
44 | |
23 | int |
45 | int |
24 | surround_check (char **layout, int i, int j, int Xsize, int Ysize) |
46 | surround_check (layout &maze, int i, int j) |
25 | { |
47 | { |
26 | /* 1 = wall to left, |
48 | /* 1 = wall to left, |
27 | 2 = wall to right, |
49 | 2 = wall to right, |
28 | 4 = wall above |
50 | 4 = wall above |
29 | 8 = wall below */ |
51 | 8 = wall below */ |
30 | int surround_index = 0; |
52 | int surround_index = 0; |
31 | |
53 | |
32 | if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) |
54 | if ((i > 0) && (maze[i - 1][j] != 0 && maze[i - 1][j] != '.')) surround_index |= 1; |
33 | surround_index += 1; |
55 | if ((i < maze.w - 1) && (maze[i + 1][j] != 0 && maze[i + 1][j] != '.')) surround_index |= 2; |
34 | |
56 | if ((j > 0) && (maze[i][j - 1] != 0 && maze[i][j - 1] != '.')) surround_index |= 4; |
35 | if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) |
57 | if ((j < maze.h - 1) && (maze[i][j + 1] != 0 && maze[i][j + 1] != '.')) surround_index |= 8; |
36 | surround_index += 2; |
|
|
37 | |
|
|
38 | if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) |
|
|
39 | surround_index += 4; |
|
|
40 | |
|
|
41 | if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) |
|
|
42 | surround_index += 8; |
|
|
43 | |
58 | |
44 | return surround_index; |
59 | return surround_index; |
45 | } |
60 | } |
46 | |
61 | |
47 | /* actually make the layout: we work by a reduction process: |
62 | /* actually make the maze: we work by a reduction process: |
48 | * first we make everything a wall, then we remove areas to make rooms |
63 | * first we make everything a wall, then we remove areas to make rooms |
49 | */ |
64 | */ |
50 | void |
65 | void |
51 | roguelike_layout_gen (Layout maze, int options) |
66 | roguelike_layout_gen (layout &maze, int options) |
52 | { |
67 | { |
53 | int i, j; |
68 | int i, j; |
54 | Room *walk; |
69 | Room *walk; |
55 | int nrooms = 0; |
70 | int nrooms = 0; |
56 | int tries = 0; |
71 | int tries = 0; |
57 | |
72 | |
58 | int xsize = maze->w; |
73 | int xsize = maze.w; |
59 | int ysize = maze->h; |
74 | int ysize = maze.h; |
60 | |
75 | |
61 | /* minimum room size is basically 5x5: if xsize/ysize is |
76 | /* minimum room size is basically 5x5: if xsize/ysize is |
62 | less than 3x that then hollow things out, stick in |
77 | less than 3x that then hollow things out, stick in |
63 | a stairsup and stairs down, and exit */ |
78 | a stairsup and stairs down, and exit */ |
64 | if (xsize < 11 || ysize < 11) |
79 | if (xsize < 11 || ysize < 11) |
65 | { |
80 | { |
66 | maze->clear (); |
81 | maze.clear (); |
67 | maze->border (); |
82 | maze.border (); |
68 | |
83 | |
69 | maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
84 | maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
70 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
85 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
71 | |
86 | |
72 | return; |
87 | return; |
73 | } |
88 | } |
74 | |
89 | |
75 | maze->clear ('#'); |
90 | maze.fill ('#'); |
76 | |
91 | |
77 | /* decide on the number of rooms */ |
92 | /* decide on the number of rooms */ |
78 | nrooms = rndm (10) + 6; |
93 | nrooms = rmg_rndm (10) + 6; |
79 | Room *rooms = salloc0<Room> (nrooms + 1); |
94 | Room *rooms = salloc0<Room> (nrooms + 1); |
80 | |
95 | |
81 | /* actually place the rooms */ |
96 | /* actually place the rooms */ |
82 | i = 0; |
97 | i = 0; |
83 | while (tries < 450 && i < nrooms) |
98 | while (tries < 450 && i < nrooms) |
… | |
… | |
89 | i++; |
104 | i++; |
90 | } |
105 | } |
91 | |
106 | |
92 | if (i == 0) /* no can do! */ |
107 | if (i == 0) /* no can do! */ |
93 | { |
108 | { |
94 | maze->clear (); |
109 | maze.clear (); |
95 | maze->border (); |
110 | maze.border (); |
96 | |
111 | |
97 | maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
112 | maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
98 | maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
113 | maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
99 | |
114 | |
100 | sfree (rooms, nrooms + 1); |
115 | sfree (rooms, nrooms + 1); |
… | |
… | |
102 | } |
117 | } |
103 | |
118 | |
104 | /* erase the areas occupied by the rooms */ |
119 | /* erase the areas occupied by the rooms */ |
105 | roguelike_make_rooms (rooms, maze, options); |
120 | roguelike_make_rooms (rooms, maze, options); |
106 | |
121 | |
107 | roguelike_link_rooms (rooms, maze, xsize, ysize); |
122 | roguelike_link_rooms (rooms, maze); |
108 | |
123 | |
109 | /* put in the stairs */ |
124 | /* put in the stairs */ |
110 | |
125 | |
111 | maze[rooms->x][rooms->y] = '<'; |
126 | maze[rooms->x][rooms->y] = '<'; |
112 | |
127 | |
… | |
… | |
139 | if (maze[i][j] == '.') |
154 | if (maze[i][j] == '.') |
140 | maze[i][j] = 0; |
155 | maze[i][j] = 0; |
141 | |
156 | |
142 | if (maze[i][j] == 'D') |
157 | if (maze[i][j] == 'D') |
143 | { /* remove bad door. */ |
158 | { /* remove bad door. */ |
144 | int si = surround_check (maze, i, j, xsize, ysize); |
159 | int si = surround_check (maze, i, j); |
145 | |
160 | |
146 | if (si != 3 && si != 12) |
161 | if (si != 3 && si != 12) |
147 | { |
162 | { |
148 | maze[i][j] = 0; |
163 | maze[i][j] = 0; |
149 | /* back up and recheck any nearby doors */ |
164 | /* back up and recheck any nearby doors */ |
… | |
… | |
169 | |
184 | |
170 | /* decide on the base x and y sizes */ |
185 | /* decide on the base x and y sizes */ |
171 | x_basesize = xsize / isqrt (nrooms); |
186 | x_basesize = xsize / isqrt (nrooms); |
172 | y_basesize = ysize / isqrt (nrooms); |
187 | y_basesize = ysize / isqrt (nrooms); |
173 | |
188 | |
174 | tx = rndm (xsize); |
189 | tx = rmg_rndm (xsize); |
175 | ty = rndm (ysize); |
190 | ty = rmg_rndm (ysize); |
176 | |
191 | |
177 | /* generate a distribution of sizes centered about basesize */ |
192 | /* generate a distribution of sizes centered about basesize */ |
178 | sx = rndm (x_basesize) + rndm (x_basesize) + rndm (x_basesize); |
193 | sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); |
179 | sy = rndm (y_basesize) + rndm (y_basesize) + rndm (y_basesize); |
194 | sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); |
180 | sy = (int) (sy * .5); /* renormalize */ |
195 | sy >>= 1; /* renormalize */ |
181 | |
196 | |
182 | /* find the corners */ |
197 | /* find the corners */ |
183 | ax = tx - sx / 2; |
198 | ax = tx - sx / 2; |
184 | zx = tx + sx / 2 + sx % 2; |
199 | zx = tx + sx / 2 + sx % 2; |
185 | |
200 | |
… | |
… | |
205 | } |
220 | } |
206 | |
221 | |
207 | /* if we've got here, presumably the room is OK. */ |
222 | /* if we've got here, presumably the room is OK. */ |
208 | |
223 | |
209 | /* get a pointer to the first free room */ |
224 | /* get a pointer to the first free room */ |
210 | for (walk = rooms; walk->x != 0; walk++) |
225 | for (walk = rooms; walk->x; walk++) |
211 | ; |
226 | ; |
212 | |
227 | |
213 | walk->x = tx; |
228 | walk->x = tx; |
214 | walk->y = ty; |
229 | walk->y = ty; |
215 | walk->sx = sx; |
230 | walk->sx = sx; |
216 | walk->sy = sy; |
231 | walk->sy = sy; |
217 | walk->ax = ax; |
232 | walk->ax = ax; |
218 | walk->ay = ay; |
233 | walk->ay = ay; |
219 | walk->zx = zx; |
234 | walk->zx = zx; |
… | |
… | |
225 | /* write all the rooms into the maze */ |
240 | /* write all the rooms into the maze */ |
226 | static void |
241 | static void |
227 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
242 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
228 | { |
243 | { |
229 | int making_circle = 0; |
244 | int making_circle = 0; |
230 | int i, j; |
|
|
231 | int R; |
245 | int R; |
232 | Room *walk; |
246 | Room *walk; |
233 | |
247 | |
234 | for (walk = rooms; walk->x != 0; walk++) |
248 | for (walk = rooms; walk->x; walk++) |
235 | { |
249 | { |
236 | /* first decide what shape to make */ |
250 | /* first decide what shape to make */ |
237 | switch (options) |
251 | switch (options) |
238 | { |
252 | { |
239 | case 1: |
253 | case 1: |
… | |
… | |
241 | break; |
255 | break; |
242 | case 2: |
256 | case 2: |
243 | making_circle = 1; |
257 | making_circle = 1; |
244 | break; |
258 | break; |
245 | default: |
259 | default: |
246 | making_circle = ((rndm (3) == 0) ? 1 : 0); |
260 | making_circle = ((rmg_rndm (3) == 0) ? 1 : 0); |
247 | break; |
261 | break; |
248 | } |
262 | } |
249 | |
263 | |
250 | if (walk->sx < walk->sy) |
264 | if (walk->sx < walk->sy) |
251 | R = walk->sx / 2; |
265 | R = walk->sx / 2; |
252 | else |
266 | else |
253 | R = walk->sy / 2; |
267 | R = walk->sy / 2; |
254 | |
268 | |
255 | /* enscribe a rectangle */ |
269 | /* enscribe a rectangle */ |
256 | for (i = walk->ax; i < walk->zx; i++) |
270 | for (int i = walk->ax; i < walk->zx; i++) |
257 | for (j = walk->ay; j < walk->zy; j++) |
271 | for (int j = walk->ay; j < walk->zy; j++) |
258 | if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) |
272 | if (!making_circle || ((int) (0.5 + hypot (walk->x - i, walk->y - j))) <= R) |
259 | maze[i][j] = '.'; |
273 | maze[i][j] = '.'; |
260 | } |
274 | } |
261 | } |
275 | } |
262 | |
276 | |
263 | static void |
277 | static void |
264 | roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) |
278 | roguelike_link_rooms (Room *rooms, layout &maze) |
265 | { |
279 | { |
266 | Room *walk; |
280 | Room *walk; |
267 | int i, j; |
281 | int i, j; |
268 | |
282 | |
269 | /* link each room to the previous room */ |
283 | /* link each room to the previous room */ |
… | |
… | |
276 | int y1 = walk->y; |
290 | int y1 = walk->y; |
277 | int x2 = (walk - 1)->x; |
291 | int x2 = (walk - 1)->x; |
278 | int y2 = (walk - 1)->y; |
292 | int y2 = (walk - 1)->y; |
279 | int in_wall = 0; |
293 | int in_wall = 0; |
280 | |
294 | |
281 | if (rndm (2)) |
295 | if (rmg_rndm (2)) |
282 | { /* connect in x direction first */ |
296 | { /* connect in x direction first */ |
283 | /* horizontal connect */ |
297 | /* horizontal connect */ |
284 | /* swap (x1,y1) (x2,y2) if necessary */ |
298 | /* swap (x1,y1) (x2,y2) if necessary */ |
285 | |
299 | |
286 | if (x2 < x1) |
300 | if (x2 < x1) |
… | |
… | |
308 | maze[i - 1][j] = 'D'; |
322 | maze[i - 1][j] = 'D'; |
309 | } |
323 | } |
310 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
324 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
311 | maze[i][j] = 0; |
325 | maze[i][j] = 0; |
312 | } |
326 | } |
|
|
327 | |
313 | j = MIN (y1, y2); |
328 | j = min (y1, y2); |
|
|
329 | |
314 | if (maze[i][j] == '.') |
330 | if (maze[i][j] == '.') |
315 | in_wall = 0; |
331 | in_wall = 0; |
|
|
332 | |
316 | if (maze[i][j] == 0 || maze[i][j] == '#') |
333 | if (maze[i][j] == 0 || maze[i][j] == '#') |
317 | in_wall = 1; |
334 | in_wall = 1; |
|
|
335 | |
318 | for ( /* j set already */ ; j < MAX (y1, y2); j++) |
336 | for ( /* j set already */ ; j < max (y1, y2); j++) |
319 | { |
337 | { |
320 | if (in_wall == 0 && maze[i][j] == '#') |
338 | if (in_wall == 0 && maze[i][j] == '#') |
321 | { |
339 | { |
322 | in_wall = 1; |
340 | in_wall = 1; |
323 | maze[i][j] = 'D'; |
341 | maze[i][j] = 'D'; |
… | |
… | |
361 | } |
379 | } |
362 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
380 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
363 | maze[i][j] = 0; |
381 | maze[i][j] = 0; |
364 | } |
382 | } |
365 | |
383 | |
366 | i = MIN (x1, x2); |
384 | i = min (x1, x2); |
|
|
385 | |
367 | if (maze[i][j] == '.') |
386 | if (maze[i][j] == '.') |
368 | in_wall = 0; |
387 | in_wall = 0; |
|
|
388 | |
369 | if (maze[i][j] == 0 || maze[i][j] == '#') |
389 | if (maze[i][j] == 0 || maze[i][j] == '#') |
370 | in_wall = 1; |
390 | in_wall = 1; |
|
|
391 | |
371 | for ( /* i set already */ ; i < MAX (x1, x2); i++) |
392 | for ( /* i set already */ ; i < max (x1, x2); i++) |
372 | { |
393 | { |
373 | if (in_wall == 0 && maze[i][j] == '#') |
394 | if (in_wall == 0 && maze[i][j] == '#') |
374 | { |
395 | { |
375 | in_wall = 1; |
396 | in_wall = 1; |
376 | maze[i][j] = 'D'; |
397 | maze[i][j] = 'D'; |