HappySunChild

MoveTo

Sep 15th, 2021 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.35 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local targetX = tonumber(tArgs[1])
  4. local targetZ = tonumber(tArgs[2])
  5.  
  6. if not targetX and not targetZ then
  7.     term.write("No X and Z given.")
  8.     return
  9. end
  10.  
  11. function moveForward(times)
  12.     for i = 1,times do
  13.         while turtle.forward() == false do
  14.             turtle.dig()
  15.         end
  16.     end
  17. end
  18.  
  19. function moveUp(times)
  20.     for i = 1,times do
  21.         while turtle.up() == false do
  22.             turtle.digUp()
  23.         end
  24.     end
  25. end
  26.  
  27. function moveDown(times)
  28.     for i = 1,times do
  29.         while turtle.down() == false do
  30.             turtle.digDown()
  31.         end
  32.     end
  33. end
  34.  
  35. function CheckRail(location,startingPosition)
  36.     local requiredRails = (math.abs(location.x - startingPosition.x) + math.abs(location.z-startingPosition.z))
  37.     return requiredRails
  38. end
  39.  
  40. function FindRails()
  41.     for i = 1,16 do
  42.         local a = turtle.getItemDetail(i)
  43.         if a then
  44.             if a.name == "minecraft:rail" or a.name == "minecraft:powered_rail" then
  45.                 return i
  46.             end
  47.         end
  48.     end
  49.  
  50.     return false
  51. end
  52.  
  53. function neededFuel(location,startingPosition)
  54.     local currentFuel = turtle.getFuelLevel()
  55.     local requiredFuel = (math.abs(location.x - startingPosition.x) + math.abs(location.z-startingPosition.z))* 2
  56.     return requiredFuel
  57. end
  58.  
  59. function MoveToLocation(location)
  60.     local startingPosition = 0
  61.     local currentPosition = 0
  62.  
  63.     if gps.locate(2, false) then
  64.         startingPosition = vector.new(gps.locate(2,false))
  65.         currentPosition = startingPosition
  66.  
  67.         moveForward(1)
  68.         currentPosition = vector.new(gps.locate(2,false))
  69.  
  70.         local difference = startingPosition - currentPosition
  71.  
  72.         local moving = ""
  73.  
  74.         if difference.x == -1 then
  75.             moving = 3 -- east
  76.         elseif difference.x == 1 then
  77.             moving = 1 -- west
  78.         end
  79.  
  80.         if difference.z == -1 then
  81.             moving = 0 -- south
  82.         elseif difference.z == 1 then
  83.             moving = 2 -- north
  84.         end
  85.  
  86.         if currentPosition.x > location.x then
  87.             if moving == 0 then
  88.                 turtle.turnRight()
  89.             elseif moving == 2 then
  90.                 turtle.turnLeft()
  91.             elseif moving == 3 then
  92.                 turtle.turnRight()
  93.                 turtle.turnRight()
  94.             end
  95.            
  96.             moving = 1
  97.  
  98.             while currentPosition.x ~= location.x do
  99.                 moveForward(1)
  100.                 turtle.digDown()
  101.                 if FindRails() then
  102.                     turtle.select(FindRails())
  103.                     turtle.placeDown()
  104.                 end
  105.                 currentPosition = vector.new(gps.locate(2, false))
  106.             end
  107.         else
  108.             if moving == 0 then
  109.                 turtle.turnLeft()
  110.             elseif moving == 2 then
  111.                 turtle.turnRight()
  112.             elseif moving == 1 then
  113.                 turtle.turnRight()
  114.                 turtle.turnRight()
  115.             end
  116.  
  117.             moving = 3
  118.  
  119.             while currentPosition.x ~= location.x do
  120.                 moveForward(1)
  121.                 turtle.digDown()
  122.                 if FindRails() then
  123.                     turtle.select(FindRails())
  124.                     turtle.placeDown()
  125.                 end
  126.                 currentPosition = vector.new(gps.locate(2, false))
  127.             end
  128.         end
  129.  
  130.         if currentPosition.z > location.z then -- needs to go north
  131.             if moving == 0 then
  132.                 turtle.turnRight()
  133.                 turtle.turnRight()
  134.             elseif moving == 1 then
  135.                 turtle.turnRight()
  136.             elseif moving == 3 then
  137.                 turtle.turnLeft()
  138.             end
  139.  
  140.             moving = 2
  141.  
  142.             while currentPosition.z ~= location.z do
  143.                 moveForward(1)
  144.                 turtle.digDown()
  145.                 if FindRails() then
  146.                     turtle.select(FindRails())
  147.                     turtle.placeDown()
  148.                 end
  149.                 currentPosition = vector.new(gps.locate(2, false))
  150.             end
  151.         else -- needs to go south
  152.             if moving == 2 then
  153.                 turtle.turnRight()
  154.                 turtle.turnRight()
  155.             elseif moving == 1 then
  156.                 turtle.turnLeft()
  157.             elseif moving == 3 then
  158.                 turtle.turnRight()
  159.             end
  160.  
  161.             moving = 0
  162.  
  163.             while currentPosition.z ~= location.z do
  164.                 moveForward(1)
  165.                 turtle.digDown()
  166.                 if FindRails() then
  167.                     turtle.select(FindRails())
  168.                     turtle.placeDown()
  169.                 end
  170.                 currentPosition = vector.new(gps.locate(2, false))
  171.             end
  172.         end
  173.     end
  174. end
  175.  
  176. if targetX and targetZ then
  177.     local location = vector.new(targetX,0,targetZ)
  178.     local currentPosition = vector.new(gps.locate(2, false))
  179.  
  180.     print("No. Rails: " .. CheckRail(location,currentPosition))
  181.  
  182.     if neededFuel(location,currentPosition) > turtle.getFuelLevel() then
  183.         print("Cur Fuel: " .. turtle.getFuelLevel())
  184.         print("Req Fuel: " .. neededFuel(location,currentPosition))
  185.         print("Needed Fuel: " .. neededFuel(location,currentPosition) - turtle.getFuelLevel())
  186.         return
  187.     end
  188.  
  189.     MoveToLocation(vector.new(targetX,0,targetZ))
  190. end
Add Comment
Please, Sign In to add comment