Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --VolcanoINC's Pathfinding Functions
- PathFinder = {}
- PathFinder.resetValues = function()
- local rstore = game:GetService("ReplicatedStorage")
- local nodes = rstore:findFirstChild("PathNodes")
- local a = nodes:children()
- local b
- for _,b in pairs(a) do
- local c = b:findFirstChild("PathValue")
- if (c) then
- c.Value = -1
- end
- end
- end
- PathFinder.calcValues = function(startNode,sDist)
- startNode.PathValue.Value = sDist
- local content = startNode:children()
- local val
- for _,val in pairs(content) do
- if (val:IsA("ObjectValue") and val.Name == "Step") then
- local targ = val.Value
- local d = math.floor((startNode.Position - targ.Position).magnitude+0.5)
- if (targ.PathValue.Value > (sDist+d) or targ.PathValue.Value == -1) then
- PathFinder.calcValues(targ,sDist+d)
- end
- end
- end
- end
- PathFinder.findPathFrom = function(sNode)
- local cNode = sNode
- local path = {cNode}
- if (sNode.PathValue.Value == -1) then return path,false end
- while true do
- local content = cNode:children()
- local val
- local best = nil
- local dist = math.huge
- for _,val in pairs(content) do
- if (val:IsA("ObjectValue") and val.Name == "Step") then
- local targ = val.Value
- if (targ.PathValue.Value < cNode.PathValue.Value) then
- if (dist > targ.PathValue.Value) then
- best = targ
- dist = targ.PathValue.Value
- end
- end
- end
- end
- if (best) then
- cNode = best
- table.insert(path,cNode)
- if (cNode.PathValue.Value == 0) then return path,true end
- end
- end
- end
- PathFinder.findPath = function(from,to)
- if (from == to) then return {from},true end
- PathFinder.resetValues()
- PathFinder.calcValues(to,0)
- return PathFinder.findPathFrom(from)
- end
- PathFinder.findClosestNode = function(from)
- local rstore = game:GetService("ReplicatedStorage")
- local nodes = rstore:findFirstChild("PathNodes")
- if (nodes) then
- nodes = nodes:children()
- local node
- local mDist = math.huge
- local close = nil
- for _,node in pairs(nodes) do
- if (string.find(node.Name,"PathNode")) then
- local d = (from - node.Position).magnitude
- if (d < mDist) then
- close = node
- mDist = d
- end
- end
- end
- return close,mDist
- end
- end
- return PathFinder
Add Comment
Please, Sign In to add comment