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

This commit is contained in:
Simon
2018-05-31 22:13:07 -04:00
parent ee9eeeef55
commit a7bdbd2513
60 changed files with 858 additions and 295 deletions

View File

@@ -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<Integer, HardwareModule> hardwareAddresses = new HashMap<>();
private Map<Class<? extends HardwareModule>, Integer> hardwareModules = new HashMap<>();
/**
* Cubot's brain box
*/
private CPU cpu;
/**
* Display mode of the hologram hardware
* <br>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<Document> 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<Integer> 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<? extends HardwareModule> toRemove = null;
for (Class<? extends HardwareModule> 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<? extends HardwareModule> 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);
}
}

View File

@@ -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;

View File

@@ -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<GameObject> 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;
}
}

View File

@@ -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;

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -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 {
/**

View File

@@ -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<Integer, Item> 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<String, Document>) document.get("inventory")).keySet()) {
inventory.put(Integer.valueOf(i),
GameServer.INSTANCE.getRegistry().deserializeItem(((Map<String, Document>) 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<Integer, Item> 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);
}
}

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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)

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
}
}

View File

@@ -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"));
}
}
}
}

View File

@@ -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"));
}
}
}
}

View File

@@ -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"));
}
}
}
}

View File

@@ -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)");
}
}