Initial commit

This commit is contained in:
simon
2017-11-02 21:51:22 -04:00
commit b76a1adea2
130 changed files with 8441 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
classpath=net.simon987.cubotplugin.CubotPlugin
name=Cubot Plugin
version=1.0

View File

@@ -0,0 +1,113 @@
package net.simon987.cubotplugin;
import net.simon987.server.game.*;
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 EffectType currentEmote = null;
/**
* Hit points
*/
private int hp;
private int heldItem;
private CubotAction currentAction = CubotAction.IDLE;
private CubotAction lastAction = CubotAction.IDLE;
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
public Cubot() {
}
@Override
public char getMapInfo() {
return MAP_INFO;
}
@Override
public void update() {
if (currentAction == CubotAction.WALKING) {
if (!incrementLocation()) {
//Couldn't walk
currentAction = CubotAction.IDLE;
}
}
if (currentEmote != null) {
// getWorld().getQueuedGameEffects().add(new GameEffect(currentEmote, getX(), getY()));
currentEmote = null;
}
/*
* 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 = CubotAction.IDLE;
}
@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());
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");
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(CubotAction currentAction) {
this.currentAction = currentAction;
}
}

View File

@@ -0,0 +1,8 @@
package net.simon987.cubotplugin;
public enum CubotAction {
IDLE,
DIGGING,
WALKING
}

View File

@@ -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 net.simon987.server.game.TileMap;
import org.json.simple.JSONObject;
public class CubotDrill extends CpuHardware {
/**
* Hardware ID (Should be unique)
*/
public static final int HWID = 0x0005;
public static final int DEFAULT_ADDRESS = 5;
private static final int GATHER = 1;
private Cubot cubot;
public CubotDrill(Cubot cubot) {
this.cubot = cubot;
}
@Override
public void handleInterrupt(Status status) {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
if (a == GATHER) {
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
if (tile == TileMap.IRON_TILE) {
cubot.setHeldItem(TileMap.ITEM_IRON);
} else if (tile == TileMap.COPPER_TILE) {
cubot.setHeldItem(TileMap.ITEM_COPPER);
} else {
System.out.println("FAILED: dig");
}
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", 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")));
}
}

View File

@@ -0,0 +1,54 @@
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)
*/
public static final int 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 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) {
cubot.setHeldItem(0);
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", 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")));
}
}

View File

@@ -0,0 +1,83 @@
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.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)
*/
public static final int HWID = 0x0002;
public static final int DEFAULT_ADDRESS = 2;
private Cubot cubot;
private static final int WITHDRAW = 1;
public CubotLaser(Cubot cubot) {
this.cubot = cubot;
}
@Override
public void handleInterrupt(Status status) {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
int b = getCpu().getRegisterSet().getRegister("B").getValue();
if(a == WITHDRAW) {
System.out.println("withdraw");
Point frontTile = cubot.getFrontTile();
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
if(objects.size() > 0){
if (objects.get(0) instanceof InventoryHolder) {
//Take the item
if (((InventoryHolder) objects.get(0)).takeItem(b)) {
cubot.setHeldItem(b);
System.out.println("took " + b);
} else {
//The inventory holder can't provide this item
//todo Add emote here
System.out.println("FAILED: take (The inventory holder can't provide this item)");
}
}
} else {
//Nothing in front
//todo Add emote here
System.out.println("FAILED: take (Nothing in front)");
}
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", 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")));
}
}

View File

@@ -0,0 +1,77 @@
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.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)
*/
public static final int HWID = 0x0001;
private Cubot cubot;
public CubotLeg(Cubot cubot) {
this.cubot = cubot;
}
@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){
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(CubotAction.WALKING);
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", 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")));
}
}

View File

@@ -0,0 +1,56 @@
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.CpuHardwareDeserializer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.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 CubotRadar.HWID:
return CubotRadar.deserialize(hwJson);
case CubotDrill.HWID:
return CubotDrill.deserialize(hwJson);
case CubotInventory.HWID:
return CubotInventory.deserialize(hwJson);
}
return null;
}
}

View File

@@ -0,0 +1,131 @@
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.World;
import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
import java.util.ArrayList;
public class CubotRadar extends CpuHardware implements JSONSerialisable {
/**
* Hardware ID (Should be unique)
*/
public static final int 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;
public CubotRadar(Cubot cubot) {
this.cubot = cubot;
}
@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:
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);
// System.out.println(nodes.size() + " nodes");
//Write to memory
byte[] mem = getCpu().getMemory().getBytes();
int counter = 0; //todo get memory address from config/constant
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[counter++] = 0;
mem[counter++] = 3;
} else if (n.x > lastNode.x) {
//East
mem[counter++] = 0;
mem[counter++] = 1;
} else if (n.y < lastNode.y) {
//North
mem[counter++] = 0;
mem[counter++] = 0;
} else if (n.y > lastNode.y) {
//South
mem[counter++] = 0;
mem[counter++] = 2;
}
lastNode = n;
}
//Indicate end of path with 0xAAAA
mem[counter++] = -86;
mem[counter] = -86;
} else {
//Indicate invalid path 0xFFFF
mem[counter++] = -1;
mem[counter] = -1;
}
System.out.println("path to" + destX + "," + destY);
break;
case GET_MAP:
char[][] mapInfo = cubot.getWorld().getMapInfo();
int i = 0;
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]);
}
}
}
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("hwid", HWID);
json.put("cubot", cubot.getObjectId());
return json;
}
public static CubotRadar deserialize(JSONObject hwJSON){
return new CubotRadar((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
}
}

View File

@@ -0,0 +1,65 @@
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;
public static final String NAME = "Wireless Keyboard";
private static final int CLEAR_BUFFER = 0;
private static final int FETCH_KEY = 1;
/**
* Hardware ID (Should be unique)
*/
public static final int HWID = 0x0004;
private Cubot cubot;
public Keyboard(Cubot cubot) {
this.cubot = cubot;
}
@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", 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")));
}
}

View File

@@ -0,0 +1,43 @@
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.user.User;
public class CpuInitialisationListener implements GameEventListener {
@Override
public Class getListenedEventType() {
return CpuInitialisationEvent.class;
}
@Override
public void handle(GameEvent event) {
System.out.println("CPU Init");
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);
CubotRadar radarHw = new CubotRadar((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);
cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS);
cpu.attachHardware(radarHw, CubotRadar.DEFAULT_ADDRESS);
cpu.attachHardware(keyboard, Keyboard.DEFAULT_ADDRESS);
cpu.attachHardware(drillHw, CubotDrill.DEFAULT_ADDRESS);
cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
}
}

View File

@@ -0,0 +1,33 @@
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.user.User;
public class UserCreationListener implements GameEventListener {
@Override
public Class getListenedEventType() {
return UserCreationEvent.class;
}
@Override
public void handle(GameEvent event) {
User user = (User)event.getSource();
Cubot cubot = new Cubot();
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(0,0));
cubot.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
cubot.setX(6);
cubot.setY(6);
user.setControlledUnit(cubot);
}
}

View File

@@ -0,0 +1,19 @@
package net.simon987.cubotplugin;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CubotTest {
@Test
public void test(){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
assertEquals(1,2);
}
}