Refactor: changed the way game objects and cpu hardware are saved/loaded from the database #151

This commit is contained in:
simon 2018-05-12 15:32:41 -04:00
parent 8d029cf621
commit 4cd58c86a5
96 changed files with 628 additions and 1221 deletions

View File

@ -3,7 +3,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.Memory;
import net.simon987.server.game.*;
import net.simon987.server.game.objects.*;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
@ -16,7 +16,6 @@ import java.util.Random;
public class Cubot extends GameObject implements Updatable, ControllableUnit, Programmable, Attackable, Rechargeable {
private static final char MAP_INFO = 0x0080;
public static final int ID = 1;
/**
* Hologram value that is displayed
@ -171,11 +170,25 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
NORMAL
}
public Cubot() {
}
public Cubot(Document document) {
super(document);
hp = document.getInteger("hp");
shield = document.getInteger("shield");
setDirection(Direction.getDirection(document.getInteger("direction")));
heldItem = document.getInteger("heldItem");
energy = document.getInteger("energy");
ServerConfiguration config = GameServer.INSTANCE.getConfig();
maxEnergy = config.getInt("battery_max_energy");
maxHp = config.getInt("cubot_max_hp");
maxShield = config.getInt("cubot_max_shield");
}
@Override
public char getMapInfo() {
return MAP_INFO;
@ -225,12 +238,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("i", getObjectId());
json.put("t", ID);
json.put("x", getX());
json.put("y", getY());
public JSONObject jsonSerialise() {
JSONObject json = super.jsonSerialise();
json.put("direction", getDirection().ordinal());
json.put("heldItem", heldItem);
json.put("hp", hp);
@ -251,12 +260,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("t", ID);
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("direction", getDirection().ordinal());
dbObject.put("heldItem", heldItem);
dbObject.put("hp", hp);
@ -275,26 +280,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
return dbObject;
}
public static Cubot deserialize(Document obj) {
Cubot cubot = new Cubot();
cubot.setObjectId((long) obj.get("i"));
cubot.setX((int) obj.get("x"));
cubot.setY((int) obj.get("y"));
cubot.hp = (int) obj.get("hp");
cubot.shield = (int) obj.get("shield");
cubot.setDirection(Direction.getDirection((int) obj.get("direction")));
cubot.heldItem = (int) obj.get("heldItem");
cubot.energy = (int) obj.get("energy");
ServerConfiguration config = GameServer.INSTANCE.getConfig();
cubot.maxEnergy = config.getInt("battery_max_energy");
cubot.maxHp = config.getInt("cubot_max_hp");
cubot.maxShield = config.getInt("cubot_max_shield");
return cubot;
}
/**
* Reset to 'factory settings', as it were when it was first created
*/

View File

@ -1,11 +1,9 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotBattery extends CpuHardware {
public class CubotBattery extends CubotHardware {
public static final int DEFAULT_ADDRESS = 0x000A;
@ -14,12 +12,15 @@ public class CubotBattery extends CpuHardware {
*/
public static final char HWID = 0x000A;
private Cubot cubot;
private static final int BATTERY_POLL = 1;
private static final int BATTERY_GET_MAX_CAPACITY = 2;
public CubotBattery(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotBattery(Document document) {
super(document);
}
@Override
@ -43,20 +44,4 @@ public class CubotBattery extends CpuHardware {
return HWID;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotBattery deserialize(Document obj) {
return new CubotBattery((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,22 +1,18 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Programmable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable;
import org.bson.Document;
import java.awt.*;
import java.util.ArrayList;
public class CubotComPort extends CpuHardware {
public class CubotComPort extends CubotHardware {
public static final char HWID = 0xD;
public static final int DEFAULT_ADDRESS = 0xD;
private Cubot cubot;
private static final int COMPORT_BUFFER_CLEAR = 0;
private static final int COMPORT_POLL = 1;
private static final int COMPORT_FRONT_PORT_OUT = 2;
@ -24,7 +20,11 @@ public class CubotComPort extends CpuHardware {
private static final int COMPORT_CONSOLE_CLEAR = 4;
public CubotComPort(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotComPort(Document document) {
super(document);
}
private static final int MESSAGE_LENGTH = 8;
@ -125,19 +125,4 @@ public class CubotComPort extends CpuHardware {
public char getId() {
return HWID;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotComPort deserialize(Document obj) {
return new CubotComPort((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,11 +1,9 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotCore extends CpuHardware {
public class CubotCore extends CubotHardware {
public static final int DEFAULT_ADDRESS = 0x000E;
@ -17,10 +15,12 @@ public class CubotCore extends CpuHardware {
private static final int CORE_STATUS_POLL = 1;
private static final int CORE_HULL_POLL = 2;
private Cubot cubot;
public CubotCore(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotCore(Document document) {
super(document);
}
@Override
@ -39,20 +39,4 @@ public class CubotCore extends CpuHardware {
public char getId() {
return HWID;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotCore deserialize(Document obj) {
return new CubotCore((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,13 +1,11 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.objects.Action;
import net.simon987.server.game.world.TileMap;
import org.bson.Document;
public class CubotDrill extends CpuHardware {
public class CubotDrill extends CubotHardware {
/**
* Hardware ID (Should be unique)
@ -19,10 +17,12 @@ public class CubotDrill extends CpuHardware {
private static final int DRILL_POLL = 1;
private static final int DRILL_GATHER = 2; // simplified gather
private Cubot cubot;
public CubotDrill(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotDrill(Document document) {
super(document);
}
@Override
@ -55,23 +55,6 @@ public class CubotDrill extends CpuHardware {
}
}
}
}
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotDrill deserialize(Document obj) {
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,11 +1,9 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotFloppyDrive extends CpuHardware {
public class CubotFloppyDrive extends CubotHardware {
/**
* Hardware ID (Should be unique)
@ -18,15 +16,24 @@ public class CubotFloppyDrive extends CpuHardware {
private static final int FLOPPY_READ_SECTOR = 2;
private static final int FLOPPY_WRITE_SECTOR = 3;
private Cubot cubot;
private FloppyDisk floppyDisk;
public CubotFloppyDrive(Cubot cubot) {
this.cubot = cubot;
super(cubot);
floppyDisk = new FloppyDisk();
}
public CubotFloppyDrive(Document document) {
super(document);
if (document.containsKey("floppy")) {
floppyDisk = new FloppyDisk((Document) document.get("floppy"));
} else {
floppyDisk = new FloppyDisk();
}
}
@Override
public void handleInterrupt(Status status) {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
@ -77,31 +84,6 @@ public class CubotFloppyDrive extends CpuHardware {
return HWID;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
if (floppyDisk != null) {
dbObject.put("floppy", floppyDisk.mongoSerialise());
}
return dbObject;
}
public static CubotFloppyDrive deserialize(Document obj) {
CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
if (obj.containsKey("floppy")) {
drive.floppyDisk = FloppyDisk.deserialise((Document) obj.get("floppy"));
}
return drive;
}
public FloppyDisk getFloppy() {
return floppyDisk;

View File

@ -0,0 +1,27 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import org.bson.Document;
public abstract class CubotHardware extends CpuHardware {
protected Cubot cubot;
public CubotHardware(Document document) {
this.cubot = (Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot"));
}
public CubotHardware(Cubot cubot) {
this.cubot = cubot;
}
@Override
public Document mongoSerialise() {
Document document = new Document();
document.put("type", getClass().getCanonicalName());
document.put("cubot", cubot.getObjectId());
return document;
}
}

View File

@ -1,11 +1,9 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotHologram extends CpuHardware {
public class CubotHologram extends CubotHardware {
/**
@ -15,8 +13,6 @@ public class CubotHologram extends CpuHardware {
public static final int DEFAULT_ADDRESS = 9;
private Cubot cubot;
private static final int HOLO_CLEAR = 0;
private static final int HOLO_DISPLAY_HEX = 1;
private static final int HOLO_DISPLAY_STRING = 2;
@ -26,7 +22,11 @@ public class CubotHologram extends CpuHardware {
private static final int STR_MAX_LEN = 8;
public CubotHologram(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotHologram(Document document) {
super(document);
}
@Override
@ -82,19 +82,4 @@ public class CubotHologram extends CpuHardware {
return HWID;
}
public static CubotHologram deserialize(Document obj) {
return new CubotHologram((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
}

View File

@ -1,11 +1,10 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotInventory extends CpuHardware {
public class CubotInventory extends CubotHardware {
/**
* Hardware ID (Should be unique)
@ -14,13 +13,15 @@ public class CubotInventory extends CpuHardware {
public static final int DEFAULT_ADDRESS = 6;
private Cubot cubot;
private static final int INV_CLEAR = 0;
private static final int INV_POLL = 1;
public CubotInventory(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotInventory(Document document) {
super(document);
}
@Override
@ -48,20 +49,4 @@ public class CubotInventory extends CpuHardware {
}
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotInventory deserialize(Document obj) {
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,11 +1,9 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotKeyboard extends CpuHardware {
public class CubotKeyboard extends CubotHardware {
public static final int DEFAULT_ADDRESS = 4;
@ -17,10 +15,12 @@ public class CubotKeyboard extends CpuHardware {
*/
public static final char HWID = 0x0004;
private Cubot cubot;
public CubotKeyboard(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotKeyboard(Document document) {
super(document);
}
@Override
@ -50,19 +50,4 @@ public class CubotKeyboard extends CpuHardware {
}
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotKeyboard deserialize(Document obj) {
return new CubotKeyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,18 +1,16 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.InventoryHolder;
import org.bson.Document;
import java.awt.*;
import java.util.ArrayList;
public class CubotLaser extends CpuHardware {
public class CubotLaser extends CubotHardware {
/**
* Hardware ID (Should be unique)
@ -21,8 +19,6 @@ public class CubotLaser extends CpuHardware {
public static final int DEFAULT_ADDRESS = 2;
private Cubot cubot;
private static final int LASER_WITHDRAW = 1;
private static final int LASER_DEPOSIT = 2;
private static final int LASER_ATTACK = 3;
@ -30,7 +26,11 @@ public class CubotLaser extends CpuHardware {
private static final int LASER_DAMAGE = 25;
public CubotLaser(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotLaser(Document document) {
super(document);
}
@Override
@ -90,19 +90,4 @@ public class CubotLaser extends CpuHardware {
}
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLaser deserialize(Document obj) {
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,20 +1,14 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.Direction;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.Direction;
import org.bson.Document;
import org.json.simple.JSONObject;
public class CubotLeg extends CpuHardware implements JSONSerialisable {
public class CubotLeg extends CubotHardware {
public static final int DEFAULT_ADDRESS = 1;
public static final String NAME = "Cubot Leg";
private static final int LEGS_SET_DIR = 1;
private static final int LEGS_SET_DIR_AND_WALK = 2;
@ -23,10 +17,12 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
*/
static final char HWID = 0x0001;
private Cubot cubot;
public CubotLeg(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotLeg(Document document) {
super(document);
}
@Override
@ -73,31 +69,4 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
}
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
return json;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLeg deserialize(Document obj) {
return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,19 +1,15 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class CubotLidar extends CpuHardware implements JSONSerialisable {
public class CubotLidar extends CubotHardware {
/**
* Hardware ID (Should be unique)
@ -22,8 +18,6 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
public static final int DEFAULT_ADDRESS = 3;
private Cubot cubot;
private static final int LIDAR_GET_POS = 1;
private static final int LIDAR_GET_PATH = 2;
private static final int LIDAR_GET_MAP = 3;
@ -33,9 +27,12 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
private static final int MEMORY_PATH_START = 0x0000;
public CubotLidar(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotLidar(Document document) {
super(document);
}
@Override
public char getId() {
@ -134,32 +131,5 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
break;
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", (int) HWID);
json.put("cubot", cubot.getObjectId());
return json;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static CubotLidar deserialize(Document obj) {
return new CubotLidar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -4,70 +4,34 @@ import net.simon987.cubotplugin.event.ChargeShieldCommandListener;
import net.simon987.cubotplugin.event.CpuInitialisationListener;
import net.simon987.cubotplugin.event.UserCreationListener;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer {
public class CubotPlugin extends ServerPlugin {
@Override
public void init(ServerConfiguration config) {
public void init(ServerConfiguration config, GameRegistry registry) {
listeners.add(new CpuInitialisationListener());
listeners.add(new UserCreationListener());
listeners.add(new ChargeShieldCommandListener());
registry.registerGameObject(Cubot.class);
registry.registerHardware(CubotLeg.class);
registry.registerHardware(CubotLaser.class);
registry.registerHardware(CubotLidar.class);
registry.registerHardware(CubotDrill.class);
registry.registerHardware(CubotInventory.class);
registry.registerHardware(CubotKeyboard.class);
registry.registerHardware(CubotHologram.class);
registry.registerHardware(CubotBattery.class);
registry.registerHardware(CubotFloppyDrive.class);
registry.registerHardware(CubotComPort.class);
registry.registerHardware(CubotShield.class);
registry.registerHardware(CubotCore.class);
LogManager.LOGGER.info("Initialised Cubot plugin");
}
@Override
public GameObject deserializeObject(Document object) {
int objType = (int) object.get("t");
if (objType == Cubot.ID) {
return Cubot.deserialize(object);
}
return null;
}
@Override
public CpuHardware deserializeHardware(Document obj) {
int hwid = (int) obj.get("hwid");
switch (hwid) {
case CubotLeg.HWID:
return CubotLeg.deserialize(obj);
case CubotLaser.HWID:
return CubotLaser.deserialize(obj);
case CubotLidar.HWID:
return CubotLidar.deserialize(obj);
case CubotDrill.HWID:
return CubotDrill.deserialize(obj);
case CubotInventory.HWID:
return CubotInventory.deserialize(obj);
case CubotKeyboard.HWID:
return CubotKeyboard.deserialize(obj);
case CubotHologram.HWID:
return CubotHologram.deserialize(obj);
case CubotBattery.HWID:
return CubotBattery.deserialize(obj);
case CubotFloppyDrive.HWID:
return CubotFloppyDrive.deserialize(obj);
case CubotComPort.HWID:
return CubotComPort.deserialize(obj);
case CubotShield.HWID:
return CubotShield.deserialize(obj);
case CubotCore.HWID:
return CubotCore.deserialize(obj);
}
return null;
}
}

View File

@ -1,11 +1,10 @@
package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import org.bson.Document;
public class CubotShield extends CpuHardware {
public class CubotShield extends CubotHardware {
public static final char DEFAULT_ADDRESS = 0x000F;
@ -13,12 +12,15 @@ public class CubotShield extends CpuHardware {
private static final int SHIELD_CHARGE = 1;
private static final int SHIELD_POLL = 2;
private Cubot cubot;
public static final int COST = GameServer.INSTANCE.getConfig().getInt("shield_energy_cost");
public CubotShield(Cubot cubot) {
this.cubot = cubot;
super(cubot);
}
public CubotShield(Document document) {
super(document);
}
@Override
@ -26,16 +28,6 @@ public class CubotShield extends CpuHardware {
return HWID;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
@Override
public void handleInterrupt(Status status) {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
@ -48,8 +40,4 @@ public class CubotShield extends CpuHardware {
getCpu().getRegisterSet().getRegister("B").setValue(shield);
}
}
public static CubotShield deserialize(Document obj) {
return new CubotShield((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -28,6 +28,11 @@ public class FloppyDisk implements MongoSerializable {
this.memory = new Memory(512 * 1440);
}
public FloppyDisk(Document document) {
this.rwHeadTrack = (int) document.get("rwHeadTrack");
this.memory = new Memory((Document) document.get("memory"));
}
/**
* Read 512 words from the specified sector to cpu memory at specified address
*
@ -92,16 +97,6 @@ public class FloppyDisk implements MongoSerializable {
return dbObject;
}
public static FloppyDisk deserialise(Document obj) {
FloppyDisk floppyDisk = new FloppyDisk();
floppyDisk.rwHeadTrack = (int) obj.get("rwHeadTrack");
floppyDisk.memory = Memory.deserialize((Document) obj.get("memory"));
return floppyDisk;
}
public Memory getMemory() {
return memory;
}

View File

@ -5,7 +5,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.GameObject;
/**
* Debug command to add shield points to a Cubot

View File

@ -11,10 +11,18 @@ import org.bson.Document;
*/
public class Clock extends CpuHardware {
public static final char HWID = 0x0008;
private static final char HWID = 0x0008;
public static final char DEFAULT_ADDRESS = 0x0008;
public Clock() {
}
public Clock(Document document) {
super(document);
}
@Override
public void handleInterrupt(Status status) {
@ -31,17 +39,12 @@ public class Clock extends CpuHardware {
return HWID;
}
public static Clock deserialize() {
return new Clock();
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("type", getClass().getCanonicalName());
return dbObject;
}

View File

@ -2,36 +2,23 @@ package net.simon987.mischwplugin;
import net.simon987.mischwplugin.event.CpuInitialisationListener;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
/**
* Plugin that adds miscellaneous hardware to the game
*/
public class MiscHWPlugin extends ServerPlugin implements CpuHardwareDeserializer {
public class MiscHWPlugin extends ServerPlugin {
@Override
public void init(ServerConfiguration config) {
public void init(ServerConfiguration config, GameRegistry registry) {
listeners.add(new CpuInitialisationListener());
registry.registerHardware(RandomNumberGenerator.class);
registry.registerHardware(Clock.class);
LogManager.LOGGER.info("Initialised Misc Hardware Plugin");
}
@Override
public CpuHardware deserializeHardware(Document hwJson) {
int hwid = (int) hwJson.get("hwid");
switch (hwid) {
case RandomNumberGenerator.HWID:
return RandomNumberGenerator.deserialize();
case Clock.HWID:
return Clock.deserialize();
}
return null;
}
}

View File

@ -11,7 +11,7 @@ import java.util.Random;
*/
public class RandomNumberGenerator extends CpuHardware {
public static final char HWID = 0x0007;
private static final char HWID = 0x0007;
public static final char DEFAULT_ADDRESS = 0x0007;
@ -21,6 +21,11 @@ public class RandomNumberGenerator extends CpuHardware {
random = new Random();
}
public RandomNumberGenerator(Document document) {
super(document);
random = new Random();
}
@Override
public void handleInterrupt(Status status) {
@ -37,13 +42,8 @@ public class RandomNumberGenerator extends CpuHardware {
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("type", getClass().getCanonicalName());
return dbObject;
}
public static RandomNumberGenerator deserialize() {
return new RandomNumberGenerator();
}
}

View File

@ -2,11 +2,10 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Rechargeable;
import net.simon987.server.game.Updatable;
import net.simon987.server.logging.LogManager;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Rechargeable;
import net.simon987.server.game.objects.Updatable;
import org.bson.Document;
import org.json.simple.JSONObject;
@ -17,8 +16,6 @@ import java.util.ArrayList;
*/
public class ElectricBox extends GameObject implements Updatable, Attackable {
public static final int ID = 7;
/**
* Hit points
*/
@ -42,8 +39,12 @@ public class ElectricBox extends GameObject implements Updatable, Attackable {
private ArrayList<Attackable> nearObjects = new ArrayList<>();
public ElectricBox() {
hp = maxHp;
}
this.hp = maxHp;
public ElectricBox(Document document) {
super(document);
hp = (int) document.get("hp");
}
/**
@ -92,7 +93,6 @@ public class ElectricBox extends GameObject implements Updatable, Attackable {
//YOU ARE DEAD
if (hp <= 0) {
setDead(true);
LogManager.LOGGER.severe("BOX DEAD");
}
}
@ -136,13 +136,9 @@ public class ElectricBox extends GameObject implements Updatable, Attackable {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
public JSONObject jsonSerialise() {
JSONObject json = super.jsonSerialise();
json.put("i", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("t", ID);
json.put("hp", hp);
return json;
@ -150,28 +146,13 @@ public class ElectricBox extends GameObject implements Updatable, Attackable {
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
dbObject.put("hp", getHp());
return dbObject;
}
public static ElectricBox deserialize(Document obj) {
ElectricBox electricBox = new ElectricBox();
electricBox.setHp((int) obj.get("hp"));
electricBox.setObjectId((long) obj.get("i"));
electricBox.setX((int) obj.get("x"));
electricBox.setY((int) obj.get("y"));
return electricBox;
}
@Override
public boolean onDeadCallback() {
getWorld().decUpdatable();

View File

@ -1,10 +1,9 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.awt.*;
import java.util.ArrayList;
@ -16,7 +15,6 @@ import java.util.List;
public class Factory extends GameObject implements Updatable {
private static final int MAP_INFO = 0x0200;
static final int ID = 3;
/**
* Maximum number of NonPlayerCharacters assigned to this Factory
@ -49,6 +47,19 @@ public class Factory extends GameObject implements Updatable {
*/
private boolean initialised = false;
public Factory() {
}
public Factory(Document document) {
super(document);
setObjectId((long) document.get("i"));
setX((int) document.get("x"));
setY((int) document.get("y"));
tmpNpcArray = ((ArrayList) document.get("tmpNpcArray")).toArray();
}
@Override
public char getMapInfo() {
return MAP_INFO;
@ -118,27 +129,9 @@ public class Factory extends GameObject implements Updatable {
return (x == getX() + 1 || x == getX()) && (y == getY() + 1 || y == getY());
}
@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;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
Document dbObject = super.mongoSerialise();
List<Long> tmpNpcArray = new ArrayList<>(npcs.size());
@ -151,18 +144,6 @@ public class Factory extends GameObject implements Updatable {
return dbObject;
}
public static Factory deserialise(Document obj) {
Factory factory = new Factory();
factory.setObjectId((long) obj.get("i"));
factory.setX((int) obj.get("x"));
factory.setY((int) obj.get("y"));
factory.tmpNpcArray = ((ArrayList) obj.get("tmpNpcArray")).toArray();
return factory;
}
/**
* Get the first non-blocked tile that is directly adjacent to the factory, starting from the north-east corner
* going clockwise.

View File

@ -2,9 +2,9 @@ package net.simon987.npcplugin;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import net.simon987.server.game.objects.Direction;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.InventoryHolder;
import net.simon987.server.logging.LogManager;
import java.util.ArrayList;
@ -105,8 +105,5 @@ public class HarvestTask extends NPCTask {
} else {
pause--;
}
}
}

View File

@ -2,18 +2,13 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.server.game.Direction;
import net.simon987.server.game.objects.Direction;
import org.bson.Document;
import org.json.simple.JSONObject;
public class HarvesterNPC extends NonPlayerCharacter {
public static final int ID = 10;
/**
*
*/
public static final int MAX_HEALTH = GameServer.INSTANCE.getConfig().getInt("harvester_hp_max");
public static final int HEAL_RATE = GameServer.INSTANCE.getConfig().getInt("harvester_regen");
@ -26,6 +21,12 @@ public class HarvesterNPC extends NonPlayerCharacter {
setHealRate(HEAL_RATE);
}
public HarvesterNPC(Document document) {
super(document);
setDirection(Direction.getDirection(document.getInteger("direction")));
}
@Override
public void update() {
@ -56,53 +57,30 @@ public class HarvesterNPC extends NonPlayerCharacter {
getFactory().getNpcs().remove(this);
}
GameServer.INSTANCE.getEventDispatcher().dispatch(
new ObjectDeathEvent(this, ID));
GameServer.INSTANCE.getEventDispatcher().dispatch(new ObjectDeathEvent(this));
return false;
}
@Override
public JSONObject serialise() {
JSONObject json = super.serialise();
public JSONObject jsonSerialise() {
JSONObject json = super.jsonSerialise();
json.put("i", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("direction", getDirection().ordinal());
json.put("hp", getHp());
json.put("energy", energy);
json.put("action", getAction().ordinal());
json.put("t", ID);
return json;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("direction", getDirection().ordinal());
dbObject.put("hp", getHp());
dbObject.put("action", getAction().ordinal());
dbObject.put("t", ID);
return dbObject;
}
public static HarvesterNPC deserialize(Document obj) {
HarvesterNPC npc = new HarvesterNPC();
npc.setObjectId((long) obj.get("i"));
npc.setX((int) obj.get("x"));
npc.setY((int) obj.get("y"));
npc.setHp((int) obj.get("hp"));
npc.setDirection(Direction.getDirection((int) obj.get("direction")));
return npc;
}
}

View File

@ -2,10 +2,11 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.*;
import net.simon987.server.game.objects.*;
import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import java.util.ArrayList;
@ -30,11 +31,6 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
public static final int HP_MAX_DEFAULT = 100;
public static final int HP_REGEN_RATE_DEFAULT = 0;
/**
* Currently unused
*/
int energy;
/**
* Current task
*/
@ -76,6 +72,17 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
*/
private int maxHp = HP_MAX_DEFAULT;
public NonPlayerCharacter() {
}
public NonPlayerCharacter(Document document) {
super(document);
hp = document.getInteger("hp");
setDirection(Direction.getDirection(document.getInteger("direction")));
}
@Override
public char getMapInfo() {
return MAP_INFO;

View File

@ -4,17 +4,13 @@ import net.simon987.npcplugin.event.CpuInitialisationListener;
import net.simon987.npcplugin.event.VaultWorldUpdateListener;
import net.simon987.npcplugin.event.WorldCreationListener;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
import java.util.ArrayList;
public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer {
public class NpcPlugin extends ServerPlugin {
/**
* Radio tower cache
@ -22,55 +18,28 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C
private static ArrayList<RadioTower> radioTowers;
@Override
public void init(ServerConfiguration configuration) {
public void init(ServerConfiguration configuration, GameRegistry registry) {
listeners.add(new WorldCreationListener());
listeners.add(new CpuInitialisationListener());
listeners.add(new VaultWorldUpdateListener(configuration));
registry.registerGameObject(HarvesterNPC.class);
registry.registerGameObject(Factory.class);
registry.registerGameObject(RadioTower.class);
registry.registerGameObject(VaultDoor.class);
registry.registerGameObject(Obstacle.class);
registry.registerGameObject(ElectricBox.class);
registry.registerGameObject(Portal.class);
registry.registerGameObject(VaultExitPortal.class);
registry.registerHardware(RadioReceiverHardware.class);
radioTowers = new ArrayList<>(32);
LogManager.LOGGER.info("Initialised NPC plugin");
}
@Override
public GameObject deserializeObject(Document obj) {
int objType = (int) obj.get("t");
if (objType == HarvesterNPC.ID) {
return HarvesterNPC.deserialize(obj);
} else if (objType == Factory.ID) {
return Factory.deserialise(obj);
} else if (objType == RadioTower.ID) {
return RadioTower.deserialize(obj);
} else if (objType == VaultDoor.ID) {
return VaultDoor.deserialize(obj);
} else if (objType == Obstacle.ID) {
return Obstacle.deserialize(obj);
} else if (objType == ElectricBox.ID) {
return ElectricBox.deserialize(obj);
} else if (objType == Portal.ID) {
return Portal.deserialize(obj);
} else if (objType == VaultExitPortal.ID) {
return VaultExitPortal.deserialize(obj);
}
return null;
}
@Override
public CpuHardware deserializeHardware(Document obj) {
int hwid = (int) obj.get("hwid");
switch (hwid) {
case RadioReceiverHardware.HWID:
return RadioReceiverHardware.deserialize(obj);
}
return null;
}
public static ArrayList<RadioTower> getRadioTowers() {
return radioTowers;
}

View File

@ -1,7 +1,7 @@
package net.simon987.npcplugin;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
import org.bson.Document;
import org.json.simple.JSONObject;
@ -10,7 +10,6 @@ import org.json.simple.JSONObject;
*/
public class Obstacle extends GameObject implements Attackable {
public static final int ID = 6;
public static final int MAP_INFO = 0x0400;
/**
@ -33,6 +32,10 @@ public class Obstacle extends GameObject implements Attackable {
this.maxHp = hp;
}
public Obstacle(Document document) {
style = document.getInteger("style");
}
@Override
public void setHealRate(int hp) {
//No op
@ -89,12 +92,8 @@ public class Obstacle extends GameObject implements Attackable {
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
dbObject.put("hp", hp);
dbObject.put("style", style);
@ -102,27 +101,12 @@ public class Obstacle extends GameObject implements Attackable {
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
public JSONObject jsonSerialise() {
JSONObject json = super.jsonSerialise();
json.put("i", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("t", ID);
json.put("hp", hp);
json.put("style", style);
return json;
}
public static Obstacle deserialize(Document obj) {
Obstacle obstacle = new Obstacle((int) obj.get("hp"));
obstacle.setObjectId((long) obj.get("i"));
obstacle.setX((int) obj.get("x"));
obstacle.setY((int) obj.get("y"));
obstacle.setStyle((int) obj.get("style"));
return obstacle;
}
}

View File

@ -1,9 +1,13 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.*;
import net.simon987.server.game.objects.Enterable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.World;
import org.bson.Document;
import org.json.simple.JSONObject;
public class Portal extends GameObject implements Enterable {
@ -14,7 +18,22 @@ public class Portal extends GameObject implements Enterable {
public static final int MAP_INFO = 0x0020;
public static final int ID = 8;
public Portal() {
}
public Portal(Document document) {
super(document);
destination = new Location(
document.getInteger("dstWorldX"),
document.getInteger("dstWorldY"),
document.getString("dstDimension"),
document.getInteger("dstX"),
document.getInteger("dstY"));
setX(document.getInteger("x"));
setY(document.getInteger("y"));
}
/**
* Called when an object attempts to walk directly into a Enterable object
@ -48,12 +67,8 @@ public class Portal extends GameObject implements Enterable {
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
dbObject.put("dstWorldX", destination.worldX);
dbObject.put("dstWorldY", destination.worldY);
dbObject.put("dstX", destination.x);
@ -63,34 +78,6 @@ public class Portal extends GameObject implements Enterable {
return dbObject;
}
public static Portal deserialize(Document obj) {
Portal portal = new Portal();
portal.destination = new Location(
(int) obj.get("dstWorldX"),
(int) obj.get("dstWorldY"),
(String) obj.get("dstDimension"),
(int) obj.get("dstX"),
(int) obj.get("dstY"));
portal.setX((int) obj.get("x"));
portal.setY((int) obj.get("y"));
return portal;
}
@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 Location getDestination() {
return destination;
}

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.Action;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document;
import java.util.ArrayList;
@ -24,6 +24,12 @@ public class RadioReceiverHardware extends CpuHardware {
this.cubot = cubot;
}
public RadioReceiverHardware(Document document) {
super(document);
this.cubot = (ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot"));
}
@Override
public void handleInterrupt(Status status) {
int x = getCpu().getRegisterSet().getRegister("X").getValue();
@ -70,13 +76,9 @@ public class RadioReceiverHardware extends CpuHardware {
Document dbObject = new Document();
dbObject.put("hwid", (int) HWID);
dbObject.put("type", getClass().getCanonicalName());
dbObject.put("cubot", cubot.getObjectId());
return dbObject;
}
public static RadioReceiverHardware deserialize(Document obj) {
return new RadioReceiverHardware((ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) obj.get("cubot")));
}
}

View File

@ -1,10 +1,9 @@
package net.simon987.npcplugin;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Programmable;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable;
import net.simon987.server.game.objects.Updatable;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.util.ArrayList;
@ -12,12 +11,19 @@ public class RadioTower extends GameObject implements Programmable, Updatable {
private static final int MAP_INFO = 0x1000;
public static final int ID = 4;
public static final int MAX_RANGE = 3; //todo load from config
private static final int MAX_MESSAGES = 16;
public RadioTower() {
}
public RadioTower(Document document) {
super(document);
NpcPlugin.getRadioTowers().add(this);
}
@Override
public char getMapInfo() {
return MAP_INFO;
@ -50,46 +56,7 @@ public class RadioTower extends GameObject implements Programmable, Updatable {
}
}
@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;
}
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
return dbObject;
}
public static RadioTower deserialize(Document obj) {
RadioTower tower = new RadioTower();
tower.setObjectId((long) obj.get("i"));
tower.setX((int) obj.get("x"));
tower.setY((int) obj.get("y"));
NpcPlugin.getRadioTowers().add(tower);
return tower;
}
public ArrayList<char[]> getMessages() {
ArrayList<char[]> getMessages() {
return lastMessages;
}

View File

@ -1,10 +1,10 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.Direction;
import net.simon987.server.game.Location;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.Direction;
import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import java.awt.*;
@ -250,5 +250,4 @@ public class VaultDimension {
}
}
}

View File

@ -2,10 +2,13 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.crypto.RandomStringGenerator;
import net.simon987.server.game.*;
import net.simon987.server.game.objects.Enterable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import org.json.simple.JSONObject;
import java.util.Arrays;
@ -14,7 +17,6 @@ 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
*/
@ -48,6 +50,21 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
this.password = "12345678".toCharArray();
}
public VaultDoor(Document document) {
super(document);
setX(document.getInteger("x"));
setY(document.getInteger("y"));
if (document.containsKey("homeX") && document.containsKey("homeY")) {
homeX = document.getInteger("homeX");
homeY = document.getInteger("homeY");
}
password = document.getString("password").toCharArray();
}
@Override
public void update() {
@ -124,51 +141,15 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("homeX", getHomeX());
dbObject.put("homeY", getHomeY());
dbObject.put("t", ID);
dbObject.put("pw", new String(password));
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);
//Don't send the password to the client!
return json;
}
public static VaultDoor deserialize(Document obj) {
VaultDoor vaultDoor = new VaultDoor(0); //cypherId ?
vaultDoor.setX((int) obj.get("x"));
vaultDoor.setY((int) obj.get("y"));
vaultDoor.setObjectId((long) obj.get("i"));
if (obj.containsKey("homeX") && obj.containsKey("homeY")) {
vaultDoor.setHomeX((int) obj.get("homeX"));
vaultDoor.setHomeY((int) obj.get("homeY"));
}
vaultDoor.password = ((String) obj.get("pw")).toCharArray();
return vaultDoor;
}
@Override
public void initialize() {
//Get or generate vault world

View File

@ -1,8 +1,8 @@
package net.simon987.npcplugin;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Location;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.Location;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
@ -11,23 +11,21 @@ import org.bson.Document;
*/
public class VaultExitPortal extends Portal {
public static final int ID = 9;
public VaultExitPortal() {
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
}
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
dbObject.put("dstWorldX", getDestination().worldX);
dbObject.put("dstWorldY", getDestination().worldY);
dbObject.put("dstX", getDestination().x);
dbObject.put("dstY", getDestination().y);
dbObject.put("dstDimension", getDestination().dimension);
public VaultExitPortal(Document document) {
super(document);
return dbObject;
setDestination(new Location(
document.getInteger("dstWorldX"),
document.getInteger("dstWorldY"),
document.getString("dstDimension"),
document.getInteger("dstX"),
document.getInteger("dstY")));
setX(document.getInteger("x"));
setY(document.getInteger("y"));
}
@Override
@ -42,20 +40,4 @@ public class VaultExitPortal extends Portal {
return super.enter(object);
}
public static Portal deserialize(Document obj) {
VaultExitPortal portal = new VaultExitPortal();
portal.setDestination(new Location(
(int) obj.get("dstWorldX"),
(int) obj.get("dstWorldY"),
(String) obj.get("dstDimension"),
(int) obj.get("dstX"),
(int) obj.get("dstY")));
portal.setX((int) obj.get("x"));
portal.setY((int) obj.get("y"));
return portal;
}
}

View File

@ -1,8 +1,8 @@
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.game.objects.Direction;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import java.awt.*;

View File

@ -1,8 +1,8 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import java.awt.*;
import java.util.ArrayList;

View File

@ -7,7 +7,7 @@ import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -8,7 +8,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldGenerationEvent;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import java.awt.*;

View File

@ -1,14 +1,13 @@
package net.simon987.biomassplugin;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.InventoryHolder;
import org.bson.Document;
import org.json.simple.JSONObject;
public class BiomassBlob extends GameObject implements InventoryHolder {
private static final char MAP_INFO = 0x4000;
public static final int ID = 2;
/**
* Yield of the blob, in biomass units
@ -21,22 +20,26 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
private static final int ITM_BIOMASS = 1;
public BiomassBlob() {
}
public BiomassBlob(Document document) {
super(document);
biomassCount = document.getInteger("b");
}
@Override
public char getMapInfo() {
return MAP_INFO;
}
@Override
public JSONObject serialise() {
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
JSONObject json = super.jsonSerialise();
json.put("t", ID);
json.put("i", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("b", biomassCount);
// json.put("style", style);
return json;
}
@ -44,12 +47,8 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
@Override
public Document mongoSerialise() {
Document dbObject = new Document();
Document dbObject = super.mongoSerialise();
dbObject.put("t", ID);
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("b", biomassCount);
return dbObject;
@ -64,20 +63,6 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
this.biomassCount = biomassCount;
}
public static BiomassBlob deserialize(Document obj) {
BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId((long) obj.get("i"));
biomassBlob.setX((int) obj.get("x"));
biomassBlob.setY((int) obj.get("y"));
// biomassBlob.style = (int) json.get("style");
biomassBlob.biomassCount = (int) obj.get("b");
return biomassBlob;
}
/**
* Called when an object attempts to place an item in this BiomassBlob
*

View File

@ -4,35 +4,28 @@ import net.simon987.biomassplugin.event.ObjectDeathListener;
import net.simon987.biomassplugin.event.WorldCreationListener;
import net.simon987.biomassplugin.event.WorldUpdateListener;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public class BiomassPlugin extends ServerPlugin implements GameObjectDeserializer {
public class BiomassPlugin extends ServerPlugin {
@Override
public void init(ServerConfiguration config) {
public void init(ServerConfiguration config, GameRegistry registry) {
listeners.add(new WorldCreationListener());
listeners.add(new WorldUpdateListener(config));
listeners.add(new ObjectDeathListener(config));
LogManager.LOGGER.info("Initialised Biomass plugin");
}
@Override
public GameObject deserializeObject(Document object) {
int objType = (int) object.get("t");
if (objType == BiomassBlob.ID) {
return BiomassBlob.deserialize(object);
if (registry.isGameObjectRegistered("net.simon987.npcplugin.HarvesterNPC")) {
listeners.add(new ObjectDeathListener(config));
} else {
LogManager.LOGGER.severe("(BiomassPlugin) NPC plugin is not loaded so biomass will not spawn on death of HarvesterNPC");
}
return null;
registry.registerGameObject(BiomassBlob.class);
LogManager.LOGGER.info("(BiomassPlugin) Initialised Biomass plugin");
}
}

View File

@ -1,8 +1,8 @@
package net.simon987.biomassplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import java.awt.*;

View File

@ -6,8 +6,8 @@ import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.ObjectDeathEvent;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
/**
@ -29,12 +29,11 @@ public class ObjectDeathListener implements GameEventListener {
@Override
public void handle(GameEvent event) {
// TODO: setup enum with all GameObject type IDs
if (((ObjectDeathEvent) event).getSourceObjectId() == 10) {
if (event.getSource().getClass().getCanonicalName().equals("net.simon987.npcplugin.HarvesterNPC")) {
//An HarvesterNPC ObjectDeathEvent is received
GameObject dyingHarvesterNPC = (GameObject)event.getSource();
//Don't spawn biomass on World border
if (dyingHarvesterNPC.getX() != 0 && dyingHarvesterNPC.getX() != dyingHarvesterNPC.getWorld().getWorldSize() - 1 &&
dyingHarvesterNPC.getY() != 0 && dyingHarvesterNPC.getY() != dyingHarvesterNPC.getWorld().getWorldSize() - 1) {

View File

@ -7,7 +7,7 @@ import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.HashMap;

View File

@ -1,23 +1,15 @@
package net.simon987.pluginradioactivecloud;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public class RadioactiveCloudPlugin extends ServerPlugin implements GameObjectDeserializer {
public class RadioactiveCloudPlugin extends ServerPlugin {
@Override
public void init(ServerConfiguration config) {
public void init(ServerConfiguration config, GameRegistry registry) {
LogManager.LOGGER.info("Initialised Radioactive cloud plugin.");
}
@Override
public GameObject deserializeObject(Document object) {
return null;
}
}

View File

@ -5,15 +5,16 @@ import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.ReplaceOptions;
import net.simon987.server.crypto.CryptoProvider;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher;
import net.simon987.server.event.TickEvent;
import net.simon987.server.game.DayNightCycle;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.World;
import net.simon987.server.game.debug.*;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.game.world.DayNightCycle;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.PluginManager;
import net.simon987.server.user.User;
@ -43,14 +44,15 @@ public class GameServer implements Runnable {
private CryptoProvider cryptoProvider;
private MongoClient mongo = null;
private MongoClient mongo;
private UserManager userManager;
private UserStatsHelper userStatsHelper;
public GameServer() {
private GameRegistry gameRegistry;
public GameServer() {
this.config = new ServerConfiguration("config.properties");
mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port"));
@ -64,6 +66,7 @@ public class GameServer implements Runnable {
gameUniverse = new GameUniverse(config);
gameUniverse.setMongo(mongo);
pluginManager = new PluginManager();
gameRegistry = new GameRegistry();
maxExecutionTime = config.getInt("user_timeout");
@ -79,7 +82,7 @@ public class GameServer implements Runnable {
for (File pluginFile : pluginDirListing) {
if (pluginFile.getName().endsWith(".jar")) {
pluginManager.load(pluginFile, config);
pluginManager.load(pluginFile, config, gameRegistry);
}
}
} else {
@ -238,7 +241,7 @@ public class GameServer implements Runnable {
LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount());
try{
MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname"));
UpdateOptions updateOptions = new UpdateOptions();
ReplaceOptions updateOptions = new ReplaceOptions();
updateOptions.upsert(true);
int unloaded_worlds = 0;
@ -260,7 +263,6 @@ public class GameServer implements Runnable {
}
}
for (User u : GameServer.INSTANCE.getGameUniverse().getUsers()) {
if (!u.isGuest()) {
users.replaceOne(new Document("_id", u.getUsername()), u.mongoSerialise(), updateOptions);
@ -303,4 +305,8 @@ public class GameServer implements Runnable {
public UserStatsHelper getUserStatsHelper() {
return userStatsHelper;
}
public GameRegistry getRegistry() {
return gameRegistry;
}
}

View File

@ -381,12 +381,12 @@ public class CPU implements MongoSerializable {
ArrayList hardwareList = (ArrayList) obj.get("hardware");
for (Object serialisedHw : hardwareList) {
CpuHardware hardware = CpuHardware.deserialize((Document) serialisedHw);
CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw);
hardware.setCpu(cpu);
cpu.attachHardware(hardware, (int) ((Document) serialisedHw).get("address"));
}
cpu.memory = Memory.deserialize((Document) obj.get("memory"));
cpu.memory = new Memory((Document) obj.get("memory"));
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
return cpu;

View File

@ -1,15 +1,21 @@
package net.simon987.server.assembly;
import net.simon987.server.GameServer;
import net.simon987.server.io.CpuHardwareDeserializer;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
public abstract class CpuHardware implements MongoSerializable {
CPU cpu;
private CPU cpu;
public CpuHardware() {
}
public CpuHardware(Document document) {
}
/**
* Handle an HWI instruction
@ -26,22 +32,6 @@ public abstract class CpuHardware implements MongoSerializable {
public abstract char getId();
public static CpuHardware deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if (plugin instanceof CpuHardwareDeserializer) {
CpuHardware hw = ((CpuHardwareDeserializer) plugin).deserializeHardware(obj);
if (hw != null) {
return hw;
}
}
}
return null;
}
@Override
public String toString() {
return String.format("<%04X>", (int) getId());

View File

@ -38,6 +38,31 @@ public class Memory implements Target, MongoSerializable {
words = new char[size];
}
public Memory(Document document) {
String zipBytesStr = (String) document.get("zipBytes");
if (zipBytesStr != null) {
byte[] compressedBytes = Base64.getDecoder().decode((String) document.get("zipBytes"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater(true);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
inflaterOutputStream.write(compressedBytes);
inflaterOutputStream.close();
setBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} else {
LogManager.LOGGER.severe("Memory was manually deleted");
words = new char[GameServer.INSTANCE.getConfig().getInt("memory_size")];
}
}
/**
* Get the value at an address
*
@ -153,35 +178,6 @@ public class Memory implements Target, MongoSerializable {
return dbObject;
}
public static Memory deserialize(Document obj) {
Memory memory = new Memory(0);
String zipBytesStr = (String) obj.get("zipBytes");
if (zipBytesStr != null) {
byte[] compressedBytes = Base64.getDecoder().decode((String) obj.get("zipBytes"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Inflater decompressor = new Inflater(true);
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
inflaterOutputStream.write(compressedBytes);
inflaterOutputStream.close();
memory.setBytes(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
} else {
LogManager.LOGGER.severe("Memory was manually deleted");
memory = new Memory(GameServer.INSTANCE.getConfig().getInt("memory_size"));
}
return memory;
}
public void setBytes(byte[] bytes) {
this.words = new char[bytes.length / 2];
ByteBuffer.wrap(bytes).order(ByteOrder.BIG_ENDIAN).asCharBuffer().get(this.words);

View File

@ -4,17 +4,8 @@ package net.simon987.server.event;
* Event dispatched by a GameObject who has needed callbacks on death
*/
public class ObjectDeathEvent extends GameEvent {
/**
* The GameObject type ID of object that initialize this event
*/
private long sourceObjectId;
public ObjectDeathEvent(Object source, int sourceObjectId) {
public ObjectDeathEvent(Object source) {
setSource(source);
this.sourceObjectId = sourceObjectId;
}
public long getSourceObjectId() {
return sourceObjectId;
}
}

View File

@ -1,6 +1,6 @@
package net.simon987.server.event;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class WorldGenerationEvent extends GameEvent {

View File

@ -1,6 +1,6 @@
package net.simon987.server.event;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class WorldUpdateEvent extends GameEvent {

View File

@ -10,6 +10,9 @@ import net.simon987.server.assembly.Assembler;
import net.simon987.server.assembly.AssemblyResult;
import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.game.world.WorldGenerator;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.bson.Document;
@ -115,7 +118,7 @@ public class GameUniverse {
}
}
World getLoadedWorld(int x, int y, String dimension) {
public World getLoadedWorld(int x, int y, String dimension) {
// Wrapping coordinates around cyclically
x %= maxWidth;
y %= maxWidth;

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Programmable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable;
public class ComPortMsgCommandListener implements GameEventListener {

View File

@ -4,7 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class CreateWorldCommandListener implements GameEventListener {

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
public class DamageObjCommandListener implements GameEventListener {

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.Attackable;
import net.simon987.server.game.objects.GameObject;
public class HealObjCommandListener implements GameEventListener {

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.Arrays;

View File

@ -4,7 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.GameObject;
public class MoveObjCommandListener implements GameEventListener {

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import java.util.Arrays;
import java.util.Collection;
@ -37,7 +37,7 @@ public class ObjInfoCommandListener implements GameEventListener {
if (obj.isAt(e.getInt("x"), e.getInt("y")) || (obj.getX() == e.getInt("x") &&
obj.getY() == e.getInt("y"))) {
str += "Mongo:" + obj.mongoSerialise() + "\n";
str += "JSON :" + obj.serialise().toJSONString() + "\n\n";
str += "JSON :" + obj.jsonSerialise().toJSONString() + "\n\n";
}
}

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Rechargeable;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Rechargeable;
public class SetEnergyCommandListener implements GameEventListener {

View File

@ -4,7 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
public class SetTileAtCommandListener implements GameEventListener {

View File

@ -4,8 +4,8 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import org.bson.Document;
import java.util.Arrays;
@ -30,7 +30,7 @@ public class SpawnObjCommandListener implements GameEventListener {
Document dbObj = Document.parse(e.getString("data"));
dbObj.put("i", GameServer.INSTANCE.getGameUniverse().getNextObjectId());
GameObject object = GameObject.deserialize(dbObj);
GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject(dbObj);
if (object != null) {
world.addObject(object);

View File

@ -4,9 +4,9 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.world.World;
public class TpObjectCommandListener implements GameEventListener {

View File

@ -4,7 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.event.DebugCommandEvent;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.user.User;
public class UserInfoCommandListener implements GameEventListener {

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public enum Action {
IDLE,

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Objects that can be attacked or healed
@ -8,12 +8,15 @@ public interface Attackable {
void setHealRate(int hp);
int getHp();
void setHp(int hp);
int getMaxHp();
void setMaxHp(int hp);
void heal(int amount);
void damage(int amount);
}

View File

@ -1,6 +1,7 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
import net.simon987.server.assembly.Memory;
import net.simon987.server.game.world.World;
import net.simon987.server.user.User;
import java.util.ArrayList;

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Direction of a game object in a 4-direction grid-based

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Enterable {

View File

@ -1,10 +1,9 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
import net.simon987.server.GameServer;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.game.world.World;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerializable;
import net.simon987.server.plugin.ServerPlugin;
import org.bson.Document;
import org.json.simple.JSONObject;
@ -12,7 +11,7 @@ import java.awt.*;
import java.util.ArrayList;
/**
* An INSTANCE of an object (e.g. a Tree, a character ...) inside the
* An instance of an object (e.g. a Cubot, a NPC...) inside the
* game universe
*/
public abstract class GameObject implements JSONSerialisable, MongoSerializable {
@ -43,6 +42,15 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
*/
private World world;
public GameObject() {
}
public GameObject(Document document) {
objectId = document.getLong("id");
x = document.getInteger("x");
y = document.getInteger("y");
}
/**
* Increment the location of the game object by 1 tile
@ -101,9 +109,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
} else {
return false;
}
}
}
public abstract char getMapInfo();
@ -119,7 +125,6 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
} else {
return new Point(x - 1, y);
}
}
/**
@ -219,24 +224,13 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
}
@Override
public JSONObject serialise() {
return new JSONObject();
}
public static GameObject deserialize(Document obj) {
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if (plugin instanceof GameObjectDeserializer) {
GameObject object = ((GameObjectDeserializer) plugin).deserializeObject(obj);
if (object != null) {
return object;
}
}
}
return null;
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
json.put("i", getObjectId());
json.put("t", getClass().getCanonicalName());
json.put("x", getX());
json.put("y", getY());
return json;
}
@ -254,6 +248,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
/**
* Called before this GameObject is removed from the world - defaults to doing nothing
*
* @return true if cancelled
*/
public boolean onDeadCallback() {
@ -263,4 +258,17 @@ public abstract class GameObject implements JSONSerialisable, MongoSerializable
public void initialize() {
}
@Override
public Document mongoSerialise() {
Document document = new Document();
document.put("id", getObjectId());
document.put("type", getClass().getCanonicalName());
document.put("x", getX());
document.put("y", getY());
return document;
}
}

View File

@ -0,0 +1,70 @@
package net.simon987.server.game.objects;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.logging.LogManager;
import org.bson.Document;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
public class GameRegistry {
private HashMap<String, Class<? extends GameObject>> gameObjects;
private HashMap<String, Class<? extends CpuHardware>> hardware;
public GameRegistry() {
gameObjects = new HashMap<>();
hardware = new HashMap<>();
}
public void registerGameObject(Class<? extends GameObject> clazz) {
gameObjects.put(clazz.getCanonicalName(), clazz);
}
public void registerHardware(Class<? extends CpuHardware> clazz) {
hardware.put(clazz.getCanonicalName(), clazz);
}
public CpuHardware deserializeHardware(Document document) {
String type = document.getString("type");
if (hardware.containsKey(type)) {
try {
return hardware.get(type).getConstructor(Document.class).newInstance(document);
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
return null;
}
} else {
LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type);
return null;
}
}
public GameObject deserializeGameObject(Document document) {
String type = document.getString("type");
if (gameObjects.containsKey(type)) {
try {
return gameObjects.get(type).getConstructor(Document.class).newInstance(document);
} catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
return null;
}
} else {
LogManager.LOGGER.severe("Trying to deserialize unknown GameObject type: " + type);
return null;
}
}
public boolean isGameObjectRegistered(String type) {
return gameObjects.containsKey(type);
}
}

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface InventoryHolder {

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Programmable {

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
//Alpha: ±5cm
//Beta: 10-20 feet

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
public interface Rechargeable {

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.objects;
/**
* Updatable objects needs to be updated each tick

View File

@ -1,7 +1,7 @@
package net.simon987.server.game.pathfinding;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
/**
* Represents a location in the game universe

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.io.JSONSerialisable;
@ -110,7 +110,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
}
@Override
public JSONObject serialise() {
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();
byte[] terrain = new byte[width * width];
@ -164,7 +164,6 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
ArrayList<Integer> terrain = (ArrayList<Integer>) object.get("tiles");
int[][] tiles = new int[size][size];
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
tiles[x][y] = terrain.get(x * size + y);

View File

@ -1,8 +1,11 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Updatable;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;
@ -221,11 +224,11 @@ public class World implements MongoSerializable {
world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize());
ArrayList<Document> objects = (ArrayList<Document>) dbObject.get("objects");
ArrayList objects = (ArrayList) dbObject.get("objects");
for (Object obj : objects) {
GameObject object = GameObject.deserialize((Document) obj);
GameObject object = GameServer.INSTANCE.getRegistry().deserializeGameObject((Document) obj);
object.setWorld(world);
world.addObject(object);

View File

@ -1,4 +1,4 @@
package net.simon987.server.game;
package net.simon987.server.game.world;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
@ -43,7 +43,7 @@ public class WorldGenerator {
private HashMap<Point, Integer> centerPointsMap;
WorldGenerator(ServerConfiguration config) {
public WorldGenerator(ServerConfiguration config) {
centerPointCountMin = config.getInt("wg_centerPointCountMin");
centerPointCountMax = config.getInt("wg_centerPointCountMax");

View File

@ -1,10 +0,0 @@
package net.simon987.server.io;
import net.simon987.server.assembly.CpuHardware;
import org.bson.Document;
public interface CpuHardwareDeserializer {
CpuHardware deserializeHardware(Document hwJson);
}

View File

@ -1,45 +0,0 @@
package net.simon987.server.io;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.logging.LogManager;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* Manages the database: this class manages the interactions with the database
*/
public abstract class DatabaseManager {
/**
* SQL connection url
*/
private String url;
/**
* SQL username
*/
private String username;
/**
* SQL password
*/
private String password;
public DatabaseManager(ServerConfiguration config) {
this.url = config.getString("mysql_url");
this.username = config.getString("mysql_user");
this.password = config.getString("mysql_pass");
}
protected Connection getConnection() {
try {
return DriverManager.getConnection(url, this.username, password);
} catch (SQLException e) {
LogManager.LOGGER.severe("Couldn't connect to MySQL server state:" + e.getSQLState() + " error:" + e.getErrorCode());
return null;
}
}
}

View File

@ -1,22 +0,0 @@
package net.simon987.server.io;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileUtils {
private static final String DATE_FORMAT = "yyyyMMddHHmmss";
/**
* Creates a new stamp containing the current date and time
*
* @return date and time stamp
*/
private static String getDateTimeStamp() {
Date millisToDate = new Date(System.currentTimeMillis());
SimpleDateFormat f = new SimpleDateFormat(DATE_FORMAT);
return f.format(millisToDate);
}
}

View File

@ -1,10 +0,0 @@
package net.simon987.server.io;
import net.simon987.server.game.GameObject;
import org.bson.Document;
public interface GameObjectDeserializer {
GameObject deserializeObject(Document object);
}

View File

@ -4,6 +4,6 @@ import org.json.simple.JSONObject;
public interface JSONSerialisable {
JSONObject serialise();
JSONObject jsonSerialise();
}

View File

@ -1,6 +1,7 @@
package net.simon987.server.plugin;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager;
import java.io.File;
@ -21,7 +22,7 @@ public class PluginManager {
this.plugins = new ArrayList<>(10);
}
public void load(File pluginFile, ServerConfiguration config) {
public void load(File pluginFile, ServerConfiguration config, GameRegistry gameRegistry) {
LogManager.LOGGER.info("Loading plugin file " + pluginFile.getName());
@ -53,7 +54,7 @@ public class PluginManager {
plugins.add(plugin);
//Init the plugin
plugin.init(config);
plugin.init(config, gameRegistry);
} else {
LogManager.LOGGER.severe("Couldn't find plugin.properties in " + pluginFile.getName());

View File

@ -2,6 +2,7 @@ package net.simon987.server.plugin;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
@ -27,7 +28,7 @@ public abstract class ServerPlugin implements JSONSerialisable {
/**
* Called when the plugin is loaded
*/
public abstract void init(ServerConfiguration config);
public abstract void init(ServerConfiguration config, GameRegistry gameRegistry);
public String getName() {
return name;
@ -50,7 +51,7 @@ public abstract class ServerPlugin implements JSONSerialisable {
}
@Override
public JSONObject serialise() {
public JSONObject jsonSerialise() {
JSONObject json = new JSONObject();

View File

@ -5,7 +5,7 @@ import net.simon987.server.assembly.CPU;
import net.simon987.server.assembly.exception.CancelledException;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.UserCreationEvent;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.io.MongoSerializable;
import org.bson.Document;

View File

@ -1,8 +1,8 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.World;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@ -37,7 +37,7 @@ public class ObjectsRequestHandler implements MessageHandler {
for (GameObject object : world.getGameObjects()) {
objects.add(object.serialise());
objects.add(object.jsonSerialise());
}
response.put("t", "object");

View File

@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
import org.eclipse.jetty.websocket.api.Session;

View File

@ -1,109 +0,0 @@
package net.simon987.server.websocket;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.io.DatabaseManager;
import net.simon987.server.logging.LogManager;
import java.sql.*;
class SocketServerDatabase extends DatabaseManager {
public SocketServerDatabase(ServerConfiguration config) {
super(config);
}
String validateAuthToken(String token) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT username FROM mar_user WHERE authToken=?");
p.setString(1, token);
ResultSet rs = p.executeQuery();
if (rs.next()) {
return rs.getString("username");
} else {
return null;
}
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
boolean isModerator(String username) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT moderator FROM mar_user WHERE username=?");
p.setString(1, username);
ResultSet rs = p.executeQuery();
return rs.next() && rs.getBoolean("moderator");
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
byte[] getFloppy(String username) {
Connection connection = null;
try {
connection = getConnection();
PreparedStatement p = connection.prepareStatement("SELECT floppyData FROM mar_user WHERE username=?");
p.setString(1, username);
ResultSet rs = p.executeQuery();
if (rs.next()) {
Blob blob = rs.getBlob("floppyData");
if (blob != null) {
return blob.getBytes(1, (int) blob.length() - 1);
}
}
} catch (SQLException e) {
LogManager.LOGGER.severe("MySQL Error " + e.getErrorCode() + ": " + e.getMessage());
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return null;
}
}

View File

@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.World;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

View File

@ -1,7 +1,7 @@
package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject;

View File

@ -785,15 +785,15 @@ var GameClient = (function () {
}());
var ObjectType;
(function (ObjectType) {
ObjectType[ObjectType["CUBOT"] = 1] = "CUBOT";
ObjectType[ObjectType["BIOMASS"] = 2] = "BIOMASS";
ObjectType[ObjectType["HARVESTER_NPC"] = 10] = "HARVESTER_NPC";
ObjectType[ObjectType["FACTORY"] = 3] = "FACTORY";
ObjectType[ObjectType["RADIO_TOWER"] = 4] = "RADIO_TOWER";
ObjectType[ObjectType["VAULT_DOOR"] = 5] = "VAULT_DOOR";
ObjectType[ObjectType["OBSTACLE"] = 6] = "OBSTACLE";
ObjectType[ObjectType["ELECTRIC_BOX"] = 7] = "ELECTRIC_BOX";
ObjectType[ObjectType["PORTAL"] = 8] = "PORTAL";
ObjectType["CUBOT"] = "net.simon987.cubotplugin.Cubot";
ObjectType["BIOMASS"] = "net.simon987.biomassplugin.BiomassBlob";
ObjectType["HARVESTER_NPC"] = "net.simon987.npcplugin.HarvesterNPC";
ObjectType["FACTORY"] = "net.simon987.npcplugin.Factory";
ObjectType["RADIO_TOWER"] = "net.simon987.npcplugin.RadioTower";
ObjectType["VAULT_DOOR"] = "net.simon987.npcplugin.VaultDoor";
ObjectType["OBSTACLE"] = "net.simon987.npcplugin.Obstacle";
ObjectType["ELECTRIC_BOX"] = "net.simon987.npcplugin.ElectricBox";
ObjectType["PORTAL"] = "net.simon987.npcplugin.Portal";
})(ObjectType || (ObjectType = {}));
var ItemType;
(function (ItemType) {

View File

@ -1,13 +1,13 @@
enum ObjectType {
CUBOT = 1,
BIOMASS = 2,
HARVESTER_NPC = 10,
FACTORY = 3,
RADIO_TOWER = 4,
VAULT_DOOR = 5,
OBSTACLE = 6,
ELECTRIC_BOX = 7,
PORTAL = 8
CUBOT = "net.simon987.cubotplugin.Cubot",
BIOMASS = "net.simon987.biomassplugin.BiomassBlob",
HARVESTER_NPC = "net.simon987.npcplugin.HarvesterNPC",
FACTORY = "net.simon987.npcplugin.Factory",
RADIO_TOWER = "net.simon987.npcplugin.RadioTower",
VAULT_DOOR = "net.simon987.npcplugin.VaultDoor",
OBSTACLE = "net.simon987.npcplugin.Obstacle",
ELECTRIC_BOX = "net.simon987.npcplugin.ElectricBox",
PORTAL = "net.simon987.npcplugin.Portal"
}
enum ItemType {