From 4b67798180b28ccdeb697c482d1595d242443873 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 12 May 2018 20:32:10 -0400 Subject: [PATCH] Added structure class #145 and moved many constants to config.properties --- .../java/net/simon987/npcplugin/Factory.java | 67 +--------------- .../net/simon987/npcplugin/NpcPlugin.java | 2 +- .../java/net/simon987/npcplugin/Obstacle.java | 1 + .../java/net/simon987/npcplugin/Portal.java | 7 +- .../net/simon987/npcplugin/RadioTower.java | 11 +-- .../simon987/npcplugin/VaultDimension.java | 17 ++-- .../net/simon987/npcplugin/VaultDoor.java | 11 ++- .../event/WorldCreationListener.java | 10 ++- .../simon987/biomassplugin/WorldUtils.java | 1 - .../event/ObjectDeathListener.java | 1 - .../simon987/server/assembly/Assembler.java | 3 +- .../simon987/server/game/GameUniverse.java | 2 +- .../server/game/objects/Structure.java | 80 +++++++++++++++++++ .../net/simon987/server/game/world/World.java | 18 ----- .../net/simon987/server/web/PlayPage.java | 2 + .../server/websocket/CodeRequestHandler.java | 3 +- Server/src/main/resources/config.properties | 11 ++- Server/src/main/resources/templates/play.vm | 2 +- 18 files changed, 136 insertions(+), 113 deletions(-) create mode 100644 Server/src/main/java/net/simon987/server/game/objects/Structure.java diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java index c4be431..0933c49 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java @@ -1,7 +1,7 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; -import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Structure; import net.simon987.server.game.objects.Updatable; import org.bson.Document; @@ -12,7 +12,7 @@ import java.util.List; /** * Game objects that regularly creates NonPlayerCharacters */ -public class Factory extends GameObject implements Updatable { +public class Factory extends Structure implements Updatable { private static final int MAP_INFO = 0x0200; @@ -48,10 +48,11 @@ public class Factory extends GameObject implements Updatable { private boolean initialised = false; public Factory() { + super(2, 2); } public Factory(Document document) { - super(document); + super(document, 2, 2); tmpNpcArray = ((ArrayList) document.get("npcs")).toArray(); } @@ -112,18 +113,6 @@ public class Factory extends GameObject implements Updatable { } } - @Override - public boolean isAt(int x, int y) { - - /* - * Object is 2x2 tiles, the (x,y) coordinates of the object being - * at top-left. - * # . - * . . - */ - return (x == getX() + 1 || x == getX()) && (y == getY() + 1 || y == getY()); - } - @Override public Document mongoSerialise() { Document dbObject = super.mongoSerialise(); @@ -139,54 +128,6 @@ public class Factory extends GameObject implements Updatable { return dbObject; } - /** - * Get the first non-blocked tile that is directly adjacent to the factory, starting from the north-east corner - * going clockwise. - * - * @return The coordinates of the first non-blocked tile, null otherwise. - */ - public Point getAdjacentTile() { - - /* - * (2,0) - * (2,1) - * (1,2) - * (0,2) - * (-1,1) - * (-1,0) - * (0,-1) - * (1,-1) - */ - - if (!getWorld().isTileBlocked(getX() + 2, getY())) { - return new Point(getX() + 2, getY()); - - } else if (!getWorld().isTileBlocked(getX() + 2, getY() + 1)) { - return new Point(getX() + 2, getY() + 1); - - } else if (!getWorld().isTileBlocked(getX() + 1, getY() + 2)) { - return new Point(getX() + 1, getY() + 2); - - } else if (!getWorld().isTileBlocked(getX(), getY() + 2)) { - return new Point(getX(), getY() + 2); - - } else if (!getWorld().isTileBlocked(getX() + -1, getY() + 1)) { - return new Point(getX() + -1, getY() + 1); - - } else if (!getWorld().isTileBlocked(getX() + -1, getY())) { - return new Point(getX() + -1, getY()); - - } else if (!getWorld().isTileBlocked(getX(), getY() + -1)) { - return new Point(getX(), getY() + -1); - - } else if (!getWorld().isTileBlocked(getX() + 1, getY() + -1)) { - return new Point(getX() + 1, getY() + -1); - - } else { - return null; - } - - } ArrayList getNpcs() { return npcs; diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java index abc7c14..0fed570 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NpcPlugin.java @@ -20,7 +20,7 @@ public class NpcPlugin extends ServerPlugin { @Override public void init(ServerConfiguration configuration, GameRegistry registry) { - listeners.add(new WorldCreationListener()); + listeners.add(new WorldCreationListener(configuration.getInt("factory_spawn_rate"))); listeners.add(new CpuInitialisationListener()); listeners.add(new VaultWorldUpdateListener(configuration)); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java index 4eb597f..b4680e0 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Obstacle.java @@ -33,6 +33,7 @@ public class Obstacle extends GameObject implements Attackable { } public Obstacle(Document document) { + super(document); style = document.getInteger("style"); } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java index 09f9a9e..9b942d4 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Portal.java @@ -3,13 +3,14 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.game.objects.Enterable; import net.simon987.server.game.objects.GameObject; +import net.simon987.server.game.objects.Structure; import net.simon987.server.game.objects.Updatable; import net.simon987.server.game.world.Location; import net.simon987.server.game.world.World; import org.bson.Document; -public class Portal extends GameObject implements Enterable { +public class Portal extends Structure implements Enterable { /** * Destination location @@ -19,11 +20,11 @@ public class Portal extends GameObject implements Enterable { public static final int MAP_INFO = 0x0020; public Portal() { - + super(1, 1); } public Portal(Document document) { - super(document); + super(document, 1, 1); destination = new Location( document.getInteger("dstWorldX"), diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java index fca225c..114052b 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/RadioTower.java @@ -1,26 +1,27 @@ package net.simon987.npcplugin; -import net.simon987.server.game.objects.GameObject; +import net.simon987.server.GameServer; import net.simon987.server.game.objects.Programmable; +import net.simon987.server.game.objects.Structure; import net.simon987.server.game.objects.Updatable; import org.bson.Document; import java.util.ArrayList; -public class RadioTower extends GameObject implements Programmable, Updatable { +public class RadioTower extends Structure implements Programmable, Updatable { private static final int MAP_INFO = 0x1000; - public static final int MAX_RANGE = 3; //todo load from config + public static final int MAX_RANGE = GameServer.INSTANCE.getConfig().getInt("radio_tower_range"); private static final int MAX_MESSAGES = 16; public RadioTower() { - + super(1, 1); } public RadioTower(Document document) { - super(document); + super(document, 1, 1); NpcPlugin.getRadioTowers().add(this); } diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java index 241f659..e8e917f 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDimension.java @@ -1,6 +1,7 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; +import net.simon987.server.ServerConfiguration; import net.simon987.server.game.objects.Direction; import net.simon987.server.game.world.Location; import net.simon987.server.game.world.TileMap; @@ -43,16 +44,18 @@ public class VaultDimension { * 4. Choose a random world from the last layer and create the vault box there (objective) * 5. Create an exit portal in the home world * - * This process is actually done in 2 passes, in the first pass, worlds are defined + * This process is done in 2 passes, in the first pass, worlds are defined * as a set of coordinates + a list of opening directions, then they are actually generated */ - int minLayerCount = 4; - int maxLayerCount = 6; - int minAttachedWorld = 0; - int maxAttachedWorld = 4; //todo cap at 4 to avoid infinite loop - int minElectricBoxCount = 2; - int maxElectricBoxCount = 4; + ServerConfiguration config = GameServer.INSTANCE.getConfig(); + + int minLayerCount = config.getInt("vault_wg_min_layer_count"); + int maxLayerCount = config.getInt("vault_wg_max_layer_count"); + int minAttachedWorld = config.getInt("vault_wg_min_attached_world"); + int maxAttachedWorld = Math.min(config.getInt("vault_wg_max_attached_world"), 4); + int minElectricBoxCount = config.getInt("vault_wg_min_electric_box_count"); + int maxElectricBoxCount = config.getInt("vault_wg_max_electric_box_count"); HashMap> worldLayers = new HashMap<>(); VaultWorldGenerator generator = new VaultWorldGenerator(); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java index 5d7845d..97268be 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/VaultDoor.java @@ -2,10 +2,7 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.crypto.RandomStringGenerator; -import net.simon987.server.game.objects.Enterable; -import net.simon987.server.game.objects.GameObject; -import net.simon987.server.game.objects.Programmable; -import net.simon987.server.game.objects.Updatable; +import net.simon987.server.game.objects.*; import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; import org.bson.Document; @@ -13,7 +10,7 @@ import org.bson.Document; import java.util.Arrays; -public class VaultDoor extends GameObject implements Programmable, Enterable, Updatable { +public class VaultDoor extends Structure implements Programmable, Enterable, Updatable { private static final int MAP_INFO = 0x0800; @@ -43,6 +40,8 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up private int cypherId; public VaultDoor(int cypherId) { + super(1, 1); + this.cypherId = cypherId; this.randomStringGenerator = new RandomStringGenerator(); @@ -51,7 +50,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up } public VaultDoor(Document document) { - super(document); + super(document, 1, 1); setX(document.getInteger("x")); setY(document.getInteger("y")); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java index 21a26a3..83003a3 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/event/WorldCreationListener.java @@ -8,6 +8,7 @@ import net.simon987.server.GameServer; import net.simon987.server.event.GameEvent; import net.simon987.server.event.GameEventListener; import net.simon987.server.event.WorldGenerationEvent; +import net.simon987.server.game.world.TileMap; import net.simon987.server.game.world.World; import net.simon987.server.logging.LogManager; @@ -18,12 +19,15 @@ public class WorldCreationListener implements GameEventListener { /** * Spawn rate. Higher = rarer: A factory will be spawn about every FACTORY_SPAWN_RATE generated Worlds - *
TODO: Get from config.properties */ - private static final int FACTORY_SPAWN_RATE = 35; + private static int FACTORY_SPAWN_RATE = 0; private Random random = new Random(); + public WorldCreationListener(int factorySpawnRate) { + FACTORY_SPAWN_RATE = factorySpawnRate; + } + @Override public Class getListenedEventType() { return WorldGenerationEvent.class; @@ -66,7 +70,7 @@ public class WorldCreationListener implements GameEventListener { } //Also spawn a radio tower in the same World - Point p = world.getRandomPassableTile(); + Point p = world.getRandomTileWithAdjacent(8, TileMap.PLAIN_TILE); if (p != null) { while (p.x == 0 || p.x == world.getWorldSize() - 1 || p.y == world.getWorldSize() - 1 || p.y == 0) { p = world.getRandomPassableTile(); diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java index 6894f53..06467e9 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/WorldUtils.java @@ -64,7 +64,6 @@ public class WorldUtils { BiomassBlob biomassBlob = new BiomassBlob(); biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); - // biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config? biomassBlob.setBiomassCount(yield); biomassBlob.setX(p.x); biomassBlob.setY(p.y); diff --git a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java index 92ece42..702fe94 100644 --- a/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java +++ b/Plugin Plant/src/main/java/net/simon987/biomassplugin/event/ObjectDeathListener.java @@ -59,7 +59,6 @@ public class ObjectDeathListener implements GameEventListener { BiomassBlob biomassBlob = new BiomassBlob(); biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); - // biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config? biomassBlob.setBiomassCount(biomassDropCount); biomassBlob.setX(x); biomassBlob.setY(y); diff --git a/Server/src/main/java/net/simon987/server/assembly/Assembler.java b/Server/src/main/java/net/simon987/server/assembly/Assembler.java index e02a627..e11b655 100755 --- a/Server/src/main/java/net/simon987/server/assembly/Assembler.java +++ b/Server/src/main/java/net/simon987/server/assembly/Assembler.java @@ -1,5 +1,6 @@ package net.simon987.server.assembly; +import net.simon987.server.GameServer; import net.simon987.server.ServerConfiguration; import net.simon987.server.assembly.exception.*; import net.simon987.server.logging.LogManager; @@ -24,7 +25,7 @@ public class Assembler { private RegisterSet registerSet; - private static final int MEM_SIZE = 0x10000; // Size in words todo load from config + private static final int MEM_SIZE = GameServer.INSTANCE.getConfig().getInt("memory_size"); public Assembler(InstructionSet instructionSet, RegisterSet registerSet, ServerConfiguration config) { this.instructionSet = instructionSet; diff --git a/Server/src/main/java/net/simon987/server/game/GameUniverse.java b/Server/src/main/java/net/simon987/server/game/GameUniverse.java index 7251bb4..5136814 100644 --- a/Server/src/main/java/net/simon987/server/game/GameUniverse.java +++ b/Server/src/main/java/net/simon987/server/game/GameUniverse.java @@ -274,7 +274,7 @@ public class GameUniverse { public String getGuestUsername() { int i = 1; - while (i < 10000) { //todo get Max guest user cap from config + while (i < 50000) { if (getUser("guest" + String.valueOf(i)) != null) { i++; continue; diff --git a/Server/src/main/java/net/simon987/server/game/objects/Structure.java b/Server/src/main/java/net/simon987/server/game/objects/Structure.java new file mode 100644 index 0000000..4fcbf61 --- /dev/null +++ b/Server/src/main/java/net/simon987/server/game/objects/Structure.java @@ -0,0 +1,80 @@ +package net.simon987.server.game.objects; + +import org.bson.Document; + +import java.awt.*; + +/** + * Game object that is stationary. + */ +public abstract class Structure extends GameObject { + + /** + * Length of the structure in tiles for the x axis + */ + private int width; + + /** + * Lenght of the structure in tiles for the y axis + */ + private int height; + + public Structure(Document document, int width, int height) { + super(document); + this.width = width; + this.height = height; + } + + public Structure(int width, int height) { + this.width = width; + this.height = height; + } + + /** + * Get the first non-blocked tile that is directly adjacent to the factory + * + * @return The coordinates of the first non-blocked tile found, null if none is found. + */ + public Point getAdjacentTile() { + + //Top + for (int x = getX() - 1; x < getX() + width; x++) { + if (!getWorld().isTileBlocked(x, getY() - 1)) { + return new Point(x, getY() - 1); + } + } + //Right + for (int y = getY() + width; y < getY() + height; y++) { + if (!getWorld().isTileBlocked(getX() + width, y)) { + return new Point(getX() + width, y); + } + } + //Bottom + for (int x = getX() - 1; x < getX() + width; x++) { + if (!getWorld().isTileBlocked(x, getY() + height)) { + return new Point(x, getY() + height); + } + } + //Left + for (int y = getY() - 1; y < getY() + height; y++) { + if (!getWorld().isTileBlocked(getX() - 1, y)) { + return new Point(getX() - 1, y); + } + } + + return null; + } + + @Override + public boolean isAt(int x, int y) { + + /* + * Object is width x height tiles, the (x,y) coordinates of the object being + * at top-left. + * # . + * . . + */ + return x >= getX() && x < getX() + width && y >= getY() && y < getY() + height; + } + +} diff --git a/Server/src/main/java/net/simon987/server/game/world/World.java b/Server/src/main/java/net/simon987/server/game/world/World.java index 90113e8..1d07627 100644 --- a/Server/src/main/java/net/simon987/server/game/world/World.java +++ b/Server/src/main/java/net/simon987/server/game/world/World.java @@ -456,24 +456,6 @@ public class World implements MongoSerializable { return res; } - public Point getAdjacentTile(int x, int y) { - - if (!isTileBlocked(x + 1, y)) { - return new Point(x + 1, y); - - } else if (!isTileBlocked(x, y + 1)) { - return new Point(x, getY() + 1); - - } else if (!isTileBlocked(x - 1, y)) { - return new Point(x - 1, getY()); - - } else if (!isTileBlocked(x, y - 1)) { - return new Point(x, y - 1); - } else { - return null; - } - } - public Collection getGameObjects() { return gameObjects.values(); } diff --git a/Server/src/main/java/net/simon987/server/web/PlayPage.java b/Server/src/main/java/net/simon987/server/web/PlayPage.java index 71345ac..f91482d 100644 --- a/Server/src/main/java/net/simon987/server/web/PlayPage.java +++ b/Server/src/main/java/net/simon987/server/web/PlayPage.java @@ -1,5 +1,6 @@ package net.simon987.server.web; +import net.simon987.server.GameServer; import spark.ModelAndView; import spark.Request; import spark.Response; @@ -14,6 +15,7 @@ public class PlayPage implements TemplateViewRoute { public ModelAndView handle(Request request, Response response) { Map model = new HashMap<>(1); model.put("session", request.session()); + model.put("gamePageTitle", GameServer.INSTANCE.getConfig().getString("server_name")); return new ModelAndView(model, "play.vm"); } diff --git a/Server/src/main/java/net/simon987/server/websocket/CodeRequestHandler.java b/Server/src/main/java/net/simon987/server/websocket/CodeRequestHandler.java index 4aa6cdc..5bc43cb 100644 --- a/Server/src/main/java/net/simon987/server/websocket/CodeRequestHandler.java +++ b/Server/src/main/java/net/simon987/server/websocket/CodeRequestHandler.java @@ -1,5 +1,6 @@ package net.simon987.server.websocket; +import net.simon987.server.GameServer; import net.simon987.server.logging.LogManager; import org.json.simple.JSONObject; @@ -18,7 +19,7 @@ public class CodeRequestHandler implements MessageHandler { JSONObject response = new JSONObject(); response.put("t", "code"); - response.put("code", "; Create a free account to control your own Cubot with assembly language!"); //todo load from config + response.put("code", GameServer.INSTANCE.getConfig().getString("guest_user_code")); user.getWebSocket().getRemote().sendString(response.toJSONString()); diff --git a/Server/src/main/resources/config.properties b/Server/src/main/resources/config.properties index c50677e..309e31a 100644 --- a/Server/src/main/resources/config.properties +++ b/Server/src/main/resources/config.properties @@ -9,7 +9,7 @@ keyStore_password= #Server mar_port=4567 mar_address=localhost -server_name=Official MAR server +server_name=MAR dev #Database mongo_dbname=mar_beta @@ -51,6 +51,7 @@ new_user_code=; Welcome to Much Assembly required!\n\ \t; Write code here\n\ \tbrk new_user_item=0 +guest_user_code=; Create a free account to control your own Cubot with assembly language! #Cubot battery_max_energy=60000 cubot_max_shield=100 @@ -60,14 +61,22 @@ shield_energy_cost=50 npc_lifetime=1024 npc_max_factory_distance=3 factory_max_npc_count=16 +factory_spawn_rate=2 harvester_hp_max=100 harvester_regen=5 harvester_biomass_drop_count=8 +radio_tower_range=3 #Vaults vault_door_open_time=4 min_electric_box_count=1 min_electric_box_respawn_count=1 max_electric_box_respawn_count=4 +vault_wg_min_layer_count=4 +vault_wg_max_layer_count=6 +vault_wg_min_attached_world=0 +vault_wg_max_attached_world=4 +vault_wg_min_electric_box_count=2 +vault_wg_max_electric_box_count=4 #ElectricBox electric_box_hp=250 electric_box_respawnTime=256 diff --git a/Server/src/main/resources/templates/play.vm b/Server/src/main/resources/templates/play.vm index d684a2b..7c667bb 100644 --- a/Server/src/main/resources/templates/play.vm +++ b/Server/src/main/resources/templates/play.vm @@ -1,6 +1,6 @@ - #set ($title = "Official game server") ## TODO get from config + #set ($title = $gamePageTitle) #set ($cur_page = "play") #parse("head.vm")