Advertisement
Vaeb

Maze Generation

Dec 11th, 2015
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.09 KB | None | 0 0
  1. --//Recursive Backtracking Maze (Created By Vaeb) (ServerSide)
  2.  
  3. wait(1/30)
  4.  
  5. local Plrs = game:GetService("Players")
  6. local LP = Plrs.Vaeb
  7.  
  8. local IsRandom = true --false
  9. local FullX = 21 --21
  10. local FullY = 21 --21
  11. local CellWidth = 14 --12
  12. local WallWidth = 2 --2
  13. local FullHeight = 13 --13
  14. local ElasticNum = 1 --1
  15.  
  16. local HalfHeight = FullHeight/2
  17.  
  18. local WaitNum = 0
  19. local Checking = false
  20.  
  21. local EndHeight = 0.2
  22. local EndCFY = EndHeight/2
  23.  
  24. local BaseOffset = -60 --(FullY*CellWidth)/2 - CellWidth/2 OR -30
  25.  
  26. local StartCF = workspace.Base.CFrame * CFrame.new(-((FullX*CellWidth)/2), workspace.Base.Size.Y/2, (-FullY*CellWidth) + BaseOffset) --(Goes Down-Right)
  27.  
  28. local StartCellNumX = math.ceil(FullX/2)
  29. local StartCellNumY = math.ceil(FullY/2)
  30. local HalfWall = WallWidth/2
  31. local RealCellWidth = CellWidth - WallWidth
  32. local AddedCellWidth = CellWidth + WallWidth
  33. local WallOffset = RealCellWidth/2 + HalfWall
  34. local ExtraCellWidth = CellWidth + HalfWall
  35.  
  36. local NodeWidth = RealCellWidth - 1.5
  37.  
  38. local EndPart = nil
  39.  
  40. local HasChanged = {}
  41.  
  42. local CF = CFrame.new
  43. local Heartbeat = game:GetService("RunService").Heartbeat
  44.  
  45. local HitCon = nil
  46. local MazeCons = {}
  47.  
  48. local pow = math.pow
  49. local sin = math.sin
  50. local cos = math.cos
  51. local pi = math.pi
  52. local sqrt = math.sqrt
  53. local abs = math.abs
  54. local asin  = math.asin
  55.  
  56. local StepCons = {}
  57.  
  58. local CellPart = Instance.new("Part")
  59. CellPart.Name = "MazePart"
  60. CellPart.FormFactor = "Custom"
  61. CellPart.Locked = true
  62. CellPart.BrickColor = BrickColor.new("Bright yellow")
  63. CellPart.Material = "Neon"
  64. CellPart.Anchored = true
  65. CellPart.CanCollide = false
  66. CellPart.TopSurface = "SmoothNoOutlines"
  67. CellPart.BottomSurface = "SmoothNoOutlines"
  68. CellPart.FrontSurface = "SmoothNoOutlines"
  69. CellPart.BackSurface = "SmoothNoOutlines"
  70. CellPart.LeftSurface = "SmoothNoOutlines"
  71. CellPart.RightSurface = "SmoothNoOutlines"
  72.  
  73. local MazePart = Instance.new("Part")
  74. MazePart.Name = "MazePart"
  75. MazePart.FormFactor = "Custom"
  76. MazePart.Locked = true
  77. MazePart.BrickColor = BrickColor.new("Camo")
  78. MazePart.Anchored = true
  79. MazePart.CanCollide = true
  80. MazePart.Material = "Grass"
  81. MazePart.TopSurface = "SmoothNoOutlines"
  82. MazePart.BottomSurface = "SmoothNoOutlines"
  83. MazePart.FrontSurface = "SmoothNoOutlines"
  84. MazePart.BackSurface = "SmoothNoOutlines"
  85. MazePart.LeftSurface = "SmoothNoOutlines"
  86. MazePart.RightSurface = "SmoothNoOutlines"
  87.  
  88. local function QuaternionFromCFrame(cf)
  89.     local mx, my, mz, m00, m01, m02, m10, m11, m12, m20, m21, m22 = cf:components();
  90.     local trace = m00 + m11 + m22 if trace > 0 then
  91.         local s = math.sqrt(1 + trace);
  92.         local recip = 0.5/s;
  93.         return (m21-m12)*recip, (m02-m20)*recip, (m10-m01)*recip, s*0.5;
  94.     else
  95.         local i = 0;
  96.         if m11 > m00 then
  97.             i = 1;
  98.         end;
  99.         if m22 > (i == 0 and m00 or m11) then
  100.             i = 2 end if i == 0 then
  101.             local s = math.sqrt(m00-m11-m22+1);
  102.             local recip = 0.5/s return 0.5*s, (m10+m01)*recip, (m20+m02)*recip, (m21-m12)*recip;
  103.         elseif i == 1 then
  104.             local s = math.sqrt(m11-m22-m00+1);
  105.             local recip = 0.5/s;
  106.             return (m01+m10)*recip, 0.5*s, (m21+m12)*recip, (m02-m20)*recip ;
  107.         elseif i == 2 then
  108.             local s = math.sqrt(m22-m00-m11+1);
  109.             local recip = 0.5/s;
  110.             return (m02+m20)*recip, (m12+m21)*recip, 0.5*s, (m10-m01)*recip;
  111.         end;
  112.     end;
  113. end;
  114.  
  115. local function QuaternionToCFrame(px, py, pz, x, y, z, w)
  116.     local xs, ys, zs = x + x, y + y, z + z;
  117.     local wx, wy, wz = w*xs, w*ys, w*zs;
  118.     local xx = x*xs;
  119.     local xy = x*ys;
  120.     local xz = x*zs;
  121.     local yy = y*ys;
  122.     local yz = y*zs;
  123.     local zz = z*zs;
  124.     return CFrame.new(px, py, pz,1-(yy+zz), xy - wz, xz + wy,xy + wz, 1-(xx+zz), yz - wx, xz - wy, yz + wx, 1-(xx+yy))
  125. end;
  126.  
  127. local function QuaternionSlerp(a, b, t)
  128.     local cosTheta = a[1]*b[1] + a[2]*b[2] + a[3]*b[3] + a[4]*b[4];
  129.     local startInterp, finishInterp;
  130.     if cosTheta >= 0.0001 then
  131.         if (1 - cosTheta) > 0.0001 then
  132.             local theta = math.acos(cosTheta);
  133.             local invSinTheta = 1/math.sin(theta);
  134.             startInterp = math.sin((1-t)*theta)*invSinTheta;
  135.             finishInterp = math.sin(t*theta)*invSinTheta;
  136.         else
  137.             startInterp = 1-t finishInterp = t;
  138.         end;
  139.     else
  140.         if (1+cosTheta) > 0.0001 then
  141.             local theta = math.acos(-cosTheta);
  142.             local invSinTheta = 1/math.sin(theta);
  143.             startInterp = math.sin((t-1)*theta)*invSinTheta;
  144.             finishInterp = math.sin(t*theta)*invSinTheta;
  145.         else startInterp = t-1 finishInterp = t;
  146.         end;
  147.     end;
  148.     return a[1]*startInterp + b[1]*finishInterp, a[2]*startInterp + b[2]*finishInterp, a[3]*startInterp + b[3]*finishInterp, a[4]*startInterp + b[4]*finishInterp;
  149. end;
  150.  
  151. local function outElastic(t, b, c, d, a, p)
  152.   if t == 0 then return b end
  153.  
  154.   t = t / d
  155.  
  156.   if t == 1 then return b + c end
  157.  
  158.   if not p then p = d * 0.3 end
  159.  
  160.   local s
  161.  
  162.   if not a or a < abs(c) then
  163.     a = c
  164.     s = p / 4
  165.   else
  166.     s = p / (2 * pi) * asin(c/a)
  167.   end
  168.  
  169.   return a * pow(2, -10 * t) * sin((t * d - s) * (2 * pi) / p) + c + b
  170. end
  171.  
  172. function VLerpEaseElas(a,b,t)
  173.     a = CFrame.new(a)
  174.     b = CFrame.new(b)
  175.     local qa={QuaternionFromCFrame(a)};
  176.     local qb={QuaternionFromCFrame(b)};
  177.     local ax,ay,az=a.x,a.y,a.z;
  178.     local bx,by,bz=b.x,b.y,b.z;
  179.     local per = 0.5 --lower per=increase elasticity
  180.     return QuaternionToCFrame(outElastic(t, ax, bx-ax, 1, nil, per), outElastic(t, ay, by-ay, 1, nil, per), outElastic(t, az, bz-az, 1, nil, per), QuaternionSlerp(qa, qb, t)).p;
  181. end
  182.  
  183. --[[function Rand(min, max, accuracy, seed)
  184.     accuracy = (accuracy and accuracy > 100 and 100) or 100
  185.     seed = seed or (os and os.time() or tick())
  186.     function randomizeSeed(seed)
  187.         -- Make our seed more "random" so a number only repeats more than once pseudo randomly
  188.         local sum = 0
  189.         for n = 1, 100, 101 - accuracy do
  190.             local num = (seed  * n) % tonumber(string.reverse(n)) -- Something I just went with
  191.             sum = sum + num
  192.         end
  193.         return seed % sum
  194.     end
  195.     local reseed = randomizeSeed(seed)
  196.     local rnum = reseed % (max + 1 - min) + min
  197.     return math.floor(rnum)
  198. end]]
  199.  
  200. local Rand = math.random
  201.  
  202. local RandX = Rand(1, FullX)
  203. local RandY = Rand(1, FullY)
  204.  
  205. local CellStack = {}
  206. local Grid = {}
  207. local Discared = {}
  208.  
  209. function OnHit(Hit)
  210.     if Hit.Parent and Plrs:GetPlayerFromCharacter(Hit.Parent) and Checking == false then
  211.         Checking = true
  212.         local WonChar = Hit.Parent
  213.         HitCon:disconnect()
  214.         HasChanged[EndPart] = "brickcolor"
  215.         EndPart.BrickColor = BrickColor.new("Bright red")
  216.         local Hint = Instance.new("Hint", workspace)
  217.         Hint.Text = WonChar.Name .. " won the maze game!"
  218.         wait(4)
  219.         if Hint and Hint.Parent then
  220.             Hint:Destroy()
  221.         end
  222.         Checking = false
  223.     end
  224. end
  225.  
  226. for _,v in pairs(workspace:GetChildren()) do
  227.     if v.Name == "MazePart" then
  228.         v:Destroy()
  229.     end
  230. end
  231.  
  232. function ProtObj(Obj)
  233.     coroutine.wrap(function()
  234.         local ObjClone = Obj:Clone()
  235.         local StepIndex = #StepCons+1
  236.         StepCons[StepIndex] = Obj.Changed:connect(function(P)
  237.             if HasChanged[Obj] == nil or HasChanged[Obj] ~= P:lower() then
  238.                 for i = 1, FullX do
  239.                     for j = 1, FullY do
  240.                         for q = 2, 5 do
  241.                             if Grid[i][j][q] and Grid[i][j][q][1] == Obj then
  242.                                 Grid[i][j][q][1] = ObjClone
  243.                             end
  244.                         end
  245.                     end
  246.                 end
  247.                 if Obj.Parent ~= nil then
  248.                     Obj:Destroy()
  249.                 end
  250.                 ObjClone.Parent = workspace
  251.                 if Obj == EndPart then
  252.                     EndPart = ObjClone
  253.                     HitCon:disconnect()
  254.                     HitCon = nil
  255.                     Checking = false
  256.                     HitCon = EndPart.Touched:connect(OnHit)
  257.                 end
  258.                 ProtObj(ObjClone)
  259.             else
  260.                 if Obj.Parent ~= nil then
  261.                     ObjClone = Obj:Clone()
  262.                     wait(1/30)
  263.                     HasChanged[Obj] = nil
  264.                 end
  265.             end
  266.         end)
  267.     end)()
  268. end
  269.  
  270. local function CreateGrid()
  271.     for i = 1, FullX do
  272.         Grid[i] = {}
  273.         Discared[i] = {}
  274.         for j = 1, FullY do
  275.             Grid[i][j] = {3}
  276.             if i == RandX and j == RandY then
  277.                 local NewSize = Vector3.new(NodeWidth, EndHeight, NodeWidth)
  278.                 local NewCFrame = StartCF * CFrame.new(i*CellWidth, EndCFY, j*CellWidth)
  279.  
  280.                 EndPart = CellPart:Clone()
  281.                 EndPart.Transparency = 1
  282.                 EndPart.Size = NewSize
  283.                 EndPart.CFrame = NewCFrame
  284.                 EndPart.Parent = workspace
  285.  
  286.                 ProtObj(EndPart)
  287.  
  288.                 Checking = false
  289.                 HitCon = EndPart.Touched:connect(OnHit)
  290.             end
  291.         end
  292.     end
  293. end
  294.  
  295. local function CreateWalls()
  296.     local DoneX = false
  297.     local DoneY = false
  298.  
  299.     coroutine.wrap(function()
  300.         for j = 1, FullY do
  301.             local LastTab = {}
  302.  
  303.             for i = 1, FullX+1 do
  304.                 local NewSize = Vector3.new(WallWidth, FullHeight, AddedCellWidth)
  305.                 local NewCFrame = StartCF * CFrame.new(i*CellWidth - WallOffset, HalfHeight, j*CellWidth)
  306.  
  307.                 local Part = MazePart:Clone()
  308.                 Part.Size = NewSize
  309.                 Part.CFrame = NewCFrame
  310.                 Part.Parent = workspace
  311.  
  312.                 ProtObj(Part)
  313.  
  314.                 local NewTab = {Part}
  315.  
  316.                 if i ~= 1 then
  317.                     local newco = i-1
  318.                     local FoundCell = Grid[newco][j]
  319.                     FoundCell[2] = NewTab
  320.                     FoundCell[3] = LastTab
  321.                 end
  322.  
  323.                 LastTab = NewTab
  324.             end
  325.  
  326.             WaitNum = WaitNum + 1
  327.             if WaitNum % 4 == 0 then
  328.                 Heartbeat:wait()
  329.             end
  330.         end
  331.         DoneX = true
  332.     end)()
  333.  
  334.     coroutine.wrap(function()
  335.         for i = 1, FullX do
  336.             local LastTab = {}
  337.  
  338.             for j = 1, FullY+1 do
  339.                 local NewSize = Vector3.new(AddedCellWidth, FullHeight, WallWidth)
  340.                 local NewCFrame = StartCF * CFrame.new(i*CellWidth, HalfHeight, j*CellWidth - WallOffset)
  341.  
  342.                 local Part = MazePart:Clone()
  343.                 Part.Size = NewSize
  344.                 Part.CFrame = NewCFrame
  345.                 Part.Parent = workspace
  346.  
  347.                 ProtObj(Part)
  348.  
  349.                 local NewTab = {Part}
  350.  
  351.                 if j ~= 1 then
  352.                     local newco = j-1
  353.                     local FoundCell = Grid[i][newco]
  354.                     FoundCell[4] = LastTab
  355.                     FoundCell[5] = NewTab
  356.                 end
  357.  
  358.                 LastTab = NewTab
  359.             end
  360.  
  361.             WaitNum = WaitNum + 1
  362.             if WaitNum % 4 == 0 then
  363.                 Heartbeat:wait()
  364.             end
  365.         end
  366.         DoneY = true
  367.     end)()
  368.  
  369.     repeat wait(1/30) until DoneX == true and DoneY == true
  370. end
  371.  
  372. local function ChooseStart()
  373.     if IsRandom == true then
  374.         local RandX = Rand(1, FullX)
  375.         local RandY = Rand(1, FullY)
  376.         CellStack[1] = {RandX, RandY}
  377.     else
  378.         CellStack[1] = {StartCellNumX, FullY}
  379.     end
  380. end
  381.  
  382. local function NotInStack(X, Y)
  383.     if Discared[X][Y] ~= nil then
  384.         return false
  385.     end
  386.     for i = 1, #CellStack do
  387.         local TempCell = CellStack[i]
  388.         if TempCell[1] == X and TempCell[2] == Y then
  389.             return false
  390.         end
  391.     end
  392.     return true
  393. end
  394.  
  395. local function GetNear(X, Y)
  396.     local Nearby = {}
  397.     if WaitNum % 40 == 0 then
  398.         wait(1/30)
  399.     end
  400.     for i = 1, 4, 1 do
  401.         if i == 1 then --Right
  402.             local NewX = X+1
  403.             if Grid[NewX] and Grid[NewX][Y] and NotInStack(NewX, Y) then
  404.                 Nearby[#Nearby+1] = {NewX, Y, i+1}
  405.             end
  406.         elseif i == 2 then --Left
  407.             local NewX = X-1
  408.             if Grid[NewX] and Grid[NewX][Y] and NotInStack(NewX, Y) then
  409.                 Nearby[#Nearby+1] = {NewX, Y, i+1}
  410.             end
  411.         elseif i == 3 then --Up
  412.             local NewY = Y-1
  413.             if Grid[X][NewY] and NotInStack(X, NewY) then
  414.                 Nearby[#Nearby+1] = {X, NewY, i+1}
  415.             end
  416.         else --Down
  417.             local NewY = Y+1
  418.             if Grid[X][NewY] and NotInStack(X, NewY) then
  419.                 Nearby[#Nearby+1] = {X, NewY, i+1}
  420.             end
  421.         end
  422.     end
  423.     return Nearby
  424. end
  425.  
  426. local function KnockDown(X, Y, DirNum)
  427.     local PartTab = Grid[X][Y][DirNum]
  428.     local Part = PartTab[1]
  429.  
  430.     HasChanged[Part] = "parent"
  431.     Part:Destroy()
  432.  
  433.     WaitNum = WaitNum + 1
  434. end
  435.  
  436. local function ChooseNext()
  437.     local NumCell = #CellStack
  438.     if NumCell > 0 then
  439.         local CurrentCell = CellStack[NumCell]
  440.         local CurrentX = CurrentCell[1]
  441.         local CurrentY = CurrentCell[2]
  442.         local Nearby = GetNear(CurrentX, CurrentY)
  443.         if #Nearby > 0 then
  444.             local NextCell = Nearby[Rand(1, #Nearby)]
  445.             CellStack[NumCell+1] = {NextCell[1], NextCell[2]}
  446.             KnockDown(CurrentX, CurrentY, NextCell[3])
  447.         else
  448.             Discared[CurrentX][CurrentY] = true
  449.             CellStack[NumCell] = nil
  450.         end
  451.         --wait(0.3)
  452.         ChooseNext()
  453.     end
  454. end
  455.  
  456. CreateGrid()
  457.  
  458. CreateWalls()
  459.  
  460. ChooseStart()
  461.  
  462. math.randomseed(tick())
  463.  
  464. ChooseNext()
  465.  
  466. HasChanged[Grid[StartCellNumX-1][FullY][5][1]] = "parent"
  467. HasChanged[Grid[StartCellNumX][FullY][5][1]] = "parent"
  468. HasChanged[Grid[StartCellNumX+1][FullY][5][1]] = "parent"
  469.  
  470. Grid[StartCellNumX-1][FullY][5][1]:Destroy()
  471. Grid[StartCellNumX][FullY][5][1]:Destroy()
  472. Grid[StartCellNumX+1][FullY][5][1]:Destroy()
  473.  
  474. HasChanged[Grid[StartCellNumX-1][1][4][1]] = "parent"
  475. HasChanged[Grid[StartCellNumX][1][4][1]] = "parent"
  476. HasChanged[Grid[StartCellNumX+1][1][4][1]] = "parent"
  477.  
  478. Grid[StartCellNumX-1][1][4][1]:Destroy()
  479. Grid[StartCellNumX][1][4][1]:Destroy()
  480. Grid[StartCellNumX+1][1][4][1]:Destroy()
  481.  
  482. --[[Grid[1][StartCellNumY-1][5][1]:Destroy()
  483. Grid[1][StartCellNumY][5][1]:Destroy()
  484. Grid[1][StartCellNumY+1][5][1]:Destroy()
  485.  
  486. Grid[FullX][StartCellNumY-1][5][1]:Destroy()
  487. Grid[FullX][StartCellNumY][5][1]:Destroy()
  488. Grid[FullX][StartCellNumY+1][5][1]:Destroy()]]
  489.  
  490. HasChanged[EndPart] = "transparency"
  491. EndPart.Transparency = 0
  492.  
  493. print("COMPLETED")
  494.  
  495. local ChatCon = nil
  496.  
  497. local PriorityQueue = {}
  498.  
  499. function GetNearSolve(X, Y)
  500.     local Nearby = {}
  501.     for i = 1, 4, 1 do
  502.         if i == 1 then --Right
  503.             local NewX = X+1
  504.             if Grid[NewX] and Grid[NewX][Y] and Grid[X][Y][2][1].Parent == nil then
  505.                 Nearby[#Nearby+1] = {NewX, Y, 2}
  506.             end
  507.         elseif i == 2 then --Left
  508.             local NewX = X-1
  509.             if Grid[NewX] and Grid[NewX][Y] and Grid[X][Y][3][1].Parent == nil then
  510.                 Nearby[#Nearby+1] = {NewX, Y, 3}
  511.             end
  512.         elseif i == 3 then --Up
  513.             local NewY = Y-1
  514.             if Grid[X][NewY] and Grid[X][Y][4][1].Parent == nil then
  515.                 Nearby[#Nearby+1] = {X, NewY, 4}
  516.             end
  517.         else --Down
  518.             local NewY = Y+1
  519.             if Grid[X][NewY] and Grid[X][Y][5][1].Parent == nil then
  520.                 Nearby[#Nearby+1] = {X, NewY, 5}
  521.             end
  522.         end
  523.     end
  524.     return Nearby
  525. end
  526.  
  527. function GetPri()
  528.     local ReturnTab = PriorityQueue[1][2]
  529.     table.remove(PriorityQueue, 1)
  530.     return ReturnTab
  531. end
  532.  
  533. function SetPri(Node, Priority)
  534.     local AddTab = {Priority, Node}
  535.     for i = 1, #PriorityQueue do
  536.         if Priority < PriorityQueue[i][1] then
  537.             table.insert(PriorityQueue, i, AddTab)
  538.             return
  539.         end
  540.     end
  541.     table.insert(PriorityQueue, AddTab)
  542. end
  543.  
  544. function ValInCosts(Node, Costs)
  545.     for OldNode,OldCosts in pairs(Costs) do
  546.         if OldNode[1] == Node[1] and OldNode[2] == Node[2] then
  547.             return OldCosts
  548.         end
  549.     end
  550.     return false
  551. end
  552.  
  553. function TraceBack(LastNode, CameFrom)
  554.     while true do
  555.         local BackTrackedNode = CameFrom[LastNode]
  556.         if #BackTrackedNode == 0 then
  557.             print("COMPLETED SOLVE")
  558.             break
  559.         end
  560.  
  561.         LastNode = BackTrackedNode
  562.  
  563.         WaitNum = WaitNum + 1
  564.  
  565.         if WaitNum % 10 == 0 then
  566.             wait(1/30)
  567.         end
  568.  
  569.         local Part = CellPart:Clone()
  570.         Part.CanCollide = false
  571.         Part.Transparency = 0
  572.         Part.Anchored = true
  573.         Part.Material = "Wood"
  574.         Part.BrickColor = BrickColor.new("Bright bluish green")
  575.         Part.Size = Vector3.new(NodeWidth, EndHeight, NodeWidth)
  576.         Part.CFrame = StartCF * CFrame.new(LastNode[1]*CellWidth, EndCFY, LastNode[2]*CellWidth)
  577.         Part.Parent = workspace
  578.  
  579.         ProtObj(Part)
  580.     end
  581. end
  582.  
  583. function Heuristic(Goal, Current)
  584.     return abs(Goal[1] - Current[1]) + abs(Goal[2] - Current[2])
  585. end
  586.  
  587. local Ended = false
  588. ChatCon = LP.Chatted:connect(function(Msg)
  589.     if Msg:lower() == "generate" or Msg:lower() == "/e generate" then
  590.         ChatCon:disconnect()
  591.         HitCon:disconnect()
  592.         if #StepCons > 0 then
  593.             for i = 1, #StepCons do
  594.                 StepCons[i]:disconnect()
  595.             end
  596.         end
  597.         Ended = true
  598.         print("Ended last maze")
  599.     elseif Msg:lower() == "solve" or Msg:lower() == "/e solve" then
  600.         local Start = {StartCellNumX, FullY}
  601.  
  602.         SetPri(Start, 0)
  603.  
  604.         local CameFrom = {}
  605.         local TotalCost = {}
  606.  
  607.         CameFrom[Start] = {} --Maybe won't work as might record same coords on multiple searches and overwrite
  608.         TotalCost[Start] = 0
  609.  
  610.         local GoalTab = {RandX, RandY}
  611.        
  612.         while #PriorityQueue > 0 do
  613.  
  614.             if Ended == true then
  615.                 print("Stopped search")
  616.                 break
  617.             end
  618.  
  619.             local CurrentNode = GetPri()
  620.             local NodeX = CurrentNode[1]
  621.             local NodeY = CurrentNode[2]
  622.  
  623.             if NodeX == RandX and NodeY == RandY then
  624.                 print("FOUND")
  625.                 TraceBack(CurrentNode, CameFrom)
  626.                 break
  627.             end
  628.  
  629.             local Nearby = GetNearSolve(NodeX, NodeY)
  630.  
  631.             for i = 1, #Nearby do
  632.                 local NextNode = Nearby[i]
  633.                 local NewCost = TotalCost[CurrentNode] + 1
  634.                 local ValueInCosts = ValInCosts(NextNode, TotalCost)
  635.                 if ValueInCosts == false or NewCost < ValueInCosts then
  636.                     TotalCost[NextNode] = NewCost
  637.                     local Priority = NewCost + Heuristic(GoalTab, NextNode)
  638.                     SetPri(NextNode, Priority)
  639.                     CameFrom[NextNode] = CurrentNode
  640.                 end
  641.             end
  642.         end
  643.     end
  644. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement