Snippet UnitInRangeOfType

Sevion

The DIY Ninja
Reaction score
413
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
413
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
413
Updated: Version 1.02: Removed idlist, filterlist, and Alloc. Added Table and a static trigger to replace them.
 

Sevion

The DIY Ninja
Reaction score
413
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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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