AEH (Attack Effect Handling)

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Attack Effect Handling, v1.0.4
JASS:
library AEH initializer Init requires Damage
/*
    AEH - Attack Effect Handling.
    v1.0.4
    by kingking
    
    =============
    What is AEH?
    =============
    AEH makes your life on handling attack effect(Lifesteal, Mana Burn, etc) easier.
    It can restrict attack effect from being triggered, if you want.
    Imagine, you want units that have mana burn not to get lifesteal.
    AEH is aimed to solve that.
    
    ===========
    APIs :
    ===========
    Module :
    implement AEH
    
    Static Method :
    AEH_filter(DamageSource,DamagedUnit,EventDamage)-> boolean
    - Return true will make AEH create a new instance of struct.
    
    AEH_register(string key)
    - Assign the struct to AEH type(key).
    
    AEH_setTypeCompatible(string key)
    - Add the effect into compatible list.
    
    AEH_setTypePrioriy(string key, integer priority)
    - The higher the priority, the faster the type of struct will be executed.
    
    Methods :
    AEH_onCreate()-> boolean 
    **If return true, lower priority of AEH struct will not be executed.
    
    AEH_onDestroy()
    - For your struct destroying needs.
    ** Optional.
    
    Members :
    unit : attacker, target
    real : damage
    
    Static Member :
    AEH_autoDestroy
    - If set to true, then the struct will be destroyed automatically after AEH_onCreate.
    - If set to false, then you have to destroy it manually through .destroy().
    ** True as default.
    
    ============================
    Usage Example (With Heal) :
    ============================
    struct Lifesteal
        private static method AEH_filter takes unit attacker, unit target, real damage returns boolean
            return GetUnitAbilityLevel(damager,'A000')>0
        endmethod
        
        private method AEH_onCreate takes nothing returns boolean
            call HealUnit(this.attacker,this.attacker,damage*0.2,HEAL_TYPE_LIFESTEAL)
            return true
        endmethod
        
        private static method onInit takes nothing returns nothing
            call thistype.AEH_setTypePriority("Lifesteal",1)
            call thistype.AEH_register("Lifesteal")
        endmethod
        
        implement AEH
    endstruct
    
    =============
    Requires :
    =============
    Jasshelper v0.A.2.B or later
    Damage by Jesus4Lyf
    
    ========================
    Implementation steps :
    ========================
    - Implement Damage to your map.
    - Copy the trigger to your map.
    - Adjust the fire condition if you wished. */
    function FireCondition takes unit attacker, unit target returns boolean
        return Damage_IsAttack()
    endfunction
