ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/random_maps/layout.C
(Generate patch)

Comparing deliantra/server/random_maps/layout.C (file contents):
Revision 1.8 by root, Fri Jul 2 15:43:37 2010 UTC vs.
Revision 1.18 by root, Sat Jul 3 13:39:04 2010 UTC

23 23
24#include <global.h> 24#include <global.h>
25#include <random_map.h> 25#include <random_map.h>
26#include <rproto.h> 26#include <rproto.h>
27 27
28void
28layout::layout (int w, int h) 29layout::alloc (int w, int h)
29: w(w), h(h)
30{ 30{
31 assert (sizeof (cell) == 1); 31 assert (sizeof (cell) == 1);
32
33 this->w = w;
34 this->h = h;
32 35
33 // we store the layout in a single contiguous memory layout 36 // we store the layout in a single contiguous memory layout
34 // first part consists of pointers to each column, followed 37 // first part consists of pointers to each column, followed
35 // by the actual columns (not rows!) 38 // by the actual columns (not rows!)
36 int size = (sizeof (cell *) + sizeof (cell) * h) * w; 39 size = (sizeof (cell *) + sizeof (cell) * h) * w;
37
38 data = (cell **)salloc<char> (size); 40 data = (cell **)salloc<char> (size);
39 41
40 cell *p = (cell *)(data + w); 42 cell *p = (cell *)(data + w);
41 43
42 for (int x = w; x--; ) 44 for (int x = 0; x < w; ++x)
43 data [x] = p + x * h; 45 data [x] = p + x * h;
44} 46}
45 47
48layout::layout (int w, int h)
49{
50 alloc (w, h);
51}
52
53layout::layout (layout &copy)
54{
55 alloc (copy.w, copy.h);
56
57 memcpy (data [0], copy.data [0], sizeof (cell) * h * w);
58}
59
60layout::layout (layout &orig, int x1, int y1, int x2, int y2)
61{
62 w = x2 - x1;
63 h = y2 - y1;
64
65 // we only allocate space for the pointers
66 size = sizeof (cell *) * w;
67 data = (cell **)salloc<char> (size);
68
69 // and now we point back into the original layout
70 for (int x = 0; x < w; ++x)
71 data [x] = orig.data [x + x1] + y1;
72}
73
46layout::~layout () 74layout::~layout ()
47{ 75{
48 int size = (sizeof (cell *) + sizeof (cell) * h) * w;
49
50 sfree ((char *)data, size); 76 sfree ((char *)data, size);
51} 77}
52 78
53void 79void
54layout::fill (char fill) 80layout::fill (char fill)
55{ 81{
56 memset (data [0], fill, w * h); 82 //memset (data [0], fill, w * h); // only when contiguous :/
83 fill_rect (0, 0, w, h, fill);
57} 84}
58 85
59void 86void
60layout::rect (int x1, int y1, int x2, int y2, char fill) 87layout::rect (int x1, int y1, int x2, int y2, char fill)
88{
89 --x2;
90
91 memset (data [x1] + y1, fill, y2 - y1);
92 memset (data [x2] + y1, fill, y2 - y1);
93
94 while (++x1 < x2)
95 data [x1][y1] = data [x1][y2 - 1] = fill;
96}
97
98void
99layout::fill_rect (int x1, int y1, int x2, int y2, char fill)
61{ 100{
62 for (; x1 < x2; ++x1) 101 for (; x1 < x2; ++x1)
63 memset (data [x1] + y1, fill, y2 - y1); 102 memset (data [x1] + y1, fill, y2 - y1);
64} 103}
65 104
66void layout::border (char fill) 105void layout::border (char fill)
67{ 106{
68 for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; 107 rect (0, 0, w, h, fill);
69 for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill;
70} 108}
71 109
72void 110void
73layout::fill_rand (int percent) 111layout::fill_rand (int percent)
74{ 112{
156///////////////////////////////////////////////////////////////////////////// 194/////////////////////////////////////////////////////////////////////////////
157// isolation remover - ensures single connected area 195// isolation remover - ensures single connected area
158 196
159typedef fixed_stack<point> pointlist; 197typedef fixed_stack<point> pointlist;
160 198
161static noinline void 199static void noinline
162push_flood_fill (layout &dist, pointlist &seeds, int x, int y) 200push_flood_fill (layout &dist, pointlist &seeds, int x, int y)
163{ 201{
164 if (dist [x][y]) 202 if (dist [x][y])
165 return; 203 return;
166 204
177 ++y; 215 ++y;
178 } 216 }
179 217
180 while (--y >= y0) 218 while (--y >= y0)
181 { 219 {
182 if (x > 0) push_flood_fill (dist, seeds, x - 1, y); 220 if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y);
183 if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); 221 if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y);
184 } 222 }
185} 223}
186 224
187static inline void 225static inline void
188make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d) 226make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb)
189{ 227{
190 for (;;) 228 for (;;)
191 { 229 {
230 point neigh[4];
231 int ncnt = 0;
232
233 d += perturb > 1;
234
192 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 235 if (x > 0 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y);
193 --x;
194 else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) 236 if (x < dist.w - 1 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y);
195 ++x;
196 else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) 237 if (y > 0 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1);
197 --y;
198 else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) 238 if (y < dist.h - 1 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
199 ++y; 239
200 else 240 if (!ncnt)
201 break; 241 return;
242
243 point &p = neigh [perturb ? rmg_rndm (ncnt) : 0];
244
245 seeds.push (p);
246
247 x = p.x;
248 y = p.y;
202 249
203 d = dist [x][y]; 250 d = dist [x][y];
204 dist [x][y] = 1; 251 dist [x][y] = 1;
205 seeds.push (point (x, y));
206 } 252 }
207} 253}
208 254
209static void inline 255static void inline
210maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) 256maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
217 return; 263 return;
218 264
219 seeds.push (point (x, y)); 265 seeds.push (point (x, y));
220} 266}
221 267
222void 268// isolation remover, works on a "distance" map
223layout::isolation_remover (bool dirty) 269// the map must be initialised with 0 == rooms, 255 = walls
270static void noinline
271isolation_remover (layout &dist, unsigned int perturb = 2)
224{ 272{
225 layout dist (w, h);
226
227 // dist contains 273 // dist contains
228 // 0 == invisited rooms 274 // 0 == invisited rooms
229 // 1 == visited rooms 275 // 1 == visited rooms
230 // 2+ shortest distance to random near room 276 // 2+ shortest distance to random near room
231 277
232 // phase 1, initialise dist array, find seed 278 max_it (perturb, 0);
279 min_it (perturb, 2);
280
281 // phase 1, find seed
233 int cnt = 0; 282 int cnt = 0;
234 int x, y; 283 int x, y;
235 284
236 for (int i = 0; i < w; ++i) 285 for (int i = 0; i < dist.w; ++i)
237 for (int j = 0; j < h; ++j) 286 for (int j = 0; j < dist.h; ++j)
238 { 287 if (!dist [i][j] && !rmg_rndm (++cnt))
239 if (data [i][j] == '#')
240 dist [i][j] = U8 (255);
241 else
242 {
243 dist [i][j] = 0;
244 if (!rmg_rndm (++cnt))
245 x = i, y = j; 288 x = i, y = j;
246 }
247 }
248 289
249 if (!cnt) 290 if (!cnt)
250 { 291 {
251 // map is completely massive, this is not good, 292 // map is completely massive, this is not good,
252 // so make it empty instead. 293 // so make it empty instead.
253 clear (); 294 dist.fill (1);
254 border ();
255 return; 295 return;
256 } 296 }
257 297
258 fixed_stack<point> seeds (w * h * 5); 298 fixed_stack<point> seeds (dist.w * dist.h * 5);
259 299
260 // found first free space - picking the first one gives 300 // found first free space - picking the first one gives
261 // us a slight bias for tunnels, but usually you won't 301 // us a slight bias for tunnels, but usually you won't
262 // notice that in-game 302 // notice that in-game
263 seeds.push (point (x, y)); 303 seeds.push (point (x, y));
275 y = p.y; 315 y = p.y;
276 316
277 if (!dist [x][y]) 317 if (!dist [x][y])
278 { 318 {
279 // found new isolated area, make tunnel 319 // found new isolated area, make tunnel
280 if (!dirty)
281 push_flood_fill (dist, seeds, x, y); 320 push_flood_fill (dist, seeds, x, y);
282
283 make_tunnel (dist, seeds, x, y, 255); 321 make_tunnel (dist, seeds, x, y, 254, perturb);
284
285 if (dirty)
286 push_flood_fill (dist, seeds, x, y);
287 } 322 }
288 else 323 else
289 { 324 {
290 // nothing here, continue to expand 325 // nothing here, continue to expand
291 U8 d = U8 (dist [x][y]) + 1; 326 U8 d = U8 (dist [x][y]) + 1;
294 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 329 if (x > 0) maybe_push (dist, seeds, x - 1, y, d);
295 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); 330 if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d);
296 if (y > 0) maybe_push (dist, seeds, x, y - 1, d); 331 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
297 } 332 }
298 } 333 }
334}
335
336void
337layout::isolation_remover (int perturb)
338{
339 layout dist (w - 2, h - 2); // map without border
340
341 for (int x = 1; x < w - 1; ++x)
342 for (int y = 1; y < h - 1; ++y)
343 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
344
345 ::isolation_remover (dist, perturb);
299 346
300 // now copy the tunnels over 347 // now copy the tunnels over
301 for (int x = 0; x < w; ++x) 348 for (int x = 1; x < w - 1; ++x)
302 for (int y = 0; y < h; ++y) 349 for (int y = 1; y < h - 1; ++y)
303 if (data [x][y] == '#' && dist [x][y] == 1) 350 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
304 data [x][y] = 0; 351 data [x][y] = 0;
305}
306
307/////////////////////////////////////////////////////////////////////////////
308
309// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
310void
311layout::gen_cave (int subtype)
312{
313 switch (subtype)
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} 352}
346 353
347///////////////////////////////////////////////////////////////////////////// 354/////////////////////////////////////////////////////////////////////////////
348 355
349//+GPL 356//+GPL
769 else 776 else
770 make_wall (*this, dx, dy, 1); 777 make_wall (*this, dx, dy, 1);
771 } 778 }
772} 779}
773 780
781//-GPL
782
774///////////////////////////////////////////////////////////////////////////// 783/////////////////////////////////////////////////////////////////////////////
784
785// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
786void
787layout::gen_cave (int subtype)
788{
789 switch (subtype)
790 {
791 // a rough cave
792 case 0:
793 fill_rand (rmg_rndm (85, 97));
794 break;
795
796 // corridors
797 case 1:
798 fill_rand (rmg_rndm (5, 40));
799 erode_1_2 (5, 2, 10);
800 erode_1_2 (5, -1, 10);
801 erode_1_2 (5, 2, 1);
802 break;
803
804 // somewhat open, roundish
805 case 2:
806 fill_rand (45);
807 erode_1_2 (5, 0, 5);
808 erode_1_2 (5, 1, 1);
809 break;
810
811 // wide open, some room-like structures
812 case 3:
813 fill_rand (45);
814 erode_1_2 (5, 2, 4);
815 erode_1_2 (5, -1, 3);
816 break;
817 }
818
819 border ();
820 isolation_remover ();
821}
822
823void
824layout::gen_castle ()
825{
826 fill ('#');
827
828 for (int n = w * h / 30 + 1; n--; )
829 {
830 int rw = rmg_rndm (6, 10);
831 int rh = rmg_rndm (6, 10);
832
833 int rx = rmg_rndm (0, w - rw);
834 int ry = rmg_rndm (0, h - rh);
835
836 rect (rx, ry, rx + rw, ry + rh, '#');
837 fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0);
838 }
839
840 border ();
841 isolation_remover (0);
842}
843
844static void
845gen_mixed_ (layout &maze, random_map_params *RP, int dir)
846{
847 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
848 dir = 2; // stop recursion randomly
849
850 if (dir == 0 && maze.w > 16)
851 {
852 int m = rmg_rndm (8, maze.w - 8);
853
854 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
855 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
856 }
857 else if (dir == 1 && maze.h > 16)
858 {
859 int m = rmg_rndm (8, maze.h - 8);
860
861 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
862 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
863 }
864 else
865 {
866 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
867
868 if (RP->map_layout_style == LAYOUT_MULTIPLE)
869 ++RP->map_layout_style;
870
871 maze.generate (RP);
872 }
873}
874
875// recursive subdivision with random sublayouts
876static void
877gen_mixed (layout &maze, random_map_params *RP)
878{
879 random_map_params &rp = *new random_map_params (RP);
880 gen_mixed_ (maze, &rp, rmg_rndm (2));
881 delete &rp;
882
883 maze.border ();
884 maze.isolation_remover ();
885}
886
887//+GPL
775 888
776/* function selects the maze function and gives it whatever 889/* function selects the maze function and gives it whatever
777 arguments it needs. */ 890 arguments it needs. */
778void 891void
779layout::generate (random_map_params *RP) 892layout::generate (random_map_params *RP)
839 if (rmg_rndm (2)) 952 if (rmg_rndm (2))
840 doorify (); 953 doorify ();
841 954
842 break; 955 break;
843 956
957 case LAYOUT_CASTLE:
958 gen_castle ();
959
960 if (rmg_rndm (2))
961 doorify ();
962
963 break;
964
965 case LAYOUT_MULTIPLE:
966 gen_mixed (*this, RP);
967 break;
968
844 default: 969 default:
845 abort (); 970 abort ();
846 } 971 }
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} 972}
860 973
861//-GPL 974//-GPL
862 975
863#if 0 976#if 0
865{ 978{
866 demo () 979 demo ()
867 { 980 {
868 rmg_rndm.seed (time (0)); 981 rmg_rndm.seed (time (0));
869 982
870 for(int i=1;i<10;i) 983 for(int i=1;i<100;i++)
871 { 984 {
872 layout maze (10, 10); 985 layout maze (40, 25);
873 maze.fill_rand (90); 986 maze.gen_castle ();
874 maze.border ();
875 maze.isolation_remover ();
876 maze.doorify (); 987 maze.doorify ();
877 maze.symmetrize (rmg_rndm (2, 4));
878 maze.rotate (rmg_rndm (4));
879 maze.expand2x ();
880 maze.print (); 988 maze.print ();
989 exit(0);
881 } 990 }
882 991
883 exit (1); 992 exit (1);
884 } 993 }
885} demo; 994} demo;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines