Prehistoricman

Herp derp, sorry about the tabs

Oct 28th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.93 KB | None | 0 0
  1. Start = script.Parent.Start
  2. End = script.Parent.End
  3.  
  4. function Try(position, position2)
  5. --ray = Ray.new(position, (End.Position - position).unit * (End.Position - position).magnitude)
  6. part, place = game.Workspace:FindPartOnRay(Ray.new(position, (position2 - position)))
  7. if part == End then
  8. return true
  9. else
  10. return false
  11. end end
  12.  
  13. function FireRays(position, rays)
  14.  
  15. local part = Instance.new("Part", workspace.Ignore)
  16. part.BrickColor = BrickColor.new("Bright green")
  17. part.Anchored = true
  18. part.Size = Vector3.new(1, 2, 1)
  19. part.CFrame = CFrame.new(position)
  20. --game.Debris:AddItem(part, 30)
  21.  
  22.  
  23. local places = {}
  24. for i = 1, rays do
  25. local part, place = game.Workspace:FindPartOnRay(Ray.new(position, ((CFrame.new(position) * CFrame.Angles(0, math.rad(360/rays * i), 0) * CFrame.new(10, 0, 0)).p - position) .unit * 300), game.Workspace.Ignore)
  26. place = (CFrame.new(place) * (CFrame.Angles(0, math.rad(360/rays * i), 0) * CFrame.new(-0.1, 0, 0))).p
  27. if place and part then
  28. local part = Instance.new("Part", workspace.Ignore)
  29. part.BrickColor = BrickColor.new("Bright red")
  30. part.Anchored = true
  31. part.Size = Vector3.new(1, 1, 1)
  32. part.CFrame = CFrame.new(place)
  33. --game.Debris:AddItem(part, 30)
  34. end
  35. if part then
  36. table.insert(places, place)
  37. end end
  38. return places
  39. end
  40.  
  41. function findintable(tab, object)
  42. local g = false
  43. for i = 1, #tab do
  44. if tab[i] == object then
  45. g = i
  46. break
  47. end end
  48. return g
  49. end
  50.  
  51. function Scan(destination, position, info, ...)
  52. if not findintable(info.visited, position) and not info.foundtarget then
  53. local closest = 5000
  54. for i = 1, #info.visited do
  55. if closest > (info.visited[i] - position).magnitude then
  56. closest = (info.visited[i] - position).magnitude
  57. end end
  58. if closest > 2 then
  59. table.insert(info.visited, position)
  60. local hits = FireRays(position, 5)
  61. local inrange = {}
  62. for i = 1, #hits do
  63. if Try(hits[i], destination) then
  64. table.insert(inrange, hits[i])
  65. end end
  66. if #inrange == 0 then
  67. for i = 1, #hits do
  68. args = ...
  69. delay(0, function ()
  70. Scan(destination, hits[i], info, hits[i], args)
  71. end)
  72. end
  73. else
  74. local closest = inrange[1]
  75. for i = 2, #inrange do
  76. if (inrange[i] - destination).magnitude < (closest - destination).magnitude then
  77. closest = inrange[i]
  78. end end
  79. info.foundtarget = {..., closest}
  80. end end end end
  81.  
  82. function Pathfind(p1, p2)
  83. local vals = {}
  84. vals.visited = {}
  85. vals.foundtarget = false
  86. Scan(p2.Position, p1.Position, vals)
  87. repeat wait() until vals.foundtarget
  88. return foundtarget
  89. end
  90.  
  91. --[[starttime = tick()
  92. Pathfind(Start, End)
  93. endtime = tick()
  94. End.BrickColor = BrickColor.new("Really red")
  95. print("Target found. Time elapsed: ".. endtime - starttime)]]
  96.  
  97. debounce = false
  98. game.Workspace.SL.CD.MouseClick:connect(function ()
  99. if not debounce then
  100. End.BrickColor = BrickColor.new("Bright green")
  101. game.Workspace.Ignore:Destroy()
  102. Instance.new("Model", workspace).Name = "Ignore"
  103. debounce = true
  104. Pathfind(Start, End)
  105. End.BrickColor = BrickColor.new("Really red")
  106. debounce = false
  107. end end)
Advertisement
Add Comment
Please, Sign In to add comment