1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
5 | * Copyright (©) 1994-2004 Crossfire Development Team (restored, original file without copyright notice) |
6 | * |
6 | * |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
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 |
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 |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
10 | * option) any later version. |
10 | * option) any later version. |
11 | * |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
16 | * |
16 | * |
17 | * You should have received a copy of the Affero GNU General Public License |
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 |
18 | * and the GNU General Public License along with this program. If not, see |
19 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
20 | * |
20 | * |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
22 | */ |
22 | */ |
23 | |
23 | |
24 | /* generate a rogue/nethack-like layout */ |
24 | /* generate a rogue/nethack-like maze */ |
25 | #include <global.h> |
25 | #include <global.h> |
26 | #include <random_map.h> |
26 | #include <rmg.h> |
27 | #include <rproto.h> |
27 | #include <rproto.h> |
28 | |
28 | |
29 | typedef struct |
29 | typedef struct |
30 | { |
30 | { |
31 | int x; |
31 | int x; |
… | |
… | |
38 | int rtype; /* circle or rectangular */ |
38 | int rtype; /* circle or rectangular */ |
39 | } Room; |
39 | } Room; |
40 | |
40 | |
41 | 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); |
42 | static void roguelike_make_rooms (Room *rooms, char **maze, int options); |
42 | static void roguelike_make_rooms (Room *rooms, char **maze, int options); |
43 | static void roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize); |
43 | static void roguelike_link_rooms (Room *rooms, layout &maze); |
44 | |
44 | |
45 | int |
45 | int |
46 | surround_check (char **layout, int i, int j, int Xsize, int Ysize) |
46 | surround_check (layout &maze, int i, int j) |
47 | { |
47 | { |
48 | /* 1 = wall to left, |
48 | /* 1 = wall to left, |
49 | 2 = wall to right, |
49 | 2 = wall to right, |
50 | 4 = wall above |
50 | 4 = wall above |
51 | 8 = wall below */ |
51 | 8 = wall below */ |
52 | int surround_index = 0; |
52 | int surround_index = 0; |
53 | |
53 | |
54 | if ((i > 0) && (layout[i - 1][j] != 0 && layout[i - 1][j] != '.')) surround_index |= 1; |
54 | if ((i > 0) && (maze[i - 1][j] != 0 && maze[i - 1][j] != '.')) surround_index |= 1; |
55 | if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) surround_index |= 2; |
55 | if ((i < maze.w - 1) && (maze[i + 1][j] != 0 && maze[i + 1][j] != '.')) surround_index |= 2; |
56 | if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) surround_index |= 4; |
56 | if ((j > 0) && (maze[i][j - 1] != 0 && maze[i][j - 1] != '.')) surround_index |= 4; |
57 | if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) surround_index |= 8; |
57 | if ((j < maze.h - 1) && (maze[i][j + 1] != 0 && maze[i][j + 1] != '.')) surround_index |= 8; |
58 | |
58 | |
59 | return surround_index; |
59 | return surround_index; |
60 | } |
60 | } |
61 | |
61 | |
62 | /* actually make the layout: we work by a reduction process: |
62 | /* actually make the maze: we work by a reduction process: |
63 | * 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 |
64 | */ |
64 | */ |
65 | void |
65 | void |
66 | roguelike_layout_gen (Layout &maze, int options) |
66 | roguelike_layout_gen (layout &maze, int options) |
67 | { |
67 | { |
68 | int i, j; |
68 | int i, j; |
69 | Room *walk; |
69 | Room *walk; |
70 | int nrooms = 0; |
70 | int nrooms = 0; |
71 | int tries = 0; |
71 | int tries = 0; |
… | |
… | |
117 | } |
117 | } |
118 | |
118 | |
119 | /* erase the areas occupied by the rooms */ |
119 | /* erase the areas occupied by the rooms */ |
120 | roguelike_make_rooms (rooms, maze, options); |
120 | roguelike_make_rooms (rooms, maze, options); |
121 | |
121 | |
122 | roguelike_link_rooms (rooms, maze, xsize, ysize); |
122 | roguelike_link_rooms (rooms, maze); |
123 | |
123 | |
124 | /* put in the stairs */ |
124 | /* put in the stairs */ |
125 | |
125 | |
126 | maze[rooms->x][rooms->y] = '<'; |
126 | maze[rooms->x][rooms->y] = '<'; |
127 | |
127 | |
… | |
… | |
154 | if (maze[i][j] == '.') |
154 | if (maze[i][j] == '.') |
155 | maze[i][j] = 0; |
155 | maze[i][j] = 0; |
156 | |
156 | |
157 | if (maze[i][j] == 'D') |
157 | if (maze[i][j] == 'D') |
158 | { /* remove bad door. */ |
158 | { /* remove bad door. */ |
159 | int si = surround_check (maze, i, j, xsize, ysize); |
159 | int si = surround_check (maze, i, j); |
160 | |
160 | |
161 | if (si != 3 && si != 12) |
161 | if (si != 3 && si != 12) |
162 | { |
162 | { |
163 | maze[i][j] = 0; |
163 | maze[i][j] = 0; |
164 | /* back up and recheck any nearby doors */ |
164 | /* back up and recheck any nearby doors */ |
… | |
… | |
190 | ty = rmg_rndm (ysize); |
190 | ty = rmg_rndm (ysize); |
191 | |
191 | |
192 | /* generate a distribution of sizes centered about basesize */ |
192 | /* generate a distribution of sizes centered about basesize */ |
193 | sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); |
193 | sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); |
194 | sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); |
194 | sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); |
195 | sy = (int) (sy * .5); /* renormalize */ |
195 | sy >>= 1; /* renormalize */ |
196 | |
196 | |
197 | /* find the corners */ |
197 | /* find the corners */ |
198 | ax = tx - sx / 2; |
198 | ax = tx - sx / 2; |
199 | zx = tx + sx / 2 + sx % 2; |
199 | zx = tx + sx / 2 + sx % 2; |
200 | |
200 | |
… | |
… | |
220 | } |
220 | } |
221 | |
221 | |
222 | /* if we've got here, presumably the room is OK. */ |
222 | /* if we've got here, presumably the room is OK. */ |
223 | |
223 | |
224 | /* get a pointer to the first free room */ |
224 | /* get a pointer to the first free room */ |
225 | for (walk = rooms; walk->x != 0; walk++) |
225 | for (walk = rooms; walk->x; walk++) |
226 | ; |
226 | ; |
227 | |
227 | |
228 | walk->x = tx; |
228 | walk->x = tx; |
229 | walk->y = ty; |
229 | walk->y = ty; |
230 | walk->sx = sx; |
230 | walk->sx = sx; |
231 | walk->sy = sy; |
231 | walk->sy = sy; |
232 | walk->ax = ax; |
232 | walk->ax = ax; |
233 | walk->ay = ay; |
233 | walk->ay = ay; |
234 | walk->zx = zx; |
234 | walk->zx = zx; |
… | |
… | |
240 | /* write all the rooms into the maze */ |
240 | /* write all the rooms into the maze */ |
241 | static void |
241 | static void |
242 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
242 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
243 | { |
243 | { |
244 | int making_circle = 0; |
244 | int making_circle = 0; |
245 | int i, j; |
|
|
246 | int R; |
245 | int R; |
247 | Room *walk; |
246 | Room *walk; |
248 | |
247 | |
249 | for (walk = rooms; walk->x != 0; walk++) |
248 | for (walk = rooms; walk->x; walk++) |
250 | { |
249 | { |
251 | /* first decide what shape to make */ |
250 | /* first decide what shape to make */ |
252 | switch (options) |
251 | switch (options) |
253 | { |
252 | { |
254 | case 1: |
253 | case 1: |
… | |
… | |
266 | R = walk->sx / 2; |
265 | R = walk->sx / 2; |
267 | else |
266 | else |
268 | R = walk->sy / 2; |
267 | R = walk->sy / 2; |
269 | |
268 | |
270 | /* enscribe a rectangle */ |
269 | /* enscribe a rectangle */ |
271 | for (i = walk->ax; i < walk->zx; i++) |
270 | for (int i = walk->ax; i < walk->zx; i++) |
272 | for (j = walk->ay; j < walk->zy; j++) |
271 | for (int j = walk->ay; j < walk->zy; j++) |
273 | 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) |
274 | maze[i][j] = '.'; |
273 | maze[i][j] = '.'; |
275 | } |
274 | } |
276 | } |
275 | } |
277 | |
276 | |
278 | static void |
277 | static void |
279 | roguelike_link_rooms (Room *rooms, char **maze, int xsize, int ysize) |
278 | roguelike_link_rooms (Room *rooms, layout &maze) |
280 | { |
279 | { |
281 | Room *walk; |
280 | Room *walk; |
282 | int i, j; |
281 | int i, j; |
283 | |
282 | |
284 | /* link each room to the previous room */ |
283 | /* link each room to the previous room */ |