Advertisement
Guest User

canopy

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