Advertisement
onix331

Stripmining V.2.6 For all CC-Versions Turtle-side

Aug 1st, 2014
4,546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.04 KB | None | 0 0
  1. --This code is developed by me (onix331) and is available under the GNU General Public License (Version 3)
  2. --For all CC-Versions Turtle-Side!
  3. --See http://opensource.org/licenses/GPL-3.0
  4. --Have fun
  5. space = 16--Defines the space of the turtle (If an update comes out and I don't update the program)
  6. ver = "2.6"--For updates
  7. status = "---"--You can modify the init-status that will be transferred to the host
  8.  
  9. --It is usually not necessary to modify the next variables
  10.  
  11. fw = 0--Unnecessary to know:P
  12. chest = 0--If the turtle has stopped at a chest
  13. stop = 0--If the turtle has stopped at a chest
  14. shaftrun = 0--How many shafts the turtle has run
  15. senderId = 65534--Because RedNet is a bitch:D
  16.  
  17. function smartfuel()--Check for fuel and asks for refueling
  18.     clear()
  19.     local fuelneeded = (length * count) * 2 + count * 3 --Calculate Fuel for mining blocks
  20.     local movefuelneeded = ((length * count) * 2 + count) * 0.6  --Calculate Fuel for movement
  21.     local fuelneeded = fuelneeded + movefuelneeded
  22.     if turtle.getFuelLevel() < fuelneeded then
  23.         insert = fuelneeded - turtle.getFuelLevel()
  24.         print ("Current fuel-level is too low (", turtle.getFuelLevel(), ")")
  25.         print ("Needed:" , fuelneeded)
  26.         print ("Please insert ", insert, " fuel anywhere")
  27.         print ("When done, press ENTER")
  28.         debugfuel = turtle.getFuelLevel()
  29.         while true do
  30.             local sEvent, param = os.pullEvent("key")
  31.             if sEvent == "key" then
  32.                 if param == 28 then
  33.                     for i = 1, space do
  34.                         for i = 1, 64 do
  35.                             clear()
  36.                             if turtle.getFuelLevel() < fuelneeded then
  37.                                 shell.run("refuel")
  38.                             else
  39.                                 turtle.select(1)
  40.                                 break
  41.                             end
  42.                         end
  43.                         if debugfuel > turtle.getFuelLevel() then
  44.                             turtle.select(debugi)
  45.                         else
  46.                             debugi = i
  47.                             turtle.select(i)
  48.                         end
  49.                     end
  50.                     turtle.select(1)
  51.                     break
  52.                 end
  53.             end
  54.         end
  55.     if turtle.getFuelLevel() < fuelneeded then
  56.         smartfuel()
  57.     end
  58.     end
  59. end
  60.  
  61. function pair()
  62.     print ("Sending pairing requests")
  63.     local i = 0
  64.     rednet.broadcast("pair")
  65.     os.sleep(0.1)
  66.     rednet.broadcast("pair")
  67.     os.sleep(0.1)
  68.     rednet.broadcast("pair")
  69.     os.sleep(0.1)
  70.     rednet.broadcast("pair")
  71.     os.sleep(0.1)
  72.     rednet.broadcast("pair")
  73.     os.sleep(0.1)
  74.     rednet.broadcast("pair")
  75.     os.sleep(0.1)
  76.     senderId, message, protocol = rednet.receive()
  77.     if message=="succesfull" then
  78.         print("Succesfull paired, continuing...")
  79.         os.sleep(0.1)
  80.     end
  81. end
  82.  
  83. function clear()--To clear the screen
  84.     term.clear()
  85.     term.setCursorPos(1,1)
  86. end
  87.  
  88. function sendinitial()--Send the initial data
  89.     if enablerednet=="y" then
  90.         os.sleep(1)
  91.         rednet.send(senderId, length)
  92.         os.sleep(0.1)
  93.         rednet.send(senderId, count)
  94.         os.sleep(0.1)
  95.         rednet.send(senderId, direction)
  96.         os.sleep(0.1)
  97.     end
  98. end
  99.  
  100. function senddata()--To send all the data
  101.     if enablerednet=="y" then
  102.         rednet.send(senderId, lengthleft)
  103.         rednet.send(senderId, count)
  104.         rednet.send(senderId, turtle.getFuelLevel())
  105.         rednet.send(senderId, status)
  106.     end
  107. end
  108.  
  109. function main()--Main Program
  110.     smartfuel()
  111.     if enablerednet=="y" then
  112.         pair()
  113.         sendinitial()
  114.     end
  115.     status = "Called main"
  116.     senddata()
  117.     if called==1 then
  118.         called = 0
  119.         countn = countn - 1
  120.     end
  121.     while true do
  122.         if tonumber(count)>=1 then 
  123.             senddata()
  124.             if direction=="r" then
  125.                 fw = 1
  126.                 move()
  127.                 turnaround()
  128.                 fw = 0
  129.                 move()
  130.                 count = count - 1
  131.                 checkspace()
  132.             elseif direction=="l" then
  133.                 fw = 1
  134.                 move()
  135.                 turnaround()
  136.                 fw = 0
  137.                 move()
  138.                 count = count - 1
  139.                 checkspace()
  140.             end
  141.         else
  142.             break
  143.         end
  144.     end
  145. end
  146.  
  147. function returnmining()--To return to the latest shaft
  148.     status = "Return to mine..."
  149.     senddata()
  150.     chest = 0
  151.     stop = 1
  152.     if direction=="r" then
  153.         turtle.turnRight()
  154.         for i = 0, shaftrun do
  155.             turtle.forward()
  156.             turtle.forward()
  157.             turtle.forward()
  158.         end
  159.         turtle.turnLeft()
  160.         main()
  161.     elseif direction=="l" then
  162.         turtle.turnLeft()
  163.         for i = 0, shaftrun do
  164.             turtle.forward()
  165.             turtle.forward()
  166.             turtle.forward()
  167.         end
  168.         turtle.turnRight()
  169.         main()
  170.     end
  171. end
  172.  
  173. function checkl() --To check if length is valid (>0, not a String), also transforms the string into a number
  174.     length = tonumber(length)
  175.     if length==nil then
  176.         print ("Invalid number!")
  177.         length = read()
  178.         checkl()
  179.     elseif length <= 0 then
  180.         print ("Invalid number!")
  181.         length = read()
  182.         checkl()
  183.     end
  184. end
  185.  
  186. function checka()--To check if count is valid (>0, not a String), also transforms the string into a number
  187.     count = tonumber(count)
  188.     if count==nil then
  189.         print ("Invalid number!")
  190.         count = read()
  191.         checka()
  192.     elseif count <= 0 then
  193.         print ("Invalid number!")
  194.         count = read()
  195.         checka()
  196.     end
  197. end
  198.  
  199. function checkr()--To check if direction is valid ((r) or (l))
  200.     if direction=="r" then
  201.         print ("")
  202.     elseif direction=="l" then
  203.         print ("")
  204.     else
  205.         print ("Invalid input!")
  206.         direction = io.read()
  207.         checkr()
  208.     end
  209. end
  210.  
  211. function checkfue()--To check if fuel is valid (ok))
  212.     if fuel=="ok" then
  213.         print ("")
  214.     else
  215.         print ("Invalid input!")
  216.         fuel = io.read()
  217.         checkfue()
  218.     end
  219. end
  220.  
  221. function checkred()--To check if enablerednet is valid ((y) or (n))
  222.     if enablerednet=="y" then
  223.         print ("")
  224.     elseif enablerednet=="n" then
  225.         print ("")
  226.     else
  227.         print ("Invalid input!")
  228.         enablerednet = io.read()
  229.         checkred()
  230.     end
  231. end
  232.  
  233. function move()--Basic movement function
  234.     status = "Moving..."
  235.     local debug = 4--Used to avoid gavel, sand, etc.
  236.     for i = 1, length do
  237.         senddata()
  238.         for i = 1, debug do
  239.             turtle.dig()
  240.         end
  241.         turtle.forward()
  242.         if fw==1 then
  243.             lengthleft = lengthleft - 1
  244.         end
  245.         turtle.digDown()
  246.         for i = 1, debug do
  247.             turtle.digUp()
  248.         end
  249.         for i = 1, debug do
  250.             turtle.digDown()
  251.         end
  252.     end
  253. end
  254.  
  255. function turnaround()--Turns the turtle at 90 degrees
  256.     status = "Turning around..."
  257.     senddata()
  258.     turtle.turnRight()
  259.     turtle.turnRight()
  260. end
  261.  
  262. function nextshaftr()--Next shaft (right)
  263.     status = "Going to next shaft..."
  264.     senddata()
  265.     countn = countn - 1
  266.     if countn>=2 then
  267.         shaftrun = shaftrun + 1
  268.         lengthleft = lengthleftstore
  269.         turtle.turnLeft()
  270.         turtle.dig()
  271.         turtle.digUp()
  272.         turtle.digDown()
  273.         turtle.forward()
  274.         turtle.dig()
  275.         turtle.digUp()
  276.         turtle.digDown()
  277.         turtle.forward()
  278.         senddata()
  279.         turtle.dig()
  280.         turtle.digUp()
  281.         turtle.digDown()
  282.         turtle.forward()
  283.         turtle.digUp()
  284.         turtle.digDown()
  285.         turtle.turnLeft()
  286.     end
  287. end
  288.  
  289. function nextshaftl()--Next shaft (left)
  290.     status = "Going to next shaft..."
  291.     senddata()
  292.     countn = countn - 1
  293.     if countn>=2 then  
  294.         shaftrun = shaftrun + 1
  295.         lengthleft = lengthleftstore
  296.         turtle.turnRight()
  297.         turtle.dig()
  298.         turtle.digUp()
  299.         turtle.digDown()
  300.         turtle.forward()
  301.         turtle.dig()
  302.         turtle.digUp()
  303.         turtle.digDown()
  304.         turtle.forward()
  305.         senddata()
  306.         turtle.dig()
  307.         turtle.digUp()
  308.         turtle.digDown()
  309.         turtle.forward()
  310.         turtle.digUp()
  311.         turtle.digDown()
  312.         turtle.turnRight()
  313.     end
  314. end
  315.  
  316. function returnpoint()--Return to initial point (Chest)
  317.     status = "Returning to chest..."
  318.     senddata()
  319.     if direction=="r" then
  320.         turtle.turnRight()
  321.         for i = 1, shaftrun do
  322.             turtle.forward()
  323.             turtle.forward()
  324.             senddata()
  325.             turtle.forward()
  326.         end
  327.         if stop==1 then
  328.             turtle.forward()
  329.             turtle.forward()
  330.             senddata()
  331.             turtle.forward()
  332.         end
  333.         turtle.turnLeft()
  334.         status = "Dropping things in chest..."
  335.         senddata()
  336.         for i = 1, space do
  337.             turtle.select(i)
  338.             turtle.drop()
  339.         end
  340.         turtle.select(1)
  341.     elseif direction=="l" then
  342.         turtle.turnLeft()
  343.         for i = 1, shaftrun do
  344.             turtle.forward()
  345.             turtle.forward()
  346.             senddata()
  347.             turtle.forward()
  348.         end
  349.         if stop==1 then
  350.             turtle.forward()
  351.             turtle.forward()
  352.             senddata()
  353.             turtle.forward()
  354.         end
  355.         turtle.turnRight()
  356.         status = "Dropping things in chest..."
  357.         senddata()
  358.         for i = 1, space do
  359.             turtle.select(i)
  360.             turtle.drop()
  361.         end
  362.         turtle.select(1)
  363.     end
  364.     turnaround()
  365.     if chest==1 then
  366.         returnmining()
  367.     end
  368. end
  369.  
  370. function checkspace()--Check for space and if necessary, return to initial point to the chest
  371.     status = "Checking space"
  372.     senddata()
  373.     local spaceleft = 0
  374.     for i = 1, space do
  375.         if turtle.getItemSpace(i) > 10 then
  376.             spaceleft = spaceleft + 1
  377.         end
  378.     end
  379.     if spaceleft < 3 then
  380.         chest = 1
  381.         called = 1
  382.         status = "Inventory full, returning to chest"
  383.         returnpoint()
  384.     elseif direction=="r" then
  385.         nextshaftr()
  386.     else
  387.         nextshaftl()
  388.     end
  389. end
  390.  
  391. --Print header
  392. clear()
  393. print ("Strip-mining-program by onix331")
  394. print ("Version ", ver)
  395. os.sleep(2)
  396.  
  397. --Start user input and define some variables
  398. clear()
  399. print ("Length of the single shaft(s):(Type help for help)")
  400. length = read()
  401. if length=="help" then
  402.     print ("How long the shafts the turtle will mine be")
  403.     length = read()
  404.     checkl()
  405. else
  406.     checkl()
  407. end
  408. lengthleft = length
  409. lengthleftstore = length
  410.  
  411. clear()
  412. print ("Count of the shafts:(Type help for help)")
  413. count = read()
  414. if count=="help" then
  415.     print ("How much shafts the turtle will mine")
  416.     count = read()
  417.     checka()
  418. else
  419. end
  420. countn = count
  421. countn = countn + 1
  422.  
  423. clear()
  424. print ("Direction of the shafts (Left (l), Right (r):(Type help for help)")
  425. direction = read()
  426. if direction=="help" then
  427.     print ("In which direction the shafts will go")
  428.     direction = read()
  429.     checkr()
  430. else
  431.     checkr()
  432. end
  433.  
  434. clear()
  435. print ("Use rednet? (Yes (y), No (n):(Type help for help)")
  436. enablerednet = read()
  437. if enablerednet=="help" then
  438.     print ("If RedNet and a hostcomputer should be used (See my thread at http://www.computercraft.info/forums2/index.php?/topic/19860-stripmining-turtle-for-cc16/")
  439.     enablerednet = read()
  440. else
  441.     checkred()
  442.     end
  443. if enablerednet=="y" then
  444.     rednet.open("right")--Defines the place of the modem (Usually right on the turtle)
  445. end
  446.  
  447. --Print user inputs
  448. clear()
  449. print ("Input of Length:")
  450. print (length)
  451. print ("Input of count")
  452. print (count)
  453. print ("Direction of the shafts:")
  454. if direction=="r" then
  455.     print (direction, "ight")
  456. else
  457.     print (direction, "eft")
  458. end
  459.  
  460. print ("3")
  461. os.sleep(1)
  462. print ("2")
  463. os.sleep(1)
  464. print ("1")
  465.  
  466.  
  467. --Fire up the main program
  468. main()
  469.  
  470. ende = 1
  471. returnpoint()
  472.  
  473. status = "Done!"
  474. senddata()--Sends the status "Done!" to the host
  475. print ("Done!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement