Added support for multiple dimensions

This commit is contained in:
simon
2018-02-25 14:15:03 -05:00
parent 6a1519d97d
commit 8c6e580ea9
13 changed files with 134 additions and 66 deletions

View File

@@ -56,7 +56,7 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
if (newX < 0 || newY < 0 || newX >= world.getWorldSize() || newY >= world.getWorldSize()) {
//Next tile is out of world bounds, move to next world
World nextWorld = GameServer.INSTANCE.getGameUniverse().getWorld(
world.getX() + direction.dX, world.getY() + direction.dY, true);
world.getX() + direction.dX, world.getY() + direction.dY, true, world.getDimension());
//Move object to next World
world.removeObject(this);

View File

@@ -54,17 +54,16 @@ public class GameUniverse {
*
* @return World, null if not found
*/
private World loadWorld(int x, int y){
private World loadWorld(int x, int y, String dimension) {
DB db = mongo.getDB("mar");
DBCollection worlds = db.getCollection("world");
BasicDBObject whereQuery = new BasicDBObject();
whereQuery.put("_id", World.idFromCoordinates(x,y));
whereQuery.put("_id", World.idFromCoordinates(x, y, dimension));
DBCursor cursor = worlds.find(whereQuery);
if (cursor.hasNext()) {
World w = World.deserialize(cursor.next());
return w;
return World.deserialize(cursor.next());
}
else{
return null;
@@ -80,23 +79,23 @@ public class GameUniverse {
*
* @return World, null if not found and not created.
*/
public World getWorld(int x, int y, boolean createNew) {
public World getWorld(int x, int y, boolean createNew, String dimension) {
// Wrapping coordinates around cyclically
x %= maxWidth+1;
y %= maxWidth+1;
// Looks for a previously loaded world
World world = getLoadedWorld(x,y);
World world = getLoadedWorld(x, y, dimension);
if (world != null){
return world;
}
// Tries loading the world from the database
world = loadWorld(x,y);
world = loadWorld(x, y, dimension);
if (world != null){
addWorld(world);
LogManager.LOGGER.fine("Loaded world "+(World.idFromCoordinates(x,y))+" from mongodb.");
LogManager.LOGGER.fine("Loaded world " + (World.idFromCoordinates(x, y, dimension)) + " from mongodb.");
return world;
}
@@ -105,19 +104,19 @@ public class GameUniverse {
// Creates a new world
world = createWorld(x, y);
addWorld(world);
LogManager.LOGGER.fine("Created new world "+(World.idFromCoordinates(x,y))+".");
LogManager.LOGGER.fine("Created new world " + (World.idFromCoordinates(x, y, dimension)) + ".");
return world;
} else {
return null;
}
}
}
public World getLoadedWorld(int x, int y) {
World getLoadedWorld(int x, int y, String dimension) {
// Wrapping coordinates around cyclically
x %= maxWidth+1;
y %= maxWidth+1;
return worlds.get(World.idFromCoordinates(x,y));
return worlds.get(World.idFromCoordinates(x, y, dimension));
}
/**
@@ -139,8 +138,8 @@ public class GameUniverse {
* @param x the x coordinate of the world to be removed
* @param y the y coordinate of the world to be removed
*/
public void removeWorld(int x, int y){
World w = worlds.remove(World.idFromCoordinates(x,y));
public void removeWorld(int x, int y, String dimension) {
World w = worlds.remove(World.idFromCoordinates(x, y, dimension));
if (w != null){
w.setUniverse(null);
}

View File

@@ -31,6 +31,8 @@ public class World implements MongoSerialisable {
private TileMap tileMap;
private static String dimension = "w-";
private ConcurrentHashMap<Long, GameObject> gameObjects = new ConcurrentHashMap<>(8);
/**
@@ -50,6 +52,10 @@ public class World implements MongoSerialisable {
this.worldSize = worldSize;
}
public String getDimension() {
return dimension;
}
public TileMap getTileMap() {
return tileMap;
}
@@ -70,9 +76,8 @@ public class World implements MongoSerialisable {
*
* @return long
*/
public static String idFromCoordinates(int x, int y){
return "w-"+"0x"+Integer.toHexString(x)+"-"+"0x"+Integer.toHexString(y);
//return ((long)x)*(((long)maxWidth)+1)+((long)y);
public static String idFromCoordinates(int x, int y, String dimension) {
return dimension + "0x" + Integer.toHexString(x) + "-" + "0x" + Integer.toHexString(y);
}
/**
@@ -81,7 +86,7 @@ public class World implements MongoSerialisable {
* @return long
*/
public String getId(){
return World.idFromCoordinates(x,y);
return World.idFromCoordinates(x, y, dimension);
}
public int getX() {
@@ -365,7 +370,7 @@ public class World implements MongoSerialisable {
this.universe = universe;
}
public ArrayList<World> getNeighbouringLoadedWorlds(){
private ArrayList<World> getNeighbouringLoadedWorlds() {
ArrayList<World> neighbouringWorlds = new ArrayList<>();
if (universe == null){
@@ -373,13 +378,13 @@ public class World implements MongoSerialisable {
}
for (int dx=-1; dx<=+1; dx+=2){
World nw = universe.getLoadedWorld(x+dx,y);
World nw = universe.getLoadedWorld(x + dx, y, dimension);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
for (int dy=-1; dy<=+1; dy+=2){
World nw = universe.getLoadedWorld(x,y+dy);
World nw = universe.getLoadedWorld(x, y + dy, dimension);
if (nw != null){
neighbouringWorlds.add(nw);
}
@@ -388,28 +393,29 @@ public class World implements MongoSerialisable {
return neighbouringWorlds;
}
public ArrayList<World> getNeighbouringExistingWorlds(){
ArrayList<World> neighbouringWorlds = new ArrayList<>();
if (universe == null){
return neighbouringWorlds;
}
for (int dx=-1; dx<=+1; dx+=2){
World nw = universe.getWorld(x+dx,y,false);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
for (int dy=-1; dy<=+1; dy+=2){
World nw = universe.getWorld(x,y+dy,false);
if (nw != null){
neighbouringWorlds.add(nw);
}
}
return neighbouringWorlds;
}
//Unused
// public ArrayList<World> getNeighbouringExistingWorlds(){
// ArrayList<World> neighbouringWorlds = new ArrayList<>();
//
// if (universe == null){
// return neighbouringWorlds;
// }
//
// for (int dx=-1; dx<=+1; dx+=2){
// World nw = universe.getWorld(x+dx,y,false);
// if (nw != null){
// neighbouringWorlds.add(nw);
// }
// }
// for (int dy=-1; dy<=+1; dy+=2){
// World nw = universe.getWorld(x,y+dy,false);
// if (nw != null){
// neighbouringWorlds.add(nw);
// }
// }
//
// return neighbouringWorlds;
// }
public boolean canUnload(){

View File

@@ -38,14 +38,16 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("y"),
(int) (long) json.get("newTile"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
(int) (long) json.get("worldY"),
(String) json.get("dimension")));
break;
case "createWorld":
response.put("message", createWorld(
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
(int) (long) json.get("worldY"),
(String) json.get("dimension")));
break;
@@ -54,7 +56,8 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
(int) (long) json.get("worldY"),
(String) json.get("dimension")));
break;
case "objInfo":
@@ -62,7 +65,8 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY")));
(int) (long) json.get("worldY"),
(String) json.get("dimension")));
break;
@@ -81,7 +85,8 @@ public class DebugHandler implements MessageHandler {
response.put("message", spawnObj(
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY"),
(String) json.get("data")));
(String) json.get("data"),
(String) json.get("dimension")));
break;
default:
@@ -99,9 +104,9 @@ public class DebugHandler implements MessageHandler {
/**
* Create a world at coordinates
*/
private String createWorld(int worldX, int worldY) {
private String createWorld(int worldX, int worldY, String dimension) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, true);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, true, dimension);
if (world != null) {
@@ -116,8 +121,8 @@ public class DebugHandler implements MessageHandler {
/**
* Change the tile at coordinate
*/
private String setTileAt(int x, int y, int newTile, int worldX, int worldY) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
private String setTileAt(int x, int y, int newTile, int worldX, int worldY, String dimension) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false, dimension);
if (world != null) {
@@ -129,9 +134,9 @@ public class DebugHandler implements MessageHandler {
}
}
private String spawnObj(int worldX, int worldY, String data) {
private String spawnObj(int worldX, int worldY, String data, String dimension) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false, dimension);
try {
DBObject dbObj = (DBObject) JSON.parse(data);
@@ -155,9 +160,9 @@ public class DebugHandler implements MessageHandler {
}
private String killAll(int x, int y, int worldX, int worldY) {
private String killAll(int x, int y, int worldX, int worldY, String dimension) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false, dimension);
try {
@@ -175,9 +180,9 @@ public class DebugHandler implements MessageHandler {
}
}
private String objInfo(int x, int y, int worldX, int worldY) {
private String objInfo(int x, int y, int worldX, int worldY, String dimension) {
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false, dimension);
try {
Collection<GameObject> objs = world.getGameObjects();

View File

@@ -16,15 +16,17 @@ public class ObjectsRequestHandler implements MessageHandler {
// LogManager.LOGGER.fine("(WS) Objects request from " + user.getUser().getUsername());
int x, y;
String dimension;
try {
x = Long.valueOf((long) json.get("x")).intValue();
y = Long.valueOf((long) json.get("y")).intValue();
dimension = (String) json.get("dimension");
} catch (Exception e) {
LogManager.LOGGER.severe("(WS) Malformed Objects request from " + user.getUser().getUsername());
return;
}
World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y, false);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y, false, dimension);
if (world != null) {

View File

@@ -10,14 +10,16 @@ public class TerrainRequestHandler implements MessageHandler {
@Override
public void handle(OnlineUser user, JSONObject json) {
if (json.get("t").equals("terrain") && json.containsKey("x") && json.containsKey("y")) {
if (json.get("t").equals("terrain") && json.containsKey("x") && json.containsKey("y") &&
json.containsKey("dimension")) {
// LogManager.LOGGER.fine("Terrain request from " + user.getUser().getUsername());
World world;
try {
world = GameServer.INSTANCE.getGameUniverse().getWorld(
Long.valueOf((long) json.get("x")).intValue(),
Long.valueOf((long) json.get("y")).intValue(), true);
Long.valueOf((long) json.get("y")).intValue(), false,
(String) json.get("dimension"));
} catch (NullPointerException e) {
LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler");
return;

View File

@@ -19,11 +19,13 @@ public class UserInfoRequestHandler implements MessageHandler {
if (user.isGuest()) {
json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX"));
json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY"));
json.put("dimension", GameServer.INSTANCE.getConfig().getString("new_user_dimension"));
} else {
GameObject object = (GameObject) user.getUser().getControlledUnit();
json.put("worldX", object.getWorld().getX());
json.put("worldY", object.getWorld().getY());
json.put("dimension", object.getWorld().getDimension());
}

View File

@@ -27,6 +27,7 @@ memory_size=65536
# Initial location of new user's controlled unit
new_user_worldX = 32767
new_user_worldY = 32767
new_user_dimension=w-
# Default user code
new_user_code=; Welcome to Much Assembly required!\n\
; You will find useful information on the game here: https://github.com/simon987/Much-Assembly-Required/wiki\n\