morganamilo

Turtle API for digging programs v1.0

May 12th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.51 KB | None | 0 0
  1. --by morganamilo
  2.  
  3. local x = 0 -- increases as turtle goes forward relative to direction it faced when first placed
  4. local y = 0 -- increases as turtle goes left relative to direction it faced when first placed, decreases when turtle goes right
  5. local z = 0 -- increases as turtle goes up, decreases as turtle does down
  6.  
  7. local xDiff = {1, 0, -1, 0} -- what to add to 'x' depending on the orientation
  8. local yDiff = {0, 1, 0, -1}
  9.  
  10. local orient = 1
  11.  
  12. function get(var) -- returns respective varible
  13.   if     var == 'x' then return x
  14.   elseif var == 'y' then return y
  15.   elseif var == 'z' then return z
  16.   elseif var == 'orient' then return orient end
  17. end
  18.  
  19. function refuelIfEmpty() -- refuels if fuel level is 0
  20.   if turtle.getFuelLevel() == 0 then
  21.     print('Out of fuel\nWaiting for fuel in slot ' .. turtle.getSelectedSlot() .. '...')
  22.     while not turtle.refuel do os.pullEvent('turtle_inventory') end
  23.     print('Fuel level is: ' .. turtle.getFuelLevel())
  24.   end
  25. end
  26.  
  27. function refuelAtIfNeeded(fuelX, fuelY, fuelZ) -- refuels using chest at specified location
  28.   local currentX, currentY, currentZ, currentO = x, y, z, orient  
  29.  
  30.   if turtle.getFuelLevel() == getDistanceFrom(fuelX, fuelY, fuelZ) then
  31.     print('looking for fuel in current slot...')
  32.     if turtle.refuel() then
  33.       print('fuel found, continuing')
  34.     else
  35.       print('heading back to refuel...')
  36.       turtle.drop()
  37.  
  38.       if     x < fuelX then goTo(fuelX + 1, fuelY, fuelZ, 'z' 'y') end
  39.       elseif x > fuelX then goTo(fuelX - 1, fuelY, fuelZ, 'z' 'y') end
  40.  
  41.       turtle.suck()
  42.       if not turtle.refuel() then
  43.         print('fuel not found, waiting for fuel in current slot...')
  44.         while not turtle.refuel() do os.pullEvent('turtle_inventory') end
  45.       end
  46.  
  47.       print('fuel found, continuing')
  48.     end
  49.     goTo(currentX, currentY, currentZ, 'z' 'y')
  50.     faceDir(currentO)
  51.   end
  52. end
  53.  
  54. function forward(limit) -- moves forward, digs up/down if respective blocks need to be dug
  55.   x = x + xDiff[orient]
  56.   y = y + yDiff[orient]
  57.    
  58.   while not turtle.forward() do
  59.     refuelIfEmpty()
  60.     if turtle.detect() then turtle.dig() end
  61.   end
  62.  
  63.   if z > -limit + 1 and turtle.detectDown() then turtle.digDown() end
  64.   if turtle.detectUp() and z < limit -1  then turtle.digUp() end
  65. end
  66.  
  67. function left() -- turns left, takes 1 from orient
  68.   orient = (orient + 2) % 4 + 1
  69.   turtle.turnLeft()    
  70. end
  71.  
  72. function right() -- turns right, adds 1 to orient
  73.   orient = orient % 4 + 1
  74.   turtle.turnRight()
  75. end
  76.  
  77. function faceDir(direction) -- turns to face specified direction, using any number other than '1, 2, 3, 4' will cause a u-turn
  78.   if direction ~= orient then
  79.     if     direction == (orient + 2) % 4 + 1 then left()
  80.     elseif direction == orient % 4 + 1 then right()
  81.     else left() left() end
  82.   end
  83. end
  84.  
  85. function up(limit) -- moves up, adds 1 to z
  86.   repeat
  87.     if turtle.detectUp() then turtle.digUp() end
  88.   until turtle.up()
  89.  
  90.   z = z + 1
  91.   if turtle.detectUp() and z < limit - 1 then turtle.digUp() end
  92. end
  93.  
  94. function down(limit) -- moves down, takes 1 from z
  95.   repeat
  96.     if turtle.detectDown() then turtle.digDown() end
  97.   until turtle.down()
  98.  
  99.   z = z - 1
  100.   if turtle.detectDown() and z > -limit + 1 then turtle.digDown() end
  101. end
  102.  
  103. function goTo(newX, newY, newZ, prior1, prior2) -- goes to given coordinate
  104.   local functs = {x = {goToX, newX}, y = {goToY, newY}, z = {goToZ, newZ}}
  105.   local prior3
  106.    
  107.   for k, v in pairs({'x', 'y', 'z'}) do
  108.     if not (prior1 == v or prior2 == v) then prior3 = v end
  109.   end
  110.  
  111.   for k, v in pairs({prior1, prior2, prior3}) do
  112.     functs[v][1](functs[v][2])
  113.   end
  114. end
  115.  
  116. function goToX(newX)
  117.   if newX > x then faceDir(1) end
  118.   if newX < x then faceDir(3) end
  119.   while newX ~= x do forward(1) end
  120. end
  121.  
  122. function goToY(newY)
  123.   if newY > y then faceDir(2) end
  124.   if newY < y then faceDir(4) end
  125.   while newY ~= y do forward(1) end
  126. end
  127.  
  128. function goToZ(newZ)
  129.   while newZ > z do up(1) end
  130.   while newZ < z do down(1) end
  131. end
  132.  
  133. function round(n) -- rounds numbers
  134.   return math.floor(n + 0.5)
  135. end
  136.  
  137. function inRad(x, y, rad) -- checks if given x and y are in radius
  138.   return round(math.sqrt(x*x+y*y)) < rad
  139. end
  140.  
  141. function nextInRad(rad)
  142.   nextX = x + xDiff[orient]
  143.   nextY = y + yDiff[orient]
  144.   return inRad(nextX, nextY, rad)
  145. end
  146.  
  147. function isEven(n) -- true if n is even
  148.   if n % 2 == 0 then return true
  149.   else return false end
  150. end
  151.  
  152. function turn(n) -- turns left if n is even otherwise right, used for alternating turning in for loops
  153.   if isEven(n) then left()
  154.   else right() end
  155. end
Advertisement
Add Comment
Please, Sign In to add comment