From de10df7866244c2680a2abdf1e19aa54787acf96 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Thu, 22 Aug 2019 15:23:33 -0400 Subject: [PATCH] v0.7.5: added new functionality to maze generation --- .../levels/features/Maze.java | 45 ++++++++++++++----- .../levels/rooms/MazeRoom.java | 1 + .../rooms/connection/MazeConnectionRoom.java | 1 + .../levels/rooms/secret/SecretMazeRoom.java | 1 + 4 files changed, 37 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/features/Maze.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/features/Maze.java index 46fa5d110..cd2dde428 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/features/Maze.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/features/Maze.java @@ -56,6 +56,19 @@ public class Maze { return generate(r.width()+1, r.height()+1); } + public static boolean[][] generate(Rect r, int[] terrain, int width, int filledTerrainType){ + boolean[][] maze = new boolean[r.width()][r.height()]; + for (int x = 0; x < maze.length; x++) { + for (int y = 0; y < maze[0].length; y++) { + if (terrain[x + r.left + (y + r.top)*width] == filledTerrainType){ + maze[x][y] = FILLED; + } + } + } + + return generate(maze); + } + public static boolean[][] generate(int width, int height){ return generate(new boolean[width][height]); } @@ -123,22 +136,32 @@ public class Maze { return null; } + public static boolean allowDiagonals = false; + private static boolean checkValidMove( boolean[][] maze, int x, int y, int[] mov){ int sideX = 1 - Math.abs(mov[0]); int sideY = 1 - Math.abs(mov[1]); - //checking two tiles forward in the movement, and the tiles to their left/right - for (int i = 0; i < 2; i ++) { - x += mov[0]; - y += mov[1]; - //checks if tiles we're examining are valid and open - if ( x > 0 && x < maze.length-1 && y > 0 && y < maze[0].length-1 && - !maze[x][y] && !maze[x + sideX][y+ sideY] && !maze[x - sideX][y - sideY]){ - continue; - } else { - return false; - } + x += mov[0]; + y += mov[1]; + + if ( x <= 0 || x >= maze.length-1 || y <= 0 || y >= maze[0].length-1){ + return false; + } else if (maze[x][y] || maze[x + sideX][y + sideY] || maze[x - sideX][y - sideY]){ + return false; } + + x += mov[0]; + y += mov[1]; + + if ( x <= 0 || x >= maze.length-1 || y <= 0 || y >= maze[0].length-1){ + return false; + } else if (maze[x][y]){ + return false; + } else if (!allowDiagonals && (maze[x + sideX][y + sideY] || maze[x - sideX][y - sideY])){ + return false; + } + return true; } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/MazeRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/MazeRoom.java index a712f5a2c..0e8f31771 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/MazeRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/MazeRoom.java @@ -33,6 +33,7 @@ public class MazeRoom extends Room { Painter.fill(level, this, 1, Terrain.EMPTY); //true = space, false = wall + Maze.allowDiagonals = false; boolean[][] maze = Maze.generate(this); Painter.fill(level, this, 1, Terrain.EMPTY); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/MazeConnectionRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/MazeConnectionRoom.java index 87240361f..4839ac8a1 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/MazeConnectionRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/connection/MazeConnectionRoom.java @@ -33,6 +33,7 @@ public class MazeConnectionRoom extends ConnectionRoom { Painter.fill(level, this, 1, Terrain.EMPTY); //true = space, false = wall + Maze.allowDiagonals = false; boolean[][] maze = Maze.generate(this); Painter.fill(level, this, 1, Terrain.EMPTY); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretMazeRoom.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretMazeRoom.java index 73d2751e9..43b476ea0 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretMazeRoom.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/rooms/secret/SecretMazeRoom.java @@ -62,6 +62,7 @@ public class SecretMazeRoom extends SecretRoom { Painter.fill(level, this, 1, Terrain.EMPTY); //true = space, false = wall + Maze.allowDiagonals = false; boolean[][] maze = Maze.generate(this); boolean[] passable = new boolean[width()*height()];