Advertisement
meuced

[LUA] minage turtle v1.0

Mar 5th, 2013
3,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.96 KB | None | 0 0
  1. local direction = 0                     -- direction
  2. local drapeauBedrock = false            -- indicateur pour savoir si on est arrivé à la bedrock
  3. local profondeur = 0                    -- indique de combien on a creusé
  4. local longueur = 9                      -- indique la longueur (x) de la zone à miner
  5. local largeur = 9                       -- indique la largeur (y) de la zone à miner
  6. local xPosition = 0                     -- indique la position courante en x
  7. local zPosition = 0                     -- indique la position courante en z
  8. local niveauFuelMini = 5                -- niveau de déplacements auquel on doit refaire le plein de fuel
  9. local niveauCharbonMini = 5             -- quantité de charbons restants à laquelle on doit refaire le plein de charbon
  10.  
  11. local premierSlot = 4                   -- premier slot où se trouve le minerai ramassé
  12. local dernierSlot = 13                  -- dernier slot à surveiller pour enclencher le vidage de l'inventaire
  13. local enderchestSlot = 14               -- slot où se trouve l'enderchest pour les minerais
  14. local enderchestCharbonSlot = 15        -- slot où se trouve l'enderchest pour les minerais
  15. local charbonSlot = 16                  -- slot ou est stocké le charbon
  16.  
  17. local plan = {}                         -- tableau pour stocker les coordonnées relatives des puits de minage.
  18.  
  19. local idAffichage = 22                  -- id du computer vers lequel on envoie les infos par rednet
  20.  
  21. function compare_mine()                 -- fonction qui compare et mine, tourne à droite et direction++
  22.  
  23.     local slot = 0
  24.     local resultat = false
  25.    
  26.     for slot=1,3 do
  27.         turtle.select(slot)
  28.         if turtle.compare() or resultat then
  29.             resultat = true
  30.         end
  31.     end
  32.    
  33.     if resultat == false then
  34.         turtle.dig()
  35.         if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  36.             print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  37.             videInventaire()
  38.         end
  39.     end
  40.    
  41.     turtle.turnRight()
  42.     direction=direction+1
  43.    
  44. end
  45.  
  46. function verifFuel()                    -- vérifie si on a assez de fuel (déplacements) en réserve.
  47.   -- 1 charbon = 96 deplacements
  48.   -- On vérifie le niveau de fuel
  49.     local niveauFuel = turtle.getFuelLevel()
  50.     if (niveauFuel ~= "unlimited") then
  51.         if (niveauFuel < niveauFuelMini) then
  52.             -- On a besoin de faire le plein
  53.             if turtle.getItemCount(charbonSlot) < niveauCharbonMini then
  54.                 rechargeCharbon() -- on refait le plein de charbon
  55.             end
  56.             turtle.select(charbonSlot)
  57.             turtle.refuel(1) -- on recharge pour 96 deplacements
  58.         end
  59.     end
  60. end
  61.  
  62. function rechargeCharbon()              -- permet de refaire le plein en charbon
  63.    
  64.     turtle.dig()
  65.    
  66.     if turtle.getItemCount(dernierSlot-1) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  67.             print("vidage inventaire rech_charbon1; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  68.             videInventaire()
  69.     end
  70.    
  71.     turtle.select(enderchestCharbonSlot)
  72.     turtle.placeUp()
  73.     turtle.select(charbonSlot)
  74.     turtle.suckUp()
  75.    
  76.     turtle.select(enderchestCharbonSlot)
  77.     turtle.digUp()
  78.    
  79. end
  80.  
  81. function videInventaire()               -- vide l'inventaire de la tortue dans l'enderchest dédié à ça
  82.  
  83.     local slot
  84.     turtle.select(enderchestSlot)
  85.     turtle.placeUp()
  86.     for slot=premierSlot,dernierSlot do
  87.         turtle.select(slot)
  88.         while turtle.getItemCount(slot) > 0 do
  89.             turtle.dropUp(turtle.getItemCount(slot))
  90.             if turtle.getItemCount(slot) > 0 then
  91.                 sleep(0.5)
  92.             end
  93.         end
  94.     end
  95.  
  96.     turtle.select(enderchestSlot)
  97.     turtle.digUp()
  98.    
  99. end
  100.  
  101. function calcPlan()                     -- calcule les emplacements des puits de minage
  102.  
  103.     local x, z, temp, xTemp
  104.     temp = 1
  105.     -- pour forcer à miner le point de départ
  106.     plan[temp] = {}
  107.     plan[temp][1] = 0
  108.     plan[temp][2] = 0
  109.     temp = temp + 1
  110.    
  111.     -- on boucle sur les colonnes
  112.     for z=0,largeur do
  113.         x = 0
  114.         print("z : "..z)
  115.        
  116.         --on calcule le x du 1er premier puit de minage pour la colonne z
  117.         x = 5 - (z*2) +x
  118.         while x < 0 do
  119.             x = x + 5
  120.         end
  121.         plan[temp] = {}
  122.         plan[temp][1] = x
  123.         plan[temp][2] = z
  124.         temp = temp + 1
  125.         print("x : "..x)
  126.  
  127.         -- et ensuite on trouve automatiquement les autres emplacements de la colonne z
  128.         while x <= longueur do
  129.             x = x + 5
  130.             if x <= longueur then
  131.                 plan[temp] = {}
  132.                 plan[temp][1] = x
  133.                 plan[temp][2] = z
  134.                 temp = temp + 1
  135.                 print("x : "..x)
  136.             end
  137.         end
  138.         z = z + 1
  139.     end
  140. end
  141.  
  142. function deplacement(r,s)               -- pour aller à des coordonnées précises
  143.  
  144.     local nbX
  145.     local nbZ
  146.    
  147. -- On commence par se déplacer en x
  148.     print("r : "..r.." ; s : "..s)
  149.     print("xPosition : "..xPosition.." ; zPosition : "..zPosition)
  150.    
  151.     r = tonumber(r)
  152.     s = tonumber(s)
  153.    
  154.     if r > xPosition then
  155.         nbX = r - xPosition
  156.         print("dans r>= xposition")
  157.         while direction ~= 0 do -- ici on se met dans la bonne direction
  158.             turtle.turnRight()
  159.             direction=direction+1
  160.             if direction == 4 then direction = 0 end
  161.         end
  162.     elseif r < xPosition then
  163.         nbX = xPosition - r
  164.         print("dans xposition > r")
  165.         while direction ~= 2 do -- ici on se met dans la bonne direction
  166.             turtle.turnRight()
  167.             direction=direction+1
  168.             if direction == 4 then direction = 0 end
  169.         end
  170.     end
  171.    
  172.     if r ~= xPosition then
  173.         print("nbX : "..nbX)
  174.         while nbX > 0 do
  175.             if not turtle.forward() then
  176.                 turtle.dig() -- ici, on n'a pas réussi à avancer, donc on creuse devant soit pour dégager le passage
  177.                 if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  178.                     print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  179.                     videInventaire()
  180.                 end
  181.                 turtle.forward()
  182.             end
  183.             if direction == 0 then xPosition = xPosition + 1 else xPosition = xPosition - 1 end
  184.             verifFuel()
  185.             nbX = nbX - 1
  186.         end
  187.     end
  188.  
  189. -- Ensuite on fait le déplacement en z
  190.    
  191.     if s > zPosition then
  192.         nbZ = s - zPosition
  193.        
  194.         while direction ~= 1 do -- ici on se met dans la bonne direction
  195.             turtle.turnRight()
  196.             direction=direction+1
  197.             if direction == 4 then direction = 0 end
  198.         end
  199.     elseif s < zPosition then
  200.         nbZ = zPosition - s
  201.        
  202.         while direction ~= 3 do -- ici on se met dans la bonne direction
  203.             turtle.turnRight()
  204.             direction=direction+1
  205.             if direction == 4 then direction = 0 end
  206.         end
  207.     end
  208.    
  209.     if s ~= zPosition then
  210.         while nbZ > 0 do
  211.             if not turtle.forward() then
  212.                 turtle.dig() -- ici, on n'a pas réussi à avancer, donc on creuse devant soit pour dégager le passage
  213.                 if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  214.                     print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  215.                     videInventaire()
  216.                 end
  217.                 turtle.forward()
  218.             end
  219.             if direction == 1 then zPosition = zPosition + 1 else zPosition = zPosition - 1 end
  220.             verifFuel()
  221.             nbZ = nbZ - 1
  222.         end
  223.     end
  224.    
  225.     --on se remet en direction "zéro"
  226.     while direction ~= 0 do
  227.         turtle.turnRight()
  228.         direction=direction+1
  229.         if direction == 4 then direction = 0 end
  230.     end
  231.  
  232. end
  233.  
  234. --********************************************--
  235. --********** Programme principal *************--
  236. --********************************************--
  237.  
  238. print("Entrez les dimensions de la zone à miner.")
  239. print("")
  240. print("Largeur (vers la droite) : ")
  241. largeur = tonumber(read())
  242. print("Longueur (en face) : ")
  243. longueur = tonumber(read())
  244.  
  245. calcPlan() -- on calcule les emplacements des puits de forage
  246.  
  247. local p, pmax = 1, #plan
  248.  
  249. -- ici, affichage du nombre de puits à creuser et attente confirmation pour commencer
  250. -- puis à chaque down ou up, affichage de la profondeur et màj du "puit en cours/nb puits total"
  251. -- et affichage lorsqu'on vide l'inventaire ou que l'on reprend du charbon
  252.  
  253. rednet.open("right")
  254. rednet.send(idAffichage,"etat:attente")
  255. print("")
  256. print("Nombre de puits à creuser : "..#plan)
  257. rednet.send(idAffichage,"nbPuitsTotal:"..#plan)
  258. rednet.send(idAffichage,"profondeur:"..profondeur)
  259. print("Appuyez sur [ENTREE] pour commencer !")
  260. read()
  261. rednet.send(idAffichage,"etat:encours")
  262. read()
  263.  
  264. while p <= pmax do
  265.    
  266.     rednet.send(idAffichage,"nbPuits:"..p)
  267.     drapeauBedrock = false --avant tout, on reset ce flag
  268.     deplacement(plan[p][1],plan[p][2]) -- puis on se déplace sur le 1er puit à forer
  269.     turtle.digDown() --creuse le bloc dessous
  270.     sleep(0.2)
  271.     turtle.down() --descend d'un cran
  272.     profondeur = profondeur+1
  273.     rednet.send(idAffichage,"profondeur:"..profondeur)
  274.     verifFuel()
  275.  
  276.     while drapeauBedrock == false do
  277.  
  278.         -- ici, direction = 0
  279.         while direction~=4 do
  280.             --compare et mine, tourne à droite et direction++
  281.             compare_mine()
  282.         end
  283.         direction=0
  284.        
  285.         if turtle.detectDown() == true then   -- on vérifie si il y a un bloc en dessous
  286.             if turtle.digDown() == false then -- si on n'arrive pas à creuser en dessous, alors c'est la bedrock
  287.                 drapeauBedrock = true         -- donc je met le drapeau à true pour sortir de la boucle
  288.                 print("bedrock !")
  289.             else
  290.                 if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  291.                     print("vidage inventaire princ1; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  292.                     videInventaire()
  293.                 end
  294.                 turtle.down()
  295.                 profondeur = profondeur+1
  296.                 rednet.send(idAffichage,"profondeur:"..profondeur)
  297.                 verifFuel()
  298.             end
  299.         else                    -- si il n'y a pas de bloc alors c'est de l'air, de l'eau ou de la lave
  300.             turtle.down()       -- alors on descend simplement (la tortue ne craint pas la lave ou l'eau) et on continue à miner
  301.             profondeur = profondeur+1
  302.             rednet.send(idAffichage,"profondeur:"..profondeur)
  303.             verifFuel()
  304.         end
  305.        
  306.     end
  307.    
  308.     print("fin de la boucle "..profondeur)
  309.     -- ici je remonte à la surface
  310.     while profondeur ~= 0 do
  311.         if turtle.detectUp() then turtle.digUp() end
  312.         turtle.up()
  313.         profondeur = profondeur-1
  314.         rednet.send(idAffichage,"profondeur:"..profondeur)
  315.         verifFuel()
  316.     end
  317.    
  318.     p = p + 1
  319. end
  320.  
  321. rednet.send(idAffichage,"etat:retour")
  322. deplacement(0,0) -- retour au point de départ
  323. rednet.send(idAffichage,"etat:fin")
  324. rednet.close(right)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement