Snippet DetectRevivingStructure

Troll-Brain

You can change this now in User CP.
Reaction score
85
Because i love making useless things that don't exist in the trigger editor, and because also someone "need" it, i decided to create the GetRevivingStructure(), the building which ressurect the hero.

Here is the code :

This library have a removal detection, the "system" itself can't have any leak.

Librairies needed :

Table , TimerUtils (used by RemovalDetection)

JASS:
// *************************************************************
// *   DetectRevivingStructure with a Removal Detection -- Version 2.0
// *                        by Troll-Brain
// *************************************************************
// *
// *    CREDITS:
// *        - Vexorian (JassHelper)
// *        - PitzerMike (JassNewGenPack)
// *        - Pipedream (Grimoire)
// *        - Anitarf (v 2.0)
// *
// *    WHAT THIS LIBRARY DOES:
// *        - When an hero is ressurected with a building or why not an other unit wicth can ressurect heroes like an altar,
// *          there is not Get to know witch unit did that. This library let to know that. Ofc it works for all revive events.
// *  
// *    REQUIRES:
// *        - The Jass New Gen Pack
// *        - Table : <a href="http://www.wc3campaigns.net/showthread.php?t=101246" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://www.wc3campaigns.net/showthread.php?t=101246</a>
// *        - TimerUtils : <a href="http://www.wc3campaigns.net/showthread.php?t=101322" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://www.wc3campaigns.net/showthread.php?t=101322</a>
// *        - RemovalDetection : <a href="http://www.wc3campaigns.net/showthread.php?t=102588" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://www.wc3campaigns.net/showthread.php?t=102588</a>
// *
// *    HOW TO USE:
// *        - check the examples
// *
// *        - If you plan to ressurect heroes with a trigger action you must use the functions ReviveHeroAlt or ReviveHeroLocAlt
// *          witch work exactly as the natives ones, ReviveHero and ReviveHeroLoc.
// *          Or you can also use the functions InitTriggeringRessurect and EndTriggeringRessurect like in the gui example.
// *          You have also to care about the GetRevivingStructure usage (if you use TriggerSleepAction or PolledWait), use a local and
// *          set it to GetRevivingStructure before any wait.
// *
// *    CHANGELOG:
// *        - v 2.0 * Edited the code, because it did leaks, now require the library RemovalDetection and so TimerUtils as well.
// *                * Adding Table to avoid using one more game cache.
// *                * Renamed library and functions to follow jass conventions
// *
// *        - v 1.0 Initial Release
// *************************************************************

library DetectRevivingStructure initializer init uses RemovalDetection , Table

globals
    private HandleTable Ht
    private boolean IsATriggerAction= false
endglobals

private struct s_data
    unit revivingstructure
    unit hero
    
    static method create takes unit u returns s_data
        local s_data s = s_data.allocate()
        
        set Ht<u> = s
        set s.hero = u
        return s
    endmethod
    
    method onDestroy takes nothing returns nothing
        call Ht.flush(.hero)
        set .revivingstructure = null
        set .hero = null
    endmethod
    
endstruct

globals
    private s_data S_data = 0
endglobals

public function IsUnitHero takes unit u returns boolean
    return IsHeroUnitId(GetUnitTypeId(u))
endfunction

private function Conditions takes nothing returns boolean
    local unit u = GetOrderTargetUnit()
    
    if GetIssuedOrderId()&gt;=852027 and GetIssuedOrderId()&lt;=852038 and IsUnitHero(u) and IsUnitType(u,UNIT_TYPE_DEAD) then
        set S_data = Ht<u>
        
        if S_data == 0 then
            set S_data= s_data.create(u)
        endif
        set S_data.revivingstructure=GetOrderedUnit()
        
    endif
    
    set u=null
    return false
    
endfunction

private function DestroyData takes nothing returns boolean
    
    if IsUnitHero(GetScopingUnit()) then
        set S_data = Ht[GetScopingUnit()]
        call S_data.destroy()
    endif
    return false
endfunction

function GetRevivingStructure takes nothing returns unit
    local eventid evd = GetTriggerEventId()
    
    if evd==EVENT_PLAYER_HERO_REVIVE_START or evd==EVENT_PLAYER_HERO_REVIVE_CANCEL or evd==EVENT_PLAYER_HERO_REVIVE_FINISH or evd==EVENT_UNIT_HERO_REVIVE_START or evd==EVENT_UNIT_HERO_REVIVE_CANCEL or evd==EVENT_UNIT_HERO_REVIVE_FINISH then
    
        if IsATriggerAction then
            set evd = null
            return null
        else
            set evd = null
            set S_data = Ht[GetRevivingUnit()]
            return S_data.revivingstructure
        endif
        
    else
        debug call BJDebugMsg(&quot;You tried to use GetRevivingStructure() with a wrong event, use an EVENT_PLAYER/UNIT_HERO_REVIVE&quot;)
    endif
    
    set evd = null
    return null
