Snippet UnitInRangeOfType

Sevion

The DIY Ninja
Reaction score
424
UnitInRangeOfType

Version 1.03​

Requirements
- JASS NewGen
- Event
- AIDS
- Table

JASS:
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//~~ UnitInRangeOfType ~~ By Sevion ~~ Version 1.03 ~~
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//  What is UnitInRangeOfType?
//         - UnitInRangeOfType implements an intuitive method of registering InRange events for unit-types.
//
//    =Pros=
//         - Blizzard-like Event Registration.
//         - Very intuitive to use.
//
//    =Cons=
//         - Fairly inefficient at the moment, but this isn't designed for heavy use right now anyhow.
//
//    Functions:
//         - TriggerRegisterUnitInRangeOfType( trigger whichTrigger, integer whichType, real range, boolexpr filter )
//
//           This function is used exactly as other event registration functions are. Returns an Event.
//
//         - GetCenterUnit()
//
//         - This function is used just as GetEnteringUnit is used. It's for retrieving the unit that was approached.
//           This can be used in the filter function as well.
//
//  Details:
//         - UnitInRangeOfType is used just as other Blizzard event registration functions are.
//
//         - Very intuitive to use because of this fact.
//
//         - I am still in the process of optimization, which should come in later updates.
//
//  How to import:
//         - Create a trigger named UnitInRangeOfType.
//         - Convert it to custom text and replace the whole trigger text with this.
//
//  Thanks:
//         - Bribe for some optimization suggestions.
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
library UnitInRangeOfType requires AIDS, Event, Table
    globals
        private key EVENT_KEY
        private key TRIGGER_KEY
        private key RANGE_KEY
        private key TYPE_KEY
        private key UNIT_KEY
        private key DESTROY_KEY
    endglobals
    
    private struct data extends array
        readonly static trigger filterCheck
        public static hashtable hash
        readonly static unit lastcenter
        public static Table idlist
        
        private static method AIDS_filter takes unit u returns boolean
            return thistype.idlist.has(GetUnitTypeId(u))
        endmethod
        
        private static method OnEvent takes nothing returns boolean
            local unit u
            
            set u = thistype.lastcenter
            set thistype.lastcenter = LoadUnitHandle(thistype.hash, GetHandleId(GetTriggeringTrigger()), UNIT_KEY)
            
            if ( TriggerEvaluate(data.filterCheck) ) then
                call FireEvent(Event(LoadInteger(data.hash, LoadInteger(thistype.hash, GetHandleId(GetTriggeringTrigger()), TYPE_KEY), EVENT_KEY)))
            else
                set thistype.lastcenter = u
            endif
            
            set u = null
            return false
        endmethod
        
        private method AIDS_onCreate takes nothing returns nothing
            local trigger t = CreateTrigger()
            local integer i = GetUnitTypeId(this.unit)
            
            call TriggerRegisterUnitInRange(t, this.unit, LoadReal(thistype.hash, i, RANGE_KEY), null)
            call TriggerAddCondition(t, Condition(function thistype.OnEvent))
            call SaveInteger(thistype.hash, GetHandleId(t), TYPE_KEY, i)
            call SaveUnitHandle(thistype.hash, GetHandleId(t), UNIT_KEY, this.unit)
            call SaveTriggerHandle(thistype.hash, i, DESTROY_KEY, t)
            
            set t = null
        endmethod
        
        private static method AIDS_onInit takes nothing returns nothing
            set thistype.hash = InitHashtable()
            set thistype.lastcenter = null
            set thistype.filterCheck = CreateTrigger()
            set thistype.idlist = Table.create()
        endmethod
        
        private method AIDS_onDestroy takes nothing returns nothing
            call DestroyTrigger(LoadTriggerHandle(thistype.hash, GetHandleId(this.unit), DESTROY_KEY))
        endmethod
        
        //! runtextmacro AIDS()
    endstruct
    
    function GetCenterUnit takes nothing returns unit
        return data.lastcenter
    endfunction
    
    function TriggerRegisterUnitInRangeOfType takes trigger whichTrigger, integer whichType, real range, boolexpr filter returns Event
        local Event e
        
        if ( HaveSavedInteger(data.hash, whichType, EVENT_KEY) ) then
            set e = LoadInteger(data.hash, whichType, EVENT_KEY)
        else
            set e = Event.create()
            call SaveInteger(data.hash, whichType, EVENT_KEY, e)
        endif
        
        call TriggerRegisterEvent(whichTrigger, e)
        
        set data.idlist[whichType] = 1
        call SaveTriggerHandle(data.hash, whichType, TRIGGER_KEY, whichTrigger)
        call SaveReal(data.hash, whichType, RANGE_KEY, range)
        
        call TriggerAddCondition(data.filterCheck, filter)
        
        return e
    endfunction
