• 🏆 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!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Distance Between Destructible and Unit

Status
Not open for further replies.

ndh

ndh

Level 8
Joined
Apr 21, 2012
Messages
344
Hey, I need a system that calculates the distance between 800 destructibles and 1 unit, then determines which destructible is closest to that unit. Any help/advice would be nice, can't seem to figure it out.
 
Level 17
Joined
Feb 11, 2011
Messages
1,860
I will write something up quick...
How are the destructables picked? In a region?

EDIT: Here is a vJASS system I wrote up. It checks each destructable in the specified rect (region in GUI). The system code:

JASS:
library DestructableEnum                // by Mr_Bean

    globals
        private destructable closest    // Stores current closest destructable.
        private real distance           // Stores distance of current closest destructable.
        private real unitX              // Stores x co-ordinate of checked unit.
        private real unitY              // Stores y co-ordinate of checked unit.
    endglobals

    private function ClosestDestrEnum takes nothing returns nothing
        local destructable d = GetEnumDestructable()
        local real dx
        local real dy
        local real dist
        
        if (GetWidgetLife(d) > 0.405) then
        
            set dx = GetDestructableX(d) - unitX
            set dy = GetDestructableY(d) - unitY
            set dist = SquareRoot((dx * dx) + (dy * dy))
        
            if (dist < distance) then
                set closest = d
                set distance = dist
            endif
            
        endif
        
        set d = null
    endfunction
    
    function GetClosestDestructable takes unit whichUnit, rect whichRect returns destructable
        set distance = 999999.0
        set closest = null
        set unitX = GetUnitX(whichUnit)
        set unitY = GetUnitY(whichUnit)
        call EnumDestructablesInRect(whichRect, null, function ClosestDestrEnum)
        
        return closest
    endfunction

endlibrary
And example usage:

  • Test Trig
    • Events
      • Player - Player 1 (Red) types a chat message containing -closest as An exact match
    • Conditions
    • Actions
      • Custom script: call BJDebugMsg(GetDestructableName(GetClosestDestructable(udg_hero, bj_mapInitialPlayableArea)))
This prints the name of the closest destructable on the map to the unit stored in the hero variable.
 
Last edited:
Status
Not open for further replies.
Top