Advertisement
Guest User

canopy

a guest
Apr 24th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.37 KB | None | 0 0
  1. -- Set constants
  2.  
  3. MAX_RADIUS = 20
  4. EMPTY_HARVEST = 10
  5. EMPTY_LEVELS = 3
  6.  
  7. -- Get the first parameter value that is passed to the program.  This will be
  8. -- the radius of the expanding square search.  If no paramter is passed, default
  9. -- to a radius of 10.
  10.  
  11. args = {...}
  12.  
  13. if #args == 0 then
  14.    args[1] = 10
  15. end
  16.  
  17. if args[1] > MAX_RADIUS then
  18.    args[1] = MAX_RADIUS
  19. end
  20.  
  21. harvestLeg = {}
  22.  
  23. for i = 1, 5, 1 do
  24.    harvestLeg[i] = args[1] * 4
  25. end
  26.  
  27. -- Initialize the variables needed to run the loop and keep track of the
  28. -- harvesting progress.
  29.  
  30. levelTotal = 0
  31. legTotal = 0
  32. harvestTotal = 0
  33.  
  34. heading = 0
  35. xPos = 0
  36. yPos = 0
  37. emptyLevels = 0
  38.  
  39. moveDown = 1
  40.  
  41. -- Refuel the turtle as needed.
  42.  
  43. if turtle.getFuelLevel() == 0 then
  44.    turtle.select(1)
  45.    turtle.refuel()
  46. end
  47.  
  48. -- Loop for each level that is harvested.
  49.  
  50. while moveDown == 1 do
  51.  
  52.    legCount = 0
  53.    legTarget = 0
  54.    legLength = 1
  55.    harvestCount = 0
  56.  
  57.    -- Examine the last five levels that have been processed, and find the
  58.    -- maximum leg on which something was harvested.
  59.  
  60.    for i = 1, 5, 1 do
  61.       if harvestLeg[i] > legTarget then
  62.          legTarget = harvestLeg[i]
  63.       end
  64.    end
  65.  
  66.    legTarget = legTarget + 12
  67.  
  68.    -- Move the turtle down to the next level and begin recording whether
  69.    -- anything is harvested.
  70.  
  71.    lastHarvestLeg = -1
  72.  
  73.    if turtle.detectDown() then
  74.       turtle.digDown()
  75.       harvestCount = harvestCount + 1
  76.       lastHarvestLeg = 0
  77.    end
  78.  
  79.    turtle.down()
  80.    levelTotal = levelTotal + 1
  81.  
  82.    -- Begin looping through the different legs of the search. Note that the
  83.    -- target leg value will be revaluated after each leg, and can expand as
  84.    -- more harvesting takes place.
  85.  
  86.    while legCount < legTarget do
  87.  
  88.       legCount = legCount + 1
  89.  
  90.       -- On every second leg, the length of the leg increases by 1.  Ensure
  91.       -- that there is enough fuel to accomplish the leg.
  92.  
  93.       if legCount % 2 == 0 then
  94.          legLength = legLength + 1
  95.       end
  96.  
  97.       if legCount < 5 then
  98.              
  99.       print("On leg " .. legCount .. ", length " .. legLength)
  100.  
  101.       end
  102.  
  103.       while turtle.getFuelLevel() < legLength + 1 and turtle.getItemCount(1) > 0 do
  104.          turtle.select(1)
  105.          turtle.refuel()
  106.       end
  107.  
  108.       -- Run through the length of the leg, harvesting as needed and keeping
  109.       -- track of the progress.
  110.  
  111.       for blockIndex = 1, legLength, 1 do
  112.  
  113.          if turtle.detect() then
  114.             turtle.dig()
  115.             harvestCount = harvestCount + 1
  116.             lastHarvestLeg = legCount
  117.          end
  118.  
  119.          turtle.forward()
  120.  
  121.          if heading == 0 then
  122.             yPos = yPos + 1
  123.          elseif heading == 90 then
  124.             xPos = xPos + 1
  125.          elseif heading == 180 then
  126.             yPos = yPos - 1
  127.          elseif heading == 270 then
  128.             xPos = xPos - 1
  129.          end
  130.  
  131.       end
  132.  
  133.       -- As new items are harvested within the canopy, reset the target value.
  134.  
  135.       if legTarget < lastHarvestLeg + 12 then
  136.          legTarget = lastHarvestLeg + 12
  137.       end
  138.  
  139.       if legTarget > MAX_RADIUS * 4 then
  140.          legTarget = MAX_RADIUS * 4
  141.       end
  142.  
  143.       -- Turn the turtle 90 degrees to the right and adjust its heading value.
  144.  
  145.       turtle.turnRight()
  146.  
  147.       heading = heading + 90
  148.  
  149.       if heading == 360 then
  150.          heading = 0
  151.       end
  152.  
  153.    end
  154.  
  155.    -- The turtle has now completed all of the legs needed for this level.
  156.    -- Update all of the tracking variables and print progress messages.
  157.  
  158.    legTotal = legTotal + legCount
  159.    harvestTotal = harvestTotal + harvestCount
  160.  
  161.    print("Level " .. levelTotal .. ": " .. legCount .. " legs, " .. harvestCount .. " harvested")
  162.  
  163.    -- Ensure that the turtle has enough fuel to return to the starting position.
  164.  
  165.    while turtle.getFuelLevel() < legLength + 10 and turtle.getItemCount(1) > 0 do
  166.       turtle.select(1)
  167.       turtle.refuel()
  168.    end
  169.  
  170.    -- Return the turtle to its starting position. Turn it 90 degrees to the
  171.    -- right to backtrack along the last leg.
  172.  
  173.    turtle.turnRight()
  174.    heading = heading + 90
  175.  
  176.    if heading == 360 then
  177.       heading = 0
  178.    end
  179.  
  180.    -- Move the turtle foward until it is even with its starting position.
  181.  
  182.    if heading == 0 then
  183.       while yPos ~= 0 do
  184.          turtle.forward()
  185.          yPos = yPos + 1
  186.       end
  187.    elseif heading == 90 then
  188.       while xPos ~= 0 do
  189.          turtle.forward()
  190.          xPos = xPos + 1
  191.       end
  192.    elseif heading == 180 then
  193.       while yPos ~= 0 do
  194.          turtle.forward()
  195.          yPos = yPos - 1
  196.       end
  197.    elseif heading == 270 then
  198.       while xPos ~= 0 do
  199.          turtle.forward()
  200.          xPos = xPos - 1
  201.       end
  202.    end
  203.  
  204.    -- Turn the turtle 90 degrees to the left, and move it to the starting
  205.    -- position.
  206.  
  207.    turtle.turnLeft()
  208.    heading = heading - 90
  209.  
  210.    if heading == 360 then
  211.       heading = 0
  212.    end
  213.  
  214.    if heading == 0 then
  215.       while yPos ~= 0 do
  216.          turtle.forward()
  217.          yPos = yPos + 1
  218.       end
  219.    elseif heading == 90 then
  220.       while xPos ~= 0 do
  221.          turtle.forward()
  222.          xPos = xPos + 1
  223.       end
  224.    elseif heading == 180 then
  225.       while yPos ~= 0 do
  226.          turtle.forward()
  227.          yPos = yPos - 1
  228.       end
  229.    elseif heading == 270 then
  230.       while xPos ~= 0 do
  231.          turtle.forward()
  232.          xPos = xPos - 1
  233.       end
  234.    end
  235.  
  236.    moveDown = 0
  237.  
  238. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement