Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --{program="MJZ FarmBot",version="1.5"}
- ---------------------------------------
- -- 9.8.23 v1.0 initial
- -- 10.8.23 v1.1 improvements
- -- 10.8.23 v1.2 improvements on having multiple turtles and unique save files
- -- v1.3 added dropdown in bottom chest, added repeat loop function and timer
- -- 11.8.23 v1.4 General code improvements, torches calculation,
- -- 12.8.23 v1.5 Working now for even and uneven farm size, configurable timer
- -- More UX / UI improvements
- ---------------------------------------
- -- ToDo:
- -- announce available update, refuel from chest on top
- ---------------------------------------
- ---- DESCRIPTION ----------------------
- ---------------------------------------
- -- FarmBot: Modular Automated Turtle
- -- wheat farm (farming turtle)
- -- Details see information during
- -- program execution.
- -- Questions? Check on
- -- https://pastebin.com/P4Zcvj9B
- -- Please credit me if you use my
- -- program <3 <3 <3
- ---------------------------------------
- ---- PARAMETERS -----------------------
- ---------------------------------------
- local cVersion ="1.5"
- local cPrgName ="MJZ FarmBot"
- local debugging=false
- local slotFuel=16 -- where to put fuel
- local minFuel=300 -- the minimal amount of fuel required
- local sleepTime=400 -- the amount of time the turtle will wait until running another run
- -- Full name of crop as item and block and ripeness:
- local config = {
- crop = {
- block = "minecraft:wheat",
- item = "minecraft:wheat_seeds",
- ripe = 7 -- Default: 7 (crops have grow stages between 0 and 7)
- },
- }
- --[[
- /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
- Do not change anything below if you
- don't want the code to break!
- /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
- ]]--
- ---------------------------------------
- ---- VARIABLES ------------------------
- ---------------------------------------
- local width
- local length
- local time = os.time()
- local formattedTime = textutils.formatTime(time, true)
- local blnShowUsage=false
- local blnAskForParameters=true
- local blnAutoSetup=false
- local strSimpleCheck="Press enter to start: "
- local intCounter=0
- local maxCounter=0
- local fenceNeeded=0
- local torchesNeeded=0
- -- Dictionary of block names and its effects on the turtle:
- local BlockList = {}
- local computerID = os.getComputerID()
- local filename = "VarsFarmBot_" .. tostring(computerID)
- local skipConfig=false
- local sleepSeconds
- ---------------------------------------
- ---- tArgs ----------------------------
- ---------------------------------------
- local tArgs = {...}
- if #tArgs >= 1 then -- no error check
- blnAskForParameters=false
- if tArgs[1]=="help" then blnShowUsage=true end
- if tArgs[1]=="setup" then blnAutoSetup=true end
- if tArgs[1]=="set-up" then blnAutoSetup=true end
- if tonumber(tArgs[1])~=nil then
- maxCounter=tonumber(tArgs[1])
- end
- end
- -- Function to print debug information
- local function debug_print(txt, ...)
- if debugging then
- io.write(string.format(txt .. "\n", ...))
- end
- end
- -- Add blocks to check for to BlockList dictionary:
- local function newBlock(blockName, blockFunction)
- local temp = {
- block = blockName,
- fn = blockFunction
- }
- BlockList[blockName] = blockFunction
- return temp
- end
- ---------------------------------------
- -- BASIC FUNCTIONS FOR TURTLE CONTROL -
- ---------------------------------------
- local function writeFile()
- local handle = fs.open(filename, "w")
- handle.write(tostring(length).."\n")
- handle.write(tostring(width).."\n")
- handle.write(tostring(fenceNeeded).."\n")
- handle.write(tostring(torchesNeeded).."\n")
- handle.write(tostring(minFuel).."\n")
- handle.close()
- end
- local function readFile()
- local handle = fs.open(filename, "r")
- length = tonumber( handle.readLine() )
- width = tonumber( handle.readLine() )
- fenceNeeded = tonumber( handle.readLine() )
- torchesNeeded = tonumber( handle.readLine() )
- minFuel = tonumber( handle.readLine() )
- handle.close()
- end
- local function askForInputText(textt)
- local at="" -- check prompting texts
- if textt==nil then textt="Enter text:" end -- ask for input
- write(textt)
- at=read()
- return at
- end
- -- function for checking fuel
- local function checkFuel()
- local tmp=turtle.getFuelLevel()
- return tmp
- end
- -- Function for turtle behaviour
- local function ss(s) turtle.select(s) debug_print("Selecting Slot") end
- local function ff() turtle.forward() debug_print("Moving Forward") end
- local function bb() turtle.back() debug_print("moving backward") end
- local function up() turtle.up() debug_print("moving up") end
- local function dn() turtle.down() debug_print("moving down") end
- local function tl() turtle.turnLeft() debug_print("turning left") end
- local function tr() turtle.turnRight() debug_print("turning right") end
- local function dd() turtle.digDown() debug_print("digging down") end
- local function pu() turtle.placeUp() debug_print("placing up") end
- local function pd() turtle.placeDown() debug_print("placing down") end
- local function cd() turtle.detectDown() debug_print("detecting down") end
- local function sk() turtle.suckDown() sleep(0.1) turtle.suckDown() debug_print("gathering items") end
- local function Du(n) if n==nil then n=64 end turtle.dropUp(n) debug_print("dropping up") end
- local function Dd(n) if n==nil then n=64 end turtle.dropDown(n) debug_print("dropping down") end
- local function wt() write(". ") end
- local strSimpleCheck="Press enter to continue:"
- local success, data = turtle.inspectDown()
- local function dps()
- for i=2,16 do
- ss(i) Dd()
- end
- end
- local function checkRefuel(minFuel, slotFuel)
- if slotFuel==nil then slotFuel=16 end
- if minFuel==nil then minFuel=1000 end
- local tmpFuel=0 tmpFuel2=0
- local tmpItems=65 --turtle.getItemCount(slotFuel)
- local cSleep=5
- -- step 1 check if more fuel is required
- tmpFuel=turtle.getFuelLevel()
- if fuelLevel == "unlimited" then
- return true
- end
- tmpFuel2=tmpFuel-1 -- triggers print at least once
- if tmpFuel<minFuel then
- ss(slotFuel)
- -- step 2 refuel loop
- while tmpFuel<minFuel do
- -- step 2.1 need to update fuel level?
- if tmpFuel2~=tmpFuel then
- -- fuel still too low and there have been items consumed
- print("Need more fuel ("..tmpFuel.."/"..minFuel..") in slot "..slotFuel)
- end
- -- step 2.2 try to refuel
- if tmpItems>0 then
- -- try to refuel this items
- turtle.refuel()
- else
- os.sleep(cSleep)
- end
- -- step 2.3 update variables
- tmpItems=turtle.getItemCount(slotFuel)
- tmpFuel2=tmpFuel
- tmpFuel=turtle.getFuelLevel()
- end
- end
- -- step 3 either no need to refuel
- -- or successfully refuelled
- print("| Fuel level ok ("..tmpFuel.."/"..minFuel..")") term.setCursorPos(39, 10) print ("|")
- end
- -- Tilling Seeding Harvesting
- local function isCropReadyToHarvest(block)
- return block.state.age >= config.crop.ripe
- end
- -- Function to plant a crop
- local function plantCrop()
- local currentItem = turtle.getItemDetail()
- if currentItem and currentItem.name == config.crop.item then
- debug_print("Placing down crop!")
- pd()
- sk()
- return
- end
- debug_print("Locating crop to replant...")
- for i = 1, 16 do
- turtle.select(i)
- local newItem = turtle.getItemDetail()
- if newItem and newItem.name == config.crop.item then
- debug_print("Placing down crop!")
- pd()
- sk()
- return
- end
- end
- -- Failed to replant:
- debug_print("Could not locate crop in inventory to replant... carrying on.")
- end
- -- Function to handle harvesting and replanting
- local function hr()
- local success, data = turtle.inspectDown()
- if success and data.name == config.crop.block then
- local block = data
- debug_print("Crop found...")
- if isCropReadyToHarvest(block) then
- debug_print("Ready to harvest!")
- dd()
- plantCrop()
- sk()
- else
- debug_print("Still growing...")
- sk()
- end
- else
- debug_print("There isn't a crop under me: tilling.")
- dd()
- plantCrop()
- end
- end
- local function placeFence()
- pd() ff()
- end
- local function placeFenceAndTorch()
- pd() up() ss(3) pd() ss(2) ff() dn()
- end
- local function placeCorner()
- pd() up() ss(3) pd() ss(2) tr() ff() dn()
- end
- local function placeTorchesAndFences(count)
- placeCorner()
- for i = count,1,-1 do
- if i % 3 == 0 and i > 1 and i <= count - 1 then
- placeFenceAndTorch()
- count = count - 1
- else
- placeFence()
- end
- end
- end
- ---------------------------------------
- ---- set-up farm ----------------------
- ---------------------------------------
- -- Check if tutorial is needed
- if blnShowUsage then
- term.clear() term.setCursorPos(1,1)
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ |")
- print("+-------------------------------------+")
- print("Usage:")
- print("FarmBot [setup]")
- print(" Will start auto setup")
- print(" or:")
- print("FarmBot [maxCounter]")
- print(" 0=will farm infinitely")
- print(" x=will farm x rounds")
- print("FarmBot [help]")
- print(" Will show this message again")
- return
- end
- -- Check if autosetup false and if VarsFarmBot exists
- if blnAutoSetup==false then
- if fs.exists(filename) then
- if tonumber(tArgs[1])~=nil then
- shouldExit = true
- end
- if not shouldExit then
- term.clear() term.setCursorPos(1,1)
- readFile()
- print ("File Loaded")
- print("+-------------------------------------+")
- write("Farmsize: ") write("Length = ") write(length) write(" Width = ") print(width)
- print ("Using Stored Variables")
- textutils.slowPrint ("Is that ok? [y/n]")
- local input = read()
- if input=="y" then
- sleep(1)
- write ("Auto Setup ") print (blnAutoSetup)
- sleep(0.5)
- print (" Starting the Farm")
- elseif input=="n" then blnAutoSetup=true
- print("No")
- write ("Auto Setup set to ") print (blnAutoSetup)
- sleep(1)
- else
- print("Wrong answer. Please try again.")
- return
- end
- end
- else
- term.clear() term.setCursorPos(1,1)
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ |")
- print("+-------------------------------------+")
- textutils.slowPrint ("Invalid Arguments")
- sleep(0.2)
- print ("Please run the Setup or type ")
- print(" FarmBot [help]")
- print ("without the brackets for more info")
- textutils.slowPrint ("............")
- return
- end
- end
- -- Check if autosetup true
- if blnAutoSetup then
- write("Setting up farm...")
- sleep(1)
- -- step 1 get size
- term.clear() term.setCursorPos(1,1)
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ (1/3)|")
- print("+-------------------------------------+")
- print("Welcome to FarmBot for ComputerCraft")
- print("Let's prepare the farm.")
- textutils.slowPrint ("What's the length?")
- print("Type the number and press enter")
- length = read()
- write("Ok.")
- print("+-------------------------------------+")
- textutils.slowPrint("What's the width?") write ("going to the right side)")
- print("Type the number and press enter")
- width = read()
- print("Okay, let's prepare the farm")
- fenceNeeded = ((length+2)*2+width*2)-1
- local function calculateTorchAmount(sideLength)
- return math.floor((sideLength - 1) / 3)
- end
- torchesNeeded=calculateTorchAmount(length)+calculateTorchAmount(width)+calculateTorchAmount(length)+calculateTorchAmount(width-1)+4
- minFuel = (fenceNeeded*3+(length + width * 2)*4)
- sleep(1)
- -- writing the variables to the file
- writeFile()
- print("+-------------------------------------+")
- print("Farmsize: ") write("Length = ") print(length) write("Width = ") print(width)
- write("Sqm = ") print(length*width)
- for i=1,3 do
- write(". ")
- sleep(0.2)
- end
- -- sleep(1)
- end
- -- step 2 Final check and Setup
- if blnAutoSetup then
- repeat
- term.clear() term.setCursorPos(1,1)
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ (2/3)|")
- print("+-------------------------------------+")
- print("| 1.Farm set-up: Place farming turtle |")
- print("| down (bottom left corner of farm) |")
- print("| It needs a hoe on the side! |")
- print("| |")
- print("| Materials needed for auto set-up: |")
- print("| slot 2: fences ("..fenceNeeded..")")
- term.setCursorPos(39,9) print("|") term.setCursorPos(1,10)
- print("| slot 3: torches ("..torchesNeeded..")")
- term.setCursorPos(39,10) print("|") term.setCursorPos(1,11)
- print("| slot 4: chest (2) |")
- print("+-------------------------------------+")
- if blnAutoSetup then
- if turtle.getItemCount(4)<2 or turtle.getItemCount(2)<fenceNeeded or turtle.getItemCount(3)<torchesNeeded then
- -- inventory not ready for set-up
- strSimpleCheck="Fill in slots 2-4 and press enter:"
- else
- strSimpleCheck="Press enter to start:"
- end
- else
- strSimpleCheck="Press enter to start:"
- end
- if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
- until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
- end
- term.clear() term.setCursorPos(1,1)
- if blnAutoSetup then
- -- end of theorical Setup
- print("Setup starting...")
- print("Wait for the setup to happen and run")
- print("FarmBot [maxCounter]")
- print("Setting up the farm...")
- sleep(1)
- checkRefuel(minFuel,slotFuel)
- ----- Do the job
- ----- Place Chests
- term.clear() term.setCursorPos(1,1)
- repeat
- print("+-------------------------------------+")
- print("| |")
- print("| I don't have a pick, can you |")
- print("| Dig 1 Bloc Under the Turtle |")
- print("| |")
- print("| |")
- print("| Please ;) |")
- print("| |")
- print("| |")
- print("| |")
- print("| |")
- print("+-------------------------------------+")
- if turtle.detectDown()==true then end
- until turtle.detectDown()==false
- print("| Thank you <3 |")
- print("| |")
- print("| |")
- print("| Placing Chests |")
- print("| |")
- ss(4) write("|. ") pd() write(". ") up() write(". ") pu() write(". ") ss(2) write(". ") tl() write(". ") ff() write(". ")
- print("| |")
- print("| Placing Fences |")
- -- Place Fences
- placeTorchesAndFences(length)
- placeTorchesAndFences(width)
- placeTorchesAndFences(length)
- placeTorchesAndFences(width-1)
- -- Empty Useless Mats
- for i = 2, 4 do
- ss(i)
- Du()
- wt()
- end
- tr() dn()
- print("| |")
- print("| |")
- print("| |")
- print("| Done ! |")
- print("| |")
- print("| |")
- print("| Ready for Farming Program |")
- print("| You can now run the Farm |")
- print("+-------------------------------------+")
- return end
- ---------------------------------------
- ---- main -----------------------------
- ---------------------------------------
- repeat
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ (3/3)|")
- print("+-------------------------------------+")
- print("| 2. Running the farm: |")
- print("| Farming turtle sits above chest |")
- print("| (as after running set-up). |")
- print("| Please empty the top chest and add|")
- print("| fuel into it. |")
- print("| |")
- print("|Turtle inventory: |")
- print("| slot 1: seeds (2+x) |")
- print("+-------------------------------------+")
- if turtle.getItemCount(1)<2 then
- -- inventory not ready
- strSimpleCheck="Provide seeds in slot 1 and press enter:"
- else
- strSimpleCheck="Press enter to start:"
- end
- if not blnAskForParameters and strSimpleCheck=="Press enter to start:" then break end
- until askForInputText(strSimpleCheck)=="" and strSimpleCheck=="Press enter to start:"
- ---------------------------------------
- ---- run -----------------------------
- ---------------------------------------
- local repeatTimes = tonumber(tArgs[1])
- print("| Running the farm " ..repeatTimes.. " times ")
- if repeatTimes == 0 then
- repeatTimes = math.huge -- Repeat indefinitely
- elseif not repeatTimes then
- repeatTimes = 1
- end
- for a = 1, repeatTimes do
- local function Waiting()
- if a < repeatTimes then
- for i = 1, sleepTime do
- sleep(1)
- term.setCursorPos(1, 12) -- Adjust the y-coordinate to match your desired position
- local sleepSeconds = i
- if sleepSeconds < 10 then
- sleepSeconds = "0" .. sleepSeconds
- end
- print("| Sleep " .. sleepSeconds .." of " .. sleepTime .. "") term.setCursorPos(39, 12) print ("|")
- end
- end
- end
- if debugging then
- print("Debugging: enabled")
- end
- readFile()
- term.clear() term.setCursorPos(1,1)
- print("+-------------------------------------+")
- print("|"..cPrgName.." "..cVersion..",by MaxwellJoneZ |")
- print("+-------------------------------------+")
- print("| |")
- print("| |")
- print("| Run N° "..a.. " of " ..repeatTimes.."") term.setCursorPos(39, 6) print ("|")
- print("| |")
- print("| |")
- print("| |")
- checkRefuel(minFuel,slotFuel)
- print("| |")
- up() ff() hr()
- local n=0
- for i=1,width-1 do
- n=n+1
- for i=1,length-1 do
- ff() hr()
- end
- if(n%2==0)
- then tl() ff() hr() tl()
- else tr() ff() hr() tr()
- end
- end
- for i=1,length-1 do
- ff() hr()
- end
- -- then go back
- if(width%2==0) then
- tr()
- for i=1,width-1 do
- ff()
- end
- tr() bb() dn() dps() Waiting()
- else
- for i=1,length-1 do
- bb()
- end
- tr()
- for i=1,width-1 do
- bb()
- end
- tl() bb() dn() dps() Waiting()
- end
- end
Add Comment
Please, Sign In to add comment