Advertisement
TriiNoxYs

[ComputerCraft] WoodCutter

Jul 6th, 2014
602
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.02 KB | None | 0 0
  1. -- Ce programme [cree pas TriiNoxYs] est libre de droits ;)
  2. -- Si vous le modifier/publiez/presentez/etc, SVP pensez a citer son auteur.
  3. -- Consultez tout les autres scripts de TriiNoxys sur le lien pastein.com/benjfn71
  4. -- Ou sur la page de TriiNoxYs: pastebin.com/u/TriiNoxYs
  5.  
  6. local InfiniteWork = true --set an infinite loop or not
  7. local CoolDownDuration = 10--idle time to let the trees grow : not necessary for big forrests.
  8. local MinimumRide = 800 --necessary fuel to start : the max fuel consumation is if the forrest is full of grown trees.
  9. local MinimumRoots = 8 --The turtle does not start if there is less saplings.
  10. local FurnaceFuelDose = 8 --amount of charcoal to save for the furnace : 8 means a stack of wood can burn
  11. local MinFuelStock = 10 --turtle always keeps this minimal amount of charcoal. The furnace will use wood if there is too few charcoals.
  12. local FuelSlot = 16
  13. local RootSlot = 15
  14. local EdgeSlot = 14
  15. local WoodSlot = 1
  16. local Roof = 5  --max ups that the turtle has to do (inc ase there is a roof)
  17. local dir = {L=0,R=1,U=2,D=3,F=4}
  18. local AvoidRoot = { dir.R,dir.F,dir.L,dir.F,dir.F, dir.L, dir.F,dir.R }
  19. local RightTurn = { dir.R,dir.F,dir.F,dir.F,dir.F, dir.R}
  20. local LeftTurn =  { dir.L,dir.F,dir.F,dir.F,dir.F, dir.L}
  21. local bAltDirection
  22.  
  23. function move(directions)
  24.     turtle.select(RootSlot)
  25.     for i=0, table.maxn(directions) do
  26.         direction = directions[i]
  27.         if direction==dir.L then
  28.             turtle.turnLeft()
  29.         elseif direction==dir.R then
  30.             turtle.turnRight()
  31.         elseif direction==dir.U then       
  32.             if turtle.up()==false then
  33.                 return false
  34.             end
  35.         elseif direction==dir.D then
  36.             if turtle.down()==false then
  37.                 return false
  38.             end
  39.         elseif direction==dir.F then
  40.             if turtle.forward()==false then
  41.                 return false
  42.             end
  43.         end
  44.             turtle.suck()
  45.     end
  46.     return true;
  47. end
  48. function goBack()
  49.     turtle.turnRight()
  50.     while turtle.detect()==false do
  51.         turtle.suck()
  52.         turtle.forward()
  53.     end
  54.     if turtle.detectDown() then
  55.         goBack()
  56.     end
  57. end
  58. function turn180()
  59.     turtle.turnRight()
  60.     turtle.turnRight()
  61. end
  62. function meetEdge()
  63.     local success
  64.     if bAltDirection>0 then
  65.         success= move(LeftTurn)
  66.     else
  67.         success= move(RightTurn)
  68.     end
  69.     if success then
  70.         bAltDirection = (bAltDirection+1)%2
  71.         doRow(0)
  72.     else
  73.         goBack()
  74.     end
  75.        
  76. end
  77. function dig360()
  78.     turtle.select(EdgeSlot)
  79.     if turtle.compare()==false then --avoid breaking the edge slot..
  80.         turtle.dig() turtle.suck()
  81.     end
  82.     turtle.turnLeft()
  83.     turtle.dig()turtle.suck()
  84.     turn180()--nothing to suck behind
  85.     turtle.dig()turtle.suck()
  86.     turtle.turnLeft()  
  87. end
  88. function digTree()
  89.     turtle.dig()
  90.     turtle.suck()
  91.     turtle.select(RootSlot)
  92.     turtle.digUp()
  93.     turtle.suckUp()
  94.  
  95.     if turtle.getItemCount(RootSlot)>1 then
  96.         turtle.place() 
  97.     end
  98.     turtle.up()
  99.     ups = 1
  100.    
  101.     while turtle.detect() and ups<Roof do      
  102.         dig360()       
  103.         turtle.digUp()turtle.suckUp()
  104.         turtle.up()
  105.         ups=ups+1
  106.     end
  107.     turtle.dig()
  108.     turtle.forward()
  109.     dig360()
  110.     turtle.forward()
  111.     if ups==0 then return end
  112.     while ups>0 do
  113.         dig360()
  114.         turtle.digDown()
  115.         turtle.suckDown()
  116.         turtle.down()
  117.         ups=ups-1
  118.     end
  119.    
  120.     -- turtle.dig()
  121.     -- turtle.forward()
  122.     -- ups = 0
  123.     -- turtle.select(WoodSlot)
  124.     -- while turtle.compareUp() do
  125.         -- turtle.digUp()
  126.         -- turtle.up()
  127.         -- ups=ups+1
  128.     -- end
  129.     -- if ups==0 then
  130.         -- return
  131.     -- end
  132.     -- while ups>1 do --back to level 1 to plant the new root !
  133.         -- turtle.down()
  134.         -- ups=ups-1
  135.     -- end
  136.  
  137.     -- if turtle.getItemCount(RootSlot)>0 then
  138.         -- turtle.select(RootSlot)
  139.         -- turtle.placeDown()
  140.         -- turtle.dig()--make one step in front to save the root
  141.         -- turtle.forward()
  142.         -- turtle.down()
  143.     -- else
  144.         -- turtle.down()
  145.     -- end
  146. end
  147. function doRow(offset)
  148.     turtle.select(RootSlot)
  149.     stepcounter=offset
  150.     while turtle.detect()==false do
  151.         turtle.suck()
  152.         if stepcounter%4==1 and turtle.detectDown() and turtle.getItemCount(RootSlot)>1 then
  153.             --detection means the turtle can plant
  154.             turtle.forward()
  155.             turn180()
  156.             turtle.select(RootSlot)
  157.             turtle.place()
  158.             turn180()          
  159.         else
  160.             turtle.forward()
  161.         end
  162.         stepcounter=stepcounter+1
  163.     end
  164.     --detect edge
  165.     turtle.select(EdgeSlot)
  166.     if turtle.compare() then
  167.         meetEdge()
  168.     else
  169.         turtle.select(WoodSlot)
  170.         if turtle.compare() then
  171.             digTree()
  172.         else           
  173.             if move(AvoidRoot)==false then
  174.                 error("Unexpected block during AvoidRoot operation")
  175.             end    
  176.         end
  177.         doRow(2)
  178.         return
  179.     end
  180. end
  181. function unloadInventory()
  182.     turtle.select(WoodSlot)
  183.     if (turtle.getItemCount(WoodSlot)>1) then
  184.         turtle.dropUp(turtle.getItemCount(WoodSlot)-1)--keep one block of wood
  185.     end
  186.     index = WoodSlot+1 
  187.     while index<EdgeSlot and turtle.getItemCount(index) > 0 do
  188.         turtle.select(index)
  189.         turtle.drop()
  190.         index=index+1
  191.     end
  192. end
  193. function furnaceAndChestOp()
  194.     --front of the furnace.
  195.    
  196.     --Drop furnace fuel : charcoal first, but keep minimal amount
  197.     dropped = false
  198.     if turtle.getItemCount(FuelSlot) >= (FurnaceFuelDose+MinFuelStock) then
  199.         turtle.select(FuelSlot)
  200.         if turtle.drop(FurnaceFuelDose) then
  201.             dropped=true
  202.             print("turtle has dropped "..FurnaceFuelDose.." charcoals !")
  203.         end
  204.     end
  205.     if dropped==false and turtle.getItemCount(WoodSlot)>1 then
  206.         --drop wood instead
  207.         turtle.select(WoodSlot)
  208.         woodtodrop = math.min(FurnaceFuelDose, turtle.getItemCount(WoodSlot)-1)
  209.         turtle.drop(woodtodrop)
  210.         print("turtle has dropped "..woodtodrop.." Wood")
  211.     end
  212.    
  213.     --Drop wood
  214.     turtle.digUp()--yes, there can be a leaf block
  215.     turtle.up()
  216.     turtle.forward()
  217.     turn180()
  218.    
  219.     turtle.select(WoodSlot)
  220.     if turtle.getItemCount(WoodSlot)>1 then
  221.         turtle.dropDown(turtle.getItemCount(WoodSlot)-1)
  222.     end
  223.    
  224.     index=WoodSlot+1
  225.     turtle.select(index)
  226.     while index < EdgeSlot and turtle.compareTo(WoodSlot) and turtle.getItemCount(index)>0 do
  227.         if turtle.dropDown()==false then --fails if the furnace is full
  228.             index=EdgeSlot --Leave Loop
  229.         end
  230.         index=index+1
  231.         turtle.select(index)
  232.     end
  233.    
  234.     step=10
  235.     delay = CoolDownDuration/10
  236.     while step<=100 do
  237.         sleep(delay)       
  238.         print(step.."%")
  239.         step=step+10
  240.     end
  241.     --suck results
  242.     turtle.forward()
  243.     turtle.down()
  244.     turtle.down()
  245.     turtle.back()
  246.     turtle.select(FuelSlot)
  247.     turtle.suckUp()
  248.    
  249.     --go to chest
  250.     turtle.forward()
  251.     turtle.turnLeft()
  252.     turtle.up()
  253.     unloadInventory()
  254.    
  255.     --back to position
  256.     turtle.turnRight()
  257. end
  258.  
  259. function mainloop()
  260.     --checking fuel
  261.     turtle.select(FuelSlot)
  262.     while turtle.getFuelLevel()< MinimumRide do
  263.         if turtle.refuel(1)==false then
  264.             error("The turtle needs more fuel on slot 16")
  265.         end
  266.     end
  267.     --checking saplings
  268.     if turtle.getItemCount(RootSlot)< MinimumRoots then
  269.         error("The turtle needs more roots on slot 15")
  270.     end
  271.     print("starting run with fuel level "..turtle.getFuelLevel().." and "..turtle.getItemCount(RootSlot).." saplings")
  272.     bAltDirection=0
  273.     doRow(0)
  274.     print("finishing run with fuel level "..turtle.getFuelLevel().." and "..turtle.getItemCount(RootSlot).." saplings")
  275.     turtle.turnLeft()--face furnace
  276.     furnaceAndChestOp()
  277. end
  278.  
  279.  
  280. mainloop()
  281. print("Wood collect over")
  282.  
  283. while InfiniteWork==true do
  284.     parallel.waitForAny(
  285.         mainloop(),
  286.         function()
  287.             ans = read()
  288.             error("bye")
  289.         end)
  290. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement