DeepFriedBabeez

General Turtle Movement

Aug 28th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.16 KB | None | 0 0
  1. --[[
  2.         X+(0)
  3.           ^
  4.     Z-(3) < Turtle > Z+(1)
  5.           v
  6.         X-(2)
  7.  
  8. ]]--
  9.  
  10. x = 0
  11. y = 0
  12. z = 0
  13. dir = 0
  14. autoFuel = false --refuels using picked up fuel items (eg. coal + wood)
  15. allowDrain = false --allow turtle to take directions even if there is not enough fuel
  16. hardFuelLimit = false --dont allow turtle to refuel if the next fuel item exceeds limit (will stop using items after cap is reached regardless of this option, allow autofuel + allowDrain for trips exceeding fuel limit)
  17.  
  18. --[[ END OF VARIABLES ]]--
  19.  
  20. fuel = turtle.getFuelLevel()
  21. needsFuel = true
  22. if (fuel == "unlimited") then
  23.     needsFuel = false
  24.     autoFuel = false
  25.     allowDrain = true
  26. else
  27.     fuelLim = turtle.getFuelLimit()
  28. end
  29.  
  30. function align(toward)
  31.     for i=1, math.abs(toward - dir) do
  32.         if (toward < dir) then
  33.             turtle.turnLeft()
  34.             dir = (dir + 3) % 4
  35.         else
  36.             turtle.turnRight()
  37.             dir = (dir + 1) % 4
  38.         end
  39.     end
  40. end
  41.  
  42. function distTo(destX, destY, destZ)
  43.     return math.abs(destX - x) + math.abs(destY - y) + math.abs(destZ - z)
  44. end
  45.  
  46. function refuel(amount)
  47.     fuel = turtle.getFuelLevel()
  48.     total = fuel + amount
  49.     for i=1, 16 do
  50.         turtle.select(i)
  51.         turtle.refuel(1)
  52.         fuelVal = turtle.getFuelLevel() - fuel
  53.         if (fuelVal > 0) then
  54.             turtle.refuel(math.min(64,math.ceil(math.min(fuelLim, (amount - fuelVal)) / fuelVal)))
  55.             fuel = turtle.getFuelLevel()
  56.             amount = total - fuel
  57.             if (amount <= 0) then
  58.                 return true
  59.             end
  60.         end
  61.     end
  62.     return false
  63. end
  64.  
  65. function dig(toward, spaces)
  66.     spaces = spaces or 1
  67.     align(toward)
  68.  
  69.     for i=1, spaces do
  70.         while not (turtle.forward()) do
  71.             turtle.dig()
  72.             turtle.attack()
  73.         end
  74.  
  75.         if (dir == 0) then
  76.             x = x + 1
  77.         elseif (dir == 1) then
  78.             z = z + 1
  79.         elseif (dir == 2) then
  80.             x = x - 1
  81.         elseif (dir == 3) then
  82.             z = z - 1
  83.         end
  84.     end
  85. end
  86.  
  87. function digTo(newX, newY, newZ)
  88.     xDist = newX - x
  89.     yDist = newY - y
  90.     zDist = newZ - z
  91.  
  92.     if (xDist > 0) then
  93.         align(0)
  94.     elseif (xDist < 0) then
  95.         align(2)
  96.     end
  97.     dig(dir, math.abs(xDist))
  98.  
  99.     if (zDist > 0) then
  100.         align(1)
  101.     elseif (zDist < 0 ) then
  102.         align(3)
  103.     end
  104.     dig(dir, math.abs(zDist))
  105.  
  106.     for i=1, math.abs(yDist) do
  107.         if (yDist > 0) then
  108.             while not (turtle.up()) do
  109.                 turtle.digUp()
  110.                 turtle.attackUp()
  111.             end
  112.             y = y + 1
  113.         elseif (yDist < 0) then
  114.             while not (turtle.down()) do
  115.                 turtle.digDown()
  116.                 turtle.attackDown()
  117.             end
  118.             y = y - 1
  119.         end
  120.     end
  121.     return true
  122. end
  123.  
  124. function digSquare(corner, width, length)
  125.     --[[ corner values, width is always x, length is always z
  126.         3----0
  127.         |    |
  128.         2----1  
  129.     ]]--
  130.    
  131.    
  132.     if (corner == 0 or corner == 1) then
  133.         align(3)
  134.     elseif (corner == 2 or corner == 3) then
  135.         align(1)
  136.     else
  137.         return 0
  138.     end
  139.    
  140.     for i=1, length do
  141.         if (i ~= 1) then
  142.             if (i % 2 == corner % 2) then
  143.                 align(dir - 1)
  144.                 dig(dir)
  145.                 align(dir - 1)
  146.             else
  147.                 align(dir + 1)
  148.                 dig(dir)
  149.                 align(dir + 1)
  150.             end
  151.         end
  152.         dig(dir, width - 1)
  153.     end
  154. end
  155.  
  156. function digCube(corner, width, length, depth)
  157.     for i=1, depth do
  158.         digSquare(corner, width, length)
  159.         digTo(x, y-1, z)
  160.         align(0)
  161.         if (length % 2 == 1) then
  162.             corner = (corner + 2) % 4
  163.         else
  164.             corner = (corner + 1) - 2 * (corner % 2)
  165.         end
  166.     end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment