
scriptName combocombat extends activemagiceffect


import debug
import utility
import input

spell[] property add_spell auto
spell[] property dispel_spell auto

spell property combo1_spell auto
spell property combo2_spell auto
spell property combo3_spell auto
spell property attack1_spell auto
spell property attack2_spell auto
spell property attack3_spell auto
spell property attack4_spell auto
spell property attack5_spell auto

actor selfref

int combo
int WeaponL
int WeaponR


Event OnEffectStart(Actor akTarget, Actor akCaster)

    selfref = akTarget
    CheckMelee()
    ;Notification("ComboCombat running")

EndEvent


Event OnEffectFinish(Actor akTarget, Actor akCaster)

    selfref.RemoveSpell(combo1_spell)
    selfref.RemoveSpell(combo2_spell)
    selfref.RemoveSpell(combo3_spell)
    selfref.RemoveSpell(attack1_spell)
    selfref.RemoveSpell(attack2_spell)
    selfref.RemoveSpell(attack3_spell)
    selfref.RemoveSpell(attack4_spell)
    selfref.RemoveSpell(attack5_spell)

    unRegisterForAnimationEvent(selfref, "WeaponSwing")
    unRegisterForAnimationEvent(selfref, "WeaponLeftSwing")
    unRegisterForAnimationEvent(selfref, "AttackStop")
    unRegisterForAnimationEvent(selfref, "PowerAttackStop")

    selfref = none
    Notification("ComboCombat uninstalled")

EndEvent


Event OnObjectEquipped(Form akBaseObject, ObjectReference akReference)

    if akBaseObject as weapon
        CheckMelee()
    endif

endEvent


Event OnObjectUnequipped(Form akBaseObject, ObjectReference akReference)

    if akBaseObject as weapon
        CheckMelee()
    endif

EndEvent


Event OnAnimationEvent(ObjectReference akSource, string asEventName)

    if asEventName == "WeaponSwing" || asEventName == "WeaponLeftSwing"

        if !selfref.GetAnimationVariableBool("bAllowRotation")

            if combo == 0
                combo = 1
                selfref.AddSpell(attack1_spell, false)
            elseif combo == 1
                combo = 2
                selfref.DispelSpell(attack1_spell)
                selfref.AddSpell(combo1_spell, false)
                selfref.AddSpell(attack2_spell, false)
            elseif combo == 2
                combo = 3
                selfref.DispelSpell(attack2_spell)
                selfref.AddSpell(attack3_spell, false)
            elseif combo == 3
                combo = 4
                selfref.DispelSpell(combo1_spell)
                selfref.DispelSpell(attack3_spell)
                selfref.AddSpell(attack4_spell, false)
                selfref.AddSpell(combo2_spell, false)
            elseif combo == 4
                combo = 5
                selfref.DispelSpell(attack4_spell)
                selfref.AddSpell(attack5_spell, false)
            elseif combo == 5
                combo = 0
                selfref.DispelSpell(attack5_spell)
                selfref.DispelSpell(combo2_spell)
            endif

            RegisterForAnimationEvent(selfref, "AttackStop")

        else

            RegisterForAnimationEvent(selfref, "AttackStop")

        endif

    elseif asEventName == "AttackStop"

        combo = 0
        unRegisterForAnimationEvent(selfref, "AttackStop")

        selfref.DispelSpell(combo2_spell)
        selfref.DispelSpell(combo1_spell)
        selfref.DispelSpell(attack5_spell)
        selfref.DispelSpell(attack4_spell)
        selfref.DispelSpell(attack3_spell)
        selfref.DispelSpell(attack2_spell)
        selfref.DispelSpell(attack1_spell)
        ;Notification("combo x"+combo)

    endif

EndEvent


 Function CheckMelee()

    WeaponL = selfref.GetEquippedItemType(0)
    WeaponR = selfref.GetEquippedItemType(1)
    if WeaponR < 7 || WeaponR == 8 || WeaponR == 11
        RegisterForAnimationEvent(selfref, "WeaponSwing")
        ;RegisterForAnimationEvent(selfref, "PreHitFrame")
        ;RegisterForAnimationEvent(selfref, "AttackStop")
    else
        unRegisterForAnimationEvent(selfref, "WeaponSwing")
    endif
    if WeaponL < 5 || WeaponL == 8 || WeaponL == 11 
        RegisterForAnimationEvent(selfref, "WeaponLeftSwing")
    else
        unRegisterForAnimationEvent(selfref, "WeaponLeftSwing")
    endIf
    combo = 0

EndFunction
