001package squidpony.squidgrid.mapping; 002 003import squidpony.squidmath.*; 004 005import java.util.ArrayList; 006import java.util.List; 007 008/** 009 * Generate dungeons with between 1 and 3 primary "lanes" going from the upper left "base" to the bottom right "base" 010 * (and vice versa, since this is symmetrical). Also fills the area not covered by lanes with "jungle" (random, but 011 * symmetrical, room or cave connections). Dungeons are produced by MixedGenerator, like those SerpentMapGenerator 012 * makes, but include the wide lanes going from corner to corner. You can call different methods like putCaveCarvers(), 013 * putBoxRoomCarvers(), putWalledRoundRoomCarvers(), etc. to affect the "jungle", which defaults to caves unless one or 014 * more of the putXXXCarvers methods was called. The lanes are always 5 floor cells wide, measured 8-way. This supports 015 * the getEnvironment() method, which can be used in conjunction with RoomFinder to find where separate room, corridor, 016 * and cave areas have been placed. 017 * <br> 018 * A preview can be seen here https://gist.github.com/tommyettinger/4f57cff23eead11b17bf , with dungeons created with 019 * one, two, and three lanes, and only using box-shaped rooms for "jungle." Currently, the two-lane dungeon seems to be 020 * ideal for maps that aren't incredibly large; the samples are 80x80, but larger maps may have better jungle layout 021 * with three lanes than those three-lane maps can manage on smaller sizes. Another potential advantage of the two-lane 022 * approach is that it can be used to generate a "ring" of wide paths around a central "core" of jungle, which wasn't 023 * originally intended as a use of this generator but could be very useful for games that, for instance, want guards 024 * patrolling an obvious ring, while the player, monsters, and/or other prisoners start in the jungle. 025 * Created by Tommy Ettinger on 10/24/2015. 026 */ 027public class LanesMapGenerator implements IDungeonGenerator { 028 protected SymmetryDungeonGenerator mix; 029 protected int[] columns, rows; 030 protected IRNG random; 031 protected int lanes; 032 /** 033 * This prepares a map generator that will generate a map with the given width and height, using the given RNG. 034 * The dungeon will have the specified number of wide lanes going from upper left to lower right, possibly taking a 035 * longer path to approach the other corners. You call the different carver-adding methods to affect what the 036 * non-lane portion of the dungeon will look like, putCaveCarvers(), putBoxRoomCarvers(), and putRoundRoomCarvers(), 037 * defaulting to only caves if none are called. You call generate() after adding carvers, which returns a char[][] 038 * for a map. 039 * @param width the width of the final map in cells 040 * @param height the height of the final map in cells 041 * @param rng an RNG object to use for random choices; this make a lot of random choices. 042 * @param lanes between 1 and 3; the number of wide paths to generate going from upper left to lower right. 043 * @see MixedGenerator 044 */ 045 public LanesMapGenerator(int width, int height, IRNG rng, int lanes) 046 { 047 if(width <= 8 || height <= 8) 048 throw new IllegalArgumentException("width and height must be greater than 8"); 049 CoordPacker.init(); 050 this.lanes = (lanes < 1 || lanes > 3) ? 1 : lanes; 051 random = rng; 052 /* 053 long columnAlterations = random.nextLong(); 054 float columnBase = width / (Long.bitCount(columnAlterations) + 16.0f); 055 long rowAlterations = random.nextLong(); 056 float rowBase = height / (Long.bitCount(rowAlterations) + 16.0f); 057 058 columns = new int[32]; 059 rows = new int[32]; 060 int csum = 0, rsum = 0; 061 long b = 3; 062 for (int i = 0; i < 32; i++, b <<= 2) { 063 columns[i] = csum + (int)(columnBase * 0.5f * (1 + Long.bitCount(columnAlterations & b))); 064 csum += (int)(columnBase * (1 + Long.bitCount(columnAlterations & b))); 065 rows[i] = rsum + (int)(rowBase * 0.5f * (1 + Long.bitCount(rowAlterations & b))); 066 rsum += (int)(rowBase * (1 + Long.bitCount(rowAlterations & b))); 067 } 068 int cs = (width - csum); 069 int rs = (height - rsum); 070 int cs2 = cs, rs2 = rs, cs3 = cs, rs3 = rs; 071 for (int i = 15; i >= 0; i--) { 072 cs2= cs2 * i / 15; 073 rs2 = rs2 * i / 15; 074 columns[i] -= cs2; 075 rows[i] -= rs2; 076 } 077 for (int i = 15; i >= 16; i--) { 078 cs3 = cs3 * (i - 16) / 16; 079 rs3 = rs3 * (i - 16) / 16; 080 columns[i] += cs3; 081 rows[i] += rs3; 082 } 083 */ 084 long columnAlterations = random.nextLong(0x1000000000000L); 085 float columnBase = width / (Long.bitCount(columnAlterations) + 48.0f); 086 long rowAlterations = random.nextLong(0x1000000000000L); 087 float rowBase = height / (Long.bitCount(rowAlterations) + 48.0f); 088 089 columns = new int[16]; 090 rows = new int[16]; 091 int csum = 0, rsum = 0; 092 long b = 7; 093 for (int i = 0; i < 16; i++, b <<= 3) { 094 columns[i] = csum + (int)(columnBase * 0.5f * (3 + Long.bitCount(columnAlterations & b))); 095 csum += (int)(columnBase * (3 + Long.bitCount(columnAlterations & b))); 096 rows[i] = rsum + (int)(rowBase * 0.5f * (3 + Long.bitCount(rowAlterations & b))); 097 rsum += (int)(rowBase * (3 + Long.bitCount(rowAlterations & b))); 098 } 099 int cs = width - csum; 100 int rs = height - rsum; 101 int cs2 = cs, rs2 = rs, cs3 = cs, rs3 = rs; 102 for (int i = 0; i <= 7; i++) { 103 cs2= 0; 104 rs2 = 0; 105 columns[i] -= cs2; 106 rows[i] -= rs2; 107 } 108 for (int i = 15; i >= 8; i--) { 109 cs3 = cs3 * (i - 8) / 8; 110 rs3 = rs3 * (i - 8) / 8; 111 columns[i] += cs3; 112 rows[i] += rs3; 113 } 114 115 116 OrderedMap<Coord, List<Coord>> connections = new OrderedMap<>(80); 117 Coord temp, t; 118 int m = random.nextInt(32), r = random.between(8, 24); 119 temp = CoordPacker.hilbertToCoord(m); 120 Coord starter = CoordPacker.hilbertToCoord(m); 121 m += r; 122 ArrayList<Coord> cl = new ArrayList<>(4); 123 for (int i = r; i < 256 && m < 256 - 9; i += r, m += r) { 124 cl.clear(); 125 cl.add(Coord.get(columns[temp.x], rows[temp.y])); 126 temp = CoordPacker.hilbertToCoord(m); 127 r = random.between(8, 24); 128 for (int j = 0, p = r - 1; 129 j < 3 && p > 2 && Math.min(random.nextDouble(), random.nextDouble()) < 0.2; 130 j++, p -= random.between(1, p)) { 131 t = CoordPacker.hilbertToCoord(m + p); 132 cl.add(Coord.get(columns[t.x], rows[t.y])); 133 } 134 connections.put(Coord.get(columns[temp.x], rows[temp.y]), cl); 135 } 136 connections.get(Coord.get(columns[temp.x], rows[temp.y])).add( 137 Coord.get(columns[starter.x], rows[starter.y])); 138 mix = new SymmetryDungeonGenerator(width, height, random, connections, 0.6f); 139 boolean[][] fixed = new boolean[width][height]; 140 141 if(lanes != 2) 142 { 143 List<Coord> path = DDALine.line(3, 3, width - 4, height - 4); 144 for(Coord c : path) 145 { 146 for (int x = c.x - 2; x <= c.x + 2; x++) { 147 for (int y = c.y - 2; y <= c.y + 2; y++) { 148 fixed[x][y] = true; 149 } 150 } 151 } 152 } 153 if(lanes > 1) 154 { 155 List<Coord> path = DDALine.line(3, 3, 3, height - 4); 156 path.addAll(DDALine.line(3, 3, width - 4, 3)); 157 for(Coord c : path) 158 { 159 for (int x = c.x - 2; x <= c.x + 2; x++) { 160 for (int y = c.y - 2; y <= c.y + 2; y++) { 161 fixed[x][y] = true; 162 } 163 } 164 } 165 } 166 mix.setFixedRooms(fixed); 167 } 168 169 170 /** 171 * Changes the number of "carvers" that will create caves from one room to the next. If count is 0 or less, no caves 172 * will be made. If count is at least 1, caves are possible, and higher numbers relative to the other carvers make 173 * caves more likely. Carvers are shuffled when used, then repeat if exhausted during generation. Since typically 174 * about 30-40 rooms are carved, large totals for carver count aren't really needed; aiming for a total of 10 175 * between the count of putCaveCarvers(), putBoxRoomCarvers(), and putRoundRoomCarvers() is reasonable. 176 * @see MixedGenerator 177 * @param count the number of carvers making caves between rooms; only matters in relation to other carvers 178 */ 179 public void putCaveCarvers(int count) 180 { 181 mix.putCaveCarvers(count); 182 } 183 /** 184 * Changes the number of "carvers" that will create right-angle corridors from one room to the next, create rooms 185 * with a random size in a box shape at the start and end, and a small room at the corner if there is one. If count 186 * is 0 or less, no box-shaped rooms will be made. If count is at least 1, box-shaped rooms are possible, and higher 187 * numbers relative to the other carvers make box-shaped rooms more likely. Carvers are shuffled when used, then 188 * repeat if exhausted during generation. Since typically about 30-40 rooms are carved, large totals for carver 189 * count aren't really needed; aiming for a total of 10 between the count of putCaveCarvers(), putBoxRoomCarvers(), 190 * and putRoundRoomCarvers() is reasonable. 191 * @see MixedGenerator 192 * @param count the number of carvers making box-shaped rooms and corridors between them; only matters in relation 193 * to other carvers 194 */ 195 public void putBoxRoomCarvers(int count) 196 { 197 mix.putBoxRoomCarvers(count); 198 } 199 /** 200 * Changes the number of "carvers" that will create right-angle corridors from one room to the next, create rooms 201 * with a random size in a box shape at the start and end, and a small room at the corner if there is one. This also 202 * ensures walls will be placed around the room, only allowing corridors and small cave openings to pass. If count 203 * is 0 or less, no box-shaped rooms will be made. If count is at least 1, box-shaped rooms are possible, and higher 204 * numbers relative to the other carvers make box-shaped rooms more likely. Carvers are shuffled when used, then 205 * repeat if exhausted during generation. Since typically about 30-40 rooms are carved, large totals for carver 206 * count aren't really needed; aiming for a total of 10 between the count of putCaveCarvers(), putBoxRoomCarvers(), 207 * and putRoundRoomCarvers() is reasonable. 208 * @see MixedGenerator 209 * @param count the number of carvers making box-shaped rooms and corridors between them; only matters in relation 210 * to other carvers 211 */ 212 public void putWalledBoxRoomCarvers(int count) 213 { 214 mix.putWalledBoxRoomCarvers(count); 215 } 216 /** 217 * Changes the number of "carvers" that will create right-angle corridors from one room to the next, create rooms 218 * with a random size in a circle shape at the start and end, and a small circular room at the corner if there is 219 * one. If count is 0 or less, no circular rooms will be made. If count is at least 1, circular rooms are possible, 220 * and higher numbers relative to the other carvers make circular rooms more likely. Carvers are shuffled when used, 221 * then repeat if exhausted during generation. Since typically about 30-40 rooms are carved, large totals for carver 222 * count aren't really needed; aiming for a total of 10 between the count of putCaveCarvers(), putBoxRoomCarvers(), 223 * and putRoundRoomCarvers() is reasonable. 224 * @see MixedGenerator 225 * @param count the number of carvers making circular rooms and corridors between them; only matters in relation 226 * to other carvers 227 */ 228 public void putRoundRoomCarvers(int count) 229 { 230 mix.putRoundRoomCarvers(count); 231 } 232 233 /** 234 * Changes the number of "carvers" that will create right-angle corridors from one room to the next, create rooms 235 * with a random size in a circle shape at the start and end, and a small circular room at the corner if there is 236 * one. This also ensures walls will be placed around the room, only allowing corridors and small cave openings to 237 * pass. If count is 0 or less, no circular rooms will be made. If count is at least 1, circular rooms are possible, 238 * and higher numbers relative to the other carvers make circular rooms more likely. Carvers are shuffled when used, 239 * then repeat if exhausted during generation. Since typically about 30-40 rooms are carved, large totals for carver 240 * count aren't really needed; aiming for a total of 10 between the count of putCaveCarvers(), putBoxRoomCarvers(), 241 * and putRoundRoomCarvers() is reasonable. 242 * @see MixedGenerator 243 * @param count the number of carvers making circular rooms and corridors between them; only matters in relation 244 * to other carvers 245 */ 246 public void putWalledRoundRoomCarvers(int count) 247 { 248 mix.putWalledRoundRoomCarvers(count); 249 } 250 251 /** 252 * This generates a new map by stretching a 16x16 grid of potential rooms to fit the width and height passed to the 253 * constructor, randomly expanding columns and rows before contracting the whole to fit perfectly. This uses the 254 * Moore Curve, a space-filling curve that loops around on itself, to guarantee that the rooms will always have a 255 * long path through the dungeon that, if followed completely, will take you back to your starting room. Some small 256 * branches are possible, and large rooms may merge with other rooms nearby. This uses MixedGenerator. 257 * @see MixedGenerator 258 * @return a char[][] where '#' is a wall and '.' is a floor or corridor; x first y second 259 */ 260 public char[][] generate() 261 { 262 return mix.generate(); 263 } 264 265 /** 266 * Gets a 2D array of int constants, each representing a type of environment corresponding to a static field of 267 * MixedGenerator. This array will have the same size as the last char 2D array prduced by generate(), and the value 268 * of this method if called before generate() is undefined, but probably will be a 2D array of all 0 (UNTOUCHED). 269 * <ul> 270 * <li>MixedGenerator.UNTOUCHED, equal to 0, is used for any cells that aren't near a floor.</li> 271 * <li>MixedGenerator.ROOM_FLOOR, equal to 1, is used for floor cells inside wide room areas.</li> 272 * <li>MixedGenerator.ROOM_WALL, equal to 2, is used for wall cells around wide room areas.</li> 273 * <li>MixedGenerator.CAVE_FLOOR, equal to 3, is used for floor cells inside rough cave areas.</li> 274 * <li>MixedGenerator.CAVE_WALL, equal to 4, is used for wall cells around rough cave areas.</li> 275 * <li>MixedGenerator.CORRIDOR_FLOOR, equal to 5, is used for floor cells inside narrow corridor areas.</li> 276 * <li>MixedGenerator.CORRIDOR_WALL, equal to 6, is used for wall cells around narrow corridor areas.</li> 277 * </ul> 278 * @return a 2D int array where each element is an environment type constant in MixedGenerator 279 */ 280 public int[][] getEnvironment() 281 { 282 return mix.getEnvironment(); 283 } 284 285 public char[][] getDungeon() { 286 return mix.getDungeon(); 287 } 288}