Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FarmerBot v1.0.0
- -- Banned Mods --
- Natura > This mod was not properly built in the sense that the bot cannot understand when crops are done growing.
- HarvestCraft > This mod does not work well with the learning system. I will keep it in, but it will struggle with any harvestcraft crops.
- -- Instructions --
- 1) Place your bot on top of a chest, which is one space below the bottom-right of the farm.
- 2) Run this script.
- 3) Enter in how many rows there are (how many rows are to the left?)
- 4) Enter in how long each row is (how far does the bot have to go down the row)
- 5) Enter in what seed the bot will place on empty farmland.
- -- Side Notes --
- ~ The bot will stop working properly if you do not give it more fuel.
- ~ The bot will dynamically learn new props from the mod list.
- ~ The bot will refuel automatically if it runs into a fuel source (like feeding it a lava bucket in a hopper) before it goes again.
- ~ This script will give detailed information on each newly learned crop.
- -- Future Plans --
- ~ The bot will the ability to ignore a specific seed (enter in 0 instead of a seed ID) and place whatever it has in its inventory.
- -- Known Issues --
- ~ For some reason, after the first go, the bot will randomly go off the grid. This may be a rogue .turnLeft() call.
- ]]--
- -- Globals --
- local crops = {
- { id = 1, mature = 7, name = "minecraft:carrots", seed = "minecraft:carrot", label = "Carrots" },
- { id = 2, mature = 7, name = "minecraft:wheat", seed = "minecraft:wheat_seeds", label = "Wheat" },
- { id = 3, mature = 7, name = "minecraft:potatoes", seed = "minecraft:potato", label = "Potatoes" },
- { id = 4, mature = 7, name = "magicalcrops:MinicioCrop", seed = "magicalcrops:MinicioSeeds", label = "Minicio" },
- { id = 5, mature = 7, name = "magicalcrops:FireCrop", seed = "magicalcrops:FireSeeds", label = "Fire" },
- { id = 6, mature = 7, name = "magicalcrops:SheepCrop", seed = "magicalcrops:SheepSeeds", label = "Sheep" },
- { id = 7, mature = 7, name = "magicalcrops:NatureCrop", seed = "magicalcrops:NatureSeeds", label = "Nature" },
- { id = 8, mature = 7, name = "magicalcrops:FluixCrop", seed = "magicalcrops:FluixSeeds", label = "Fluix" },
- { id = 9, mature = 7, name = "magicalcrops:QuartzCrop", seed = "magicalcrops:QuartzSeeds", label = "Quartz" },
- { id = 10, mature = 7, name = "magicalcrops:WaterCrop", seed = "magicalcrops:WaterSeeds", label = "Water" },
- { id = 11, mature = 7, name = "magicalcrops:NetherCrop", seed = "magicalcrops:NetherSeeds", label = "Nether" },
- { id = 12, mature = 7, name = "magicalcrops:EarthCrop", seed = "magicalcrops:EarthSeeds", label = "Earth" },
- { id = 13, mature = 7, name = "magicalcrops:DiamondCrop", seed = "magicalcrops:DiamondSeeds", label = "Diamond" }
- }
- local mods = { "harvestcraft", "botania", "growthcraft", "extrautilities", "magicalcrops", "witchery" }
- -- [[ START PLAYER INPUT ]]--
- local input = { rows = 0, length = 0, crop = 0 }
- term.write("Enter number of rows (left): ")
- input.rows = tonumber(read())
- term.write("Enter in length of row (forward): ")
- input.length = tonumber(read())
- for k,v in pairs (crops) do
- print("#", v.id," - ", v.label)
- end
- term.write("Enter crop ID (or 0) for seeding: ")
- input.crop = tonumber(read())
- -- [[ END PLAYER INPUT ]]--
- --[[ refuel()
- Task: To check for any fuel in the slots and consume it.
- ]]--
- function refuel()
- for i = 16,1,-1 do
- turtle.select(i)
- turtle.refuel( math.ceil(turtle.getItemCount()) )
- end
- end
- --[[ dropItems()
- Task: Select each item and put the item into the chest below.
- ]]--
- function dropItems()
- for i = 16,1,-1 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- --[[ handleModem()
- Task: Allow the bot to remotely speak to a specific computer.
- ]]--
- function handleModem()
- if rednet.isOpen("right") then
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected to modem!", "bttp")
- else
- rednet.open("right")
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Turning on modem...", "bttp")
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Connected successfully!", "bttp")
- end
- end
- --[[ fixBlock()
- Task: Place a seed crop based on <input.crop>, or any seed if no specific crop.
- ]]--
- function fixBlock()
- turtle.digDown()
- -- If the crop is 0, then the user wants the bot to place any seeds in the inventory.
- if input.crop == 0 then
- for k,v in pairs(crops) do
- if hasSeeds(v.id) then
- turtle.placeDown()
- break
- end
- end
- else
- -- The user has selected a specific crop.
- if hasSeeds(input.crop) then
- turtle.placeDown()
- end
- end
- end
- -- 3.0 Function: Have the bot learn crops instead of manually entering them.
- function learnCrop(cropBlock)
- -- Determine if the block is in any of the mods we listed.
- for i = 1, getSize(mods) do
- if string.match( string.lower(cropBlock.name) , string.lower(mods[i]) ) then
- -- Step 1: Get the current list of crops and make the ID.
- local id = getSize(crops) + 1
- local cropName = cropBlock.name
- -- Step 1b: Learn the maturity...
- local maturity = 7
- if cropBlock ~= nil then maturity = cropBlock.metadata end
- -- Step 2: Break the block to find the seed.
- turtle.digDown()
- local unknown = {}
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- --Look through each inventory space to find the unknown crop
- local isKnown = false
- for k,v in pairs(crops) do
- if data ~= nil then
- -- The crop is known, therefore skip it.
- if data.name == v.seed then
- isKnown = true
- end
- end
- end
- if isKnown == false then
- local checkDuplicates = false
- if data ~= nil then
- for j=1, getSize(unknown) do
- if unknown[j].name == data.name then
- checkDuplicates = true
- end
- end
- end
- if checkDuplicates == false then
- unknown[getSize(unknown) + 1] = data
- end
- end
- end
- -- Step 3: Find the seed using the unknown blocks in the turtle.
- -- Quick Logic: If only one new item is found, then that is the seed.
- -- Other quick logic: If more than one (probably 2 at most) then check for 'seed' in text.
- local seedName = ""
- getSize(unknown)
- if getSize(unknown) == 1 then
- seedName = unknown[1].name
- else
- for i=1,getSize(unknown) do
- if string.match( string.lower(unknown[i].name), string.lower("seed") ) then
- seedName = unknown[i].name
- break
- end
- end
- end
- -- Step 4: Enter in new crop to list of crops.
- local crop = { id = id, mature = maturity, name = cropName, seed = seedName, label = cropName }
- crops[getSize(crops) + 1] = crop
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
- print(">> NEW CROP <<")
- print("Crop: ", crop.name)
- print("Seed:" , crop.seed)
- print("Maturity: ", crop.mature)
- -- Step 5: Replace seed.
- if hasSeeds(id) then
- turtle.placeDown()
- else
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Cannot replace seed: " .. cropName, "bttp")
- end
- end
- end
- end
- --[[ Helper Functions ]]--
- function getSize(tbl)
- local count = 0
- if tbl == nil then return count end
- for _ in pairs(tbl) do count = count + 1 end
- return count
- end
- function isGrown(row, length, current, expected)
- if current == expected then
- return true
- end
- return false
- end
- function hasSeeds(id)
- for k,v in pairs(crops) do
- if v.id == id then
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- if data ~= nil then
- if data.name == v.seed then
- turtle.select(i)
- return true
- end
- end
- end
- end
- end
- return false
- end
- --[[ End Helper Functions ]]--
- --[[ START FARMING TASK ]]--
- -- Adjust bot placement.
- if turtle.detectDown() then
- turtle.up()
- turtle.forward()
- end
- -- Make sure the bot is connected to the network.
- handleModem()
- -- Declare settings entered so user can confirm.
- rednet.send(12, "----- " .. os.getComputerLabel() .. " Task -----", "bttp")
- rednet.send(12, "" .. input.rows .. "x" .. input.length, "bttp")
- while true do
- for i = 1, input.rows do
- for j = 1, input.length do
- -- Determine if there is a crop below the bot.
- if turtle.detectDown() then
- local success, data = turtle.inspectDown()
- if success then
- -- Bot AI: Determine if the crop is known, or if its even a crop.
- local isKnown = false
- if data.name ~= "minecraft:cobblestone" or data.name ~= "minecraft:water" or data.name ~= "minecraft:cobblestone_slab" then
- for k,v in pairs(crops) do
- if data.name == v.name then
- isKnown = true
- if isGrown(i,j,data.metadata,v.mature) then
- -- The crop is fully matured. Need to break crop and replace with seed.
- turtle.digDown()
- turtle.suckDown()
- local curpos = turtle.getSelectedSlot();
- if hasSeeds(v.id) then
- turtle.placeDown()
- else
- rednet.send(12, "[" .. os.getComputerLabel() .. "] No seeds for " .. v.label .. " @ (" .. i .. "," .. j .. ")", "bttp")
- end
- -- Legend: "Reset the positioning of the selected slot so bot can stack crops properly."
- turtle.select(curpos)
- end
- end
- end
- -- If the crop is unknown, have the bot attempt to learn the crop.
- if isKnown == false then
- learnCrop(data)
- end
- end
- else
- rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
- end
- else
- -- The block below does not have a crop. Attempt to remedy this.
- fixBlock()
- end
- -- Go forward one to continue.
- if j < input.length then
- turtle.forward()
- end
- end
- -- Done reviewing the row. Now move to new row.
- local isEven = i % 2 == 0
- if isEven == false then
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- else
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- end
- end
- -- Goal: Attempt to go back to start.
- -- Logic: If the number of rows to the left are even, then the bot will end up on a different side.
- if input.rows % 2 == 1 then
- for k = 1, input.length do
- turtle.forward()
- end
- else
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- end
- turtle.turnLeft()
- for l = 1, input.rows do
- turtle.forward()
- end
- turtle.turnLeft()
- -- Drop items in hopper below.
- turtle.down()
- -- Attempt to refuel if refueling is setup.
- os.sleep(2)
- refuel()
- -- Drop all items in the inventory into chest below.
- dropItems()
- -- Wait for crops to grow a bit.
- os.sleep(300)
- -- Reset bot position for next harvest.
- turtle.up()
- turtle.forward()
- end
- --[[ END FARMING TASK ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement