|
|
1 | /* |
|
|
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
|
|
3 | * |
|
|
4 | * Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) 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 */ |
… | |
… | |
14 | int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */ |
36 | int ax, ay, zx, zy; /* coordinates of extrema of the rectangle */ |
15 | |
37 | |
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 | if ((i < Xsize - 1) && (layout[i + 1][j] != 0 && layout[i + 1][j] != '.')) |
56 | if ((j > 0) && (maze[i][j - 1] != 0 && maze[i][j - 1] != '.')) surround_index |= 4; |
35 | surround_index += 2; |
57 | if ((j < maze.h - 1) && (maze[i][j + 1] != 0 && maze[i][j + 1] != '.')) surround_index |= 8; |
36 | if ((j > 0) && (layout[i][j - 1] != 0 && layout[i][j - 1] != '.')) |
58 | |
37 | surround_index += 4; |
|
|
38 | if ((j < Ysize - 1) && (layout[i][j + 1] != 0 && layout[i][j + 1] != '.')) |
|
|
39 | surround_index += 8; |
|
|
40 | return surround_index; |
59 | return surround_index; |
41 | } |
60 | } |
42 | |
61 | |
43 | |
|
|
44 | /* actually make the layout: we work by a reduction process: |
62 | /* actually make the maze: we work by a reduction process: |
45 | * 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 |
46 | */ |
64 | */ |
47 | |
65 | void |
48 | char ** |
66 | roguelike_layout_gen (layout &maze, int options) |
49 | roguelike_layout_gen (int xsize, int ysize, int options) |
|
|
50 | { |
67 | { |
51 | int i, j; |
68 | int i, j; |
52 | Room *Rooms = 0; |
|
|
53 | Room *walk; |
69 | Room *walk; |
54 | int nrooms = 0; |
70 | int nrooms = 0; |
55 | int tries = 0; |
71 | int tries = 0; |
56 | |
72 | |
57 | /* allocate that array, write walls everywhere up */ |
73 | int xsize = maze.w; |
58 | char **maze = (char **) malloc (sizeof (char *) * xsize); |
74 | int ysize = maze.h; |
59 | |
|
|
60 | for (i = 0; i < xsize; i++) |
|
|
61 | { |
|
|
62 | maze[i] = (char *) malloc (sizeof (char) * ysize); |
|
|
63 | for (j = 0; j < ysize; j++) |
|
|
64 | maze[i][j] = '#'; |
|
|
65 | } |
|
|
66 | |
75 | |
67 | /* minimum room size is basically 5x5: if xsize/ysize is |
76 | /* minimum room size is basically 5x5: if xsize/ysize is |
68 | less than 3x that then hollow things out, stick in |
77 | less than 3x that then hollow things out, stick in |
69 | a stairsup and stairs down, and exit */ |
78 | a stairsup and stairs down, and exit */ |
70 | |
|
|
71 | if (xsize < 11 || ysize < 11) |
79 | if (xsize < 11 || ysize < 11) |
72 | { |
80 | { |
73 | for (i = 1; i < xsize - 1; i++) |
81 | maze.clear (); |
74 | for (j = 1; j < ysize - 1; j++) |
82 | maze.border (); |
75 | maze[i][j] = 0; |
83 | |
76 | maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; |
84 | maze[(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
77 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
85 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
|
|
86 | |
78 | return maze; |
87 | return; |
79 | } |
88 | } |
|
|
89 | |
|
|
90 | maze.fill ('#'); |
80 | |
91 | |
81 | /* decide on the number of rooms */ |
92 | /* decide on the number of rooms */ |
82 | nrooms = rndm (10) + 6; |
93 | nrooms = rmg_rndm (10) + 6; |
83 | Rooms = (Room *) calloc (nrooms + 1, sizeof (Room)); |
94 | Room *rooms = salloc0<Room> (nrooms + 1); |
84 | |
95 | |
85 | /* actually place the rooms */ |
96 | /* actually place the rooms */ |
86 | i = 0; |
97 | i = 0; |
87 | while (tries < 450 && i < nrooms) |
98 | while (tries < 450 && i < nrooms) |
88 | { |
99 | { |
89 | /* try to place the room */ |
100 | /* try to place the room */ |
90 | if (!roguelike_place_room (Rooms, xsize, ysize, nrooms)) |
101 | if (!roguelike_place_room (rooms, xsize, ysize, nrooms)) |
91 | tries++; |
102 | tries++; |
92 | else |
103 | else |
93 | i++; |
104 | i++; |
94 | } |
105 | } |
95 | |
106 | |
96 | if (i == 0) |
107 | if (i == 0) /* no can do! */ |
97 | { /* no can do! */ |
108 | { |
98 | for (i = 1; i < xsize - 1; i++) |
109 | maze.clear (); |
99 | for (j = 1; j < ysize - 1; j++) |
110 | maze.border (); |
100 | maze[i][j] = 0; |
111 | |
101 | maze[(xsize - 1) / 2][(ysize - 1) / 2] = '>'; |
112 | maze [(xsize - 1) / 2][(ysize - 1) / 2 ] = '>'; |
102 | maze[(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
113 | maze [(xsize - 1) / 2][(ysize - 1) / 2 + 1] = '<'; |
103 | free (Rooms); |
114 | |
|
|
115 | sfree (rooms, nrooms + 1); |
104 | return maze; |
116 | return; |
105 | } |
117 | } |
106 | |
|
|
107 | |
118 | |
108 | /* erase the areas occupied by the rooms */ |
119 | /* erase the areas occupied by the rooms */ |
109 | roguelike_make_rooms (Rooms, maze, options); |
120 | roguelike_make_rooms (rooms, maze, options); |
110 | |
121 | |
111 | roguelike_link_rooms (Rooms, maze, xsize, ysize); |
122 | roguelike_link_rooms (rooms, maze); |
112 | |
123 | |
113 | /* put in the stairs */ |
124 | /* put in the stairs */ |
114 | |
125 | |
115 | maze[Rooms->x][Rooms->y] = '<'; |
126 | maze[rooms->x][rooms->y] = '<'; |
|
|
127 | |
116 | /* get the last one */ |
128 | /* get the last one */ |
117 | for (walk = Rooms; walk->x != 0; walk++); |
129 | for (walk = rooms; walk->x != 0; walk++) |
|
|
130 | ; |
|
|
131 | |
118 | /* back up one */ |
132 | /* back up one */ |
119 | walk--; |
133 | walk--; |
|
|
134 | |
120 | if (walk == Rooms) |
135 | if (walk == rooms) |
121 | { |
136 | { |
122 | /* In this case, there is only a single room. We don't want to |
137 | /* In this case, there is only a single room. We don't want to |
123 | * clobber are up exit (above) with a down exit, so put the |
138 | * clobber are up exit (above) with a down exit, so put the |
124 | * other exit one space up/down, depending which is a space |
139 | * other exit one space up/down, depending which is a space |
125 | * and not a wall. |
140 | * and not a wall. |
… | |
… | |
136 | for (i = 0; i < xsize; i++) |
151 | for (i = 0; i < xsize; i++) |
137 | for (j = 0; j < ysize; j++) |
152 | for (j = 0; j < ysize; j++) |
138 | { |
153 | { |
139 | if (maze[i][j] == '.') |
154 | if (maze[i][j] == '.') |
140 | maze[i][j] = 0; |
155 | maze[i][j] = 0; |
|
|
156 | |
141 | if (maze[i][j] == 'D') |
157 | if (maze[i][j] == 'D') |
142 | { /* remove bad door. */ |
158 | { /* remove bad door. */ |
143 | int si = surround_check (maze, i, j, xsize, ysize); |
159 | int si = surround_check (maze, i, j); |
144 | |
160 | |
145 | if (si != 3 && si != 12) |
161 | if (si != 3 && si != 12) |
146 | { |
162 | { |
147 | maze[i][j] = 0; |
163 | maze[i][j] = 0; |
148 | /* back up and recheck any nearby doors */ |
164 | /* back up and recheck any nearby doors */ |
… | |
… | |
150 | j = 0; |
166 | j = 0; |
151 | } |
167 | } |
152 | } |
168 | } |
153 | } |
169 | } |
154 | |
170 | |
155 | free (Rooms); |
171 | sfree (rooms, nrooms + 1); |
156 | return maze; |
|
|
157 | } |
172 | } |
158 | |
173 | |
159 | |
|
|
160 | |
|
|
161 | static int |
174 | static int |
162 | roguelike_place_room (Room * Rooms, int xsize, int ysize, int nrooms) |
175 | roguelike_place_room (Room *rooms, int xsize, int ysize, int nrooms) |
163 | { |
176 | { |
164 | |
|
|
165 | int tx, ty; /* trial center locations */ |
177 | int tx, ty; /* trial center locations */ |
166 | int sx, sy; /* trial sizes */ |
178 | int sx, sy; /* trial sizes */ |
167 | int ax, ay; /* min coords of rect */ |
179 | int ax, ay; /* min coords of rect */ |
168 | int zx, zy; /* max coords of rect */ |
180 | int zx, zy; /* max coords of rect */ |
169 | int x_basesize; |
181 | int x_basesize; |
170 | int y_basesize; |
182 | int y_basesize; |
171 | Room *walk; |
183 | Room *walk; |
172 | |
184 | |
173 | /* decide on the base x and y sizes */ |
185 | /* decide on the base x and y sizes */ |
174 | |
|
|
175 | x_basesize = xsize / isqrt (nrooms); |
186 | x_basesize = xsize / isqrt (nrooms); |
176 | y_basesize = ysize / isqrt (nrooms); |
187 | y_basesize = ysize / isqrt (nrooms); |
177 | |
188 | |
178 | tx = rndm (xsize); |
189 | tx = rmg_rndm (xsize); |
179 | ty = rndm (ysize); |
190 | ty = rmg_rndm (ysize); |
180 | |
191 | |
181 | /* generate a distribution of sizes centered about basesize */ |
192 | /* generate a distribution of sizes centered about basesize */ |
182 | sx = rndm (x_basesize) + rndm (x_basesize) + rndm (x_basesize); |
193 | sx = rmg_rndm (x_basesize) + rmg_rndm (x_basesize) + rmg_rndm (x_basesize); |
183 | sy = rndm (y_basesize) + rndm (y_basesize) + rndm (y_basesize); |
194 | sy = rmg_rndm (y_basesize) + rmg_rndm (y_basesize) + rmg_rndm (y_basesize); |
184 | sy = (int) (sy * .5); /* renormalize */ |
195 | sy >>= 1; /* renormalize */ |
185 | |
196 | |
186 | /* find the corners */ |
197 | /* find the corners */ |
187 | ax = tx - sx / 2; |
198 | ax = tx - sx / 2; |
188 | zx = tx + sx / 2 + sx % 2; |
199 | zx = tx + sx / 2 + sx % 2; |
189 | |
200 | |
190 | ay = ty - sy / 2; |
201 | ay = ty - sy / 2; |
191 | zy = ty + sy / 2 + sy % 2; |
202 | zy = ty + sy / 2 + sy % 2; |
192 | |
203 | |
193 | /* check to see if it's in the map */ |
204 | /* check to see if it's in the map */ |
194 | if (zx > xsize - 1 || ax < 1) |
205 | if (zx > xsize - 1 || ax < 1) return 0; |
195 | return 0; |
|
|
196 | if (zy > ysize - 1 || ay < 1) |
206 | if (zy > ysize - 1 || ay < 1) return 0; |
197 | return 0; |
|
|
198 | |
207 | |
199 | /* no small fish */ |
208 | /* no small fish */ |
200 | if (sx < 3 || sy < 3) |
209 | if (sx < 3 || sy < 3) |
201 | return 0; |
210 | return 0; |
202 | |
211 | |
203 | /* check overlap with existing rooms */ |
212 | /* check overlap with existing rooms */ |
204 | for (walk = Rooms; walk->x != 0; walk++) |
213 | for (walk = rooms; walk->x != 0; walk++) |
205 | { |
214 | { |
206 | int dx = abs (tx - walk->x); |
215 | int dx = abs (tx - walk->x); |
207 | int dy = abs (ty - walk->y); |
216 | int dy = abs (ty - walk->y); |
208 | |
217 | |
209 | if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2)) |
218 | if ((dx < (walk->sx + sx) / 2 + 2) && (dy < (walk->sy + sy) / 2 + 2)) |
… | |
… | |
211 | } |
220 | } |
212 | |
221 | |
213 | /* if we've got here, presumably the room is OK. */ |
222 | /* if we've got here, presumably the room is OK. */ |
214 | |
223 | |
215 | /* get a pointer to the first free room */ |
224 | /* get a pointer to the first free room */ |
216 | for (walk = Rooms; walk->x != 0; walk++); |
225 | for (walk = rooms; walk->x; walk++) |
|
|
226 | ; |
|
|
227 | |
217 | walk->x = tx; |
228 | walk->x = tx; |
218 | walk->y = ty; |
229 | walk->y = ty; |
219 | walk->sx = sx; |
230 | walk->sx = sx; |
220 | walk->sy = sy; |
231 | walk->sy = sy; |
221 | walk->ax = ax; |
232 | walk->ax = ax; |
222 | walk->ay = ay; |
233 | walk->ay = ay; |
223 | walk->zx = zx; |
234 | walk->zx = zx; |
224 | walk->zy = zy; |
235 | walk->zy = zy; |
|
|
236 | |
225 | return 1; /* success */ |
237 | return 1; /* success */ |
226 | |
|
|
227 | } |
238 | } |
228 | |
|
|
229 | |
239 | |
230 | /* write all the rooms into the maze */ |
240 | /* write all the rooms into the maze */ |
231 | static void |
241 | static void |
232 | roguelike_make_rooms (Room * Rooms, char **maze, int options) |
242 | roguelike_make_rooms (Room *rooms, char **maze, int options) |
233 | { |
243 | { |
234 | int making_circle = 0; |
244 | int making_circle = 0; |
235 | int i, j; |
|
|
236 | int R; |
245 | int R; |
237 | Room *walk; |
246 | Room *walk; |
238 | |
247 | |
239 | for (walk = Rooms; walk->x != 0; walk++) |
248 | for (walk = rooms; walk->x; walk++) |
240 | { |
249 | { |
241 | /* first decide what shape to make */ |
250 | /* first decide what shape to make */ |
242 | switch (options) |
251 | switch (options) |
243 | { |
252 | { |
244 | case 1: |
253 | case 1: |
… | |
… | |
246 | break; |
255 | break; |
247 | case 2: |
256 | case 2: |
248 | making_circle = 1; |
257 | making_circle = 1; |
249 | break; |
258 | break; |
250 | default: |
259 | default: |
251 | making_circle = ((rndm (3) == 0) ? 1 : 0); |
260 | making_circle = ((rmg_rndm (3) == 0) ? 1 : 0); |
252 | break; |
261 | break; |
253 | } |
262 | } |
254 | |
263 | |
255 | if (walk->sx < walk->sy) |
264 | if (walk->sx < walk->sy) |
256 | R = walk->sx / 2; |
265 | R = walk->sx / 2; |
257 | else |
266 | else |
258 | R = walk->sy / 2; |
267 | R = walk->sy / 2; |
259 | |
268 | |
260 | /* enscribe a rectangle */ |
269 | /* enscribe a rectangle */ |
261 | for (i = walk->ax; i < walk->zx; i++) |
270 | for (int i = walk->ax; i < walk->zx; i++) |
262 | for (j = walk->ay; j < walk->zy; j++) |
271 | for (int j = walk->ay; j < walk->zy; j++) |
263 | { |
|
|
264 | 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) |
265 | maze[i][j] = '.'; |
273 | maze[i][j] = '.'; |
266 | } |
|
|
267 | } |
274 | } |
268 | } |
275 | } |
269 | |
276 | |
270 | |
|
|
271 | |
|
|
272 | static void |
277 | static void |
273 | roguelike_link_rooms (Room * Rooms, char **maze, int xsize, int ysize) |
278 | roguelike_link_rooms (Room *rooms, layout &maze) |
274 | { |
279 | { |
275 | Room *walk; |
280 | Room *walk; |
276 | int i, j; |
281 | int i, j; |
277 | |
282 | |
278 | /* link each room to the previous room */ |
283 | /* link each room to the previous room */ |
279 | if (Rooms[1].x == 0) |
284 | if (rooms[1].x == 0) |
280 | return; /* only 1 room */ |
285 | return; /* only 1 room */ |
281 | |
286 | |
282 | for (walk = Rooms + 1; walk->x != 0; walk++) |
287 | for (walk = rooms + 1; walk->x != 0; walk++) |
283 | { |
288 | { |
284 | int x1 = walk->x; |
289 | int x1 = walk->x; |
285 | int y1 = walk->y; |
290 | int y1 = walk->y; |
286 | int x2 = (walk - 1)->x; |
291 | int x2 = (walk - 1)->x; |
287 | int y2 = (walk - 1)->y; |
292 | int y2 = (walk - 1)->y; |
288 | int in_wall = 0; |
293 | int in_wall = 0; |
289 | |
294 | |
290 | if (rndm (2)) |
295 | if (rmg_rndm (2)) |
291 | { /* connect in x direction first */ |
296 | { /* connect in x direction first */ |
292 | /* horizontal connect */ |
297 | /* horizontal connect */ |
293 | /* swap (x1,y1) (x2,y2) if necessary */ |
298 | /* swap (x1,y1) (x2,y2) if necessary */ |
294 | |
299 | |
295 | if (x2 < x1) |
300 | if (x2 < x1) |
… | |
… | |
317 | maze[i - 1][j] = 'D'; |
322 | maze[i - 1][j] = 'D'; |
318 | } |
323 | } |
319 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
324 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
320 | maze[i][j] = 0; |
325 | maze[i][j] = 0; |
321 | } |
326 | } |
|
|
327 | |
322 | j = MIN (y1, y2); |
328 | j = min (y1, y2); |
|
|
329 | |
323 | if (maze[i][j] == '.') |
330 | if (maze[i][j] == '.') |
324 | in_wall = 0; |
331 | in_wall = 0; |
|
|
332 | |
325 | if (maze[i][j] == 0 || maze[i][j] == '#') |
333 | if (maze[i][j] == 0 || maze[i][j] == '#') |
326 | in_wall = 1; |
334 | in_wall = 1; |
|
|
335 | |
327 | for ( /* j set already */ ; j < MAX (y1, y2); j++) |
336 | for ( /* j set already */ ; j < max (y1, y2); j++) |
328 | { |
337 | { |
329 | if (in_wall == 0 && maze[i][j] == '#') |
338 | if (in_wall == 0 && maze[i][j] == '#') |
330 | { |
339 | { |
331 | in_wall = 1; |
340 | in_wall = 1; |
332 | maze[i][j] = 'D'; |
341 | maze[i][j] = 'D'; |
… | |
… | |
370 | } |
379 | } |
371 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
380 | else if (maze[i][j] != 'D' && maze[i][j] != '.') |
372 | maze[i][j] = 0; |
381 | maze[i][j] = 0; |
373 | } |
382 | } |
374 | |
383 | |
375 | i = MIN (x1, x2); |
384 | i = min (x1, x2); |
|
|
385 | |
376 | if (maze[i][j] == '.') |
386 | if (maze[i][j] == '.') |
377 | in_wall = 0; |
387 | in_wall = 0; |
|
|
388 | |
378 | if (maze[i][j] == 0 || maze[i][j] == '#') |
389 | if (maze[i][j] == 0 || maze[i][j] == '#') |
379 | in_wall = 1; |
390 | in_wall = 1; |
|
|
391 | |
380 | for ( /* i set already */ ; i < MAX (x1, x2); i++) |
392 | for ( /* i set already */ ; i < max (x1, x2); i++) |
381 | { |
393 | { |
382 | if (in_wall == 0 && maze[i][j] == '#') |
394 | if (in_wall == 0 && maze[i][j] == '#') |
383 | { |
395 | { |
384 | in_wall = 1; |
396 | in_wall = 1; |
385 | maze[i][j] = 'D'; |
397 | maze[i][j] = 'D'; |
… | |
… | |
396 | |
408 | |
397 | } |
409 | } |
398 | |
410 | |
399 | } |
411 | } |
400 | } |
412 | } |
|
|
413 | |