Advertisement
Ranger1230

Advanced mining turtle 1.1.?

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