[Snippet] GetUnitRegeneration

Scripts

14 years
edited 8 years
The title explains it all.

Code (jass) Select
/********************************************************
*
*   GetUnitRegeneration
*   v2.0.0.0
*   By Magtheridon96
*
*   - Gets unit regeneration values
*
*   Requirements:
*   -------------
*
*       - UnitIndexer by Nestharus
*           - https://wc3modding.info/4607/unitindexer/
*       - DamageEvent by Nestharus
*           - hiveworkshop.com/forums/jass-resources-412/snippet-damageevent-186829/
*       - Heal by Magtheridon96
*           - https://wc3modding.info/4910/system-heal/
*       - CTL by Nestharus
*           - hiveworkshop.com/forums/jass-resources-412/snippet-constant-timer-loop-32-a-201381/
*
*       Optional:
*       ---------
*
*       - DamageControl by PurgeandFire111
*           - hiveworkshop.com/forums/jass-resources-412/system-damage-control-204672/
*
*   API:
*   ----
*
*       - function IsUnitRegenerating takes unit u returns boolean
*           - Tells whether a unit is regenerating or not.
*       - function IsUnitRegeneratingById takes integer i returns boolean
*           - Tells whether a unit is regenerating or not given the Id.
*       - function GetUnitRegeneration takes unit u returns real
*           - Gets the amount of regeneration for a given unit.
*       - function GetUnitRegenerationById takes integer i returns real
*           - Gets the amount of regeneration for a given unit given the Id.
*       - function IsRegenerationPositive takes unit u returns boolean
*           - Tells whether the regeneration is positive.
*       - function IsRegenerationPositiveById takes integer i returns boolean
*           - Tells whether the regeneration is positive given the Id.
*
********************************************************/
library GetUnitRegeneration requires UnitIndexer, DamageEvent, Heal, CTL, optional DamageControl
   
    private struct UnitRegen extends array
       
        static thistype array next
        static thistype array prev
       
        static real array life
        static real array max
        static real array damaged
        static real array amount
       
        static boolean array regenerating
       
        static method damage takes nothing returns nothing
            static if LIBRARY_DamageControl then
                set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageControl.result
            else
                set damaged[DamageEvent.targetId] = damaged[DamageEvent.targetId] + DamageEvent.amount
            endif
        endmethod
       
        static method heal takes nothing returns nothing
            set damaged[Heal.targetId] = damaged[Heal.targetId] - Heal.effective
        endmethod
       
        static method index takes nothing returns nothing
            // Add index to list
            set next[GetIndexedUnitId()] = 0
            set prev[GetIndexedUnitId()] = prev[0]
            set next[prev[0]] = GetIndexedUnitId()
            set prev[0] = GetIndexedUnitId()
           
            // Setting current life and max life
            set life[GetIndexedUnitId()] = GetWidgetLife(GetIndexedUnit())
            set max[GetIndexedUnitId()] = GetUnitState(GetIndexedUnit(), UNIT_STATE_MAX_LIFE)
        endmethod
       
        static method deindex takes nothing returns nothing           
            // Remove from list
            set next[prev[GetIndexedUnitId()]] = next[GetIndexedUnitId()]
            set prev[next[GetIndexedUnitId()]] = prev[GetIndexedUnitId()]
           
            // Resetting Data
            set amount[GetIndexedUnitId()] = 0
            set damaged[GetIndexedUnitId()] = 0
           
            set regenerating[GetIndexedUnitId()] = false
        endmethod
       
        implement CT32
       
            local thistype this = next[0]
            local real hp = 0
            local real ml = 0
           
            loop
                exitwhen 0 == this
               
                set ml = GetUnitState(GetUnitById(this), UNIT_STATE_MAX_LIFE)
               
                if max[this] != ml then
                    set life[this] = life[this] * (ml / max[this])
                endif
               
                set hp = GetWidgetLife(GetUnitById(this)) + damaged[this]
               
                if hp != life[this] then
                    set amount[this] = (hp - life[this]) / 0.03125
                    set regenerating[this] = true
                    set life[this] = hp - damaged[this]
                else
                    set regenerating[this] = false
                    set amount[this] = 0
                endif
               
                set damaged[this] = 0
                set max[this] = ml
               
                set this = next[this]
            endloop
           
        implement CT32End
       
        private static code indexCode
        private static code deindexCode
        private static code damageCode
        private static code healCode
       
        private static method onInit takes nothing returns nothing
       
            set indexCode = function thistype.index
            set deindexCode = function thistype.deindex
            set damageCode = function thistype.damage
            set healCode = function thistype.heal
           
            static if LIBRARY_DamageControl then
                call DamageControl.FINAL.register(Filter(damageCode))
            else
                call DamageEvent.ANY.register(Filter(damageCode))
            endif
           
            call RegisterAnyHealEvent(healCode)
            call UnitIndexer.INDEX.register(Filter(indexCode))
            call UnitIndexer.DEINDEX.register(Filter(deindexCode))
        endmethod
       
    endstruct
   
    function IsUnitRegenerating takes unit u returns boolean
        return UnitRegen.regenerating[GetUnitUserData(u)]
    endfunction
   
    function IsUnitRegeneratingById takes integer i returns boolean
        return UnitRegen.regenerating[i]
    endfunction
   
    function GetUnitRegeneration takes unit u returns real
        return UnitRegen.amount[GetUnitUserData(u)]
    endfunction
   
    function GetUnitRegenerationById takes integer i returns real
        return UnitRegen.amount[i]
    endfunction
   
    function IsRegenerationPositive takes unit u returns boolean
        return UnitRegen.amount[GetUnitUserData(u)] >= 0
    endfunction
   
    function IsRegenerationPositiveById takes integer i returns boolean
        return UnitRegen.amount[i] >= 0
    endfunction
   
endlibrary

library IsUnitRegenerating requires GetUnitRegeneration
endlibrary


Feel free to comment..
14 years
This one looks really useful, I can think of some stuffs that you can do here, but it will really nice if you can put a list of all the things that you can do with this script. ;)
14 years
Thanks :D
Updated the code. I found a useless line in the deindex method.