Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set constants.
- DEFAULT_RADIUS = 10
- MAX_RADIUS = 20
- EMPTY_HARVEST = 10
- EMPTY_LEVELS = 3
- -- Define functions.
- function refuel(distance)
- if distance == nil then
- distance = 1
- end
- if turtle.getFuelLevel() < distance then
- continue = true
- else
- continue = false
- end
- slot = 0
- while continue do
- slot = slot + 1
- turtle.select(slot)
- if turtle.refuel(0) then
- while continue and turtle.getItemCount > 0 do
- turtle.refuel(1)
- if turtle.getFuelLevel >= distance then
- continue = false
- end
- end
- end
- if slot == 16 then
- continue = false
- end
- end
- end
- function burrow(levels)
- returnValue = false
- if levels == nil then
- levels = 1
- end
- for i = 1, levels, 1 do
- if turtle.detectDown() then
- turtle.digDown()
- harvestCount = harvestCount + 1
- returnValue = true
- end
- turtle.down()
- levelTotal = levelTotal + 1
- end
- return returnValue
- end
- function move()
- turtle.forward()
- if heading == 0 then
- yPos = yPos + 1
- elseif heading == 90 then
- xPos = xPos + 1
- elseif heading == 180 then
- yPos = yPos - 1
- elseif heading == 270 then
- xPos = xPos - 1
- end
- end
- function turn(direction)
- if direction == nill then
- direction = "RIGHT"
- end
- if direction == "RIGHT" then
- turtle.turnRight()
- heading = heading + 90
- elseif direction == "LEFT" then
- turtle.turnLeft()
- heading = heading - 90
- end
- if heading == -90 then
- heading = 270
- elseif heading == 360 then
- heading = 0
- end
- end
- -- Get the first parameter value that is passed to the program. This will be
- -- the radius of the expanding square search. If no paramter is passed, default
- -- to a radius of 10.
- args = {...}
- if #args == 0 then
- args[1] = DEFAULT_RADIUS
- end
- args[1] = tonumber(args[1])
- if args[1] == nil then
- args[1] = DEFAULT_RADIUS
- elseif args[1] > MAX_RADIUS then
- args[1] = MAX_RADIUS
- end
- harvestLeg = {}
- for i = 1, 3, 1 do
- harvestLeg[i] = args[1] * 4
- end
- -- Initialize the variables needed to run the loop and keep track of the
- -- harvesting progress.
- levelTotal = 0
- legTotal = 0
- harvestTotal = 0
- heading = 0
- xPos = 0
- yPos = 0
- emptyLevels = 0
- moveDown = 1
- -- Refuel the turtle as needed.
- refuel(2)
- -- Loop for each level that is harvested.
- while moveDown == 1 do
- legCount = 0
- legTarget = 0
- legLength = 0
- lastHarvestLeg = 0
- harvestCount = 0
- -- Examine the last five levels that have been processed, and find the
- -- maximum leg on which something was harvested.
- for i = 1, 3, 1 do
- if harvestLeg[i] > legTarget then
- legTarget = harvestLeg[i]
- end
- end
- legTarget = legTarget + 8
- -- Move the turtle down to the next level and begin recording whether
- -- anything is harvested.
- burrow(2)
- if turtle.detectDown() then
- turtle.digDown()
- harvestCount = harvestCount + 1
- end
- -- Begin looping through the different legs of the search. Note that the
- -- target leg value will be revaluated after each leg, and can expand as
- -- more harvesting takes place.
- while legCount < legTarget do
- legCount = legCount + 1
- -- On every second leg, the length of the leg increases by 1. Ensure
- -- that there is enough fuel to accomplish the leg.
- if legCount % 2 == 1 then
- legLength = legLength + 1
- end
- refuel(legLength)
- -- Run through the length of the leg, harvesting as needed and keeping
- -- track of the progress.
- for blockIndex = 1, legLength, 1 do
- if turtle.detect() then
- turtle.dig()
- harvestCount = harvestCount + 1
- lastHarvestLeg = legCount
- end
- move()
- if turtle.detectUp() then
- turtle.digUp()
- harvestCount = harvestCount + 1
- lastHarvestLeg = legCount
- end
- if turtle.detectDown() then
- turtle.digDown()
- harvestCount = harvestCount + 1
- lastHarvestLeg = legCount
- end
- end
- -- As new items are harvested within the canopy, reset the target value.
- if legTarget < lastHarvestLeg + 8 then
- legTarget = lastHarvestLeg + 8
- end
- if legTarget > MAX_RADIUS * 4 then
- legTarget = MAX_RADIUS * 4
- end
- -- Turn the turtle 90 degrees to the right and adjust its heading value.
- turn("RIGHT")
- end
- -- The turtle has now completed all of the legs needed for this level.
- -- Update all of the tracking variables and print progress messages.
- legTotal = legTotal + legCount
- harvestTotal = harvestTotal + harvestCount
- print("Level " .. levelTotal .. ": " .. legCount .. " legs, " .. harvestCount .. " harvested")
- -- Ensure that the turtle has enough fuel to return to the starting position.
- refuel(legLength + 10)
- -- Return the turtle to its starting position. Turn it 90 degrees to the
- -- right to backtrack along the last leg.
- turn("RIGHT")
- -- Move the turtle foward until it is even with its starting position.
- if heading == 0 or heading == 180 then
- while yPos ~= 0 do
- move()
- end
- elseif heading == 90 or heading == 270 then
- while xPos ~= 0 do
- move()
- end
- end
- -- Turn the turtle 90 degrees to the left, and move it to the starting
- -- position.
- turn("LEFT")
- if heading == 0 or heading == 180 then
- while yPos ~= 0 do
- move()
- end
- elseif heading == 90 or heading == 270 then
- while xPos ~= 0 do
- move()
- end
- end
- -- Perform a series of right turns until the turtle is facing in its
- -- original direction.
- while heading ~= 0 do
- turn("RIGHT")
- end
- -- Move down one block to sit on top of what will now be the next solid level.
- burrow(1)
- moveDown = 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement