Advertisement
Zaflis

ccraft orequarry

Jan 15th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.51 KB | None | 0 0
  1. -- Mining starts from lower right corner of quarry heading forward
  2. -- Place 1 stone (not cobblestone) in slot 1
  3.  
  4. -- Turtle facing rotations
  5. --   0
  6. -- 1   3
  7. --   2
  8.  
  9. x = 1
  10. y = 1
  11. z = 1
  12. rot = 0 -- 0:+Z, 1:+X, 2:-Z, 3: -X
  13. ix = 1
  14. bedrock = false
  15. resuming = false
  16.  
  17. function writeStatus()
  18.   local f = fs.open("_status", "wb")
  19.   f.write(2) -- Orequarry ID
  20.   f.write(x)
  21.   f.write(y)
  22.   f.write(z)
  23.   f.write(rot)
  24.   f.write(ix+1) -- Byte is limited to 0, so -1 would loop over to 255
  25.   f.write(ty)
  26.   f.close()
  27. end
  28.  
  29. function quarry(sx, sy, sz)
  30.   local fuelDelay = 260
  31.   local oldix = ix
  32.   if (sx < 1) or (sy < 1) or (sz < 1) then
  33.     return
  34.   end
  35.  
  36.   fuel(1000, 10000)
  37.   empty()
  38.   if resuming then
  39.     local f = fs.open("_status", "rb")
  40.     f.read() -- Orequarry ID
  41.     x = f.read()
  42.     y = f.read()
  43.     z = f.read()
  44.     rot = f.read()
  45.     ix = f.read()-1
  46.     ty = f.read()
  47.     f.close()
  48.     print(string.format("Status: %d %d %d %d %d", x, y, z, rot, ix))
  49.   else    
  50.     if sx*sy*sz == 1 then return
  51.     elseif sy > 2 then ty = 2
  52.     end
  53.     if sx > 255 then sx = 255 end
  54.     if sy > 255 then sy = 255 end
  55.     if sz > 255 then sz = 255 end
  56.     local f = fs.open("_area", "wb")
  57.     f.write(sx)
  58.     f.write(sy)
  59.     f.write(sz)
  60.     f.close()
  61.   end
  62.  
  63.   -- Start quarry loop
  64.   sdelay = 0
  65.   while true do
  66.     if sdelay == 0 then sleep(1) end
  67.     sdelay = (sdelay+1) % 10
  68.    
  69.     oldix = ix
  70.     if y < ty then
  71.       turtle.digDown()
  72.       if turtle.down() then
  73.         y = y+1  writeStatus()
  74.         empty()
  75.       else
  76.         bedrock = true
  77.         ty = y
  78.       end
  79.     elseif rot == 0 then -- Going forward
  80.       if z < sz then
  81.         if dig(sy) then
  82.           z = z+1  writeStatus()
  83.         end
  84.       else -- Reached edge
  85.         if ix > 0 then turnTo(1)
  86.         else turnTo(3)
  87.         end
  88.       end
  89.     elseif rot == 2 then -- Going backwards
  90.       if z > 1 then
  91.         if dig(sy) then
  92.           z = z-1  writeStatus()
  93.         end
  94.       else -- Reached edge
  95.         if ix > 0 then turnTo(1)
  96.         else turnTo(3)
  97.         end
  98.       end
  99.     elseif rot == 1 then -- Going left
  100.       if x < sx then
  101.         if dig(sy) then
  102.           x = x+1  writeStatus()
  103.         end
  104.       else
  105.         ty = math.min(y+3, sy)
  106.         ix = -ix  writeStatus()
  107.         while turtle.detectUp() do
  108.           if turtle.compareUp() then break end
  109.           if not turtle.digUp() then break end
  110.         end
  111.         if (math.abs(ty-y)<1.5) or bedrock then break end
  112.       end
  113.       if z < 2 then turnTo(0)
  114.       else turnTo(2)
  115.       end
  116.     elseif rot == 3 then -- Going right
  117.       if x > 1 then
  118.         if dig(sy) then
  119.           x = x-1  writeStatus()
  120.         end
  121.       else
  122.         ty = math.min(y+3, sy)
  123.         ix = -ix  writeStatus()
  124.         while turtle.detectUp() do
  125.           if turtle.compareUp() then break end
  126.           if not turtle.digUp() then break end
  127.         end
  128.         if (math.abs(ty-y)<1.5) or bedrock then break end
  129.       end
  130.       if z < 2 then turnTo(0)
  131.       else turnTo(2)
  132.       end
  133.     end
  134.    
  135.     fuelDelay = fuelDelay-1
  136.     if fuelDelay < 0 then
  137.       fuelDelay = 260
  138.       if turtle.getFuelLevel() < 600 then
  139.         fuel(10000, 10000)
  140.       end
  141.     end
  142.   end
  143.  
  144.   fs.delete("_status")
  145.   fs.delete("_area")
  146.  
  147.   -- Return to surface
  148.  
  149.   -- Come up half way
  150.   while y > 1+sy/2 do
  151.     y = y-1
  152.     turtle.digUp()  turtle.up()
  153.   end
  154.  
  155.   -- Come to X = 1
  156.   -- Turn to left
  157.   turnTo(3, true) -- Turn but don't save
  158.   while x > 1 do
  159.     x = x-1
  160.     turtle.dig()  turtle.forward()
  161.   end  
  162.  
  163.   -- Come to Z = 1
  164.   -- Turn to backwards
  165.   rot = 2
  166.   turtle.turnRight()
  167.   while z > 1 do
  168.     z = z-1
  169.     turtle.dig()  turtle.forward()
  170.   end  
  171.  
  172.   -- Come up all the way
  173.   while y > 1 do
  174.     y = y-1
  175.     turtle.digUp()  turtle.up()
  176.   end
  177.   -- Finally empty items to chest
  178.   doEmpty()
  179.   print("Mining trip finished!")
  180. end
  181.  
  182. function dig(sy)
  183.   turtle.select(1)
  184.   while turtle.detectUp() do
  185.     if turtle.compareUp() then break end
  186.     if not turtle.digUp() then break end
  187.   end
  188.   if (y < sy) and (not bedrock) and turtle.detectDown() then
  189.     if not turtle.compareDown() then
  190.       if not turtle.digDown() then
  191.         bedrock = true
  192.       end
  193.     end
  194.   end
  195.   while turtle.detect() do
  196.     if not turtle.dig() then
  197.       repeat
  198.         empty()
  199.       until not turtle.suck()
  200.       break
  201.     end
  202.     empty()
  203.   end
  204.   return turtle.forward()
  205. end
  206.  
  207. function doEmpty()
  208.   print("Emptying inventory...")
  209.   while turtle.detectUp() do
  210.     if not turtle.digUp() then
  211.       break
  212.     end
  213.   end
  214.   turtle.select(16) -- Select ender chest for placing
  215.   if not turtle.placeUp() then
  216.     print("Error placing ender chest")
  217.     return
  218.   end  
  219.   for i=2,14 do
  220.     turtle.select(i)
  221.     turtle.dropUp()
  222.   end
  223.   turtle.select(16)
  224.   turtle.digUp()
  225.   sleep(1)
  226.   print("Emptying finished")
  227. end
  228.  
  229. function empty()
  230.   if (turtle.getItemCount(13) > 0) or (turtle.getItemCount(14) > 0) then
  231.     doEmpty()
  232.   end
  233. end
  234.  
  235. function fuel(minfuel, maxfuel)
  236.   empty() -- Make sure slot 14 is empty
  237.   write("Checking fuel...")
  238.   if turtle.getFuelLevel() > minfuel then
  239.     print("Fuel tank already full")
  240.     return
  241.   end
  242.   while turtle.detectUp() do
  243.     if not turtle.digUp() then
  244.       break
  245.     end
  246.   end
  247.   local chest = false
  248.   turtle.select(14)
  249.   while turtle.getFuelLevel() < maxfuel do
  250.     turtle.refuel()
  251.     if not chest then
  252.       turtle.select(15)
  253.       if not turtle.placeUp() then
  254.         print("Error placing ender chest")
  255.         break
  256.       end
  257.       chest = true
  258.       turtle.select(14)
  259.     end
  260.     turtle.suckUp()
  261.     print("Fuel "..turtle.getFuelLevel())
  262.   end
  263.   if chest then
  264.     turtle.select(15)
  265.     turtle.digUp()
  266.   end
  267.   print("Fuel tank filled")
  268. end
  269.  
  270. function turnTo(newRot, nosave)
  271.   function capRot(r)
  272.     if r < -0.5 then r = r+4
  273.     elseif r > 3.5 then r = r-4
  274.     end
  275.     return r
  276.   end
  277.  
  278.   if rot ~= newRot then
  279.     if capRot(rot-1) == newRot then
  280.       turtle.turnRight()
  281.     elseif capRot(rot+1) == newRot then
  282.       turtle.turnLeft()
  283.     else
  284.       turtle.turnRight() turtle.turnRight()
  285.     end
  286.     rot = newRot
  287.     if nosave == nil then writeStatus() end
  288.   end  
  289. end
  290.  
  291. -- Main program begins
  292.  
  293. if fs.exists("_status") and fs.exists("_area") then
  294.   local f = fs.open("_status", "rb")
  295.   m = f.read()
  296.   f.close()
  297.   if m == 2 then
  298.     resuming = true
  299.     local f = fs.open("_area", "rb")
  300.     local sx = f.read()
  301.     local sy = f.read()
  302.     local sz = f.read()
  303.     f.close()
  304.     print(string.format("Resuming quarry (%d x %d x %d)", sx, sy, sz))
  305.     quarry(sx, sy, sz)
  306.     fs.delete("_status")
  307.   end
  308. else
  309.   if turtle.getItemCount(1) == 0 then
  310.     print("Missing filter, normally stone (slot 1)")
  311.     return
  312.   end
  313.   if turtle.getItemCount(16) == 0 then
  314.     print("Missing enderchest (slot 16) for emptying")
  315.     print("Continue anyway? [y/n] ")
  316.     if read() ~= "y" then return end
  317.   end
  318.   if turtle.getItemCount(15) == 0 then
  319.     print("Missing enderchest (slot 15) for fuel")
  320.     print("Continue anyway? [y/n] ")
  321.     if read() ~= "y" then return end
  322.   end
  323.  
  324.   param = {...}
  325.   if #param == 3 then
  326.     quarry(0+param[1], 0+param[2], 0+param[3])
  327.   elseif #param == 2 then
  328.     quarry(0+param[1], 255, 0+param[2])
  329.   else
  330.     print("Usage: orequarry [width] ([height] optional) [depth]")
  331.     print("Example: orequarry 30 99 30")
  332.     print("     or: orequarry 32 64")
  333.     print("Meeting bedrock will stop the turtle.")
  334.     print("Turtle will advance forward and left")
  335.   end
  336.   fs.delete("_status")
  337. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement