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

[Snippet] IsTerrainWalkable

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,522
Hi,

The original threads containing IsTerrainWalkable are gone, so here is a backup. This is not my script, but was developed by Anitarf and Vexorian.

Regards,

JASS:
/**
 * IsTerrainWalkable snippet for estimating the walkability status of a co-ordinate pair, credits 
 * to Anitarf and Vexorian.
 * 
 * API:
 *     boolean IsTerrainWalkable(real x, real y) - returns the walkability of (x,y)
 */ 
library IsTerrainWalkable initializer init
    globals
	
        // this value is how far from a point the item may end up for the point to be considered pathable
        private constant real MAX_RANGE=10.
		
        // the following two variables are set to the position of the item after each pathing check
        // that way, if a point isn't pathable, these will be the coordinates of the nearest point that is
        public real X=0.
        public real Y=0.
        private rect r
        private item check
        private item array hidden
        private integer hiddenMax=0
    endglobals

    private function init takes nothing returns nothing
        set check=CreateItem('ciri',0.,0.)
        call SetItemVisible(check,false)
        set r=Rect(0.0,0.0,128.0,128.0)
    endfunction

    private function hideBothersomeItem takes nothing returns nothing
        if IsItemVisible(GetEnumItem()) then
            set hidden[hiddenMax]=GetEnumItem()
            call SetItemVisible(hidden[hiddenMax],false)
            set hiddenMax=hiddenMax+1
        endif
    endfunction

    function IsTerrainWalkable takes real x, real y returns boolean
	
        // first, hide any items in the area so they don't get in the way of our item
        call MoveRectTo(r,x,y)
        call EnumItemsInRect(r,null,function hideBothersomeItem)
		
        // try to move the check item and get its coordinates
		// this unhides the item...
        call SetItemPosition(check,x,y)
        set X=GetItemX(check)
        set Y=GetItemY(check)
		
		//...so we must hide it again
        call SetItemVisible(check,false)
		
        // before returning, unhide any items that got hidden at the start
        loop
            exitwhen hiddenMax==0
            set hiddenMax=hiddenMax-1
            call SetItemVisible(hidden[hiddenMax],true)
        endloop
		
        // return pathability status
        return (x-X)*(x-X)+(y-Y)*(y-Y)<MAX_RANGE*MAX_RANGE
    endfunction
endlibrary
 
Good.

"init" should be in a module so it works on initialization (sorry, it is stupid but necessary).

I don't know whether enforce JPAG for naming (capital function names, camelCase globals [which you do for the most part already]). The rules say yes, but my butt says no.

We really should implement an A* search.

Can't beat Wc3 default pathing because JASS is slow.
 

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,522
"init" should be in a module so it works on initialization (sorry, it is stupid but necessary).

If you can think of a use case for IsTerrainWalkable() inside a library initializer that warrants the shim, then sure.

I don't know whether enforce JPAG for naming (capital function names, camelCase globals [which you do for the most part already]). The rules say yes, but my butt says no.

Only the public API matters, which is not mine to edit (IsTerrainWalkable is okay, but X,Y public variables are questionable)
 
If you can think of a use case for IsTerrainWalkable() inside a library initializer that warrants the shim, then sure.

I'll need some time to think of a case. Although, it is more of a general principle. You never know what a user will do. Then again, you can't protect them from everything.

For now, it is fine though.

Only the public API matters, which is not mine to edit (IsTerrainWalkable is okay, but X,Y public variables are questionable)

Why are X and Y public in the first place? Backwards compatibility? Is it meant to represent the closest pathable point? Even so, it seems like a pretty weird implementation. I would use something else for that, tbh.
 

Cokemonkey11

Code Reviewer
Level 29
Joined
May 9, 2006
Messages
3,522
Why are X and Y public in the first place? Backwards compatibility? Is it meant to represent the closest pathable point? Even so, it seems like a pretty weird implementation. I would use something else for that, tbh.

Backwards compatibility. It's not my script, and not my script to change.
 
Top