Advertisement
Ranger1230

Advanced mining turtle 1.1.7

Jun 28th, 2013
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.49 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1,1)
  3. sel = 0
  4. fuelThreshold = 300
  5. if turtle.getFuelLevel() == "unlimited" then
  6.         turtle.getFuelLevel = function()
  7.                 return 2 + fuelThreshold
  8.         end
  9. end
  10. if fs.exists("coords") == false then
  11.     print("Hi, I am your mining robot!\n\nI will need to ask you a couple of questions before I start mining:\n\nWhat diameter would you like me to mine?\n")
  12.      setSize =read()
  13.     print("\nCool! Now insert the turtles current y coordinate\n")
  14.     input = read()
  15.     setDepth = input-5
  16.     print("\nJust one more thing- put any blocks you don't want me to mine in slots 1-4 and press enter.. or just leave it empty to mine everything (I will be slower the more you put there)\n")
  17.     temp=read()
  18.     setJunkSlots = 0
  19.     for i = 1,4 do
  20.         if turtle.getItemCount(i) ~= 0 then
  21.             setJunkSlots = setJunkSlots + 1
  22.         end
  23.     end
  24.     print("\nOkay, I will start mining now :D")
  25.    
  26.    
  27.     w = fs.open("coords","w")
  28.     w.write(setSize.."\n"..setDepth.."\n1\n1\n1\n0\n"..setJunkSlots)
  29.     w.close()
  30.     w = fs.open("coords2","w")
  31.     w.write(setSize.."\n"..setDepth.."\n1\n1\n1\n0\n"..setJunkSlots)
  32.     w.close()
  33.     w = fs.open("startup","w")
  34.     w.write('shell.run("dig")')
  35.     w.close()
  36.     sleep(3)
  37.     term.clear()
  38.     term.setCursorPos(1,1)
  39.    
  40. end
  41.  
  42. --Gets the necessary variables from the file coords
  43.  
  44. function Load()
  45.     h = fs.open("coords","r")
  46.     size = tonumber(h.readLine(1))
  47.     depth = tonumber(h.readLine(2))
  48.     x = tonumber(h.readLine(3))
  49.     y = tonumber(h.readLine(4))
  50.     z = tonumber(h.readLine(5))
  51.     facing = tonumber(h.readLine(6))
  52.     junkSlots = tonumber(h.readLine(7))
  53.     h.close()
  54. end
  55. --A backup file in case the first one is corrupted.
  56. function Load2()
  57.     h = fs.open("coords2","r")
  58.     size = tonumber(h.readLine(1))
  59.     depth = tonumber(h.readLine(2))
  60.     x = tonumber(h.readLine(3))
  61.     y = tonumber(h.readLine(4))
  62.     z = tonumber(h.readLine(5))
  63.     facing = tonumber(h.readLine(6))
  64.     junkSlots = tonumber(h.readLine(7))
  65.     h.close()
  66. end
  67. function drop(fslot,lslot)
  68.     for i=fslot,lslot do
  69.         turtle.select(i)
  70.         while turtle.drop() == false and turtle.getItemCount(i) ~= 0 do
  71.             print("Can't drop items- full?")
  72.             sleep(2)
  73.         end
  74.     end
  75. end
  76.  
  77. --Currently saves to two files for backup purposes. Will be broken up later for unloaded chunk handling.
  78. function save()
  79.         s = fs.open("coords","w")
  80.         s.writeLine(size)
  81.         s.writeLine(depth)
  82.         s.writeLine(x)
  83.         s.writeLine(y)
  84.         s.writeLine(z)
  85.         s.writeLine(facing)
  86.         s.writeLine(junkSlots)
  87.         s.close()
  88.        
  89.         s = fs.open("coords2","w")
  90.         s.writeLine(size)
  91.         s.writeLine(depth)
  92.         s.writeLine(x)
  93.         s.writeLine(y)
  94.         s.writeLine(z)
  95.         s.writeLine(facing)
  96.         s.writeLine(junkSlots)
  97.         s.close()
  98. end
  99. --save function, prints the turtles orientation and coords to a file
  100. function rflforw()
  101.     if turtle.forward() == true then
  102.         if facing == 0  then
  103.           x = x+1
  104.           save()
  105.         elseif facing == 1 then
  106.           y= y-1
  107.           save()
  108.         elseif facing == 2  then
  109.           x = x - 1
  110.           save()
  111.         elseif facing == 3 then
  112.           y = y + 1
  113.           save()
  114.         else
  115.             error("Invalid facing variable")
  116.         end
  117.     else
  118.         print("Can't move forward, trying again...")
  119.         while turtle.forward() == false do
  120.             turtle.dig()
  121.             sleep(0.3)
  122.         end
  123.         if facing == 0  then
  124.           x = x+1
  125.           save()
  126.         elseif facing == 1 then
  127.           y= y-1
  128.           save()
  129.         elseif facing == 2  then
  130.           x = x - 1
  131.           save()
  132.         elseif facing == 3 then
  133.           y = y + 1
  134.           save()
  135.         end
  136.     end
  137. end
  138. --refuel-less forward function
  139. function dig()
  140.   turtle.dig()
  141. end
  142. function digdown()
  143.    turtle.digDown()
  144. end
  145. function digup()
  146.   turtle.digUp()
  147. end
  148. function refuel()
  149.     local char = ""
  150.     if turtle.getFuelLevel() < fuelThreshold then
  151.         if turtle.getItemCount(16) == 0 then
  152.             oldfacing = facing
  153.             oldx = x
  154.             oldy = y
  155.             oldz = z
  156.             while facing ~= 1 do
  157.                 left()
  158.             end
  159.             while y ~= 1 do
  160.                 rflforw()
  161.             end
  162.             while facing ~= 2 do
  163.                 left()
  164.             end
  165.             while x ~= 1 do
  166.                 rflforw()
  167.             end
  168.             while z ~= 1 do
  169.                 up()
  170.             end
  171.             drop(junkSlots + 1, 15)
  172.             while facing ~= 1 do
  173.                 left()
  174.             end
  175.             turtle.select(16)
  176.             turtle.drop()
  177.             turtle.suck()
  178.             while turtle.getItemCount(16) < 16 do
  179.                 print('Not enough fuel, Refill the chest and press enter')
  180.                 temp=read()
  181.                 turtle.drop()
  182.                 turtle.suck()
  183.                 turtle.refuel(1)
  184.             end
  185.             while facing ~= 0 do
  186.                 left()
  187.             end
  188.             while z ~= oldz do
  189.                 down()
  190.             end
  191.             while x ~= oldx do
  192.                 rflforw()
  193.             end
  194.             while facing ~= 3 do
  195.                     left()
  196.             end
  197.             while y ~= oldy do
  198.                     forw()
  199.             end
  200.             while facing ~= oldfacing do
  201.                     left()
  202.             end
  203.             else
  204.                 while true do
  205.                     turtle.select(16)
  206.                     turtle.refuel(1)
  207.                     turtle.select(1)
  208.                     if turtle.getFuelLevel() > fuelThreshold then
  209.                         break
  210.                     elseif turtle.getItemCount(16) == 0 then
  211.                         refuel()
  212.                     end
  213.                 end
  214.             end
  215.     end
  216. end
  217. function forw()
  218.     refuel()
  219.     if turtle.forward() == true then
  220.         if facing == 0  then
  221.           x = x+1
  222.           save()
  223.         elseif facing == 1 then
  224.           y= y-1
  225.           save()
  226.         elseif facing == 2  then
  227.           x = x - 1
  228.           save()
  229.         elseif facing == 3 then
  230.           y = y + 1
  231.           save()
  232.         else
  233.             error("Invalid facing variable")
  234.         end
  235.     else
  236.         print("Can't move forward, trying again...")
  237.         while turtle.forward() == false do
  238.             turtle.dig()
  239.             sleep(0.3)
  240.         end
  241.         if facing == 0  then
  242.           x = x+1
  243.           save()
  244.         elseif facing == 1 then
  245.           y= y-1
  246.           save()
  247.         elseif facing == 2  then
  248.           x = x - 1
  249.           save()
  250.         elseif facing == 3 then
  251.           y = y + 1
  252.           save()
  253.         end
  254.     end
  255. end
  256. function down()
  257.         if turtle.down() == true then
  258.                 z = z + 1
  259.         else
  260.                 print("Something is blocking my way down, trying again!")
  261.                 while turtle.down() == false do
  262.                         sleep(0.3)
  263.                         digdown()
  264.                 end
  265.                 z= z + 1
  266.         end
  267.                        
  268. end
  269. function up()
  270.         if turtle.up() == true then
  271.                 z = z - 1
  272.         else
  273.                 print("Something is blocking my way up, trying again!")
  274.                 while turtle.up() == false do
  275.                         sleep(0.3)
  276.                         digup()
  277.                 end
  278.                 z= z - 1
  279.         end
  280.                        
  281. end
  282. function left()
  283.         turtle.turnLeft()
  284.         facing = facing - 1
  285.         facing = facing % 4
  286.         save()
  287. end
  288. function right()
  289.         turtle.turnRight()
  290.         facing = facing + 1
  291.         facing = facing % 4
  292.         save()
  293. end
  294. function comine()
  295.     local isJunkd = false
  296.     local isJunku = false
  297.     sel = sel + 1
  298.     if junkSlots == 0 then
  299.         sel = 0
  300.     else
  301.         sel = sel % junkSlots
  302.     end
  303.     if sel == 0 then
  304.         for i = 1 , junkSlots do
  305.             turtle.select(i)
  306.             if turtle.compareUp()== true then
  307.                 isJunku = true
  308.             end
  309.             if turtle.compareDown()== true then
  310.                 isJunkd = true
  311.             end
  312.         end
  313.     else
  314.         for i = junkSlots , 1, -1 do
  315.             turtle.select(i)
  316.             if turtle.compareUp()== true then
  317.                 isJunku = true
  318.             end
  319.             if turtle.compareDown()== true then
  320.                 isJunkd = true
  321.             end
  322.         end
  323.     end
  324.     if isJunku == false then
  325.         digup()
  326.     end
  327.     if isJunkd == false then
  328.         digdown()
  329.     end
  330.        
  331. end
  332. function diglayer()
  333.         for j = 1,size - 1 do
  334.                         comine()
  335.                         dig()
  336.                         forw()
  337.                         if turtle.getItemCount(15) > 0 then
  338.                                 oldfacing = facing
  339.                                 oldx = x
  340.                                 oldy = y
  341.                                 oldz = z
  342.                                 while facing ~= 1 do
  343.                                         left()
  344.                                 end
  345.                                 while y ~= 1 do
  346.                                         forw()
  347.                                 end
  348.                                 while facing ~= 2 do
  349.                                         left()
  350.                                 end
  351.                                 while x ~= 1 do
  352.                                         forw()
  353.                                 end
  354.                                 while z ~= 1 do
  355.                                         up()
  356.                                 end
  357.                                 drop(junkSlots + 1, 15)
  358.                                 turtle.select(sel + 1)
  359.                                 while facing ~= 0 do
  360.                                         left()
  361.                                 end
  362.                                 while z ~= oldz do
  363.                                         down()
  364.                                 end
  365.                                 while x ~= oldx do
  366.                                         forw()
  367.                                 end
  368.                                 while facing ~= 3 do
  369.                                         left()
  370.                                 end
  371.                                 while y ~= oldy do
  372.                                         forw()
  373.                                 end
  374.                                 while facing ~= oldfacing do
  375.                                         left()
  376.                                 end
  377.                         end
  378.                 end
  379.         end
  380. function program()
  381.     if size % 2 == 0 then
  382.         for i = 1 , size / 2 - 1 do
  383.             diglayer()
  384.             comine()
  385.             left()
  386.             dig()
  387.             forw()
  388.             left()
  389.             diglayer()
  390.             comine()
  391.             right()
  392.             dig()
  393.             forw()
  394.             right()
  395.         end
  396.         diglayer()
  397.         comine()
  398.         left()
  399.         dig()
  400.         forw()
  401.         left()
  402.         diglayer()
  403.         comine()
  404.         left()
  405.         for i = 1, size-1 do
  406.             forw()
  407.         end
  408.         left()
  409.     else
  410.         for i = 1, (size - 1)/2 do
  411.             diglayer()
  412.             comine()
  413.             left()
  414.             dig()
  415.             forw()
  416.             left()
  417.             diglayer()
  418.             comine()
  419.             right()
  420.             dig()
  421.             forw()
  422.             right()
  423.         end
  424.         diglayer()
  425.         comine()
  426.         right()
  427.         while y ~= 1 do
  428.             forw()
  429.         end
  430.         right()
  431.         while x ~= 1 do
  432.             forw()
  433.         end
  434.         left()
  435.         left()
  436.     end
  437. end
  438. if(pcall(Load) == false) then
  439.     Load2()
  440. end
  441. refuel()
  442. refuel()
  443. if x ~= 1 or y ~= 1 or facing ~= 0 then
  444.         while facing ~= 1 do
  445.             left()
  446.         end
  447.         while y ~= 1 do
  448.             forw()
  449.         end
  450.         while facing ~= 2 do
  451.             right()
  452.         end
  453.         while x ~= 1 do
  454.             forw()
  455.         end
  456.         while facing ~= 0 do
  457.             left()
  458.         end
  459. end
  460. while z ~= depth do
  461.         program()
  462.         for i = 1, 3 do
  463.             if z == depth then
  464.                 break
  465.             end
  466.             digdown()
  467.             down()
  468.         end
  469. end
  470. program()
  471. while facing ~=2 do
  472.         left()
  473. end
  474. while z ~= 1 do
  475.         up()
  476. end
  477. drop(1,16)
  478. fs.delete("startup")
  479. fs.delete("coords")
  480. print("I am done now :D")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement