Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- PRESETS --
- local userFuelLimit = 1 --Minimum fuel before turtle will start felling
- -- SLOT SETTINGS --
- local fuelSlot = 1
- local woodSlot = 2
- local saplingSlot = 3
- local dirtSlot = 4
- -- STATE VALUES --
- state_nothing = 0
- state_refueling = 1
- state_notEnoughFuel = 2
- state_searching = 3
- state_chopping = 4
- -- STATE VARIABLES --
- local currentState = 0
- local canPlant = 1
- local foundTree = 0
- local shutDown = 0 -- activate to break out of mainloop and end program
- -- == FUNCTION PROTOTYPES == --
- local obstacle = 0
- function newPass()
- turtle.turnRight()
- if not turtle.detect() then
- turtle.forward()
- turtle.turnRight()
- else
- for i=1,2 do turtle.turnRight() end
- for i=1,2 do trutle.moveForward() end
- end
- return 0
- end
- --== FIND TREE == --
- function findTree()
- while(foundTree==0) do
- turtle.select(woodSlot)
- print("checking if tree")
- if turtle.compare() then
- foundTree = 1
- print("Found one!")
- print("Running FellTree")
- return state_chopping
- else
- while(obstacle == 0) do
- if not turtle.detect() then
- turtle.forward()
- -- plantSapling() --TEMP ADD, MIGHT NOT WORK?
- elseif not turtle.compare() then
- print("Obstacle Found")
- obstacle = 1
- obstacle = newPass()
- else
- break
- end
- end
- end
- end
- end
- --FUEL CHECK -- STATE 0 --
- function checkFuel()
- if(turtle.getFuelLevel()<userFuelLimit)then
- print("Current Fuel Level: "..turtle.getFuelLevel())
- return state_refueling
- else
- print("Loaded with Fuel, begin search")
- return state_searching
- end
- end
- -- GET FUEL --
- function getFuel()
- for i = 1,16 do
- print("checked slot: "..i)
- turtle.select(i)
- turtle.refuel()
- if(turtle.getFuelLevel() > userFuelLimit)then
- print("Fuel at limit, ready to start")
- return state_searching
- end
- end
- print("Not enough fuel in inventory")
- return state_notEnoughFuel
- end
- -- CHOP TREE --
- function chopTree()
- -- print("here") -- DEBUG FOR FUNCTION TEST --
- -- turtle.select(1) -- +++++
- -- turtle.refuel() -- +++++
- local height = 0 --current height above ground
- turtle.select(woodSlot) --selected slot for wood (see variables @ top)
- while true do
- if(turtle.compare())then --Check for wood ahead, dig wood if true
- turtle.dig()
- print("Getting Wood")
- else --Otherwise drop back to ground, enter search state
- print("no Wood, return to search")
- for i = 0,height do
- turtle.down()
- print(height)
- print("Moving Down")
- end
- return state_searching
- end
- if(not turtle.detectUp())then --If nothing above, move up, inc height
- turtle.up()
- height = height + 1
- print("Free path above, moving up")
- else --Remove obstruction above, move up, inc height
- turtle.digUp()
- turtle.up()
- height = height + 1
- print("Obstructed path above, digging and moving up")
- end
- end
- end
- -- PLANT SAPPLING --
- -- To use, set the canPlant = plantSapling()
- function plantSapling()
- local dirtBelow = 1
- turtle.select(dirtSlot) --selects dirt slot
- if(not turtle.compareDown())then --if the block below isn't dirt
- print("No dirt below..")
- if(turtle.getItemCount(dirtSlot) > 1) then --checks if it has at least 2 dirt blocks, digs and replaces the the block
- print("replacing with dirt")
- turtle.digDown()
- turtle.placeDown()
- dirtBelow = 1
- else
- print("Out of replacement dirt")
- dirtBelow = 0
- end
- end
- turtle.select(saplingSlot)
- if(canPlant and dirtBelow) then
- print("attempting plant")
- if(turtle.getItemCount(saplingSlot) > 1) then --turtle has sapling, plants
- print("planting")
- if(turtle.detectUp()) then turtle.digUp() end
- turtle.up()
- turtle.placeDown()
- if(turtle.detect()) then turtle.dig() end
- turtle.forward()
- turtle.down()
- else
- print("Out of saplings")
- return 0
- end
- end
- return 1
- end
- --TURN--
- function aroundRight()
- turtle.turnRight()
- turtle.forward()
- turtle.turnRight()
- turtle.forward()
- end
- function aroundLeft()
- turtle.turnLeft()
- turtle.forward()
- turtle.turnLeft()
- turtle.forward()
- end
- -- ==MAIN LOOP== --
- currentState = findTree()
- print(currentState)
- if currentState == state_chopping then
- currentState = chopTree()
- else
- print("Check didn't work")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement