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,462
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,462
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.

      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