Vault Door generation

This commit is contained in:
simon 2018-01-17 22:01:59 -05:00
parent 815f3de234
commit ffca185fe5
5 changed files with 132 additions and 23 deletions

View File

@ -42,6 +42,8 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C
return Factory.deserialise(obj); return Factory.deserialise(obj);
} else if (objType == RadioTower.ID) { } else if (objType == RadioTower.ID) {
return RadioTower.deserialize(obj); return RadioTower.deserialize(obj);
} else if (objType == VaultDoor.ID) {
return VaultDoor.deserialize(obj);
} }
return null; return null;

View File

@ -7,7 +7,6 @@ import net.simon987.server.game.Programmable;
import net.simon987.server.game.Updatable; import net.simon987.server.game.Updatable;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.awt.*;
import java.util.ArrayList; import java.util.ArrayList;
public class RadioTower extends GameObject implements Programmable, Updatable { public class RadioTower extends GameObject implements Programmable, Updatable {
@ -96,25 +95,4 @@ public class RadioTower extends GameObject implements Programmable, Updatable {
return lastMessages; 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;
}
}
} }

View File

@ -1,6 +1,7 @@
package net.simon987.npcplugin; package net.simon987.npcplugin;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.crypto.RandomStringGenerator; import net.simon987.server.crypto.RandomStringGenerator;
import net.simon987.server.game.Enterable; 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.Programmable;
import net.simon987.server.game.Updatable; import net.simon987.server.game.Updatable;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;
import java.util.Arrays; import java.util.Arrays;
@ -16,6 +18,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
private static final int MAP_INFO = 0x0800; private static final int MAP_INFO = 0x0800;
public static final int ID = 5;
/** /**
* Password to open the vault door * Password to open the vault door
*/ */
@ -107,7 +110,37 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
@Override @Override
public BasicDBObject mongoSerialise() { 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;
} }
} }

View File

@ -3,6 +3,7 @@ package net.simon987.npcplugin.event;
import net.simon987.npcplugin.Factory; import net.simon987.npcplugin.Factory;
import net.simon987.npcplugin.NpcPlugin; import net.simon987.npcplugin.NpcPlugin;
import net.simon987.npcplugin.RadioTower; import net.simon987.npcplugin.RadioTower;
import net.simon987.npcplugin.VaultDoor;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener; import net.simon987.server.event.GameEventListener;
@ -93,6 +94,45 @@ public class WorldCreationListener implements GameEventListener {
") (" + p.x + ", " + p.y + ")"); ") (" + 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 + ")");
}
} }
} }
} }

View File

@ -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() { public long getObjectId() {
return objectId; return objectId;
} }