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.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      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