Erlendftw

autoTreFarm Beta 1

Apr 10th, 2013
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.47 KB | None | 0 0
  1. --VARS
  2.  
  3. farm_length = 5;
  4. farm_width = 5;
  5.  
  6. turtle_slot_saplings_1 = 1
  7. turtle_slot_saplings_2 = 2
  8. turtle_slot_dirt_1 = 3
  9. turtle_slot_dirt_2 = 4
  10.  
  11.  
  12. --COMMON FUNCTIONS
  13.  
  14. function printMsg(content)
  15.     print('[TreeBot] ' .. content)
  16. end
  17.  
  18. function exit(msg)
  19.     error(msg)
  20.     return
  21. end
  22.  
  23.  
  24. -- COMMON TURTLE FUNCTIONS
  25. function selectSlot(slot)
  26.     turtle_slot_current = slot
  27.     turtle.select(slot)
  28. end
  29.  
  30. function findSaplingSlot()
  31.     if ((turtle.getItemCount(turtle_slot_saplings_1)) > 0) then
  32.         selectSlot(turtle_slot_saplings_1)
  33.     elseif ((turtle.getItemCount(turtle_slot_saplings_2)) > 0) then
  34.         selectSlot(turtle_slot_saplings_2)
  35.     else
  36.         exit('No saplings available')
  37.     end
  38. end
  39.  
  40. function findDirtSlot()
  41.     if ((turtle.getItemCount(turtle_slot_dirt_1)) > 0) then
  42.         selectSlot(turtle_slot_dirt_1)
  43.     elseif ((turtle.getItemCount(turtle_slot_dirt_2)) > 0) then
  44.         selectSlot(turtle_slot_dirt_2)
  45.     else
  46.         exit('No dirt available')
  47.     end
  48. end
  49.  
  50. function moveDig(direction, times)
  51.  
  52.     for i = 1, times do
  53.         if (direction == 'forward') then
  54.             while(turtle.forward() ~= true) do
  55.                 while (turtle.detect()) do
  56.                     turtle.dig()
  57.                 end
  58.             end
  59.         elseif (direction == 'up') then
  60.             while(turtle.up() ~= true) do
  61.                 while (turtle.detectUp()) do
  62.                     turtle.digUp()
  63.                 end
  64.             end
  65.         elseif (direction == 'down') then
  66.             while(turtle.down() ~= true) do
  67.                 while (turtle.detectDown()) do
  68.                     turtle.digDown()
  69.                 end
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. function rotate(direction, times)
  76.     for i = 1, times do
  77.         if (direction == 'left') then
  78.             turtle.turnLeft()
  79.         elseif (direction == 'right') then
  80.             turtle.turnRight()
  81.         end
  82.     end
  83. end
  84.  
  85. --SINGLE FUNCTIONS
  86.  
  87. function checkCurrentSpot()
  88.     if (turtle.detect()) then
  89.        
  90.         printMsg('Found tree, trying to remove')
  91.  
  92.         newHeight = 0
  93.         while (turtle.detect()) do
  94.             moveDig('up', 1)
  95.             newHeight = newHeight + 1
  96.         end
  97.  
  98.         moveDig('forward', 1)
  99.  
  100.         for i = 1, (newHeight + 1) do
  101.             moveDig('down', 1)
  102.         end
  103.         moveDig('up', 1)
  104.  
  105.         findSaplingSlot()
  106.         turtle.placeDown()
  107.  
  108.         printMsg('Tree removed')   
  109.         return true
  110.     else
  111.         return false
  112.     end
  113. end
  114.  
  115.  
  116. -- FUNCTIONALITY FUNCTIONS
  117.  
  118. function checkFarm()
  119.     --bot is at init position
  120.     currWidth = 0
  121.     while (currWidth < farm_width) do
  122.     currWidth = currWidth + 1
  123.  
  124.         for currLength = 1, farm_length do
  125.  
  126.             printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
  127.  
  128.             removeResult = checkCurrentSpot()
  129.                
  130.             if (removeResult ~= true) then
  131.                 moveDig('forward', 1)
  132.                 tile_saplingIsPlanted = turtle.detectDown()
  133.                 if (tile_saplingIsPlanted ~= true) then
  134.                     --check wether dirt exists
  135.                     moveDig('down', 1)
  136.                     tile_dirtIsPlanted = turtle.detectDown()
  137.                     if (tile_dirtIsPlanted ~= true) then
  138.                         findDirtSlot()
  139.                         turtle.placeDown()
  140.                     end
  141.                     moveDig('up', 1)
  142.  
  143.                     findSaplingSlot()
  144.                     turtle.placeDown()
  145.                 end
  146.                 moveDig('forward', 3)
  147.             else
  148.                 moveDig('forward', 3)
  149.             end
  150.  
  151.             --check if it should return
  152.        
  153.             if (currLength == farm_length) then
  154.                 moveDig('forward', 1)
  155.                 rotate('left', 1)
  156.                 moveDig('forward', 4)
  157.                 rotate('left', 1)
  158.                 moveDig('forward', 3)
  159.  
  160.                 currWidth = currWidth + 1
  161.  
  162.                 -- check if operation is complete
  163.                 if (currWidth < farm_width) then
  164.  
  165.                     for backLength = 1, (farm_length) do
  166.                         --process on the way home
  167.  
  168.                         printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
  169.  
  170.                         removeResult = checkCurrentSpot()
  171.                         if (removeResult ~= true) then
  172.                             moveDig('forward', 1)
  173.                             tile_saplingIsPlanted = turtle.detectDown()
  174.                             if (tile_saplingIsPlanted ~= true) then
  175.                                 --check wether dirt exists
  176.                                 moveDig('down', 1)
  177.                                 tile_dirtIsPlanted = turtle.detectDown()
  178.                                 if (tile_dirtIsPlanted ~= true) then
  179.                                     findDirtSlot()
  180.                                     turtle.placeDown()
  181.                                 end
  182.                                 moveDig('up', 1)
  183.  
  184.                                 findSaplingSlot()
  185.                                 turtle.placeDown()
  186.                             end
  187.                             moveDig('forward', 3)
  188.                         else
  189.                             moveDig('forward', 3)
  190.                         end
  191.                     end
  192.  
  193.                     rotate('right', 1)
  194.                     moveDig('forward', 4)
  195.                     rotate('right', 1)
  196.                     moveDig('forward', 2)
  197.                 else
  198.                     --operation is complete
  199.                     moveDig('forward', (farm_length * 4))
  200.                     rotate('left', 1)
  201.                     moveDig('forward', (farm_width * 4)) -- line up with last col
  202.                     rotate('left', 1)
  203.                     moveDig('forward', 2) --now at original position
  204.  
  205.                 end
  206.             end
  207.            
  208.        
  209.            
  210.         end
  211.     end
  212.  
  213.     printMsg('End of journey. Returning to original position')
  214. end
  215.  
  216. checkFarm();
Advertisement
Add Comment
Please, Sign In to add comment