Advertisement
slay_mithos

Logging v1.2

Jan 11th, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. local args = {...}
  2. local saplingSlot = 1
  3. local logSlot = 2
  4. local length = 20
  5. local rows = 2
  6. local treeSpacing = 4
  7. local rowsSpacing = 4
  8. local endOfLineOffset = 3
  9. local maxCuttingHeight = 10
  10.  
  11. local repeats = 1
  12.  
  13.  
  14. local function forward()
  15.     while (not turtle.forward()) do
  16.         if (turtle.detect()) then
  17.             turtle.dig()
  18.         else
  19.             turtle.attack()
  20.         end
  21.     end
  22. end
  23.  
  24. local function turn(curRow)
  25.     if (curRow%2 == 1) then
  26.         turtle.turnRight()
  27.     else
  28.         turtle.turnLeft()
  29.     end
  30. end
  31.  
  32. local function detectTree()
  33.     if (turtle.detectUp()) then
  34.         turtle.select(logSlot)
  35.         if (turtle.compareUp()) then
  36.             return true
  37.         end
  38.     end
  39.     return false
  40. end
  41.  
  42. local function cutTree()
  43.     local height = 0
  44.     turtle.digDown()
  45.     while (detectTree()) do
  46.         turtle.digUp()
  47.         if (height < maxCuttingHeight) then
  48.             while (not turtle.up()) do
  49.                 if (turtle.detectUp()) then
  50.                     turtle.digUp()
  51.                 else
  52.                     turtle.attackUp()
  53.                 end
  54.             end
  55.         end
  56.         height = height +1
  57.     end
  58.     for i=1, height do
  59.         while (not turtle.down()) do
  60.             if (turtle.detectDown()) then
  61.                 turtle.digDown()
  62.             else
  63.                 turtle.attackDown()
  64.             end
  65.         end
  66.     end
  67. end
  68.  
  69. local function plantSapling()
  70.     turtle.select(saplingSlot)
  71.     turtle.placeDown()
  72.     turtle.suckDown()
  73. end
  74.  
  75. local function moveLoop()
  76.    
  77.     for r=1, rows do
  78.         for i=1, endOfLineOffset do
  79.             forward()
  80.         end
  81.        
  82.         for i=1, length do
  83.             if (i%treeSpacing == 0 and turtle.getItemCount(saplingSlot)>1 ) then
  84.                 plantSapling()
  85.             end
  86.            
  87.             forward()
  88.             if (detectTree()) then
  89.                 cutTree()
  90.             end
  91.         end
  92.        
  93.         for i=1, endOfLineOffset do
  94.             forward()
  95.         end
  96.        
  97.         if (r ~= rows) then
  98.             turn(r)
  99.             for i=1, rowsSpacing do
  100.                 forward()
  101.             end
  102.             turn(r)
  103.         end
  104.     end
  105. end
  106.  
  107. local function returnBase()
  108.     local distanceToBase = (rows-1) * rowsSpacing -1
  109.     local distanceToRowStart = 0
  110.    
  111.     if (rows%2 == 1) then
  112.         distanceToRowStart = length+2*endOfLineOffset-1
  113.         turn(1)
  114.         forward()
  115.         turn(1)
  116.         for i=0, distanceToRowStart do
  117.             forward()
  118.         end
  119.         turn(1)
  120.         forward()
  121.     else
  122.         turn(1)
  123.     end
  124.    
  125.     for i=0, distanceToBase do
  126.         forward()
  127.     end
  128.     turn(1)
  129. end
  130.  
  131. local function messageNeedSapplings()
  132.     term.clear()
  133.     print("Please put the correct sort of saplings in the first slot\nor in the chest the turtle is currently facing.")
  134.     os.sleep(30)
  135. end
  136.  
  137. local function manageInventory()
  138.     local logDropCount = turtle.getItemCount(2)-1
  139.     local fuelNeeded = ((length + (2*endOfLineOffset) + rowsSpacing) * rows + ((length/treeSpacing) * rows * maxCuttingHeight) ) - turtle.getFuelLevel()
  140.     local saplingsNeeded = (length/treeSpacing*rows) - turtle.getItemCount(1)
  141.    
  142.     turtle.turnLeft()
  143.     if (saplingsNeeded >0) then
  144.         turtle.select(1)
  145.         while (saplingsNeeded >0) do
  146.             if (turtle.suck(saplingsNeeded)) then
  147.                 saplingsNeeded = (length/treeSpacing*rows) - turtle.getItemCount(1)
  148.                 if (saplingsNeeded > 0) then
  149.                     messageNeedSapplings()
  150.                 end
  151.             else
  152.                 messageNeedSapplings()
  153.             end
  154.         end
  155.        
  156.     end
  157.    
  158.     turtle.select(2)
  159.     turtle.dropDown(logDropCount)
  160.    
  161.    
  162.     for i=3, 16 do
  163.         turtle.select(i)
  164.         if (turtle.compareTo(1)) then
  165.             turtle.drop()
  166.         else
  167.             turtle.dropDown()
  168.         end
  169.     end
  170.    
  171.     while (fuelNeeded > 0) do
  172.         if (turtle.suckUp()) then
  173.             print("Refuelling...")
  174.         else
  175.             term.clear()
  176.             print("Waiting for fuel in the last inventory slot\nor in an inventory on top.")
  177.             os.sleep(30)
  178.         end
  179.         turtle.refuel()
  180.         fuelNeeded = ((length + (2*endOfLineOffset) + rowsSpacing) * rows + ((length/treeSpacing) * rows * maxCuttingHeight) ) - turtle.getFuelLevel()
  181.     end
  182.    
  183.     turtle.turnRight()
  184. end
  185.  
  186. local function mainLoop()
  187.     local i=0
  188.     if (args[1] > 1) then
  189.         repeats = args[1]
  190.     end
  191.     while (i<repeats) do
  192.         term.clear()
  193.         print("Managing inventory")
  194.         manageInventory()
  195.         print("Starting move loop")
  196.         moveLoop()
  197.         print("Returning base")
  198.         returnBase()
  199.        
  200. --~         Real line:
  201.         os.sleep(300)
  202.        
  203.         i = i+1
  204.        
  205.     end
  206. end
  207.  
  208. mainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement