mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-10 14:26:45 +00:00
Added support for multiple dimensions
This commit is contained in:
parent
6a1519d97d
commit
8c6e580ea9
@ -30,7 +30,8 @@ public class UserCreationListener implements GameEventListener {
|
||||
while (point == null || cubot.getWorld() == null) {
|
||||
int spawnX = GameServer.INSTANCE.getConfig().getInt("new_user_worldX") + 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();
|
||||
}
|
||||
|
@ -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 + "-";
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -44,6 +44,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
this.cypherId = cypherId;
|
||||
this.randomStringGenerator = new RandomStringGenerator();
|
||||
|
||||
this.password = "12345678".toCharArray();
|
||||
}
|
||||
|
||||
|
||||
@ -66,6 +67,9 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
@Override
|
||||
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 (!open) {
|
||||
openVault();
|
||||
@ -78,16 +82,16 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
}
|
||||
}
|
||||
|
||||
private void openVault(){
|
||||
private void openVault() {
|
||||
open = true;
|
||||
openedTimer = OPEN_TIME;
|
||||
LogManager.LOGGER.fine("Opened Vault door ID: " + getObjectId());
|
||||
}
|
||||
|
||||
private void keepVaultOpen(){
|
||||
private void keepVaultOpen() {
|
||||
open = true;
|
||||
openedTimer = OPEN_TIME;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean enter(GameObject object) {
|
||||
@ -96,6 +100,8 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
|
||||
if (open) {
|
||||
//TODO: Enter in the vault
|
||||
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@ -116,6 +122,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
dbObject.put("x", getX());
|
||||
dbObject.put("y", getY());
|
||||
dbObject.put("t", ID);
|
||||
dbObject.put("pw", new String(password));
|
||||
|
||||
return dbObject;
|
||||
}
|
||||
@ -129,6 +136,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
json.put("x", getX());
|
||||
json.put("y", getY());
|
||||
json.put("t", ID);
|
||||
//Don't send the password to the client!
|
||||
|
||||
return json;
|
||||
}
|
||||
@ -139,6 +147,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
|
||||
vaultDoor.setObjectId((long) obj.get("i"));
|
||||
vaultDoor.setX((int) obj.get("x"));
|
||||
vaultDoor.setY((int) obj.get("y"));
|
||||
// vaultDoor.password = (char[]) obj.get("pw");
|
||||
|
||||
return vaultDoor;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package net.simon987.npcplugin;
|
||||
|
||||
public class VaultWorldGenerator {
|
||||
|
||||
public VaultWorld generateVaultWorld() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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(){
|
||||
|
@ -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();
|
||||
|
@ -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) {
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
|
@ -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\
|
||||
|
Loading…
x
Reference in New Issue
Block a user