Advertisement
meuced

WoodCutter.lua

May 11th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.54 KB | None | 0 0
  1. local p = peripheral.wrap("back")
  2.  
  3. function detect()               -- on détecte si l'arbre a poussé ou pas
  4.     local success, res = turtle.inspect()
  5.     print(res.name)
  6.     if res.name == "minecraft:log" then
  7.         return true
  8.     else
  9.         return false
  10.     end
  11. end
  12.  
  13. function detectUp()             -- on détecte la fin de l'arbre ou pas
  14.     local success, res = turtle.inspectUp()
  15.     if res.name == "minecraft:log" then
  16.         return true
  17.     else
  18.         return false
  19.     end
  20. end
  21.  
  22. function leavesCleaning()       -- on nettoie les feuilles autour de la turtle + récup de saplings
  23.     local i = 1
  24.     for i=1,4 do
  25.         turtle.dig()
  26.         turtle.turnRight()
  27.     end
  28. end
  29.  
  30. function searchSaplingTurtle()  -- on cherche le slot de la turtle contenant les saplings
  31.     local s = 1
  32.     for s=1,16 do
  33.         local res = turtle.getItemDetail(s)
  34.         if res ~= nil then
  35.             if res.name == "minecraft:sapling" then
  36.                 return s
  37.             end
  38.         end
  39.     end
  40.     return false
  41. end
  42.  
  43. function getSaplingChest()      -- On prend un sapling dans le coffre
  44.     local fin = p.getInventorySize()
  45.     while true do
  46.         local s = 1
  47.         for s=1,fin do
  48.             local res = p.getStackInSlot(s)
  49.             if res ~= nil then
  50.                 if res["id"] == "minecraft:sapling" then
  51.                     return p.pushItemIntoSlot("south",s,1)
  52.                 end
  53.             end
  54.         end
  55.         term.clear()
  56.         print("Pas de sapling dans le coffre, en attente...")
  57.         sleep(1)
  58.     end
  59. end
  60.  
  61. function planteSapling()        -- on plante un sapling
  62.     local s = searchSaplingTurtle()
  63.     print(s)    -- on cherche le slot des saplings dans la turtle
  64.     if s ~= false then
  65.         turtle.select(s)
  66.         turtle.place()              -- si on en trouve, on le plante
  67.         return true
  68.     else                            -- sinon on en cherche dans le coffre
  69.         getSaplingChest()       -- si plus de sapling dans le coffre alors on attend avec un message à l'écran
  70.         planteSapling()         -- on plante dès qu'on a de nouveau des saplings
  71.         return true
  72.     end
  73. end
  74.  
  75. function timber()               -- on coupe l'arbre
  76.     local r = 0
  77.     print(r)
  78.     turtle.dig()
  79.     turtle.forward()
  80.     turtle.digUp()
  81.     turtle.up()
  82.     r = r + 1
  83.     leavesCleaning()
  84.     print(r)
  85.     while detectUp() do
  86.         turtle.digUp()
  87.         turtle.up()
  88.         r = r + 1
  89.         leavesCleaning()
  90.         print("fin boucle while "..r)
  91.     end
  92.     while r > 0 do
  93.         turtle.down()
  94.         r = r -1
  95.         print("fin boucle while down "..r)
  96.     end
  97.     turtle.back()
  98.     planteSapling()
  99. end
  100.  
  101. function main()             -- fonction principale
  102.     while true do
  103.         if detect() then
  104.             timber()
  105.         else
  106.             sleep(5)
  107.         end
  108.     end
  109. end
  110.  
  111. main()
  112.  
  113. -- getInventorySize() renvoie un chiffre
  114. -- getStackInSlot(slotnum) renvoie un tableau, notamment la clé raw_name = "item.apple", id = "minecraft:apple"/"minecraft:log" ou display_name = "Apple"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement