mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-04-10 14:26:45 +00:00
Support for variable World size
This commit is contained in:
parent
9bb0dc9034
commit
4293fc0315
@ -6,7 +6,6 @@ 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;
|
||||
import net.simon987.server.game.pathfinding.Pathfinder;
|
||||
import net.simon987.server.io.JSONSerialisable;
|
||||
@ -115,8 +114,8 @@ public class CubotLidar extends CpuHardware implements JSONSerialisable {
|
||||
char[][] mapInfo = cubot.getWorld().getMapInfo();
|
||||
|
||||
int i = MEMORY_MAP_START;
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < cubot.getWorld().getWorldSize(); y++) {
|
||||
for (int x = 0; x < cubot.getWorld().getWorldSize(); x++) {
|
||||
getCpu().getMemory().set(i++, mapInfo[x][y]);
|
||||
}
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class WorldCreationListener implements GameEventListener {
|
||||
//Also spawn a radio tower in the same World
|
||||
Point p = world.getRandomPassableTile();
|
||||
if (p != null) {
|
||||
while (p.x == 0 || p.x == World.WORLD_SIZE - 1 || p.y == World.WORLD_SIZE - 1 || p.y == 0) {
|
||||
while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0) {
|
||||
p = world.getRandomPassableTile();
|
||||
|
||||
if (p == null) {
|
||||
|
@ -23,8 +23,8 @@ public class WorldUtils {
|
||||
//set the desired amount of blobs to the plain tile count
|
||||
int[][] tiles = world.getTileMap().getTiles();
|
||||
int plainCount = 0;
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < world.getWorldSize(); y++) {
|
||||
for (int x = 0; x < world.getWorldSize(); x++) {
|
||||
|
||||
if (tiles[x][y] == 0) {
|
||||
plainCount++;
|
||||
@ -44,7 +44,7 @@ public class WorldUtils {
|
||||
|
||||
//Don't block worlds
|
||||
int counter = 0;
|
||||
while (p.x == 0 || p.y == 0 || p.x == World.WORLD_SIZE - 1 || p.y == World.WORLD_SIZE - 1 ||
|
||||
while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 ||
|
||||
world.getGameObjectsAt(p.x, p.y).size() != 0) {
|
||||
p = world.getTileMap().getRandomPlainTile();
|
||||
counter++;
|
||||
|
@ -90,9 +90,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
|
||||
leftWorld.incUpdatable();
|
||||
setWorld(leftWorld);
|
||||
|
||||
x = World.WORLD_SIZE - 1;
|
||||
x = leftWorld.getWorldSize() - 1;
|
||||
}
|
||||
} else if (newX >= World.WORLD_SIZE) {
|
||||
} else if (newX >= world.getWorldSize()) {
|
||||
//Move object to adjacent World (right)
|
||||
World rightWorld;
|
||||
if (world.getX() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
|
||||
@ -129,9 +129,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
|
||||
upWorld.incUpdatable();
|
||||
setWorld(upWorld);
|
||||
|
||||
y = World.WORLD_SIZE - 1;
|
||||
y = upWorld.getWorldSize() - 1;
|
||||
}
|
||||
} else if (newY >= World.WORLD_SIZE) {
|
||||
} else if (newY >= world.getWorldSize()) {
|
||||
//Move object to adjacent World (down)
|
||||
World downWorld;
|
||||
if (world.getY() == GameServer.INSTANCE.getGameUniverse().getMaxWidth()) {
|
||||
|
@ -15,8 +15,6 @@ import java.util.Base64;
|
||||
import java.util.Random;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.Inflater;
|
||||
import java.util.zip.InflaterOutputStream;
|
||||
|
||||
/**
|
||||
* A 2D map of Tile objects of size width*height
|
||||
@ -56,9 +54,9 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
|
||||
tiles = new int[width][height];
|
||||
}
|
||||
|
||||
public TileMap(int[][] tiles) {
|
||||
this.width = World.WORLD_SIZE;
|
||||
this.height = World.WORLD_SIZE;
|
||||
public TileMap(int[][] tiles, int size) {
|
||||
this.width = size;
|
||||
this.height = size;
|
||||
|
||||
this.tiles = tiles;
|
||||
}
|
||||
@ -115,8 +113,8 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
|
||||
|
||||
byte[] terrain = new byte[width * width];
|
||||
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < width; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
terrain[x * width + y] = (byte) tiles[x][y];
|
||||
}
|
||||
}
|
||||
@ -150,53 +148,22 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
|
||||
|
||||
}
|
||||
|
||||
public static TileMap deserialize(DBObject object) {
|
||||
public static TileMap deserialize(DBObject object, int size) {
|
||||
|
||||
BasicDBList terrain = (BasicDBList) object.get("tiles");
|
||||
|
||||
int[][] tiles = new int[World.WORLD_SIZE][World.WORLD_SIZE];
|
||||
int[][] tiles = new int[size][size];
|
||||
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < size; x++) {
|
||||
for (int y = 0; y < size; y++) {
|
||||
tiles[x][y] = (int) ((BasicDBList) terrain.get(x)).get(y);
|
||||
}
|
||||
}
|
||||
|
||||
return new TileMap(tiles);
|
||||
return new TileMap(tiles, size);
|
||||
|
||||
}
|
||||
|
||||
public static TileMap deserialize(JSONObject object) {
|
||||
|
||||
TileMap tileMap = new TileMap(World.WORLD_SIZE, World.WORLD_SIZE);
|
||||
|
||||
byte[] compressedBytes = Base64.getDecoder().decode((String) object.get("z"));
|
||||
|
||||
try {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Inflater decompressor = new Inflater(true);
|
||||
InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(baos, decompressor);
|
||||
inflaterOutputStream.write(compressedBytes);
|
||||
inflaterOutputStream.close();
|
||||
|
||||
byte[] terrain = baos.toByteArray();
|
||||
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
tileMap.tiles[x][y] = terrain[x * World.WORLD_SIZE + y];
|
||||
}
|
||||
}
|
||||
|
||||
return tileMap;
|
||||
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public Point getRandomPlainTile() {
|
||||
|
||||
Random random = new Random();
|
||||
@ -210,8 +177,8 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
|
||||
return null;
|
||||
}
|
||||
|
||||
int rx = random.nextInt(World.WORLD_SIZE);
|
||||
int ry = random.nextInt(World.WORLD_SIZE);
|
||||
int rx = random.nextInt(width);
|
||||
int ry = random.nextInt(height);
|
||||
|
||||
if (tiles[rx][ry] == TileMap.PLAIN_TILE) {
|
||||
return new Point(rx, ry);
|
||||
|
@ -8,7 +8,6 @@ import net.simon987.server.event.GameEvent;
|
||||
import net.simon987.server.event.WorldUpdateEvent;
|
||||
import net.simon987.server.game.pathfinding.Pathfinder;
|
||||
import net.simon987.server.io.MongoSerialisable;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.ArrayList;
|
||||
@ -17,9 +16,9 @@ import java.util.Random;
|
||||
public class World implements MongoSerialisable {
|
||||
|
||||
/**
|
||||
* Size of the side of a world
|
||||
* Size of the side of this world
|
||||
*/
|
||||
public static final int WORLD_SIZE = 16;
|
||||
private int worldSize;
|
||||
|
||||
private static final char INFO_BLOCKED = 0x8000;
|
||||
private static final char INFO_IRON = 0x0200;
|
||||
@ -41,10 +40,12 @@ public class World implements MongoSerialisable {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.tileMap = tileMap;
|
||||
|
||||
this.worldSize = tileMap.getWidth();
|
||||
}
|
||||
|
||||
private World() {
|
||||
|
||||
private World(int worldSize) {
|
||||
this.worldSize = worldSize;
|
||||
}
|
||||
|
||||
public TileMap getTileMap() {
|
||||
@ -129,6 +130,7 @@ public class World implements MongoSerialisable {
|
||||
|
||||
dbObject.put("x", x);
|
||||
dbObject.put("y", y);
|
||||
dbObject.put("size", worldSize);
|
||||
|
||||
dbObject.put("updatable", updatable);
|
||||
|
||||
@ -142,8 +144,8 @@ public class World implements MongoSerialisable {
|
||||
String str = "World (" + x + ", " + y + ")\n";
|
||||
int[][] tileMap = this.tileMap.getTiles();
|
||||
|
||||
for (int x = 0; x < WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < worldSize; x++) {
|
||||
for (int y = 0; y < worldSize; y++) {
|
||||
str += tileMap[x][y] + " ";
|
||||
}
|
||||
str += "\n";
|
||||
@ -153,34 +155,14 @@ public class World implements MongoSerialisable {
|
||||
|
||||
}
|
||||
|
||||
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.updatable = (int) (long) json.get("u");
|
||||
//
|
||||
// world.tileMap = TileMap.deserialize((JSONObject) json.get("t"));
|
||||
//
|
||||
//
|
||||
// for (JSONObject objJson : (ArrayList<JSONObject>) json.get("o")) {
|
||||
//
|
||||
// GameObject object = GameObject.deserialize(objJson);
|
||||
//
|
||||
// object.setWorld(world);
|
||||
// world.gameObjects.add(object);
|
||||
// }
|
||||
|
||||
return world;
|
||||
}
|
||||
|
||||
public static World deserialize(DBObject dbObject) {
|
||||
|
||||
World world = new World();
|
||||
World world = new World((int) dbObject.get("size"));
|
||||
world.x = (int) dbObject.get("x");
|
||||
world.y = (int) dbObject.get("y");
|
||||
world.updatable = (int) dbObject.get("updatable");
|
||||
|
||||
world.tileMap = TileMap.deserialize((BasicDBObject) dbObject.get("terrain"));
|
||||
world.tileMap = TileMap.deserialize((BasicDBObject) dbObject.get("terrain"), world.getWorldSize());
|
||||
|
||||
BasicDBList objects = (BasicDBList) dbObject.get("objects");
|
||||
|
||||
@ -204,24 +186,23 @@ public class World implements MongoSerialisable {
|
||||
*/
|
||||
public char[][] getMapInfo() {
|
||||
|
||||
char[][] mapInfo = new char[World.WORLD_SIZE][World.WORLD_SIZE];
|
||||
char[][] mapInfo = new char[worldSize][worldSize];
|
||||
int[][] tiles = tileMap.getTiles();
|
||||
|
||||
//Tile
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < worldSize; y++) {
|
||||
for (int x = 0; x < worldSize; x++) {
|
||||
|
||||
if (tiles[x][y] == TileMap.PLAIN_TILE) {
|
||||
|
||||
mapInfo[x][y] = 0;
|
||||
|
||||
} else if (tiles[x][y] == TileMap.WALL_TILE) {
|
||||
|
||||
mapInfo[x][y] = INFO_BLOCKED;
|
||||
|
||||
} else if (tiles[x][y] == TileMap.COPPER_TILE) {
|
||||
|
||||
mapInfo[x][y] = INFO_COPPER;
|
||||
} else if (tiles[x][y] == TileMap.IRON_TILE) {
|
||||
|
||||
} else if (tiles[x][y] == TileMap.IRON_TILE) {
|
||||
mapInfo[x][y] = INFO_IRON;
|
||||
}
|
||||
}
|
||||
@ -259,8 +240,8 @@ public class World implements MongoSerialisable {
|
||||
return null;
|
||||
}
|
||||
|
||||
int rx = random.nextInt(World.WORLD_SIZE);
|
||||
int ry = random.nextInt(World.WORLD_SIZE);
|
||||
int rx = random.nextInt(worldSize);
|
||||
int ry = random.nextInt(worldSize);
|
||||
|
||||
if (!isTileBlocked(rx, ry)) {
|
||||
|
||||
@ -330,4 +311,8 @@ public class World implements MongoSerialisable {
|
||||
public boolean shouldUpdate() {
|
||||
return updatable > 0;
|
||||
}
|
||||
|
||||
public int getWorldSize() {
|
||||
return worldSize;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,8 @@ public class WorldGenerator {
|
||||
private int minCopperCount;
|
||||
private int maxCopperCount;
|
||||
|
||||
private static final int DEFAULT_WORLD_SIZE = 24;
|
||||
|
||||
/**
|
||||
* Map of center points
|
||||
*/
|
||||
@ -85,7 +87,7 @@ public class WorldGenerator {
|
||||
*/
|
||||
private static World generateEmptyWorld(int locX, int locY) {
|
||||
|
||||
return new World(locX, locY, new TileMap(World.WORLD_SIZE, World.WORLD_SIZE));
|
||||
return new World(locX, locY, new TileMap(DEFAULT_WORLD_SIZE, DEFAULT_WORLD_SIZE));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -104,12 +106,12 @@ public class WorldGenerator {
|
||||
for (int i = centerPointCount; i >= 0; i--) {
|
||||
|
||||
int tile = random.nextInt(wallPlainRatio) == 0 ? 1 : 0;
|
||||
centerPointsMap.put(new Point(random.nextInt(World.WORLD_SIZE), random.nextInt(World.WORLD_SIZE)), tile);
|
||||
centerPointsMap.put(new Point(random.nextInt(DEFAULT_WORLD_SIZE), random.nextInt(DEFAULT_WORLD_SIZE)), tile);
|
||||
}
|
||||
|
||||
//Fill unset tiles
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < DEFAULT_WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < DEFAULT_WORLD_SIZE; x++) {
|
||||
int tile = getClosestCenterPointTile(x, y);
|
||||
/*
|
||||
* There is 1-tile thick wall around the World, with 4-tile wide entrances
|
||||
@ -134,7 +136,7 @@ public class WorldGenerator {
|
||||
* 1 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1
|
||||
*/
|
||||
|
||||
if (x == 0 || x == World.WORLD_SIZE - 1) {
|
||||
if (x == 0 || x == DEFAULT_WORLD_SIZE - 1) {
|
||||
//Vertical (West & East) walls
|
||||
if (y < 6 || y > 9) {
|
||||
tile = 1;
|
||||
@ -142,7 +144,7 @@ public class WorldGenerator {
|
||||
tile = 0;
|
||||
}
|
||||
}
|
||||
if (y == 0 || y == World.WORLD_SIZE - 1) {
|
||||
if (y == 0 || y == DEFAULT_WORLD_SIZE - 1) {
|
||||
// Horizontal (North & South) walls
|
||||
if (x < 6 || x > 9) {
|
||||
tile = 1;
|
||||
@ -150,8 +152,8 @@ public class WorldGenerator {
|
||||
tile = 0;
|
||||
}
|
||||
}
|
||||
if (((x == 1 || x == World.WORLD_SIZE - 2) && y > 0 && y < World.WORLD_SIZE - 1) ||
|
||||
((y == 1 || y == World.WORLD_SIZE - 2) && x > 0 && x < World.WORLD_SIZE - 1)) {
|
||||
if (((x == 1 || x == DEFAULT_WORLD_SIZE - 2) && y > 0 && y < DEFAULT_WORLD_SIZE - 1) ||
|
||||
((y == 1 || y == DEFAULT_WORLD_SIZE - 2) && x > 0 && x < DEFAULT_WORLD_SIZE - 1)) {
|
||||
//Inner border
|
||||
tile = 0;
|
||||
}
|
||||
|
@ -47,10 +47,10 @@ public class Pathfinder {
|
||||
SortedArrayList open = new SortedArrayList();
|
||||
|
||||
//Initialize node map
|
||||
Node[][] nodes = new Node[World.WORLD_SIZE][World.WORLD_SIZE];
|
||||
Node[][] nodes = new Node[world.getWorldSize()][world.getWorldSize()];
|
||||
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < world.getWorldSize(); x++) {
|
||||
for (int y = 0; y < world.getWorldSize(); y++) {
|
||||
|
||||
nodes[x][y] = new Node(x, y);
|
||||
|
||||
@ -66,9 +66,7 @@ public class Pathfinder {
|
||||
open.add(start);
|
||||
|
||||
|
||||
int counter = 0;
|
||||
while (open.size() > 0) {
|
||||
counter++;
|
||||
Node current = open.first();
|
||||
|
||||
if (Util.manhattanDist(current.x, current.y, gX, gY) <= range) {
|
||||
@ -138,7 +136,7 @@ public class Pathfinder {
|
||||
}
|
||||
|
||||
//Check if the right neighbor is within the World boundaries and isn't blocked
|
||||
if (node.x != (World.WORLD_SIZE - 1) && !world.isTileBlocked(node.x + 1, node.y)) {
|
||||
if (node.x != (world.getWorldSize() - 1) && !world.isTileBlocked(node.x + 1, node.y)) {
|
||||
neighbors.add(nodes[node.x + 1][node.y]);
|
||||
}
|
||||
|
||||
@ -148,7 +146,7 @@ public class Pathfinder {
|
||||
}
|
||||
|
||||
//Check if the bottom neighbor is within the World boundaries and isn't blocked
|
||||
if (node.y != (World.WORLD_SIZE - 1) && !world.isTileBlocked(node.x, node.y + 1)) {
|
||||
if (node.y != (world.getWorldSize() - 1) && !world.isTileBlocked(node.x, node.y + 1)) {
|
||||
neighbors.add(nodes[node.x][node.y + 1]);
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ public class TerrainRequestHandler implements MessageHandler {
|
||||
try {
|
||||
world = GameServer.INSTANCE.getGameUniverse().getWorld(
|
||||
Long.valueOf((long) json.get("x")).intValue(),
|
||||
Long.valueOf((long) json.get("y")).intValue(), false);
|
||||
Long.valueOf((long) json.get("y")).intValue(), true);
|
||||
} catch (NullPointerException e) {
|
||||
LogManager.LOGGER.severe("FIXME: handle TerrainRequestHandler");
|
||||
return;
|
||||
@ -31,8 +31,8 @@ public class TerrainRequestHandler implements MessageHandler {
|
||||
JSONArray terrain = new JSONArray();
|
||||
|
||||
int[][] tiles = world.getTileMap().getTiles();
|
||||
for (int x = 0; x < World.WORLD_SIZE; x++) {
|
||||
for (int y = 0; y < World.WORLD_SIZE; y++) {
|
||||
for (int x = 0; x < world.getWorldSize(); x++) {
|
||||
for (int y = 0; y < world.getWorldSize(); y++) {
|
||||
terrain.add(tiles[y][x]);
|
||||
}
|
||||
}
|
||||
@ -40,6 +40,7 @@ public class TerrainRequestHandler implements MessageHandler {
|
||||
response.put("t", "terrain");
|
||||
response.put("ok", true);
|
||||
response.put("terrain", terrain);
|
||||
response.put("size", world.getWorldSize());
|
||||
|
||||
user.getWebSocket().send(response.toJSONString());
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user