Snippet GetNearestUnit

Dirac

22710180
Reaction score
147
The fastest, easiest and with more flexible way to retrieve the nearest unit from a point

JASS:
library GetNearestUnit /* v1.1.1

*/uses/*
*/  MergeSort           /*  thehelper.net/forums/showthread.php/168621-MergeSort
*/  LinkedListModule    /*  thehelper.net/forums/showthread.php/168775-LinkedListModule

***********************************************************************
*
*   function GetNearestUnit takes real x, real y, boolexpr filter returns unit
*   function GetFarthestUnit takes real x, real y, boolexpr filter returns unit
*
*   function GetNearestUnitEx takes real x, real y, real range, boolexpr filter, integer i returns unit
*   function GetFarthestUnitEx takes real x, real y, real range, boolexpr filter, integer i returns unit
*       -   The extra integer argument skips that many units in the
*       -   list of near units.
*       -   Ex: If takes "2" then it would return the second nearest
*       -   unit.
*
***********************************************************************
*
*   HOW TO USE:
*
*   function SomeFilter takes nothing returns boolean
*       return GetUnitTypeId(GetFilterUnit())=='hfoo'
*   endfunction
*
*   call GetNearestUnit(0,0,Filter(function SomeFilter)
*
*   -   In the example above the script would only pick the closest
*   -   footman from the point 0,0
*
**********************************************************************/

    globals
        //*********************************************************************
        //  Default circumference pick range for non extended functions.
        private constant real DEFAULT_RANGE = 2000
    endglobals

    private struct List extends array
        implement LinkedList
        
        unit unit
        real distance
        
        //! runtextmacro MERGE_SORT("sort","v1.distance>v2.distance")
        
        static method for takes real x, real y, real r, boolexpr c returns nothing
            local List node
            local unit e
            call GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c)
            loop
                set e = FirstOfGroup(bj_lastCreatedGroup)
                exitwhen e==null
                set node = List.allocate()
                set node.unit = e
                set node.distance = (GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)
                call base.insertNode(node)
                call GroupRemoveUnit(bj_lastCreatedGroup,e)
            endloop
            call sort(base)
            set e = null
        endmethod
        
    endstruct

    function GetNearestUnitEx takes real x, real y, real r, boolexpr c, integer i returns unit
        local List this = List.base
        call List.for(x,y,r,c)
        loop
            set this = this.next
            set i = i-1
            exitwhen i==0 or this.head
        endloop
        call List.base.clearNode()
        return this.unit
    endfunction
    
    function GetFarthestUnitEx takes real x, real y, real r, boolexpr c, integer i returns unit
        local List this = List.base
        call List.for(x,y,r,c)
        loop
            set this = this.prev
            set i = i-1
            exitwhen i==0 or this.head
        endloop
        call List.base.clearNode()
        return this.unit
    endfunction
    
    function GetNearestUnit takes real x, real y, boolexpr c returns unit
        return GetNearestUnitEx(x,y,DEFAULT_RANGE,c,1)
    endfunction
    
    function GetFarthestUnit takes real x, real y, boolexpr c returns unit
        return GetFarthestUnitEx(x,y,DEFAULT_RANGE,c,1)
    endfunction

endlibrary
 

Laiev

Hey Listen!!
Reaction score
188
Please, do this:
JASS:
library GetNearestUnit /* v1.1.0

*/uses/*
*/  MergeSort           /*  thehelper.net/forums/showthread.php/168621-MergeSort
*/  LinkedListModule    /*  thehelper.net/forums/showthread.php/168775-LinkedListModule

***********************************************************************
*
*   function GetNearestUnit takes real x, real y, boolexpr filter returns unit
*   function GetFarthestUnit takes real x, real y, boolexpr filter returns unit
*
*   function GetNearestUnitEx takes real x, real y, real r, boolexpr filter, integer i returns unit
*   function GetFarthestUnitEx takes real x, real y, real r, boolexpr filter, integer i returns unit
*       -   The extra integer argument skips that many units in the
*       -   list of near units.
*       -   Ex: If takes "2" then it would return the second nearest
*       -   unit.
*       -   The extra real argument is the range to pick unit
*       -   return null if no unit is picked.
***********************************************************************
*
*   HOW TO USE:
*
*   function SomeFilter takes nothing returns boolean
*       return GetUnitTypeId(GetFilterUnit())=='hfoo'
*   endfunction
*
*   call GetNearestUnit(0,0,Filter(function SomeFilter)
*
*   -   In the example above the script would only pick the closest
*   -   footman from the point 0,0
*
**********************************************************************/

    globals
        //*********************************************************************
        //  Will not be used on Ex's functions
        private constant real DEFAULT_RANGE = 99999.
    endglobals

    private struct List extends array
        implement LinkedList
        
        unit unit
        real distance
        
        //! runtextmacro MERGE_SORT("sort","v1.distance>v2.distance")
        
        static method for takes real x, real y, real r, boolexpr c returns thistype
            local List this = List.createNode()
            local List node
            local unit e
            call GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c)
            loop
                set e = FirstOfGroup(bj_lastCreatedGroup)
                exitwhen e==null
                set node = List.allocate()
                set node.unit = e
                set node.distance = (GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)
                call this.insertNode(node)
                call GroupRemoveUnit(bj_lastCreatedGroup,e)
            endloop
            call sort(this)
            return this
        endmethod
        
    endstruct

    function GetNearestUnitEx takes real x, real y, real r, boolexpr c, integer i returns unit
        local List this = List.for(x,y,r,c)
        local unit e
        loop
            set this = this.next
            set i = i-1
            exitwhen i==0 or this.head
        endloop
        set e = this.unit
        call this.flushNode()
        return e
    endfunction
    
    function GetFarthestUnitEx takes real x, real y, real r, boolexpr c, integer i returns unit
        local List this = List.for(x,y,r,c)
        local unit e
        loop
            set this = this.prev
            set i = i-1
            exitwhen i==0 or this.head
        endloop
        set e = this.unit
        call this.flushNode()
        return e
    endfunction
    
    function GetNearestUnit takes real x, real y, boolexpr c returns unit
        local List this = List.for(x,y,DEFAULT_RANGE,c)
        local unit e = this.next.unit
        call this.flushNode()
        return e
    endfunction
    
    function GetFarthestUnit takes real x, real y, boolexpr c returns unit
        local List this = List.for(x,y,DEFAULT_RANGE,c)
        local unit e = this.prev.unit
        call this.flushNode()
        return e
    endfunction

endlibrary
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Why would that be any faster than a function that inlines: the grouping, then iterates over every unit and calculates the distance and thus finds the closes/farthest unit? Function calls take time, and ones with arguments take even more time =), if speed is what you're aming for.
 

muzk

Member
Reaction score
3
Why not just:
JASS:
globals
    private constant real MAX_DIST_POW = 99999999
endglobals

function GetNearestUnit takes real x, real y,real r, boolexpr c returns unit
    local unit u
    local unit e
    local real du=MAX_DIST_POW
    local real de
    call GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c)
    loop
        set e=FirstOfGroup(bj_lastCreatedGroup)
        exitwhen e==null
        set de=(GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)
        if de<du then
            set u=e
            set du=d
        endif
        call GroupRemoveUnit(bj_lastCreatedGroup,e)
    endloop
    return u
endfunction
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
there should be another way to find the closest unit.
You don't save anything from "speed" of code.
GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c) forces to get units from a certain area, which searches for all units and then puts them into a group and then you search for every unit of them to find the closest.

An A.I.D.S. like group enumeration and then a search through every single unit is faster. You save an extra search.

Although there is a graphs node solution with Dijkstra algorithm but I don't know if it's faster.
 

Dirac

22710180
Reaction score
147
there should be another way to find the closest unit.
You don't save anything from "speed" of code.
GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c) forces to get units from a certain area, which searches for all units and then puts them into a group and then you search for every unit of them to find the closest.

An A.I.D.S. like group enumeration and then a search through every single unit is faster. You save an extra search.

Although there is a graphs node solution with Dijkstra algorithm but I don't know if it's faster.
I already conducted this using Nestharus's Unit List and it's by far slower.
The native is the best way to pick units.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
>I already conducted this using Nestharus's Unit List and it's by far slower.
The native is the best way to pick units.

I agree.

> which searches for all units and then puts them into a group
How do you know that? You've seen the code?

Even if that's the case it is still native code (C++ probably) vs interpreted (Jass2 probably) =).
 

NoobImbaPro

You can change this now in User CP.
Reaction score
60
Yes i've seen it, but you may be right. the coding is in C, which is really faster than jass2.
The native takes all units in game and checks their range, if it's the range you've put or lower the unit goes in group. The first unit must be the closest one i think, check it.
 

muzk

Member
Reaction score
3
Why not just:
JASS:
globals
    private constant real MAX_DIST_POW = 99999999
endglobals

function GetNearestUnit takes real x, real y,real r, boolexpr c returns unit
    local unit u
    local unit e
    local real du=MAX_DIST_POW
    local real de
    call GroupEnumUnitsInRange(bj_lastCreatedGroup,x,y,r,c)
    loop
        set e=FirstOfGroup(bj_lastCreatedGroup)
        exitwhen e==null
        set de=(GetUnitX(e)-x)*(GetUnitX(e)-x)+(GetUnitY(e)-y)*(GetUnitY(e)-y)
        if de<du then
            set u=e
            set du=d
        endif
        call GroupRemoveUnit(bj_lastCreatedGroup,e)
    endloop
    return u
endfunction

?
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
@muzik
Yes this is basically the fastest way to do it (That I know of), although you are forgetting to null the e variable so it actually leaks [not a big deal though =)]

Well, actually the fastest way to do it, that I know of would be this:

JASS:
globals
    group G0 = CreateGroup()
    unit  U0 = null
    unit  U1 = null
    real  R0
    real  R1
    real  R2
    real  R3
endglobals

//! textmacro CLOSEST_UNIT takes x, y, range, filter, returns_unit
    set R0 = 9999999
    call GroupEnumUnitsInRange(G0, $x$, $y$, $range$, $filter$)

    loop
        set U0 = FirstOfGroup(G0)
        exitwhen U1 == null
        
        set R1 = $x$ - GetUnitX(U0)
        set R2 = $y$ - GetUnitY(U0)
        set R3 = R1 * R1 + R2 * R2

        if R0 > R3 then
            set R0 = R3
            set U1 = U0
        endif

        call GroupRemoveUnit(G0, U0)
    endloop
            
    set $returns_unit$ = U1
//! endtextmacro


I.e to inline even the call to the function =):

An example use might be:
JASS:
local unit u
//! runtextmacro CLOSEST_UNIT("GetUnitX(Hero[0]), "GetUnitY(Hero[0]", "RANGE", "null", "u")
call KillUnit(u) // actually kills Hero[0] because the filter is null =)
 

luorax

Invasion in Duskwood
Reaction score
67
@muzik
Yes this is basically the fastest way to do it (That I know of), although you are forgetting to null the e variable so it actually leaks [not a big deal though =)]

"u" leaks as well :p That's why using a global variable to return the nearest unit is better.
 

muzk

Member
Reaction score
3
Oh yeah, I forgot about "u" leak, using a global may solve it.
Not necesary null "e", because loop condition is exitwhen e==null, so e is null when loop ends.
 

Dirac

22710180
Reaction score
147
I just went all full retard when i realize there was no need for that local, or a globals to replace it. The List.unit array's value isn't lost when the list is flushed, mild brain blackout I had there.

Update v1.1.1
-Improved Code efficiency

I'm sticking with mergesort because I really would like to be able to know the 2nd, 3rd or any unit in the list.
The speed difference is so minimal it's not even worth the debate
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
>I'm sticking with mergesort because I really would like to be able to know the 2nd, 3rd or any unit in the list.

If you don't mind can you give me an example where based on the distance the 2nd, 3rd, etc. unit is actually needed?


>The speed difference is so minimal it's not even worth the debate
=) Well... I did a little benchmark 1000 calls for all and the results were (same arguments were passed for all cases of course):

Dirac's: time = 0.872
muzk's: time = 0.178
inlined: time = 0.134 (inlined using textmacro and only two calls to the GetUnitX/Y natives)

So no offense but yours is actually 5 to 6 times slower, but you are probably right it's not worth the debate =).
 

Dirac

22710180
Reaction score
147
If you don't mind can you give me an example where based on the distance the 2nd, 3rd, etc. unit is actually needed?
Use your imagination, maybe an ability that deals decaying AoE damage to nearby units, starting by 300 to the closest, 270 for the second, 240 for the third and so on.
 

luorax

Invasion in Duskwood
Reaction score
67
An instant casted Chain Lightning which jumps thorugh each unit, from the closest to the farthest.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Monovertex Monovertex:
    How are you all? :D
    +1
  • Ghan Ghan:
    Howdy
  • 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

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top