From 98b0c480b990fcab6a3ed67809c6008b1cb5cdc4 Mon Sep 17 00:00:00 2001 From: simon Date: Sun, 5 Nov 2017 14:13:53 -0500 Subject: [PATCH] Various bug fixes --- .../src/net/simon987/cubotplugin/Cubot.java | 7 ++-- .../net/simon987/cubotplugin/CubotAction.java | 4 ++- .../net/simon987/cubotplugin/CubotDrill.java | 27 +++++++++++----- .../simon987/cubotplugin/CubotInventory.java | 7 +++- .../net/simon987/cubotplugin/CubotLaser.java | 14 +++++--- .../net/simon987/cubotplugin/CubotLeg.java | 7 +++- .../net/simon987/cubotplugin/CubotRadar.java | 8 ++++- .../net/simon987/cubotplugin/Keyboard.java | 7 +++- .../event/WorldCreationListener.java | 7 ++-- Server/src/net/simon987/server/Main.java | 15 +++++---- .../src/net/simon987/server/assembly/CPU.java | 14 ++++++++ .../simon987/server/assembly/CpuHardware.java | 2 ++ .../assembly/instruction/HwiInstruction.java | 8 +++++ .../assembly/instruction/HwqInstruction.java | 32 +++++++++++++++++++ .../src/net/simon987/server/game/World.java | 18 +++++------ .../server/webserver/OnlineUserManager.java | 2 -- .../server/webserver/SocketServer.java | 1 + .../webserver/UserInfoRequestHandler.java | 4 --- config.properties | 29 +++-------------- 19 files changed, 145 insertions(+), 68 deletions(-) create mode 100644 Server/src/net/simon987/server/assembly/instruction/HwqInstruction.java diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java b/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java index b10680a..9865d44 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/Cubot.java @@ -70,7 +70,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { json.put("heldItem", heldItem); json.put("hp", hp); json.put("action", lastAction.ordinal()); - if(parent != null){ + if (parent != null) { json.put("parent", parent.getUsername()); //Only used client-side for now } @@ -86,7 +86,6 @@ 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.heldItem = (int)(long)json.get("heldItem"); return cubot; @@ -125,4 +124,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit { public void setParent(User parent) { this.parent = parent; } + + public CubotAction getAction() { + return lastAction; + } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotAction.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotAction.java index e754576..98b60fb 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotAction.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotAction.java @@ -3,6 +3,8 @@ package net.simon987.cubotplugin; public enum CubotAction { IDLE, DIGGING, - WALKING + WALKING, + WITHDRAWING, + DEPOSITING } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java index 5eca059..b4207d8 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotDrill.java @@ -11,7 +11,7 @@ public class CubotDrill extends CpuHardware { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0005; + static final char HWID = 0x0005; public static final int DEFAULT_ADDRESS = 5; @@ -23,24 +23,35 @@ public class CubotDrill extends CpuHardware { this.cubot = cubot; } + @Override + public char getId() { + return HWID; + } + @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 (cubot.getAction() != CubotAction.IDLE) { + int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY()); - if (tile == TileMap.IRON_TILE) { - cubot.setHeldItem(TileMap.ITEM_IRON); + 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); + } else if (tile == TileMap.COPPER_TILE) { + cubot.setHeldItem(TileMap.ITEM_COPPER); + cubot.setCurrentAction(CubotAction.DIGGING); - } else { - System.out.println("FAILED: dig"); + } 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 16538ad..649c1c5 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotInventory.java @@ -10,7 +10,7 @@ public class CubotInventory extends CpuHardware { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0006; + static final int HWID = 0x0006; public static final int DEFAULT_ADDRESS = 6; @@ -23,6 +23,11 @@ public class CubotInventory extends CpuHardware { this.cubot = cubot; } + @Override + public char getId() { + return HWID; + } + @Override public void handleInterrupt(Status status) { diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java index 13002c4..ab02c26 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLaser.java @@ -15,7 +15,7 @@ public class CubotLaser extends CpuHardware { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0002; + static final int HWID = 0x0002; public static final int DEFAULT_ADDRESS = 2; @@ -28,6 +28,11 @@ public class CubotLaser extends CpuHardware { this.cubot = cubot; } + @Override + public char getId() { + return HWID; + } + @Override public void handleInterrupt(Status status) { @@ -42,7 +47,8 @@ public class CubotLaser extends CpuHardware { Point frontTile = cubot.getFrontTile(); ArrayList objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y); - if(objects.size() > 0){ + + if (cubot.getAction() != CubotAction.IDLE && objects.size() > 0) { if (objects.get(0) instanceof InventoryHolder) { //Take the item @@ -50,6 +56,7 @@ public class CubotLaser extends CpuHardware { cubot.setHeldItem(b); System.out.println("took " + b); + cubot.setCurrentAction(CubotAction.WITHDRAWING); } else { //The inventory holder can't provide this item @@ -60,8 +67,7 @@ public class CubotLaser extends CpuHardware { } } else { //Nothing in front - //todo Add emote here - System.out.println("DEBUG: FAILED: take (Nothing in front)"); + System.out.println("DEBUG: FAILED: take (Nothing in front or Cubot is busy)"); } } diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java index 7fb1795..b95ed75 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotLeg.java @@ -19,7 +19,7 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0001; + static final int HWID = 0x0001; private Cubot cubot; @@ -27,6 +27,11 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable { this.cubot = cubot; } + @Override + public char getId() { + return HWID; + } + @Override public void handleInterrupt(Status status) { int a = getCpu().getRegisterSet().getRegister("A").getValue(); diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/CubotRadar.java b/Plugin Cubot/src/net/simon987/cubotplugin/CubotRadar.java index bbe810e..87ca35c 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/CubotRadar.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/CubotRadar.java @@ -16,7 +16,7 @@ public class CubotRadar extends CpuHardware implements JSONSerialisable { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0003; + public static final char HWID = 0x0003; public static final int DEFAULT_ADDRESS = 3; @@ -30,6 +30,12 @@ public class CubotRadar extends CpuHardware implements JSONSerialisable { this.cubot = cubot; } + + @Override + public char getId() { + return HWID; + } + @Override public void handleInterrupt(Status status) { diff --git a/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java b/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java index 3049a25..bc7dd1c 100644 --- a/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java +++ b/Plugin Cubot/src/net/simon987/cubotplugin/Keyboard.java @@ -18,7 +18,7 @@ public class Keyboard extends CpuHardware { /** * Hardware ID (Should be unique) */ - public static final int HWID = 0x0004; + public static final char HWID = 0x0004; private Cubot cubot; @@ -26,6 +26,11 @@ public class Keyboard extends CpuHardware { this.cubot = cubot; } + @Override + public char getId() { + return HWID; + } + @Override public void handleInterrupt(Status status) { diff --git a/Plugin Plant/src/net/simon987/plantplugin/event/WorldCreationListener.java b/Plugin Plant/src/net/simon987/plantplugin/event/WorldCreationListener.java index f43f49b..605c91d 100644 --- a/Plugin Plant/src/net/simon987/plantplugin/event/WorldCreationListener.java +++ b/Plugin Plant/src/net/simon987/plantplugin/event/WorldCreationListener.java @@ -6,7 +6,6 @@ import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.event.WorldGenerationEvent; import net.simon987.server.game.World; -import net.simon987.server.game.WorldGenerator; import net.simon987.server.logging.LogManager; import java.awt.*; @@ -62,9 +61,13 @@ public class WorldCreationListener implements GameEventListener { for (int i = 0; i < treeCount; i++) { Point p = world.getTileMap().getRandomPlainTile(); - if (p != null) { + //Don't block worlds + while (p.x == 0 || p.y == 0 || p.x == World.WORLD_SIZE - 1 || p.y == World.WORLD_SIZE - 1) { + p = world.getTileMap().getRandomPlainTile(); + } + for (Plant plant : plants) { if (plant.getX() == p.x && plant.getY() == p.y) { //There is already a plant here diff --git a/Server/src/net/simon987/server/Main.java b/Server/src/net/simon987/server/Main.java index e651772..f939735 100644 --- a/Server/src/net/simon987/server/Main.java +++ b/Server/src/net/simon987/server/Main.java @@ -10,21 +10,17 @@ import java.net.InetSocketAddress; public class Main { public static void main(String[] args){ + + //TODO: Object information Window (Hover, click ?) //TODO: Docs /* * - Intel 8086 p.14 design * - Memory: Storage organisation: From a storage pov, 8086 memory spaces are * organised as identical arrays of 16-bit words */ - //TODO: Object information Window (Hover, click ?) //TODO: Website front page //TODO: Account page //TODO: Chat (Slack?) - //TODO: Inventory indicator - //TODO: Dig animations - //TODO: Withdraw animation / action - //TODO: HWN, HWQ Instruction - //TODO: Prevent Biomass from blocking Worlds //TODO: Change code documentation (Check for "Database" etc..) //TODO: Load and save: handle no save / invalid save // - Make sure the Hardware is saved and can be loaded @@ -33,6 +29,7 @@ public class Main { //--------------------------------- + //TODO: Inventory indicator (Multiple items) //TODO: Software Interrupts (PIC): Interupt flag? /* * - INT/INTO instruction @@ -53,6 +50,11 @@ public class Main { //TODO: Cache objects requests? //TODO: Ability to toggle debug stuff //TODO: Data segment, DB, DW, DD, DQ + //TODO: Set client animation speed relative to TICK_LENGTH + //TODO: Withdraw animation / action + //TODO: Prevent World creation out of bounds, warp around universe + //TODO: Multiple Biomass style (and yield, rarity) + LogManager.initialize(); @@ -69,5 +71,4 @@ public class Main { (new Thread(socketServer)).start(); (new Thread(GameServer.INSTANCE)).start(); } - } diff --git a/Server/src/net/simon987/server/assembly/CPU.java b/Server/src/net/simon987/server/assembly/CPU.java index 343c9b5..da65b40 100755 --- a/Server/src/net/simon987/server/assembly/CPU.java +++ b/Server/src/net/simon987/server/assembly/CPU.java @@ -92,6 +92,7 @@ public class CPU implements JSONSerialisable{ instructionSet.add(new JnsInstruction(this)); instructionSet.add(new JsInstruction(this)); instructionSet.add(new HwiInstruction(this)); + instructionSet.add(new HwqInstruction(this)); status = new Status(); memory = new Memory(config.getInt("memory_size")); @@ -423,6 +424,17 @@ public class CPU implements JSONSerialisable{ } } + 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 public String toString() { @@ -433,4 +445,6 @@ public class CPU implements JSONSerialisable{ return str; } + + } diff --git a/Server/src/net/simon987/server/assembly/CpuHardware.java b/Server/src/net/simon987/server/assembly/CpuHardware.java index f1e62cf..0940565 100644 --- a/Server/src/net/simon987/server/assembly/CpuHardware.java +++ b/Server/src/net/simon987/server/assembly/CpuHardware.java @@ -24,6 +24,8 @@ public abstract class CpuHardware implements JSONSerialisable { this.cpu = cpu; } + public abstract char getId(); + public static CpuHardware deserialize(JSONObject hwJson){ for(ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()){ diff --git a/Server/src/net/simon987/server/assembly/instruction/HwiInstruction.java b/Server/src/net/simon987/server/assembly/instruction/HwiInstruction.java index 73877ee..2fe09b8 100755 --- a/Server/src/net/simon987/server/assembly/instruction/HwiInstruction.java +++ b/Server/src/net/simon987/server/assembly/instruction/HwiInstruction.java @@ -3,6 +3,7 @@ package net.simon987.server.assembly.instruction; import net.simon987.server.assembly.CPU; import net.simon987.server.assembly.Instruction; import net.simon987.server.assembly.Status; +import net.simon987.server.assembly.Target; /** * Send hardware interupt @@ -20,6 +21,13 @@ public class HwiInstruction extends Instruction { this.cpu = cpu; } + @Override + public Status execute(Target src, int srcIndex, Status status) { + status.setErrorFlag(cpu.hardwareInterrupt(src.get(srcIndex))); + + return status; + } + @Override public Status execute(int src, Status status) { diff --git a/Server/src/net/simon987/server/assembly/instruction/HwqInstruction.java b/Server/src/net/simon987/server/assembly/instruction/HwqInstruction.java new file mode 100644 index 0000000..85fa169 --- /dev/null +++ b/Server/src/net/simon987/server/assembly/instruction/HwqInstruction.java @@ -0,0 +1,32 @@ +package net.simon987.server.assembly.instruction; + +import net.simon987.server.assembly.CPU; +import net.simon987.server.assembly.Instruction; +import net.simon987.server.assembly.Status; +import net.simon987.server.assembly.Target; + +public class HwqInstruction extends Instruction { + + private static final int OPCODE = 28; + + private CPU cpu; + + public HwqInstruction(CPU cpu) { + super("hwq", OPCODE); + this.cpu = cpu; + } + + @Override + public Status execute(Target src, int srcIndex, Status status) { + cpu.hardwareQuery(src.get(srcIndex)); + + return status; + } + + @Override + public Status execute(int src, Status status) { + cpu.hardwareQuery(src); + + return status; + } +} diff --git a/Server/src/net/simon987/server/game/World.java b/Server/src/net/simon987/server/game/World.java index 5230ffe..3b5814e 100644 --- a/Server/src/net/simon987/server/game/World.java +++ b/Server/src/net/simon987/server/game/World.java @@ -67,13 +67,13 @@ public class World implements JSONSerialisable{ ArrayList gameObjects_ = new ArrayList<>(gameObjects); for(GameObject object : gameObjects_){ - if(object instanceof Updatable){ - ((Updatable) object).update(); - } if(object.isDead()){ System.out.println("Removed" + object.getObjectId()); gameObjects.remove(object); } + if (object instanceof Updatable) { + ((Updatable) object).update(); + } } } @@ -145,17 +145,17 @@ public class World implements JSONSerialisable{ for (int y = 0; y < World.WORLD_SIZE; y++) { for (int x = 0; x < World.WORLD_SIZE; x++) { - - if (tiles[y][x] == TileMap.PLAIN_TILE) { + if (tiles[x][y] == TileMap.PLAIN_TILE) { mapInfo[x][y] = 0; - - } else if (tiles[y][x] == TileMap.WALL_TILE) { + } else if (tiles[x][y] == TileMap.WALL_TILE) { mapInfo[x][y] = INFO_BLOCKED; - } else if (tiles[y][x] == TileMap.COPPER_TILE) { + } else if (tiles[x][y] == TileMap.COPPER_TILE) { + mapInfo[x][y] = INFO_COPPER; - } else if (tiles[y][x] == TileMap.IRON_TILE) { + } else if (tiles[x][y] == TileMap.IRON_TILE) { + mapInfo[x][y] = INFO_IRON; } } diff --git a/Server/src/net/simon987/server/webserver/OnlineUserManager.java b/Server/src/net/simon987/server/webserver/OnlineUserManager.java index d04e737..c58f8c6 100644 --- a/Server/src/net/simon987/server/webserver/OnlineUserManager.java +++ b/Server/src/net/simon987/server/webserver/OnlineUserManager.java @@ -1,6 +1,5 @@ package net.simon987.server.webserver; -import net.simon987.server.GameServer; import org.java_websocket.WebSocket; import java.util.ArrayList; @@ -42,7 +41,6 @@ public class OnlineUserManager { */ public void remove(OnlineUser user) { onlineUsers.remove(user); - GameServer.INSTANCE.getGameUniverse().removeUser(user.getUser()); } public ArrayList getOnlineUsers() { diff --git a/Server/src/net/simon987/server/webserver/SocketServer.java b/Server/src/net/simon987/server/webserver/SocketServer.java index 2c510e0..7a192ed 100644 --- a/Server/src/net/simon987/server/webserver/SocketServer.java +++ b/Server/src/net/simon987/server/webserver/SocketServer.java @@ -110,6 +110,7 @@ public class SocketServer extends WebSocketServer { public void onError(WebSocket conn, Exception ex) { System.err.println("an error occured on connection " + conn.getRemoteSocketAddress() + ':' + ex); userManager.remove(userManager.getUser(conn)); + conn.close(); ex.printStackTrace(); } diff --git a/Server/src/net/simon987/server/webserver/UserInfoRequestHandler.java b/Server/src/net/simon987/server/webserver/UserInfoRequestHandler.java index 47ef249..fabc128 100644 --- a/Server/src/net/simon987/server/webserver/UserInfoRequestHandler.java +++ b/Server/src/net/simon987/server/webserver/UserInfoRequestHandler.java @@ -20,8 +20,6 @@ public class UserInfoRequestHandler implements MessageHandler { json.put("t", "userInfo"); json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX")); json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY")); - json.put("x", 1); - json.put("y", 1); user.getWebSocket().send(json.toJSONString()); } else { @@ -31,8 +29,6 @@ public class UserInfoRequestHandler implements MessageHandler { json.put("t", "userInfo"); json.put("worldX", object.getWorld().getX()); json.put("worldY", object.getWorld().getY()); - json.put("x", object.getX()); - json.put("y", object.getY()); user.getWebSocket().send(json.toJSONString()); } diff --git a/config.properties b/config.properties index 9674814..e09f0c2 100644 --- a/config.properties +++ b/config.properties @@ -18,14 +18,9 @@ org_offset=1024 stack_bottom=32768 # Size of the memory in bytes memory_size=65536 -# The game server will process the new user every 'new_user_interval' ticks -new_user_interval=10 -# todo set to 10^ # Initial location of new user's controlled unit new_user_worldX = 0 new_user_worldY = 0 -# Effect when the new user's controlled unit spawns -new_user_effect=WARNING # Default user code new_user_code=; Welcome to Much Assembly required!\n\ ; You will find useful information on the game here: https://github.com/simon987/Much-Assembly-Required/wiki\n\ @@ -35,7 +30,6 @@ new_user_code=; Welcome to Much Assembly required!\n\ # Default held item new_user_item=0 # ---------------------------------------------- - # Biomass units yield for a plant plant_yield=2 # Grow time in ticks for a plant to grow @@ -45,20 +39,6 @@ minTreeCount=3 # Maximum tree count for the WorldGenerator maxTreeCount=10 # ---------------------------------------------- - -#Number of ticks of cooking for 1 unit of biomass -biomass_fuel_value=32 -#Number of ticks required to cook iron (1-255) -iron_cook_time=8 -#Number of ticks required to cook copper (1-255) -copper_cook_time=8 -# Hit points of the Tortoise GameObject -tortoise_hp=10 -# Number of biomass parts required to make a biogas unit -# The number of biomass units required to make a biogas unit= -# biomass_cost / biomass_fuel_value -biogas_cost=48 -# ---------------------------------------------- # Minimum center point count for the WorldGenerator wg_centerPointCountMin=5 # Maximum center point count for the WorldGenerator @@ -66,14 +46,13 @@ wg_centerPointCountMax=15 # Wall/Plain tile ratio for the WorldGenerator wg_wallPlainRatio=4 # Minimum iron tiles count for the WorldGenerator -wg_minIronCount=1 +wg_minIronCount=0 # Minimum iron tile count for the WorldGenerator -wg_maxIronCount=3 +wg_maxIronCount=2 # Minimum copper tile count for the WorldGenerator -wg_minCopperCount=1 +wg_minCopperCount=0 # Maximum copper tile count for the WorldGenerator -wg_maxCopperCount=3 +wg_maxCopperCount=2 # ---------------------------------------------- - # Maximum execution time of user code in ms user_timeout=40 \ No newline at end of file