How to get the nearest 3 units?

FireBladesX

Eating my wings!
Reaction score
123
How do you pick the closest X units to the caster? Like the nearest 3?
In other words, with a trigger, I'd like to be able to get the three closest units to a point, and do something to them.

Relatively resolved.
 

FireBladesX

Eating my wings!
Reaction score
123
Sure.
With a trigger, I'd like to be able to get the three closest units to a point, and do something to them.

I'll edit the first post, why not.
 

Draphoelix

It's not the wintercold that's killing me
Reaction score
132
I think there were a JASS system to do that.

I think it was something like loop units whtin 1 range + var
where var increases and then store thoose units etc.
 

Ninja_sheep

Heavy is credit to team!
Reaction score
64
It's not that hard. You make 3 variables for the 3 closest units. Then you pick every unit on the map beside the caster and put it into a unit group variable.
Then pick a random unit and put it on the first variable. Remove it from the unit group.
Do the same with the other 2 variables so you have a unit in each of them and the units aren't in the group variable anymore.
Count the units in the group and make a loop that loops as often as the number of units you just counted.

In the loop you do the following:
Pick a random unit. Compare the distance of the unit to the caster with the distance of the unit in the first unit variable to the caster. If the distance is smaller, then you overwrite the first unit variable with the randomly picked unit and remove the randomly picked unit from the unit group variable.
If the distance is bigger you do the same thing for the second and the third unit variable. If all units in the 3 variables are closer to the caster then the randomly picked one you remove the randomly picked one from the unit group.

Now you have the 3 closest units in the 3 variables.

Hope you understand it :<
 

Grymlax

Probably not around
Reaction score
138
I remember doing a spell like that once. wait a minute or two iand i will see if i can locate it.
 

FireBladesX

Eating my wings!
Reaction score
123
Yes, I do understand. I think I tried that in an old map and it failed, but I was bad at WE back then, heh.
Thx every1 :D
I'll look at what you have too, grym.
Editing my first post.


I just thought of a roundabout way... Using damage detection and a fan of knives. Less work, but I won't use it.
 

Grymlax

Probably not around
Reaction score
138
okay here it is. the spell is called Chronic Pain and it damages the 3 closest enemies every 5 sec.

i'm sorry about the file size it contains an uncompressed skin in it.
 

Attachments

  • PhysiqueMage.w3x
    422.6 KB · Views: 75

AceHart

Your Friendly Neighborhood Admin
Reaction score
1,495
Sorting... for the times when you want 2 or 3 or 5 or ...

Code:
Set Count = 3
Set Point = Position of (Triggering unit)
Set UnitGroup = Unit within 500 of Point matching (((Matching unit) belongs to an enemy of (Owner of (Triggering unit))) equal to true
Set TempInteger = 0
Pick every unit in UnitGroup and do
    Set TempInteger = TempInteger + 1
    Set Units[TempInteger] = (Picked unit)
    Set TempPoint = Position of (Picked unit)
    Set Distances[TempInteger] = Distance from Point to TempPoint
    Custom script: call RemoveLocation( udg_TempPoint )
Custom script: call RemoveLocation( udg_Point )
Custom script: call DestroyGroup( udg_UnitGroup )
For each Integer A from 1 to (TempInteger - 1)
    For each Integer B from (Integer A + 1) to TempInteger
        If Distances[Integer A] greater than Distances[Integer B] then
            Set TempReal = Distances[Integer A]
            Set Distances[Integer A] = Distances[Integer B]
            Set Distances[Integer B] = TempReal
            Set TempUnit = Units[Integer A]
            Set Units[Integer A] = Units[Integer B]
            Set Units[Integer B] = TempUnit
For each Integer A from 1 to Count
    Game - Display: "Nearest " + String(Integer A) + ": " + Name of (Units[Integer A])
 

Akolyt0r

New Member
Reaction score
33
have you got JassNewGen ?
if yes you could use following JassScript ...
(i could edit it aswell so you could use it even without JassNewGen)
JASS:
library NearestUnits
globals
    private player TempPlayer
endglobals

private function IsEnemy takes nothing returns boolean
return IsPlayerEnemy(TempPlayer,GetOwningPlayer(GetFilterUnit()))and GetWidgetLife(GetFilterUnit())&gt;0
endfunction

function getNearestUnits takes unit u, integer units, integer maxradius returns group
local location l=GetUnitLoc(u)
local group g=CreateGroup()
local integer radius=0
loop
    set TempPlayer=GetOwningPlayer(u)
    call GroupEnumUnitsInRangeOfLoc(g, l, radius, Condition(function IsEnemy))
    if(CountUnitsInGroup(g)==units or radius&gt;maxradius)then
        exitwhen(true)
    endif
    call GroupClear(g)
    set radius=radius+10
endloop
set l=null
return g
endfunction
endlibrary

You call it like following:

Custom Script - set udg_<SomeGroupVariable>=getNearestUnits(udg_<SomeUnitVariable>,<integer units>,<integer maxradius>)
SomeUnitVariable is the central unit you search around for units near it...
the integer units is the ammount of units you want ...
the integer maxradius is the maximum radius you want to search in..
so
set udg_TempGroup=getNearestUnits(udg_TempUnit,3,1000)
will get 3 nearest enemy living units around udg_TempUnit in an radius of 1000,
or 0,1,2 units if there arent 3 units in the radius of 1000.

after you used the units to do your stuff dont forget to clean the unitgroupvariable with sth like
Custom script: call DestroyGroup(udg_TempGroup)

its pretty easy to use:
Trigger:
  • Test
    • Events
      • Player - Player 1 (Red) skips a cinematic sequence
    • Conditions
    • Actions
      • Set TempUnit = Blademaster 0002 &lt;gen&gt;
      • Custom script: set udg_TempGroup=getNearestUnits(udg_TempUnit,3,1000)
      • Unit Group - Pick every unit in TempGroup and do (Actions)
        • Loop - Actions
          • Game - Display to (All players) the text: (Name of (Picked unit))
          • Unit - Explode (Picked unit)
      • Custom script: call DestroyGroup(udg_TempGroup)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top