mirror of
https://github.com/simon987/Much-Assembly-Required.git
synced 2025-12-13 22:59:02 +00:00
Added electric boxes, debug command to teleport objects across Worlds.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -245,4 +245,5 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
129
Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java
Normal file
129
Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java
Normal 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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user