From 72808cdc179f544115031f598b51a55cea9fde54 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 28 Apr 2017 20:18:19 -0400 Subject: [PATCH] v0.6.0: generalized branch placement logic --- .../levels/builders/Builder.java | 71 ++++++++++++++++ .../levels/builders/LineBuilder.java | 72 ++-------------- .../levels/builders/LoopBuilder.java | 85 ++----------------- 3 files changed, 83 insertions(+), 145 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/Builder.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/Builder.java index 9f63a69c4..842630766 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/Builder.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/Builder.java @@ -22,6 +22,7 @@ package com.shatteredpixel.shatteredpixeldungeon.levels.builders; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room; +import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.tunnel.TunnelRoom; import com.watabou.utils.GameMath; import com.watabou.utils.Point; import com.watabou.utils.PointF; @@ -241,4 +242,74 @@ public abstract class Builder { return -1; } } + + //places the rooms in roomsToBranch into branches from rooms in branchable. + //note that the three arrays should be separate, they may contain the same rooms however + protected static void createBranches( ArrayList rooms, ArrayList branchable, + ArrayList roomsToBranch, float[] tunnelChances){ + + int i = 0; + float angle; + int tries; + Room curr; + ArrayList tunnelsThisBranch = new ArrayList<>(); + while (i < roomsToBranch.size()){ + + tunnelsThisBranch.clear(); + curr = Random.element(branchable); + + int tunnels = Random.chances(tunnelChances); + for (int j = 0; j < tunnels; j++){ + TunnelRoom t = new TunnelRoom(); + tries = 10; + + do { + angle = placeRoom(rooms, curr, t, Random.Float(360f)); + tries--; + } while (angle == -1 && tries >= 0); + + if (angle == -1) { + for (Room r : tunnelsThisBranch){ + r.clearConnections(); + rooms.remove(r); + } + tunnelsThisBranch.clear(); + break; + } else { + tunnelsThisBranch.add(t); + rooms.add(t); + } + + curr = t; + } + + if (tunnelsThisBranch.size() != tunnels){ + continue; + } + + Room r = roomsToBranch.get(i); + + tries = 10; + + do { + angle = placeRoom(rooms, curr, r, Random.Float(360f)); + tries--; + } while (angle == -1 && tries >= 0); + + if (angle == -1){ + for (Room t : tunnelsThisBranch){ + t.clearConnections(); + rooms.remove(t); + } + tunnelsThisBranch.clear(); + continue; + } + + + if (r.maxConnections(Room.ALL) > 1 && Random.Int(2) == 0) + branchable.add(r); + + i++; + } + } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LineBuilder.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LineBuilder.java index 6f82af602..45d0d8c1e 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LineBuilder.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LineBuilder.java @@ -130,74 +130,12 @@ public class LineBuilder extends Builder { curr = r; } - //place branches - int i = roomsOnPath; - float angle; - int tries; - ArrayList tunnelsThisBranch = new ArrayList<>(); - while (i < multiConnections.size() + singleConnections.size()){ - - tunnelsThisBranch.clear(); - curr = Random.element(branchable); - - int tunnels = Random.chances(branchTunnelChances); - for (int j = 0; j < tunnels; j++){ - TunnelRoom t = new TunnelRoom(); - tries = 10; - - do { - angle = placeRoom(rooms, curr, t, Random.Float(360f)); - tries--; - } while (angle == -1 && tries >= 0); - - if (angle == -1) { - for (Room r : tunnelsThisBranch){ - r.clearConnections(); - rooms.remove(r); - } - tunnelsThisBranch.clear(); - break; - } else { - tunnelsThisBranch.add(t); - rooms.add(t); - } - - curr = t; - } - - if (tunnelsThisBranch.size() != tunnels){ - continue; - } - - Room r; - if (i < multiConnections.size()) { - r = multiConnections.get(i); - } else { - r = singleConnections.get(i - multiConnections.size()); - } - - tries = 10; - - do { - angle = placeRoom(rooms, curr, r, Random.Float(360f)); - tries--; - } while (angle == -1 && tries >= 0); - - if (angle == -1){ - for (Room t : tunnelsThisBranch){ - t.clearConnections(); - rooms.remove(t); - } - tunnelsThisBranch.clear(); - continue; - } - - - if (r.maxConnections(Room.ALL) > 1 && Random.Int(2) == 0) - branchable.add(r); - - i++; + ArrayList roomsToBranch = new ArrayList<>(); + for (int i = roomsOnPath; i < multiConnections.size(); i++){ + roomsToBranch.add(multiConnections.get(i)); } + roomsToBranch.addAll(singleConnections); + createBranches(rooms, branchable, roomsToBranch, branchTunnelChances); findNeighbours(rooms); diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LoopBuilder.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LoopBuilder.java index 563e6803d..aca2e65ee 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LoopBuilder.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/levels/builders/LoopBuilder.java @@ -22,10 +22,9 @@ package com.shatteredpixel.shatteredpixeldungeon.levels.builders; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.Room; -import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.special.ShopRoom; +import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.EmptyRoom; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.EntranceRoom; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.ExitRoom; -import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.standard.StandardRoom; import com.shatteredpixel.shatteredpixeldungeon.levels.rooms.tunnel.TunnelRoom; import com.watabou.utils.Random; @@ -78,8 +77,6 @@ public class LoopBuilder extends Builder { entrance = r; } else if (r instanceof ExitRoom) { exit = r; - } else if (r instanceof ShopRoom && r.maxConnections(Room.ALL) == 1){ - shop = r; } else if (r.maxConnections(Room.ALL) > 1){ multiConnections.add(r); } else if (r.maxConnections(Room.ALL) == 1){ @@ -122,9 +119,7 @@ public class LoopBuilder extends Builder { for (int i = 0; i < loop.size(); i++){ Room r = loop.get(i); targetAngle += angleChange; - float placeAngle; - if ((placeAngle = placeRoom(rooms, prev, r, targetAngle)) != -1) { - //targetAngle += (targetAngle - placeAngle); + if (placeRoom(rooms, prev, r, targetAngle) != -1) { prev = r; if (!rooms.contains(prev)) rooms.add(prev); @@ -138,79 +133,13 @@ public class LoopBuilder extends Builder { ArrayList branchable = new ArrayList<>(); for (Room r : loop){ - if (r instanceof StandardRoom) branchable.add(r); + if (r instanceof EmptyRoom) branchable.add(r); } - int i = 0; - - Room curr; - float angle; - int tries; - ArrayList tunnelsThisBranch = new ArrayList<>(); - //TODO this is almost identical to logic in linebuilder, can probably generalize - while (i < multiConnections.size() + singleConnections.size()){ - - tunnelsThisBranch.clear(); - curr = Random.element(branchable); - - int tunnels = Random.chances(branchTunnelChances); - for (int j = 0; j < tunnels; j++){ - TunnelRoom t = new TunnelRoom(); - tries = 10; - - do { - angle = placeRoom(rooms, curr, t, Random.Float(360f)); - tries--; - } while (angle == -1 && tries >= 0); - - if (angle == -1) { - for (Room r : tunnelsThisBranch){ - r.clearConnections(); - rooms.remove(r); - } - tunnelsThisBranch.clear(); - break; - } else { - tunnelsThisBranch.add(t); - rooms.add(t); - } - - curr = t; - } - - if (tunnelsThisBranch.size() != tunnels){ - continue; - } - - Room r; - if (i < multiConnections.size()) { - r = multiConnections.get(i); - } else { - r = singleConnections.get(i - multiConnections.size()); - } - - tries = 10; - - do { - angle = placeRoom(rooms, curr, r, Random.Float(360f)); - tries--; - } while (angle == -1 && tries >= 0); - - if (angle == -1){ - for (Room t : tunnelsThisBranch){ - t.clearConnections(); - rooms.remove(t); - } - tunnelsThisBranch.clear(); - continue; - } - - - if (r.maxConnections(Room.ALL) > 1 && Random.Int(2) == 0) - branchable.add(r); - - i++; - } + ArrayList roomsToBranch = new ArrayList<>(); + roomsToBranch.addAll(multiConnections); + roomsToBranch.addAll(singleConnections); + createBranches(rooms, branchable, roomsToBranch, branchTunnelChances); findNeighbours(rooms);