Advertisement
sanderronde

bake_bread.lua

Jan 3rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.96 KB | None | 0 0
  1. local crafted = 0
  2.  
  3. local function get_used_slots()
  4.     local used = 0
  5.     for s = 1,15 do
  6.         if turtle.getItemCount(s) > 0 then
  7.             used = used + 1
  8.         end
  9.     end
  10.     return used
  11. end
  12.  
  13. local function get_items_in_slots(slots)
  14.     local items = 0
  15.     for s = 1, slots do
  16.         items = items + turtle.getItemCount(s)
  17.     end
  18.     return items
  19. end
  20.  
  21. local function dump_bread()
  22.     turtle.turnRight()
  23.     turtle.turnRight()
  24.     turtle.select(16)
  25.     crafted = crafted + turtle.getItemCount(16)
  26.     turtle.drop(64)
  27.     turtle.turnLeft()
  28.     turtle.turnLeft()
  29. end
  30.  
  31. local function do_crafting()
  32.     -- Select slot 16 for the destination
  33.     turtle.select(16)
  34.     -- Should only have bread-stuff now, craft
  35.     turtle.craft()
  36.  
  37.     -- Will now have bread in slot 16
  38.     -- move the bread into the destination chest
  39.     dump_bread()
  40. end
  41.  
  42. local function selected_is_wheat()
  43.     if turtle.getItemCount() == 0 then
  44.         return false
  45.     end
  46.     if turtle.getItemDetail().name == "minecraft:wheat" then
  47.         return true
  48.     else
  49.         return false
  50.     end
  51. end
  52.  
  53. local function selected_is_seeds()
  54.     if turtle.getItemCount() == 0 then
  55.         return false
  56.     end
  57.     if turtle.getItemDetail().name == "minecraft:wheat_seeds" then
  58.         return true
  59.     else
  60.         return false
  61.     end
  62. end
  63.  
  64. local function check_fuel(required)
  65.     -- Always assumes the turtle is currently at the
  66.     -- bread creation place
  67.  
  68.     if turtle.getFuelLevel() == 'unlimited' or turtle.getFuelLevel() > required + 1 then
  69.         return
  70.     end
  71.  
  72.     -- Not enough fuel
  73.     -- Try to refuel through inventory
  74.     turtle.select(16)
  75.     if turtle.refuel() then
  76.         return
  77.     end
  78.  
  79.     -- Turn right and check the chest for fuel
  80.     turtle.turnRight()
  81.  
  82.     -- We need an additional action now (turn left)
  83.     -- so increment required
  84.     required = required + 1
  85.    
  86.     empty_spots = {}
  87.  
  88.     -- Suck it all up and call refuel on every item
  89.     for s = 1, 16 do
  90.         if turtle.getItemCount(s) == 0 then
  91.             empty_spots[s] = 1
  92.             turtle.suck()
  93.             turtle.refuel()
  94.             if turtle.getFuelLevel() > required + 1 then
  95.                 return
  96.             end
  97.         else
  98.             empty_spots[s] = 0
  99.         end
  100.     end
  101.     for s = 1, 16 do
  102.         if empty_spots[s] == 1 then
  103.             turtle.dump()
  104.         end
  105.     end
  106.  
  107.     -- Ask for fuel
  108.     turtle.select(16)
  109.     print("Need fuel!")
  110.  
  111.     -- Will be reduced by 3 at the destination
  112.     redstone.setAnalogOutput("bottom", 14)
  113.     while not turtle.refuel() or turtle.getFuelLevel() < required + 1 do
  114.         os.pullEvent( "turtle_inventory")
  115.     end
  116.     redstone.setOutput("bottom", false)
  117.     print("Got fuel!")
  118.  
  119.     turtle.turnLeft()
  120. end
  121.  
  122. local function dump_drops()
  123.     turtle.turnRight()
  124.     for s = 1, 15 do
  125.         turtle.select(s)
  126.         turtle.drop(64)
  127.     end
  128.     turtle.turnLeft()
  129. end
  130.  
  131. local function dump_fuel(slot)
  132.     if not slot then
  133.         slot = 16
  134.     end
  135.     turtle.turnLeft()
  136.     turtle.select(slot)
  137.     turtle.drop()
  138.     turtle.turnRight()
  139. end
  140.  
  141.  
  142. -- Returns 0 if it got wheat,
  143. -- 1 if it got seeds and
  144. -- 2 if it got fuel or nothing
  145. local function get_wheat_stack(slot)
  146.     turtle.select(slot)
  147.     turtle.suck(64)
  148.     if turtle.getItemCount() == 0 then
  149.         -- Picked up nothing, end
  150.         return 2
  151.     end
  152.     if selected_is_seeds() == true then
  153.         -- Drop seeds off at the seed box
  154.         turtle.turnLeft()
  155.         turtle.turnLeft()
  156.         turtle.drop(64)
  157.         turtle.turnRight()
  158.         turtle.turnRight()
  159.         return 1
  160.     end
  161.     if selected_is_wheat() == false then
  162.         -- Just picked up fuel,
  163.         -- drop it off again
  164.         dump_fuel(s)
  165.         return 2
  166.     end
  167.     return 0
  168. end
  169.  
  170. local function get_wheat()
  171.     turtle.turnRight()
  172.     for s = 1, 3 do
  173.         while get_wheat_stack(s) == 1 do
  174.             -- Repeat this to remove all seeds from
  175.             -- storage
  176.         end
  177.     end
  178.     turtle.turnLeft()
  179. end
  180.  
  181. local function balance(used_slots, items_per_slot)
  182.     if used_slots == 1 then
  183.         -- Just move one third at a time from the
  184.         -- first slot to the second and third
  185.         turtle.select(1)
  186.         turtle.transferTo(2, items_per_slot)
  187.         turtle.transferTo(3, items_per_slot)
  188.     else
  189.         -- Get the amount the first slot has to "lose"
  190.         -- then check howmuch we can transfer to slot
  191.         -- 2 and then transfer the rest to slot 3
  192.         local first_too_many = turtle.getItemCount(1) - items_per_slot
  193.         local second_needed = items_per_slot - turtle.getItemCount(2)
  194.         if second_needed < 0 then
  195.             -- Must get rid of some items,
  196.             -- this means we just have to dump the first's items in
  197.             -- the third slot
  198.             turtle.select(1)
  199.             turtle.transferTo(3, first_too_many)
  200.             -- as well as the second's
  201.             turtle.select(2)
  202.             turtle.transferTo(3, -second_needed)
  203.         else
  204.             -- Transfer what the second one needs to
  205.             -- the second one and the rest to the third
  206.             turtle.select(1)
  207.             turtle.transferTo(2, second_needed)
  208.             -- as well as the second's
  209.             turtle.select(1)
  210.             turtle.transferTo(3, first_too_many - second_needed)
  211.         end
  212.     end
  213. end
  214.  
  215. local function suck_all()
  216.     for s = 1, 15 do
  217.         turtle.select(s)
  218.         turtle.suck()
  219.     end
  220. end
  221.  
  222. local function get_fuel()
  223.     turtle.turnLeft()
  224.    
  225.     for s = 1, 15 do
  226.         turtle.select(s)
  227.         turtle.suck()
  228.         if not selected_is_wheat() and not selected_is_seeds() then
  229.             -- Picked up fuel
  230.             turtle.transferTo(16, 64)
  231.             break
  232.         end
  233.     end
  234.  
  235.     -- Dump it all again
  236.     for s = 1, 15 do
  237.         turtle.select(s)
  238.         if turtle.getItemCount() > 0 then
  239.             turtle.drop(64)
  240.         end
  241.     end
  242.  
  243.     turtle.turnRight()
  244. end
  245.  
  246. local function make_bread()
  247.     dump_fuel()
  248.  
  249.     -- Suck it all up
  250.     suck_all()
  251.  
  252.     -- If we picked up nothing, just end
  253.     if get_used_slots() == 0 then
  254.         get_fuel()
  255.         return
  256.     end
  257.     print("Making bread")
  258.  
  259.     -- Dump all wheat for now
  260.     dump_drops()
  261.  
  262.  
  263.     local used_slots = get_used_slots()
  264.     local items = get_items_in_slots(used_slots)
  265.  
  266.     while true do
  267.         -- Pick up a max of 3 stacks from the chest
  268.         get_wheat()
  269.  
  270.         local used_slots = get_used_slots()
  271.         local items = get_items_in_slots(used_slots)
  272.  
  273.  
  274.         if items == 0 then
  275.             break
  276.         elseif items < 3 then
  277.             -- Spit it out
  278.             turtle.select(1)
  279.             turtle.drop(64)
  280.         end
  281.  
  282.         local remaining_items = items % 3
  283.         if remaining_items > 0 then
  284.             -- Got some items left, dump those in the chest
  285.             turtle.select(used_slots)
  286.             turtle.turnRight()
  287.             turtle.drop(items % 3)
  288.             turtle.turnLeft()
  289.             items = get_items_in_slots(used_slots)
  290.         end
  291.        
  292.         local items_per_slot = items / 3
  293.         balance(used_slots, items_per_slot)
  294.  
  295.         do_crafting()
  296.     end
  297.  
  298.     get_fuel()
  299.  
  300.     print("Made a total of ", crafted, " breads")
  301.     print("Going back to sleep")
  302. end
  303.  
  304. local function faces_chest()
  305.     local success, data = turtle.inspect()
  306.     if not success then
  307.         return false
  308.     end
  309.     if data.name == "minecraft:chest" or data.name == "IronChest:BlockIronChest" then
  310.         return true
  311.     else
  312.         return false
  313.     end
  314. end
  315.  
  316. local function is_at_start()
  317.     turtle.turnLeft()
  318.     if not faces_chest() then
  319.         turtle.turnRight()
  320.         return false
  321.     end
  322.    
  323.     turtle.turnLeft()
  324.     if not faces_chest() then
  325.         turtle.turnRight()
  326.         turtle.turnRight()
  327.         return false
  328.     end
  329.  
  330.     turtle.turnLeft()
  331.     if not faces_chest() then
  332.         turtle.turnRight()
  333.         turtle.turnRight()
  334.         turtle.turnRight()
  335.         return false
  336.     end
  337.  
  338.  
  339.     turtle.turnLeft()
  340.     return true
  341. end
  342.  
  343. local function init()
  344.     -- Check fuel first
  345.     check_fuel(1)
  346.     print("Fuel remaining: ", turtle.getFuelLevel())
  347.     make_bread()
  348. end
  349.  
  350. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement