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 static final int SELF_CLEAR = 0;
private static final int POLL = 1;
private static final int OUT = 2;
private static final int FRONT_PORT_OUT = 2;
private static final int SELF_OUT = 3;
public ComPort(Cubot cubot) {
this.cubot = cubot;
@@ -32,20 +33,69 @@ public class ComPort extends CpuHardware {
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) {
//Get object directly in front of the Cubot
Point frontTile = cubot.getFrontTile();
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsBlockingAt(frontTile.x, frontTile.y);
if (objects.size() > 0 && objects.get(0) instanceof Programmable) {
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
Point frontTile = cubot.getFrontTile();
//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) {
int x = getCpu().getRegisterSet().getRegister("X").getValue();
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);
//Send it to the Programmable object
((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);
@@ -54,9 +104,7 @@ public class ComPort extends CpuHardware {
//Get MESSAGE_LENGTH-word message pointed by X
char[] message = new char[MESSAGE_LENGTH];
System.arraycopy(getCpu().getMemory().getWords(), x, message, 0, MESSAGE_LENGTH);
//Send it to the Programmable object
((Programmable) objects.get(0)).sendMessage(message);
cubot.sendMessage(message);
}
}
}

View File

@@ -8,7 +8,7 @@ import org.json.simple.JSONObject;
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;
public static final int ID = 1;
@@ -16,7 +16,6 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private int hologram = 0;
private String hologramString = "";
private HologramMode hologramMode = 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<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 User parent;
@@ -38,6 +42,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private int maxEnergy;
private static final float SOLAR_PANEL_MULTIPLIER = 1;
private static final int CONSOLE_BUFFER_MAX_SIZE = 40;
public Cubot() {
@@ -75,6 +80,13 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
//Same principle for hologram
lastHologramMode = hologramMode;
hologramMode = HologramMode.CLEARED;
//And the console
lastConsoleMode = consoleMode;
consoleMode = ConsoleMode.NORMAL;
lastConsoleMessagesBuffer = new ArrayList<>(consoleMessagesBuffer);
consoleMessagesBuffer.clear();
}
@Override
@@ -228,8 +240,34 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
COLOR
}
public enum ConsoleMode {
CLEAR,
NORMAL
}
@Override
public void setAction(Action 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,16 +55,18 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
} else if (a == SET_DIR_AND_WALK) {
Direction dir = Direction.getDirection(b);
if (cubot.getMaxEnergy() >= 100) {
Direction dir = Direction.getDirection(b);
if (dir != null) {
cubot.setDirection(Direction.getDirection(b));
status.setErrorFlag(false);
} else {
status.setErrorFlag(true);
if (dir != null) {
cubot.setDirection(Direction.getDirection(b));
status.setErrorFlag(false);
} else {
status.setErrorFlag(true);
}
cubot.setCurrentAction(Action.WALKING);
}
cubot.setCurrentAction(Action.WALKING);
}
}