Advertisement
Helmi

log2

Feb 15th, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. -- Log V2
  2.  
  3. local Sapling = 1
  4. local Tree = 2
  5. local Wall = 3
  6. local Home = 4
  7.  
  8. local Height = 1
  9.  
  10. local RandMove = 4
  11.  
  12. function checkSpace()
  13.   -- let's check if there's space for more wood
  14.   local space = 0
  15.  
  16.   for i=5,16 do
  17.  
  18.     turtle.select(i)
  19.  
  20.     if turtle.compareTo(Tree) then
  21.  
  22.       if turtle.getItemSpace(i) >= 1 then
  23.  
  24.         space = space + turtle.getItemSpace(i)
  25.  
  26.       end
  27.  
  28.     elseif turtle.getItemSpace(i) == 64 then
  29.  
  30.         space = space + 64
  31.  
  32.     end
  33.  
  34.   end
  35.  
  36.   if space >= 15 then
  37.  
  38.     rednet.send(Console,"Space:"..space.." Felling now!")
  39.     return true
  40.  
  41.   else
  42.  
  43.     rednet.send(Console,"Not enough space - not felling!")
  44.     return false
  45.  
  46.   end
  47.  
  48. end
  49.  
  50. function plantTree()
  51.  
  52.   turtle.select(Sapling)
  53.  
  54.   if turtle.getItemCount(Sapling) >= 2 then
  55.  
  56.     turtle.place(1)
  57.     rednet.send(Console, "Sapling placed, "..turtle.getItemCount(Sapling).." Saplings left")
  58.     return true
  59.  
  60.   else
  61.    
  62.     if saplingRefill() and turtle.getItemCount(Sapling) >= 2 then
  63.       turtle.place(1)
  64.       rednet.send(Console, "Sapling placed, "..turtle.getItemCount(Sapling).." Saplings left")
  65.       return true
  66.  
  67.     else
  68.  
  69.       return false
  70.  
  71.     end
  72.  
  73.   end
  74.  
  75.   if checkSpace() == false then
  76.  
  77.     goHome()
  78.  
  79.   end
  80.  
  81. end
  82.  
  83. function saplingRefill()
  84.  
  85.   for i = 5,16 do
  86.  
  87.     turtle.select(i)
  88.  
  89.     if turtle.compareTo(Sapling) then
  90.      
  91.       rednet.send(Console,"Found Saplings - refilling...")
  92.  
  93.       if turtle.transferTo(Sapling) then
  94.  
  95.         rednet.send(Console,"Refilled Saplings!")
  96.         return true
  97.      
  98.       else
  99.  
  100.         rednet.send(Console,"Sapling Refill failed!")
  101.         return false
  102.  
  103.       end
  104.  
  105.     end
  106.  
  107.   end
  108.  
  109. end
  110.  
  111. function farming()
  112.   -- do whatever we need to plant or harvest a tree
  113.  
  114.   if turtle.detect() then
  115.  
  116.     turtle.select(Sapling)
  117.  
  118.     if turtle.compare() then
  119.  
  120.       return true;
  121.  
  122.     else
  123.    
  124.       logTree()
  125.  
  126.     end
  127.  
  128.   else
  129.  
  130.     freeSpaceAround = true;
  131.  
  132.     if turtle.forward() then
  133.  
  134.       turtle.suck()
  135.      
  136.       if turtle.detect() then
  137.        
  138.         freeSpaceAround = false
  139.  
  140.       else
  141.  
  142.         turtle.back()
  143.  
  144.         for i=1,4 do
  145.        
  146.           turtle.turnRight()
  147.           turtle.suck()
  148.  
  149.           if turtle.detect() == true then
  150.  
  151.             logTree()
  152.             freeSpaceAround = false
  153.  
  154.           end
  155.        
  156.         end
  157.  
  158.       end
  159.    
  160.     else
  161.    
  162.       freeSpaceAround = false
  163.    
  164.     end
  165.  
  166.     if freeSpaceAround == true then
  167.  
  168.       if not plantTree() then
  169.  
  170.         rednet.send(Console,"Planting failed!")
  171.  
  172.       end
  173.  
  174.     end
  175.  
  176.   end
  177.  
  178. end
  179.  
  180. function logTree()
  181.  
  182.   turtle.select(Tree)
  183.  
  184.   if turtle.compare() then
  185.  
  186.     if checkSpace() then
  187.  
  188.       turtle.dig()
  189.       turtle.forward()
  190.  
  191.       while turtle.detectUp() do
  192.         turtle.digUp()
  193.         turtle.up()
  194.         Height = Height + 1
  195.         --print("Height: "..Height)
  196.       end
  197.  
  198.       while Height > 1 do
  199.         turtle.digDown()
  200.         turtle.down()
  201.         Height = Height - 1
  202.       end
  203.  
  204.       turtle.suck()
  205.       turtle.turnRight()
  206.       turtle.suck()
  207.       turtle.turnRight()
  208.       turtle.suck()
  209.       turtle.turnRight()
  210.       turtle.suck()
  211.       turtle.turnRight()
  212.       turtle.suck()
  213.       turtle.back()
  214.       turtle.suck()
  215.       turtle.select(Sapling)
  216.       plantTree()
  217.  
  218.     else
  219.  
  220.       goHome()
  221.  
  222.     end
  223.  
  224.   end
  225.  
  226. end
  227.  
  228. function move()
  229.   turn = math.random(2)
  230.  
  231.   if turn == 1 then
  232.     turtle.turnLeft()
  233.   else
  234.     turtle.turnRight()
  235.   end
  236.  
  237.   turtle.suck()
  238.  
  239.   rand = math.random(RandMove)
  240.  
  241.   for i=1,rand do
  242.     turtle.forward()
  243.     turtle.suck()
  244.   end
  245.  
  246. end
  247.  
  248. function goHome()
  249.  
  250. rednet.send(Console,"Going home...")
  251.  
  252. -- find the next wall
  253.  
  254.   failcount = 0
  255.  
  256.   while not turtle.compare() do
  257.  
  258.     turtle.select(Wall)
  259.     turtle.turnLeft()
  260.  
  261.     while not turtle.detect() do
  262.  
  263.         turtle.forward()
  264.  
  265.     end
  266.  
  267.     failcount = failcount + 1
  268.  
  269.     rednet.send(Console,"Failcount "..failcount)
  270.  
  271.     if failcount >= 5 then
  272.  
  273.       turtle.select(Sapling)
  274.  
  275.       if turtle.compare() then
  276.  
  277.         turtle.dig()
  278.         failcount = 0
  279.         turtle.forward()
  280.  
  281.       end
  282.  
  283.     end
  284.  
  285.   end
  286.  
  287.   rednet.send(Console,"Found wall - driving home along now...")
  288.  
  289. -- move along the wall to go home
  290.  
  291.   restart = false
  292.  
  293.   while not restart do
  294.  
  295.     while not turtle.detect() do
  296.  
  297.       turtle.forward()
  298.  
  299.     end
  300.  
  301.     turtle.select(Home)
  302.  
  303.     if turtle.compare() then
  304.  
  305.       -- we're at home - do some home stuff
  306.  
  307.       saplingRefill()
  308.  
  309.       turtle.select(Tree)
  310.       TreeNum = turtle.getItemCount(Tree)
  311.       turtle.dropDown(TreeNum-1)
  312.  
  313.       for i=5,16 do
  314.         turtle.select(i)
  315.         turtle.dropDown()
  316.       end
  317.  
  318.       turtle.turnRight()
  319.       turtle.turnRight()
  320.  
  321.       restart = true
  322.  
  323.       shell.run("reboot")
  324.  
  325.     else
  326.  
  327.       turtle.turnLeft()
  328.  
  329.     end
  330.  
  331.   end
  332.  
  333.   return true
  334.  
  335. end
  336.  
  337.  
  338. -- The program starts here!
  339.  
  340. term.clear()
  341. print("Logger V2 - by Helmi74")
  342. print("----------------------")
  343.  
  344.  
  345. rednet.open("right")
  346.  
  347. rednet.send(Console,"starting to work now...")
  348.  
  349. ready = true
  350.  
  351. for i=1,4 do
  352.   turtle.select(i)
  353.   if turtle.getItemCount(i) == 0 then
  354.     print("not ready - please fill slots 1 to 4")
  355.     print("--- --- --- --- ---")
  356.     print("Put a stack of saplings in slot "..Sapling)
  357.     print("Put one tree log of the same sort in slot "..Tree)
  358.     print("Put one wall block in slot "..Wall)
  359.     print("Put one home block in slot "..Home)
  360.  
  361.     rednet.send(Conosole,"Not ready -instructions printed to local console")
  362.     ready = false
  363.     break
  364.   end
  365. end    
  366.  
  367. if ready == true then
  368.  
  369.   while true do
  370.  
  371.     move()
  372.     farming()
  373.  
  374.   end
  375.  
  376. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement