Added structure class #145 and moved many constants to config.properties

This commit is contained in:
simon 2018-05-12 20:32:10 -04:00
parent be8dd14d36
commit 4b67798180
18 changed files with 136 additions and 113 deletions

View File

@ -1,7 +1,7 @@
package net.simon987.npcplugin; package net.simon987.npcplugin;
import net.simon987.server.GameServer; 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 net.simon987.server.game.objects.Updatable;
import org.bson.Document; import org.bson.Document;
@ -12,7 +12,7 @@ import java.util.List;
/** /**
* Game objects that regularly creates NonPlayerCharacters * 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; private static final int MAP_INFO = 0x0200;
@ -48,10 +48,11 @@ public class Factory extends GameObject implements Updatable {
private boolean initialised = false; private boolean initialised = false;
public Factory() { public Factory() {
super(2, 2);
} }
public Factory(Document document) { public Factory(Document document) {
super(document); super(document, 2, 2);
tmpNpcArray = ((ArrayList) document.get("npcs")).toArray(); 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 @Override
public Document mongoSerialise() { public Document mongoSerialise() {
Document dbObject = super.mongoSerialise(); Document dbObject = super.mongoSerialise();
@ -139,54 +128,6 @@ public class Factory extends GameObject implements Updatable {
return dbObject; 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<NonPlayerCharacter> getNpcs() { ArrayList<NonPlayerCharacter> getNpcs() {
return npcs; return npcs;

View File

@ -20,7 +20,7 @@ public class NpcPlugin extends ServerPlugin {
@Override @Override
public void init(ServerConfiguration configuration, GameRegistry registry) { 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 CpuInitialisationListener());
listeners.add(new VaultWorldUpdateListener(configuration)); listeners.add(new VaultWorldUpdateListener(configuration));

View File

@ -33,6 +33,7 @@ public class Obstacle extends GameObject implements Attackable {
} }
public Obstacle(Document document) { public Obstacle(Document document) {
super(document);
style = document.getInteger("style"); style = document.getInteger("style");
} }

View File

@ -3,13 +3,14 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.game.objects.Enterable; import net.simon987.server.game.objects.Enterable;
import net.simon987.server.game.objects.GameObject; 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.objects.Updatable;
import net.simon987.server.game.world.Location; import net.simon987.server.game.world.Location;
import net.simon987.server.game.world.World; import net.simon987.server.game.world.World;
import org.bson.Document; import org.bson.Document;
public class Portal extends GameObject implements Enterable { public class Portal extends Structure implements Enterable {
/** /**
* Destination location * Destination location
@ -19,11 +20,11 @@ public class Portal extends GameObject implements Enterable {
public static final int MAP_INFO = 0x0020; public static final int MAP_INFO = 0x0020;
public Portal() { public Portal() {
super(1, 1);
} }
public Portal(Document document) { public Portal(Document document) {
super(document); super(document, 1, 1);
destination = new Location( destination = new Location(
document.getInteger("dstWorldX"), document.getInteger("dstWorldX"),

View File

@ -1,26 +1,27 @@
package net.simon987.npcplugin; 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.Programmable;
import net.simon987.server.game.objects.Structure;
import net.simon987.server.game.objects.Updatable; import net.simon987.server.game.objects.Updatable;
import org.bson.Document; import org.bson.Document;
import java.util.ArrayList; 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; 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; private static final int MAX_MESSAGES = 16;
public RadioTower() { public RadioTower() {
super(1, 1);
} }
public RadioTower(Document document) { public RadioTower(Document document) {
super(document); super(document, 1, 1);
NpcPlugin.getRadioTowers().add(this); NpcPlugin.getRadioTowers().add(this);
} }

View File

@ -1,6 +1,7 @@
package net.simon987.npcplugin; package net.simon987.npcplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
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.TileMap;
@ -43,16 +44,18 @@ 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)
* 5. Create an exit portal in the home world * 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 * as a set of coordinates + a list of opening directions, then they are actually generated
*/ */
int minLayerCount = 4; ServerConfiguration config = GameServer.INSTANCE.getConfig();
int maxLayerCount = 6;
int minAttachedWorld = 0; int minLayerCount = config.getInt("vault_wg_min_layer_count");
int maxAttachedWorld = 4; //todo cap at 4 to avoid infinite loop int maxLayerCount = config.getInt("vault_wg_max_layer_count");
int minElectricBoxCount = 2; int minAttachedWorld = config.getInt("vault_wg_min_attached_world");
int maxElectricBoxCount = 4; 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<Integer, ArrayList<WorldBluePrint>> worldLayers = new HashMap<>(); HashMap<Integer, ArrayList<WorldBluePrint>> worldLayers = new HashMap<>();
VaultWorldGenerator generator = new VaultWorldGenerator(); VaultWorldGenerator generator = new VaultWorldGenerator();

