• 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.

Detecting Least Values

Status
Not open for further replies.
Level 9
Joined
Oct 11, 2009
Messages
477
Hello.... I'm just asking how to detect least real/integer values. I mean that for example, I want to add a unit which is the nearest to a point to a unit group.
 
Level 13
Joined
Mar 24, 2010
Messages
950
Just set 2 integer values, 1 to the point of the unit and 1 to the nearest point of a unit in the unitgroup.. not sure how your going to decide where the "point of the unitgroup is since units in a unitgroup can be anywhere"
Then just compare those real int values and then you have your distance you're looking for :)
 

Rui

Rui

Level 41
Joined
Jan 7, 2005
Messages
7,551
Well, you'd have to pick every unit in a certain range of a point (the range preferably not being one that would pick units in the entire map, imagining you have a lot of units...), and calculate the vector that results from a unit's distance to that point. Do you know JASS? If you do, I think it'd work if you did it like this:
JASS:
function Find_Unit_Closest_To_Location takes location WhichLocation returns unit
    local group g=CreateGroup()
    local unit u
    local real v
    local real x
    local real y
    local real newVector=99999 //those are 5 nines
    local unit closestUnit
    call GroupEnumUnitsInRange( g , GetLocationX( WhichLocation ) , GetLocationY( WhichLocation ) , radius, null(?) )
//Pick every unit in a certain radius (distance) around the location.
//If the point is constant, you can use real numbers (coordinates) instead of the GetLocationXY functions, which will also speed up our custom function
        loop
            set u=FirstOfGroup(g)
            exitwhen u==null
            set x = GetUnitX( u ) - GetLocationX( WhichLocation )
            set y = GetUnitY( u ) - GetLocationY( WhichLocation )
            set v = SquareRoot(x * x + y * y) //Calculating vector norm
                if v<newVector then
                    set newVector = v
                    set closestUnit = u
                endif
                //If the calculated distance is bigger than the previous, do nothing.
                //If the calculated distance is smaller, this might be the unit we're looking for.
                //Therefore, let's set this as the closest unit yet.
                //The remaining units can be farther away from the location, in which case they're ignored.
                //If a unit is closer, the previous unit is not the closest, so we discard the old unit.
                //The loop keeps looping until there are no units left.
            call GroupRemoveUnit( g , u )
        endloop
    call DestroyGroup(g)
    set g=null
    return closestUnit
endfunction

I'm sorry, but I never learned how GUI loops worked, so I may need to take a while longer to find how to do it with normal Triggers.
 
Last edited:
Status
Not open for further replies.
Top