Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ## Before running, you need two parts, "Goal" and "Start"
- -- ## Put these parts into workspace and put an IntValue named "G" inside of Start.
- local node_Spacing = 2
- -- Required for A* pathfinding algorithm.
- local open_List = {workspace.Start}
- local closed_List = {}
- local goal = workspace.Goal
- local start = workspace.Start
- local valid_Directions = {
- Vector3.new(1, 0, 0), Vector3.new(-1, 0, 0)
- , Vector3.new(1, 0, 1), Vector3.new(-1, 0, -1)
- ,Vector3.new(1, 0, -1), Vector3.new(-1, 0, 1)
- , Vector3.new(0, 0, 1), Vector3.new(0, 0, -1)
- --, Vector3.new(0, 1, 0), Vector3.new(0, -1, 0) [[Is used for vertical movement.]]
- }
- function create_Part(position, parent)
- local part = Instance.new("Part")
- part.Name = "Node"
- part.BrickColor = BrickColor.Blue()
- part.Position = position
- part.Size = Vector3.new(1, 1, 1)
- part.Anchored = true
- part.CanCollide = false
- part.Parent = parent
- local G = Instance.new("IntValue")
- G.Name = "G"
- G.Value = parent.G.Value + 1
- G.Parent = part
- return part
- end
- function cast_Ray(start, direction)
- return workspace:FindPartOnRay(Ray.new(start, direction * node_Spacing))
- end
- function trace_Path(final_Node)
- local current = final_Node
- repeat --wait()
- current.BrickColor = BrickColor.Yellow()
- current = current.Parent
- until current.Parent == workspace
- end
- function scan_Field()
- local final = nil
- repeat --wait()
- local low_F = math.huge
- local best = nil
- for l,node in ipairs(open_List)do
- local H = (node.Position - goal.Position).Magnitude * node_Spacing
- local G = node.G.Value
- if G + H < low_F then
- low_F = G + H
- best = {node, l}
- end
- end
- table.insert(closed_List, best[1])
- if best[1] ~= start then
- best[1].BrickColor = BrickColor.Blue()
- end
- table.remove(open_List, best[2])
- for _,vector in ipairs(valid_Directions)do
- local ray_Part, ray_Pos = cast_Ray(best[1].Position, vector)
- if not ray_Part then
- local part = create_Part(ray_Pos, best[1])
- table.insert(open_List, part)
- end
- end
- final = best[1]
- until #open_List <= 0 or (best[1].Position - goal.Position).Magnitude <= node_Spacing * 1.5
- trace_Path(final)
- end
- --wait(5)
- scan_Field()
- print("Finished Cycling")
- -- Made by 32bitPC
Advertisement
Add Comment
Please, Sign In to add comment