View File

@ -2,10 +2,7 @@ package net.simon987.npcplugin;
import net.simon987.server.GameServer; import net.simon987.server.GameServer;
import net.simon987.server.crypto.RandomStringGenerator; import net.simon987.server.crypto.RandomStringGenerator;
import net.simon987.server.game.objects.Enterable; import net.simon987.server.game.objects.*;
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.world.World; import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.bson.Document; import org.bson.Document;
@ -13,7 +10,7 @@ import org.bson.Document;
import java.util.Arrays; 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; private static final int MAP_INFO = 0x0800;
@ -43,6 +40,8 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
private int cypherId; private int cypherId;
public VaultDoor(int cypherId) { public VaultDoor(int cypherId) {
super(1, 1);
this.cypherId = cypherId; this.cypherId = cypherId;
this.randomStringGenerator = new RandomStringGenerator(); this.randomStringGenerator = new RandomStringGenerator();
@ -51,7 +50,7 @@ public class VaultDoor extends GameObject implements Programmable, Enterable, Up
} }
public VaultDoor(Document document) { public VaultDoor(Document document) {
super(document); super(document, 1, 1);
setX(document.getInteger("x")); setX(document.getInteger("x"));
setY(document.getInteger("y")); setY(document.getInteger("y"));

View File

@ -8,6 +8,7 @@ import net.simon987.server.GameServer;
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.World; import net.simon987.server.game.world.World;
import net.simon987.server.logging.LogManager; 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 * Spawn rate. Higher = rarer: A factory will be spawn about every FACTORY_SPAWN_RATE generated Worlds
* <br>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(); private Random random = new Random();
public WorldCreationListener(int factorySpawnRate) {
FACTORY_SPAWN_RATE = factorySpawnRate;
}
@Override @Override
public Class getListenedEventType() { public Class getListenedEventType() {
return WorldGenerationEvent.class; return WorldGenerationEvent.class;
@ -66,7 +70,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.getRandomPassableTile(); Point p = world.getRandomTileWithAdjacent(8, TileMap.PLAIN_TILE);
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

@ -64,7 +64,6 @@ public class WorldUtils {
BiomassBlob biomassBlob = new BiomassBlob(); BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
// biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
biomassBlob.setBiomassCount(yield); biomassBlob.setBiomassCount(yield);
biomassBlob.setX(p.x); biomassBlob.setX(p.x);
biomassBlob.setY(p.y); biomassBlob.setY(p.y);

View File

@ -59,7 +59,6 @@ public class ObjectDeathListener implements GameEventListener {
BiomassBlob biomassBlob = new BiomassBlob(); BiomassBlob biomassBlob = new BiomassBlob();
biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId()); biomassBlob.setObjectId(GameServer.INSTANCE.getGameUniverse().getNextObjectId());
// biomassBlob.setStyle(0); //TODO: set style depending on difficulty level? or random? from config?
biomassBlob.setBiomassCount(biomassDropCount); biomassBlob.setBiomassCount(biomassDropCount);
biomassBlob.setX(x); biomassBlob.setX(x);
biomassBlob.setY(y); biomassBlob.setY(y);

View File

@ -1,5 +1,6 @@
package net.simon987.server.assembly; package net.simon987.server.assembly;
import net.simon987.server.GameServer;
import net.simon987.server.ServerConfiguration; import net.simon987.server.ServerConfiguration;
import net.simon987.server.assembly.exception.*; import net.simon987.server.assembly.exception.*;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
@ -24,7 +25,7 @@ public class Assembler {
private RegisterSet registerSet; 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) { public Assembler(InstructionSet instructionSet, RegisterSet registerSet, ServerConfiguration config) {
this.instructionSet = instructionSet; this.instructionSet = instructionSet;

View File

@ -274,7 +274,7 @@ public class GameUniverse {
public String getGuestUsername() { public String getGuestUsername() {
int i = 1; int i = 1;
while (i < 10000) { //todo get Max guest user cap from config while (i < 50000) {
if (getUser("guest" + String.valueOf(i)) != null) { if (getUser("guest" + String.valueOf(i)) != null) {
i++; i++;
continue; continue;

View File

@ -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;
}
}

View File

@ -456,24 +456,6 @@ public class World implements MongoSerializable {
return res; 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<GameObject> getGameObjects() { public Collection<GameObject> getGameObjects() {
return gameObjects.values(); return gameObjects.values();
} }

View File

@ -1,5 +1,6 @@
package net.simon987.server.web; package net.simon987.server.web;
import net.simon987.server.GameServer;
import spark.ModelAndView; import spark.ModelAndView;
import spark.Request; import spark.Request;
import spark.Response; import spark.Response;
@ -14,6 +15,7 @@ public class PlayPage implements TemplateViewRoute {
public ModelAndView handle(Request request, Response response) { public ModelAndView handle(Request request, Response response) {
Map<String, Object> model = new HashMap<>(1); Map<String, Object> model = new HashMap<>(1);
model.put("session", request.session()); model.put("session", request.session());
model.put("gamePageTitle", GameServer.INSTANCE.getConfig().getString("server_name"));
return new ModelAndView(model, "play.vm"); return new ModelAndView(model, "play.vm");
} }

View File

@ -1,5 +1,6 @@
package net.simon987.server.websocket; package net.simon987.server.websocket;
import net.simon987.server.GameServer;
import net.simon987.server.logging.LogManager; import net.simon987.server.logging.LogManager;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
@ -18,7 +19,7 @@ public class CodeRequestHandler implements MessageHandler {
JSONObject response = new JSONObject(); JSONObject response = new JSONObject();
response.put("t", "code"); 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()); user.getWebSocket().getRemote().sendString(response.toJSONString());

View File

@ -9,7 +9,7 @@ keyStore_password=
#Server #Server
mar_port=4567 mar_port=4567
mar_address=localhost mar_address=localhost
server_name=Official MAR server server_name=MAR dev
#Database #Database
mongo_dbname=mar_beta mongo_dbname=mar_beta
@ -51,6 +51,7 @@ new_user_code=; Welcome to Much Assembly required!\n\
\t; Write code here\n\ \t; Write code here\n\
\tbrk \tbrk
new_user_item=0 new_user_item=0
guest_user_code=; Create a free account to control your own Cubot with assembly language!
#Cubot #Cubot
battery_max_energy=60000 battery_max_energy=60000
cubot_max_shield=100 cubot_max_shield=100
@ -60,14 +61,22 @@ shield_energy_cost=50
npc_lifetime=1024 npc_lifetime=1024
npc_max_factory_distance=3 npc_max_factory_distance=3
factory_max_npc_count=16 factory_max_npc_count=16
factory_spawn_rate=2
harvester_hp_max=100 harvester_hp_max=100
harvester_regen=5 harvester_regen=5
harvester_biomass_drop_count=8 harvester_biomass_drop_count=8
radio_tower_range=3
#Vaults #Vaults
vault_door_open_time=4 vault_door_open_time=4
min_electric_box_count=1 min_electric_box_count=1
min_electric_box_respawn_count=1 min_electric_box_respawn_count=1
max_electric_box_respawn_count=4 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 #ElectricBox
electric_box_hp=250 electric_box_hp=250
electric_box_respawnTime=256 electric_box_respawnTime=256

View File

@ -1,6 +1,6 @@
<!doctype html> <!doctype html>
<html lang="en"> <html lang="en">
#set ($title = "Official game server") ## TODO get from config #set ($title = $gamePageTitle)
#set ($cur_page = "play") #set ($cur_page = "play")
#parse("head.vm") #parse("head.vm")
<body> <body>