--- deliantra/server/random_maps/layout.C 2010/07/02 04:05:15 1.6 +++ deliantra/server/random_maps/layout.C 2010/07/04 01:01:42 1.21 @@ -25,62 +25,105 @@ #include #include -Layout::Layout (int w, int h) -: w(w), h(h) +void +layout::alloc (int w, int h) { - int size = (sizeof (char *) + sizeof (char) * h) * w; + 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!) + size = (sizeof (cell *) + sizeof (cell) * h) * w; + data = (cell **)salloc (size); - col = (char **)salloc (size); + cell *p = (cell *)(data + w); - char *data = (char *)(col + w); + for (int x = 0; x < w; ++x) + data [x] = p + x * h; +} - for (int x = w; x--; ) - col [x] = data + x * h; +layout::layout (int w, int h) +{ + alloc (w, h); } -Layout::~Layout () +layout::layout (layout ©) { - int size = (sizeof (char *) + sizeof (char) * h) * w; + alloc (copy.w, copy.h); - sfree ((char *)col, size); + 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); // only when contiguous :/ + fill_rect (0, 0, w, h, fill); } void -Layout::fill (char fill) +layout::rect (int x1, int y1, int x2, int y2, char fill) { - memset (col [0], fill, w * h); + --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::rect (int x1, int y1, int x2, int y2, char fill) +layout::fill_rect (int x1, int y1, int x2, int y2, char fill) { for (; x1 < x2; ++x1) - memset (col [x1] + y1, fill, y2 - y1); + memset (data [x1] + y1, fill, y2 - y1); } -void Layout::border (char fill) +void layout::border (char fill) { - for (int i = 0; i < w; i++) col [i][0] = col [i][h - 1] = fill; - for (int j = 0; j < h; j++) col [0][j] = col [w - 1][j] = fill; + rect (0, 0, w, h, fill); } void -Layout::fill_rand (int percent) +layout::fill_rand (int percent) { percent = lerp (percent, 0, 100, 0, 256); for (int x = w - 1; --x > 0; ) for (int y = h - 1; --y > 0; ) - col [x][y] = rmg_rndm (256) > percent ? 0 : '#'; + data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; } ///////////////////////////////////////////////////////////////////////////// // erode by cellular automata void -Layout::erode_1_2 (int c1, int c2, int repeat) +layout::erode_1_2 (int c1, int c2, int repeat) { - Layout neu (w, h); + layout neu (w, h); while (repeat--) { @@ -106,7 +149,7 @@ int nx = x + dds [i][0]; int ny = y + dds [i][1]; - if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !col [nx][ny]) + if (!IN_RANGE_EXC (nx, 0, w) || !IN_RANGE_EXC (ny, 0, h) || !data [nx][ny]) { n1 += dds [i][2]; n2++; @@ -124,13 +167,13 @@ ///////////////////////////////////////////////////////////////////////////// void -Layout::print () const +layout::print () const { for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { - U8 c = (U8)col [x][y]; + U8 c = (U8)data [x][y]; if (!c) c = ' '; @@ -153,8 +196,8 @@ typedef fixed_stack pointlist; -static noinline void -push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) +static void noinline +push_flood_fill (layout &dist, pointlist &seeds, int x, int y) { if (dist [x][y]) return; @@ -174,35 +217,43 @@ 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); } } static inline void -make_tunnel (Layout &dist, pointlist &seeds, int x, int y, U8 d) +make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) { 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; + + d += perturb > 1; + + 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 [perturb ? rmg_rndm (ncnt) : 0]; + + seeds.push (p); + + x = p.x; + y = p.y; d = dist [x][y]; dist [x][y] = 1; - seeds.push (point (x, y)); } } static void inline -maybe_push (Layout &dist, pointlist &seeds, int x, int y, U8 d) +maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) { char &D = dist [x][y]; @@ -214,43 +265,36 @@ 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, unsigned int perturb = 2) { - 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 + clamp_it (perturb, 0, 2); + + // 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 (col [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 @@ -272,13 +316,8 @@ if (!dist [x][y]) { // found new isolated area, make tunnel - if (!dirty) - push_flood_fill (dist, seeds, x, y); - - make_tunnel (dist, seeds, x, y, 255); - - if (dirty) - push_flood_fill (dist, seeds, x, y); + push_flood_fill (dist, seeds, x, y); + make_tunnel (dist, seeds, x, y, 254, perturb); } else { @@ -291,67 +330,37 @@ 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 (col [x][y] == '#' && dist [x][y] == 1) - col [x][y] = 0; } -///////////////////////////////////////////////////////////////////////////// - -// inspired mostly by http://www.jimrandomh.org/misc/caves.txt void -Layout::gen_cave (int subtype) +layout::isolation_remover (int perturb) { - 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, perturb); - // 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; } ///////////////////////////////////////////////////////////////////////////// //+GPL -/* puts doors at appropriate locations in a layout. */ +/* puts doors at appropriate locations in a maze. */ void -Layout::doorify () +layout::doorify () { int ndoors = w * h / 60; /* reasonable number of doors. */ - int doorlocs = 0; /* # of available doorlocations */ - uint16 *doorlist_x = salloc (w * h); - uint16 *doorlist_y = salloc (w * h); + fixed_stack doorloc (w * h); /* make a list of possible door locations */ for (int i = 1; i < w - 1; i++) @@ -360,46 +369,33 @@ int sindex = surround_flag (*this, i, j); if (sindex == 3 || sindex == 12) /* these are possible door sindex */ - { - doorlist_x [doorlocs] = i; - doorlist_y [doorlocs] = j; - doorlocs++; - } + doorloc.push (point (i, j)); } - while (ndoors > 0 && doorlocs > 0) + while (ndoors && doorloc.size) { - int di = rmg_rndm (doorlocs); - int i = doorlist_x [di]; - int j = doorlist_y [di]; - int sindex = surround_flag (*this, i, j); + point p = doorloc.remove (rmg_rndm (doorloc.size)); + + int sindex = surround_flag (*this, p.x, p.y); if (sindex == 3 || sindex == 12) /* these are possible door sindex */ { - col [i][j] = 'D'; - ndoors--; + data [p.x][p.y] = 'D'; + --ndoors; } - - /* reduce the size of the list */ - doorlocs--; - doorlist_x[di] = doorlist_x [doorlocs]; - doorlist_y[di] = doorlist_y [doorlocs]; } - - sfree (doorlist_x, w * h); - sfree (doorlist_y, w * h); } /* takes a map and makes it symmetric: adjusts Xsize and * Ysize to produce a symmetric map. */ void -Layout::symmetrize (int symmetry) +layout::symmetrize (int symmetry) { if (symmetry == SYMMETRY_NONE) return; - Layout sym_layout ( + layout sym_layout ( symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h ); @@ -409,7 +405,7 @@ for (int j = 0; j < sym_layout.h; j++) { sym_layout[i ][j] = - sym_layout[sym_layout.w - i - 1][j] = col[i][j]; + sym_layout[sym_layout.w - i - 1][j] = data [i][j]; } if (symmetry == SYMMETRY_Y) @@ -417,7 +413,7 @@ for (int j = 0; j < sym_layout.h / 2 + 1; j++) { sym_layout[i][j ] = - sym_layout[i][sym_layout.h - j - 1] = col[i][j]; + sym_layout[i][sym_layout.h - j - 1] = data [i][j]; } if (symmetry == SYMMETRY_XY) @@ -427,7 +423,7 @@ sym_layout[i ][j ] = sym_layout[i ][sym_layout.h - j - 1] = sym_layout[sym_layout.w - i - 1][j ] = - sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = col[i][j]; + sym_layout[sym_layout.w - i - 1][sym_layout.h - j - 1] = data [i][j]; } /* need to run the isolation remover for some layouts */ @@ -453,17 +449,17 @@ //-GPL void -Layout::rotate (int rotation) +layout::rotate (int rotation) { switch (rotation & 3) { case 2: /* a reflection */ { - Layout new_layout (w, h); + layout new_layout (w, h); for (int i = 0; i < w; i++) /* copy a reflection back */ for (int j = 0; j < h; j++) - new_layout [i][j] = col [w - i - 1][h - j - 1]; + new_layout [i][j] = data [w - i - 1][h - j - 1]; swap (new_layout); } @@ -472,17 +468,17 @@ case 1: case 3: { - Layout new_layout (h, w); + layout new_layout (h, w); if (rotation == 1) /* swap x and y */ for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) - new_layout [j][i] = col [i][j]; + new_layout [j][i] = data [i][j]; if (rotation == 3) /* swap x and y */ for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) - new_layout [j][i] = col [w - i - 1][h - j - 1]; + new_layout [j][i] = data [w - i - 1][h - j - 1]; swap (new_layout); } @@ -495,7 +491,7 @@ //+GPL /* - * Expands a layout by 2x in each dimension. + * Expands a maze by 2x in each dimension. * H. S. Teoh */ @@ -505,9 +501,9 @@ * \0 \0 */ static void inline -expand_misc (Layout &newlayout, int i, int j, Layout &layout) +expand_misc (layout &newlayout, int i, int j, layout &maze) { - newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; + newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = maze[i][j]; /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that * for us.) */ } @@ -520,19 +516,19 @@ * and the possible combinations thereof. */ static int noinline -calc_pattern (char ch, Layout &layout, int i, int j) +calc_pattern (char ch, layout &maze, int i, int j) { int pattern = 0; - if (i + 1 < layout.w && layout[i + 1][j] == ch) + if (i + 1 < maze.w && maze[i + 1][j] == ch) pattern |= 1; - if (j + 1 < layout.h) + if (j + 1 < maze.h) { - if (layout[i][j + 1] == ch) + if (maze[i][j + 1] == ch) pattern |= 2; - if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) + if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) pattern |= 4; } @@ -544,33 +540,33 @@ * walls. */ static void inline -expand_wall (Layout &newlayout, int i, int j, Layout &layout) +expand_wall (layout &newlayout, int i, int j, layout &maze) { - int wall_pattern = calc_pattern ('#', layout, i, j); - int door_pattern = calc_pattern ('D', layout, i, j); + int wall_pattern = calc_pattern ('#', maze, i, j); + int door_pattern = calc_pattern ('D', maze, i, j); int both_pattern = wall_pattern | door_pattern; newlayout[i * 2][j * 2] = '#'; - if (i + 1 < layout.w) + if (i + 1 < maze.w) { if (both_pattern & 1) { /* join walls/doors to the right */ /* newlayout[i*2+1][j*2] = '#'; */ - newlayout[i * 2 + 1][j * 2] = layout[i + 1][j]; + newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; } } - if (j + 1 < layout.h) + if (j + 1 < maze.h) { if (both_pattern & 2) { /* join walls/doors to the bottom */ /* newlayout[i*2][j*2+1] = '#'; */ - newlayout[i * 2][j * 2 + 1] = layout[i][j + 1]; + newlayout[i * 2][j * 2 + 1] = maze[i][j + 1]; } if (wall_pattern == 7) - { /* if orig layout is a 2x2 wall block, + { /* if orig maze is a 2x2 wall block, * we fill the result with walls. */ newlayout[i * 2 + 1][j * 2 + 1] = '#'; } @@ -582,10 +578,10 @@ * that it doesn't know how to correctly expand. */ static void inline -expand_door (Layout &newlayout, int i, int j, Layout &layout) +expand_door (layout &newlayout, int i, int j, layout &maze) { - int wall_pattern = calc_pattern ('#', layout, i, j); - int door_pattern = calc_pattern ('D', layout, i, j); + int wall_pattern = calc_pattern ('#', maze, i, j); + int door_pattern = calc_pattern ('D', maze, i, j); int join_pattern; /* Doors "like" to connect to walls more than other doors. If there is @@ -598,27 +594,27 @@ newlayout[i * 2][j * 2] = 'D'; - if (i + 1 < layout.w) + if (i + 1 < maze.w) if (join_pattern & 1) /* there is a door/wall to the right */ newlayout[i * 2 + 1][j * 2] = 'D'; - if (j + 1 < layout.h) + if (j + 1 < maze.h) if (join_pattern & 2) /* there is a door/wall below */ newlayout[i * 2][j * 2 + 1] = 'D'; } void -Layout::expand2x () +layout::expand2x () { - Layout new_layout (w * 2 - 1, h * 2 - 1); + layout new_layout (w * 2 - 1, h * 2 - 1); new_layout.clear (); for (int i = 0; i < w; i++) for (int j = 0; j < h; j++) - switch (col [i][j]) + switch (data [i][j]) { case '#': expand_wall (new_layout, i, j, *this); break; case 'D': expand_door (new_layout, i, j, *this); break; @@ -630,11 +626,11 @@ ///////////////////////////////////////////////////////////////////////////// -/* checks the layout to see if I can stick a horizontal(dir = 0) wall +/* checks the maze to see if I can stick a horizontal(dir = 0) wall (or vertical, dir == 1) here which ends up on other walls sensibly. */ static int -can_make_wall (const Layout &maze, int dx, int dy, int dir) +can_make_wall (const layout &maze, int dx, int dy, int dir) { int i1; int length = 0; @@ -731,7 +727,7 @@ } void -Layout::roomify () +layout::roomify () { int tries = w * h / 30; @@ -766,12 +762,125 @@ } } +//-GPL + ///////////////////////////////////////////////////////////////////////////// -/* function selects the layout function and gives it whatever +// 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, some room-like structures + case 2: + fill_rand (45); + erode_1_2 (5, 2, 4); + erode_1_2 (5, -1, 3); + break; + + // wide open, roundish + case 3: + fill_rand (45); + erode_1_2 (5, 0, 5); + erode_1_2 (5, 1, 1); + break; + } + + border (); + isolation_remover (1); +} + +void +layout::gen_castle () +{ + fill ('#'); + + for (int n = w * h / 30 + 1; n--; ) + { + int rw = rmg_rndm (6, 10); + int rh = rmg_rndm (6, 10); + + if (rw > w || rh > h) + continue; + + int rx = rmg_rndm (0, w - rw); + int ry = rmg_rndm (0, h - rh); + + rect (rx, ry, rx + rw, ry + rh, '#'); + fill_rect (rx + 1, ry + 1, rx + rw - 1, ry + rh - 1, 0); + } + + border (); + isolation_remover (0); +} + +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 + else + dir = maze.w > maze.h; + + 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); + layout m2 (maze, m, 0, maze.w, maze.h); gen_mixed_ (m2, RP); + } + 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); + layout m2 (maze, 0, m, maze.w, maze.h); gen_mixed_ (m2, RP); + } + 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); + delete &rp; + + maze.border (); + maze.isolation_remover (0); +} + +//+GPL + +/* function selects the maze function and gives it whatever arguments it needs. */ void -Layout::generate (random_map_params *RP) +layout::generate (random_map_params *RP) { switch (RP->map_layout_style) { @@ -836,43 +945,60 @@ break; - default: - abort (); - } + case LAYOUT_CASTLE: + gen_castle (); - /* rotate the layout randomly */ - rotate (rmg_rndm (4)); + if (rmg_rndm (2)) + doorify (); - symmetrize (RP->symmetry_used); + break; -#if 0 - print ();//D -#endif + case LAYOUT_MULTIPLE: + gen_mixed (*this, RP); + break; - if (RP->expand2x) - expand2x (); + default: + abort (); + } } //-GPL #if 0 +static void +gen_village (layout &maze) +{ + maze.clear (); + maze.border (); + + for (int n = maze.w * maze.h / 200 + 1; n--; ) + { + int rw = rmg_rndm (6, 10); + int rh = rmg_rndm (6, 10); + + int rx = rmg_rndm (2, maze.w - rw - 2); + int ry = rmg_rndm (2, maze.h - rh - 2); + + maze.rect (rx, ry, rx + rw, ry + rh, '#'); + } + + maze.border (); + maze.isolation_remover (2); +} + static struct demo { demo () { 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); - maze.border (); - maze.isolation_remover (); + layout maze (40, 30); + gen_village (maze); maze.doorify (); - maze.symmetrize (rmg_rndm (2, 4)); - maze.rotate (rmg_rndm (4)); - maze.expand2x (); maze.print (); + exit(0); } exit (1);