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.16 by root, Sat Jul 3 11:52:11 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)
189{ 227{
190 for (;;) 228 for (;;)
191 { 229 {
230 point neigh[4];
231 int ncnt = 0;
232
192 if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) 233 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) 234 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) 235 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) 236 if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1);
199 ++y; 237
200 else 238 if (!ncnt)
201 break; 239 return;
240
241 point &p = neigh [rmg_rndm (ncnt)];
242
243 seeds.push (p);
244
245 x = p.x;
246 y = p.y;
202 247
203 d = dist [x][y]; 248 d = dist [x][y];
204 dist [x][y] = 1; 249 dist [x][y] = 1;
205 seeds.push (point (x, y));
206 } 250 }
207} 251}
208 252
209static void inline 253static void inline
210maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) 254maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d)
217 return; 261 return;
218 262
219 seeds.push (point (x, y)); 263 seeds.push (point (x, y));
220} 264}
221 265
222void 266// isolation remover, works on a "distance" map
223layout::isolation_remover (bool dirty) 267// the map must be initialised with 0 == rooms, 255 = walls
268static void noinline
269isolation_remover (layout &dist)
224{ 270{
225 layout dist (w, h);
226
227 // dist contains 271 // dist contains
228 // 0 == invisited rooms 272 // 0 == invisited rooms
229 // 1 == visited rooms 273 // 1 == visited rooms
230 // 2+ shortest distance to random near room 274 // 2+ shortest distance to random near room
231 275
232 // phase 1, initialise dist array, find seed 276 // phase 1, find seed
233 int cnt = 0; 277 int cnt = 0;
234 int x, y; 278 int x, y;
235 279
236 for (int i = 0; i < w; ++i) 280 for (int i = 0; i < dist.w; ++i)
237 for (int j = 0; j < h; ++j) 281 for (int j = 0; j < dist.h; ++j)
238 { 282 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; 283 x = i, y = j;
246 }
247 }
248 284
249 if (!cnt) 285 if (!cnt)
250 { 286 {
251 // map is completely massive, this is not good, 287 // map is completely massive, this is not good,
252 // so make it empty instead. 288 // so make it empty instead.
253 clear (); 289 dist.fill (1);
254 border ();
255 return; 290 return;
256 } 291 }
257 292
258 fixed_stack<point> seeds (w * h * 5); 293 fixed_stack<point> seeds (dist.w * dist.h * 5);
259 294
260 // found first free space - picking the first one gives 295 // found first free space - picking the first one gives
261 // us a slight bias for tunnels, but usually you won't 296 // us a slight bias for tunnels, but usually you won't
262 // notice that in-game 297 // notice that in-game
263 seeds.push (point (x, y)); 298 seeds.push (point (x, y));
275 y = p.y; 310 y = p.y;
276 311
277 if (!dist [x][y]) 312 if (!dist [x][y])
278 { 313 {
279 // found new isolated area, make tunnel 314 // found new isolated area, make tunnel
280 if (!dirty)
281 push_flood_fill (dist, seeds, x, y); 315 push_flood_fill (dist, seeds, x, y);
282
283 make_tunnel (dist, seeds, x, y, 255); 316 make_tunnel (dist, seeds, x, y, 255);
284
285 if (dirty)
286 push_flood_fill (dist, seeds, x, y);
287 } 317 }
288 else 318 else
289 { 319 {
290 // nothing here, continue to expand 320 // nothing here, continue to expand
291 U8 d = U8 (dist [x][y]) + 1; 321 U8 d = U8 (dist [x][y]) + 1;
294 if (x > 0) maybe_push (dist, seeds, x - 1, y, d); 324 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); 325 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); 326 if (y > 0) maybe_push (dist, seeds, x, y - 1, d);
297 } 327 }
298 } 328 }
329}
330
331void
332layout::isolation_remover ()
333{
334 layout dist (w - 2, h - 2); // map without border
335
336 for (int x = 1; x < w - 1; ++x)
337 for (int y = 1; y < h - 1; ++y)
338 dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0;
339
340 ::isolation_remover (dist);
299 341
300 // now copy the tunnels over 342 // now copy the tunnels over
301 for (int x = 0; x < w; ++x) 343 for (int x = 1; x < w - 1; ++x)
302 for (int y = 0; y < h; ++y) 344 for (int y = 1; y < h - 1; ++y)
303 if (data [x][y] == '#' && dist [x][y] == 1) 345 if (data [x][y] == '#' && dist [x - 1][y - 1] == 1)
304 data [x][y] = 0; 346 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} 347}
346 348
347///////////////////////////////////////////////////////////////////////////// 349/////////////////////////////////////////////////////////////////////////////
348 350
349//+GPL 351//+GPL
770 make_wall (*this, dx, dy, 1); 772 make_wall (*this, dx, dy, 1);
771 } 773 }
772} 774}
773 775
774///////////////////////////////////////////////////////////////////////////// 776/////////////////////////////////////////////////////////////////////////////
777
778// inspired mostly by http://www.jimrandomh.org/misc/caves.txt
779void
780layout::gen_cave (int subtype)
781{
782 switch (subtype)
783 {
784 // a rough cave
785 case 0:
786 fill_rand (rmg_rndm (85, 97));
787 break;
788
789 // corridors
790 case 1:
791 fill_rand (rmg_rndm (5, 40));
792 erode_1_2 (5, 2, 10);
793 erode_1_2 (5, -1, 10);
794 erode_1_2 (5, 2, 1);
795 break;
796
797 // somewhat open, roundish
798 case 2:
799 fill_rand (45);
800 erode_1_2 (5, 0, 5);
801 erode_1_2 (5, 1, 1);
802 break;
803
804 // wide open, some room-like structures
805 case 3:
806 fill_rand (45);
807 erode_1_2 (5, 2, 4);
808 erode_1_2 (5, -1, 3);
809 break;
810 }
811
812 border ();
813 isolation_remover ();
814}
815
816static void
817gen_mixed_ (layout &maze, random_map_params *RP, int dir)
818{
819 if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3))
820 dir = 2; // stop recursion randomly
821
822 if (dir == 0 && maze.w > 16)
823 {
824 int m = rmg_rndm (8, maze.w - 8);
825
826 layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir);
827 layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
828 }
829 else if (dir == 1 && maze.h > 16)
830 {
831 int m = rmg_rndm (8, maze.h - 8);
832
833 layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir);
834 layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir);
835 }
836 else
837 {
838 RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1;
839
840 if (RP->map_layout_style == LAYOUT_MULTIPLE)
841 ++RP->map_layout_style;
842
843 maze.generate (RP);
844 }
845}
846
847// recursive subdivision with random sublayouts
848static void
849gen_mixed (layout &maze, random_map_params *RP)
850{
851 random_map_params &rp = *new random_map_params (RP);
852 gen_mixed_ (maze, &rp, rmg_rndm (2));
853 delete &rp;
854
855 maze.border ();
856 maze.isolation_remover ();
857}
775 858
776/* function selects the maze function and gives it whatever 859/* function selects the maze function and gives it whatever
777 arguments it needs. */ 860 arguments it needs. */
778void 861void
779layout::generate (random_map_params *RP) 862layout::generate (random_map_params *RP)
839 if (rmg_rndm (2)) 922 if (rmg_rndm (2))
840 doorify (); 923 doorify ();
841 924
842 break; 925 break;
843 926
927 case LAYOUT_MULTIPLE:
928 gen_mixed (*this, RP);
929 break;
930
844 default: 931 default:
845 abort (); 932 abort ();
846 } 933 }
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} 934}
860 935
861//-GPL 936//-GPL
862 937
863#if 0 938#if 0
865{ 940{
866 demo () 941 demo ()
867 { 942 {
868 rmg_rndm.seed (time (0)); 943 rmg_rndm.seed (time (0));
869 944
870 for(int i=1;i<10;i) 945 for(int i=1;i<100;i++)
871 { 946 {
872 layout maze (10, 10); 947 layout maze (40, 25);
873 maze.fill_rand (90); 948 maze.fill_rand (85);
874 maze.border (); 949 maze.border ();
875 maze.isolation_remover (); 950 maze.isolation_remover ();
876 maze.doorify ();
877 maze.symmetrize (rmg_rndm (2, 4));
878 maze.rotate (rmg_rndm (4));
879 maze.expand2x ();
880 maze.print (); 951 maze.print ();
881 } 952 }
882 953
883 exit (1); 954 exit (1);
884 } 955 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines