Advertisement
meuced

perso

Jun 12th, 2014
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.63 KB | None | 0 0
  1.    
  2.  
  3.     local fonctionnement = "normal"                 -- choix du vidage : "normal" (avec coffre au départ) ou "enderchest"
  4.                                                                                                                                                                     -- normal : coffre récup derrière départ tortue, coffre charbon au dessus tortue
  5.                                                                                                                                                                     -- enderchest : voir variable pour emplacements des enderchest dans l'inventaire
  6.     local direction = 0                                     -- direction
  7.     local drapeauBedrock = false                    -- indicateur pour savoir si on est arrivé à la bedrock
  8.     local profondeur = 0                                    -- indique de combien on a creusé
  9.     local longueur = 9                                              -- indique la longueur (x) de la zone à miner
  10.     local largeur = 9                                               -- indique la largeur (y) de la zone à miner
  11.     local xPosition = 0                                             -- indique la position courante en x
  12.     local zPosition = 0                                             -- indique la position courante en z
  13.     local niveauFuelMini = 5                                -- niveau de déplacements auquel on doit refaire le plein de fuel
  14.     local niveauCharbonMini = 5                             -- quantité de charbons restants à laquelle on doit refaire le plein de charbon
  15.      
  16.     local premierSlot = 4                                   -- premier slot où se trouve le minerai ramassé
  17.     local dernierSlot = 13                                  -- dernier slot à surveiller pour enclencher le vidage de l'inventaire
  18.     if fonctionnement == "normal" then dernierSlot = 15 end  -- si on est en mode normal, on gagne 2 slots d'inventaire
  19.     local enderchestSlot = 14                               -- slot où se trouve l'enderchest pour les minerais
  20.     local enderchestCharbonSlot = 15                -- slot où se trouve l'enderchest pour les minerais
  21.     local charbonSlot = 16                                  -- slot ou est stocké le charbon
  22.      
  23.     local plan = {}                                                 -- tableau pour stocker les coordonnées relatives des puits de minage.
  24.      
  25.     --local idAffichage = 22                                  -- id du computer vers lequel on envoie les infos par rednet
  26.      
  27.     local idChannel = 123                                                                   -- num du canal pour envoyer les infos via -- modem
  28.      
  29.     local resDown = true
  30.      
  31.     function compare_mine()                                 -- fonction qui compare et mine, tourne à droite et direction++
  32.      
  33.                     local slot = 0
  34.                     local resultat = false
  35.                
  36.                     for slot=1,3 do
  37.                                     turtle.select(slot)
  38.                                     if turtle.compare() or resultat then
  39.                                                     resultat = true
  40.                                     end
  41.                     end
  42.                
  43.                     if resultat == false then
  44.                             turtle.dig()
  45.                     end
  46.                
  47.                     turtle.turnRight()
  48.                     direction=direction+1
  49.                    
  50.                     if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  51.                             print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  52.                             --videInventaire()
  53.                             --videInventaireCoffre()
  54.                             return vidage()
  55.                     end
  56.                
  57.     end
  58.      
  59.     function verifFuel()                                    -- vérifie si on a assez de fuel (déplacements) en réserve.
  60.       -- 1 charbon = 96 deplacements
  61.       -- On vérifie le niveau de fuel
  62.                     local niveauFuel = turtle.getFuelLevel()
  63.                     if (niveauFuel ~= "unlimited") then
  64.                                     if (niveauFuel < niveauFuelMini) then
  65.                                                     -- On a besoin de faire le plein
  66.                                                     turtle.select(charbonSlot)
  67.                                                     turtle.refuel(1) -- on recharge pour 96 deplacements
  68.                                                     -- et on vérifie si il nous reste assez de charbon
  69.                                                     if turtle.getItemCount(charbonSlot) < niveauCharbonMini then
  70.                                                                     return recharge() -- on refait le plein de charbon
  71.                                                     end
  72.                                     end
  73.                     end
  74.     end
  75.      
  76.     function rechargeCharbon()                              -- permet de refaire le plein en charbon
  77.                
  78.             turtle.select(enderchestCharbonSlot)
  79.             turtle.placeUp()
  80.             turtle.select(charbonSlot)
  81.             turtle.suckUp()
  82.        
  83.             turtle.select(enderchestCharbonSlot)
  84.             turtle.digUp()
  85.                
  86.     end
  87.      
  88.     function rechargeCharbonCoffre()
  89.      
  90.             -- d'abord je sauv les coordonnées x, z, profondeur et direction
  91.                     xPositionSv = xPosition
  92.                     zPositionSv = zPosition
  93.                     profondeurSv = profondeur
  94.                     directionSv = direction
  95.                
  96.                     --je préviens que je remonte pour recharger en charbon
  97.                     -- modem.transmit(idChannel,idChannel,"etat:charbon")
  98.                
  99.                     -- je remonte à la surface
  100.                     while profondeurSv ~= 0 do
  101.                                     if turtle.detectUp() then turtle.digUp() end
  102.                                     turtle.up()
  103.                                     profondeurSv = profondeurSv-1
  104.                                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeurSv)
  105.                                     verifFuel()
  106.                     end
  107.                
  108.                     -- je me déplace vers le point de départ
  109.                     deplacement(0,0)
  110.                
  111.                     --on prend du charbon dans le coffre au-dessus de la tortue
  112.                     turtle.select(charbonSlot)
  113.                     turtle.suckUp()
  114.                
  115.                     --on retourne au-dessus du puit en cours de minage
  116.                     deplacement(xPositionSv, zPositionSv)
  117.                
  118.                     --on descend à la dernière profondeur
  119.                     while profondeurSv ~= profondeur do
  120.                                     if turtle.detectDown() then turtle.digDown() end
  121.                                     turtle.down()
  122.                                     profondeurSv = profondeurSv+1
  123.                                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeurSv)
  124.                                     verifFuel()
  125.                     end
  126.                
  127.                     --on se met dans la bonne direction
  128.                     while directionSv ~= direction do
  129.                                     turtle.turnRight()
  130.                                     direction=direction+1
  131.                                     if direction == 4 then direction = 0 end
  132.                     end
  133.                
  134.                     --et normalement c'est bon.
  135.                     -- modem.transmit(idChannel,idChannel,"etat:encours")
  136.      
  137.     end
  138.      
  139.     function recharge()
  140.      
  141.                     if fonctionnement == "normal" then
  142.                                     return rechargeCharbonCoffre()
  143.                     elseif fonctionnement == "enderchest" then
  144.                                     return rechargeCharbon()
  145.                     end
  146.      end
  147.                
  148.     function videInventaire()                               -- vide l'inventaire de la tortue dans l'enderchest dédié à ça
  149.      
  150.                     local slot
  151.                     turtle.select(enderchestSlot)
  152.                     turtle.placeUp()
  153.                     for slot=premierSlot,dernierSlot do
  154.                                     turtle.select(slot)
  155.                                     while turtle.getItemCount(slot) > 0 do
  156.                                                     turtle.dropUp(turtle.getItemCount(slot))
  157.                                                     if turtle.getItemCount(slot) > 0 then
  158.                                                                     sleep(0.5)
  159.                                                     end
  160.                                     end
  161.                     end
  162.      
  163.                     turtle.select(enderchestSlot)
  164.                     turtle.digUp()
  165.                
  166.     end
  167.      
  168.     function videInventaireCoffre()
  169.      
  170.                     -- d'abord je sauv les coordonnées x, z, profondeur et direction
  171.                     xPositionSv = xPosition
  172.                     zPositionSv = zPosition
  173.                     profondeurSv = profondeur
  174.                     directionSv = direction
  175.                
  176.                     --je préviens que je remonte pour vider mon inventaire
  177.                     -- modem.transmit(idChannel,idChannel,"etat:vidage")
  178.                
  179.                     -- je remonte à la surface
  180.                     while profondeurSv ~= 0 do
  181.                                     if turtle.detectUp() then turtle.digUp() end
  182.                                     turtle.up()
  183.                                     profondeurSv = profondeurSv-1
  184.                                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeurSv)
  185.                                     verifFuel()
  186.                     end
  187.                
  188.                     -- je me déplace vers le point de départ
  189.                     deplacement(0,0)
  190.                
  191.                     --on se remet en direction "deux", face au coffre
  192.                     while direction ~= 2 do
  193.                                     turtle.turnRight()
  194.                                     direction=direction+1
  195.                                     if direction == 4 then direction = 0 end
  196.                     end
  197.                
  198.                     --on vide l'inventaire
  199.                     for slot=premierSlot,dernierSlot do
  200.                                     turtle.select(slot)
  201.                                     while turtle.getItemCount(slot) > 0 do
  202.                                                     turtle.drop(turtle.getItemCount(slot))
  203.                                                     if turtle.getItemCount(slot) > 0 then
  204.                                                                     sleep(0.5)
  205.                                                     end
  206.                                     end
  207.                     end
  208.                
  209.                     --on se remet en direction "zéro"
  210.                     while direction ~= 0 do
  211.                                     turtle.turnRight()
  212.                                     direction=direction+1
  213.                                     if direction == 4 then direction = 0 end
  214.                     end
  215.                
  216.                     --on retourne au-dessus du puit en cours de minage
  217.                     deplacement(xPositionSv, zPositionSv)
  218.                
  219.                     --on descend à la dernière profondeur
  220.                     while profondeurSv ~= profondeur do
  221.                                     if turtle.detectDown() then turtle.digDown() end
  222.                                     turtle.down()
  223.                                     profondeurSv = profondeurSv+1
  224.                                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeurSv)
  225.                                     verifFuel()
  226.                     end
  227.                
  228.                     --on se met dans la bonne direction
  229.                     while directionSv ~= direction do
  230.                                     turtle.turnRight()
  231.                                     direction=direction+1
  232.                                     if direction == 4 then direction = 0 end
  233.                     end
  234.                
  235.                     --et normalement c'est bon.
  236.                     -- modem.transmit(idChannel,idChannel,"etat:encours")
  237.     end
  238.      
  239.     function vidage()
  240.      
  241.                     if fonctionnement == "normal" then
  242.                                     return videInventaireCoffre()
  243.                     elseif fonctionnement == "enderchest" then
  244.                                     return videInventaire()
  245.                     end
  246.      
  247.     end
  248.      
  249.     function calcPlan()                                     -- calcule les emplacements des puits de minage
  250.      
  251.                     local x, z, temp, xTemp
  252.                     temp = 1
  253.                     -- pour forcer à miner le point de départ
  254.                     plan[temp] = {}
  255.                     plan[temp][1] = 0
  256.                     plan[temp][2] = 0
  257.                     temp = temp + 1
  258.                
  259.                     -- on boucle sur les colonnes
  260.                     for z=0,largeur do
  261.                                     x = 0
  262.                                     print("z : "..z)
  263.                                
  264.                                     --on calcule le x du 1er premier puit de minage pour la colonne z
  265.                                     x = 5 - (z*2) +x
  266.                                     while x < 0 do
  267.                                                     x = x + 5
  268.                                     end
  269.                                     plan[temp] = {}
  270.                                     plan[temp][1] = x
  271.                                     plan[temp][2] = z
  272.                                     temp = temp + 1
  273.                                     print("x : "..x)
  274.      
  275.                                     -- et ensuite on trouve automatiquement les autres emplacements de la colonne z
  276.                                     while x <= longueur do
  277.                                                     x = x + 5
  278.                                                     if x <= longueur then
  279.                                                                     plan[temp] = {}
  280.                                                                     plan[temp][1] = x
  281.                                                                     plan[temp][2] = z
  282.                                                                     temp = temp + 1
  283.                                                                     print("x : "..x)
  284.                                                     end
  285.                                     end
  286.                                     z = z + 1
  287.                     end
  288.     end
  289.      
  290.     function deplacement(r,s)                               -- pour aller à des coordonnées précises
  291.      
  292.                     local nbX
  293.                     local nbZ
  294.                
  295.     -- On commence par se déplacer en x
  296.                     print("f_depl r : "..r.." ; s : "..s)
  297.                     print("f_depl xPosition : "..xPosition.." ; zPosition : "..zPosition)
  298.                
  299.                     r = tonumber(r)
  300.                     s = tonumber(s)
  301.                
  302.                     if r > xPosition then
  303.                                     nbX = r - xPosition
  304.                                     print("dans r>= xposition")
  305.                                     while direction ~= 0 do -- ici on se met dans la bonne direction
  306.                                                     turtle.turnRight()
  307.                                                     direction=direction+1
  308.                                                     if direction == 4 then direction = 0 end
  309.                                     end
  310.                     elseif r < xPosition then
  311.                                     nbX = xPosition - r
  312.                                     print("dans xposition > r")
  313.                                     while direction ~= 2 do -- ici on se met dans la bonne direction
  314.                                                     turtle.turnRight()
  315.                                                     direction=direction+1
  316.                                                     if direction == 4 then direction = 0 end
  317.                                     end
  318.                     end
  319.                
  320.                     if r ~= xPosition then
  321.                                     print("nbX : "..nbX)
  322.                                     while nbX > 0 do
  323.                                                     if not turtle.forward() then
  324.                                                                     turtle.dig() -- ici, on n'a pas réussi à avancer, donc on creuse devant soit pour dégager le passage
  325.                                                                     if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  326.                                                                                     print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  327.                                                                                     --videInventaire()
  328.                                                                                     --videInventaireCoffre()
  329.                                                                                     vidage()
  330.                                                                     end
  331.                                                                     turtle.forward()
  332.                                                     end
  333.                                                     if direction == 0 then xPosition = xPosition + 1 else xPosition = xPosition - 1 end
  334.                                                     verifFuel()
  335.                                                     nbX = nbX - 1
  336.                                     end
  337.                     end
  338.      
  339.     -- Ensuite on fait le déplacement en z
  340.                
  341.                     if s > zPosition then
  342.                                     nbZ = s - zPosition
  343.                                
  344.                                     while direction ~= 1 do -- ici on se met dans la bonne direction
  345.                                                     turtle.turnRight()
  346.                                                     direction=direction+1
  347.                                                     if direction == 4 then direction = 0 end
  348.                                     end
  349.                     elseif s < zPosition then
  350.                                     nbZ = zPosition - s
  351.                                
  352.                                     while direction ~= 3 do -- ici on se met dans la bonne direction
  353.                                                     turtle.turnRight()
  354.                                                     direction=direction+1
  355.                                                     if direction == 4 then direction = 0 end
  356.                                     end
  357.                     end
  358.                
  359.                     if s ~= zPosition then
  360.                                     while nbZ > 0 do
  361.                                                     if not turtle.forward() then
  362.                                                                     turtle.dig() -- ici, on n'a pas réussi à avancer, donc on creuse devant soit pour dégager le passage
  363.                                                                     if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  364.                                                                                     print("vidage inventaire comp_mine; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  365.                                                                                     --videInventaire()
  366.                                                                                     --videInventaireCoffre()
  367.                                                                                     vidage()
  368.                                                                     end
  369.                                                                     turtle.forward()
  370.                                                     end
  371.                                                     if direction == 1 then zPosition = zPosition + 1 else zPosition = zPosition - 1 end
  372.                                                     verifFuel()
  373.                                                     nbZ = nbZ - 1
  374.                                     end
  375.                     end
  376.                
  377.                     --on se remet en direction "zéro"
  378.                     while direction ~= 0 do
  379.                                     turtle.turnRight()
  380.                                     direction=direction+1
  381.                                     if direction == 4 then direction = 0 end
  382.                     end
  383.      
  384.     end
  385.      
  386.     function turtleDown()
  387.             if turtle.detectDown() == true then   -- on vérifie si il y a un bloc en dessous
  388.                     if turtle.digDown() == false then -- si on n'arrive pas à creuser en dessous, alors c'est la bedrock
  389.                                     drapeauBedrock = true             -- donc je met le drapeau à true pour sortir de la boucle
  390.                                     print("bedrock !")
  391.                     else
  392.                             if turtle.getItemCount(dernierSlot) > 0 then  -- on vérifie si on doit vider l'inventaire de la tortue
  393.                                     print("vidage inventaire princ1; prof "..profondeur.." ; nbitem ds slot "..dernierSlot.." : "..turtle.getItemCount(dernierSlot).." ; ")
  394.                                     --videInventaire()
  395.                                     --videInventaireCoffre()
  396.                                     vidage()
  397.                             end
  398.                             --resDown = turtle.down()
  399.                             while turtle.down() == false do
  400.                                     sleep(0.1)
  401.                                     print("fu_turtledown resultat du down : false :o( ")
  402.                             end
  403.                             print("fu_turtledown resultat du down : true :o) ")
  404.                             profondeur = profondeur+1
  405.                             -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeur)
  406.                             verifFuel()
  407.                     end
  408.             else                                    -- si il n'y a pas de bloc alors c'est de l'air, de l'eau ou de la lave
  409.                     --resDown = turtle.down()           -- alors on descend simplement (la tortue ne craint pas la lave ou l'eau) et on continue à miner
  410.                     while turtle.down() == false do
  411.                     sleep(0.1)
  412.                     print("fu_turtledown resultat du down : false :o( ")
  413.                     end
  414.                     print("fu_turtledown resultat du down : true :o) ")
  415.                     profondeur = profondeur+1
  416.                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeur)
  417.                     verifFuel()
  418.             end
  419.             --if resDown ==false then print("fu_turtledown resultat du down : false")
  420.             --else print("fu_turtledown resultat du down : true")
  421.             --end
  422.     end
  423.      
  424.      
  425.     --********************************************--
  426.     --********** Programme principal *************--
  427.     --********************************************--
  428.      
  429.     print("Entrez les dimensions de la zone à miner.")
  430.     print("")
  431.     print("Largeur (vers la droite) : ")
  432.     largeur = tonumber(read())
  433.     print("Longueur (en face) : ")
  434.     longueur = tonumber(read())
  435.      
  436.     calcPlan() -- on calcule les emplacements des puits de forage
  437.      
  438.     local p, pmax = 1, #plan
  439.      
  440.     -- ici, affichage du nombre de puits à creuser et attente confirmation pour commencer
  441.     -- puis à chaque down ou up, affichage de la profondeur et màj du "puit en cours/nb puits total"
  442.     -- et affichage lorsqu'on vide l'inventaire ou que l'on reprend du charbon
  443.      
  444.     -- modem = peripheral.wrap("right")
  445.     -- modem.transmit(idChannel,idChannel,"etat:attente")
  446.     print("")
  447.     print("Nombre de puits à creuser : "..#plan)
  448.     -- modem.transmit(idChannel,idChannel,"nbPuitsTotal:"..#plan)
  449.     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeur)
  450.     print("Appuyez sur [ENTREE] pour commencer !")
  451.     read()
  452.     -- modem.transmit(idChannel,idChannel,"etat:encours")
  453.     --read()
  454.      
  455.     while p <= pmax do
  456.             print("rentre dans boucle p")
  457.             -- modem.transmit(idChannel,idChannel,"nbPuits:"..p)
  458.                     drapeauBedrock = false --avant tout, on reset ce flag
  459.                     verifFuel() -- on refait le plein si besoin
  460.                     deplacement(plan[p][1],plan[p][2]) -- puis on se déplace sur le puit à forer
  461.      
  462.                     turtleDown()
  463.      
  464.                     while drapeauBedrock == false do
  465.      
  466.                             -- ici, direction = 0
  467.                             while direction~=4 do
  468.                                             --compare et mine, tourne à droite et direction++
  469.                                             compare_mine()
  470.                             end
  471.                             direction=0
  472.                                
  473.                             turtleDown()
  474.                                
  475.                     end
  476.                
  477.                     print("fin de la boucle "..profondeur)
  478.                     -- ici je remonte à la surface
  479.                     while profondeur ~= 0 do
  480.                                     if turtle.detectUp() then turtle.digUp() end
  481.                                     turtle.up()
  482.                                     profondeur = profondeur-1
  483.                                     -- modem.transmit(idChannel,idChannel,"profondeur:"..profondeur)
  484.                                     verifFuel()
  485.                     end
  486.                
  487.                     p = p + 1
  488.     end
  489.      
  490.     -- modem.transmit(idChannel,idChannel,"etat:retour")
  491.     deplacement(0,0) -- retour au point de départ
  492.     -- modem.transmit(idChannel,idChannel,"etat:fin")
  493.     --rednet.close(right)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement