// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ . // ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌· // ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀ // ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌ // ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀ // Magicbane Emulator Project © 2013 - 2022 // www.magicbane.com package engine.objects; import engine.Enum; import engine.Enum.ModType; import engine.Enum.SourceType; import engine.gameManager.ChatManager; import engine.gameManager.DbManager; import engine.powers.EffectsBase; import engine.server.MBServerStatics; import org.pmw.tinylog.Logger; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashSet; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; public class Resists { private static ConcurrentHashMap mobResists = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); private ConcurrentHashMap resists = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); private ConcurrentHashMap immuneTo = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW); private SourceType protection; private int protectionTrains = 0; private boolean immuneToAll; /** * Generic Constructor */ public Resists(String type) { switch (type) { case "Building": setBuildingResists(); break; case "Mine": setMineResists(); break; default: setGenericResists(); break; } } public Resists(Resists r) { for (SourceType dt : r.resists.keySet()) this.resists.put(dt, r.resists.get(dt)); for (SourceType dt : r.immuneTo.keySet()) this.immuneTo.put(dt, r.immuneTo.get(dt)); this.protection = r.protection; this.protectionTrains = r.protectionTrains; this.immuneToAll = r.immuneToAll; } /** * Generic Constructor for player */ public Resists(PlayerCharacter pc) { setGenericResists(); } public Resists(Mob mob) { setGenericResists(); } /** * Called for mobBase when getting from the db fails */ public Resists(MobBase mobBase) { setGenericResists(); } /** * Database Constructor */ public Resists(ResultSet rs) throws SQLException { this.immuneToAll = false; this.resists.put(SourceType.Slashing, rs.getFloat("slash")); this.resists.put(SourceType.Crushing, rs.getFloat("crush")); this.resists.put(SourceType.Piercing, rs.getFloat("pierce")); this.resists.put(SourceType.Magic, rs.getFloat("magic")); this.resists.put(SourceType.Bleeding, rs.getFloat("bleed")); this.resists.put(SourceType.Poison, rs.getFloat("poison")); this.resists.put(SourceType.Mental, rs.getFloat("mental")); this.resists.put(SourceType.Holy, rs.getFloat("holy")); this.resists.put(SourceType.Unholy, rs.getFloat("unholy")); this.resists.put(SourceType.Lightning, rs.getFloat("lightning")); this.resists.put(SourceType.Fire, rs.getFloat("fire")); this.resists.put(SourceType.Cold, rs.getFloat("cold")); this.resists.put(SourceType.Healing, 0f); } //Handle Fortitudes private static float handleFortitude(AbstractCharacter target, SourceType type, float damage) { if (target == null || !(target.getObjectType().equals(Enum.GameObjectType.PlayerCharacter))) return damage; PlayerBonuses bonus = target.getBonuses(); //see if there is a fortitude float damageCap = bonus.getFloatPercentAll(ModType.DamageCap, SourceType.None); if (damageCap == 0f || type == SourceType.Healing) return damage; //is fortitude, Are we under the cap? float maxHealth = target.getHealthMax(); float capFire = maxHealth * (damageCap); if (damage < capFire) return damage; //let's see if valid damagetype to apply it boolean exclusive; HashSet forts = bonus.getList(ModType.IgnoreDamageCap); if (forts == null) { exclusive = true; forts = bonus.getList(ModType.ExclusiveDamageCap); } else exclusive = false; if (forts == null || !isValidDamageCapType(forts, type, exclusive)) return damage; float adjustedDamage = bonus.getFloatPercentAll(ModType.AdjustAboveDmgCap, SourceType.None); //Adjust damage down and return new amount float aadc = 1 + adjustedDamage; return capFire * aadc; } //Test if Damagetype is valid for foritude private static boolean isValidDamageCapType(HashSet forts, SourceType damageType, boolean exclusive) { for (SourceType fort : forts) { SourceType dt = SourceType.valueOf(fort.name()); if (dt == SourceType.None) continue; if (dt == damageType) { return exclusive; } } return !exclusive; } /** * Calculate Current Resists for Player */ public static void calculateResists(AbstractCharacter ac) { if (ac.getResists() != null) ac.getResists().calculateResists(ac, true); else Logger.error("Unable to find resists for character " + ac.getObjectUUID()); } private static float[] getArmorResists(Item armor, float[] phys) { if (armor == null) return phys; ItemBase ab = armor.getItemBase(); if (ab == null) return phys; phys[0] += ab.getSlashResist(); phys[1] += ab.getCrushResist(); phys[2] += ab.getPierceResist(); return phys; } /** * Get mob resists from db if there, otherwise set defaults */ public static Resists getResists(int resistID) { //check cache first if (mobResists.containsKey(resistID)) return new Resists(mobResists.get(resistID)); //get from database Resists resists = DbManager.ResistQueries.GET_RESISTS_FOR_MOB(resistID); if (resists != null) { mobResists.put(resistID, resists); return new Resists(resists); } //failed, may want to debug this return null; } /** * Create generic resists for buildings */ public final void setBuildingResists() { this.immuneToAll = false; this.resists.put(SourceType.Slashing, 85f); this.resists.put(SourceType.Crushing, 85f); this.resists.put(SourceType.Siege, 0f); this.immuneTo.put(SourceType.Piercing, true); this.immuneTo.put(SourceType.Magic, true); this.immuneTo.put(SourceType.Bleeding, true); this.immuneTo.put(SourceType.Poison, true); this.immuneTo.put(SourceType.Mental, true); this.immuneTo.put(SourceType.Holy, true); this.immuneTo.put(SourceType.Unholy, true); this.immuneTo.put(SourceType.Lightning, true); this.immuneTo.put(SourceType.Fire, true); this.immuneTo.put(SourceType.Cold, true); } /** * Create generic resists for mines */ public final void setMineResists() { this.immuneToAll = false; this.immuneTo.put(SourceType.Slashing, true); this.immuneTo.put(SourceType.Crushing, true); this.immuneTo.put(SourceType.Piercing, true); this.immuneTo.put(SourceType.Magic, true); this.immuneTo.put(SourceType.Bleeding, true); this.immuneTo.put(SourceType.Poison, true); this.immuneTo.put(SourceType.Mental, true); this.immuneTo.put(SourceType.Holy, true); this.immuneTo.put(SourceType.Unholy, true); this.immuneTo.put(SourceType.Lightning, true); this.immuneTo.put(SourceType.Fire, true); this.immuneTo.put(SourceType.Cold, true); this.resists.put(SourceType.Siege, 0f); } /** * Create generic resists */ public final void setGenericResists() { this.immuneToAll = false; this.resists.put(SourceType.Slashing, 0f); this.resists.put(SourceType.Crushing, 0f); this.resists.put(SourceType.Piercing, 0f); this.resists.put(SourceType.Magic, 0f); this.resists.put(SourceType.Bleeding, 0f); this.resists.put(SourceType.Poison, 0f); this.resists.put(SourceType.Mental, 0f); this.resists.put(SourceType.Holy, 0f); this.resists.put(SourceType.Unholy, 0f); this.resists.put(SourceType.Lightning, 0f); this.resists.put(SourceType.Fire, 0f); this.resists.put(SourceType.Cold, 0f); this.resists.put(SourceType.Healing, 0f); this.immuneTo.put(SourceType.Siege, true); } /** * Get a resist */ public float getResist(SourceType type, int trains) { //get resisted amount Float amount = 0f; if (this.resists.containsKey(type)) amount = this.resists.get(type); //add protection if (trains > 0 && protection != null && type.equals(this.protection)) { float prot = 50 + this.protectionTrains - trains; amount += (prot >= 0) ? prot : 0; } if (amount == null) return 0f; if (amount > 75f) return 75f; return amount; } /** * get immuneTo */ public boolean immuneTo(SourceType type) { if (this.immuneTo.containsKey(type)) return this.immuneTo.get(type); else return false; } /** * get immuneToAll */ public boolean immuneToAll() { return this.immuneToAll; } public boolean immuneToAttacks() { return immuneTo(SourceType.ImmuneToAttack); } /** * Set a resist */ public void setResist(SourceType type, float value) { this.resists.put(type, value); } /** * set immunities from mobbase */ public void setImmuneTo(int immune) { setImmuneTo(SourceType.Stun, ((immune & 1) != 0)); setImmuneTo(SourceType.Powerblock, ((immune & 2) != 0)); setImmuneTo(SourceType.Drain, ((immune & 4) != 0)); setImmuneTo(SourceType.Snare, ((immune & 8) != 0)); setImmuneTo(SourceType.Siege, ((immune & 16) != 0)); setImmuneTo(SourceType.Slashing, ((immune & 32) != 0)); setImmuneTo(SourceType.Crushing, ((immune & 64) != 0)); setImmuneTo(SourceType.Piercing, ((immune & 128) != 0)); setImmuneTo(SourceType.Magic, ((immune & 256) != 0)); setImmuneTo(SourceType.Bleeding, ((immune & 512) != 0)); setImmuneTo(SourceType.Poison, ((immune & 1024) != 0)); setImmuneTo(SourceType.Mental, ((immune & 2048) != 0)); setImmuneTo(SourceType.Holy, ((immune & 4096) != 0)); setImmuneTo(SourceType.Unholy, ((immune & 8192) != 0)); setImmuneTo(SourceType.Lightning, ((immune & 16384) != 0)); setImmuneTo(SourceType.Fire, ((immune & 32768) != 0)); setImmuneTo(SourceType.Cold, ((immune & 65536) != 0)); setImmuneTo(SourceType.Steal, ((immune & 131072) != 0)); } /** * set/unset immuneTo */ public void setImmuneTo(SourceType type, boolean value) { this.immuneTo.put(type, value); } /** * set immuneToAll */ public void setImmuneToAll(boolean value) { this.immuneToAll = value; } /** * set resists from mobbase */ public void setMobResists(int resistID) { //TODO add this in later //calls `static_npc_mob_resists` table WHERE `ID`='resistID' } /** * get Damage after resist * Expects heals as negative damage and damage as positive damage for fortitudes. */ public float getResistedDamage(AbstractCharacter source, AbstractCharacter target, SourceType type, float damage, int trains) { //handle fortitudes damage = handleFortitude(target, type, damage); //calculate armor piercing float ap = source.getBonuses().getFloatPercentAll(ModType.ArmorPiercing, SourceType.None); float damageAfterResists = damage * (1 - (this.getResist(type, trains) * 0.01f) + ap); //check to see if any damage absorbers should cancel if (target != null) { //debug damage shields if any found if (source.getDebug(2) && source.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) { Effect da = target.getDamageAbsorber(); if (da != null && da.getEffectsBase() != null) { EffectsBase eb = da.getEffectsBase(); String text = "Damage: " + damage + '\n'; text += "Damage after resists: " + damageAfterResists + '\n'; text += "Attack damage type: " + type.name() + '\n'; text += "Fortitude damage types; " + eb.getDamageTypes() + '\n'; text += "Fortitude damage before attack: " + da.getDamageAmount() + '\n'; text += "Fortitude total health: " + eb.getDamageAmount(da.getTrains()) + '\n'; text += "Fortitude trains: " + da.getTrains(); ChatManager.chatSystemInfo((PlayerCharacter) source, text); } } target.cancelOnTakeDamage(type, (damageAfterResists)); } return damageAfterResists; } public void calculateResists(AbstractCharacter ac, boolean val) { this.immuneTo.clear(); // get resists for runes PlayerBonuses rb = ac.getBonuses(); float slash = 0f, crush = 0f, pierce = 0f, magic = 0f, bleed = 0f, mental = 0f, holy = 0f, unholy = 0f, poison = 0f, lightning = 0f, fire = 0f, cold = 0f, healing = 0f; if (rb != null) { // Handle immunities if (rb.getBool(ModType.ImmuneTo, SourceType.Stun)) this.immuneTo.put(SourceType.Stun, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Blind)) this.immuneTo.put(SourceType.Blind, true); if (rb.getBool(ModType.ImmuneToAttack, SourceType.None)) this.immuneTo.put(SourceType.ImmuneToAttack, true); if (rb.getBool(ModType.ImmuneToPowers, SourceType.None)) this.immuneTo.put(SourceType.ImmuneToPowers, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Powerblock)) this.immuneTo.put(SourceType.Powerblock, true); if (rb.getBool(ModType.ImmuneTo, SourceType.DeBuff)) this.immuneTo.put(SourceType.DeBuff, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Fear)) this.immuneTo.put(SourceType.Fear, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Charm)) this.immuneTo.put(SourceType.Charm, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Root)) this.immuneTo.put(SourceType.Root, true); if (rb.getBool(ModType.ImmuneTo, SourceType.Snare)) this.immuneTo.put(SourceType.Snare, true); // Handle resists slash += rb.getFloat(ModType.Resistance, SourceType.Slashing); crush += rb.getFloat(ModType.Resistance, SourceType.Crushing); pierce += rb.getFloat(ModType.Resistance, SourceType.Piercing); magic += rb.getFloat(ModType.Resistance, SourceType.Magic); bleed += rb.getFloat(ModType.Resistance, SourceType.Bleeding); poison += rb.getFloat(ModType.Resistance, SourceType.Poison); mental += rb.getFloat(ModType.Resistance, SourceType.Mental); holy += rb.getFloat(ModType.Resistance, SourceType.Holy); unholy += rb.getFloat(ModType.Resistance, SourceType.Unholy); lightning += rb.getFloat(ModType.Resistance, SourceType.Lightning); fire += rb.getFloat(ModType.Resistance, SourceType.Fire); cold += rb.getFloat(ModType.Resistance, SourceType.Cold); healing += rb.getFloat(ModType.Resistance, SourceType.Healing); // DamageType.Healing.name()); } // get resists from equipment if (ac.getObjectType().equals(Enum.GameObjectType.PlayerCharacter)) { if (ac.getCharItemManager() != null && ac.getCharItemManager().getEquipped() != null) { float[] phys = {0f, 0f, 0f}; ConcurrentHashMap equip = ac.getCharItemManager().getEquipped(); // get base physical resists phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_HELMET), phys); phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_CHEST), phys); phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_ARMS), phys); phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_GLOVES), phys); phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_LEGGINGS), phys); phys = Resists.getArmorResists(equip.get(MBServerStatics.SLOT_FEET), phys); slash += phys[0]; crush += phys[1]; pierce += phys[2]; } } this.resists.put(SourceType.Slashing, slash); this.resists.put(SourceType.Crushing, crush); this.resists.put(SourceType.Piercing, pierce); this.resists.put(SourceType.Magic, magic); this.resists.put(SourceType.Bleeding, bleed); this.resists.put(SourceType.Poison, poison); this.resists.put(SourceType.Mental, mental); this.resists.put(SourceType.Holy, holy); this.resists.put(SourceType.Unholy, unholy); this.resists.put(SourceType.Lightning, lightning); this.resists.put(SourceType.Fire, fire); this.resists.put(SourceType.Cold, cold); this.resists.put(SourceType.Healing, healing); this.immuneTo.put(SourceType.Siege, true); // debug printing of resists // printResists(pc); } public void printResistsToClient(PlayerCharacter pc) { for (SourceType dt : resists.keySet()) ChatManager.chatSystemInfo(pc, " resist." + dt.name() + ": " + resists.get(dt)); for (SourceType dt : immuneTo.keySet()) ChatManager.chatSystemInfo(pc, " immuneTo." + dt.name() + ": " + immuneTo.get(dt)); ChatManager.chatSystemInfo(pc, " immuneToAll: " + this.immuneToAll); if (protection != null) ChatManager.chatSystemInfo(pc, " Protection: " + protection.name() + ", Trains: " + protectionTrains); else ChatManager.chatSystemInfo(pc, " Protection: None"); } public String getResists(PlayerCharacter pc) { String out = pc.getName(); out += "Resists: "; Iterator it = this.resists.keySet().iterator(); while (it.hasNext()) { SourceType damType = it.next(); String dtName = damType.name(); out += dtName + '=' + this.resists.get(dtName) + ", "; } out += "ImmuneTo: "; it = this.immuneTo.keySet().iterator(); while (it.hasNext()) { SourceType damType = it.next(); String dtName = damType.name(); out += dtName + '=' + this.resists.get(dtName) + ", "; } if (protection != null) out += "Protection: " + protection.name() + ", Trains: " + protectionTrains; else out += "Protection: none"; return out; } }