diff --git a/install.lua b/install.lua index fa4cc99..a9768ef 100644 --- a/install.lua +++ b/install.lua @@ -1,5 +1,6 @@ local LIST_FILES = { 'miner.lua', + 'simple-harvester.lua', 'libs/turtle-utils.lua', 'libs/robot.lua' }; diff --git a/simple-harvester.lua b/simple-harvester.lua new file mode 100644 index 0000000..a45f7b2 --- /dev/null +++ b/simple-harvester.lua @@ -0,0 +1,99 @@ +local VERSION = "0.1.0" +local IDLE_TIME = 2 + +function isMatureCrop() + local isBlock, block = turtle.inspect() + + local blockStateAge = block and block.state and block.state.age + return blockStateAge == 7 +end + +function isInventory() + local periph = peripheral.wrap('front') + + if not periph then + return false + end + + return peripheral.hasType(periph, 'inventory') +end + +function isSeed(item) + local tags = item.tags or {} + return tags['forge:seeds'] or tags['mysticalagriculture:seeds'] or false +end + +function selectSeed() + for i=1, 16,1 do + local details = turtle.getItemDetail(i, true) + + if details and isSeed(details) then + turtle.select(i) + return true + end + end + + error("selectSeed error: seed not found") +end + +function strictDrop(idx) + if turtle.getSelectedSlot() ~= idx then + turtle.select(idx) + end + + local ok, err = turtle.drop() + + if not ok then + error('strictDrop error: ' .. err) + end + + return true +end + +function dropAll() + for i=1, 16, 1 do + local count = turtle.getItemCount(i) + + if count > 0 then + strictDrop(i) + end + end +end + +function waitFor(predicate, sleepTime) + while true do + local result = predicate() + + if result ~= nil and result ~= false then + return result + end + + os.sleep(sleepTime) + end +end + +function emptyInventory() + turtle.turnLeft() + turtle.turnLeft() + + waitFor(isInventory, IDLE_TIME) + dropAll() + + turtle.turnLeft() + turtle.turnLeft() +end + +function main() + while true do + waitFor(isMatureCrop, IDLE_TIME) + turtle.dig() + selectSeed() + turtle.place() + emptyInventory() + end +end + +print("Starting Trap's simple harvester v" .. VERSION) +print("=========================") +print() +main()