mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-14 15:19:04 +00:00
I broke everything pt. 1: Moved helditem into CubotInventory, added debug commands for CubotInventory, Added vault complete GameEvent, moved CPU to Cubot, renamed CpuHardware to HardwareModule, moved HardwareModule to Cubot & added HardwareHost interface, removed getParent() in ControllableUnit, moved items to the Item class, started moving tiles to Tile classes, renamed Programmable to MessageReceiver
This commit is contained in:
@@ -12,6 +12,8 @@ import net.simon987.server.event.GameEventDispatcher;
|
||||
import net.simon987.server.event.TickEvent;
|
||||
import net.simon987.server.game.GameUniverse;
|
||||
import net.simon987.server.game.debug.*;
|
||||
import net.simon987.server.game.item.ItemCopper;
|
||||
import net.simon987.server.game.item.ItemIron;
|
||||
import net.simon987.server.game.objects.GameRegistry;
|
||||
import net.simon987.server.game.world.DayNightCycle;
|
||||
import net.simon987.server.game.world.World;
|
||||
@@ -108,6 +110,9 @@ public class GameServer implements Runnable {
|
||||
eventDispatcher.getListeners().add(new DamageObjCommandListener());
|
||||
eventDispatcher.getListeners().add(new SetEnergyCommandListener());
|
||||
eventDispatcher.getListeners().add(new SaveGameCommandListener());
|
||||
|
||||
gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class);
|
||||
gameRegistry.registerItem(ItemIron.ID, ItemIron.class);
|
||||
}
|
||||
|
||||
public GameUniverse getGameUniverse() {
|
||||
@@ -163,13 +168,13 @@ public class GameServer implements Runnable {
|
||||
//Process user code
|
||||
for (User user : gameUniverse.getUsers()) {
|
||||
|
||||
if (user.getCpu() != null) {
|
||||
if (user.getControlledUnit() != null && user.getControlledUnit().getCpu() != null) {
|
||||
try {
|
||||
|
||||
int timeout = Math.min(user.getControlledUnit().getEnergy(), maxExecutionTime);
|
||||
|
||||
user.getCpu().reset();
|
||||
int cost = user.getCpu().execute(timeout);
|
||||
user.getControlledUnit().getCpu().reset();
|
||||
int cost = user.getControlledUnit().getCpu().execute(timeout);
|
||||
user.getControlledUnit().spendEnergy(cost);
|
||||
|
||||
} catch (Exception e) {
|
||||
|
||||
@@ -6,15 +6,12 @@ import net.simon987.server.assembly.exception.CancelledException;
|
||||
import net.simon987.server.assembly.instruction.*;
|
||||
import net.simon987.server.event.CpuInitialisationEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.game.objects.ControllableUnit;
|
||||
import net.simon987.server.game.objects.HardwareHost;
|
||||
import net.simon987.server.io.MongoSerializable;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.user.User;
|
||||
import org.bson.Document;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* CPU: Central Processing Unit. A CPU is capable of reading bytes from
|
||||
* a Memory object and execute them. A CPU object holds registers objects and
|
||||
@@ -54,9 +51,10 @@ public class CPU implements MongoSerializable {
|
||||
private int ip;
|
||||
|
||||
/**
|
||||
* List of attached hardware, 'modules'
|
||||
* Hardware is connected to the hardwareHost
|
||||
*/
|
||||
private HashMap<Integer, CpuHardware> attachedHardware;
|
||||
private HardwareHost hardwareHost;
|
||||
|
||||
|
||||
private ServerConfiguration config;
|
||||
|
||||
@@ -68,11 +66,10 @@ public class CPU implements MongoSerializable {
|
||||
/**
|
||||
* Creates a new CPU
|
||||
*/
|
||||
public CPU(ServerConfiguration config, User user) throws CancelledException {
|
||||
public CPU(ServerConfiguration config, ControllableUnit unit) throws CancelledException {
|
||||
this.config = config;
|
||||
instructionSet = new DefaultInstructionSet();
|
||||
registerSet = new DefaultRegisterSet();
|
||||
attachedHardware = new HashMap<>();
|
||||
codeSectionOffset = config.getInt("org_offset");
|
||||
|
||||
instructionSet.add(new JmpInstruction(this));
|
||||
@@ -105,7 +102,7 @@ public class CPU implements MongoSerializable {
|
||||
status = new Status();
|
||||
memory = new Memory(config.getInt("memory_size"));
|
||||
|
||||
GameEvent event = new CpuInitialisationEvent(this, user);
|
||||
GameEvent event = new CpuInitialisationEvent(this, unit);
|
||||
GameServer.INSTANCE.getEventDispatcher().dispatch(event);
|
||||
if (event.isCancelled()) {
|
||||
throw new CancelledException();
|
||||
@@ -355,36 +352,17 @@ public class CPU implements MongoSerializable {
|
||||
dbObject.put("registerSet", registerSet.mongoSerialise());
|
||||
dbObject.put("codeSegmentOffset", codeSectionOffset);
|
||||
|
||||
List<Document> hardwareList = new ArrayList<>();
|
||||
|
||||
for (Integer address : attachedHardware.keySet()) {
|
||||
|
||||
CpuHardware hardware = attachedHardware.get(address);
|
||||
|
||||
Document serialisedHw = hardware.mongoSerialise();
|
||||
serialisedHw.put("address", address);
|
||||
hardwareList.add(serialisedHw);
|
||||
}
|
||||
|
||||
dbObject.put("hardware", hardwareList);
|
||||
|
||||
return dbObject;
|
||||
|
||||
}
|
||||
|
||||
public static CPU deserialize(Document obj, User user) throws CancelledException {
|
||||
public static CPU deserialize(Document obj, ControllableUnit unit) throws CancelledException {
|
||||
|
||||
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user);
|
||||
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), unit);
|
||||
|
||||
cpu.codeSectionOffset = obj.getInteger("codeSegmentOffset");
|
||||
|
||||
ArrayList hardwareList = (ArrayList) obj.get("hardware");
|
||||
|
||||
for (Object serialisedHw : hardwareList) {
|
||||
CpuHardware hardware = GameServer.INSTANCE.getRegistry().deserializeHardware((Document) serialisedHw, user.getControlledUnit());
|
||||
hardware.setCpu(cpu);
|
||||
cpu.attachHardware(hardware, ((Document) serialisedHw).getInteger("address"));
|
||||
}
|
||||
|
||||
cpu.memory = new Memory((Document) obj.get("memory"));
|
||||
cpu.registerSet = RegisterSet.deserialize((Document) obj.get("registerSet"));
|
||||
@@ -421,56 +399,21 @@ public class CPU implements MongoSerializable {
|
||||
this.codeSectionOffset = codeSectionOffset;
|
||||
}
|
||||
|
||||
public void attachHardware(CpuHardware hardware, int address) {
|
||||
attachedHardware.put(address, hardware);
|
||||
}
|
||||
|
||||
public void detachHardware(int address) {
|
||||
attachedHardware.remove(address);
|
||||
}
|
||||
|
||||
public boolean hardwareInterrupt(int address) {
|
||||
CpuHardware hardware = attachedHardware.get(address);
|
||||
|
||||
if (hardware != null) {
|
||||
hardware.handleInterrupt(status);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
|
||||
String str = "------------------------\n";
|
||||
str += registerSet.toString();
|
||||
String str = registerSet.toString();
|
||||
str += status.toString();
|
||||
str += "HW: ";
|
||||
for (CpuHardware hw : attachedHardware.values()) {
|
||||
str += hw + ", ";
|
||||
}
|
||||
str += "\n------------------------\n";
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
public CpuHardware getHardware(int address) {
|
||||
|
||||
return attachedHardware.get(address);
|
||||
|
||||
public HardwareHost getHardwareHost() {
|
||||
return hardwareHost;
|
||||
}
|
||||
|
||||
|
||||
public void setHardwareHost(HardwareHost hardwareHost) {
|
||||
this.hardwareHost = hardwareHost;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,15 +6,15 @@ import net.simon987.server.io.MongoSerializable;
|
||||
import org.bson.Document;
|
||||
|
||||
|
||||
public abstract class CpuHardware implements MongoSerializable {
|
||||
public abstract class HardwareModule implements MongoSerializable {
|
||||
|
||||
private CPU cpu;
|
||||
|
||||
public CpuHardware() {
|
||||
public HardwareModule() {
|
||||
|
||||
}
|
||||
|
||||
public CpuHardware(Document document, ControllableUnit unit) {
|
||||
public HardwareModule(Document document, ControllableUnit unit) {
|
||||
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public abstract class CpuHardware implements MongoSerializable {
|
||||
*/
|
||||
public abstract void handleInterrupt(Status status);
|
||||
|
||||
public CPU getCpu() {
|
||||
protected CPU getCpu() {
|
||||
return cpu;
|
||||
}
|
||||
|
||||
@@ -35,6 +35,6 @@ public abstract class CpuHardware implements MongoSerializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("<%04X>", (int) getId());
|
||||
return String.format("{%s}", getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public class HwiInstruction extends Instruction {
|
||||
|
||||
@Override
|
||||
public Status execute(Target src, int srcIndex, Status status) {
|
||||
status.setErrorFlag(cpu.hardwareInterrupt(src.get(srcIndex)));
|
||||
status.setErrorFlag(cubot.hardwareInterrupt(src.get(srcIndex), cpu.getStatus()));
|
||||
|
||||
return status;
|
||||
}
|
||||
@@ -31,7 +31,7 @@ public class HwiInstruction extends Instruction {
|
||||
@Override
|
||||
public Status execute(int src, Status status) {
|
||||
|
||||
status.setErrorFlag(cpu.hardwareInterrupt(src));
|
||||
status.setErrorFlag(cubot.hardwareInterrupt(src, cpu.getStatus()));
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -18,14 +18,14 @@ public class HwqInstruction extends Instruction {
|
||||
|
||||
@Override
|
||||
public Status execute(Target src, int srcIndex, Status status) {
|
||||
cpu.hardwareQuery(src.get(srcIndex));
|
||||
cubot.hardwareQuery(src.get(srcIndex));
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status execute(int src, Status status) {
|
||||
cpu.hardwareQuery(src);
|
||||
cubot.hardwareQuery(src);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package net.simon987.server.event;
|
||||
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.user.User;
|
||||
import net.simon987.server.game.objects.ControllableUnit;
|
||||
|
||||
public class CpuInitialisationEvent extends GameEvent {
|
||||
|
||||
private User user;
|
||||
private ControllableUnit unit;
|
||||
|
||||
public CpuInitialisationEvent(CPU cpu, User user) {
|
||||
public CpuInitialisationEvent(CPU cpu, ControllableUnit unit) {
|
||||
setSource(cpu);
|
||||
this.user = user;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
public User getUser() {
|
||||
return user;
|
||||
public ControllableUnit getUnit() {
|
||||
return unit;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ import com.mongodb.client.MongoCursor;
|
||||
import com.mongodb.client.MongoDatabase;
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.ServerConfiguration;
|
||||
import net.simon987.server.assembly.Assembler;
|
||||
import net.simon987.server.assembly.AssemblyResult;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.assembly.exception.CancelledException;
|
||||
import net.simon987.server.game.objects.GameObject;
|
||||
import net.simon987.server.game.world.World;
|
||||
@@ -190,20 +187,6 @@ public class GameUniverse {
|
||||
try {
|
||||
if (makeControlledUnit) {
|
||||
user = new User();
|
||||
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
|
||||
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
|
||||
|
||||
//Compile user code
|
||||
AssemblyResult ar = new Assembler(user.getCpu().getInstructionSet(), user.getCpu().getRegisterSet(),
|
||||
GameServer.INSTANCE.getConfig()).parse(user.getUserCode());
|
||||
|
||||
user.getCpu().getMemory().clear();
|
||||
|
||||
//Write assembled code to mem
|
||||
char[] assembledCode = ar.getWords();
|
||||
|
||||
user.getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||
user.getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
|
||||
|
||||
|
||||
} else {
|
||||
|
||||
@@ -5,7 +5,7 @@ import net.simon987.server.event.DebugCommandEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
import net.simon987.server.game.objects.GameObject;
|
||||
import net.simon987.server.game.objects.Programmable;
|
||||
import net.simon987.server.game.objects.MessageReceiver;
|
||||
|
||||
public class ComPortMsgCommandListener implements GameEventListener {
|
||||
|
||||
@@ -27,12 +27,12 @@ public class ComPortMsgCommandListener implements GameEventListener {
|
||||
|
||||
if (object != null) {
|
||||
|
||||
if (object instanceof Programmable) {
|
||||
if (object instanceof MessageReceiver) {
|
||||
|
||||
e.reply("Result: " + ((Programmable) object).sendMessage(e.getString("message").toCharArray()));
|
||||
e.reply("Result: " + ((MessageReceiver) object).sendMessage(e.getString("message").toCharArray()));
|
||||
|
||||
} else {
|
||||
e.reply("Object " + objectId + " not Programmable");
|
||||
e.reply("Object " + objectId + " not MessageReceiver");
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
@@ -32,7 +32,7 @@ public class UserInfoCommandListener implements GameEventListener {
|
||||
ControllableUnit unit = user.getControlledUnit();
|
||||
message += "ControlledUnit: " + unit.getObjectId() + " at (" + unit.getX() + ", " + unit.getY() + ")\n";
|
||||
|
||||
message += "CPU:" + user.getCpu() + "\n";
|
||||
message += "CPU:" + user.getControlledUnit().getCpu() + "\n";
|
||||
message += "Code: " + user.getUserCode();
|
||||
|
||||
e.reply(message);
|
||||
|
||||
54
Server/src/main/java/net/simon987/server/game/item/Item.java
Normal file
54
Server/src/main/java/net/simon987/server/game/item/Item.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package net.simon987.server.game.item;
|
||||
|
||||
import net.simon987.server.game.objects.ControllableUnit;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
import net.simon987.server.io.MongoSerializable;
|
||||
import org.bson.Document;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public abstract class Item implements JSONSerialisable, MongoSerializable {
|
||||
|
||||
|
||||
public Item(Document document) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when polled
|
||||
*
|
||||
* @return result of poll operation
|
||||
*/
|
||||
public abstract char poll();
|
||||
|
||||
/**
|
||||
* Called when a controllableUnit clears this item from inventory
|
||||
*/
|
||||
public void clear(ControllableUnit unit) {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to uniquely identify an item type in the database and in the game
|
||||
*/
|
||||
public abstract int getId();
|
||||
|
||||
@Override
|
||||
public JSONObject jsonSerialise() {
|
||||
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
json.put("type", getId());
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document mongoSerialise() {
|
||||
Document document = new Document();
|
||||
|
||||
document.put("type", getId());
|
||||
|
||||
return document;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package net.simon987.server.game.item;
|
||||
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
public class ItemCopper extends Item {
|
||||
|
||||
public static final int ID = 0x0003;
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public ItemCopper(Document document) {
|
||||
super(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char poll() {
|
||||
return ID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package net.simon987.server.game.item;
|
||||
|
||||
import org.bson.Document;
|
||||
|
||||
public class ItemIron extends Item {
|
||||
|
||||
public static final int ID = 0x0004;
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return ID;
|
||||
}
|
||||
|
||||
public ItemIron(Document document) {
|
||||
super(document);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char poll() {
|
||||
return ID;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.simon987.server.game.item;
|
||||
|
||||
/**
|
||||
* Invalid/empty item
|
||||
*/
|
||||
public class ItemVoid extends Item {
|
||||
|
||||
public ItemVoid() {
|
||||
super(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public char poll() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
package net.simon987.server.game.objects;
|
||||
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.assembly.Memory;
|
||||
import net.simon987.server.game.item.Item;
|
||||
import net.simon987.server.game.world.World;
|
||||
import net.simon987.server.user.User;
|
||||
|
||||
@@ -35,4 +37,8 @@ public interface ControllableUnit {
|
||||
ArrayList<char[]> getConsoleMessagesBuffer();
|
||||
|
||||
int getConsoleMode();
|
||||
|
||||
CPU getCpu();
|
||||
|
||||
void giveItem(Item item);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.simon987.server.game.objects;
|
||||
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.HardwareModule;
|
||||
import net.simon987.server.game.item.Item;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.bson.Document;
|
||||
|
||||
@@ -10,24 +11,29 @@ import java.util.HashMap;
|
||||
public class GameRegistry {
|
||||
|
||||
private HashMap<String, Class<? extends GameObject>> gameObjects;
|
||||
private HashMap<String, Class<? extends CpuHardware>> hardware;
|
||||
private HashMap<String, Class<? extends HardwareModule>> hardware;
|
||||
private HashMap<Integer, Class<? extends Item>> items;
|
||||
|
||||
|
||||
public GameRegistry() {
|
||||
gameObjects = new HashMap<>();
|
||||
hardware = new HashMap<>();
|
||||
items = new HashMap<>();
|
||||
}
|
||||
|
||||
public void registerGameObject(Class<? extends GameObject> clazz) {
|
||||
gameObjects.put(clazz.getCanonicalName(), clazz);
|
||||
}
|
||||
|
||||
public void registerHardware(Class<? extends CpuHardware> clazz) {
|
||||
public void registerHardware(Class<? extends HardwareModule> clazz) {
|
||||
hardware.put(clazz.getCanonicalName(), clazz);
|
||||
}
|
||||
|
||||
public void registerItem(int id, Class<? extends Item> clazz) {
|
||||
items.put(id, clazz);
|
||||
}
|
||||
|
||||
public CpuHardware deserializeHardware(Document document, ControllableUnit controllableUnit) {
|
||||
public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) {
|
||||
String type = document.getString("type");
|
||||
|
||||
if (hardware.containsKey(type)) {
|
||||
@@ -40,7 +46,7 @@ public class GameRegistry {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
LogManager.LOGGER.severe("Trying to deserialize unknown CpuHardware type: " + type);
|
||||
LogManager.LOGGER.severe("Trying to deserialize unknown HardwareModule type: " + type);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -68,6 +74,51 @@ public class GameRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
public Item deserializeItem(Document document) {
|
||||
|
||||
int type = document.getInteger("type");
|
||||
|
||||
if (items.containsKey(type)) {
|
||||
|
||||
try {
|
||||
return items.get(type).getConstructor(Document.class).newInstance(document);
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
} catch (InvocationTargetException e) {
|
||||
LogManager.LOGGER.severe("Error while trying to deserialize object of type " + type + ": " + e.getTargetException().getMessage());
|
||||
LogManager.LOGGER.severe(document.toJson());
|
||||
e.getTargetException().printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
LogManager.LOGGER.severe("Trying to deserialize unknown Item type: " + type);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an item with default values
|
||||
*
|
||||
* @param itemId id of the item
|
||||
*/
|
||||
public Item makeItem(int itemId) {
|
||||
if (items.containsKey(itemId)) {
|
||||
|
||||
try {
|
||||
return items.get(itemId).getConstructor().newInstance();
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
|
||||
| InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
} else {
|
||||
LogManager.LOGGER.severe("Trying to create an unknown Item type: " + itemId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isGameObjectRegistered(String type) {
|
||||
return gameObjects.containsKey(type);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package net.simon987.server.game.objects;
|
||||
|
||||
import net.simon987.server.assembly.HardwareModule;
|
||||
import net.simon987.server.assembly.Status;
|
||||
|
||||
public interface HardwareHost {
|
||||
|
||||
void attachHardware(HardwareModule hardware, int address);
|
||||
|
||||
void detachHardware(int address);
|
||||
|
||||
boolean hardwareInterrupt(int address, Status status);
|
||||
|
||||
int hardwareQuery(int address);
|
||||
|
||||
}
|
||||
@@ -1,25 +1,23 @@
|
||||
package net.simon987.server.game.objects;
|
||||
|
||||
|
||||
import net.simon987.server.game.item.Item;
|
||||
|
||||
public interface InventoryHolder {
|
||||
|
||||
/**
|
||||
* Place an item into the inventory
|
||||
*
|
||||
* @param item item id (see MarConstants.ITEM_*)
|
||||
*/
|
||||
boolean placeItem(int item);
|
||||
boolean placeItem(Item item);
|
||||
|
||||
/**
|
||||
* Take an item from the inventory
|
||||
*
|
||||
* @param item Desired item id (see MarConstants.ITEM_*)
|
||||
*/
|
||||
void takeItem(int item);
|
||||
void takeItem(int itemId);
|
||||
|
||||
/**
|
||||
* @param item item to take
|
||||
* @param itemId id of the item
|
||||
* @return true if the InventoryHolder can provide this item
|
||||
*/
|
||||
boolean canTakeItem(int item);
|
||||
boolean canTakeItem(int itemId);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package net.simon987.server.game.objects;
|
||||
|
||||
public interface Programmable {
|
||||
public interface MessageReceiver {
|
||||
|
||||
boolean sendMessage(char[] message);
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package net.simon987.server.game.world;
|
||||
|
||||
import net.simon987.server.game.item.Item;
|
||||
|
||||
public abstract class Tile {
|
||||
|
||||
protected boolean blocked;
|
||||
|
||||
/**
|
||||
* @return Unique id of the tile
|
||||
*/
|
||||
public abstract int getId();
|
||||
|
||||
public Item onDrill() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isBlocked() {
|
||||
return blocked;
|
||||
}
|
||||
}
|
||||
@@ -298,7 +298,7 @@ public class World implements MongoSerializable {
|
||||
* and will be able to leave the World
|
||||
* <br>
|
||||
* Note: This function is quite expensive and shouldn't be used
|
||||
* by some CpuHardware in its current state
|
||||
* by some HardwareModule in its current state
|
||||
*
|
||||
* @return random non-blocked tile
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package net.simon987.server.user;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.assembly.exception.CancelledException;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.UserCreationEvent;
|
||||
@@ -20,8 +19,6 @@ public class User implements MongoSerializable {
|
||||
private String password;
|
||||
private String accessToken;
|
||||
|
||||
private CPU cpu;
|
||||
|
||||
private ControllableUnit controlledUnit;
|
||||
|
||||
private boolean guest = false;
|
||||
@@ -52,7 +49,6 @@ public class User implements MongoSerializable {
|
||||
dbObject.put("username", username);
|
||||
dbObject.put("code", userCode);
|
||||
dbObject.put("controlledUnit", controlledUnit.getObjectId());
|
||||
dbObject.put("cpu", cpu.mongoSerialise());
|
||||
dbObject.put("password", password);
|
||||
dbObject.put("moderator", moderator);
|
||||
dbObject.put("stats", stats.mongoSerialise());
|
||||
@@ -70,9 +66,6 @@ public class User implements MongoSerializable {
|
||||
user.moderator = (boolean) obj.get("moderator");
|
||||
user.stats = new UserStats((Document) obj.get("stats"));
|
||||
|
||||
user.getControlledUnit().setParent(user);
|
||||
|
||||
user.cpu = CPU.deserialize((Document) obj.get("cpu"), user);
|
||||
|
||||
return user;
|
||||
}
|
||||
@@ -85,14 +78,6 @@ public class User implements MongoSerializable {
|
||||
this.userCode = userCode;
|
||||
}
|
||||
|
||||
public CPU getCpu() {
|
||||
return cpu;
|
||||
}
|
||||
|
||||
public void setCpu(CPU cpu) {
|
||||
this.cpu = cpu;
|
||||
}
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,7 @@ public class UserManager {
|
||||
|
||||
userCollection.insertOne(dbUser);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
throw new RegistrationException("An exception occurred while trying to create user: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package net.simon987.server.websocket;
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.Assembler;
|
||||
import net.simon987.server.assembly.AssemblyResult;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
@@ -24,17 +25,20 @@ public class CodeUploadHandler implements MessageHandler {
|
||||
user.getUser().setUserCode((String) json.get("code"));
|
||||
|
||||
if (user.getUser().getUserCode() != null) {
|
||||
AssemblyResult ar = new Assembler(user.getUser().getCpu().getInstructionSet(),
|
||||
user.getUser().getCpu().getRegisterSet(),
|
||||
|
||||
CPU cpu = user.getUser().getControlledUnit().getCpu();
|
||||
|
||||
AssemblyResult ar = new Assembler(cpu.getInstructionSet(),
|
||||
cpu.getRegisterSet(),
|
||||
GameServer.INSTANCE.getConfig()).parse(user.getUser().getUserCode());
|
||||
|
||||
user.getUser().getCpu().getMemory().clear();
|
||||
cpu.getMemory().clear();
|
||||
|
||||
//Write assembled code to mem
|
||||
char[] assembledCode = ar.getWords();
|
||||
|
||||
user.getUser().getCpu().getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||
user.getUser().getCpu().setCodeSectionOffset(ar.getCodeSectionOffset());
|
||||
cpu.getMemory().write((char) ar.origin, assembledCode, 0, assembledCode.length);
|
||||
cpu.setCodeSectionOffset(ar.getCodeSectionOffset());
|
||||
|
||||
//Clear keyboard buffer
|
||||
if (user.getUser().getControlledUnit() != null &&
|
||||
@@ -43,7 +47,7 @@ public class CodeUploadHandler implements MessageHandler {
|
||||
}
|
||||
|
||||
//Clear registers
|
||||
user.getUser().getCpu().getRegisterSet().clear();
|
||||
cpu.getRegisterSet().clear();
|
||||
|
||||
JSONObject response = new JSONObject();
|
||||
response.put("t", "codeResponse");
|
||||
|
||||
16
Server/src/main/resources/static/js/mar.js
vendored
16
Server/src/main/resources/static/js/mar.js
vendored
@@ -253,8 +253,6 @@ var WorldIndicator = (function (_super) {
|
||||
};
|
||||
return WorldIndicator;
|
||||
}(DebugMessage));
|
||||
{
|
||||
}
|
||||
var RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
||||
var RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
||||
var DEBUG = true;
|
||||
@@ -442,6 +440,20 @@ var Debug = (function () {
|
||||
Debug.saveGame = function () {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "saveGame"});
|
||||
};
|
||||
Debug.popItem = function (objectId) {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "popItem", objectId: objectId});
|
||||
};
|
||||
Debug.putItem = function (objectId, item) {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "putItem", objectId: objectId, item: item});
|
||||
};
|
||||
Debug.setInventoryPosition = function (objectId, position) {
|
||||
mar.client.sendDebugCommand({
|
||||
t: "debug",
|
||||
command: "setInventoryPosition",
|
||||
objectId: objectId,
|
||||
position: position
|
||||
});
|
||||
};
|
||||
return Debug;
|
||||
}());
|
||||
DEBUG = false;
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
<link rel="stylesheet" href="css/bootstrap-reboot.min.css">
|
||||
<link rel="stylesheet" href="css/bootstrap4-neon-glow.min.css">
|
||||
<link rel="stylesheet" href="css/mar.css">
|
||||
<link rel="stylesheet" href="css/console.css">
|
||||
|
||||
<title>$title</title>
|
||||
</head>
|
||||
@@ -1,5 +1,4 @@
|
||||
{
|
||||
}// Typescript V2.4.1
|
||||
// Typescript V2.4.1
|
||||
|
||||
let RENDERER_WIDTH = document.getElementById("game").clientWidth * window.devicePixelRatio;
|
||||
let RENDERER_HEIGHT = (window.innerHeight / 1.40) * window.devicePixelRatio;
|
||||
@@ -95,7 +94,6 @@ class Util {
|
||||
case Direction.SOUTH:
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static itemColor(item) {
|
||||
@@ -109,7 +107,6 @@ class Util {
|
||||
return config.itemCopper;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -220,10 +217,27 @@ class Debug {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "saveGame"});
|
||||
}
|
||||
|
||||
public static popItem(objectId) {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "popItem", objectId: objectId})
|
||||
}
|
||||
|
||||
public static putItem(objectId, item) {
|
||||
mar.client.sendDebugCommand({t: "debug", command: "putItem", objectId: objectId, item: item})
|
||||
}
|
||||
|
||||
public static setInventoryPosition(objectId, position) {
|
||||
mar.client.sendDebugCommand({
|
||||
t: "debug",
|
||||
command: "setInventoryPosition",
|
||||
objectId: objectId,
|
||||
position: position
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
DEBUG = false; // todo remove
|
||||
DEBUG = false;
|
||||
|
||||
let mar = new MarGame();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user