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: 76

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.
  • Ghan Ghan:
    Howdy
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top