Added Battery Hardware. #2

This commit is contained in:
simon 2017-11-12 10:27:07 -05:00
parent f7ea14275c
commit a3fa3c4c09
21 changed files with 224 additions and 99 deletions

View File

@ -1,5 +1,6 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.ControllableUnit; import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.Direction; import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject; import net.simon987.server.game.GameObject;
@ -30,6 +31,9 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private User parent; private User parent;
private int energy;
private int maxEnergy;
public Cubot() { public Cubot() {
} }
@ -43,8 +47,12 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
public void update() { public void update() {
if (currentAction == CubotAction.WALKING) { if (currentAction == CubotAction.WALKING) {
if (!incrementLocation()) { if (spendEnergy(100)) {
//Couldn't walk if (!incrementLocation()) {
//Couldn't walk
currentAction = CubotAction.IDLE;
}
} else {
currentAction = CubotAction.IDLE; currentAction = CubotAction.IDLE;
} }
} }
@ -74,6 +82,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
json.put("hp", hp); json.put("hp", hp);
json.put("action", lastAction.ordinal()); json.put("action", lastAction.ordinal());
json.put("holo", (int) lastHologram); json.put("holo", (int) lastHologram);
json.put("energy", (int) lastHologram);
if (parent != null) { if (parent != null) {
json.put("parent", parent.getUsername()); //Only used client-side for now 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.hp = (int)(long)json.get("hp");
cubot.setDirection(Direction.getDirection((int)(long)json.get("direction"))); cubot.setDirection(Direction.getDirection((int)(long)json.get("direction")));
cubot.heldItem = (int)(long)json.get("heldItem"); 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; return cubot;
@ -141,4 +152,33 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
public char getHologram() { public char getHologram() {
return lastHologram; 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;
}
} }

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

View File

@ -40,19 +40,19 @@ public class CubotDrill extends CpuHardware {
} else if (a == GATHER_SLOW || a == GATHER_FAST) { } else if (a == GATHER_SLOW || a == GATHER_FAST) {
if (cubot.getAction() == CubotAction.IDLE) { if (cubot.spendEnergy(1400)) {
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); if (cubot.getAction() == CubotAction.IDLE) {
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
if (tile == TileMap.IRON_TILE) { if (tile == TileMap.IRON_TILE) {
cubot.setHeldItem(TileMap.ITEM_IRON); cubot.setHeldItem(TileMap.ITEM_IRON);
cubot.setCurrentAction(CubotAction.DIGGING); cubot.setCurrentAction(CubotAction.DIGGING);
} else if (tile == TileMap.COPPER_TILE) { } else if (tile == TileMap.COPPER_TILE) {
cubot.setHeldItem(TileMap.ITEM_COPPER); cubot.setHeldItem(TileMap.ITEM_COPPER);
cubot.setCurrentAction(CubotAction.DIGGING); cubot.setCurrentAction(CubotAction.DIGGING);
} else { }
//System.out.println("FAILED: dig");
} }
} }

View File

@ -38,7 +38,9 @@ public class CubotInventory extends CpuHardware {
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem()); getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem());
} else if (a == CLEAR) { } else if (a == CLEAR) {
cubot.setHeldItem(0); if (cubot.spendEnergy(100)) {
cubot.setHeldItem(0);
}
} }
} }

View File

@ -22,6 +22,7 @@ public class CubotLaser extends CpuHardware {
private Cubot cubot; private Cubot cubot;
private static final int WITHDRAW = 1; private static final int WITHDRAW = 1;
private static final int DEPOSIT = 2;
public CubotLaser(Cubot cubot) { public CubotLaser(Cubot cubot) {
@ -42,33 +43,29 @@ public class CubotLaser extends CpuHardware {
if(a == WITHDRAW) { if(a == WITHDRAW) {
//System.out.println("withdraw");
Point frontTile = cubot.getFrontTile(); Point frontTile = cubot.getFrontTile();
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y); ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
if (cubot.getAction() != CubotAction.IDLE && objects.size() > 0) { if (cubot.getAction() != CubotAction.IDLE && objects.size() > 0) {
//FIXME: Problem here if more than 1 object
if (objects.get(0) instanceof InventoryHolder) { if (objects.get(0) instanceof InventoryHolder) {
//Take the item if (((InventoryHolder) objects.get(0)).canTakeItem(b)) {
if (((InventoryHolder) objects.get(0)).takeItem(b)) { if (cubot.spendEnergy(30)) {
//Take the item
((InventoryHolder) objects.get(0)).takeItem(b);
cubot.setHeldItem(b); cubot.setHeldItem(b);
//System.out.println("took " + b); cubot.setCurrentAction(CubotAction.WITHDRAWING);
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)");
} }
} }
} else {
//Nothing in front
// System.out.println("DEBUG: FAILED: take (Nothing in front or Cubot is busy)");
} }
} else if (a == DEPOSIT) {
} }
} }

