Erlendftw

Trefarm rev 3

May 24th, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --VARS
  2.  
  3. farm_length = 7;
  4. farm_width = 7;
  5.  
  6. turtle_slot_current = 1
  7. turtle_slot_saplings_1 = 1
  8. turtle_slot_saplings_2 = 2
  9. turtle_slot_dirt_1 = 3
  10. turtle_slot_dirt_2 = 4
  11.  
  12.  
  13. --COMMON FUNCTIONS
  14.  
  15. function printMsg(content)
  16.     print('[TreeBot] ' .. content)
  17. end
  18.  
  19. function exit(msg)
  20.     error(msg)
  21.     return
  22. end
  23.  
  24.  
  25. -- COMMON TURTLE FUNCTIONS
  26. function selectSlot(slot)
  27.     turtle_slot_current = slot
  28.     turtle.select(slot)
  29. end
  30.  
  31. function findSaplingSlot()
  32.     if ((turtle.getItemCount(turtle_slot_saplings_1)) > 0) then
  33.         selectSlot(turtle_slot_saplings_1)
  34.     elseif ((turtle.getItemCount(turtle_slot_saplings_2)) > 0) then
  35.         selectSlot(turtle_slot_saplings_2)
  36.     else
  37.         exit('No saplings available')
  38.     end
  39. end
  40.  
  41. function findDirtSlot()
  42.     if ((turtle.getItemCount(turtle_slot_dirt_1)) > 0) then
  43.         selectSlot(turtle_slot_dirt_1)
  44.     elseif ((turtle.getItemCount(turtle_slot_dirt_2)) > 0) then
  45.         selectSlot(turtle_slot_dirt_2)
  46.     else
  47.         exit('No dirt available')
  48.     end
  49. end
  50.  
  51. function storage_disposeAllItems()
  52.     --dispose all items
  53.     for i = 1, 16 do
  54.         selectSlot(i)
  55.         turtle.drop()
  56.     end
  57. end
  58.  
  59. function moveDig(direction, times)
  60.  
  61.     for i = 1, times do
  62.         if (direction == 'forward') then
  63.             while(turtle.forward() ~= true) do
  64.                 while (turtle.detect()) do
  65.                     turtle.dig()
  66.                 end
  67.             end
  68.         elseif (direction == 'up') then
  69.             while(turtle.up() ~= true) do
  70.                 while (turtle.detectUp()) do
  71.                     turtle.digUp()
  72.                 end
  73.             end
  74.         elseif (direction == 'down') then
  75.             while(turtle.down() ~= true) do
  76.                 while (turtle.detectDown()) do
  77.                     turtle.digDown()
  78.                 end
  79.             end
  80.         end
  81.     end
  82. end
  83.  
  84. function rotate(direction, times)
  85.     for i = 1, times do
  86.         if (direction == 'left') then
  87.             turtle.turnLeft()
  88.         elseif (direction == 'right') then
  89.             turtle.turnRight()
  90.         end
  91.     end
  92. end
  93.  
  94. --SINGLE FUNCTIONS
  95.  
  96. function checkCurrentSpot()
  97.     if (turtle.detect()) then
  98.        
  99.         printMsg('Found tree, trying to remove')
  100.  
  101.         newHeight = 0
  102.         while (turtle.detect()) do
  103.             moveDig('up', 1)
  104.             newHeight = newHeight + 1
  105.         end
  106.  
  107.         moveDig('forward', 1)
  108.  
  109.         for i = 1, (newHeight + 1) do
  110.             moveDig('down', 1)
  111.         end
  112.         moveDig('up', 1)
  113.  
  114.         findSaplingSlot()
  115.         turtle.placeDown()
  116.  
  117.         printMsg('Tree removed')   
  118.         return true
  119.     else
  120.         return false
  121.     end
  122. end
  123.  
  124.  
  125. -- FUNCTIONALITY FUNCTIONS
  126.  
  127. function checkFarm()
  128.     --bot is at collection position
  129.     --check that storage is valid etc
  130.     processStorage(false)
  131.  
  132.     moveDig('forward', 5)
  133.  
  134.     --bot is at init position
  135.     currWidth = 0
  136.     while (currWidth < farm_width) do
  137.     currWidth = currWidth + 1
  138.  
  139.         for currLength = 1, farm_length do
  140.  
  141.             printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
  142.  
  143.             removeResult = checkCurrentSpot()
  144.                
  145.             if (removeResult ~= true) then
  146.                 moveDig('forward', 1)
  147.                 tile_saplingIsPlanted = turtle.detectDown()
  148.                 if (tile_saplingIsPlanted ~= true) then
  149.                     --check wether dirt exists
  150.                     moveDig('down', 1)
  151.                     tile_dirtIsPlanted = turtle.detectDown()
  152.                     if (tile_dirtIsPlanted ~= true) then
  153.                         findDirtSlot()
  154.                         turtle.placeDown()
  155.                     end
  156.                     moveDig('up', 1)
  157.  
  158.                     findSaplingSlot()
  159.                     turtle.placeDown()
  160.                 end
  161.                 moveDig('forward', 3)
  162.             else
  163.                 moveDig('forward', 3)
  164.             end
  165.  
  166.             --check if it should return
  167.        
  168.             if (currLength == farm_length) then
  169.                 moveDig('forward', 1)
  170.                 rotate('left', 1)
  171.                 moveDig('forward', 4)
  172.                 rotate('left', 1)
  173.                 moveDig('forward', 3)
  174.  
  175.                 currWidth = currWidth + 1
  176.  
  177.                 -- check if operation is complete
  178.                 if (currWidth < farm_width) then
  179.  
  180.                     for backLength = 1, (farm_length) do
  181.                         --process on the way home
  182.  
  183.                         printMsg('Length: ' .. currLength .. ' Width: ' .. currWidth)
  184.  
  185.                         removeResult = checkCurrentSpot()
  186.                         if (removeResult ~= true) then
  187.                             moveDig('forward', 1)
  188.                             tile_saplingIsPlanted = turtle.detectDown()
  189.                             if (tile_saplingIsPlanted ~= true) then
  190.                                 --check wether dirt exists
  191.                                 moveDig('down', 1)
  192.                                 tile_dirtIsPlanted = turtle.detectDown()
  193.                                 if (tile_dirtIsPlanted ~= true) then
  194.                                     findDirtSlot()
  195.                                     turtle.placeDown()
  196.                                 end
  197.                                 moveDig('up', 1)
  198.  
  199.                                 findSaplingSlot()
  200.                                 turtle.placeDown()
  201.                             end
  202.                             moveDig('forward', 3)
  203.                         else
  204.                             moveDig('forward', 3)
  205.                         end
  206.                     end
  207.  
  208.                     rotate('right', 1)
  209.                     moveDig('forward', 4)
  210.                     rotate('right', 1)
  211.                     moveDig('forward', 2)
  212.                 else
  213.                     --operation is complete
  214.                     moveDig('forward', (farm_length * 4))
  215.                     rotate('left', 1)
  216.                     moveDig('forward', (farm_width * 4)) -- line up with last col
  217.                     rotate('right', 1)
  218.                     moveDig('forward', 3)
  219.                    
  220.                     storage_disposeAllItems()
  221.  
  222.                     rotate('right', 2)
  223.                 end
  224.             end
  225.            
  226.        
  227.            
  228.         end
  229.     end
  230.  
  231.     printMsg('End of journey. Returning to original position')
  232. end
  233.  
  234. function processStorage(isFacingDump)
  235.     if (isFacingDump ~= true) then
  236.         rotate('right', 2)
  237.     end
  238.  
  239.     storage_disposeAllItems()
  240.  
  241.     --check fuel level
  242.     if (turtle.getFuelLevel() < 2000) then
  243.         moveDig('up', 2)
  244.         selectSlot(1)
  245.         turtle.suck()
  246.  
  247.         fuelUnits = 0
  248.         while ((turtle.getItemCount(1) >= 1) and (turtle.getFuelLevel() <= 8000)) do
  249.             fuelUnits = fuelUnits + 1
  250.             turtle.refuel(1)
  251.             printMsg('Refueled ' .. fuelUnits .. ' units')
  252.         end
  253.  
  254.         moveDig('down', 2)
  255.  
  256.         storage_disposeAllItems()
  257.     end
  258.  
  259.     --load up saplings
  260.     rotate('left', 1)
  261.     selectSlot(turtle_slot_saplings_1)
  262.     turtle.suck()
  263.     selectSlot(turtle_slot_saplings_2)
  264.     turtle.suck()
  265.  
  266.     --load up dirt
  267.     rotate('right', 2)
  268.     selectSlot(turtle_slot_dirt_1)
  269.     turtle.suck()
  270.     selectSlot(turtle_slot_dirt_2)
  271.     turtle.suck()
  272.  
  273.     --rotate to init position
  274.     rotate('right', 1)
  275. end
  276.  
  277. checkFarm();
  278.  
  279. currsec = 1680
  280. while (true) do
  281.     if (currsec <= 0) then
  282.         checkFarm()
  283.         currsec = 1680
  284.     else
  285.         printMsg('New round in ' .. currsec .. ' sec')
  286.         currsec = currsec - 1
  287.         sleep(1)
  288.     end
  289. end
Advertisement
Add Comment
Please, Sign In to add comment