diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java index 54c1c6c..558f992 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java @@ -1,5 +1,11 @@ package net.simon987.npcplugin; +import net.simon987.server.GameServer; +import net.simon987.server.game.Direction; +import net.simon987.server.game.World; + +import java.util.ArrayList; + public class VaultDimension { /** @@ -11,7 +17,18 @@ public class VaultDimension { name = "v" + vaultDoorId + "-"; + VaultWorldGenerator generator = new VaultWorldGenerator(); + + ArrayList openings = new ArrayList<>(); + openings.add(Direction.WEST); + openings.add(Direction.SOUTH); + openings.add(Direction.EAST); + openings.add(Direction.NORTH); + + World vWorld = generator.generateVaultWorld(0x7FFF, 0x7FFF, openings, name); + + GameServer.INSTANCE.getGameUniverse().addWorld(vWorld); } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java index d78e2bf..2cc2796 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java @@ -4,10 +4,7 @@ import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import net.simon987.server.GameServer; import net.simon987.server.crypto.RandomStringGenerator; -import net.simon987.server.game.Enterable; -import net.simon987.server.game.GameObject; -import net.simon987.server.game.Programmable; -import net.simon987.server.game.Updatable; +import net.simon987.server.game.*; import net.simon987.server.logging.LogManager; import org.json.simple.JSONObject; @@ -31,6 +28,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up */ private boolean open = false; + private World homeWorld; /** * Number of ticks to remain the door open @@ -40,11 +38,25 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up private int openedTimer = 0; private int cypherId; - public VaultDoor(int cypherId) { + public VaultDoor(int cypherId, long objectId) { this.cypherId = cypherId; this.randomStringGenerator = new RandomStringGenerator(); + setObjectId(objectId); this.password = "12345678".toCharArray(); + + + //Get or generate vault world + World world = GameServer.INSTANCE.getGameUniverse().getWorld(0x7FFF, 0x7FFF, + false, "v" + getObjectId() + "-"); + + if (world != null) { + homeWorld = world; + } else { + + VaultDimension vaultDimension = new VaultDimension(getObjectId()); + } + } @@ -143,11 +155,10 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up public static VaultDoor deserialize(DBObject obj) { - VaultDoor vaultDoor = new VaultDoor(0); //cypherId ? - vaultDoor.setObjectId((long) obj.get("i")); + VaultDoor vaultDoor = new VaultDoor(0, (long) obj.get("i")); //cypherId ? vaultDoor.setX((int) obj.get("x")); vaultDoor.setY((int) obj.get("y")); - // vaultDoor.password = (char[]) obj.get("pw"); + vaultDoor.password = ((String) obj.get("pw")).toCharArray(); return vaultDoor; } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorld.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorld.java deleted file mode 100644 index 5848c02..0000000 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorld.java +++ /dev/null @@ -1,15 +0,0 @@ -package net.simon987.npcplugin; - -import net.simon987.server.game.TileMap; -import net.simon987.server.game.World; - -public class VaultWorld extends World { - - private String dimension; - - public VaultWorld(int x, int y, TileMap tileMap) { - super(x, y, tileMap); - } - - -} diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java index 8c1a572..ae465ed 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultWorldGenerator.java @@ -1,9 +1,112 @@ package net.simon987.npcplugin; +import net.simon987.server.game.Direction; +import net.simon987.server.game.TileMap; +import net.simon987.server.game.World; +import net.simon987.server.logging.LogManager; + +import java.awt.*; +import java.util.ArrayList; + public class VaultWorldGenerator { - public VaultWorld generateVaultWorld() { - return null; + public World generateVaultWorld(int x, int y, ArrayList openings, String dimension) { + + LogManager.LOGGER.info("Generating vault World"); + + /* + * Openings are always at the same spot (marked by '?') + * 1. Rectangles of random size are generated + * 2. They are connected with tunnels from bottom to top, + * by their roomCenter, alternating between horizontal first + * tunnels and vertical first + * 3. Tiles adjacent to floor tiles are set to wall tiles + * Example map: + * + * # # # # # # # - - - # ? ? # - - - - - - - - - - + * # o o o o o # - - - # ? ? # - - - - - - - - - - + * # o o o o o # # # # # # o # - - - - - - - - - - + * # o o o o o o o o o o o o # - - - - - - - - - - + * # o o o o o # # # # # # # # # # # - - - - - - - + * # o o o o o # - - - - # o o o o # - - - - - - - + * # o o o o o # # # # # # o o o o # - - - - - - - + * # # o o o o o o o o o o o o o o # - - - - - - - + * - # o # # # # # # # # # o o o o # - - - - - - - + * - # o # - - - - - - - # # # # # # - - - - - - - + * # # o # - - - - - - - - - - - - - - - - - # # # + * ? ? o # # # # # # # # # # # # # # # # # # # ? ? + * ? ? o o o o o o o o o o o o o o o o o o o o ? ? + * # # # # # # # # # # # # # # # # # # o # # # # # + * - - - - - - - - - - - - - - - - - # o # - - - - + * - # # # # # # # # # - - - - - - - # o # - - - - + * - # o o o o o o o # - - - # # # # # o # # # # - + * - # o o o o o o o # # # # # o o o o o o o o # - + * - # o o o o o o o o o o o # o o o o o o o o # - + * - # o o o o o o o # # # o # o o o o o o o o # - + * - # o o o o o o o # - # o # # # # # # # # # # - + * - # o o o o o o o # # # o # - - - - - - - - - - + * - # # # # # # # # # # ? ? # - - - - - - - - - - + * - - - - - - - - - - # ? ? # - - - - - - - - - - + */ + + int worldSize = 20; + int floorTile = 4; + int wallTile = 5; + int minRoomCount = 3; + int maxRoomCount = 6; + int minRoomWidth = 4; + int minRoomHeight = 4; + int maxRoomWidth = 8; + int maxRoomHeight = 8; + + ArrayList roomCenters = new ArrayList<>(); + + World world = new World(x, y, new TileMap(worldSize, worldSize), dimension); + + TileMap map = world.getTileMap(); + + //Create openings + for (Direction opening : openings) { + switch (opening) { + case NORTH: + + map.setTileAt(floorTile, worldSize / 2, 0); + map.setTileAt(floorTile, worldSize / 2, 1); + map.setTileAt(floorTile, worldSize / 2 - 1, 0); + map.setTileAt(floorTile, worldSize / 2 - 1, 1); + roomCenters.add(new Point(worldSize / 2, 1)); + break; + case EAST: + + map.setTileAt(floorTile, worldSize - 1, worldSize / 2); + map.setTileAt(floorTile, worldSize - 1, worldSize / 2 - 1); + map.setTileAt(floorTile, worldSize - 2, worldSize / 2); + map.setTileAt(floorTile, worldSize - 2, worldSize / 2 - 1); + roomCenters.add(new Point(worldSize - 1, worldSize / 2 - 1)); + break; + case SOUTH: + + map.setTileAt(floorTile, worldSize / 2, worldSize - 1); + map.setTileAt(floorTile, worldSize / 2, worldSize - 2); + map.setTileAt(floorTile, worldSize / 2 - 1, worldSize - 1); + map.setTileAt(floorTile, worldSize / 2 - 1, worldSize - 2); + roomCenters.add(new Point(worldSize / 2, worldSize - 2)); + break; + case WEST: + + map.setTileAt(floorTile, 0, worldSize / 2); + map.setTileAt(floorTile, 0, worldSize / 2 - 1); + map.setTileAt(floorTile, 1, worldSize / 2); + map.setTileAt(floorTile, 1, worldSize / 2 - 1); + roomCenters.add(new Point(0, worldSize / 2 - 1)); + break; + } + } + + //Generate rectangles of random size + + return world; + } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java index 6bbcf34..83c9129 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java @@ -99,10 +99,9 @@ public class WorldCreationListener implements GameEventListener { p = world.getRandomPassableTile(); if (p != null) { - VaultDoor vaultDoor = new VaultDoor(0); //todo cypherId ? + VaultDoor vaultDoor = new VaultDoor(0, GameServer.INSTANCE.getGameUniverse().getNextObjectId()); //todo cypherId ? vaultDoor.setWorld(world); - vaultDoor.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); vaultDoor.setX(p.x); vaultDoor.setY(p.y); diff --git a/Server/src/main/java/net/simon987/server/game/GameUniverse.java b/Server/src/main/java/net/simon987/server/game/GameUniverse.java index 350b8b7..2f8d693 100644 --- a/Server/src/main/java/net/simon987/server/game/GameUniverse.java +++ b/Server/src/main/java/net/simon987/server/game/GameUniverse.java @@ -82,8 +82,8 @@ public class GameUniverse { public World getWorld(int x, int y, boolean createNew, String dimension) { // Wrapping coordinates around cyclically - x %= maxWidth+1; - y %= maxWidth+1; + x %= maxWidth; + y %= maxWidth; // Looks for a previously loaded world World world = getLoadedWorld(x, y, dimension); @@ -113,8 +113,8 @@ public class GameUniverse { World getLoadedWorld(int x, int y, String dimension) { // Wrapping coordinates around cyclically - x %= maxWidth+1; - y %= maxWidth+1; + x %= maxWidth; + y %= maxWidth; return worlds.get(World.idFromCoordinates(x, y, dimension)); } diff --git a/Server/src/main/java/net/simon987/server/game/World.java b/Server/src/main/java/net/simon987/server/game/World.java index f239d2d..19f3db7 100644 --- a/Server/src/main/java/net/simon987/server/game/World.java +++ b/Server/src/main/java/net/simon987/server/game/World.java @@ -31,7 +31,7 @@ public class World implements MongoSerialisable { private TileMap tileMap; - private static String dimension = "w-"; + private String dimension; private ConcurrentHashMap gameObjects = new ConcurrentHashMap<>(8); @@ -40,10 +40,11 @@ public class World implements MongoSerialisable { */ private int updatable = 0; - public World(int x, int y, TileMap tileMap) { + public World(int x, int y, TileMap tileMap, String dimension) { this.x = x; this.y = y; this.tileMap = tileMap; + this.dimension = dimension; this.worldSize = tileMap.getWidth(); } @@ -174,6 +175,7 @@ public class World implements MongoSerialisable { dbObject.put("_id", getId()); + dbObject.put("dimension", getDimension()); dbObject.put("objects", objects); dbObject.put("terrain", tileMap.mongoSerialise()); @@ -210,6 +212,7 @@ public class World implements MongoSerialisable { World world = new World((int) dbObject.get("size")); world.x = (int) dbObject.get("x"); world.y = (int) dbObject.get("y"); + world.dimension = (String) dbObject.get("dimension"); world.updatable = (int) dbObject.get("updatable"); world.tileMap = TileMap.deserialize((BasicDBObject) dbObject.get("terrain"), world.getWorldSize()); diff --git a/Server/src/main/java/net/simon987/server/game/WorldGenerator.java b/Server/src/main/java/net/simon987/server/game/WorldGenerator.java index 37066b6..be669ba 100755 --- a/Server/src/main/java/net/simon987/server/game/WorldGenerator.java +++ b/Server/src/main/java/net/simon987/server/game/WorldGenerator.java @@ -87,7 +87,7 @@ public class WorldGenerator { */ private static World generateEmptyWorld(int locX, int locY) { - return new World(locX, locY, new TileMap(DEFAULT_WORLD_SIZE, DEFAULT_WORLD_SIZE)); + return new World(locX, locY, new TileMap(DEFAULT_WORLD_SIZE, DEFAULT_WORLD_SIZE), "w-"); } /**