• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[General] Nearest Unit

Status
Not open for further replies.
Level 21
Joined
Mar 27, 2012
Messages
3,232
Nearest is actually not that hard. Just needs some thinking to get straight.

First you enumerate(pick every unit in group) a group of units.
Set a real variable to 999999 or any other high number and a unit variable to "no unit".
Check for each unit in the group(pick every unit in group) if they are closer than the real variable, if so, then set the real to distance between them and your point of reference.
 
Level 6
Joined
Jun 20, 2012
Messages
195
Nearest is actually not that hard. Just needs some thinking to get straight.

First you enumerate(pick every unit in group) a group of units.
Set a real variable to 999999 or any other high number and a unit variable to "no unit".
Check for each unit in the group(pick every unit in group) if they are closer than the real variable, if so, then set the real to distance between them and your point of reference.

Trying this one right now..:D
 
Level 25
Joined
Sep 26, 2009
Messages
2,381
What Xonok wrote is the standard approach.

  • Set ClosestUnit = No unit
  • Set ShortestDistance = 900000.00
  • Set StartPoint = *somewhere*
  • Custom script: set bj_wantDestroyGroup = true
  • Unit Group - Pick every unit in (Units within 900.00 of StartPoint matching (((Matching unit) is alive) Equal to True)) and do (Actions)
    • Loop - Actions
      • Set PickedUnit = (Picked unit)
      • Set UnitLocation = (Position of PickedUnit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (Distance between StartPoint and UnitLocation) Less than ShortestDistance
        • Then - Actions
          • Set ClosestUnit = PickedUnit
          • Set ShortestDistance = (Distance between StartPoint and UnitLocation)
        • Else - Actions
      • Custom script: call RemoveLocation(udg_UnitLocation)
  • Custom script: call RemoveLocation(udg_StartLocation)
  • Custom script: set udg_StartLocation = true
  • Custom script: set udg_UnitLocation = true
Where UnitLocation and StartLocation and point variables, PickedUnit and ClosestUnit are unit variables and ShortestDistance is real variable.
ATM that trigger picks the closest unit to some point, if you do this for spells, don't forget to correctly set up conditions for the unit group pick.
After this part of code the closest unit is saved in ClosestUnit variable.

Also, if you inverse this logic, you can also pick the furthest unit from a point in certain radius of that point.
 
Status
Not open for further replies.
Top