Advanced help req

ultimate11

Active Member
Reaction score
25
Hi ragezoners.
I need some help in creating a spell.What I want from you is to help me finding a way to create a system or an algorithm that detect what hero is closer to my casting unit.
 

Happy

Well-Known Member
Reaction score
71
take a look here...i answered this question just some days ago....


PLEASE PLEASE PLEASE....use the search function -.-
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
Assuming you use jass, if i remember correctly you do, just rename this lib whatever you want and require it for any libraries you want to use it with.

JASS:
library closestunit
    function GetClosestUnit takes real x, real y, real radius returns unit
        local real r = radius + 1
        local unit u
        local unit ret = null
        call GroupEnumUnitsInRange(bj_lastCreatedGroup, x, y, radius, null)
        loop
            set u = FirstOfGroup(bj_lastCreatedGroup)
            exitwhen u == null
            call GroupRemoveUnit(bj_lastCreatedGroup, u)
            if r > SquareRoot(Pow(x - GetUnitX(u)) + Pow(y - GetUnitY(u))) then
                set ret = u
                set r = SquareRoot(Pow(x - GetUnitX(u)) + Pow(y - GetUnitY(u)))
            endif
        endloop
        return ret
    endfunction
endlibrary


ps. u ends up being null in the end every time anyway, so no need to null it, and ret is the return... so no need to null :/

@Happy
While he probably could have found his solution, what you posted is merely a description of it, and people could just as easily get lost trying to understand what you said, better just to post an actual solution for them to see the first time, then link to that instead.
 

tommerbob

Minecraft. :D
Reaction score
110
If you want the closest hero to the caster, I amended Gfreak's code a bit:

JASS:
library closestunit
    private function getHero takes nothing returns boolean
        return IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO)
    endfunction
    function GetClosestUnit takes real x, real y, real radius returns unit
        local real r = radius + 1
        local unit u
        local unit ret = null
        call GroupEnumUnitsInRange(bj_lastCreatedGroup, x, y, radius, Filter(function getHero))
        loop
            set u = FirstOfGroup(bj_lastCreatedGroup)
            exitwhen u == null
            call GroupRemoveUnit(bj_lastCreatedGroup, u)
            if r > SquareRoot(Pow(x - GetUnitX(u)) + Pow(y - GetUnitY(u))) then
                set ret = u
                set r = SquareRoot(Pow(x - GetUnitX(u)) + Pow(y - GetUnitY(u)))
            endif
        endloop
        return ret
    endfunction
endlibrary
 

Accname

2D-Graphics enthusiast
Reaction score
1,463
It really isnt hard if you think about it just a little bit. You should actually be able to come up with the solution yourself if you just try.

Here is the basic idea, you should really be able to translate that pseudo code in GUI by yourself:

Code:
set the maximum pick range := MPR = X
set a temporary unit variable := TUV = null
set a temporary real variable := TRV = 999999.99
  pick all units within the range MPR to a POINT
    if PICKED UNIT is a hero == false
      next
    end
    if distance between PICKED UNIT and POINT <= TRV 
      set TUV = PICKED UNIT
      set TRV = DISTANCE
    end
  end
what shoul that do?
You start with a temporary unit variable with no unit (because no unit was found yet) and temporary real variable which represents the distance to that unit. (Its initialized as a ridiculously high value at start!)
You iterate through all units within a given max range, you check if the picked unit is a hero, now you check whether the distance to the picked unit is smaller then to the temporary unit (its distance is saved in the variable) and if it is the picked unit will now become our temporary unit.
In the end you have iterated through all units and you have always choosen the unit with the smallest distance to our POINT and set it to be our temporary unit.
The temporary unit variable will now hold the closest unit to that point and the temporary real variable will be the distance.
 

ultimate11

Active Member
Reaction score
25
Well I really dont understand that jass code but I had followd the accname advice and I figure out a way n GUI :D.
Here is the code .for the moment work fine.If you find some leaks or something wrong pls replay.
Trigger:
  • Untitled Trigger 001
    • Events
      • Player - Player 1 (Red) types a chat message containing a as An exact match
    • Conditions
    • Actions
      • Custom script: set udg_Combo_target = null
      • Set MPR = 11111111.00
      • Set TCV = 0
      • Set TPV[1] = (Position of Archmage 0006 &lt;gen&gt;)
      • Set TGV = (Units within MPR of TPV[1] matching (((Matching unit) is A Hero) Equal to True))
      • Unit Group - Remove Archmage 0006 &lt;gen&gt; from TGV
      • Set TCGV = (Number of units in TGV)
      • For each (Integer A) from 1 to TCGV, do (Actions)
        • Loop - Actions
          • Set TUV[1] = (Random unit from TGV)
          • Unit Group - Add TUV[3] to TGV
          • Set TPV[2] = (Position of TUV[1])
          • Set TRV[1] = (Distance between TPV[1] and TPV[2])
          • Unit Group - Remove TUV[1] from TGV
          • For each (Integer B) from 1 to (TCGV - 1), do (Actions)
            • Loop - Actions
              • Set TUV[2] = (Random unit from TGV)
              • Set TPV[3] = (Position of TUV[2])
              • Set TRV[2] = (Distance between TPV[1] and TPV[3])
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • TRV[1] Less than or equal to TRV[2]
                • Then - Actions
                  • Set TCV = (TCV + 1)
                  • If (TCV Equal to (TCGV - 1)) then do (Set Combo_target = TUV[1]) else do (Do nothing)
                • Else - Actions
              • Unit Group - Remove TUV[2] from TGV
              • Unit Group - Add TUV[2] to TGV_Copy
              • Custom script: call RemoveLocation(udg_TPV[3])
              • Custom script: set udg_TUV[2] = null
          • Unit Group - Remove all units from TGV
          • Unit Group - Add all units of TGV_Copy to TGV
          • Unit Group - Remove all units from TGV_Copy
          • Set TUV[3] = TUV[1]
          • Custom script: set udg_TUV[1] = null
          • Custom script: call RemoveLocation(udg_TPV[2])
      • Custom script: call RemoveLocation(udg_TPV[1])
      • Custom script: call DestroyGroup(udg_TGV)
      • Game - Display to (All players) the text: (Name of Combo_target)
 

Accname

2D-Graphics enthusiast
Reaction score
1,463
You actually dont need 2 loops inside each other. I dont have the editor at hand but when i will i might post you a GUI version.
 

GFreak45

I didnt slap you, i high 5'd your face.
Reaction score
130
this trigger uses the following variables:
Unit variable Input (what unit you are comparing distance with)
Unit variable output (closest unit)
Real variable Radius (distance allowed)
Real variable Temp_Real (to compare shortest distance)
Point variable array Temp_Point (for leak clearing)
Group variable Temp_Group (for leak clearing)

Trigger:
  • Closest Unit Trigger
    • Events
    • Conditions
    • Actions
      • Set Temp_Real = Radius + 1
      • Set Temp_Point[1] = Position of Input
      • Set Temp_Group = Units within Radius range of Temp_Point[1]
      • Set Output = No Unit
      • Unit Group - Pick every unit in Temp_Group matching (Matching unit is not equal to Input and (your conditions, ie: enemy, ally, hero etc)) and do actions
        • Loop - Actions
          • Set Temp_Point[2] = Position of (Picked unit)
          • If Then Else
            • If (Conditions)
              • Distance between Temp_Point[1] and Temp_Point[2] is less than Temp_Real
            • Then (Actions)
              • Set Output = Picked unit
              • Set Temp_Real = Distance between Temp_Point[1] and Temp_Point[2]
            • Else (Actions)
          • Custom Script: call RemoveLocation(udg_Temp_Point[2])
      • Custom Script: call RemoveLocation(udg_Temp_Point[1])
      • Custom Script: call DestroyGroup(udg_Temp_Group)


Then you can use it like this:

Trigger:
  • Using closest unit:
    • Events
      • Your event
    • Conditions
      • Your conditions
    • Actions
      • Set Input = Triggering unit
      • Set Radius = 500.00
      • Trigger - Run Closest Unit Trigger &lt;gen&gt; checking conditions
      • If Then Else
        • If (Conditions)
          • Temp_Real is greater than 200
        • Then (Actions)
          • Do something for a unit being between 200 and 500 range (using Output as the closest unit)
        • Else
          • Do something for a unit being between you and 200 range (using Output as the closest unit)
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • 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 The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      The Helper Discord

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top