• 🏆 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!

Attack nearest enemy – is it possible?

Status
Not open for further replies.
Level 25
Joined
Sep 26, 2009
Messages
2,378
This was posted by Maker in some other thread

  • Closest Unit
    • Events
      • Your event
    • Conditions
      • Your conditions
    • Actions
      • Set point1 = (Position of (Triggering unit))
      • Set real1 = 10000.00
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units within 1500.00 of point1) and do (Actions)
        • Loop - Actions
          • Set point2 = (Position of (Picked unit))
          • Set real2 = (Distance between point1 and point2)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • real2 Less than real1
            • Then - Actions
              • -------- We have found our closest unit --------
              • Set Unit1 = (Picked unit)
              • Set real1 = real2
            • Else - Actions
          • Custom script: call RemoveLocation(udg_point2)
      • Custom script: call RemoveLocation(udg_point1)
      • -------- Closest unit to point1 is Unit1 --------
point1 and point2 are point variables, real1 and real2 real variables and unit1 is unit variable.

You can tweak it a bit by adding conditions for unit group and increasing/decreasing the range.
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
If you order to attack on the ground on the unit position it will look for the nearest enemy that represents a threat.
 
This was posted by Maker in some other thread

  • Closest Unit
    • Events
      • Your event
    • Conditions
      • Your conditions
    • Actions
      • Set point1 = (Position of (Triggering unit))
      • Set real1 = 10000.00
      • Custom script: set bj_wantDestroyGroup = true
      • Unit Group - Pick every unit in (Units within 1500.00 of point1) and do (Actions)
        • Loop - Actions
          • Set point2 = (Position of (Picked unit))
          • Set real2 = (Distance between point1 and point2)
          • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
            • If - Conditions
              • real2 Less than real1
            • Then - Actions
              • -------- We have found our closest unit --------
              • Set Unit1 = (Picked unit)
              • Set real1 = real2
            • Else - Actions
          • Custom script: call RemoveLocation(udg_point2)
      • Custom script: call RemoveLocation(udg_point1)
      • -------- Closest unit to point1 is Unit1 --------
point1 and point2 are point variables, real1 and real2 real variables and unit1 is unit variable.

You can tweak it a bit by adding conditions for unit group and increasing/decreasing the range.



I don't get why is real1 10,000. Since you pick units in 1500 range, the ITE always returns true so the first picked unit is sorted.
 
Level 12
Joined
Feb 22, 2010
Messages
1,115
what? You cant know that, maybe there is no unit around in 9000 range and nearest unit in 9500 range?
 
Level 25
Joined
Sep 26, 2009
Messages
2,378
real1 is just some random high number. It's meaning is in the sorting of units. The first unit picked in the unit group changes that number and any other unit which is closer will also change it. In the end, you end up with the closest unit.

e.g.
real1 = 10 000
1st picked unit => real2 (range) = 400 ... is real2 less than real1? Yes => set real1 to 400
now it compares if other units are closer than 400. If yes, it changes real1 to that new value and saves the unit.
 
Level 2
Joined
Jun 29, 2013
Messages
13
hi all ! i gotta be honnest i stayed up all night trying to figure this out, and i somehow did, but still i cant manage to get it all . so i did it like this in my description, didnt test yet cuz i didnt still wrote Ai for the comp, but can u tell me at least am i going in the right direction. blue player is human, and pink is ai..
 

Attachments

  • asgasg.png
    asgasg.png
    47 KB · Views: 281
Status
Not open for further replies.
Top