Snippet GUI-Friendly Damage Detection

Discussion in 'Systems and Snippets' started by Weep, Oct 2, 2009.

  1. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    GUI-Friendly Damage Detection
    by Weep
    Version 1.2.1​

    -- General Information --
    This system provides a leak-free, GUI-friendly implementation of an "any unit takes damage" event. It requires no JASS knowledge to use, nor any other systems, nor any software other than the World Editor. It will not interfere with other systems, either.

    It uses the Game - Value Of Real Variable event as its method of activating other triggers, and passes the event responses through a few globals.

    Before you copy triggers that use GDD into a new map, you need to copy over GDD with its GDD Variable Creator trigger, or the variables won't be automatically created correctly.
    If you pasted GDD-using triggers before pasting GDD, to fix it, change the type of the variable GDD_Event to be a Real in the Trigger Editor's Variables window, and then find the triggers that became disabled and change their event to use the variable GDD_Event.


    -- How To Implement --
    1. Be sure "Automatically create unknown variables while pasting trigger data" is enabled in the World Editor general preferences.
    2. Copy the trigger category "GDD" from the demo map and paste it into your map. (Alternately: create the variables listed in the globals block below, create a trigger named GUI Friendly Damage Detection", and paste in this entire text.)
    3. Create your damage triggers using Game - Value Of Real Variable, select GDD_Event as the variable, and leave the rest of the settings to the default "becomes Equal to 0.00".
    The event responses are the following variables:
    • GDD_Damage is the amount of damage, replacing Event Response - Damage Taken.
    • GDD_DamagedUnit is the damaged unit, replacing Event Response - Triggering Unit.
      • However, Triggering Unit can still be used, if you need to use waits. Read the Notes section below for more info.
    • GDD_DamageSource is the damaging unit, replacing Event Response - Damage Source.

    -- Example Usage --
    Trigger:
    • Display Damage
      • Events
        • Game - GDD_Event becomes Equal to 0.00
      • Conditions
      • Actions
        • Game - Display to (All players) for 1.00 seconds the text: ((((Name of GDD_DamageSource) + damaged ) + (Name of GDD_DamagedUnit)) + ( for + ((String(GDD_Damage)) + damage.)))

    -- System Code --
    JASS:
    // GUI-Friendly Damage Detection -- v1.2.1 -- by Weep
    //    http://www.thehelper.net/forums/showthread.php?t=137957
    //
    //    Requires: only this trigger and its variables.
    //
    // -- What? --
    //    This system provides a leak-free, GUI-friendly implementation of an "any unit takes
    //    damage" event.  It requires no JASS knowledge to use.
    //
    //    It uses the Game - Value Of Real Variable event as its method of activating other
    //    triggers, and passes the event responses through a few globals.
    //
    // -- Why? --
    //    The traditional GUI method of setting up a trigger than runs when any unit is damaged
    //    leaks trigger events.  This system is easy to implement and removes the need to do
    //    you own GUI damage detection setup.
    //
    // -- How To Implement --
    //    0. Before you copy triggers that use GDD into a new map, you need to copy over GDD
    //       with its GDD Variable Creator trigger, or there will be a problem: the variables
    //       won't be automatically created correctly.
    //
    //    1. Be sure "Automatically create unknown variables while pasting trigger data" is
    //       enabled in the World Editor general preferences.
    //    2. Copy this trigger category ("GDD") and paste it into your map.
    //       (Alternately: create the variables listed in the globals block below, create a
    //       trigger named "GUI Friendly Damage Detection", and paste in this entire text.)
    //    3. Create your damage triggers using Game - Value Of Real Variable as the event,
    //       select GDD_Event as the variable, and leave the rest of the settings to the default
    //       "becomes Equal to 0.00".
    //       The event responses are the following variables:
    //          GDD_Damage is the amount of damage, replacing Event Response - Damage Taken.
    //          GDD_DamagedUnit is the damaged unit, replacing Event Response - Triggering Unit.
    //              Triggering Unit may still be used, if you need to use waits.
    //              Read the -- Notes -- section below for more info.
    //          GDD_DamageSource is the damaging unit, replacing Event Response - Damage Source.
    //
    // -- Notes --
    //    GDD's event response variables are not wait-safe; you can't use them after a wait in
    //    a trigger.  If you need to use waits, Triggering Unit (a.k.a. GetTriggerUnit()) can
    //    be used in place of GDD_DamageSource.  There is no usable wait-safe equivalent to
    //    Event Damage or Damage Source; you'll need to save the values yourself.
    //
    //    Don't write any values to the variables used as the event responses, or it will mess
    //    up any other triggers using this system for their triggering.  Only use their values.
    //
    //    This uses arrays, so can detect damage for a maximum of 8190 units at a time, and
    //    cleans up data at a rate of 33.33 per second, by default.  This should be enough for
    //    most maps, but if you want to change the rate, change the value returned in the
    //    GDD_RecycleRate function at the top of the code, below.
    //
    //    By default, GDD will not register units that have Locust at the moment of their
    //    entering the game, and will not recognize when they take damage (which can only
    //    happen if the Locust ability is later removed from the unit.)  To allow a unit to have
    //    Locust yet still cause GDD damage events if Locust is removed, you can either design
    //    the unit to not have Locust by default and add it via triggers after creation, or
    //    edit the GDD_Filter function at the top of the code, below.
    //
    // -- Credits --
    //    Captain Griffin on wc3c.net for the research and concept of GroupRefresh.
    //
    //    Credit in your map not needed, but please include this README.
    //
    // -- Version History --
    //    1.2.1: Minor code cleaning.  Added configuration functions.  Updated documentation.
    //    1.2.0: Made this system work properly with recursive damage.
    //    1.1.1: Added a check in order to not index units with the Locust ability (dummy units).
    //           If you wish to check for damage taken by a unit that is unselectable, do not
    //           give the unit-type Locust in the object editor; instead, add the Locust ability
    //           'Aloc' via a trigger after its creation, then remove it.
    //    1.1.0: Added a check in case a unit gets moved out of the map and back.
    //    1.0.0: First release.
    
    
    //===================================================================
    // Configurables.
    function GDD_RecycleRate takes nothing returns real //The rate at which the system checks units to see if they've been removed from the game
        return 0.03
    endfunction
    
    function GDD_Filter takes unit u returns boolean //The condition a unit has to pass to have it registered for damage detection
        return GetUnitAbilityLevel(u, 'Aloc') == 0 //By default, the system ignores Locust units, because they normally can't take damage anyway
    endfunction
    
    //===================================================================
    // This is just for reference.
    // If you use JassHelper, you could uncomment this section instead of creating the variables in the trigger editor.
    
    // globals
    //  real udg_GDD_Event = 0.
    //  real udg_GDD_Damage = 0.
    //  unit udg_GDD_DamagedUnit
    //  unit udg_GDD_DamageSource
    //  trigger array udg_GDD__TriggerArray
    //  integer array udg_GDD__Integers
    //  unit array udg_GDD__UnitArray
    //  group udg_GDD__LeftMapGroup = CreateGroup()
    // endglobals
    
    //===================================================================
    // System code follows.  Don't touch!
    function GDD_Event takes nothing returns boolean
        local unit damagedcache = udg_GDD_DamagedUnit
        local unit damagingcache = udg_GDD_DamageSource
        local real damagecache = udg_GDD_Damage
        set udg_GDD_DamagedUnit = GetTriggerUnit()
        set udg_GDD_DamageSource = GetEventDamageSource()
        set udg_GDD_Damage = GetEventDamage()
        set udg_GDD_Event = 1.
        set udg_GDD_Event = 0.
        set udg_GDD_DamagedUnit = damagedcache
        set udg_GDD_DamageSource = damagingcache
        set udg_GDD_Damage = damagecache
        set damagedcache = null
        set damagingcache = null
        return false
    endfunction
    
    function GDD_AddDetection takes nothing returns boolean
    //  if(udg_GDD__Integers[0] > 8190) then
    //      call BJDebugMsg("GDD: Too many damage events!  Decrease number of units present in the map or increase recycle rate.")
    //      ***Recycle rate is specified in the GDD_RecycleRate function at the top of the code.  Smaller is faster.***
    //      return
    //  endif
        if(IsUnitInGroup(GetFilterUnit(), udg_GDD__LeftMapGroup)) then
            call GroupRemoveUnit(udg_GDD__LeftMapGroup, GetFilterUnit())
        elseif(GDD_Filter(GetFilterUnit())) then
            set udg_GDD__Integers[0] = udg_GDD__Integers[0]+1
            set udg_GDD__UnitArray[udg_GDD__Integers[0]] = GetFilterUnit()
            set udg_GDD__TriggerArray[udg_GDD__Integers[0]] = CreateTrigger()
            call TriggerRegisterUnitEvent(udg_GDD__TriggerArray[udg_GDD__Integers[0]], udg_GDD__UnitArray[udg_GDD__Integers[0]], EVENT_UNIT_DAMAGED)
            call TriggerAddCondition(udg_GDD__TriggerArray[udg_GDD__Integers[0]], Condition(function GDD_Event))
        endif
        return false
    endfunction
    
    function GDD_PreplacedDetection takes nothing returns nothing
        local group g = CreateGroup()
        local integer i = 0
        loop
            call GroupEnumUnitsOfPlayer(g, Player(i), Condition(function GDD_AddDetection))
            set i = i+1
            exitwhen i == bj_MAX_PLAYER_SLOTS
        endloop
        call DestroyGroup(g)
        set g = null
    endfunction
    
    function GDD_GroupRefresh takes nothing returns nothing
    // Based on GroupRefresh by Captain Griffen on wc3c.net
        if (bj_slotControlUsed[5063] == true) then
            call GroupClear(udg_GDD__LeftMapGroup)
            set bj_slotControlUsed[5063] = false
        endif
        call GroupAddUnit(udg_GDD__LeftMapGroup, GetEnumUnit())
    endfunction
    
    function GDD_Recycle takes nothing returns nothing
        if(udg_GDD__Integers[0] <= 0) then
            return
        elseif(udg_GDD__Integers[1] <= 0) then
            set udg_GDD__Integers[1] = udg_GDD__Integers[0]
        endif
        if(GetUnitTypeId(udg_GDD__UnitArray[udg_GDD__Integers[1]]) == 0) then
            call DestroyTrigger(udg_GDD__TriggerArray[udg_GDD__Integers[1]])
            set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = null
            set udg_GDD__TriggerArray[udg_GDD__Integers[1]] = udg_GDD__TriggerArray[udg_GDD__Integers[0]]
            set udg_GDD__UnitArray[udg_GDD__Integers[1]] = udg_GDD__UnitArray[udg_GDD__Integers[0]]
            set udg_GDD__UnitArray[udg_GDD__Integers[0]] = null
            set udg_GDD__Integers[0] = udg_GDD__Integers[0]-1
        endif
        set udg_GDD__Integers[1] = udg_GDD__Integers[1]-1
    endfunction
    
    function GDD_LeaveMap takes nothing returns boolean
        local boolean cached = bj_slotControlUsed[5063]
        if(udg_GDD__Integers[2] < 64) then
            set udg_GDD__Integers[2] = udg_GDD__Integers[2]+1
        else
            set bj_slotControlUsed[5063] = true
            call ForGroup(udg_GDD__LeftMapGroup, function GDD_GroupRefresh)
            set udg_GDD__Integers[2] = 0
        endif
        call GroupAddUnit(udg_GDD__LeftMapGroup, GetFilterUnit())
        set bj_slotControlUsed[5063] = cached
        return false
    endfunction
    
    // ===========================================================================
    function InitTrig_GUI_Friendly_Damage_Detection takes nothing returns nothing
        local region r = CreateRegion()
        call RegionAddRect(r, GetWorldBounds())
        call TriggerRegisterEnterRegion(CreateTrigger(), r, Condition(function GDD_AddDetection))
        call TriggerRegisterLeaveRegion(CreateTrigger(), r, Condition(function GDD_LeaveMap))
        call GDD_PreplacedDetection()
        call TimerStart(CreateTimer(), GDD_RecycleRate(), true, function GDD_Recycle)
        set r = null
    endfunction

    -- Notes --
    GDD's event response variables are not wait-safe; you can't use them after a wait in a trigger. If you need to use waits, Triggering Unit (a.k.a. GetTriggerUnit()) can be used in place of GDD_DamageSource. There is no usable wait-safe equivalent to GDD_Damage or GDD_DamageSource; you'll need to save the values yourself.

    A common problem when using any damage detection system occurs when you try to cause the damage source to damage the damaged unit from a damage event - for example, if triggering bonus damage. This causes the unit to repeatedly damage the target, because dealing damage in the trigger causes that trigger to run again, and again in turn, infinitely. One solution is to add the action Turn off (This trigger) before dealing damage and add the action Turn on (This trigger) afterward.

    Don't write any values to the variables used as the event responses, or it will mess up any other triggers using this system for their triggering. Only use their values.

    This uses arrays, so can detect damage for a maximum of 8190 units at a time, and cleans up data at a rate of 33.33 per second, by default. This should be enough for most maps, but if you want to change the rate, change the value returned in the GDD_RecycleRate function at the top of the code.

    By default, GDD will not register units that have Locust at the moment of their entering the game, and will not recognize when they take damage (which can only happen if the Locust ability is later removed from the unit.) To allow a unit to have Locust yet still cause GDD damage events if Locust is removed, you can either design the unit to not have Locust by default and add it via triggers after creation, or edit the GDD_Filter function at the top of the code.

    -- Credits --
    Captain Griffin on wc3c.net for the research and concept of GroupRefresh.

    Credit in your map not needed, but please include the README.

    -- Version History --
    1.2.1: Minor code cleaning. Added configuration functions. Updated documentation.
    1.2.0: Made this system work properly with recursive damage.
    1.1.1: Added a check in order to not index units with the Locust ability (dummy units). If you wish to check for damage taken by a unit that is unselectable, do not give the unit-type Locust in the object editor; instead, add the Locust ability 'Aloc' via a trigger after its creation, then remove it.
    1.1.0: Added a check in case a unit gets moved out of the map and back.
    1.0.0: First release.

    --------------------------------------​

    All comments welcome.
     

    Attached Files:

    • Like Like x 13
  2. Renendaru

    Renendaru (Evol)ution is nothing without love.

    Ratings:
    +309 / 0 / -0
    Is there a point to:
    JASS:
    set udg_GDD_Event = 0.
    set udg_GDD_Event = 1


    At all?

    And, I don't see how your event is all that useful, how do you even use it.

    JASS:
    call RegionAddRect(r, GetWorldBounds()


    ...Could try to use GetPlayableMapRect() or the Bj form of it.
     
  3. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Um, that's how it works. Instead of using hundreds of trigger-generated "unit takes damage" events, a GUI trigger writer can use the event "Game - GDD_Event becomes Equal to 0.00". Thus, this sets that value to 0 when the damage event occurs, then changes it to another value so it can later become equal to 0 again.

    A unit can leave the playable map area and thus would be double-registered if it re-entered. I feel it's pretty justified to use GetWorldBounds if J4L is considering changing AIDS to use it.
     
    • Like Like x 2
  4. Renendaru

    Renendaru (Evol)ution is nothing without love.

    Ratings:
    +309 / 0 / -0
    My question to the first, is why did you first set it to 0, then 1. That doesn't do anything but set it to 1, with an extra line in there.
     
  5. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    It does, when there are other triggers using the event "GDD_Event becomes Equal to 0.00", which how I'm allowing GUI triggers to hook into this system, as I explained twice... -_-
     
  6. Renendaru

    Renendaru (Evol)ution is nothing without love.

    Ratings:
    +309 / 0 / -0
    I suppose, I misread it, but it's a very round-a-bout method. I'm sure you could've used some other method albeit.
     
  7. Sevion

    Sevion The DIY Ninja

    Ratings:
    +423 / 0 / -0
    I actually like the method ;o I've used it a few times, in different contexts.
     
  8. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Experimental arrayless version:

    JASS:
    //globals
    //    real udg_GDD_Event
    //    real udg_GDD_Damage
    //    unit udg_GDD_DamagedUnit
    //    unit udg_GDD_DamageSource
    //endglobals
    
    function GDD_Event takes nothing returns boolean
        set udg_GDD_DamagedUnit = GetTriggerUnit()
        set udg_GDD_DamageSource = GetEventDamageSource()
        set udg_GDD_Damage = GetEventDamage()
        set udg_GDD_Event = 0.
        set udg_GDD_Event = 1.
        return false
    endfunction
    
    function GDD_Detection takes nothing returns nothing
        local unit u = GetFilterUnit()
        local trigger t = CreateTrigger()
        call TriggerRegisterUnitEvent(t, u, EVENT_UNIT_DAMAGED)
        call TriggerAddCondition(t, Condition(function GDD_Event)) 
        loop
            exitwhen GetUnitTypeId(u) == 0
            call TriggerSleepAction(GetRandomReal(8., 12.))
        endloop
        call DestroyTrigger(t)
        set u = null
        set t = null
    endfunction
    
    function GDD_Add_Detection takes nothing returns boolean
        call ExecuteFunc("GDD_Detection")
        return false
    endfunction
    
    function GDD_Preset_Detection takes nothing returns nothing
        local group g = CreateGroup()
        local integer i = 0
        set i = 0
        loop
            call GroupEnumUnitsOfPlayer(g, Player(i), Condition(function GDD_Add_Detection))
            call GroupClear(g)
            set i = i+1
            exitwhen i == bj_MAX_PLAYER_SLOTS
        endloop
        call DestroyGroup(g)
        set g = null
    endfunction
    
    //===========================================================================
    function InitTrig_GUI_Friendly_Damage_Detection takes nothing returns nothing
        local region r = CreateRegion()
        call RegionAddRect(r, GetWorldBounds())
        call TriggerRegisterEnterRegion(CreateTrigger(), r, Condition(function GDD_Add_Detection))
        call GDD_Preset_Detection()
        set udg_GDD_Event = 1.
        set r = null
    endfunction

    Seems to work correctly with similar CPU load and memory usage when pitting 300 riflemen against 300 more riflemen (maybe slightly higher memory usage).
     
  9. quraji

    quraji zap

    Ratings:
    +143 / 0 / -0
    That's pretty interesting. I really like the event to catch the value of the variable...I totally forgot that existed. It's too bad that you have to use "Game - GDD_Event becomes Equal to 0.00" though, that's not very intuitive. Could you import a custom event definition (that just uses that event), like EGUI or whatever?

    Always like seeing your stuff Weep...your Mac oppresses you and brings out your creativity.
     
  10. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    >A unit can leave the playable map area and thus would be double-registered if it re-entered.
    Same for get world bounds.
    Use a group to check... ;)

    Also, yes I'm going to use WorldBounds in AIDS some time soon, and also going to add a check in case it was removed and came back.

    This looks pretty good. The version in the first post works the same way as Damage.

    I don't think I like your arrayless version... =/
    I once had trouble with mass waits in the background of a map...
     
  11. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Interesting note: if I use TriggerExecute() to run other triggers, those triggers can still use GetTriggerUnit(), GetEventDamage(), etc. That might be more "friendly" in terms of making the GUI triggers that would be run by this snippet, but less friendly in terms of adding the event; you'd have to paste a "run trigger" action into yet another trigger, and to merge spells using this snippet, you'd have to group all those actions. In my opinion, no system-type setup should require any modifications to its own code or triggers in order to use it in a map.

    Besides, this pre-saves the event responses to variables, and as we know, triggers should do this anyway since variable lookup is faster than calling a function every time you need the value.

    Ew, modifying the World Editor. Most sites seem to disallow the use of GUI modifications for any resources, and I intend to follow that.

    I'd originally made the event "GDD_Event becomes Greater than 0.00" which is slightly more intuitive (0 does not seem "active"), but then I realized that it would be faster for the user if I used the default setting that appears when you add that event.

    Heh, no, this is more oppression by GUI. I like GUI. I also like JASS, because it's less limited and more hands-on, but I keep returning to GUI when I'm trying to make something in-game like a spell.
    Besides, my Mac doesn't oppress me. It's all Blizzard's fault. :p

    [edit] Simulpost go! :p
    ...darn! I thought moving a unit out of the map would freeze no matter what, but that's not true. Also, Rect(-100000000., -100000000., 100000000., 100000000.) seems to get clamped to the map size. :(

    Hmm. Too bad, it was a lot cleaner, if questionably as efficient. Have any more specific findings to share?
     
  12. quraji

    quraji zap

    Ratings:
    +143 / 0 / -0
    That's fine, but could one do it? I don't know as I haven't really checked out how to do that stuff. Or maybe you can use a boolean variable instead ("Game - GDD_DamageEvent becomes Equal to True")? Just wondering :)
     
  13. Jesus4Lyf

    Jesus4Lyf Good Idea™

    Ratings:
    +394 / 0 / -0
    >Have any more specific findings to share?
    I could post the map.

    It was a long time ago when I wrote it. Very long ago. But across the map, for every unit in the map (in this case, wards) I would periodically loop every second. Eventually the map... stopped, nearly. It just blew up in lag - unit orders started to respond highly delayed and such. It was strange.

    Needless to say, I abandoned that project.

    (I could do it with timers now, but I'm talking years ago.)

    Edit: Posted the map, it's like... two years old. And wow, I used GC and H2I. ;)
     

    Attached Files:

  14. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Updated with double-register protection (a clever method if I may say so) and group refreshing, just in case. The globals spam increases. :(

    Yes, you could mod the World Editor. No, there's no boolean event.

    Egad. Well, I can't run it even after modifying war3map.j to use GetHandleId, and I can't open it in WE - it gives an error saying no such function InitGameCache. :(

    ...and I can't make heads or tails of what's going on from the .j either.
     
  15. Sevion

    Sevion The DIY Ninja

    Ratings:
    +423 / 0 / -0
    Quraji, you could just have an event:

    Code:
    Unit - Unit Takes Damage
    And the function would be:

    JASS:
    function GDD_UnitDamaged /* or something similar */ takes trigger t returns nothing
        call TriggerRegisterVariableEvent(udg_GDD_DamageEvent, GREATER_THAN, 0.00)
    endfunction


    Freehanded, >_< Might have some errors somewhere.

    But, imo, it'd be easy to add the interface, but not needed. Nice to look at, but not needed ;)
     
  16. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Would it be a desirable addition for this to not-index units that have Locust? It's very rare for a unit that starts with Locust to later become damageable, and you could still achieve the effect by adding then removing Locust on a regular unit via trigger.

    Added the aforementioned change to not index units that have Locust.

    Updated to use fewer globals, and handle recursive damage.
     
    • Like Like x 1
  17. Joccaren

    Joccaren You can change this now in User CP.

    Ratings:
    +54 / 0 / -0
    Thank you Weep!!!!

    This works perfectly for me, thanks for making this easy for GUI users and require no programs other than WE. +rep
     
  18. Sim

    Sim Forum Administrator Staff Member

    Ratings:
    +531 / 0 / -0
    Approved!
     
  19. Weep

    Weep Godspeed to the sound of the pounding

    Ratings:
    +400 / 0 / -0
    Awesome, thanks Daxtreme! :)
     
  20. unpro

    unpro New Member

    Ratings:
    +1 / 0 / -0
    Used it in my map. No JASS knowledge required whatsoever. Just a simple copy and paste of a folder of 2 triggers and voila!, you can detect damage with a simple event and flexible conditions! Thank you Weep for making this option available for GUI users like me =)
     

Share This Page