/*  - Enjoy!                                    */
    
    globals
        private hashtable hasht=InitHashtable()
        private trigger DamageTrig=CreateTrigger()
        private unit DamageSource=null
        private unit DamagedUnit=null
        private real EventDamage=0.0
    endglobals
    
    private function FireTrig takes trigger trig, unit u1, unit u2, real dam returns boolean
        local unit oldu1=DamageSource
        local unit oldu2=DamagedUnit
        local real olddam=EventDamage
        local boolean boo
        set DamageSource=u1
        set DamagedUnit=u2
        set EventDamage=dam
        set boo=TriggerEvaluate(trig)
        set DamageSource=oldu1
        set DamagedUnit=oldu2
        set EventDamage=olddam
        set oldu1=null
        set oldu2=null
        return boo
    endfunction
    
    private struct StringList
        readonly string s
        readonly thistype prev
        readonly thistype next
        
        static method createList takes nothing returns thistype
            local thistype this=thistype.allocate()
            set this.prev=this
            set this.next=this
            return this
        endmethod
        method addString takes string s returns nothing
            local thistype node
            if not HaveSavedBoolean(hasht,-this,StringHash(s)) then
                set node=thistype.allocate()
                set node.s=s
                
                set this.prev.next=node
                set node.prev=this.prev
                set node.next=this
                set this.prev=node
                call SaveBoolean(hasht,-this,StringHash(s),true)
            endif
        endmethod
        method copyList takes thistype list returns nothing
            local thistype listx=list
            set list=list.next
            loop
            exitwhen list==listx
                call this.addString(list.s)
                set list=list.next
            endloop
        endmethod
        method isStringExists takes string s returns boolean
            return LoadBoolean(hasht,-this,StringHash(s))
        endmethod
        method chainDestroy takes nothing returns nothing
            local thistype ori=this
            set this=this.next
            loop
            exitwhen this==ori
                set this.prev.next=this.next
                set this.next.prev=this.prev
                call this.destroy()
                set this=this.next
            endloop
            call this.destroy()
            call FlushChildHashtable(hasht,-this)
        endmethod
    endstruct
    
    private struct Field
    /*
        Field at here, means a group of node.
        The nodes in group have the same keyword.
    */
        private trigger trig
        private string word
        private thistype next
        private thistype prev
        private thistype nextx
        private thistype prevx
        private integer priorityx
        private StringList s
        
        private static method autoLink takes thistype head, thistype node returns nothing
            local thistype this=head
            set this.prev.next=node
            set node.prev=this.prev
            set node.next=this
            set this.prev=node
        endmethod
    
        private static method autoLinkX takes thistype node returns nothing
            local thistype this=thistype(0).nextx
            loop
                if node.priorityx>=this.priorityx then
                    set this.prevx.nextx=node
                    set node.prevx=this.prevx
                    set node.nextx=this
                    set this.prevx=node
                    return
                endif
                set this=this.nextx
            endloop
        endmethod
        
        method setTrue takes string w returns nothing
            local string s=this.word
            if this.s==0 then
                set this.s=StringList.createList()
            endif
            call this.s.addString(w)
                
            set this=LoadInteger(hasht,StringHash(w),0)
            if this==0 then
                set this=thistype.allocate()
                set this.word=w
                set this.prev=this
                set this.next=this
                call SaveInteger(hasht,StringHash(w),0,this)
                call thistype.autoLinkX(this)
            endif
            if this.s==0 then
                set this.s=StringList.createList()
            endif
            call this.s.addString(s)
            set s=null
        endmethod
        
        static method addTrig takes trigger trig, string w returns thistype
            local thistype this=LoadInteger(hasht,StringHash(w),0)
            local thistype new
            if this==0 then
                set this=thistype.allocate()
                set this.word=w
                set this.prev=this
                set this.next=this
                call SaveInteger(hasht,StringHash(w),0,this)
                call thistype.autoLinkX(this)
            endif
            set new=thistype.allocate()
            set new.word=w
            set new.trig=trig
            call thistype.autoLink(this,new)
            return this
        endmethod
        
        static method setFieldPriority takes string w, integer priority returns thistype
            local thistype this=LoadInteger(hasht,StringHash(w),0)
            if this==0 then
                set this=thistype.allocate()
                set this.word=w
                set this.prev=this
                set this.next=this
                call SaveInteger(hasht,StringHash(w),0,this)
            else
                set this.prevx.nextx=this.nextx
                set this.nextx.prevx=this.prevx
            endif
            set this.priorityx=priority
            call thistype.autoLinkX(this)
            return this
        endmethod
        
        private method fireLocalTrig takes unit u1, unit u2, real r returns boolean
            local thistype ori=this
            local boolean boo=false
            set this=this.next
            loop
            exitwhen this==ori
                if FireTrig(this.trig,u1,u2,r) then
                    set boo=true
                endif
                set this=this.next
            endloop
            return boo
        endmethod
        
        static method fireFieldTrig takes nothing returns boolean
            local thistype this=thistype(0).nextx
            local thistype array fired
            local integer firedCount=0
            local boolean isFired=false
            local integer i
            local StringList sl=StringList.create()
            loop
            exitwhen this==0 or isFired
                if this.fireLocalTrig(GetEventDamageSource(),GetTriggerUnit(),GetEventDamage()) then
                    set isFired=true
                    call sl.copyList(this.s)
                endif
                set this=this.nextx
            endloop
            if sl.prev==sl then
                return false
            endif
            loop
            exitwhen this==0
                if sl.isStringExists(this.word) and this.fireLocalTrig(GetEventDamageSource(),GetTriggerUnit(),GetEventDamage()) then
                    call sl.copyList(this.s)
                endif
                set this=this.nextx
            endloop
            return false
        endmethod
    endstruct
    
    private function IsAttack takes nothing returns boolean
        return FireCondition(GetEventDamageSource(),GetTriggerUnit()) and Field.fireFieldTrig()
    endfunction
    
    private function Init takes nothing returns nothing
        call Damage_RegisterEvent(DamageTrig)
        call TriggerAddCondition(DamageTrig,Condition(function IsAttack))
    endfunction
    
    module AEH
        private static trigger trig
        private static Field wordF
        static boolean AEH_autoDestroy=true
        unit attacker
        unit target
        real damage
        
        private method destroy takes nothing returns nothing
            static if thistype.AEH_onDestroy.exists then
                call this.AEH_onDestroy()
            endif
        endmethod
        
        private static method AEHEvent takes nothing returns boolean
            local thistype this
            local boolean boo=false
            static if thistype.AEH_filter.exists then
                if AEH_filter(DamageSource,DamagedUnit,EventDamage) then
                    set this=thistype.allocate()
                    set this.attacker=DamageSource
                    set this.target=DamagedUnit
                    set this.damage=EventDamage
                    set boo=this.AEH_onCreate()
                    if thistype.AEH_autoDestroy then
                        call this.destroy()
                    endif
                endif
            else
                set this=thistype.allocate()
                set this.attacker=DamageSource
                set this.target=DamagedUnit
                set this.damage=EventDamage
                set boo=this.AEH_onCreate()
                if thistype.AEH_autoDestroy then
                    call this.destroy()
                endif
            endif
            return boo
        endmethod
        
        static method AEH_setCompatible takes string w returns nothing
            call thistype.wordF.setTrue(w)
        endmethod
        
        static method AEH_setTypePriority takes string w, integer p returns nothing
            set thistype.wordF=Field.setFieldPriority(w,p)
        endmethod
        
        static method AEH_register takes string w returns nothing
            set thistype.trig=CreateTrigger()
            call TriggerAddCondition(thistype.trig,Condition(function thistype.AEHEvent))
            set thistype.wordF=Field.addTrig(thistype.trig,w)
        endmethod
    endmodule
endlibrary


Related Link : Damage

Demo Map (Please replace the updated code above to demo map) includes Mana Burn and Lifesteal item.
Mana Burn will override Lifesteal, so Lifesteal will not be triggered when the unit has Mana Burn.

History :
  • v1.0.4
    • Optimized loop.
  • v1.0.3
    • First Release
 

Attachments

  • AEH.w3x
    55.4 KB · Views: 380
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top