Advertisement
Guest User

mine

a guest
Nov 29th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. local modem = peripheral.wrap("left")
  2. local idealIron = 22
  3. local idealDiamond = 12
  4. local originX, originY, originZ = gps.locate()
  5. local currentX, currentY, currentZ = gps.locate()
  6. local counter = 0
  7. if not currentX then
  8.   log("Cannot determine my position! Exiting...")
  9.   error()
  10. end
  11. if not originX then
  12.   log("Cannot determine my origin! Exiting...")
  13.   error()
  14. end
  15.  
  16. function log(message)
  17.   modem.transmit(99,1,message)
  18. end
  19.  
  20. function move(direction)
  21.   if direction == "fwd" then
  22.     while not turtle.forward() do
  23.       turtle.forward()
  24.       log("Can't move forward")
  25.     end
  26.   elseif direction == "bwd" then
  27.     while not turtle.backward() do
  28.       turtle.back()
  29.       log("Can't move backward")
  30.     end
  31.   elseif direction == "left" then
  32.     turtle.turnLeft()
  33.     while not turtle.forward() do
  34.       turtle.forward()
  35.       log("Can't move to the left")
  36.     end
  37.     turtle.turnRight()
  38.   elseif direction == "right" then
  39.     turtle.turnRight()
  40.     while not turtle.forward() do
  41.       turtle.forward()
  42.       log("Can't move to the right")
  43.     end
  44.     turtle.turnLeft()
  45.   else
  46.     log("Invalid direction passed for move(Str)")
  47.     error()
  48.   end
  49.   updatePos()  
  50. end
  51.  
  52. function updatePos()
  53.   local newX, newY, newZ = gps.locate()
  54.   if not newX then
  55.     log("Cannot determine my current position! Exiting...")
  56.     error()
  57.   end
  58.   currentX = newX
  59.   currentY = newY
  60.   currentZ = newZ
  61. end
  62.  
  63. function dig(direction)
  64.   local oX, oY, oZ = gps.locate()
  65.   local distance = 0
  66.   if not oX then
  67.     log("Cannot determine position before digging")
  68.     error()
  69.   end
  70.   if direction == "left" then
  71.     turtle.turnLeft()
  72.   elseif direction == "right" then
  73.     turtle.turnRight()
  74.   else
  75.     log("Invalid direction for digging given!")
  76.   end
  77.   while distance < 32 do
  78.     os.sleep(0.5)
  79.     turtle.dig()
  80.     turtle.digUp()
  81.     turtle.forward()
  82.     updatePos()
  83.     local dX = currentX - oX
  84.     local dY = currentY - oY
  85.     local dZ = currentZ - oZ
  86.     distance = math.sqrt(dX*dX+dY*dY+dZ*dZ)
  87.   end
  88.   turtle.turnLeft()
  89.   turtle.turnLeft()
  90.   log("Returning..")
  91.   while distance > 0 do
  92.     turtle.dig()
  93.     turtle.digUp()
  94.     turtle.forward()
  95.     local dX = currentX - oX
  96.     local dY = currentY - oY
  97.     local dZ = currentZ - oZ
  98.     updatePos()
  99.     distance = math.sqrt(dX*dX+dY*dY+dZ*dZ)
  100.   end
  101.   if direction == "left" then
  102.     turtle.turnLeft()
  103.   elseif direction == "right" then
  104.     turtle.turnRight()
  105.   end
  106. end
  107.  
  108. function store()
  109.   local c
  110.   for i=1,16 do
  111.     turtle.select(i)
  112.     c = c + turtle.getItemCount(i)
  113.     turtle.drop(turtle.getItemCount(i))
  114.   end
  115.   log("Stored "..c.." items!")
  116. end
  117.  
  118. while true do
  119.   if(turtle.detect()) then
  120.     turtle.dig()
  121.     turtle.digUp()
  122.   end
  123.   move("fwd")
  124.   if(turtle.detect()) then
  125.     turtle.dig()
  126.     turtle.digUp()
  127.   end
  128.   move("fwd")
  129.   dig("left")
  130.   dig("right")
  131.   counter = counter + 2
  132.   log("Completed tunnel #"..counter/2)
  133.   if turtle.getItemCount(16)==64 then
  134.     log("I think I'm full. Let'g go home")
  135.     local pos = counter
  136.     while counter>0 do
  137.       move("bwd")
  138.       counter = counter - 1
  139.     end
  140.     turtle.turnLeft()
  141.     store()
  142.     turtle.turnRight()
  143.     while counter<pos do
  144.       turtle.move("fwd")
  145.       counter = counter +1
  146.     end
  147.   end
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement