Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- fonction principale : a chaque step, check si le block en dessous est un
- -- chest et, si tel est le cas, vide son inventaire (sauf un stack de saplings
- -- pour replanter) puis lance checkifLogs avant de lancer goForth et de
- -- recommencer
- function wander()
- while true do --condition a remplacer ?
- --check if there is a chest
- local success, data = turtle.inspectDown()
- if success then
- if data.name == "minecraft:chest" then
- emptyInventory()
- elseif data.name ~= "minecraft:stonebrick" then -- stonebrick == nom du "chemin"
- print("je suis perdu, tu peux me ramener chez moi puis relancer le programme ?")
- return
- end
- else --il n'y a pas de block en dessous
- print("je suis perdu, tu peux me ramener chez moi puis relancer le programme ?")
- return
- end
- checkIfLogs()
- goForth()
- end
- end
- -- cuts the "root" of a tree and a 3x3*h area over it then comes back to
- -- original position and direction
- -- where h is the number of non air blocks over the "root"
- -- and "root" is the log at the bottom of the tree
- function cutTree()
- turtle.dig()
- tryForward()
- while turtle.detectUp() do
- turtle.digUp()
- tryUp()
- digSquare()
- end
- tryToFloor()
- tryBack()
- tryPlant()
- end
- -- ckecks if the blocks ont the right and left of the turtle are logs
- -- if yes call cutTree on them
- function checkIfLogs()
- turtle.turnRight()
- local success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:log" then
- cutTree()
- end
- else
- tryPlant() -- ne plantera rien si le block vise fait partie du chemin
- end
- turtleHalfTurn()
- success, data = turtle.inspect()
- if success then
- if data.name == "minecraft:log" then
- cutTree()
- end
- else
- tryPlant()
- end
- --en se redirige vers la direction de départ
- turtle.turnRight()
- end
- function refueling()
- turtle.select(16) -- selectionne la derniere case qui est sencee contenir un coffre
- if not turtle.placeUp() then --si echec du placage de coffre
- turtle.digUp() --on casse le block au dessus
- if not turtle.placeUp() then -- si encore echec -> car pas de coffre
- print("I need a chest in slot 16 !!")
- end
- while not turtle.placeUp() do
- sleep(5)
- end
- end
- --transfere autant de buches que possible dans le slot 16
- for i=1,15 do
- local data = turtle.getItemDetail(i)
- --si il y a un objet
- if data then
- --et que c'est une buche
- if data.name == "minecraft:log" then
- --la transfert pour etre craft
- turtle.select(i)
- turtle.transferTo(16)
- end
- end
- end
- --transfere tous les autres items dans le chest
- for i=1,15 do
- turtle.select(i)
- turtle.dropUp()
- end
- --craft les planches et refuel
- turtle.select(1)
- if turtle.getItemCount(16) == 0 then
- print("need a log in the last slot to refuel")
- while turtle.getItemCount(16) == 0 do
- sleep(5)
- end
- end
- while turtle.getItemCount(16) > 0 do -- tant quil reste des buches
- turtle.craft() -- les seuls items dans linventaire sont des buches -> craft des planches
- turtle.refuel()
- end
- --recupere les objets du coffre et le coffre
- while turtle.suckUp() do
- --nothing
- end
- turtle.select(16)
- turtle.digUp() -- automatically stores item in inventory
- end
- -- parcours un plan horizontal 3x3 dans le sens horlogique depuis le centre
- -- et fini dans la direction et la position de depart
- -- > > v ce sont les les directions de deplacement de la turtle
- -- ^ ^ v
- -- ^ < <
- function digSquare()
- turtle.dig()
- tryForward()
- turtle.turnRight()
- turtle.dig()
- tryForward()
- turtle.turnRight()
- turtle.dig()
- tryForward()
- turtle.dig()
- tryForward()
- turtle.turnRight()
- turtle.dig()
- tryForward()
- turtle.dig()
- tryForward()
- turtle.turnRight()
- turtle.dig()
- tryForward()
- turtle.dig()
- tryForward()
- turtle.turnRight()
- tryForward()
- turtle.turnRight()
- tryForward()
- turtleHalfTurn()
- end
- --empty turtle s inventory in the chest under the turtle apart from one stack
- -- of saplings at most
- function emptyInventory()
- turtle.select(1)
- local data = turtle.getItemDetail(1)
- if data then
- if not data.name == "minecraft:sapling" then -- si c'est pas des saplings
- if not turtle.dropDown() then -- chest full
- print("Le chest est plein. Vide le pour que je puisse continuer mon travail!")
- while not turtle.dropDown() do -- stops and wait while chest is full
- sleep(5)
- end
- end
- end
- end
- for i=2,15 do
- local data = turtle.getItemDetail(i)
- if data then -- if there is aan item
- turtle.select(i)
- if data.name == "minecraft:sapling" then -- if there are saplings, try sending to slot 1
- turtle.transferTo(1) -- remaining saplings transfered to chest
- end
- if not turtle.dropDown() then -- soit slot vide soit chest full
- if turtle.getItemCount(i) > 0 then --cas chest full
- print("Le chest est plein. Vide le pour que je puisse continuer mon travail!")
- while not turtle.dropDown() do -- stops and wait if chest is full
- sleep(5)
- end
- end
- end
- end
- end
- end
- --go one block further down the path
- -- priorities : left > front > right
- function goForth()
- turtle.turnLeft()
- if turtle.detect() then -- si il y a un block a gauche
- turtle.turnRight()
- if turtle.detect() then -- si il y a un block en face
- turtle.turnRight()
- tryForward()
- else
- tryForward()
- end
- else
- tryForward()
- local success, data = turtle.inspectDown()
- if success then
- if ( data.name ~= "minecraft:chest") and ( data.name ~= "minecraft:stonebrick") then
- tryBack()
- turtle.turnRight()
- if turtle.detect() then -- si il y a un block en face
- turtle.turnRight()
- tryForward()
- else
- tryForward()
- end
- end
- end
- end
- end
- function tryForward()
- if not turtle.forward() then --si echec de forward car pas de carburant
- refueling()
- turtle.forward()
- end
- end
- function tryUp()
- if not turtle.up() then -- si echec de up car pas de carburant
- refueling()
- turtle.forward()
- end
- end
- function tryDown()
- if not turtle.down() then --si echec de down car pas de carburant
- refueling()
- turtle.forward()
- end
- end
- function tryBack()
- if not turtle.back() then --si echec de back car pas de carburant
- refueling()
- turtle.forward()
- end
- end
- --calls tryDown until it hits the floor
- function tryToFloor()
- while not turtle.detectDown() do
- tryDown()
- end
- end
- -- iterates over inventory if saplings are found select corresponding slot and
- -- return true, if no saplings found return false
- function selectSaplings()
- for i=1,15 do
- local data = turtle.getItemDetail(i)
- if data then -- si il y a un item
- if data.name == "minecraft:sapling" then -- et si c'est une sapling
- turtle.select(i)
- return true
- end
- end
- end
- return false
- end
- function tryPlant()
- if selectSaplings() then -- place sapling if inventory contains some
- turtle.place()
- end
- end
- function turtleHalfTurn()
- turtle.turnRight()
- turtle.turnRight()
- end
- wander()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement