9 years
edited 8 years
This simple snippet allows you to check for the walkability of the terrain between two points.
1 library IsLineWalkable /*
2
3 */uses /*
4
5 */TerrainPathability /*
6 - http://www.wc3c.net/showthread.php?t=103862 */7 8 //! novjass9 ________________
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 any21 any unwalkable coordinates including some doodads, cliffs, mapboundary, and other similar stuffs22
23 */function GetLastWalkableX takes nothing returns real/*
24 - Returns the value of the x-coordinate of the last walkable terrain when using IsLineWalkable25
26 */function GetLastWalkableY takes nothing returns real/*
27 - Returns the value of the y-coordinate of the last walkable terrain when using IsLineWalkable28
29 *///! endnovjass
30 31 globals32 ///////////////////////////////////////////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 endglobals40 41 //==============================================================================42 43 globals44 private real X
45 private real Y
46 endglobals47 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 endif60 else61 set angle = Atan2(dy, dx)
62 set x = checkInterval*Cos(angle)
63 set y = checkInterval*Sin(angle)
64 loop65 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 endif73 endloop74 endif75 set X = TerrainPathability_X
76 set Y = TerrainPathability_Y
77 return true
78 endfunction79 80 function GetLastWalkableX takes nothing returns real
81 return X82 endfunction83 84 function GetLastWalkableY takes nothing returns real
85 return Y86 endfunction87 88 89 endlibrary