endfunction

function ReviveHeroAlt takes unit whichHero, real x, real y, boolean doEyecandy returns boolean
    local boolean b=false
    set IsATriggerAction=true
    set b=ReviveHero(whichHero,x,y,doEyecandy)
    set IsATriggerAction=false
    return b
endfunction

function ReviveHeroLocAlt takes unit whichHero, location loc , boolean doEyecandy returns boolean
    local boolean b=false
    set IsATriggerAction=true
    set b=ReviveHeroLoc(whichHero,loc,doEyecandy)
    set IsATriggerAction=false
    return b
endfunction

function InitTriggeringRessurect takes nothing returns nothing
    set IsATriggerAction=true 
endfunction

function EndTriggeringRessurect takes nothing returns nothing
    set IsATriggerAction=false
endfunction

public function init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    
    set Ht = HandleTable.create()
    
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
    call TriggerAddCondition(trig,Condition(function Conditions))
    
    set trig = CreateTrigger()
    call TriggerRegisterOutOfScopeEvent(trig)
    call TriggerAddCondition(trig,Condition(function DestroyData))
    
    set trig = null
endfunction

endlibrary</u></u>


For some reasons if the requirement of the library RemovalDetection is too mutch i have made this one :

Librairies needed :

Table

Since there is not a removal detection, the "system" itself can leak, but only if some heroes are removed during the game.
If you care about it i have made a custom function RemoveHero.
Even if you don't use this function the "system" will be fail only if you try to ressurect with a building more than 8190 different heroes.

JASS:
// *************************************************************
// *                   DetectRevivingStructure -- Version 2.0
// *                          WITHOUT removal detection
// *                              by Troll-Brain
// *************************************************************
// *
// *    CREDITS:
// *        - Vexorian (JassHelper)
// *        - PitzerMike (JassNewGenPack)
// *        - Pipedream (Grimoire)
// *        - Anitarf (v 2.0)
// *
// *    WHAT THIS LIBRARY DOES:
// *        - When an hero is ressurected with a building or why not an other unit witch can ressurect heroes like an altar,
// *          there is no Get to know witch unit did that. This library let to know that. Ofc it works for all revive events.
// *  
// *    REQUIRES:
// *        - The Jass New Gen Pack
// *        - Table : <a href="http://www.wc3campaigns.net/showthread.php?t=101246" target="_blank" class="link link--external" rel="nofollow ugc noopener">http://www.wc3campaigns.net/showthread.php?t=101246</a>
// *
// *    HOW TO USE:
// *        - check the examples
// *
// *        - If you plan to ressurect heroes with a trigger action you must use the functions ReviveHeroAlt or ReviveHeroLocAlt
// *          which work exactly as the natives ones, ReviveHero and ReviveHeroLoc.
// *          Or you can also use the functions InitTriggeringRessurect and EndTriggeringRessurect like in the gui example.
// *          You have also to care about the GetRevivingStructure usage (if you use TriggerSleepAction or PolledWait), use a local and
// *          set it to GetRevivingStructure before any wait.
// *
// *        - CAREFULL !!!
// *          In this library i don&#039;t use the removal detection that can be too much in some maps.
// *          If you already use the library RemovalDetection don&#039;t use this one. It can&#039;t leak, if the heroes in the map are not removed.
// *          But it will leak if heroes are removed without the function RemoveHero.
// *          Also if the hereoes are removed without this function, it will fail if you try to ressurect with a building more than 8190 different heroes during the game.
// *
// *    CHANGELOG:
// *        - v 2.0 * Edited the code, because it did leaks, now require the library RemovalDetection and so TimerUtils as well.
// *                * Adding Table to avoid using one more game cache.
// *                * Renamed library and functions to follow jass conventions
// *
// *        - v 1.0 Initial Release
// *************************************************************

library DetectRevivingStructure initializer init uses Table

globals
    private HandleTable Ht
    private boolean IsATriggerAction= false
endglobals

private struct s_data
    unit revivingstructure
    unit hero
    
    static method create takes unit u returns s_data
        local s_data s = s_data.allocate()
        
        set Ht<u> = s
        set s.hero = u
        return s
    endmethod
    
    method onDestroy takes nothing returns nothing
        call Ht.flush(.hero)
        set .revivingstructure = null
        set .hero = null
    endmethod
    
endstruct

globals
    private s_data S_data = 0
endglobals

public function IsUnitHero takes unit u returns boolean
    return IsHeroUnitId(GetUnitTypeId(u))
endfunction

private function Conditions takes nothing returns boolean
    local unit u = GetOrderTargetUnit()
    
    if GetIssuedOrderId()&gt;=852027 and GetIssuedOrderId()&lt;=852038 and IsUnitHero(u) and IsUnitType(u,UNIT_TYPE_DEAD) then
        set S_data = Ht<u>
        
        if S_data == 0 then
            set S_data= s_data.create(u)
        endif
        set S_data.revivingstructure=GetOrderedUnit()
        
    endif
    
    set u=null
    return false
    
endfunction

function GetRevivingStructure takes nothing returns unit
    local eventid evd = GetTriggerEventId()
    
    if evd==EVENT_PLAYER_HERO_REVIVE_START or evd==EVENT_PLAYER_HERO_REVIVE_CANCEL or evd==EVENT_PLAYER_HERO_REVIVE_FINISH or evd==EVENT_UNIT_HERO_REVIVE_START or evd==EVENT_UNIT_HERO_REVIVE_CANCEL or evd==EVENT_UNIT_HERO_REVIVE_FINISH then
    
        if IsATriggerAction then
            set evd = null
            return null
        else
            set evd = null
            set S_data = Ht[GetRevivingUnit()]
            return S_data.revivingstructure
        endif
        
    else
        debug call BJDebugMsg(&quot;You tried to use GetRevivingStructure() with a wrong event, use an EVENT_UNIT/PLAYER_HERO_REVIVE_&quot;)
    endif
    
    set evd = null
    return null
endfunction

function ReviveHeroAlt takes unit whichHero, real x, real y, boolean doEyecandy returns boolean
    local boolean b=false
    set IsATriggerAction=true
    set b=ReviveHero(whichHero,x,y,doEyecandy)
    set IsATriggerAction=false
    return b
endfunction

function ReviveHeroLocAlt takes unit whichHero, location loc , boolean doEyecandy returns boolean
    local boolean b=false
    set IsATriggerAction=true
    set b=ReviveHeroLoc(whichHero,loc,doEyecandy)
    set IsATriggerAction=false
    return b
endfunction

function RemoveHero takes unit witchHero returns nothing
    if witchHero == null then
        debug call BJDebugMsg(&quot;function RemoveHero : the witchHero argument is null&quot;)
        return
    elseif not IsUnitHero(witchHero) then
        debug call BJDebugMsg(&quot;function RemoveHero : the witchHero argument is not an hero&quot;)
        return
    endif
    set S_data = Ht[witchHero]
    if S_data != 0 then
        call S_data.destroy()
    endif
    call RemoveUnit(witchHero)
endfunction

function InitTriggeringRessurect takes nothing returns nothing
    set IsATriggerAction=true 
endfunction

function EndTriggeringRessurect takes nothing returns nothing
    set IsATriggerAction=false
endfunction

public function init takes nothing returns nothing
    local trigger trig = CreateTrigger()
    
    set Ht = HandleTable.create()
    
    call TriggerRegisterAnyUnitEventBJ(trig,EVENT_PLAYER_UNIT_ISSUED_TARGET_ORDER)
    call TriggerAddCondition(trig,Condition(function Conditions))
    
    set trig = null
    
endfunction

endlibrary</u></u>


In the attached map, there are examples of usage.

Critics of code are welcome.
 

Attachments

  • DetectRevivingStructure.w3x
    36.7 KB · Views: 218

Romek

Super Moderator
Reaction score
964
Something minor caught my eye:
JASS:
 // ...
    else
        debug call BJDebugMsg//...
   endif

Should be:
JASS:
//...
   debug else
        debug call BJDebugMsg//...
   endif


This seems quite useful, and I'm surprised it hasn't got many comments already.
I'll look over it in more detail later.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
If this will be approved, i will "fix" the else.
And add new feature(s), like know since let knowing how much time the hero is actually resurrecting.
 

WolfieeifloW

WEHZ Helper
Reaction score
372
Usually it goes the opposite way :p .
You fix/add stuff first then it gets approved.

At the snippet itself;
This could be useful for some people indeed ;) .
 

WolfieeifloW

WEHZ Helper
Reaction score
372
I can see it for like RPG's or something maybe.
A building revives and players need to be warned or something.

EDIT: And I'm pretty sure this needs more feedback/comments to get approved.
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
Last bump ever.
If you want approve it but something is wrong with this for you, tell me what.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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