Get Nearest Destructible To Unit (MUI)

gref

New Member
Reaction score
33
This is a function that returns the nearest destructable in <radius> to a given <unit>.
Code:
GetNearestDestructableToUnit(<unit>, <radius>)

It works fine, and I'm using it to power the AI that makes a Night Elf Ancient Protector eat a tree when they are low on health. However, it's not MUI and I don't know of a way to make it MUI.
I tried attaching variables to GetTriggeringTrigger() but they didn't carry through (I'm assuming because this function is called, not directly in a trigger), and short of using arrays up to X units, and recycling (which aren't infintely MUI anyway, I'm lost on a solution).

Can someone please help?

(This would all be far easier if they were units and I could use unit groups)


Global variables:
Code:
globals
    real a_x
    real a_y
    real a_nearest_distance
    destructable a_nearest
endglobals

Function called on each destructable in range:
Code:
function CompareDestructableDistance takes nothing returns nothing
    local real nearest_distance = GetAttachedReal(GetTriggeringTrigger(), "cdd_nearest_distance")
    local real x
    local real y    
    local real x2
    local real y2
    
    local real current_distance    

    set nearest_distance = a_nearest_distance
    set x = a_x
    set y = a_y
                                               
    set x2 = GetDestructableX(GetEnumDestructable()) - x
    set y2 = GetDestructableY(GetEnumDestructable()) - y

    set current_distance = SquareRoot(x2*x2 + y2*y2)

    if (current_distance < nearest_distance)and(GetDestructableLife(GetEnumDestructable()) > 0) then   
        set a_nearest = GetEnumDestructable()
        set a_nearest_distance = current_distance
    endif

endfunction

Function calling the loop of destructables:
Code:
function GetNearestDestructableToUnit takes unit focus, real radius returns destructable
    local real centerX = GetUnitX(focus)
    local real centerY = GetUnitY(focus)
    local destructable nearest
    local rect r
    
    set a_nearest = null
    set a_nearest_distance = radius
    set a_x = centerX
    set a_y = centerY
    
    if (radius >= 0) then
        set bj_enumDestructableCenter = GetUnitLoc(focus)
        set bj_enumDestructableRadius = radius
        set r = Rect(centerX - radius, centerY - radius, centerX + radius, centerY + radius)
        call EnumDestructablesInRect(r, filterEnumDestructablesInCircleBJ, function CompareDestructableDistance)
        call RemoveRect(r)
    endif
    
    set nearest = a_nearest
            
    return nearest                         
endfunction
 

Pyrogasm

There are some who would use any excuse to ban me.
Reaction score
134
What are you fretting about with "MUI"?! It's a function call. Without waits. There's nothing to bug out about.

And you shouldn't need attaching, just a few globals, like so:
JASS:
library NearestDestructible initializer Init
    globals
        private destructable Nearest
        private real X
        private real Y
        private real NearestDistance
        private boolexpr BOOLEXPR_TRUE
    endglobals

    private function True takes nothing returns boolean
        return true
    endfunction

    private function Enum takes nothing returns nothing
        local destructable D = GetEnumDestructable()
        local real DX = GetDestructableX(D)
        local real DY = GetDestructableY(D)
        local real Distance = (X-DX)*(X-DX)+(Y-DY)*(Y-DY)

        if Distance &lt;= NearestDistance then
            set NearestDistance = Distance
            set Nearest = D
        endif

        set D = null
    endfunction

    function GetNearestDestructableToUnit takes unit Focus, real Radius returns destructable
        local rect R

        set X = GetUnitX(Focus)
        set Y = GetUnitY(Focus)
        set NearestDistance = Radius*Radius
        set Nearest = null

        set R = CreateRect(X-Radius, Y-Radius, X+Radius, Y+Radius)
        call EnumDestructablesInRect(R, BOOLEXPR_TRUE, function Enum)
        call RemoveRect(R)
        set R = null

        return Nearest
    endfunction

    private function Init takes nothing returns nothing
        set BOOLEXPR_TRUE = Condition(function True)
    endfunction
endlibrary
 

gref

New Member
Reaction score
33
Ok thanks... I had assumed that the amount of time taken by the function would be infinitessimaly small, however I don't know how multithreading or whatnot would affect it, so I wanted to be sure.
 
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

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top