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

Closest unit in unit group.

Status
Not open for further replies.
Level 26
Joined
Aug 18, 2009
Messages
4,097
You iterate through the group, filter out the non-enemies, set the first unit and its distance as initial values and for every other unit compare whether the distance is shorter. If it is, set the new distance as reference value and overwrite the resulting unit.

pseudo-code:

Code:
resultUnit = null

for unit in group do
    if (isEnemy(unit) == false) then
        removeUnit(group)
    endif
endfor

if (firstOf(group) != null) then
    set resultUnit = firstOf(group)

    set distX = GetUnitX(resultUnit) - x
    set distY = GetUnitY(resultUnit) - y

    set lastDistSquare = distX * distX + distY * distY

    for unit in group do
        set distX = GetUnitX(unit) - x
        set distY = GetUnitY(unit) - y

        set distSquare = distX * distX + distY * distY

        if (distSquare < lastDistSquare) then
            lastDistSquare = distSquare
            resultUnit = unit
        endif
    endfor
endif

As you can see, comparing the squares of the distances is enough. It saves one sqrt call per unit.
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Does not have to do with jass and there are several ways in detail to realize it. Just do what I have described:

You want the closest unit in a variable after the algorithm.
Set this variable to "No unit" in the beginning to cover the case that no unit is being picked. Filter out any units that you do not want to be in the group. You can also do this parallel to investigating the distances but then have to set a flag when the first unit was being found because this first unit shall not have its distance compared (because there is none at this point) or you set the starting distance to be greater than the map's size, so the first unit will always be under that threshold. As for the remaining units, you get their distance each and compare it to the previously lowest range. If it's lower, adjust the lowest range to the new value and save the unit this distance belongs to as owner of the lowest range.

Just try it. The algorithm is very simple and intuitive. Jass is legible btw, you only need to understand a bit of English and apply logic thinking.
 
Level 14
Joined
Aug 8, 2010
Messages
1,022
Actually i think the problem is there that i don't understand what must be a variable and what must not be a variable in Jass, so... i don't know. I cant even explain my confusing. At this point i know that only "reesultUnit" is a variable... I don't know if "(firstOf(group)" must be "(firstOf(GroupVariable)" or if "GetUnitX(unit)" must be "GetUnitX(UnitVariable)".

There is a big mess in my head right now. :confused:
 
Level 26
Joined
Aug 18, 2009
Messages
4,097
Ignore this code if you cannot read it. Concentrate on my descriptive remarks.

Yes, you need two variables. One that holds the currently lowest range (real variable) and one that holds the unit with the currently lowest range (unit variable). You could also save this unit only but then would have to potentially retrieve its distance multiple times.
 
Status
Not open for further replies.
Top