Bimgo

RapidQuary v2.2

May 28th, 2013 (edited)
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.02 KB | None | 0 0
  1. -- Paramètre / Données de base
  2. xCoord = 0
  3. zCoord = 0
  4. yCoord = 150
  5. xTarget = 0
  6. zTarget = 0
  7. yTarget = 150
  8. startorientation = 1
  9. orientation = 2
  10. orientations = {"Nord", "Est", "Sud", "Ouest"}
  11. zDif = {-1, 0, 1, 0}
  12. xDif = {0, 1, 0, -1}
  13. longeurLigne = 10
  14. lignes = 10
  15. yMin = 200
  16. yMinEnd = 5
  17. local maxnrgy = 1000000
  18. local triitem
  19. local gps_present = 0 --gps présent : si oui =1 si non =0
  20. local isWirelessTurtle
  21. --myid = os.getComputerLabel()
  22.  
  23. -- Detection modem, si oui activation modem
  24. function modem()
  25.     isWirelessTurtle = peripheral.isPresent("right")
  26.     if (isWirelessTurtle == true) then
  27.                 turtleId = os.getComputerLabel()
  28.                 rednet.open("right")
  29.     end
  30. end
  31.    
  32. -- statut avec message rednet
  33. function info(message)
  34.     if (isWirelessTurtle == true) then
  35.         if (turtleId == nil) then -- Brodcast message si pas de label
  36.             rednet.broadcast(message)
  37.         else -- Broadcast le message (avec l'id en prefixe)
  38.             rednet.broadcast("[".. turtleId.."] "..message)
  39.         end
  40.     end
  41. end
  42.          
  43. -- mode minage
  44. function choix()
  45.     modem()
  46.     print("Que voulez-vous faire?")
  47.     print("[L]=Layer [Q]=Quary [D]=Quary sans tri [R]=Recharge [S]=Stop")
  48.     local event,input = os.pullEvent("char")
  49.     if input == "L" then
  50.         digLayer()
  51.     end
  52.     if input == "Q" then
  53.         triitem = 1
  54.         coordonnee()
  55.         digQuary()
  56.     end
  57.     if input == "D" then
  58.         triitem = 0
  59.         coordonnee()
  60.         digQuary()
  61.     end
  62.     if input == "R" then
  63.         chargestation()
  64.     end
  65.     if input == "S" then -- arrêt programme
  66.     end
  67. end
  68.  
  69. -- Choix des coordonnées de départ
  70. function coordonnee()
  71.     print("Saisir l'altitude : Y =")
  72.     yCoord = tonumber(read())
  73.     if gps_present == 1 then
  74.         print("Orientation [1=Nord, 2=Est, 3=Sud, 4=Ouest]:")  
  75.         startorientation = tonumber(read())
  76.     else
  77.         startorientation = 1
  78.     end
  79.     orientation = startorientation
  80.     print("Longeur à miner :")
  81.     lignes = tonumber(read())
  82.     longeurLigne = lignes
  83.     shell.run('clear')
  84.     xHome = xCoord
  85.     zHome = zCoord
  86.     yHome = yCoord
  87.     yTravel = yCoord
  88. end
  89.  
  90. -- choix mode minage rapide (sans cobble, dirt, gravel, ...)
  91. -- placer les items à ne pas garder dans les slot 13, 14 et 15
  92. function blocinutile()
  93.     for i = 1,12 do
  94.         turtle.select(i)
  95.         if turtle.compareTo(13) == true or turtle.compareTo(14) == true or turtle.compareTo(15) == true then
  96.             turtle.drop()
  97.         end
  98.     end
  99.     for i = 13,15 do
  100.         turtle.select(i)
  101.                 nbItem = turtle.getItemCount(i)
  102.                 if nbItem > 1 then
  103.             nbItem = turtle.getItemCount(i) - 1
  104.             turtle.drop(nbItem)
  105.         end
  106.     end
  107.     turtle.select(1)
  108. end
  109.  
  110. -- function de déplacement
  111. function gauche()
  112.     orientation = orientation - 1
  113.     orientation = (orientation - 1) % 4
  114.     orientation = orientation + 1
  115.     turtle.turnLeft()
  116. end
  117.  
  118. function droite()
  119.     orientation = orientation - 1
  120.     orientation = (orientation + 1) % 4
  121.     orientation = orientation + 1
  122.     turtle.turnRight()
  123. end
  124.  
  125. function avance()
  126.     xCoord = xCoord + xDif[orientation]  
  127.     zCoord = zCoord + zDif[orientation]
  128.     while turtle.detect() do
  129.         turtle.dig()
  130.     end
  131.     moved = false
  132.     while not(moved) do -- dans le cas ou la turtle n'avance pas (obstacle, mob)
  133.         turtle.dig()
  134.         moved = turtle.forward()
  135.     end
  136. end
  137.  
  138. function haut()
  139.     yCoord = yCoord + 1
  140.     turtle.digUp()
  141.     moved = false
  142.     while not(moved) do
  143.         moved = turtle.up()
  144.     end
  145. end
  146.  
  147. function bas()
  148.         yCoord = yCoord - 1
  149.         turtle.digDown()
  150.         moved = false
  151.         while not(moved) do
  152.                         moved = turtle.down()
  153.         end
  154.         if yMin > yCoord then
  155.                         yMin = yCoord
  156.         end
  157. end
  158.  
  159. -- Rotation de la turtle
  160. function look(direction)
  161.         while direction ~= orientations[orientation] do
  162.                         droite()
  163.         end      
  164. end
  165.  
  166. --Système de déplacement goto
  167. function goto(xTarget, zTarget, yTarget)
  168.         -- déplacement  
  169.         while yTarget < yCoord do
  170.                 bas()
  171.         end
  172.         while yTarget > yCoord do
  173.                 haut()
  174.         end
  175.         if xTarget < xCoord then
  176.                 look("Ouest")
  177.                 while xTarget < xCoord do
  178.                         avance()
  179.                 end
  180.         end
  181.         if xTarget > xCoord then
  182.                 look("Est")
  183.                 while xTarget > xCoord do
  184.                         avance()
  185.                 end
  186.         end
  187.         if zTarget < zCoord then
  188.                 look("Nord")
  189.                 while zTarget < zCoord do
  190.                         avance()
  191.                 end
  192.         end
  193.         if zTarget > zCoord then
  194.                 look("Sud")
  195.                 while zTarget > zCoord do
  196.                         avance()
  197.                 end
  198.         end
  199. end
  200.  
  201. function digLine()
  202.         LL = longeurLigne - 1
  203.         for i = 1,LL do
  204.                 if InventairePlein() then
  205.                         returnItems()
  206.                 end
  207.                 if i == 1 then
  208.                         turtle.digUp()
  209.                         turtle.digDown()
  210.                 end
  211.                 avance()
  212.                 turtle.digUp()
  213.                 turtle.digDown()
  214.         end
  215. end
  216.  
  217. function digLayer() -- il a été admis que la turtle mine sur la droite et en avant par rapport à la position initiale
  218.         for i = 1, lignes do
  219.                 digLine()
  220.                 if (i%2) == 1 and i < lignes then
  221.                         droite()
  222.                         avance()
  223.                         droite()
  224.                 elseif i < lignes then
  225.                         gauche()
  226.                         avance()
  227.                         gauche()
  228.                 end
  229.         end
  230.         goto(xQuary, zQuary, yCoord)
  231.         look(orientations[startorientation])
  232.         bas()
  233.         bas()
  234.         bas()
  235.         blocinutile()
  236. end
  237.  
  238. function digQuary(xTarget, zTarget, yTarget)
  239.         xQuary = xCoord
  240.         yQuary = yCoord
  241.         zQuary = zCoord
  242.         fuelCheckStart() -- nbDepl()
  243.         while yCoord > yMinEnd do
  244.                 digLayer()
  245.         end
  246.         goto(xHome, zHome, yTravel)
  247.         goto(xHome, zHome, yHome)
  248.         yMin = 999
  249. end
  250.  
  251. function InventairePlein()
  252.         turtle.select(16)
  253.         full = turtle.getItemCount(16) > 0
  254.         turtle.select(1)
  255.         return full
  256. end
  257.  
  258. function returnItems()
  259.         if triitem == 1 then
  260.             blocinutile()
  261.         end
  262.         xProgress = xCoord
  263.         zProgress = zCoord
  264.         yProgress = yCoord
  265.         oProgress = orientation
  266.         print("Progress: X="..xProgress.." Y="..yProgress.." Z="..zProgress.." o="..oProgress)
  267.         print("@ Home: "..xHome.." "..zHome)
  268.         goto(xHome, zHome, yTravel)
  269.         goto(xHome, zHome, yHome)
  270.         chest_pos = startorientation + 2
  271.         look(orientations[chest_pos])
  272.         sleep(2)
  273.         shell.run('clear')  
  274.         if triitem == 1 then -- tri des item activé (option quary avec filtre)
  275.             for i = 1,12 do
  276.                 turtle.select(i)
  277.                 turtle.drop()
  278.             end
  279.             i = 16
  280.             turtle.select(i)
  281.             turtle.drop()
  282.         else
  283.             for i = 1,16 do
  284.                 turtle.select(i)
  285.                 turtle.drop()
  286.             end
  287.         end
  288.         turtle.select(1)
  289.         look(orientations[startorientation])
  290.         -- fuelCheck()
  291.         goto(xProgress,zProgress,yTravel)
  292.         goto(xProgress,zProgress,yProgress)
  293.         look(orientations[oProgress])
  294. end
  295.          
  296. function nbDeplQuary()
  297.         DeplQuary = tonumber(longeurLigne) * tonumber(lignes) * ((tonumber(yQuary) - tonumber(yMinEnd))/3) + (tonumber(yQuary) - tonumber(yMinEnd)) -- y compris ajouts déplacement du yQuary au yMinEnd
  298. end
  299.  
  300. function fuelCheckStart()
  301.         nbDeplQuary()
  302.         Depl = 0
  303.         TotalDepl = DeplQuary + Depl
  304.         FuelActuel = turtle.getFuelLevel()
  305.         FuelNeed = FuelActuel - TotalDepl
  306.         while TotalDepl - turtle.getFuelLevel() >= 0 do
  307.                 manque = TotalDepl - turtle.getFuelLevel()
  308.                 manqueCoal = math.ceil(manque/80) + 5 -- 1 coal = 80??? 96 deplacements; 1 lava cell = 1200 déplacements; avec 5 coal de reserve = 400 déplacements
  309.                 print("Veuillez placer "..(manqueCoal).." Coal ou Charcoal et validez avec ENTER")
  310.                 print("TotalDepl="..TotalDepl.." FuelActuel="..FuelActuel)
  311.                 read()
  312.                 if turtle.getItemCount(1) > 0 then
  313.                         if manqueCoal > 64 then
  314.                                 print("le fuel necessaire etant > 64 utiliser egalement le slot 2")
  315.                                 turtle.select(1)
  316.                                 turtle.refuel(64)
  317.                                 turtle.select(2)
  318.                                 reste = manqueCoal - 64
  319.                                 turtle.refuel(reste)
  320.                         else
  321.                                 turtle.refuel(manqueCoal)
  322.                         end
  323.                 end
  324.                 print("Plein effectué, au travail")
  325.                 fuelLevel = turtle.getFuelLevel()
  326.                 print("Information : fuel level="..fuelLevel)
  327.                 sleep(2)
  328.                 shell.run('clear')    
  329.         end
  330. end
  331.  
  332. function fuelCheck()
  333.         nbDeplacement ()
  334.         manqueCoal = math.ceil(Deplacement/80) -- 1 coal = 80?? 96 deplacements; 1 lava cell = 1200 déplacements
  335.         print("Veuillez placer "..(manqueCoal).." et validez avec ENTER")
  336.         print("Information : Fuel restant =")
  337.         read()
  338.         if turtle.getItemCount(1) >= manqueCoal then
  339.                 qty = turtle.getItemCount(1)
  340.                 turtle.refuel(qty)
  341.                 print("Plein effectué, au travail")
  342.                 fuelLevel = turtle.getFuelLevel()
  343.                 print("Information : fuel level="..fuelLevel)
  344.                 sleep(2)
  345.                 shell.run('clear')
  346.         end
  347. end
  348.  
  349. function chargestation() -- station de recharge de turtle (charge station)
  350.         a = turtle.getFuelLevel()
  351.         print("En attente pour recharge")
  352.         print("Turtle "..myid)
  353.         print("Niveau de charge : "..a)
  354.         mess = "Mode charge actif"
  355.         info(mess)
  356.         shell.run('clear')
  357.         while a < maxnrgy do
  358.                 a = turtle.getFuelLevel()
  359.                 turtle.refuel()
  360.                 print("Niveau de charge "..myid.." : "..a.." / "..maxnrgy)
  361.                 mess = a
  362.                 info(mess)
  363.                 sleep(10)
  364.                 a1 = turtle.getFuelLevel()
  365.                 if a == a1 then
  366.                     turtle.select(1)
  367.                     turtle.digDown()
  368.                     turtle.placeDown()
  369.                 end
  370.                 shell.run('clear')
  371.         end
  372.         print("Chargement termine!")
  373.         mess = "Charge terminee"
  374.         info(mess)
  375.         read()
  376. end
  377. -----------PROGRAMME----------------
  378. local myid = os.getComputerLabel()
  379. local fuellvl = turtle.getFuelLevel()
  380.  
  381. shell.run('clear')
  382. print("Turtle : "..myid)
  383. print("Niveau de charge : "..fuellvl)
  384. print("")
  385. choix()
Add Comment
Please, Sign In to add comment