endlibrary


Demonstration
JASS:
struct a extends array
    private static unit u
    implement Alloc
    
    private static method filt2 takes nothing returns boolean
        call BJDebugMsg("Filter 2")
        return true
    endmethod
    
    private static method filt takes nothing returns boolean
        call BJDebugMsg("Filter")
        return true
    endmethod
    
    private static method act takes nothing returns nothing
        call BJDebugMsg("Act")
        call KillUnit(GetEnteringUnit()) // Kills the Paladin
        //call KillUnit(GetCenterUnit()) // Kills the Footman
    endmethod
    
    private static method onInit takes nothing returns nothing
        local trigger t = CreateTrigger()
        set thistype.u = CreateUnit(Player(0), 'hfoo', 0, 0, 0)
        call CreateUnit(Player(0), 'Hpal', 550, 0, 0)
        call CreateUnit(Player(0), 'Hpal', -550, 0, 0)
        call TriggerRegisterUnitInRangeOfType(t, 'hfoo', 500, Condition(function thistype.filt))
        call TriggerRegisterUnitInRangeOfType(t, 'hfoo', 500, Condition(function thistype.filt2))
        call TriggerAddAction(t, function thistype.act)
    endmethod
endstruct

Still needs optimization work, but works nonetheless :thumbs_up:

Updates
- Version 1.03: Removed old code that was causing syntax errors.
- Version 1.02: Removed idlist, filterlist, and Alloc. Added Table and a static trigger to replace them.
- Version 1.01: Moved [ljass]//! runtextmacro AIDS()[/ljass] to the bottom of the struct. Nulled some things.
- Version 1.00: Release.
 

Dirac

22710180
Reaction score
147
This sounds very useful, you should mention exactly how it works, what i can deduct from the demostration you made is that the event will register any unit that enters the radius of another unit, GetCenterUnit returns the unit the event was created for and GetEnteringUnit returns the unit entering it's radius. Again this sounds like very useful.

Some notes:
-[ljass]//! runtextmacro AIDS()[/ljass] goes at the end of the struct
-Alloc should be optional
-Only works with newest versions of W3 that allows hashtables.
 

Sevion

The DIY Ninja
Reaction score
424
The way it works is that on AIDS_onCreate it simply creates an AIDS instance of this struct for all units that have been set to be registered with the user function and the Filter looks through the idlist struct (I think this might be able to be made more efficient). It makes a trigger for that unit that fires on InRange. That trigger has an action called OnEvent which will use a trigger to evaluate the given filters (which are stored and accessed the same way as the idlist struct) and if one of them is true, it will fire the original action given.

I always forget that AIDS macro goes at the bottom. I just never use AIDS lol.

Alloc is required because it's possible that this system will be put under heavy use. Besides, Alloc simply allows this to be more efficient.

Using the latest version of WarCraft 3 should be a given.
 

Sevion

The DIY Ninja
Reaction score
424
Updated: Version 1.02: Removed idlist, filterlist, and Alloc. Added Table and a static trigger to replace them.
 

Sevion

The DIY Ninja
Reaction score
424
Updated the code. Old code had remaining code that was causing a syntax error.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
I think you need to update this resource:

JASS:
        private method AIDS_onDestroy takes nothing returns nothing
            //this line does nothing because you attached the trigger to the type_id(i variable) not the unit
            // i.e this leak triggers 
            call DestroyTrigger(LoadTriggerHandle(thistype.hash, GetHandleId(this.unit), DESTROY_KEY))
        endmethod


In the TriggerRegisterUnitInRangeOfType function:

JASS:
        // this line does nothing as well, that also means that the TRIGGER_KEY is useless
        call SaveTriggerHandle(data.hash, whichType, TRIGGER_KEY, whichTrigger)



Edit:

In the demonstration:

If one of the filters (filt, filt2) returns false then the act method is never called. But it should
because there are two different TriggerRegisterUnitInRangeOfType calls that just happen to share
the same action function/static method.


Edit2:
Okay, here's my attempt with this:

