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 (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
4 | * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
|
|
5 | * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) |
5 | * |
6 | * |
6 | * 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 |
7 | * 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 |
8 | * 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 |
9 | * option) any later version. |
10 | * option) any later version. |
… | |
… | |
20 | * 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> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #include <global.h> |
24 | #include <global.h> |
24 | #include <random_map.h> |
25 | #include <random_map.h> |
|
|
26 | #include <rproto.h> |
25 | |
27 | |
|
|
28 | void |
26 | Layout::Layout (int w, int h) |
29 | layout::alloc (int w, int h) |
27 | : w(w), h(h) |
|
|
28 | { |
30 | { |
|
|
31 | assert (sizeof (cell) == 1); |
|
|
32 | |
|
|
33 | this->w = w; |
|
|
34 | this->h = h; |
|
|
35 | |
|
|
36 | // we store the layout in a single contiguous memory layout |
|
|
37 | // first part consists of pointers to each column, followed |
|
|
38 | // by the actual columns (not rows!) |
29 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
39 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
30 | |
40 | |
31 | col = (char **)salloc<char> (size); |
41 | data = (cell **)salloc<char> (size); |
32 | |
42 | |
33 | char *data = (char *)(col + w); |
43 | cell *p = (cell *)(data + w); |
34 | |
44 | |
35 | for (int x = w; x--; ) |
45 | for (int x = w; x--; ) |
36 | col [x] = data + x * h; |
46 | data [x] = p + x * h; |
37 | } |
47 | } |
38 | |
48 | |
|
|
49 | layout::layout (int w, int h) |
|
|
50 | { |
|
|
51 | alloc (w, h); |
|
|
52 | } |
|
|
53 | |
|
|
54 | layout::layout (layout ©) |
|
|
55 | { |
|
|
56 | alloc (copy.w, copy.h); |
|
|
57 | |
|
|
58 | memcpy (data [0], copy.data [0], sizeof (cell) * h * w); |
|
|
59 | } |
|
|
60 | |
39 | Layout::~Layout () |
61 | layout::~layout () |
40 | { |
62 | { |
41 | int size = (sizeof (char *) + sizeof (char) * h) * w; |
63 | int size = (sizeof (cell *) + sizeof (cell) * h) * w; |
42 | |
64 | |
43 | sfree ((char *)col, size); |
65 | sfree ((char *)data, size); |
44 | } |
66 | } |
45 | |
67 | |
46 | void |
68 | void |
47 | Layout::fill (char fill) |
69 | layout::fill (char fill) |
48 | { |
70 | { |
49 | memset (col [0], fill, w * h); |
71 | memset (data [0], fill, w * h); |
50 | } |
72 | } |
51 | |
73 | |
52 | void |
74 | void |
53 | Layout::rect (int x1, int y1, int x2, int y2, char fill) |
75 | layout::rect (int x1, int y1, int x2, int y2, char fill) |
|
|
76 | { |
|
|
77 | --x2; |
|
|
78 | |
|
|
79 | memset (data [x1] + y1, fill, y2 - y1); |
|
|
80 | memset (data [x2] + y1, fill, y2 - y1); |
|
|
81 | |
|
|
82 | while (++x1 < x2) |
|
|
83 | data [x1][y1] = data [x1][y2 - 1] = fill; |
|
|
84 | } |
|
|
85 | |
|
|
86 | void |
|
|
87 | layout::fill_rect (int x1, int y1, int x2, int y2, char fill) |
54 | { |
88 | { |
55 | for (; x1 < x2; ++x1) |
89 | for (; x1 < x2; ++x1) |
56 | memset (col [x1] + y1, fill, y2 - y1); |
90 | memset (data [x1] + y1, fill, y2 - y1); |
57 | } |
91 | } |
58 | |
92 | |
59 | void Layout::border (char fill) |
93 | void layout::border (char fill) |
60 | { |
94 | { |
61 | for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; |
95 | rect (0, 0, w, h, fill); |
62 | for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; |
|
|
63 | } |
96 | } |
64 | |
97 | |
65 | void |
98 | void |
66 | Layout::fill_rand (int percent) |
99 | layout::fill_rand (int percent) |
67 | { |
100 | { |
68 | percent = lerp (percent, 0, 100, 0, 256); |
101 | percent = lerp (percent, 0, 100, 0, 256); |
69 | |
102 | |
70 | for (int x = w - 1; --x > 0; ) |
103 | for (int x = w - 1; --x > 0; ) |
71 | for (int y = h - 1; --y > 0; ) |
104 | for (int y = h - 1; --y > 0; ) |
72 | col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
105 | data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; |
73 | } |
106 | } |
74 | |
107 | |
75 | ///////////////////////////////////////////////////////////////////////////// |
108 | ///////////////////////////////////////////////////////////////////////////// |
76 | |
109 | |
77 | // erode by cellular automata |
110 | // erode by cellular automata |
78 | void |
111 | void |
79 | Layout::erode_1_2 (int c1, int c2, int repeat) |
112 | layout::erode_1_2 (int c1, int c2, int repeat) |
80 | { |
113 | { |
81 | Layout neu (w, h); |
114 | layout neu (w, h); |
82 | |
115 | |
83 | while (repeat--) |
116 | while (repeat--) |
84 | { |
117 | { |
85 | for (int x = 0; x < w; ++x) |
118 | for (int x = 0; x < w; ++x) |
86 | { |
119 | { |
… | |
… | |
90 | { |
123 | { |
91 | int n1 = 0, n2 = 0; |
124 | int n1 = 0, n2 = 0; |
92 | |
125 | |
93 | // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2) |
126 | // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2) |
94 | static I8 dds[][3] = { |
127 | static I8 dds[][3] = { |
95 | { -2, -1, 0 }, |
128 | { -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 }, |
96 | { -2, 0, 0 }, |
129 | { -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 }, |
97 | { -2, 1, 0 }, |
130 | { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 }, |
98 | |
131 | { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 }, |
99 | { -1, -2, 0 }, |
132 | { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, |
100 | { -1, -1, 1 }, |
|
|
101 | { -1, 0, 1 }, |
|
|
102 | { -1, 1, 1 }, |
|
|
103 | { -1, 2, 0 }, |
|
|
104 | |
|
|
105 | { 0, -2, 0 }, |
|
|
106 | { 0, -1, 1 }, |
|
|
107 | { 0, 0, 1 }, |
|
|
108 | { 0, 1, 1 }, |
|
|
109 | { 0, 2, 0 }, |
|
|
110 | |
|
|
111 | { 1, -2, 0 }, |
|
|
112 | { 1, -1, 1 }, |
|
|
113 | { 1, 0, 1 }, |
|
|
114 | { 1, 1, 1 }, |
|
|
115 | { 1, 2, 0 }, |
|
|
116 | |
|
|
117 | { 2, -1, 0 }, |
|
|
118 | { 2, 0, 0 }, |
|
|
119 | { 2, 1, 0 }, |
|
|
120 | }; |
133 | }; |
121 | |
134 | |
122 | for (int i = array_length (dds); i--; ) |
135 | for (int i = array_length (dds); i--; ) |
123 | { |
136 | { |
124 | int nx = x + dds [i][0]; |
137 | int nx = x + dds [i][0]; |
125 | int ny = y + dds [i][1]; |
138 | int ny = y + dds [i][1]; |
126 | |
139 | |
127 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) |
140 | if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) |
128 | { |
141 | { |
129 | n1 += dds [i][2]; |
142 | n1 += dds [i][2]; |
130 | n2++; |
143 | n2++; |
131 | } |
144 | } |
132 | } |
145 | } |
… | |
… | |
140 | } |
153 | } |
141 | |
154 | |
142 | ///////////////////////////////////////////////////////////////////////////// |
155 | ///////////////////////////////////////////////////////////////////////////// |
143 | |
156 | |
144 | void |
157 | void |
145 | Layout::print () |
158 | layout::print () const |
146 | { |
159 | { |
147 | for (int y = 0; y < h; y++) |
160 | for (int y = 0; y < h; y++) |
148 | { |
161 | { |
149 | for (int x = 0; x < w; x++) |
162 | for (int x = 0; x < w; x++) |
150 | { |
163 | { |
151 | U8 c = (U8)col [x][y]; |
164 | U8 c = (U8)data [x][y]; |
152 | |
165 | |
153 | if (!c) |
166 | if (!c) |
154 | c = ' '; |
167 | c = ' '; |
155 | else if (c < 10) |
168 | else if (c < 10) |
156 | c += '0'; |
169 | c += '0'; |
… | |
… | |
169 | ///////////////////////////////////////////////////////////////////////////// |
182 | ///////////////////////////////////////////////////////////////////////////// |
170 | // isolation remover - ensures single connected area |
183 | // isolation remover - ensures single connected area |
171 | |
184 | |
172 | typedef fixed_stack<point> pointlist; |
185 | typedef fixed_stack<point> pointlist; |
173 | |
186 | |
174 | static noinline void |
187 | static void noinline |
175 | push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) |
188 | push_flood_fill (layout &dist, pointlist &seeds, int x, int y) |
176 | { |
189 | { |
177 | if (dist [x][y]) |
190 | if (dist [x][y]) |
178 | return; |
191 | return; |
179 | |
192 | |
180 | while (y > 0 && !dist [x][y - 1]) |
193 | while (y > 0 && !dist [x][y - 1]) |
… | |
… | |
190 | ++y; |
203 | ++y; |
191 | } |
204 | } |
192 | |
205 | |
193 | while (--y >= y0) |
206 | while (--y >= y0) |
194 | { |
207 | { |
195 | if (x > 0) push_flood_fill (dist, seeds, x - 1, y); |
208 | if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); |
196 | if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); |
209 | if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); |
197 | } |
210 | } |
198 | } |
211 | } |
199 | |
212 | |
200 | static inline void |
213 | static inline void |
201 | make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
214 | make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) |
202 | { |
215 | { |
203 | for (;;) |
216 | for (;;) |
204 | { |
217 | { |
|
|
218 | point neigh[4]; |
|
|
219 | int ncnt = 0; |
|
|
220 | |
205 | if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) |
221 | if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); |
206 | --x; |
|
|
207 | else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) |
222 | if (x < dist.w - 1 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); |
208 | ++x; |
|
|
209 | else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) |
223 | if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); |
210 | --y; |
|
|
211 | else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) |
224 | if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); |
212 | ++y; |
225 | |
213 | else |
226 | if (!ncnt) |
214 | break; |
227 | return; |
|
|
228 | |
|
|
229 | point &p = neigh [rmg_rndm (ncnt)]; |
|
|
230 | |
|
|
231 | seeds.push (p); |
|
|
232 | |
|
|
233 | x = p.x; |
|
|
234 | y = p.y; |
215 | |
235 | |
216 | d = dist [x][y]; |
236 | d = dist [x][y]; |
217 | dist [x][y] = 1; |
237 | dist [x][y] = 1; |
218 | seeds.push (point (x, y)); |
|
|
219 | } |
238 | } |
220 | } |
239 | } |
221 | |
240 | |
222 | static void inline |
241 | static void inline |
223 | maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) |
242 | maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) |
224 | { |
243 | { |
225 | char &D = dist [x][y]; |
244 | char &D = dist [x][y]; |
226 | |
245 | |
227 | if (U8 (D) > d) // if wall and higher distance, lower distance |
246 | if (U8 (D) > d) // if wall and higher distance, lower distance |
228 | D = d; |
247 | D = d; |
… | |
… | |
230 | return; |
249 | return; |
231 | |
250 | |
232 | seeds.push (point (x, y)); |
251 | seeds.push (point (x, y)); |
233 | } |
252 | } |
234 | |
253 | |
235 | static void |
254 | // isolation remover, works on a "distance" map |
|
|
255 | // the map must be initialised with 0 == rooms, 255 = walls |
|
|
256 | static void noinline |
236 | isolation_remover (Layout &maze, bool dirty) |
257 | isolation_remover (layout &dist) |
237 | { |
258 | { |
238 | Layout dist (maze.w, maze.h); |
|
|
239 | |
|
|
240 | // dist contains |
259 | // dist contains |
241 | // 0 == invisited rooms |
260 | // 0 == invisited rooms |
242 | // 1 == visited rooms |
261 | // 1 == visited rooms |
243 | // 2+ shortest distance to random near room |
262 | // 2+ shortest distance to random near room |
244 | |
263 | |
245 | // phase 1, initialise dist array, find seed |
264 | // phase 1, find seed |
246 | int cnt = 0; |
265 | int cnt = 0; |
247 | int x, y; |
266 | int x, y; |
248 | |
267 | |
249 | for (int i = 0; i < maze.w; ++i) |
268 | for (int i = 0; i < dist.w; ++i) |
250 | for (int j = 0; j < maze.h; ++j) |
269 | for (int j = 0; j < dist.h; ++j) |
251 | { |
270 | if (!dist [i][j] && !rmg_rndm (++cnt)) |
252 | if (maze [i][j] == '#') |
|
|
253 | dist [i][j] = U8 (255); |
|
|
254 | else |
|
|
255 | { |
|
|
256 | dist [i][j] = 0; |
|
|
257 | if (!rmg_rndm (++cnt)) |
|
|
258 | x = i, y = j; |
271 | x = i, y = j; |
259 | } |
|
|
260 | } |
|
|
261 | |
272 | |
262 | if (!cnt) |
273 | if (!cnt) |
|
|
274 | { |
|
|
275 | // map is completely massive, this is not good, |
|
|
276 | // so make it empty instead. |
|
|
277 | dist.fill (1); |
263 | return; |
278 | return; |
|
|
279 | } |
264 | |
280 | |
265 | fixed_stack<point> seeds (maze.w * maze.h * 5); |
281 | fixed_stack<point> seeds (dist.w * dist.h * 5); |
266 | |
282 | |
267 | // found first free space - picking the first one gives |
283 | // found first free space - picking the first one gives |
268 | // us a slight bias for tunnels, but usually you won't |
284 | // us a slight bias for tunnels, but usually you won't |
269 | // notice that in-game |
285 | // notice that in-game |
270 | seeds.push (point (x, y)); |
286 | seeds.push (point (x, y)); |
… | |
… | |
282 | y = p.y; |
298 | y = p.y; |
283 | |
299 | |
284 | if (!dist [x][y]) |
300 | if (!dist [x][y]) |
285 | { |
301 | { |
286 | // found new isolated area, make tunnel |
302 | // found new isolated area, make tunnel |
287 | if (!dirty) |
|
|
288 | push_flood_fill (dist, seeds, x, y); |
303 | push_flood_fill (dist, seeds, x, y); |
289 | |
|
|
290 | make_tunnel (dist, seeds, x, y, 255); |
304 | make_tunnel (dist, seeds, x, y, 255); |
291 | |
|
|
292 | if (dirty) |
|
|
293 | push_flood_fill (dist, seeds, x, y); |
|
|
294 | } |
305 | } |
295 | else |
306 | else |
296 | { |
307 | { |
297 | // nothing here, continue to expand |
308 | // nothing here, continue to expand |
298 | U8 d = U8 (dist [x][y]) + 1; |
309 | U8 d = U8 (dist [x][y]) + 1; |
… | |
… | |
301 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
312 | if (x > 0) maybe_push (dist, seeds, x - 1, y, d); |
302 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
313 | if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); |
303 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
314 | if (y > 0) maybe_push (dist, seeds, x, y - 1, d); |
304 | } |
315 | } |
305 | } |
316 | } |
|
|
317 | } |
|
|
318 | |
|
|
319 | void |
|
|
320 | layout::isolation_remover () |
|
|
321 | { |
|
|
322 | layout dist (w - 2, h - 2); // map without border |
|
|
323 | |
|
|
324 | for (int x = 1; x < w - 1; ++x) |
|
|
325 | for (int y = 1; y < h - 1; ++y) |
|
|
326 | dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; |
|
|
327 | |
|
|
328 | ::isolation_remover (dist); |
306 | |
329 | |
307 | // now copy the tunnels over |
330 | // now copy the tunnels over |
308 | for (int x = 0; x < maze.w; ++x) |
331 | for (int x = 1; x < w - 1; ++x) |
309 | for (int y = 0; y < maze.h; ++y) |
332 | for (int y = 1; y < h - 1; ++y) |
310 | if (maze [x][y] == '#' && dist [x][y] == 1) |
333 | if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) |
311 | maze [x][y] = 0; |
334 | data [x][y] = 0; |
312 | } |
|
|
313 | |
|
|
314 | void |
|
|
315 | Layout::isolation_remover (bool dirty) |
|
|
316 | { |
|
|
317 | ::isolation_remover (*this, dirty); |
|
|
318 | } |
335 | } |
319 | |
336 | |
320 | ///////////////////////////////////////////////////////////////////////////// |
337 | ///////////////////////////////////////////////////////////////////////////// |
321 | |
338 | |
322 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
339 | // inspired mostly by http://www.jimrandomh.org/misc/caves.txt |
323 | void |
340 | void |
324 | Layout::gen_cave (int subtype) |
341 | layout::gen_cave (int subtype) |
325 | { |
342 | { |
326 | switch (subtype) |
343 | switch (subtype) |
327 | { |
344 | { |
328 | // a rough cave |
345 | // a rough cave |
329 | case 0: |
346 | case 0: |
330 | fill_rand (rmg_rndm (80, 95)); |
347 | fill_rand (rmg_rndm (85, 97)); |
331 | break; |
348 | break; |
332 | |
349 | |
333 | // corridors |
350 | // corridors |
334 | case 1: |
351 | case 1: |
335 | fill_rand (rmg_rndm (5, 40)); |
352 | fill_rand (rmg_rndm (5, 40)); |
… | |
… | |
355 | |
372 | |
356 | border (); |
373 | border (); |
357 | isolation_remover (); |
374 | isolation_remover (); |
358 | } |
375 | } |
359 | |
376 | |
|
|
377 | ///////////////////////////////////////////////////////////////////////////// |
|
|
378 | |
|
|
379 | //+GPL |
|
|
380 | |
|
|
381 | /* puts doors at appropriate locations in a maze. */ |
|
|
382 | void |
|
|
383 | layout::doorify () |
|
|
384 | { |
|
|
385 | int ndoors = w * h / 60; /* reasonable number of doors. */ |
|
|
386 | int doorlocs = 0; /* # of available doorlocations */ |
|
|
387 | |
|
|
388 | uint16 *doorlist_x = salloc<uint16> (w * h); |
|
|
389 | uint16 *doorlist_y = salloc<uint16> (w * h); |
|
|
390 | |
|
|
391 | /* make a list of possible door locations */ |
|
|
392 | for (int i = 1; i < w - 1; i++) |
|
|
393 | for (int j = 1; j < h - 1; j++) |
|
|
394 | { |
|
|
395 | int sindex = surround_flag (*this, i, j); |
|
|
396 | |
|
|
397 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
398 | { |
|
|
399 | doorlist_x [doorlocs] = i; |
|
|
400 | doorlist_y [doorlocs] = j; |
|
|
401 | doorlocs++; |
|
|
402 | } |
|
|
403 | } |
|
|
404 | |
|
|
405 | while (ndoors > 0 && doorlocs > 0) |
|
|
406 | { |
|
|
407 | int di = rmg_rndm (doorlocs); |
|
|
408 | int i = doorlist_x [di]; |
|
|
409 | int j = doorlist_y [di]; |
|
|
410 | int sindex = surround_flag (*this, i, j); |
|
|
411 | |
|
|
412 | if (sindex == 3 || sindex == 12) /* these are possible door sindex */ |
|
|
413 | { |
|
|
414 | data [i][j] = 'D'; |
|
|
415 | ndoors--; |
|
|
416 | } |
|
|
417 | |
|
|
418 | /* reduce the size of the list */ |
|
|
419 | doorlocs--; |
|
|
420 | doorlist_x[di] = doorlist_x [doorlocs]; |
|
|
421 | doorlist_y[di] = doorlist_y [doorlocs]; |
|
|
422 | } |
|
|
423 | |
|
|
424 | sfree (doorlist_x, w * h); |
|
|
425 | sfree (doorlist_y, w * h); |
|
|
426 | } |
|
|
427 | |
|
|
428 | /* takes a map and makes it symmetric: adjusts Xsize and |
|
|
429 | * Ysize to produce a symmetric map. |
|
|
430 | */ |
|
|
431 | void |
|
|
432 | layout::symmetrize (int symmetry) |
|
|
433 | { |
|
|
434 | if (symmetry == SYMMETRY_NONE) |
|
|
435 | return; |
|
|
436 | |
|
|
437 | layout sym_layout ( |
|
|
438 | symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, |
|
|
439 | symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h |
|
|
440 | ); |
|
|
441 | |
|
|
442 | if (symmetry == SYMMETRY_X) |
|
|
443 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
|
|
444 | for (int j = 0; j < sym_layout.h; j++) |
|
|
445 | { |
|
|
446 | sym_layout[i ][j] = |
|
|
447 | sym_layout[sym_layout.w - i - 1][j] = data [i][j]; |
|
|
448 | } |
|
|
449 | |
|
|
450 | if (symmetry == SYMMETRY_Y) |
|
|
451 | for (int i = 0; i < sym_layout.w; i++) |
|
|
452 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
|
|
453 | { |
|
|
454 | sym_layout[i][j ] = |
|
|
455 | sym_layout[i][sym_layout.h - j - 1] = data [i][j]; |
|
|
456 | } |
|
|
457 | |
|
|
458 | if (symmetry == SYMMETRY_XY) |
|
|
459 | for (int i = 0; i < sym_layout.w / 2 + 1; i++) |
|
|
460 | for (int j = 0; j < sym_layout.h / 2 + 1; j++) |
|
|
461 | { |
|
|
462 | sym_layout[i ][j ] = |
|
|
463 | sym_layout[i ][sym_layout.h - j - 1] = |
|
|
464 | sym_layout[sym_layout.w - i - 1][j ] = |
|
|
465 | sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; |
|
|
466 | } |
|
|
467 | |
|
|
468 | /* need to run the isolation remover for some layouts */ |
|
|
469 | #if 0 |
|
|
470 | switch (RP->map_layout_style) |
|
|
471 | { |
|
|
472 | case LAYOUT_ONION: |
|
|
473 | case LAYOUT_SNAKE: |
|
|
474 | case LAYOUT_SQUARE_SPIRAL: |
|
|
475 | // safe |
|
|
476 | break; |
|
|
477 | |
|
|
478 | default: |
|
|
479 | sym_layout.isolation_remover (); |
|
|
480 | break; |
|
|
481 | } |
|
|
482 | #endif |
|
|
483 | sym_layout.isolation_remover (); |
|
|
484 | |
|
|
485 | swap (sym_layout); |
|
|
486 | } |
|
|
487 | |
|
|
488 | //-GPL |
|
|
489 | |
|
|
490 | void |
|
|
491 | layout::rotate (int rotation) |
|
|
492 | { |
|
|
493 | switch (rotation & 3) |
|
|
494 | { |
|
|
495 | case 2: /* a reflection */ |
|
|
496 | { |
|
|
497 | layout new_layout (w, h); |
|
|
498 | |
|
|
499 | for (int i = 0; i < w; i++) /* copy a reflection back */ |
|
|
500 | for (int j = 0; j < h; j++) |
|
|
501 | new_layout [i][j] = data [w - i - 1][h - j - 1]; |
|
|
502 | |
|
|
503 | swap (new_layout); |
|
|
504 | } |
|
|
505 | break; |
|
|
506 | |
|
|
507 | case 1: |
|
|
508 | case 3: |
|
|
509 | { |
|
|
510 | layout new_layout (h, w); |
|
|
511 | |
|
|
512 | if (rotation == 1) /* swap x and y */ |
|
|
513 | for (int i = 0; i < w; i++) |
|
|
514 | for (int j = 0; j < h; j++) |
|
|
515 | new_layout [j][i] = data [i][j]; |
|
|
516 | |
|
|
517 | if (rotation == 3) /* swap x and y */ |
|
|
518 | for (int i = 0; i < w; i++) |
|
|
519 | for (int j = 0; j < h; j++) |
|
|
520 | new_layout [j][i] = data [w - i - 1][h - j - 1]; |
|
|
521 | |
|
|
522 | swap (new_layout); |
|
|
523 | } |
|
|
524 | break; |
|
|
525 | } |
|
|
526 | } |
|
|
527 | |
|
|
528 | ///////////////////////////////////////////////////////////////////////////// |
|
|
529 | |
|
|
530 | //+GPL |
|
|
531 | |
|
|
532 | /* |
|
|
533 | * Expands a maze by 2x in each dimension. |
|
|
534 | * H. S. Teoh |
|
|
535 | */ |
|
|
536 | |
|
|
537 | /* Copy the old tile X into the new one at location (i*2, j*2) and |
|
|
538 | * fill up the rest of the 2x2 result with \0: |
|
|
539 | * X ---> X \0 |
|
|
540 | * \0 \0 |
|
|
541 | */ |
|
|
542 | static void inline |
|
|
543 | expand_misc (layout &newlayout, int i, int j, layout &maze) |
|
|
544 | { |
|
|
545 | newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j]; |
|
|
546 | /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that |
|
|
547 | * for us.) */ |
|
|
548 | } |
|
|
549 | |
|
|
550 | /* Returns a bitmap that represents which squares on the right and bottom |
|
|
551 | * edges of a square (i,j) match the given character: |
|
|
552 | * 1 match on (i+1, j) |
|
|
553 | * 2 match on (i, j+1) |
|
|
554 | * 4 match on (i+1, j+1) |
|
|
555 | * and the possible combinations thereof. |
|
|
556 | */ |
|
|
557 | static int noinline |
|
|
558 | calc_pattern (char ch, layout &maze, int i, int j) |
|
|
559 | { |
|
|
560 | int pattern = 0; |
|
|
561 | |
|
|
562 | if (i + 1 < maze.w && maze[i + 1][j] == ch) |
|
|
563 | pattern |= 1; |
|
|
564 | |
|
|
565 | if (j + 1 < maze.h) |
|
|
566 | { |
|
|
567 | if (maze[i][j + 1] == ch) |
|
|
568 | pattern |= 2; |
|
|
569 | |
|
|
570 | if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) |
|
|
571 | pattern |= 4; |
|
|
572 | } |
|
|
573 | |
|
|
574 | return pattern; |
|
|
575 | } |
|
|
576 | |
|
|
577 | /* Expand a wall. This function will try to sensibly connect the resulting |
|
|
578 | * wall to adjacent wall squares, so that the result won't have disconnected |
|
|
579 | * walls. |
|
|
580 | */ |
|
|
581 | static void inline |
|
|
582 | expand_wall (layout &newlayout, int i, int j, layout &maze) |
|
|
583 | { |
|
|
584 | int wall_pattern = calc_pattern ('#', maze, i, j); |
|
|
585 | int door_pattern = calc_pattern ('D', maze, i, j); |
|
|
586 | int both_pattern = wall_pattern | door_pattern; |
|
|
587 | |
|
|
588 | newlayout[i * 2][j * 2] = '#'; |
|
|
589 | |
|
|
590 | if (i + 1 < maze.w) |
|
|
591 | { |
|
|
592 | if (both_pattern & 1) |
|
|
593 | { /* join walls/doors to the right */ |
|
|
594 | /* newlayout[i*2+1][j*2] = '#'; */ |
|
|
595 | newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; |
|
|
596 | } |
|
|
597 | } |
|
|
598 | |
|
|
599 | if (j + 1 < maze.h) |
|
|
600 | { |
|
|
601 | if (both_pattern & 2) |
|
|
602 | { /* join walls/doors to the bottom */ |
|
|
603 | /* newlayout[i*2][j*2+1] = '#'; */ |
|
|
604 | newlayout[i * 2][j * 2 + 1] = maze[i][j + 1]; |
|
|
605 | } |
|
|
606 | |
|
|
607 | if (wall_pattern == 7) |
|
|
608 | { /* if orig maze is a 2x2 wall block, |
|
|
609 | * we fill the result with walls. */ |
|
|
610 | newlayout[i * 2 + 1][j * 2 + 1] = '#'; |
|
|
611 | } |
|
|
612 | } |
|
|
613 | } |
|
|
614 | |
|
|
615 | /* This function will try to sensibly connect doors so that they meet up with |
|
|
616 | * adjacent walls. Note that it will also presumptuously delete (ignore) doors |
|
|
617 | * that it doesn't know how to correctly expand. |
|
|
618 | */ |
|
|
619 | static void inline |
|
|
620 | expand_door (layout &newlayout, int i, int j, layout &maze) |
|
|
621 | { |
|
|
622 | int wall_pattern = calc_pattern ('#', maze, i, j); |
|
|
623 | int door_pattern = calc_pattern ('D', maze, i, j); |
|
|
624 | int join_pattern; |
|
|
625 | |
|
|
626 | /* Doors "like" to connect to walls more than other doors. If there is |
|
|
627 | * a wall and another door, this door will connect to the wall and |
|
|
628 | * disconnect from the other door. */ |
|
|
629 | if (wall_pattern & 3) |
|
|
630 | join_pattern = wall_pattern; |
|
|
631 | else |
|
|
632 | join_pattern = door_pattern; |
|
|
633 | |
|
|
634 | newlayout[i * 2][j * 2] = 'D'; |
|
|
635 | |
|
|
636 | if (i + 1 < maze.w) |
|
|
637 | if (join_pattern & 1) |
|
|
638 | /* there is a door/wall to the right */ |
|
|
639 | newlayout[i * 2 + 1][j * 2] = 'D'; |
|
|
640 | |
|
|
641 | if (j + 1 < maze.h) |
|
|
642 | if (join_pattern & 2) |
|
|
643 | /* there is a door/wall below */ |
|
|
644 | newlayout[i * 2][j * 2 + 1] = 'D'; |
|
|
645 | } |
|
|
646 | |
|
|
647 | void |
|
|
648 | layout::expand2x () |
|
|
649 | { |
|
|
650 | layout new_layout (w * 2 - 1, h * 2 - 1); |
|
|
651 | |
|
|
652 | new_layout.clear (); |
|
|
653 | |
|
|
654 | for (int i = 0; i < w; i++) |
|
|
655 | for (int j = 0; j < h; j++) |
|
|
656 | switch (data [i][j]) |
|
|
657 | { |
|
|
658 | case '#': expand_wall (new_layout, i, j, *this); break; |
|
|
659 | case 'D': expand_door (new_layout, i, j, *this); break; |
|
|
660 | default: expand_misc (new_layout, i, j, *this); break; |
|
|
661 | } |
|
|
662 | |
|
|
663 | swap (new_layout); |
|
|
664 | } |
|
|
665 | |
|
|
666 | ///////////////////////////////////////////////////////////////////////////// |
|
|
667 | |
|
|
668 | /* checks the maze to see if I can stick a horizontal(dir = 0) wall |
|
|
669 | (or vertical, dir == 1) |
|
|
670 | here which ends up on other walls sensibly. */ |
|
|
671 | static int |
|
|
672 | can_make_wall (const layout &maze, int dx, int dy, int dir) |
|
|
673 | { |
|
|
674 | int i1; |
|
|
675 | int length = 0; |
|
|
676 | |
|
|
677 | /* dont make walls if we're on the edge. */ |
|
|
678 | if (dx == 0 || dx == (maze.w - 1) || dy == 0 || dy == (maze.h - 1)) |
|
|
679 | return -1; |
|
|
680 | |
|
|
681 | /* don't make walls if we're ON a wall. */ |
|
|
682 | if (maze [dx][dy] != 0) |
|
|
683 | return -1; |
|
|
684 | |
|
|
685 | if (dir == 0) /* horizontal */ |
|
|
686 | { |
|
|
687 | int y = dy; |
|
|
688 | |
|
|
689 | for (i1 = dx - 1; i1 > 0; i1--) |
|
|
690 | { |
|
|
691 | int sindex = surround_flag2 (maze, i1, y); |
|
|
692 | |
|
|
693 | if (sindex == 1) break; |
|
|
694 | if (sindex != 0) return -1; /* can't make horiz. wall here */ |
|
|
695 | if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
|
|
696 | |
|
|
697 | length++; |
|
|
698 | } |
|
|
699 | |
|
|
700 | for (i1 = dx + 1; i1 < maze.w - 1; i1++) |
|
|
701 | { |
|
|
702 | int sindex = surround_flag2 (maze, i1, y); |
|
|
703 | |
|
|
704 | if (sindex == 2) break; |
|
|
705 | if (sindex != 0) return -1; /* can't make horiz. wall here */ |
|
|
706 | if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ |
|
|
707 | |
|
|
708 | length++; |
|
|
709 | } |
|
|
710 | return length; |
|
|
711 | } |
|
|
712 | else |
|
|
713 | { /* vertical */ |
|
|
714 | int x = dx; |
|
|
715 | |
|
|
716 | for (i1 = dy - 1; i1 > 0; i1--) |
|
|
717 | { |
|
|
718 | int sindex = surround_flag2 (maze, x, i1); |
|
|
719 | |
|
|
720 | if (sindex == 4) break; |
|
|
721 | if (sindex != 0) return -1; /* can't make vert. wall here */ |
|
|
722 | if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
|
|
723 | |
|
|
724 | length++; |
|
|
725 | } |
|
|
726 | |
|
|
727 | for (i1 = dy + 1; i1 < maze.h - 1; i1++) |
|
|
728 | { |
|
|
729 | int sindex = surround_flag2 (maze, x, i1); |
|
|
730 | |
|
|
731 | if (sindex == 8) break; |
|
|
732 | if (sindex != 0) return -1; /* can't make verti. wall here */ |
|
|
733 | if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ |
|
|
734 | |
|
|
735 | length++; |
|
|
736 | } |
|
|
737 | |
|
|
738 | return length; |
|
|
739 | } |
|
|
740 | |
|
|
741 | return -1; |
|
|
742 | } |
|
|
743 | |
|
|
744 | int |
|
|
745 | make_wall (char **maze, int x, int y, int dir) |
|
|
746 | { |
|
|
747 | maze[x][y] = 'D'; /* mark a door */ |
|
|
748 | |
|
|
749 | switch (dir) |
|
|
750 | { |
|
|
751 | case 0: /* horizontal */ |
|
|
752 | { |
|
|
753 | for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#'; |
|
|
754 | for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#'; |
|
|
755 | break; |
|
|
756 | } |
|
|
757 | case 1: /* vertical */ |
|
|
758 | { |
|
|
759 | for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#'; |
|
|
760 | for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#'; |
|
|
761 | break; |
|
|
762 | } |
|
|
763 | } |
|
|
764 | |
|
|
765 | return 0; |
|
|
766 | } |
|
|
767 | |
|
|
768 | void |
|
|
769 | layout::roomify () |
|
|
770 | { |
|
|
771 | int tries = w * h / 30; |
|
|
772 | |
|
|
773 | for (int ti = 0; ti < tries; ti++) |
|
|
774 | { |
|
|
775 | /* starting location for looking at creating a door */ |
|
|
776 | int dx = rmg_rndm (w); |
|
|
777 | int dy = rmg_rndm (h); |
|
|
778 | |
|
|
779 | /* results of checking on creating walls. */ |
|
|
780 | int cx = can_make_wall (*this, dx, dy, 0); /* horizontal */ |
|
|
781 | int cy = can_make_wall (*this, dx, dy, 1); /* vertical */ |
|
|
782 | |
|
|
783 | if (cx == -1) |
|
|
784 | { |
|
|
785 | if (cy != -1) |
|
|
786 | make_wall (*this, dx, dy, 1); |
|
|
787 | |
|
|
788 | continue; |
|
|
789 | } |
|
|
790 | |
|
|
791 | if (cy == -1) |
|
|
792 | { |
|
|
793 | make_wall (*this, dx, dy, 0); |
|
|
794 | continue; |
|
|
795 | } |
|
|
796 | |
|
|
797 | if (cx < cy) |
|
|
798 | make_wall (*this, dx, dy, 0); |
|
|
799 | else |
|
|
800 | make_wall (*this, dx, dy, 1); |
|
|
801 | } |
|
|
802 | } |
|
|
803 | |
|
|
804 | ///////////////////////////////////////////////////////////////////////////// |
|
|
805 | |
|
|
806 | /* function selects the maze function and gives it whatever |
|
|
807 | arguments it needs. */ |
|
|
808 | void |
|
|
809 | layout::generate (random_map_params *RP) |
|
|
810 | { |
|
|
811 | switch (RP->map_layout_style) |
|
|
812 | { |
|
|
813 | case LAYOUT_ONION: |
|
|
814 | map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); |
|
|
815 | |
|
|
816 | if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF))) |
|
|
817 | roomify (); |
|
|
818 | |
|
|
819 | break; |
|
|
820 | |
|
|
821 | case LAYOUT_MAZE: |
|
|
822 | maze_gen (*this, RP->get_iv ("maze_type", rmg_rndm (4))); |
|
|
823 | |
|
|
824 | if (rmg_rndm (2)) |
|
|
825 | doorify (); |
|
|
826 | |
|
|
827 | break; |
|
|
828 | |
|
|
829 | case LAYOUT_SPIRAL: |
|
|
830 | map_gen_spiral (*this, RP->layoutoptions1); |
|
|
831 | |
|
|
832 | if (rmg_rndm (2)) |
|
|
833 | doorify (); |
|
|
834 | |
|
|
835 | break; |
|
|
836 | |
|
|
837 | case LAYOUT_ROGUELIKE: |
|
|
838 | /* Don't put symmetry in rogue maps. There isn't much reason to |
|
|
839 | * do so in the first place (doesn't make it any more interesting), |
|
|
840 | * but more importantly, the symmetry code presumes we are symmetrizing |
|
|
841 | * spirals, or maps with lots of passages - making a symmetric rogue |
|
|
842 | * map fails because its likely that the passages the symmetry process |
|
|
843 | * creates may not connect the rooms. |
|
|
844 | */ |
|
|
845 | RP->symmetry_used = SYMMETRY_NONE; |
|
|
846 | roguelike_layout_gen (*this, RP->layoutoptions1); |
|
|
847 | /* no doorifying... done already */ |
|
|
848 | break; |
|
|
849 | |
|
|
850 | case LAYOUT_SNAKE: |
|
|
851 | make_snake_layout (*this, RP->layoutoptions1); |
|
|
852 | |
|
|
853 | if (rmg_rndm (2)) |
|
|
854 | roomify (); |
|
|
855 | |
|
|
856 | break; |
|
|
857 | |
|
|
858 | case LAYOUT_SQUARE_SPIRAL: |
|
|
859 | make_square_spiral_layout (*this, RP->layoutoptions1); |
|
|
860 | |
|
|
861 | if (rmg_rndm (2)) |
|
|
862 | roomify (); |
|
|
863 | |
|
|
864 | break; |
|
|
865 | |
|
|
866 | case LAYOUT_CAVE: |
|
|
867 | gen_cave (RP->get_iv ("cave_type", rmg_rndm (4))); |
|
|
868 | |
|
|
869 | if (rmg_rndm (2)) |
|
|
870 | doorify (); |
|
|
871 | |
|
|
872 | break; |
|
|
873 | |
|
|
874 | default: |
|
|
875 | abort (); |
|
|
876 | } |
|
|
877 | |
|
|
878 | /* rotate the maze randomly */ |
|
|
879 | rotate (rmg_rndm (4)); |
|
|
880 | |
|
|
881 | symmetrize (RP->symmetry_used); |
|
|
882 | |
|
|
883 | #if 0 |
|
|
884 | print ();//D |
|
|
885 | #endif |
|
|
886 | |
|
|
887 | if (RP->expand2x) |
|
|
888 | expand2x (); |
|
|
889 | } |
|
|
890 | |
|
|
891 | //-GPL |
|
|
892 | |
360 | #if 0 |
893 | #if 0 |
361 | static struct demo |
894 | static struct demo |
362 | { |
895 | { |
363 | demo () |
896 | demo () |
364 | { |
897 | { |
365 | Layout maze (40, 25); |
|
|
366 | rmg_rndm.seed (time (0)); |
898 | rmg_rndm.seed (time (0)); |
367 | |
899 | |
368 | for(int i=1;i<10;i) |
900 | for(int i=1;i<100;i++) |
369 | { |
901 | { |
|
|
902 | layout maze (40, 25); |
370 | maze.fill_rand (97); |
903 | maze.fill_rand (85); |
371 | maze.border (); |
904 | maze.border (); |
372 | maze.isolation_remover (); |
905 | maze.isolation_remover (); |
373 | maze.print (); |
906 | maze.print (); |
374 | } |
907 | } |
375 | |
908 | |