Added electric boxes, debug command to teleport objects across Worlds.

This commit is contained in:
simon 2018-02-27 16:49:32 -05:00
parent f530dafdee
commit 039088ac00
16 changed files with 426 additions and 42 deletions

View File

@ -0,0 +1,30 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.Updatable;
public class ElectricBox extends Obstacle implements Updatable {
public static final int STYLE = 1;
private static final int maxHp = GameServer.INSTANCE.getConfig().getInt("electric_box_hp");
private static final int damage = GameServer.INSTANCE.getConfig().getInt("electric_box_damage");
private static final int energyGiven = GameServer.INSTANCE.getConfig().getInt("electric_box_energy_given");
public ElectricBox() {
super(maxHp);
setStyle(STYLE);
}
@Override
public void update() {
}
@Override
public boolean onDeadCallback() {
getWorld().decUpdatable();
return false;
}
}

View File

@ -46,7 +46,9 @@ public class HarvesterNPC extends NonPlayerCharacter {
}
@Override
public void onDeadCallback() {
public boolean onDeadCallback() {
getWorld().decUpdatable();
if (getFactory() != null && getFactory().getNpcs() != null) {
getFactory().getNpcs().remove(this);
@ -54,6 +56,8 @@ public class HarvesterNPC extends NonPlayerCharacter {
GameServer.INSTANCE.getEventDispatcher().dispatch(
new ObjectDeathEvent(this, ID));
return false;
}
@Override

View File

@ -245,4 +245,5 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
public int getAge() {
return age;
}
}

View File

@ -2,6 +2,7 @@ package net.simon987.npcplugin;
import com.mongodb.DBObject;
import net.simon987.npcplugin.event.CpuInitialisationListener;
import net.simon987.npcplugin.event.VaultWorldUpdateListener;
import net.simon987.npcplugin.event.WorldCreationListener;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.CpuHardware;
@ -25,6 +26,7 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C
listeners.add(new WorldCreationListener());
listeners.add(new CpuInitialisationListener());
listeners.add(new VaultWorldUpdateListener(configuration));
radioTowers = new ArrayList<>(32);
@ -44,6 +46,8 @@ public class NpcPlugin extends ServerPlugin implements GameObjectDeserializer, C
return RadioTower.deserialize(obj);
} else if (objType == VaultDoor.ID) {
return VaultDoor.deserialize(obj);
} else if (objType == Obstacle.ID) {
return Obstacle.deserialize(obj);
}
return null;

View File

@ -0,0 +1,129 @@
package net.simon987.npcplugin;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import net.simon987.server.game.Attackable;
import net.simon987.server.game.GameObject;
import org.json.simple.JSONObject;
/**
* Generic game object that blocks the path.
* Some types of obstacles might have some more interesting features (see ElectricBox)
*/
public class Obstacle extends GameObject implements Attackable {
public static final int ID = 6;
/**
* Style of the obstacle. Will tell the client which sprite to display
*/
private int style = 0;
/**
* Current health of the npc
*/
private int hp;
/**
* Maximum health of the npc
*/
private int maxHp;
public Obstacle(int hp) {
this.hp = hp;
this.maxHp = hp;
}
@Override
public void setHealRate(int hp) {
//No op
}
@Override
public void heal(int amount) {
//No op
}
@Override
public int getHp() {
return hp;
}
@Override
public void setHp(int hp) {
this.hp = hp;
}
@Override
public int getMaxHp() {
return maxHp;
}
@Override
public void setMaxHp(int hp) {
this.maxHp = hp;
this.hp = hp;
}
@Override
public void damage(int amount) {
hp -= amount;
//YOU ARE DEAD
if (hp <= 0) {
setDead(true);
}
}
@Override
public char getMapInfo() {
return 0x0400;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
@Override
public BasicDBObject mongoSerialise() {
BasicDBObject dbObject = new BasicDBObject();
dbObject.put("i", getObjectId());
dbObject.put("x", getX());
dbObject.put("y", getY());
dbObject.put("t", ID);
dbObject.put("style", style);
return dbObject;
}
@Override
public JSONObject serialise() {
JSONObject json = new JSONObject();
json.put("i", getObjectId());
json.put("x", getX());
json.put("y", getY());
json.put("t", ID);
json.put("hp", hp);
json.put("style", style);
return json;
}
public static Obstacle deserialize(DBObject obj) {
//Doesn't matter if we don't store maxHP, since obstacles can't be healed
Obstacle obstacle = new Obstacle((int) obj.get("hp"));
obstacle.setObjectId((long) obj.get("i"));
obstacle.setX((int) obj.get("x"));
obstacle.setY((int) obj.get("y"));
obstacle.setStyle((int) obj.get("style"));
return obstacle;
}
}

View File

@ -72,18 +72,18 @@ public class VaultDimension {
for (int j = 0; j < attachedWorlds; j++) {
int rDirIndex = random.nextInt(4);
int randDirIndex = random.nextInt(4);
//Try 4 directions (wrap around 0..3)
for (int attemptCount = 0; attemptCount < 4; attemptCount++) {
Direction rDir = Direction.getDirection(rDirIndex);
Direction randomDirection = Direction.getDirection(randDirIndex);
//Don't attach a world at the same spot twice
if (!worldExists(world.coordinatesOf(rDir), worldLayers)) {
WorldBluePrint attachedWorld = world.attachWorld(rDir);
if (!worldExists(world.coordinatesOf(randomDirection), worldLayers)) {
WorldBluePrint attachedWorld = world.attachWorld(randomDirection);
worldLayers.get(i).add(attachedWorld);
}
rDirIndex = (rDirIndex + 1) % 4;
randDirIndex = (randDirIndex + 1) % 4;
}
}
}
@ -117,11 +117,17 @@ public class VaultDimension {
return worldLayers.values().stream().flatMap(Collection::stream).anyMatch(bp -> bp.coords.equals(coords));
}
/**
* Helper class to plan the layout of a vault dimension
*/
private class WorldBluePrint {
public ArrayList<Direction> openings = new ArrayList<>(4);
ArrayList<Direction> openings = new ArrayList<>(4);
public Point coords = new Point();
/**
* Coordinates of the world
*/
Point coords = new Point();
/**
* Update the blueprint's openings to allow traveling to the newly attached world
@ -129,7 +135,7 @@ public class VaultDimension {
* @param direction direction of the world to attach (relative to this one)
* @return The blueprint of the attached world
*/
public WorldBluePrint attachWorld(Direction direction) {
WorldBluePrint attachWorld(Direction direction) {
WorldBluePrint attachedWorld = new WorldBluePrint();

View File

@ -0,0 +1,75 @@
package net.simon987.npcplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
import java.awt.*;
import java.util.ArrayList;
import java.util.Random;
public class VaultWorldUtils {
public static ArrayList<ElectricBox> generateElectricBoxes(World world, int minCount, int maxCount) {
Random random = new Random();
int boxesCount = random.nextInt(maxCount - minCount) + minCount;
ArrayList<ElectricBox> electricBoxes = new ArrayList<>(boxesCount);
//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
int[][] tiles = world.getTileMap().getTiles();
int floorCount = 0;
for (int y = 0; y < world.getWorldSize(); y++) {
for (int x = 0; x < world.getWorldSize(); x++) {
if (tiles[x][y] == TileMap.VAULT_FLOOR) {
floorCount++;
}
}
}
if (boxesCount > floorCount) {
boxesCount = floorCount;
}
outerLoop:
for (int i = 0; i < boxesCount; i++) {
Point p = world.getTileMap().getRandomTile(TileMap.VAULT_FLOOR);
if (p != null) {
//Don't block worlds
int counter = 0;
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().getRandomTile(TileMap.VAULT_FLOOR);
counter++;
if (counter > 25) {
continue outerLoop;
}
}
for (ElectricBox box : electricBoxes) {
if (box.getX() == p.x && box.getY() == p.y) {
//There is already a box here
continue outerLoop;
}
}
ElectricBox box = new ElectricBox();
box.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
box.setX(p.x);
box.setY(p.y);
box.setWorld(world);
electricBoxes.add(box);
}
}
return electricBoxes;
}
}

View File

@ -0,0 +1,70 @@
package net.simon987.npcplugin.event;
import net.simon987.npcplugin.ElectricBox;
import net.simon987.npcplugin.VaultWorldUtils;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration;
import net.simon987.server.event.GameEvent;
import net.simon987.server.event.GameEventListener;
import net.simon987.server.event.WorldUpdateEvent;
import net.simon987.server.game.World;
import java.util.ArrayList;
import java.util.HashMap;
public class VaultWorldUpdateListener implements GameEventListener {
private HashMap<World, Long> worldWaitMap = new HashMap<>(200);
private static int minElectricBoxCount;
private static int maxElectricBoxCount;
private static int waitTime;
private static int electricBoxThreshold;
public VaultWorldUpdateListener(ServerConfiguration config) {
minElectricBoxCount = config.getInt("min_electric_box_respawn_count");
maxElectricBoxCount = config.getInt("max_electric_box_respawn_count");
waitTime = config.getInt("electric_box_respawnTime");
electricBoxThreshold = config.getInt("min_electric_box_count");
}
@Override
public Class getListenedEventType() {
return WorldUpdateEvent.class;
}
@Override
public void handle(GameEvent event) {
World world = ((WorldUpdateEvent) event).getWorld();
if (world.getDimension().startsWith("v")) {
//If there is less than the respawn threshold,
if (world.findObjects(ElectricBox.class).size() < electricBoxThreshold) {
//Set a timer for respawn_time ticks
if (!worldWaitMap.containsKey(world) || worldWaitMap.get(world) == 0L) {
worldWaitMap.put(world, GameServer.INSTANCE.getGameUniverse().getTime() + waitTime);
} else {
long waitUntil = worldWaitMap.get(world);
if (GameServer.INSTANCE.getGameUniverse().getTime() >= waitUntil) {
//If the timer was set less than respawn_time ticks ago, respawn the blobs
ArrayList<ElectricBox> newBlobs = VaultWorldUtils.generateElectricBoxes(world, minElectricBoxCount,
maxElectricBoxCount);
for (ElectricBox blob : newBlobs) {
world.addObject(blob);
}
//Set the 'waitUntil' time to 0 to indicate that we are not waiting
worldWaitMap.replace(world, 0L);
}
}
}
}
}
}

View File

@ -1,6 +1,7 @@
package net.simon987.biomassplugin;
import net.simon987.server.GameServer;
import net.simon987.server.game.TileMap;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
@ -26,7 +27,7 @@ public class WorldUtils {
for (int y = 0; y < world.getWorldSize(); y++) {
for (int x = 0; x < world.getWorldSize(); x++) {
if (tiles[x][y] == 0) {
if (tiles[x][y] == TileMap.PLAIN_TILE) {
plainCount++;
}
}
@ -39,14 +40,14 @@ public class WorldUtils {
outerLoop:
for (int i = 0; i < blobCount; i++) {
Point p = world.getTileMap().getRandomPlainTile();
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE);
if (p != null) {
//Don't block worlds
int counter = 0;
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();
p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE);
counter++;
if (counter > 25) {

View File

@ -44,31 +44,32 @@ public class WorldUpdateListener implements GameEventListener {
World world = ((WorldUpdateEvent) event).getWorld();
//If there is less than the respawn threshold,
if (world.findObjects(BiomassBlob.class).size() < blobThreshold) {
if (world.getDimension().startsWith("w")) {
//If there is less than the respawn threshold,
if (world.findObjects(BiomassBlob.class).size() < blobThreshold) {
//Set a timer for respawn_time ticks
if (!worldWaitMap.containsKey(world) || worldWaitMap.get(world) == 0L) {
worldWaitMap.put(world, GameServer.INSTANCE.getGameUniverse().getTime() + waitTime);
} else {
//Set a timer for respawn_time ticks
if (!worldWaitMap.containsKey(world) || worldWaitMap.get(world) == 0L) {
worldWaitMap.put(world, GameServer.INSTANCE.getGameUniverse().getTime() + waitTime);
} else {
long waitUntil = worldWaitMap.get(world);
long waitUntil = worldWaitMap.get(world);
if (GameServer.INSTANCE.getGameUniverse().getTime() >= waitUntil) {
if (GameServer.INSTANCE.getGameUniverse().getTime() >= waitUntil) {
//If the timer was set less than respawn_time ticks ago, respawn the blobs
ArrayList<BiomassBlob> newBlobs = WorldUtils.generateBlobs(world, minBlobCount,
maxBlobCount, blobYield);
for (BiomassBlob blob : newBlobs) {
world.addObject(blob);
//If the timer was set less than respawn_time ticks ago, respawn the blobs
ArrayList<BiomassBlob> newBlobs = WorldUtils.generateBlobs(world, minBlobCount,
maxBlobCount, blobYield);
for (BiomassBlob blob : newBlobs) {
world.addObject(blob);
world.incUpdatable();
}
//Set the 'waitUntil' time to 0 to indicate that we are not waiting
worldWaitMap.replace(world, 0L);
}
//Set the 'waitUntil' time to 0 to indicate that we are not waiting
worldWaitMap.replace(world, 0L);
}
}
}
}
}

View File

@ -254,6 +254,9 @@ public abstract class GameObject implements JSONSerialisable, MongoSerialisable
/**
* Called before this GameObject is removed from the world - defaults to doing nothing
* @return cancelled
*/
public void onDeadCallback() { }
public boolean onDeadCallback() {
return false;
}
}

View File

@ -25,6 +25,8 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
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;
public static final int ITEM_IRON = 3;
public static final int ITEM_COPPER = 4;
@ -164,7 +166,7 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
}
public Point getRandomPlainTile() {
public Point getRandomTile(int tile) {
Random random = new Random();
@ -180,7 +182,7 @@ public class TileMap implements JSONSerialisable, MongoSerialisable {
int rx = random.nextInt(width);
int ry = random.nextInt(height);
if (tiles[rx][ry] == TileMap.PLAIN_TILE) {
if (tiles[rx][ry] == tile) {
return new Point(rx, ry);
}
}

View File

@ -66,7 +66,10 @@ public class World implements MongoSerialisable {
*/
public boolean isTileBlocked(int x, int y) {
return getGameObjectsBlockingAt(x, y).size() > 0 || tileMap.getTileAt(x, y) == TileMap.WALL_TILE;
int tile = tileMap.getTileAt(x, y);
return getGameObjectsBlockingAt(x, y).size() > 0 || tile == TileMap.WALL_TILE ||
tile == TileMap.VAULT_WALL;
}
/**
@ -154,9 +157,10 @@ public class World implements MongoSerialisable {
for (GameObject object : gameObjects.values()) {
//Clean up dead objects
if (object.isDead()) {
object.onDeadCallback();
removeObject(object);
//LogManager.LOGGER.fine("Removed object " + object + " id: " + object.getObjectId());
if (!object.onDeadCallback()) {
removeObject(object);
}
} else if (object instanceof Updatable) {
((Updatable) object).update();
}
@ -249,7 +253,7 @@ public class World implements MongoSerialisable {
if (tiles[x][y] == TileMap.PLAIN_TILE) {
mapInfo[x][y] = 0;
} else if (tiles[x][y] == TileMap.WALL_TILE) {
} else if (tiles[x][y] == TileMap.WALL_TILE || tiles[x][y] == TileMap.VAULT_WALL) {
mapInfo[x][y] = INFO_BLOCKED;
} else if (tiles[x][y] == TileMap.COPPER_TILE) {

View File

@ -169,7 +169,7 @@ public class WorldGenerator {
for (int i = 0; i < ironCount; i++) {
Point p = world.getTileMap().getRandomPlainTile();
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE);
if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.IRON_TILE;
@ -177,7 +177,7 @@ public class WorldGenerator {
}
for (int i = 0; i < copperCount; i++) {
Point p = world.getTileMap().getRandomPlainTile();
Point p = world.getTileMap().getRandomTile(TileMap.PLAIN_TILE);
if (p != null) {
world.getTileMap().getTiles()[p.x][p.y] = TileMap.COPPER_TILE;

View File

@ -5,6 +5,7 @@ import com.mongodb.util.JSON;
import net.simon987.server.GameServer;
import net.simon987.server.game.ControllableUnit;
import net.simon987.server.game.GameObject;
import net.simon987.server.game.Updatable;
import net.simon987.server.game.World;
import net.simon987.server.logging.LogManager;
import net.simon987.server.user.User;
@ -88,6 +89,15 @@ public class DebugHandler implements MessageHandler {
(String) json.get("data"),
(String) json.get("dimension")));
break;
case "tpObj":
response.put("message", moveObj(
(long) json.get("objectId"),
(int) (long) json.get("x"),
(int) (long) json.get("y"),
(int) (long) json.get("worldX"),
(int) (long) json.get("worldY"),
(String) json.get("dimension")));
break;
default:
LogManager.LOGGER.severe("Unknown command: " + command);
@ -213,12 +223,48 @@ public class DebugHandler implements MessageHandler {
object.setX(x);
object.setY(y);
return "Sucess";
return "Success";
} else {
return "Object not found: " + objectId;
}
}
private String moveObj(long objectId, int x, int y, int worldX, int worldY, String dimension) {
GameObject object = GameServer.INSTANCE.getGameUniverse().getObject(objectId);
World world = GameServer.INSTANCE.getGameUniverse().getWorld(worldX, worldY, false, dimension);
if (object != null) {
if (world != null) {
if (object instanceof Updatable) {
object.getWorld().decUpdatable();
}
object.getWorld().removeObject(object);
object.setWorld(world);
world.addObject(object);
if (object instanceof Updatable) {
world.incUpdatable();
}
object.setX(x);
object.setY(y);
return "Success";
} else {
return "World not found: " + World.idFromCoordinates(worldX, worldY, dimension);
}
} else {
return "Object not found: " + objectId;
}
}
private String userInfo(String username) {
User user = GameServer.INSTANCE.getGameUniverse().getUser(username);
@ -243,4 +289,5 @@ public class DebugHandler implements MessageHandler {
}
}
}

View File

@ -88,4 +88,11 @@ user_timeout=100
# ----------------------------------------------
vault_door_open_time=4
vault_door_open_time=4
electric_box_hp=250
min_electric_box_count=3
min_electric_box_respawn_count=3
max_electric_box_respawn_count=5
electric_box_respawnTime=64
electric_box_damage=5
electric_box_energy_given=70