View File

@ -39,11 +39,14 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
if(a == SET_DIR){ if(a == SET_DIR){
Direction dir = Direction.getDirection(b); Direction dir = Direction.getDirection(b);
if(dir != null){ if(dir != null){
cubot.setDirection(Direction.getDirection(b)); if (cubot.spendEnergy(20)) {
status.setErrorFlag(false); cubot.setDirection(Direction.getDirection(b));
status.setErrorFlag(false);
}
} else { } else {
status.setErrorFlag(true); status.setErrorFlag(true);
} }

View File

@ -52,75 +52,79 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
getCpu().getRegisterSet().getRegister("Y").setValue(cubot.getY()); getCpu().getRegisterSet().getRegister("Y").setValue(cubot.getY());
break; break;
case GET_PATH: case GET_PATH:
int b = getCpu().getRegisterSet().getRegister("B").getValue(); if (cubot.spendEnergy(50)) {
int destX = getCpu().getRegisterSet().getRegister("X").getValue(); int b = getCpu().getRegisterSet().getRegister("B").getValue();
int destY = getCpu().getRegisterSet().getRegister("Y").getValue(); int destX = getCpu().getRegisterSet().getRegister("X").getValue();
int destY = getCpu().getRegisterSet().getRegister("Y").getValue();
//Get path //Get path
ArrayList<Node> nodes = Pathfinder.findPath(cubot.getWorld(), cubot.getX(), cubot.getY(), ArrayList<Node> nodes = Pathfinder.findPath(cubot.getWorld(), cubot.getX(), cubot.getY(),
destX, destY, b); destX, destY, b);
// System.out.println(nodes.size() + " nodes"); //Write to memory
byte[] mem = getCpu().getMemory().getBytes();
//Write to memory int counter = MEMORY_PATH_START;
byte[] mem = getCpu().getMemory().getBytes();
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) { if (lastNode == null) {
//Store the path as a sequence of directions 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; lastNode = n;
continue;
} }
if (n.x < lastNode.x) { //Indicate end of path with 0xAAAA
//West mem[counter++] = -86;
mem[counter++] = 0; mem[counter] = -86;
mem[counter++] = 3; } else {
} else if (n.x > lastNode.x) { //Indicate invalid path 0xFFFF
//East mem[counter++] = -1;
mem[counter++] = 0; mem[counter] = -1;
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 LogManager.LOGGER.fine("DEBUG: path to" + destX + "," + destY);
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);
break; break;
case GET_MAP: case GET_MAP:
char[][] mapInfo = cubot.getWorld().getMapInfo(); if (cubot.spendEnergy(10)) {
char[][] mapInfo = cubot.getWorld().getMapInfo();
int i = MEMORY_MAP_START; int i = MEMORY_MAP_START;
for (int y = 0; y < World.WORLD_SIZE; y++) { for (int y = 0; y < World.WORLD_SIZE; y++) {
for (int x = 0; x < World.WORLD_SIZE; x++) { for (int x = 0; x < World.WORLD_SIZE; x++) {
getCpu().getMemory().set(i++, mapInfo[x][y]); getCpu().getMemory().set(i++, mapInfo[x][y]);
}
} }
} }
break; break;
case GET_WORLD_POS: case GET_WORLD_POS:
getCpu().getRegisterSet().getRegister("X").setValue(cubot.getWorld().getX()); getCpu().getRegisterSet().getRegister("X").setValue(cubot.getWorld().getX());

View File

@ -53,6 +53,8 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
return Keyboard.deserialize(hwJson); return Keyboard.deserialize(hwJson);
case CubotHologram.HWID: case CubotHologram.HWID:
return CubotHologram.deserialize(hwJson); return CubotHologram.deserialize(hwJson);
case CubotBattery.HWID:
return CubotBattery.deserialize(hwJson);
} }
return null; return null;

View File

@ -9,9 +9,6 @@ public class Keyboard extends CpuHardware {
public static final int DEFAULT_ADDRESS = 4; 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 CLEAR_BUFFER = 0;
private static final int FETCH_KEY = 1; private static final int FETCH_KEY = 1;

View File

@ -35,6 +35,8 @@ public class CpuInitialisationListener implements GameEventListener {
invHw.setCpu(cpu); invHw.setCpu(cpu);
CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit()); CubotHologram emoteHw = new CubotHologram((Cubot) user.getControlledUnit());
emoteHw.setCpu(cpu); emoteHw.setCpu(cpu);
CubotBattery batteryHw = new CubotBattery((Cubot) user.getControlledUnit());
batteryHw.setCpu(cpu);
cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS); cpu.attachHardware(legHw, CubotLeg.DEFAULT_ADDRESS);
cpu.attachHardware(laserHw, CubotLaser.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(invHw, CubotInventory.DEFAULT_ADDRESS); cpu.attachHardware(invHw, CubotInventory.DEFAULT_ADDRESS);
cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS); cpu.attachHardware(emoteHw, CubotHologram.DEFAULT_ADDRESS);
cpu.attachHardware(batteryHw, CubotBattery.DEFAULT_ADDRESS);
} }
} }

View File

@ -34,6 +34,9 @@ public class UserCreationListener implements GameEventListener {
cubot.setHeldItem(GameServer.INSTANCE.getConfig().getInt("new_user_item")); 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); cubot.setParent(user);
Point point = cubot.getWorld().getRandomPassableTile(); Point point = cubot.getWorld().getRandomPassableTile();

View File

@ -19,7 +19,12 @@ public class CpuInitialisationListener implements GameEventListener {
CPU cpu = (CPU) event.getSource(); CPU cpu = (CPU) event.getSource();
cpu.attachHardware(new RandomNumberGenerator(), RandomNumberGenerator.DEFAULT_ADDRESS); RandomNumberGenerator rngHW = new RandomNumberGenerator();
cpu.attachHardware(new Clock(), Clock.DEFAULT_ADDRESS); rngHW.setCpu(cpu);
Clock clock = new Clock();
clock.setCpu(cpu);
cpu.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS);
cpu.attachHardware(clock, Clock.DEFAULT_ADDRESS);
} }
} }

View File

@ -132,30 +132,28 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
return false; 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. * 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 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_*) * @param item item id (see MarConstants.ITEM_*)
* @return true if the requested item is ITEM_BIOMASS and if the plant is grown
*/ */
@Override @Override
public boolean takeItem(int item) { public void takeItem(int item) {
if (item == ITM_BIOMASS) { if (item == ITM_BIOMASS) {
if (grown && biomassCount > 1) { if (grown && biomassCount > 1) {
biomassCount--; biomassCount--;
return true;
} else if (grown) { } else if (grown) {
//Delete plant //Delete plant
setDead(true); setDead(true);
return true;
} else {
return false;
} }
} else {
return false;
} }
} }

View File

@ -15,6 +15,7 @@ import org.json.simple.JSONObject;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
public class GameServer implements Runnable { public class GameServer implements Runnable {
@ -112,7 +113,9 @@ public class GameServer implements Runnable {
} }
//Process each worlds //Process each worlds
for (World world : gameUniverse.getWorlds()) { //Avoid concurrent modification
ArrayList<World> worlds = new ArrayList<>(gameUniverse.getWorlds());
for (World world : worlds) {
world.update(); world.update();
} }

View File

@ -444,5 +444,11 @@ public class CPU implements JSONSerialisable{
return str; return str;
} }
public CpuHardware getHardware(int address) {
return attachedHardware.get(address);
}
} }

View File

@ -120,9 +120,6 @@ public abstract class GameObject implements JSONSerialisable {
x = newX; x = newX;
y = newY; y = newY;
} else { } else {
//Display error when object is trying to walk in a wall
//TODO Add emote here
//System.out.println("DEBUG: FAILED walk");
return false; return false;
} }

View File

@ -13,8 +13,12 @@ public interface InventoryHolder {
/** /**
* Take an item from the inventory * Take an item from the inventory
* @param item Desired item id (see MarConstants.ITEM_*) * @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);
} }

View File

@ -41,6 +41,8 @@ plant_grow_time=32
minTreeCount=3 minTreeCount=3
# Maximum tree count for the WorldGenerator # Maximum tree count for the WorldGenerator
maxTreeCount=10 maxTreeCount=10
# Maximum energy of the battery hardware in kJ
battery_max_energy=60000
# ---------------------------------------------- # ----------------------------------------------
# Minimum center point count for the WorldGenerator # Minimum center point count for the WorldGenerator
wg_centerPointCountMin=5 wg_centerPointCountMin=5

Binary file not shown.

Binary file not shown.

Binary file not shown.