Advertisement
Guest User

quarry

a guest
Jul 24th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. arg = {...}
  2.  
  3. width = nil
  4. length = nil
  5.  
  6. compareSlots = 3
  7. fuelSlots = 1
  8.  
  9. if arg[1] ~= nil and arg[2] ~= nil then
  10.   width = arg[1]
  11.   length = arg[2]
  12. else
  13.   term.setTextColor(colors.red)
  14.   print("Usage: quarry <width> <length>")
  15.   return
  16. end
  17.  
  18. x = 0
  19. z = 0
  20. y = 0
  21. facing = 0
  22.  
  23. function empty()
  24.   turtle.select(1)
  25.   while turtle.detect() do
  26.     turtle.dig()
  27.   end
  28.   turtle.place()
  29.   c = peripheral.wrap("front")
  30.   for slot = 2+compareSlots,16-fuelSlots do
  31.     emptySpace = false
  32.     while not emptySpace do
  33.       itemsNb = #c.getAllStacks()
  34.       invSize = c.getInventorySize()
  35.       if itemsNb < invSize then
  36.         emptySpace = true
  37.       end
  38.       sleep(1)
  39.     end
  40.     turtle.select(slot)
  41.     turtle.drop()
  42.   end
  43.   c = nil
  44.   turtle.dig()
  45. end
  46.  
  47. function mineMinerals()
  48.   shouldDigUp = true
  49.   shouldDigDown = true
  50.   for slot = 2, 1+compareSlots do
  51.     turtle.select(slot)
  52.     if turtle.compareUp() and shouldDigUp then
  53.       shouldDigUp = false
  54.     else
  55.     end
  56.     if turtle.compareDown() and shouldDigDown then
  57.       shouldDigDown = false
  58.     end
  59.   end
  60.   if shouldDigDown then digDown() end
  61.   if shouldDigUp then digUp() end
  62. end
  63.  
  64. function quarry()
  65.   while down() do end
  66.   up()
  67.   mineLayer()
  68.   while nextLayer() do
  69.     mineLayer()
  70.   end
  71.   empty()
  72. end
  73.  
  74. function printInfo()
  75.   term.setCursorPos(1,1)
  76.   term.clear()
  77.   print("x: "..x.." y: "..y.." z: "..z.." f: "..facing)
  78. end
  79.  
  80. function nextLayer()
  81.   next = true
  82.   if width % 2 == 0 then
  83.     turnRight()
  84.     while x > 0 do step() end
  85.   else
  86.     turnLeft()
  87.     while x > 0 do step() end
  88.     turnLeft()
  89.     while z > 0 do step() end
  90.   end
  91.   lastLayer = y
  92.   while y < lastLayer + 3 do
  93.     if y < 0 then
  94.       up()
  95.     end
  96.   end
  97.   while facing ~= 0 do
  98.     if width % 2 == 0 then
  99.       turnRight()
  100.     else
  101.       turnLeft()
  102.     end
  103.   end
  104.   if y == 0 then next = false end
  105.   return next
  106. end
  107.  
  108. function refuel()
  109.   if turtle.getFuelLevel() < 5 then
  110.     for slot = 16-fuelSlots, 16 do
  111.       if turtle.getItemCount(slot) > 1 then
  112.         turtle.select(slot)
  113.         turtle.refuel(1)
  114.         return true
  115.       end
  116.     end
  117.   end
  118.   return false
  119. end
  120.  
  121. function step()
  122.   printInfo()
  123.   mineMinerals()
  124.   while not turtle.forward() do
  125.     if not refuel() then
  126.       if turtle.detect() then
  127.         dig()
  128.       else
  129.         turtle.attack()
  130.       end
  131.     end
  132.   end
  133.   if facing == 0 then z = z + 1 end
  134.   if facing == 2 then z = z - 1 end
  135.   if facing == 1 then x = x + 1 end
  136.   if facing == 3 then x = x - 1 end
  137. end
  138.  
  139. function up()
  140.   printInfo()
  141.   while not turtle.up() do
  142.     if not refuel() then
  143.       if turtle.detectUp() then
  144.         digUp()
  145.       else
  146.         turtle.attackUp()
  147.       end
  148.     end
  149.   end
  150.   y = y + 1
  151. end
  152.  
  153. function down()
  154.   printInfo()
  155.   done = false
  156.   while not done do
  157.     done = turtle.down()
  158.     if done then
  159.       y = y - 1
  160.       return true
  161.     end
  162.     if not refuel() then
  163.       if turtle.detectDown() then
  164.         if digDown() then
  165.           turtle.down()
  166.           y = y - 1
  167.           return true
  168.         else return false end
  169.       else
  170.         turtle.attackDown()
  171.       end
  172.     end
  173.   end
  174. end
  175.  
  176. function turnLeft()
  177.   printInfo()
  178.   turtle.turnLeft()
  179.   facing = facing - 1
  180.   if facing < 0 then facing = 3 end
  181. end
  182.  
  183. function turnRight()
  184.   printInfo()
  185.   turtle.turnRight()
  186.   facing = facing + 1
  187.   if facing > 3 then facing = 0 end
  188. end
  189.  
  190. function turnAround()
  191.   if z == length-1 then
  192.     turn = function() turnRight() end
  193.   elseif z == 0 then
  194.     turn = function() turnLeft() end
  195.   end
  196.   turn()
  197.   step()
  198.   turn()
  199. end
  200.  
  201. function mineLayer()
  202.   for i = 1,width do
  203.     for j = 1,length-1 do
  204.       step()
  205.     end
  206.     if x ~= width - 1 then
  207.       turnAround()
  208.     end
  209.   end
  210. end
  211.  
  212. function shouldEmpty()
  213.   if turtle.getItemCount(16 - fuelSlots - 1) > 0 then
  214.     empty()
  215.   end
  216. end
  217.  
  218. function dig()
  219.   shouldEmpty()
  220.   return turtle.dig()
  221. end
  222.  
  223. function digDown()
  224.   shouldEmpty()
  225.   return turtle.digDown()
  226. end
  227.  
  228. function digUp()
  229.   shouldEmpty()
  230.   return turtle.digUp()
  231. end
  232.  
  233. quarry()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement