Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Author: KROM
- --
- -- --------------------------------
- local bUseModem = false
- local bBoneMeal = true
- local nRuns = 0
- local bNoError = true
- local bRedNet = false
- local nMasterComputerID = nil
- -- 30
- local vX, vY = 0,-1
- local posX, posY, posZ = 0,0,0
- local stopCause = 0
- -- --------------------------------
- local function scanForSaplings()
- return turtle.getItemCount(2)
- end
- -- --------------------------------
- local function scanForBoneMeal()
- return turtle.getItemCount(3)
- end
- -- --------------------------------
- local function say(message)
- if bRedNet == true then
- rednet.send(nMasterComputerID, message)
- end
- print(message)
- end
- -- --------------------------------
- local function turnRight()
- turtle.turnRight()
- vX, vY = vY, -vX
- end
- -- --------------------------------
- local function turnLeft()
- turtle.turnLeft()
- vX, vY = -vY, vX
- end
- -- ----------------------------------------------------------
- local function tryDown()
- stopCause = 0
- if posZ == 0 then
- say("tryDown: already at max depth")
- return false
- end
- if not turtle.down() then
- if turtle.digDown() then
- end
- if not turtle.down() then
- stopCause = 1
- return false
- end
- end
- posZ = posZ - 1
- return true
- end
- -- ----------------------------------------------------------
- local function tryForwards()
- stopCause = 0
- while not turtle.forward() do
- if turtle.dig() then
- -- all cool
- else
- sleep(0.8)
- if turtle.dig() then
- -- all cool
- else
- stopCause = 1
- return false
- end
- end
- end
- return true
- end
- -- --------------------------------
- local function work(nHowDeep, nSize)
- local depth,x,y
- local startZ = posZ
- local bFirstLoop = false
- for depth=0,nHowDeep do
- for x=0,(nSize-1) do
- -- first row has no turn move
- -- which needs to be offset
- local startY = 1
- if x == 0 and startZ == 0 and depth == 0 then
- startY = 0
- elseif bFirstLoop == true then
- startY = 0
- end
- if x == 0 then
- say("SY: "..startY)
- end
- for y=startY,(nSize-1) do
- local start = posX..","..posY..","..posZ
- v = math.fmod(x,2)
- -- v == 0 = facing away. like start
- -- v == 1 = facing to start
- -- say("V: "..v.." LD: "..layerdirection.." SY: "..startY.." xyz: "..x..","..y..","..depth)
- if not tryForwards() then
- if stopCause == 2 then
- -- inventory full?!
- say("Failed to move forward. Inv. full?!")
- return false
- else
- say("Failed to move forward.")
- return false
- end
- end
- if layerdirection == 1 then
- if v == 0 then
- posY = posY + 1
- else
- posY = posY - 1
- end
- else
- if v == 0 then
- posY = posY - 1
- else
- posY = posY + 1
- end
- end
- -- say("P: "..start.." -> "..posX..","..posY..","..posZ)
- -- say("Dir: "..xDir..","..yDir)
- -- end y loop
- end
- bFirstLoop = false;
- say("completed one line")
- -- if not on last line
- if x < nSize-1 then
- -- move to next line (turn, move, turn)
- if v == 0 then
- say("Moving to next row (1)")
- turnRight()
- if not tryForwards() then
- if stopCause == 2 then
- -- inventory full?!
- say("Failed to move forward. Inv. full?!")
- return false
- else
- return false
- end
- end
- turnRight()
- else
- say("Moving to next row (2)")
- turnLeft()
- if not tryForwards() then
- if stopCause == 2 then
- -- inventory full?!
- say("Failed to move forward. Inv. full?!")
- return false
- else
- return false
- end
- end
- turnLeft()
- end
- if layerdirection == 1 then
- posX = posX + 1
- else
- posX = posX - 1
- end
- end
- -- end x loop
- end
- say("completed one layer")
- -- checks if depth reached
- if depth < nHowDeep then
- if not tryDown() then
- -- unable to move down?
- -- assume we hit bedrock
- say("! we failed to move down")
- return false
- end
- if layerdirection == 1 then
- layerdirection = 0
- else
- layerdirection = 1
- end
- turnLeft()
- turnLeft()
- else
- say("finished all layer")
- -- finished
- return true
- end
- -- end z loop
- end
- say("should not hit this code line")
- end
- -- --------------------------------
- local function giantTreeMode(nCurrentHeight)
- local startX, startY, startZ = 0,0,nCurrentHeight
- posX, posY, posZ = 0,0,nCurrentHeight
- vX, vY = 0,-1
- local nRadius = 4
- local nMinHeight = 3
- local nMaxHeight = 9
- -- go to start position
- while posZ < nCurrentHeight do
- if turtle.detectUp() then
- turtle.digUp()
- end
- turtle.up()
- posZ = posZ + 1
- end
- for i=0,nRadius do
- if turtle.detect() then
- turtle.dig()
- end
- turtle.forward()
- posY = posY + 1
- end
- turnLeft()
- for i=0,nRadius do
- if turtle.detect() then
- turtle.dig()
- end
- turtle.forward()
- posX = posX - 1
- end
- turnRight()
- turnRight()
- work(nMaxHeight - nMinHeight, nRadius)
- end
- -- --------------------------------
- local function harvestTree()
- local nHeight = 0
- say("Harvesting Tree")
- turtle.dig()
- turtle.forward()
- while turtle.detectUp() do
- turtle.digUp()
- turtle.up()
- nHeight = nHeight + 1
- end
- -- check if giant tree.
- -- check by height...
- if nHeight > 6 then
- say("*** GIANT TREE MODE ***")
- return giantTreeMode(nHeight)
- else
- -- normal tree. go back down
- while not turtle.detectDown() do
- turtle.down()
- end
- turtle.back()
- end
- say("Finished harvesting tree")
- end
- -- --------------------------------
- local function detectTree()
- if turtle.detect() == false then
- say("No tree detected(1)")
- return false
- end
- if turtle.up() == false then
- say("Cannot move up, must be a tree")
- return true
- end
- if turtle.detect() == true then
- turtle.down()
- say("Tree detected")
- return true
- else
- turtle.down()
- say("Possible sapling detected")
- return false
- end
- return false
- end
- -- --------------------------------
- local function applyBoneMeal()
- say("Trying to apply BoneMeal")
- -- place while there is no tree
- local isatree
- local abort = false
- local nBoneMeal = 0
- local nMax = 5
- while abort == false do
- say("Looping for BoneMeal")
- isatree = detectTree()
- nBoneMeal = scanForBoneMeal()
- if isatree == false and nBoneMeal > 0 and turtle.detect() == true then
- turtle.select(3)
- turtle.place()
- say("Applied BoneMeal")
- end
- if isatree == true or nBoneMeal == 0 then
- abort = true
- end
- nMax = nMax - 1
- if nMax <= 0 then
- abort = true
- say("BoneMeal: Max tries")
- end
- end
- end
- -- --------------------------------
- local function plantTree(harvested)
- say("Planting new Tree")
- turtle.select(2)
- turtle.place()
- if bBoneMeal == true and harvested == false then
- applyBoneMeal()
- end
- say("Finished planting new Tree")
- end
- -- --------------------------------
- local function spaceLeft()
- local nSpace = 0
- for n=1,16 do
- local x = turtle.getItemCount(n)
- if x == 0 then
- nSpace = nSpace + 1
- end
- end
- return nSpace
- end
- -- --------------------------------
- local function moveForward(nSteps, pickupStuff)
- if nSteps == 0 then
- return true
- end
- while nSteps > 0 do
- if pickupStuff == true then
- turtle.suck()
- end
- if turtle.forward() == false then
- return false
- end
- nSteps = nSteps - 1
- end
- end
- -- --------------------------------
- local function dump(keepBattery)
- local x = 1
- if keepBattery == true then
- x = 2
- end
- for n=x,16 do
- turtle.select(n)
- turtle.drop()
- end
- end
- -- --------------------------------
- local function restock()
- -- move to first box (energy)
- say("move to first box (energy)");
- turnLeft()
- moveForward(2,true)
- turnRight()
- -- pick up energy cells
- turtle.select(1)
- turtle.suck()
- -- move to second box (saplings)
- say("move to second box (saplings)");
- turnLeft()
- moveForward(2,true)
- turnRight()
- -- pick up saplings
- turtle.select(2)
- turtle.suck()
- if bBoneMeal then
- -- move to third box (bone meal)
- say("move to third box (bone meal)");
- turnLeft()
- moveForward(2,true)
- turnRight()
- -- pick up bone meal
- turtle.select(3)
- turtle.suck()
- end
- turnRight()
- -- return to start
- say("return to start");
- if bBoneMeal then
- moveForward(6,true)
- else
- moveForward(4,true)
- end
- end
- -- --------------------------------
- -- ----------------------------------------
- -- Main -----------------------------------
- -- ----------------------------------------
- local function mainRun()
- -- go to restock position
- say("go to restock position")
- turnRight()
- -- dump any contents
- say("dump any contents")
- dump(false)
- -- wait?
- -- restock
- say("restock")
- restock()
- -- face in original position
- say("face in original position")
- turnRight()
- -- check fuel
- nFuel = turtle.getFuelLevel()
- bHasFuel = true
- while nFuel < 100 and bHasFuel == true do
- turtle.select(1)
- bHasFuel = turtle.refuel(1)
- nFuel = turtle.getFuelLevel()
- end
- if bHasFuel == false then
- say("Fuel level below minimum treshold of 100")
- return false
- end
- -- inital checks if everything is on board
- -- check for saplings
- if scanForSaplings() == false then
- say("We have no saplings")
- return false
- end
- -- check for BoneMeal
- if bBoneMeal == true and scanForBoneMeal() == 0 then
- say("We have no bonemeal. Continue, but disable feature.")
- bBoneMeal = false
- end
- -- check if space in inventory
- if spaceLeft() == 0 then
- say("Inventory is full. Need attention.")
- return false
- end
- -- move to first position ------------------------
- if moveForward(2,true) == false then
- say("Move(1): Failed to move forward")
- return false
- end
- turnRight()
- if moveForward(3,true) == false then
- say("Move(1,2): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- move to second position ------------------------
- if moveForward(4,true) == false then
- say("Move(2,1): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- move to second position ------------------------
- if moveForward(4,true) == false then
- say("Move(3,1): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- move to 4th position ------------------------
- if moveForward(1,true) == false then
- say("Move(4,1): Failed to move forward")
- return false
- end
- turnLeft()
- if moveForward(6,true) == false then
- say("Move(3,2): Failed to move forward")
- return false
- end
- turnLeft()
- if moveForward(1,true) == false then
- say("Move(3,1): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- move to fourth position ------------------------
- if moveForward(4,true) == false then
- say("Move(2,1): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- move to fourth position ------------------------
- if moveForward(4,true) == false then
- say("Move(2,1): Failed to move forward")
- return false
- end
- turnLeft()
- -- check for tree
- if detectTree() == true then
- harvestTree()
- plantTree(true)
- elseif turtle.detect() == false then
- say("No Tree found. Planting")
- plantTree(false)
- elseif bBoneMeal == true then
- applyBoneMeal()
- end
- turnRight()
- -- return ------------------------
- if moveForward(3,true) == false then
- say("Move(4,1): Failed to move forward")
- return false
- end
- turnLeft()
- if moveForward(8,true) == false then
- say("Move(4,2): Failed to move forward")
- return false
- end
- -- say("Should be in home position, facing chest. Dumping stuff.")
- say("Dumping stuff.")
- dump(false)
- say("Face start position")
- turnLeft()
- local c = turtle.getFuelLevel()
- c = nFuel - c
- say("==== Run "..nRuns.." complete. "..c.." E consumed ====")
- nRuns = nRuns + 1
- return true
- end
- -- -------------------------
- -- init wireless
- ----------------------------
- if bUserModem == true then
- bRedNet = rednet.open("right")
- if not bRedNet == true then
- bRedNet = rednet.open("left")
- end
- bRedNet = true
- end
- if bRedNet == true then
- say("Rednet enabled")
- rednet.send(nMasterComputerID, "HarvestBot 01 reporting in.")
- else
- say("no rednet available")
- end
- -- check fuel
- nFuel = turtle.getFuelLevel()
- bHasFuel = true
- while nFuel < 100 and bHasFuel == true do
- turtle.select(1)
- bHasFuel = turtle.refuel(1)
- nFuel = turtle.getFuelLevel()
- end
- if bHasFuel == false then
- say("Fuel level below minimum treshold of 100")
- return false
- end
- while bNoError == true do
- print ("Starting Run "..(nRuns+1))
- bNoError = mainRun()
- nFuel = turtle.getFuelLevel()
- print ("Fuel level: "..nFuel)
- if bNoError == true then
- say("Sleeping for 60 seconds")
- sleep(10)
- say("Sleeping for 50 seconds")
- sleep(10)
- say("Sleeping for 40 seconds")
- sleep(10)
- say("Sleeping for 30 seconds")
- sleep(10)
- say("Sleeping for 20 seconds")
- sleep(10)
- say("Sleeping for 10 seconds")
- sleep(10)
- else
- say("Aborting after "..nRuns.." runs")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment