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

@ -30,7 +30,8 @@ public class UserCreationListener implements GameEventListener {
while (point == null || cubot.getWorld() == null) { while (point == null || cubot.getWorld() == null) {
int spawnX = GameServer.INSTANCE.getConfig().getInt("new_user_worldX") + random.nextInt(5); int spawnX = GameServer.INSTANCE.getConfig().getInt("new_user_worldX") + random.nextInt(5);
int spawnY = GameServer.INSTANCE.getConfig().getInt("new_user_worldY") + random.nextInt(5); int spawnY = GameServer.INSTANCE.getConfig().getInt("new_user_worldY") + random.nextInt(5);
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(spawnX, spawnY, true)); String dimension = GameServer.INSTANCE.getConfig().getString("new_user_dimension");
cubot.setWorld(GameServer.INSTANCE.getGameUniverse().getWorld(spawnX, spawnY, true, dimension));
point = cubot.getWorld().getRandomPassableTile(); point = cubot.getWorld().getRandomPassableTile();
} }

View File

@ -0,0 +1,17 @@
package net.simon987.npcplugin;
public class VaultDimension {
/**
* Name of the dimension
*/
private String name;
public VaultDimension(long vaultDoorId) {
name = "v" + vaultDoorId + "-";
}
}

View File

@ -44,6 +44,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
this.cypherId = cypherId; this.cypherId = cypherId;
this.randomStringGenerator = new RandomStringGenerator(); this.randomStringGenerator = new RandomStringGenerator();
this.password = "12345678".toCharArray();
} }
@ -66,6 +67,9 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
@Override @Override
public boolean sendMessage(char[] message) { public boolean sendMessage(char[] message) {
System.out.println("message: " + new String(message));
System.out.println("password: " + new String(password));
if (Arrays.equals(message, password)) { if (Arrays.equals(message, password)) {
if (!open) { if (!open) {
openVault(); openVault();
@ -78,13 +82,13 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
} }
} }
private void openVault(){ private void openVault() {
open = true; open = true;
openedTimer = OPEN_TIME; openedTimer = OPEN_TIME;
LogManager.LOGGER.fine("Opened Vault door ID: " + getObjectId()); LogManager.LOGGER.fine("Opened Vault door ID: " + getObjectId());
} }
private void keepVaultOpen(){ private void keepVaultOpen() {
open = true; open = true;
openedTimer = OPEN_TIME; openedTimer = OPEN_TIME;
} }
@ -96,6 +100,8 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
if (open) { if (open) {
//TODO: Enter in the vault //TODO: Enter in the vault
return true; return true;
} else { } else {
return false; return false;
@ -116,6 +122,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
dbObject.put("x", getX()); dbObject.put("x", getX());
dbObject.put("y", getY()); dbObject.put("y", getY());
dbObject.put("t", ID); dbObject.put("t", ID);
dbObject.put("pw", new String(password));
return dbObject; return dbObject;
} }
@ -129,6 +136,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
json.put("x", getX()); json.put("x", getX());
json.put("y", getY()); json.put("y", getY());
json.put("t", ID); json.put("t", ID);
//Don't send the password to the client!
return json; return json;
} }
@ -139,6 +147,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
vaultDoor.setObjectId((long) obj.get("i")); vaultDoor.setObjectId((long) obj.get("i"));
vaultDoor.setX((int) obj.get("x")); vaultDoor.setX((int) obj.get("x"));
vaultDoor.setY((int) obj.get("y")); vaultDoor.setY((int) obj.get("y"));
// vaultDoor.password = (char[]) obj.get("pw");
return vaultDoor; return vaultDoor;
} }

View File

@ -0,0 +1,15 @@
package net.simon987.npcplugin;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
public class VaultWorld extends World {
private String dimension;
public VaultWorld(int x, int y, TileMap tileMap) {
super(x, y, tileMap);
}
}

View File

@ -0,0 +1,9 @@
package net.simon987.npcplugin;
public class VaultWorldGenerator {
public VaultWorld generateVaultWorld() {
return null;
}
}

View File

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

View File

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

View File

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

View File

@ -38,14 +38,16 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("y"), (int) (long) json.get("y"),
(int) (long) json.get("newTile"), (int) (long) json.get("newTile"),
(int) (long) json.get("worldX"), (int) (long) json.get("worldX"),
(int) (long) json.get("worldY"))); (int) (long) json.get("worldY"),
(String) json.get("dimension")));
break; break;
case "createWorld": case "createWorld":
response.put("message", createWorld( response.put("message", createWorld(
(int) (long) json.get("worldX"), (int) (long) json.get("worldX"),
(int) (long) json.get("worldY"))); (int) (long) json.get("worldY"),
(String) json.get("dimension")));
break; break;
@ -54,7 +56,8 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("x"), (int) (long) json.get("x"),
(int) (long) json.get("y"), (int) (long) json.get("y"),
(int) (long) json.get("worldX"), (int) (long) json.get("worldX"),
(int) (long) json.get("worldY"))); (int) (long) json.get("worldY"),
(String) json.get("dimension")));
break; break;
case "objInfo": case "objInfo":
@ -62,7 +65,8 @@ public class DebugHandler implements MessageHandler {
(int) (long) json.get("x"), (int) (long) json.get("x"),
(int) (long) json.get("y"), (int) (long) json.get("y"),
(int) (long) json.get("worldX"), (int) (long) json.get("worldX"),
(int) (long) json.get("worldY"))); (int) (long) json.get("worldY"),
(String) json.get("dimension")));
break; break;
@ -81,7 +85,8 @@ public class DebugHandler implements MessageHandler {
response.put("message", spawnObj( response.put("message", spawnObj(
(int) (long) json.get("worldX"), (int) (long) json.get("worldX"),
(int) (long) json.get("worldY"), (int) (long) json.get("worldY"),
(String) json.get("data"))); (String) json.get("data"),
(String) json.get("dimension")));
break; break;
default: default:
@ -99,9 +104,9 @@ public class DebugHandler implements MessageHandler {
/** /**
* Create a world at coordinates * 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) { if (world != null) {
@ -116,8 +121,8 @@ public class DebugHandler implements MessageHandler {
/** /**
* Change the tile at coordinate * Change the tile at coordinate
*/ */
private String setTileAt(int x, int y, int newTile, int worldX, int worldY) { private String setTileAt(int x, int y, int newTile, 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);
if (world != null) { 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 { try {
DBObject dbObj = (DBObject) JSON.parse(data); 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 { 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 { try {
Collection<GameObject> objs = world.getGameObjects(); 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()); // LogManager.LOGGER.fine("(WS) Objects request from " + user.getUser().getUsername());
int x, y; int x, y;
String dimension;
try { try {
x = Long.valueOf((long) json.get("x")).intValue(); x = Long.valueOf((long) json.get("x")).intValue();
y = Long.valueOf((long) json.get("y")).intValue(); y = Long.valueOf((long) json.get("y")).intValue();
dimension = (String) json.get("dimension");
} catch (Exception e) { } catch (Exception e) {
LogManager.LOGGER.severe("(WS) Malformed Objects request from " + user.getUser().getUsername()); LogManager.LOGGER.severe("(WS) Malformed Objects request from " + user.getUser().getUsername());
return; return;
} }
World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y, false); World world = GameServer.INSTANCE.getGameUniverse().getWorld(x, y, false, dimension);
if (world != null) { if (world != null) {

View File

@ -10,14 +10,16 @@ public class TerrainRequestHandler implements MessageHandler {
@Override @Override
public void handle(OnlineUser user, JSONObject json) { 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()); // LogManager.LOGGER.fine("Terrain request from " + user.getUser().getUsername());
World world; World world;
try { try {
world = GameServer.INSTANCE.getGameUniverse().getWorld( world = GameServer.INSTANCE.getGameUniverse().getWorld(
Long.valueOf((long) json.get("x")).intValue(), 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) { } catch (NullPointerException e) {
LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler"); LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler");
return; return;

View File

@ -19,11 +19,13 @@ public class UserInfoRequestHandler implements MessageHandler {
if (user.isGuest()) { if (user.isGuest()) {
json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX")); json.put("worldX", GameServer.INSTANCE.getConfig().getInt("new_user_worldX"));
json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY")); json.put("worldY", GameServer.INSTANCE.getConfig().getInt("new_user_worldY"));
json.put("dimension", GameServer.INSTANCE.getConfig().getString("new_user_dimension"));
} else { } else {
GameObject object = (GameObject) user.getUser().getControlledUnit(); GameObject object = (GameObject) user.getUser().getControlledUnit();
json.put("worldX", object.getWorld().getX()); json.put("worldX", object.getWorld().getX());
json.put("worldY", object.getWorld().getY()); 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 # Initial location of new user's controlled unit
new_user_worldX = 32767 new_user_worldX = 32767
new_user_worldY = 32767 new_user_worldY = 32767
new_user_dimension=w-
# Default user code # Default user code
new_user_code=; Welcome to Much Assembly required!\n\ 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\ ; You will find useful information on the game here: https://github.com/simon987/Much-Assembly-Required/wiki\n\