[Snippet] IsLineWalkable

Scripts

9 years
edited 8 years
This simple snippet allows you to check for the walkability of the terrain between two points.


Code (jass) Select
library IsLineWalkable /*

    */uses /*

    */TerrainPathability /*
          - http://www.wc3c.net/showthread.php?t=103862 */

//! novjass
     ________________
    |                |
    | Written by AGD |
    |________________|


    |=====|
    | API |
    |=====|

        function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean/*
        - Checks if the line between points P1(x1, y1) and P2(x2, y2) does not contain any
          any unwalkable coordinates including some doodads, cliffs, mapboundary, and other similar stuffs

      */function GetLastWalkableX takes nothing returns real/*
        - Returns the value of the x-coordinate of the last walkable terrain when using IsLineWalkable

      */function GetLastWalkableY takes nothing returns real/*
        - Returns the value of the y-coordinate of the last walkable terrain when using IsLineWalkable

*///! endnovjass

    globals
        ///////////////////////////////////////////
        // The path walkability check distance   //
        // interval. Lower value increases       //
        // precision but may cost performance.   //
        // Suggested value limit (10.00 - 25.00) //
        ///////////////////////////////////////////
        private constant real checkInterval = 20.00
    endglobals

    //==============================================================================

    globals
        private real X
        private real Y
    endglobals

    function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean
        local real dx = x2 - x1
        local real dy = y2 - y1
        local real angle
        local real x
        local real y
        if dx*dx + dy*dy < checkInterval*checkInterval then
            if not IsTerrainWalkable(x2, y2) then
                set X = TerrainPathability_X
                set Y = TerrainPathability_Y
                return false
            endif
        else
            set angle = Atan2(dy, dx)
            set x = checkInterval*Cos(angle)
            set y = checkInterval*Sin(angle)
            loop
                set x1 = x1 + x
                set y1 = y1 + y
                exitwhen x1 > x2 or y1 > y2
                if not IsTerrainWalkable(x1, y1) then
                    set X = TerrainPathability_X
                    set Y = TerrainPathability_Y
                    return false
                endif
            endloop
        endif
        set X = TerrainPathability_X
        set Y = TerrainPathability_Y
        return true
    endfunction

    function GetLastWalkableX takes nothing returns real
        return X
    endfunction

    function GetLastWalkableY takes nothing returns real
        return Y
    endfunction


endlibrary
7 years
What do you mean by "publishing"?