--- deliantra/server/random_maps/layout.C 2010/07/02 15:43:37 1.8 +++ deliantra/server/random_maps/layout.C 2010/07/03 11:52:11 1.16 @@ -25,48 +25,86 @@ #include #include -layout::layout (int w, int h) -: w(w), h(h) +void +layout::alloc (int w, int h) { assert (sizeof (cell) == 1); + this->w = w; + this->h = h; + // we store the layout in a single contiguous memory layout // first part consists of pointers to each column, followed // by the actual columns (not rows!) - int size = (sizeof (cell *) + sizeof (cell) * h) * w; - + size = (sizeof (cell *) + sizeof (cell) * h) * w; data = (cell **)salloc (size); cell *p = (cell *)(data + w); - for (int x = w; x--; ) + for (int x = 0; x < w; ++x) data [x] = p + x * h; } -layout::~layout () +layout::layout (int w, int h) +{ + alloc (w, h); +} + +layout::layout (layout ©) { - int size = (sizeof (cell *) + sizeof (cell) * h) * w; + alloc (copy.w, copy.h); + + memcpy (data [0], copy.data [0], sizeof (cell) * h * w); +} + +layout::layout (layout &orig, int x1, int y1, int x2, int y2) +{ + w = x2 - x1; + h = y2 - y1; + + // we only allocate space for the pointers + size = sizeof (cell *) * w; + data = (cell **)salloc (size); + + // and now we point back into the original layout + for (int x = 0; x < w; ++x) + data [x] = orig.data [x + x1] + y1; +} +layout::~layout () +{ sfree ((char *)data, size); } void layout::fill (char fill) { - memset (data [0], fill, w * h); + //memset (data [0], fill, w * h); // only when contiguous :/ + fill_rect (0, 0, w, h, fill); } void layout::rect (int x1, int y1, int x2, int y2, char fill) { + --x2; + + memset (data [x1] + y1, fill, y2 - y1); + memset (data [x2] + y1, fill, y2 - y1); + + while (++x1 < x2) + data [x1][y1] = data [x1][y2 - 1] = fill; +} + +void +layout::fill_rect (int x1, int y1, int x2, int y2, char fill) +{ for (; x1 < x2; ++x1) memset (data [x1] + y1, fill, y2 - y1); } void layout::border (char fill) { - for (int i = 0; i < w; i++) data [i][0] = data [i][h - 1] = fill; - for (int j = 0; j < h; j++) data [0][j] = data [w - 1][j] = fill; + rect (0, 0, w, h, fill); } void @@ -158,7 +196,7 @@ typedef fixed_stack pointlist; -static noinline void +static void noinline push_flood_fill (layout &dist, pointlist &seeds, int x, int y) { if (dist [x][y]) @@ -179,8 +217,8 @@ while (--y >= y0) { - if (x > 0) push_flood_fill (dist, seeds, x - 1, y); - if (x < dist.w - 1) push_flood_fill (dist, seeds, x + 1, y); + if (x > 0 && !dist [x - 1][y]) push_flood_fill (dist, seeds, x - 1, y); + if (x < dist.w - 1 && !dist [x + 1][y]) push_flood_fill (dist, seeds, x + 1, y); } } @@ -189,20 +227,26 @@ { for (;;) { - if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) - --x; - else if (x < dist.w - 2 && U8 (dist [x + 1][y]) < d && dist [x + 1][y] > 1) - ++x; - else if (y > 1 && U8 (dist [x][y - 1]) < d && dist [x][y - 1] > 1) - --y; - else if (y < dist.h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) - ++y; - else - break; + point neigh[4]; + int ncnt = 0; + + if (x > 0 && U8 (dist [x - 1][y]) <= d && dist [x - 1][y] > 1) neigh [ncnt++] = point (x - 1, y); + if (x < dist.w - 1 && U8 (dist [x + 1][y]) <= d && dist [x + 1][y] > 1) neigh [ncnt++] = point (x + 1, y); + if (y > 0 && U8 (dist [x][y - 1]) <= d && dist [x][y - 1] > 1) neigh [ncnt++] = point (x, y - 1); + if (y < dist.h - 1 && U8 (dist [x][y + 1]) <= d && dist [x][y + 1] > 1) neigh [ncnt++] = point (x, y + 1); + + if (!ncnt) + return; + + point &p = neigh [rmg_rndm (ncnt)]; + + seeds.push (p); + + x = p.x; + y = p.y; d = dist [x][y]; dist [x][y] = 1; - seeds.push (point (x, y)); } } @@ -219,43 +263,34 @@ seeds.push (point (x, y)); } -void -layout::isolation_remover (bool dirty) +// isolation remover, works on a "distance" map +// the map must be initialised with 0 == rooms, 255 = walls +static void noinline +isolation_remover (layout &dist) { - layout dist (w, h); - // dist contains // 0 == invisited rooms // 1 == visited rooms // 2+ shortest distance to random near room - // phase 1, initialise dist array, find seed + // phase 1, find seed int cnt = 0; int x, y; - for (int i = 0; i < w; ++i) - for (int j = 0; j < h; ++j) - { - if (data [i][j] == '#') - dist [i][j] = U8 (255); - else - { - dist [i][j] = 0; - if (!rmg_rndm (++cnt)) - x = i, y = j; - } - } + for (int i = 0; i < dist.w; ++i) + for (int j = 0; j < dist.h; ++j) + if (!dist [i][j] && !rmg_rndm (++cnt)) + x = i, y = j; if (!cnt) { // map is completely massive, this is not good, // so make it empty instead. - clear (); - border (); + dist.fill (1); return; } - fixed_stack seeds (w * h * 5); + fixed_stack seeds (dist.w * dist.h * 5); // found first free space - picking the first one gives // us a slight bias for tunnels, but usually you won't @@ -277,13 +312,8 @@ if (!dist [x][y]) { // found new isolated area, make tunnel - if (!dirty) - push_flood_fill (dist, seeds, x, y); - + push_flood_fill (dist, seeds, x, y); make_tunnel (dist, seeds, x, y, 255); - - if (dirty) - push_flood_fill (dist, seeds, x, y); } else { @@ -296,52 +326,24 @@ if (y > 0) maybe_push (dist, seeds, x, y - 1, d); } } - - // now copy the tunnels over - for (int x = 0; x < w; ++x) - for (int y = 0; y < h; ++y) - if (data [x][y] == '#' && dist [x][y] == 1) - data [x][y] = 0; } -///////////////////////////////////////////////////////////////////////////// - -// inspired mostly by http://www.jimrandomh.org/misc/caves.txt void -layout::gen_cave (int subtype) +layout::isolation_remover () { - switch (subtype) - { - // a rough cave - case 0: - fill_rand (rmg_rndm (80, 95)); - break; + layout dist (w - 2, h - 2); // map without border - // corridors - case 1: - fill_rand (rmg_rndm (5, 40)); - erode_1_2 (5, 2, 10); - erode_1_2 (5, -1, 10); - erode_1_2 (5, 2, 1); - break; + for (int x = 1; x < w - 1; ++x) + for (int y = 1; y < h - 1; ++y) + dist [x - 1][y - 1] = data [x][y] == '#' ? U8 (255) : 0; - // somewhat open, roundish - case 2: - fill_rand (45); - erode_1_2 (5, 0, 5); - erode_1_2 (5, 1, 1); - break; + ::isolation_remover (dist); - // wide open, some room-like structures - case 3: - fill_rand (45); - erode_1_2 (5, 2, 4); - erode_1_2 (5, -1, 3); - break; - } - - border (); - isolation_remover (); + // now copy the tunnels over + for (int x = 1; x < w - 1; ++x) + for (int y = 1; y < h - 1; ++y) + if (data [x][y] == '#' && dist [x - 1][y - 1] == 1) + data [x][y] = 0; } ///////////////////////////////////////////////////////////////////////////// @@ -773,6 +775,87 @@ ///////////////////////////////////////////////////////////////////////////// +// inspired mostly by http://www.jimrandomh.org/misc/caves.txt +void +layout::gen_cave (int subtype) +{ + switch (subtype) + { + // a rough cave + case 0: + fill_rand (rmg_rndm (85, 97)); + break; + + // corridors + case 1: + fill_rand (rmg_rndm (5, 40)); + erode_1_2 (5, 2, 10); + erode_1_2 (5, -1, 10); + erode_1_2 (5, 2, 1); + break; + + // somewhat open, roundish + case 2: + fill_rand (45); + erode_1_2 (5, 0, 5); + erode_1_2 (5, 1, 1); + break; + + // wide open, some room-like structures + case 3: + fill_rand (45); + erode_1_2 (5, 2, 4); + erode_1_2 (5, -1, 3); + break; + } + + border (); + isolation_remover (); +} + +static void +gen_mixed_ (layout &maze, random_map_params *RP, int dir) +{ + if (maze.w < 20 && maze.h < 20 && !rmg_rndm (3)) + dir = 2; // stop recursion randomly + + if (dir == 0 && maze.w > 16) + { + int m = rmg_rndm (8, maze.w - 8); + + layout m1 (maze, 0, 0, m , maze.h); gen_mixed_ (m1, RP, !dir); + layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); + } + else if (dir == 1 && maze.h > 16) + { + int m = rmg_rndm (8, maze.h - 8); + + layout m1 (maze, 0, 0, maze.w, m ); gen_mixed_ (m1, RP, !dir); + layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP, !dir); + } + else + { + RP->map_layout_style = rmg_rndm (NROFLAYOUTS - 2) + 1; + + if (RP->map_layout_style == LAYOUT_MULTIPLE) + ++RP->map_layout_style; + + maze.generate (RP); + } +} + +// recursive subdivision with random sublayouts +static void +gen_mixed (layout &maze, random_map_params *RP) +{ + random_map_params &rp = *new random_map_params (RP); + gen_mixed_ (maze, &rp, rmg_rndm (2)); + delete &rp; + + maze.border (); + maze.isolation_remover (); +} + /* function selects the maze function and gives it whatever arguments it needs. */ void @@ -841,21 +924,13 @@ break; + case LAYOUT_MULTIPLE: + gen_mixed (*this, RP); + break; + default: abort (); } - - /* rotate the maze randomly */ - rotate (rmg_rndm (4)); - - symmetrize (RP->symmetry_used); - -#if 0 - print ();//D -#endif - - if (RP->expand2x) - expand2x (); } //-GPL @@ -867,16 +942,12 @@ { rmg_rndm.seed (time (0)); - for(int i=1;i<10;i) + for(int i=1;i<100;i++) { - layout maze (10, 10); - maze.fill_rand (90); + layout maze (40, 25); + maze.fill_rand (85); maze.border (); maze.isolation_remover (); - maze.doorify (); - maze.symmetrize (rmg_rndm (2, 4)); - maze.rotate (rmg_rndm (4)); - maze.expand2x (); maze.print (); }