onix331

Mining-turtle (Stripmining) V.1.7

Aug 1st, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. --This code is developed by me (onix331) and is availiable under the GNU General Public License (Version 3)
  2. --See http://opensource.org/licenses/GPL-3.0
  3. --Have fun
  4.  
  5. space = 16--Defines the space of the turtle
  6. ver = 1.7--For updates
  7. chest = 0--If the turtle has stopped at a chest
  8. stop = 0--If the turtle has stopped at a chest
  9. shaftrun = 0--How many shafts the turtle has run
  10.  
  11. function main()--Main Program
  12.     while true do
  13.         if count>=1 then   
  14.             if direction=="r" then
  15.                 move()
  16.                 turnaround()
  17.                 move()
  18.                 count = count - 1
  19.                 checkspace()
  20.             elseif direction=="l" then
  21.                 move()
  22.                 turnaround()
  23.                 move()
  24.                 count = count - 1
  25.                 checkspace()
  26.             end
  27.         else
  28.             break
  29.         end
  30.     end
  31. end
  32.  
  33. function returnmining()--To return to the latest shaft
  34.     chest = 0
  35.     stop = 1
  36.     if direction=="r" then
  37.         turtle.turnRight()
  38.         for i = 0, shaftrun do
  39.             turtle.forward()
  40.             turtle.forward()
  41.             turtle.forward()
  42.         end
  43.         turtle.turnLeft()
  44.         main()
  45.     elseif direction=="l" then
  46.         turtle.turnLeft()
  47.         for i = 0, shaftrun do
  48.             turtle.forward()
  49.             turtle.forward()
  50.             turtle.forward()
  51.         end
  52.         turtle.turnRight()
  53.         main()
  54.     end
  55. end
  56.  
  57. function checkl() --To check if length is valid (>0, not a String), also transforms the string into a number
  58.     length = tonumber(length)
  59.     if length==nil then
  60.         print ("Invalid number!")
  61.         length = read()
  62.         checkl()
  63.     elseif length <= 0 then
  64.         print ("Invalid number!")
  65.         length = read()
  66.         checkl()
  67.     end
  68. end
  69.  
  70. function checka()--To check if count is valid (>0, not a String), also transforms the string into a number
  71.     count = tonumber(count)
  72.     if count==nil then
  73.         print ("Invalid number!")
  74.         count = read()
  75.         checka()
  76.     elseif count <= 0 then
  77.         print ("Invalid number!")
  78.         count = read()
  79.         checka()
  80.     end
  81. end
  82.  
  83. function checkr()--To check if direction is valid ((r) or (l))
  84.     if direction=="r" then
  85.         print ("")
  86.     elseif direction=="l" then
  87.         print ("")
  88.     else
  89.         print ("Invalid input!")
  90.         direction = io.read()
  91.         checkr()
  92.     end
  93. end
  94.  
  95. function checkref()--To check if refuel is valid ((y) or (n))
  96.     if refuel=="y" then
  97.         print ("")
  98.     elseif refuel=="n" then
  99.         print ("")
  100.     else
  101.         print ("Invalid input!")
  102.         refuel = io.read()
  103.         checkref()
  104.     end
  105. end
  106.  
  107. function move()--Basic movement function
  108.     local debug = 8--Used to avoid gavel, sand, etc.
  109.     for i = 1, length do
  110.         if turtle.detect() then
  111.             for i = 1, debug do
  112.                 turtle.dig()
  113.             end
  114.             turtle.forward()
  115.         else
  116.             turtle.forward()
  117.         end
  118.         if turtle.detectUp() then
  119.             for i = 1, debug do
  120.                 turtle.digUp()
  121.             end
  122.         end
  123.     end
  124. end
  125.  
  126. function turnaround()--Turns the turtle at 90 degrees
  127.     turtle.turnRight()
  128.     turtle.turnRight()
  129. end
  130.  
  131. function nextshaftr()--Next shaft (right)
  132.     countn = countn - 1
  133.     if countn>=2 then
  134.         shaftrun = shaftrun + 1
  135.         turtle.turnLeft()
  136.         turtle.dig()
  137.         turtle.digUp()
  138.         turtle.forward()
  139.         turtle.dig()
  140.         turtle.digUp()
  141.         turtle.forward()
  142.         turtle.dig()
  143.         turtle.digUp()
  144.         turtle.forward()
  145.         turtle.digUp()
  146.         turtle.turnLeft()
  147.     end
  148. end
  149.  
  150. function nextshaftl()--Next shaft (left)
  151.     countn = countn - 1
  152.     if countn>=2 then  
  153.         shaftrun = shaftrun + 1
  154.         turtle.turnRight()
  155.         turtle.dig()
  156.         turtle.digUp()
  157.         turtle.forward()
  158.         turtle.dig()
  159.         turtle.digUp()
  160.         turtle.forward()
  161.         turtle.dig()
  162.         turtle.digUp()
  163.         turtle.forward()
  164.         turtle.digUp()
  165.         turtle.turnRight()
  166.     end
  167. end
  168.  
  169. function returnpoint()--Return to initial point
  170.    
  171.     if direction=="r" then
  172.         turtle.turnRight()
  173.         for i = 1, shaftrun do
  174.             turtle.forward()
  175.             turtle.forward()
  176.             turtle.forward()
  177.         end
  178.         if stop==1 then
  179.             turtle.forward()
  180.             turtle.forward()
  181.             turtle.forward()
  182.         end
  183.         turtle.turnLeft()
  184.         for i = 1, space do
  185.             turtle.select(i)
  186.             turtle.drop()
  187.         end
  188.         turtle.select(1)
  189.     elseif direction=="l" then
  190.         turtle.turnLeft()
  191.         for i = 1, shaftrun do
  192.             turtle.forward()
  193.             turtle.forward()
  194.             turtle.forward()
  195.         end
  196.         if stop==1 then
  197.             turtle.forward()
  198.             turtle.forward()
  199.             turtle.forward()
  200.         end
  201.         turtle.turnRight()
  202.         for i = 1, space do
  203.             turtle.select(i)
  204.             turtle.drop()
  205.         end
  206.         turtle.select(1)
  207.     end
  208.     turnaround()
  209.     if chest==1 then
  210.         returnmining()
  211.     end
  212. end
  213.  
  214. function checkspace()--Check for space and if necessary, return to initial point to the chest
  215.     local spaceleft = 0
  216.     for i = 1, space do
  217.         if turtle.getItemSpace(i) > 10 then
  218.             spaceleft = spaceleft + 1
  219.         end
  220.     end
  221.     if spaceleft < 5 then
  222.         chest = 1
  223.         countn = countn - 1
  224.         returnpoint()
  225.     else
  226.         if direction=="r" then
  227.             nextshaftr()
  228.         else
  229.             nextshaftl()
  230.         end
  231.     end
  232. end
  233.  
  234. shell.run ("clear")
  235. print ("Strip-mining-program by onix331")
  236. print ("Version ", ver)
  237.  
  238. --Start user input
  239. print ("Length of the single shaft(s):")
  240. length = read()
  241. checkl()
  242.  
  243. print ("Count of the shafts:")
  244. count = read()
  245. checka()
  246. countn = count
  247. countn = countn + 1
  248.  
  249. print ("Direction of the shafts (Left (l), Right (r):")
  250. direction = read()
  251. checkr()
  252. --Refueling, a little bit complicatet
  253. print ("Dont forget to refuel!")
  254. print ("Now? (Yes (y), No (n):")
  255. refuel = read()
  256. checkref()
  257. if refuel=="y" then
  258.     print ("Please insert fuel anywhere")
  259.     print ("When done, press ENTER")
  260.     while true do
  261.         local sEvent, param = os.pullEvent("key")
  262.         if sEvent == "key" then
  263.             if param == 28 then
  264.                 for i = 1, space do
  265.                     for i = 1, 64 do
  266.                         shell.run("refuel", rafzahl)
  267.                     end
  268.                     turtle.select(i)
  269.                 end
  270.                 turtle.select(1)
  271.                 break
  272.             end
  273.         end
  274.     end
  275. end
  276.  
  277. --Print user inputs
  278. shell.run ("clear")
  279. print ("Input of Length:")
  280. print (length)
  281. print ("Input of count")
  282. print (count)
  283. print ("Direction of the shafts:")
  284. print (direction)
  285. os.sleep(2)
  286. print ("3")
  287. os.sleep(1)
  288. print ("2")
  289. os.sleep(1)
  290. print ("1")
  291. os.sleep(1)
  292. print("Start!")
  293. main()
  294.  
  295. returnpoint()
  296. print ("Done!")
Advertisement
Add Comment
Please, Sign In to add comment