the-vindex

Strip mining

Sep 29th, 2013
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.63 KB | None | 0 0
  1. --Strip Mining Program--
  2. -- I didn't write it, just took and upgraded.
  3.    
  4.     torchDist = 10
  5.    
  6.     --Start of Torch stuff
  7.     function placeTorch()
  8.       print("Placing torch...")
  9.       if turtle.getItemCount(3) == 1 then
  10.         turtle.select(4) --Considering adding a method of returning home if no torches present in second slot
  11.             turtle.place()
  12.       else
  13.        turtle.select(3)
  14.               turtle.place()
  15.        turtle.select(1)
  16.       end
  17.       torchDist = 1
  18.     end
  19.    
  20.     function needTorch()
  21.       if torchDist == 10 then
  22.         placeTorch()
  23.       end
  24.     end
  25.     --End of Torch stuff
  26.    
  27.     function forward()
  28.       while not turtle.forward() do
  29.         turtle.dig()
  30.             sleep(.1)
  31.       end
  32.     end
  33.    
  34.     function dig()
  35.       while turtle.detect() do
  36.         turtle.dig()
  37.             sleep(.8)
  38.       end
  39.     end
  40.    
  41.     function digUp()
  42.       while turtle.detectUp() do
  43.         turtle.digUp()
  44.             sleep(.8)
  45.       end
  46.     end
  47.    
  48.     function clear()
  49.       shell.run("clear")
  50.     end
  51.    
  52.     function calculate()  --Change this when you start having him loop
  53.       local fuel = turtle.getFuelLevel()
  54.       local chests = turtle.getItemCount(2)
  55.       local torches = turtle.getItemCount(3)+turtle.getItemCount(4)
  56.       local fuelCycles = math.floor(fuel/180)
  57.       local torchCycles = math.floor(torches/4)
  58.       local chestCycles = math.floor(chests*2)
  59.       local cycles = 0
  60.    
  61.       if fuelCycles<torchCycles then   --This big block takes cycles and figures out which between chests, fuel, and torches can perform
  62.         if fuelCycles<chestCycles then --least amount of cycles and sets cycles to that amount. Ensures resources for every cycle.
  63.                cycles = fuelCycles
  64.             else
  65.                cycles = chestCycles
  66.             end
  67.       elseif torchCycles<fuelCycles then
  68.         if torchCycles<chestCycles then
  69.                cycles = torchCycles
  70.             else
  71.                cycles = chestCycles
  72.             end
  73.       elseif fuelCycles<chestCycles then
  74.         if fuelCycles<torchCycles then
  75.                cycles = fuelCycles
  76.             else
  77.                cycles = torchCycles
  78.             end
  79.       elseif chestCycles<fuelCycles then
  80.         if chestCycles< torchCycles then
  81.                cycles = chestCycles
  82.             else
  83.                cycles = torchCycles
  84.             end
  85.       elseif torchCycles<chestCycles then
  86.         if torchCycles< fuelCycles then
  87.                cycles = torchCycles
  88.             else
  89.                cycles = fuelCycles
  90.             end
  91.       elseif chestCycles<torchCycles then
  92.         if chestCycles< fuelCycles then
  93.                cycles = chestCycles
  94.             else
  95.                cycles = fuelCycles
  96.             end
  97.       else
  98.         cycles = fuelCycles
  99.       end
  100.    
  101.       if fuel <500 or torches<10 then
  102.         print("I need both 500 fuel and at least 10 torches.")
  103.             print("I have "..torches.." torches and "..fuel.." fuel.")
  104.       else
  105.      clear()
  106.             print("-----------------------------------")
  107.             print("Fuel is: "..fuel)
  108.             print(chests.." chests in slot 2")
  109.             print(torches.." torches in slots 3 and 4")
  110.             print("Doing "..cycles.." cycles with these resources.")
  111.             print("-----------------------------------")
  112.             print("Is this acceptable? (y/n)")
  113.             local input = "{"
  114.             while not input == "y" or "n" do
  115.               input = read()
  116.               if input == "y" then
  117.                 branchControl(cycles)
  118.               elseif input == "n" then
  119.                 print("Shutting down program...")
  120.                     sleep(1.5)
  121.                     clear()
  122.                     break
  123.               else
  124.                 print("That is not a valid option! Please type y or n and hit ENTER")
  125.               end
  126.             end
  127.          
  128.          
  129.       end
  130.     end
  131.    
  132.     function chestDrop()
  133.       if turtle.detectDown() then
  134.         local x = 5
  135.             while x<17 do
  136.               turtle.select(x)
  137.               turtle.dropDown()
  138.               x=x+1
  139.             end
  140.       else
  141.       local x=5
  142.         turtle.select(2)
  143.             turtle.placeDown()
  144.             while x<17 do
  145.               turtle.select(x)
  146.               turtle.dropDown()
  147.               x=x+1
  148.             end
  149.       end
  150.     end
  151.    
  152.     function branchControl(cycles) --Don't indefinitely loop it yet
  153.      while cycles>0 do
  154.      if cycles>0 then
  155.       tunnel(5, false)
  156.       turtle.turnRight()
  157.       forward()
  158.       tunnel(30, true)
  159.       turtle.up()
  160.       forward()
  161.       chestDrop()
  162.       forward()
  163.       turtle.down()
  164.       cycles = cycles -1
  165.      else
  166.        break
  167.      end
  168.      if cycles>0 then
  169.       tunnel(30, true)
  170.       turtle.up()
  171.       forward()
  172.       chestDrop()
  173.       turtle.turnRight()
  174.       forward()
  175.       turtle.turnLeft()
  176.       dig()
  177.       digUp()
  178.       turtle.up()
  179.       dig()
  180.       turtle.turnRight()
  181.       turtle.turnRight()
  182.       dig()
  183.       turtle.down()
  184.       dig()
  185.       turtle.digDown()
  186.       turtle.down()
  187.       if turtle.detectDown() then
  188.         turtle.select(1)
  189.         turtle.placeDown()
  190.       end
  191.       dig()
  192.       turtle.turnLeft()
  193.       turtle.turnLeft()
  194.       dig()
  195.       turtle.turnRight()
  196.       cycles = cycles-1
  197.       else
  198.         break
  199.       end
  200.      end
  201.     end
  202.    
  203.     function tunnel(distance, isReturn) --Test this before trying to automate it!
  204.       local x =0
  205.       while x<distance do
  206.         forward()
  207.             if not turtle.detectDown() then
  208.               turtle.select(1)
  209.               turtle.placeDown()
  210.             end
  211.             turtle.turnLeft()
  212.             dig()
  213.             needTorch()
  214.             digUp()
  215.             turtle.up()
  216.             dig()
  217.             digUp()
  218.             turtle.up()
  219.             if not turtle.detectUp() then  --Sort of helps cover the place? Top and bottom at least get inspection XD
  220.               turtle.select(1)
  221.               turtle.placeUp()
  222.             end
  223.             dig()
  224.             turtle.turnRight()
  225.             turtle.turnRight()
  226.             dig()
  227.             turtle.down()
  228.             dig()
  229.             turtle.down()
  230.             dig()
  231.             turtle.turnLeft()
  232.             x=x+1
  233.             torchDist = torchDist+1
  234.       end
  235.    
  236.       --And here begins the returning home process
  237.       turtle.turnLeft()
  238.       turtle.turnLeft()
  239.       if isReturn == true then
  240.         local x=0
  241.         while x<distance do
  242.           forward()
  243.               x=x+1
  244.         end
  245.       end
  246.     end
  247.    
  248.    
  249.     function init()
  250.       calculate()
  251.     end
  252.    
  253.     init()
Advertisement
Add Comment
Please, Sign In to add comment