diff --git a/Plugin Cubot/Plugin Cubot.iml b/Plugin Cubot/Plugin Cubot.iml index 6182e32..f96bbb7 100644 --- a/Plugin Cubot/Plugin Cubot.iml +++ b/Plugin Cubot/Plugin Cubot.iml @@ -20,7 +20,9 @@ - + + + diff --git a/Plugin Misc HW/Plugin Misc HW.iml b/Plugin Misc HW/Plugin Misc HW.iml index e8b738c..7e03db0 100644 --- a/Plugin Misc HW/Plugin Misc HW.iml +++ b/Plugin Misc HW/Plugin Misc HW.iml @@ -19,7 +19,9 @@ - + + + diff --git a/Plugin NPC/Plugin NPC.iml b/Plugin NPC/Plugin NPC.iml index df01d12..b9c98db 100644 --- a/Plugin NPC/Plugin NPC.iml +++ b/Plugin NPC/Plugin NPC.iml @@ -22,7 +22,9 @@ - + + + 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 a7ce582..b1ae4cb 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/Factory.java @@ -65,6 +65,13 @@ public class Factory extends Structure implements Updatable, MessageReceiver { Settlement settlement = NpcPlugin.settlementMap.get(getWorld().getId()); + if (settlement == null) { + //Only happens when server is killed during save function + getWorld().decUpdatable(); + setDead(true); + return; + } + if (cooldown == 0) { if (settlement.getNpcs().size() < MAX_NPC_COUNT) { Point p = getAdjacentTile(); @@ -72,6 +79,9 @@ public class Factory extends Structure implements Updatable, MessageReceiver { if (p != null) { NonPlayerCharacter npc = spawnNPC(p); settlement.addNpc(npc); + + getWorld().addObject(npc); + getWorld().incUpdatable(); } } @@ -102,8 +112,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver { npc.setObjectId(new ObjectId()); npc.setX(p.x); npc.setY(p.y); - getWorld().addObject(npc); - getWorld().incUpdatable(); return npc; } @@ -114,8 +122,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver { npc.setObjectId(new ObjectId()); npc.setX(p.x); npc.setY(p.y); - getWorld().addObject(npc); - getWorld().incUpdatable(); this.locked = true; this.programIndex = 0; @@ -126,8 +132,6 @@ public class Factory extends Structure implements Updatable, MessageReceiver { @Override public boolean sendMessage(char[] message) { - String strMessage = String.valueOf(message); - if (locked) { Settlement settlement = NpcPlugin.settlementMap.get(getWorld().getId()); @@ -136,10 +140,10 @@ public class Factory extends Structure implements Updatable, MessageReceiver { return true; } - } else if (programIndex <= 2048) { //todo config + } else if (programIndex <= PROGRAM_SIZE) { if (programIndex == 0) { - program = new char[2048]; + program = new char[PROGRAM_SIZE]; } System.arraycopy(message, 0, program, programIndex, message.length); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/HackedNPC.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/HackedNPC.java index 904a54a..fb3fcb0 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/HackedNPC.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/HackedNPC.java @@ -2,6 +2,7 @@ package net.simon987.npcplugin; import net.simon987.server.GameServer; import net.simon987.server.assembly.*; +import net.simon987.server.event.ObjectDeathEvent; import net.simon987.server.game.item.Item; import net.simon987.server.game.item.ItemVoid; import net.simon987.server.game.objects.Action; @@ -87,6 +88,11 @@ public class HackedNPC extends NonPlayerCharacter implements ControllableUnit { module.update(); } + //Self-destroy when age limit is reached + if (getAge() >= NonPlayerCharacter.LIFETIME) { + setDead(true); + } + //Don't bother calling checkCompleted() getTask().tick(this); } @@ -312,6 +318,20 @@ public class HackedNPC extends NonPlayerCharacter implements ControllableUnit { return json; } + @Override + public boolean onDeadCallback() { + + getWorld().decUpdatable(); + + if (getSettlement() != null && getSettlement().getNpcs() != null) { + getSettlement().getNpcs().remove(this); + } + + GameServer.INSTANCE.getEventDispatcher().dispatch(new ObjectDeathEvent(this)); + + return false; + } + @Override public JSONObject debugJsonSerialise() { return jsonSerialise(); diff --git a/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java b/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java index 8ad7c65..ddabc73 100644 --- a/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java +++ b/Plugin NPC/src/main/java/net/simon987/npcplugin/NonPlayerCharacter.java @@ -43,12 +43,6 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable private Settlement settlement; - /** - * If set to true, the NPC will be destroyed next tick if it is - * not linked to a Factory - */ - private boolean selfDestroyNextTick = false; - /** * Age of the npc, in ticks */ @@ -92,11 +86,7 @@ public abstract class NonPlayerCharacter extends GameObject implements Updatable //Destroy NPCs that are not linked with a Settlement if (settlement == null) { - if (selfDestroyNextTick) { - setDead(true); - } - - selfDestroyNextTick = true; + setDead(true); } //Heal the NPC 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 609e727..aae031c 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 @@ -37,7 +37,8 @@ public class WorldCreationListener implements GameEventListener { World world = (World) event.getSource(); try { - NpcPlugin.settlementMap.put(world.getId(), new Settlement(world)); + Settlement settlement = new Settlement(world); + NpcPlugin.settlementMap.put(world.getId(), settlement); } catch (WorldGenerationException e) { LogManager.LOGGER.fine(String.format("Exception during settlement generation: %s.", e.getMessage())); diff --git a/Plugin Plant/Plugin Plant.iml b/Plugin Plant/Plugin Plant.iml index df01d12..b9c98db 100644 --- a/Plugin Plant/Plugin Plant.iml +++ b/Plugin Plant/Plugin Plant.iml @@ -22,7 +22,9 @@ - + + + diff --git a/Server/Server.iml b/Server/Server.iml index badff86..fbb67a2 100644 --- a/Server/Server.iml +++ b/Server/Server.iml @@ -21,7 +21,9 @@ - + + + diff --git a/Server/pom.xml b/Server/pom.xml index 88c0c53..07c5697 100644 --- a/Server/pom.xml +++ b/Server/pom.xml @@ -106,8 +106,8 @@ org.mongodb - mongo-java-driver - 3.7.0 + mongodb-driver-sync + 3.9.1 org.springframework.security diff --git a/Server/src/main/java/net/simon987/server/GameServer.java b/Server/src/main/java/net/simon987/server/GameServer.java index 4777c42..8b542a5 100644 --- a/Server/src/main/java/net/simon987/server/GameServer.java +++ b/Server/src/main/java/net/simon987/server/GameServer.java @@ -1,9 +1,7 @@ package net.simon987.server; -import com.mongodb.MongoClient; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoCursor; -import com.mongodb.client.MongoDatabase; +import com.mongodb.MongoClientException; +import com.mongodb.client.*; import com.mongodb.client.model.ReplaceOptions; import net.simon987.server.crypto.CryptoProvider; import net.simon987.server.crypto.SecretKeyGenerator; @@ -58,7 +56,9 @@ public class GameServer implements Runnable { public GameServer() { this.config = new ServerConfiguration("config.properties"); - mongo = new MongoClient(config.getString("mongo_address"), config.getInt("mongo_port")); + String connString = String.format("mongodb://%s:%d", + config.getString("mongo_address"), config.getInt("mongo_port")); + mongo = MongoClients.create(connString); MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname")); MongoCollection userCollection = db.getCollection("user"); @@ -243,7 +243,16 @@ public class GameServer implements Runnable { public void save() { LogManager.LOGGER.info("Saving to MongoDB | W:" + gameUniverse.getWorldCount() + " | U:" + gameUniverse.getUserCount()); + + ClientSession session = null; try { + try { + session = mongo.startSession(); + session.startTransaction(); + } catch (MongoClientException e) { + LogManager.LOGGER.fine("Could not create mongoDB session, will not start a transaction."); + } + MongoDatabase db = mongo.getDatabase(config.getString("mongo_dbname")); ReplaceOptions updateOptions = new ReplaceOptions(); updateOptions.upsert(true); @@ -284,11 +293,18 @@ public class GameServer implements Runnable { //A constant id ensures only one entry is kept and updated, instead of a new entry created every save. server.replaceOne(new Document("_id", "serverinfo"), serverObj, updateOptions); + if (session != null) { + session.commitTransaction(); + } LogManager.LOGGER.info("" + insertedWorlds + " worlds saved, " + unloaded_worlds + " unloaded"); } catch (Exception e) { LogManager.LOGGER.severe("Problem happened during save function"); e.printStackTrace(); + + if (session != null) { + session.commitTransaction(); + } } } diff --git a/Server/src/main/java/net/simon987/server/Main.java b/Server/src/main/java/net/simon987/server/Main.java index a81a075..ca351a4 100644 --- a/Server/src/main/java/net/simon987/server/Main.java +++ b/Server/src/main/java/net/simon987/server/Main.java @@ -8,6 +8,7 @@ import spark.Spark; public class Main { public static void main(String[] args) { + ServerConfiguration config = new ServerConfiguration("config.properties"); LogManager.initialize(config); 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 d3415a6..85c3f20 100644 --- a/Server/src/main/java/net/simon987/server/game/GameUniverse.java +++ b/Server/src/main/java/net/simon987/server/game/GameUniverse.java @@ -1,6 +1,6 @@ package net.simon987.server.game; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoCursor; import com.mongodb.client.MongoDatabase; @@ -20,7 +20,6 @@ import java.util.concurrent.ConcurrentHashMap; public class GameUniverse { - //private ArrayList worlds; private ConcurrentHashMap worlds; //username:user private ConcurrentHashMap users; @@ -233,6 +232,7 @@ public class GameUniverse { } LogManager.LOGGER.severe("Couldn't find object: " + id); + Thread.dumpStack(); return null; } diff --git a/Server/src/main/resources/config.properties b/Server/src/main/resources/config.properties index 8162a50..391d76a 100644 --- a/Server/src/main/resources/config.properties +++ b/Server/src/main/resources/config.properties @@ -40,7 +40,7 @@ wg_maxCopperCount=2 wg_fluidCenterPointMin=0 wg_fluidCenterPointMax=2 #CPU -tick_length=1000 +tick_length=50 org_offset=512 stack_bottom=65536 memory_size=65536 @@ -67,7 +67,7 @@ npc_lifetime=1024 npc_max_factory_distance=3 factory_max_npc_count=16 factory_program_size=1024 -settlement_spawn_rate=5 +settlement_spawn_rate=35 harvester_hp_max=100 harvester_regen=5 harvester_biomass_drop_count=8 diff --git a/Server/src/main/resources/static/css/mar.css b/Server/src/main/resources/static/css/mar.css index 0d10035..74042f6 100644 --- a/Server/src/main/resources/static/css/mar.css +++ b/Server/src/main/resources/static/css/mar.css @@ -115,7 +115,6 @@ .bottom-panel { min-height: 18px; max-height: 100%; - height: 235px; width: 100%; position: fixed; bottom: 0; diff --git a/Server/src/main/resources/static/js/mar.js b/Server/src/main/resources/static/js/mar.js index b34b8c1..6ea23c9 100644 --- a/Server/src/main/resources/static/js/mar.js +++ b/Server/src/main/resources/static/js/mar.js @@ -1368,7 +1368,7 @@ var VaultDoor = (function (_super) { _this.anchor.set(0.55, 0.55); _this.inputEnabled = true; _this.events.onInputDown.add(function (self) { - Debug.goToHex("7FFF", "7FFF", "v" + self.id + "-"); + Debug.goToHex("7FFF", "7FFF", "v" + self.id); document.body.style.cursor = 'default'; document.body.setAttribute("title", ""); }, _this); @@ -1449,7 +1449,7 @@ var ElectricBox = (function (_super) { var Portal = (function (_super) { __extends(Portal, _super); function Portal(json) { - var _this = _super.call(this, Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/Portal") || this; + var _this = _super.call(this, Util.getIsoX(json.x), Util.getIsoY(json.y), 15, "sheet", "objects/portal") || this; _this.anchor.set(0.5, 0.3); _this.tint = config.portal.tint; _this.setText("Portal"); diff --git a/Server/src/main/resources/templates/play.vm b/Server/src/main/resources/templates/play.vm index 678ff99..9761245 100644 --- a/Server/src/main/resources/templates/play.vm +++ b/Server/src/main/resources/templates/play.vm @@ -118,11 +118,7 @@ #parse("footer.vm") -##Console - - -
+
@@ -151,7 +147,6 @@
-

- -