mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 14:49:03 +00:00
Added maven framework support. Started working on NPCs #19
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package net.simon987.mischwplugin;
|
||||
|
||||
import net.simon987.server.GameServer;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import net.simon987.server.assembly.Util;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Clock extends CpuHardware {
|
||||
|
||||
public static final char HWID = 0x0008;
|
||||
|
||||
public static final char DEFAULT_ADDRESS = 0x0008;
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
int time = (int) GameServer.INSTANCE.getGameUniverse().getTime();
|
||||
|
||||
//Will need to be changed to quadword in about 136 years
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(Util.getHigherWord(time));
|
||||
getCpu().getRegisterSet().getRegister("C").setValue(Util.getLowerWord(time));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
public static Clock deserialize(JSONObject hwJSON) {
|
||||
return new Clock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
|
||||
return json;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package net.simon987.mischwplugin;
|
||||
|
||||
import net.simon987.mischwplugin.event.CpuInitialisationListener;
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.io.CpuHardwareDeserializer;
|
||||
import net.simon987.server.logging.LogManager;
|
||||
import net.simon987.server.plugin.ServerPlugin;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class MiscHWPlugin extends ServerPlugin implements CpuHardwareDeserializer {
|
||||
|
||||
|
||||
@Override
|
||||
public void init() {
|
||||
listeners.add(new CpuInitialisationListener());
|
||||
|
||||
LogManager.LOGGER.info("Initialised Misc Hardware Plugin");
|
||||
}
|
||||
|
||||
@Override
|
||||
public CpuHardware deserializeHardware(JSONObject hwJson) {
|
||||
int hwid = (int) (long) hwJson.get("hwid");
|
||||
|
||||
switch (hwid) {
|
||||
case RandomNumberGenerator.HWID:
|
||||
return RandomNumberGenerator.deserialize(hwJson);
|
||||
case Clock.HWID:
|
||||
return Clock.deserialize(hwJson);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package net.simon987.mischwplugin;
|
||||
|
||||
import net.simon987.server.assembly.CpuHardware;
|
||||
import net.simon987.server.assembly.Status;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomNumberGenerator extends CpuHardware {
|
||||
|
||||
public static final char HWID = 0x0007;
|
||||
|
||||
public static final char DEFAULT_ADDRESS = 0x0007;
|
||||
|
||||
private Random random;
|
||||
|
||||
public RandomNumberGenerator() {
|
||||
random = new Random();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleInterrupt(Status status) {
|
||||
|
||||
getCpu().getRegisterSet().getRegister("B").setValue(random.nextInt(0xFFFF));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public char getId() {
|
||||
return HWID;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JSONObject serialise() {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("hwid", (int) HWID);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
public static RandomNumberGenerator deserialize(JSONObject hwJSON) {
|
||||
return new RandomNumberGenerator();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package net.simon987.mischwplugin.event;
|
||||
|
||||
import net.simon987.mischwplugin.Clock;
|
||||
import net.simon987.mischwplugin.RandomNumberGenerator;
|
||||
import net.simon987.server.assembly.CPU;
|
||||
import net.simon987.server.event.CpuInitialisationEvent;
|
||||
import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.GameEventListener;
|
||||
|
||||
public class CpuInitialisationListener implements GameEventListener {
|
||||
|
||||
@Override
|
||||
public Class getListenedEventType() {
|
||||
return CpuInitialisationEvent.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handle(GameEvent event) {
|
||||
|
||||
CPU cpu = (CPU) event.getSource();
|
||||
|
||||
RandomNumberGenerator rngHW = new RandomNumberGenerator();
|
||||
rngHW.setCpu(cpu);
|
||||
Clock clock = new Clock();
|
||||
clock.setCpu(cpu);
|
||||
|
||||
cpu.attachHardware(rngHW, RandomNumberGenerator.DEFAULT_ADDRESS);
|
||||
cpu.attachHardware(clock, Clock.DEFAULT_ADDRESS);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user