southpole

Quarry

Oct 19th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.49 KB | None | 0 0
  1. function saveFile()
  2.   local file = fs.open("dataList","w")
  3.   file.write(textutils.serialize(dataList))
  4.   file.close()
  5. end
  6.  
  7. function loadFile()
  8.   local file = fs.open("dataList","r")
  9.   local data = file.readAll()
  10.   file.close()
  11.   if string.len(data) > 0 and data ~= nil then
  12.     return textutils.unserialize(data)
  13.   end
  14. end
  15.  
  16. local slot = 1
  17. dataList = {}
  18. dataList.x = 0
  19. dataList.y = 0
  20. dataList.z = 0
  21. dataList.side = 0
  22. dataList.ignore = 0
  23. dataList.total = 0
  24. local oldx = 0
  25. local oldy = 0
  26. local oldz = 0
  27. local oldside = 0
  28.  
  29. dataList = loadFile()
  30.  
  31. --parameter for hole (square)
  32. local tArgs = { ... }
  33. if #tArgs ~= 1 then
  34.     print( "Usage: miner <diameter>" )
  35.     return
  36. end
  37. local size = tonumber( tArgs[1] )-1
  38. if size < 1 then
  39.     print( "Miner diameter must be positive." )
  40.     return
  41. end
  42.  
  43.  
  44. --takes all important start informations and gives infos
  45. function start()
  46.   if dataList.total == 0 then  
  47.   print("Place a chest with fuel on the left of the turtle!")
  48.   print("The turtle will also consume coal and other fuel resources, which it mines.")
  49.   print(" ")
  50.   print("Place blocks you don't want to get mined in the first few slots!")
  51.   print("The 1st slot will be used to cover the holes.")
  52.   print("Please place a cobble stone in the 2nd slot!")
  53.   print("When you've done, press any key!")
  54.   os.pullEvent("key")
  55.   local i
  56.   for i=1, 16, 1 do
  57.     turtle.select(i)
  58.     if turtle.getItemCount(i) > 0 then
  59.       dataList.ignore = dataList.ignore +1
  60.     end
  61.   end
  62.   print("You want to ignore " ..dataList.ignore.." blocks.")
  63.   turn(3)
  64.   for i=0,2,1 do
  65.     turtle.select(ignore+1)
  66.     turtle.suck()
  67.     turtle.refuel()
  68.     turtle.drop()
  69.   end
  70.   local fuel = turtle.getFuelLevel()
  71.   print("Fuel level: "..fuel)
  72.   turn(1)
  73.   dataList.total = dataList.total+1
  74.   end
  75. end
  76.  
  77. --drops all the items and counts them
  78. function drop()
  79.   local slot
  80.   home()
  81.   turn(2)
  82.   print("Full inventory, dropping items into chest.")
  83.   for slot=1, ignore ,1 do
  84.     turtle.select(slot)
  85.     local items = math.floor(turtle.getItemCount(slot)/2)
  86.     dataList.total = dataList.total + items
  87.     turtle.drop(items)
  88.   end
  89.   for slot=dataList.ignore+1, 16 do
  90.     turtle.select(slot)
  91.     local items = turtle.getItemCount(slot)
  92.     dataList.total = dataList.total + items
  93.     turtle.drop()
  94.   end
  95.   while dataList.side ~= 0 do
  96.     turn(1)
  97.   end
  98.   back()
  99. end
  100.  
  101. --checks if theres enough fuel for the movement
  102. function fuelcheck(togo)
  103.   local way = dataList.x+dataList.y+math.abs(dataList.z)+togo
  104.   local fuelLevel = turtle.getFuelLevel()
  105.   if fuelLevel == "unlimited" then
  106.     return true
  107.   end
  108.   if turtle.getFuelLevel() < way then
  109.     local fueled = false
  110.     for n=1,16 do
  111.       if turtle.getItemCount(n) > 0 then
  112.         turtle.select(n)
  113.         if turtle.refuel(1) then
  114.           while turtle.getItemCount(n) > 0 and turtle.getFuelLevel() < way do
  115.             turtle.refuel(1)
  116.           end
  117.           if turtle.getFuelLevel() >= way then
  118.             turtle.select(1)
  119.             return true
  120.           end
  121.         end
  122.       end
  123.     end
  124.     turtle.select(1)
  125.     return false
  126.   end
  127.   print("Fuel Level: " ..fuelLevel)
  128.   return true
  129. end
  130.  
  131. function refuel()
  132.   print("refueling.")
  133.   home()
  134.   turn(3)
  135.   for i=0,2,1 do
  136.     turtle.select(dataList.ignore+1)
  137.     turtle.suck()
  138.     turtle.refuel()
  139.     turtle.drop()
  140.   end
  141.   print("Fuel Level: " ..turtle.getFuelLevel())
  142.   back()
  143. end
  144.  
  145. function turn(times)
  146.   local i  
  147.   for i=1, times, 1 do
  148.     turtle.turnRight()
  149.     dataList.side = (dataList.side +1)%4
  150.   end
  151. end
  152.  
  153. function forward(dist)
  154.   local i
  155.   for i=1, dist, 1 do
  156.     while not turtle.forward() do
  157.       turtle.dig()
  158.       turtle.attack()
  159.     end
  160.     if dataList.side == 0 then
  161.       dataList.x = dataList.x+1
  162.     elseif dataList.side == 1 then
  163.       dataList.y = dataList.y+1
  164.     elseif dataList.side == 2 then
  165.       dataList.x = dataList.x-1
  166.     elseif dataList.side == 3 then
  167.       dataList.y = dataList.y-1
  168.     end
  169.   end
  170. end
  171.  
  172.  
  173. function down(dist)
  174.   local i
  175.   for i=1, dist, 1 do
  176.     if turtle.down() == false then
  177.       turtle.digDown()
  178.       if turtle.down() == false then
  179.         return false
  180.       end
  181.     end
  182.   dataList.z = dataList.z-1
  183.   end
  184.   return true
  185. end
  186.  
  187. function up()
  188.   local i
  189.   local tempz = math.abs(z)
  190.   for i=1, tempz, 1 do
  191.     while not turtle.up() do
  192.       turtle.digUp()
  193.       turtle.attackUp()
  194.     end
  195.   dataList.z = dataList.z+1
  196.   end
  197. end
  198.  
  199. function dropCobble()
  200.   local i
  201.   turtle.select(2)
  202.   for i=3,16,1 do
  203.     if turtle.compareTo(i) then
  204.       turtle.select(i)
  205.       turtle.dropDown()
  206.       turtle.select(2)
  207.     end
  208.   end
  209. end
  210.  
  211. function cover()
  212.   turtle.select(1)
  213.   down(1)
  214.   dropCobble()
  215.   local i
  216.   for i=1,4,1 do
  217.     if turtle.detect() == false then
  218.       if turtle.getItemCount(1) == 0 then
  219.         home()
  220.         print("No blocks to cover the holes!")
  221.         print("Place some blocks in the first slot")
  222.         print("Press any key if you've done")
  223.         os.pullEvent("key")
  224.         back()
  225.       end      
  226.         turtle.place()
  227.     end
  228.     turn(1)
  229.   end
  230.   turtle.up()
  231.   dataList.z=dataList.z+1
  232.   if turtle.getItemCount(1) == 0 then
  233.     home()
  234.     print("No blocks to cover the holes!")
  235.     print("Place some blocks in the first slot")
  236.     print("Press any key if you've done")
  237.     os.pullEvent("key")
  238.     back()
  239.   end
  240.   turtle.placeDown()
  241.   print("Covered hole.")
  242. end
  243.  
  244.  
  245. function home()
  246.   print("Going home.")
  247.   oldz = dataList.z
  248.   oldx = dataList.x
  249.   oldy = dataList.y
  250.   oldside = side
  251.   up()
  252.   while dataList.side ~= 2 do
  253.     turn(1)
  254.   end
  255.   forward(x)
  256.   turn(1)
  257.   forward(y)
  258.   while dataList.side ~= 0 do
  259.     turn(1)
  260.   end
  261. end
  262.  
  263. function back()
  264.   print("Going back to work.")
  265.   if fuelcheck((oldx+oldy+oldz)*2) then
  266.     while dataList.side ~= 0 do
  267.       turn(1)
  268.     end  
  269.     forward(oldx)
  270.     turn(1)
  271.     forward(oldy)
  272.     down(oldz)
  273.     while dataList.side ~= oldside do
  274.       turn(1)
  275.     end
  276.   else
  277.     print("Not enough fuel!")
  278.     refuel()
  279.   end
  280. end
  281.  
  282. function count()
  283.   local count = 0
  284.   local slot
  285.   for slot=1, 16, 1 do
  286.     count = count + turtle.getItemCount(slot)
  287.   end
  288.   return count
  289. end
  290.  
  291. function clean()
  292.   local slot
  293.   local i
  294.   local items = 0
  295.   for slot=1, 16, 1 do
  296.     if turtle.getItemCount(slot) > 0 then
  297.       items = items+1
  298.     end
  299.   end
  300.   if items == 16 then
  301.     for slot=1, 16, 1 do
  302.       turtle.select(slot)    
  303.       if turtle.getItemCount(slot) > 0 then
  304.         for i=slot+1, 16, 1 do
  305.           if turtle.compareTo(i) then
  306.             turtle.select(i)
  307.             turtle.transferTo(slot)
  308.             turtle.select(slot)
  309.           end
  310.         end  
  311.       end
  312.     end
  313.   end
  314. end
  315.  
  316. --turns around and checks for blocks to mine
  317. function miner()
  318.   local i
  319.   for i=0, 3, 1 do  
  320.     local test = 0
  321.     local slot
  322.     if turtle.detect() then
  323.       for slot=1, ignore ,1 do
  324.         turtle.select(slot)
  325.         if turtle.compare() then
  326.           test = test+1
  327.         end
  328.       end
  329.       if test == 0 then
  330.         local ammount = count()
  331.         clean()
  332.         turtle.dig()
  333.         if ammount == count() then
  334.           drop()
  335.         end
  336.       end
  337.     end
  338.       turn(1)
  339.   end
  340. end
  341.  
  342. --digs hole
  343. function dig()
  344.   if fuelcheck(140) then
  345.     while down(1) do  
  346.       miner()
  347.     end
  348.     up()
  349.     cover()
  350.   else
  351.     print("Not enough fuel!")
  352.     refuel()
  353.   end
  354. end
  355.  
  356.  
  357. --makes the pattern for the holes
  358. function mover()
  359.   if fuelcheck(10) then
  360.     if side == 0 then
  361.       if dataList.x+5 < size then
  362.         forward(5)
  363.       elseif dataList.y+1 <= size then
  364.         local dist = size-dataList.x
  365.         forward(dist)
  366.         turn(1)
  367.         forward(1)
  368.         turn(1)
  369.           if (dataList.x-(dist+3)%5) > 0 then
  370.             forward((dist+3)%5)
  371.           else
  372.             mover()
  373.           end
  374.       else
  375.         return false
  376.       end
  377.     elseif dataList.side == 2 then
  378.       if dataList.x-5 > 0 then
  379.         forward(5)
  380.       elseif dataList.y+1 <= size then
  381.         local dist = dataList.x
  382.         forward(x)
  383.         turn(3)
  384.         forward(1)
  385.         turn(3)
  386.           if (dataList.x-(dist+2)%5) < size then
  387.             forward((dist+2)%5)
  388.           else
  389.             mover()
  390.           end
  391.       else
  392.         return false
  393.       end
  394.     end
  395.   else
  396.     print("Not enough fuel!")
  397.     refuel()
  398.   end
  399.   return true
  400. end
  401.  
  402.  
  403.  
  404.  
  405. --main process
  406. function process()
  407.   start()
  408.   dig()
  409.   while mover() do
  410.     dig()
  411.   end
  412.   home()
  413.   drop()  
  414.   print("I've mined "..total.." items.")
  415. end
  416.  
  417. process()
Advertisement
Add Comment
Please, Sign In to add comment