Added Console output, Cubots can receive data from ComPort, Fixed BOM being added to string literals, Fixed legs changing direction for free when energy < 100, Changed some log levels.

This commit is contained in:
simon 2017-12-30 15:30:44 -05:00
parent 2ef6f492c4
commit 55d4c19fe1
14 changed files with 151 additions and 38 deletions

View File

@ -17,9 +17,10 @@ public class ComPort extends CpuHardware {
private Cubot cubot; private Cubot cubot;
private static final int SELF_CLEAR = 0;
private static final int POLL = 1; private static final int POLL = 1;
private static final int FRONT_PORT_OUT = 2;
private static final int OUT = 2; private static final int SELF_OUT = 3;
public ComPort(Cubot cubot) { public ComPort(Cubot cubot) {
this.cubot = cubot; this.cubot = cubot;
@ -32,15 +33,40 @@ public class ComPort extends CpuHardware {
int a = getCpu().getRegisterSet().getRegister("A").getValue(); int a = getCpu().getRegisterSet().getRegister("A").getValue();
if (a == POLL) { if (a == SELF_CLEAR) {
/* No-op */ cubot.getConsoleMessagesBuffer().clear();
cubot.setConsoleMode(Cubot.ConsoleMode.CLEAR);
} else if (a == OUT) { } else if (a == POLL) {
if (cubot.spendEnergy(4)) {
int x = getCpu().getRegisterSet().getRegister("X").getValue();
//Read all messages in the console buffer to memory at X
for (char[] message : cubot.getConsoleMessagesBuffer()) {
if (x + MESSAGE_LENGTH >= getCpu().getMemory().getWords().length) {
//todo set interrupt ?
getCpu().getStatus().setErrorFlag(true);
} else {
System.arraycopy(message, 0, getCpu().getMemory().getWords(), x, MESSAGE_LENGTH);
}
}
//Set B = number of messages
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getConsoleMessagesBuffer().size());
}
} else if (a == FRONT_PORT_OUT) {
if (cubot.spendEnergy(20)) {
//Get object directly in front of the Cubot //Get object directly in front of the Cubot
Point frontTile = cubot.getFrontTile(); Point frontTile = cubot.getFrontTile();
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsBlockingAt(frontTile.x, frontTile.y); //Todo will have to add getGameObjectsBlockingAt to enable Factory
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
if (objects.size() > 0 && objects.get(0) instanceof Programmable) { if (objects.size() > 0 && objects.get(0) instanceof Programmable) {
@ -57,6 +83,28 @@ public class ComPort extends CpuHardware {
//Send it to the Programmable object //Send it to the Programmable object
((Programmable) objects.get(0)).sendMessage(message); ((Programmable) objects.get(0)).sendMessage(message);
System.out.println("Sent message to " + ((Cubot) objects.get(0)).getParent().getUsername());
}
}
}
} else if (a == SELF_OUT) {
if (cubot.spendEnergy(1)) {
int x = getCpu().getRegisterSet().getRegister("X").getValue();
//Write a single message to console buffer
if (x + MESSAGE_LENGTH >= getCpu().getMemory().getWords().length) {
//todo set interrupt ?
getCpu().getStatus().setErrorFlag(true);
} else {
//Get MESSAGE_LENGTH-word message pointed by X
char[] message = new char[MESSAGE_LENGTH];
System.arraycopy(getCpu().getMemory().getWords(), x, message, 0, MESSAGE_LENGTH);
cubot.sendMessage(message);
} }
} }
} }

View File

@ -8,7 +8,7 @@ import org.json.simple.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
public class Cubot extends GameObject implements Updatable, ControllableUnit { public class Cubot extends GameObject implements Updatable, ControllableUnit, Programmable {
private static final char MAP_INFO = 0x0080; private static final char MAP_INFO = 0x0080;
public static final int ID = 1; public static final int ID = 1;
@ -16,7 +16,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private int hologram = 0; private int hologram = 0;
private String hologramString = ""; private String hologramString = "";
private HologramMode hologramMode = HologramMode.CLEARED; private HologramMode hologramMode = HologramMode.CLEARED;
private HologramMode lastHologramMode = HologramMode.CLEARED; private HologramMode lastHologramMode = HologramMode.CLEARED;
/** /**
@ -30,6 +29,11 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private ArrayList<Integer> keyboardBuffer = new ArrayList<>(); private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
private ArrayList<char[]> consoleMessagesBuffer = new ArrayList<>(CONSOLE_BUFFER_MAX_SIZE);
private ArrayList<char[]> lastConsoleMessagesBuffer = new ArrayList<>(CONSOLE_BUFFER_MAX_SIZE);
private ConsoleMode consoleMode = ConsoleMode.NORMAL;
private ConsoleMode lastConsoleMode = ConsoleMode.NORMAL;
private FloppyDisk floppyDisk; private FloppyDisk floppyDisk;
private User parent; private User parent;
@ -38,6 +42,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private int maxEnergy; private int maxEnergy;
private static final float SOLAR_PANEL_MULTIPLIER = 1; private static final float SOLAR_PANEL_MULTIPLIER = 1;
private static final int CONSOLE_BUFFER_MAX_SIZE = 40;
public Cubot() { public Cubot() {
@ -75,6 +80,13 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
//Same principle for hologram //Same principle for hologram
lastHologramMode = hologramMode; lastHologramMode = hologramMode;
hologramMode = HologramMode.CLEARED; hologramMode = HologramMode.CLEARED;
//And the console
lastConsoleMode = consoleMode;
consoleMode = ConsoleMode.NORMAL;
lastConsoleMessagesBuffer = new ArrayList<>(consoleMessagesBuffer);
consoleMessagesBuffer.clear();
} }
@Override @Override
@ -228,8 +240,34 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
COLOR COLOR
} }
public enum ConsoleMode {
CLEAR,
NORMAL
}
@Override @Override
public void setAction(Action action) { public void setAction(Action action) {
currentAction = action; currentAction = action;
} }
@Override
public void sendMessage(char[] message) {
if (consoleMessagesBuffer.size() < CONSOLE_BUFFER_MAX_SIZE) {
consoleMessagesBuffer.add(message);
}
}
public ArrayList<char[]> getConsoleMessagesBuffer() {
return lastConsoleMessagesBuffer;
}
public int getConsoleMode() {
return lastConsoleMode.ordinal();
}
public void setConsoleMode(ConsoleMode consoleMode) {
this.consoleMode = consoleMode;
}
} }

View File

@ -55,6 +55,7 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
} else if (a == SET_DIR_AND_WALK) { } else if (a == SET_DIR_AND_WALK) {
if (cubot.getMaxEnergy() >= 100) {
Direction dir = Direction.getDirection(b); Direction dir = Direction.getDirection(b);
if (dir != null) { if (dir != null) {
@ -67,6 +68,7 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
cubot.setCurrentAction(Action.WALKING); cubot.setCurrentAction(Action.WALKING);
} }
} }
}
@Override @Override
public JSONObject serialise() { public JSONObject serialise() {

View File

@ -155,7 +155,7 @@ public class Assembler {
String string = value.substring(1, value.length() - 1); String string = value.substring(1, value.length() - 1);
string = StringEscapeUtils.unescapeJava(string); string = StringEscapeUtils.unescapeJava(string);
out.write(string.getBytes(StandardCharsets.UTF_16)); out.write(string.getBytes(StandardCharsets.UTF_16BE));
} else if (labels != null && labels.containsKey(value)) { } else if (labels != null && labels.containsKey(value)) {
//Handle label //Handle label
out.writeChar(labels.get(value)); out.writeChar(labels.get(value));

View File

@ -28,4 +28,8 @@ public interface ControllableUnit {
void setAction(Action listening); void setAction(Action listening);
World getWorld(); World getWorld();
ArrayList<char[]> getConsoleMessagesBuffer();
int getConsoleMode();
} }

View File

@ -9,7 +9,7 @@ public class CodeRequestHandler implements MessageHandler {
if (json.get("t").equals("codeRequest")) { if (json.get("t").equals("codeRequest")) {
LogManager.LOGGER.info("(WS) Code request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) Code request from " + user.getUser().getUsername());
if (user.isGuest()) { if (user.isGuest()) {

View File

@ -12,7 +12,7 @@ public class CodeUploadHandler implements MessageHandler {
public void handle(OnlineUser user, JSONObject json) { public void handle(OnlineUser user, JSONObject json) {
if (json.get("t").equals("uploadCode")) { if (json.get("t").equals("uploadCode")) {
LogManager.LOGGER.info("(WS) Code upload from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) Code upload from " + user.getUser().getUsername());
if (user.isGuest()) { if (user.isGuest()) {
//Ignore //Ignore

View File

@ -13,7 +13,7 @@ public class FloppyHandler implements MessageHandler {
if (json.get("t").equals("floppyDown")) { if (json.get("t").equals("floppyDown")) {
LogManager.LOGGER.info("(WS) Floppy download request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) Floppy download request from " + user.getUser().getUsername());
if (user.isGuest()) { if (user.isGuest()) {
return; return;
@ -27,7 +27,7 @@ public class FloppyHandler implements MessageHandler {
} else if (json.get("t").equals("floppyUp")) { } else if (json.get("t").equals("floppyUp")) {
LogManager.LOGGER.info("(WS) Floppy upload request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) Floppy upload request from " + user.getUser().getUsername());
//Check newly uploaded file on the database //Check newly uploaded file on the database
byte[] bytes = db.getFloppy(user.getUser().getUsername()); byte[] bytes = db.getFloppy(user.getUser().getUsername());

View File

@ -1,6 +1,5 @@
package net.simon987.server.webserver; package net.simon987.server.webserver;
import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.util.ArrayList; import java.util.ArrayList;
@ -14,7 +13,7 @@ public class KeypressHandler implements MessageHandler {
if (!user.isGuest()) { if (!user.isGuest()) {
if (json.get("t").equals("k")) { if (json.get("t").equals("k")) {
LogManager.LOGGER.info("(WS) Received keypress"); //LogManager.LOGGER.fine("(WS) Received keypress");
int key = (int) (long) json.get("k"); int key = (int) (long) json.get("k");

View File

@ -33,11 +33,11 @@ public class MessageEventDispatcher {
handler.handle(user, json); handler.handle(user, json);
} }
} else { } else {
LogManager.LOGGER.info("Malformed JSON sent by " + user.getUser().getUsername()); LogManager.LOGGER.severe("Malformed JSON sent by " + user.getUser().getUsername());
} }
} catch (ParseException e) { } catch (ParseException e) {
LogManager.LOGGER.info("Malformed JSON sent by " + user.getUser().getUsername()); LogManager.LOGGER.severe("Malformed JSON sent by " + user.getUser().getUsername());
} }
} }
} }

View File

@ -15,14 +15,14 @@ public class ObjectsRequestHandler implements MessageHandler {
@Override @Override
public void handle(OnlineUser user, JSONObject json) { public void handle(OnlineUser user, JSONObject json) {
if (json.get("t").equals("object")) { if (json.get("t").equals("object")) {
LogManager.LOGGER.info("(WS) Objects request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) Objects request from " + user.getUser().getUsername());
int x, y; int x, y;
try { try {
x = Long.valueOf((long) json.get("x")).intValue(); x = Long.valueOf((long) json.get("x")).intValue();
y = Long.valueOf((long) json.get("y")).intValue(); y = Long.valueOf((long) json.get("y")).intValue();
} catch (Exception e) { } catch (Exception e) {
LogManager.LOGGER.info("(WS) Malformed Objects request from " + user.getUser().getUsername()); LogManager.LOGGER.severe("(WS) Malformed Objects request from " + user.getUser().getUsername());
return; return;
} }

View File

@ -2,6 +2,7 @@ package net.simon987.server.webserver;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User; import net.simon987.server.user.User;
import org.java_websocket.WebSocket; import org.java_websocket.WebSocket;
@ -175,17 +176,38 @@ public class SocketServer extends WebSocketServer {
user.getWebSocket().send(json.toJSONString()); user.getWebSocket().send(json.toJSONString());
} else { } else {
//Send keyboard updated buffer
try { try {
ArrayList<Integer> kbBuffer = user.getUser().getControlledUnit().getKeyboardBuffer(); ControllableUnit unit = user.getUser().getControlledUnit();
System.out.println("Sent " + unit.getConsoleMessagesBuffer().size() + " messages to " + user.getUser().getUsername());
//Send keyboard updated buffer
ArrayList<Integer> kbBuffer = unit.getKeyboardBuffer();
JSONArray keys = new JSONArray(); JSONArray keys = new JSONArray();
keys.addAll(kbBuffer); keys.addAll(kbBuffer);
json.put("keys", keys); json.put("keys", keys);
//Send console buffer
if (unit.getConsoleMessagesBuffer().size() > 0) {
JSONArray buff = new JSONArray();
for (char[] message : unit.getConsoleMessagesBuffer()) {
buff.add(new String(message));
}
json.put("c", buff);
}
json.put("cm", unit.getConsoleMode());
//Send tick message //Send tick message
user.getWebSocket().send(json.toJSONString()); user.getWebSocket().send(json.toJSONString());
} catch (NullPointerException e) { } catch (NullPointerException e) {
//User is online but not completely initialised //User is online but not completely initialised
} }
} }

View File

@ -12,7 +12,7 @@ public class TerrainRequestHandler implements MessageHandler {
public void handle(OnlineUser user, JSONObject json) { public void handle(OnlineUser user, JSONObject json) {
if (json.get("t").equals("terrain") && json.containsKey("x") && json.containsKey("y")) { if (json.get("t").equals("terrain") && json.containsKey("x") && json.containsKey("y")) {
LogManager.LOGGER.info("Terrain request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("Terrain request from " + user.getUser().getUsername());
World world; World world;
try { try {
world = GameServer.INSTANCE.getGameUniverse().getWorld( world = GameServer.INSTANCE.getGameUniverse().getWorld(

View File

@ -13,7 +13,7 @@ public class UserInfoRequestHandler implements MessageHandler {
if (message.get("t").equals("userInfo")) { if (message.get("t").equals("userInfo")) {
LogManager.LOGGER.info("(WS) User info request from " + user.getUser().getUsername()); LogManager.LOGGER.fine("(WS) User info request from " + user.getUser().getUsername());
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
if (user.isGuest()) { if (user.isGuest()) {