Advertisement
sanderronde

plant.lua

Jan 3rd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.13 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. local function check_fuel(required)
  13.     -- Always assumes the turtle is currently at the
  14.     -- bread creation place
  15.  
  16.     if turtle.getFuelLevel() == 'unlimited' or turtle.getFuelLevel() > required + 1 then
  17.         return
  18.     end
  19.  
  20.     -- Not enough fuel
  21.     -- Try to refuel through inventory
  22.     turtle.select(16)
  23.     if turtle.refuel() then
  24.         return
  25.     end
  26.  
  27.     -- Turn right and check the chest for fuel
  28.     turtle.turnRight()
  29.  
  30.     -- We need an additional action now (turn left)
  31.     -- so increment required
  32.     required = required + 1
  33.    
  34.     empty_spots = {}
  35.  
  36.     -- Suck it all up and call refuel on every item
  37.     for s = 1, 16 do
  38.         if turtle.getItemCount(s) == 0 then
  39.             empty_spots[s] = 1
  40.             turtle.suck()
  41.             turtle.refuel()
  42.             if turtle.getFuelLevel() > required + 1 then
  43.                 return
  44.             end
  45.         else
  46.             empty_spots[s] = 0
  47.         end
  48.     end
  49.     for s = 1, 16 do
  50.         if empty_spots[s] == 1 then
  51.             turtle.drop()
  52.         end
  53.     end
  54.  
  55.     -- Ask for fuel
  56.     turtle.select(16)
  57.     print("Need fuel!")
  58.  
  59.     -- Will be reduced by 3 at the destination
  60.     redstone.setAnalogOutput("bottom", 14)
  61.     while not turtle.refuel() or turtle.getFuelLevel() < required + 1 do
  62.         os.pullEvent( "turtle_inventory")
  63.     end
  64.     redstone.setOutput("bottom", false)
  65.     print("Got fuel!")
  66.  
  67.     turtle.turnLeft()
  68. end
  69.  
  70. local function faces_chest()
  71.     local success, data = turtle.inspect()
  72.     if not success then
  73.         return false
  74.     end
  75.     if data.name == "minecraft:chest" or data.name == "IronChest:BlockIronChest" then
  76.         return true
  77.     else
  78.         return false
  79.     end
  80. end
  81.  
  82. local function is_at_start()
  83.     turtle.turnLeft()
  84.     if not faces_chest() then
  85.         turtle.turnRight()
  86.         return false
  87.     end
  88.    
  89.     turtle.turnLeft()
  90.     if not faces_chest() then
  91.         turtle.turnRight()
  92.         turtle.turnRight()
  93.         return false
  94.     end
  95.  
  96.     turtle.turnLeft()
  97.     if not faces_chest() then
  98.         turtle.turnRight()
  99.         turtle.turnRight()
  100.         turtle.turnRight()
  101.         return false
  102.     end
  103.  
  104.  
  105.     turtle.turnLeft()
  106.     return true
  107. end
  108.  
  109.  
  110. local function move(dir, amount)
  111.     if dir == "fw" then
  112.         for t = 1, amount do
  113.             turtle.forward()
  114.         end
  115.     elseif dir == "bw" then
  116.         for t = 1, amount do
  117.             turtle.back()
  118.         end
  119.     elseif dir == "up" then
  120.         for t = 1, amount do
  121.             turtle.up()
  122.         end
  123.     elseif dir == "down" then
  124.         for t = 1, amount do
  125.             turtle.down()
  126.         end
  127.     end
  128. end
  129.  
  130. local function initial_farm_path()
  131.     -- check_fuel(12)
  132.  
  133.     move("fw", 1)
  134.     turtle.turnLeft()
  135.     move("fw", 6)
  136.     move("up", 4)
  137.     turtle.turnLeft()
  138. end
  139.  
  140. local function selected_is_wheat()
  141.     if turtle.getItemCount() == 0 then
  142.         return false
  143.     end
  144.     if turtle.getItemDetail().name == "minecraft:wheat" then
  145.         return true
  146.     else
  147.         return false
  148.     end
  149. end
  150.  
  151. local function selected_is_seeds()
  152.     if turtle.getItemCount() == 0 then
  153.         return false
  154.     end
  155.     if turtle.getItemDetail().name == "minecraft:wheat_seeds" then
  156.         return true
  157.     else
  158.         return false
  159.     end
  160. end
  161.  
  162. local row_offset = 0
  163.  
  164. local function find_seeds()
  165.     for s = 1, 15 do
  166.         turtle.select(s)
  167.         if selected_is_seeds() then
  168.             return true
  169.         end
  170.     end
  171.     return false
  172. end
  173.  
  174. local function place_seed()
  175.     if not find_seeds() then
  176.         return false
  177.     end
  178.     turtle.digDown()
  179.     turtle.placeDown()
  180.     return true
  181. end
  182.  
  183. local function return_from_row(len_traveled)
  184.     turtle.turnLeft()
  185.     turtle.turnLeft()
  186.  
  187.     for t = 0, len_traveled do
  188.         move("down", 1)
  189.         if t == len_traveled then
  190.             move("fw", 8)
  191.         else
  192.             move("fw", 7)
  193.         end
  194.     end
  195.    
  196.     turtle.turnLeft()
  197.     turtle.turnLeft()
  198. end
  199.  
  200. local function farm_single_row()
  201.     -- check_fuel((length * 16) + 1)
  202.    
  203.     local len_traveled = 0
  204.  
  205.     local do_cancel = false
  206.     for l = 1, length do
  207.         local local_len = 7
  208.         if l == 1 then
  209.             -- First row is 1 longer
  210.             local_len = 8
  211.         else
  212.             move("up", 1)
  213.         end
  214.  
  215.         if do_cancel then
  216.             break
  217.         end
  218.  
  219.         len_traveled = len_traveled + 1
  220.  
  221.         for f = 1, local_len do
  222.             move("fw", 1)
  223.             if not place_seed() then
  224.                 -- No more seeds, cancel after this row
  225.                 do_cancel = true
  226.             end
  227.         end
  228.     end
  229.  
  230.     return_from_row(len_traveled)
  231. end
  232.  
  233. local function farm_double_row()
  234.     -- check_fuel((length * 23) + 1)
  235.    
  236.     local len_traveled = 0
  237.  
  238.     local do_cancel = false
  239.     for l = 1, length do
  240.         local local_len = 7
  241.         if l == 1 then
  242.             -- First row is 1 longer
  243.             local_len = 8
  244.         else
  245.             move("up", 1)
  246.         end
  247.  
  248.         if do_cancel then
  249.             break
  250.         end
  251.  
  252.         len_traveled = len_traveled + 1
  253.  
  254.         for f = 1, local_len / 2 do
  255.             move("fw", 1)
  256.             if not place_seed() then
  257.                 -- No more seeds, cancel after this row
  258.                 do_cancel = true
  259.             end
  260.             turtle.turnRight()
  261.             move("fw", 1)
  262.             if not place_seed() then
  263.                 -- No more seeds, cancel after this row
  264.                 do_cancel = true
  265.             end
  266.             turtle.turnLeft()
  267.             move("fw", 1)
  268.             if not place_seed() then
  269.                 -- No more seeds, cancel after this row
  270.                 do_cancel = true
  271.             end
  272.             turtle.turnLeft()
  273.             move("fw", 1)
  274.             if not place_seed() then
  275.                 -- No more seeds, cancel after this row
  276.                 do_cancel = true
  277.             end
  278.             turtle.turnRight()
  279.         end
  280.         if local_len % 2 > 0 then
  281.             move("fw", 1)
  282.             if not place_seed() then
  283.                 -- No more seeds, cancel after this row
  284.                 do_cancel = true
  285.             end
  286.             turtle.turnRight()
  287.             move("fw", 1)
  288.             if not place_seed() then
  289.                 -- No more seeds, cancel after this row
  290.                 do_cancel = true
  291.             end
  292.             turtle.turnLeft()
  293.             turtle.turnLeft()
  294.             move("fw", 1)
  295.             turtle.turnRight()
  296.         end
  297.     end
  298.  
  299.     return_from_row(len_traveled)
  300. end
  301.  
  302. local function go_home()
  303.     -- check_fuel(row_offset + 12)
  304.  
  305.     turtle.turnLeft()
  306.     move("fw", row_offset)
  307.     move("fw", 4)
  308.     move("down", 4)
  309.     move("fw", 2)
  310.     turtle.turnRight()
  311.     move("fw", 1)
  312.     turtle.turnLeft()
  313.     turtle.turnLeft()
  314. end
  315.  
  316. local function dump_excess()
  317.     check_fuel(6)
  318.  
  319.     move("up", 1)
  320.     turtle.turnRight()
  321.     move("fw", 2)
  322.     for s = 1, 15 do
  323.         turtle.select(s)
  324.         turtle.drop(64)
  325.     end
  326.  
  327.     turtle.turnLeft()
  328.     turtle.turnLeft()
  329.     move("fw", 2)
  330.     turtle.turnRight()
  331.     move("down", 1)
  332. end
  333.  
  334. local function next_row(doubled)
  335.     local amount = nil
  336.     if doubled then
  337.         amount = 3
  338.     else
  339.         amount = 2
  340.     end
  341.  
  342.     -- check_fuel(amount)
  343.  
  344.     row_offset = row_offset + amount
  345.     turtle.turnRight()
  346.     move("fw", amount)
  347.     turtle.turnLeft()
  348. end
  349.  
  350. local function pick_up_seeds()
  351.     turtle.turnLeft()
  352.     for s = 1, 15 do
  353.         turtle.select(s)
  354.         turtle.suck(64)
  355.     end
  356.     turtle.turnRight()
  357. end
  358.  
  359. local function farm()
  360.     if rows < 3 then
  361.         check_fuel(12 + (((length * 16) + 1) * rows) + ((rows - 1) * 4) + 12)
  362.     else
  363.         check_fuel(12 + 12 + 4 + 4 + ((((length * 23) + 1) + 3 + 3) * (rows - 2)) +
  364.             (((length * 16) + 1) * 2) + (8))
  365.     end
  366.  
  367.     pick_up_seeds()
  368.     initial_farm_path()
  369.  
  370.     if rows < 1 then
  371.         print("Amount of rows is set to lower than 1")
  372.         return
  373.     end
  374.  
  375.     farm_single_row()
  376.     for r = 0, rows - 2 do
  377.         if r == 0 then
  378.             next_row()
  379.         else
  380.             next_row(true)
  381.         end
  382.         farm_double_row()
  383.     end
  384.     if rows > 1 then
  385.         if rows > 2 then
  386.             next_row(true)
  387.         else
  388.             next_row()
  389.         end
  390.         farm_single_row()
  391.     end
  392.  
  393.     go_home()
  394.     dump_excess()
  395. end
  396.  
  397. local function init()
  398.     -- Check fuel first
  399.     check_fuel(1)
  400.     print("Fuel remaining: ", turtle.getFuelLevel())
  401.  
  402.     farm()
  403. end
  404.  
  405. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement