tyridge77

Untitled

Sep 10th, 2015
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.26 KB | None | 0 0
  1. --VolcanoINC's Pathfinding Functions
  2. PathFinder = {}
  3.  
  4. PathFinder.resetValues = function()
  5.     local rstore = game:GetService("ReplicatedStorage")
  6.     local nodes = rstore:findFirstChild("PathNodes")
  7.    
  8.     local a = nodes:children()
  9.     local b
  10.     for _,b in pairs(a) do
  11.         local c = b:findFirstChild("PathValue")
  12.         if (c) then
  13.             c.Value = -1
  14.         end
  15.     end
  16. end
  17.  
  18. PathFinder.calcValues = function(startNode,sDist)
  19.     startNode.PathValue.Value = sDist
  20.    
  21.     local content = startNode:children()
  22.     local val
  23.     for _,val in pairs(content) do
  24.         if (val:IsA("ObjectValue") and val.Name == "Step") then
  25.             local targ = val.Value
  26.            
  27.             local d = math.floor((startNode.Position - targ.Position).magnitude+0.5)
  28.             if (targ.PathValue.Value > (sDist+d) or targ.PathValue.Value == -1) then
  29.                 PathFinder.calcValues(targ,sDist+d)
  30.             end
  31.         end
  32.     end
  33. end
  34.  
  35. PathFinder.findPathFrom = function(sNode)
  36.     local cNode = sNode
  37.     local path = {cNode}
  38.    
  39.     if (sNode.PathValue.Value == -1) then return path,false end
  40.    
  41.     while true do
  42.         local content = cNode:children()
  43.         local val
  44.        
  45.         local best = nil
  46.         local dist = math.huge
  47.         for _,val in pairs(content) do
  48.             if (val:IsA("ObjectValue") and val.Name == "Step") then
  49.                 local targ = val.Value
  50.                
  51.                 if (targ.PathValue.Value < cNode.PathValue.Value) then
  52.                     if (dist > targ.PathValue.Value) then
  53.                         best = targ
  54.                         dist = targ.PathValue.Value
  55.                     end
  56.                 end
  57.             end
  58.         end
  59.        
  60.         if (best) then
  61.             cNode = best
  62.             table.insert(path,cNode)
  63.             if (cNode.PathValue.Value == 0) then return path,true end
  64.         end
  65.     end
  66. end
  67.  
  68. PathFinder.findPath = function(from,to)
  69.     if (from == to) then return {from},true end
  70.     PathFinder.resetValues()
  71.     PathFinder.calcValues(to,0)
  72.     return PathFinder.findPathFrom(from)
  73. end
  74.  
  75. PathFinder.findClosestNode = function(from)
  76.     local rstore = game:GetService("ReplicatedStorage")
  77.     local nodes = rstore:findFirstChild("PathNodes")
  78.    
  79.     if (nodes) then
  80.         nodes = nodes:children()
  81.         local node
  82.         local mDist = math.huge
  83.         local close = nil
  84.        
  85.         for _,node in pairs(nodes) do
  86.             if (string.find(node.Name,"PathNode")) then
  87.                 local d = (from - node.Position).magnitude
  88.                 if (d < mDist) then
  89.                     close = node
  90.                     mDist = d
  91.                 end
  92.             end
  93.         end
  94.        
  95.         return close,mDist
  96.     end
  97. end
  98.  
  99. return PathFinder
Add Comment
Please, Sign In to add comment