Advertisement
Guest User

Untitled

a guest
Apr 24th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.18 KB | None | 0 0
  1. -- Set constants.
  2.  
  3. DEFAULT_RADIUS = 10
  4. MAX_RADIUS = 20
  5. EMPTY_HARVEST_LIMIT = 15
  6. EMPTY_LEVELS_LIMIT = 2
  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.  
  150. moveDown = 1
  151.  
  152. -- Refuel the turtle as needed.
  153.  
  154. refuel(2)  
  155.  
  156. -- Loop for each level that is harvested.
  157.  
  158. while moveDown == 1 do
  159.  
  160.    legCount = 0
  161.    legTarget = 0
  162.    legLength = 0
  163.    lastHarvestLeg = 0
  164.  
  165.    levelUp = 0
  166.    levelEven = 0
  167.    levelDown = 0
  168.    harvestCount = 0
  169.  
  170.    -- Examine the last five levels that have been processed, and find the
  171.    -- maximum leg on which something was harvested.
  172.  
  173.    for i = 1, 3, 1 do
  174.       if harvestLeg[i] > legTarget then
  175.          legTarget = harvestLeg[i]
  176.       end
  177.    end
  178.  
  179.    legTarget = legTarget + 8
  180.  
  181.    -- Move the turtle down to the next level and begin recording whether
  182.    -- anything is harvested.
  183.  
  184.    if burrow(1) then
  185.       levelUp = 1
  186.    end
  187.  
  188.    if burrow(1) then
  189.       levelEven = 1
  190.    end
  191.  
  192.    if turtle.detectDown() then
  193.       turtle.digDown()
  194.       harvestCount = harvestCount + 1
  195.    end
  196.  
  197.    -- Begin looping through the different legs of the search. Note that the
  198.    -- target leg value will be revaluated after each leg, and can expand as
  199.    -- more harvesting takes place.
  200.  
  201.    while legCount < legTarget do
  202.  
  203.       legCount = legCount + 1
  204.  
  205.       -- On every second leg, the length of the leg increases by 1.  Ensure
  206.       -- that there is enough fuel to accomplish the leg.
  207.  
  208.       if legCount % 2 == 1 then
  209.          legLength = legLength + 1
  210.       end
  211.  
  212.       refuel(legLength)
  213.  
  214.       -- Run through the length of the leg, harvesting as needed and keeping
  215.       -- track of the progress.
  216.  
  217.       for blockIndex = 1, legLength, 1 do
  218.  
  219.          if turtle.detect() then
  220.             turtle.dig()
  221.             harvestCount = harvestCount + 1
  222.             lastHarvestLeg = legCount
  223.             levelEven = 0
  224.          end
  225.  
  226.          move()
  227.  
  228.          if turtle.detectUp() then
  229.             turtle.digUp()
  230.             harvestCount = harvestCount + 1
  231.             lastHarvestLeg = legCount
  232.             levelUp = 1
  233.          end
  234.  
  235.          if turtle.detectDown() then
  236.             turtle.digDown()
  237.             harvestCount = harvestCount + 1
  238.             lastHarvestLeg = legCount
  239.             levelDown = 1
  240.          end
  241.  
  242.       end
  243.  
  244.       -- As new items are harvested within the canopy, reset the target value.
  245.  
  246.       if legTarget < lastHarvestLeg + 8 then
  247.          legTarget = lastHarvestLeg + 8
  248.       end
  249.  
  250.       if legTarget > MAX_RADIUS * 4 then
  251.          legTarget = MAX_RADIUS * 4
  252.       end
  253.  
  254.       -- Turn the turtle 90 degrees to the right and adjust its heading value.
  255.  
  256.       turn("RIGHT")
  257.  
  258.    end
  259.  
  260.    -- The turtle has now completed all of the legs needed for this level.
  261.    -- Update all of the tracking variables and print progress messages.
  262.  
  263.    legTotal = legTotal + legCount
  264.    harvestTotal = harvestTotal + harvestCount
  265.  
  266.    print("Level " .. levelTotal .. ": " .. legCount .. " legs, " .. harvestCount .. " harvested")
  267.  
  268.    -- Ensure that the turtle has enough fuel to return to the starting position.
  269.  
  270.    refuel(legLength + 10)
  271.  
  272.    -- Return the turtle to its starting position. Turn it 90 degrees to the
  273.    -- right to backtrack along the last leg.
  274.  
  275.    turn("RIGHT")
  276.  
  277.    -- Move the turtle foward until it is even with its starting position.
  278.  
  279.    if heading == 0 or heading == 180 then
  280.       while yPos ~= 0 do
  281.          move()
  282.       end
  283.    elseif heading == 90 or heading == 270 then
  284.       while xPos ~= 0 do
  285.          move()
  286.       end
  287.    end
  288.  
  289.    -- Turn the turtle 90 degrees to the left, and move it to the starting
  290.    -- position.
  291.  
  292.    turn("LEFT")
  293.  
  294.    if heading == 0 or heading == 180 then
  295.       while yPos ~= 0 do
  296.          move()
  297.       end
  298.    elseif heading == 90 or heading == 270 then
  299.       while xPos ~= 0 do
  300.          move()
  301.       end
  302.    end
  303.  
  304.    -- Perform a series of right turns until the turtle is facing in its
  305.    -- original direction.
  306.  
  307.    while heading ~= 0 do
  308.       turn("RIGHT")
  309.    end
  310.  
  311.    -- Move down one block to sit on top of what will now be the next solid level.
  312.  
  313.    burrow(1)
  314.  
  315.    -- Store the last leg which resulted in harvesting in the historical
  316.    -- recording array, deleting the least-recent record.
  317.  
  318.    harvestLeg[3] = harvestLeg[2]
  319.    harvestLeg[2] = harvestLeg[1]
  320.    harvestLeg[1] = lastHarvestLeg  
  321.  
  322.    -- Determine whether so few items were harvested on this level that it
  323.    -- qualifies as an empty level, keeping track of how many empty levels
  324.    -- have been encountered in a row.
  325.  
  326.    if harvestCount / (levelUp + levelEven + levelDown) < EMPTY_HARVEST_LIMIT then
  327.       emptyLevels = emptyLevels + 1
  328.    else
  329.       emptyLevels = 0
  330.    end
  331.  
  332.    if emptyLevels >= EMPTY_LEVELS_LIMIT then
  333.        moveDown = 0
  334.    end if
  335.  
  336. end
  337.  
  338. while not turtle.detectDown() do
  339.    turtle.down()
  340. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement