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

How to detect unit that is most near from some point?

Status
Not open for further replies.
Level 13
Joined
Mar 4, 2009
Messages
1,156
For example if you have some harvesters they will automatically go to nearest house,now how can i order my unit to do something like that


something like this

order (my unit) to attack nearest (enemy)

or

kill the most near unit from center of playable map area ?
 
Level 13
Joined
Mar 4, 2009
Messages
1,156
Sorry i don't really understand much JASS and it looks to big for such thing that should be simple,im sure blizzard is doing it on another way,

anyway i did it like this and i didn't have any problems/lags/bugs

"RANGE = A x 100" means that it will not be more correct than 100 range,so it might miss for 100 range..you can set 50 if you want

thanks anyway :D

  • Actions
    • Set TF = True
    • Set POINT = (Center of (Playable map area))
    • For each (Integer A) from 1 to 1000000000, do (Actions)
      • Loop - Actions
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • TF Equal to True
          • Then - Actions
            • Set RANGE = ((Integer A) x 100)
            • Set UNITGROUP = (Units within (Real(RANGE)) of POINT matching (((Matching unit) is alive) Equal to True))
            • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
              • If - Conditions
                • (UNITGROUP is empty) Not equal to True
              • Then - Actions
                • Unit - Kill (Random unit from UNITGROUP)
                • Set TF = False
              • Else - Actions
            • Custom script: call DestroyGroup(udg_UNITGROUP)
          • Else - Actions
    • Custom script: call RemoveLocation(udg_POINT)
 
Last edited:
Level 8
Joined
Dec 8, 2007
Messages
312
this is just a basic operation to find the smallest value (distance)
  • Actions
    • Set Point1 = (Center of (Playable map area))
    • Set tempGroup = (Units in (Playable map area))
    • Set UNIT1 = (Random unit from tempGroup)
    • Set Point2 = (Position of UNIT2)
    • Set Distance1 = (Distance between Point1 and Point2)
    • Custom script: call RemoveLocation (udg_Point2)
    • Unit Group - Pick every unit in tempGroup and do (Actions)
      • Loop - Actions
        • Set UNIT2 = (Picked unit)
        • Set Point2 = (Position of UNIT2)
        • Set Distance2 = (Distance between Point1 and Point2)
        • Custom script: call RemoveLocation (udg_Point2)
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Distance2 Less than Distance1
          • Then - Actions
            • Set UNIT1 = UNIT2
          • Else - Actions
    • Custom script: call DestroyGroup(tempGroup)
    • Custom script: call RemoveLocation (udg_Point1)
    • -------- Now UNIT1 is the closest to the point --------
 
Level 13
Joined
Mar 4, 2009
Messages
1,156
this is just a basic operation to find the smallest value (distance)
  • Actions
    • Set Point1 = (Center of (Playable map area))
    • Set tempGroup = (Units in (Playable map area))
    • Set UNIT1 = (Random unit from tempGroup)
    • Set Point2 = (Position of UNIT2)
    • Set Distance1 = (Distance between Point1 and Point2)
    • Custom script: call RemoveLocation (udg_Point2)
    • Unit Group - Pick every unit in tempGroup and do (Actions)
      • Loop - Actions
        • Set UNIT2 = (Picked unit)
        • Set Point2 = (Position of UNIT2)
        • Set Distance2 = (Distance between Point1 and Point2)
        • Custom script: call RemoveLocation (udg_Point2)
        • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
          • If - Conditions
            • Distance2 Less than Distance1
          • Then - Actions
            • Set UNIT1 = UNIT2
          • Else - Actions
    • Custom script: call DestroyGroup(tempGroup)
    • Custom script: call RemoveLocation (udg_Point1)
    • -------- Now UNIT1 is the closest to the point --------

Doesn't look like it could work but i didn't do it jet,did you check it with more units?
 
Level 8
Joined
Dec 8, 2007
Messages
312
I didn't check it at all. I'm not near my home computer and one I'm currently using is so damn slow that I don't have a wish to test it.

EDIT:
Idea of trigger was to take one random unit in group and set it as closest. Then I compare distance between the point1 and other units in group. if it's smaller then I make that unit new closest.
Does it sound like it could work now?

Another Edit:
I missed one thing in if/then/else part. It should also set Distance2 to Distance1.
  • If - Conditions
  • Distance2 Less than Distance1
  • Then - Actions
  • Set UNIT1 = UNIT2
  • Set Distance1= Distance2
  • Else - Actions
FINAL EDIT:
now when I'm back to my home computer I tested my trigger and it works good. I'm also attaching sample map.
 

Attachments

  • closest unit.w3x
    34.6 KB · Views: 66
Last edited:
Status
Not open for further replies.
Top