da_Aresinger

Michael Reeve's stupid potato farm

Jun 3rd, 2021 (edited)
2,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.29 KB | None | 0 0
  1. -- IF YOU'RE LOST TRY > [program] help
  2.  
  3. --variables
  4. local args = {...}
  5. local combustibles = {'minecraft:coal_block', 'minecraft:coal', 'minecraft:bamboo'}
  6. local seeds = {'minecraft:potato', 'minecraft:wheat_seeds', 'minecraft:carrot', 'minecraft:beetroot_seeds', 'minecraft:nether_wart', 'minecraft:sweet_berries', 'minecraft:cocoa_beans'}
  7. local produce = {'minecraft:potato', 'minecraft:wheat', 'minecraft:carrot', 'minecraft:beetroot', 'minecraft:nether_wart', 'minecraft:sweet_berries', 'minecraft:cocoa_beans'}
  8. local crops = {'minecraft:potatoes', 'minecraft:wheat', 'minecraft:carrots', 'minecraft:beetroots', 'minecraft:nether_wart', 'minecraft:sweet_berry_bush', 'minecraft:cocoa'}
  9. local harvestAge = 7
  10. local sleepTime = 60
  11.  
  12. local latestChanges = 'changes functionality\ntrash functionality\nsetup functionality\nimproved help menu\n\t\t '..
  13.     '\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'
  14. local helpText = {
  15.     'arguments marked \'-t\' terminate program\n'..
  16.     'optional params \'?[...]\' can be ignored\n\n'..
  17.     'info\n\t general information about this \n\t program -t\n\n'..
  18.     'help\n\t this -t',
  19.    
  20.     'update ?[name]\n\t downloads latest version from \n\t pastebin and saves to [name] -t\n\n'..
  21.     'changes \n\t most recent changes -t\n\n'..
  22.     'sleep [number]\n\t seconds to wait between cycles',
  23.    
  24.     'age [number]\n\t growthstages before harvesting a \n\t plant\n\n'..
  25.     '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',
  26.    
  27.     'inspect\n\t use on mature plant for information \n\t about cropId and growthstages -t'
  28. }
  29. local infoText = {'This is a crop farming turtle program inspired by the Michael Reeves Potato Empire. \n'..
  30.     'This program currently supports all vanilla crops and will farm and store produce automatically. \n',
  31.    
  32.     'It is fairly easy to add additional support for modded crops, as long as they follow the vanilla growth model. \n'..
  33.     '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',
  34.    
  35.     'Despite all, there are still many kinks to be ironed out, so check for updates every now and then.\n'..
  36.     'Thanks for checking out this program.\n'..
  37.     '\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'
  38. }
  39.  
  40. --helper methods
  41.     --checks if block to front is crop
  42. local function isCrop()
  43.     local ret, block = turtle.inspect()
  44.     for i, crop in ipairs(crops) do
  45.         if (block ~= nil and block.name == crop) then
  46.             return true
  47.         end
  48.     end
  49.     return false
  50. end
  51.  
  52.     --returns crop age
  53. local function checkCrop()
  54.     ret, block = turtle.inspect()
  55.     if (isCrop()) then
  56.         return block.state.age
  57.     else
  58.         return 0
  59.     end
  60. end
  61.  
  62.     --steps to next crop, returns false if blocked
  63. local function step()
  64.     turtle.turnLeft()
  65.     if (turtle.detect()) then  
  66.         turtle.turnRight()
  67.         return false
  68.     end
  69.     turtle.forward()
  70.     turtle.turnRight()
  71.     return true
  72. end
  73.  
  74.     --returns to home position, must be facing crops
  75.     --turtle will be left facing crop chest
  76. local function home()
  77.     turtle.turnRight()
  78.     while (not turtle.detect()) do
  79.         turtle.forward()
  80.     end
  81. end
  82.  
  83.     --checks whether item is seed
  84. local function isSeed()
  85.     item = turtle.getItemDetail()
  86.     for i, seed in ipairs(seeds) do
  87.         if (item ~= nil and item.name == seed) then
  88.             return true
  89.         end
  90.     end
  91.     return false
  92. end
  93.  
  94.     --selects the next slot with seeds
  95.     -- returns false if there are no seeds and selects slot 1
  96. local function findSeed()
  97.     for i = 1, 16, 1 do
  98.         turtle.select(i)
  99.         if (isSeed()) then
  100.             return true
  101.         end
  102.     end
  103.     turtle.select(1)
  104.     return false
  105. end
  106.    
  107.     --picks up as long as there is something to pick up
  108. local function suckAll()
  109.     while (turtle.suck()) do
  110.     end
  111. end
  112.  
  113.     --runs a single harvest cycle
  114. local function harvest()
  115.    if (checkCrop() >= harvestAge) then
  116.        turtle.dig()
  117.        suckAll()
  118.        if (findSeed()) then
  119.            turtle.place()
  120.        else
  121.            error('no more seeds')
  122.        end
  123.    end
  124. end
  125.  
  126.     --checks if item is produce
  127. local function isProduce()
  128.     item = turtle.getItemDetail()
  129.     for i, prod in ipairs(produce) do
  130.         if (item ~= nil and item.name == prod) then
  131.             return true
  132.         end
  133.     end
  134.     return false
  135. end
  136.    
  137.     --deposits produce to the front and seeds to the top if there is a block (hopefully a chest)
  138.     --produce test takes priority
  139. local function deposit()
  140.     for i = 1, 16, 1 do
  141.         turtle.select(i)
  142.         if (isProduce()) then
  143.             turtle.drop()
  144.         end
  145.         if (isSeed() and turtle.detectUp()) then
  146.             turtle.dropUp()
  147.         end
  148.     end
  149. end
  150.  
  151.     --runs a full farm cycle
  152. local function farm()
  153.     repeat
  154.         harvest()
  155.     until (not step())
  156.     home()
  157.     deposit()
  158.     turtle.turnLeft()
  159. end
  160.                
  161.     --checks item for combustability
  162. local function isCombustible()
  163.     item = turtle.getItemDetail()
  164.     for i, combustible in ipairs(combustibles) do
  165.         if (item ~= nil and item.name == combustible) then
  166.             return true
  167.         end
  168.     end
  169.     return false
  170. end
  171.  
  172.     --checks inventory for combustibles and selects relevant slot
  173.     --returns true when found, false when no combustibles available
  174.     --selects slot 1 if no combustibles available
  175. local function findFuel()
  176.     turtle.select(1)
  177.     found = false
  178.     for i = 1, 16, 1 do
  179.         turtle.select(i)
  180.         found = isCombustible()
  181.         if (found) then
  182.             break
  183.         end
  184.     end
  185.    
  186.     if (not found) then
  187.         turtle.select(1)
  188.     end
  189.     return found
  190. end
  191.  
  192.     --out of fuel
  193. local function noFuel()
  194.     if (turtle.getFuelLevel() == 0) then
  195.         shell.run("label set HELP")
  196.         shell.run('clear')
  197.         print("I am out of fuel")
  198.         print("press any key to continue")
  199.         local event, key = os.pullEvent('key')
  200.         shell.run("label clear")
  201.         shell.run('clear')
  202.         if (findFuel()) then
  203.             turtle.refuel()
  204.         end
  205.     end
  206. end
  207.  
  208.     --collects additional combustibles
  209. local function getFuel()
  210.     turtle.turnLeft()
  211.     while (not turtle.detect()) do
  212.         noFuel()
  213.         turtle.forward()
  214.     end
  215.     if (not findFuel()) then
  216.         turtle.select(16)
  217.     end
  218.     turtle.suck(64 - turtle.getItemCount())
  219.     turtle.turnRight()
  220.     if (not findFuel()) then
  221.         print('current fuelLevel: '..turtle.getFuelLevel())
  222.         error('\ncannot refuel')
  223.     end
  224. end    
  225.  
  226.     --initiates refuel when fuel<800 fueling at minimum to 5000
  227. local function fuelUp()
  228.     if (turtle.getFuelLevel()<800) then
  229.         while (turtle.getFuelLevel()<5000) do
  230.            if (findFuel()) then
  231.                turtle.refuel()
  232.            else
  233.                getFuel()
  234.            end
  235.         end
  236.     end
  237. end
  238.  
  239.     --trashes unusable items
  240. local function trash()
  241.     for i = 1, 16, 1 do
  242.         turtle.select(i)
  243.         if not (isSeed() or isProduce() or isCombustible()) then
  244.             turtle.dropDown()
  245.         end
  246.     end
  247. end
  248.  
  249.     -- collects compatibility information for adding additional crops to the program
  250. local function getBlockData()
  251.     shell.run('clear')
  252.     ret, block = turtle.inspect()
  253.     if (not ret) then
  254.         print('nothing to inspect...')
  255.         return
  256.     end
  257.     print('crop: ' .. block.name)
  258.     if (block.state ~= nil and block.state.age ~= nil) then
  259.         print('current age: ' .. block.state.age)
  260.     else
  261.         print('This does not seem to be a compatible crop')
  262.     end
  263. end
  264.  
  265.     -- sets up the runway and chests
  266. local function setup(length)
  267.     reqfuel = 4*length-4
  268.     shell.run('clear')
  269.         -- ensures there is enough fuel
  270.     while true do
  271.         if (turtle.getFuelLevel() < reqfuel) then
  272.             if findFuel() then
  273.                 turtle.refuel()
  274.             else
  275.                 print('Not enough fuel\nrequired: ' .. reqfuel ..'\ncurrent: '.. turtle.getFuelLevel() ..'\n\t\tpress any key to continue')
  276.                 local event, key = os.pullEvent("key")
  277.                 shell.run('clear')
  278.             end
  279.         else
  280.             break
  281.         end
  282.     end
  283.         -- requests and equips diamond hoe
  284.     local hoe = false
  285.     while not hoe do
  286.         for i=1, 16, 1 do
  287.             turtle.select(i)
  288.             item = turtle.getItemDetail()
  289.             if (item ~= nil and item.name == 'minecraft:diamond_hoe') then
  290.                 hoe = true
  291.                 turtle.equipLeft()
  292.                 local item = turtle.getItemDetail()
  293.                 if (item ~= nil and item.name == 'minecraft:diamond_pickaxe') then
  294.                     turtle.equipRight()
  295.                 end
  296.                 break
  297.             end
  298.         end
  299.         if not hoe then
  300.              print('I need a diamond hoe\n\t\tpress any key to continue')
  301.             local event, key = os.pullEvent("key")
  302.             shell.run('clear')
  303.         end
  304.     end
  305.         -- requests chests
  306.     local chests = false
  307.     while not chests do
  308.         for i=1, 16, 1 do
  309.             turtle.select(i)
  310.             item = turtle.getItemDetail()
  311.             if (item ~= nil and item.name == 'minecraft:chest' and turtle.getItemCount() >= 4) then
  312.                 chests = true
  313.                 break
  314.             end
  315.         end
  316.         if not chests then
  317.             print('I need 4 chests (currently only vanilla)\n\t\tpress any key to continue')
  318.             local event, key = os.pullEvent("key")
  319.             shell.run('clear')
  320.          end
  321.     end
  322.         -- actual setup
  323.     turtle.digDown('right')
  324.     turtle.placeDown()
  325.     turtle.digUp('right')
  326.     turtle.placeUp()
  327.     turtle.turnRight()
  328.     turtle.dig('right')
  329.     turtle.place()
  330.     turtle.turnLeft()
  331.     turtle.dig('right')
  332.     turtle.dig('left')
  333.     turtle.turnLeft()
  334.     for i = 1, length - 1, 1 do
  335.         turtle.dig('right')
  336.         turtle.forward()
  337.         turtle.turnRight()
  338.         turtle.dig('right')
  339.         turtle.dig('left')
  340.         turtle.turnLeft()
  341.     end
  342.     turtle.dig('right')
  343.     turtle.place()
  344.     turtle.turnRight()
  345.     home()
  346.         -- clean inventory
  347.     deposit()
  348.     turtle.turnLeft()
  349.     trash()
  350.         -- unequip hoe
  351.     for i = 1, 16, 1 do
  352.         if turtle.getItemCount() == 0 then
  353.             turtle.equipLeft()
  354.             turtle.dropDown()
  355.             break
  356.         end
  357.     end
  358.         -- request seeds
  359.     turtle.suckUp()
  360.     local seeds = false
  361.     while not seeds do
  362.         if (findSeed() and turtle.getItemCount() >= length) then
  363.             seeds = true
  364.             for i = 1, length - 1, 1 do
  365.                 turtle.place()
  366.                 turtle.turnLeft()
  367.                 turtle.forward()
  368.                 turtle.turnRight()
  369.             end
  370.             turtle.place()
  371.         else
  372.             print('I need '.. length ..' seeds (currently only vanilla)\n\t\tpress any key to continue')
  373.             local event, key = os.pullEvent("key")
  374.             shell.run('clear')
  375.         end
  376.     end
  377. end
  378.  
  379.     -- player input exit request
  380. local function exitRequest()
  381.     local event, key = os.pullEvent("key")
  382.     shell.run('clear')
  383.     return (x == 'e' or x == 'q')
  384. end
  385.  
  386.     -- help menu
  387. local function help()
  388.     local pages = table.getn(helpText)
  389.     for key, page in pairs(helpText) do
  390.         shell.run('clear')
  391.         print('Help ('.. key ..'/'.. pages ..') - e/q to exit, other keys to continue\n')
  392.         print(page)
  393.         if exitRequest() then return end
  394.     end
  395. end
  396.  
  397.     -- prints general info page
  398. local function info()
  399.     local pages = table.getn(infoText)
  400.     for key, page in pairs(infoText) do
  401.         shell.run('clear')
  402.         print('Info ('.. key ..'/'.. pages ..') - e/q to exit, other keys to continue\n')
  403.         print(page)
  404.         if exitRequest() then return end
  405.     end
  406. end
  407.  
  408.     -- updates the program
  409. local function update(name)
  410.     if(fs.exists(name)) then
  411.         print(name .. ' exists, using temp')
  412.         shell.run('pastebin get BbMyDvEx temp')
  413.         if (fs.exists('temp')) then
  414.             print('replacing ' .. name .. ' with temp')
  415.             fs.delete(name)
  416.             fs.move('temp', name)
  417.             if (fs.exists(name)) then
  418.                 print(name .. ' updated successfully')
  419.             else
  420.                 error('Error: could not update correctly. Check files for [temp] and [' .. name .. ']')
  421.             end
  422.         end
  423.     else
  424.         print('changing program name to ' .. name)
  425.         shell.run('pastebin get BbMyDvEx ' .. name)
  426.         if (fs.exists(name)) then
  427.             fs.delete(debug.getinfo(1).source:match('[^@]*$'))
  428.         else
  429.             error('update failed - maintaining old version')
  430.         end
  431.     end
  432.    
  433. end
  434.  
  435.     -- interprets args
  436. local function checkArgs()
  437.     i = 1
  438.     while (args[i] ~= nil) do
  439.         arg = args[i]
  440.         i = i+1
  441.         if (arg == 'sleep' and args[i] ~= nil) then
  442.             arg = tonumber(args[i])
  443.             if (arg ~= nil) then
  444.                 print('setting sleep time to '..arg)
  445.                 sleepTime = arg
  446.                 i = i+1
  447.                 sleep(2)
  448.             else
  449.                 print('arg' .. i - 1 .. ' discarded - try [program] help')
  450.                 return false
  451.             end
  452.         elseif (arg == 'age' and args[i] ~= nil) then
  453.             arg = tonumber(args[i])
  454.             if (arg ~= nil) then
  455.                 print('setting harvestAge to '..arg)
  456.                 harvestAge = arg
  457.                 i = i+1
  458.                 sleep(2)
  459.             else
  460.                 print('arg' .. i - 1 .. ' discarded - try [program] help')
  461.                 return false
  462.             end
  463.         elseif (arg == 'setup' and args[i] ~= nil) then
  464.             arg = tonumber(args[i])
  465.             continue = (args[i+1] ~= nil and args[i+1] == 'continue')
  466.             if continue then i = i+1 end
  467.             if (arg ~= nil) then
  468.                 setup(arg)
  469.                 i = i+1
  470.                 if not continue then
  471.                     return false
  472.                 end
  473.             else
  474.                 print('arg' .. i - 1 .. ' discarded - try [program] help')
  475.                 return false
  476.             end
  477.         elseif (arg == 'inspect') then
  478.             getBlockData()
  479.             return false
  480.         elseif (arg == 'help') then
  481.             help()
  482.             return false
  483.         elseif (arg == 'info') then
  484.             info()
  485.             return false
  486.         elseif (arg == 'update') then
  487.             if (args[i] ~= nil) then
  488.                 update(args[i])
  489.             else
  490.                 update(debug.getinfo(1).source:match('[^@]*$'))
  491.             end
  492.             return false
  493.         elseif (arg == 'changes') then
  494.             print(latestChanges)
  495.             return false
  496.         else
  497.             print('arg' .. i - 1 .. ' discarded - try [program] help')
  498.             return false
  499.         end
  500.     end
  501.     return true
  502. end
  503.  
  504.  
  505. --init
  506. shell.run('clear')
  507.  
  508. if (not checkArgs()) then
  509.     return
  510. end
  511.  
  512. shell.run('clear')
  513. 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.\"')
  514.  
  515. --main loop
  516. while (true) do
  517.     fuelUp()
  518.     farm()
  519.     trash()
  520.     sleep(sleepTime)
  521. end
Advertisement
Add Comment
Please, Sign In to add comment