From a7bdbd2513e36b7fd798314c0cd515675d3119ab Mon Sep 17 00:00:00 2001 From: Simon Date: Thu, 31 May 2018 22:13:07 -0400 Subject: [PATCH] I broke everything pt. 1: Moved helditem into CubotInventory, added debug commands for CubotInventory, Added vault complete GameEvent, moved CPU to Cubot, renamed CpuHardware to HardwareModule, moved HardwareModule to Cubot & added HardwareHost interface, removed getParent() in ControllableUnit, moved items to the Item class, started moving tiles to Tile classes, renamed Programmable to MessageReceiver --- .../java/net/simon987/cubotplugin/Cubot.java | 148 +++++++++++++++--- .../simon987/cubotplugin/CubotBattery.java | 2 +- .../simon987/cubotplugin/CubotComPort.java | 10 +- .../net/simon987/cubotplugin/CubotCore.java | 2 +- .../net/simon987/cubotplugin/CubotDrill.java | 16 +- .../cubotplugin/CubotFloppyDrive.java | 6 +- ...Hardware.java => CubotHardwareModule.java} | 8 +- .../simon987/cubotplugin/CubotHologram.java | 2 +- .../simon987/cubotplugin/CubotInventory.java | 93 +++++++++-- .../simon987/cubotplugin/CubotKeyboard.java | 2 +- .../net/simon987/cubotplugin/CubotLaser.java | 7 +- .../net/simon987/cubotplugin/CubotLeg.java | 2 +- .../net/simon987/cubotplugin/CubotLidar.java | 2 +- .../net/simon987/cubotplugin/CubotPlugin.java | 8 +- .../net/simon987/cubotplugin/CubotShield.java | 2 +- .../event/CpuInitialisationListener.java | 54 +++---- .../event/PopItemCommandListener.java | 43 +++++ .../event/PutItemCommandListener.java | 53 +++++++ .../event/SetInventoryPosition.java | 45 ++++++ .../event/UserCreationListener.java | 26 ++- .../java/net/simon987/mischwplugin/Clock.java | 4 +- .../mischwplugin/RandomNumberGenerator.java | 4 +- .../event/CpuInitialisationListener.java | 4 +- .../net/simon987/npcplugin/NpcPlugin.java | 2 + .../npcplugin/RadioReceiverHardware.java | 4 +- .../net/simon987/npcplugin/RadioTower.java | 4 +- .../net/simon987/npcplugin/VaultDoor.java | 2 +- .../simon987/npcplugin/VaultExitPortal.java | 8 +- .../event/CpuInitialisationListener.java | 9 +- .../npcplugin/event/VaultCompleteEvent.java | 26 +++ .../event/VaultCompleteListener.java | 27 ++++ .../simon987/biomassplugin/BiomassBlob.java | 21 +-- .../simon987/biomassplugin/BiomassPlugin.java | 2 +- .../simon987/biomassplugin/ItemBiomass.java | 35 +++++ .../java/net/simon987/server/GameServer.java | 11 +- .../net/simon987/server/assembly/CPU.java | 87 ++-------- .../{CpuHardware.java => HardwareModule.java} | 10 +- .../assembly/instruction/HwiInstruction.java | 4 +- .../assembly/instruction/HwqInstruction.java | 4 +- .../server/event/CpuInitialisationEvent.java | 12 +- .../simon987/server/game/GameUniverse.java | 17 -- .../game/debug/ComPortMsgCommandListener.java | 8 +- .../game/debug/UserInfoCommandListener.java | 2 +- .../net/simon987/server/game/item/Item.java | 54 +++++++ .../simon987/server/game/item/ItemCopper.java | 23 +++ .../simon987/server/game/item/ItemIron.java | 22 +++ .../simon987/server/game/item/ItemVoid.java | 21 +++ .../server/game/objects/ControllableUnit.java | 6 + .../server/game/objects/GameRegistry.java | 61 +++++++- .../server/game/objects/HardwareHost.java | 16 ++ .../server/game/objects/InventoryHolder.java | 14 +- ...Programmable.java => MessageReceiver.java} | 2 +- .../net/simon987/server/game/world/Tile.java | 21 +++ .../net/simon987/server/game/world/World.java | 2 +- .../java/net/simon987/server/user/User.java | 15 -- .../net/simon987/server/user/UserManager.java | 1 + .../server/websocket/CodeUploadHandler.java | 16 +- Server/src/main/resources/static/js/mar.js | 16 +- Server/src/main/resources/templates/head.vm | 1 - Server/src/main/typescript/mar.ts | 24 ++- 60 files changed, 858 insertions(+), 295 deletions(-) rename Plugin Cubot/src/main/java/net/simon987/cubotplugin/{CubotHardware.java => CubotHardwareModule.java} (63%) create mode 100644 Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PopItemCommandListener.java create mode 100644 Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PutItemCommandListener.java create mode 100644 Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/SetInventoryPosition.java create mode 100644 Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteEvent.java create mode 100644 Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteListener.java create mode 100644 Plugin Plant/src/main/java/net/simon987/biomassplugin/ItemBiomass.java rename Server/src/main/java/net/simon987/server/assembly/{CpuHardware.java => HardwareModule.java} (65%) create mode 100644 Server/src/main/java/net/simon987/server/game/item/Item.java create mode 100644 Server/src/main/java/net/simon987/server/game/item/ItemCopper.java create mode 100644 Server/src/main/java/net/simon987/server/game/item/ItemIron.java create mode 100644 Server/src/main/java/net/simon987/server/game/item/ItemVoid.java create mode 100644 Server/src/main/java/net/simon987/server/game/objects/HardwareHost.java rename Server/src/main/java/net/simon987/server/game/objects/{Programmable.java => MessageReceiver.java} (71%) create mode 100644 Server/src/main/java/net/simon987/server/game/world/Tile.java diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java index 041ddf3..8cef8e3 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java @@ -2,18 +2,24 @@ package net.simon987.cubotplugin; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; +import net.simon987.server.assembly.CPU; +import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Memory; +import net.simon987.server.assembly.Status; +import net.simon987.server.assembly.exception.CancelledException; +import net.simon987.server.game.item.Item; +import net.simon987.server.game.item.ItemVoid; import net.simon987.server.game.objects.*; -import net.simon987.server.logging.LogManager; import net.simon987.server.user.User; import org.bson.Document; import org.json.simple.JSONObject; import java.awt.*; -import java.util.ArrayList; -import java.util.Random; +import java.util.*; +import java.util.List; -public class Cubot extends GameObject implements Updatable, ControllableUnit, Programmable, Attackable, Rechargeable { +public class Cubot extends GameObject implements Updatable, ControllableUnit, MessageReceiver, + Attackable, Rechargeable, HardwareHost { private static final char MAP_INFO = 0x0080; @@ -62,11 +68,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr */ private int maxShield; - /** - * Item ID of the current 'active' item - */ - private int heldItem; - /** * Action that was set during the current tick. It is set to IDLE by default */ @@ -136,6 +137,17 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr */ private static final int CONSOLE_BUFFER_MAX_SIZE = 40; + /** + * List of attached hardware, 'modules' + */ + private Map hardwareAddresses = new HashMap<>(); + private Map, Integer> hardwareModules = new HashMap<>(); + + /** + * Cubot's brain box + */ + private CPU cpu; + /** * Display mode of the hologram hardware *
TODO: move this inside CubotHologram class @@ -180,13 +192,26 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr 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"); + + try { + cpu = CPU.deserialize((Document) document.get("cpu"), this); + + ArrayList hardwareList = (ArrayList) document.get("hardware"); + + for (Object serialisedHw : hardwareList) { + HardwareModule hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw, this); + hardware.setCpu(cpu); + attachHardware(hardware, ((Document) serialisedHw).getInteger("address")); + } + } catch (CancelledException e) { + e.printStackTrace(); + } } @Override @@ -241,6 +266,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr public JSONObject jsonSerialise() { JSONObject json = super.jsonSerialise(); json.put("direction", getDirection().ordinal()); + CubotInventory inv = (CubotInventory) getHardware(CubotInventory.class); + int heldItem = inv.getInventory().getOrDefault(inv.getPosition(), new ItemVoid()).getId(); json.put("heldItem", heldItem); json.put("hp", hp); json.put("shield", shield); @@ -263,7 +290,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr Document dbObject = super.mongoSerialise(); dbObject.put("direction", getDirection().ordinal()); - dbObject.put("heldItem", heldItem); dbObject.put("hp", hp); dbObject.put("shield", shield); dbObject.put("action", lastAction.ordinal()); @@ -277,6 +303,20 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr dbObject.put("parent", parent.getUsername()); //Only used client-side for now } + List hardwareList = new ArrayList<>(); + + for (Integer address : hardwareAddresses.keySet()) { + + HardwareModule hardware = hardwareAddresses.get(address); + + Document serialisedHw = hardware.mongoSerialise(); + serialisedHw.put("address", address); + hardwareList.add(serialisedHw); + } + + dbObject.put("hardware", hardwareList); + + dbObject.put("cpu", cpu.mongoSerialise()); return dbObject; } @@ -287,7 +327,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr setDead(false); setHp(maxHp); setShield(0); - setHeldItem(0); setEnergy(maxEnergy); clearKeyboardBuffer(); consoleMessagesBuffer.clear(); @@ -300,7 +339,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr @Override public boolean onDeadCallback() { - LogManager.LOGGER.info(getParent().getUsername() + "'s Cubot died"); + //TODO make death event instead +// LogManager.LOGGER.info(getParent().getUsername() + "'s Cubot died"); reset(); @@ -326,14 +366,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr return true; } - public void setHeldItem(int heldItem) { - this.heldItem = heldItem; - } - - public int getHeldItem() { - return heldItem; - } - @Override public void setKeyboardBuffer(ArrayList kbBuffer) { keyboardBuffer = kbBuffer; @@ -352,14 +384,15 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr this.currentAction = currentAction; } - public User getParent() { - return parent; - } - public void setParent(User parent) { this.parent = parent; } + @Override + public User getParent() { + return parent; + } + public Action getAction() { return lastAction; } @@ -446,7 +479,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr @Override public Memory getFloppyData() { - CubotFloppyDrive drive = ((CubotFloppyDrive) getParent().getCpu().getHardware(CubotFloppyDrive.DEFAULT_ADDRESS)); + //TODO change DEFAULT_ADDRESS to getHW(class) to allow mutable addresses + CubotFloppyDrive drive = ((CubotFloppyDrive) getHardware(CubotFloppyDrive.DEFAULT_ADDRESS)); if (drive.getFloppy() != null) { return drive.getFloppy().getMemory(); @@ -568,4 +602,66 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr setDead(true); } } + + public void attachHardware(HardwareModule hardware, int address) { + hardwareAddresses.put(address, hardware); + hardwareModules.put(hardware.getClass(), address); + } + + public void detachHardware(int address) { + hardwareAddresses.remove(address); + + Class toRemove = null; + for (Class clazz : hardwareModules.keySet()) { + if (hardwareModules.get(clazz) == address) { + toRemove = clazz; + } + } + hardwareModules.remove(toRemove); + } + + public boolean hardwareInterrupt(int address, Status status) { + HardwareModule hardware = hardwareAddresses.get(address); + + if (hardware != null) { + hardware.handleInterrupt(status); + return true; + } else { + return false; + } + } + + public int hardwareQuery(int address) { + HardwareModule hardware = hardwareAddresses.get(address); + + + if (hardware != null) { + return hardware.getId(); + } else { + return 0; + } + } + + public HardwareModule getHardware(Class clazz) { + return hardwareAddresses.get(hardwareModules.get(clazz)); + } + + public HardwareModule getHardware(int address) { + return hardwareAddresses.get(address); + } + + @Override + public CPU getCpu() { + return cpu; + } + + public void setCpu(CPU cpu) { + this.cpu = cpu; + } + + @Override + public void giveItem(Item item) { + //Overwrite item at current position + ((CubotInventory) getHardware(CubotInventory.class)).putItem(item); + } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java index 7a50b6b..d345501 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotBattery.java @@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotBattery extends CubotHardware { +public class CubotBattery extends CubotHardwareModule { public static final int DEFAULT_ADDRESS = 0x000A; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java index 38dbdf4..a281dd7 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotComPort.java @@ -3,13 +3,13 @@ package net.simon987.cubotplugin; 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.Programmable; +import net.simon987.server.game.objects.MessageReceiver; import org.bson.Document; import java.awt.*; import java.util.ArrayList; -public class CubotComPort extends CubotHardware { +public class CubotComPort extends CubotHardwareModule { public static final char HWID = 0xD; public static final int DEFAULT_ADDRESS = 0xD; @@ -73,7 +73,7 @@ public class CubotComPort extends CubotHardware { //Todo will have to add getGameObjectsBlockingAt to enable Factory ArrayList objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y); - if (objects.size() > 0 && objects.get(0) instanceof Programmable) { + if (objects.size() > 0 && objects.get(0) instanceof MessageReceiver) { int x = getCpu().getRegisterSet().getRegister("X").getValue(); @@ -86,9 +86,9 @@ public class CubotComPort extends CubotHardware { char[] message = new char[MESSAGE_LENGTH]; System.arraycopy(getCpu().getMemory().getWords(), x, message, 0, MESSAGE_LENGTH); - //Send it to the Programmable object + //Send it to the MessageReceiver object getCpu().getRegisterSet().getRegister("B").setValue( - ((Programmable) objects.get(0)).sendMessage(message) ? 1 : 0); + ((MessageReceiver) objects.get(0)).sendMessage(message) ? 1 : 0); return; } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java index 9c3f7a4..74a303f 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotCore.java @@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotCore extends CubotHardware { +public class CubotCore extends CubotHardwareModule { public static final int DEFAULT_ADDRESS = 0x000E; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java index 09f6f89..85f6f2c 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotDrill.java @@ -3,10 +3,9 @@ package net.simon987.cubotplugin; import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.ControllableUnit; -import net.simon987.server.game.world.TileMap; import org.bson.Document; -public class CubotDrill extends CubotHardware { +public class CubotDrill extends CubotHardwareModule { /** * Hardware ID (Should be unique) @@ -43,17 +42,10 @@ public class CubotDrill extends CubotHardware { if (cubot.spendEnergy(1400)) { if (cubot.getCurrentAction() == Action.IDLE) { - int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); - if (tile == TileMap.IRON_TILE) { - cubot.setHeldItem(TileMap.ITEM_IRON); - cubot.setCurrentAction(Action.DIGGING); - - } else if (tile == TileMap.COPPER_TILE) { - cubot.setHeldItem(TileMap.ITEM_COPPER); - cubot.setCurrentAction(Action.DIGGING); - - } + //TODO: Get Tile instance and call onDig() + //int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); + //cubot.setCurrentAction(Action.DIGGING); } } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java index 16f1c4e..e042828 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotFloppyDrive.java @@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotFloppyDrive extends CubotHardware { +public class CubotFloppyDrive extends CubotHardwareModule { /** * Hardware ID (Should be unique) @@ -58,7 +58,7 @@ public class CubotFloppyDrive extends CubotHardware { int x = getCpu().getRegisterSet().getRegister("X").getValue(); int y = getCpu().getRegisterSet().getRegister("Y").getValue(); - floppyDisk.readSector(x, cubot.getParent().getCpu().getMemory(), y); + floppyDisk.readSector(x, cubot.getCpu().getMemory(), y); } } @@ -73,7 +73,7 @@ public class CubotFloppyDrive extends CubotHardware { int x = getCpu().getRegisterSet().getRegister("X").getValue(); int y = getCpu().getRegisterSet().getRegister("Y").getValue(); - floppyDisk.writeSector(x, cubot.getParent().getCpu().getMemory(), y); + floppyDisk.writeSector(x, cubot.getCpu().getMemory(), y); } } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardwareModule.java similarity index 63% rename from Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java rename to Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardwareModule.java index 1595404..4f9a5f5 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardware.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHardwareModule.java @@ -1,18 +1,18 @@ package net.simon987.cubotplugin; -import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.assembly.HardwareModule; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public abstract class CubotHardware extends CpuHardware { +public abstract class CubotHardwareModule extends HardwareModule { protected Cubot cubot; - public CubotHardware(Document document, ControllableUnit cubot) { + public CubotHardwareModule(Document document, ControllableUnit cubot) { this.cubot = (Cubot) cubot; } - public CubotHardware(Cubot cubot) { + public CubotHardwareModule(Cubot cubot) { this.cubot = cubot; } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java index 8819eec..0d90155 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotHologram.java @@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotHologram extends CubotHardware { +public class CubotHologram extends CubotHardwareModule { /** diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java index efe9819..0569a5a 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotInventory.java @@ -2,10 +2,15 @@ package net.simon987.cubotplugin; import net.simon987.server.GameServer; import net.simon987.server.assembly.Status; +import net.simon987.server.game.item.Item; +import net.simon987.server.game.item.ItemCopper; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotInventory extends CubotHardware { +import java.util.HashMap; +import java.util.Map; + +public class CubotInventory extends CubotHardwareModule { /** * Hardware ID (Should be unique) @@ -17,12 +22,53 @@ public class CubotInventory extends CubotHardware { private static final int INV_CLEAR = 0; private static final int INV_POLL = 1; + private int inventorySize = 4; + + private Map inventory; + private int position = 0; + public CubotInventory(Cubot cubot) { super(cubot); + + inventory = new HashMap<>(); + inventory.put(2, new ItemCopper(new Document())); } public CubotInventory(Document document, ControllableUnit cubot) { super(document, cubot); + + position = document.getInteger("position"); + inventorySize = document.getInteger("size"); + + inventory = new HashMap<>(); + for (String i : ((Map) document.get("inventory")).keySet()) { + inventory.put(Integer.valueOf(i), + GameServer.INSTANCE.getRegistry().deserializeItem(((Map) document.get("inventory")).get(i))); + } + } + + public void putItem(Item item) { + inventory.put(position, item); + } + + public Item popItem() { + Item item = inventory.get(position); + item.clear(cubot); + inventory.remove(position); + + return item; + } + + public int getPosition() { + return position; + } + + public void setPosition(int inventoryPosition) { + this.position = inventoryPosition; + } + + public Map getInventory() { + return inventory; } @Override @@ -36,18 +82,45 @@ public class CubotInventory extends CubotHardware { int a = getCpu().getRegisterSet().getRegister("A").getValue(); if (a == INV_POLL) { - getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem()); + Item item = inventory.get(position); + char result; + if (item == null) { + result = 0; + } else { + result = item.poll(); + } + getCpu().getRegisterSet().getRegister("B").setValue(result); } else if (a == INV_CLEAR) { - if (cubot.getHeldItem() == 0x0001) { - int energy = GameServer.INSTANCE.getConfig().getInt("biomassEnergyValue"); - cubot.storeEnergy(energy); - cubot.setHeldItem(0); - - } else if (cubot.spendEnergy(100)) { - cubot.setHeldItem(0); - } + popItem(); } } + + @Override + public Document mongoSerialise() { + Document document = super.mongoSerialise(); + + document.put("position", position); + document.put("size", inventorySize); + + Document items = new Document(); + + for (Integer i : inventory.keySet()) { + items.put(i.toString(), inventory.get(i).mongoSerialise()); + } + + document.put("inventory", items); + + return document; + } + + @Override + public String toString() { + String itemList = ""; + for (Integer i : inventory.keySet()) { + itemList += i + ": " + inventory.get(i).getClass().getSimpleName() + ", "; + } + return String.format("{CubotInventory[%d/%d] @ %d [%s]}", inventory.size(), inventorySize, position, itemList); + } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java index 534ffb2..b1ff947 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotKeyboard.java @@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotKeyboard extends CubotHardware { +public class CubotKeyboard extends CubotHardwareModule { public static final int DEFAULT_ADDRESS = 4; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java index db658a3..186f2e6 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLaser.java @@ -1,5 +1,6 @@ package net.simon987.cubotplugin; +import net.simon987.server.GameServer; import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.*; import org.bson.Document; @@ -7,7 +8,7 @@ import org.bson.Document; import java.awt.*; import java.util.ArrayList; -public class CubotLaser extends CubotHardware { +public class CubotLaser extends CubotHardwareModule { /** * Hardware ID (Should be unique) @@ -51,12 +52,12 @@ public class CubotLaser extends CubotHardware { if (cubot.getCurrentAction() == Action.IDLE && objects.size() > 0) { //FIXME: Problem here if more than 1 object if (objects.get(0) instanceof InventoryHolder) { + if (((InventoryHolder) objects.get(0)).canTakeItem(b)) { if (cubot.spendEnergy(30)) { //Take the item ((InventoryHolder) objects.get(0)).takeItem(b); - - cubot.setHeldItem(b); + cubot.giveItem(GameServer.INSTANCE.getRegistry().makeItem(b)); cubot.setCurrentAction(Action.WITHDRAWING); } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java index bf970d7..99710e1 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLeg.java @@ -6,7 +6,7 @@ import net.simon987.server.game.objects.ControllableUnit; import net.simon987.server.game.objects.Direction; import org.bson.Document; -public class CubotLeg extends CubotHardware { +public class CubotLeg extends CubotHardwareModule { public static final int DEFAULT_ADDRESS = 1; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java index f88bb1e..23ec707 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotLidar.java @@ -10,7 +10,7 @@ import org.bson.Document; import java.util.ArrayList; -public class CubotLidar extends CubotHardware { +public class CubotLidar extends CubotHardwareModule { /** * Hardware ID (Should be unique) diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java index a0e3407..5d90c32 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotPlugin.java @@ -1,8 +1,6 @@ package net.simon987.cubotplugin; -import net.simon987.cubotplugin.event.ChargeShieldCommandListener; -import net.simon987.cubotplugin.event.CpuInitialisationListener; -import net.simon987.cubotplugin.event.UserCreationListener; +import net.simon987.cubotplugin.event.*; import net.simon987.server.ServerConfiguration; import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.logging.LogManager; @@ -15,7 +13,11 @@ public class CubotPlugin extends ServerPlugin { public void init(ServerConfiguration config, GameRegistry registry) { listeners.add(new CpuInitialisationListener()); listeners.add(new UserCreationListener()); + //Debug commands listeners.add(new ChargeShieldCommandListener()); + listeners.add(new SetInventoryPosition()); + listeners.add(new PutItemCommandListener()); + listeners.add(new PopItemCommandListener()); registry.registerGameObject(Cubot.class); diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java index 41791b2..8353256 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/CubotShield.java @@ -5,7 +5,7 @@ import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; -public class CubotShield extends CubotHardware { +public class CubotShield extends CubotHardwareModule { public static final char DEFAULT_ADDRESS = 0x000F; diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/CpuInitialisationListener.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/CpuInitialisationListener.java index 05135c6..2e8d715 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/CpuInitialisationListener.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/CpuInitialisationListener.java @@ -5,7 +5,6 @@ import net.simon987.server.assembly.CPU; import net.simon987.server.event.CpuInitialisationEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.user.User; public class CpuInitialisationListener implements GameEventListener { @Override @@ -17,45 +16,46 @@ public class CpuInitialisationListener implements GameEventListener { public void handle(GameEvent event) { CPU cpu = (CPU) event.getSource(); - User user = ((CpuInitialisationEvent) event).getUser(); + Cubot cubot = (Cubot) ((CpuInitialisationEvent) event).getUnit(); + cpu.setHardwareHost(cubot); - CubotLeg legHw = new CubotLeg((Cubot) user.getControlledUnit()); + CubotLeg legHw = new CubotLeg(cubot); legHw.setCpu(cpu); - CubotLaser laserHw = new CubotLaser((Cubot) user.getControlledUnit()); + CubotLaser laserHw = new CubotLaser(cubot); laserHw.setCpu(cpu); - CubotLidar radarHw = new CubotLidar((Cubot) user.getControlledUnit()); + CubotLidar radarHw = new CubotLidar(cubot); radarHw.setCpu(cpu); - CubotKeyboard keyboard = new CubotKeyboard((Cubot) user.getControlledUnit()); + CubotKeyboard keyboard = new CubotKeyboard(cubot); keyboard.setCpu(cpu); - CubotDrill drillHw = new CubotDrill((Cubot) user.getControlledUnit()); + CubotDrill drillHw = new CubotDrill(cubot); drillHw.setCpu(cpu); - CubotInventory invHw = new CubotInventory((Cubot) user.getControlledUnit()); + CubotInventory invHw = new CubotInventory(cubot); invHw.setCpu(cpu); - CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit()); + CubotHologram emoteHw = new CubotHologram(cubot); emoteHw.setCpu(cpu); - CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit()); + CubotBattery batteryHw = new CubotBattery(cubot); batteryHw.setCpu(cpu); - CubotFloppyDrive floppyHw = new CubotFloppyDrive((Cubot) user.getControlledUnit()); + CubotFloppyDrive floppyHw = new CubotFloppyDrive(cubot); floppyHw.setCpu(cpu); - CubotComPort comPortHw = new CubotComPort((Cubot) user.getControlledUnit()); + CubotComPort comPortHw = new CubotComPort(cubot); comPortHw.setCpu(cpu); - CubotCore coreHw = new CubotCore((Cubot) user.getControlledUnit()); + CubotCore coreHw = new CubotCore(cubot); coreHw.setCpu(cpu); - CubotShield shieldHw = new CubotShield((Cubot) user.getControlledUnit()); + CubotShield shieldHw = new CubotShield(cubot); shieldHw.setCpu(cpu); - cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS); - cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS); - cpu.attachHardware(radarHw, CubotLidar.DEFAULT_ADDRESS); - cpu.attachHardware(keyboard, CubotKeyboard.DEFAULT_ADDRESS); - cpu.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS); - cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); - cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); - cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS); - cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS); - cpu.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS); - cpu.attachHardware(comPortHw, CubotComPort.DEFAULT_ADDRESS); - cpu.attachHardware(coreHw, CubotCore.DEFAULT_ADDRESS); - cpu.attachHardware(shieldHw, CubotShield.DEFAULT_ADDRESS); + cubot.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS); + cubot.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS); + cubot.attachHardware(radarHw, CubotLidar.DEFAULT_ADDRESS); + cubot.attachHardware(keyboard, CubotKeyboard.DEFAULT_ADDRESS); + cubot.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS); + cubot.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); + cubot.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); + cubot.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS); + cubot.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS); + cubot.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS); + cubot.attachHardware(comPortHw, CubotComPort.DEFAULT_ADDRESS); + cubot.attachHardware(coreHw, CubotCore.DEFAULT_ADDRESS); + cubot.attachHardware(shieldHw, CubotShield.DEFAULT_ADDRESS); } } diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PopItemCommandListener.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PopItemCommandListener.java new file mode 100644 index 0000000..cee70db --- /dev/null +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PopItemCommandListener.java @@ -0,0 +1,43 @@ +package net.simon987.cubotplugin.event; + +import net.simon987.cubotplugin.Cubot; +import net.simon987.cubotplugin.CubotInventory; +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.objects.GameObject; + +public class PopItemCommandListener implements GameEventListener { + + @Override + public Class getListenedEventType() { + return DebugCommandEvent.class; + } + + @Override + public void handle(GameEvent event) { + + DebugCommandEvent e = (DebugCommandEvent) event; + + if (e.getName().equals("popItem")) { + + GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId")); + + if (object != null) { + + if (object instanceof Cubot) { + + CubotInventory inventory = (CubotInventory) ((Cubot) object).getHardware(CubotInventory.class); + + e.reply("Removed item from inventory: " + inventory.popItem()); + } else { + e.reply("Object is not a Cubot"); + } + + } else { + e.reply("Object not found: " + e.getLong("objectId")); + } + } + } +} \ No newline at end of file diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PutItemCommandListener.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PutItemCommandListener.java new file mode 100644 index 0000000..ab380e9 --- /dev/null +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/PutItemCommandListener.java @@ -0,0 +1,53 @@ +package net.simon987.cubotplugin.event; + +import net.simon987.cubotplugin.Cubot; +import net.simon987.cubotplugin.CubotInventory; +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.item.Item; +import net.simon987.server.game.objects.GameObject; +import org.bson.Document; + +public class PutItemCommandListener implements GameEventListener { + + @Override + public Class getListenedEventType() { + return DebugCommandEvent.class; + } + + @Override + public void handle(GameEvent event) { + + DebugCommandEvent e = (DebugCommandEvent) event; + + if (e.getName().equals("putItem")) { + + GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId")); + + if (object != null) { + + if (object instanceof Cubot) { + + CubotInventory inventory = (CubotInventory) ((Cubot) object).getHardware(CubotInventory.class); + Item item = GameServer.INSTANCE.getRegistry().deserializeItem(Document.parse(e.getString("item"))); + + if (item != null) { + inventory.putItem(item); + e.reply("Set item to " + item.getClass().getSimpleName()); + + } else { + e.reply("Couldn't deserialize item"); + } + + } else { + e.reply("Object is not a Cubot"); + } + + } else { + e.reply("Object not found: " + e.getLong("objectId")); + } + } + } +} diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/SetInventoryPosition.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/SetInventoryPosition.java new file mode 100644 index 0000000..e538a7b --- /dev/null +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/SetInventoryPosition.java @@ -0,0 +1,45 @@ +package net.simon987.cubotplugin.event; + +import net.simon987.cubotplugin.Cubot; +import net.simon987.cubotplugin.CubotInventory; +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.objects.GameObject; + +public class SetInventoryPosition implements GameEventListener { + + @Override + public Class getListenedEventType() { + return DebugCommandEvent.class; + } + + @Override + public void handle(GameEvent event) { + + DebugCommandEvent e = (DebugCommandEvent) event; + + if (e.getName().equals("setInventoryPosition")) { + + GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(e.getLong("objectId")); + + if (object != null) { + + if (object instanceof Cubot) { + + int position = e.getInt("position"); + CubotInventory inventory = (CubotInventory) ((Cubot) object).getHardware(CubotInventory.class); + + inventory.setPosition(position); + e.reply("Set inventory position to " + position); + } else { + e.reply("Object is not a Cubot"); + } + + } else { + e.reply("Object not found: " + e.getLong("objectId")); + } + } + } +} diff --git a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/UserCreationListener.java b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/UserCreationListener.java index a39f568..e95829b 100644 --- a/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/UserCreationListener.java +++ b/Plugin Cubot/src/main/java/net/simon987/cubotplugin/event/UserCreationListener.java @@ -4,6 +4,10 @@ import net.simon987.cubotplugin.Cubot; import net.simon987.cubotplugin.CubotStatus; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; +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.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.event.UserCreationEvent; @@ -45,7 +49,6 @@ public class UserCreationListener implements GameEventListener { cubot.getWorld().addObject(cubot); cubot.getWorld().incUpdatable(); - cubot.setHeldItem(config.getInt("new_user_item")); cubot.setEnergy(config.getInt("battery_max_energy")); cubot.setMaxEnergy(config.getInt("battery_max_energy")); @@ -56,6 +59,27 @@ public class UserCreationListener implements GameEventListener { cubot.setParent(user); user.setControlledUnit(cubot); + //Create CPU + try { + cubot.setCpu(new CPU(GameServer.INSTANCE.getConfig(), cubot)); + cubot.getCpu().setHardwareHost(cubot); + user.setUserCode(config.getString("new_user_code")); + + //Compile user code + AssemblyResult ar = new Assembler(cubot.getCpu().getInstructionSet(), cubot.getCpu().getRegisterSet(), + GameServer.INSTANCE.getConfig()).parse(user.getUserCode()); + + cubot.getCpu().getMemory().clear(); + + //Write assembled code to mem + char[] assembledCode = ar.getWords(); + + cubot.getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length); + cubot.getCpu().setCodeSectionOffset(ar.getCodeSectionOffset()); + } catch (CancelledException e) { + e.printStackTrace(); + } + LogManager.LOGGER.fine("(Plugin) Handled User creation event (Cubot Plugin)"); } } diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java index ed8b288..43f59b5 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/Clock.java @@ -1,7 +1,7 @@ package net.simon987.mischwplugin; import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Util; import net.simon987.server.game.objects.ControllableUnit; @@ -10,7 +10,7 @@ import org.bson.Document; /** * Hardware to get game time */ -public class Clock extends CpuHardware { +public class Clock extends HardwareModule { private static final char HWID = 0x0008; diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java index 118c7e8..df4f2f0 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/RandomNumberGenerator.java @@ -1,6 +1,6 @@ package net.simon987.mischwplugin; -import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Status; import net.simon987.server.game.objects.ControllableUnit; import org.bson.Document; @@ -10,7 +10,7 @@ import java.util.Random; /** * Hardware to generate random numbers */ -public class RandomNumberGenerator extends CpuHardware { +public class RandomNumberGenerator extends HardwareModule { private static final char HWID = 0x0007; diff --git a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/event/CpuInitialisationListener.java b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/event/CpuInitialisationListener.java index 2587d49..5563ba0 100644 --- a/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/event/CpuInitialisationListener.java +++ b/Plugin Misc HW/src/main/java/net/simon987/mischwplugin/event/CpuInitialisationListener.java @@ -24,7 +24,7 @@ public class CpuInitialisationListener implements GameEventListener { Clock clock = new Clock(); clock.setCpu(cpu); - cpu.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS); - cpu.attachHardware(clock, Clock.DEFAULT_ADDRESS); + cubot.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS); + cubot.attachHardware(clock, Clock.DEFAULT_ADDRESS); } } \ No newline at end of file diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java index 0fed570..b2e3369 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java @@ -1,6 +1,7 @@ package net.simon987.npcplugin; import net.simon987.npcplugin.event.CpuInitialisationListener; +import net.simon987.npcplugin.event.VaultCompleteListener; import net.simon987.npcplugin.event.VaultWorldUpdateListener; import net.simon987.npcplugin.event.WorldCreationListener; import net.simon987.server.ServerConfiguration; @@ -23,6 +24,7 @@ public class NpcPlugin extends ServerPlugin { listeners.add(new WorldCreationListener(configuration.getInt("factory_spawn_rate"))); listeners.add(new CpuInitialisationListener()); listeners.add(new VaultWorldUpdateListener(configuration)); + listeners.add(new VaultCompleteListener()); registry.registerGameObject(HarvesterNPC.class); registry.registerGameObject(Factory.class); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java index c7cf509..0fd24a0 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioReceiverHardware.java @@ -1,7 +1,7 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Util; import net.simon987.server.game.objects.Action; @@ -10,7 +10,7 @@ import org.bson.Document; import java.util.ArrayList; -public class RadioReceiverHardware extends CpuHardware { +public class RadioReceiverHardware extends HardwareModule { public static final char HWID = 0xC; //12 diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java index 114052b..dc1c8ea 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java @@ -1,14 +1,14 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.game.objects.Programmable; +import net.simon987.server.game.objects.MessageReceiver; import net.simon987.server.game.objects.Structure; import net.simon987.server.game.objects.Updatable; import org.bson.Document; import java.util.ArrayList; -public class RadioTower extends Structure implements Programmable, Updatable { +public class RadioTower extends Structure implements MessageReceiver, Updatable { private static final int MAP_INFO = 0x1000; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java index 97268be..5a5aa5a 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java @@ -10,7 +10,7 @@ import org.bson.Document; import java.util.Arrays; -public class VaultDoor extends Structure implements Programmable, Enterable, Updatable { +public class VaultDoor extends Structure implements MessageReceiver, Enterable, Updatable { private static final int MAP_INFO = 0x0800; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java index 384fd12..c6f305e 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultExitPortal.java @@ -1,9 +1,7 @@ package net.simon987.npcplugin; -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; /** @@ -31,12 +29,8 @@ public class VaultExitPortal extends Portal { @Override public boolean enter(GameObject object) { - if (object instanceof ControllableUnit) { - LogManager.LOGGER.info(((ControllableUnit) object).getParent().getUsername() + " Completed vault " + - object.getWorld().getDimension()); - ((ControllableUnit) object).getParent().getStats().addToStringSet("completedVaults", getWorld().getDimension()); - } + //TODO: Trigger vault complete event instead return super.enter(object); } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/CpuInitialisationListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/CpuInitialisationListener.java index f6bf356..fbbd422 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/CpuInitialisationListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/CpuInitialisationListener.java @@ -5,7 +5,7 @@ import net.simon987.server.assembly.CPU; import net.simon987.server.event.CpuInitialisationEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; -import net.simon987.server.user.User; +import net.simon987.server.game.objects.ControllableUnit; public class CpuInitialisationListener implements GameEventListener { @Override @@ -17,11 +17,12 @@ public class CpuInitialisationListener implements GameEventListener { @Override public void handle(GameEvent event) { CPU cpu = (CPU) event.getSource(); - User user = ((CpuInitialisationEvent) event).getUser(); - RadioReceiverHardware radioHw = new RadioReceiverHardware(user.getControlledUnit()); + ControllableUnit controllableUnit = ((CpuInitialisationEvent) event).getUnit(); + + RadioReceiverHardware radioHw = new RadioReceiverHardware(controllableUnit); radioHw.setCpu(cpu); - cpu.attachHardware(radioHw, RadioReceiverHardware.DEFAULT_ADDRESS); + cubot.attachHardware(radioHw, RadioReceiverHardware.DEFAULT_ADDRESS); } } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteEvent.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteEvent.java new file mode 100644 index 0000000..3afbe39 --- /dev/null +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteEvent.java @@ -0,0 +1,26 @@ +package net.simon987.npcplugin.event; + +import net.simon987.npcplugin.VaultExitPortal; +import net.simon987.server.event.GameEvent; +import net.simon987.server.game.objects.GameObject; + +public class VaultCompleteEvent extends GameEvent { + + private VaultExitPortal portal; + + public VaultCompleteEvent(GameObject object, VaultExitPortal portal) { + + //TODO: Add completion time? + setSource(object); + this.portal = portal; + } + + @Override + public GameObject getSource() { + return (GameObject) super.getSource(); + } + + public VaultExitPortal getPortal() { + return portal; + } +} diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteListener.java new file mode 100644 index 0000000..1c69919 --- /dev/null +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/VaultCompleteListener.java @@ -0,0 +1,27 @@ +package net.simon987.npcplugin.event; + +import net.simon987.server.event.GameEvent; +import net.simon987.server.event.GameEventListener; +import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.objects.GameObject; +import net.simon987.server.logging.LogManager; + +public class VaultCompleteListener implements GameEventListener { + @Override + public Class getListenedEventType() { + return VaultCompleteEvent.class; + } + + @Override + public void handle(GameEvent event) { + VaultCompleteEvent vaultCompleteEvent = (VaultCompleteEvent) event; + GameObject object = vaultCompleteEvent.getSource(); + if (object instanceof ControllableUnit) { + LogManager.LOGGER.info(((ControllableUnit) object).getParent().getUsername() + " Completed vault " + + object.getWorld().getDimension()); + + ((ControllableUnit) object).getParent().getStats().addToStringSet("completedVaults", + vaultCompleteEvent.getPortal().getWorld().getDimension()); + } + } +} diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java index 4cd0a24..d373a21 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassBlob.java @@ -1,5 +1,6 @@ package net.simon987.biomassplugin; +import net.simon987.server.game.item.Item; import net.simon987.server.game.objects.GameObject; import net.simon987.server.game.objects.InventoryHolder; import org.bson.Document; @@ -13,12 +14,6 @@ public class BiomassBlob extends GameObject implements InventoryHolder { * Yield of the blob, in biomass units */ private int biomassCount; - /** - * Style of the blob (Only visual) - */ - // private int style; - - private static final int ITM_BIOMASS = 1; public BiomassBlob() { } @@ -66,31 +61,28 @@ public class BiomassBlob extends GameObject implements InventoryHolder { /** * Called when an object attempts to place an item in this BiomassBlob * - * @param item item id (see MarConstants.ITEM_*) * @return Always returns false */ @Override - public boolean placeItem(int item) { + public boolean placeItem(Item item) { //Why would you want to place an item in a blob? return false; } @Override - public boolean canTakeItem(int item) { - return item == ITM_BIOMASS && biomassCount >= 1; + public boolean canTakeItem(int itemId) { + return itemId == ItemBiomass.ID && biomassCount >= 1; } /** * Called when an object attempts to take an item from this BiomassBlob. * If the object requests biomass, it will be subtracted from biomassCount, and * if it reaches 0, the plant is deleted - * - * @param item item id (see MarConstants.ITEM_*) */ @Override - public void takeItem(int item) { + public void takeItem(int itemId) { - if (item == ITM_BIOMASS) { + if (itemId == ItemBiomass.ID) { if (biomassCount > 1) { biomassCount--; } else { @@ -98,6 +90,5 @@ public class BiomassBlob extends GameObject implements InventoryHolder { setDead(true); } } - } } diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java index 525a713..8028689 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/BiomassPlugin.java @@ -23,8 +23,8 @@ public class BiomassPlugin extends ServerPlugin { LogManager.LOGGER.severe("(BiomassPlugin) NPC plugin is not loaded so biomass will not spawn on death of HarvesterNPC"); } - registry.registerGameObject(BiomassBlob.class); + registry.registerItem(ItemBiomass.ID, ItemBiomass.class); LogManager.LOGGER.info("(BiomassPlugin) Initialised Biomass plugin"); } diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/ItemBiomass.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/ItemBiomass.java new file mode 100644 index 0000000..e41cba3 --- /dev/null +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/ItemBiomass.java @@ -0,0 +1,35 @@ +package net.simon987.biomassplugin; + +import net.simon987.server.GameServer; +import net.simon987.server.game.item.Item; +import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.objects.Rechargeable; +import org.bson.Document; + +public class ItemBiomass extends Item { + + public static final int ID = 0x0001; + + private static final int energy = GameServer.INSTANCE.getConfig().getInt("biomassEnergyValue"); + + @Override + public int getId() { + return ID; + } + + public ItemBiomass(Document document) { + super(document); + } + + @Override + public void clear(ControllableUnit unit) { + if (unit instanceof Rechargeable) { + ((Rechargeable) unit).storeEnergy(energy); + } + } + + @Override + public char poll() { + return ID; + } +} diff --git a/Server/src/main/java/net/simon987/server/GameServer.java b/Server/src/main/java/net/simon987/server/GameServer.java index e95be4b..7d5cbae 100644 --- a/Server/src/main/java/net/simon987/server/GameServer.java +++ b/Server/src/main/java/net/simon987/server/GameServer.java @@ -12,6 +12,8 @@ import net.simon987.server.event.GameEventDispatcher; import net.simon987.server.event.TickEvent; import net.simon987.server.game.GameUniverse; import net.simon987.server.game.debug.*; +import net.simon987.server.game.item.ItemCopper; +import net.simon987.server.game.item.ItemIron; import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.game.world.DayNightCycle; import net.simon987.server.game.world.World; @@ -108,6 +110,9 @@ public class GameServer implements Runnable { eventDispatcher.getListeners().add(new DamageObjCommandListener()); eventDispatcher.getListeners().add(new SetEnergyCommandListener()); eventDispatcher.getListeners().add(new SaveGameCommandListener()); + + gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class); + gameRegistry.registerItem(ItemIron.ID, ItemIron.class); } public GameUniverse getGameUniverse() { @@ -163,13 +168,13 @@ public class GameServer implements Runnable { //Process user code for (User user : gameUniverse.getUsers()) { - if (user.getCpu() != null) { + if (user.getControlledUnit() != null && user.getControlledUnit().getCpu() != null) { try { int timeout = Math.min(user.getControlledUnit().getEnergy(), maxExecutionTime); - user.getCpu().reset(); - int cost = user.getCpu().execute(timeout); + user.getControlledUnit().getCpu().reset(); + int cost = user.getControlledUnit().getCpu().execute(timeout); user.getControlledUnit().spendEnergy(cost); } catch (Exception e) { diff --git a/Server/src/main/java/net/simon987/server/assembly/CPU.java b/Server/src/main/java/net/simon987/server/assembly/CPU.java index 38d6792..e1875e0 100755 --- a/Server/src/main/java/net/simon987/server/assembly/CPU.java +++ b/Server/src/main/java/net/simon987/server/assembly/CPU.java @@ -6,15 +6,12 @@ import net.simon987.server.assembly.exception.CancelledException; import net.simon987.server.assembly.instruction.*; import net.simon987.server.event.CpuInitialisationEvent; import net.simon987.server.event.GameEvent; +import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.game.objects.HardwareHost; import net.simon987.server.io.MongoSerializable; import net.simon987.server.logging.LogManager; -import net.simon987.server.user.User; import org.bson.Document; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - /** * CPU: Central Processing Unit. A CPU is capable of reading bytes from * a Memory object and execute them. A CPU object holds registers objects and @@ -54,9 +51,10 @@ public class CPU implements MongoSerializable { private int ip; /** - * List of attached hardware, 'modules' + * Hardware is connected to the hardwareHost */ - private HashMap attachedHardware; + private HardwareHost hardwareHost; + private ServerConfiguration config; @@ -68,11 +66,10 @@ public class CPU implements MongoSerializable { /** * Creates a new CPU */ - public CPU(ServerConfiguration config, User user) throws CancelledException { + public CPU(ServerConfiguration config, ControllableUnit unit) throws CancelledException { this.config = config; instructionSet = new DefaultInstructionSet(); registerSet = new DefaultRegisterSet(); - attachedHardware = new HashMap<>(); codeSectionOffset = config.getInt("org_offset"); instructionSet.add(new JmpInstruction(this)); @@ -105,7 +102,7 @@ public class CPU implements MongoSerializable { status = new Status(); memory = new Memory(config.getInt("memory_size")); - GameEvent event = new CpuInitialisationEvent(this, user); + GameEvent event = new CpuInitialisationEvent(this, unit); GameServer.INSTANCE.getEventDispatcher().dispatch(event); if (event.isCancelled()) { throw new CancelledException(); @@ -355,36 +352,17 @@ public class CPU implements MongoSerializable { dbObject.put("registerSet", registerSet.mongoSerialise()); dbObject.put("codeSegmentOffset", codeSectionOffset); - List hardwareList = new ArrayList<>(); - - for (Integer address : attachedHardware.keySet()) { - - CpuHardware hardware = attachedHardware.get(address); - - Document serialisedHw = hardware.mongoSerialise(); - serialisedHw.put("address", address); - hardwareList.add(serialisedHw); - } - - dbObject.put("hardware", hardwareList); return dbObject; } - public static CPU deserialize(Document obj, User user) throws CancelledException { + public static CPU deserialize(Document obj, ControllableUnit unit) throws CancelledException { - CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user); + CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), unit); cpu.codeSectionOffset = obj.getInteger("codeSegmentOffset"); - ArrayList hardwareList = (ArrayList) obj.get("hardware"); - - for (Object serialisedHw : hardwareList) { - CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw, user.getControlledUnit()); - hardware.setCpu(cpu); - cpu.attachHardware(hardware, ((Document) serialisedHw).getInteger("address")); - } cpu.memory = new Memory((Document) obj.get("memory")); cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet")); @@ -421,56 +399,21 @@ public class CPU implements MongoSerializable { this.codeSectionOffset = codeSectionOffset; } - public void attachHardware(CpuHardware hardware, int address) { - attachedHardware.put(address, hardware); - } - - public void detachHardware(int address) { - attachedHardware.remove(address); - } - - public boolean hardwareInterrupt(int address) { - CpuHardware hardware = attachedHardware.get(address); - - if (hardware != null) { - hardware.handleInterrupt(status); - return true; - } else { - return false; - } - } - - public void hardwareQuery(int address) { - CpuHardware hardware = attachedHardware.get(address); - - if (hardware != null) { - - registerSet.getRegister("B").setValue(hardware.getId()); - } else { - registerSet.getRegister("B").setValue(0); - } - } @Override public String toString() { - String str = "------------------------\n"; - str += registerSet.toString(); + String str = registerSet.toString(); str += status.toString(); - str += "HW: "; - for (CpuHardware hw : attachedHardware.values()) { - str += hw + ", "; - } - str += "\n------------------------\n"; return str; } - public CpuHardware getHardware(int address) { - - return attachedHardware.get(address); - + public HardwareHost getHardwareHost() { + return hardwareHost; } - + public void setHardwareHost(HardwareHost hardwareHost) { + this.hardwareHost = hardwareHost; + } } diff --git a/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java b/Server/src/main/java/net/simon987/server/assembly/HardwareModule.java similarity index 65% rename from Server/src/main/java/net/simon987/server/assembly/CpuHardware.java rename to Server/src/main/java/net/simon987/server/assembly/HardwareModule.java index 44f3b3c..129bf3f 100644 --- a/Server/src/main/java/net/simon987/server/assembly/CpuHardware.java +++ b/Server/src/main/java/net/simon987/server/assembly/HardwareModule.java @@ -6,15 +6,15 @@ import net.simon987.server.io.MongoSerializable; import org.bson.Document; -public abstract class CpuHardware implements MongoSerializable { +public abstract class HardwareModule implements MongoSerializable { private CPU cpu; - public CpuHardware() { + public HardwareModule() { } - public CpuHardware(Document document, ControllableUnit unit) { + public HardwareModule(Document document, ControllableUnit unit) { } @@ -23,7 +23,7 @@ public abstract class CpuHardware implements MongoSerializable { */ public abstract void handleInterrupt(Status status); - public CPU getCpu() { + protected CPU getCpu() { return cpu; } @@ -35,6 +35,6 @@ public abstract class CpuHardware implements MongoSerializable { @Override public String toString() { - return String.format("<%04X>", (int) getId()); + return String.format("{%s}", getClass().getSimpleName()); } } diff --git a/Server/src/main/java/net/simon987/server/assembly/instruction/HwiInstruction.java b/Server/src/main/java/net/simon987/server/assembly/instruction/HwiInstruction.java index f7459d1..b65a4c2 100755 --- a/Server/src/main/java/net/simon987/server/assembly/instruction/HwiInstruction.java +++ b/Server/src/main/java/net/simon987/server/assembly/instruction/HwiInstruction.java @@ -23,7 +23,7 @@ public class HwiInstruction extends Instruction { @Override public Status execute(Target src, int srcIndex, Status status) { - status.setErrorFlag(cpu.hardwareInterrupt(src.get(srcIndex))); + status.setErrorFlag(cubot.hardwareInterrupt(src.get(srcIndex), cpu.getStatus())); return status; } @@ -31,7 +31,7 @@ public class HwiInstruction extends Instruction { @Override public Status execute(int src, Status status) { - status.setErrorFlag(cpu.hardwareInterrupt(src)); + status.setErrorFlag(cubot.hardwareInterrupt(src, cpu.getStatus())); return status; } diff --git a/Server/src/main/java/net/simon987/server/assembly/instruction/HwqInstruction.java b/Server/src/main/java/net/simon987/server/assembly/instruction/HwqInstruction.java index 85fa169..6759cc3 100644 --- a/Server/src/main/java/net/simon987/server/assembly/instruction/HwqInstruction.java +++ b/Server/src/main/java/net/simon987/server/assembly/instruction/HwqInstruction.java @@ -18,14 +18,14 @@ public class HwqInstruction extends Instruction { @Override public Status execute(Target src, int srcIndex, Status status) { - cpu.hardwareQuery(src.get(srcIndex)); + cubot.hardwareQuery(src.get(srcIndex)); return status; } @Override public Status execute(int src, Status status) { - cpu.hardwareQuery(src); + cubot.hardwareQuery(src); return status; } diff --git a/Server/src/main/java/net/simon987/server/event/CpuInitialisationEvent.java b/Server/src/main/java/net/simon987/server/event/CpuInitialisationEvent.java index 336e443..c35a7b5 100644 --- a/Server/src/main/java/net/simon987/server/event/CpuInitialisationEvent.java +++ b/Server/src/main/java/net/simon987/server/event/CpuInitialisationEvent.java @@ -1,18 +1,18 @@ package net.simon987.server.event; import net.simon987.server.assembly.CPU; -import net.simon987.server.user.User; +import net.simon987.server.game.objects.ControllableUnit; public class CpuInitialisationEvent extends GameEvent { - private User user; + private ControllableUnit unit; - public CpuInitialisationEvent(CPU cpu, User user) { + public CpuInitialisationEvent(CPU cpu, ControllableUnit unit) { setSource(cpu); - this.user = user; + this.unit = unit; } - public User getUser() { - return user; + public ControllableUnit getUnit() { + return unit; } } diff --git a/Server/src/main/java/net/simon987/server/game/GameUniverse.java b/Server/src/main/java/net/simon987/server/game/GameUniverse.java index 5136814..9eeec62 100644 --- a/Server/src/main/java/net/simon987/server/game/GameUniverse.java +++ b/Server/src/main/java/net/simon987/server/game/GameUniverse.java @@ -6,9 +6,6 @@ import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; -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; @@ -190,20 +187,6 @@ public class GameUniverse { try { if (makeControlledUnit) { user = new User(); - user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user)); - user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code")); - - //Compile user code - AssemblyResult ar = new Assembler(user.getCpu().getInstructionSet(), user.getCpu().getRegisterSet(), - GameServer.INSTANCE.getConfig()).parse(user.getUserCode()); - - user.getCpu().getMemory().clear(); - - //Write assembled code to mem - char[] assembledCode = ar.getWords(); - - user.getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length); - user.getCpu().setCodeSectionOffset(ar.getCodeSectionOffset()); } else { diff --git a/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java index 3048e6a..2c2cf49 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/ComPortMsgCommandListener.java @@ -5,7 +5,7 @@ import net.simon987.server.event.DebugCommandEvent; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.game.objects.GameObject; -import net.simon987.server.game.objects.Programmable; +import net.simon987.server.game.objects.MessageReceiver; public class ComPortMsgCommandListener implements GameEventListener { @@ -27,12 +27,12 @@ public class ComPortMsgCommandListener implements GameEventListener { if (object != null) { - if (object instanceof Programmable) { + if (object instanceof MessageReceiver) { - e.reply("Result: " + ((Programmable) object).sendMessage(e.getString("message").toCharArray())); + e.reply("Result: " + ((MessageReceiver) object).sendMessage(e.getString("message").toCharArray())); } else { - e.reply("Object " + objectId + " not Programmable"); + e.reply("Object " + objectId + " not MessageReceiver"); } } else { diff --git a/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java b/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java index 150dd07..d1a7d23 100644 --- a/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java +++ b/Server/src/main/java/net/simon987/server/game/debug/UserInfoCommandListener.java @@ -32,7 +32,7 @@ public class UserInfoCommandListener implements GameEventListener { ControllableUnit unit = user.getControlledUnit(); message += "ControlledUnit: " + unit.getObjectId() + " at (" + unit.getX() + ", " + unit.getY() + ")\n"; - message += "CPU:" + user.getCpu() + "\n"; + message += "CPU:" + user.getControlledUnit().getCpu() + "\n"; message += "Code: " + user.getUserCode(); e.reply(message); diff --git a/Server/src/main/java/net/simon987/server/game/item/Item.java b/Server/src/main/java/net/simon987/server/game/item/Item.java new file mode 100644 index 0000000..34f01c8 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/item/Item.java @@ -0,0 +1,54 @@ +package net.simon987.server.game.item; + +import net.simon987.server.game.objects.ControllableUnit; +import net.simon987.server.io.JSONSerialisable; +import net.simon987.server.io.MongoSerializable; +import org.bson.Document; +import org.json.simple.JSONObject; + +public abstract class Item implements JSONSerialisable, MongoSerializable { + + + public Item(Document document) { + + } + + /** + * Called when polled + * + * @return result of poll operation + */ + public abstract char poll(); + + /** + * Called when a controllableUnit clears this item from inventory + */ + public void clear(ControllableUnit unit) { + + } + + /** + * Used to uniquely identify an item type in the database and in the game + */ + public abstract int getId(); + + @Override + public JSONObject jsonSerialise() { + + JSONObject json = new JSONObject(); + + json.put("type", getId()); + + return json; + } + + @Override + public Document mongoSerialise() { + Document document = new Document(); + + document.put("type", getId()); + + return document; + } + +} diff --git a/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java b/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java new file mode 100644 index 0000000..f5eb5ff --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/item/ItemCopper.java @@ -0,0 +1,23 @@ +package net.simon987.server.game.item; + + +import org.bson.Document; + +public class ItemCopper extends Item { + + public static final int ID = 0x0003; + + @Override + public int getId() { + return ID; + } + + public ItemCopper(Document document) { + super(document); + } + + @Override + public char poll() { + return ID; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/item/ItemIron.java b/Server/src/main/java/net/simon987/server/game/item/ItemIron.java new file mode 100644 index 0000000..dcebf1d --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/item/ItemIron.java @@ -0,0 +1,22 @@ +package net.simon987.server.game.item; + +import org.bson.Document; + +public class ItemIron extends Item { + + public static final int ID = 0x0004; + + @Override + public int getId() { + return ID; + } + + public ItemIron(Document document) { + super(document); + } + + @Override + public char poll() { + return ID; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/item/ItemVoid.java b/Server/src/main/java/net/simon987/server/game/item/ItemVoid.java new file mode 100644 index 0000000..982f0f8 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/item/ItemVoid.java @@ -0,0 +1,21 @@ +package net.simon987.server.game.item; + +/** + * Invalid/empty item + */ +public class ItemVoid extends Item { + + public ItemVoid() { + super(null); + } + + @Override + public char poll() { + return 0; + } + + @Override + public int getId() { + return 0; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java b/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java index be32be4..460a513 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java +++ b/Server/src/main/java/net/simon987/server/game/objects/ControllableUnit.java @@ -1,6 +1,8 @@ package net.simon987.server.game.objects; +import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.Memory; +import net.simon987.server.game.item.Item; import net.simon987.server.game.world.World; import net.simon987.server.user.User; @@ -35,4 +37,8 @@ public interface ControllableUnit { ArrayList getConsoleMessagesBuffer(); int getConsoleMode(); + + CPU getCpu(); + + void giveItem(Item item); } diff --git a/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java index 6e42a21..3c863ec 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java +++ b/Server/src/main/java/net/simon987/server/game/objects/GameRegistry.java @@ -1,6 +1,7 @@ package net.simon987.server.game.objects; -import net.simon987.server.assembly.CpuHardware; +import net.simon987.server.assembly.HardwareModule; +import net.simon987.server.game.item.Item; import net.simon987.server.logging.LogManager; import org.bson.Document; @@ -10,24 +11,29 @@ import java.util.HashMap; public class GameRegistry { private HashMap> gameObjects; - private HashMap> hardware; + private HashMap> hardware; + private HashMap> items; public GameRegistry() { gameObjects = new HashMap<>(); hardware = new HashMap<>(); + items = new HashMap<>(); } public void registerGameObject(Class clazz) { gameObjects.put(clazz.getCanonicalName(), clazz); } - public void registerHardware(Class clazz) { + public void registerHardware(Class clazz) { hardware.put(clazz.getCanonicalName(), clazz); } + public void registerItem(int id, Class clazz) { + items.put(id, clazz); + } - public CpuHardware deserializeHardware(Document document, ControllableUnit controllableUnit) { + public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) { String type = document.getString("type"); if (hardware.containsKey(type)) { @@ -40,7 +46,7 @@ public class GameRegistry { return null; } } else { - LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type); + LogManager.LOGGER.severe("Trying to deserialize unknown HardwareModule type: " + type); return null; } } @@ -68,6 +74,51 @@ public class GameRegistry { } } + public Item deserializeItem(Document document) { + + int type = document.getInteger("type"); + + if (items.containsKey(type)) { + + try { + return items.get(type).getConstructor(Document.class).newInstance(document); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) { + e.printStackTrace(); + 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 { + LogManager.LOGGER.severe("Trying to deserialize unknown Item type: " + type); + return null; + } + } + + /** + * Creates an item with default values + * + * @param itemId id of the item + */ + public Item makeItem(int itemId) { + if (items.containsKey(itemId)) { + + try { + return items.get(itemId).getConstructor().newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException + | InvocationTargetException e) { + e.printStackTrace(); + return null; + } + + } else { + LogManager.LOGGER.severe("Trying to create an unknown Item type: " + itemId); + return null; + } + } + public boolean isGameObjectRegistered(String type) { return gameObjects.containsKey(type); } diff --git a/Server/src/main/java/net/simon987/server/game/objects/HardwareHost.java b/Server/src/main/java/net/simon987/server/game/objects/HardwareHost.java new file mode 100644 index 0000000..09060d3 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/objects/HardwareHost.java @@ -0,0 +1,16 @@ +package net.simon987.server.game.objects; + +import net.simon987.server.assembly.HardwareModule; +import net.simon987.server.assembly.Status; + +public interface HardwareHost { + + void attachHardware(HardwareModule hardware, int address); + + void detachHardware(int address); + + boolean hardwareInterrupt(int address, Status status); + + int hardwareQuery(int address); + +} diff --git a/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java b/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java index 60c9ec6..505b968 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java +++ b/Server/src/main/java/net/simon987/server/game/objects/InventoryHolder.java @@ -1,25 +1,23 @@ package net.simon987.server.game.objects; +import net.simon987.server.game.item.Item; + public interface InventoryHolder { /** * Place an item into the inventory - * - * @param item item id (see MarConstants.ITEM_*) */ - boolean placeItem(int item); + boolean placeItem(Item item); /** * Take an item from the inventory - * - * @param item Desired item id (see MarConstants.ITEM_*) */ - void takeItem(int item); + void takeItem(int itemId); /** - * @param item item to take + * @param itemId id of the item * @return true if the InventoryHolder can provide this item */ - boolean canTakeItem(int item); + boolean canTakeItem(int itemId); } diff --git a/Server/src/main/java/net/simon987/server/game/objects/Programmable.java b/Server/src/main/java/net/simon987/server/game/objects/MessageReceiver.java similarity index 71% rename from Server/src/main/java/net/simon987/server/game/objects/Programmable.java rename to Server/src/main/java/net/simon987/server/game/objects/MessageReceiver.java index eed27ce..c329496 100644 --- a/Server/src/main/java/net/simon987/server/game/objects/Programmable.java +++ b/Server/src/main/java/net/simon987/server/game/objects/MessageReceiver.java @@ -1,6 +1,6 @@ package net.simon987.server.game.objects; -public interface Programmable { +public interface MessageReceiver { boolean sendMessage(char[] message); diff --git a/Server/src/main/java/net/simon987/server/game/world/Tile.java b/Server/src/main/java/net/simon987/server/game/world/Tile.java new file mode 100644 index 0000000..776c517 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/world/Tile.java @@ -0,0 +1,21 @@ +package net.simon987.server.game.world; + +import net.simon987.server.game.item.Item; + +public abstract class Tile { + + protected boolean blocked; + + /** + * @return Unique id of the tile + */ + public abstract int getId(); + + public Item onDrill() { + return null; + } + + public boolean isBlocked() { + return blocked; + } +} diff --git a/Server/src/main/java/net/simon987/server/game/world/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java index 1d07627..7b8f335 100644 --- a/Server/src/main/java/net/simon987/server/game/world/World.java +++ b/Server/src/main/java/net/simon987/server/game/world/World.java @@ -298,7 +298,7 @@ public class World implements MongoSerializable { * and will be able to leave the World *
* Note: This function is quite expensive and shouldn't be used - * by some CpuHardware in its current state + * by some HardwareModule in its current state * * @return random non-blocked tile */ diff --git a/Server/src/main/java/net/simon987/server/user/User.java b/Server/src/main/java/net/simon987/server/user/User.java index b9e49dd..5e223ed 100755 --- a/Server/src/main/java/net/simon987/server/user/User.java +++ b/Server/src/main/java/net/simon987/server/user/User.java @@ -1,7 +1,6 @@ package net.simon987.server.user; import net.simon987.server.GameServer; -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; @@ -20,8 +19,6 @@ public class User implements MongoSerializable { private String password; private String accessToken; - private CPU cpu; - private ControllableUnit controlledUnit; private boolean guest = false; @@ -52,7 +49,6 @@ public class User implements MongoSerializable { dbObject.put("username", username); dbObject.put("code", userCode); dbObject.put("controlledUnit", controlledUnit.getObjectId()); - dbObject.put("cpu", cpu.mongoSerialise()); dbObject.put("password", password); dbObject.put("moderator", moderator); dbObject.put("stats", stats.mongoSerialise()); @@ -70,9 +66,6 @@ public class User implements MongoSerializable { user.moderator = (boolean) obj.get("moderator"); user.stats = new UserStats((Document) obj.get("stats")); - user.getControlledUnit().setParent(user); - - user.cpu = CPU.deserialize((Document) obj.get("cpu"), user); return user; } @@ -85,14 +78,6 @@ public class User implements MongoSerializable { this.userCode = userCode; } - public CPU getCpu() { - return cpu; - } - - public void setCpu(CPU cpu) { - this.cpu = cpu; - } - public String getUsername() { return username; } diff --git a/Server/src/main/java/net/simon987/server/user/UserManager.java b/Server/src/main/java/net/simon987/server/user/UserManager.java index ef938a5..d7a4182 100644 --- a/Server/src/main/java/net/simon987/server/user/UserManager.java +++ b/Server/src/main/java/net/simon987/server/user/UserManager.java @@ -76,6 +76,7 @@ public class UserManager { userCollection.insertOne(dbUser); } catch (Exception e) { + e.printStackTrace(); throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage()); } } diff --git a/Server/src/main/java/net/simon987/server/websocket/CodeUploadHandler.java b/Server/src/main/java/net/simon987/server/websocket/CodeUploadHandler.java index 4558cd7..fc8a038 100644 --- a/Server/src/main/java/net/simon987/server/websocket/CodeUploadHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/CodeUploadHandler.java @@ -3,6 +3,7 @@ package net.simon987.server.websocket; import net.simon987.server.GameServer; import net.simon987.server.assembly.Assembler; import net.simon987.server.assembly.AssemblyResult; +import net.simon987.server.assembly.CPU; import net.simon987.server.logging.LogManager; import org.json.simple.JSONObject; @@ -24,17 +25,20 @@ public class CodeUploadHandler implements MessageHandler { user.getUser().setUserCode((String) json.get("code")); if (user.getUser().getUserCode() != null) { - AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(), - user.getUser().getCpu().getRegisterSet(), + + CPU cpu = user.getUser().getControlledUnit().getCpu(); + + AssemblyResult ar = new Assembler(cpu.getInstructionSet(), + cpu.getRegisterSet(), GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode()); - user.getUser().getCpu().getMemory().clear(); + cpu.getMemory().clear(); //Write assembled code to mem char[] assembledCode = ar.getWords(); - user.getUser().getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length); - user.getUser().getCpu().setCodeSectionOffset(ar.getCodeSectionOffset()); + cpu.getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length); + cpu.setCodeSectionOffset(ar.getCodeSectionOffset()); //Clear keyboard buffer if (user.getUser().getControlledUnit() != null && @@ -43,7 +47,7 @@ public class CodeUploadHandler implements MessageHandler { } //Clear registers - user.getUser().getCpu().getRegisterSet().clear(); + cpu.getRegisterSet().clear(); JSONObject response = new JSONObject(); response.put("t", "codeResponse"); diff --git a/Server/src/main/resources/static/js/mar.js b/Server/src/main/resources/static/js/mar.js index 40d0235..6f0ac0d 100644 --- a/Server/src/main/resources/static/js/mar.js +++ b/Server/src/main/resources/static/js/mar.js @@ -253,8 +253,6 @@ var WorldIndicator = (function (_super) { }; return WorldIndicator; }(DebugMessage)); -{ -} var RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio; var RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio; var DEBUG = true; @@ -442,6 +440,20 @@ var Debug = (function () { Debug.saveGame = function () { mar.client.sendDebugCommand({t: "debug", command: "saveGame"}); }; + Debug.popItem = function (objectId) { + mar.client.sendDebugCommand({t: "debug", command: "popItem", objectId: objectId}); + }; + Debug.putItem = function (objectId, item) { + mar.client.sendDebugCommand({t: "debug", command: "putItem", objectId: objectId, item: item}); + }; + Debug.setInventoryPosition = function (objectId, position) { + mar.client.sendDebugCommand({ + t: "debug", + command: "setInventoryPosition", + objectId: objectId, + position: position + }); + }; return Debug; }()); DEBUG = false; diff --git a/Server/src/main/resources/templates/head.vm b/Server/src/main/resources/templates/head.vm index c45daa0..7b32231 100644 --- a/Server/src/main/resources/templates/head.vm +++ b/Server/src/main/resources/templates/head.vm @@ -4,7 +4,6 @@ - $title \ No newline at end of file diff --git a/Server/src/main/typescript/mar.ts b/Server/src/main/typescript/mar.ts index d00181c..ed279dd 100644 --- a/Server/src/main/typescript/mar.ts +++ b/Server/src/main/typescript/mar.ts @@ -1,5 +1,4 @@ -{ -}// Typescript V2.4.1 +// Typescript V2.4.1 let RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio; let RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio; @@ -95,7 +94,6 @@ class Util { case Direction.SOUTH: return 1; } - } static itemColor(item) { @@ -109,7 +107,6 @@ class Util { return config.itemCopper; } - } } @@ -220,10 +217,27 @@ class Debug { mar.client.sendDebugCommand({t: "debug", command: "saveGame"}); } + public static popItem(objectId) { + mar.client.sendDebugCommand({t: "debug", command: "popItem", objectId: objectId}) + } + + public static putItem(objectId, item) { + mar.client.sendDebugCommand({t: "debug", command: "putItem", objectId: objectId, item: item}) + } + + public static setInventoryPosition(objectId, position) { + mar.client.sendDebugCommand({ + t: "debug", + command: "setInventoryPosition", + objectId: objectId, + position: position + }) + } + } -DEBUG = false; // todo remove +DEBUG = false; let mar = new MarGame();