sanderronde

bot.lua

Jan 3rd, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.76 KB | None | 0 0
  1. local tArgs = { ... }
  2. local rows = nil
  3. local length = nil
  4. if #tArgs < 1 then
  5.     print( "Usage: farm [number:rows] [number:length]" )
  6.     return
  7. else
  8.     rows = tonumber(tArgs[1])
  9.     length = tonumber(tArgs[2])
  10. end
  11.  
  12.  
  13.  
  14. local crafted = 0
  15.  
  16. local function get_used_slots()
  17.     local used = 0
  18.     for s = 1,15 do
  19.         if turtle.getItemCount(s) > 0 then
  20.             used = used + 1
  21.         end
  22.     end
  23.     return used
  24. end
  25.  
  26. local function get_items_in_slots(slots)
  27.     local items = 0
  28.     for s = 1, slots do
  29.         items = items + turtle.getItemCount(s)
  30.     end
  31.     return items
  32. end
  33.  
  34. local function dump_bread()
  35.     turtle.turnRight()
  36.     turtle.turnRight()
  37.     turtle.select(16)
  38.     crafted = crafted + turtle.getItemCount(16)
  39.     turtle.drop(64)
  40.     turtle.turnLeft()
  41.     turtle.turnLeft()
  42. end
  43.  
  44. local function do_crafting()
  45.     -- Select slot 16 for the destination
  46.     turtle.select(16)
  47.     -- Should only have bread-stuff now, craft
  48.     turtle.craft()
  49.  
  50.     -- Will now have bread in slot 16
  51.     -- move the bread into the destination chest
  52.     dump_bread()
  53. end
  54.  
  55. local function selected_is_wheat()
  56.     if turtle.getItemCount() == 0 then
  57.         return false
  58.     end
  59.     if turtle.getItemDetail().name == "minecraft:wheat" then
  60.         return true
  61.     else
  62.         return false
  63.     end
  64. end
  65.  
  66. local function selected_is_seeds()
  67.     if turtle.getItemCount() == 0 then
  68.         return false
  69.     end
  70.     if turtle.getItemDetail().name == "minecraft:wheat_seeds" then
  71.         return true
  72.     else
  73.         return false
  74.     end
  75. end
  76.  
  77. local function check_fuel(required)
  78.     -- Always assumes the turtle is currently at the
  79.     -- bread creation place
  80.  
  81.     if turtle.getFuelLevel() == 'unlimited' or turtle.getFuelLevel() > required + 1 then
  82.         return
  83.     end
  84.  
  85.     -- Not enough fuel
  86.     -- Try to refuel through inventory
  87.     turtle.select(16)
  88.     if turtle.refuel() then
  89.         return
  90.     end
  91.  
  92.     -- Turn right and check the chest for fuel
  93.     turtle.turnRight()
  94.  
  95.     -- We need an additional action now (turn left)
  96.     -- so increment required
  97.     required = required + 1
  98.    
  99.     empty_spots = {}
  100.  
  101.     -- Suck it all up and call refuel on every item
  102.     for s = 1, 16 do
  103.         if turtle.getItemCount(s) == 0 then
  104.             empty_spots[s] = 1
  105.             turtle.suck()
  106.             turtle.refuel()
  107.             if turtle.getFuelLevel() > required + 1 then
  108.                 return
  109.             end
  110.         else
  111.             empty_spots[s] = 0
  112.         end
  113.     end
  114.     for s = 1, 16 do
  115.         if empty_spots[s] == 1 then
  116.             turtle.dump()
  117.         end
  118.     end
  119.  
  120.     -- Ask for fuel
  121.     turtle.select(16)
  122.     print("Need fuel!")
  123.  
  124.     -- Will be reduced by 3 at the destination
  125.     redstone.setAnalogOutput("bottom", 14)
  126.     while not turtle.refuel() or turtle.getFuelLevel() < required + 1 do
  127.         os.pullEvent( "turtle_inventory")
  128.     end
  129.     redstone.setOutput("bottom", false)
  130.     print("Got fuel!")
  131.  
  132.     turtle.turnLeft()
  133. end
  134.  
  135. local function dump_drops()
  136.     turtle.turnRight()
  137.     for s = 1, 15 do
  138.         turtle.select(s)
  139.         turtle.drop(64)
  140.     end
  141.     turtle.turnLeft()
  142. end
  143.  
  144. local function dump_fuel(slot)
  145.     if not slot then
  146.         slot = 16
  147.     end
  148.     turtle.turnLeft()
  149.     turtle.select(slot)
  150.     turtle.drop()
  151.     turtle.turnRight()
  152. end
  153.  
  154.  
  155. -- Returns 0 if it got wheat,
  156. -- 1 if it got seeds and
  157. -- 2 if it got fuel or nothing
  158. local function get_wheat_stack(slot)
  159.     turtle.select(slot)
  160.     turtle.suck(64)
  161.     if turtle.getItemCount() == 0 then
  162.         -- Picked up nothing, end
  163.         return 2
  164.     end
  165.     if selected_is_seeds() == true then
  166.         -- Drop seeds off at the seed box
  167.         turtle.turnLeft()
  168.         turtle.turnLeft()
  169.         turtle.drop(64)
  170.         turtle.turnRight()
  171.         turtle.turnRight()
  172.         return 1
  173.     end
  174.     if selected_is_wheat() == false then
  175.         -- Just picked up fuel,
  176.         -- drop it off again
  177.         dump_fuel(s)
  178.         return 2
  179.     end
  180.     return 0
  181. end
  182.  
  183. local function get_wheat()
  184.     turtle.turnRight()
  185.     for s = 1, 3 do
  186.         while get_wheat_stack(s) == 1 do
  187.             -- Repeat this to remove all seeds from
  188.             -- storage
  189.         end
  190.     end
  191.     turtle.turnLeft()
  192. end
  193.  
  194. local function balance(used_slots, items_per_slot)
  195.     if used_slots == 1 then
  196.         -- Just move one third at a time from the
  197.         -- first slot to the second and third
  198.         turtle.select(1)
  199.         turtle.transferTo(2, items_per_slot)
  200.         turtle.transferTo(3, items_per_slot)
  201.     else
  202.         -- Get the amount the first slot has to "lose"
  203.         -- then check howmuch we can transfer to slot
  204.         -- 2 and then transfer the rest to slot 3
  205.         local first_too_many = turtle.getItemCount(1) - items_per_slot
  206.         local second_needed = items_per_slot - turtle.getItemCount(2)
  207.         if second_needed < 0 then
  208.             -- Must get rid of some items,
  209.             -- this means we just have to dump the first's items in
  210.             -- the third slot
  211.             turtle.select(1)
  212.             turtle.transferTo(3, first_too_many)
  213.             -- as well as the second's
  214.             turtle.select(2)
  215.             turtle.transferTo(3, -second_needed)
  216.         else
  217.             -- Transfer what the second one needs to
  218.             -- the second one and the rest to the third
  219.             turtle.select(1)
  220.             turtle.transferTo(2, second_needed)
  221.             -- as well as the second's
  222.             turtle.select(1)
  223.             turtle.transferTo(3, first_too_many - second_needed)
  224.         end
  225.     end
  226. end
  227.  
  228. local function suck_all()
  229.     for s = 1, 15 do
  230.         turtle.select(s)
  231.         turtle.suck()
  232.     end
  233. end
  234.  
  235. local function get_fuel()
  236.     turtle.turnLeft()
  237.    
  238.     for s = 1, 15 do
  239.         turtle.select(s)
  240.         turtle.suck()
  241.         if not selected_is_wheat() and not selected_is_seeds() then
  242.             -- Picked up fuel
  243.             turtle.transferTo(16, 64)
  244.             break
  245.         end
  246.     end
  247.  
  248.     -- Dump it all again
  249.     for s = 1, 15 do
  250.         turtle.select(s)
  251.         if turtle.getItemCount() > 0 then
  252.             turtle.drop(64)
  253.         end
  254.     end
  255.  
  256.     turtle.turnRight()
  257. end
  258.  
  259. local function make_bread()
  260.     sleep(120)
  261.     return
  262.  
  263.  
  264.     -- dump_fuel()
  265.  
  266.     -- -- Suck it all up
  267.     -- suck_all()
  268.  
  269.     -- -- If we picked up nothing, just end
  270.     -- if get_used_slots() == 0 then
  271.     --  get_fuel()
  272.     --  return
  273.     -- end
  274.     -- print("Making bread")
  275.  
  276.     -- -- Dump all wheat for now
  277.     -- dump_drops()
  278.  
  279.  
  280.     -- local used_slots = get_used_slots()
  281.     -- local items = get_items_in_slots(used_slots)
  282.  
  283.     -- while true do
  284.     --  -- Pick up a max of 3 stacks from the chest
  285.     --  get_wheat()
  286.  
  287.     --  local used_slots = get_used_slots()
  288.     --  local items = get_items_in_slots(used_slots)
  289.  
  290.  
  291.     --  if items == 0 then
  292.     --      break
  293.     --  elseif items < 3 then
  294.     --      -- Spit it out
  295.     --      turtle.select(1)
  296.     --      turtle.drop(64)
  297.     --  end
  298.  
  299.     --  local remaining_items = items % 3
  300.     --  if remaining_items > 0 then
  301.     --      -- Got some items left, dump those in the chest
  302.     --      turtle.select(used_slots)
  303.     --      turtle.turnRight()
  304.     --      turtle.drop(items % 3)
  305.     --      turtle.turnLeft()
  306.     --      items = get_items_in_slots(used_slots)
  307.     --  end
  308.        
  309.     --  local items_per_slot = items / 3
  310.     --  balance(used_slots, items_per_slot)
  311.  
  312.     --  do_crafting()
  313.     -- end
  314.  
  315.     -- get_fuel()
  316.  
  317.     -- print("Made a total of ", crafted, " breads")
  318.     -- print("Going back to sleep")
  319. end
  320.  
  321. local function faces_chest()
  322.     local success, data = turtle.inspect()
  323.     if not success then
  324.         return false
  325.     end
  326.     if data.name == "minecraft:chest" or data.name == "IronChest:BlockIronChest" then
  327.         return true
  328.     else
  329.         return false
  330.     end
  331. end
  332.  
  333. local function is_at_start()
  334.     turtle.turnLeft()
  335.     if not faces_chest() then
  336.         turtle.turnRight()
  337.         return false
  338.     end
  339.    
  340.     turtle.turnLeft()
  341.     if not faces_chest() then
  342.         turtle.turnRight()
  343.         turtle.turnRight()
  344.         return false
  345.     end
  346.  
  347.     turtle.turnLeft()
  348.     if not faces_chest() then
  349.         turtle.turnRight()
  350.         turtle.turnRight()
  351.         turtle.turnRight()
  352.         return false
  353.     end
  354.  
  355.  
  356.     turtle.turnLeft()
  357.     return true
  358. end
  359.  
  360.  
  361. local function move(dir, amount)
  362.     if dir == "fw" then
  363.         for t = 1, amount do
  364.             turtle.forward()
  365.         end
  366.     elseif dir == "bw" then
  367.         for t = 1, amount do
  368.             turtle.back()
  369.         end
  370.     elseif dir == "up" then
  371.         for t = 1, amount do
  372.             turtle.up()
  373.         end
  374.     elseif dir == "down" then
  375.         for t = 1, amount do
  376.             turtle.down()
  377.         end
  378.     end
  379. end
  380.  
  381. local function initial_farm_path()
  382.     -- check_fuel(12)
  383.  
  384.     move("fw", 1)
  385.     turtle.turnLeft()
  386.     move("fw", 6)
  387.     move("up", 2)
  388.     turtle.turnLeft()
  389. end
  390.  
  391. local row_offset = 0
  392.  
  393. local function find_seeds()
  394.     for s = 1, 15 do
  395.         turtle.select(s)
  396.         if selected_is_seeds() then
  397.             return true
  398.         end
  399.     end
  400.     return false
  401. end
  402.  
  403. local function place_seed()
  404.     if not find_seeds() then
  405.         return false
  406.     end
  407.     turtle.digDown()
  408.     turtle.placeDown()
  409.     return true
  410. end
  411.  
  412. local function return_from_row(len_traveled)
  413.     turtle.turnLeft()
  414.     turtle.turnLeft()
  415.  
  416.     for t = 0, len_traveled do
  417.         move("down", 1)
  418.         if t == len_traveled then
  419.             move("fw", 8)
  420.         else
  421.             move("fw", 7)
  422.         end
  423.     end
  424.    
  425.     turtle.turnLeft()
  426.     turtle.turnLeft()
  427. end
  428.  
  429. local function farm_single_row()
  430.     -- check_fuel((length * 16) + 1)
  431.    
  432.     local len_traveled = 0
  433.  
  434.     local do_cancel = false
  435.     for l = 1, length do
  436.         local local_len = 7
  437.         if l == 1 then
  438.             -- First row is 1 longer
  439.             local_len = 8
  440.         else
  441.             move("up", 1)
  442.         end
  443.  
  444.         if do_cancel then
  445.             break
  446.         end
  447.  
  448.         len_traveled = len_traveled + 1
  449.  
  450.         for f = 1, local_len do
  451.             move("fw", 1)
  452.             if not place_seed() then
  453.                 -- No more seeds, cancel after this row
  454.                 do_cancel = true
  455.             end
  456.         end
  457.     end
  458.  
  459.     return_from_row(len_traveled)
  460. end
  461.  
  462. local function farm_double_row()
  463.     -- check_fuel((length * 23) + 1)
  464.    
  465.     local len_traveled = 0
  466.  
  467.     local do_cancel = false
  468.     for l = 1, length do
  469.         local local_len = 7
  470.         if l == 1 then
  471.             -- First row is 1 longer
  472.             local_len = 8
  473.         else
  474.             move("up", 1)
  475.         end
  476.  
  477.         if do_cancel then
  478.             break
  479.         end
  480.  
  481.         len_traveled = len_traveled + 1
  482.  
  483.         for f = 1, local_len / 2 do
  484.             move("fw", 1)
  485.             if not place_seed() then
  486.                 -- No more seeds, cancel after this row
  487.                 do_cancel = true
  488.             end
  489.             turtle.turnRight()
  490.             move("fw", 1)
  491.             if not place_seed() then
  492.                 -- No more seeds, cancel after this row
  493.                 do_cancel = true
  494.             end
  495.             turtle.turnLeft()
  496.             move("fw", 1)
  497.             if not place_seed() then
  498.                 -- No more seeds, cancel after this row
  499.                 do_cancel = true
  500.             end
  501.             turtle.turnLeft()
  502.             move("fw", 1)
  503.             if not place_seed() then
  504.                 -- No more seeds, cancel after this row
  505.                 do_cancel = true
  506.             end
  507.             turtle.turnRight()
  508.         end
  509.         if local_len % 2 > 0 then
  510.             move("fw", 1)
  511.             if not place_seed() then
  512.                 -- No more seeds, cancel after this row
  513.                 do_cancel = true
  514.             end
  515.             turtle.turnRight()
  516.             move("fw", 1)
  517.             if not place_seed() then
  518.                 -- No more seeds, cancel after this row
  519.                 do_cancel = true
  520.             end
  521.             turtle.turnLeft()
  522.             turtle.turnLeft()
  523.             move("fw", 1)
  524.             turtle.turnRight()
  525.         end
  526.     end
  527.  
  528.     return_from_row(len_traveled)
  529. end
  530.  
  531. local function go_home()
  532.     -- check_fuel(row_offset + 12)
  533.  
  534.     turtle.turnLeft()
  535.     move("fw", row_offset)
  536.     move("fw", 4)
  537.     move("down", 4)
  538.     move("fw", 2)
  539.     turtle.turnRight()
  540.     move("fw", 1)
  541.     turtle.turnLeft()
  542.     turtle.turnLeft()
  543. end
  544.  
  545. local function dump_excess()
  546.     check_fuel(6)
  547.  
  548.     move("up", 1)
  549.     turtle.turnRight()
  550.     move("fw", 2)
  551.     for s = 1, 15 do
  552.         turtle.select(s)
  553.         turtle.drop(64)
  554.     end
  555.  
  556.     turtle.turnLeft()
  557.     turtle.turnLeft()
  558.     move("fw", 2)
  559.     turtle.turnRight()
  560.     move("down", 1)
  561. end
  562.  
  563. local function next_row(doubled)
  564.     local amount = nil
  565.     if doubled then
  566.         amount = 3
  567.     else
  568.         amount = 2
  569.     end
  570.  
  571.     -- check_fuel(amount)
  572.  
  573.     row_offset = row_offset + amount
  574.     turtle.turnRight()
  575.     move("fw", amount)
  576.     turtle.turnLeft()
  577. end
  578.  
  579. local function pick_up_seeds()
  580.     turtle.turnLeft()
  581.     for s = 1, 15 do
  582.         turtle.select(s)
  583.         turtle.suck(64)
  584.     end
  585.     turtle.turnRight()
  586. end
  587.  
  588. local function farm()
  589.     sleep(120)
  590.     return
  591.  
  592.     -- if rows < 3 then
  593.     --  check_fuel(12 + (((length * 16) + 1) * rows) + ((rows - 1) * 4) + 12)
  594.     -- else
  595.     --  check_fuel(12 + 12 + 4 + 4 + ((((length * 23) + 1) + 3 + 3) * (rows - 2)) +
  596.     --      (((length * 16) + 1) * 2) + (8))
  597.     -- end
  598.  
  599.     -- pick_up_seeds()
  600.     -- initial_farm_path()
  601.  
  602.     -- if rows < 1 then
  603.     --  print("Amount of rows is set to lower than 1")
  604.     --  return
  605.     -- end
  606.  
  607.     -- farm_single_row()
  608.     -- for r = 0, rows - 2 do
  609.     --  if r == 0 then
  610.     --      next_row()
  611.     --  else
  612.     --      next_row(true)
  613.     --  end
  614.     --  farm_double_row()
  615.     -- end
  616.     -- if rows > 1 then
  617.     --  if rows > 2 then
  618.     --      next_row(true)
  619.     --  else
  620.     --      next_row()
  621.     --  end
  622.     --  farm_single_row()
  623.     -- end
  624.  
  625.     -- go_home()
  626.     -- dump_excess()
  627. end
  628.  
  629. local function signal_task_start()
  630.     redstone.setAnalogOutput("bottom", 13)
  631. end
  632.  
  633. local function signal_task_end()
  634.     redstone.setAnalogOutput("bottom", 12)
  635. end
  636.  
  637. local function listen()
  638.     while true do
  639.         os.pullEvent("redstone")
  640.         if redstone.getAnalogInput("top") > 0 then
  641.             -- Go to the data source
  642.             check_fuel(2)
  643.             move("fw", 1)
  644.  
  645.             local val = redstone.getAnalogInput("right")
  646.             move("bw", 1)
  647.  
  648.             if val == 5 then
  649.                 signal_task_start()
  650.                 make_bread()
  651.                 signal_task_end()
  652.             elseif val == 6 then
  653.                 signal_task_start()
  654.                 print("Replanting farm")
  655.                 farm()
  656.                 signal_task_end()
  657.             end
  658.         end
  659.     end
  660. end
  661.  
  662. local function init()
  663.     -- Check fuel first
  664.     check_fuel(1)
  665.     print("Fuel remaining: ", turtle.getFuelLevel())
  666.  
  667.     -- Check its position
  668.     if not is_at_start() then
  669.         print("Turtle is not at start position!")
  670.     end
  671.  
  672.     redstone.setAnalogOutput("bottom", 11)
  673.     sleep(10)
  674.     redstone.setAnalogOutput("bottom", 0)
  675.  
  676.     listen()
  677. end
  678.  
  679. init()
Add Comment
Please, Sign In to add comment