Public Repository for the Magicbane Shadowbane Emulator
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

467 lines
18 KiB

// • ▌ ▄ ·. ▄▄▄· ▄▄ • ▪ ▄▄· ▄▄▄▄· ▄▄▄· ▐▄▄▄ ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀ █▪▀▀▀ ▀ ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀ ▀ ▀ ▀▀ █▪ ▀▀▀
// 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.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<Integer, Resists> mobResists = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
private ConcurrentHashMap<Enum.DamageType, Float> resists = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
private ConcurrentHashMap<Enum.DamageType, Boolean> immuneTo = new ConcurrentHashMap<>(MBServerStatics.CHM_INIT_CAP, MBServerStatics.CHM_LOAD, MBServerStatics.CHM_THREAD_LOW);
private Enum.DamageType 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 (Enum.DamageType dt : r.resists.keySet())
this.resists.put(dt, r.resists.get(dt));
for (Enum.DamageType 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(Enum.DamageType.Slash, rs.getFloat("slash"));
this.resists.put(Enum.DamageType.Crush, rs.getFloat("crush"));
this.resists.put(Enum.DamageType.Pierce, rs.getFloat("pierce"));
this.resists.put(Enum.DamageType.Magic, rs.getFloat("magic"));
this.resists.put(Enum.DamageType.Bleed, rs.getFloat("bleed"));
this.resists.put(Enum.DamageType.Poison, rs.getFloat("poison"));
this.resists.put(Enum.DamageType.Mental, rs.getFloat("mental"));
this.resists.put(Enum.DamageType.Holy, rs.getFloat("holy"));
this.resists.put(Enum.DamageType.Unholy, rs.getFloat("unholy"));
this.resists.put(Enum.DamageType.Lightning, rs.getFloat("lightning"));
this.resists.put(Enum.DamageType.Fire, rs.getFloat("fire"));
this.resists.put(Enum.DamageType.Cold, rs.getFloat("cold"));
this.resists.put(Enum.DamageType.Healing, 0f);
}
//Handle Fortitudes
private static float handleFortitude(AbstractCharacter target, Enum.DamageType 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 == Enum.DamageType.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<SourceType> 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<SourceType> forts, Enum.DamageType damageType, boolean exclusive) {
for (SourceType fort : forts) {
Enum.DamageType dt = Enum.DamageType.valueOf(fort.name());
if (dt.equals(Enum.DamageType.None))
continue;
if (dt.equals(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;
if (armor.template.item_type.equals(Enum.ItemType.ARMOR)) {
phys[0] += armor.template.combat_attack_resist.get("SLASHING");
phys[1] += armor.template.combat_attack_resist.get("CRUSHING");
phys[2] += armor.template.combat_attack_resist.get("PIERCING");
}
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(Enum.DamageType.Slash, 85f);
this.resists.put(Enum.DamageType.Crush, 85f);
this.resists.put(Enum.DamageType.Siege, 0f);
this.immuneTo.put(Enum.DamageType.Pierce, true);
this.immuneTo.put(Enum.DamageType.Magic, true);
this.immuneTo.put(Enum.DamageType.Bleed, true);
this.immuneTo.put(Enum.DamageType.Poison, true);
this.immuneTo.put(Enum.DamageType.Mental, true);
this.immuneTo.put(Enum.DamageType.Holy, true);
this.immuneTo.put(Enum.DamageType.Unholy, true);
this.immuneTo.put(Enum.DamageType.Lightning, true);
this.immuneTo.put(Enum.DamageType.Fire, true);
this.immuneTo.put(Enum.DamageType.Cold, true);
}
/**
* Create generic resists for mines
*/
public final void setMineResists() {
this.immuneToAll = false;
this.immuneTo.put(Enum.DamageType.Slash, true);
this.immuneTo.put(Enum.DamageType.Crush, true);
this.immuneTo.put(Enum.DamageType.Pierce, true);
this.immuneTo.put(Enum.DamageType.Magic, true);
this.immuneTo.put(Enum.DamageType.Bleed, true);
this.immuneTo.put(Enum.DamageType.Poison, true);
this.immuneTo.put(Enum.DamageType.Mental, true);
this.immuneTo.put(Enum.DamageType.Holy, true);
this.immuneTo.put(Enum.DamageType.Unholy, true);
this.immuneTo.put(Enum.DamageType.Lightning, true);
this.immuneTo.put(Enum.DamageType.Fire, true);
this.immuneTo.put(Enum.DamageType.Cold, true);
this.resists.put(Enum.DamageType.Siege, 0f);
}
/**
* Create generic resists
*/
public final void setGenericResists() {
this.immuneToAll = false;
this.resists.put(Enum.DamageType.Slash, 0f);
this.resists.put(Enum.DamageType.Crush, 0f);
this.resists.put(Enum.DamageType.Pierce, 0f);
this.resists.put(Enum.DamageType.Magic, 0f);
this.resists.put(Enum.DamageType.Bleed, 0f);
this.resists.put(Enum.DamageType.Poison, 0f);
this.resists.put(Enum.DamageType.Mental, 0f);
this.resists.put(Enum.DamageType.Holy, 0f);
this.resists.put(Enum.DamageType.Unholy, 0f);
this.resists.put(Enum.DamageType.Lightning, 0f);
this.resists.put(Enum.DamageType.Fire, 0f);
this.resists.put(Enum.DamageType.Cold, 0f);
this.resists.put(Enum.DamageType.Healing, 0f);
this.immuneTo.put(Enum.DamageType.Siege, true);
}
/**
* Get a resist
*/
public float getResist(Enum.DamageType 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(Enum.DamageType 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(Enum.DamageType.Attack);
}
/**
* Set a resist
*/
public void setResist(Enum.DamageType type, float value) {
this.resists.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, Enum.DamageType 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)
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(Enum.DamageType.Stun, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Blind))
this.immuneTo.put(Enum.DamageType.Blind, true);
if (rb.getBool(ModType.ImmuneToAttack, SourceType.None))
this.immuneTo.put(Enum.DamageType.Attack, true);
if (rb.getBool(ModType.ImmuneToPowers, SourceType.None))
this.immuneTo.put(Enum.DamageType.Powers, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Powerblock))
this.immuneTo.put(Enum.DamageType.Powerblock, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.DeBuff))
this.immuneTo.put(Enum.DamageType.DeBuff, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Fear))
this.immuneTo.put(Enum.DamageType.Fear, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Charm))
this.immuneTo.put(Enum.DamageType.Charm, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Root))
this.immuneTo.put(Enum.DamageType.Root, true);
if (rb.getBool(ModType.ImmuneTo, SourceType.Snare))
this.immuneTo.put(Enum.DamageType.Snare, true);
// Handle resists
slash += rb.getFloat(ModType.Resistance, SourceType.Slash);
crush += rb.getFloat(ModType.Resistance, SourceType.Crush);
pierce += rb.getFloat(ModType.Resistance, SourceType.Pierce);
magic += rb.getFloat(ModType.Resistance, SourceType.Magic);
bleed += rb.getFloat(ModType.Resistance, SourceType.Bleed);
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.charItemManager != null && ac.charItemManager.getEquipped() != null) {
float[] phys = {0f, 0f, 0f};
ConcurrentHashMap<Enum.EquipSlotType, Item> equip = ac.charItemManager.getEquipped();
// get base physical resists
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.HELM), phys);
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.CHEST), phys);
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.UPARM), phys);
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.HANDS), phys);
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.LEGS), phys);
phys = Resists.getArmorResists(equip.get(Enum.EquipSlotType.FEET), phys);
slash += phys[0];
crush += phys[1];
pierce += phys[2];
}
}
this.resists.put(Enum.DamageType.Slash, slash);
this.resists.put(Enum.DamageType.Crush, crush);
this.resists.put(Enum.DamageType.Pierce, pierce);
this.resists.put(Enum.DamageType.Magic, magic);
this.resists.put(Enum.DamageType.Bleed, bleed);
this.resists.put(Enum.DamageType.Poison, poison);
this.resists.put(Enum.DamageType.Mental, mental);
this.resists.put(Enum.DamageType.Holy, holy);
this.resists.put(Enum.DamageType.Unholy, unholy);
this.resists.put(Enum.DamageType.Lightning, lightning);
this.resists.put(Enum.DamageType.Fire, fire);
this.resists.put(Enum.DamageType.Cold, cold);
this.resists.put(Enum.DamageType.Healing, healing);
this.immuneTo.put(Enum.DamageType.Siege, true);
// debug printing of resists
// printResists(pc);
}
public void printResistsToClient(PlayerCharacter pc) {
for (Enum.DamageType dt : resists.keySet())
ChatManager.chatSystemInfo(pc, " resist." + dt.name() + ": " + resists.get(dt));
for (Enum.DamageType 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<Enum.DamageType> it = this.resists.keySet().iterator();
while (it.hasNext()) {
Enum.DamageType damType = it.next();
String dtName = damType.name();
out += dtName + '=' + this.resists.get(dtName) + ", ";
}
out += "ImmuneTo: ";
it = this.immuneTo.keySet().iterator();
while (it.hasNext()) {
Enum.DamageType 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;
}
}