xXTurner

automine - computercraft

May 18th, 2020
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ---------------
  2. -- functions --
  3. ---------------
  4.  
  5. function mineCycle()
  6.   turtle.dig()
  7.   turtle.forward()
  8.   turtle.digDown()
  9.   turtle.turnLeft()
  10.   turtle.dig()
  11.   turtle.down()
  12.   turtle.dig()
  13.   turtle.turnRight()
  14.   turtle.turnRight()
  15.   turtle.dig()
  16.   turtle.up()
  17.   turtle.dig()
  18.   turtle.turnLeft()
  19. end
  20.  
  21. function isInventoryFull()
  22.   local slotsOccupied = 0
  23.   for i=1,16 do
  24.     if turtle.getItemCount(i) > 0 then
  25.       slotsOccupied = slotsOccupied + 1
  26.     end
  27.   end
  28.  
  29.   if slotsOccupied == 16 then return true end
  30. end
  31.  
  32. function placeTorch(torchAmount)
  33.   turtle.placeDown()
  34.   torchAmount = torchAmount - 1
  35.   if turtle.getItemCount(1) == 0 then
  36.     turtle.select(math.ceil(torchAmount / 64) + 1)
  37.     turtle.transferTo(1)
  38.     turtle.select(1)
  39.   end
  40. end
  41.  
  42. function goBack_noFuel(traveledBlocks, torchAmount)
  43.   turtle.turnRight()
  44.   turtle.turnRight()
  45.  
  46.   for i=1,traveledBlocks / 3 do
  47.     if turtle.forward() == false then
  48.       turtle.dig()
  49.       turtle.forward()
  50.     end
  51.   end
  52.  
  53.   turtle.down()
  54.   turtle.turnLeft()
  55.  
  56.   dumpItems(torchAmount)
  57.  
  58.   print("\n\nGorivoto mi svurshi, spiram programata!")
  59. end
  60.  
  61. function goBack_finished(traveledBlocks, distance)
  62.   turtle.turnRight()
  63.   turtle.turnRight()
  64.  
  65.   for i=1,traveledBlocks / 3 do
  66.     if turtle.forward() == false then
  67.       turtle.dig()
  68.       turtle.forward()
  69.     end
  70.   end
  71.  
  72.   turtle.down()
  73.   turtle.turnLeft()
  74.  
  75.   dumpItems_full()
  76.  
  77.   print("\n\nUspeshno izkopah "..distance.." bloka tunel :)")
  78. end
  79.  
  80. function dumpItems(torchAmount)
  81.   if torchAmount > 0 then
  82.     for i=math.ceil(torchAmount / 64) + 1,16 do
  83.       turtle.select(i)
  84.       if (turtle.drop() == false) and (turtle.getItemCount() ~= 0) then
  85.         print("Chesta e pulen, nemoga da se raztovarq, spiram programata")
  86.       end
  87.     end
  88.   else
  89.     dumpItems_full()
  90.   end
  91.  
  92.   turtle.select(1)
  93. end
  94.  
  95. function dumpItems_full()
  96.   for i=1,16 do
  97.     turtle.select(i)
  98.     if (turtle.drop() == false) and (turtle.getItemCount() ~= 0) then
  99.         print("Chesta e pulen, nemoga da se raztovarq, no programata veche beshe svurshila :)")
  100.     end
  101.   end
  102. end
  103.  
  104. ----------
  105. -- CODE --
  106. ----------
  107.  
  108. -- automine <distance>
  109. local distance = ...
  110.  
  111. if distance == nil then
  112.   print("automine <rastoqnie>\n")
  113.   print("Lqv chest = Tuk se slagat torchovete\n")
  114.   print("Desen chest = Tuk se slaga gorivoto (samo purviq put predi start, posle ne e nujno), i se slagat resursite koito sa izkopani")
  115.   return
  116. elseif tonumber(distance) == nil then
  117.   print("Samo chislo e pozvoleno, "..distance.." ne e chislo!")
  118.   return
  119. end
  120.  
  121. --inv check
  122. for i=1,16 do
  123.   if turtle.getItemCount(i) > 0 then
  124.     print("Izchistete inventara na miner-a.")
  125.     return
  126.   end
  127. end
  128.  
  129. -- fuel check
  130. print("Proverqvam za gorivo..")
  131.  
  132. turtle.turnRight()
  133. if turtle.suck() == false then
  134.   print("Nqma nikakvo gorivo vuv desniq chest.")
  135.   turtle.turnLeft()
  136.   return
  137. end
  138.  
  139. while turtle.suck() == true do
  140.   turtle.suck()
  141. end
  142.  
  143. local rawFuelAmount = 0
  144.  
  145. for i=1,16 do
  146.   rawFuelAmount = rawFuelAmount + turtle.getItemCount(i)
  147. end
  148.  
  149. -- refuiling
  150. local oldFuel = turtle.getFuelLevel()
  151.  
  152. for i=1,16 do
  153.   turtle.select(i)
  154.   if turtle.getFuelLevel() ~= turtle.getFuelLimit() then
  155.     turtle.refuel()
  156.   end
  157. end
  158.  
  159. if turtle.getFuelLevel() < 300 then
  160.   print("Imash samo "..turtle.getFuelLevel().." gorivo. Ne e dustatachno! Slujete gorivo vuv desniq chest i pusnete programata otnovo.")
  161.  
  162.   for i=1,16 do
  163.     if turtle.getItemCount(i) > 0 then
  164.       turtle.select(i)
  165.       if turtle.drop() == false then
  166.         print("Nemoga da vurna predmetite koito ne sa gorivo! (Pulen e desniq chest).")
  167.       end
  168.     end
  169.   end
  170.   turtle.select(1)
  171. end
  172.  
  173. if (turtle.getFuelLevel() - oldFuel) == 0 then
  174.   print("Gorivoto e pulno do-gore, nqma nujda za prezarejdane, vrushtat se predmetite v desniq chest")
  175. else
  176.   print("Imash "..rawFuelAmount.." predmeta, ot koito se napraviha "..(turtle.getFuelLevel() - oldFuel).." gorivo.")
  177. end
  178.  
  179. -- inventory cleanup if junk items found
  180. for i=1,16 do
  181.   if turtle.getItemCount(i) > 0 then
  182.     turtle.select(i)
  183.     if turtle.drop() == false then
  184.       print("Nemoga da vurna predmetite koito ne sa gorivo! (Pulen e desniq chest).")
  185.     end
  186.   end
  187. end
  188. turtle.select(1)
  189. turtle.turnLeft()
  190.  
  191. -- torch check
  192. print("\nProverqvam za torch-ove..")
  193.  
  194. turtle.turnLeft()
  195. if turtle.suck() == false then
  196.   print("Nqma nikakvi predmeti vuv leviq chest.")
  197.   turtle.turnRight()
  198.   return
  199. end
  200.  
  201. while turtle.suck() == true do
  202.   turtle.suck()
  203. end
  204.  
  205. local torchAmount = 0
  206.  
  207. for i=1,16 do
  208.   torchAmount = torchAmount + turtle.getItemCount(i)
  209. end
  210.  
  211. print("Imash "..torchAmount.." torch-a koito shte budat izpolzvani (ako ne svurshi gorivoto predi tova).")
  212.  
  213. turtle.select(1)
  214. turtle.turnRight()
  215.  
  216. -- Mining
  217. local traveledBlocks = 0
  218.  
  219. print("\n\nZapochvam da kupaq "..distance.." bloka tunel..")
  220.  
  221. turtle.digUp()
  222. turtle.up()
  223. traveledBlocks = traveledBlocks + 1
  224.  
  225. while true do
  226.   if (math.floor(traveledBlocks / 3)) == tonumber(distance) then
  227.     goBack_finished(traveledBlocks, distance)
  228.     turtle.turnLeft()
  229.     return
  230.   end
  231.  
  232.   if (traveledBlocks + 3 + 1) >= turtle.getFuelLevel() then
  233.     goBack_noFuel(traveledBlocks, torchAmount)
  234.     turtle.turnLeft()
  235.     return
  236.   end
  237.  
  238.   mineCycle()
  239.   traveledBlocks = traveledBlocks + 3
  240.  
  241.   if ((math.floor(traveledBlocks / 3) - 1) % 11) == 0 then
  242.     placeTorch(torchAmount)
  243.   end
  244.  
  245.   if isInventoryFull() then
  246.     local temp_traveledBlocks = 0
  247.  
  248.     turtle.turnRight()
  249.     turtle.turnRight()
  250.  
  251.     for i=1,traveledBlocks / 3 do
  252.       turtle.forward()
  253.       temp_traveledBlocks = temp_traveledBlocks + 1
  254.     end
  255.  
  256.     traveledBlocks = traveledBlocks + temp_traveledBlocks + 1
  257.     turtle.down()
  258.  
  259.     turtle.turnLeft()
  260.  
  261.     dumpItems(torchAmount)
  262.  
  263.     turtle.turnLeft()
  264.     turtle.up()
  265.  
  266.     local temp_traveledBlocksBack = 0
  267.     for i=1,temp_traveledBlocks do
  268.       turtle.forward()
  269.       temp_traveledBlocksBack = temp_traveledBlocksBack + 1
  270.     end
  271.  
  272.     traveledBlocks = traveledBlocks + temp_traveledBlocksBack
  273.   end
  274. end
RAW Paste Data