Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- IF YOU'RE LOST TRY > [program] help
- --variables
- local args = {...}
- local combustibles = {'minecraft:coal_block', 'minecraft:coal', 'minecraft:bamboo'}
- local seeds = {'minecraft:potato', 'minecraft:wheat_seeds', 'minecraft:carrot', 'minecraft:beetroot_seeds', 'minecraft:nether_wart', 'minecraft:sweet_berries', 'minecraft:cocoa_beans'}
- local produce = {'minecraft:potato', 'minecraft:wheat', 'minecraft:carrot', 'minecraft:beetroot', 'minecraft:nether_wart', 'minecraft:sweet_berries', 'minecraft:cocoa_beans'}
- local crops = {'minecraft:potatoes', 'minecraft:wheat', 'minecraft:carrots', 'minecraft:beetroots', 'minecraft:nether_wart', 'minecraft:sweet_berry_bush', 'minecraft:cocoa'}
- local harvestAge = 7
- local sleepTime = 60
- local latestChanges = 'changes functionality\ntrash functionality\nsetup functionality\nimproved help menu\n\t\t '..
- '\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t - June 2020'
- local helpText = {
- 'arguments marked \'-t\' terminate program\n'..
- 'optional params \'?[...]\' can be ignored\n\n'..
- 'info\n\t general information about this \n\t program -t\n\n'..
- 'help\n\t this -t',
- 'update ?[name]\n\t downloads latest version from \n\t pastebin and saves to [name] -t\n\n'..
- 'changes \n\t most recent changes -t\n\n'..
- 'sleep [number]\n\t seconds to wait between cycles',
- 'age [number]\n\t growthstages before harvesting a \n\t plant\n\n'..
- 'setup [number] ?continue\n\t builds farm of length [number]\n\t optional param to\n\t continue after setup \n\t (requires resources) -t\n\n',
- 'inspect\n\t use on mature plant for information \n\t about cropId and growthstages -t'
- }
- local infoText = {'This is a crop farming turtle program inspired by the Michael Reeves Potato Empire. \n'..
- 'This program currently supports all vanilla crops and will farm and store produce automatically. \n',
- 'It is fairly easy to add additional support for modded crops, as long as they follow the vanilla growth model. \n'..
- 'There are multiple setup arguments and functionalities to improve customization and ease of use. These include help features, an update command and auto setup functionality. \n',
- 'Despite all, there are still many kinks to be ironed out, so check for updates every now and then.\n'..
- 'Thanks for checking out this program.\n'..
- '\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t Da_Aresinger'
- }
- --helper methods
- --checks if block to front is crop
- local function isCrop()
- local ret, block = turtle.inspect()
- for i, crop in ipairs(crops) do
- if (block ~= nil and block.name == crop) then
- return true
- end
- end
- return false
- end
- --returns crop age
- local function checkCrop()
- ret, block = turtle.inspect()
- if (isCrop()) then
- return block.state.age
- else
- return 0
- end
- end
- --steps to next crop, returns false if blocked
- local function step()
- turtle.turnLeft()
- if (turtle.detect()) then
- turtle.turnRight()
- return false
- end
- turtle.forward()
- turtle.turnRight()
- return true
- end
- --returns to home position, must be facing crops
- --turtle will be left facing crop chest
- local function home()
- turtle.turnRight()
- while (not turtle.detect()) do
- turtle.forward()
- end
- end
- --checks whether item is seed
- local function isSeed()
- item = turtle.getItemDetail()
- for i, seed in ipairs(seeds) do
- if (item ~= nil and item.name == seed) then
- return true
- end
- end
- return false
- end
- --selects the next slot with seeds
- -- returns false if there are no seeds and selects slot 1
- local function findSeed()
- for i = 1, 16, 1 do
- turtle.select(i)
- if (isSeed()) then
- return true
- end
- end
- turtle.select(1)
- return false
- end
- --picks up as long as there is something to pick up
- local function suckAll()
- while (turtle.suck()) do
- end
- end
- --runs a single harvest cycle
- local function harvest()
- if (checkCrop() >= harvestAge) then
- turtle.dig()
- suckAll()
- if (findSeed()) then
- turtle.place()
- else
- error('no more seeds')
- end
- end
- end
- --checks if item is produce
- local function isProduce()
- item = turtle.getItemDetail()
- for i, prod in ipairs(produce) do
- if (item ~= nil and item.name == prod) then
- return true
- end
- end
- return false
- end
- --deposits produce to the front and seeds to the top if there is a block (hopefully a chest)
- --produce test takes priority
- local function deposit()
- for i = 1, 16, 1 do
- turtle.select(i)
- if (isProduce()) then
- turtle.drop()
- end
- if (isSeed() and turtle.detectUp()) then
- turtle.dropUp()
- end
- end
- end
- --runs a full farm cycle
- local function farm()
- repeat
- harvest()
- until (not step())
- home()
- deposit()
- turtle.turnLeft()
- end
- --checks item for combustability
- local function isCombustible()
- item = turtle.getItemDetail()
- for i, combustible in ipairs(combustibles) do
- if (item ~= nil and item.name == combustible) then
- return true
- end
- end
- return false
- end
- --checks inventory for combustibles and selects relevant slot
- --returns true when found, false when no combustibles available
- --selects slot 1 if no combustibles available
- local function findFuel()
- turtle.select(1)
- found = false
- for i = 1, 16, 1 do
- turtle.select(i)
- found = isCombustible()
- if (found) then
- break
- end
- end
- if (not found) then
- turtle.select(1)
- end
- return found
- end
- --out of fuel
- local function noFuel()
- if (turtle.getFuelLevel() == 0) then
- shell.run("label set HELP")
- shell.run('clear')
- print("I am out of fuel")
- print("press any key to continue")
- local event, key = os.pullEvent('key')
- shell.run("label clear")
- shell.run('clear')
- if (findFuel()) then
- turtle.refuel()
- end
- end
- end
- --collects additional combustibles
- local function getFuel()
- turtle.turnLeft()
- while (not turtle.detect()) do
- noFuel()
- turtle.forward()
- end
- if (not findFuel()) then
- turtle.select(16)
- end
- turtle.suck(64 - turtle.getItemCount())
- turtle.turnRight()
- if (not findFuel()) then
- print('current fuelLevel: '..turtle.getFuelLevel())
- error('\ncannot refuel')
- end
- end
- --initiates refuel when fuel<800 fueling at minimum to 5000
- local function fuelUp()
- if (turtle.getFuelLevel()<800) then
- while (turtle.getFuelLevel()<5000) do
- if (findFuel()) then
- turtle.refuel()
- else
- getFuel()
- end
- end
- end
- end
- --trashes unusable items
- local function trash()
- for i = 1, 16, 1 do
- turtle.select(i)
- if not (isSeed() or isProduce() or isCombustible()) then
- turtle.dropDown()
- end
- end
- end
- -- collects compatibility information for adding additional crops to the program
- local function getBlockData()
- shell.run('clear')
- ret, block = turtle.inspect()
- if (not ret) then
- print('nothing to inspect...')
- return
- end
- print('crop: ' .. block.name)
- if (block.state ~= nil and block.state.age ~= nil) then
- print('current age: ' .. block.state.age)
- else
- print('This does not seem to be a compatible crop')
- end
- end
- -- sets up the runway and chests
- local function setup(length)
- reqfuel = 4*length-4
- shell.run('clear')
- -- ensures there is enough fuel
- while true do
- if (turtle.getFuelLevel() < reqfuel) then
- if findFuel() then
- turtle.refuel()
- else
- print('Not enough fuel\nrequired: ' .. reqfuel ..'\ncurrent: '.. turtle.getFuelLevel() ..'\n\t\tpress any key to continue')
- local event, key = os.pullEvent("key")
- shell.run('clear')
- end
- else
- break
- end
- end
- -- requests and equips diamond hoe
- local hoe = false
- while not hoe do
- for i=1, 16, 1 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if (item ~= nil and item.name == 'minecraft:diamond_hoe') then
- hoe = true
- turtle.equipLeft()
- local item = turtle.getItemDetail()
- if (item ~= nil and item.name == 'minecraft:diamond_pickaxe') then
- turtle.equipRight()
- end
- break
- end
- end
- if not hoe then
- print('I need a diamond hoe\n\t\tpress any key to continue')
- local event, key = os.pullEvent("key")
- shell.run('clear')
- end
- end
- -- requests chests
- local chests = false
- while not chests do
- for i=1, 16, 1 do
- turtle.select(i)
- item = turtle.getItemDetail()
- if (item ~= nil and item.name == 'minecraft:chest' and turtle.getItemCount() >= 4) then
- chests = true
- break
- end
- end
- if not chests then
- print('I need 4 chests (currently only vanilla)\n\t\tpress any key to continue')
- local event, key = os.pullEvent("key")
- shell.run('clear')
- end
- end
- -- actual setup
- turtle.digDown('right')
- turtle.placeDown()
- turtle.digUp('right')
- turtle.placeUp()
- turtle.turnRight()
- turtle.dig('right')
- turtle.place()
- turtle.turnLeft()
- turtle.dig('right')
- turtle.dig('left')
- turtle.turnLeft()
- for i = 1, length - 1, 1 do
- turtle.dig('right')
- turtle.forward()
- turtle.turnRight()
- turtle.dig('right')
- turtle.dig('left')
- turtle.turnLeft()
- end
- turtle.dig('right')
- turtle.place()
- turtle.turnRight()
- home()
- -- clean inventory
- deposit()
- turtle.turnLeft()
- trash()
- -- unequip hoe
- for i = 1, 16, 1 do
- if turtle.getItemCount() == 0 then
- turtle.equipLeft()
- turtle.dropDown()
- break
- end
- end
- -- request seeds
- turtle.suckUp()
- local seeds = false
- while not seeds do
- if (findSeed() and turtle.getItemCount() >= length) then
- seeds = true
- for i = 1, length - 1, 1 do
- turtle.place()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnRight()
- end
- turtle.place()
- else
- print('I need '.. length ..' seeds (currently only vanilla)\n\t\tpress any key to continue')
- local event, key = os.pullEvent("key")
- shell.run('clear')
- end
- end
- end
- -- player input exit request
- local function exitRequest()
- local event, key = os.pullEvent("key")
- shell.run('clear')
- return (x == 'e' or x == 'q')
- end
- -- help menu
- local function help()
- local pages = table.getn(helpText)
- for key, page in pairs(helpText) do
- shell.run('clear')
- print('Help ('.. key ..'/'.. pages ..') - e/q to exit, other keys to continue\n')
- print(page)
- if exitRequest() then return end
- end
- end
- -- prints general info page
- local function info()
- local pages = table.getn(infoText)
- for key, page in pairs(infoText) do
- shell.run('clear')
- print('Info ('.. key ..'/'.. pages ..') - e/q to exit, other keys to continue\n')
- print(page)
- if exitRequest() then return end
- end
- end
- -- updates the program
- local function update(name)
- if(fs.exists(name)) then
- print(name .. ' exists, using temp')
- shell.run('pastebin get BbMyDvEx temp')
- if (fs.exists('temp')) then
- print('replacing ' .. name .. ' with temp')
- fs.delete(name)
- fs.move('temp', name)
- if (fs.exists(name)) then
- print(name .. ' updated successfully')
- else
- error('Error: could not update correctly. Check files for [temp] and [' .. name .. ']')
- end
- end
- else
- print('changing program name to ' .. name)
- shell.run('pastebin get BbMyDvEx ' .. name)
- if (fs.exists(name)) then
- fs.delete(debug.getinfo(1).source:match('[^@]*$'))
- else
- error('update failed - maintaining old version')
- end
- end
- end
- -- interprets args
- local function checkArgs()
- i = 1
- while (args[i] ~= nil) do
- arg = args[i]
- i = i+1
- if (arg == 'sleep' and args[i] ~= nil) then
- arg = tonumber(args[i])
- if (arg ~= nil) then
- print('setting sleep time to '..arg)
- sleepTime = arg
- i = i+1
- sleep(2)
- else
- print('arg' .. i - 1 .. ' discarded - try [program] help')
- return false
- end
- elseif (arg == 'age' and args[i] ~= nil) then
- arg = tonumber(args[i])
- if (arg ~= nil) then
- print('setting harvestAge to '..arg)
- harvestAge = arg
- i = i+1
- sleep(2)
- else
- print('arg' .. i - 1 .. ' discarded - try [program] help')
- return false
- end
- elseif (arg == 'setup' and args[i] ~= nil) then
- arg = tonumber(args[i])
- continue = (args[i+1] ~= nil and args[i+1] == 'continue')
- if continue then i = i+1 end
- if (arg ~= nil) then
- setup(arg)
- i = i+1
- if not continue then
- return false
- end
- else
- print('arg' .. i - 1 .. ' discarded - try [program] help')
- return false
- end
- elseif (arg == 'inspect') then
- getBlockData()
- return false
- elseif (arg == 'help') then
- help()
- return false
- elseif (arg == 'info') then
- info()
- return false
- elseif (arg == 'update') then
- if (args[i] ~= nil) then
- update(args[i])
- else
- update(debug.getinfo(1).source:match('[^@]*$'))
- end
- return false
- elseif (arg == 'changes') then
- print(latestChanges)
- return false
- else
- print('arg' .. i - 1 .. ' discarded - try [program] help')
- return false
- end
- end
- return true
- end
- --init
- shell.run('clear')
- if (not checkArgs()) then
- return
- end
- shell.run('clear')
- print('Reeves knock-off farming turtle\n\t-by daAresinger\n\n\"I will be running left and right, harvesting your ORGANS!!!\n I mean ... veggies.\"')
- --main loop
- while (true) do
- fuelUp()
- farm()
- trash()
- sleep(sleepTime)
- end
Advertisement
Add Comment
Please, Sign In to add comment