From 2e6ebbb9c31c8d02ed81760efc5852ffb5debac4 Mon Sep 17 00:00:00 2001 From: MagicBot Date: Tue, 8 Aug 2023 10:29:01 -0400 Subject: [PATCH] Refactor of mob powers loading for efficiency. --- src/engine/db/handlers/dbMobBaseHandler.java | 19 ------ src/engine/db/handlers/dbPowerHandler.java | 70 ++++++++++++++++++++ src/engine/gameManager/DbManager.java | 2 + src/engine/gameManager/PowersManager.java | 22 +----- src/engine/objects/Mob.java | 18 +++-- src/engine/powers/MobPowerEntry.java | 17 +++++ src/engine/server/world/WorldServer.java | 2 +- 7 files changed, 103 insertions(+), 47 deletions(-) create mode 100644 src/engine/db/handlers/dbPowerHandler.java create mode 100644 src/engine/powers/MobPowerEntry.java diff --git a/src/engine/db/handlers/dbMobBaseHandler.java b/src/engine/db/handlers/dbMobBaseHandler.java index fce26d02..0337ff76 100644 --- a/src/engine/db/handlers/dbMobBaseHandler.java +++ b/src/engine/db/handlers/dbMobBaseHandler.java @@ -21,7 +21,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; -import java.util.HashMap; public class dbMobBaseHandler extends dbHandlerBase { @@ -71,24 +70,6 @@ public class dbMobBaseHandler extends dbHandlerBase { } return mobbaseList; } - public HashMap LOAD_STATIC_POWERS(int mobBaseUUID) { - - HashMap powersList = new HashMap<>(); - - try (Connection connection = DbManager.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM `static_npc_mobbase_powers` WHERE `mobbaseUUID`=?")) { - - preparedStatement.setInt(1, mobBaseUUID); - ResultSet rs = preparedStatement.executeQuery(); - - while (rs.next()) - powersList.put(rs.getInt("token"), rs.getInt("rank")); - - } catch (SQLException e) { - Logger.error(e); - } - return powersList; - } public ArrayList GET_RUNEBASE_EFFECTS(int runeID) { diff --git a/src/engine/db/handlers/dbPowerHandler.java b/src/engine/db/handlers/dbPowerHandler.java new file mode 100644 index 00000000..4b0b984a --- /dev/null +++ b/src/engine/db/handlers/dbPowerHandler.java @@ -0,0 +1,70 @@ +// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . +// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· +// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ +// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ +// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ +// Magicbane Emulator Project © 2013 - 2022 +// www.magicbane.com + + +package engine.db.handlers; + +import engine.gameManager.DbManager; +import engine.objects.Mob; +import engine.powers.MobPowerEntry; +import org.pmw.tinylog.Logger; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.HashMap; + +public class dbPowerHandler extends dbHandlerBase { + + public dbPowerHandler() { + this.localClass = Mob.class; + this.localObjectType = engine.Enum.GameObjectType.valueOf(this.localClass.getSimpleName()); + } + + public HashMap> LOAD_MOB_POWERS() { + + HashMap> mobPowers = new HashMap<>(); + MobPowerEntry mobPowerEntry; + + int mobbaseID; + int recordsRead = 0; + + try (Connection connection = DbManager.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM static_npc_mobbase_powers")) { + + ResultSet rs = preparedStatement.executeQuery(); + + while (rs.next()) { + + recordsRead++; + + mobbaseID = rs.getInt("mobbaseID"); + mobPowerEntry = new MobPowerEntry(rs); + + if (mobPowers.get(mobbaseID) == null) { + ArrayList powerList = new ArrayList<>(); + powerList.add(mobPowerEntry); + mobPowers.put(mobbaseID, powerList); + } else { + ArrayList powerList = mobPowers.get(mobbaseID); + powerList.add(mobPowerEntry); + mobPowers.put(mobbaseID, powerList); + } + } + } catch (SQLException e) { + Logger.error(e); + return mobPowers; + } + + Logger.info("read: " + recordsRead + " cached: " + mobPowers.size()); + return mobPowers; + } + +} diff --git a/src/engine/gameManager/DbManager.java b/src/engine/gameManager/DbManager.java index 25792846..437a9d5c 100644 --- a/src/engine/gameManager/DbManager.java +++ b/src/engine/gameManager/DbManager.java @@ -72,6 +72,8 @@ public enum DbManager { public static final dbShrineHandler ShrineQueries = new dbShrineHandler(); public static final dbHeightMapHandler HeightMapQueries = new dbHeightMapHandler(); public static final dbRunegateHandler RunegateQueries = new dbRunegateHandler(); + + public static final dbPowerHandler PowerQueries = new dbPowerHandler(); private static final EnumMap> objectCache = new EnumMap<>(GameObjectType.class); public static Hasher hasher; private static HikariDataSource connectionPool = null; diff --git a/src/engine/gameManager/PowersManager.java b/src/engine/gameManager/PowersManager.java index 5cdd14f5..06b37eaa 100644 --- a/src/engine/gameManager/PowersManager.java +++ b/src/engine/gameManager/PowersManager.java @@ -55,7 +55,7 @@ public enum PowersManager { public static HashMap ActionTokenByIDString = new HashMap<>(); public static HashMap modifiersByToken = new HashMap<>(); public static HashMap AnimationOverrides = new HashMap<>(); - public static HashMap> AllMobPowers = new HashMap<>(); + public static HashMap> AllMobPowers = new HashMap<>(); private static JobScheduler js; private PowersManager() { @@ -2778,26 +2778,6 @@ public enum PowersManager { } } - public static void LoadAllMobPowers() { - - int count = 0; - - for (AbstractGameObject mobBaseAgo : DbManager.getList(GameObjectType.MobBase)) { - - int mobBaseID = ((MobBase) mobBaseAgo).getLoadID(); - - HashMap powersList = DbManager.MobBaseQueries.LOAD_STATIC_POWERS(mobBaseID); - - if (powersList.isEmpty()) - continue; - ; - - AllMobPowers.put(mobBaseID, powersList); - count++; - } - - Logger.info("Powers loaded for " + count + " Mobbases/"); - } } diff --git a/src/engine/objects/Mob.java b/src/engine/objects/Mob.java index 43eca335..1619b44b 100644 --- a/src/engine/objects/Mob.java +++ b/src/engine/objects/Mob.java @@ -29,6 +29,7 @@ import engine.net.DispatchMessage; import engine.net.client.msg.PetMsg; import engine.net.client.msg.PlaceAssetMsg; import engine.powers.EffectsBase; +import engine.powers.MobPowerEntry; import engine.server.MBServerStatics; import org.joda.time.DateTime; import org.pmw.tinylog.Logger; @@ -1950,19 +1951,23 @@ public class Mob extends AbstractIntelligenceAgent { // Powers from mobbase if (PowersManager.AllMobPowers.containsKey(this.getMobBaseID())) - mobPowers.putAll(PowersManager.AllMobPowers.get(this.getMobBaseID())); + for (MobPowerEntry mobPowerEntry : PowersManager.AllMobPowers.get(this.getMobBaseID())) + mobPowers.put(mobPowerEntry.token, mobPowerEntry.rank); // Powers from contract if (PowersManager.AllMobPowers.containsKey(this.contract.getContractID())) - mobPowers.putAll(PowersManager.AllMobPowers.get(this.contract.getContractID())); + for (MobPowerEntry mobPowerEntry : PowersManager.AllMobPowers.get(this.contract.getContractID())) + mobPowers.put(mobPowerEntry.token, mobPowerEntry.rank); if (this.equip == null) { Logger.error("Null equipset returned for uuid " + currentID); this.equip = new HashMap<>(0); } + // Combine mobbase and mob aggro arrays into one bitvector //skip for pets + if (this.isPet() == false && this.isSummonedPet() == false && this.isNecroPet() == false) { if (this.getMobBase().notEnemy.size() > 0) this.notEnemy.addAll(this.getMobBase().notEnemy); @@ -1970,6 +1975,7 @@ public class Mob extends AbstractIntelligenceAgent { if (this.getMobBase().enemy.size() > 0) this.enemy.addAll(this.getMobBase().enemy); } + try { NPCManager.applyRuneSetEffects(this); recalculateStats(); @@ -1980,6 +1986,7 @@ public class Mob extends AbstractIntelligenceAgent { Bounds mobBounds = Bounds.borrow(); mobBounds.setBounds(this.getLoc()); this.setBounds(mobBounds); + if (this.contract != null && this.contract.getContractID() == 910) { this.isPlayerGuard = true; this.BehaviourType = MobBehaviourType.GuardCaptain; @@ -1990,6 +1997,7 @@ public class Mob extends AbstractIntelligenceAgent { if (!this.isGuard() && !this.isPlayerGuard() && !this.isPet() && !this.isNecroPet() && !this.isSummonedPet() && !this.isCharmedPet()) { this.patrolPoints = new ArrayList<>(); + for (int i = 0; i < 5; ++i) { float patrolRadius = this.getSpawnRadius(); @@ -2001,10 +2009,12 @@ public class Mob extends AbstractIntelligenceAgent { Vector3fImmutable newPatrolPoint = Vector3fImmutable.getRandomPointInCircle(this.getBindLoc(), patrolRadius); this.patrolPoints.add(newPatrolPoint); + if (i == 1) MovementManager.translocate(this, newPatrolPoint, null); } } + if (this.BehaviourType == null) this.BehaviourType = this.getMobBase().fsm; @@ -2049,10 +2059,6 @@ public class Mob extends AbstractIntelligenceAgent { this.isSiege = isSiege; } - public long getTimeToSpawnSiege() { - return timeToSpawnSiege; - } - public void setTimeToSpawnSiege(long timeToSpawnSiege) { this.timeToSpawnSiege = timeToSpawnSiege; } diff --git a/src/engine/powers/MobPowerEntry.java b/src/engine/powers/MobPowerEntry.java new file mode 100644 index 00000000..1aee9848 --- /dev/null +++ b/src/engine/powers/MobPowerEntry.java @@ -0,0 +1,17 @@ +package engine.powers; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class MobPowerEntry { + + public int token; + public int rank; + + + public MobPowerEntry(ResultSet rs) throws SQLException { + this.token = rs.getInt("token"); + this.rank = rs.getInt("rank"); + } + +} diff --git a/src/engine/server/world/WorldServer.java b/src/engine/server/world/WorldServer.java index cdcf6f90..023a85e9 100644 --- a/src/engine/server/world/WorldServer.java +++ b/src/engine/server/world/WorldServer.java @@ -349,7 +349,7 @@ public class WorldServer { DbManager.MobBaseQueries.GET_ALL_MOBBASES(); Logger.info("Loading Mob Powers for MobBases"); - PowersManager.LoadAllMobPowers(); + PowersManager.AllMobPowers = DbManager.PowerQueries.LOAD_MOB_POWERS(); Logger.info("Loading item enchants"); DbManager.LootQueries.LOAD_ENCHANT_VALUES();