Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --TreeFarmer needs a 25x25 prepared plot and storage chests structure
- local WAR = nil
- local colCnt = 4
- local smeltCnt = 0
- local smeltLoaded = 0
- local tName = os.getComputerLabel()
- local owner = nil
- local function round(num)
- if num >= 0 then
- return math.floor(num+.5)
- else
- return math.ceil(num-.5)
- end
- end
- local function circleSuck()
- --pick up anything around turtle
- for spin = 1, 4 do
- turtle.suck()
- turtle.turnLeft()
- end
- end
- local function determineAction()
- --if empty to the right then plant
- --if sapling ignore
- --anything else harvest
- local success, blockInfo = turtle.inspect()
- local rc = ""
- if success then
- if string.find(blockInfo.name,":sapling") ~= nil then
- --sapling found so do nothing
- rc = "ignore"
- else
- rc = "harvest"
- end
- else
- rc = "plant"
- end
- return rc
- end
- local function plantSapling()
- mta.forward(1)
- circleSuck()
- mta.back(1)
- local sSlot = mta.getItemSlot("sapling")
- if sSlot > -1 then
- turtle.select(sSlot)
- turtle.place()
- end
- end
- local function doubleDig()
- local rc = false
- local xPos = 0
- local x = 0
- for x = 1,3 do
- if turtle.detect() then
- rc = true
- mta.dig()
- mta.forward()
- xPos = xPos + 1
- else
- break
- end
- end
- if xPos > 0 then
- mta.back(xPos)
- end
- return rc
- end
- local function harvest()
- --this is a simple linear implementation
- local harvestedSomething = true
- local zPos = 0
- --dig and move forward. should put turtle squarely under trunk
- mta.dig()
- mta.forward(1)
- while harvestedSomething == true do
- harvestedSomething = false
- --check all four sides and harvest anything found
- for spin = 1, 4 do
- harvestedSomething = doubleDig()
- turtle.turnLeft()
- end
- --check above
- while turtle.detectUp() do
- mta.digUp()
- harvestedSomething = true
- end
- --if found anything at this level then go up a level
- if harvestedSomething == true then
- mta.up(1)
- zPos = zPos + 1
- end
- end
- --go back to ground level
- while zPos > 0 do
- mta.down()
- zPos = zPos - 1
- end
- circleSuck()
- mta.back(1)
- --replant sapling
- local sSlot = mta.getItemSlot("sapling")
- if sSlot > -1 then
- turtle.select(sSlot)
- turtle.place()
- end
- end
- local function dropOffOther()
- local success, blockInfo = turtle.inspect()
- if success then
- if string.find(blockInfo.name,"chest") ~= nil then
- --in front of a chest so drop off anything not sapling or wood
- for xSlot=1, 16 do
- if turtle.getItemCount(xSlot) > 0 then
- turtle.select(xSlot)
- blockInfo = turtle.getItemDetail()
- if blockInfo then
- --if not sapling, log or coal
- if (blockInfo.name ~="minecraft:sapling") and (blockInfo.name ~= "minecraft:log") and (blockInfo.name ~= "minecraft:coal") then
- if turtle.drop() == false then
- print("Unable to offload to 'Other'")
- while turtle.drop() == false do
- sleep(5)
- end
- end
- end
- end
- end
- end
- end
- end
- end
- local function dropOffSaplings()
- local success, blockInfo = turtle.inspect()
- if success then
- if string.find(blockInfo.name,"chest") ~= nil then
- --in front of a chest so drop off anything not sapling or wood
- for xSlot=1, 16 do
- if turtle.getItemCount(xSlot) > 0 then
- turtle.select(xSlot)
- blockInfo = turtle.getItemDetail()
- if blockInfo then
- --if sapling then drop off
- if blockInfo.name =="minecraft:sapling" then
- if turtle.drop() == false then
- print("Unable to offload saplings")
- while turtle.drop() == false do
- sleep(5)
- end
- end
- end
- end
- end
- end
- --now load back up with saplings
- turtle.select(16)
- turtle.suck(colCnt * 4)
- end
- end
- end
- local function dropOffWood()
- local success, blockInfo = turtle.inspect()
- if success then
- if string.find(blockInfo.name,"chest") ~= nil then
- --in front of a chest so drop off anything not sapling or wood
- for xSlot=1, 16 do
- if turtle.getItemCount(xSlot) > 0 then
- turtle.select(xSlot)
- blockInfo = turtle.getItemDetail()
- if blockInfo then
- --if wood then drop off
- if blockInfo.name =="minecraft:log" then
- if turtle.drop() == false then
- print("Unable to offload wood")
- while turtle.drop() == false do
- sleep(5)
- end
- end
- end
- end
- end
- end
- end
- end
- end
- local function loadSmeltSlot()
- local slotAmt = 0
- local dropAmt = 0
- local x = 0
- --try to load
- while smeltLoaded < smeltCnt do
- x = mta.getItemSlot("log")
- if x == -1 then
- --can't find any logs
- print("Smelt loaded not reached but no logs found")
- error()
- else
- turtle.select(x)
- slotAmt = turtle.getItemCount(x)
- if (smeltCnt - smeltLoaded) >= slotAmt then
- dropAmt = slotAmt
- else
- dropAmt = smeltCnt - smeltLoaded
- end
- turtle.drop(dropAmt)
- smeltLoaded = smeltLoaded + dropAmt
- end
- end
- end
- local function loadFuelSlot()
- --drop whatever is left in fuel slot
- local slotAmt = 0
- local x = -1
- --try to load
- x = mta.getItemSlot("log")
- while x > -1 do
- turtle.select(x)
- slotAmt = turtle.getItemCount(x)
- turtle.drop(slotAmt)
- x = mta.getItemSlot("log")
- end
- end
- local function CalcFuelAndSmeltAmts()
- local logCnt = mta.itemInventoryCount("log")
- smeltCnt = round((logCnt/5) * 3)
- smeltLoaded = 0
- end
- local function getOwnerName()
- local name = nil
- --if owner.name file present then extract owner name
- if fs.exists("owner.name") then
- --open and read for user name
- local f = fs.open("owner.name","r")
- if f then
- name = f.readLine()
- end
- f.close()
- end
- return name
- end
- local function sendWirelessMessage(msg, protocol)
- if msg then
- protocal = protocol or giP
- if WAR then
- --send message
- rednet.send(WAR, msg, protocol)
- end
- end
- end
- --*********************MAIN PROGRAM CODE***************
- local action = ""
- --see colCnt parameter at top of file, this sets how many columns to travel
- print("TreeFarm requires 26X26 area")
- print("Plants/harvests 20 trees")
- print("Place coal and saplings in any slot")
- sleep(3)
- if _G["cra"] == false then
- error("CRA API not loaded.")
- end
- if _G["mta"] == false then
- error("MTA API not loaded.")
- end
- owner = getOwnerName()
- local mSide = cra.findPeripheral("modem") --find modem
- if mSide then
- --open modem
- rednet.open(mSide)
- --find id of computer responsible for recieving ore alerts and rebroadcasting to user
- WAR = cra.findWAR()
- mta.setWAR(WAR)
- end
- local wSpaces = 0
- --endless loop
- while true do
- --if don't have min number of saplings then wait
- if mta.itemInventoryCount("sapling") < (4*colCnt) then
- print("More saplings needed")
- if owner then
- sendWirelessMessage(tName.." needs saplings.|"..owner,"TurtleDistress")
- end
- while mta.itemInventoryCount("sapling") < (4*colCnt) do
- sleep(4)
- end
- end
- --fuel check
- if mta.fuelSelfCoal(800) == false then
- print("Fuel Low!")
- if owner then
- sendWirelessMessage(tName.." is low on fuel.|"..owner,"TurtleDistress")
- end
- while mta.fuelSelfCoal(800) == false do
- sleep(4)
- end
- end
- print("Beginning route")
- fuel = turtle.getFuelLevel()
- print("Fuel: "..tostring(fuel))
- for cols = 1, colCnt do
- for targetSpots = 1, 4 do --how many trees per column; this is hardcoded to 4 assuming a 25x25 tree farm
- if targetSpots == 1 then
- wSpaces = 4
- else
- wSpaces = 5
- end
- for walk = 1, wSpaces do
- turtle.suck()
- mta.forward(1)
- end
- if math.fmod(cols,2) ~= 0 then
- turtle.turnRight()
- else
- turtle.turnLeft()
- end
- action = determineAction()
- if action == "plant" then
- plantSapling()
- elseif action == "harvest" then
- harvest()
- end
- if math.fmod(cols,2) ~= 0 then
- turtle.turnLeft()
- else
- turtle.turnRight()
- end
- end
- mta.forward(4)
- --on last column head towards storage otherwise continue on
- if cols ~= colCnt then
- if math.fmod(cols,2) ~= 0 then
- turtle.turnRight()
- mta.forward(5)
- turtle.turnRight()
- else
- turtle.turnLeft()
- mta.forward(5)
- turtle.turnLeft()
- end
- else
- turtle.turnRight()
- mta.forward(13)
- end
- end
- --made a circuit of the field now drop off anything picked up and top off supplies
- --should be at first storage chest which is anything not sapling or wood
- turtle.turnLeft()
- dropOffOther()
- --go to sapling storage
- turtle.turnRight()
- mta.forward(2)
- turtle.turnLeft()
- dropOffSaplings()
- CalcFuelAndSmeltAmts()
- --drop off 3/5 of wood for smelt slot of furnace
- turtle.turnRight()
- mta.up(4)
- loadSmeltSlot()
- --and the rest for fuel slot of furnace
- turtle.turnRight()
- mta.forward()
- mta.down()
- turtle.turnLeft()
- loadFuelSlot()
- --get coal for fuel
- mta.down(3)
- turtle.turnLeft()
- mta.forward()
- turtle.turnRight()
- --get fuel if needed
- --while mta.fuelSelfCoal(800) == false do
- --try to retrieve fuel
- -- print("retrieving fuel...")
- -- while turtle.suck() == false do
- --wait for fuel to arrive
- -- sleep(10)
- -- end
- -- print("fuel retrieved")
- --end
- --get ready to go again...
- turtle.turnRight()
- --...tomorrow at 6:00 am
- local alarm = os.setAlarm(6)
- local sleepyTime = true
- while sleepyTime do
- local evt, arg = os.pullEvent("alarm")
- if arg == alarm then
- sleepyTime = false
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement