/** * Generate a char[][] dungeon with extra features given a baseDungeon that has already been * generated, and that already has staircases represented by greater than and less than signs. * Typically, you want to call generate with a TilesetType or no argument for the easiest * generation; this method is meant for adding features like water and doors to existing simple * maps. This uses '#' for walls, '.' for floors, '~' for deep water, ',' for shallow water, '^' * for traps, '+' for doors that provide horizontal passage, and '/' for doors that provide * vertical passage. Use the addDoors, addWater, addGrass, and addTraps methods of this class to * request these in the generated map. Also sets the fields stairsUp and stairsDown to null, and * expects stairs to be already handled. * * @param baseDungeon a pre-made dungeon consisting of '#' for walls and '.' for floors, with * stairs already in; may be modified in-place * @return a char[][] dungeon */ public char[][] generateRespectingStairs(char[][] baseDungeon) { if (!seedFixed) { rebuildSeed = rng.getState(); } seedFixed = false; char[][] map = DungeonUtility.wallWrap(baseDungeon); DijkstraMap dijkstra = new DijkstraMap(map); stairsUp = null; stairsDown = null; dijkstra.clearGoals(); ArrayList<Coord> stairs = DungeonUtility.allMatching(map, '<', '>'); for (int j = 0; j < stairs.size(); j++) { dijkstra.setGoal(stairs.get(j)); } dijkstra.scan(null); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (dijkstra.gradientMap[i][j] >= DijkstraMap.FLOOR) { map[i][j] = '#'; } } } return innerGenerate(map); }
/** * Generate a char[][] dungeon with extra features given a baseDungeon that has already been * generated. Typically, you want to call generate with a TilesetType or no argument for the * easiest generation; this method is meant for adding features like water and doors to existing * simple maps. This uses '#' for walls, '.' for floors, '~' for deep water, ',' for shallow * water, '^' for traps, '+' for doors that provide horizontal passage, and '/' for doors that * provide vertical passage. Use the addDoors, addWater, addGrass, and addTraps methods of this * class to request these in the generated map. Also sets the fields stairsUp and stairsDown to * two randomly chosen, distant, connected, walkable cells. <br> * Special behavior here: If tab characters are present in the 2D char array, they will be * replaced with '.' in the final dungeon, but will also be tried first as valid staircase * locations (with a high distance possible to travel away from the starting staircase). If no tab * characters are present this will search for '.' floors to place stairs on, as normal. This * tab-first behavior is useful in conjunction with some methods that establish a good path in an * existing dungeon; an example is {@code DungeonUtility.ensurePath(dungeon, rng, '\t', '#');} * then passing dungeon (which that code modifies) in as baseDungeon to this method. * * @param baseDungeon a pre-made dungeon consisting of '#' for walls and '.' for floors; may be * modified in-place * @return a char[][] dungeon */ public char[][] generate(char[][] baseDungeon) { if (!seedFixed) { rebuildSeed = rng.getState(); } seedFixed = false; char[][] map = DungeonUtility.wallWrap(baseDungeon); width = map.length; height = map[0].length; DijkstraMap dijkstra = new DijkstraMap(map); int frustrated = 0; do { dijkstra.clearGoals(); stairsUp = utility.randomMatchingTile(map, '\t'); if (stairsUp == null) { stairsUp = utility.randomFloor(map); if (stairsUp == null) { frustrated++; continue; } } dijkstra.setGoal(stairsUp); dijkstra.scan(null); frustrated++; } while (dijkstra.getMappedCount() < width + height && frustrated < 8); if (frustrated >= 8) { return generate(); } double maxDijkstra = 0.0; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { if (dijkstra.gradientMap[i][j] >= DijkstraMap.FLOOR) { map[i][j] = '#'; } else if (dijkstra.gradientMap[i][j] > maxDijkstra) { maxDijkstra = dijkstra.gradientMap[i][j]; } if (map[i][j] == '\t') { map[i][j] = '.'; } } } stairsDown = new GreasedRegion(dijkstra.gradientMap, maxDijkstra * 0.7, DijkstraMap.FLOOR) .singleRandom(rng); return innerGenerate(map); }
private void postMove() { phase = Phase.MONSTER_ANIM; // The next two lines are important to avoid monsters treating cells the player WAS in as goals. getToPlayer.clearGoals(); getToPlayer.resetMap(); // now that goals are cleared, we can mark the current player position as a goal. getToPlayer.setGoal(player.gridX, player.gridY); // this is an important piece of DijkstraMap usage; the argument is a Set of Points for squares // that // temporarily cannot be moved through (not walls, which are automatically known because the map // char[][] // was passed to the DijkstraMap constructor, but things like moving creatures and objects). LinkedHashSet<Coord> monplaces = monsters.positions(); pathMap = getToPlayer.scan(monplaces); // recalculate FOV, store it in fovmap for the render to use. fovmap = fov.calculateFOV(res, player.gridX, player.gridY, 8, Radius.SQUARE); // handle monster turns ArrayList<Coord> nextMovePositions = new ArrayList<Coord>(25); for (Coord pos : monsters.positions()) { Monster mon = monsters.get(pos); // monster values are used to store their aggression, 1 for actively stalking the player, 0 // for not. if (mon.state > 0 || fovmap[pos.x][pos.y] > 0.1) { if (mon.state == 0) { messages.appendMessage( "The PHANTOM cackles at you, \"" + FakeLanguageGen.RUSSIAN_AUTHENTIC.sentence( rng, 1, 3, new String[] {",", ",", ",", " -"}, new String[] {"!"}, 0.25) + "\""); } // this block is used to ensure that the monster picks the best path, or a random choice if // there // is more than one equally good best option. Direction choice = null; double best = 9999.0; Direction[] ds = new Direction[8]; rng.shuffle(Direction.OUTWARDS, ds); for (Direction d : ds) { Coord tmp = pos.translate(d); if (pathMap[tmp.x][tmp.y] < best && !checkOverlap(mon, tmp.x, tmp.y, nextMovePositions)) { // pathMap is a 2D array of doubles where 0 is the goal (the player). // we use best to store which option is closest to the goal. best = pathMap[tmp.x][tmp.y]; choice = d; } } if (choice != null) { Coord tmp = pos.translate(choice); // if we would move into the player, instead damage the player and give newMons the // current // position of this monster. if (tmp.x == player.gridX && tmp.y == player.gridY) { display.tint(player.gridX * 2, player.gridY, SColor.PURE_CRIMSON, 0, 0.415f); display.tint(player.gridX * 2 + 1, player.gridY, SColor.PURE_CRIMSON, 0, 0.415f); health--; // player.setText("" + health); monsters.positionalModify(pos, mon.change(1)); } // otherwise store the new position in newMons. else { /*if (fovmap[mon.getKey().x][mon.getKey().y] > 0.0) { display.put(mon.getKey().x, mon.getKey().y, 'M', 11); }*/ nextMovePositions.add(tmp); monsters.positionalModify(pos, mon.change(1)); monsters.move(pos, tmp); display.slide(mon.entity, tmp.x, tmp.y); } } else { monsters.positionalModify(pos, mon.change(1)); } } } }
@Override public void create() { // gotta have a random number generator. Here the RNG is unseeded, which means a different // dungeon every time, // among many other changes. rng = new RNG(); // seeds can be given easily to RNG's constructor. If you want to request the current state from // a random number // generator or change the state in the middle of usage, you need to use a StatefulRNG (or // EditRNG) instead of a // normal RNG; StatefulRNG and EditRNG can also be constructed with seeds like RNG can. // rng = new RNG("seeeeeeeed"); // rng = new RNG(0xBADACE); // for demo purposes, we allow changing the SquidColorCenter and the filter effect associated // with it. // next, we populate the colorCenters array with the SquidColorCenters that will modify any // colors we request // of them using the filter we specify. Only one SquidColorCenter will be used at any time for // foreground, and // sometimes another will be used for background. colorCenters = new SquidColorCenter[18]; // MultiLerpFilter here is given two colors to tint everything toward one of; this is meant to // reproduce the // "Hollywood action movie poster" style of using primarily light orange (explosions) and // gray-blue (metal). colorCenters[0] = new SquidColorCenter( new Filters.MultiLerpFilter( new Color[] {SColor.GAMBOGE_DYE, SColor.COLUMBIA_BLUE}, new float[] {0.25f, 0.2f})); colorCenters[1] = colorCenters[0]; // MultiLerpFilter here is given three colors to tint everything toward one of; this is meant to // look bolder. colorCenters[2] = new SquidColorCenter( new Filters.MultiLerpFilter( new Color[] {SColor.RED_PIGMENT, SColor.MEDIUM_BLUE, SColor.LIME_GREEN}, new float[] {0.2f, 0.25f, 0.25f})); colorCenters[3] = colorCenters[2]; // ColorizeFilter here is given a slightly-grayish dark brown to imitate a sepia tone. colorCenters[4] = new SquidColorCenter(new Filters.ColorizeFilter(SColor.CLOVE_BROWN, 0.7f, -0.05f)); colorCenters[5] = new SquidColorCenter(new Filters.ColorizeFilter(SColor.CLOVE_BROWN, 0.65f, 0.07f)); // HallucinateFilter makes all the colors very saturated and move even when you aren't doing // anything. colorCenters[6] = new SquidColorCenter(new Filters.HallucinateFilter()); colorCenters[7] = colorCenters[6]; // SaturationFilter here is used to over-saturate the colors slightly. Background is less // saturated. colorCenters[8] = new SquidColorCenter(new Filters.SaturationFilter(1.35f)); colorCenters[9] = new SquidColorCenter(new Filters.SaturationFilter(1.15f)); // SaturationFilter here is used to de-saturate the colors slightly. Background is less // saturated. colorCenters[10] = new SquidColorCenter(new Filters.SaturationFilter(0.7f)); colorCenters[11] = new SquidColorCenter(new Filters.SaturationFilter(0.5f)); // WiggleFilter here is used to randomize the colors slightly. colorCenters[12] = new SquidColorCenter(new Filters.WiggleFilter()); colorCenters[13] = colorCenters[12]; // SaturationFilter here is used to de-saturate the colors slightly. Background is less // saturated. colorCenters[14] = new SquidColorCenter(new Filters.PaletteFilter(SColor.BLUE_GREEN_SERIES)); colorCenters[15] = new SquidColorCenter(new Filters.PaletteFilter(SColor.ACHROMATIC_SERIES)); colorCenters[16] = DefaultResources.getSCC(); colorCenters[17] = colorCenters[16]; fgCenter = colorCenters[16]; bgCenter = colorCenters[17]; currentCenter = 8; batch = new SpriteBatch(); width = 60; height = 30; // NOTE: cellWidth and cellHeight are assigned values that are significantly larger than the // corresponding sizes // in the EverythingDemoLauncher's main method. Because they are scaled up by an integer here, // they can be scaled // down when rendered, allowing certain small details to appear sharper. This _only_ works with // distance field, // a.k.a. stretchable, fonts! INTERNAL_ZOOM is a tradeoff between rendering more pixels to // increase quality (when // values are high) or rendering fewer pixels for speed (when values are low). Using 2 seems to // work well. cellWidth = 13 * INTERNAL_ZOOM; cellHeight = 26 * INTERNAL_ZOOM; // getStretchableFont loads an embedded font, Inconsolata-LGC-Custom, that is a distance field // font as mentioned // earlier. We set the smoothing multiplier on it only because we are using internal zoom to // increase sharpness // on small details, but if the smoothing is incorrect some sizes look blurry or over-sharpened. // This can be set // manually if you use a constant internal zoom; here we use 1f for internal zoom 1, about 2/3f // for zoom 2, and // about 1/2f for zoom 3. If you have more zooms as options for some reason, this formula should // hold for many // cases but probably not all. textFactory = DefaultResources.getStretchableFont() .setSmoothingMultiplier(2f / (INTERNAL_ZOOM + 1f)) .width(cellWidth) .height(cellHeight) .initBySize(); // Creates a layered series of text grids in a SquidLayers object, using the previously set-up // textFactory and // SquidColorCenters. display = new SquidLayers( width * 2, height, cellWidth, cellHeight, textFactory.copy(), bgCenter, fgCenter); // subCell is a SquidPanel, the same class that SquidLayers has for each of its layers, but we // want to render // certain effects on top of all other panels, which can't be done in the all-in-one-pass // rendering of the grids // in SquidLayers, though it could be done with a slight hassle if the effects are made into // AnimatedEntity // objects or Actors, then rendered separately like the monsters are (see render() below). It is // called subCell // because its text will be made smaller than a full cell, and appears in the upper left corner // for things like // the current health of the player and an '!' for alerted monsters. subCell = new SquidPanel(width * 2, height, textFactory.copy(), fgCenter); display.setAnimationDuration(0.1f); messages = new SquidMessageBox(width * 2, 4, textFactory.copy()); // a bit of a hack to increase the text height slightly without changing the size of the cells // they're in. // this causes a tiny bit of overlap between cells, which gets rid of an annoying gap between // vertical lines. // if you use '#' for walls instead of box drawing chars, you don't need this. messages.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM * 2); display.setTextSize(cellWidth, cellHeight + INTERNAL_ZOOM * 2); // The subCell SquidPanel uses a smaller size here; the numbers 8 and 16 should change if // cellWidth or cellHeight // change, and the INTERNAL_ZOOM multiplier keeps things sharp, the same as it does all over // here. subCell.setTextSize(8 * INTERNAL_ZOOM, 16 * INTERNAL_ZOOM); viewport = new StretchViewport(width * 2 * cellWidth, (height + 4) * cellHeight); stage = new Stage(viewport, batch); // These need to have their positions set before adding any entities if there is an offset // involved. messages.setBounds(0, 0, cellWidth * width * 2, cellHeight * 4); display.setPosition(0, messages.getHeight()); subCell.setPosition(0, messages.getHeight()); messages.appendWrappingMessage( "Use numpad or vi-keys (hjklyubn) to move. Use ? for help, f to change colors, q to quit." + " Click the top or bottom border of this box to scroll."); counter = 0; dungeonGen = new SectionDungeonGenerator(width, height, rng); dungeonGen.addWater(SectionDungeonGenerator.ALL, 8, 6); dungeonGen.addGrass(SectionDungeonGenerator.CAVE, 5); dungeonGen.addBoulders(SectionDungeonGenerator.CAVE, 10); dungeonGen.addDoors(18, false); dungeonGen.addMaze(8); SerpentMapGenerator serpent = new SerpentMapGenerator(width, height, rng); serpent.putCaveCarvers(2); serpent.putWalledBoxRoomCarvers(2); serpent.putWalledRoundRoomCarvers(1); char[][] mg = serpent.generate(); decoDungeon = dungeonGen.generate(mg, serpent.getEnvironment()); Coord pl = dungeonGen.stairsUp, tgt = dungeonGen.stairsDown; decoDungeon[pl.x][pl.y] = '<'; decoDungeon[tgt.x][tgt.y] = '>'; // DefaultResources has not only default fonts but now also default icons. // These need the actual assets to be downloaded as part of the zip or tar.gz // archive of assets, or separately fetched from GitHub in the assets/ folder. atlas = DefaultResources.getIconAtlas(); region = atlas.findRegion("haunting"); // change the TilesetType to lots of different choices to see what dungeon works best. // bareDungeon = dungeonGen.generate(TilesetType.DEFAULT_DUNGEON); bareDungeon = dungeonGen.getBareDungeon(); lineDungeon = DungeonUtility.doubleWidth(DungeonUtility.hashesToLines(decoDungeon, true)); // it's more efficient to get random floors from a packed set containing only (compressed) floor // positions. short[] placement = CoordPacker.pack(bareDungeon, '.'); // Coord pl = dungeonGen.utility.randomCell(placement); placement = CoordPacker.removeSeveralPacked(placement, pl, tgt); int numMonsters = 60; monsters = new SpatialMap<Integer, Monster>(numMonsters); for (int i = 0; i < numMonsters; i++) { Coord monPos = dungeonGen.utility.randomCell(placement); placement = CoordPacker.removePacked(placement, monPos.x, monPos.y); monsters.put( monPos, i, new Monster( display.animateActor( monPos.x, monPos.y, region, fgCenter.filter(display.getPalette().get(11)), true), 0)); // monsters.put(monPos, i, new Monster(display.animateActor(monPos.x, monPos.y, 'Я', // fgCenter.filter(display.getPalette().get(11))), 0)); } // your choice of FOV matters here. fov = new FOV(FOV.RIPPLE_TIGHT); res = DungeonUtility.generateResistances(decoDungeon); fovmap = fov.calculateFOV(res, pl.x, pl.y, 8, Radius.SQUARE); getToPlayer = new DijkstraMap(decoDungeon, DijkstraMap.Measurement.CHEBYSHEV); getToPlayer.rng = rng; getToPlayer.setGoal(pl); pathMap = getToPlayer.scan(null); player = display.animateActor( pl.x, pl.y, '@', fgCenter.loopingGradient(SColor.CAPE_JASMINE, SColor.HAN_PURPLE, 45), 1.5f, true); // fgCenter.filter(display.getPalette().get(30))); cursor = Coord.get(-1, -1); toCursor = new ArrayList<Coord>(10); awaitedMoves = new ArrayList<Coord>(10); playerToCursor = new DijkstraMap(decoDungeon, DijkstraMap.Measurement.EUCLIDEAN); final int[][] initialColors = DungeonUtility.generatePaletteIndices(lineDungeon), initialBGColors = DungeonUtility.generateBGPaletteIndices(lineDungeon); colors = new Color[width * 2][height]; bgColors = new Color[width * 2][height]; ArrayList<Color> palette = display.getPalette(); bgColor = SColor.DARK_SLATE_GRAY; for (int i = 0; i < width * 2; i++) { for (int j = 0; j < height; j++) { colors[i][j] = palette.get(initialColors[i][j]); bgColors[i][j] = palette.get(initialBGColors[i][j]); } } lights = DungeonUtility.generateLightnessModifiers(decoDungeon, counter); seen = new boolean[width][height]; lang = FakeLanguageGen.RUSSIAN_AUTHENTIC.sentence( rng, 4, 6, new String[] {",", ",", ",", " -"}, new String[] {"..."}, 0.25); // this is a big one. // SquidInput can be constructed with a KeyHandler (which just processes specific keypresses), a // SquidMouse // (which is given an InputProcessor implementation and can handle multiple kinds of mouse // move), or both. // keyHandler is meant to be able to handle complex, modified key input, typically for games // that distinguish // between, say, 'q' and 'Q' for 'quaff' and 'Quip' or whatever obtuse combination you choose. // The // implementation here handles hjklyubn keys for 8-way movement, numpad for 8-way movement, // arrow keys for // 4-way movement, and wasd for 4-way movement. Shifted letter keys produce capitalized chars // when passed to // KeyHandler.handle(), but we don't care about that so we just use two case statements with the // same body, // one for the lower case letter and one for the upper case letter. // You can also set up a series of future moves by clicking within FOV range, using mouseMoved // to determine the // path to the mouse position with a DijkstraMap (called playerToCursor), and using touchUp to // actually trigger // the event when someone clicks. input = new VisualInput( new SquidInput.KeyHandler() { @Override public void handle(char key, boolean alt, boolean ctrl, boolean shift) { switch (key) { case SquidInput.UP_ARROW: case 'k': case 'w': case 'K': case 'W': { move(0, -1); break; } case SquidInput.DOWN_ARROW: case 'j': case 's': case 'J': case 'S': { move(0, 1); break; } case SquidInput.LEFT_ARROW: case 'h': case 'a': case 'H': case 'A': { move(-1, 0); break; } case SquidInput.RIGHT_ARROW: case 'l': case 'd': case 'L': case 'D': { move(1, 0); break; } case SquidInput.UP_LEFT_ARROW: case 'y': case 'Y': { move(-1, -1); break; } case SquidInput.UP_RIGHT_ARROW: case 'u': case 'U': { move(1, -1); break; } case SquidInput.DOWN_RIGHT_ARROW: case 'n': case 'N': { move(1, 1); break; } case SquidInput.DOWN_LEFT_ARROW: case 'b': case 'B': { move(-1, 1); break; } case '?': { toggleHelp(); break; } case 'Q': case 'q': case SquidInput.ESCAPE: { Gdx.app.exit(); break; } case 'f': case 'F': { currentCenter = (currentCenter + 1) % 9; // idx is 3 when we use the HallucinateFilter, which needs special work changingColors = currentCenter == 3; fgCenter = colorCenters[currentCenter * 2]; bgCenter = colorCenters[currentCenter * 2 + 1]; display.setFGColorCenter(fgCenter); display.setBGColorCenter(bgCenter); break; } } } }, new SquidMouse( cellWidth, cellHeight, width * 2, height, 0, 0, new InputAdapter() { // if the user clicks within FOV range and there are no awaitedMoves queued up, // generate toCursor if it // hasn't been generated already by mouseMoved, then copy it over to awaitedMoves. @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { if (fovmap[(screenX) / 2][screenY] > 0.0 && awaitedMoves.isEmpty()) { if (toCursor.isEmpty()) { cursor = Coord.get((screenX) / 2, screenY); // Uses DijkstraMap to get a path. from the player's position to the cursor toCursor = playerToCursor.findPath( 30, null, null, Coord.get(player.gridX, player.gridY), cursor); } awaitedMoves = new ArrayList<Coord>(toCursor); } return false; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { return mouseMoved(screenX, screenY); } // causes the path to the mouse position to become highlighted (toCursor contains // a list of points that // receive highlighting). Uses DijkstraMap.findPath() to find the path, which is // surprisingly fast. @Override public boolean mouseMoved(int screenX, int screenY) { if (!awaitedMoves.isEmpty()) return false; if (cursor.x == screenX && cursor.y == screenY) { return false; } if (fovmap[(screenX) / 2][screenY] > 0.0) { cursor = Coord.get((screenX) / 2, screenY); // Uses DijkstraMap to get a path. from the player's position to the cursor toCursor = playerToCursor.findPath( 30, null, null, Coord.get(player.gridX, player.gridY), cursor); } return false; } })); // set this to true to test visual input on desktop input.forceButtons = false; // actions to give names to in the visual input menu input.init("filter", "??? help?", "quit"); // ABSOLUTELY NEEDED TO HANDLE INPUT Gdx.input.setInputProcessor(new InputMultiplexer(input, stage)); subCell.setOffsetY(messages.getGridHeight() * cellHeight); // and then add display and messages, our two visual components, to the list of things that act // in Stage. stage.addActor(display); // stage.addActor(subCell); // this is not added since it is manually drawn after other steps stage.addActor(messages); viewport = input.resizeInnerStage(stage); }