Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.15 KB | None | 0 0
  1. -- fonction principale : a chaque step, check si le block en dessous est un
  2. -- chest et, si tel est le cas, vide son inventaire (sauf un stack de saplings
  3. -- pour replanter) puis lance checkifLogs avant de lancer goForth et de
  4. -- recommencer
  5. function wander()
  6.     while true do --condition a remplacer ?
  7.         --check if there is a chest
  8.         local success, data = turtle.inspectDown()
  9.         if success then
  10.             if data.name == "minecraft:chest" then
  11.                 emptyInventory()
  12.             elseif data.name ~= "minecraft:stonebrick" then -- stonebrick == nom du "chemin"
  13.                 print("je suis perdu, tu peux me ramener chez moi puis relancer le programme ?")
  14.                 return
  15.             end
  16.         else --il n'y a pas de block en dessous
  17.             print("je suis perdu, tu peux me ramener chez moi puis relancer le programme ?")
  18.             return
  19.         end
  20.  
  21.         checkIfLogs()
  22.         goForth()
  23.     end
  24. end
  25.  
  26. -- cuts the "root" of a tree and a 3x3*h area over it then comes back to
  27. -- original position and direction
  28. -- where h is the number of non air blocks over the "root"
  29. -- and "root" is the log at the bottom of the tree
  30. function cutTree()
  31.     turtle.dig()
  32.     tryForward()
  33.  
  34.     while turtle.detectUp() do
  35.         turtle.digUp()
  36.         tryUp()
  37.         digSquare()
  38.     end
  39.     tryToFloor()
  40.     tryBack()
  41.     tryPlant()
  42. end
  43.  
  44. -- ckecks if the blocks ont the right and left of the turtle are logs
  45. -- if yes call cutTree on them
  46. function checkIfLogs()
  47.     turtle.turnRight()
  48.     local success, data = turtle.inspect()
  49.     if success then
  50.         if data.name == "minecraft:log" then
  51.             cutTree()
  52.         end
  53.     else
  54.         tryPlant() -- ne plantera rien si le block vise fait partie du chemin
  55.     end
  56.  
  57.     turtleHalfTurn()
  58.  
  59.     success, data = turtle.inspect()
  60.     if success then
  61.         if data.name == "minecraft:log" then
  62.           cutTree()
  63.         end
  64.     else
  65.         tryPlant()
  66.     end
  67.  
  68.   --en se redirige vers la direction de départ
  69.     turtle.turnRight()
  70. end
  71.  
  72. function refueling()
  73.     turtle.select(16) -- selectionne la derniere case qui est sencee contenir un coffre
  74.     if not turtle.placeUp() then --si echec du placage de coffre
  75.         turtle.digUp() --on casse le block au dessus
  76.         if not turtle.placeUp() then -- si encore echec -> car pas de coffre
  77.             print("I need a chest in slot 16 !!")
  78.         end
  79.         while not turtle.placeUp() do
  80.             sleep(5)
  81.         end
  82.     end
  83.  
  84.     --transfere autant de buches que possible dans le slot 16
  85.     for i=1,15 do
  86.         local data = turtle.getItemDetail(i)
  87.         --si il y a un objet
  88.         if data then
  89.             --et que c'est une buche
  90.             if data.name == "minecraft:log" then
  91.                 --la transfert pour etre craft
  92.                 turtle.select(i)
  93.                 turtle.transferTo(16)
  94.             end
  95.         end
  96.     end
  97.  
  98.     --transfere tous les autres items dans le chest
  99.     for i=1,15 do
  100.         turtle.select(i)
  101.         turtle.dropUp()
  102.     end
  103.  
  104.     --craft les planches et refuel
  105.     turtle.select(1)
  106.     if turtle.getItemCount(16) == 0 then
  107.         print("need a log in the last slot to refuel")
  108.         while turtle.getItemCount(16) == 0 do
  109.             sleep(5)
  110.         end
  111.     end
  112.     while turtle.getItemCount(16) > 0 do -- tant quil reste des buches
  113.         turtle.craft() -- les seuls items dans linventaire sont des buches -> craft des planches
  114.         turtle.refuel()
  115.     end
  116.  
  117.     --recupere les objets du coffre et le coffre
  118.     while turtle.suckUp() do
  119.         --nothing
  120.     end
  121.     turtle.select(16)
  122.     turtle.digUp() -- automatically stores item in inventory
  123. end
  124.  
  125. -- parcours  un plan horizontal 3x3 dans le sens horlogique depuis le centre
  126. -- et fini dans la direction et la position de depart
  127. --  >  >  v    ce sont les les directions de deplacement de la turtle
  128. --  ^  ^  v
  129. --  ^  <  <
  130. function digSquare()
  131.     turtle.dig()
  132.     tryForward()
  133.  
  134.     turtle.turnRight()
  135.     turtle.dig()
  136.     tryForward()
  137.  
  138.     turtle.turnRight()
  139.     turtle.dig()
  140.     tryForward()
  141.  
  142.     turtle.dig()
  143.     tryForward()
  144.  
  145.     turtle.turnRight()
  146.     turtle.dig()
  147.     tryForward()
  148.  
  149.     turtle.dig()
  150.     tryForward()
  151.  
  152.     turtle.turnRight()
  153.     turtle.dig()
  154.     tryForward()
  155.  
  156.     turtle.dig()
  157.     tryForward()
  158.  
  159.     turtle.turnRight()
  160.     tryForward()
  161.  
  162.     turtle.turnRight()
  163.     tryForward()
  164.     turtleHalfTurn()
  165. end
  166.  
  167. --empty turtle s inventory in the chest under the turtle apart from one stack
  168. -- of saplings at most
  169. function emptyInventory()
  170.     turtle.select(1)
  171.     local data = turtle.getItemDetail(1)
  172.     if data then
  173.         if not data.name == "minecraft:sapling" then -- si c'est pas des saplings
  174.             if not turtle.dropDown() then -- chest full
  175.                 print("Le chest est plein. Vide le pour que je puisse continuer mon travail!")
  176.                 while not turtle.dropDown() do -- stops and wait while chest is full
  177.                     sleep(5)
  178.                 end
  179.             end
  180.         end
  181.     end
  182.  
  183.     for i=2,15 do
  184.         local data = turtle.getItemDetail(i)
  185.         if data then -- if there is aan item
  186.             turtle.select(i)
  187.             if data.name == "minecraft:sapling" then -- if there are saplings, try sending to slot 1
  188.                 turtle.transferTo(1) -- remaining saplings transfered to chest
  189.             end
  190.             if not turtle.dropDown() then -- soit slot vide soit chest full
  191.                 if turtle.getItemCount(i) > 0 then --cas chest full
  192.                     print("Le chest est plein. Vide le pour que je puisse continuer mon travail!")
  193.                     while not turtle.dropDown() do -- stops and wait if chest is full
  194.                         sleep(5)
  195.                     end
  196.                 end
  197.             end
  198.         end
  199.     end
  200. end
  201.  
  202. --go one block further down the path
  203. -- priorities : left > front > right
  204. function goForth()
  205.     turtle.turnLeft()
  206.     if turtle.detect() then -- si il y a un block a gauche
  207.         turtle.turnRight()
  208.         if turtle.detect() then -- si il y a un block en face
  209.             turtle.turnRight()
  210.             tryForward()
  211.         else
  212.             tryForward()
  213.         end
  214.     else
  215.         tryForward()
  216.         local success, data = turtle.inspectDown()
  217.         if success then
  218.             if ( data.name ~= "minecraft:chest") and ( data.name ~= "minecraft:stonebrick") then
  219.                 tryBack()
  220.                 turtle.turnRight()
  221.                 if turtle.detect() then -- si il y a un block en face
  222.                     turtle.turnRight()
  223.                     tryForward()
  224.                 else
  225.                     tryForward()
  226.                 end
  227.             end
  228.         end
  229.     end
  230. end
  231.  
  232. function tryForward()
  233.     if not turtle.forward() then --si echec de forward car pas de carburant
  234.         refueling()
  235.         turtle.forward()
  236.     end
  237. end
  238.  
  239. function tryUp()
  240.     if not turtle.up() then -- si echec de up car pas de carburant
  241.         refueling()
  242.         turtle.forward()
  243.     end
  244. end
  245.  
  246. function tryDown()
  247.     if not turtle.down() then --si echec de down car pas de carburant
  248.         refueling()
  249.         turtle.forward()
  250.     end
  251. end
  252.  
  253. function tryBack()
  254.     if not turtle.back() then --si echec de back car pas de carburant
  255.         refueling()
  256.         turtle.forward()
  257.     end
  258. end
  259.  
  260. --calls tryDown until it hits the floor
  261. function tryToFloor()
  262.     while not turtle.detectDown() do
  263.         tryDown()
  264.     end
  265. end
  266.  
  267. -- iterates over inventory if saplings are found select corresponding slot and
  268. -- return true, if no saplings found return false
  269. function selectSaplings()
  270.     for i=1,15 do
  271.         local data = turtle.getItemDetail(i)
  272.         if data then -- si il y a un item
  273.             if data.name == "minecraft:sapling" then -- et si c'est une sapling
  274.                 turtle.select(i)
  275.                 return true
  276.             end
  277.         end
  278.     end
  279.     return false
  280. end
  281.  
  282. function tryPlant()
  283.     if selectSaplings() then -- place sapling if inventory contains some
  284.         turtle.place()
  285.     end
  286. end
  287.  
  288. function turtleHalfTurn()
  289.     turtle.turnRight()
  290.     turtle.turnRight()
  291. end
  292.  
  293. wander()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement