--- deliantra/server/random_maps/layout.C 2010/07/02 03:40:14 1.5 +++ deliantra/server/random_maps/layout.C 2010/07/02 15:03:57 1.7 @@ -25,7 +25,7 @@ #include #include -Layout::Layout (int w, int h) +layout::layout (int w, int h) : w(w), h(h) { int size = (sizeof (char *) + sizeof (char) * h) * w; @@ -38,7 +38,7 @@ col [x] = data + x * h; } -Layout::~Layout () +layout::~layout () { int size = (sizeof (char *) + sizeof (char) * h) * w; @@ -46,26 +46,26 @@ } void -Layout::fill (char fill) +layout::fill (char fill) { memset (col [0], fill, w * h); } void -Layout::rect (int x1, int y1, int x2, int y2, char fill) +layout::rect (int x1, int y1, int x2, int y2, char fill) { for (; x1 < x2; ++x1) memset (col [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; } void -Layout::fill_rand (int percent) +layout::fill_rand (int percent) { percent = lerp (percent, 0, 100, 0, 256); @@ -78,9 +78,9 @@ // 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--) { @@ -124,7 +124,7 @@ ///////////////////////////////////////////////////////////////////////////// void -Layout::print () const +layout::print () const { for (int y = 0; y < h; y++) { @@ -154,7 +154,7 @@ typedef fixed_stack pointlist; static noinline void -push_flood_fill (Layout &dist, pointlist &seeds, int x, int y) +push_flood_fill (layout &dist, pointlist &seeds, int x, int y) { if (dist [x][y]) return; @@ -180,7 +180,7 @@ } 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) { for (;;) { @@ -202,7 +202,7 @@ } 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,10 +214,10 @@ seeds.push (point (x, y)); } -static void -isolation_remover (Layout &maze, bool dirty) +void +layout::isolation_remover (bool dirty) { - Layout dist (maze.w, maze.h); + layout dist (w, h); // dist contains // 0 == invisited rooms @@ -228,10 +228,10 @@ int cnt = 0; int x, y; - for (int i = 0; i < maze.w; ++i) - for (int j = 0; j < maze.h; ++j) + for (int i = 0; i < w; ++i) + for (int j = 0; j < h; ++j) { - if (maze [i][j] == '#') + if (col [i][j] == '#') dist [i][j] = U8 (255); else { @@ -242,9 +242,15 @@ } if (!cnt) - return; + { + // map is completely massive, this is not good, + // so make it empty instead. + clear (); + border (); + return; + } - fixed_stack seeds (maze.w * maze.h * 5); + fixed_stack seeds (w * h * 5); // found first free space - picking the first one gives // us a slight bias for tunnels, but usually you won't @@ -287,23 +293,17 @@ } // now copy the tunnels over - for (int x = 0; x < maze.w; ++x) - for (int y = 0; y < maze.h; ++y) - if (maze [x][y] == '#' && dist [x][y] == 1) - maze [x][y] = 0; -} - -void -Layout::isolation_remover (bool dirty) -{ - ::isolation_remover (*this, dirty); + 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::gen_cave (int subtype) { switch (subtype) { @@ -343,9 +343,9 @@ //+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 */ @@ -394,12 +394,12 @@ * 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 ); @@ -453,13 +453,13 @@ //-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++) @@ -472,7 +472,7 @@ 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++) @@ -495,7 +495,7 @@ //+GPL /* - * Expands a layout by 2x in each dimension. + * Expands a maze by 2x in each dimension. * H. S. Teoh */ @@ -505,9 +505,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 +520,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 +544,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 +582,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,21 +598,21 @@ 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 (); @@ -630,11 +630,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 +731,7 @@ } void -Layout::roomify () +layout::roomify () { int tries = w * h / 30; @@ -768,10 +768,10 @@ ///////////////////////////////////////////////////////////////////////////// -/* function selects the layout function and gives it whatever +/* 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) { @@ -840,7 +840,7 @@ abort (); } - /* rotate the layout randomly */ + /* rotate the maze randomly */ rotate (rmg_rndm (4)); symmetrize (RP->symmetry_used); @@ -860,14 +860,18 @@ { demo () { - Layout maze (40, 25); rmg_rndm.seed (time (0)); for(int i=1;i<10;i) { - maze.fill_rand (97); + layout maze (10, 10); + maze.fill_rand (90); maze.border (); maze.isolation_remover (); + maze.doorify (); + maze.symmetrize (rmg_rndm (2, 4)); + maze.rotate (rmg_rndm (4)); + maze.expand2x (); maze.print (); }