Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1. --Don't touch
  2. local pathLib = {}
  3. local master_node_table, mnt_index
  4. local NormalIds = Enum.NormalId:GetEnumItems()
  5.  
  6.  
  7. --Touch
  8. local ai_max_range = math.huge
  9. local printAStarPerformance = false
  10. local Orakel = require(game.ReplicatedStorage.Abyss.Main)
  11.  
  12. function pathLib.SearchById(masterTable, searchId)
  13. for i, j in pairs(masterTable) do
  14. if j.ID == searchId then
  15. return j.Brick
  16. end
  17. end
  18. return nil
  19. end
  20.  
  21.  
  22. function pathLib.SearchByBrick(masterTable, brick)
  23. for i, j in pairs(masterTable) do
  24. if j.Brick == brick then
  25. return j.ID
  26. end
  27. end
  28. return nil
  29. end
  30.  
  31. function pathLib.GetNodeData(masterTable, node)
  32. for _, nodeInTable in pairs(masterTable) do
  33. if type(node) == "number" then
  34. if nodeInTable.ID == node then
  35. return nodeInTable
  36. end
  37. else
  38. if nodeInTable.Brick == node then
  39. return nodeInTable
  40. end
  41. end
  42. end
  43. return nil
  44. end
  45.  
  46.  
  47. function DrawColoredSurface(part, face)
  48. local surfGui = Instance.new("SurfaceGui", part)
  49. surfGui.Name = tostring(face).."Layer"
  50. surfGui.CanvasSize = Vector2.new(64, 64)
  51. surfGui.Adornee = part
  52. surfGui.Face = face
  53. surfGui.Enabled = true
  54. local lab = Instance.new("ImageLabel", surfGui)
  55. lab.Size = UDim2.new(1, 0, 1, 0)
  56. lab.BorderSizePixel = 1
  57. return lab
  58. end
  59.  
  60.  
  61. local function DrawColoredSurfaces(part, color, transparency)
  62. local labs = {}
  63. for label = 1, 6 do
  64. labs[label] = DrawColoredSurface(part, NormalIds[label])
  65. end
  66. for _, v in pairs(labs) do
  67. v.BackgroundColor3 = color
  68. v.BackgroundTransparency = transparency
  69. v.BorderColor3 = color
  70. end
  71. end
  72.  
  73.  
  74.  
  75.  
  76. function DrawLine(p, a, b, c)
  77. local distance = (b.Position - a.Position).magnitude
  78. local part = Instance.new("Part", p)
  79. part.Transparency = 1
  80. part.Anchored = true
  81. part.CanCollide = false
  82. part.TopSurface = Enum.SurfaceType.Smooth
  83. part.BottomSurface = Enum.SurfaceType.Smooth
  84. part.formFactor = Enum.FormFactor.Custom
  85. part.Size = Vector3.new(0.2, 0.2, distance)
  86. part.CFrame = CFrame.new(b.Position, a.Position) * CFrame.new(0, 0, -distance/2)
  87. part.Archivable = false
  88. local pl = Instance.new("PointLight", part)
  89. pl.Shadows = true
  90. pl.Range = 16
  91. pl.Color = c
  92. DrawColoredSurfaces(part, c, 0.2)
  93. return part
  94. end
  95.  
  96.  
  97. function pathLib.DrawPath(path)
  98. local drawn = Instance.new("Model", workspace)
  99. drawn.Name = "DrawnPath"
  100. local red, green, last
  101. local num = 0
  102. local pLen = #path
  103. local colorInterval = (255 / pLen) / 255
  104. local a = path[1]
  105. local b = path[pLen]
  106. for _, p in pairs(path) do
  107. if num < 255 then
  108. num = num + 1
  109. end
  110. if not (last == nil)then
  111. red = num * colorInterval
  112. DrawLine(drawn, last, p, Color3.new(1 - red, red, 0))
  113. end
  114. last = p
  115. end
  116. return drawn
  117. end
  118.  
  119.  
  120.  
  121. function NodeObjectFromBrick(brick, weighted, mainpath)
  122. print(weighted)
  123. if brick.ClassName ~= "Part" then
  124. return nil
  125. end
  126. local node_index = mnt_index
  127. master_node_table[node_index] = {
  128. ID = mnt_index,
  129. Brick = brick,
  130. Connections = {},
  131. Weight = math.random() * 255
  132. }
  133.  
  134. local applyWeighting = weighted
  135.  
  136. if mainpath ~= nil then
  137. for _, nodeonpath in pairs(mainpath) do
  138. if brick == nodeonpath then
  139. applyWeighting = true
  140. master_node_table[node_index].Weight = 2550
  141. end
  142. end
  143. end
  144.  
  145. for i, child in pairs(brick:GetChildren()) do
  146. if child.ClassName == "ObjectValue" and child.Name == "connection" then
  147. if child.Value == nil then
  148. error("Connection didn't have a node reference (it was nil), This usually happens when a node is removed without removing its connections. Fix your nodegraph.")
  149. end
  150.  
  151.  
  152. local brick2 = child.Value
  153. local ID = pathLib.SearchByBrick(master_node_table, brick2)
  154. local ID2 = pathLib.SearchByBrick(master_node_table, brick)
  155. if ID == nil then
  156. mnt_index = mnt_index + 1
  157. ID = NodeObjectFromBrick(brick2, weighted, mainpath)
  158. end
  159. local con = {
  160. ID = ID;
  161. G = (master_node_table[ID].Brick.Position - brick.Position).magnitude;
  162. }
  163. if applyWeighting then
  164. con = {
  165. ID = ID;
  166. G = master_node_table[ID].Weight;
  167. }
  168. end
  169. table.insert(master_node_table[node_index].Connections, con)
  170. end
  171. end
  172. return node_index
  173. end
  174.  
  175.  
  176. function pathLib.CollectNodes(model, weighted, mainpath)
  177. master_node_table = {}
  178. mnt_index = 1
  179. for i, child in pairs(model:GetChildren()) do
  180. if child.ClassName == "Part" and pathLib.SearchByBrick(master_node_table, child) == nil then
  181. if NodeObjectFromBrick(child, weighted, mainpath) then
  182. mnt_index = mnt_index + 1
  183. end
  184. end
  185. end
  186. return master_node_table, mnt_index
  187. end
  188.  
  189.  
  190. function Heuristic(id1, id2, weighted)
  191. local p1 = master_node_table[id1].Brick.Position
  192. local p2 = master_node_table[id2].Brick.Position
  193. if weighted then
  194. return 0 -- (p1 - p2).magnitude * (master_node_table[id2].Weight * 5)
  195. else
  196. return (p1 - p2).magnitude
  197. end
  198. end
  199.  
  200.  
  201. function Len(t)
  202. local l = 0
  203. for i, j in pairs(t) do
  204. if j ~= nil then
  205. l = l + 1
  206. end
  207. end
  208. return l
  209. end
  210.  
  211.  
  212. function GetPath(t, n)
  213. if t[n] == nil then
  214. return {n}
  215. else
  216. local t2 = GetPath(t, t[n])
  217. table.insert(t2, n)
  218. return t2
  219. end
  220. end
  221.  
  222.  
  223. function pathLib.GetFarthestNode(position, returnBrick, dir, masterTable)
  224. local nodeDir = dir
  225. if type(dir) ~= "table" then
  226. nodeDir = dir:children()
  227. end
  228. local farthest = nodeDir[1]
  229. for n = 2, #nodeDir do
  230. local old = (farthest.Position - position).magnitude
  231. local new = (position - nodeDir[n].Position).magnitude
  232. if new > old then
  233. farthest = nodeDir[n]
  234. end
  235. end
  236. if returnBrick then
  237. return farthest
  238. else
  239. return pathLib.SearchByBrick(masterTable, farthest)
  240. end
  241. end
  242.  
  243.  
  244. function pathLib.GetNearestNode(position, returnBrick, dir, masterTable)
  245. local nodeDir = dir
  246. if type(dir) ~= "table" then
  247. nodeDir = dir:children()
  248. end
  249. local nearest = nodeDir[1]
  250. for n = 2, #nodeDir do
  251. local old = (nearest.Position - position).magnitude
  252. local new = (position - nodeDir[n].Position).magnitude
  253. if new < old then
  254. nearest = nodeDir[n]
  255. end
  256. end
  257. if returnBrick then
  258. return nearest
  259. else
  260. return pathLib.SearchByBrick(masterTable, nearest)
  261. end
  262. end
  263.  
  264.  
  265. function pathLib.GetNearestNodeVis(subject, position, visRange, returnBrick, dir, masterTable, ignore)
  266. local nodeDir = dir
  267. if type(dir) ~= "table" then
  268. nodeDir = dir:children()
  269. end
  270. local nearest = nodeDir[1]
  271. for n = 2, #nodeDir do
  272. local old = (nearest.Position - position).magnitude
  273. local new = (position - nodeDir[n].Position).magnitude
  274. if new < old then
  275. nearest = nodeDir[n]
  276. end
  277. end
  278.  
  279. local ray = Ray.new(subject.Position,(nearest.Position - subject.Position).unit * visRange)
  280. local lineOfSight = workspace:FindPartOnRayWithIgnoreList(ray, ignore)
  281.  
  282. if lineOfSight then
  283. if returnBrick then
  284. return nearest
  285. else
  286. return pathLib.SearchByBrick(masterTable, nearest)
  287. end
  288. end
  289. return nil
  290. end
  291.  
  292.  
  293.  
  294.  
  295. function pathLib.GetNodesVisibleInRadius(position, radius, returnBrick, dir, masterTable, ignore)
  296. local list = {}
  297. local map = Orakel.GetMap()
  298. for _, node in pairs(dir:GetChildren()) do
  299. local dist = (node.Position - position).magnitude
  300. if dist <= radius then
  301. local ray = Ray.new(position,(node.Position-position).unit * dist)
  302. local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, ignore or {})
  303. if hit ~= nil then
  304. if hit == node then
  305. if returnBrick then
  306. table.insert(list, node)
  307. else
  308. table.insert(list, pathLib.SearchByBrick(node))
  309. end
  310. end
  311. end
  312. end
  313. end
  314. return list
  315. end
  316.  
  317.  
  318. function pathLib.AStar(masterTable, startID, endID, weighted)
  319. local takeSafestPath = false
  320. local now = tick()
  321. local closed = {}
  322. local open = {startID}
  323. local previous = {}
  324. local g_score = {}
  325. local f_score = {}
  326.  
  327. g_score[startID] = 0
  328. f_score[startID] = Heuristic(startID, endID, weighted)
  329.  
  330. while Len(open) > 0 do
  331. local current, current_i = nil, nil
  332. for i, j in pairs(open) do
  333. if current == nil then
  334. current = j
  335. current_i = i
  336. else
  337. if j ~= nil then
  338. if f_score[j] < f_score[current] then
  339. current = j
  340. current_i = i
  341. end
  342. end
  343. end
  344. end
  345.  
  346. if current == endID then
  347. local path = GetPath(previous, endID)
  348. local ret = {}
  349. for i, j in pairs(path) do
  350. table.insert(ret, masterTable[j].Brick)
  351. end
  352. if printAStarPerformance then
  353. print("Time taken for AStar to run: "..tostring(tick() - now))
  354. end
  355. return ret
  356. end
  357.  
  358. open[current_i] = nil
  359. table.insert(closed, current)
  360.  
  361. for i, j in pairs(masterTable[current].Connections) do
  362. local in_closed = false
  363. for k, l in pairs(closed) do
  364. if l == j.ID then
  365. in_closed = true
  366. break
  367. end
  368. end
  369. if in_closed == false then
  370. local tentative_score = g_score[current] + j.G
  371. local in_open = false
  372. for k, l in pairs(open) do
  373. if l == j.ID then
  374. in_open = true
  375. break
  376. end
  377. end
  378. if in_open == false or tentative_score < g_score[j.ID] then
  379. previous[j.ID] = current
  380. g_score[j.ID] = tentative_score
  381. f_score[j.ID] = g_score[j.ID] + Heuristic(j.ID, endID, weighted)
  382. if in_open == false then
  383. table.insert(open, j.ID)
  384. end
  385. end
  386. end
  387. end
  388. end
  389. return nil
  390. end
  391.  
  392.  
  393. return pathLib
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement