Advertisement
SuperRavenSn1per

turtleTemp

Feb 12th, 2023 (edited)
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. --Open Rednet
  2. if not rednet.isOpen() then
  3.     rednet.open("right")
  4. end
  5. --Get current turtle coordinates
  6. local function getCoord(coord)
  7.     local f = fs.open("coords/"..coord..".loc", "r")
  8.     local c = f.readAll()
  9.     f.close()
  10.     return tonumber(c)
  11. end
  12. --Update the coordinates of the turtle
  13. local function updateCoord(coord, change)
  14.     local f = fs.open("coords/"..coord..".loc", "w")
  15.     f.write(change)
  16.     f.close()
  17. end
  18. --Move the turtle one block forward
  19. local function walk()
  20.     if turtle.getFuelLevel() > 0 then
  21.         local x,z,r = getCoord("x"),getCoord("z"),getCoord("r")
  22.         turtle.forward()
  23.         if r == 1 then
  24.             updateCoord("x", x + 1)
  25.         elseif r == 2 then
  26.             updateCoord("z", z + 1)
  27.         elseif r == 3 then
  28.             updateCoord("x", x - 1)
  29.         elseif r == 4 then
  30.             updateCoord("z", z - 1)
  31.         end
  32.     else
  33.         print("Insufficient fuel!")
  34.     end
  35. end
  36. --Rotate the turtle once
  37. local function rotate()
  38.     local r = getCoord("r")
  39.     turtle.turnRight()
  40.     if r < 4 then
  41.         updateCoord("r", r + 1)
  42.     else
  43.         updateCoord("r", 1)
  44.     end
  45. end
  46. --Mine 3 blocks
  47. local function path()
  48.     turtle.dig()
  49.     turtle.digUp()
  50.     turtle.digDown()
  51. end
  52. --Goes to selected coordinates, X first then Z
  53. local function goTo(dX, dZ)
  54.     if turtle.getFuelLevel() < (dX + dZ) then
  55.         term.setTextColor(colors.red)
  56.         write("[X] ")
  57.         term.setTextColor(colors.white)
  58.         write("Turtle ID#"..os.getComputerID().." does not have enough fuel to go to specified location.")
  59.         return
  60.     elseif turtle.getFuelLevel() < (dX + dZ) * 2 then
  61.         term.setTextColor(colors.orange)
  62.         write("[!] ")
  63.         term.setTextColor(colors.white)
  64.         write("Turtle ID#"..os.getComputerID().." does not have enough fuel to travel and return. Continue (Y/n)?")
  65.         print("")
  66.         while true do
  67.             local input = read()
  68.             if string.lower(input) == "n" then
  69.                 print("Cancelled!")
  70.                 return
  71.             elseif string.lower(input) == "y" then
  72.                 break
  73.             else
  74.                 print("Not understood. Please try again.")
  75.             end
  76.         end
  77.     end
  78.     local x, z, r = getCoord("x"), getCoord("z"), getCoord("r")
  79.     if x ~= dX then
  80.         if x < dX then
  81.             if r ~= 1 then
  82.                 repeat rotate() r = getCoord("r") until r == 1
  83.             end
  84.         elseif x > dX then
  85.             if r ~= 3 then
  86.                 repeat rotate() r = getCoord("r") until r == 3
  87.             end
  88.         end
  89.         repeat walk() xC = getCoord("x") until xC == dX
  90.     end
  91.     if z ~= dZ then
  92.         if x < dZ then
  93.             if r ~= 2 then
  94.                 repeat rotate() r = getCoord("r") until r == 2
  95.             end
  96.         elseif z > dZ then
  97.             if r ~= 4 then
  98.                 repeat rotate() r = getCoord("r") until r == 4
  99.             end
  100.         end
  101.         repeat walk() zC = getCoord("z") until zC == dZ
  102.     end
  103. end
  104. local function mineLine(length)
  105.     for i = 1,length - 1 do
  106.         path()
  107.         walk()
  108.     end
  109.     local r = getCoord("r")
  110.     if r == 1 then
  111.         rotate()
  112.     else
  113.         rotate()
  114.         rotate()
  115.         rotate()
  116.     end
  117.     path()
  118.     walk()
  119.     if r ==1 then
  120.         rotate()
  121.     else
  122.         rotate()
  123.         rotate()
  124.         rotate()
  125.     end
  126. end
  127. local function mineBox(bW, bH)
  128.     for i = 1,bW do
  129.         mineLine(bH)
  130.     end
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement