• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[snippet] UnitStateEvent

Level 31
Joined
Jul 10, 2007
Messages
6,306
JASS:
library UnitStateEvent /* v1.0.0.1
*************************************************************************************
*
*   TriggerRegisterUnitStateEvent for UNIT_STATE_LIFE and UNIT_STATE_MANA for all units.
*
*************************************************************************************
*   */uses/*
*   
*       */ UnitIndexer /*                 hiveworkshop.com/forums/jass-functions-413/snippet-event-186555/
*
************************************************************************************
*
*   function EnableEventUnitState takes boolean b returns nothing
*
*   function GetEventUnitStateUnitId takes nothing returns integer
*   function GetEventUnitStateUnit takes nothing returns unit
*
*   function GetHealthEvent takes nothing returns Event
*   function GetManaEvent takes nothing returns Event
*
************************************************************************************/
    globals
        private constant integer WASTE = 15
    
        private trigger q = CreateTrigger()     //state event
        
        private Event o = 0                     //hp event
        private Event p = 0                     //mana event
        
        private integer w = 0                   //waste
        private integer e = 0                   //total
        private integer array r                 //next
        private integer array t                 //previous
        
        private integer f = 0                   //event unit id
    endglobals
    
    function EnableEventUnitState takes boolean b returns nothing
        if (b) then
            call EnableTrigger(q)
        else
            call DisableTrigger(q)
        endif
    endfunction
    function GetEventUnitStateUnitId takes nothing returns integer
        return f
    endfunction
    function GetEventUnitStateUnit takes nothing returns unit
        return GetUnitById(f)
    endfunction
    function GetHealthEvent takes nothing returns Event
        return o
    endfunction
    function GetManaEvent takes nothing returns Event
        return p
    endfunction
    
    private module Init
        private static method ev takes nothing returns boolean
            local integer d = f
            set f = GetUnitId(GetTriggerUnit())
            if (GetEventUnitState() == UNIT_STATE_LIFE) then
                call o.fire()
            else
                call p.fire()
            endif
            set f = d
            return false
        endmethod
        private static method add takes nothing returns boolean
            local integer d = GetIndexedUnitId()
            call TriggerRegisterUnitStateEvent(q, GetUnitById(d), UNIT_STATE_LIFE, GREATER_THAN_OR_EQUAL, .405)
            call TriggerRegisterUnitStateEvent(q, GetUnitById(d), UNIT_STATE_MANA, GREATER_THAN_OR_EQUAL, .405)
            set t[d] = t[0]
            set r[t[0]] = d
            set r[d] = 0
            set t[0] = d
            return false
        endmethod
        private static method rem takes nothing returns boolean
            local integer d = GetIndexedUnitId()
            set r[t[d]] = r[d]
            set t[r[d]] = t[d]
            set w = w + 1
            if (w > e and w >= WASTE) then
                call TriggerClearConditions(q)
                call DestroyTrigger(q)
                set q = CreateTrigger()
                call TriggerAddCondition(q, Condition(function thistype.ev))
                set d = r[0]
                loop
                    exitwhen d == 0
                    call TriggerRegisterUnitStateEvent(q, GetUnitById(d), UNIT_STATE_LIFE, GREATER_THAN_OR_EQUAL, .405)
                    call TriggerRegisterUnitStateEvent(q, GetUnitById(d), UNIT_STATE_MANA, GREATER_THAN_OR_EQUAL, .405)
                    set d = r[d]
                endloop
            endif
            return false
        endmethod
        private static method onInit takes nothing returns nothing
            set o = Event.create()
            set p = Event.create()
            call TriggerAddCondition(q, Condition(function thistype.ev))
            call RegisterUnitIndexEvent(Condition(function thistype.add), UnitIndexer.INDEX)
            call RegisterUnitIndexEvent(Condition(function thistype.rem), UnitIndexer.DEINDEX)
        endmethod
    endmodule
    
    private struct Inits extends array
        implement Init
    endstruct
endlibrary
 
Last edited:
Top