diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java b/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java index 116f6fa..bc75d55 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java @@ -1,5 +1,6 @@ package net.simon987.cubotplugin; +import net.simon987.server.GameServer; import net.simon987.server.game.ControllableUnit; import net.simon987.server.game.Direction; import net.simon987.server.game.GameObject; @@ -30,6 +31,9 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { private User parent; + private int energy; + private int maxEnergy; + public Cubot() { } @@ -43,8 +47,12 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { public void update() { if (currentAction == CubotAction.WALKING) { - if (!incrementLocation()) { - //Couldn't walk + if (spendEnergy(100)) { + if (!incrementLocation()) { + //Couldn't walk + currentAction = CubotAction.IDLE; + } + } else { currentAction = CubotAction.IDLE; } } @@ -74,6 +82,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { json.put("hp", hp); json.put("action", lastAction.ordinal()); json.put("holo", (int) lastHologram); + json.put("energy", (int) lastHologram); if (parent != null) { json.put("parent", parent.getUsername()); //Only used client-side for now @@ -91,6 +100,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { 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; @@ -141,4 +152,33 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { 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; + } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotBattery.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotBattery.java new file mode 100644 index 0000000..93f57d2 --- /dev/null +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotBattery.java @@ -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"))); + } + +} diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java index beaa338..9f8060e 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java @@ -40,19 +40,19 @@ public class CubotDrill extends CpuHardware { } else if (a == GATHER_SLOW || a == GATHER_FAST) { - if (cubot.getAction() == CubotAction.IDLE) { - int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); + if (cubot.spendEnergy(1400)) { + if (cubot.getAction() == CubotAction.IDLE) { + int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); - if (tile == TileMap.IRON_TILE) { - cubot.setHeldItem(TileMap.ITEM_IRON); - cubot.setCurrentAction(CubotAction.DIGGING); + if (tile == TileMap.IRON_TILE) { + cubot.setHeldItem(TileMap.ITEM_IRON); + cubot.setCurrentAction(CubotAction.DIGGING); - } else if (tile == TileMap.COPPER_TILE) { - cubot.setHeldItem(TileMap.ITEM_COPPER); - cubot.setCurrentAction(CubotAction.DIGGING); + } else if (tile == TileMap.COPPER_TILE) { + cubot.setHeldItem(TileMap.ITEM_COPPER); + cubot.setCurrentAction(CubotAction.DIGGING); - } else { - //System.out.println("FAILED: dig"); + } } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java index 7557366..3b3cc98 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java @@ -38,7 +38,9 @@ public class CubotInventory extends CpuHardware { getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem()); } else if (a == CLEAR) { - cubot.setHeldItem(0); + if (cubot.spendEnergy(100)) { + cubot.setHeldItem(0); + } } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java index af33087..2a36834 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java @@ -22,6 +22,7 @@ public class CubotLaser extends CpuHardware { private Cubot cubot; private static final int WITHDRAW = 1; + private static final int DEPOSIT = 2; public CubotLaser(Cubot cubot) { @@ -42,33 +43,29 @@ public class CubotLaser extends CpuHardware { if(a == WITHDRAW) { - //System.out.println("withdraw"); Point frontTile = cubot.getFrontTile(); ArrayList objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y); if (cubot.getAction() != CubotAction.IDLE && objects.size() > 0) { - + //FIXME: Problem here if more than 1 object if (objects.get(0) instanceof InventoryHolder) { - //Take the item - if (((InventoryHolder) objects.get(0)).takeItem(b)) { + if (((InventoryHolder) objects.get(0)).canTakeItem(b)) { + if (cubot.spendEnergy(30)) { + //Take the item + ((InventoryHolder) objects.get(0)).takeItem(b); - cubot.setHeldItem(b); - //System.out.println("took " + b); - cubot.setCurrentAction(CubotAction.WITHDRAWING); - - } else { - //The inventory holder can't provide this item - //todo Add emote here - // System.out.println("DEBUG: FAILED: take (The inventory holder can't provide this item)"); + cubot.setHeldItem(b); + cubot.setCurrentAction(CubotAction.WITHDRAWING); + } } - } - } else { - //Nothing in front - // System.out.println("DEBUG: FAILED: take (Nothing in front or Cubot is busy)"); } + + + } else if (a == DEPOSIT) { + } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java index 2fb905b..17dff77 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java @@ -39,11 +39,14 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable { if(a == SET_DIR){ + Direction dir = Direction.getDirection(b); if(dir != null){ - cubot.setDirection(Direction.getDirection(b)); - status.setErrorFlag(false); + if (cubot.spendEnergy(20)) { + cubot.setDirection(Direction.getDirection(b)); + status.setErrorFlag(false); + } } else { status.setErrorFlag(true); } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLidar.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLidar.java index c9512b5..e3b5f06 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLidar.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLidar.java @@ -52,75 +52,79 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable { 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(); + 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 nodes = Pathfinder.findPath(cubot.getWorld(), cubot.getX(), cubot.getY(), - destX, destY, b); + //Get path + ArrayList 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(); - //Write to memory - byte[] mem = getCpu().getMemory().getBytes(); + int counter = MEMORY_PATH_START; - int counter = MEMORY_PATH_START; + if (nodes != null) { - if (nodes != null) { + Node lastNode = null; - Node lastNode = null; + for (Node n : nodes) { + //Store the path as a sequence of directions - 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; + } - 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; } - //Indicate end of path with 0xAAAA - mem[counter++] = -86; - mem[counter] = -86; - } else { - //Indicate invalid path 0xFFFF - mem[counter++] = -1; - mem[counter] = -1; + LogManager.LOGGER.fine("DEBUG: path to" + destX + "," + destY); } - LogManager.LOGGER.fine("DEBUG: path to" + destX + "," + destY); break; case GET_MAP: - char[][] mapInfo = cubot.getWorld().getMapInfo(); + 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]); + 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()); diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotPlugin.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotPlugin.java index 31cc603..b96ad16 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotPlugin.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotPlugin.java @@ -53,6 +53,8 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, return Keyboard.deserialize(hwJson); case CubotHologram.HWID: return CubotHologram.deserialize(hwJson); + case CubotBattery.HWID: + return CubotBattery.deserialize(hwJson); } return null; diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java b/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java index 46830d4..e3bab5e 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java @@ -9,9 +9,6 @@ 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; diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/event/CpuInitialisationListener.java b/Plugin Cubot/src/net/simon987/cubotplugin/event/CpuInitialisationListener.java index 7c962f8..00df370 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/event/CpuInitialisationListener.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/event/CpuInitialisationListener.java @@ -35,6 +35,8 @@ public class CpuInitialisationListener implements GameEventListener { invHw.setCpu(cpu); CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit()); emoteHw.setCpu(cpu); + CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit()); + batteryHw.setCpu(cpu); cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS); cpu.attachHardware(laserHw, CubotLaser.DEFAULT_ADDRESS); @@ -44,5 +46,6 @@ public class CpuInitialisationListener implements GameEventListener { cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS); cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS); + cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS); } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/event/UserCreationListener.java b/Plugin Cubot/src/net/simon987/cubotplugin/event/UserCreationListener.java index 72ed7cd..918d7a5 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/event/UserCreationListener.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/event/UserCreationListener.java @@ -34,6 +34,9 @@ public class UserCreationListener implements GameEventListener { 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(); diff --git a/Plugin Misc HW/src/net/simon987/mischwplugin/event/CpuInitialisationListener.java b/Plugin Misc HW/src/net/simon987/mischwplugin/event/CpuInitialisationListener.java index 8d6973c..2587d49 100644 --- a/Plugin Misc HW/src/net/simon987/mischwplugin/event/CpuInitialisationListener.java +++ b/Plugin Misc HW/src/net/simon987/mischwplugin/event/CpuInitialisationListener.java @@ -19,7 +19,12 @@ public class CpuInitialisationListener implements GameEventListener { CPU cpu = (CPU) event.getSource(); - cpu.attachHardware(new RandomNumberGenerator(), RandomNumberGenerator.DEFAULT_ADDRESS); - cpu.attachHardware(new Clock(), Clock.DEFAULT_ADDRESS); + RandomNumberGenerator rngHW = new RandomNumberGenerator(); + rngHW.setCpu(cpu); + Clock clock = new Clock(); + clock.setCpu(cpu); + + cpu.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS); + cpu.attachHardware(clock, Clock.DEFAULT_ADDRESS); } } \ No newline at end of file diff --git a/Plugin Plant/src/net/simon987/plantplugin/Plant.java b/Plugin Plant/src/net/simon987/plantplugin/Plant.java index 0a9275f..b1729f7 100644 --- a/Plugin Plant/src/net/simon987/plantplugin/Plant.java +++ b/Plugin Plant/src/net/simon987/plantplugin/Plant.java @@ -132,30 +132,28 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{ return false; } + @Override + public boolean canTakeItem(int item) { + return item == ITM_BIOMASS && grown && biomassCount >= 1; + } + /** * Called when an object attempts to take an item from this Plant. * If the object requests biomass, it will be subtracted from biomassCount, and * if it reaches 0, the plant is deleted * * @param item item id (see MarConstants.ITEM_*) - * @return true if the requested item is ITEM_BIOMASS and if the plant is grown */ @Override - public boolean takeItem(int item) { + public void takeItem(int item) { if (item == ITM_BIOMASS) { if (grown && biomassCount > 1) { biomassCount--; - return true; } else if (grown) { //Delete plant setDead(true); - return true; - } else { - return false; } - } else { - return false; } } diff --git a/Server/src/net/simon987/server/GameServer.java b/Server/src/net/simon987/server/GameServer.java index 385eae7..1f26406 100644 --- a/Server/src/net/simon987/server/GameServer.java +++ b/Server/src/net/simon987/server/GameServer.java @@ -15,6 +15,7 @@ import org.json.simple.JSONObject; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.util.ArrayList; public class GameServer implements Runnable { @@ -112,7 +113,9 @@ public class GameServer implements Runnable { } //Process each worlds - for (World world : gameUniverse.getWorlds()) { + //Avoid concurrent modification + ArrayList worlds = new ArrayList<>(gameUniverse.getWorlds()); + for (World world : worlds) { world.update(); } diff --git a/Server/src/net/simon987/server/assembly/CPU.java b/Server/src/net/simon987/server/assembly/CPU.java index da5778a..d9d7543 100755 --- a/Server/src/net/simon987/server/assembly/CPU.java +++ b/Server/src/net/simon987/server/assembly/CPU.java @@ -444,5 +444,11 @@ public class CPU implements JSONSerialisable{ return str; } + public CpuHardware getHardware(int address) { + + return attachedHardware.get(address); + + } + } diff --git a/Server/src/net/simon987/server/game/GameObject.java b/Server/src/net/simon987/server/game/GameObject.java index 3ac815a..a5e45ca 100755 --- a/Server/src/net/simon987/server/game/GameObject.java +++ b/Server/src/net/simon987/server/game/GameObject.java @@ -120,9 +120,6 @@ public abstract class GameObject implements JSONSerialisable { x = newX; y = newY; } else { - //Display error when object is trying to walk in a wall - //TODO Add emote here - //System.out.println("DEBUG: FAILED walk"); return false; } diff --git a/Server/src/net/simon987/server/game/InventoryHolder.java b/Server/src/net/simon987/server/game/InventoryHolder.java index 7df1111..f60b2cd 100644 --- a/Server/src/net/simon987/server/game/InventoryHolder.java +++ b/Server/src/net/simon987/server/game/InventoryHolder.java @@ -13,8 +13,12 @@ public interface InventoryHolder { /** * Take an item from the inventory * @param item Desired item id (see MarConstants.ITEM_*) - * @return true is the take item action executed properly, true also means that the desired item - * was removed from the inventory */ - boolean takeItem(int item); + void takeItem(int item); + + /** + * @param item item to take + * @return true if the InventoryHolder can provide this item + */ + boolean canTakeItem(int item); } diff --git a/config.properties b/config.properties index fe03606..a1eac5a 100644 --- a/config.properties +++ b/config.properties @@ -41,6 +41,8 @@ plant_grow_time=32 minTreeCount=3 # Maximum tree count for the WorldGenerator maxTreeCount=10 +# Maximum energy of the battery hardware in kJ +battery_max_energy=60000 # ---------------------------------------------- # Minimum center point count for the WorldGenerator wg_centerPointCountMin=5 diff --git a/plugins/Cubot.jar b/plugins/Cubot.jar index 2810bdf..fb49b22 100644 Binary files a/plugins/Cubot.jar and b/plugins/Cubot.jar differ diff --git a/plugins/Plant.jar b/plugins/Plant.jar index 197eb9e..9e8de1f 100644 Binary files a/plugins/Plant.jar and b/plugins/Plant.jar differ diff --git a/plugins/Plugin Misc HW.jar b/plugins/Plugin Misc HW.jar index 33c3207..689cf46 100644 Binary files a/plugins/Plugin Misc HW.jar and b/plugins/Plugin Misc HW.jar differ