Octagonal_Square

returnToStart

Jan 30th, 2022 (edited)
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.50 KB | None | 0 0
  1. os.loadAPI("inv")
  2. os.loadAPI("t")
  3.  
  4. local COAL = "coal"
  5. local CHAR = "char"
  6.  
  7. local STARTX =
  8. local STARTY =
  9. local STARTZ =
  10.  
  11. function round(exact)
  12.     quantum = 1
  13.     local quant,frac = math.modf(exact/quantum)
  14.     return quantum * (quant + (frac > 0.5 and 1 or 0))
  15. end
  16.  
  17. function refuelIfNeeded()
  18.     local fuelLevel = turtle.getFuelLevel()
  19.     if fuelLevel < 70 then
  20.         print("Fuel level is " .. fuelLevel .. ", refueling...")
  21.         if inv.selectItem(COAL) then
  22.             turtle.refuel(1)
  23.             print("Refueled! new level is " .. turtle.getFuelLevel())
  24.             return true
  25.         end
  26.         printError("No fuel in inventory!")
  27.     end
  28.     return false
  29. end
  30.  
  31. turtle.turnLeft()
  32.  
  33. local x,y,z = gps.locate(15)
  34.  
  35. x = round(x)
  36. y = round(y)
  37. z = round(z)
  38.  
  39. print(x,y,z)
  40.  
  41. t.fw()
  42.  
  43. local newX, newY, newZ = gps.locate(15)
  44.  
  45. t.back()
  46.  
  47. newX = round(newX)
  48. newY = round(newY)
  49. newZ = round(newZ)
  50.  
  51. if x > newX then
  52.     turtle.turnLeft()
  53.     turtle.turnLeft()
  54. end
  55.  
  56. if z > newZ then
  57.     turtle.turnRight()
  58. elseif z < newZ then
  59.     turtle.turnLeft()
  60. end
  61.  
  62. local difx = math.abs(x - STARTX)
  63. local dify = math.abs(y - STARTY)
  64. local difz = math.abs(z - STARTZ)
  65.  
  66. print(difx,dify,difz)
  67.  
  68. if x > STARTX then
  69.     turtle.turnLeft()
  70.     turtle.turnLeft()
  71.     t.fw(difx)
  72.     turtle.turnLeft()
  73.     turtle.turnLeft()
  74. elseif x < STARTX then
  75.     t.fw(difx)
  76. end
  77.  
  78. if z > STARTZ then
  79.     turtle.turnLeft()
  80.     t.fw(difz)
  81.     turtle.turnRight()
  82. elseif z < STARTZ then
  83.     turtle.turnRight()
  84.     t.fw(difz)
  85.     turtle.turnLeft()
  86. end
  87.  
  88. turtle.turnLeft()
Add Comment
Please, Sign In to add comment