Tuxi

Autominer

Sep 13th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 KB | None | 0 0
  1. lavaslot = 15
  2. chestslot = 16
  3.  
  4. comingBack = false
  5. length = 5
  6.  
  7. x,z,orient = 0,0,0
  8.  
  9. function start()
  10.     if loadLoc() then
  11.         checkChest()
  12.         fixOrient()
  13.         isComingBack()
  14.     end
  15. end
  16.  
  17. function checkChest()
  18.     if turtle.getItemCount(chestslot) == 0 then
  19.         turtle.select(chestslot)
  20.         turtle.dig()
  21.     end
  22.     turtle.select(1)
  23. end
  24.  
  25. function fixOrient()
  26.     if orient == 1 then
  27.         if x == length and isEven(z) == false then
  28.             right()
  29.         elseif x == length and isEven(z) then
  30.             print("hep")
  31.             forward()
  32.             right()
  33.         end
  34.         if x == 0 and isEven(z) == false then
  35.             forward()
  36.             left()
  37.         elseif x == 0 and isEven(z) then
  38.             left()
  39.         end
  40.     end
  41. end
  42.  
  43. function isComingBack()
  44.     if isEven(z) then
  45.         comingBack = false
  46.     else
  47.         comingBack = true
  48.     end
  49. end
  50.  
  51. function saveLoc()
  52.     h = fs.open("location.dat","w")
  53.     h.writeLine(tostring(x))
  54.     h.writeLine(tostring(z))
  55.     h.writeLine(tostring(orient))
  56.     h.close()  
  57. end
  58.  
  59. function loadLoc()
  60.     if fs.exists("location.dat") then
  61.         h = fs.open("location.dat","r")
  62.         x = tonumber(h.readLine())
  63.         z = tonumber(h.readLine())
  64.         orient = tonumber(h.readLine())
  65.         h.close()
  66.         return true
  67.     else
  68.         return false
  69.     end
  70. end
  71.  
  72. function isEven(number)
  73.     if number%2 == 0 then
  74.         return true
  75.     else
  76.         return false
  77.     end
  78. end
  79.  
  80. function grabLava()
  81.     turtle.select(lavaslot)
  82.    
  83.     if turtle.place() then
  84.         if turtle.refuel() == false then
  85.             turtle.place()
  86.         end
  87.     end
  88.    
  89.     if turtle.placeUp() then
  90.         if turtle.refuel() == false then
  91.             turtle.placeUp()
  92.         end
  93.     end
  94.    
  95.     if turtle.placeDown() then
  96.         if turtle.refuel() == false then
  97.             turtle.placeDown()
  98.         end
  99.     end
  100.     turtle.select(1)
  101. end
  102.  
  103. function dig()
  104.     grabLava()
  105.     turtle.digUp()
  106.     turtle.digDown()
  107. end
  108.  
  109. --[[
  110. function forward()
  111.     while turtle.forward() == false do
  112.         turtle.dig()
  113.         sleep(0)
  114.     end
  115.     saveLoc()
  116.     if orient == 0 then
  117.         x = x+1
  118.     elseif orient == 1 then
  119.         z = z+1
  120.     elseif orient == 2 then
  121.         x = x-1
  122.     elseif orient == 3 then
  123.         z = z-1
  124.     end
  125. end
  126. --]]
  127.  
  128. function forward()
  129.     local cont = true
  130.     turtle.dig()
  131.    
  132.     while cont do
  133.         if orient == 0 then
  134.             x = x+1
  135.         elseif orient == 1 then
  136.             z = z+1
  137.         elseif orient == 2 then
  138.             x = x-1
  139.         elseif orient == 3 then
  140.             z = z-1
  141.         end
  142.         saveLoc()
  143.         if turtle.forward() then
  144.             cont = false
  145.         else
  146.             turtle.dig()
  147.             if orient == 0 then
  148.                 x = x-1
  149.             elseif orient == 1 then
  150.                 z = z-1
  151.             elseif orient == 2 then
  152.                 x = x+1
  153.             elseif orient == 3 then
  154.                 z = z+1
  155.             end
  156.             saveLoc()
  157.             sleep(1)
  158.         end    
  159.     end
  160. end
  161.  
  162. function right()   
  163.     if orient == 3 then
  164.         orient = 0
  165.     else
  166.         orient = orient+1
  167.     end
  168.     saveLoc()
  169.     turtle.turnRight()
  170. end
  171.  
  172. function left()
  173.     if orient == 0 then
  174.         orient = 3
  175.     else
  176.         orient = orient-1
  177.     end
  178.     saveLoc()
  179.     turtle.turnLeft()
  180. end
  181.  
  182. function dump()
  183.     if turtle.getItemCount(14) > 0 then
  184.         turtle.select(chestslot)
  185.         while turtle.place() == false do
  186.             turtle.dig()
  187.             sleep(0)
  188.         end
  189.         for i = 1, 14 do
  190.             turtle.select(i)
  191.             while turtle.drop() == false and turtle.getItemCount(i) > 0 do
  192.                 sleep(0)
  193.             end    
  194.         end
  195.         turtle.select(chestslot)
  196.         while turtle.dig() == false do
  197.             sleep(0)
  198.         end
  199.         turtle.select(1)
  200.     end
  201. end
  202.  
  203. function move()
  204.     if comingBack then
  205.         if x > 0 then
  206.             forward()
  207.         else
  208.             left()
  209.             forward()
  210.             left()
  211.             comingBack = false
  212.         end
  213.     else
  214.         if x < length then
  215.             forward()
  216.         else
  217.             right()
  218.             forward()
  219.             right()
  220.             comingBack = true
  221.         end
  222.     end
  223. end
  224.  
  225.  
  226. start()
  227. while true do
  228.     dig()
  229.     dump()
  230.     move()
  231.     sleep(0)
  232. end
Advertisement
Add Comment
Please, Sign In to add comment