• 🏆 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!

[System] Get Nearest Unit v1.00

This bundle is marked as useful / simple. Simplicity is bliss, low effort and/or may contain minor bugs.
Easiest and with more flexible way to retrieve the nearest unit from a point


JASS:
library Neartest
//*************************************************
//* _   _                          _   
//*| \ | | ___  __ _ _ __ ___  ___| |_ 
//*|  \| |/ _ \/ _` | '__/ _ \/ __| __|
//*| |\  |  __/ (_| | | |  __/\__ \ |_ 
//*|_| \_|\___|\__,_|_|  \___||___/\__|
//* by dhguardianes
//*  this idea by Tom_Kazansky
//* -------------
//* Easiest and with more flexible way to retrieve the nearest unit from a point
//*
//* function GetNearestUnit( X,Y,Radius,Boolexpr)
//*
//*      The extra integer argument skips that many units in the
//*      list of near units.
//*      Example: If takes "3" then it would return the second nearest
//*      unit.
//*      The extra real argument is the range to pick unit
//*      return null if no unit is picked.
//* Example:
//*
//* set TempUnit = GetNearestUnit(GetLocationX(TempLoc),GetLocationY(TempLoc),800,Condition(function IsBuilding))
//*
//************************************************

//=========================================================================================
    globals
        private unit GetUnit
        private real Distance
        private real x
        private real y
        private real value
        private constant group g = CreateGroup()
    endglobals
//=========================================================================================
    function IsHero takes nothing returns boolean
        return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_HERO) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) == true )
    endfunction
    function IsBuilding takes nothing returns boolean
        return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) == true )
    endfunction
    function IsBuildingPlayer0 takes nothing returns boolean
        return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) and ( GetOwningPlayer(GetFilterUnit()) == Player(0) ) == true )
    endfunction
    function IsBuildingPlayer6 takes nothing returns boolean
        return ( IsUnitType(GetFilterUnit(), UNIT_TYPE_STRUCTURE) and (GetUnitState(GetFilterUnit(), UNIT_STATE_LIFE) > 0) and ( GetOwningPlayer(GetFilterUnit()) == Player(6) ) == true )
    endfunction

    private function callback takes nothing returns nothing
        set value = SquareRoot((GetUnitX(GetEnumUnit()) - x) * (GetUnitX(GetEnumUnit()) - x) + (GetUnitY(GetEnumUnit()) - y) * (GetUnitY(GetEnumUnit()) - y))
        if ( value < Distance ) then
            set Distance = value
            set GetUnit = GetEnumUnit()
        endif
    endfunction
    function GetNearestUnit takes real newX, real newY, real newR, boolexpr newB returns unit
        set Distance = newR
        set x = newX
        set y = newY
        set GetUnit = null
        call GroupEnumUnitsInRangeOfLoc(g, Location(newX, newY), newR, newB)
        call ForGroup(g, function callback)
        call GroupClear(g)
        return GetUnit
    endfunction
endlibrary

Keywords:
Nearest,dhguardianes,System,vjass
Contents

[System] Get Nearest Unit v1.00 (Map)

Reviews
29th Jan 2012 Bribe: This already exists in the JASS Resources forum in many variations. http://www.hiveworkshop.com/forums/jass-functions-413/

Moderator

M

Moderator

29th Jan 2012
Bribe: This already exists in the JASS Resources forum in many variations.

http://www.hiveworkshop.com/forums/jass-functions-413/
 
Level 5
Joined
Oct 13, 2010
Messages
84
I think jass will work better than the GUI and less leak.

I'm pretty sure about the phase "jass works better than the GUI" but i disagrees the phase "less leak". Less leak or not, it do NOT relate with GUI/Jass. Coder is the purpose of the leak.

Here is the example: Leak location
JASS:
call GroupEnumUnitsInRangeOfLoc(g, Location(newX, newY), newR, newB)

The last is : I dont know why U used Location instead of Coordinates???

read this for more informations
 
Top