Advertisement
yoshizawa81

Quarry modoki 0.1

Nov 9th, 2014
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.65 KB | None | 0 0
  1. ------------------------------------
  2. -- Quarry modoki
  3. -- by yoshizawa81
  4. -- version 0.1
  5.  
  6. -- Set backside chest empty. Turtle dump items this chest.
  7. -- Set underchest fuel. Need one (or lager) empty slot in this chest.
  8. -- Set turtle's inventory 16th slot (the lower right) fuel.
  9. -- This program need just 3 parameters.(depth width hight)
  10. -- Enjoy minecraft !
  11. ------------------------------------
  12.  
  13.  
  14. ------------------------------------
  15. -- config
  16. ------------------------------------
  17.  
  18. refuel_level = 1024
  19. fuel_inventory = 16
  20.  
  21. max_x = 64
  22. max_y = 64
  23. max_z = 128
  24.  
  25. -- moving config
  26. retry = 10
  27. wait = 3
  28.  
  29. ------------------------------------
  30. -- global vars
  31. ------------------------------------
  32.  
  33. -- turtle position
  34. x = 0 --depth
  35. y = 0 --width
  36. z = 0 --hight
  37. face = 0 --0:forword 1:right 2:back 3:left
  38.  
  39. -- digging position
  40. dx = 0 --depth
  41. dy = 0 --width
  42. dz = 0 --hight
  43. dface = 0 --0:forword 1:right 2:back 3:left
  44.  
  45. -- target position
  46. tgt_x = 0
  47. tgt_y = 0
  48. tgt_z = 0
  49.  
  50. ------------------------------------
  51. -- command line parameter include
  52. ------------------------------------
  53.  
  54. args = {...}
  55.  
  56. ------------------------------------
  57. -- functions
  58. ------------------------------------
  59.  
  60. -- command line parameter check
  61. function cmdline_check()
  62.    
  63.     if #args > 3 then
  64.         print("This program need just 3 parameters. depth width hight")
  65.         os.queueEvent("terminate")
  66.         sleep(1) --bad know haw...
  67.     end
  68.    
  69.     tgt_x = tonumber(args[1])
  70.     tgt_y = tonumber(args[2])
  71.     tgt_z = tonumber(args[3])
  72.    
  73.     if tgt_x > max_x then
  74.         print("Set depth less than "..max_x)
  75.         os.queueEvent("terminate")
  76.         sleep(1) --bad know haw...
  77.     end
  78.    
  79.     if tgt_y > max_y then
  80.         print("Set width less than "..max_y)
  81.         os.queueEvent("terminate")
  82.         sleep(1) --bad know haw...
  83.     end
  84.    
  85.     if tgt_z > max_z then
  86.         print("Set hight less than "..max_z)
  87.         os.queueEvent("terminate")
  88.         sleep(1) --bad know haw...
  89.     end
  90.    
  91.     tgt_y = tgt_y - 1
  92.    
  93. end
  94.  
  95. function my_forword()
  96.    
  97.     local tf = true
  98.     local cause = "string"
  99.     local i = 0
  100.    
  101.     tf,cause=turtle.forward()
  102.    
  103.     if tf==false then
  104.         if cause=="Movement obstructed" then
  105.             while i < retry do
  106.                 tf=turtle.forward()
  107.                 if tf == true then
  108.                     break
  109.                 end
  110.             sleep(wait)
  111.             i = i + 1
  112.             end
  113.            
  114.             if tf == false then
  115.                 print("Error. Obstructed.")
  116.                 os.queueEvent("terminate")
  117.                 sleep(1) --bad know haw...
  118.             end
  119.         end
  120.     end
  121. end
  122.  
  123. -- Came back home position.
  124. function back_home()
  125.     while z > 0 do
  126.         turtle.up()
  127.         z = z - 1
  128.     end
  129.    
  130.     if face == 0 then
  131.         turtle.turnRight()
  132.         face = 1
  133.         while y > 0 do
  134.             my_forword()
  135.             y = y - 1
  136.         end
  137.     elseif face == 1 then
  138.         while y > 0 do
  139.             my_forword()
  140.             y = y - 1
  141.         end
  142.     elseif face == 2 then
  143.         turtle.turnLeft()
  144.         face = 1
  145.         while y > 0 do
  146.             my_forword()
  147.             y = y - 1
  148.         end
  149.     elseif face == 3 then
  150.         turtle.turnRight()
  151.         turtle.turnRight()
  152.         face = 1
  153.         while y > 0 do
  154.             my_forword()
  155.             y = y - 1
  156.         end
  157.     end
  158.    
  159.     turtle.turnRight()
  160.     face = 2
  161.     while x > 0 do
  162.             my_forword()
  163.             x = x - 1
  164.     end
  165.  
  166. end
  167.  
  168. function my_down()
  169.    
  170.     local tf = true
  171.     local cause = "string"
  172.     local i = 0
  173.    
  174.     tf,cause=turtle.down()
  175.    
  176.     if tf==false then
  177.         if cause=="Movement obstructed" then
  178.             while i < retry do
  179.                 tf=turtle.down()
  180.                 if tf == true then
  181.                     break
  182.                 end
  183.             end
  184.             sleep(wait)
  185.             i = i + 1
  186.         end
  187.            
  188.         if tf == false then
  189.             back_home()
  190.             print("Error. Obstructed? End?")
  191.             os.queueEvent("terminate")
  192.             sleep(1) --bad know haw...
  193.         end
  194.     end
  195. end
  196.  
  197. -- Go digging position.
  198. function go_digpos()
  199.     turtle.turnRight()
  200.     turtle.turnRight()
  201.    
  202.     face = 0
  203.    
  204.     while x < dx do
  205.         my_forword()
  206.         x = x + 1
  207.     end
  208.    
  209.     turtle.turnLeft()
  210.    
  211.     face = 3
  212.    
  213.     while y < dy do
  214.         my_forword()
  215.         y = y + 1
  216.     end
  217.    
  218.     while z < dz do
  219.         my_down()
  220.         z = z + 1
  221.     end
  222.    
  223.     if dface==0 then
  224.         turtle.turnRight()
  225.         face=0
  226.     elseif dface==1 then
  227.         turtle.turnRight()
  228.         turtle.turnRight()
  229.         face=1
  230.     elseif dface==2 then
  231.         turtle.turnLeft()
  232.         face=2
  233.     end
  234. end
  235.  
  236. -- Get fuel from chest.
  237. function get_fuel()
  238.     turtle.select(fuel_inventory)
  239.    
  240.     local tf = false
  241.     local cause = "string"
  242.    
  243.     tf,cause = turtle.dropDown()
  244.    
  245.     if tf == false and cause == "Inventory full" then
  246.         print("After check chests, hit any key.")
  247.         os.pullEvent(key)
  248.         get_fuel()
  249.     end
  250.    
  251.     if turtle.suckDown() == false then
  252.         print("After check chests, hit any key.")
  253.         os.pullEvent(key)
  254.         get_fuel()
  255.     end
  256. end
  257.  
  258. -- Dump items to chest.
  259. function dump_items()
  260.    
  261.     local i = 1
  262.    
  263.     while i < 16 do
  264.         turtle.select(i)
  265.         turtle.drop()
  266.         i = i + 1
  267.     end
  268.    
  269.     if not(turtle.getItemSpace(15) == 64 and turtle.getItemSpace(14) == 64 and turtle.getItemSpace(13) == 64) then
  270.         print("After check chests, hit any key.")
  271.         os.pullEvent(key)
  272.         dump_items()
  273.     end
  274.    
  275. end
  276.  
  277. -- error trap of empty fuel or full items.
  278. function error_trap()
  279.     if (x == 0 and y ==0 and z == 0) then
  280.         print("After check chests, hit any key.")
  281.         os.pullEvent(key)
  282.     else
  283.         back_home()
  284.     end
  285.     dump_items()
  286.     get_fuel()
  287.    
  288.     go_digpos()
  289. end
  290.  
  291. -- refuel from inventory
  292. function refuel_inv()
  293.     turtle.select(fuel_inventory)
  294.     while turtle.getFuelLevel() < refuel_level do
  295.         if turtle.refuel(1) == false then
  296.             error_trap()
  297.         end
  298.     end
  299.     turtle.select(1)
  300. end
  301.  
  302. -- Check inventry space.
  303. function check_inv()
  304.     if not(turtle.getItemSpace(15) == 64 and turtle.getItemSpace(14) == 64 and turtle.getItemSpace(13) == 64) then
  305.         error_trap()
  306.     end
  307.     turtle.select(1)
  308. end
  309.  
  310. ------------------------------------
  311. -- main program
  312. ------------------------------------
  313.  
  314. cmdline_check()
  315.  
  316. while z <= tgt_z do
  317.  
  318.     while y <= tgt_y do
  319.        
  320.         if face == 0 then
  321.             while x < tgt_x do
  322.                 refuel_inv()
  323.                 check_inv()
  324.                
  325.                 turtle.dig()
  326.                 my_forword()
  327.                 x = x+1
  328.                 dx=dx+1
  329.             end
  330.         end
  331.        
  332.         if y == tgt_y then
  333.             break
  334.         end
  335.        
  336.         if face == 0 then
  337.             turtle.turnLeft()
  338.             face=3
  339.             dface=3
  340.            
  341.             turtle.dig()
  342.             my_forword()
  343.             y = y + 1
  344.             dy = dy + 1
  345.            
  346.             turtle.turnLeft()
  347.             face=2
  348.             dface=2
  349.         end
  350.        
  351.         if face == 2 then
  352.             while x > 1 do
  353.                 refuel_inv()
  354.                 check_inv()
  355.                
  356.                 turtle.dig()
  357.                 my_forword()
  358.                 x = x - 1
  359.                 dx = dx - 1
  360.             end
  361.         end
  362.        
  363.         if y == tgt_y then
  364.             break
  365.         end
  366.        
  367.         if face == 2 then
  368.             turtle.turnRight()
  369.             face=3
  370.             dface=3
  371.            
  372.             turtle.dig()
  373.             my_forword()
  374.             y = y + 1
  375.             dy = dy + 1
  376.            
  377.             turtle.turnRight()
  378.             face=0
  379.             dface=0
  380.         end
  381.        
  382.     end
  383.    
  384.     if z == tgt_z then
  385.         break
  386.     end
  387.    
  388.     turtle.digDown()
  389.     my_down()
  390.     z=z+1
  391.     dz=dz+1
  392.    
  393.     turtle.turnRight()
  394.     turtle.turnRight()
  395.    
  396.     if face == 0 then
  397.         face=2
  398.         dface=2
  399.     elseif face == 2 then
  400.         face=0
  401.         dface=0
  402.     end
  403.    
  404.     while y >= 0 do
  405.        
  406.         if face == 0 then
  407.             while x < tgt_x do
  408.                 refuel_inv()
  409.                 check_inv()
  410.                
  411.                 turtle.dig()
  412.                 my_forword()
  413.                 x = x + 1
  414.                 dx = dx + 1
  415.             end
  416.         end
  417.        
  418.         if y == 0 then
  419.             break
  420.         end
  421.        
  422.         if face == 0 then
  423.             turtle.turnRight()
  424.             face=1
  425.             dface=1
  426.            
  427.             turtle.dig()
  428.             my_forword()
  429.             y = y - 1
  430.             dy = dy - 1
  431.            
  432.             turtle.turnRight()
  433.             face=2
  434.             dface=2
  435.         end
  436.        
  437.         if face == 2 then
  438.             while x > 1 do
  439.                 refuel_inv()
  440.                 check_inv()
  441.                
  442.                 turtle.dig()
  443.                 my_forword()
  444.                 x = x-1
  445.                 dx = dx - 1
  446.             end
  447.         end
  448.        
  449.         if y == 0 then
  450.             break
  451.         end
  452.        
  453.         if face == 2 then
  454.             turtle.turnLeft()
  455.             face=1
  456.             dface=1
  457.            
  458.             turtle.dig()
  459.             my_forword()
  460.             y = y - 1
  461.             dy = dy - 1
  462.            
  463.             turtle.turnLeft()
  464.             face=0
  465.             dface=0
  466.         end
  467.        
  468.     end
  469.    
  470.     if z == tgt_z then
  471.         break
  472.     end
  473.    
  474.     turtle.digDown()
  475.     my_down()
  476.     z = z + 1
  477.     dz = dz + 1
  478.    
  479.     turtle.turnRight()
  480.     turtle.turnRight()
  481.    
  482.     if face == 0 then
  483.         face=2
  484.         dface=2
  485.     elseif face == 2 then
  486.         face=0
  487.         dface=0
  488.     end
  489.  
  490. end
  491.  
  492. back_home()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement