mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-14 07:09:04 +00:00
Added maven framework support. Started working on NPCs #19
This commit is contained in:
196
Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java
Normal file
196
Plugin Cubot/src/main/java/net/simon987/cubotplugin/Cubot.java
Normal file
@@ -0,0 +1,196 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.game.*;
|
||||
import net.simon987.server.user.User;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Cubot extends GameObject implements Updatable, ControllableUnit {
|
||||
|
||||
private static final char MAP_INFO = 0x0080;
|
||||
public static final int ID = 1;
|
||||
|
||||
private char hologram = 0;
|
||||
private char lastHologram = 0;
|
||||
|
||||
/**
|
||||
* Hit points
|
||||
*/
|
||||
private int hp;
|
||||
private int heldItem;
|
||||
|
||||
private Action currentAction = Action.IDLE;
|
||||
private Action lastAction = Action.IDLE;
|
||||
|
||||
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
|
||||
|
||||
private FloppyDisk floppyDisk;
|
||||
|
||||
private User parent;
|
||||
|
||||
private int energy;
|
||||
private int maxEnergy;
|
||||
|
||||
public Cubot() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getMapInfo() {
|
||||
return MAP_INFO;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
|
||||
if (currentAction == Action.WALKING) {
|
||||
if (spendEnergy(100)) {
|
||||
if (!incrementLocation()) {
|
||||
//Couldn't walk
|
||||
currentAction = Action.IDLE;
|
||||
}
|
||||
} else {
|
||||
currentAction = Action.IDLE;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* CurrentAction is set during the code execution and this function is called right after
|
||||
* If no action as been set, the action sent to the client is the action in currentAction that
|
||||
* was set last tick (IDLE)
|
||||
*/
|
||||
lastAction = currentAction;
|
||||
currentAction = Action.IDLE;
|
||||
|
||||
//Same principle for hologram
|
||||
lastHologram = hologram;
|
||||
hologram = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("id", getObjectId());
|
||||
json.put("type", ID);
|
||||
json.put("x", getX());
|
||||
json.put("y", getY());
|
||||
json.put("direction", getDirection().ordinal());
|
||||
json.put("heldItem", heldItem);
|
||||
json.put("hp", hp);
|
||||
json.put("action", lastAction.ordinal());
|
||||
json.put("holo", (int) lastHologram);
|
||||
json.put("energy", energy);
|
||||
|
||||
if (parent != null) {
|
||||
json.put("parent", parent.getUsername()); //Only used client-side for now
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static Cubot deserialize(JSONObject json) {
|
||||
|
||||
Cubot cubot = new Cubot();
|
||||
cubot.setObjectId((int) (long) json.get("id"));
|
||||
cubot.setX((int) (long) json.get("x"));
|
||||
cubot.setY((int) (long) json.get("y"));
|
||||
cubot.hp = (int) (long) json.get("hp");
|
||||
cubot.setDirection(Direction.getDirection((int) (long) json.get("direction")));
|
||||
cubot.heldItem = (int) (long) json.get("heldItem");
|
||||
cubot.energy = (int) (long) json.get("energy");
|
||||
cubot.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
|
||||
|
||||
return cubot;
|
||||
|
||||
}
|
||||
|
||||
public void setHeldItem(int heldItem) {
|
||||
this.heldItem = heldItem;
|
||||
}
|
||||
|
||||
public int getHeldItem() {
|
||||
return heldItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setKeyboardBuffer(ArrayList<Integer> kbBuffer) {
|
||||
keyboardBuffer = kbBuffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ArrayList<Integer> getKeyboardBuffer() {
|
||||
return keyboardBuffer;
|
||||
}
|
||||
|
||||
public void clearKeyboardBuffer() {
|
||||
keyboardBuffer.clear();
|
||||
}
|
||||
|
||||
public void setCurrentAction(Action currentAction) {
|
||||
this.currentAction = currentAction;
|
||||
}
|
||||
|
||||
public User getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public void setParent(User parent) {
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
public Action getAction() {
|
||||
return lastAction;
|
||||
}
|
||||
|
||||
public void setHologram(char hologram) {
|
||||
this.hologram = hologram;
|
||||
}
|
||||
|
||||
public char getHologram() {
|
||||
return lastHologram;
|
||||
}
|
||||
|
||||
|
||||
public int getEnergy() {
|
||||
return energy;
|
||||
}
|
||||
|
||||
public void setEnergy(int energy) {
|
||||
this.energy = energy;
|
||||
}
|
||||
|
||||
public boolean spendEnergy(int spent) {
|
||||
|
||||
if (energy - spent < 0) {
|
||||
return false;
|
||||
} else {
|
||||
energy -= spent;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void setMaxEnergy(int maxEnergy) {
|
||||
this.maxEnergy = maxEnergy;
|
||||
}
|
||||
|
||||
public int getMaxEnergy() {
|
||||
return maxEnergy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Memory getFloppyData() {
|
||||
|
||||
CubotFloppyDrive drive = ((CubotFloppyDrive) getParent().getCpu().getHardware(CubotFloppyDrive.DEFAULT_ADDRESS));
|
||||
|
||||
if (drive.getFloppy() != null) {
|
||||
return drive.getFloppy().getMemory();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotBattery extends CpuHardware {
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 0x000A;
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
public static final char HWID = 0x000A;
|
||||
|
||||
private Cubot cubot;
|
||||
private static final int POLL = 1;
|
||||
private static final int GET_MAX_CAPACITY = 2;
|
||||
|
||||
public CubotBattery(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == POLL) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getEnergy());
|
||||
|
||||
} else if (a == GET_MAX_CAPACITY) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getMaxEnergy());
|
||||
} else if (a == 0xFFFF) {
|
||||
cubot.setEnergy(cubot.getMaxEnergy());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotBattery deserialize(JSONObject hwJSON) {
|
||||
return new CubotBattery((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.game.Action;
|
||||
import net.simon987.server.game.TileMap;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotDrill extends CpuHardware {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x0005;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 5;
|
||||
|
||||
private static final int POLL = 1;
|
||||
private static final int GATHER_SLOW = 2;
|
||||
private static final int GATHER_FAST = 3;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
public CubotDrill(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == POLL) {
|
||||
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
|
||||
} else if (a == GATHER_SLOW || a == GATHER_FAST) {
|
||||
|
||||
if (cubot.spendEnergy(1400)) {
|
||||
if (cubot.getAction() == Action.IDLE) {
|
||||
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
|
||||
|
||||
if (tile == TileMap.IRON_TILE) {
|
||||
cubot.setHeldItem(TileMap.ITEM_IRON);
|
||||
cubot.setCurrentAction(Action.DIGGING);
|
||||
|
||||
} else if (tile == TileMap.COPPER_TILE) {
|
||||
cubot.setHeldItem(TileMap.ITEM_COPPER);
|
||||
cubot.setCurrentAction(Action.DIGGING);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotDrill deserialize(JSONObject hwJSON) {
|
||||
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotFloppyDrive extends CpuHardware {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x000B;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 0x000B;
|
||||
|
||||
private static final int POLL = 1;
|
||||
private static final int READ_SECTOR = 2;
|
||||
private static final int WRITE_SECTOR = 3;
|
||||
|
||||
private Cubot cubot;
|
||||
private FloppyDisk floppyDisk;
|
||||
|
||||
public CubotFloppyDrive(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
|
||||
floppyDisk = new FloppyDisk();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == POLL) {
|
||||
|
||||
if (floppyDisk != null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
}
|
||||
|
||||
} else if (a == READ_SECTOR) {
|
||||
|
||||
if (floppyDisk == null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
if (cubot.spendEnergy(1)) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
|
||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||
|
||||
floppyDisk.readSector(x, cubot.getParent().getCpu().getMemory(), y);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (a == WRITE_SECTOR) {
|
||||
if (floppyDisk == null) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(0);
|
||||
} else {
|
||||
if (cubot.spendEnergy(1)) {
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(1);
|
||||
|
||||
int x = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
int y = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||
|
||||
floppyDisk.writeSector(x, cubot.getParent().getCpu().getMemory(), y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
if (floppyDisk != null) {
|
||||
json.put("floppy", floppyDisk.serialise());
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotFloppyDrive deserialize(JSONObject hwJSON) {
|
||||
|
||||
CubotFloppyDrive drive = new CubotFloppyDrive((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
|
||||
if (hwJSON.containsKey("floppy")) {
|
||||
drive.floppyDisk = FloppyDisk.deserialise((JSONObject) hwJSON.get("floppy"));
|
||||
}
|
||||
|
||||
return drive;
|
||||
}
|
||||
|
||||
public FloppyDisk getFloppy() {
|
||||
return floppyDisk;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotHologram extends CpuHardware {
|
||||
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x0009;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 9;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
public CubotHologram(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
char a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
cubot.setHologram(a);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
public static CubotHologram deserialize(JSONObject hwJSON) {
|
||||
return new CubotHologram((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotInventory extends CpuHardware {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x0006;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 6;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
private static final int POLL = 1;
|
||||
private static final int CLEAR = 2;
|
||||
|
||||
public CubotInventory(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == POLL) {
|
||||
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem());
|
||||
|
||||
} else if (a == CLEAR) {
|
||||
if (cubot.spendEnergy(100)) {
|
||||
cubot.setHeldItem(0);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotInventory deserialize(JSONObject hwJSON) {
|
||||
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.game.Action;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.game.InventoryHolder;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CubotLaser extends CpuHardware {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x0002;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 2;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
private static final int WITHDRAW = 1;
|
||||
private static final int DEPOSIT = 2;
|
||||
|
||||
|
||||
public CubotLaser(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
int b = getCpu().getRegisterSet().getRegister("B").getValue();
|
||||
|
||||
|
||||
if (a == WITHDRAW) {
|
||||
|
||||
|
||||
Point frontTile = cubot.getFrontTile();
|
||||
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
|
||||
|
||||
|
||||
if (cubot.getAction() != Action.IDLE && objects.size() > 0) {
|
||||
//FIXME: Problem here if more than 1 object
|
||||
if (objects.get(0) instanceof InventoryHolder) {
|
||||
if (((InventoryHolder) objects.get(0)).canTakeItem(b)) {
|
||||
if (cubot.spendEnergy(30)) {
|
||||
//Take the item
|
||||
((InventoryHolder) objects.get(0)).takeItem(b);
|
||||
|
||||
cubot.setHeldItem(b);
|
||||
cubot.setCurrentAction(Action.WITHDRAWING);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} else if (a == DEPOSIT) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotLaser deserialize(JSONObject hwJSON) {
|
||||
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.game.Action;
|
||||
import net.simon987.server.game.Direction;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotLeg extends CpuHardware implements JSONSerialisable {
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 1;
|
||||
|
||||
public static final String NAME = "Cubot Leg";
|
||||
|
||||
private static final int SET_DIR = 1;
|
||||
private static final int SET_DIR_AND_WALK = 2;
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
static final char HWID = 0x0001;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
public CubotLeg(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
int b = getCpu().getRegisterSet().getRegister("B").getValue();
|
||||
|
||||
if (a == SET_DIR) {
|
||||
|
||||
|
||||
Direction dir = Direction.getDirection(b);
|
||||
|
||||
if (dir != null) {
|
||||
if (cubot.spendEnergy(20)) {
|
||||
cubot.setDirection(Direction.getDirection(b));
|
||||
status.setErrorFlag(false);
|
||||
}
|
||||
} else {
|
||||
status.setErrorFlag(true);
|
||||
}
|
||||
|
||||
|
||||
} else if (a == SET_DIR_AND_WALK) {
|
||||
|
||||
Direction dir = Direction.getDirection(b);
|
||||
|
||||
if (dir != null) {
|
||||
cubot.setDirection(Direction.getDirection(b));
|
||||
status.setErrorFlag(false);
|
||||
} else {
|
||||
status.setErrorFlag(true);
|
||||
}
|
||||
|
||||
cubot.setCurrentAction(Action.WALKING);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotLeg deserialize(JSONObject hwJSON) {
|
||||
return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.game.World;
|
||||
import net.simon987.server.game.pathfinding.Node;
|
||||
import net.simon987.server.game.pathfinding.Pathfinder;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class CubotLidar extends CpuHardware implements JSONSerialisable {
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
public static final char HWID = 0x0003;
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 3;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
private static final int GET_POS = 1;
|
||||
private static final int GET_PATH = 2;
|
||||
private static final int GET_MAP = 3;
|
||||
private static final int GET_WORLD_POS = 4;
|
||||
|
||||
private static final int MEMORY_MAP_START = 0x0100;
|
||||
private static final int MEMORY_PATH_START = 0x0000;
|
||||
|
||||
public CubotLidar(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
switch (a) {
|
||||
case GET_POS:
|
||||
getCpu().getRegisterSet().getRegister("X").setValue(cubot.getX());
|
||||
getCpu().getRegisterSet().getRegister("Y").setValue(cubot.getY());
|
||||
break;
|
||||
case GET_PATH:
|
||||
if (cubot.spendEnergy(50)) {
|
||||
int b = getCpu().getRegisterSet().getRegister("B").getValue();
|
||||
int destX = getCpu().getRegisterSet().getRegister("X").getValue();
|
||||
int destY = getCpu().getRegisterSet().getRegister("Y").getValue();
|
||||
|
||||
//Get path
|
||||
ArrayList<Node> nodes = Pathfinder.findPath(cubot.getWorld(), cubot.getX(), cubot.getY(),
|
||||
destX, destY, b);
|
||||
|
||||
//Write to memory
|
||||
Memory mem = getCpu().getMemory();
|
||||
|
||||
int counter = MEMORY_PATH_START;
|
||||
|
||||
if (nodes != null) {
|
||||
|
||||
Node lastNode = null;
|
||||
|
||||
for (Node n : nodes) {
|
||||
//Store the path as a sequence of directions
|
||||
|
||||
if (lastNode == null) {
|
||||
lastNode = n;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (n.x < lastNode.x) {
|
||||
//West
|
||||
mem.set(counter++, 3);
|
||||
} else if (n.x > lastNode.x) {
|
||||
//East
|
||||
mem.set(counter++, 1);
|
||||
} else if (n.y < lastNode.y) {
|
||||
//North
|
||||
mem.set(counter++, 0);
|
||||
} else if (n.y > lastNode.y) {
|
||||
//South
|
||||
mem.set(counter++, 2);
|
||||
}
|
||||
|
||||
lastNode = n;
|
||||
}
|
||||
|
||||
//Indicate end of path with 0xAAAA
|
||||
mem.set(counter, 0xAAAA);
|
||||
} else {
|
||||
//Indicate invalid path 0xFFFF
|
||||
mem.set(counter, 0xFFFF);
|
||||
}
|
||||
|
||||
LogManager.LOGGER.fine("DEBUG: path to" + destX + "," + destY);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case GET_MAP:
|
||||
if (cubot.spendEnergy(10)) {
|
||||
char[][] mapInfo = cubot.getWorld().getMapInfo();
|
||||
|
||||
int i = MEMORY_MAP_START;
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
getCpu().getMemory().set(i++, mapInfo[x][y]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case GET_WORLD_POS:
|
||||
getCpu().getRegisterSet().getRegister("X").setValue(cubot.getWorld().getX());
|
||||
getCpu().getRegisterSet().getRegister("Y").setValue(cubot.getWorld().getY());
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static CubotLidar deserialize(JSONObject hwJSON) {
|
||||
return new CubotLidar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.cubotplugin.event.CpuInitialisationListener;
|
||||
import net.simon987.cubotplugin.event.UserCreationListener;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.game.GameObject;
|
||||
import net.simon987.server.io.CpuHardwareDeserializer;
|
||||
import net.simon987.server.io.GameObjectDeserializer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer {
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
listeners.add(new CpuInitialisationListener());
|
||||
listeners.add(new UserCreationListener());
|
||||
|
||||
LogManager.LOGGER.info("Initialised Cubot plugin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public GameObject deserializeObject(JSONObject object) {
|
||||
|
||||
int objType = (int) (long) object.get("type");
|
||||
|
||||
if (objType == Cubot.ID) {
|
||||
|
||||
return Cubot.deserialize(object);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CpuHardware deserializeHardware(JSONObject hwJson) {
|
||||
int hwid = (int) (long) hwJson.get("hwid");
|
||||
|
||||
switch (hwid) {
|
||||
case CubotLeg.HWID:
|
||||
return CubotLeg.deserialize(hwJson);
|
||||
case CubotLaser.HWID:
|
||||
return CubotLaser.deserialize(hwJson);
|
||||
case CubotLidar.HWID:
|
||||
return CubotLidar.deserialize(hwJson);
|
||||
case CubotDrill.HWID:
|
||||
return CubotDrill.deserialize(hwJson);
|
||||
case CubotInventory.HWID:
|
||||
return CubotInventory.deserialize(hwJson);
|
||||
case Keyboard.HWID:
|
||||
return Keyboard.deserialize(hwJson);
|
||||
case CubotHologram.HWID:
|
||||
return CubotHologram.deserialize(hwJson);
|
||||
case CubotBattery.HWID:
|
||||
return CubotBattery.deserialize(hwJson);
|
||||
case CubotFloppyDrive.HWID:
|
||||
return CubotFloppyDrive.deserialize(hwJson);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
/**
|
||||
* Represents a floppy disk that is inside a floppy drive.
|
||||
* Floppies contains 80 tracks with 18 sectors per track.
|
||||
* That's 1440 sectors of 512 words. (total 1,474,560 bytes / 737,280 words / 1.44Mb)
|
||||
*/
|
||||
public class FloppyDisk implements JSONSerialisable {
|
||||
|
||||
/**
|
||||
* Contents of the disk
|
||||
*/
|
||||
private Memory memory;
|
||||
|
||||
/**
|
||||
* Current location of the read/write head.
|
||||
* Used to calculate seek time
|
||||
*/
|
||||
private int rwHeadTrack = 0;
|
||||
|
||||
|
||||
public FloppyDisk() {
|
||||
this.memory = new Memory(512 * 1440);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read 512 words from the specified sector to cpu memory at specified address
|
||||
*
|
||||
* @param sector sector to read (0-1440)
|
||||
* @param cpuMemory Cpu memory to write to
|
||||
* @param ramAddress address of the data to write in CPU memory
|
||||
* @return Whether or not the read operation was in the same track as the last r/w
|
||||
*/
|
||||
public boolean readSector(int sector, Memory cpuMemory, int ramAddress) {
|
||||
|
||||
if (sector <= 1440) {
|
||||
cpuMemory.write(ramAddress, memory.getWords(), sector * 512, 512);
|
||||
|
||||
//Calculate seek time
|
||||
int deltaTrack = (sector / 80) - rwHeadTrack;
|
||||
|
||||
if (deltaTrack != 0) {
|
||||
rwHeadTrack = (sector / 80);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write 512 words to the specified sector from cpu memory at the specified address
|
||||
*
|
||||
* @param sector sector to write (0-1440)
|
||||
* @param cpuMemory Cpu memory to read from
|
||||
* @param ramAddress address of the data to read in CPU memory
|
||||
* @return Whether or not the read operation was in the same track as the last r/w
|
||||
*/
|
||||
public boolean writeSector(int sector, Memory cpuMemory, int ramAddress) {
|
||||
|
||||
if (sector <= 1440) {
|
||||
memory.write(sector * 512, cpuMemory.getWords(), ramAddress, 512);
|
||||
|
||||
//Calculate seek time
|
||||
int deltaTrack = (sector / 80) - rwHeadTrack;
|
||||
|
||||
if (deltaTrack != 0) {
|
||||
rwHeadTrack = (sector / 80);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("rwHeadTrack", rwHeadTrack);
|
||||
json.put("memory", memory.serialise());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static FloppyDisk deserialise(JSONObject json) {
|
||||
|
||||
FloppyDisk floppyDisk = new FloppyDisk();
|
||||
|
||||
floppyDisk.rwHeadTrack = (int) (long) json.get("rwHeadTrack");
|
||||
floppyDisk.memory = Memory.deserialize((JSONObject) json.get("memory"));
|
||||
|
||||
return floppyDisk;
|
||||
}
|
||||
|
||||
public Memory getMemory() {
|
||||
return memory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package net.simon987.cubotplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Keyboard extends CpuHardware {
|
||||
|
||||
public static final int DEFAULT_ADDRESS = 4;
|
||||
|
||||
private static final int CLEAR_BUFFER = 0;
|
||||
private static final int FETCH_KEY = 1;
|
||||
|
||||
/**
|
||||
* Hardware ID (Should be unique)
|
||||
*/
|
||||
public static final char HWID = 0x0004;
|
||||
|
||||
private Cubot cubot;
|
||||
|
||||
public Keyboard(Cubot cubot) {
|
||||
this.cubot = cubot;
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int a = getCpu().getRegisterSet().getRegister("A").getValue();
|
||||
|
||||
if (a == CLEAR_BUFFER) {
|
||||
|
||||
cubot.clearKeyboardBuffer();
|
||||
|
||||
} else if (a == FETCH_KEY) {
|
||||
//pop
|
||||
int key = 0;
|
||||
if (cubot.getKeyboardBuffer().size() > 0) {
|
||||
key = cubot.getKeyboardBuffer().get(0);
|
||||
cubot.getKeyboardBuffer().remove(0);
|
||||
}
|
||||
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(key);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
json.put("cubot", cubot.getObjectId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static Keyboard deserialize(JSONObject hwJSON) {
|
||||
return new Keyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package net.simon987.cubotplugin.event;
|
||||
|
||||
import net.simon987.cubotplugin.*;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.event.CpuInitialisationEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.user.User;
|
||||
|
||||
public class CpuInitialisationListener implements GameEventListener {
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return CpuInitialisationEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
LogManager.LOGGER.fine("(Plugin) Handled CPU Initialisation event (Cubot Plugin)");
|
||||
|
||||
CPU cpu = (CPU) event.getSource();
|
||||
User user = ((CpuInitialisationEvent) event).getUser();
|
||||
|
||||
CubotLeg legHw = new CubotLeg((Cubot) user.getControlledUnit());
|
||||
legHw.setCpu(cpu);
|
||||
CubotLaser laserHw = new CubotLaser((Cubot) user.getControlledUnit());
|
||||
laserHw.setCpu(cpu);
|
||||
CubotLidar radarHw = new CubotLidar((Cubot) user.getControlledUnit());
|
||||
radarHw.setCpu(cpu);
|
||||
Keyboard keyboard = new Keyboard((Cubot) user.getControlledUnit());
|
||||
keyboard.setCpu(cpu);
|
||||
CubotDrill drillHw = new CubotDrill((Cubot) user.getControlledUnit());
|
||||
drillHw.setCpu(cpu);
|
||||
CubotInventory invHw = new CubotInventory((Cubot) user.getControlledUnit());
|
||||
invHw.setCpu(cpu);
|
||||
CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit());
|
||||
emoteHw.setCpu(cpu);
|
||||
CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit());
|
||||
batteryHw.setCpu(cpu);
|
||||
CubotFloppyDrive floppyHw = new CubotFloppyDrive((Cubot) user.getControlledUnit());
|
||||
floppyHw.setCpu(cpu);
|
||||
|
||||
cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(radarHw, CubotLidar.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(keyboard, Keyboard.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(floppyHw, CubotFloppyDrive.DEFAULT_ADDRESS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package net.simon987.cubotplugin.event;
|
||||
|
||||
import net.simon987.cubotplugin.Cubot;
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.event.UserCreationEvent;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.user.User;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class UserCreationListener implements GameEventListener {
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return UserCreationEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
User user = (User) event.getSource();
|
||||
|
||||
LogManager.LOGGER.fine("(Plugin) Handled User creation event (Cubot Plugin)");
|
||||
|
||||
Cubot cubot = new Cubot();
|
||||
|
||||
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(
|
||||
GameServer.INSTANCE.getConfig().getInt("new_user_worldX"),
|
||||
GameServer.INSTANCE.getConfig().getInt("new_user_worldY")));
|
||||
cubot.getWorld().getGameObjects().add(cubot);
|
||||
|
||||
cubot.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
|
||||
|
||||
cubot.setHeldItem(GameServer.INSTANCE.getConfig().getInt("new_user_item"));
|
||||
|
||||
cubot.setEnergy(GameServer.INSTANCE.getConfig().getInt("battery_max_energy"));
|
||||
cubot.setMaxEnergy(GameServer.INSTANCE.getConfig().getInt("battery_max_energy"));
|
||||
|
||||
cubot.setParent(user);
|
||||
|
||||
Point point = cubot.getWorld().getRandomPassableTile();
|
||||
|
||||
cubot.setX(point.x);
|
||||
cubot.setY(point.y);
|
||||
|
||||
user.setControlledUnit(cubot);
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user