Andrewgosu
The Silent Pandaren Helper
- Reaction score
- 716
Just a little nifty function, for checking whether a position is pathable - useful for jumping spell and such, where you want to avoid moving the unit onto a cliff or a blockage!
function TestFunc takes nothing returns nothing
local unit test = CreateUnit(Player(15), 039;hfoo039;, GetRandomReal(0, 200), GetRandomReal(0, 200), 0)
if (IsPositionPathable(GetUnitX(test), GetUnitY(test))) then
call BJDebugMsg("true")
else
call BJDebugMsg("false")
endif
endfunction
So far it works as designed (please, prove me wrong. I know it can be done).
A tree, doodad or a unit, not to mention a cliff, is in the way - BOOM!
False.
If the reason the test item wasn't in the right spot is because there's an item already there, then the terrain is pathable.
library PathingFuncs initializer init
globals
private constant real ItemSafeX = 0. //This is the X coordinate of a point that nobody can get to on your map.
private constant real ItemSafeY = 0. //This is the Y coordinate of the same point. It's to keep from having hidden
private item I //item errors, such as comps picking them up, etc.
private unit U
endglobals
function IsPointPathable takes real x, real y, boolean evalunits returns boolean
local boolean b
if(evalunits)then
call ShowUnit(U,true)
call SetUnitPosition(U,x,y)
set b=GetUnitX(U)==x and GetUnitY(U)==y
call ShowUnit(U,false)
else
call SetItemVisible(I,true)
call SetItemPosition(I,x,y)
set b=GetItemX(I)==x and GetItemY(I)==y
call SetItemPosition(I,ItemSafeX,ItemSafeY)
call SetItemVisible(I,false)
endif
return b
endfunction
private function init takes nothing returns nothing
set I=CreateItem(039;ward039;,ItemSafeX,ItemSafeY)
call SetItemInvulnerable(I,true)
call SetItemVisible(I,false)
set U=CreateUnit(Player(15),039;hfoo039;,ItemSafeX,ItemSafeY,0.)
call SetUnitInvulnerable(U,true)
call ShowUnit(U,false)
endfunction
endlibrary
library Pathability
function FiltLiving takes nothing returns boolean
return IsUnitType(GetFilterUnit(),UNIT_TYPE_MECHANICAL) == false
endfunction
function PathOff takes nothing returns nothing
call SetUnitPathing(GetEnumUnit(),false)
endfunction
function PathOn takes nothing returns nothing
call SetUnitPathing(GetEnumUnit(),true)
endfunction
function IsPositionPathable takes real x, real y, integer radius returns boolean // radius to define the AOE around the location
local boolean flag // in which the units collision shall be turned off
local real xnew = I2R(R2I(x))
local real ynew = I2R(R2I(y))
local group g = CreateGroup()
local unit tester
call GroupEnumUnitsInRange(g,x,y,radius,Filter(function FiltLiving))
call ForGroup(g, function PathOff)
set tester = CreateUnit(Player(15), 039;hmtm039;, xnew, ynew, 0)
set flag = (GetUnitX(tester) == xnew) and (GetUnitY(tester) == ynew)
call RemoveUnit(tester)
call ForGroup(g, function PathOn)
call DestroyGroup(g)
set g = null
set tester = null
return flag
endfunction
endlibrary