Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Set constants
- MAX_RADIUS = 20
- EMPTY_HARVEST = 10
- EMPTY_LEVELS = 3
- -- 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] = 10
- end
- if args[1] > MAX_RADIUS then
- args[1] = MAX_RADIUS
- end
- harvestLeg = {}
- for i = 1, 5, 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.
- if turtle.getFuelLevel() == 0 then
- turtle.select(1)
- turtle.refuel()
- end
- -- Loop for each level that is harvested.
- while moveDown == 1 do
- legCount = 0
- legTarget = 0
- legLength = 1
- harvestCount = 0
- -- Examine the last five levels that have been processed, and find the
- -- maximum leg on which something was harvested.
- for i = 1, 5, 1 do
- if harvestLeg[i] > legTarget then
- legTarget = harvestLeg[i]
- end
- end
- legTarget = legTarget + 12
- -- Move the turtle down to the next level and begin recording whether
- -- anything is harvested.
- lastHarvestLeg = -1
- if turtle.detectDown() then
- turtle.digDown()
- harvestCount = harvestCount + 1
- lastHarvestLeg = 0
- end
- turtle.down()
- levelTotal = levelTotal + 1
- -- 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 == 0 then
- legLength = legLength + 1
- end
- if legCount < 5 then
- print("On leg " .. legCount .. ", length " .. legLength)
- end
- while turtle.getFuelLevel() < legLength + 1 and turtle.getItemCount(1) > 0 do
- turtle.select(1)
- turtle.refuel()
- end
- -- 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
- 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
- -- As new items are harvested within the canopy, reset the target value.
- if legTarget < lastHarvestLeg + 12 then
- legTarget = lastHarvestLeg + 12
- 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.
- turtle.turnRight()
- heading = heading + 90
- if heading == 360 then
- heading = 0
- end
- 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.
- while turtle.getFuelLevel() < legLength + 10 and turtle.getItemCount(1) > 0 do
- turtle.select(1)
- turtle.refuel()
- end
- -- Return the turtle to its starting position. Turn it 90 degrees to the
- -- right to backtrack along the last leg.
- turtle.turnRight()
- heading = heading + 90
- if heading == 360 then
- heading = 0
- end
- -- Move the turtle foward until it is even with its starting position.
- if heading == 0 then
- while yPos ~= 0 do
- turtle.forward()
- yPos = yPos + 1
- end
- elseif heading == 90 then
- while xPos ~= 0 do
- turtle.forward()
- xPos = xPos + 1
- end
- elseif heading == 180 then
- while yPos ~= 0 do
- turtle.forward()
- yPos = yPos - 1
- end
- elseif heading == 270 then
- while xPos ~= 0 do
- turtle.forward()
- xPos = xPos - 1
- end
- end
- -- Turn the turtle 90 degrees to the left, and move it to the starting
- -- position.
- turtle.turnLeft()
- heading = heading - 90
- if heading == 360 then
- heading = 0
- end
- if heading == 0 then
- while yPos ~= 0 do
- turtle.forward()
- yPos = yPos + 1
- end
- elseif heading == 90 then
- while xPos ~= 0 do
- turtle.forward()
- xPos = xPos + 1
- end
- elseif heading == 180 then
- while yPos ~= 0 do
- turtle.forward()
- yPos = yPos - 1
- end
- elseif heading == 270 then
- while xPos ~= 0 do
- turtle.forward()
- xPos = xPos - 1
- end
- end
- moveDown = 0
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement