Advertisement
Guest User

IsTerrainWalkable

a guest
Jun 1st, 2012
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.07 KB | None | 0 0
  1.  
  2. //Thanks to anitarf and Vexorian @ wc3c.net for this library, it makes things easier.
  3.  
  4. library IsTerrainWalkable initializer Init
  5.     globals
  6.         // this value is how far from a point the item may end up for the point to be considered pathable
  7.         private constant real MAX_RANGE=10.
  8.         // the following two variables are set to the position of the item after each pathing check
  9.         // that way, if a point isn't pathable, these will be the coordinates of the nearest point that is
  10.         public real X=0.
  11.         public real Y=0.
  12.         private rect r
  13.         private item check
  14.         private item array hidden
  15.         private integer hiddenMax=0
  16.     endglobals
  17.  
  18.     private function Init takes nothing returns nothing
  19.         set check=CreateItem('ciri',0,0)
  20.         call SetItemVisible(check,false)
  21.         set r=Rect(0.0,0.0,128.0,128.0)
  22.     endfunction
  23.  
  24.     private function HideBothersomeItem takes nothing returns nothing
  25.         if IsItemVisible(GetEnumItem()) then
  26.             set hidden[hiddenMax]=GetEnumItem()
  27.             call SetItemVisible(hidden[hiddenMax],false)
  28.             set hiddenMax=hiddenMax+1
  29.         endif
  30.     endfunction
  31.  
  32.     function IsTerrainWalkable takes real x, real y returns boolean
  33.         // first, hide any items in the area so they don't get in the way of our item
  34.         call MoveRectTo(r,x,y)
  35.         call EnumItemsInRect(r,null,function HideBothersomeItem)
  36.         // try to move the check item and get it's coordinates
  37.         call SetItemPosition(check,x,y)//this unhides the item...
  38.         set X=GetItemX(check)
  39.         set Y=GetItemY(check)
  40.         call SetItemVisible(check,false)//...so we must hide it again
  41.         // before returning, unhide any items that got hidden at the start
  42.         loop
  43.             exitwhen hiddenMax<=0
  44.             set hiddenMax=hiddenMax-1
  45.             call SetItemVisible(hidden[hiddenMax],true)
  46.             set hidden[hiddenMax]=null
  47.         endloop
  48.         // return pathability status
  49.         return (x-X)*(x-X)+(y-Y)*(y-Y)<MAX_RANGE*MAX_RANGE
  50.     endfunction
  51. endlibrary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement