From ffca185fe50fbbdcb85760433366a315e63583b8 Mon Sep 17 00:00:00 2001 From: simon Date: Wed, 17 Jan 2018 22:01:59 -0500 Subject: [PATCH] Vault Door generation --- .../net/simon987/npcplugin/NpcPlugin.java | 2 + .../net/simon987/npcplugin/RadioTower.java | 22 -------- .../net/simon987/npcplugin/VaultDoor.java | 35 +++++++++++- .../event/WorldCreationListener.java | 40 +++++++++++++ .../net/simon987/server/game/GameObject.java | 56 +++++++++++++++++++ 5 files changed, 132 insertions(+), 23 deletions(-) diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java index c1fc220..2315624 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java @@ -42,6 +42,8 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C return Factory.deserialise(obj); } else if (objType == RadioTower.ID) { return RadioTower.deserialize(obj); + } else if (objType == VaultDoor.ID) { + return VaultDoor.deserialize(obj); } return null; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java index 2dca182..8fa2c7c 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java @@ -7,7 +7,6 @@ import net.simon987.server.game.Programmable; import net.simon987.server.game.Updatable; import org.json.simple.JSONObject; -import java.awt.*; import java.util.ArrayList; public class RadioTower extends GameObject implements Programmable, Updatable { @@ -96,25 +95,4 @@ public class RadioTower extends GameObject implements Programmable, Updatable { return lastMessages; } - /** - * Get the first directly adjacent tile (starting east, going clockwise) - */ - public Point getAdjacentTile() { - - if (!getWorld().isTileBlocked(getX() + 1, getY())) { - return new Point(getX() + 1, getY()); - - } else if (!getWorld().isTileBlocked(getX(), getY() + 1)) { - return new Point(getX(), getY() + 1); - - } else if (!getWorld().isTileBlocked(getX() - 1, getY())) { - return new Point(getX() - 1, getY()); - - } else if (!getWorld().isTileBlocked(getX(), getY() - 1)) { - return new Point(getX(), getY() - 1); - } else { - return null; - } - - } } 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 e5afa6f..7461fa6 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java @@ -1,6 +1,7 @@ package net.simon987.npcplugin; 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; @@ -8,6 +9,7 @@ import net.simon987.server.game.GameObject; import net.simon987.server.game.Programmable; import net.simon987.server.game.Updatable; import net.simon987.server.logging.LogManager; +import org.json.simple.JSONObject; import java.util.Arrays; @@ -16,6 +18,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up private static final int MAP_INFO = 0x0800; + public static final int ID = 5; /** * Password to open the vault door */ @@ -107,7 +110,37 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up @Override public BasicDBObject mongoSerialise() { - return null; + BasicDBObject dbObject = new BasicDBObject(); + + dbObject.put("i", getObjectId()); + dbObject.put("x", getX()); + dbObject.put("y", getY()); + dbObject.put("t", ID); + + return dbObject; + } + + @Override + public JSONObject serialise() { + + JSONObject json = new JSONObject(); + + json.put("i", getObjectId()); + json.put("x", getX()); + json.put("y", getY()); + json.put("t", ID); + + return json; + } + + public static VaultDoor deserialize(DBObject obj) { + + VaultDoor vaultDoor = new VaultDoor(0); //cypherId ? + vaultDoor.setObjectId((long) obj.get("i")); + vaultDoor.setX((int) obj.get("x")); + vaultDoor.setY((int) obj.get("y")); + + return vaultDoor; } } 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 b623680..6bbcf34 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 @@ -3,6 +3,7 @@ package net.simon987.npcplugin.event; import net.simon987.npcplugin.Factory; import net.simon987.npcplugin.NpcPlugin; import net.simon987.npcplugin.RadioTower; +import net.simon987.npcplugin.VaultDoor; import net.simon987.server.GameServer; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; @@ -93,6 +94,45 @@ public class WorldCreationListener implements GameEventListener { ") (" + p.x + ", " + p.y + ")"); } } + + //Also spawn a Vault in the same World + p = world.getRandomPassableTile(); + if (p != null) { + + VaultDoor vaultDoor = new VaultDoor(0); //todo cypherId ? + + vaultDoor.setWorld(world); + vaultDoor.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); + vaultDoor.setX(p.x); + vaultDoor.setY(p.y); + + int counter = 300; + while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0 + || vaultDoor.getAdjacentTileCount(true) < 8) { + p = world.getRandomPassableTile(); + + if (p == null) { + //World is full + return; + } + + vaultDoor.setX(p.x); + vaultDoor.setY(p.y); + + counter--; + + if (counter <= 0) { + //Reached maximum amount of retries + return; + } + } + + world.addObject(vaultDoor); + world.incUpdatable(); //In case the Factory & Radio Tower couldn't be spawned. + + LogManager.LOGGER.info("Spawned Vault Door at (" + world.getX() + ", " + world.getY() + + ") (" + p.x + ", " + p.y + ")"); + } } } } diff --git a/Server/src/main/java/net/simon987/server/game/GameObject.java b/Server/src/main/java/net/simon987/server/game/GameObject.java index 355d14d..cd2b30e 100755 --- a/Server/src/main/java/net/simon987/server/game/GameObject.java +++ b/Server/src/main/java/net/simon987/server/game/GameObject.java @@ -122,6 +122,62 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable } + /** + * Get the first directly adjacent tile (starting east, going clockwise) + */ + public Point getAdjacentTile() { + + if (!getWorld().isTileBlocked(getX() + 1, getY())) { + return new Point(getX() + 1, getY()); + + } else if (!getWorld().isTileBlocked(getX(), getY() + 1)) { + return new Point(getX(), getY() + 1); + + } else if (!getWorld().isTileBlocked(getX() - 1, getY())) { + return new Point(getX() - 1, getY()); + + } else if (!getWorld().isTileBlocked(getX(), getY() - 1)) { + return new Point(getX(), getY() - 1); + } else { + return null; + } + } + + public int getAdjacentTileCount(boolean diagonals) { + + int count = 0; + + if (!getWorld().isTileBlocked(getX() + 1, getY())) { + count++; + } + if (!getWorld().isTileBlocked(getX(), getY() + 1)) { + count++; + } + if (!getWorld().isTileBlocked(getX() - 1, getY())) { + count++; + } + if (!getWorld().isTileBlocked(getX(), getY() - 1)) { + count++; + } + + if (diagonals) { + if (!getWorld().isTileBlocked(getX() + 1, getY() + 1)) { + count++; + } + if (!getWorld().isTileBlocked(getX() - 1, getY() + 1)) { + count++; + } + if (!getWorld().isTileBlocked(getX() + 1, getY() - 1)) { + count++; + } + if (!getWorld().isTileBlocked(getX() - 1, getY() - 1)) { + count++; + } + } + + return count; + } + public long getObjectId() { return objectId; }