Added maven framework support. Started working on NPCs #19

This commit is contained in:
simon 2017-11-21 20:22:10 -05:00
parent 12db25e726
commit 6be2a496c6
158 changed files with 1002 additions and 333 deletions

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="Server" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
</component>
</module>

40
Plugin Cubot/pom.xml Normal file
View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.simon987.plugincubot</groupId>
<artifactId>Plugin Cubot</artifactId>
<version>1.2a</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.simon987.server</groupId>
<artifactId>Server</artifactId>
<version>1.2a</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

View File

@ -2,10 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.Memory;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.*;
import net.simon987.server.user.User;
import org.json.simple.JSONObject;
@ -25,8 +22,8 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
private int hp;
private int heldItem;
private CubotAction currentAction = CubotAction.IDLE;
private CubotAction lastAction = CubotAction.IDLE;
private Action currentAction = Action.IDLE;
private Action lastAction = Action.IDLE;
private ArrayList<Integer> keyboardBuffer = new ArrayList<>();
@ -49,14 +46,14 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
@Override
public void update() {
if (currentAction == CubotAction.WALKING) {
if (currentAction == Action.WALKING) {
if (spendEnergy(100)) {
if (!incrementLocation()) {
//Couldn't walk
currentAction = CubotAction.IDLE;
currentAction = Action.IDLE;
}
} else {
currentAction = CubotAction.IDLE;
currentAction = Action.IDLE;
}
}
@ -66,7 +63,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
* was set last tick (IDLE)
*/
lastAction = currentAction;
currentAction = CubotAction.IDLE;
currentAction = Action.IDLE;
//Same principle for hologram
lastHologram = hologram;
@ -85,7 +82,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
json.put("hp", hp);
json.put("action", lastAction.ordinal());
json.put("holo", (int) lastHologram);
json.put("energy", (int) lastHologram);
json.put("energy", energy);
if (parent != null) {
json.put("parent", parent.getUsername()); //Only used client-side for now
@ -97,12 +94,12 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
public static Cubot deserialize(JSONObject json) {
Cubot cubot = new Cubot();
cubot.setObjectId((int)(long)json.get("id"));
cubot.setX((int)(long)json.get("x"));
cubot.setY((int)(long)json.get("y"));
cubot.hp = (int)(long)json.get("hp");
cubot.setDirection(Direction.getDirection((int)(long)json.get("direction")));
cubot.heldItem = (int)(long)json.get("heldItem");
cubot.setObjectId((int) (long) json.get("id"));
cubot.setX((int) (long) json.get("x"));
cubot.setY((int) (long) json.get("y"));
cubot.hp = (int) (long) json.get("hp");
cubot.setDirection(Direction.getDirection((int) (long) json.get("direction")));
cubot.heldItem = (int) (long) json.get("heldItem");
cubot.energy = (int) (long) json.get("energy");
cubot.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
@ -128,11 +125,11 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
return keyboardBuffer;
}
public void clearKeyboardBuffer(){
public void clearKeyboardBuffer() {
keyboardBuffer.clear();
}
public void setCurrentAction(CubotAction currentAction) {
public void setCurrentAction(Action currentAction) {
this.currentAction = currentAction;
}
@ -144,7 +141,7 @@ public class Cubot extends GameObject implements Updatable, ControllableUnit {
this.parent = parent;
}
public CubotAction getAction() {
public Action getAction() {
return lastAction;
}

View File

@ -3,6 +3,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.TileMap;
import org.json.simple.JSONObject;
@ -41,16 +42,16 @@ public class CubotDrill extends CpuHardware {
} else if (a == GATHER_SLOW || a == GATHER_FAST) {
if (cubot.spendEnergy(1400)) {
if (cubot.getAction() == CubotAction.IDLE) {
if (cubot.getAction() == Action.IDLE) {
int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
if (tile == TileMap.IRON_TILE) {
cubot.setHeldItem(TileMap.ITEM_IRON);
cubot.setCurrentAction(CubotAction.DIGGING);
cubot.setCurrentAction(Action.DIGGING);
} else if (tile == TileMap.COPPER_TILE) {
cubot.setHeldItem(TileMap.ITEM_COPPER);
cubot.setCurrentAction(CubotAction.DIGGING);
cubot.setCurrentAction(Action.DIGGING);
}
}
@ -69,7 +70,7 @@ public class CubotDrill extends CpuHardware {
return json;
}
public static CubotDrill deserialize(JSONObject hwJSON){
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
public static CubotDrill deserialize(JSONObject hwJSON) {
return new CubotDrill((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
}
}

View File

@ -33,7 +33,7 @@ public class CubotInventory extends CpuHardware {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
if(a == POLL) {
if (a == POLL) {
getCpu().getRegisterSet().getRegister("B").setValue(cubot.getHeldItem());
@ -55,7 +55,7 @@ public class CubotInventory extends CpuHardware {
return json;
}
public static CubotInventory deserialize(JSONObject hwJSON){
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
public static CubotInventory deserialize(JSONObject hwJSON) {
return new CubotInventory((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
}
}

View File

@ -3,6 +3,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import org.json.simple.JSONObject;
@ -41,14 +42,14 @@ public class CubotLaser extends CpuHardware {
int b = getCpu().getRegisterSet().getRegister("B").getValue();
if(a == WITHDRAW) {
if (a == WITHDRAW) {
Point frontTile = cubot.getFrontTile();
ArrayList<GameObject> objects = cubot.getWorld().getGameObjectsAt(frontTile.x, frontTile.y);
if (cubot.getAction() != CubotAction.IDLE && objects.size() > 0) {
if (cubot.getAction() != Action.IDLE && objects.size() > 0) {
//FIXME: Problem here if more than 1 object
if (objects.get(0) instanceof InventoryHolder) {
if (((InventoryHolder) objects.get(0)).canTakeItem(b)) {
@ -57,7 +58,7 @@ public class CubotLaser extends CpuHardware {
((InventoryHolder) objects.get(0)).takeItem(b);
cubot.setHeldItem(b);
cubot.setCurrentAction(CubotAction.WITHDRAWING);
cubot.setCurrentAction(Action.WITHDRAWING);
}
}
}
@ -80,7 +81,7 @@ public class CubotLaser extends CpuHardware {
return json;
}
public static CubotLaser deserialize(JSONObject hwJSON){
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
public static CubotLaser deserialize(JSONObject hwJSON) {
return new CubotLaser((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
}
}

View File

@ -3,6 +3,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.Action;
import net.simon987.server.game.Direction;
import net.simon987.server.io.JSONSerialisable;
import org.json.simple.JSONObject;
@ -37,12 +38,12 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
int b = getCpu().getRegisterSet().getRegister("B").getValue();
if(a == SET_DIR){
if (a == SET_DIR) {
Direction dir = Direction.getDirection(b);
if(dir != null){
if (dir != null) {
if (cubot.spendEnergy(20)) {
cubot.setDirection(Direction.getDirection(b));
status.setErrorFlag(false);
@ -52,18 +53,18 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
}
} else if(a == SET_DIR_AND_WALK){
} else if (a == SET_DIR_AND_WALK) {
Direction dir = Direction.getDirection(b);
if(dir != null){
if (dir != null) {
cubot.setDirection(Direction.getDirection(b));
status.setErrorFlag(false);
} else {
status.setErrorFlag(true);
}
cubot.setCurrentAction(CubotAction.WALKING);
cubot.setCurrentAction(Action.WALKING);
}
}
@ -77,8 +78,8 @@ public class CubotLeg extends CpuHardware implements JSONSerialisable {
return json;
}
public static CubotLeg deserialize(JSONObject hwJSON){
return new CubotLeg((Cubot)GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
public static CubotLeg deserialize(JSONObject hwJSON) {
return new CubotLeg((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
}

View File

@ -2,6 +2,7 @@ package net.simon987.cubotplugin;
import net.simon987.server.GameServer;
import net.simon987.server.assembly.CpuHardware;
import net.simon987.server.assembly.Memory;
import net.simon987.server.assembly.Status;
import net.simon987.server.game.World;
import net.simon987.server.game.pathfinding.Node;
@ -46,7 +47,7 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
switch (a){
switch (a) {
case GET_POS:
getCpu().getRegisterSet().getRegister("X").setValue(cubot.getX());
getCpu().getRegisterSet().getRegister("Y").setValue(cubot.getY());
@ -62,7 +63,7 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
destX, destY, b);
//Write to memory
byte[] mem = getCpu().getMemory().getBytes();
Memory mem = getCpu().getMemory();
int counter = MEMORY_PATH_START;
@ -80,32 +81,26 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
if (n.x < lastNode.x) {
//West
mem[counter++] = 0;
mem[counter++] = 3;
mem.set(counter++, 3);
} else if (n.x > lastNode.x) {
//East
mem[counter++] = 0;
mem[counter++] = 1;
mem.set(counter++, 1);
} else if (n.y < lastNode.y) {
//North
mem[counter++] = 0;
mem[counter++] = 0;
mem.set(counter++, 0);
} else if (n.y > lastNode.y) {
//South
mem[counter++] = 0;
mem[counter++] = 2;
mem.set(counter++, 2);
}
lastNode = n;
}
//Indicate end of path with 0xAAAA
mem[counter++] = -86;
mem[counter] = -86;
mem.set(counter, 0xAAAA);
} else {
//Indicate invalid path 0xFFFF
mem[counter++] = -1;
mem[counter] = -1;
mem.set(counter, 0xFFFF);
}
LogManager.LOGGER.fine("DEBUG: path to" + destX + "," + destY);

View File

@ -10,7 +10,7 @@ import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.json.simple.JSONObject;
public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer{
public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer, CpuHardwareDeserializer {
@Override
@ -24,9 +24,9 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
@Override
public GameObject deserializeObject(JSONObject object) {
int objType = (int)(long)object.get("type");
int objType = (int) (long) object.get("type");
if(objType == Cubot.ID) {
if (objType == Cubot.ID) {
return Cubot.deserialize(object);
}
@ -36,9 +36,9 @@ public class CubotPlugin extends ServerPlugin implements GameObjectDeserializer,
@Override
public CpuHardware deserializeHardware(JSONObject hwJson) {
int hwid = (int)(long)hwJson.get("hwid");
int hwid = (int) (long) hwJson.get("hwid");
switch (hwid){
switch (hwid) {
case CubotLeg.HWID:
return CubotLeg.deserialize(hwJson);
case CubotLaser.HWID:

View File

@ -11,7 +11,7 @@ public class Keyboard extends CpuHardware {
private static final int CLEAR_BUFFER = 0;
private static final int FETCH_KEY = 1;
/**
* Hardware ID (Should be unique)
*/
@ -33,14 +33,14 @@ public class Keyboard extends CpuHardware {
int a = getCpu().getRegisterSet().getRegister("A").getValue();
if(a == CLEAR_BUFFER){
if (a == CLEAR_BUFFER) {
cubot.clearKeyboardBuffer();
} else if (a == FETCH_KEY){
} else if (a == FETCH_KEY) {
//pop
int key = 0;
if(cubot.getKeyboardBuffer().size() > 0){
if (cubot.getKeyboardBuffer().size() > 0) {
key = cubot.getKeyboardBuffer().get(0);
cubot.getKeyboardBuffer().remove(0);
}
@ -61,7 +61,7 @@ public class Keyboard extends CpuHardware {
return json;
}
public static Keyboard deserialize(JSONObject hwJSON){
return new Keyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int)(long)hwJSON.get("cubot")));
public static Keyboard deserialize(JSONObject hwJSON) {
return new Keyboard((Cubot) GameServer.INSTANCE.getGameUniverse().getObject((int) (long) hwJSON.get("cubot")));
}
}

View File

@ -18,8 +18,8 @@ public class CpuInitialisationListener implements GameEventListener {
public void handle(GameEvent event) {
LogManager.LOGGER.fine("(Plugin) Handled CPU Initialisation event (Cubot Plugin)");
CPU cpu = (CPU)event.getSource();
User user = ((CpuInitialisationEvent)event).getUser();
CPU cpu = (CPU) event.getSource();
User user = ((CpuInitialisationEvent) event).getUser();
CubotLeg legHw = new CubotLeg((Cubot) user.getControlledUnit());
legHw.setCpu(cpu);

View File

@ -19,7 +19,7 @@ public class UserCreationListener implements GameEventListener {
@Override
public void handle(GameEvent event) {
User user = (User)event.getSource();
User user = (User) event.getSource();
LogManager.LOGGER.fine("(Plugin) Handled User creation event (Cubot Plugin)");

View File

@ -1,3 +0,0 @@
classpath=net.simon987.kilnplugin.KilnPlugin
name=Kiln Plugin
version=1.0

View File

@ -1,21 +0,0 @@
package net.simon987.kilnplugin;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.plugin.ServerPlugin;
import org.json.simple.JSONObject;
public class KilnPlugin extends ServerPlugin implements GameObjectDeserializer {
@Override
public void init() {
}
@Override
public GameObject deserializeObject(JSONObject object) {
return null;
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="Server" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
</component>
</module>

39
Plugin Misc HW/pom.xml Normal file
View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.simon987.pluginmischw</groupId>
<artifactId>Plugin Misc HW</artifactId>
<version>1.2a</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>net.simon987.server</groupId>
<artifactId>Server</artifactId>
<version>1.2a</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

21
Plugin NPC/Plugin NPC.iml Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="module" module-name="Server" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
</component>
</module>

View File

@ -0,0 +1,3 @@
classpath=net.simon987.npcplugin.NpcPlugin
name=NPC Plugin
version=1.0

36
Plugin NPC/pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.simon987.pluginnpc</groupId>
<artifactId>Plugin NPC</artifactId>
<version>1.2a</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.simon987.server</groupId>
<artifactId>Server</artifactId>
<version>1.2a</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,100 @@
package net.simon987.npcplugin;
import net.simon987.server.assembly.Util;
import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import net.simon987.server.logging.LogManager;
import java.util.ArrayList;
import java.util.Random;
public class HarvestTask extends NPCTask {
private Random random;
private int pause;
public HarvestTask() {
random = new Random();
pause = 0;
}
private Direction nextWorldDirection = null;
@Override
public boolean checkCompleted() {
return false;
}
@Override
public void tick(NonPlayerCharacter npc) {
if (pause == 0) {
//Get biomass
ArrayList<GameObject> biomass = new ArrayList<>(10);
for (GameObject object : npc.getWorld().getGameObjects()) {
//Plant MAP_INFO
if ((object.getMapInfo() & 0x4000) == 0x4000) {
biomass.add(object);
}
}
//Get closest one
int minDist = Integer.MAX_VALUE;
GameObject minBiomass = null;
for (GameObject plant : biomass) {
int dist = Util.manhattanDist(npc.getX(), npc.getY(), plant.getX(), plant.getY());
if (dist < minDist) {
minDist = dist;
minBiomass = plant;
}
}
//Move towards it
if (minBiomass != null && minDist == 1) {
//Reached biomass, change direction to face it
Direction newDirection = Direction.getFacing(npc.getX(), npc.getY(),
minBiomass.getX(), minBiomass.getY());
if (newDirection != null) {
npc.setDirection(newDirection);
//Reached biomass, harvest it
if (minBiomass instanceof InventoryHolder) {
((InventoryHolder) minBiomass).takeItem(1);
pause += 6;
}
} else {
LogManager.LOGGER.severe("FIXME: tick:HarvestTask, Direction is null");
}
nextWorldDirection = null;
} else if (minBiomass != null && npc.moveTo(minBiomass.getX(), minBiomass.getY(), 1)) {
//Moving towards biomass...
nextWorldDirection = null;
} else {
if (nextWorldDirection == null) {
nextWorldDirection = Direction.getDirection(random.nextInt(3));
pause += 6;
}
npc.gotoWorld(nextWorldDirection);
}
} else {
pause--;
}
}
}

View File

@ -0,0 +1,66 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.Direction;
import org.json.simple.JSONObject;
public class HarvesterNPC extends NonPlayerCharacter {
public static final int ID = 10; //todo change
public HarvesterNPC() {
setTask(new HarvestTask());
hp = 10;
}
@Override
public void update() {
if (hp <= 0) {
setDead(true);
//TODO: drop biomass
}
if (getTask().checkCompleted()) {
setTask(new HarvestTask());
} else {
getTask().tick(this);
}
}
@Override
public JSONObject serialise() {
JSONObject json = super.serialise();
json.put("id", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("direction", getDirection().ordinal());
json.put("hp", hp);
json.put("energy", energy);
json.put("action", getAction().ordinal());
json.put("type", 10);
return json;
}
public static HarvesterNPC deserialize(JSONObject json) {
HarvesterNPC npc = new HarvesterNPC();
npc.setObjectId((int) (long) json.get("id"));
npc.setX((int) (long) json.get("x"));
npc.setY((int) (long) json.get("y"));
npc.hp = (int) (long) json.get("hp");
npc.setDirection(Direction.getDirection((int) (long) json.get("direction")));
npc.energy = (int) (long) json.get("energy");
npc.maxEnergy = GameServer.INSTANCE.getConfig().getInt("battery_max_energy");
return npc;
}
}

View File

@ -0,0 +1,10 @@
package net.simon987.npcplugin;
public abstract class NPCTask {
public abstract boolean checkCompleted();
public abstract void tick(NonPlayerCharacter npc);
}

View File

@ -0,0 +1,100 @@
package net.simon987.npcplugin;
import net.simon987.server.game.Action;
import net.simon987.server.game.Direction;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.pathfinding.Node;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.logging.LogManager;
import java.util.ArrayList;
public abstract class NonPlayerCharacter extends GameObject implements Updatable {
private static final int MAP_INFO = 0x0040;
protected int hp;
protected int energy;
protected int maxEnergy;
private NPCTask task;
private Action lastAction = Action.IDLE;
@Override
public char getMapInfo() {
return MAP_INFO;
}
public boolean moveTo(int x, int y, int range) {
ArrayList<Node> path = Pathfinder.findPath(getWorld(), getX(), getY(), x, y, range);
if (path != null && path.size() > 0) {
Node nextTile = path.get(1);
Direction newDirection = Direction.getFacing(getX(), getY(), nextTile.x, nextTile.y);
if (newDirection != null) {
setDirection(newDirection);
} else {
LogManager.LOGGER.severe("FIXME: moveTo:NonPlayerCharacter, Direction is null");
}
if (incrementLocation()) {
lastAction = Action.WALKING;
return true;
}
}
lastAction = Action.IDLE;
return false;
}
public boolean gotoWorld(Direction direction) {
System.out.println("going " + direction);
if (direction == Direction.NORTH) {
if (!moveTo(8, 0, 0)) {
setDirection(Direction.NORTH);
return incrementLocation();
}
} else if (direction == Direction.EAST) {
if (!moveTo(15, 8, 0)) {
setDirection(Direction.EAST);
return incrementLocation();
}
} else if (direction == Direction.SOUTH) {
if (!moveTo(7, 15, 0)) {
setDirection(Direction.SOUTH);
return incrementLocation();
}
} else if (direction == Direction.WEST) {
if (!moveTo(0, 7, 0)) {
setDirection(Direction.WEST);
return incrementLocation();
}
} else {
return false;
}
return true;
}
public NPCTask getTask() {
return task;
}
public void setTask(NPCTask task) {
this.task = task;
}
public Action getAction() {
return lastAction;
}
}

View File

@ -0,0 +1,32 @@
package net.simon987.npcplugin;
import net.simon987.npcplugin.event.WorldUpdateListener;
import net.simon987.server.game.GameObject;
import net.simon987.server.io.GameObjectDeserializer;
import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.ServerPlugin;
import org.json.simple.JSONObject;
public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer {
@Override
public void init() {
listeners.add(new WorldUpdateListener());
LogManager.LOGGER.info("Initialised NPC plugin");
}
@Override
public GameObject deserializeObject(JSONObject object) {
;
int objType = (int) (long) object.get("type");
if (objType == HarvesterNPC.ID) {
return HarvesterNPC.deserialize(object);
}
return null;
}
}

View File

@ -0,0 +1,59 @@
package net.simon987.npcplugin.event;
import net.simon987.npcplugin.HarvesterNPC;
import net.simon987.npcplugin.NonPlayerCharacter;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
import java.awt.*;
public class WorldUpdateListener implements GameEventListener {
private boolean ok = true;
@Override
public Class getListenedEventType() {
return WorldUpdateEvent.class;
}
@Override
public void handle(GameEvent event) {
//LogManager.LOGGER.info("Time is " + );
World world = ((WorldUpdateEvent) event).getWorld();
if (GameServer.INSTANCE.getGameUniverse().getTime() % 10 == 0) {
if (ok) {
ok = false;
LogManager.LOGGER.info("Spawning Harvester\n--------------------------------------");
NonPlayerCharacter npc = new HarvesterNPC();
//todo set max iteration
Point p = null;
while (p == null) {
p = world.getRandomPassableTile();
}
npc.setWorld(world);
npc.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
npc.setX(p.x);
npc.setY(p.y);
world.getGameObjects().add(npc);
}
}
}
}

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="module" module-name="Server" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
</component>
</module>

36
Plugin Plant/pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.simon987.pluginplant</groupId>
<artifactId>Plugin Plant</artifactId>
<version>1.2a</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>net.simon987.server</groupId>
<artifactId>Server</artifactId>
<version>1.2a</version>
</dependency>
</dependencies>
</project>

View File

@ -1,31 +1,13 @@
package net.simon987.plantplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.InventoryHolder;
import net.simon987.server.game.Updatable;
import org.json.simple.JSONObject;
public class Plant extends GameObject implements Updatable, InventoryHolder{
public class Plant extends GameObject implements InventoryHolder {
private static final char MAP_INFO = 0x4000;
public static final int ID = 2;
/**
* Grow time (see config.properties)
*/
private static final int GROW_TIME = GameServer.INSTANCE.getConfig().getInt("plant_grow_time");
/**
* Game time of the creation of this Plant
*/
private long creationTime;
/**
* Whether the plant is grown or not
*/
private boolean grown;
/**
* Yield of the plant, in biomass units
*/
@ -51,43 +33,12 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
json.put("id", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("creationTime", creationTime);
json.put("grown", grown);
json.put("biomassCount", biomassCount);
json.put("style", style);
return json;
}
/**
* Called every tick
*/
@Override
public void update() {
if (!grown) {
//Check grow
if (creationTime + GROW_TIME <= GameServer.INSTANCE.getGameUniverse().getTime()) {
grown = true;
biomassCount = GameServer.INSTANCE.getConfig().getInt("plant_yield");
}
}
}
public long getCreationTime() {
return creationTime;
}
public void setCreationTime(long creationTime) {
this.creationTime = creationTime;
}
public boolean isGrown() {
return grown;
}
public void setGrown(boolean grown) {
this.grown = grown;
}
public int getBiomassCount() {
return biomassCount;
@ -105,17 +56,15 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
this.style = style;
}
public static Plant deserialize(JSONObject json){
public static Plant deserialize(JSONObject json) {
Plant plant = new Plant();
plant.setObjectId((int)(long)json.get("id"));
plant.setX((int)(long)json.get("x"));
plant.setY((int)(long)json.get("y"));
plant.grown = (boolean)json.get("grown");
plant.creationTime = (long)json.get("creationTime");
plant.style = (int)(long)json.get("style");
plant.biomassCount = (int)(long)json.get("biomassCount");
plant.setObjectId((int) (long) json.get("id"));
plant.setX((int) (long) json.get("x"));
plant.setY((int) (long) json.get("y"));
plant.style = (int) (long) json.get("style");
plant.biomassCount = (int) (long) json.get("biomassCount");
return plant;
}
@ -134,7 +83,7 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
@Override
public boolean canTakeItem(int item) {
return item == ITM_BIOMASS && grown && biomassCount >= 1;
return item == ITM_BIOMASS && biomassCount >= 1;
}
/**
@ -148,9 +97,9 @@ public class Plant extends GameObject implements Updatable, InventoryHolder{
public void takeItem(int item) {
if (item == ITM_BIOMASS) {
if (grown && biomassCount > 1) {
if (biomassCount > 1) {
biomassCount--;
} else if (grown) {
} else {
//Delete plant
setDead(true);
}

View File

@ -10,7 +10,6 @@ import org.json.simple.JSONObject;
public class PlantPlugin extends ServerPlugin implements GameObjectDeserializer {
@Override
public void init() {
listeners.add(new WorldCreationListener());
@ -20,9 +19,9 @@ public class PlantPlugin extends ServerPlugin implements GameObjectDeserializer
@Override
public GameObject deserializeObject(JSONObject object) {
int objType = (int)(long)object.get("type");
int objType = (int) (long) object.get("type");
if(objType == Plant.ID) {
if (objType == Plant.ID) {
return Plant.deserialize(object);
}

View File

@ -21,9 +21,9 @@ public class WorldCreationListener implements GameEventListener {
@Override
public void handle(GameEvent event) {
ArrayList<Plant> plants = generatePlants(((WorldGenerationEvent)event).getWorld());
ArrayList<Plant> plants = generatePlants(((WorldGenerationEvent) event).getWorld());
((WorldGenerationEvent)event).getWorld().getGameObjects().addAll(plants);
((WorldGenerationEvent) event).getWorld().getGameObjects().addAll(plants);
}
@ -79,7 +79,6 @@ public class WorldCreationListener implements GameEventListener {
plant.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
plant.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
plant.setBiomassCount(plant_yield);
plant.setCreationTime(0); // Plants generated by the world generator always have creationTime of 0
plant.setX(p.x);
plant.setY(p.y);
plant.setWorld(world);

21
Server/Server.iml Normal file
View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: org.java-websocket:Java-WebSocket:1.3.6" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.42" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.json-simple:json-simple:1.1.1" level="project" />
</component>
</module>

49
Server/pom.xml Normal file
View File

@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.simon987.server</groupId>
<artifactId>Server</artifactId>
<version>1.2a</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.java-websocket</groupId>
<artifactId>Java-WebSocket</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>
</project>

View File

@ -1,7 +1,9 @@
package net.simon987.server;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventDispatcher;
import net.simon987.server.event.TickEvent;
import net.simon987.server.game.GameUniverse;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
@ -44,16 +46,16 @@ public class GameServer implements Runnable {
File pluginDir = new File("plugins/");
File[] pluginDirListing = pluginDir.listFiles();
if(pluginDirListing != null) {
for(File pluginFile : pluginDirListing) {
if (pluginDirListing != null) {
for (File pluginFile : pluginDirListing) {
if(pluginFile.getName().endsWith(".jar")){
if (pluginFile.getName().endsWith(".jar")) {
pluginManager.load(pluginFile);
}
}
} else {
if(!pluginDir.mkdir()) {
if (!pluginDir.mkdir()) {
LogManager.LOGGER.severe("Couldn't create plugin directory");
}
}
@ -107,11 +109,16 @@ public class GameServer implements Runnable {
private void tick() {
gameUniverse.incrementTime();
//Dispatch tick event
GameEvent event = new TickEvent(gameUniverse.getTime());
GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation
//Process user code
ArrayList<User> users_ = gameUniverse.getUsers();
for (User user : users_) {
if(user.getCpu() != null){
if (user.getCpu() != null) {
try {
int timeout = Math.min(user.getControlledUnit().getEnergy(), maxExecutionTime);
@ -132,7 +139,7 @@ public class GameServer implements Runnable {
//Avoid concurrent modification
ArrayList<World> worlds = new ArrayList<>(gameUniverse.getWorlds());
for (World world : worlds) {
world.update();
world.update();
}
//Save
@ -144,11 +151,13 @@ public class GameServer implements Runnable {
LogManager.LOGGER.info("Processed " + gameUniverse.getWorlds().size() + " worlds");
}
/**
* Save game universe to file in JSON format
*
* @param file JSON file to save
*/
public void save(File file){
public void save(File file) {
try {
FileWriter fileWriter = new FileWriter(file);
@ -157,7 +166,7 @@ public class GameServer implements Runnable {
JSONArray plugins = new JSONArray();
for(ServerPlugin plugin : pluginManager.getPlugins()){
for (ServerPlugin plugin : pluginManager.getPlugins()) {
plugins.add(plugin.serialise());
}

View File

@ -8,7 +8,7 @@ import java.net.InetSocketAddress;
public class Main {
public static void main(String[] args){
public static void main(String[] args) {
LogManager.initialize();
@ -22,6 +22,7 @@ public class Main {
GameServer.INSTANCE.setSocketServer(socketServer);
(new Thread(socketServer)).start();
(new Thread(GameServer.INSTANCE)).start();
}

View File

@ -208,7 +208,7 @@ public class Assembler {
} else {
//Handle integer value
char s = (char)(int)Integer.decode(value);
char s = (char) (int) Integer.decode(value);
for (int i = 0; i < factor; i++) {
out.write(Util.getHigherByte(s));
@ -281,7 +281,7 @@ public class Assembler {
if (tokens[1].toUpperCase().equals("EQU") && tokens.length == 3) {
try {
//Save value as a label
labels.put(tokens[0], (char)(int)Integer.decode(tokens[2]));
labels.put(tokens[0], (char) (int) Integer.decode(tokens[2]));
} catch (NumberFormatException e) {
throw new InvalidOperandException("Usage: constant_name EQU immediate_value", currentLine);
}

View File

@ -20,7 +20,7 @@ import java.util.HashMap;
* a Memory object and execute them. A CPU object holds registers objects &
* a Memory object.
*/
public class CPU implements JSONSerialisable{
public class CPU implements JSONSerialisable {
/**
*
@ -68,7 +68,7 @@ public class CPU implements JSONSerialisable{
/**
* Creates a new CPU
*/
public CPU(ServerConfiguration config, User user) throws CancelledException{
public CPU(ServerConfiguration config, User user) throws CancelledException {
this.config = config;
instructionSet = new DefaultInstructionSet();
registerSet = new DefaultRegisterSet();
@ -103,7 +103,7 @@ public class CPU implements JSONSerialisable{
GameEvent event = new CpuInitialisationEvent(this, user);
GameServer.INSTANCE.getEventDispatcher().dispatch(event);
if(event.isCancelled()){
if (event.isCancelled()) {
throw new CancelledException();
}
}
@ -358,7 +358,7 @@ public class CPU implements JSONSerialisable{
JSONArray hardwareList = new JSONArray();
for(Integer address : attachedHardware.keySet()){
for (Integer address : attachedHardware.keySet()) {
CpuHardware hardware = attachedHardware.get(address);
@ -376,17 +376,17 @@ public class CPU implements JSONSerialisable{
CPU cpu = new CPU(GameServer.INSTANCE.getConfig(), user);
cpu.codeSegmentOffset = (int)(long)json.get("codeSegmentOffset");
cpu.codeSegmentOffset = (int) (long) json.get("codeSegmentOffset");
JSONArray hardwareList = (JSONArray)json.get("hardware");
JSONArray hardwareList = (JSONArray) json.get("hardware");
for(JSONObject serialisedHw : (ArrayList<JSONObject>)hardwareList){
for (JSONObject serialisedHw : (ArrayList<JSONObject>) hardwareList) {
CpuHardware hw = CpuHardware.deserialize(serialisedHw);
hw.setCpu(cpu);
cpu.attachHardware(hw, (int)(long)serialisedHw.get("address"));
cpu.attachHardware(hw, (int) (long) serialisedHw.get("address"));
}
cpu.memory = Memory.deserialize((JSONObject)json.get("memory"));
cpu.memory = Memory.deserialize((JSONObject) json.get("memory"));
cpu.registerSet = RegisterSet.deserialize((JSONObject) json.get("registerSet"));
return cpu;
@ -421,18 +421,18 @@ public class CPU implements JSONSerialisable{
this.codeSegmentOffset = codeSegmentOffset;
}
public void attachHardware(CpuHardware hardware, int address){
public void attachHardware(CpuHardware hardware, int address) {
attachedHardware.put(address, hardware);
}
public void detachHardware(int address){
public void detachHardware(int address) {
attachedHardware.remove(address);
}
public boolean hardwareInterrupt(int address){
public boolean hardwareInterrupt(int address) {
CpuHardware hardware = attachedHardware.get(address);
if(hardware != null){
if (hardware != null) {
hardware.handleInterrupt(status);
return true;
} else {

View File

@ -26,14 +26,14 @@ public abstract class CpuHardware implements JSONSerialisable {
public abstract char getId();
public static CpuHardware deserialize(JSONObject hwJson){
public static CpuHardware deserialize(JSONObject hwJson) {
for(ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()){
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if(plugin instanceof CpuHardwareDeserializer){
if (plugin instanceof CpuHardwareDeserializer) {
CpuHardware hw = ((CpuHardwareDeserializer) plugin).deserializeHardware(hwJson);
if(hw != null){
if (hw != null) {
return hw;
}
}

View File

@ -56,10 +56,10 @@ public class DefaultInstructionSet implements InstructionSet {
public Instruction get(int opcode) {
Instruction instruction = instructionMap.get(opcode);
if(instruction != null){
if (instruction != null) {
return instruction;
} else {
// System.out.println("Invalid instruction " + opcode);
// System.out.println("Invalid instruction " + opcode);
//Todo: Notify user? Set error flag?
return defaultInstruction;
}

View File

@ -126,7 +126,7 @@ public class Memory implements Target, JSONSerialisable {
return json;
}
public static Memory deserialize(JSONObject json){
public static Memory deserialize(JSONObject json) {
Memory memory = new Memory(0);

View File

@ -87,7 +87,7 @@ public class RegisterSet implements Target, JSONSerialisable {
Register register = registers.get(address);
if(register != null){
if (register != null) {
return register.getValue();
} else {
return 0;
@ -106,7 +106,7 @@ public class RegisterSet implements Target, JSONSerialisable {
Register register = registers.get(address);
if(register != null){
if (register != null) {
register.setValue(value);
} else {
LogManager.LOGGER.info("DEBUG: trying to set unknown reg index : " + address);
@ -144,7 +144,7 @@ public class RegisterSet implements Target, JSONSerialisable {
@Override
public JSONObject serialise() {
JSONArray registers = new JSONArray();
for(Integer index : this.registers.keySet()){
for (Integer index : this.registers.keySet()) {
JSONObject register = new JSONObject();
register.put("index", index);
@ -164,14 +164,14 @@ public class RegisterSet implements Target, JSONSerialisable {
RegisterSet registerSet = new RegisterSet();
JSONArray registers = (JSONArray)json.get("registers");
JSONArray registers = (JSONArray) json.get("registers");
for(JSONObject jsonRegister : (ArrayList<JSONObject>)registers){
for (JSONObject jsonRegister : (ArrayList<JSONObject>) registers) {
Register register = new Register((String)jsonRegister.get("name"));
register.setValue((int)(long)jsonRegister.get("value"));
Register register = new Register((String) jsonRegister.get("name"));
register.setValue((int) (long) jsonRegister.get("value"));
registerSet.registers.put((int)(long)jsonRegister.get("index"), register);
registerSet.registers.put((int) (long) jsonRegister.get("index"), register);
}
@ -182,7 +182,7 @@ public class RegisterSet implements Target, JSONSerialisable {
public String toString() {
String str = "";
for(Integer index: registers.keySet()){
for (Integer index : registers.keySet()) {
str += index + " " + registers.get(index).getName() + "=" + Util.toHex(registers.get(index).getValue()) + "\n";
}

View File

@ -41,7 +41,7 @@ public class Util {
return s & 0x0000FFFF;
}
public static String toHex(int a){
public static String toHex(int a) {
return String.format("%04X ", uShort(a));
}

View File

@ -8,8 +8,8 @@ import net.simon987.server.assembly.Util;
/**
* Add two numbers together, the result is stored in the destination operand
* <p>
* ADD A, B
* A = A + B
* ADD A, B
* A = A + B
* </p>
*/
public class AddInstruction extends Instruction {
@ -39,8 +39,8 @@ public class AddInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int b = (char) src.get(srcIndex);
return add(a, b, status, dst, dstIndex);
}
@ -48,8 +48,8 @@ public class AddInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src;
int a = (char) dst.get(dstIndex);
int b = (char) src;
return add(a, b, status, dst, dstIndex);
}

View File

@ -8,8 +8,8 @@ import net.simon987.server.assembly.Util;
/**
* AND two numbers together, the result is stored in the destination operand
* <p>
* AND A, B
* A = A & B
* AND A, B
* A = A & B
* </p>
* FLAGS: OF=0 S=* Z=* X=0
*/
@ -27,8 +27,8 @@ public class AndInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a =(char)dst.get(dstIndex);
int b = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int b = (char) src.get(srcIndex);
int result = (a & b);
@ -45,8 +45,8 @@ public class AndInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src;
int a = (char) dst.get(dstIndex);
int b = (char) src;
int result = (a & b);

View File

@ -23,8 +23,8 @@ public class CmpInstruction extends Instruction {
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int b = (char) src.get(srcIndex);
int result = a - b;
@ -40,8 +40,8 @@ public class CmpInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src;
int a = (char) dst.get(dstIndex);
int b = (char) src;
int result = a - b;

View File

@ -28,16 +28,16 @@ public class DivInstruction extends Instruction {
public Status execute(Target src, int srcIndex, Status status) {
//Source = Y:A
int source = ((((char)cpu.getRegisterSet().getRegister("Y").getValue() & 0xFFFF) << 16)) |
((char)cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
int source = ((((char) cpu.getRegisterSet().getRegister("Y").getValue() & 0xFFFF) << 16)) |
((char) cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
if (src.get(srcIndex) == 0) {
//Division by 0
status.setBreakFlag(true);
status.setErrorFlag(true);
} else {
cpu.getRegisterSet().getRegister("A").setValue((char)(source / (char)src.get(srcIndex)));
cpu.getRegisterSet().getRegister("Y").setValue((char)(source % (char)src.get(srcIndex)));
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src.get(srcIndex)));
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src.get(srcIndex)));
}
return status;
@ -48,16 +48,16 @@ public class DivInstruction extends Instruction {
//Source = Y:A
int source = ((((char)cpu.getRegisterSet().getRegister("Y").getValue() & 0xFFFF) << 16)) |
((char)cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
int source = ((((char) cpu.getRegisterSet().getRegister("Y").getValue() & 0xFFFF) << 16)) |
((char) cpu.getRegisterSet().getRegister("A").getValue() & 0xFFFF);
if (src == 0) {
//Division by 0
status.setBreakFlag(true);
status.setErrorFlag(true);
} else {
cpu.getRegisterSet().getRegister("A").setValue((char)(source / (char)src));
cpu.getRegisterSet().getRegister("Y").setValue((char)(source % (char)src));
cpu.getRegisterSet().getRegister("A").setValue((char) (source / (char) src));
cpu.getRegisterSet().getRegister("Y").setValue((char) (source % (char) src));
}

View File

@ -18,7 +18,7 @@ public class MulInstruction extends Instruction {
@Override
public Status execute(Target src, int srcIndex, Status status) {
int result = cpu.getRegisterSet().getRegister("A").getValue() * (char)src.get(srcIndex);
int result = cpu.getRegisterSet().getRegister("A").getValue() * (char) src.get(srcIndex);
int hWord = Util.getHigherWord(result);
if (hWord != 0) {
@ -37,7 +37,7 @@ public class MulInstruction extends Instruction {
public Status execute(int src, Status status) {
int result = cpu.getRegisterSet().getRegister("A").getValue() * (char)src;
int result = cpu.getRegisterSet().getRegister("A").getValue() * (char) src;
int hWord = Util.getHigherWord(result);
if (hWord != 0) {

View File

@ -22,8 +22,8 @@ public class OrInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int b = (char) src.get(srcIndex);
int result = (a | b);
@ -40,8 +40,8 @@ public class OrInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int b = (char)src;
int a = (char) dst.get(dstIndex);
int b = (char) src;
int result = (a | b);

Some files were not shown because too many files have changed in this diff Show More