• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Solved] In JASS, how to select the nearest unit?

Status
Not open for further replies.
Level 21
Joined
Mar 27, 2012
Messages
3,232
In JASS, how to select the nearest unit to my unit? Thanks a lot.

There is no native for this, so you'll have to do it yourself.
Luckily for you, I have made those functions already.

Note that most of this in here is irrelevant. You can just skip to GetClosestUnitOfGroup
JASS:
//
//                  Extrafunctions
//
//       This library provides functions that extend
//       the editor functionality and/or make things
//       more efficient.
//
//

globals
    integer ExtraInt
    real ExtraReal
    real ExtraX
    real ExtraY
    unit ExtraUnit
endglobals

library ExtraFunctions
    //System stuff that shouldn't be used outside
    function UnitTypeIsInteger takes nothing returns boolean
        return GetUnitTypeId(GetFilterUnit()) == ExtraInt
    endfunction
    
    function ClosestUnitLoop takes nothing returns nothing
        local unit u = GetEnumUnit()
        local real x = GetUnitX(u) - ExtraX
        local real y = GetUnitY(u) - ExtraY
        local real r = SquareRoot(x * x + y * y)
        if r < ExtraReal then
            set ExtraReal = r
            set ExtraUnit = u
        endif
        set u = null
    endfunction
    
    //Actual functions
    function Debug takes string s returns nothing
        call DisplayTextToPlayer(GetLocalPlayer(),0,0,s)
    endfunction
    
    function GetClosestUnitOfGroup takes group g,real x,real y returns unit
        set ExtraReal = 100000
        set ExtraX = x
        set ExtraY = y
        call ForGroup(g, function ClosestUnitLoop)
        return ExtraUnit
    endfunction
    
    function FindClosestUnitOfType takes integer UnitType,real x,real y returns unit
        local group g = CreateGroup()
        local unit u
        set ExtraInt = UnitType
        call GroupEnumUnitsInRange(g,0,0,100000,function UnitTypeIsInteger)
        set u = GetClosestUnitOfGroup(g,x,y)
        //Clearing up
        call DestroyGroup(g)
        set g = null
        return u
    endfunction
    
    function IntPower takes integer number,integer power returns integer
        local integer i = 1
        local integer resultnumber = number
        if power == 0 then
            return 1
        endif
        loop
            exitwhen i == power
            set resultnumber = resultnumber*number
            set i = i + 1
        endloop
        return resultnumber
    endfunction
    
    function AddUnitState takes unit u, unitstate us, real amount returns nothing
        call SetUnitState(u,us,GetUnitState(u,us)+amount)
    endfunction
endlibrary

function InitTrig_ExtraFunctions takes nothing returns nothing
endfunction
 
Level 11
Joined
Oct 11, 2012
Messages
711
There is no native for this, so you'll have to do it yourself.
Luckily for you, I have made those functions already.

Note that most of this in here is irrelevant. You can just skip to GetClosestUnitOfGroup
JASS:
//
//                  Extrafunctions
//
//       This library provides functions that extend
//       the editor functionality and/or make things
//       more efficient.
//
//

globals
    integer ExtraInt
    real ExtraReal
    real ExtraX
    real ExtraY
    unit ExtraUnit
endglobals

library ExtraFunctions
    //System stuff that shouldn't be used outside
    function UnitTypeIsInteger takes nothing returns boolean
        return GetUnitTypeId(GetFilterUnit()) == ExtraInt
    endfunction
    
    function ClosestUnitLoop takes nothing returns nothing
        local unit u = GetEnumUnit()
        local real x = GetUnitX(u) - ExtraX
        local real y = GetUnitY(u) - ExtraY
        local real r = SquareRoot(x * x + y * y)
        if r < ExtraReal then
            set ExtraReal = r
            set ExtraUnit = u
        endif
        set u = null
    endfunction
    
    //Actual functions
    function Debug takes string s returns nothing
        call DisplayTextToPlayer(GetLocalPlayer(),0,0,s)
    endfunction
    
    function GetClosestUnitOfGroup takes group g,real x,real y returns unit
        set ExtraReal = 100000
        set ExtraX = x
        set ExtraY = y
        call ForGroup(g, function ClosestUnitLoop)
        return ExtraUnit
    endfunction
    
    function FindClosestUnitOfType takes integer UnitType,real x,real y returns unit
        local group g = CreateGroup()
        local unit u
        set ExtraInt = UnitType
        call GroupEnumUnitsInRange(g,0,0,100000,function UnitTypeIsInteger)
        set u = GetClosestUnitOfGroup(g,x,y)
        //Clearing up
        call DestroyGroup(g)
        set g = null
        return u
    endfunction
    
    function IntPower takes integer number,integer power returns integer
        local integer i = 1
        local integer resultnumber = number
        if power == 0 then
            return 1
        endif
        loop
            exitwhen i == power
            set resultnumber = resultnumber*number
            set i = i + 1
        endloop
        return resultnumber
    endfunction
    
    function AddUnitState takes unit u, unitstate us, real amount returns nothing
        call SetUnitState(u,us,GetUnitState(u,us)+amount)
    endfunction
endlibrary

function InitTrig_ExtraFunctions takes nothing returns nothing
endfunction


Thank both of you! :)
 
Status
Not open for further replies.
Top