Merge pull request #5 from simon987/master

Updated project base.
This commit is contained in:
Deniz Beştepe 2018-11-25 20:47:50 +03:00 committed by GitHub
commit 5a12296180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 288 additions and 107 deletions

View File

@ -1,8 +1,10 @@
package net.simon987.cubotplugin; package net.simon987.cubotplugin;
import net.simon987.server.assembly.Status; import net.simon987.server.assembly.Status;
import net.simon987.server.game.item.Item;
import net.simon987.server.game.objects.Action; import net.simon987.server.game.objects.Action;
import net.simon987.server.game.objects.ControllableUnit; import net.simon987.server.game.objects.ControllableUnit;
import net.simon987.server.game.world.Tile;
import org.bson.Document; import org.bson.Document;
public class CubotDrill extends CubotHardwareModule { public class CubotDrill extends CubotHardwareModule {
@ -43,9 +45,13 @@ public class CubotDrill extends CubotHardwareModule {
if (cubot.spendEnergy(1400)) { if (cubot.spendEnergy(1400)) {
if (cubot.getCurrentAction() == Action.IDLE) { if (cubot.getCurrentAction() == Action.IDLE) {
//TODO: Get Tile instance and call onDig() Tile tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
//int tile = cubot.getWorld().getTileMap().getTileAt(cubot.getX(), cubot.getY());
//cubot.setCurrentAction(Action.DIGGING); Item newItem = tile.drill();
if (newItem != null) {
cubot.setCurrentAction(Action.DIGGING);
cubot.giveItem(newItem);
}
} }
} }
} }

View File

@ -4,6 +4,8 @@ import net.simon987.npcplugin.event.CpuInitialisationListener;
import net.simon987.npcplugin.event.VaultCompleteListener; import net.simon987.npcplugin.event.VaultCompleteListener;
import net.simon987.npcplugin.event.VaultWorldUpdateListener; import net.simon987.npcplugin.event.VaultWorldUpdateListener;
import net.simon987.npcplugin.event.WorldCreationListener; import net.simon987.npcplugin.event.WorldCreationListener;
import net.simon987.npcplugin.world.TileVaultFloor;
import net.simon987.npcplugin.world.TileVaultWall;
import net.simon987.server.ServerConfiguration; import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
@ -37,6 +39,9 @@ public class NpcPlugin extends ServerPlugin {
registry.registerHardware(RadioReceiverHardware.class); registry.registerHardware(RadioReceiverHardware.class);
registry.registerTile(TileVaultFloor.ID, TileVaultFloor.class);
registry.registerTile(TileVaultWall.ID, TileVaultWall.class);
radioTowers = new ArrayList<>(32); radioTowers = new ArrayList<>(32);
LogManager.LOGGER.info("Initialised NPC plugin"); LogManager.LOGGER.info("Initialised NPC plugin");

View File

@ -1,10 +1,10 @@
package net.simon987.npcplugin; package net.simon987.npcplugin;
import net.simon987.npcplugin.world.TileVaultFloor;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.ServerConfiguration;
import net.simon987.server.game.objects.Direction; import net.simon987.server.game.objects.Direction;
import net.simon987.server.game.world.Location; import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -140,7 +140,7 @@ public class VaultDimension {
//4. Choose a random world from the last layer and create the vault box there (objective) //4. Choose a random world from the last layer and create the vault box there (objective)
World objectiveWorld = lastLayerWorlds.get(random.nextInt(lastLayerWorlds.size())); World objectiveWorld = lastLayerWorlds.get(random.nextInt(lastLayerWorlds.size()));
Point exitPortalPt = objectiveWorld.getRandomTileWithAdjacent(8, TileMap.VAULT_FLOOR); Point exitPortalPt = objectiveWorld.getRandomTileWithAdjacent(8, TileVaultFloor.ID);
if (exitPortalPt != null) { if (exitPortalPt != null) {
@ -159,7 +159,7 @@ public class VaultDimension {
} }
//5. Create an exit portal in the home World //5. Create an exit portal in the home World
Point homePortalPt = homeWorld.getRandomTileWithAdjacent(8, TileMap.VAULT_FLOOR); Point homePortalPt = homeWorld.getRandomTileWithAdjacent(8, TileVaultFloor.ID);
if (homePortalPt != null) { if (homePortalPt != null) {
Portal homePortal = new Portal(); Portal homePortal = new Portal();

View File

@ -2,6 +2,7 @@ package net.simon987.npcplugin;
import net.simon987.server.game.objects.Direction; import net.simon987.server.game.objects.Direction;
import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.TileVoid;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import java.awt.*; import java.awt.*;
@ -216,7 +217,7 @@ public class VaultWorldGenerator {
for (int x = 0; x < worldSize; x++) { for (int x = 0; x < worldSize; x++) {
for (int y = 0; y < worldSize; y++) { for (int y = 0; y < worldSize; y++) {
if (map.getTileAt(x, y) != floorTile && hasTileAdjacent(x, y, map, floorTile)) { if (map.getTileIdAt(x, y) != floorTile && hasTileAdjacent(x, y, map, floorTile)) {
map.setTileAt(wallTile, x, y); map.setTileAt(wallTile, x, y);
} }
} }
@ -226,8 +227,8 @@ public class VaultWorldGenerator {
for (int x = 0; x < worldSize; x++) { for (int x = 0; x < worldSize; x++) {
for (int y = 0; y < worldSize; y++) { for (int y = 0; y < worldSize; y++) {
if (map.getTileAt(x, y) != floorTile && map.getTileAt(x, y) != wallTile) { if (map.getTileIdAt(x, y) != floorTile && map.getTileIdAt(x, y) != wallTile) {
map.setTileAt(TileMap.VOID, x, y); map.setTileAt(new TileVoid(), x, y);
} }
} }
} }
@ -241,7 +242,7 @@ public class VaultWorldGenerator {
for (int dX = -1; dX <= 1; dX++) { for (int dX = -1; dX <= 1; dX++) {
for (int dY = -1; dY <= 1; dY++) { for (int dY = -1; dY <= 1; dY++) {
if (map.getTileAt(x + dX, y + dY) == tile) { if (map.getTileIdAt(x + dX, y + dY) == tile) {
return true; return true;
} }
} }

View File

@ -1,5 +1,6 @@
package net.simon987.npcplugin; package net.simon987.npcplugin;
import net.simon987.npcplugin.world.TileVaultFloor;
import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -19,12 +20,12 @@ public class VaultWorldUtils {
//Count number of floor tiles. If there is less plain tiles than desired amount of boxes, //Count number of floor tiles. If there is less plain tiles than desired amount of boxes,
//set the desired amount of blobs to the plain tile count //set the desired amount of blobs to the plain tile count
int[][] tiles = world.getTileMap().getTiles(); TileMap m = world.getTileMap();
int floorCount = 0; int floorCount = 0;
for (int y = 0; y < world.getWorldSize(); y++) { for (int y = 0; y < world.getWorldSize(); y++) {
for (int x = 0; x < world.getWorldSize(); x++) { for (int x = 0; x < world.getWorldSize(); x++) {
if (tiles[x][y] == TileMap.VAULT_FLOOR) { if (m.getTileIdAt(x, y) == TileVaultFloor.ID) {
floorCount++; floorCount++;
} }
} }
@ -37,14 +38,14 @@ public class VaultWorldUtils {
outerLoop: outerLoop:
for (int i = 0; i < boxesCount; i++) { for (int i = 0; i < boxesCount; i++) {
Point p = world.getTileMap().getRandomTile(TileMap.VAULT_FLOOR); Point p = m.getRandomTile(TileVaultFloor.ID);
if (p != null) { if (p != null) {
//Don't block worlds //Don't block worlds
int counter = 0; int counter = 0;
while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 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) { world.getGameObjectsAt(p.x, p.y).size() != 0) {
p = world.getTileMap().getRandomTile(TileMap.VAULT_FLOOR); p = m.getRandomTile(TileVaultFloor.ID);
counter++; counter++;
if (counter > 25) { if (counter > 25) {

View File

@ -7,7 +7,7 @@ import net.simon987.npcplugin.VaultDoor;
import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener; import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldGenerationEvent; import net.simon987.server.event.WorldGenerationEvent;
import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.TilePlain;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -69,7 +69,7 @@ public class WorldCreationListener implements GameEventListener {
} }
//Also spawn a radio tower in the same World //Also spawn a radio tower in the same World
Point p = world.getRandomTileWithAdjacent(8, TileMap.PLAIN_TILE); Point p = world.getRandomTileWithAdjacent(8, TilePlain.ID);
if (p != null) { if (p != null) {
while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0) { while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0) {
p = world.getRandomPassableTile(); p = world.getRandomPassableTile();

View File

@ -0,0 +1,13 @@
package net.simon987.npcplugin.world;
import net.simon987.server.game.world.Tile;
public class TileVaultFloor extends Tile {
public static final int ID = 4;
@Override
public int getId() {
return ID;
}
}

View File

@ -0,0 +1,24 @@
package net.simon987.npcplugin.world;
import net.simon987.server.game.objects.GameObject;
import net.simon987.server.game.world.Tile;
public class TileVaultWall extends Tile {
public static final int ID = 5;
@Override
public int getId() {
return ID;
}
@Override
public boolean walk(GameObject object) {
return false; //always blocked
}
@Override
public boolean isBlocked() {
return true;
}
}

View File

@ -1,6 +1,7 @@
package net.simon987.biomassplugin; package net.simon987.biomassplugin;
import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.TileMap;
import net.simon987.server.game.world.TilePlain;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import org.bson.types.ObjectId; import org.bson.types.ObjectId;
@ -21,12 +22,12 @@ public class WorldUtils {
//Count number of plain tiles. If there is less plain tiles than desired amount of blobs, //Count number of plain tiles. If there is less plain tiles than desired amount of blobs,
//set the desired amount of blobs to the plain tile count //set the desired amount of blobs to the plain tile count
int[][] tiles = world.getTileMap().getTiles(); TileMap m = world.getTileMap();
int plainCount = 0; int plainCount = 0;
for (int y = 0; y < world.getWorldSize(); y++) { for (int y = 0; y < world.getWorldSize(); y++) {
for (int x = 0; x < world.getWorldSize(); x++) { for (int x = 0; x < world.getWorldSize(); x++) {
if (tiles[x][y] == TileMap.PLAIN_TILE) { if (m.getTileIdAt(x, y) == TilePlain.ID) {
plainCount++; plainCount++;
} }
} }
@ -39,14 +40,14 @@ public class WorldUtils {
outerLoop: outerLoop:
for (int i = 0; i < blobCount; i++) { for (int i = 0; i < blobCount; i++) {
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); Point p = m.getRandomTile(TilePlain.ID);
if (p != null) { if (p != null) {
//Don't block worlds //Don't block worlds
int counter = 0; int counter = 0;
while (p.x == 0 || p.y == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 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) { world.getGameObjectsAt(p.x, p.y).size() != 0) {
p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); p = m.getRandomTile(TilePlain.ID);
counter++; counter++;
if (counter > 25) { if (counter > 25) {

View File

@ -15,8 +15,7 @@ import net.simon987.server.game.debug.*;
import net.simon987.server.game.item.ItemCopper; import net.simon987.server.game.item.ItemCopper;
import net.simon987.server.game.item.ItemIron; import net.simon987.server.game.item.ItemIron;
import net.simon987.server.game.objects.GameRegistry; import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.game.world.DayNightCycle; import net.simon987.server.game.world.*;
import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import net.simon987.server.plugin.PluginManager; import net.simon987.server.plugin.PluginManager;
import net.simon987.server.user.User; import net.simon987.server.user.User;
@ -122,6 +121,12 @@ public class GameServer implements Runnable {
gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class); gameRegistry.registerItem(ItemCopper.ID, ItemCopper.class);
gameRegistry.registerItem(ItemIron.ID, ItemIron.class); gameRegistry.registerItem(ItemIron.ID, ItemIron.class);
gameRegistry.registerTile(TileVoid.ID, TileVoid.class);
gameRegistry.registerTile(TilePlain.ID, TilePlain.class);
gameRegistry.registerTile(TileWall.ID, TileWall.class);
gameRegistry.registerTile(TileCopper.ID, TileCopper.class);
gameRegistry.registerTile(TileIron.ID, TileIron.class);
} }
public GameUniverse getGameUniverse() { public GameUniverse getGameUniverse() {

View File

@ -5,7 +5,7 @@ import org.bson.Document;
public class ItemCopper extends Item { public class ItemCopper extends Item {
public static final int ID = 0x0003; public static final int ID = 0x0004;
@Override @Override
public int getId() { public int getId() {

View File

@ -4,7 +4,7 @@ import org.bson.Document;
public class ItemIron extends Item { public class ItemIron extends Item {
public static final int ID = 0x0004; public static final int ID = 0x0003;
@Override @Override
public int getId() { public int getId() {

View File

@ -2,6 +2,7 @@ package net.simon987.server.game.objects;
import net.simon987.server.assembly.HardwareModule; import net.simon987.server.assembly.HardwareModule;
import net.simon987.server.game.item.Item; import net.simon987.server.game.item.Item;
import net.simon987.server.game.world.Tile;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.bson.Document; import org.bson.Document;
@ -13,12 +14,14 @@ public class GameRegistry {
private HashMap<String, Class<? extends GameObject>> gameObjects; private HashMap<String, Class<? extends GameObject>> gameObjects;
private HashMap<String, Class<? extends HardwareModule>> hardware; private HashMap<String, Class<? extends HardwareModule>> hardware;
private HashMap<Integer, Class<? extends Item>> items; private HashMap<Integer, Class<? extends Item>> items;
private HashMap<Integer, Class<? extends Tile>> tiles;
public GameRegistry() { public GameRegistry() {
gameObjects = new HashMap<>(); gameObjects = new HashMap<>();
hardware = new HashMap<>(); hardware = new HashMap<>();
items = new HashMap<>(); items = new HashMap<>();
tiles = new HashMap<>();
} }
public void registerGameObject(Class<? extends GameObject> clazz) { public void registerGameObject(Class<? extends GameObject> clazz) {
@ -33,6 +36,10 @@ public class GameRegistry {
items.put(id, clazz); items.put(id, clazz);
} }
public void registerTile(int id, Class<? extends Tile> clazz) {
tiles.put(id, clazz);
}
public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) { public HardwareModule deserializeHardware(Document document, ControllableUnit controllableUnit) {
String type = document.getString("type"); String type = document.getString("type");
@ -119,6 +126,23 @@ public class GameRegistry {
} }
} }
public Tile makeTile(int tileId) {
if (tiles.containsKey(tileId)) {
try {
return tiles.get(tileId).getConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException
| InvocationTargetException e) {
e.printStackTrace();
return null;
}
} else {
LogManager.LOGGER.severe("Trying to create an unknown Tile type: " + tileId);
return null;
}
}
public boolean isGameObjectRegistered(String type) { public boolean isGameObjectRegistered(String type) {
return gameObjects.containsKey(type); return gameObjects.containsKey(type);
} }

View File

@ -1,21 +1,37 @@
package net.simon987.server.game.world; package net.simon987.server.game.world;
import net.simon987.server.game.item.Item; import net.simon987.server.game.item.Item;
import net.simon987.server.game.objects.GameObject;
public abstract class Tile { public abstract class Tile {
protected boolean blocked;
/** /**
* @return Unique id of the tile * @return Unique id of the tile
*/ */
public abstract int getId(); public abstract int getId();
public Item onDrill() { /**
* Called when an object attempts to drill this tile
*
* @return The item obtained by drilling, return null for no item
*/
public Item drill() {
return null; return null;
} }
/**
* Called when a player attempts to walk on this tile
*
* @return true if the object can walk on this tile, false if blocked
*/
public boolean walk(GameObject object) {
return true;
}
/**
* Check if a tile should be considered 'blocked' (by the LiDAR, for example)
*/
public boolean isBlocked() { public boolean isBlocked() {
return blocked; return false;
} }
} }

View File

@ -0,0 +1,19 @@
package net.simon987.server.game.world;
import net.simon987.server.game.item.Item;
import net.simon987.server.game.item.ItemCopper;
public class TileCopper extends Tile {
public static final int ID = 3;
@Override
public int getId() {
return ID;
}
@Override
public Item drill() {
return new ItemCopper();
}
}

View File

@ -0,0 +1,19 @@
package net.simon987.server.game.world;
import net.simon987.server.game.item.Item;
import net.simon987.server.game.item.ItemIron;
public class TileIron extends Tile {
public static final int ID = 2;
@Override
public int getId() {
return ID;
}
@Override
public Item drill() {
return new ItemIron();
}
}

View File

@ -1,38 +1,27 @@
package net.simon987.server.game.world; package net.simon987.server.game.world;
import net.simon987.server.GameServer;
import net.simon987.server.game.objects.GameRegistry;
import net.simon987.server.io.JSONSerialisable; import net.simon987.server.io.JSONSerialisable;
import net.simon987.server.io.MongoSerializable; import net.simon987.server.io.MongoSerializable;
import org.bson.Document; import org.bson.Document;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.awt.*; import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Base64;
import java.util.Random; import java.util.Random;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
/** /**
* A 2D map of Tile objects of size width*height * A 2D map of Tile objects of size width*height
*/ */
public class TileMap implements JSONSerialisable, MongoSerializable { public class TileMap implements JSONSerialisable, MongoSerializable {
public static final int VOID = -1;
public static final int PLAIN_TILE = 0;
public static final int WALL_TILE = 1;
public static final int IRON_TILE = 2;
public static final int COPPER_TILE = 3;
public static final int VAULT_FLOOR = 4;
public static final int VAULT_WALL = 5;
/** /**
* The map of tile * The map of tile
*/ */
private int[][] tiles; private Tile[][] tiles;
/** /**
* width, in tiles * width, in tiles
@ -51,10 +40,10 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
this.width = width; this.width = width;
this.height = height; this.height = height;
tiles = new int[width][height]; tiles = new Tile[width][height];
} }
public TileMap(int[][] tiles, int size) { public TileMap(Tile[][] tiles, int size) {
this.width = size; this.width = size;
this.height = size; this.height = size;
@ -63,40 +52,62 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
/** /**
* Change the tile at a specified position * Change the tile at a specified position
* Sets the modified flag
* *
* @param tileId id of the new Tile * @param tileId id of the new Tile
* @param x X coordinate of the tile to set * @param x X coordinate of the tile to set
* @param y Y coordinate of the tile to set * @param y Y coordinate of the tile to set
*/ */
public void setTileAt(int tileId, int x, int y) { public void setTileAt(int tileId, int x, int y) {
setTileAt(GameServer.INSTANCE.getRegistry().makeTile(tileId), x, y);
}
/**
* Change the tile at a specified position
*
* @param tile new Tile
* @param x X coordinate of the tile to set
* @param y Y coordinate of the tile to set
*/
public void setTileAt(Tile tile, int x, int y) {
try { try {
tiles[x][y] = tileId; tiles[x][y] = tile;
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {
//Shouldn't happen //Shouldn't happen
e.printStackTrace(); e.printStackTrace();
} }
} }
/**
* Get the tile id at a specific position
*
* @param x X coordinate of the tile to get
* @param y Y coordinate of the tile to get
* @return the tile id at the specified position, -1 if out of bounds
*/
public int getTileIdAt(int x, int y) {
try {
return tiles[x][y].getId();
} catch (ArrayIndexOutOfBoundsException e) {
return -1;
}
}
/** /**
* Get the tile at a specific position * Get the tile at a specific position
* *
* @param x X coordinate of the tile to get * @param x X coordinate of the tile to get
* @param y Y coordinate of the tile to get * @param y Y coordinate of the tile to get
* @return the tile at the specified position, -1 if out of bounds * @return the tile id at the specified position, null if out of bounds
*/ */
public int getTileAt(int x, int y) { public Tile getTileAt(int x, int y) {
try { try {
return tiles[x][y]; return tiles[x][y];
} catch (ArrayIndexOutOfBoundsException e) { } catch (ArrayIndexOutOfBoundsException e) {
return -1; return null;
} }
} }
public int[][] getTiles() {
return tiles;
}
public int getWidth() { public int getWidth() {
@ -111,28 +122,15 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
public JSONObject jsonSerialise() { public JSONObject jsonSerialise() {
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
byte[] terrain = new byte[width * width]; JSONArray terrain = new JSONArray();
for (int x = 0; x < width; x++) { for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) { for (int y = 0; y < height; y++) {
terrain[x * width + y] = (byte) tiles[x][y]; terrain.add(tiles[y][x].getId());
} }
} }
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Deflater compressor = new Deflater(Deflater.BEST_COMPRESSION, true);
DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(stream, compressor);
deflaterOutputStream.write(terrain); json.put("terrain", terrain);
deflaterOutputStream.close();
byte[] compressedBytes = stream.toByteArray();
json.put("z", new String(Base64.getEncoder().encode(compressedBytes)));
} catch (IOException e) {
e.printStackTrace();
}
return json; return json;
} }
@ -152,7 +150,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
for (int x = 0; x < this.width; x++) { for (int x = 0; x < this.width; x++) {
for (int y = 0; y < this.height; y++) { for (int y = 0; y < this.height; y++) {
bsonTiles.add(tiles[x][y]); bsonTiles.add(tiles[x][y].getId());
} }
} }
@ -165,19 +163,19 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
public static TileMap deserialize(Document object, int size) { public static TileMap deserialize(Document object, int size) {
ArrayList<Integer> terrain = (ArrayList<Integer>) object.get("tiles"); ArrayList<Integer> terrain = (ArrayList<Integer>) object.get("tiles");
GameRegistry reg = GameServer.INSTANCE.getRegistry();
int[][] tiles = new int[size][size]; Tile[][] tiles = new Tile[size][size];
for (int x = 0; x < size; x++) { for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) { for (int y = 0; y < size; y++) {
tiles[x][y] = terrain.get(x * size + y); tiles[x][y] = reg.makeTile(terrain.get(x * size + y));
} }
} }
return new TileMap(tiles, size); return new TileMap(tiles, size);
} }
public Point getRandomTile(int tile) { public Point getRandomTile(int tileId) {
Random random = new Random(); Random random = new Random();
@ -193,7 +191,7 @@ public class TileMap implements JSONSerialisable, MongoSerializable {
int rx = random.nextInt(width); int rx = random.nextInt(width);
int ry = random.nextInt(height); int ry = random.nextInt(height);
if (tiles[rx][ry] == tile) { if (tiles[rx][ry].getId() == tileId) {
return new Point(rx, ry); return new Point(rx, ry);
} }
} }

View File

@ -0,0 +1,11 @@
package net.simon987.server.game.world;
public class TilePlain extends Tile {
public static final int ID = 0;
@Override
public int getId() {
return ID;
}
}

View File

@ -0,0 +1,23 @@
package net.simon987.server.game.world;
import net.simon987.server.game.objects.GameObject;
public class TileVoid extends Tile {
public static final int ID = -1;
@Override
public int getId() {
return ID;
}
@Override
public boolean walk(GameObject object) {
return false; //Shouldn't happen!
}
@Override
public boolean isBlocked() {
return true;
}
}

View File

@ -0,0 +1,23 @@
package net.simon987.server.game.world;
import net.simon987.server.game.objects.GameObject;
public class TileWall extends Tile {
public static final int ID = 1;
@Override
public int getId() {
return ID;
}
@Override
public boolean walk(GameObject object) {
return false; //always blocked
}
@Override
public boolean isBlocked() {
return true;
}
}

View File

@ -25,6 +25,7 @@ public class World implements MongoSerializable {
*/ */
private int worldSize; private int worldSize;
//TODO: This info should be pulled from the Tile class
private static final char INFO_BLOCKED = 0x8000; private static final char INFO_BLOCKED = 0x8000;
private static final char INFO_IRON = 0x0200; private static final char INFO_IRON = 0x0200;
private static final char INFO_COPPER = 0x0100; private static final char INFO_COPPER = 0x0100;
@ -68,11 +69,7 @@ public class World implements MongoSerializable {
* Check if a tile is blocked, either by a game object or an impassable tile type * Check if a tile is blocked, either by a game object or an impassable tile type
*/ */
public boolean isTileBlocked(int x, int y) { public boolean isTileBlocked(int x, int y) {
return getGameObjectsBlockingAt(x, y).size() > 0 || tileMap.getTileAt(x, y).isBlocked();
int tile = tileMap.getTileAt(x, y);
return getGameObjectsBlockingAt(x, y).size() > 0 || tile == TileMap.WALL_TILE ||
tile == TileMap.VAULT_WALL || tile == TileMap.VOID;
} }
/** /**
@ -213,11 +210,10 @@ public class World implements MongoSerializable {
public String toString() { public String toString() {
StringBuilder str = new StringBuilder("World (" + x + ", " + y + ")\n"); StringBuilder str = new StringBuilder("World (" + x + ", " + y + ")\n");
int[][] tileMap = this.tileMap.getTiles();
for (int x = 0; x < worldSize; x++) { for (int x = 0; x < worldSize; x++) {
for (int y = 0; y < worldSize; y++) { for (int y = 0; y < worldSize; y++) {
str.append(tileMap[x][y]).append(" "); str.append(tileMap.getTileIdAt(x, y)).append(" ");
} }
str.append("\n"); str.append("\n");
} }
@ -260,22 +256,22 @@ public class World implements MongoSerializable {
public char[][] getMapInfo() { public char[][] getMapInfo() {
char[][] mapInfo = new char[worldSize][worldSize]; char[][] mapInfo = new char[worldSize][worldSize];
int[][] tiles = tileMap.getTiles();
//Tile //Tile
for (int y = 0; y < worldSize; y++) { for (int y = 0; y < worldSize; y++) {
for (int x = 0; x < worldSize; x++) { for (int x = 0; x < worldSize; x++) {
if (tiles[x][y] == TileMap.PLAIN_TILE) { if (tileMap.getTileIdAt(x, y) == TilePlain.ID) {
mapInfo[x][y] = 0; mapInfo[x][y] = 0;
} else if (tiles[x][y] == TileMap.WALL_TILE || tiles[x][y] == TileMap.VAULT_WALL) { } else if (tileMap.getTileAt(x, y).isBlocked()) {
mapInfo[x][y] = INFO_BLOCKED; mapInfo[x][y] = INFO_BLOCKED;
} else if (tiles[x][y] == TileMap.COPPER_TILE) { //TODO: Tiles should have their .getMapInfo() method
} else if (tileMap.getTileIdAt(x, y) == TileCopper.ID) {
mapInfo[x][y] = INFO_COPPER; mapInfo[x][y] = INFO_COPPER;
} else if (tiles[x][y] == TileMap.IRON_TILE) { } else if (tileMap.getTileIdAt(x, y) == TileIron.ID) {
mapInfo[x][y] = INFO_IRON; mapInfo[x][y] = INFO_IRON;
} }
} }

View File

@ -159,7 +159,7 @@ public class WorldGenerator {
} }
world.getTileMap().getTiles()[x][y] = tile; world.getTileMap().setTileAt(tile, x, y);
} }
} }
@ -169,18 +169,18 @@ public class WorldGenerator {
for (int i = 0; i < ironCount; i++) { for (int i = 0; i < ironCount; i++) {
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); Point p = world.getTileMap().getRandomTile(TilePlain.ID);
if (p != null) { if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE; world.getTileMap().setTileAt(new TileIron(), p.x, p.y);
} }
} }
for (int i = 0; i < copperCount; i++) { for (int i = 0; i < copperCount; i++) {
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE); Point p = world.getTileMap().getRandomTile(TilePlain.ID);
if (p != null) { if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE; world.getTileMap().setTileAt(new TileCopper(), p.x, p.y);
} }
} }

View File

@ -3,7 +3,6 @@ package net.simon987.server.websocket;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import java.io.IOException; import java.io.IOException;
@ -30,20 +29,10 @@ public class TerrainRequestHandler implements MessageHandler {
//todo It might be a good idea to cache this //todo It might be a good idea to cache this
if (world != null) { if (world != null) {
JSONObject response = new JSONObject(); JSONObject response = world.getTileMap().jsonSerialise();
JSONArray terrain = new JSONArray();
int[][] tiles = world.getTileMap().getTiles();
for (int x = 0; x < world.getWorldSize(); x++) {
for (int y = 0; y < world.getWorldSize(); y++) {
terrain.add(tiles[y][x]);
}
}
response.put("t", "terrain"); response.put("t", "terrain");
response.put("ok", true); response.put("ok", true);
response.put("terrain", terrain);
response.put("size", world.getWorldSize()); response.put("size", world.getWorldSize());
user.getWebSocket().getRemote().sendString(response.toJSONString()); user.getWebSocket().getRemote().sendString(response.toJSONString());

View File

@ -1,7 +1,14 @@
var __extends = (this && this.__extends) || (function () { var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf || var extendStatics = function (d, b) {
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || extendStatics = Object.setPrototypeOf ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; ({__proto__: []} instanceof Array && function (d, b) {
d.__proto__ = b;
}) ||
function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
};
return extendStatics(d, b);
}
return function (d, b) { return function (d, b) {
extendStatics(d, b); extendStatics(d, b);
function __() { this.constructor = d; } function __() { this.constructor = d; }