JASS:
library UnitInRangeOfType requires AIDS
    globals
        private key DUMMY_TRIGGER_KEY
        private key TRIGGER_DATA_KEY
        private key CENTER_UNIT_KEY
        private key DESTROY_KEY
        private key TRIGGER_COUNT_KEY
        
        private hashtable HT = InitHashtable()
    endglobals
    
    private struct triggerdata
        trigger  user_trigger
        integer  unit_type_id
        real     range
        boolexpr user_boolexpr

        trigger  dummy_trigger
        boolexpr user_boolexpr_and_run_user_code
        
        method clear takes nothing returns nothing
            call DestroyTrigger(dummy_trigger)
            call DestroyBoolExpr(user_boolexpr_and_run_user_code)
            call destroy()
        endmethod
    endstruct
    
    private struct UnitInRangeOfType extends array

        private static method run_user_code takes nothing returns boolean
            // if this function/static method is executing, this means that the user's filter/boolexpr has returned true
            // or that the user passed null for the boolexpr parameter
            local triggerdata td = LoadInteger(HT, GetHandleId(GetTriggeringTrigger()), DUMMY_TRIGGER_KEY)
            
            // run user code
            if IsTriggerEnabled(td.user_trigger) then
                if TriggerEvaluate(td.user_trigger) then
                    call TriggerExecute(td.user_trigger)
                endif
            endif
            
            return false
        endmethod
        
        private method AIDS_onCreate takes nothing returns nothing
            local triggerdata td      = 0
            local triggerdata td2     = 0
            local integer     type_id = GetUnitTypeId(this.unit)
            local integer     i       = 0
            local trigger     t       = null
            local integer     tc      = LoadInteger(HT, type_id, TRIGGER_COUNT_KEY)
            
            loop
                exitwhen i >= tc

                set t  = LoadTriggerHandle(HT, type_id, i)
                set td = LoadInteger(HT, GetHandleId(t), TRIGGER_DATA_KEY)
                
                if IsTriggerEnabled(t) then
                    set td2 = triggerdata.create()
                    set td2.user_trigger  = t
                    set td2.dummy_trigger = CreateTrigger()
                    
                    if td.user_boolexpr != null then
                        set td2.user_boolexpr_and_run_user_code = And(td.user_boolexpr, Condition(function UnitInRangeOfType.run_user_code))
                    else
                        set td2.user_boolexpr_and_run_user_code = Condition(function UnitInRangeOfType.run_user_code)
                    endif
                    
                    call TriggerRegisterUnitInRange(td2.dummy_trigger, this.unit, td.range, null)
                    call TriggerAddCondition(td2.dummy_trigger, td2.user_boolexpr_and_run_user_code)

                    call SaveInteger(HT, GetHandleId(td2.dummy_trigger), DUMMY_TRIGGER_KEY, td2)

                    call SaveUnitHandle(HT, GetHandleId(td2.dummy_trigger), CENTER_UNIT_KEY, this.unit)
        
                    call SaveInteger(HT, GetHandleId(this.unit), DESTROY_KEY, td2)
                endif
                
                set i = i + 1
            endloop
        endmethod
        
        private method AIDS_onDestroy takes nothing returns nothing
            call triggerdata(LoadInteger(HT, GetHandleId(this.unit), DESTROY_KEY)).clear()
        endmethod

        //! runtextmacro AIDS()
    endstruct
    
    function GetCenterUnit takes nothing returns unit
        return LoadUnitHandle(HT, GetHandleId(GetTriggeringTrigger()), CENTER_UNIT_KEY)
    endfunction
    
    function TriggerRegisterUnitInRangeOfType takes trigger t, integer type_id, real range, boolexpr filter returns nothing
        local triggerdata td = 0

        set td               = triggerdata.create()
        set td.user_trigger  = t
        set td.unit_type_id  = type_id
        set td.range         = range
        set td.user_boolexpr = filter
        call SaveInteger(HT, GetHandleId(t), TRIGGER_DATA_KEY, td)
        
        // type_id.trigger[type_id.trigger_count] = t
        call SaveTriggerHandle(HT, type_id, LoadInteger(HT, type_id, TRIGGER_COUNT_KEY), t)
        // type_id.trigger_count++
        call SaveInteger(HT, type_id, TRIGGER_COUNT_KEY, LoadInteger(HT, type_id, TRIGGER_COUNT_KEY) + 1)

    endfunction
endlibrary
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top