mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-19 18:46:43 +00:00
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:
parent
ee9eeeef55
commit
a7bdbd2513
@ -2,18 +2,24 @@ package net.simon987.cubotplugin;
|
|||||||
|
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.ServerConfiguration;
|
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.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.game.objects.*;
|
||||||
import net.simon987.server.logging.LogManager;
|
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.user.User;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Random;
|
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;
|
private static final char MAP_INFO = 0x0080;
|
||||||
|
|
||||||
@ -62,11 +68,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
*/
|
*/
|
||||||
private int maxShield;
|
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
|
* 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;
|
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
|
* Display mode of the hologram hardware
|
||||||
* <br>TODO: move this inside CubotHologram class
|
* <br>TODO: move this inside CubotHologram class
|
||||||
@ -180,13 +192,26 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
hp = document.getInteger("hp");
|
hp = document.getInteger("hp");
|
||||||
shield = document.getInteger("shield");
|
shield = document.getInteger("shield");
|
||||||
setDirection(Direction.getDirection(document.getInteger("direction")));
|
setDirection(Direction.getDirection(document.getInteger("direction")));
|
||||||
heldItem = document.getInteger("heldItem");
|
|
||||||
energy = document.getInteger("energy");
|
energy = document.getInteger("energy");
|
||||||
|
|
||||||
ServerConfiguration config = GameServer.INSTANCE.getConfig();
|
ServerConfiguration config = GameServer.INSTANCE.getConfig();
|
||||||
maxEnergy = config.getInt("battery_max_energy");
|
maxEnergy = config.getInt("battery_max_energy");
|
||||||
maxHp = config.getInt("cubot_max_hp");
|
maxHp = config.getInt("cubot_max_hp");
|
||||||
maxShield = config.getInt("cubot_max_shield");
|
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
|
@Override
|
||||||
@ -241,6 +266,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
public JSONObject jsonSerialise() {
|
public JSONObject jsonSerialise() {
|
||||||
JSONObject json = super.jsonSerialise();
|
JSONObject json = super.jsonSerialise();
|
||||||
json.put("direction", getDirection().ordinal());
|
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("heldItem", heldItem);
|
||||||
json.put("hp", hp);
|
json.put("hp", hp);
|
||||||
json.put("shield", shield);
|
json.put("shield", shield);
|
||||||
@ -263,7 +290,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
Document dbObject = super.mongoSerialise();
|
Document dbObject = super.mongoSerialise();
|
||||||
|
|
||||||
dbObject.put("direction", getDirection().ordinal());
|
dbObject.put("direction", getDirection().ordinal());
|
||||||
dbObject.put("heldItem", heldItem);
|
|
||||||
dbObject.put("hp", hp);
|
dbObject.put("hp", hp);
|
||||||
dbObject.put("shield", shield);
|
dbObject.put("shield", shield);
|
||||||
dbObject.put("action", lastAction.ordinal());
|
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
|
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;
|
return dbObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -287,7 +327,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
setDead(false);
|
setDead(false);
|
||||||
setHp(maxHp);
|
setHp(maxHp);
|
||||||
setShield(0);
|
setShield(0);
|
||||||
setHeldItem(0);
|
|
||||||
setEnergy(maxEnergy);
|
setEnergy(maxEnergy);
|
||||||
clearKeyboardBuffer();
|
clearKeyboardBuffer();
|
||||||
consoleMessagesBuffer.clear();
|
consoleMessagesBuffer.clear();
|
||||||
@ -300,7 +339,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onDeadCallback() {
|
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();
|
reset();
|
||||||
|
|
||||||
@ -326,14 +366,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeldItem(int heldItem) {
|
|
||||||
this.heldItem = heldItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getHeldItem() {
|
|
||||||
return heldItem;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setKeyboardBuffer(ArrayList<Integer> kbBuffer) {
|
public void setKeyboardBuffer(ArrayList<Integer> kbBuffer) {
|
||||||
keyboardBuffer = kbBuffer;
|
keyboardBuffer = kbBuffer;
|
||||||
@ -352,14 +384,15 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
this.currentAction = currentAction;
|
this.currentAction = currentAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getParent() {
|
|
||||||
return parent;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setParent(User parent) {
|
public void setParent(User parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public User getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
public Action getAction() {
|
public Action getAction() {
|
||||||
return lastAction;
|
return lastAction;
|
||||||
}
|
}
|
||||||
@ -446,7 +479,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
@Override
|
@Override
|
||||||
public Memory getFloppyData() {
|
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) {
|
if (drive.getFloppy() != null) {
|
||||||
return drive.getFloppy().getMemory();
|
return drive.getFloppy().getMemory();
|
||||||
@ -568,4 +602,66 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit, Pr
|
|||||||
setDead(true);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotBattery extends CubotHardware {
|
public class CubotBattery extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final int DEFAULT_ADDRESS = 0x000A;
|
public static final int DEFAULT_ADDRESS = 0x000A;
|
||||||
|
|
||||||
|
@ -3,13 +3,13 @@ package net.simon987.cubotplugin;
|
|||||||
import net.simon987.server.assembly.Status;
|
import net.simon987.server.assembly.Status;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import net.simon987.server.game.objects.GameObject;
|
import net.simon987.server.game.objects.GameObject;
|
||||||
import net.simon987.server.game.objects.Programmable;
|
import net.simon987.server.game.objects.MessageReceiver;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class CubotComPort extends CubotHardware {
|
public class CubotComPort extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final char HWID = 0xD;
|
public static final char HWID = 0xD;
|
||||||
public static final int DEFAULT_ADDRESS = 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
|
//Todo will have to add getGameObjectsBlockingAt to enable Factory
|
||||||
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
|
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();
|
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||||
|
|
||||||
@ -86,9 +86,9 @@ public class CubotComPort extends CubotHardware {
|
|||||||
char[] message = new char[MESSAGE_LENGTH];
|
char[] message = new char[MESSAGE_LENGTH];
|
||||||
System.arraycopy(getCpu().getMemory().getWords(), x, message, 0, 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(
|
getCpu().getRegisterSet().getRegister("B").setValue(
|
||||||
((Programmable) objects.get(0)).sendMessage(message) ? 1 : 0);
|
((MessageReceiver) objects.get(0)).sendMessage(message) ? 1 : 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotCore extends CubotHardware {
|
public class CubotCore extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final int DEFAULT_ADDRESS = 0x000E;
|
public static final int DEFAULT_ADDRESS = 0x000E;
|
||||||
|
|
||||||
|
@ -3,10 +3,9 @@ package net.simon987.cubotplugin;
|
|||||||
import net.simon987.server.assembly.Status;
|
import net.simon987.server.assembly.Status;
|
||||||
import net.simon987.server.game.objects.Action;
|
import net.simon987.server.game.objects.Action;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import net.simon987.server.game.world.TileMap;
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotDrill extends CubotHardware {
|
public class CubotDrill extends CubotHardwareModule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hardware ID (Should be unique)
|
* Hardware ID (Should be unique)
|
||||||
@ -43,17 +42,10 @@ public class CubotDrill extends CubotHardware {
|
|||||||
|
|
||||||
if (cubot.spendEnergy(1400)) {
|
if (cubot.spendEnergy(1400)) {
|
||||||
if (cubot.getCurrentAction() == Action.IDLE) {
|
if (cubot.getCurrentAction() == Action.IDLE) {
|
||||||
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
|
|
||||||
|
|
||||||
if (tile == TileMap.IRON_TILE) {
|
//TODO: Get Tile instance and call onDig()
|
||||||
cubot.setHeldItem(TileMap.ITEM_IRON);
|
//int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
|
||||||
cubot.setCurrentAction(Action.DIGGING);
|
//cubot.setCurrentAction(Action.DIGGING);
|
||||||
|
|
||||||
} else if (tile == TileMap.COPPER_TILE) {
|
|
||||||
cubot.setHeldItem(TileMap.ITEM_COPPER);
|
|
||||||
cubot.setCurrentAction(Action.DIGGING);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotFloppyDrive extends CubotHardware {
|
public class CubotFloppyDrive extends CubotHardwareModule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hardware ID (Should be unique)
|
* Hardware ID (Should be unique)
|
||||||
@ -58,7 +58,7 @@ public class CubotFloppyDrive extends CubotHardware {
|
|||||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||||
int y = getCpu().getRegisterSet().getRegister("Y").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 x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||||
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||||
|
|
||||||
floppyDisk.writeSector(x, cubot.getParent().getCpu().getMemory(), y);
|
floppyDisk.writeSector(x, cubot.getCpu().getMemory(), y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package net.simon987.cubotplugin;
|
package net.simon987.cubotplugin;
|
||||||
|
|
||||||
import net.simon987.server.assembly.CpuHardware;
|
import net.simon987.server.assembly.HardwareModule;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public abstract class CubotHardware extends CpuHardware {
|
public abstract class CubotHardwareModule extends HardwareModule {
|
||||||
|
|
||||||
protected Cubot cubot;
|
protected Cubot cubot;
|
||||||
|
|
||||||
public CubotHardware(Document document, ControllableUnit cubot) {
|
public CubotHardwareModule(Document document, ControllableUnit cubot) {
|
||||||
this.cubot = (Cubot) cubot;
|
this.cubot = (Cubot) cubot;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CubotHardware(Cubot cubot) {
|
public CubotHardwareModule(Cubot cubot) {
|
||||||
this.cubot = cubot;
|
this.cubot = cubot;
|
||||||
}
|
}
|
||||||
|
|
@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotHologram extends CubotHardware {
|
public class CubotHologram extends CubotHardwareModule {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2,10 +2,15 @@ package net.simon987.cubotplugin;
|
|||||||
|
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.assembly.Status;
|
import net.simon987.server.assembly.Status;
|
||||||
|
import net.simon987.server.game.item.Item;
|
||||||
|
import net.simon987.server.game.item.ItemCopper;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
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)
|
* 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_CLEAR = 0;
|
||||||
private static final int INV_POLL = 1;
|
private static final int INV_POLL = 1;
|
||||||
|
|
||||||
|
private int inventorySize = 4;
|
||||||
|
|
||||||
|
private Map<Integer, Item> inventory;
|
||||||
|
private int position = 0;
|
||||||
|
|
||||||
public CubotInventory(Cubot cubot) {
|
public CubotInventory(Cubot cubot) {
|
||||||
super(cubot);
|
super(cubot);
|
||||||
|
|
||||||
|
inventory = new HashMap<>();
|
||||||
|
inventory.put(2, new ItemCopper(new Document()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public CubotInventory(Document document, ControllableUnit cubot) {
|
public CubotInventory(Document document, ControllableUnit cubot) {
|
||||||
super(document, 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
|
@Override
|
||||||
@ -36,18 +82,45 @@ public class CubotInventory extends CubotHardware {
|
|||||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||||
|
|
||||||
if (a == INV_POLL) {
|
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) {
|
} else if (a == INV_CLEAR) {
|
||||||
if (cubot.getHeldItem() == 0x0001) {
|
popItem();
|
||||||
int energy = GameServer.INSTANCE.getConfig().getInt("biomassEnergyValue");
|
|
||||||
cubot.storeEnergy(energy);
|
|
||||||
cubot.setHeldItem(0);
|
|
||||||
|
|
||||||
} else if (cubot.spendEnergy(100)) {
|
|
||||||
cubot.setHeldItem(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotKeyboard extends CubotHardware {
|
public class CubotKeyboard extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final int DEFAULT_ADDRESS = 4;
|
public static final int DEFAULT_ADDRESS = 4;
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.simon987.cubotplugin;
|
package net.simon987.cubotplugin;
|
||||||
|
|
||||||
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.assembly.Status;
|
import net.simon987.server.assembly.Status;
|
||||||
import net.simon987.server.game.objects.*;
|
import net.simon987.server.game.objects.*;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
@ -7,7 +8,7 @@ import org.bson.Document;
|
|||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class CubotLaser extends CubotHardware {
|
public class CubotLaser extends CubotHardwareModule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hardware ID (Should be unique)
|
* Hardware ID (Should be unique)
|
||||||
@ -51,12 +52,12 @@ public class CubotLaser extends CubotHardware {
|
|||||||
if (cubot.getCurrentAction() == Action.IDLE && objects.size() > 0) {
|
if (cubot.getCurrentAction() == Action.IDLE && objects.size() > 0) {
|
||||||
//FIXME: Problem here if more than 1 object
|
//FIXME: Problem here if more than 1 object
|
||||||
if (objects.get(0) instanceof InventoryHolder) {
|
if (objects.get(0) instanceof InventoryHolder) {
|
||||||
|
|
||||||
if (((InventoryHolder) objects.get(0)).canTakeItem(b)) {
|
if (((InventoryHolder) objects.get(0)).canTakeItem(b)) {
|
||||||
if (cubot.spendEnergy(30)) {
|
if (cubot.spendEnergy(30)) {
|
||||||
//Take the item
|
//Take the item
|
||||||
((InventoryHolder) objects.get(0)).takeItem(b);
|
((InventoryHolder) objects.get(0)).takeItem(b);
|
||||||
|
cubot.giveItem(GameServer.INSTANCE.getRegistry().makeItem(b));
|
||||||
cubot.setHeldItem(b);
|
|
||||||
cubot.setCurrentAction(Action.WITHDRAWING);
|
cubot.setCurrentAction(Action.WITHDRAWING);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@ import net.simon987.server.game.objects.ControllableUnit;
|
|||||||
import net.simon987.server.game.objects.Direction;
|
import net.simon987.server.game.objects.Direction;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotLeg extends CubotHardware {
|
public class CubotLeg extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final int DEFAULT_ADDRESS = 1;
|
public static final int DEFAULT_ADDRESS = 1;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import org.bson.Document;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class CubotLidar extends CubotHardware {
|
public class CubotLidar extends CubotHardwareModule {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Hardware ID (Should be unique)
|
* Hardware ID (Should be unique)
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
package net.simon987.cubotplugin;
|
package net.simon987.cubotplugin;
|
||||||
|
|
||||||
import net.simon987.cubotplugin.event.ChargeShieldCommandListener;
|
import net.simon987.cubotplugin.event.*;
|
||||||
import net.simon987.cubotplugin.event.CpuInitialisationListener;
|
|
||||||
import net.simon987.cubotplugin.event.UserCreationListener;
|
|
||||||
import net.simon987.server.ServerConfiguration;
|
import net.simon987.server.ServerConfiguration;
|
||||||
import net.simon987.server.game.objects.GameRegistry;
|
import net.simon987.server.game.objects.GameRegistry;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
@ -15,7 +13,11 @@ public class CubotPlugin extends ServerPlugin {
|
|||||||
public void init(ServerConfiguration config, GameRegistry registry) {
|
public void init(ServerConfiguration config, GameRegistry registry) {
|
||||||
listeners.add(new CpuInitialisationListener());
|
listeners.add(new CpuInitialisationListener());
|
||||||
listeners.add(new UserCreationListener());
|
listeners.add(new UserCreationListener());
|
||||||
|
//Debug commands
|
||||||
listeners.add(new ChargeShieldCommandListener());
|
listeners.add(new ChargeShieldCommandListener());
|
||||||
|
listeners.add(new SetInventoryPosition());
|
||||||
|
listeners.add(new PutItemCommandListener());
|
||||||
|
listeners.add(new PopItemCommandListener());
|
||||||
|
|
||||||
registry.registerGameObject(Cubot.class);
|
registry.registerGameObject(Cubot.class);
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import net.simon987.server.assembly.Status;
|
|||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
public class CubotShield extends CubotHardware {
|
public class CubotShield extends CubotHardwareModule {
|
||||||
|
|
||||||
public static final char DEFAULT_ADDRESS = 0x000F;
|
public static final char DEFAULT_ADDRESS = 0x000F;
|
||||||
|
|
||||||
|
@ -5,7 +5,6 @@ import net.simon987.server.assembly.CPU;
|
|||||||
import net.simon987.server.event.CpuInitialisationEvent;
|
import net.simon987.server.event.CpuInitialisationEvent;
|
||||||
import net.simon987.server.event.GameEvent;
|
import net.simon987.server.event.GameEvent;
|
||||||
import net.simon987.server.event.GameEventListener;
|
import net.simon987.server.event.GameEventListener;
|
||||||
import net.simon987.server.user.User;
|
|
||||||
|
|
||||||
public class CpuInitialisationListener implements GameEventListener {
|
public class CpuInitialisationListener implements GameEventListener {
|
||||||
@Override
|
@Override
|
||||||
@ -17,45 +16,46 @@ public class CpuInitialisationListener implements GameEventListener {
|
|||||||
public void handle(GameEvent event) {
|
public void handle(GameEvent event) {
|
||||||
|
|
||||||
CPU cpu = (CPU) event.getSource();
|
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);
|
legHw.setCpu(cpu);
|
||||||
CubotLaser laserHw = new CubotLaser((Cubot) user.getControlledUnit());
|
CubotLaser laserHw = new CubotLaser(cubot);
|
||||||
laserHw.setCpu(cpu);
|
laserHw.setCpu(cpu);
|
||||||
CubotLidar radarHw = new CubotLidar((Cubot) user.getControlledUnit());
|
CubotLidar radarHw = new CubotLidar(cubot);
|
||||||
radarHw.setCpu(cpu);
|
radarHw.setCpu(cpu);
|
||||||
CubotKeyboard keyboard = new CubotKeyboard((Cubot) user.getControlledUnit());
|
CubotKeyboard keyboard = new CubotKeyboard(cubot);
|
||||||
keyboard.setCpu(cpu);
|
keyboard.setCpu(cpu);
|
||||||
CubotDrill drillHw = new CubotDrill((Cubot) user.getControlledUnit());
|
CubotDrill drillHw = new CubotDrill(cubot);
|
||||||
drillHw.setCpu(cpu);
|
drillHw.setCpu(cpu);
|
||||||
CubotInventory invHw = new CubotInventory((Cubot) user.getControlledUnit());
|
CubotInventory invHw = new CubotInventory(cubot);
|
||||||
invHw.setCpu(cpu);
|
invHw.setCpu(cpu);
|
||||||
CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit());
|
CubotHologram emoteHw = new CubotHologram(cubot);
|
||||||
emoteHw.setCpu(cpu);
|
emoteHw.setCpu(cpu);
|
||||||
CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit());
|
CubotBattery batteryHw = new CubotBattery(cubot);
|
||||||
batteryHw.setCpu(cpu);
|
batteryHw.setCpu(cpu);
|
||||||
CubotFloppyDrive floppyHw = new CubotFloppyDrive((Cubot) user.getControlledUnit());
|
CubotFloppyDrive floppyHw = new CubotFloppyDrive(cubot);
|
||||||
floppyHw.setCpu(cpu);
|
floppyHw.setCpu(cpu);
|
||||||
CubotComPort comPortHw = new CubotComPort((Cubot) user.getControlledUnit());
|
CubotComPort comPortHw = new CubotComPort(cubot);
|
||||||
comPortHw.setCpu(cpu);
|
comPortHw.setCpu(cpu);
|
||||||
CubotCore coreHw = new CubotCore((Cubot) user.getControlledUnit());
|
CubotCore coreHw = new CubotCore(cubot);
|
||||||
coreHw.setCpu(cpu);
|
coreHw.setCpu(cpu);
|
||||||
CubotShield shieldHw = new CubotShield((Cubot) user.getControlledUnit());
|
CubotShield shieldHw = new CubotShield(cubot);
|
||||||
shieldHw.setCpu(cpu);
|
shieldHw.setCpu(cpu);
|
||||||
|
|
||||||
cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
|
cubot.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS);
|
cubot.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(radarHw, CubotLidar.DEFAULT_ADDRESS);
|
cubot.attachHardware(radarHw, CubotLidar.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(keyboard, CubotKeyboard.DEFAULT_ADDRESS);
|
cubot.attachHardware(keyboard, CubotKeyboard.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS);
|
cubot.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
cubot.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
cubot.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS);
|
cubot.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS);
|
cubot.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS);
|
cubot.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(comPortHw, CubotComPort.DEFAULT_ADDRESS);
|
cubot.attachHardware(comPortHw, CubotComPort.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(coreHw, CubotCore.DEFAULT_ADDRESS);
|
cubot.attachHardware(coreHw, CubotCore.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(shieldHw, CubotShield.DEFAULT_ADDRESS);
|
cubot.attachHardware(shieldHw, CubotShield.DEFAULT_ADDRESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,10 @@ import net.simon987.cubotplugin.Cubot;
|
|||||||
import net.simon987.cubotplugin.CubotStatus;
|
import net.simon987.cubotplugin.CubotStatus;
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.ServerConfiguration;
|
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.GameEvent;
|
||||||
import net.simon987.server.event.GameEventListener;
|
import net.simon987.server.event.GameEventListener;
|
||||||
import net.simon987.server.event.UserCreationEvent;
|
import net.simon987.server.event.UserCreationEvent;
|
||||||
@ -45,7 +49,6 @@ public class UserCreationListener implements GameEventListener {
|
|||||||
cubot.getWorld().addObject(cubot);
|
cubot.getWorld().addObject(cubot);
|
||||||
cubot.getWorld().incUpdatable();
|
cubot.getWorld().incUpdatable();
|
||||||
|
|
||||||
cubot.setHeldItem(config.getInt("new_user_item"));
|
|
||||||
cubot.setEnergy(config.getInt("battery_max_energy"));
|
cubot.setEnergy(config.getInt("battery_max_energy"));
|
||||||
cubot.setMaxEnergy(config.getInt("battery_max_energy"));
|
cubot.setMaxEnergy(config.getInt("battery_max_energy"));
|
||||||
|
|
||||||
@ -56,6 +59,27 @@ public class UserCreationListener implements GameEventListener {
|
|||||||
cubot.setParent(user);
|
cubot.setParent(user);
|
||||||
user.setControlledUnit(cubot);
|
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)");
|
LogManager.LOGGER.fine("(Plugin) Handled User creation event (Cubot Plugin)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package net.simon987.mischwplugin;
|
package net.simon987.mischwplugin;
|
||||||
|
|
||||||
import net.simon987.server.GameServer;
|
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.Status;
|
||||||
import net.simon987.server.assembly.Util;
|
import net.simon987.server.assembly.Util;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
@ -10,7 +10,7 @@ import org.bson.Document;
|
|||||||
/**
|
/**
|
||||||
* Hardware to get game time
|
* Hardware to get game time
|
||||||
*/
|
*/
|
||||||
public class Clock extends CpuHardware {
|
public class Clock extends HardwareModule {
|
||||||
|
|
||||||
private static final char HWID = 0x0008;
|
private static final char HWID = 0x0008;
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.simon987.mischwplugin;
|
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.assembly.Status;
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
@ -10,7 +10,7 @@ import java.util.Random;
|
|||||||
/**
|
/**
|
||||||
* Hardware to generate random numbers
|
* Hardware to generate random numbers
|
||||||
*/
|
*/
|
||||||
public class RandomNumberGenerator extends CpuHardware {
|
public class RandomNumberGenerator extends HardwareModule {
|
||||||
|
|
||||||
private static final char HWID = 0x0007;
|
private static final char HWID = 0x0007;
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ public class CpuInitialisationListener implements GameEventListener {
|
|||||||
Clock clock = new Clock();
|
Clock clock = new Clock();
|
||||||
clock.setCpu(cpu);
|
clock.setCpu(cpu);
|
||||||
|
|
||||||
cpu.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS);
|
cubot.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS);
|
||||||
cpu.attachHardware(clock, Clock.DEFAULT_ADDRESS);
|
cubot.attachHardware(clock, Clock.DEFAULT_ADDRESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package net.simon987.npcplugin;
|
package net.simon987.npcplugin;
|
||||||
|
|
||||||
import net.simon987.npcplugin.event.CpuInitialisationListener;
|
import net.simon987.npcplugin.event.CpuInitialisationListener;
|
||||||
|
import net.simon987.npcplugin.event.VaultCompleteListener;
|
||||||
import net.simon987.npcplugin.event.VaultWorldUpdateListener;
|
import net.simon987.npcplugin.event.VaultWorldUpdateListener;
|
||||||
import net.simon987.npcplugin.event.WorldCreationListener;
|
import net.simon987.npcplugin.event.WorldCreationListener;
|
||||||
import net.simon987.server.ServerConfiguration;
|
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 WorldCreationListener(configuration.getInt("factory_spawn_rate")));
|
||||||
listeners.add(new CpuInitialisationListener());
|
listeners.add(new CpuInitialisationListener());
|
||||||
listeners.add(new VaultWorldUpdateListener(configuration));
|
listeners.add(new VaultWorldUpdateListener(configuration));
|
||||||
|
listeners.add(new VaultCompleteListener());
|
||||||
|
|
||||||
registry.registerGameObject(HarvesterNPC.class);
|
registry.registerGameObject(HarvesterNPC.class);
|
||||||
registry.registerGameObject(Factory.class);
|
registry.registerGameObject(Factory.class);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package net.simon987.npcplugin;
|
package net.simon987.npcplugin;
|
||||||
|
|
||||||
import net.simon987.server.GameServer;
|
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.Status;
|
||||||
import net.simon987.server.assembly.Util;
|
import net.simon987.server.assembly.Util;
|
||||||
import net.simon987.server.game.objects.Action;
|
import net.simon987.server.game.objects.Action;
|
||||||
@ -10,7 +10,7 @@ import org.bson.Document;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class RadioReceiverHardware extends CpuHardware {
|
public class RadioReceiverHardware extends HardwareModule {
|
||||||
|
|
||||||
public static final char HWID = 0xC; //12
|
public static final char HWID = 0xC; //12
|
||||||
|
|
||||||
|
@ -1,14 +1,14 @@
|
|||||||
package net.simon987.npcplugin;
|
package net.simon987.npcplugin;
|
||||||
|
|
||||||
import net.simon987.server.GameServer;
|
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.Structure;
|
||||||
import net.simon987.server.game.objects.Updatable;
|
import net.simon987.server.game.objects.Updatable;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
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;
|
private static final int MAP_INFO = 0x1000;
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ import org.bson.Document;
|
|||||||
import java.util.Arrays;
|
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;
|
private static final int MAP_INFO = 0x0800;
|
||||||
|
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
package net.simon987.npcplugin;
|
package net.simon987.npcplugin;
|
||||||
|
|
||||||
import net.simon987.server.game.objects.ControllableUnit;
|
|
||||||
import net.simon987.server.game.objects.GameObject;
|
import net.simon987.server.game.objects.GameObject;
|
||||||
import net.simon987.server.game.world.Location;
|
import net.simon987.server.game.world.Location;
|
||||||
import net.simon987.server.logging.LogManager;
|
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -31,12 +29,8 @@ public class VaultExitPortal extends Portal {
|
|||||||
@Override
|
@Override
|
||||||
public boolean enter(GameObject object) {
|
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);
|
return super.enter(object);
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import net.simon987.server.assembly.CPU;
|
|||||||
import net.simon987.server.event.CpuInitialisationEvent;
|
import net.simon987.server.event.CpuInitialisationEvent;
|
||||||
import net.simon987.server.event.GameEvent;
|
import net.simon987.server.event.GameEvent;
|
||||||
import net.simon987.server.event.GameEventListener;
|
import net.simon987.server.event.GameEventListener;
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
|
|
||||||
public class CpuInitialisationListener implements GameEventListener {
|
public class CpuInitialisationListener implements GameEventListener {
|
||||||
@Override
|
@Override
|
||||||
@ -17,11 +17,12 @@ public class CpuInitialisationListener implements GameEventListener {
|
|||||||
@Override
|
@Override
|
||||||
public void handle(GameEvent event) {
|
public void handle(GameEvent event) {
|
||||||
CPU cpu = (CPU) event.getSource();
|
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);
|
radioHw.setCpu(cpu);
|
||||||
|
|
||||||
cpu.attachHardware(radioHw, RadioReceiverHardware.DEFAULT_ADDRESS);
|
cubot.attachHardware(radioHw, RadioReceiverHardware.DEFAULT_ADDRESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
package net.simon987.biomassplugin;
|
package net.simon987.biomassplugin;
|
||||||
|
|
||||||
|
import net.simon987.server.game.item.Item;
|
||||||
import net.simon987.server.game.objects.GameObject;
|
import net.simon987.server.game.objects.GameObject;
|
||||||
import net.simon987.server.game.objects.InventoryHolder;
|
import net.simon987.server.game.objects.InventoryHolder;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
@ -13,12 +14,6 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
|
|||||||
* Yield of the blob, in biomass units
|
* Yield of the blob, in biomass units
|
||||||
*/
|
*/
|
||||||
private int biomassCount;
|
private int biomassCount;
|
||||||
/**
|
|
||||||
* Style of the blob (Only visual)
|
|
||||||
*/
|
|
||||||
// private int style;
|
|
||||||
|
|
||||||
private static final int ITM_BIOMASS = 1;
|
|
||||||
|
|
||||||
public BiomassBlob() {
|
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
|
* Called when an object attempts to place an item in this BiomassBlob
|
||||||
*
|
*
|
||||||
* @param item item id (see MarConstants.ITEM_*)
|
|
||||||
* @return Always returns false
|
* @return Always returns false
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean placeItem(int item) {
|
public boolean placeItem(Item item) {
|
||||||
//Why would you want to place an item in a blob?
|
//Why would you want to place an item in a blob?
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canTakeItem(int item) {
|
public boolean canTakeItem(int itemId) {
|
||||||
return item == ITM_BIOMASS && biomassCount >= 1;
|
return itemId == ItemBiomass.ID && biomassCount >= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when an object attempts to take an item from this BiomassBlob.
|
* 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 the object requests biomass, it will be subtracted from biomassCount, and
|
||||||
* if it reaches 0, the plant is deleted
|
* if it reaches 0, the plant is deleted
|
||||||
*
|
|
||||||
* @param item item id (see MarConstants.ITEM_*)
|
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void takeItem(int item) {
|
public void takeItem(int itemId) {
|
||||||
|
|
||||||
if (item == ITM_BIOMASS) {
|
if (itemId == ItemBiomass.ID) {
|
||||||
if (biomassCount > 1) {
|
if (biomassCount > 1) {
|
||||||
biomassCount--;
|
biomassCount--;
|
||||||
} else {
|
} else {
|
||||||
@ -98,6 +90,5 @@ public class BiomassBlob extends GameObject implements InventoryHolder {
|
|||||||
setDead(true);
|
setDead(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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");
|
LogManager.LOGGER.severe("(BiomassPlugin) NPC plugin is not loaded so biomass will not spawn on death of HarvesterNPC");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
registry.registerGameObject(BiomassBlob.class);
|
registry.registerGameObject(BiomassBlob.class);
|
||||||
|
registry.registerItem(ItemBiomass.ID, ItemBiomass.class);
|
||||||
|
|
||||||
LogManager.LOGGER.info("(BiomassPlugin) Initialised Biomass plugin");
|
LogManager.LOGGER.info("(BiomassPlugin) Initialised Biomass plugin");
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,8 @@ import net.simon987.server.event.GameEventDispatcher;
|
|||||||
import net.simon987.server.event.TickEvent;
|
import net.simon987.server.event.TickEvent;
|
||||||
import net.simon987.server.game.GameUniverse;
|
import net.simon987.server.game.GameUniverse;
|
||||||
import net.simon987.server.game.debug.*;
|
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.objects.GameRegistry;
|
||||||
import net.simon987.server.game.world.DayNightCycle;
|
import net.simon987.server.game.world.DayNightCycle;
|
||||||
import net.simon987.server.game.world.World;
|
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 DamageObjCommandListener());
|
||||||
eventDispatcher.getListeners().add(new SetEnergyCommandListener());
|
eventDispatcher.getListeners().add(new SetEnergyCommandListener());
|
||||||
eventDispatcher.getListeners().add(new SaveGameCommandListener());
|
eventDispatcher.getListeners().add(new SaveGameCommandListener());
|
||||||
|
|
||||||
|
gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class);
|
||||||
|
gameRegistry.registerItem(ItemIron.ID, ItemIron.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GameUniverse getGameUniverse() {
|
public GameUniverse getGameUniverse() {
|
||||||
@ -163,13 +168,13 @@ public class GameServer implements Runnable {
|
|||||||
//Process user code
|
//Process user code
|
||||||
for (User user : gameUniverse.getUsers()) {
|
for (User user : gameUniverse.getUsers()) {
|
||||||
|
|
||||||
if (user.getCpu() != null) {
|
if (user.getControlledUnit() != null && user.getControlledUnit().getCpu() != null) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
int timeout = Math.min(user.getControlledUnit().getEnergy(), maxExecutionTime);
|
int timeout = Math.min(user.getControlledUnit().getEnergy(), maxExecutionTime);
|
||||||
|
|
||||||
user.getCpu().reset();
|
user.getControlledUnit().getCpu().reset();
|
||||||
int cost = user.getCpu().execute(timeout);
|
int cost = user.getControlledUnit().getCpu().execute(timeout);
|
||||||
user.getControlledUnit().spendEnergy(cost);
|
user.getControlledUnit().spendEnergy(cost);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -6,15 +6,12 @@ import net.simon987.server.assembly.exception.CancelledException;
|
|||||||
import net.simon987.server.assembly.instruction.*;
|
import net.simon987.server.assembly.instruction.*;
|
||||||
import net.simon987.server.event.CpuInitialisationEvent;
|
import net.simon987.server.event.CpuInitialisationEvent;
|
||||||
import net.simon987.server.event.GameEvent;
|
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.io.MongoSerializable;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
import net.simon987.server.user.User;
|
|
||||||
import org.bson.Document;
|
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
|
* 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
|
* 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;
|
private int ip;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* List of attached hardware, 'modules'
|
* Hardware is connected to the hardwareHost
|
||||||
*/
|
*/
|
||||||
private HashMap<Integer, CpuHardware> attachedHardware;
|
private HardwareHost hardwareHost;
|
||||||
|
|
||||||
|
|
||||||
private ServerConfiguration config;
|
private ServerConfiguration config;
|
||||||
|
|
||||||
@ -68,11 +66,10 @@ public class CPU implements MongoSerializable {
|
|||||||
/**
|
/**
|
||||||
* Creates a new CPU
|
* Creates a new CPU
|
||||||
*/
|
*/
|
||||||
public CPU(ServerConfiguration config, User user) throws CancelledException {
|
public CPU(ServerConfiguration config, ControllableUnit unit) throws CancelledException {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
instructionSet = new DefaultInstructionSet();
|
instructionSet = new DefaultInstructionSet();
|
||||||
registerSet = new DefaultRegisterSet();
|
registerSet = new DefaultRegisterSet();
|
||||||
attachedHardware = new HashMap<>();
|
|
||||||
codeSectionOffset = config.getInt("org_offset");
|
codeSectionOffset = config.getInt("org_offset");
|
||||||
|
|
||||||
instructionSet.add(new JmpInstruction(this));
|
instructionSet.add(new JmpInstruction(this));
|
||||||
@ -105,7 +102,7 @@ public class CPU implements MongoSerializable {
|
|||||||
status = new Status();
|
status = new Status();
|
||||||
memory = new Memory(config.getInt("memory_size"));
|
memory = new Memory(config.getInt("memory_size"));
|
||||||
|
|
||||||
GameEvent event = new CpuInitialisationEvent(this, user);
|
GameEvent event = new CpuInitialisationEvent(this, unit);
|
||||||
GameServer.INSTANCE.getEventDispatcher().dispatch(event);
|
GameServer.INSTANCE.getEventDispatcher().dispatch(event);
|
||||||
if (event.isCancelled()) {
|
if (event.isCancelled()) {
|
||||||
throw new CancelledException();
|
throw new CancelledException();
|
||||||
@ -355,36 +352,17 @@ public class CPU implements MongoSerializable {
|
|||||||
dbObject.put("registerSet", registerSet.mongoSerialise());
|
dbObject.put("registerSet", registerSet.mongoSerialise());
|
||||||
dbObject.put("codeSegmentOffset", codeSectionOffset);
|
dbObject.put("codeSegmentOffset", codeSectionOffset);
|
||||||
|
|
||||||
List<Document> 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;
|
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");
|
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.memory = new Memory((Document) obj.get("memory"));
|
||||||
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
|
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
|
||||||
@ -421,56 +399,21 @@ public class CPU implements MongoSerializable {
|
|||||||
this.codeSectionOffset = codeSectionOffset;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
||||||
String str = "------------------------\n";
|
String str = registerSet.toString();
|
||||||
str += registerSet.toString();
|
|
||||||
str += status.toString();
|
str += status.toString();
|
||||||
str += "HW: ";
|
|
||||||
for (CpuHardware hw : attachedHardware.values()) {
|
|
||||||
str += hw + ", ";
|
|
||||||
}
|
|
||||||
str += "\n------------------------\n";
|
|
||||||
|
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CpuHardware getHardware(int address) {
|
public HardwareHost getHardwareHost() {
|
||||||
|
return hardwareHost;
|
||||||
return attachedHardware.get(address);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setHardwareHost(HardwareHost hardwareHost) {
|
||||||
|
this.hardwareHost = hardwareHost;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,15 @@ import net.simon987.server.io.MongoSerializable;
|
|||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
|
|
||||||
public abstract class CpuHardware implements MongoSerializable {
|
public abstract class HardwareModule implements MongoSerializable {
|
||||||
|
|
||||||
private CPU cpu;
|
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 abstract void handleInterrupt(Status status);
|
||||||
|
|
||||||
public CPU getCpu() {
|
protected CPU getCpu() {
|
||||||
return cpu;
|
return cpu;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,6 +35,6 @@ public abstract class CpuHardware implements MongoSerializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return String.format("<%04X>", (int) getId());
|
return String.format("{%s}", getClass().getSimpleName());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,7 +23,7 @@ public class HwiInstruction extends Instruction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Status execute(Target src, int srcIndex, Status status) {
|
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;
|
return status;
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ public class HwiInstruction extends Instruction {
|
|||||||
@Override
|
@Override
|
||||||
public Status execute(int src, Status status) {
|
public Status execute(int src, Status status) {
|
||||||
|
|
||||||
status.setErrorFlag(cpu.hardwareInterrupt(src));
|
status.setErrorFlag(cubot.hardwareInterrupt(src, cpu.getStatus()));
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
@ -18,14 +18,14 @@ public class HwqInstruction extends Instruction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Status execute(Target src, int srcIndex, Status status) {
|
public Status execute(Target src, int srcIndex, Status status) {
|
||||||
cpu.hardwareQuery(src.get(srcIndex));
|
cubot.hardwareQuery(src.get(srcIndex));
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Status execute(int src, Status status) {
|
public Status execute(int src, Status status) {
|
||||||
cpu.hardwareQuery(src);
|
cubot.hardwareQuery(src);
|
||||||
|
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,18 @@
|
|||||||
package net.simon987.server.event;
|
package net.simon987.server.event;
|
||||||
|
|
||||||
import net.simon987.server.assembly.CPU;
|
import net.simon987.server.assembly.CPU;
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.game.objects.ControllableUnit;
|
||||||
|
|
||||||
public class CpuInitialisationEvent extends GameEvent {
|
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);
|
setSource(cpu);
|
||||||
this.user = user;
|
this.unit = unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User getUser() {
|
public ControllableUnit getUnit() {
|
||||||
return user;
|
return unit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,6 @@ import com.mongodb.client.MongoCursor;
|
|||||||
import com.mongodb.client.MongoDatabase;
|
import com.mongodb.client.MongoDatabase;
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.ServerConfiguration;
|
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.assembly.exception.CancelledException;
|
||||||
import net.simon987.server.game.objects.GameObject;
|
import net.simon987.server.game.objects.GameObject;
|
||||||
import net.simon987.server.game.world.World;
|
import net.simon987.server.game.world.World;
|
||||||
@ -190,20 +187,6 @@ public class GameUniverse {
|
|||||||
try {
|
try {
|
||||||
if (makeControlledUnit) {
|
if (makeControlledUnit) {
|
||||||
user = new User();
|
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 {
|
} else {
|
||||||
|
@ -5,7 +5,7 @@ import net.simon987.server.event.DebugCommandEvent;
|
|||||||
import net.simon987.server.event.GameEvent;
|
import net.simon987.server.event.GameEvent;
|
||||||
import net.simon987.server.event.GameEventListener;
|
import net.simon987.server.event.GameEventListener;
|
||||||
import net.simon987.server.game.objects.GameObject;
|
import net.simon987.server.game.objects.GameObject;
|
||||||
import net.simon987.server.game.objects.Programmable;
|
import net.simon987.server.game.objects.MessageReceiver;
|
||||||
|
|
||||||
public class ComPortMsgCommandListener implements GameEventListener {
|
public class ComPortMsgCommandListener implements GameEventListener {
|
||||||
|
|
||||||
@ -27,12 +27,12 @@ public class ComPortMsgCommandListener implements GameEventListener {
|
|||||||
|
|
||||||
if (object != null) {
|
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 {
|
} else {
|
||||||
e.reply("Object " + objectId + " not Programmable");
|
e.reply("Object " + objectId + " not MessageReceiver");
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -32,7 +32,7 @@ public class UserInfoCommandListener implements GameEventListener {
|
|||||||
ControllableUnit unit = user.getControlledUnit();
|
ControllableUnit unit = user.getControlledUnit();
|
||||||
message += "ControlledUnit: " + unit.getObjectId() + " at (" + unit.getX() + ", " + unit.getY() + ")\n";
|
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();
|
message += "Code: " + user.getUserCode();
|
||||||
|
|
||||||
e.reply(message);
|
e.reply(message);
|
||||||
|
54
Server/src/main/java/net/simon987/server/game/item/Item.java
Normal file
54
Server/src/main/java/net/simon987/server/game/item/Item.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,8 @@
|
|||||||
package net.simon987.server.game.objects;
|
package net.simon987.server.game.objects;
|
||||||
|
|
||||||
|
import net.simon987.server.assembly.CPU;
|
||||||
import net.simon987.server.assembly.Memory;
|
import net.simon987.server.assembly.Memory;
|
||||||
|
import net.simon987.server.game.item.Item;
|
||||||
import net.simon987.server.game.world.World;
|
import net.simon987.server.game.world.World;
|
||||||
import net.simon987.server.user.User;
|
import net.simon987.server.user.User;
|
||||||
|
|
||||||
@ -35,4 +37,8 @@ public interface ControllableUnit {
|
|||||||
ArrayList<char[]> getConsoleMessagesBuffer();
|
ArrayList<char[]> getConsoleMessagesBuffer();
|
||||||
|
|
||||||
int getConsoleMode();
|
int getConsoleMode();
|
||||||
|
|
||||||
|
CPU getCpu();
|
||||||
|
|
||||||
|
void giveItem(Item item);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package net.simon987.server.game.objects;
|
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 net.simon987.server.logging.LogManager;
|
||||||
import org.bson.Document;
|
import org.bson.Document;
|
||||||
|
|
||||||
@ -10,24 +11,29 @@ import java.util.HashMap;
|
|||||||
public class GameRegistry {
|
public class GameRegistry {
|
||||||
|
|
||||||
private HashMap<String, Class<? extends GameObject>> gameObjects;
|
private HashMap<String, Class<? extends GameObject>> gameObjects;
|
||||||
private HashMap<String, Class<? extends CpuHardware>> hardware;
|
private HashMap<String, Class<? extends HardwareModule>> hardware;
|
||||||
|
private HashMap<Integer, Class<? extends Item>> items;
|
||||||
|
|
||||||
|
|
||||||
public GameRegistry() {
|
public GameRegistry() {
|
||||||
gameObjects = new HashMap<>();
|
gameObjects = new HashMap<>();
|
||||||
hardware = new HashMap<>();
|
hardware = new HashMap<>();
|
||||||
|
items = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerGameObject(Class<? extends GameObject> clazz) {
|
public void registerGameObject(Class<? extends GameObject> clazz) {
|
||||||
gameObjects.put(clazz.getCanonicalName(), clazz);
|
gameObjects.put(clazz.getCanonicalName(), clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerHardware(Class<? extends CpuHardware> clazz) {
|
public void registerHardware(Class<? extends HardwareModule> clazz) {
|
||||||
hardware.put(clazz.getCanonicalName(), clazz);
|
hardware.put(clazz.getCanonicalName(), clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void registerItem(int id, Class<? extends Item> clazz) {
|
||||||
|
items.put(id, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
public CpuHardware deserializeHardware(Document document, ControllableUnit controllableUnit) {
|
public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) {
|
||||||
String type = document.getString("type");
|
String type = document.getString("type");
|
||||||
|
|
||||||
if (hardware.containsKey(type)) {
|
if (hardware.containsKey(type)) {
|
||||||
@ -40,7 +46,7 @@ public class GameRegistry {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type);
|
LogManager.LOGGER.severe("Trying to deserialize unknown HardwareModule type: " + type);
|
||||||
return null;
|
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) {
|
public boolean isGameObjectRegistered(String type) {
|
||||||
return gameObjects.containsKey(type);
|
return gameObjects.containsKey(type);
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -1,25 +1,23 @@
|
|||||||
package net.simon987.server.game.objects;
|
package net.simon987.server.game.objects;
|
||||||
|
|
||||||
|
|
||||||
|
import net.simon987.server.game.item.Item;
|
||||||
|
|
||||||
public interface InventoryHolder {
|
public interface InventoryHolder {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Place an item into the inventory
|
* 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
|
* 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
|
* @return true if the InventoryHolder can provide this item
|
||||||
*/
|
*/
|
||||||
boolean canTakeItem(int item);
|
boolean canTakeItem(int itemId);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package net.simon987.server.game.objects;
|
package net.simon987.server.game.objects;
|
||||||
|
|
||||||
public interface Programmable {
|
public interface MessageReceiver {
|
||||||
|
|
||||||
boolean sendMessage(char[] message);
|
boolean sendMessage(char[] message);
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -298,7 +298,7 @@ public class World implements MongoSerializable {
|
|||||||
* and will be able to leave the World
|
* and will be able to leave the World
|
||||||
* <br>
|
* <br>
|
||||||
* Note: This function is quite expensive and shouldn't be used
|
* 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
|
* @return random non-blocked tile
|
||||||
*/
|
*/
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package net.simon987.server.user;
|
package net.simon987.server.user;
|
||||||
|
|
||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.assembly.CPU;
|
|
||||||
import net.simon987.server.assembly.exception.CancelledException;
|
import net.simon987.server.assembly.exception.CancelledException;
|
||||||
import net.simon987.server.event.GameEvent;
|
import net.simon987.server.event.GameEvent;
|
||||||
import net.simon987.server.event.UserCreationEvent;
|
import net.simon987.server.event.UserCreationEvent;
|
||||||
@ -20,8 +19,6 @@ public class User implements MongoSerializable {
|
|||||||
private String password;
|
private String password;
|
||||||
private String accessToken;
|
private String accessToken;
|
||||||
|
|
||||||
private CPU cpu;
|
|
||||||
|
|
||||||
private ControllableUnit controlledUnit;
|
private ControllableUnit controlledUnit;
|
||||||
|
|
||||||
private boolean guest = false;
|
private boolean guest = false;
|
||||||
@ -52,7 +49,6 @@ public class User implements MongoSerializable {
|
|||||||
dbObject.put("username", username);
|
dbObject.put("username", username);
|
||||||
dbObject.put("code", userCode);
|
dbObject.put("code", userCode);
|
||||||
dbObject.put("controlledUnit", controlledUnit.getObjectId());
|
dbObject.put("controlledUnit", controlledUnit.getObjectId());
|
||||||
dbObject.put("cpu", cpu.mongoSerialise());
|
|
||||||
dbObject.put("password", password);
|
dbObject.put("password", password);
|
||||||
dbObject.put("moderator", moderator);
|
dbObject.put("moderator", moderator);
|
||||||
dbObject.put("stats", stats.mongoSerialise());
|
dbObject.put("stats", stats.mongoSerialise());
|
||||||
@ -70,9 +66,6 @@ public class User implements MongoSerializable {
|
|||||||
user.moderator = (boolean) obj.get("moderator");
|
user.moderator = (boolean) obj.get("moderator");
|
||||||
user.stats = new UserStats((Document) obj.get("stats"));
|
user.stats = new UserStats((Document) obj.get("stats"));
|
||||||
|
|
||||||
user.getControlledUnit().setParent(user);
|
|
||||||
|
|
||||||
user.cpu = CPU.deserialize((Document) obj.get("cpu"), user);
|
|
||||||
|
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
@ -85,14 +78,6 @@ public class User implements MongoSerializable {
|
|||||||
this.userCode = userCode;
|
this.userCode = userCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CPU getCpu() {
|
|
||||||
return cpu;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCpu(CPU cpu) {
|
|
||||||
this.cpu = cpu;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
@ -76,6 +76,7 @@ public class UserManager {
|
|||||||
|
|
||||||
userCollection.insertOne(dbUser);
|
userCollection.insertOne(dbUser);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage());
|
throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package net.simon987.server.websocket;
|
|||||||
import net.simon987.server.GameServer;
|
import net.simon987.server.GameServer;
|
||||||
import net.simon987.server.assembly.Assembler;
|
import net.simon987.server.assembly.Assembler;
|
||||||
import net.simon987.server.assembly.AssemblyResult;
|
import net.simon987.server.assembly.AssemblyResult;
|
||||||
|
import net.simon987.server.assembly.CPU;
|
||||||
import net.simon987.server.logging.LogManager;
|
import net.simon987.server.logging.LogManager;
|
||||||
import org.json.simple.JSONObject;
|
import org.json.simple.JSONObject;
|
||||||
|
|
||||||
@ -24,17 +25,20 @@ public class CodeUploadHandler implements MessageHandler {
|
|||||||
user.getUser().setUserCode((String) json.get("code"));
|
user.getUser().setUserCode((String) json.get("code"));
|
||||||
|
|
||||||
if (user.getUser().getUserCode() != null) {
|
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());
|
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
|
||||||
|
|
||||||
user.getUser().getCpu().getMemory().clear();
|
cpu.getMemory().clear();
|
||||||
|
|
||||||
//Write assembled code to mem
|
//Write assembled code to mem
|
||||||
char[] assembledCode = ar.getWords();
|
char[] assembledCode = ar.getWords();
|
||||||
|
|
||||||
user.getUser().getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
cpu.getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||||
user.getUser().getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
|
cpu.setCodeSectionOffset(ar.getCodeSectionOffset());
|
||||||
|
|
||||||
//Clear keyboard buffer
|
//Clear keyboard buffer
|
||||||
if (user.getUser().getControlledUnit() != null &&
|
if (user.getUser().getControlledUnit() != null &&
|
||||||
@ -43,7 +47,7 @@ public class CodeUploadHandler implements MessageHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//Clear registers
|
//Clear registers
|
||||||
user.getUser().getCpu().getRegisterSet().clear();
|
cpu.getRegisterSet().clear();
|
||||||
|
|
||||||
JSONObject response = new JSONObject();
|
JSONObject response = new JSONObject();
|
||||||
response.put("t", "codeResponse");
|
response.put("t", "codeResponse");
|
||||||
|
16
Server/src/main/resources/static/js/mar.js
vendored
16
Server/src/main/resources/static/js/mar.js
vendored
@ -253,8 +253,6 @@ var WorldIndicator = (function (_super) {
|
|||||||
};
|
};
|
||||||
return WorldIndicator;
|
return WorldIndicator;
|
||||||
}(DebugMessage));
|
}(DebugMessage));
|
||||||
{
|
|
||||||
}
|
|
||||||
var RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
var RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
||||||
var RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
var RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
||||||
var DEBUG = true;
|
var DEBUG = true;
|
||||||
@ -442,6 +440,20 @@ var Debug = (function () {
|
|||||||
Debug.saveGame = function () {
|
Debug.saveGame = function () {
|
||||||
mar.client.sendDebugCommand({t: "debug", command: "saveGame"});
|
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;
|
return Debug;
|
||||||
}());
|
}());
|
||||||
DEBUG = false;
|
DEBUG = false;
|
||||||
|
@ -4,7 +4,6 @@
|
|||||||
<link rel="stylesheet" href="css/bootstrap-reboot.min.css">
|
<link rel="stylesheet" href="css/bootstrap-reboot.min.css">
|
||||||
<link rel="stylesheet" href="css/bootstrap4-neon-glow.min.css">
|
<link rel="stylesheet" href="css/bootstrap4-neon-glow.min.css">
|
||||||
<link rel="stylesheet" href="css/mar.css">
|
<link rel="stylesheet" href="css/mar.css">
|
||||||
<link rel="stylesheet" href="css/console.css">
|
|
||||||
|
|
||||||
<title>$title</title>
|
<title>$title</title>
|
||||||
</head>
|
</head>
|
@ -1,5 +1,4 @@
|
|||||||
{
|
// Typescript V2.4.1
|
||||||
}// Typescript V2.4.1
|
|
||||||
|
|
||||||
let RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
let RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
||||||
let RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
let RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
||||||
@ -95,7 +94,6 @@ class Util {
|
|||||||
case Direction.SOUTH:
|
case Direction.SOUTH:
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static itemColor(item) {
|
static itemColor(item) {
|
||||||
@ -109,7 +107,6 @@ class Util {
|
|||||||
return config.itemCopper;
|
return config.itemCopper;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -220,10 +217,27 @@ class Debug {
|
|||||||
mar.client.sendDebugCommand({t: "debug", command: "saveGame"});
|
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();
|
let mar = new MarGame();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user