Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ************ TreeFarm **************
- -- * 1.6 fixed bug: everthing in slot 1 can be fuel
- -- * 1.5 fixed bug: crash when 1x1 tree in 2x2 farm
- -- * 1.4 track support in progress (still not functional)
- -- * 1.3 fixed bug in go()
- -- * 1.2 fixed sapling collection at the sides, code refactor, 2x2 tree girth support
- -- * 1.1 inventroy fix, intro sign, wood counter, timer
- -- * 1.0 release!
- -- ************************************
- verNum = "v1.6"
- local startTime = os.clock()
- local tArgs = { ... }
- local delay = 0.25
- local level = 1
- local nextTurn = 0
- local mode
- local nWood
- local screenX, screenY = term.getSize()
- local tTurns = {
- [0] = turtle.turnRight,
- [1] = turtle.turnLeft,
- }
- local forward = {
- go = turtle.forward,
- dig = turtle.dig,
- detect = turtle.detect,
- inspect = turtle.inspect,
- attack = turtle.attack,
- }
- inspect = function ()
- return {false, ""}
- end
- local back = {
- go = turtle.back,
- inspect = function () return false end,
- }
- local up = {
- go = turtle.up,
- dig = turtle.digUp,
- detect = turtle.detectUp,
- inspect = turtle.inspectUp,
- attack = turtle.attackUp,
- }
- local down = {
- go = turtle.down,
- dig = turtle.digDown,
- detect = turtle.detectDown,
- inspect = turtle.inspectDown,
- attack = turtle.attackDown,
- place = turtle.placeDown,
- }
- local sapling = {
- name = "Sapling",
- ["minecraft:sapling"] = true,
- ["minecraft:golden_rail"] = true,
- ["Railcraft:track"] = true,
- ["IC2:blockRubSapling"] = true,
- }
- local leaves = {
- ["minecraft:leaves"] = true,
- ["IC2:blockRubLeaves"] = true,
- }
- local wood = {
- ["minecraft:log"] = true,
- ["minecraft:log2"] = true,
- ["IC2:blockRubWood"] = true,
- }
- local fuel = {
- ["minecraft:coal"] = 80,
- ["minecraft:log"] = 15,
- ["minecraft:log2"] = 15,
- ["minecraft:stick"] = 5,
- ["IC2:blockRubWood"] = 15,
- }
- container = {
- ["minecraft:chest"] = true,
- ["minecraft:trapped_chest"] = true,
- ["JABBA:barrel"] = true,
- ["IronChest:BlockIronChest"] = true,
- ["EnderStorage:enderChest"] = true,
- }
- local function displayLogo()
- term.clear()
- term.setCursorPos(1,2)
- print("**** KaosKrise "..mode.." TreeFarm "..verNum.." ****")
- print()
- print("Welcom, Gud Sir! Im Gonna be ur guide on dis marvelous advendchur.")
- print()
- end
- local function formatSeconds(seconds)
- if seconds >= 3600 then
- return string.format("%d:%.2d:%.2d", math.floor(seconds/3600), math.floor(seconds/60%60), math.floor(seconds%60))
- else
- return string.format("%d:%.2d", math.floor(seconds/60%60), math.floor(seconds%60))
- end
- end
- local function displayTime()
- local x, y = term.getCursorPos()
- local time = os.clock()
- local runTime = time - startTime
- local string = formatSeconds(runTime)
- term.setCursorPos(screenX -(#string) ,screenY)
- write(string)
- term.setCursorPos(x, y)
- return true
- end
- local function countWood()
- nWood = nWood +1
- local x, y = term.getCursorPos()
- term.setCursorPos(1,screenY)
- write("I has chopd "..nWood.." wood itamz")
- term.setCursorPos(x, y)
- end
- local function isItem(slot)
- slot = slot or turtle.getSelectedSlot()
- local data = turtle.getItemDetail(slot)
- if data then
- return data.name
- else
- return false
- end
- end
- local function selectItem(item)
- for i=1,16 do
- if item[isItem(i)] then
- turtle.select(i)
- return true
- end
- end
- return false
- end
- local function inspectBlock(direction)
- if direction == nil then direction = forward end
- local success, data = direction.inspect()
- if success then
- return data.name
- else
- return false
- end
- end
- local function refuel()
- local fuelLevel = turtle.getFuelLevel()
- if fuelLevel == "unlimited" or fuelLevel > 0 then
- return true
- end
- local function tryRefuel()
- local bestFuel = 1
- local bestFuelVal = 0
- for i=1,16 do
- local itemInSlot = isItem(i)
- if fuel[itemInSlot] and fuel[itemInSlot] > bestFuelVal then
- bestFuel = i
- bestFuelVal = fuel[itemInSlot]
- end
- end
- if bestFuel then
- turtle.select(bestFuel)
- if turtle.refuel(1) then
- turtle.select(1)
- return true
- end
- end
- return false
- end
- if not tryRefuel() then
- displayLogo()
- print( "Add more fuel to continue." )
- while not tryRefuel() do
- displayTime()
- sleep(1)
- end
- displayLogo()
- print( "Resuming..." )
- end
- return true
- end
- local function tryDig(direction, block)
- block = block or inspectBlock(direction)
- turtle.select(1)
- if direction.dig() then
- if wood[block] then
- countWood()
- end
- return true
- end
- return false
- end
- local function goDirection(direction, block)
- block = block or inspectBlock(direction)
- if not refuel() then
- return false
- end
- while displayTime() and not direction.go() do
- if block and not tryDig(direction, block) then
- return false
- elseif direction.attack() then
- print("smack!")
- else
- sleep(delay)
- end
- end
- return true
- end
- --[[
- Syntax: go( string detectedBlockname)
- Returns: boolean whether the turtle succeeded.
- --]]
- local function go(block)
- return goDirection(forward, block)
- end
- local function goBack(block)
- return goDirection(back, block)
- end
- local function goSuck(block)
- turtle.suckDown()
- return goDirection(forward, block)
- end
- local function goUp(block)
- level = level + 1
- return goDirection(up, block)
- end
- local function goDown(block)
- level = level -1
- return goDirection(down, block)
- end
- local function plant()
- if selectItem(sapling) and down.place() then
- return true
- else
- return false
- end
- end
- -- Tree Girth: 1x1:
- local function chopDownTree_1x1()
- local block = inspectBlock(up)
- while wood[block] do
- goUp(block)
- block = inspectBlock(up)
- end
- while level > 1 do
- goDown()
- end
- tryDig(down)
- end
- -- Tree Girth: 2x2:
- local function chopDownTree_2x2()
- local topBlockFound = false
- local turnCount
- -- helper function - turn until find wood log and return num of turns
- local function findWood()
- local turnCount = 0
- for i=1,4 do
- if wood[inspectBlock()] then
- return turnCount
- end
- turtle.turnRight()
- turnCount = i
- end
- return turnCount
- end
- -- helper function - finding pos at top and choping the lonely top block
- local function findPos()
- local block = inspectBlock()
- if not wood[block] then -- top block found
- topBlockFound = true
- go()
- goDown()
- else -- top block still there
- tryDig(forward, block)
- go()
- end
- block = inspectBlock(up)
- if wood[block] then -- top block found
- tryDig(up, block)
- topBlockFound = true
- end
- turnCount = findWood()
- if turnCount % 4 == 0 then
- -- local event, key = os.pullEvent( "key" ) -- DEBUG BREAK POINT
- goBack()
- return false
- else
- go()
- tTurns[math.floor(turnCount / 2)]()
- end
- if not topBlockFound then
- block = inspectBlock(up)
- if wood[block] then -- top block found
- tryDig(up, block)
- topBlockFound = true
- else
- goUp()
- end
- end
- tryDig(forward)
- return true
- end
- -- check if it's a little tree
- if not wood[inspectBlock()] then
- local x, y = term.getCursorPos()
- term.clearLine()
- print("Awwww, it's a little one!")
- term.clearLine()
- term.setCursorPos(x,y)
- return chopDownTree_1x1()
- end
- -- going up the tree:
- local blockUp = inspectBlock(up)
- while wood[blockUp] do
- tryDig(forward)
- goUp(blockUp)
- blockUp = inspectBlock(up)
- end
- local bigTree = false
- if findPos() then
- bigTree = true
- end
- -- going back down
- while level > 1 do
- goDown()
- tryDig(forward)
- end
- -- remove the stump and replant
- if bigTree then
- for i=1,3 do
- tryDig(down)
- plant()
- if i >= 2 and i <= 3 then
- tTurns[math.floor(turnCount / 2)]()
- end
- go()
- end
- end
- tryDig(down)
- end
- local function tryUnload()
- local x, y = term.getCursorPos()
- turtle.select(1)
- term.clearLine()
- print("I has findz chest.")
- term.clearLine()
- print("Will try 2 unload mah stuff...")
- local item = isItem(16)
- if "minecraft:coal" ~= item then
- turtle.select(16)
- turtle.dropDown()
- end
- for i=14,15 do
- item = isItem(i)
- if item and not sapling[item] then
- turtle.select(i)
- if item == "minecraft:coal" then
- if not turtle.transferTo(16) then
- turtle.dropDown()
- end
- else
- turtle.dropDown()
- end
- end
- end
- for i=1,13 do
- item = isItem(i)
- if item then
- turtle.select(i)
- if sapling[item] then
- if not turtle.transferTo(15) then
- if not turtle.transferTo(14) then
- turtle.dropDown()
- end
- end
- elseif item == "minecraft:coal" then
- if not turtle.transferTo(16) then
- turtle.dropDown()
- end
- else
- turtle.dropDown()
- end
- end
- end
- term.setCursorPos(x,y+1)
- term.clearLine()
- print("I has putz teh itamz in teh chest. Yay!")
- term.setCursorPos(x,y)
- end
- local function refill()
- if container[inspectBlock(up)] then
- if turtle.getItemSpace(16) > 0 then
- turtle.select(16)
- turtle.suckUp(turtle.getItemSpace())
- end
- end
- turtle.select(1)
- end
- local function turn()
- goSuck()
- local blockDown = inspectBlock(down)
- if container[blockDown] then
- tryUnload()
- refill()
- tTurns[nextTurn]()
- go()
- else
- refill()
- tTurns[nextTurn]()
- goSuck()
- end
- tTurns[nextTurn]()
- blockDown = inspectBlock(down)
- if container[blockDown] then
- tryUnload()
- refill()
- go()
- else
- refill()
- goSuck()
- end
- goSuck()
- local blockDown = inspectBlock(down)
- if blockDown then
- if not sapling[blockDown] then
- if wood[blockDown] then
- chopDownTree()
- elseif leaves[block] then
- tryDig(down)
- plant()
- goSuck()
- else
- tTurns[nextTurn]()
- goSuck()
- end
- end
- end
- nextTurn = 1 - nextTurn
- end
- local function patrol()
- while sapling[inspectBlock(down)] do
- goSuck()
- end
- end
- -- ************MAIN************
- arg = tArgs[1]
- if arg and arg == "2x2" then
- chopDownTree = chopDownTree_2x2
- mode = "2x2"
- else
- chopDownTree = chopDownTree_1x1
- mode = ""
- end
- nWood = 0
- turtle.select(1)
- displayLogo()
- go()
- while true do
- patrol()
- local block = inspectBlock(down)
- if block then
- if wood[block] then
- chopDownTree()
- plant()
- goSuck()
- elseif leaves[block] then
- tryDig(down)
- plant()
- goSuck()
- else
- turn()
- end
- else
- plant()
- goSuck()
- end
- end
- -- local event, key = os.pullEvent( "key" ) -- DEBUG BREAK POINT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement