Advertisement
BILLPC2684

pathfind.lua

Dec 1st, 2020
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 KB | None | 0 0
  1. --simulated program for OpenComputers Robots
  2. -- OC:robot = require("robot")
  3. cords = {112, 21,398, 0}
  4. dest  = {103, 64, 85, 2}
  5. steps = 0
  6.  
  7. N=0 S=2 -- +z -z
  8. E=1 W=3 -- +x -x
  9.  
  10. function ChangeDirection(dir)
  11.  while cords[4] ~= dir do
  12.   robot.turnRight()
  13.   cords[4] = (cords[4]+1)%4
  14.  end
  15. end
  16.  
  17. function run() -- OC:*remove this line*
  18.  while not(cords[1]==dest[1] and cords[2]==dest[2] and cords[3]==dest[3]) do
  19.   print("\\My Cords are: ["..tostring(cords[1])..", "..tostring(cords[2])..", "..tostring(cords[3]).."] |  Dir:"..tostring(cords[4]))
  20.   print(" \\My current distance is: "..tostring(math.sqrt(math.pow((dest[1]-cords[1]),2)+math.pow((dest[2]-cords[2]),2)+math.pow((cords[3]-dest[3]),2))))
  21.   distsum = {
  22.    {math.sqrt(math.pow((dest[1]-(cords[1]+1)),2)+math.pow((dest[2]- cords[2]   ),2)+math.pow((dest[3]- cords[3]   ),2)),"+x"},
  23.    {math.sqrt(math.pow((dest[1]-(cords[1]-1)),2)+math.pow((dest[2]- cords[2]   ),2)+math.pow((dest[3]- cords[3]   ),2)),"-x"},
  24.    {math.sqrt(math.pow((dest[1]- cords[1]   ),2)+math.pow((dest[2]-(cords[2]+1)),2)+math.pow((dest[3]- cords[3]   ),2)),"+y"},
  25.    {math.sqrt(math.pow((dest[1]- cords[1]   ),2)+math.pow((dest[2]-(cords[2]-1)),2)+math.pow((dest[3]- cords[3]   ),2)),"-y"},
  26.    {math.sqrt(math.pow((dest[1]- cords[1]   ),2)+math.pow((dest[2]- cords[2]   ),2)+math.pow((dest[3]-(cords[3]+1)),2)),"+z"},
  27.    {math.sqrt(math.pow((dest[1]- cords[1]   ),2)+math.pow((dest[2]- cords[2]   ),2)+math.pow((dest[3]-(cords[3]-1)),2)),"-z"},
  28.   } table.sort(distsum, function(a,b) return a[1]<b[1] end)
  29.   print("  \\My best directions is on the "..tostring(distsum[1][2]).." axis distance: "..tostring(distsum[1][1]))
  30.   i=1
  31.   while true do
  32.    if distsum[i][2] == "+x" then ChangeDirection(E); if robot.forward() == true then cords[1]=cords[1]+1; break end end
  33.    if distsum[i][2] == "-x" then ChangeDirection(S); if robot.forward() == true then cords[1]=cords[1]-1; break end end
  34.    if distsum[i][2] == "+y" then                     if robot.up()      == true then cords[2]=cords[2]+1; break end end
  35.    if distsum[i][2] == "-y" then                     if robot.down()    == true then cords[2]=cords[2]-1; break end end
  36.    if distsum[i][2] == "+z" then ChangeDirection(N); if robot.forward() == true then cords[3]=cords[3]+1; break end end
  37.    if distsum[i][2] == "-z" then ChangeDirection(W); if robot.forward() == true then cords[3]=cords[3]-1; break end end
  38.    print("  /My Cords are: ["..tostring(cords[1])..", "..tostring(cords[2])..", "..tostring(cords[3]).."] |  Dir:"..tostring(cords[4]))
  39.    print(" /My current distance is: "..tostring(math.sqrt(math.pow((cords[1]-dest[1]),2)+math.pow((cords[2]-dest[2]),2)+math.pow((cords[3]-dest[3]),2))))
  40.    i=i+1; print("/ Redirecting...")
  41.   end steps=steps+1
  42.  end ChangeDirection(dest[4])
  43.  print("\\My Cords are: ["..tostring(cords[1])..", "..tostring(cords[2])..", "..tostring(cords[3]).."] | Dir:"..tostring(cords[4]))
  44.  print(" \\Number of Steps: "..tostring(steps))
  45. end -- OC:*from here and below remove*
  46.  
  47. --simulating blocks
  48. blocks = {
  49.  ["111,22,390"] = 1, ["112,22,390"] = 1, ["112,22,389"] = 1,
  50.  ["111,21,390"] = 1, ["112,21,390"] = 1, ["112,21,389"] = 1,
  51.  ["111,20,390"] = 1, ["112,20,390"] = 1, ["112,20,389"] = 1,
  52. }
  53. --simulating robot
  54. robot = {
  55.  ["facing"] = 0,
  56.  ["turnLeft"] =  function() robot.facing=(robot.facing-1)%4 end,
  57.  ["turnRight"] = function() robot.facing=(robot.facing+1)%4 end,
  58.  ["forward"] = function()
  59.   for i,k in pairs(blocks) do
  60.    if     robot.facing == E then
  61.     if tostring(cords[1]+1)..","..tostring(cords[2])..","..tostring(cords[3]  ) == i then cords[1]=cords[1]+1; return false end
  62.    elseif robot.facing == W then
  63.     if tostring(cords[1]-1)..","..tostring(cords[2])..","..tostring(cords[3]  ) == i then cords[1]=cords[1]-1; return false end
  64.    elseif robot.facing == N then
  65.     if tostring(cords[1]  )..","..tostring(cords[2])..","..tostring(cords[3]+1) == i then cords[3]=cords[3]+1; return false end
  66.    elseif robot.facing == S then
  67.     if tostring(cords[1]  )..","..tostring(cords[2])..","..tostring(cords[3]-1) == i then cords[3]=cords[3]-1; return false end
  68.    end
  69.   end return true
  70.  end,
  71.  ["back"] = function()
  72.   for i,k in pairs(blocks) do
  73.    if     robot.facing == E then
  74.     if tostring(cords[1]+1)..","..tostring(cords[2])..","..tostring(cords[3]  ) == i then cords[1]=cords[1]-1; return false end
  75.    elseif robot.facing == W then
  76.     if tostring(cords[1]-1)..","..tostring(cords[2])..","..tostring(cords[3]  ) == i then cords[1]=cords[1]+1; return false end
  77.    elseif robot.facing == N then
  78.     if tostring(cords[1]  )..","..tostring(cords[2])..","..tostring(cords[3]+1) == i then cords[3]=cords[3]-1; return false end
  79.    elseif robot.facing == S then
  80.     if tostring(cords[1]  )..","..tostring(cords[2])..","..tostring(cords[3]-1) == i then cords[3]=cords[3]+1; return false end
  81.    end
  82.   end return true
  83.  end,
  84.  ["up"] = function()
  85.   for i,k in pairs(blocks) do
  86.    if tostring(cords[1])..","..tostring(cords[2]+1)..","..tostring(cords[3]) == i then
  87.     return false
  88.    end
  89.   end return true
  90.  end,
  91.  ["down"] = function()
  92.   for i,k in pairs(blocks) do
  93.    if tostring(cords[1])..","..tostring(cords[2]-1)..","..tostring(cords[3]) == i then
  94.     return false
  95.    end
  96.   end return true
  97.  end,
  98. }
  99. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement