_James

Open Source Pathfinding Script

Mar 26th, 2015
4,933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.24 KB | None | 0 0
  1. -- ## Before running, you need two parts, "Goal" and "Start"
  2. -- ## Put these parts into workspace and put an IntValue named "G" inside of Start.
  3.  
  4.  
  5. local node_Spacing = 2
  6.  
  7. -- Required for A* pathfinding algorithm.
  8. local open_List = {workspace.Start}
  9. local closed_List = {}
  10.  
  11. local goal = workspace.Goal
  12. local start = workspace.Start
  13.  
  14. local valid_Directions = {
  15. Vector3.new(1, 0, 0), Vector3.new(-1, 0, 0)
  16. , Vector3.new(1, 0, 1), Vector3.new(-1, 0, -1)
  17. ,Vector3.new(1, 0, -1), Vector3.new(-1, 0, 1)
  18. , Vector3.new(0, 0, 1), Vector3.new(0, 0, -1)
  19. --, Vector3.new(0, 1, 0), Vector3.new(0, -1, 0) [[Is used for vertical movement.]]
  20. }
  21.  
  22. function create_Part(position, parent)
  23.     local part = Instance.new("Part")
  24.     part.Name = "Node"
  25.     part.BrickColor = BrickColor.Blue()
  26.     part.Position = position
  27.     part.Size = Vector3.new(1, 1, 1)
  28.     part.Anchored = true
  29.     part.CanCollide = false
  30.     part.Parent = parent
  31.    
  32.     local G = Instance.new("IntValue")
  33.     G.Name = "G"
  34.     G.Value = parent.G.Value + 1
  35.     G.Parent = part
  36.    
  37.     return part
  38. end
  39.  
  40. function cast_Ray(start, direction)
  41.     return workspace:FindPartOnRay(Ray.new(start, direction * node_Spacing))
  42. end
  43.  
  44. function trace_Path(final_Node)
  45.     local current = final_Node
  46.     repeat --wait()
  47.         current.BrickColor = BrickColor.Yellow()
  48.         current = current.Parent
  49.     until current.Parent == workspace
  50. end
  51.  
  52. function scan_Field()
  53.     local final = nil
  54.     repeat --wait()
  55.         local low_F = math.huge
  56.         local best = nil
  57.         for l,node in ipairs(open_List)do
  58.             local H = (node.Position - goal.Position).Magnitude * node_Spacing
  59.             local G = node.G.Value
  60.             if G + H < low_F then
  61.                 low_F = G + H
  62.                 best = {node, l}
  63.             end
  64.         end
  65.         table.insert(closed_List, best[1])
  66.         if best[1] ~= start then
  67.             best[1].BrickColor = BrickColor.Blue()
  68.         end
  69.         table.remove(open_List, best[2])
  70.        
  71.         for _,vector in ipairs(valid_Directions)do
  72.             local ray_Part, ray_Pos = cast_Ray(best[1].Position, vector)
  73.             if not ray_Part then
  74.                 local part = create_Part(ray_Pos, best[1])
  75.                 table.insert(open_List, part)
  76.             end
  77.         end
  78.         final = best[1]
  79.     until #open_List <= 0 or (best[1].Position - goal.Position).Magnitude <= node_Spacing * 1.5
  80.     trace_Path(final)
  81. end
  82.  
  83. --wait(5)
  84.  
  85. scan_Field()
  86.  
  87. print("Finished Cycling")
  88. -- Made by 32bitPC
Advertisement
Add Comment
Please, Sign In to add comment