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

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

View File

@@ -5,8 +5,8 @@ import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Target;
/**
* +-----------------+
* | |
* +-----------------+
* | |
* CF < 0<0<0<0<0<0<0<0 <-+
*/
public class RolInstruction extends Instruction {

View File

@@ -5,10 +5,9 @@ import net.simon987.server.assembly.Status;
import net.simon987.server.assembly.Target;
/**
* +-----------------+
* | |
* +-> 0>0>0>0>0>0>0>0 > CF
*
* +-----------------+
* | |
* +-> 0>0>0>0>0>0>0>0 > CF
*/
public class RorInstruction extends Instruction {

View File

@@ -22,8 +22,8 @@ public class ShlInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a = (char)dst.get(dstIndex);
int count = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int count = (char) src.get(srcIndex);
int result = a << count;
@@ -39,8 +39,8 @@ public class ShlInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int count = (char)src;
int a = (char) dst.get(dstIndex);
int count = (char) src;
int result = a << count;

View File

@@ -22,8 +22,8 @@ public class ShrInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, Target src, int srcIndex, Status status) {
int a = (char)dst.get(dstIndex);
int count = (char)src.get(srcIndex);
int a = (char) dst.get(dstIndex);
int count = (char) src.get(srcIndex);
int result = a >> count;
@@ -52,8 +52,8 @@ public class ShrInstruction extends Instruction {
@Override
public Status execute(Target dst, int dstIndex, int src, Status status) {
int a = (char)dst.get(dstIndex);
int count = (char)src;
int a = (char) dst.get(dstIndex);
int count = (char) src;
int result = a >> count;

View File

@@ -22,8 +22,8 @@ public class SubInstruction 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 SubInstruction 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

@@ -22,8 +22,8 @@ public class TestInstruction 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);
@@ -37,8 +37,8 @@ public class TestInstruction 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

@@ -20,8 +20,8 @@ public class XorInstruction 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);
@@ -38,8 +38,8 @@ public class XorInstruction 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

@@ -3,7 +3,7 @@ package net.simon987.server.event;
import net.simon987.server.assembly.CPU;
import net.simon987.server.user.User;
public class CpuInitialisationEvent extends GameEvent{
public class CpuInitialisationEvent extends GameEvent {
private User user;

View File

@@ -12,10 +12,10 @@ public class GameEventDispatcher {
this.pluginManager = pluginManager;
}
public void dispatch(GameEvent event){
for(ServerPlugin plugin: pluginManager.getPlugins()){
for(GameEventListener listener : plugin.getListeners()){
if(event.getClass().equals(listener.getListenedEventType())){
public void dispatch(GameEvent event) {
for (ServerPlugin plugin : pluginManager.getPlugins()) {
for (GameEventListener listener : plugin.getListeners()) {
if (event.getClass().equals(listener.getListenedEventType())) {
listener.handle(event);
}
}

View File

@@ -0,0 +1,14 @@
package net.simon987.server.event;
public class TickEvent extends GameEvent {
long time;
public TickEvent(long time) {
this.time = time;
}
public long getTime() {
return time;
}
}

View File

@@ -8,8 +8,8 @@ public class WorldGenerationEvent extends GameEvent {
setSource(world);
}
public World getWorld(){
return (World)getSource();
public World getWorld() {
return (World) getSource();
}
}

View File

@@ -0,0 +1,16 @@
package net.simon987.server.event;
import net.simon987.server.game.World;
public class WorldUpdateEvent extends GameEvent {
private World world;
public WorldUpdateEvent(World world) {
this.world = world;
}
public World getWorld() {
return world;
}
}

View File

@@ -0,0 +1,10 @@
package net.simon987.server.game;
public enum Action {
IDLE,
DIGGING,
WALKING,
WITHDRAWING,
DEPOSITING
}

View File

@@ -36,4 +36,26 @@ public enum Direction {
return null;
}
}
/**
* Get the direction so that the object at (x1, y2) faces the object at (x2, y2),
* assumes that the objects are 1 tile away (manhattan distance)
*
* @return the Direction of the first coordinates so that it faces the second coordinates
*/
public static Direction getFacing(int x1, int y1, int x2, int y2) {
if (x2 < x1) {
return WEST;
} else if (x2 > x1) {
return EAST;
} else if (y2 < y1) {
return NORTH;
} else if (y2 > y1) {
return SOUTH;
} else {
return null;
}
}
}

View File

@@ -8,7 +8,7 @@ import org.json.simple.JSONObject;
* <br>
* These effects are purely visual and could be changed or ignored by the client
*/
public class GameEffect implements JSONSerialisable{
public class GameEffect implements JSONSerialisable {
/**

View File

@@ -69,7 +69,7 @@ public abstract class GameObject implements JSONSerialisable {
}
//Check if out of World bounds / collision
if(newX < 0) {
if (newX < 0) {
//Move object to adjacent World (left)
World leftWorld;
if (world.getX() == 0) {
@@ -80,14 +80,14 @@ public abstract class GameObject implements JSONSerialisable {
leftWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() - 1, world.getY());
}
if(leftWorld != null){
if (leftWorld != null) {
world.getGameObjects().remove(this);
leftWorld.getGameObjects().add(this);
setWorld(leftWorld);
x = World.WORLD_SIZE - 1;
}
} else if(newX >= World.WORLD_SIZE) {
} else if (newX >= World.WORLD_SIZE) {
//Move object to adjacent World (right)
World rightWorld;
if (world.getX() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
@@ -97,7 +97,7 @@ public abstract class GameObject implements JSONSerialisable {
rightWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX() + 1, world.getY());
}
if(rightWorld != null){
if (rightWorld != null) {
world.getGameObjects().remove(this);
rightWorld.getGameObjects().add(this);
setWorld(rightWorld);
@@ -115,7 +115,7 @@ public abstract class GameObject implements JSONSerialisable {
upWorld = GameServer.INSTANCE.getGameUniverse().getWorld(world.getX(), world.getY() - 1);
}
if(upWorld != null){
if (upWorld != null) {
world.getGameObjects().remove(this);
upWorld.getGameObjects().add(this);
setWorld(upWorld);
@@ -217,12 +217,12 @@ public abstract class GameObject implements JSONSerialisable {
public static GameObject deserialize(JSONObject objJson) {
for(ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()){
for (ServerPlugin plugin : GameServer.INSTANCE.getPluginManager().getPlugins()) {
if(plugin instanceof GameObjectDeserializer){
if (plugin instanceof GameObjectDeserializer) {
GameObject object = ((GameObjectDeserializer) plugin).deserializeObject(objJson);
if(object != null){
if (object != null) {
return object;
}
}

View File

@@ -19,7 +19,7 @@ import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class GameUniverse implements JSONSerialisable{
public class GameUniverse implements JSONSerialisable {
private ArrayList<World> worlds;
private ArrayList<User> users;
@@ -91,17 +91,17 @@ public class GameUniverse implements JSONSerialisable{
return null;
}
public User getOrCreateUser(String username, boolean makeControlledUnit){
public User getOrCreateUser(String username, boolean makeControlledUnit) {
User user = getUser(username);
if(user != null) {
if (user != null) {
return user;
} else {
LogManager.LOGGER.info("Creating new User: " + username);
try {
if(makeControlledUnit) {
if (makeControlledUnit) {
user = new User();
user.setCpu(new CPU(GameServer.INSTANCE.getConfig(), user));
user.setUserCode(GameServer.INSTANCE.getConfig().getString("new_user_code"));
@@ -149,8 +149,8 @@ public class GameUniverse implements JSONSerialisable{
//
for (World world : worlds) {
for(GameObject object : world.getGameObjects()){
if(object.getObjectId() == id){
for (GameObject object : world.getGameObjects()) {
if (object.getObjectId() == id) {
return object;
}
}
@@ -160,7 +160,7 @@ public class GameUniverse implements JSONSerialisable{
}
public void incrementTime(){
public void incrementTime() {
time++;
}
@@ -179,13 +179,13 @@ public class GameUniverse implements JSONSerialisable{
JSONArray worlds = new JSONArray();
ArrayList<World> worlds_ = new ArrayList<>(this.worlds);
for (World world : worlds_){
for (World world : worlds_) {
worlds.add(world.serialise());
}
JSONArray users = new JSONArray();
ArrayList<User> users_ = new ArrayList<User>(this.users);
for (User user : users_){
for (User user : users_) {
if (!user.isGuest()) {
users.add(user.serialise());
}
@@ -203,9 +203,10 @@ public class GameUniverse implements JSONSerialisable{
/**
* Load game universe from JSON save file
*
* @param file JSON save file
*/
public void load(File file){
public void load(File file) {
JSONParser parser = new JSONParser();
@@ -248,7 +249,7 @@ public class GameUniverse implements JSONSerialisable{
int i = 1;
while (i < 1000) { //todo get Max guest user cap from config
if(getUser("guest" + String.valueOf(i)) != null) {
if (getUser("guest" + String.valueOf(i)) != null) {
i++;
continue;
}

View File

@@ -12,6 +12,7 @@ public interface InventoryHolder {
/**
* Take an item from the inventory
*
* @param item Desired item id (see MarConstants.ITEM_*)
*/
void takeItem(int item);

View File

@@ -102,11 +102,11 @@ public class TileMap implements JSONSerialisable {
public JSONObject serialise() {
JSONObject json = new JSONObject();
byte[] terrain = new byte[width*width];
byte[] terrain = new byte[width * width];
for (int x = 0; x < World.WORLD_SIZE; x++) {
for (int y = 0; y < World.WORLD_SIZE; y++) {
terrain[x * width + y] = (byte)tiles[x][y];
terrain[x * width + y] = (byte) tiles[x][y];
}
}
try {
@@ -133,7 +133,7 @@ public class TileMap implements JSONSerialisable {
TileMap tileMap = new TileMap(World.WORLD_SIZE, World.WORLD_SIZE);
byte[] compressedBytes = Base64.getDecoder().decode((String)object.get("zipTerrain"));
byte[] compressedBytes = Base64.getDecoder().decode((String) object.get("zipTerrain"));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();

View File

@@ -1,5 +1,8 @@
package net.simon987.server.game;
import net.simon987.server.GameServer;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.pathfinding.Pathfinder;
import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.logging.LogManager;
@@ -10,7 +13,7 @@ import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class World implements JSONSerialisable{
public class World implements JSONSerialisable {
/**
* Size of the side of a world
@@ -34,7 +37,7 @@ public class World implements JSONSerialisable{
this.tileMap = tileMap;
}
private World(){
private World() {
}
@@ -63,12 +66,16 @@ public class World implements JSONSerialisable{
return gameObjects;
}
public void update(){
public void update() {
//Dispatch update event
GameEvent event = new WorldUpdateEvent(this);
GameServer.INSTANCE.getEventDispatcher().dispatch(event); //Ignore cancellation
ArrayList<GameObject> gameObjects_ = new ArrayList<>(gameObjects);
for(GameObject object : gameObjects_){
if(object.isDead()){
for (GameObject object : gameObjects_) {
if (object.isDead()) {
gameObjects.remove(object);
LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
}
@@ -116,12 +123,12 @@ public class World implements JSONSerialisable{
public static World deserialize(JSONObject json) {
World world = new World();
world.x = (int)(long)json.get("x");
world.y = (int)(long)json.get("y");
world.x = (int) (long) json.get("x");
world.y = (int) (long) json.get("y");
world.tileMap = TileMap.deserialize((JSONObject)json.get("terrain"));
world.tileMap = TileMap.deserialize((JSONObject) json.get("terrain"));
for(JSONObject objJson : (ArrayList<JSONObject>)json.get("objects")){
for (JSONObject objJson : (ArrayList<JSONObject>) json.get("objects")) {
GameObject object = GameObject.deserialize(objJson);
@@ -183,7 +190,7 @@ public class World implements JSONSerialisable{
*
* @return random non-blocked tile
*/
public Point getRandomPassableTile(){
public Point getRandomPassableTile() {
Random random = new Random();
int counter = 0;
@@ -198,11 +205,11 @@ public class World implements JSONSerialisable{
int rx = random.nextInt(World.WORLD_SIZE);
int ry = random.nextInt(World.WORLD_SIZE);
if(!isTileBlocked(rx, ry)){
if (!isTileBlocked(rx, ry)) {
Object path = Pathfinder.findPath(this, rx, ry, 0, 6, 0);
if(path != null) {
if (path != null) {
return new Point(rx, ry);
}
}

View File

@@ -92,7 +92,7 @@ public class WorldGenerator {
/**
* Create a randomly generated World
*/
public World generateWorld(int locX, int locY) throws CancelledException{
public World generateWorld(int locX, int locY) throws CancelledException {
LogManager.LOGGER.info("Generating random world");
Random random = new Random();
@@ -186,7 +186,7 @@ public class WorldGenerator {
GameEvent event = new WorldGenerationEvent(world);
GameServer.INSTANCE.getEventDispatcher().dispatch(event);
if(event.isCancelled()){
if (event.isCancelled()) {
throw new CancelledException();
}

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