[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
1
library IsLineWalkable /*
2
3
    */uses /*
4
5
    */TerrainPathability /*
6
          - http://www.wc3c.net/showthread.php?t=103862 */
7
8
//! novjass
9
     ________________
10
    |                |
11
    | Written by AGD |
12
    |________________|
13
14
15
    |=====|
16
    | API |
17
    |=====|
18
19
        function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean/*
20
        - Checks if the line between points P1(x1, y1) and P2(x2, y2) does not contain any
21
          any unwalkable coordinates including some doodads, cliffs, mapboundary, and other similar stuffs
22
23
      */function GetLastWalkableX takes nothing returns real/*
24
        - Returns the value of the x-coordinate of the last walkable terrain when using IsLineWalkable
25
26
      */function GetLastWalkableY takes nothing returns real/*
27
        - Returns the value of the y-coordinate of the last walkable terrain when using IsLineWalkable
28
29
*///! endnovjass
30
31
    globals
32
        ///////////////////////////////////////////
33
        // The path walkability check distance   //
34
        // interval. Lower value increases       //
35
        // precision but may cost performance.   //
36
        // Suggested value limit (10.00 - 25.00) //
37
        ///////////////////////////////////////////
38
        private constant real checkInterval = 20.00
39
    endglobals
40
41
    //==============================================================================
42
43
    globals
44
        private real X
45
        private real Y
46
    endglobals
47
48
    function IsLineWalkable takes real x1, real y1, real x2, real y2 returns boolean
49
        local real dx = x2 - x1
50
        local real dy = y2 - y1
51
        local real angle
52
        local real x
53
        local real y
54
        if dx*dx + dy*dy < checkInterval*checkInterval then
55
            if not IsTerrainWalkable(x2, y2) then
56
                set X = TerrainPathability_X
57
                set Y = TerrainPathability_Y
58
                return false
59
            endif
60
        else
61
            set angle = Atan2(dy, dx)
62
            set x = checkInterval*Cos(angle)
63
            set y = checkInterval*Sin(angle)
64
            loop
65
                set x1 = x1 + x
66
                set y1 = y1 + y
67
                exitwhen x1 > x2 or y1 > y2
68
                if not IsTerrainWalkable(x1, y1) then
69
                    set X = TerrainPathability_X
70
                    set Y = TerrainPathability_Y
71
                    return false
72
                endif
73
            endloop
74
        endif
75
        set X = TerrainPathability_X
76
        set Y = TerrainPathability_Y
77
        return true
78
    endfunction
79
80
    function GetLastWalkableX takes nothing returns real
81
        return X
82
    endfunction
83
84
    function GetLastWalkableY takes nothing returns real
85
        return Y
86
    endfunction
87
88
89
endlibrary
8 years
What do you mean by "publishing"?