Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- FarmerBot v1.0.0
- Guide
- -----------------
- Crops: Wheat
- -----------------
- Goals:
- Farm down a line of crops and constantly check the crops.
- 2.0 Goals:
- - Allow multiple bots at the same time (designating their name in logs)
- - Attempt to till the ground below it (excluding water)
- - Add more crops
- ]]--
- -- 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", "natura", "growthcraft", "extrautilities", "magicalcrops" }
- -- [[ START PLAYER INPUT ]]--
- local input = { rows = 0, length = 0, crop = 0 }
- term.write("Enter number of rows (left): ")
- input.rows = tonumber(read())
- term.clear()
- term.write("Enter in length of row (forward): ")
- input.length = tonumber(read())
- term.clear()
- for k,v in pairs (crops) do
- print("#", v.id," - ", v.label)
- end
- term.write("Enter crop ID for seeding: ")
- input.crop = tonumber(read())
- term.clear()
- -- [[ END PLAYER INPUT ]]--
- rednet.send(12, "----- " .. os.getComputerLabel() .. " Task -----", "bttp")
- rednet.send(12, " " .. input.rows .. "x" .. input.length, "bttp")
- -- Helper Function: Determine if the crop is fully developed. If not, alert the user.
- function isGrown(row, length, current, expected)
- if current == expected then
- return true
- end
- return false
- end
- -- Helper Function: Determines if the crop ID has any seeds for said crop. Selects seed if true.
- 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
- --refueling
- function refuel(level)
- for i = 1, 16 do
- if turtle.getFuelLevel() > level then
- break
- end
- turtle.select(i)
- if refuel(0) then
- turtle.refuel( math.ceil(turtle.getItemCount()) )
- end
- end
- end
- function dropItems()
- for i = 16,1,-1 do
- turtle.select(i)
- turtle.dropDown()
- end
- end
- -- Network Handling (modem on right)
- 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
- -- 2.0 Function: Attempt to fix an un-tilled grass block
- function fixBlock()
- turtle.digDown()
- if hasSeeds(input.crop) then
- turtle.placeDown()
- end
- end
- -- Helper Function: Finds the table size.
- function getSize(tbl)
- local count = 0
- for _ in pairs(tbl) do count = count + 1 end
- return count
- 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 2: Break the block to find the seed.
- local seedName = ""
- turtle.digDown()
- local unknown = {}
- for i = 1, 16 do
- local data = turtle.getItemDetail(i)
- --Look through each inventory space to find the unknown crop
- for k,v in pairs(crops) do
- if data ~= nil then
- if data.name ~= v.name then
- --Found something not recognized.
- unknown[getSize(unknown) + 1] = data
- end
- 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.
- 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.name
- break
- end
- end
- end
- -- Step 4: Enter in new crop to list of crops.
- local crop = { id = id, mature = 7, name = cropName, seed = seedName, label = cropName }
- crops[getSize(crops) + 1] = crop
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Learned -> " .. cropName, "bttp")
- -- Step 5: Replace seed.
- getSeeds(id)
- turtle.placeDown()
- else
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Unable to learn crop.", "bttp")
- end
- end
- end
- -- Preface: Determine if the bot was placed on the ground. If so, go up one block.
- if turtle.detectDown() then
- turtle.up()
- turtle.forward()
- end
- --[[ START FARMING TASK ]]--
- while true do
- -- Start farming the area.
- for i = 1, input.rows do
- for j = 1, input.length do
- -- Check status of current crop.
- if turtle.detectDown() then
- --There is something below. Determine if it is something we can handle.
- local success, data = turtle.inspectDown()
- if success then
- local isKnown = false
- for k,v in pairs(crops) do
- if data.name == v.name then
- isKnown = true
- if isGrown(i,j,data.metadata,v.mature) then
- --Break the block... Then replace...
- turtle.digDown()
- turtle.suckDown() -- Pick up the local crop.
- 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
- turtle.select(curpos)
- end
- end
- end
- -- 3.0 Change: Have the robot learn the new crop.
- if isKnown == false then
- rednet.send(12, "[" .. os.getComputerLabel() .. "] Learning @ (" .. i .. "," .. j .. ")", "bttp")
- learnCrop(data)
- end
- else
- rednet.send(12, "[" .. os.getComputerLabel() .. "] ERROR: No Block @ (" .. i .. "," .. j .. ")", "bttp")
- end
- else
- 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
- -- Go back to start where chest is.
- 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()
- os.sleep(2)
- local needs = input.rows * input.length + input.rows + input.length + 2
- refuel(needs)
- dropItems()
- os.sleep(300)
- turtle.up()
- turtle.forward()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement