// • ▌ ▄ ·.  ▄▄▄·  ▄▄ • ▪   ▄▄· ▄▄▄▄·  ▄▄▄·  ▐▄▄▄  ▄▄▄ .
// ·██ ▐███▪▐█ ▀█ ▐█ ▀ ▪██ ▐█ ▌▪▐█ ▀█▪▐█ ▀█ •█▌ ▐█▐▌·
// ▐█ ▌▐▌▐█·▄█▀▀█ ▄█ ▀█▄▐█·██ ▄▄▐█▀▀█▄▄█▀▀█ ▐█▐ ▐▌▐▀▀▀
// ██ ██▌▐█▌▐█ ▪▐▌▐█▄▪▐█▐█▌▐███▌██▄▪▐█▐█ ▪▐▌██▐ █▌▐█▄▄▌
// ▀▀  █▪▀▀▀ ▀  ▀ ·▀▀▀▀ ▀▀▀·▀▀▀ ·▀▀▀▀  ▀  ▀ ▀▀  █▪ ▀▀▀
//      Magicbane Emulator Project © 2013 - 2022
//                www.magicbane.com

package engine.gameManager;

import engine.job.JobContainer;
import engine.job.JobScheduler;
import engine.jobs.AttackJob;
import engine.jobs.DeferredPowerJob;
import engine.mbEnums;
import engine.net.client.ClientConnection;
import engine.net.client.msg.TargetedActionMsg;
import engine.net.client.msg.UpdateStateMsg;
import engine.objects.*;
import engine.powers.DamageShield;
import engine.powers.effectmodifiers.AbstractEffectModifier;
import engine.powers.effectmodifiers.WeaponProcEffectModifier;
import engine.server.MBServerStatics;
import org.pmw.tinylog.Logger;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadLocalRandom;

import static java.lang.Math.pow;

public enum CombatManager {


    // MB Dev notes:
    // Class implements all combat mechanics for Magicbane.
    //
    // Combat initiates in "combatCycle" to determine which hands will be swung, and attack is processed based on that.
    //  Handles all combat for AbstractCharacter including player characters and mobiles.
    // Controls toggling of combat and sit/stand for all AbstractCharacters
    //
    //

    COMBAT_MANAGER;
    public static final int COMBAT_BLOCK_ANIMATION = 298;
    public static final int COMBAT_PARRY_ANIMATION = 299;
    public static final int COMBAT_DODGE_ANIMATION = 300;

    public static void combatCycle(AbstractCharacter attacker, AbstractWorldObject target) {

        //early exit checks

        if (attacker == null || target == null || !attacker.isAlive() || !target.isAlive())
            return;

        if (attacker.getObjectType().equals(mbEnums.GameObjectType.Mob))
            if (((Mob) attacker).nextAttackTime > System.currentTimeMillis())
                return;

        switch (target.getObjectType()) {
            case Building:
                if (!((Building) target).isVulnerable())
                    return;
                break;
            case PlayerCharacter:
            case Mob:
                PlayerBonuses bonuses = ((AbstractCharacter) target).getBonuses();
                if (bonuses != null && bonuses.getBool(mbEnums.ModType.ImmuneToAttack, mbEnums.SourceType.None))
                    return;
                break;
            case NPC:
                return;
        }

        Item mainWeapon = attacker.charItemManager.getEquipped().get(mbEnums.EquipSlotType.RHELD);
        Item offWeapon = attacker.charItemManager.getEquipped().get(mbEnums.EquipSlotType.LHELD);

        if (mainWeapon == null && offWeapon == null) {
            //no weapons equipped, punch with both fists
            processAttack(attacker, target, mbEnums.EquipSlotType.RHELD);
            if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
                processAttack(attacker, target, mbEnums.EquipSlotType.LHELD);
            return;
        }

        if (mainWeapon != null && offWeapon == null) {
            //swing right hand only
            processAttack(attacker, target, mbEnums.EquipSlotType.RHELD);
            return;
        }

        if (mainWeapon == null && offWeapon != null && !offWeapon.template.item_skill_used.equals("Block")) {
            //swing left hand only
            processAttack(attacker, target, mbEnums.EquipSlotType.LHELD);
            return;
        }

        if (mainWeapon == null && offWeapon != null && offWeapon.template.item_skill_used.equals("Block")) {
            //no weapon equipped with a shield, punch with one hand
            processAttack(attacker, target, mbEnums.EquipSlotType.RHELD);
            return;
        }

        if (mainWeapon != null && offWeapon != null && offWeapon.template.item_skill_used.equals("Block")) {
            //one weapon equipped with a shield, swing with one hand
            processAttack(attacker, target, mbEnums.EquipSlotType.RHELD);
            return;
        }

        if (mainWeapon != null && offWeapon != null && !offWeapon.template.item_skill_used.equals("Block")) {
            //two weapons equipped, swing both hands
            processAttack(attacker, target, mbEnums.EquipSlotType.RHELD);
            if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
                processAttack(attacker, target, mbEnums.EquipSlotType.LHELD);
        }
    }

    public static void processAttack(AbstractCharacter attacker, AbstractWorldObject target, mbEnums.EquipSlotType slot) {

        if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
            if (!attacker.isCombat())
                return;

            //check if this slot is on attack timer, if timer has passed clear it, else early exit
            if (attacker.getTimers() != null && attacker.getTimers().containsKey("Attack" + slot.name()))
                if (attacker.getTimers().get("Attack" + slot.name()).timeToExecutionLeft() <= 0)
                    attacker.getTimers().remove("Attack" + slot.name());
                else
                    return;
        }


        // check if character is in range to attack target

        PlayerBonuses bonus = attacker.getBonuses();

        float rangeMod = 1.0f;
        float attackRange = MBServerStatics.NO_WEAPON_RANGE;

        Item weapon = attacker.charItemManager.getEquipped(slot);

        if (weapon != null) {
            if (bonus != null)
                rangeMod += bonus.getFloatPercentAll(mbEnums.ModType.WeaponRange, mbEnums.SourceType.None);

            attackRange += weapon.template.item_weapon_max_range * rangeMod;
        }

        if (attacker.getObjectType().equals(mbEnums.GameObjectType.Mob))
            if (((Mob) attacker).isSiege())
                attackRange = 300;

        float distanceSquared = attacker.loc.distanceSquared(target.loc);

        boolean inRange = false;
        if (AbstractCharacter.IsAbstractCharacter(target)) {
            attackRange += ((AbstractCharacter) target).calcHitBox();
        } else {

        }

        if (attackRange > 15 && attacker.isMoving()) {
            //cannot shoot bow while moving;
            return;
        }

        switch (target.getObjectType()) {
            case PlayerCharacter:
                attackRange += ((PlayerCharacter) target).getCharacterHeight() * 0.5f;
                if (distanceSquared <= attackRange * attackRange)
                    inRange = true;
                break;
            case Mob:
                attackRange += ((AbstractCharacter) target).calcHitBox();
                if (distanceSquared <= attackRange * attackRange)
                    inRange = true;
                break;
            case Building:
                if (attackRange > 15) {
                    float rangeSquared = (attackRange + target.getBounds().getHalfExtents().x) * (attackRange + target.getBounds().getHalfExtents().x);
                    //float distanceSquared = attacker.loc.distanceSquared(target.loc);
                    if (distanceSquared < rangeSquared) {
                        inRange = true;
                        break;
                    }
                } else {
                    float locX = target.loc.x - target.getBounds().getHalfExtents().x;
                    float locZ = target.loc.z - target.getBounds().getHalfExtents().y;
                    float sizeX = (target.getBounds().getHalfExtents().x + attackRange) * 2;
                    float sizeZ = (target.getBounds().getHalfExtents().y + attackRange) * 2;
                    Rectangle2D.Float rect = new Rectangle2D.Float(locX, locZ, sizeX, sizeZ);
                    if (rect.contains(new Point2D.Float(attacker.loc.x, attacker.loc.z)))
                        inRange = true;
                    break;
                }
        }

        //get delay for the auto attack job
        long delay = 5000;

        if (weapon != null) {

            float wepSpeed = (int) (weapon.template.item_weapon_wepspeed);

            if (weapon.getBonusPercent(mbEnums.ModType.WeaponSpeed, mbEnums.SourceType.None) != 0f) //add weapon speed bonus
                wepSpeed *= (1 + weapon.getBonus(mbEnums.ModType.WeaponSpeed, mbEnums.SourceType.None));

            if (attacker.getBonuses() != null && attacker.getBonuses().getFloatPercentAll(mbEnums.ModType.AttackDelay, mbEnums.SourceType.None) != 0f) //add effects speed bonus
                wepSpeed *= (1 + attacker.getBonuses().getFloatPercentAll(mbEnums.ModType.AttackDelay, mbEnums.SourceType.None));

            if (wepSpeed < 10)
                wepSpeed = 10; //Old was 10, but it can be reached lower with legit buffs,effects.

            delay = (long)wepSpeed * 100L;
        }

