Advertisement
Demonlord27

flash

May 26th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. -- Pathfinder API
  2. -- Crazyman32
  3. -- October 6, 2014
  4.  
  5. --[[
  6.  
  7. ----------------------------------------------------------------------------------
  8.  
  9. API:
  10.  
  11. - pathfinder:FindPath()
  12. - pathfinder:SetEmptyCutoff()
  13. - path:GetPointCoordinates()
  14. - path:CheckOcclusionAsync()
  15.  
  16. EXAMPLES:
  17.  
  18. - pathfinder = require(game.ServerScriptService.Pathfinder)
  19.  
  20. - path = pathfinder:FindPath(startPosition, endPosition)
  21.  
  22. - points = path:GetPointCoordinates()
  23. - index = path:CheckOcclusionAsync(startIndex)
  24.  
  25. - pathfinder:SetEmptyCutoff(ratio)
  26.  
  27. ----------------------------------------------------------------------------------
  28.  
  29. HOW TO USE -- Example Code:
  30.  
  31. local pathfinder = require(game.ServerScriptService.Pathfinder)
  32. local points, status = pathfinder:FindPath(startPosition, endPosition)
  33.  
  34. -- Where 'startPosition' and 'endPosition' are Vector3 values that
  35. -- represent the start and end positions of the path.
  36.  
  37. ----------------------------------------------------------------------------------
  38.  
  39. BENEFITS OVER PATHFINDINGSERVICE:
  40.  
  41. - Simplified path. Instead of having tons of points for every step
  42. of the way, this API will simplify the path by only keeping the
  43. important points: the start/end points for each straight or
  44. diagonal line.
  45.  
  46. - No distance cap. You can find a path between as far of a distance
  47. as you want. The API will connect multiple max-length paths from
  48. the PathfindingService for you and return a single list of points.
  49.  
  50. --]]
  51.  
  52.  
  53.  
  54. local Pathfinder = {}
  55. local API = {}
  56. local MT = {}
  57.  
  58. -------------------------------------------------------------------------------
  59. -- Core:
  60.  
  61. local MAX_PATHS = 10
  62.  
  63. local pathfindingService = game:GetService("PathfindingService")
  64.  
  65.  
  66. local function SimplifyPointsStraight(points)
  67. local simplified = {}
  68. local i = 1
  69. while (i < #points) do
  70. local pt = points[i]
  71. table.insert(simplified, pt)
  72. if (i < #points) then
  73. local ptNext = points[i + 1]
  74. local dx, dy, dz = (ptNext.x - pt.x), (ptNext.y - pt.y), (ptNext.z - pt.z)
  75. for j = i+1, #points do
  76. i = j
  77. local ptLast = points[i - 1]
  78. local pt2 = points[i]
  79. local dx2, dy2, dz2 = (pt2.x - ptLast.x), (pt2.y - ptLast.y), (pt2.z - ptLast.z)
  80. if (dx ~= dx2 or dy ~= dy2 or dz ~= dz2) then
  81. i = (i - 1)
  82. break
  83. end
  84. end
  85. end
  86. end
  87. table.insert(simplified, points[#points])
  88. return simplified
  89. end
  90.  
  91.  
  92. local function SimplifyPointsDiagonal(points)
  93. local simplified = {}
  94. local i = 3
  95. while (i < #points) do
  96. local p1 = points[i - 2]
  97. local p2 = points[i - 1]
  98. local p3 = points[i]
  99. local a = (p1 - p2).magnitude
  100. local b = (p2 - p3).magnitude
  101. local c = (p1 - p3).magnitude
  102. table.insert(simplified, p1)
  103. if (a == b) then
  104. i = (i + 2)
  105. else
  106. i = (i + 1)
  107. end
  108. end
  109. table.insert(simplified, points[#points])
  110. return simplified
  111. end
  112.  
  113.  
  114. function CreatePathObject(paths, points, finalStatus)
  115. local path = {}
  116. local api = {}
  117. api.Status = finalStatus
  118. function api:GetPointCoordinates()
  119. return points
  120. end
  121. function api:CheckOcclusionAsync(start)
  122. local n = 0
  123. local startIndex = 1
  124. for i,p in pairs(paths) do
  125. local numPoints = #p:GetPointCoordinates()
  126. n = (n + numPoints)
  127. if (n >= start) then
  128. startIndex = i + 1
  129. local test = p:CheckOcclusionAsync(n - numPoints + start)
  130. if (test ~= -1) then
  131. return (n - numPoints + test)
  132. end
  133. break
  134. end
  135. end
  136. for i = startIndex, #paths do
  137. local p = paths[i]
  138. local numPoints = #p:GetPointCoordinates()
  139. n = (n + numPoints)
  140. if (numPoints > 0) then
  141. local test = p:CheckOcclusionAsync(1)
  142. if (test ~= -1) then
  143. return (n - numPoints + test)
  144. end
  145. end
  146. end
  147. return -1
  148. end
  149. return setmetatable(path, {
  150. __metatable = true;
  151. __index = api;
  152. __newindex = function()
  153. error("Cannot edit Path")
  154. end;
  155. })
  156. end
  157.  
  158.  
  159. -------------------------------------------------------------------------------
  160. -- API:
  161.  
  162. function API:FindPath(pointA, pointB)
  163. local points = {}
  164. local paths = {}
  165. local path
  166. local tooLong = false
  167. repeat
  168. path = pathfindingService:ComputeRawPathAsync(pointA, pointB, 500)
  169. table.insert(paths, path)
  170. if (path.Status ~= Enum.PathStatus.FailStartNotEmpty and path.Status ~= Enum.PathStatus.FailFinishNotEmpty) then
  171. for _,pt in pairs(path:GetPointCoordinates()) do
  172. table.insert(points, pt)
  173. end
  174. if (path.Status == Enum.PathStatus.ClosestOutOfRange) then
  175. pointA = points[#points]
  176. if (#paths >= MAX_PATHS) then
  177. tooLong = true
  178. break
  179. end
  180. end
  181. end
  182. until (path.Status ~= Enum.PathStatus.ClosestOutOfRange)
  183. if (#points > 2) then
  184. points = SimplifyPointsStraight(SimplifyPointsDiagonal(points))
  185. end
  186. return CreatePathObject(paths, points, (tooLong and Enum.PathStatus.ClosestOutOfRange or path.Status))
  187. end
  188.  
  189. function API:SetEmptyCutoff(emptyCutoff)
  190. pathfindingService.EmptyCutoff = emptyCutoff
  191. end
  192.  
  193. -------------------------------------------------------------------------------
  194. -- MT:
  195.  
  196. MT.__metatable = true
  197. MT.__index = API
  198. MT.__newindex = function()
  199. error("Cannot edit Pathfinder API")
  200. end
  201. setmetatable(Pathfinder, MT)
  202.  
  203. -------------------------------------------------------------------------------
  204.  
  205. return Pathfinder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement