Advertisement
Link712011

Quick bread

Apr 17th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.56 KB | None | 0 0
  1. --[[
  2.  
  3. Farm patern:
  4.  
  5.                 chest
  6.  
  7.         seeds   turtle  seeds
  8.  
  9.         dirt    seeds   dirt
  10.  
  11.                 dirt
  12. --]]
  13.  
  14. function select_next_item(slot, item)
  15.     local data
  16.    
  17.     while slot <= 16 do
  18.         data = turtle.getItemDetail(slot)
  19.         if data and data.name == item then
  20.             turtle.select(slot)
  21.             return true
  22.         end
  23.         slot = slot + 1
  24.     end
  25.     return false
  26. end
  27. function repack_item(item)
  28.     local slot = 1
  29.     local data
  30.    
  31.     while slot < 16 do
  32.         data = turtle.getItemDetail(slot)
  33.         if not data or (data.name == item and data.count < 64) then
  34.             if select_next_item(slot + 1, item) then
  35.                 turtle.transferTo(slot)
  36.                 data = turtle.getItemDetail(slot)
  37.             else
  38.                 return true
  39.             end
  40.         end
  41.         if data and ((data.name == item and data.count == 64) or data.name ~= item) then
  42.             slot = slot + 1
  43.         end
  44.     end
  45.     return true
  46. end
  47. function craft_bread()
  48.     local slot = turtle.getSelectedSlot()
  49.    
  50.     print("Repacking seeds...")
  51.     repack_item("minecraft:wheat_seeds")
  52.     print("Cleaning seeds...")
  53.     clean_seeds()
  54.     print("Repacking wheat...")
  55.     repack_item("minecraft:wheat")
  56.     if turtle.craft() then
  57.         while select_item("minecraft:bread") do
  58.             turtle.dropUp()
  59.         end
  60.     end
  61.     turtle.select(slot)
  62. end
  63. function clear_slot(slot)
  64.     local i = turtle.getSelectedSlot()
  65.  
  66.     if turtle.getItemCount(slot) > 0 then
  67.         turtle.select(slot)
  68.         turtle.drop()
  69.         turtle.select(i)
  70.     end
  71. end
  72. function clean_seeds()
  73.     local slot = 1
  74.     local first = 0
  75.     local data
  76.    
  77.     while slot <= 16 do
  78.         data = turtle.getItemDetail(slot)
  79.         if data and data.name == "minecraft:wheat_seeds" then
  80.             if first == 0 then
  81.                 first = slot
  82.             else
  83.                 turtle.select(slot)
  84.                 turtle.drop()
  85.             end
  86.         end
  87.         slot = slot + 1
  88.     end
  89.     if first >= 1 and first <= 16 then
  90.         clear_slot(16)
  91.         turtle.select(first)
  92.         turtle.transferTo(16)
  93.     end
  94. end
  95. function select_item(item)
  96.     local slot = 1
  97.    
  98.     while slot <= 16 do
  99.         local data = turtle.getItemDetail(slot)
  100.         if data and data.name == item then
  101.             turtle.select(slot)
  102.             return true
  103.         end
  104.         slot = slot + 1
  105.     end
  106.     return false
  107. end
  108. function ble_mature()
  109.     local success, data = turtle.inspectDown()
  110.    
  111.     if success and data.name == "minecraft:wheat" and data.metadata == 7 then
  112.         return true
  113.     else
  114.         return false
  115.     end
  116. end
  117. function select_or_wait_seeds()
  118.     while not select_item("minecraft:wheat_seeds") do
  119.         print("Waiting seeds... retrying in 10 seconds")
  120.         sleep(10)
  121.     end
  122. end
  123. function harvest_bottom()
  124.     if ble_mature() then
  125.         turtle.digDown()
  126.     end
  127.     if select_item("minecraft:wheat_seeds") then
  128.         turtle.placeDown()
  129.     end
  130. end
  131. function harvest_front()
  132.     local success, data = turtle.inspect()
  133.    
  134.     if success and data.name == "minecraft:wheat" and data.metadata == 7 then
  135.         turtle.dig()
  136.     end
  137.     if select_item("minecraft:wheat_seeds") then
  138.         turtle.place()
  139.     end
  140. end
  141. function select_fuel()
  142.     local slot = 1
  143.     local data = turtle.getItemDetail(slot)
  144.     while slot <= 16 do
  145.         if data and (data.name == "minecraft:lava_bucket" or data.name == "minecraft:coal" or data.name == "minecraft:planks" or data.name == "minecraft:log") then
  146.             break
  147.         end
  148.         slot = slot + 1
  149.         if slot <= 16 then
  150.             turtle.select(slot)
  151.             data = turtle.getItemDetail(slot)
  152.         end
  153.     end
  154.     if (slot > 16) then
  155.         return false
  156.     end
  157.     turtle.select(slot)
  158.     return true
  159. end
  160. function check_fuel()
  161.     if turtle.getFuelLevel() < 10 then
  162.         select_fuel()
  163.         while turtle.refuel(1) == false do
  164.             print("Waiting fuel... retrying in 10 seconds")
  165.             sleep(10)
  166.             select_fuel()
  167.         end
  168.     end
  169. end
  170. function check_avance()
  171. local success = false
  172. check_fuel()
  173.     while success == false do
  174.         check_fuel()
  175.         success = turtle.forward()
  176.     end
  177. end
  178. function avance(i)
  179. local ii =0
  180.     while ii < i do
  181.         check_avance()
  182.         ii = ii + 1
  183.     end
  184. end
  185. function recule(i)
  186.     turtle.turnLeft()
  187.     turtle.turnLeft()
  188.     avance(i)
  189.     turtle.turnLeft()
  190.     turtle.turnLeft()
  191. end
  192. function get_coords()
  193.     local x, y, z = gps.locate(5)
  194.     if not x then
  195.         print("Failed to retreive coordonates. Retrying in 5 seconds")
  196.         sleep(5)
  197.         os.reboot()
  198.     else
  199.         return x, y, z
  200.     end
  201. end
  202. function set_direction(x, z)
  203.     local xn, yn, zn
  204.    
  205.     if turtle.detect() then
  206.         check_fuel()
  207.         while not turtle.back() do
  208.             check_fuel()
  209.         end
  210.         xn, yn, zn = get_coords()
  211.         if x > xn then turtle.turnRight()
  212.         elseif x < xn then turtle.turnLeft()
  213.         elseif z < zn then
  214.             turtle.turnLeft()
  215.             turtle.turnLeft()
  216.         end
  217.         return true
  218.     end
  219.     check_avance()
  220.     xn, yn, zn = get_coords()
  221.     if x < xn then turtle.turnRight()
  222.     elseif x > xn then turtle.turnLeft()
  223.     elseif z > zn then
  224.         turtle.turnLeft()
  225.         turtle.turnLeft()
  226.     end
  227.     return true
  228. end
  229. function absolute(nb)
  230.     if nb >= 0 then
  231.         return nb
  232.     end
  233.     return -nb
  234. end
  235. function goto_home(x, z)
  236.     local xmin, xmax, zmin, zmax = -291,-299,547,558 --change this values to corners of your fields
  237.    
  238.     recule(absolute(z) - absolute(zmin))
  239.     turtle.turnLeft()
  240.     avance(absolute(x) - absolute(xmin))
  241.     turtle.turnRight()
  242. end
  243. function drop_wheat_in_turtle()
  244.     if select_item("minecraft:wheat") then
  245.         turtle.turnLeft()
  246.         turtle.turnLeft()
  247.         turtle.dropUp()
  248.     end
  249. end
  250. function parse_field(x, z)
  251.     local xmin, xmax, zmin, zmax = -291,-299,547,558 --change this values to corners of your fields
  252.    
  253.     while absolute(x) <= absolute(xmax) do
  254.         harvest_bottom()
  255.         check_avance()
  256.         harvest_bottom()
  257.         z = z + 1
  258.         if z >= zmax then
  259.             recule(absolute(absolute(z) - absolute(zmin)))
  260.             turtle.turnRight()
  261.             check_avance()
  262.             turtle.turnLeft()
  263.             z = zmin
  264.             x = x - 1
  265.         end
  266.     end
  267.     goto_home(x, z)
  268. end
  269. function number_fuel()
  270.     local i, nb = 1, 0
  271.     local data = turtle.getItemDetail(i)
  272.    
  273.     while i <= 16 do
  274.         data = turtle.getItemDetail(i)
  275.         if data and (data.name == "minecraft:lava_bucket" or data.name == "minecraft:coal" or data.name == "minecraft:planks" or data.name == "minecraft:log") then
  276.             nb = nb + 1
  277.         end
  278.         i = i + 1
  279.     end
  280.     return nb
  281. end
  282. function select_empty()
  283.     local i = 1
  284.     local data = turtle.getItemDetail(i)
  285.    
  286.     while i <= 16 do
  287.         data = turtle.getItemDetail(i)
  288.         if not data then
  289.             turtle.select(i)
  290.             return true
  291.         end
  292.         i = i + 1
  293.     end
  294.     return false
  295. end
  296. function gather_fuel()
  297.     turtle.turnLeft()
  298.     select_empty()
  299.     while not turtle.suck() do
  300.         print("Waiting fuel in chest... Retrying in 20 secs")
  301.         sleep(20)
  302.     end
  303.     turtle.turnRight()
  304. end
  305.  
  306. while true do
  307.     harvest_front()
  308.     turtle.turnLeft()
  309.     harvest_front()
  310.     turtle.turnLeft()
  311.     harvest_front()
  312.     turtle.turnLeft()
  313.     harvest_front()
  314.     turtle.turnLeft()
  315.     harvest_bottom()
  316.     craft_bread()
  317.     print("Job finished. Waiting growing...")
  318.     sleep(150)
  319. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement