Cubot object id is no longer stored inside every CpuHardware in the database. Renamed some fields in the database to make them more readable

This commit is contained in:
simon 2018-05-12 16:16:24 -04:00
parent 4cd58c86a5
commit 3368268924
27 changed files with 83 additions and 74 deletions

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotBattery extends CubotHardware { public class CubotBattery extends CubotHardware {
@ -19,8 +20,8 @@ public class CubotBattery extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotBattery(Document document) { public CubotBattery(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override
@ -33,6 +34,8 @@ public class CubotBattery extends CubotHardware {
} else if (a == BATTERY_GET_MAX_CAPACITY) { } else if (a == BATTERY_GET_MAX_CAPACITY) {
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getMaxEnergy()); getCpu().getRegisterSet().getRegister("B").setValue(cubot.getMaxEnergy());
//TODO: Remove debug action
} else if (a == 0xFFFF) { } else if (a == 0xFFFF) {
cubot.setEnergy(cubot.getMaxEnergy()); cubot.setEnergy(cubot.getMaxEnergy());
} }

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.objects.GameObject; import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.objects.Programmable; import net.simon987.server.game.objects.Programmable;
import org.bson.Document; import org.bson.Document;
@ -23,8 +24,8 @@ public class CubotComPort extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotComPort(Document document) { public CubotComPort(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
private static final int MESSAGE_LENGTH = 8; private static final int MESSAGE_LENGTH = 8;
@ -117,8 +118,6 @@ public class CubotComPort extends CubotHardware {
getCpu().getRegisterSet().getRegister("B").setValue(0); //Failed getCpu().getRegisterSet().getRegister("B").setValue(0); //Failed
} }
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotCore extends CubotHardware { public class CubotCore extends CubotHardware {
@ -19,8 +20,8 @@ public class CubotCore extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotCore(Document document) { public CubotCore(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.TileMap;
import org.bson.Document; import org.bson.Document;
@ -21,8 +22,8 @@ public class CubotDrill extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotDrill(Document document) { public CubotDrill(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotFloppyDrive extends CubotHardware { public class CubotFloppyDrive extends CubotHardware {
@ -24,8 +25,8 @@ public class CubotFloppyDrive extends CubotHardware {
floppyDisk = new FloppyDisk(); floppyDisk = new FloppyDisk();
} }
public CubotFloppyDrive(Document document) { public CubotFloppyDrive(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
if (document.containsKey("floppy")) { if (document.containsKey("floppy")) {
floppyDisk = new FloppyDisk((Document) document.get("floppy")); floppyDisk = new FloppyDisk((Document) document.get("floppy"));

View File

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

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotHologram extends CubotHardware { public class CubotHologram extends CubotHardware {
@ -25,8 +26,8 @@ public class CubotHologram extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotHologram(Document document) { public CubotHologram(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotInventory extends CubotHardware { public class CubotInventory extends CubotHardware {
@ -20,8 +21,8 @@ public class CubotInventory extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotInventory(Document document) { public CubotInventory(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -1,6 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotKeyboard extends CubotHardware { public class CubotKeyboard extends CubotHardware {
@ -19,8 +20,8 @@ public class CubotKeyboard extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotKeyboard(Document document) { public CubotKeyboard(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -1,10 +1,7 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.*;
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 org.bson.Document;
import java.awt.*; import java.awt.*;
@ -29,8 +26,8 @@ public class CubotLaser extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotLaser(Document document) { public CubotLaser(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.objects.Direction; import net.simon987.server.game.objects.Direction;
import org.bson.Document; import org.bson.Document;
@ -21,8 +22,8 @@ public class CubotLeg extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotLeg(Document document) { public CubotLeg(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.assembly.Memory; import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.pathfinding.Node; import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder; import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
@ -30,8 +31,8 @@ public class CubotLidar extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotLidar(Document document) { public CubotLidar(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
public class CubotShield extends CubotHardware { public class CubotShield extends CubotHardware {
@ -19,8 +20,8 @@ public class CubotShield extends CubotHardware {
super(cubot); super(cubot);
} }
public CubotShield(Document document) { public CubotShield(Document document, ControllableUnit cubot) {
super(document); super(document, cubot);
} }
@Override @Override

View File

@ -29,7 +29,7 @@ public class FloppyDisk implements MongoSerializable {
} }
public FloppyDisk(Document document) { public FloppyDisk(Document document) {
this.rwHeadTrack = (int) document.get("rwHeadTrack"); this.rwHeadTrack = document.getInteger("rwHeadTrack");
this.memory = new Memory((Document) document.get("memory")); this.memory = new Memory((Document) document.get("memory"));
} }

View File

@ -4,6 +4,7 @@ import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Util; import net.simon987.server.assembly.Util;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
/** /**
@ -19,8 +20,8 @@ public class Clock extends CpuHardware {
} }
public Clock(Document document) { public Clock(Document document, ControllableUnit unit) {
super(document); super(document, unit);
} }
@Override @Override

View File

@ -2,6 +2,7 @@ package net.simon987.mischwplugin;
import net.simon987.server.assembly.CpuHardware; import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.objects.ControllableUnit;
import org.bson.Document; import org.bson.Document;
import java.util.Random; import java.util.Random;
@ -21,8 +22,8 @@ public class RandomNumberGenerator extends CpuHardware {
random = new Random(); random = new Random();
} }
public RandomNumberGenerator(Document document) { public RandomNumberGenerator(Document document, ControllableUnit unit) {
super(document); super(document, unit);
random = new Random(); random = new Random();
} }

View File

@ -44,7 +44,7 @@ public class ElectricBox extends GameObject implements Updatable, Attackable {
public ElectricBox(Document document) { public ElectricBox(Document document) {
super(document); super(document);
hp = (int) document.get("hp"); hp = document.getInteger("hp");
} }
/** /**

View File

@ -53,11 +53,7 @@ public class Factory extends GameObject implements Updatable {
public Factory(Document document) { public Factory(Document document) {
super(document); super(document);
setObjectId((long) document.get("i")); tmpNpcArray = ((ArrayList) document.get("npcs")).toArray();
setX((int) document.get("x"));
setY((int) document.get("y"));
tmpNpcArray = ((ArrayList) document.get("tmpNpcArray")).toArray();
} }
@Override @Override
@ -78,13 +74,12 @@ public class Factory extends GameObject implements Updatable {
for (Object id : tmpNpcArray) { for (Object id : tmpNpcArray) {
NonPlayerCharacter npc = (NonPlayerCharacter) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) id); NonPlayerCharacter npc = (NonPlayerCharacter) GameServer.INSTANCE.getGameUniverse().getObject((long) id);
if (npc != null) { if (npc != null) {
npc.setFactory(this); npc.setFactory(this);
npcs.add(npc); npcs.add(npc);
} }
} }
tmpNpcArray = null; tmpNpcArray = null;
@ -139,7 +134,7 @@ public class Factory extends GameObject implements Updatable {
tmpNpcArray.add(npc.getObjectId()); tmpNpcArray.add(npc.getObjectId());
} }
dbObject.put("n", tmpNpcArray); dbObject.put("npcs", tmpNpcArray);
return dbObject; return dbObject;
} }

View File

@ -24,6 +24,8 @@ public class HarvesterNPC extends NonPlayerCharacter {
public HarvesterNPC(Document document) { public HarvesterNPC(Document document) {
super(document); super(document);
setTask(new HarvestTask());
setDirection(Direction.getDirection(document.getInteger("direction"))); setDirection(Direction.getDirection(document.getInteger("direction")));
} }

View File

@ -24,8 +24,8 @@ public class RadioReceiverHardware extends CpuHardware {
this.cubot = cubot; this.cubot = cubot;
} }
public RadioReceiverHardware(Document document) { public RadioReceiverHardware(Document document, ControllableUnit unit) {
super(document); super(document, unit);
this.cubot = (ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot")); this.cubot = (ControllableUnit) GameServer.INSTANCE.getGameUniverse().getObject((long) document.get("cubot"));
} }

View File

@ -145,7 +145,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
dbObject.put("homeX", getHomeX()); dbObject.put("homeX", getHomeX());
dbObject.put("homeY", getHomeY()); dbObject.put("homeY", getHomeY());
dbObject.put("pw", new String(password)); dbObject.put("password", new String(password));
return dbObject; return dbObject;
} }

View File

@ -26,7 +26,7 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
public BiomassBlob(Document document) { public BiomassBlob(Document document) {
super(document); super(document);
biomassCount = document.getInteger("b"); biomassCount = document.getInteger("biomassCount");
} }
@Override @Override
@ -49,7 +49,7 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
Document dbObject = super.mongoSerialise(); Document dbObject = super.mongoSerialise();
dbObject.put("b", biomassCount); dbObject.put("biomassCount", biomassCount);
return dbObject; return dbObject;

View File

@ -376,14 +376,14 @@ public class CPU implements MongoSerializable {
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user); CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user);
cpu.codeSectionOffset = (int) obj.get("codeSegmentOffset"); cpu.codeSectionOffset = obj.getInteger("codeSegmentOffset");
ArrayList hardwareList = (ArrayList) obj.get("hardware"); ArrayList hardwareList = (ArrayList) obj.get("hardware");
for (Object serialisedHw : hardwareList) { for (Object serialisedHw : hardwareList) {
CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw); CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw, user.getControlledUnit());
hardware.setCpu(cpu); hardware.setCpu(cpu);
cpu.attachHardware(hardware, (int) ((Document) serialisedHw).get("address")); cpu.attachHardware(hardware, ((Document) serialisedHw).getInteger("address"));
} }
cpu.memory = new Memory((Document) obj.get("memory")); cpu.memory = new Memory((Document) obj.get("memory"));

View File

@ -1,6 +1,7 @@
package net.simon987.server.assembly; package net.simon987.server.assembly;
import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.io.MongoSerializable; import net.simon987.server.io.MongoSerializable;
import org.bson.Document; import org.bson.Document;
@ -13,7 +14,7 @@ public abstract class CpuHardware implements MongoSerializable {
} }
public CpuHardware(Document document) { public CpuHardware(Document document, ControllableUnit unit) {
} }

View File

@ -171,9 +171,9 @@ public class RegisterSet implements Target, MongoSerializable {
for (Object sRegister : registers) { for (Object sRegister : registers) {
Register register = new Register((String) ((Document) sRegister).get("name")); Register register = new Register((String) ((Document) sRegister).get("name"));
register.setValue((int) ((Document) sRegister).get("value")); register.setValue(((Document) sRegister).getInteger("value"));
registerSet.registers.put((int) ((Document) sRegister).get("index"), register); registerSet.registers.put(((Document) sRegister).getInteger("index"), register);
} }

View File

@ -27,13 +27,13 @@ public class GameRegistry {
} }
public CpuHardware deserializeHardware(Document document) { public CpuHardware deserializeHardware(Document document, ControllableUnit controllableUnit) {
String type = document.getString("type"); String type = document.getString("type");
if (hardware.containsKey(type)) { if (hardware.containsKey(type)) {
try { try {
return hardware.get(type).getConstructor(Document.class).newInstance(document); return hardware.get(type).getConstructor(Document.class, ControllableUnit.class).newInstance(document, controllableUnit);
} catch (InstantiationException | IllegalAccessException | } catch (InstantiationException | IllegalAccessException |
InvocationTargetException | NoSuchMethodException e) { InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
@ -53,10 +53,14 @@ public class GameRegistry {
try { try {
return gameObjects.get(type).getConstructor(Document.class).newInstance(document); return gameObjects.get(type).getConstructor(Document.class).newInstance(document);
} catch (InstantiationException | IllegalAccessException | } catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
return null; return null;
} catch (InvocationTargetException e) {
LogManager.LOGGER.severe("Error while trying to deserialize object of type " + type + ": " + e.getTargetException().getMessage());
LogManager.LOGGER.severe(document.toJson());
e.getTargetException().printStackTrace();
return null;
} }
} else { } else {
LogManager.LOGGER.severe("Trying to deserialize unknown GameObject type: " + type); LogManager.LOGGER.severe("Trying to deserialize unknown GameObject type: " + type);

View File

@ -200,27 +200,27 @@ public class World implements MongoSerializable {
@Override @Override
public String toString() { public String toString() {
String str = "World (" + x + ", " + y + ")\n"; StringBuilder str = new StringBuilder("World (" + x + ", " + y + ")\n");
int[][] tileMap = this.tileMap.getTiles(); int[][] tileMap = this.tileMap.getTiles();
for (int x = 0; x < worldSize; x++) { for (int x = 0; x < worldSize; x++) {
for (int y = 0; y < worldSize; y++) { for (int y = 0; y < worldSize; y++) {
str += tileMap[x][y] + " "; str.append(tileMap[x][y]).append(" ");
} }
str += "\n"; str.append("\n");
} }
return str; return str.toString();
} }
public static World deserialize(Document dbObject) { public static World deserialize(Document dbObject) {
World world = new World((int) dbObject.get("size")); World world = new World(dbObject.getInteger("size"));
world.x = (int) dbObject.get("x"); world.x = dbObject.getInteger("x");
world.y = (int) dbObject.get("y"); world.y = dbObject.getInteger("y");
world.dimension = (String) dbObject.get("dimension"); world.dimension = dbObject.getString("dimension");
world.updatable = (int) dbObject.get("updatable"); world.updatable = dbObject.getInteger("updatable");
world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize()); world.tileMap = TileMap.deserialize((Document) dbObject.get("terrain"), world.getWorldSize());
@ -237,7 +237,6 @@ public class World implements MongoSerializable {
} }
return world; return world;
} }
/** /**
@ -518,6 +517,5 @@ public class World implements MongoSerializable {
} }
} }
} }
} }
} }