Browse Source

Refactor of mob powers loading for efficiency.

master
MagicBot 1 year ago
parent
commit
2e6ebbb9c3
  1. 19
      src/engine/db/handlers/dbMobBaseHandler.java
  2. 70
      src/engine/db/handlers/dbPowerHandler.java
  3. 2
      src/engine/gameManager/DbManager.java
  4. 22
      src/engine/gameManager/PowersManager.java
  5. 18
      src/engine/objects/Mob.java
  6. 17
      src/engine/powers/MobPowerEntry.java
  7. 2
      src/engine/server/world/WorldServer.java

19
src/engine/db/handlers/dbMobBaseHandler.java

@ -21,7 +21,6 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
public class dbMobBaseHandler extends dbHandlerBase { public class dbMobBaseHandler extends dbHandlerBase {
@ -71,24 +70,6 @@ public class dbMobBaseHandler extends dbHandlerBase {
} }
return mobbaseList; return mobbaseList;
} }
public HashMap<Integer, Integer> LOAD_STATIC_POWERS(int mobBaseUUID) {
HashMap<Integer, Integer> 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<MobBaseEffects> GET_RUNEBASE_EFFECTS(int runeID) { public ArrayList<MobBaseEffects> GET_RUNEBASE_EFFECTS(int runeID) {

70
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<Integer, ArrayList<MobPowerEntry>> LOAD_MOB_POWERS() {
HashMap<Integer, ArrayList<MobPowerEntry>> 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<MobPowerEntry> powerList = new ArrayList<>();
powerList.add(mobPowerEntry);
mobPowers.put(mobbaseID, powerList);
} else {
ArrayList<MobPowerEntry> 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;
}
}

2
src/engine/gameManager/DbManager.java

@ -72,6 +72,8 @@ public enum DbManager {
public static final dbShrineHandler ShrineQueries = new dbShrineHandler(); public static final dbShrineHandler ShrineQueries = new dbShrineHandler();
public static final dbHeightMapHandler HeightMapQueries = new dbHeightMapHandler(); public static final dbHeightMapHandler HeightMapQueries = new dbHeightMapHandler();
public static final dbRunegateHandler RunegateQueries = new dbRunegateHandler(); public static final dbRunegateHandler RunegateQueries = new dbRunegateHandler();
public static final dbPowerHandler PowerQueries = new dbPowerHandler();
private static final EnumMap<GameObjectType, ConcurrentHashMap<Integer, AbstractGameObject>> objectCache = new EnumMap<>(GameObjectType.class); private static final EnumMap<GameObjectType, ConcurrentHashMap<Integer, AbstractGameObject>> objectCache = new EnumMap<>(GameObjectType.class);
public static Hasher hasher; public static Hasher hasher;
private static HikariDataSource connectionPool = null; private static HikariDataSource connectionPool = null;

22
src/engine/gameManager/PowersManager.java

@ -55,7 +55,7 @@ public enum PowersManager {
public static HashMap<String, Integer> ActionTokenByIDString = new HashMap<>(); public static HashMap<String, Integer> ActionTokenByIDString = new HashMap<>();
public static HashMap<Integer, AbstractEffectModifier> modifiersByToken = new HashMap<>(); public static HashMap<Integer, AbstractEffectModifier> modifiersByToken = new HashMap<>();
public static HashMap<String, Integer> AnimationOverrides = new HashMap<>(); public static HashMap<String, Integer> AnimationOverrides = new HashMap<>();
public static HashMap<Integer, HashMap<Integer, Integer>> AllMobPowers = new HashMap<>(); public static HashMap<Integer, ArrayList<MobPowerEntry>> AllMobPowers = new HashMap<>();
private static JobScheduler js; private static JobScheduler js;
private PowersManager() { 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/");
}
} }

18
src/engine/objects/Mob.java

@ -29,6 +29,7 @@ import engine.net.DispatchMessage;
import engine.net.client.msg.PetMsg; import engine.net.client.msg.PetMsg;
import engine.net.client.msg.PlaceAssetMsg; import engine.net.client.msg.PlaceAssetMsg;
import engine.powers.EffectsBase; import engine.powers.EffectsBase;
import engine.powers.MobPowerEntry;
import engine.server.MBServerStatics; import engine.server.MBServerStatics;
import org.joda.time.DateTime; import org.joda.time.DateTime;
import org.pmw.tinylog.Logger; import org.pmw.tinylog.Logger;
@ -1950,19 +1951,23 @@ public class Mob extends AbstractIntelligenceAgent {
// Powers from mobbase // Powers from mobbase
if (PowersManager.AllMobPowers.containsKey(this.getMobBaseID())) 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 // Powers from contract
if (PowersManager.AllMobPowers.containsKey(this.contract.getContractID())) 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) { if (this.equip == null) {
Logger.error("Null equipset returned for uuid " + currentID); Logger.error("Null equipset returned for uuid " + currentID);
this.equip = new HashMap<>(0); this.equip = new HashMap<>(0);
} }
// Combine mobbase and mob aggro arrays into one bitvector // Combine mobbase and mob aggro arrays into one bitvector
//skip for pets //skip for pets
if (this.isPet() == false && this.isSummonedPet() == false && this.isNecroPet() == false) { if (this.isPet() == false && this.isSummonedPet() == false && this.isNecroPet() == false) {
if (this.getMobBase().notEnemy.size() > 0) if (this.getMobBase().notEnemy.size() > 0)
this.notEnemy.addAll(this.getMobBase().notEnemy); this.notEnemy.addAll(this.getMobBase().notEnemy);
@ -1970,6 +1975,7 @@ public class Mob extends AbstractIntelligenceAgent {
if (this.getMobBase().enemy.size() > 0) if (this.getMobBase().enemy.size() > 0)
this.enemy.addAll(this.getMobBase().enemy); this.enemy.addAll(this.getMobBase().enemy);
} }
try { try {
NPCManager.applyRuneSetEffects(this); NPCManager.applyRuneSetEffects(this);
recalculateStats(); recalculateStats();
@ -1980,6 +1986,7 @@ public class Mob extends AbstractIntelligenceAgent {
Bounds mobBounds = Bounds.borrow(); Bounds mobBounds = Bounds.borrow();
mobBounds.setBounds(this.getLoc()); mobBounds.setBounds(this.getLoc());
this.setBounds(mobBounds); this.setBounds(mobBounds);
if (this.contract != null && this.contract.getContractID() == 910) { if (this.contract != null && this.contract.getContractID() == 910) {
this.isPlayerGuard = true; this.isPlayerGuard = true;
this.BehaviourType = MobBehaviourType.GuardCaptain; 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()) { if (!this.isGuard() && !this.isPlayerGuard() && !this.isPet() && !this.isNecroPet() && !this.isSummonedPet() && !this.isCharmedPet()) {
this.patrolPoints = new ArrayList<>(); this.patrolPoints = new ArrayList<>();
for (int i = 0; i < 5; ++i) { for (int i = 0; i < 5; ++i) {
float patrolRadius = this.getSpawnRadius(); float patrolRadius = this.getSpawnRadius();
@ -2001,10 +2009,12 @@ public class Mob extends AbstractIntelligenceAgent {
Vector3fImmutable newPatrolPoint = Vector3fImmutable.getRandomPointInCircle(this.getBindLoc(), patrolRadius); Vector3fImmutable newPatrolPoint = Vector3fImmutable.getRandomPointInCircle(this.getBindLoc(), patrolRadius);
this.patrolPoints.add(newPatrolPoint); this.patrolPoints.add(newPatrolPoint);
if (i == 1) if (i == 1)
MovementManager.translocate(this, newPatrolPoint, null); MovementManager.translocate(this, newPatrolPoint, null);
} }
} }
if (this.BehaviourType == null) if (this.BehaviourType == null)
this.BehaviourType = this.getMobBase().fsm; this.BehaviourType = this.getMobBase().fsm;
@ -2049,10 +2059,6 @@ public class Mob extends AbstractIntelligenceAgent {
this.isSiege = isSiege; this.isSiege = isSiege;
} }
public long getTimeToSpawnSiege() {
return timeToSpawnSiege;
}
public void setTimeToSpawnSiege(long timeToSpawnSiege) { public void setTimeToSpawnSiege(long timeToSpawnSiege) {
this.timeToSpawnSiege = timeToSpawnSiege; this.timeToSpawnSiege = timeToSpawnSiege;
} }

17
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");
}
}

2
src/engine/server/world/WorldServer.java

@ -349,7 +349,7 @@ public class WorldServer {
DbManager.MobBaseQueries.GET_ALL_MOBBASES(); DbManager.MobBaseQueries.GET_ALL_MOBBASES();
Logger.info("Loading Mob Powers for MobBases"); Logger.info("Loading Mob Powers for MobBases");
PowersManager.LoadAllMobPowers(); PowersManager.AllMobPowers = DbManager.PowerQueries.LOAD_MOB_POWERS();
Logger.info("Loading item enchants"); Logger.info("Loading item enchants");
DbManager.LootQueries.LOAD_ENCHANT_VALUES(); DbManager.LootQueries.LOAD_ENCHANT_VALUES();

Loading…
Cancel
Save