        if (attacker.getObjectType().equals(mbEnums.GameObjectType.Mob))
            ((Mob) attacker).nextAttackTime = System.currentTimeMillis() + delay;

        if (inRange) {

            if(attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)){
                if(!attacker.getTimestamps().contains(slot.name()+"Attack")){
                    attacker.getTimestamps().put(slot.name()+"Attack", System.currentTimeMillis() - 1000);
                } else if(System.currentTimeMillis() < attacker.getTimestamps().get(slot.name()+"Attack") + delay){
                    setAutoAttackJob(attacker,slot,delay);
                    return;
                }
            }


            //handle retaliate
            if (AbstractCharacter.IsAbstractCharacter(target)) {
                if (((AbstractCharacter) target).combatTarget == null || !((AbstractCharacter) target).combatTarget.isAlive()) {
                    ((AbstractCharacter) target).combatTarget = attacker;
                    if (target.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter) && ((AbstractCharacter) target).isCombat())
                        combatCycle((AbstractCharacter) target, attacker);
                }
            }

            // take stamina away from attacker if its not a mob
            if (weapon != null && !attacker.getObjectType().equals(mbEnums.GameObjectType.Mob)) {
                //check if Out of Stamina
                if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
                    if (attacker.getStamina() < (weapon.template.item_wt / 3f)) {
                        //set auto attack job
                        setAutoAttackJob(attacker, slot, delay);
                        return;
                    }
                }
                float stam = weapon.template.item_wt / 3f;
                stam = (stam < 1) ? 1 : stam;
                attacker.modifyStamina(-(stam), attacker, true);
            } else
                attacker.modifyStamina(1, attacker, true);

            //cancel things that are cancelled by an attack

            attacker.cancelOnAttackSwing();

            //declare relevant variables

            int min = attacker.minDamageHandOne;
            int max = attacker.maxDamageHandOne;
            int atr = attacker.atrHandOne;

            //get the proper stats based on which slot is attacking

            if (slot == mbEnums.EquipSlotType.LHELD) {
                min = attacker.minDamageHandTwo;
                max = attacker.maxDamageHandTwo;
                atr = attacker.atrHandTwo;
            }

            //apply weapon powers before early exit for miss or passives
            DeferredPowerJob dpj = null;

            if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {

                dpj = ((PlayerCharacter) attacker).getWeaponPower();

                if (dpj != null) {
                    dpj.attack(target, attackRange);

                    if (dpj.getPower() != null && (dpj.getPowerToken() == -1851459567 || dpj.getPowerToken() == -1851489518))
                        ((PlayerCharacter) attacker).setWeaponPower(dpj);
                }
            }


            int def = 0;

            if (AbstractCharacter.IsAbstractCharacter(target))
                def = ((AbstractCharacter) target).defenseRating;

            //calculate hit chance based off ATR and DEF

            int hitChance;
            if (def == 0)
                def = 1;
            float dif = atr * 1f / def;

            if (dif <= 0.8f)
                hitChance = 4;
            else
                hitChance = ((int) (450 * (dif - 0.8f)) + 4);

            if (target.getObjectType() == mbEnums.GameObjectType.Building)
                hitChance = 100;
            int passiveAnim = getPassiveAnimation(mbEnums.PassiveType.None); // checking for a miss due to ATR vs Def
            if (ThreadLocalRandom.current().nextInt(100) > hitChance) {
                TargetedActionMsg msg = new TargetedActionMsg(attacker, target, 0f, passiveAnim);

                if (target.getObjectType() == mbEnums.GameObjectType.PlayerCharacter)
                    DispatchManager.dispatchMsgToInterestArea(target, msg, mbEnums.DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true, false);
                else
                    DispatchManager.sendToAllInRange(attacker, msg);

                //we need to send the animation even if the attacker misses
                TargetedActionMsg cmm = new TargetedActionMsg(attacker, target, (float) 0, getSwingAnimation(weapon.template, null, slot));
                DispatchManager.sendToAllInRange(target, cmm);

                //set auto attack job
                setAutoAttackJob(attacker, slot, delay);
                return;
            }

            //calculate passive chances only if target is AbstractCharacter

            if (EnumSet.of(mbEnums.GameObjectType.PlayerCharacter, mbEnums.GameObjectType.NPC, mbEnums.GameObjectType.Mob).contains(target.getObjectType())) {
                mbEnums.PassiveType passiveType = mbEnums.PassiveType.None;
                int hitRoll = ThreadLocalRandom.current().nextInt(100);

                float dodgeChance = ((AbstractCharacter) target).getPassiveChance("Dodge", attacker.getLevel(), true);
                float blockChance = ((AbstractCharacter) target).getPassiveChance("Block", attacker.getLevel(), true);
                float parryChance = ((AbstractCharacter) target).getPassiveChance("Parry", attacker.getLevel(), true);

                // Passive chance clamped at 75

                dodgeChance = Math.max(0, Math.min(75, dodgeChance));
                blockChance = Math.max(0, Math.min(75, blockChance));
                parryChance = Math.max(0, Math.min(75, parryChance));

                if (hitRoll < dodgeChance)
                    passiveType = mbEnums.PassiveType.Dodge;
                else if (hitRoll < blockChance)
                    passiveType = mbEnums.PassiveType.Block;
                else if (hitRoll < parryChance)
                    passiveType = mbEnums.PassiveType.Parry;


                if (!passiveType.equals(mbEnums.PassiveType.None)) {
                    passiveAnim = getPassiveAnimation(passiveType);
                    TargetedActionMsg msg = new TargetedActionMsg(attacker, passiveAnim, target, passiveType.value);

                    if (target.getObjectType() == mbEnums.GameObjectType.PlayerCharacter)
                        DispatchManager.dispatchMsgToInterestArea(target, msg, mbEnums.DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true, false);

                    //we need to send the animation even if the attacker misses
                    TargetedActionMsg cmm = new TargetedActionMsg(attacker, target, (float) 0, getSwingAnimation(weapon.template, null, slot));
                    DispatchManager.sendToAllInRange(target, cmm);
                    //set auto attack job
                    setAutoAttackJob(attacker, slot, delay);
                    return;
                }
            }

            //check for proccing
            checkForProc(attacker,target,weapon);

            //calculate the base damage
            int damage = ThreadLocalRandom.current().nextInt(min, max + 1);
            if (damage == 0) {
                //set auto attack job
                setAutoAttackJob(attacker, slot, delay);
                return;
            }
            if (attacker.getObjectType().equals(mbEnums.GameObjectType.Mob) && ((Mob) attacker).isPet())
                calculatePetDamage(attacker);

            //get the damage type

            mbEnums.DamageType damageType;

            if (attacker.charItemManager.getEquipped().get(slot) == null) {
                damageType = mbEnums.DamageType.CRUSHING;
                if (attacker.getObjectType().equals(mbEnums.GameObjectType.Mob))
                    if (((Mob) attacker).isSiege())
                        damageType = mbEnums.DamageType.SIEGE;
            } else {
                damageType = (mbEnums.DamageType) attacker.charItemManager.getEquipped().get(slot).template.item_weapon_damage.keySet().toArray()[0];
            }

            //get resists

            Resists resists;

            if (!AbstractCharacter.IsAbstractCharacter(target))
                resists = ((Building) target).getResists();            //this is a building
            else
                resists = ((AbstractCharacter) target).getResists();   //this is a character

            if (AbstractCharacter.IsAbstractCharacter(target)) {
                AbstractCharacter absTarget = (AbstractCharacter) target;

                //check damage shields

                PlayerBonuses bonuses = absTarget.getBonuses();

                if (bonuses != null) {

                    ConcurrentHashMap<AbstractEffectModifier, DamageShield> damageShields = bonuses.getDamageShields();
                    float total = 0;

                    for (DamageShield ds : damageShields.values()) {

                        //get amount to damage back

                        float amount;

                        if (ds.usePercent())
                            amount = damage * ds.getAmount() / 100;
                        else
                            amount = ds.getAmount();

                        //get resisted damage for damagetype

                        if (resists != null)
                            amount = resists.getResistedDamage(absTarget, attacker, ds.getDamageType(), amount, 0);
                        total += amount;
                    }

                    if (total > 0) {
                        //apply Damage back
                        attacker.modifyHealth(-total, absTarget, true);
                        TargetedActionMsg cmm = new TargetedActionMsg(attacker, attacker, total, 0);
                        DispatchManager.sendToAllInRange(target, cmm);
                    }
                }

                if (resists != null) {

                    //check for damage type immunities

                    if (resists.immuneTo(damageType)) {
                        //set auto attack job
                        //we need to send the animation even if the attacker misses
                        TargetedActionMsg cmm = new TargetedActionMsg(attacker, target, (float) 0, getSwingAnimation(weapon.template, null, slot));
                        DispatchManager.sendToAllInRange(target, cmm);
                        setAutoAttackJob(attacker, slot, delay);
                        return;
                    }
                    //calculate resisted damage including fortitude

                    damage = (int) resists.getResistedDamage(attacker, (AbstractCharacter) target, damageType, damage, 0);
                }
            }

            //remove damage from target health

            if (damage > 0) {

                if (AbstractCharacter.IsAbstractCharacter(target)) {
                    ((AbstractCharacter) target).modifyHealth(-damage, attacker, true);
                    ((AbstractCharacter) target).cancelOnTakeDamage();
                }else {
                    ((Building) target).modifyHealth(-damage, attacker);
                }
                int attackAnim = getSwingAnimation(null, null, slot);
                if (attacker.charItemManager.getEquipped().get(slot) != null) {
                    if (attacker.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter)) {
                        DeferredPowerJob weaponPower = ((PlayerCharacter) attacker).getWeaponPower();
                        attackAnim = getSwingAnimation(weapon.template, weaponPower, slot);
                    } else {
                        attackAnim = getSwingAnimation(weapon.template, null, slot);
                    }
                }
                TargetedActionMsg cmm = new TargetedActionMsg(attacker, target, (float) damage, attackAnim);
                DispatchManager.sendToAllInRange(target, cmm);
            }
        }

        //set auto attack job
        setAutoAttackJob(attacker, slot, delay);

    }

    public static void toggleCombat(boolean toggle, ClientConnection origin) {

        PlayerCharacter playerCharacter = SessionManager.getPlayerCharacter(origin);

        if (playerCharacter == null)
            return;

        playerCharacter.setCombat(toggle);

        if (!toggle) // toggle is move it to false so clear combat target
            playerCharacter.setCombatTarget(null); //clear last combat target

        UpdateStateMsg rwss = new UpdateStateMsg();
        rwss.setPlayer(playerCharacter);
        DispatchManager.dispatchMsgToInterestArea(playerCharacter, rwss, mbEnums.DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, false, false);
    }

    public static void toggleSit(boolean toggle, ClientConnection origin) {

        PlayerCharacter playerCharacter = SessionManager.getPlayerCharacter(origin);

        if (playerCharacter == null)
            return;

        playerCharacter.setSit(toggle);
        UpdateStateMsg rwss = new UpdateStateMsg();
        rwss.setPlayer(playerCharacter);
        DispatchManager.dispatchMsgToInterestArea(playerCharacter, rwss, mbEnums.DispatchChannel.PRIMARY, MBServerStatics.CHARACTER_LOAD_RANGE, true, false);
    }


    public static void handleRetaliate(AbstractCharacter target, AbstractCharacter attacker) {

        //Called when character takes damage.

        if (attacker == null || target == null)
            return;

        if (attacker.equals(target))
            return;

        if (target.isMoving() && target.getObjectType().equals(mbEnums.GameObjectType.PlayerCharacter))
            return;

        if (!target.isAlive() || !attacker.isAlive())
            return;

        boolean isCombat = target.isCombat();

        //If target in combat and has no target, then attack back

        if (isCombat && target.combatTarget == null)
            target.setCombatTarget(attacker);
    }

    public static int getSwingAnimation(ItemTemplate wb, DeferredPowerJob dpj, mbEnums.EquipSlotType slot) {

        //No weapon, return default animation
        if (wb == null)
            return 75;

        int token;

        if (dpj != null) {

            token = (dpj.getPower() != null) ? dpj.getPower().getToken() : 0;

            if (token == 563721004) //kick animation
                return 79;
        }

        //Item has no equipment slots and should not try to return an animation, return default instead
        if (wb.item_eq_slots_or == null || wb.item_eq_slots_or.isEmpty()) {
            return 75;
        }

        //declare variables
        int anim;
        int random;

        //Item can only be equipped in one slot, return animation for that slot
        if (wb.item_eq_slots_or.size() == 1) {
            if (wb.item_eq_slots_or.iterator().next().equals(mbEnums.EquipSlotType.RHELD)) {
                anim = wb.weapon_attack_anim_right.get(0)[0];
                if (dpj != null) {
                    random = ThreadLocalRandom.current().nextInt(wb.weapon_attack_anim_right.size());
                    anim = wb.weapon_attack_anim_right.get(random)[0];
                }
            } else {
                anim = wb.weapon_attack_anim_left.get(0)[0];
                if (dpj != null) {
                    random = ThreadLocalRandom.current().nextInt(wb.weapon_attack_anim_left.size());
                    anim = wb.weapon_attack_anim_left.get(random)[0];
                }
            }
            return anim;
        }

        //Item can be equipped in either hand, and should have animation sets for each hand
        if (slot.equals(mbEnums.EquipSlotType.RHELD)) {
            anim = wb.weapon_attack_anim_right.get(0)[0];
            if (dpj != null) {
                random = ThreadLocalRandom.current().nextInt(wb.weapon_attack_anim_right.size());
                anim = wb.weapon_attack_anim_right.get(random)[0];
            }
        } else {
            anim = wb.weapon_attack_anim_left.get(0)[0];
            if (dpj != null) {
                random = ThreadLocalRandom.current().nextInt(wb.weapon_attack_anim_left.size());
                anim = wb.weapon_attack_anim_left.get(random)[0];
            }
        }
        return anim;
    }

    public static int getPassiveAnimation(mbEnums.PassiveType passiveType) {
        switch (passiveType) {
            case Block:
                return COMBAT_BLOCK_ANIMATION;
            case Parry:
                return COMBAT_PARRY_ANIMATION;
            case Dodge:
                return COMBAT_DODGE_ANIMATION;
            default:
                return 0;
        }
    }

    public static void setAutoAttackJob(AbstractCharacter attacker, mbEnums.EquipSlotType slot, long delay) {
        //calculate next allowed attack and update the timestamp

        if (attacker.getTimestamps().containsKey("Attack" + slot.name()) && attacker.getTimestamps().get("Attack" + slot.name()) > System.currentTimeMillis())
            return;

        attacker.getTimestamps().put("Attack" + slot.name(), System.currentTimeMillis() + delay);

        //handle auto attack job creation
        ConcurrentHashMap<String, JobContainer> timers = attacker.getTimers();

        if (timers != null) {
            AttackJob aj = new AttackJob(attacker, slot.ordinal(), true);
            JobContainer job;
            job = JobScheduler.getInstance().scheduleJob(aj, (System.currentTimeMillis() + delay)); // offset 1 millisecond so no overlap issue
            timers.put("Attack" + slot.name(), job);
        } else
            Logger.error("Unable to find Timers for Character " + attacker.getObjectUUID());

    }

    public static int calculatePetDamage(AbstractCharacter agent) {
        //damage calc for pet
        float range;
        float damage;
        float min = 40;
        float max = 60;
        float dmgMultiplier = 1 + agent.getBonuses().getFloatPercentAll(mbEnums.ModType.MeleeDamageModifier, mbEnums.SourceType.None);
        double minDmg = getMinDmg(min, agent);
        double maxDmg = getMaxDmg(max, agent);
        dmgMultiplier += agent.getLevel() * 0.1f;
        range = (float) (maxDmg - minDmg);
        damage = min + ((ThreadLocalRandom.current().nextFloat() * range) + (ThreadLocalRandom.current().nextFloat() * range)) / 2;
        return (int) (damage * dmgMultiplier);
    }

    public static double getMinDmg(double min, AbstractCharacter agent) {
        int primary = agent.getStatStrCurrent();
        int secondary = agent.getStatDexCurrent();
        int focusLevel = 0;
        int masteryLevel = 0;
        return min * (pow(0.0048 * primary + .049 * (primary - 0.75), 0.5) + pow(0.0066 * secondary + 0.064 * (secondary - 0.75), 0.5) + +0.01 * (focusLevel + masteryLevel));
    }

    public static double getMaxDmg(double max, AbstractCharacter agent) {
        int primary = agent.getStatStrCurrent();
        int secondary = agent.getStatDexCurrent();
        int focusLevel = 0;
        int masteryLevel = 0;
        return max * (pow(0.0124 * primary + 0.118 * (primary - 0.75), 0.5) + pow(0.0022 * secondary + 0.028 * (secondary - 0.75), 0.5) + 0.0075 * (focusLevel + masteryLevel));
    }

    public static void checkForProc(AbstractCharacter attacker, AbstractWorldObject target, Item weapon){

        if(weapon == null) // cant proc without a weapon
            return;

        for(Effect eff : weapon.effects.values()){
            for(AbstractEffectModifier mod : eff.getEffectsBase().getModifiers()){
                if(mod.modType.equals(mbEnums.ModType.WeaponProc))
                    if(ThreadLocalRandom.current().nextInt(0,101) < 6)
                        ((WeaponProcEffectModifier)mod).applyProc(attacker,target);
            }
        }


    }
}