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