TheDev321

Untitled

Jun 18th, 2021
28
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local algorithm = {}
  2. local util = require(script.Parent.Parent.Parent.Util)
  3. local nodes = {}
  4.  
  5. function algorithm:ComputeNodes(plot, position)
  6. local originalObject = nil
  7. local originalFirst = nil
  8. local path = {}
  9. local scanned = {}
  10. local ready = false
  11. local indexStartReverse = 0
  12.  
  13. local tries = 0
  14.  
  15. local lastGoodConnection = nil
  16.  
  17. local function reverseSearch(object)
  18. tries += 1
  19. local objIndexName = tonumber(object.Name) --string.sub(object.Name, string.len("node"), string.len(object.Name))
  20. object = plot.Placed.WallClass:FindFirstChild(tostring((objIndexName-1)))
  21.  
  22. if object then
  23. local connection = object:GetAttribute("Connected")
  24.  
  25. if connection == lastGoodConnection then
  26. lastGoodConnection = tostring((tonumber(lastGoodConnection)-1))
  27. table.insert(path, object)
  28. reverseSearch(object)
  29. else
  30. ready = true
  31. end
  32. else
  33. ready = true
  34. end
  35.  
  36. if tries > 120 then
  37. ready = true
  38. end
  39. end
  40.  
  41. local function deepSearch(object)
  42. --tries += 1
  43. table.insert(scanned, object)
  44.  
  45. if object:GetAttribute("Connected") then
  46. table.insert(path, object)
  47. local connection = plot.Placed.WallClass:FindFirstChild(object:GetAttribute("Connected"))
  48.  
  49. if util:FindInArray(scanned, object) then
  50. ready = true
  51. end
  52.  
  53. if connection then
  54. if connection.Name ~= originalObject.Name then
  55. deepSearch(connection)
  56. else
  57. ready = true
  58. end
  59. else
  60. ready = true
  61. end
  62. else
  63. table.insert(path, object)
  64. --ready = true
  65.  
  66. lastGoodConnection = originalObject.Name
  67. indexStartReverse = #path
  68. reverseSearch(originalObject)
  69. end
  70. end
  71.  
  72. local nearestWall = {math.huge, nil}
  73. for _, q in pairs(plot.Placed.WallClass:GetChildren()) do
  74. local mag = (position - q.Position).Magnitude
  75. if mag < nearestWall[1] then
  76. nearestWall = {mag, q}
  77. end
  78. end
  79.  
  80. originalObject = nearestWall[2]
  81. deepSearch(originalObject)
  82.  
  83. --print(indexStartReverse)
  84.  
  85. local t = tick()
  86. repeat wait()
  87. if tick() - t > 4 then
  88. --path = {originalObject}
  89. break;
  90. end
  91. until ready
  92.  
  93. warn("Room found! took: "..(tick() - t))
  94.  
  95. return path
  96. end
  97.  
  98. return algorithm
RAW Paste Data