Inksaver

Turtle powered bamboo farm

Feb 9th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 21.87 KB | Source Code | 0 0
  1. local version = 20240209.0730
  2. --[[
  3. https://pastebin.com/8F5LrfZi
  4.  
  5. installation:
  6.     pastebin get 8F5LrfZi startup.lua
  7.  
  8. This version harvests a rectangular dirt field covered in bamboo
  9. The turtle travels across the feild cutting bamboo leaving 1 block below
  10. It then travels to the side wall and finds the double chest
  11. It sits on the double chest facing the field
  12. bamboo is crafted to planks and placed in the double chest
  13. Turtle then observes the space in front waiting for a buried piston to push dirt up or modem to receive message "complete"
  14. The farming process starts, with constant checks on bamboo quantity, which is crafted to blocks
  15. ]]
  16.  
  17. local config = nil
  18. local sourceSlots = {4,8,12,13,14,15,16}
  19. local craftSlots = {1,2,3,5,6,7,9,10,11}
  20. local storage = ""
  21. local border = ""
  22.  
  23. local function clear()
  24.     term.clear()            -- clear display
  25.     term.setCursorPos(1,1)  -- reset cursor
  26. end
  27.  
  28. local function getBlockType(direction)
  29.     --[[ turtle.inspect() returns two values
  30.         1) boolean (true/false) success
  31.         2) table with two or more values:
  32.         .name
  33.         .state {axis = "y"}
  34.         .tags (["minecraft:logs"] = true, ["minecraft:logs_that_burn"] = true, ["minecraft:oak_logs"] = true}
  35.     ]]
  36.     local blockType = ""                    -- initialise blockType
  37.     local success = false
  38.     local data = nil                        -- initialise empty table variable
  39.     local inspect = turtle.inspect          -- assign inspect() function to a variable
  40.     if direction == "up" then
  41.         inspect = turtle.inspectUp          -- inspect re-assigned to inspectUp
  42.     elseif direction == "down" then
  43.         inspect = turtle.inspectDown        -- inspect re-assigned to inspectDown
  44.     end
  45.     success, data = inspect()               -- store information about the block below in a table
  46.     if success then                         -- block found
  47.         blockType = data.name               -- eg "minecraft:log"
  48.     end
  49.    
  50.     return blockType, data                  -- eg "minecraft:oak_log" , data table OR "", nil if empty
  51. end
  52.  
  53. local function values(t) -- general diy iterator
  54.     local i = 0
  55.     return function()
  56.         i = i + 1
  57.         return t[i]
  58.     end
  59. end
  60.  
  61. local function move(direction, steps, cut)
  62.     local move = turtle.forward
  63.     local dig = turtle.dig
  64.     if direction == "up" then
  65.         move = turtle.up
  66.         dig = turtle.digUp
  67.     elseif direction == "down" then
  68.         move = turtle.down
  69.         dig = turtle.digDown
  70.     elseif direction == "back" then
  71.         move = turtle.back
  72.     end
  73.    
  74.     local moves = 0
  75.     for i = 1, steps do
  76.         if cut then                         -- cut = true so dig
  77.             dig()
  78.         end
  79.         if move() then                      -- if move successful
  80.             moves = moves + 1
  81.         else
  82.             return false, moves             -- unable to move further
  83.         end
  84.     end
  85.     return true, moves
  86. end
  87.  
  88. local function down(steps, cut)
  89.     steps = steps or 1                      -- if called without parameters, set default 1
  90.     cut = cut or false                      -- if called without parameters, set default false
  91.     return move("down", steps, cut)
  92. end
  93.  
  94. local function up(steps, cut)
  95.     steps = steps or 1
  96.     cut = cut or false
  97.     return move("up", steps, cut)
  98. end
  99.  
  100. local function forward(steps, cut)    
  101.     steps = steps or 1
  102.     cut = cut or false
  103.     return move("forward", steps, cut)
  104. end
  105.  
  106. local function back(steps)
  107.     steps = steps or 1
  108.     cut = cut or false                      -- if called without parameters, set default false
  109.     return move("back", steps, cut)
  110. end
  111.  
  112. local function turnLeft(steps)
  113.     steps = steps or 1
  114.     for i = 1, steps do
  115.         turtle.turnLeft()
  116.     end
  117.     return true
  118. end
  119.  
  120. local function turnRight(steps)
  121.     steps = steps or 1
  122.     for i = 1, steps do
  123.         turtle.turnRight()
  124.     end
  125.     return true
  126. end
  127.  
  128. local function getItemSlot(item)
  129.     for i = 1, 16 do
  130.         local data = turtle.getItemDetail(i)
  131.         if data  ~= nil then
  132.             if item:find(":") == nil then   -- find partial match
  133.                 if data.name:find(item) ~= nil then
  134.                     return i
  135.                 end
  136.             else
  137.                 if data.name == item then   -- find exact match
  138.                     return i
  139.                 end
  140.             end
  141.         end
  142.     end
  143.     return 0
  144. end
  145.  
  146. local function getBambooCount(blockType)
  147.     -- count bamboo, bamboo blocks or bamboo planks
  148.     -- parameters are "", _block, _planks
  149.     blockType = blockType or ""
  150.     local count = 0
  151.     for i = 1, 16 do
  152.         data = turtle.getItemDetail(i)
  153.         if data ~= nil then
  154.             if data.name == "minecraft:bamboo".. blockType then
  155.                 count = count + data.count
  156.             end
  157.         end
  158.     end
  159.     return count
  160. end
  161.  
  162. local function go(path)
  163.     local commandList = {}
  164.     local command = ""
  165.     local direction = {"up", "forward", "down"}
  166.    
  167.     path = path:gsub(" ", "")   -- remove spaces from path
  168.     -- make a list of commands from path string eg "x0F12U1" = x0, F12, U1
  169.     for i = 1, #path do
  170.         local character = path:sub(i, i)            -- examine each character in the string
  171.         if tonumber(character) == nil then          -- char is NOT a number
  172.             if command ~= "" then                   -- command is NOT empty eg "x0"
  173.                 table.insert(commandList, command)  -- add to table eg "x0"
  174.             end
  175.             command = character:upper()             -- replace command with new character eg "F"
  176.         else                                        -- char IS a number
  177.             command = command..character:upper()    -- add eg 1 to F = F1, 2 to F1 = F12
  178.             if i == #path then                      -- last character in the string
  179.                 table.insert(commandList, command)
  180.             end
  181.         end
  182.     end
  183.     -- R# L# F# B# U# D# = Right, Left, Forward, Back, Up, Down
  184.     -- dig: X0,X1,X2 (up/fwd/down)
  185.     for cmd in values(commandList) do -- eg F12 or x1
  186.         local move = (cmd:sub(1, 1)):upper()
  187.         local modifier = tonumber(cmd:sub(2))
  188.  
  189.         if move == "B" then
  190.             back(modifier)
  191.         elseif move == "D" then
  192.             down(modifier)
  193.         elseif move == "F" then
  194.             forward(modifier)
  195.         elseif move == "U" then
  196.             up(modifier)
  197.         elseif move == "R" then
  198.             turnRight(modifier)
  199.         elseif move == "L" then
  200.             turnLeft(modifier)
  201.         elseif move == "X" then
  202.             if modifier == 0 then
  203.                 turtle.digUp()
  204.             elseif modifier == 1 then
  205.                 turtle.dig()
  206.             elseif modifier == 2 then
  207.                 turtle.digDown()
  208.             end
  209.         end
  210.     end
  211. end
  212.  
  213. local function place(blockType, direction)
  214.     direction = direction or "forward"
  215.     local success = false
  216.     -- assign place methods according to direction
  217.     local Place = turtle.place
  218.     if direction == "up" then
  219.         Place = turtle.placeUp
  220.     elseif direction == "down" then
  221.         Place = turtle.placeDown
  222.     end
  223.     local slot = getItemSlot(blockType)
  224.     if slot > 0 then -- item to place found
  225.         turtle.select(slot)
  226.         if Place() then
  227.             success = true
  228.         end
  229.     end
  230.     turtle.select(1)        -- restore selected to 1
  231.     return success, slot
  232. end
  233.  
  234. local function isItemOfType(direction, itemTable)
  235.     -- check if item next to turtle is a specific type
  236.     local success, data = false, nil
  237.     if direction == "up" then
  238.         success, data = turtle.inspectUp()              -- check block above
  239.     elseif direction == "down" then
  240.         success, data = turtle.inspectDown()            -- check block below
  241.     else
  242.         success, data = turtle.inspect()                -- check if anything in front
  243.     end
  244.     if success then                                     -- a block is in front
  245.         if type(itemTable) == "table" then              -- Are we checking for multiple items eg {"chest", "barrel"} ?
  246.             for _, item in ipairs(itemTable) do         -- iterate items
  247.                 if data.name:find(item) ~= nil then     -- match found
  248.                     return true, item
  249.                 end
  250.             end
  251.         elseif type(itemTable) == "string" then         -- checking for single item eg "dirt"
  252.             if data.name:find(itemTable) ~= nil then    -- match found
  253.                 return true, item
  254.             end
  255.         end
  256.     end
  257.     return false, ""                                    -- no match
  258. end
  259.  
  260. local function placeStorage()
  261.     -- used before crafting to place onboard chest/barrel
  262.     local lib = {}
  263.    
  264.     function lib.turn()
  265.         local turns = 0
  266.         while turtle.detect() do
  267.             turnRight(1)
  268.             turns = turns + 1
  269.             if turns == 4 then
  270.                 turns = 0
  271.                 break
  272.             end
  273.         end
  274.         turtle.dig()
  275.         place(storage, "forward")
  276.         return turns
  277.     end
  278.    
  279.     local dig = turtle.dig
  280.     local drop = turtle.drop
  281.     local suck = turtle.suck
  282.     local turns = 0
  283.     if isItemOfType("down", "chest") then
  284.         turns = lib.turn()
  285.     else
  286.         if not turtle.detectUp() then
  287.             dig = turtle.digUp
  288.             drop = turtle.dropUp
  289.             suck = turtle.suckUp
  290.             place(storage, "up")
  291.         elseif not turtle.detectDown() then
  292.             dig = turtle.digDown
  293.             drop = turtle.dropDown
  294.             suck = turtle.suckDown
  295.             place(storage, "down")
  296.         else
  297.             turns = lib.turn()
  298.         end
  299.     end
  300.     return dig, drop, suck, turns   -- return turtle methods appropriate to direction + no of turns
  301. end
  302.  
  303. local function craftPlanks()
  304.     -- only called when above main storage chest
  305.     local count = getBambooCount("_block")                      -- how many bamboo blocks
  306.     if count > 0 then
  307.         local dig, drop, suck, turns = placeStorage()           -- place onboard chest/barrel
  308.         repeat
  309.             for i = 1, 16 do                                    -- drop all bamboo planks into main storage, blocks into temp first
  310.                 local data = turtle.getItemDetail(i)
  311.                 if data ~= nil then
  312.                     if data.name == "minecraft:bamboo_block" then
  313.                         turtle.select(i)
  314.                         drop()                                  -- put blocks into craft storage
  315.                     elseif data.name == "minecraft:bamboo_planks" then
  316.                         turtle.select(i)
  317.                         turtle.dropDown()                       -- put planks into main storage
  318.                     end
  319.                 end
  320.             end
  321.             for i = 1, 16 do                                    -- drop any  remaining bamboo last
  322.                 turtle.select(i)                                -- makes sure blocks will be removed first
  323.                 drop()
  324.             end
  325.  
  326.             turtle.select(1)                                    -- select slot 1
  327.             suck(32)                                            -- attempt so retrieve 32 blocks
  328.             local data = turtle.getItemDetail(1)                -- get detail of current slot 1 contents
  329.             local nomore = false                                -- initialise flag
  330.             if data ~= nil then                                 -- nil result used as loop terminator
  331.                 if data.name == "minecraft:bamboo_block" then
  332.                     if data.count > 32 then                     -- if count > 32
  333.                         drop(data.count - 32)                   -- return excess back to craft storage
  334.                     end                                         -- saves having to drop crafted planks from multiple locations
  335.                     if turtle.craft() then                      -- attempt to craft 64 planks
  336.                         turtle.select(1)                        -- success
  337.                         turtle.dropDown()                       -- drop into main storage
  338.                     end
  339.                 else                                            -- did not get blocks, so assume there are no more
  340.                     nomore = true
  341.                 end
  342.             end
  343.         until data == nil or nomore                             -- loop exit if bamboo block NOT found or chest is empty
  344.         while suck() do end                                     -- empty craft storage
  345.         dig()                                                   -- collect craft storage
  346.         if turns > 0 then                                       -- return to starting position
  347.             turnLeft(turns)
  348.         end
  349.     end
  350. end
  351.  
  352. local function craftBamboo(threshold)
  353.     -- large amounts of bamboo collected while harvesting. convert to blocks to reduce volume by factor of 9
  354.     if threshold == nil then                                -- no threshold supplied
  355.         threshold = 256                                     -- craft if > 256 bamboo
  356.     end
  357.     local count = getBambooCount()                          -- how much bamboo onboard?
  358.     if count >= threshold then                              -- crafting required
  359.         local dig, drop, suck, turns = placeStorage()       -- place onboard chest/barrel
  360.        
  361.         repeat
  362.             for i = 1, 16 do                                -- drop all bamboo into storage first
  363.                 local data = turtle.getItemDetail(i)
  364.                 if data ~= nil then
  365.                     if data.name == "minecraft:bamboo" then
  366.                         turtle.select(i)
  367.                         drop()
  368.                     end
  369.                 end
  370.             end
  371.             local perSlot = math.floor(count / 9)           -- calculate crafting ratio (9 bamboo = 1 block)
  372.             if perSlot > 32 then                            -- ensure max of 32 blocks crafted at once
  373.                 perSlot = 32
  374.             end
  375.             for i = 1, 16 do                                -- drop all other items back into storage
  376.                 turtle.select(i)
  377.                 drop()
  378.             end
  379.            
  380.             for _, slot in ipairs(craftSlots) do            -- load all 9 crafting slots
  381.                 turtle.select(slot)
  382.                 suck(perSlot)
  383.                 local received = turtle.getItemCount(slot)  -- suck() currently broken, does not always work correctly
  384.                 if received > perSlot then                  -- got more thean required
  385.                     drop(received - perSlot)                -- return excess
  386.                 elseif received < perSlot then              -- got less then required
  387.                     suck(perSlot - received)                -- try again
  388.                 end
  389.             end
  390.             turtle.select(1)                                -- craft 32 planks into slot 1
  391.             turtle.craft()
  392.             turtle.select(1)
  393.             local fuel = turtle.getFuelLevel()              -- check if refuel needed
  394.             if fuel < 400 then
  395.                 turtle.craft()                              -- craft all 32 blocks into 64 planks
  396.                 turtle.select(1)
  397.                 turtle.refuel()                             -- refuel with 64 planks (64 * 15 = 960)
  398.             end
  399.            
  400.             while suck() do end                             -- empty crafting chest
  401.             count = getBambooCount()                        -- re-calculate bamboo remaining
  402.         until count < 9                                     -- exit loop if < 9 bamboo
  403.         dig()                                               -- recover storage
  404.         if turns > 0 then                                   -- return to original position
  405.             turnLeft(turns)
  406.         end
  407.     end
  408. end
  409.  
  410. local function wait()
  411.     -- wait for bamboo to grow approx 10 - 15 mins for full growth to level 10
  412.     -- wall / piston mechanism pushes dirt block upwards OR turtle/modem
  413.     local time = 0
  414.     local waitTime = 5
  415.    
  416.     if config.detector == "N" then
  417.         rednet.open("back")
  418.     end
  419.     while true do
  420.         clear()
  421.         -- following code for display purposes only so min/mins and sec/secs is correct
  422.         local mins = math.floor(time / 60)
  423.         local secs = time % 60
  424.         local output = "Waiting for bamboo growth:\n"..time.." seconds ("..mins.." minute"
  425.         if mins ~= 1 then  output = output.."s" end
  426.         output = output..", "..secs.." second"
  427.         if secs ~= 1 then  output = output.."s" end
  428.         output = output..")"
  429.         print(output)
  430.         sleep(waitTime)         -- sleep for 5 secs
  431.         if config.detector == "P" then
  432.             if turtle.detect() then -- dirt block has appeared, pushed up by piston below the field
  433.                 clear()
  434.                 return              -- waiting finished
  435.             end
  436.         elseif config.detector == "N" then
  437.             local id, message = rednet.receive()
  438.             if message == "complete" then
  439.                 clear()
  440.                 return              -- waiting finished
  441.             end
  442.         end
  443.         time = time + waitTime
  444.     end
  445. end
  446.  
  447. local function findBorder(direction)
  448.     -- used if auto-starting in unknown position
  449.     direction = direction or config.layout
  450.    
  451.     while turtle.down() do end                              -- over border or non-planted field
  452.     local blockType = getBlockType("down")
  453.     if blockType:find("bamboo") ~= nil then                 -- still over planted area
  454.         repeat                                              -- dig until reach non-planted area
  455.             turtle.dig()
  456.             turtle.suck()
  457.             turtle.forward()
  458.             turtle.suckDown()
  459.         until not isItemOfType("down", {"bamboo"})
  460.         while turtle.down() do end                          -- go down to field
  461.         while isItemOfType("down", {"dirt", "grass"}) do    -- while on dirt or grass move forward
  462.             turtle.forward()
  463.         end                                                 -- will now be over border wall
  464.     else                                                    -- over air / dirt / grass / border
  465.         if blockType == "" then                             -- over air
  466.             turtle.back()                                   -- move back onto border
  467.         elseif isItemOfType("down", {"dirt", "grass"}) then -- over dirt/grass
  468.             while isItemOfType("down", {"dirt", "grass"}) do-- go forward while dirt below
  469.                 turtle.forward()
  470.             end
  471.         end
  472.     end
  473.    
  474.     blockType = getBlockType("down")                        -- check below
  475.     if blockType == "" then                                 -- on air so go back
  476.         turtle.back()
  477.     else
  478.         border = blockType                                  -- on border
  479.     end
  480.     go(direction.."1")                                      -- turn to continue along border
  481. end
  482.  
  483. local function followBorder()
  484.     local lib = {}
  485.    
  486.     function lib.follow()
  487.         -- travel along border blocks until either hit outer wall/block or no longer on border
  488.         while isItemOfType("down", {border}) do
  489.             if not turtle.forward() then
  490.                 break
  491.             end
  492.         end
  493.     end
  494.    
  495.     function lib.corner()
  496.         go("B1L1F1")                        -- try left turn first
  497.         blockType = getBlockType("down")    -- is block below?
  498.         if blockType == "" then             -- air below: wrong direction
  499.             go("B2R2")                      -- go other direction
  500.         end
  501.         return getBlockType("down")         -- return block below name
  502.     end
  503.    
  504.     local inPosition = false
  505.     repeat
  506.         lib.follow()
  507.         blockType = getBlockType("down")
  508.         if turtle.detect() then --  corner blocks or outer wall present
  509.             --turnLeft(1)
  510.             go(config.turn.."1")
  511.         else
  512.             -- now over chest / or air. Over chest dirt is to the left
  513.             if blockType == "" then
  514.                 if lib.corner() == "minecraft:chest" then
  515.                     go("F1"..config.layout.."1")            -- turn corner in calculated direction
  516.                     inPosition = true
  517.                     break
  518.                 end
  519.             end
  520.         end
  521.     until blockType == "minecraft:chest"
  522.     if not inPosition then
  523.         --turnLeft(1)
  524.         go(config.turn.."1")
  525.     end
  526. end
  527.  
  528. local function checkStorageLevel()
  529.     -- use peripheral and inventory apis to detemine storage contents
  530.     local chest = peripheral.wrap("bottom")
  531.     local count = 0
  532.     for slot, item in pairs(chest.list()) do
  533.         count = count + item.count
  534.     end
  535.     return count, chest.size()
  536. end
  537.  
  538. local function farm()
  539.     --traverse field in circular motion, digging bamboo in front
  540.     local lib = {}
  541.    
  542.     function lib.suck()
  543.         turtle.select(1)
  544.         turtle.suckUp()
  545.         turtle.suckDown()
  546.         turtle.suck()
  547.     end
  548.    
  549.     function lib.clear(size)
  550.         for i = 1, size do
  551.             lib.suck()
  552.             if turtle.dig() then
  553.                 lib.suck()
  554.             end
  555.             while not turtle.forward() do
  556.                 turtle.dig()
  557.             end
  558.         end
  559.     end
  560.    
  561.     function lib.reposition(moves)
  562.         -- turn left and move forward moves x
  563.         -- turn left again
  564.         go(config.turn.."1")
  565.         for move = 1, moves do
  566.             while not turtle.forward() do
  567.                 turtle.dig()
  568.             end
  569.             lib.suck()
  570.         end
  571.         go(config.turn.."1")
  572.     end
  573.    
  574.     local outward = true    -- set direction flag
  575.     local steps = ((config.width + config.border) * 2) - 1
  576.     for col = 1, steps do
  577.         lib.clear(config.length + (config.border * 2) - 1)
  578.         craftBamboo()
  579.         if col < steps then
  580.             if outward then
  581.                 lib.reposition(2)   -- go over recently harvested row to collect fallen bamboo
  582.             else
  583.                 lib.reposition(3)   -- start new column
  584.             end
  585.             outward = not outward   -- flip flag status
  586.         end
  587.     end
  588.  
  589.     if outward then
  590.         findBorder(config.turn)
  591.     else
  592.         findBorder(config.layout)
  593.     end
  594.     followBorder()
  595. end
  596.  
  597. local function gotoFarm()
  598.     -- assume currently on main storage chest, facing farm
  599.     turtle.dig()        -- if using network detection no effect
  600.     turtle.forward()
  601.     place("minecraft:dirt", "down")
  602.     -- move to edge of the field, facing bamboo eg "U1F2 L1B1"
  603.     go("U1F"..tostring(config.border)..config.turn.."1B"..(config.border - 1))
  604. end
  605.  
  606. local function orientate()
  607.     -- turn until facing field with chest below, wall or modem behind
  608.     if isItemOfType("forward", {"dirt", "grass"}) then
  609.         return
  610.     end
  611.     local turns = 0
  612.     for i = 1, 4 do
  613.         if isItemOfType("forward", {"wall", "modem"}) then
  614.             break
  615.         end
  616.         turnRight(1)
  617.         turns = turns + 1
  618.     end
  619.     if turns > 0 then
  620.         turnLeft(turns)
  621.     end
  622. end
  623.  
  624. local function goHome(startOn)
  625.     -- a, b, f (air, bamboo, field)
  626.     findBorder(config.layout)
  627.     followBorder()
  628. end
  629.  
  630. local function isHome()
  631.     -- home is above storage chest. If double then use one furthest from the corner
  632.     if isItemOfType("down", {"chest", "barrel"}) then       -- already on storage chest / barrel
  633.         return "c"
  634.     else
  635.         local blockType = getBlockType("down")
  636.         if blockType == "" then                             -- air. could be anywhere on the farm
  637.             return "a"
  638.         elseif isItemOfType("down", {"dirt", "grass"}) then -- on field
  639.             return "f"
  640.         elseif blockType:find("bamboo") ~= nil then         -- at start position, waiting for bamboo
  641.             return "b"
  642.         else
  643.             return ""
  644.         end
  645.     end
  646. end
  647.  
  648. local function createConfig()
  649.     config = {}
  650.     local response = nil
  651.    
  652.     repeat
  653.         clear()
  654.         write("How wide is the growing area?\n(Count bamboo directly in front) ")
  655.         response = tonumber(read())
  656.     until response ~= nil
  657.     config.width = response
  658.    
  659.     repeat
  660.         clear()
  661.         write("How long is the growing area?\n(Count bamboo to right or left) ")
  662.         response = tonumber(read())
  663.     until response ~= nil
  664.     config.length = response
  665.    
  666.     repeat
  667.         clear()
  668.         write("How wide is the dirt/grass border?\n(Between bamboo and outer wall) ")
  669.         response = tonumber(read())
  670.     until response ~= nil
  671.     config.border = response
  672.    
  673.     repeat
  674.         clear()
  675.         write("Position of storage chest (R/L)?\n(Relative to player at farm border")
  676.         response = read()
  677.         response = response:upper()
  678.     until response == "R" or response == "L"
  679.     config.layout = response
  680.     if response == "R" then
  681.         config.turn = "L"
  682.     else
  683.         config.turn = "R"
  684.     end
  685.     repeat
  686.         clear()
  687.         write("Growth detection with wall/piston or modem/network system (P / N)?")
  688.         response = read()
  689.         response = response:upper()
  690.     until response == "P" or response == "N"
  691.     config.detector = response
  692. end
  693.  
  694. local function writeConfig()
  695.     local h = fs.open("config.lua", "w")
  696.     h.writeLine("return")
  697.     h.writeLine(textutils.serialise(config))
  698.     h.close()
  699.     print("Config.lua file written")
  700. end
  701.  
  702. local function checkForChest()
  703.     -- should be a barrel or chest in inventory
  704.     -- it may be placed externally
  705.     local slot = getItemSlot("minecraft:chest")
  706.     if slot == 0 then
  707.         slot = getItemSlot("minecraft:barrel")
  708.         if slot > 0 then
  709.             storage = "minecraft:barrel"
  710.         end
  711.     else
  712.         storage = "minecraft:chest"
  713.     end
  714.     if storage == "" then                       -- not found internally
  715.         local success, item = isItemOfType("up", {"chest", "barrel"})
  716.         if success then                         -- found above
  717.             while turtle.suckUp() do end        -- empty and retreive it
  718.             turtle.digUp()
  719.             storage = item
  720.         else
  721.             success, item = isItemOfType("forward", {"chest", "barrel"})
  722.             if success then                     -- found in front
  723.                 while turtle.suck() do end
  724.                 turtle.dig()
  725.                 storage = item
  726.             end
  727.         end
  728.     end
  729.     return storage
  730. end
  731.  
  732. local function main()
  733.     clear()                     -- clear terminal
  734.     --[[
  735.     return                      -- config.lua
  736.     {
  737.         width = 13,
  738.         length = 13,
  739.         border = 2,
  740.         layout = "R",
  741.         turn = "L",
  742.         detector = "P"
  743.     }
  744.     ]]
  745.     if fs.exists("config.lua") then
  746.         config = require("config")
  747.     else
  748.         createConfig()
  749.         writeConfig()
  750.     end
  751.     if checkForChest() == "" then
  752.         print("Unable to find onboard chest or barrel\nAdd to inventory and reboot")
  753.         return
  754.     end
  755.  
  756.     craftBamboo()
  757.     local startOn = isHome()    -- c = home, a = air, b = bamboo, f = field
  758.     if startOn == "" then
  759.         print("Unable to determine position\nMove to start and reboot")
  760.         return
  761.     elseif startOn ~= "c" then  -- check if starting in correct position
  762.         goHome(startOn)         -- find starting position on barrel
  763.     end
  764.     orientate()                 -- assume on top of storage barrel. look for facing double chest
  765.     while true do
  766.         craftBamboo(9)          -- deal with any bamboo previously  harvested
  767.         craftPlanks()
  768.         local count, size  = checkStorageLevel()
  769.         if count / 64 > size * 0.8 then
  770.             print("Storage at 80%. Farm disabled")
  771.             return
  772.         end
  773.         wait()
  774.         gotoFarm()
  775.         farm()
  776.         if turtle.detect() then
  777.             turtle.dig()
  778.             turtle.forward()
  779.             place("dirt", "down")
  780.             turtle.back()
  781.         end
  782.     end
  783. end
  784.  
  785. main()
Add Comment
Please, Sign In to add comment