--- deliantra/server/random_maps/layout.C 2010/06/30 20:51:02 1.1 +++ deliantra/server/random_maps/layout.C 2010/08/22 20:23:06 1.27 @@ -2,6 +2,7 @@ * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) Crossfire Development Team (restored, original file without copyright notice) * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the @@ -21,57 +22,168 @@ */ #include -#include +#include +#include -LayoutData::LayoutData (int w, int h) -: w(w), h(h) +void noinline +layout::alloc (int w, int h) { - int size = (sizeof (char *) + sizeof (char) * h) * w; + assert (sizeof (cell) == 1); - col = (char **)salloc (size); + this->w = w; + this->h = h; - char *data = (char *)(col + w); + // 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); - for (int x = w; x--; ) - col [x] = data + x * h; + cell *p = (cell *)(data + w); + + for (int x = 0; x < w; ++x) + data [x] = p + x * h; +} + +layout::layout (int w, int h) +{ + alloc (w, h); } -LayoutData::~LayoutData () +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); } -void -LayoutData::fill (char fill) +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 () { - memset (col [0], fill, w * h); + sfree ((char *)data, size); } -void -LayoutData::rect (int x1, int y1, int x2, int y2, char fill) +void noinline +layout::fill (char fill) +{ + //memset (data [0], fill, w * h); // only when contiguous :/ + fill_rect (0, 0, w, h, fill); +} + +void noinline +layout::replace (char from, char to) +{ + for (int x = 0; x < w; ++x) + for (int y = 0; y < h; ++y) + if (data [x][y] == from) + data [x][y] = to; +} + +void noinline +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 noinline +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) +{ + rect (0, 0, w, h, fill); +} + +void noinline +layout::fill_rand (int percent) +{ + percent = lerp (percent, 0, 100, 0, 256); + + for (int x = 0; x < w; ++x) + for (int y = 0; y < h; ++y) + data [x][y] = rmg_rndm (256) > percent ? 0 : '#'; } -void LayoutData::border (char fill) +///////////////////////////////////////////////////////////////////////////// + +// erode by cellular automata +void noinline +layout::erode_1_2 (int c1, int c2, int repeat) { - 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; + layout neu (w, h); + + while (repeat--) + { + for (int x = 0; x < w; ++x) + { + coroapi::cede_to_tick (); + + for (int y = 0; y < h; ++y) + { + int n1 = 0, n2 = 0; + + // a 5x5 area, dx, dy, distance (1 == <= 1, 0 <= 2) + static I8 dds[][3] = { + { -2, -1, 0 }, { -2, 0, 0 }, { -2, 1, 0 }, + { -1, -2, 0 }, { -1, -1, 1 }, { -1, 0, 1 }, { -1, 1, 1 }, { -1, 2, 0 }, + { 0, -2, 0 }, { 0, -1, 1 }, { 0, 0, 1 }, { 0, 1, 1 }, { 0, 2, 0 }, + { 1, -2, 0 }, { 1, -1, 1 }, { 1, 0, 1 }, { 1, 1, 1 }, { 1, 2, 0 }, + { 2, -1, 0 }, { 2, 0, 0 }, { 2, 1, 0 }, + }; + + for (int i = array_length (dds); i--; ) + { + 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) || !data [nx][ny]) + { + n1 += dds [i][2]; + n2++; + } + } + + neu [x][y] = n1 >= c1 || n2 <= c2 ? '#' : 0; + } + } + + swap (neu); + } } ///////////////////////////////////////////////////////////////////////////// void -Layout::print () +layout::print () const { - for (int y = 0; y < ptr->h; y++) + for (int y = 0; y < h; y++) { - for (int x = 0; x < ptr->w; x++) + for (int x = 0; x < w; x++) { - U8 c = (U8)ptr->col[x][y]; + U8 c = (U8)data [x][y]; if (!c) c = ' '; @@ -94,185 +206,826 @@ typedef fixed_stack pointlist; -static void -room (Layout &layout, int xc, int yc) -{ - layout->rect (xc - 2, yc - 2, xc + 3, yc + 3, 0); -} - -static noinline void -push_flood_fill (Layout maze, Layout dist, pointlist &seeds, int x, int y) +static void noinline +push_flood_fill (layout &dist, pointlist &seeds, int x, int y) { - if (maze [x][y]) + if (dist [x][y]) return; - while (y > 0 && !maze [x][y - 1]) + while (y > 0 && !dist [x][y - 1]) --y; int y0 = y; - while (y < maze->h && !maze [x][y]) + while (y < dist.h && !dist [x][y]) { seeds.push (point (x, y)); - maze [x][y] = 1; dist [x][y] = 1; ++y; } while (--y >= y0) { - if (x > 0) push_flood_fill (maze, dist, seeds, x - 1, y); - if (x < maze->w - 1) push_flood_fill (maze, 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 maze, Layout dist, pointlist &seeds, int x, int y, U8 d) +static void inline +make_tunnel (layout &dist, pointlist &seeds, int x, int y, U8 d, int perturb) { for (;;) { - seeds.push (point (x, y)); + 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); - maze [x][y] = 1; + x = p.x; + y = p.y; + + d = dist [x][y]; dist [x][y] = 1; + } +} + +static void inline +maybe_push (layout &dist, pointlist &seeds, int x, int y, U8 d) +{ + char &D = dist [x][y]; + + if (U8 (D) > d) // if wall and higher distance, lower distance + D = d; + else if (D) // otherwise, if it's no room, this space is uninteresting + return; + + seeds.push (point (x, y)); +} + +// 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) +{ + // dist contains + // 0 == invisited rooms + // 1 == visited rooms + // 2+ shortest distance to random near room + + clamp_it (perturb, 0, 2); + + // phase 1, find seed + int cnt = 0; + int x, y; + + 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. + dist.fill (1); + return; + } + + 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 + // notice that in-game + seeds.push (point (x, y)); + + // phase 2, while we have seeds, if + // seed is empty, floodfill, else grow + + int rem_index = 0; // used to remove "somewhat ordered" - if (x > 1 && U8 (dist [x - 1][y]) < d && dist [x - 1][y] > 1) - --x; - else if (x < maze->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 < maze->h - 2 && U8 (dist [x][y + 1]) < d && dist [x][y + 1] > 1) - ++y; + while (seeds.size) + { + coroapi::cede_to_tick (); + + int i = perturb + ? rmg_rndm (max (0, seeds.size - 8), seeds.size - 1) + : rem_index ++ % seeds.size; + + point p = seeds.remove (i); + + x = p.x; + y = p.y; + + if (!dist [x][y]) + { + // found new isolated area, make tunnel + push_flood_fill (dist, seeds, x, y); + make_tunnel (dist, seeds, x, y, 254, perturb); + } else + { + // nothing here, continue to expand + U8 d = U8 (dist [x][y]) + 1; + + if (x < dist.w - 1) maybe_push (dist, seeds, x + 1, y, d); + if (x > 0) maybe_push (dist, seeds, x - 1, y, d); + if (y < dist.h - 1) maybe_push (dist, seeds, x, y + 1, d); + if (y > 0) maybe_push (dist, seeds, x, y - 1, d); + } + } +} + +void +layout::isolation_remover (int perturb) +{ + layout dist (w - 2, h - 2); // map without border + + 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; + + ::isolation_remover (dist, perturb); + + // 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 maze. */ +void +layout::doorify () +{ + int ndoors = w * h / 60; /* reasonable number of doors. */ + + coroapi::cede_to_tick (); + + fixed_stack doorloc (w * h); + + /* make a list of possible door locations */ + for (int i = 1; i < w - 1; i++) + for (int j = 1; j < h - 1; j++) + { + int sindex = surround_flag (*this, i, j); + + if (sindex == 3 || sindex == 12) /* these are possible door sindex */ + doorloc.push (point (i, j)); + } + + while (ndoors && doorloc.size) + { + 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 */ + { + data [p.x][p.y] = 'D'; + --ndoors; + } + } +} + +/* takes a map and makes it symmetric: adjusts Xsize and + * Ysize to produce a symmetric map. + */ +void +layout::symmetrize (int symmetry) +{ + if (symmetry == SYMMETRY_NONE) + return; + + layout sym_layout ( + symmetry == SYMMETRY_X || symmetry == SYMMETRY_XY ? w * 2 - 3 : w, + symmetry == SYMMETRY_Y || symmetry == SYMMETRY_XY ? h * 2 - 3 : h + ); + + if (symmetry == SYMMETRY_X) + for (int i = 0; i < sym_layout.w / 2 + 1; i++) + for (int j = 0; j < sym_layout.h; j++) + { + sym_layout[i ][j] = + sym_layout[sym_layout.w - i - 1][j] = data [i][j]; + } + + if (symmetry == SYMMETRY_Y) + for (int i = 0; i < sym_layout.w; i++) + for (int j = 0; j < sym_layout.h / 2 + 1; j++) + { + sym_layout[i][j ] = + sym_layout[i][sym_layout.h - j - 1] = data [i][j]; + } + + if (symmetry == SYMMETRY_XY) + for (int i = 0; i < sym_layout.w / 2 + 1; i++) + for (int j = 0; j < sym_layout.h / 2 + 1; j++) + { + 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] = data [i][j]; + } + + /* need to run the isolation remover for some layouts */ +#if 0 + switch (RP->map_layout_style) + { + case LAYOUT_ONION: + case LAYOUT_SNAKE: + case LAYOUT_SQUARE_SPIRAL: + // safe break; - d = dist [x][y]; + default: + sym_layout.isolation_remover (); + break; } +#endif + sym_layout.isolation_remover (); + + swap (sym_layout); } -static void -isolation_remover (Layout maze, bool dirty) +//-GPL + +void +layout::rotate (int rotation) { - Layout dist (maze->w, maze->h); + coroapi::cede_to_tick (); + + switch (rotation & 3) + { + case 2: /* a reflection */ + { + layout new_layout (w, h); - dist->fill (255); + for (int i = 0; i < w; i++) /* copy a reflection back */ + for (int j = 0; j < h; j++) + new_layout [i][j] = data [w - i - 1][h - j - 1]; - fixed_stack seeds (maze->w * maze->h * 4); + swap (new_layout); + } + break; - // phase 1, find seed - for (int x = 1; x < maze->w; ++x) - for (int y = 1; y < maze->h; ++y) - if (!maze [x][y]) + case 1: + case 3: { - seeds.push (point (x, y)); + layout new_layout (h, w); - // phase 2, while we have seeds, if - // seed is empty, floodfill, else grow + 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] = 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] = data [w - i - 1][h - j - 1]; - while (seeds.size) - { - point p = seeds.remove (rmg_rndm (seeds.size)); + swap (new_layout); + } + break; + } +} - x = p.x; - y = p.y; +///////////////////////////////////////////////////////////////////////////// - if (!maze [x][y]) - { - // found new isolated area, make tunnel? - if (!dirty) - push_flood_fill (maze, dist, seeds, x, y); +//+GPL - make_tunnel (maze, dist, seeds, x, y, 255); +/* + * Expands a maze by 2x in each dimension. + * H. S. Teoh + */ - if (dirty) - push_flood_fill (maze, dist, seeds, x, y); - } - else - { - U8 d = U8 (dist [x][y]) + 1; +/* Copy the old tile X into the new one at location (i*2, j*2) and + * fill up the rest of the 2x2 result with \0: + * X ---> X \0 + * \0 \0 + */ +static void inline +expand_misc (layout &newlayout, int i, int j, layout &maze) +{ + 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.) */ +} - if (x < maze->w - 1 && U8 (dist [x + 1][y]) > d) - { - dist [x + 1][y] = d; - seeds.push (point (x + 1, y)); - } +/* Returns a bitmap that represents which squares on the right and bottom + * edges of a square (i,j) match the given character: + * 1 match on (i+1, j) + * 2 match on (i, j+1) + * 4 match on (i+1, j+1) + * and the possible combinations thereof. + */ +static int noinline +calc_pattern (char ch, layout &maze, int i, int j) +{ + int pattern = 0; - if (x > 0 && U8 (dist [x - 1][y]) > d) - { - dist [x - 1][y] = d; - seeds.push (point (x - 1, y)); - } + if (i + 1 < maze.w && maze[i + 1][j] == ch) + pattern |= 1; - if (y < maze->h - 1 && U8 (dist [x][y + 1]) > d) - { - dist [x][y + 1] = d; - seeds.push (point (x, y + 1)); - } + if (j + 1 < maze.h) + { + if (maze[i][j + 1] == ch) + pattern |= 2; - if (y > 0 && U8 (dist [x][y - 1]) > d) - { - dist [x][y - 1] = d; - seeds.push (point (x, y - 1)); - } - } - } + if (i + 1 < maze.w && maze[i + 1][j + 1] == ch) + pattern |= 4; + } - goto success; + return pattern; +} + +/* Expand a wall. This function will try to sensibly connect the resulting + * wall to adjacent wall squares, so that the result won't have disconnected + * walls. + */ +static void inline +expand_wall (layout &newlayout, int i, int j, layout &maze) +{ + 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 < maze.w) + { + if (both_pattern & 1) + { /* join walls/doors to the right */ +/* newlayout[i*2+1][j*2] = '#'; */ + newlayout[i * 2 + 1][j * 2] = maze[i + 1][j]; } + } -success: - dist.free (); + 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] = maze[i][j + 1]; + } + + if (wall_pattern == 7) + { /* if orig maze is a 2x2 wall block, + * we fill the result with walls. */ + newlayout[i * 2 + 1][j * 2 + 1] = '#'; + } + } +} - // we mark free but visited floors as 1, undo this here - for (int x = 0; x < maze->w; ++x) - for (int y = 0; y < maze->h; ++y) - if (maze [x][y] == 1) - maze [x][y] = 0; +/* This function will try to sensibly connect doors so that they meet up with + * adjacent walls. Note that it will also presumptuously delete (ignore) doors + * that it doesn't know how to correctly expand. + */ +static void inline +expand_door (layout &newlayout, int i, int j, layout &maze) +{ + 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 + * a wall and another door, this door will connect to the wall and + * disconnect from the other door. */ + if (wall_pattern & 3) + join_pattern = wall_pattern; + else + join_pattern = door_pattern; + + newlayout[i * 2][j * 2] = 'D'; + + 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 < maze.h) + if (join_pattern & 2) + /* there is a door/wall below */ + newlayout[i * 2][j * 2 + 1] = 'D'; } void -LayoutData::isolation_remover (bool dirty) +layout::expand2x () { - Layout maze; - maze.ptr = this; - ::isolation_remover (maze, dirty); + layout new_layout (w * 2 - 1, h * 2 - 1); + + new_layout.clear (); + + coroapi::cede_to_tick (); + + for (int i = 0; i < w; i++) + for (int j = 0; j < h; j++) + switch (data [i][j]) + { + case '#': expand_wall (new_layout, i, j, *this); break; + case 'D': expand_door (new_layout, i, j, *this); break; + default: expand_misc (new_layout, i, j, *this); break; + } + + swap (new_layout); } -#if 0 -static struct demo +///////////////////////////////////////////////////////////////////////////// + +/* 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) { - demo () - { - Layout maze (40, 25); - rmg_rndm.seed (time (0)); + int i1; + int length = 0; - for (int p = 80; p < 100; p += 1) { - maze->fill ('#'); + /* dont make walls if we're on the edge. */ + if (dx == 0 || dx == (maze.w - 1) || dy == 0 || dy == (maze.h - 1)) + return -1; + + /* don't make walls if we're ON a wall. */ + if (maze [dx][dy] != 0) + return -1; -#if 1 - for (int x = 1; x < maze->w - 1; ++x) - for (int y = 1; y < maze->h - 1; ++y) - maze [x][y] = rmg_rndm(100) < p ? '#' : 0; -#else - room (maze, 5, 5); - room (maze, 30,20); - room (maze, 10,20); - room (maze, 20,10); -#endif + if (dir == 0) /* horizontal */ + { + int y = dy; + + for (i1 = dx - 1; i1 > 0; i1--) + { + int sindex = surround_flag2 (maze, i1, y); + + if (sindex == 1) break; + if (sindex != 0) return -1; /* can't make horiz. wall here */ + if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ + + length++; + } + + for (i1 = dx + 1; i1 < maze.w - 1; i1++) + { + int sindex = surround_flag2 (maze, i1, y); + + if (sindex == 2) break; + if (sindex != 0) return -1; /* can't make horiz. wall here */ + if (maze[i1][y] != 0) return -1; /* can't make horiz. wall here */ + + length++; + } + return length; + } + else + { /* vertical */ + int x = dx; + + for (i1 = dy - 1; i1 > 0; i1--) + { + int sindex = surround_flag2 (maze, x, i1); + + if (sindex == 4) break; + if (sindex != 0) return -1; /* can't make vert. wall here */ + if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ + + length++; + } + + for (i1 = dy + 1; i1 < maze.h - 1; i1++) + { + int sindex = surround_flag2 (maze, x, i1); + + if (sindex == 8) break; + if (sindex != 0) return -1; /* can't make verti. wall here */ + if (maze[x][i1] != 0) return -1; /* can't make horiz. wall here */ + + length++; + } + + return length; + } + + return -1; +} + +int +make_wall (layout &maze, int x, int y, int dir) +{ + maze[x][y] = 'D'; /* mark a door */ + + switch (dir) + { + case 0: /* horizontal */ + { + for (int i1 = x - 1; maze[i1][y] == 0; --i1) maze[i1][y] = '#'; + for (int i1 = x + 1; maze[i1][y] == 0; ++i1) maze[i1][y] = '#'; + break; + } + case 1: /* vertical */ + { + for (int i1 = y - 1; maze[x][i1] == 0; --i1) maze[x][i1] = '#'; + for (int i1 = y + 1; maze[x][i1] == 0; ++i1) maze[x][i1] = '#'; + break; + } + } + + return 0; +} + +void +layout::roomify () +{ + int tries = w * h / 30; + + coroapi::cede_to_tick (); + + for (int ti = 0; ti < tries; ti++) + { + /* starting location for looking at creating a door */ + int dx = rmg_rndm (w); + int dy = rmg_rndm (h); + + /* results of checking on creating walls. */ + int cx = can_make_wall (*this, dx, dy, 0); /* horizontal */ + int cy = can_make_wall (*this, dx, dy, 1); /* vertical */ + + if (cx == -1) + { + if (cy != -1) + make_wall (*this, dx, dy, 1); + + continue; + } + + if (cy == -1) + { + make_wall (*this, dx, dy, 0); + continue; + } + + if (cx < cy) + make_wall (*this, dx, dy, 0); + else + make_wall (*this, dx, dy, 1); + } +} + +//-GPL + +///////////////////////////////////////////////////////////////////////////// + +// 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; + } - isolation_remover (maze, 1); - maze.print (); + 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) +{ + if (maze.w > maze.h && 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 (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); } + coroapi::cede_to_tick (); +} + +// 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 (); + + // exits currently do not work so well, as they + // are currently often found together, so nuke entrances + maze.replace ('<', ' '); + + maze.isolation_remover (0); +} + +//+GPL + +/* function selects the maze function and gives it whatever + arguments it needs. */ +void +layout::generate (random_map_params *RP) +{ + switch (RP->map_layout_style) + { + case LAYOUT_ONION: + map_gen_onion (*this, RP->layoutoptions1, RP->layoutoptions2); + + if (!(rmg_rndm (3)) && !(RP->layoutoptions1 & (RMOPT_WALLS_ONLY | RMOPT_WALL_OFF))) + roomify (); + + break; + + case LAYOUT_MAZE: + maze_gen (*this, RP->get_iv ("maze_type", rmg_rndm (4))); + + if (rmg_rndm (2)) + doorify (); + + break; + + case LAYOUT_SPIRAL: + map_gen_spiral (*this, RP->layoutoptions1); + + if (rmg_rndm (2)) + doorify (); + + break; + + case LAYOUT_ROGUELIKE: + /* Don't put symmetry in rogue maps. There isn't much reason to + * do so in the first place (doesn't make it any more interesting), + * but more importantly, the symmetry code presumes we are symmetrizing + * spirals, or maps with lots of passages - making a symmetric rogue + * map fails because its likely that the passages the symmetry process + * creates may not connect the rooms. + */ + RP->symmetry_used = SYMMETRY_NONE; + roguelike_layout_gen (*this, RP->layoutoptions1); + /* no doorifying... done already */ + break; + + case LAYOUT_SNAKE: + make_snake_layout (*this, RP->layoutoptions1); + + if (rmg_rndm (2)) + roomify (); + + break; + + case LAYOUT_SQUARE_SPIRAL: + make_square_spiral_layout (*this, RP->layoutoptions1); + + if (rmg_rndm (2)) + roomify (); + + break; + + case LAYOUT_CAVE: + gen_cave (RP->get_iv ("cave_type", rmg_rndm (4))); + + if (rmg_rndm (2)) + doorify (); + + break; + + case LAYOUT_CASTLE: + gen_castle (); + + if (rmg_rndm (2)) + doorify (); + + break; + + case LAYOUT_MULTIPLE: + gen_mixed (*this, RP); + break; + + default: + abort (); + } +} + +//-GPL + #if 0 - for(int i=1;i<10;i) +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)); + extern void hack();hack (); + + for(int i=1;i<100;i++) { - maze_gen (maze, 1); + layout maze (40, 30); + maze.fill_rand (99); + maze.border (); + maze.isolation_remover (2); maze.print (); } -#endif + exit (1); } } demo;