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)");