--- deliantra/server/random_maps/layout.C 2010/07/01 01:22:44 1.3 +++ deliantra/server/random_maps/layout.C 2010/07/02 04:05:15 1.6 @@ -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 @@ -22,6 +23,7 @@ #include #include +#include Layout::Layout (int w, int h) : w(w), h(h) @@ -92,31 +94,11 @@ // 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 }, + { -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--; ) @@ -142,7 +124,7 @@ ///////////////////////////////////////////////////////////////////////////// void -Layout::print () +Layout::print () const { for (int y = 0; y < h; y++) { @@ -232,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 @@ -246,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 { @@ -260,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 @@ -305,16 +293,10 @@ } // 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; } ///////////////////////////////////////////////////////////////////////////// @@ -357,19 +339,539 @@ isolation_remover (); } +///////////////////////////////////////////////////////////////////////////// + +//+GPL + +/* puts doors at appropriate locations in a layout. */ +void +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); + + /* 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 */ + { + doorlist_x [doorlocs] = i; + doorlist_y [doorlocs] = j; + doorlocs++; + } + } + + while (ndoors > 0 && doorlocs > 0) + { + int di = rmg_rndm (doorlocs); + int i = doorlist_x [di]; + int j = doorlist_y [di]; + int sindex = surround_flag (*this, i, j); + + if (sindex == 3 || sindex == 12) /* these are possible door sindex */ + { + col [i][j] = '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) +{ + 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] = col[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] = col[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] = col[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; + + default: + sym_layout.isolation_remover (); + break; + } +#endif + sym_layout.isolation_remover (); + + swap (sym_layout); +} + +//-GPL + +void +Layout::rotate (int rotation) +{ + switch (rotation & 3) + { + case 2: /* a reflection */ + { + 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]; + + swap (new_layout); + } + break; + + case 1: + case 3: + { + 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]; + + 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]; + + swap (new_layout); + } + break; + } +} + +///////////////////////////////////////////////////////////////////////////// + +//+GPL + +/* + * Expands a layout by 2x in each dimension. + * H. S. Teoh + */ + +/* 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 &layout) +{ + newlayout[i * 2 + rmg_rndm (1)][j * 2 + rmg_rndm (1)] = layout[i][j]; + /* (Note: no need to reset rest of 2x2 area to \0 because calloc does that + * for us.) */ +} + +/* 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 &layout, int i, int j) +{ + int pattern = 0; + + if (i + 1 < layout.w && layout[i + 1][j] == ch) + pattern |= 1; + + if (j + 1 < layout.h) + { + if (layout[i][j + 1] == ch) + pattern |= 2; + + if (i + 1 < layout.w && layout[i + 1][j + 1] == ch) + pattern |= 4; + } + + 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 &layout) +{ + int wall_pattern = calc_pattern ('#', layout, i, j); + int door_pattern = calc_pattern ('D', layout, i, j); + int both_pattern = wall_pattern | door_pattern; + + newlayout[i * 2][j * 2] = '#'; + + if (i + 1 < layout.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]; + } + } + + if (j + 1 < layout.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]; + } + + if (wall_pattern == 7) + { /* if orig layout is a 2x2 wall block, + * we fill the result with walls. */ + newlayout[i * 2 + 1][j * 2 + 1] = '#'; + } + } +} + +/* 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 &layout) +{ + int wall_pattern = calc_pattern ('#', layout, i, j); + int door_pattern = calc_pattern ('D', layout, 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 < layout.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 (join_pattern & 2) + /* there is a door/wall below */ + newlayout[i * 2][j * 2 + 1] = 'D'; +} + +void +Layout::expand2x () +{ + 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]) + { + 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); +} + +///////////////////////////////////////////////////////////////////////////// + +/* checks the layout 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) +{ + int i1; + int length = 0; + + /* 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 (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 (char **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; + + 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); + } +} + +///////////////////////////////////////////////////////////////////////////// + +/* function selects the layout 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; + + default: + abort (); + } + + /* rotate the layout randomly */ + rotate (rmg_rndm (4)); + + symmetrize (RP->symmetry_used); + +#if 0 + print ();//D +#endif + + if (RP->expand2x) + expand2x (); +} + +//-GPL + #if 0 static struct demo { 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 (); }