Bimgo

Quary_3_beta

Feb 4th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.30 KB | None | 0 0
  1. --[[ Ce programme [créé par Bimgo]
  2.  
  3. Susceptible d'être modifié à tout moment pour amélioration et/ou réparation.
  4. Dernière édition : 04.02.2013  -  17:00
  5.  
  6. Next step :
  7. * gps localisation
  8. * check consommable
  9. * suck fuel from chest **********************
  10. * put item in chest **************************
  11.  
  12. ]]--
  13.  
  14. -- Position et orientation initales
  15. xCoord = 999
  16. zCoord = 999
  17. yCoord = 150
  18.  
  19. xQuary = 999
  20. zQuary = 999
  21. yQuary = 150
  22.  
  23. xProgress = 999
  24. zProgress = 999
  25. yProgress = 150
  26. oProgress = 1
  27.  
  28. --xHome = xCoord
  29. --zHome = zCoord
  30. --yHome = yCoord
  31.  
  32. yTravel = 68
  33.  
  34. gpsnavigator = 1
  35.  
  36. startorientation = 1
  37. orientation = 2
  38. orientations = {"Nord", "Est", "Sud", "Ouest"}
  39.  
  40. zDif = {-1, 0, 1, 0}
  41. xDif = {0, 1, 0, -1}
  42.  
  43. longeurLigne = 10
  44. lignes = 10
  45.  
  46. yMin = 999
  47. yMinEnd = 7
  48.  
  49. JobDispo = true
  50.  
  51. -- continuer = 2 -------------------------------------------------------------------
  52.  
  53. manager = 7
  54.  
  55. fuelLevel = 0
  56.  
  57. --[[ FONCTIONS DEFINIES :
  58. getPos : gps localisation
  59. gauche : tourne a gauche en modifiant l orientation
  60. droite : tourne a droite en modifiant l orientation
  61. avance : avance en minant avec gestion des genes en cas d'obstacle mobile (personne, mob, npc, ...)
  62. haut : monte en minant avec gestion des genes en cas d obstacle mobile
  63. bas : descente en minant avec gestion des genes en cas d obstacle mobile
  64. look : pointage dans la bonne direction par une rotation a droite
  65. digLine : minage d une ligne
  66. digLayer : minage de la couche
  67. digQuary : minage complet
  68. nbDepl : calcul du nombre de déplacement pour arriver sur zone
  69. goto : fonction goto destination
  70. InventairePlein : verifie si le dernier slot a un item ou pas si oui -> vidage
  71. returnItems : retours des items une fois la turtle full et reprise du job ou il a ete laisse
  72. getJob : requete d'un job à la station
  73. nbDeplQuary : calcul du nombre de deplacement pour faire le quary + complément à nbDepl pour aller monter du fond du quary
  74. ndDeplacement : nombre de deplacement lors du retours des items (car le calcul inital ne le prévois pas)
  75. ** move : introduction destination + calcul nb deplacement
  76. fuelCheckStart : verification carbu necessaire au départ
  77. fuelCheck : verification carbu necessaire
  78.  
  79. ** function non utilisée dans le programme
  80.  
  81. FUNCTION                       DONNEES DEFINIES                        DONNES UTILISEES
  82. getPos                      >> iCoord et iHome                  >>
  83. getJob                      >> iQuary                           >>
  84.   nbDeplQuary               >> DeplQuary                        >> longeurLigne, lignes, yQuary, yMinEnd
  85.   digQuary                  >>                                  >> iQuary, iHome, yTravel, yMin, yMinEnd
  86.     nbDepl                  >> Depl                             >> iCoord
  87.     fuelCheckStart          >> TotalDepl, FuelActuel, FuelNeed  >> Depl, DeplQuary
  88.     goto                    >> iTarget                          >> iCoord
  89.     digLayer                >>                                  >>
  90.       digLine               >>                                  >>
  91.         InventairePlein     >>                                  >>
  92.         returnItems         >> iProgress, oProgress             >> iCoord
  93.           fuelCheck         >>                                  >>
  94.             nbDeplacement   >>                                  >> iProgress, iCoord, yTravel
  95.  
  96. -------------------------------------------------------------------------------------------
  97. shell.run('clear')
  98.  
  99. turtle.suckUp(quantity)    
  100.  
  101. _REDSTONE IMPULSE      
  102. redstone.setOutput("back", true)
  103. sleep(1)
  104. redstone.setOutput("back", false)
  105. ]]--
  106.  
  107. function getPos(posX,posY,posZ)
  108.   print("TURTLE DE MINAGE")
  109.   print("Voulez-vous utilisez la station de commande pour les job? [1=Oui, 0=Non]")
  110.   read()
  111. --  gpsnavigator = tonumber(read())
  112.  
  113.   sleep(2)
  114.   shell.run('clear')
  115.  
  116. --  if gpsnavigator = 1 then
  117. --GPS localisator
  118. --    rednet.close("right")
  119. --    rednet.open("right")
  120. --      posX, posY, posZ = gps.locate(3) -- posX, posY, posZ = gps.locate(2, false)
  121. --  print("Position : X="..posX.." Y="..posY.." Z="..posZ)
  122. --    xCoord = posX
  123. --    yCoord = posY
  124. --    zCoord = posZ
  125. --  else
  126. -- Alternative sans gps  
  127.  
  128.     print("Saisir les coordonnées du lieu d'ou part la turtle:")
  129.     print("X =")
  130.     xCoord = tonumber(read())
  131.     print("Y =")
  132.     yCoord = tonumber(read())
  133.     print("Z =")
  134.     zCoord = tonumber(read())
  135.     print("Orientation [1=Nord, 2=Est, 3=Sud, 4=Ouest]:")  
  136.     startorientation = tonumber(read())
  137.     orientation = startorientation
  138.     print(" ")
  139.     print("Coordonnees : X="..xCoord.." Y="..yCoord.." Z="..zCoord.." Orientation="..orientations[orientation])
  140.     sleep(2)
  141.     shell.run('clear')
  142. --  end
  143. -- Definition Home  
  144.   xHome = xCoord
  145.   zHome = zCoord
  146.   yHome = yCoord
  147. end
  148.  
  149. function gauche()
  150.   orientation = orientation - 1
  151.   orientation = (orientation - 1) % 4
  152.   orientation = orientation + 1
  153.  
  154.   turtle.turnLeft()
  155. end
  156.  
  157. function droite()
  158.   orientation = orientation - 1
  159.   orientation = (orientation + 1) % 4
  160.   orientation = orientation + 1
  161.  
  162.   turtle.turnRight()
  163. end
  164.  
  165. function avance()
  166.   xCoord = xCoord + xDif[orientation]  
  167.   zCoord = zCoord + zDif[orientation]
  168.  
  169.   while turtle.detect() do
  170.     turtle.dig()
  171.   end
  172.  
  173.   moved = false
  174.   while not(moved) do
  175.     moved = turtle.forward()
  176.   end
  177. end
  178.  
  179. function haut()
  180.   yCoord = yCoord + 1
  181.  
  182.   turtle.digUp()
  183.  
  184.   moved = false
  185.   while not(moved) do
  186.     moved = turtle.up()
  187.   end
  188. end
  189.  
  190. function bas()
  191.   yCoord = yCoord - 1
  192.  
  193.   turtle.digDown()
  194.  
  195.   moved = false
  196.   while not(moved) do
  197.     moved = turtle.down()
  198.   end
  199.  
  200.   if yMin > yCoord then
  201.     yMin = yCoord
  202.   end
  203. end
  204.  
  205. function look(direction)
  206.   while direction ~= orientations[orientation] do
  207.     droite()
  208.   end      
  209. end
  210.  
  211. function digLine()
  212.   LL = longeurLigne - 1
  213.   for i = 1,LL do
  214.     if InventairePlein() then
  215.       returnItems()
  216.     end
  217.     avance()
  218.   end
  219. end
  220.  
  221. function digLayer()
  222. -- il a été admis que la turtle mine sur la droite et en avant par rapport à la position initiale
  223.   for i = 1, lignes do
  224.     digLine()
  225.     if (i%2) == 1 and i < lignes then
  226.       droite()
  227.       avance()
  228.       droite()
  229.     elseif i < lignes then
  230.       gauche()
  231.       avance()
  232.       gauche()
  233.     end
  234.   end
  235.   goto(xQuary, zQuary, yCoord)
  236.   look(orientations[startorientation])
  237.   bas()
  238. --  print("Coord : X="..xCoord.." Y="..yCoord.." Z="..zCoord.." Orientation="..orientations[orientation])
  239. --  read()
  240. end
  241.  
  242. function digQuary(xTarget, zTarget, yTarget)
  243.   xQuary = xTarget
  244.   zQuary = zTarget
  245.   yQuary = yTarget
  246.  
  247.   nbDepl()
  248.   fuelCheckStart()
  249.  
  250.   goto(xQuary, zQuary, yTravel)
  251.   goto(xQuary, zQuary, yQuary)
  252.   look(orientations[startorientation])
  253.   while yMin > yMinEnd do
  254.     digLayer()
  255.   end
  256.   goto(xQuary, zQuary, yQuary)
  257.   goto(xHome, zHome, yTravel)
  258.   goto(xHome, zHome, yHome)
  259.   yMin = 999
  260. end
  261.  
  262. function nbDepl()
  263.   if yQuary < yCoord then
  264.     yDepl = yTravel - yQuary
  265.   else
  266.     yDepl = yQuary - yCoord
  267.   end
  268.   if xQuary < xCoord then
  269.     xDepl = xCoord - xQuary
  270.   else
  271.     xDepl = xQuary - xCoord
  272.   end
  273.   if zQuary < zCoord then
  274.     zDepl = zCoord - zQuary
  275.   else
  276.     zDepl = zQuary - zCoord
  277.   end
  278.   Depl = 2 * (yDepl + xDepl + zDepl)
  279. --  print("nombre de deplacement = "..Depl)
  280. end
  281.  
  282. function goto(xTarget, zTarget, yTarget)
  283. -- déplacement  
  284.   while yTarget < yCoord do
  285.     bas()
  286.   end
  287.   while yTarget > yCoord do
  288.     haut()
  289.   end
  290.  
  291.   if xTarget < xCoord then
  292.     look("Ouest")
  293.     while xTarget < xCoord do
  294.       avance()
  295.     end
  296.   end
  297.   if xTarget > xCoord then
  298.     look("Est")
  299.     while xTarget > xCoord do
  300.       avance()
  301.     end
  302.   end
  303.  
  304.   if zTarget < zCoord then
  305.     look("Nord")
  306.     while zTarget < zCoord do
  307.       avance()
  308.     end
  309.   end
  310.   if zTarget > zCoord then
  311.     look("Sud")
  312.     while zTarget > zCoord do
  313.       avance()
  314.     end
  315.   end
  316. end
  317.  
  318. function InventairePlein()
  319.   turtle.select(16)
  320.   full = turtle.getItemCount(16) > 0
  321.   turtle.select(1)
  322.   return full
  323. end
  324.  
  325. function returnItems()
  326.   xProgress = xCoord
  327.   zProgress = zCoord
  328.   yProgress = yCoord
  329.   oProgress = orientation
  330.   print("Progress: X="..xProgress.." Y="..yProgress.." Z="..zProgress.." o="..oProgress)
  331.   print("@ Home: "..xHome.." "..zHome)
  332.  
  333.   goto(xHome, zHome, yTravel)
  334.   goto(xHome, zHome, yHome)
  335.   look(orientations[startorientation])
  336.   sleep(2)
  337.   shell.run('clear')   
  338.   purge()
  339.   turtle.select(1)
  340.  
  341.   fuelCheck()
  342.  
  343.   goto(xProgress,zProgress,yTravel)
  344.   goto(xProgress,zProgress,yProgress)
  345.   look(orientations[oProgress])
  346. end
  347.  
  348.  
  349. function getJob()
  350. -- Verification utilisation avec station (gpsnavigator)
  351.  
  352. --  if gpsnavigator = 1 then
  353. --  si option gpsnavigator = 1 = GPS on
  354. --    while JobDispo do
  355. --       print("Requete d'un job...")
  356. --      rednet.send(manager, "getJob")
  357. --      id, message, dis = rednet.receive()
  358.    
  359. --      if message == "Oui" then
  360. --        id, xRec, dis = rednet.receive()
  361. --        id, zRec, dis = rednet.receive()
  362. --        id, yRec, dis = rednet.receive()
  363.      
  364. --      xQuary = tonumber(xRec)
  365. --      zQuary = tonumber(zRec)
  366. --      yQuary = tonumber(yRec)
  367.        
  368. --      print("Job dispo : "..xQuary.." "..zQuary)
  369. --      sleep(2)
  370. --      shell.run('clear') 
  371.      
  372. --      nbDeplQuary()
  373. --      digQuary(xQuary, zQuary, yQuary)        
  374.      
  375.   --        look(orientations[startorientation)
  376. --      print("Job termine")
  377. --    elseif message == "Non" then
  378. --        print("Pas de job dispo... en attente nouvelle mission.")
  379. --       JobDispo = false
  380. --      end
  381. --    end    
  382. --  else
  383. -- si option gpsnavigator = 0 = GPS off
  384. --  if continuer = 1 then----------------------------------------------
  385. --    xQuary = xCoord
  386. --    yQuary = yCoord
  387. --    zQuary = zCoord
  388. --  else
  389.       print("Saisir les coordonnées du lieu du quary:")
  390.       print("Quary X =")
  391.       xQuary = tonumber(read())
  392.       print("Quary Y =")
  393.       yQuary = tonumber(read())
  394.       print("Quary Z =")
  395.       zQuary = tonumber(read())
  396. --  end
  397.     print("Profondeur finale (yMinEnd = 7) =")
  398.     yMinEnd = tonumber(read())
  399.  
  400.     print("yTravel =")
  401.     yTravel = tonumber(read())
  402.  
  403.     print("Job dispo : "..xQuary.." "..zQuary)
  404.     sleep(2)
  405.     shell.run('clear') 
  406.    
  407.     nbDeplQuary()
  408.     digQuary(xQuary, zQuary, yQuary)        
  409.    
  410.     look(orientations[startorientation])
  411.     print("Job termine")
  412.     print("")
  413.     print("Voulez-vous continuer ? [1=oui; 2=non]")
  414.     local continuer = tonumber(read())
  415.    
  416. --  if continuer = 1 then
  417. --    getJob() --------------------------------------------------------------------------------definir : getJob(xCoord, yCoord, zCoord) ?
  418. --  else
  419. --    print("Au revoir!")
  420.  --     shell.run('clear')   
  421. --  end
  422.   --end
  423. end
  424.  
  425. function nbDeplQuary()
  426.   DeplQuary = tonumber(longeurLigne) * tonumber(lignes) * (tonumber(yQuary) - tonumber(yMinEnd)) + (tonumber(yQuary) - tonumber(yMinEnd))
  427.   -- y compris ajouts déplacement du yQuary au yMinEnd
  428. end
  429.  
  430. function nbDeplacement()
  431. --FONCTION A RACCOURCIR... nbDeplacement (xDest yDest zDest xStart yStart zStart)
  432. --  if yDest < yStart then
  433. --    yDepl = yTravel - yDest
  434. --  else
  435. --    yDepl = yDest - yStart
  436. --  end
  437.   if yProgress < yCoord then
  438.     yDeplacement = yTravel - yProgress
  439.   else
  440.     yDeplacement = yProgress - yCoord
  441.   end
  442.   if xProgress < xCoord then
  443.     xDeplacement = xCoord - xProgress
  444.   else
  445.     xDeplacement = xProgress - xCoord
  446.   end
  447.   if zProgress < zCoord then
  448.     zDeplacement = zCoord - zProgress
  449.   else
  450.     zDeplacement = zProgress - zCoord
  451.   end
  452.   Deplacement = 2 * (yDeplacement + xDeplacement + zDeplacement) -- 2x car aller et retour
  453. --  print("nombre de deplacement = "..Deplacement)
  454. end
  455.  
  456. function move() -- commande abandonnées
  457.   print("Position actuelle : X="..xCoord.." Z="..zCoord.." Y="..yCoord.." Orientation="..orientations[orientation])
  458.   xHome = xCoord
  459.   zHome = zCoord
  460.   yHome = yCoord
  461.  
  462.   print("Destination ?")
  463.   print("X :")
  464.   x = tonumber(read ())
  465.   print("Y :")
  466.   y = tonumber(read ())
  467.   print("Z :")
  468.   z = tonumber(read())
  469.  
  470.   print("@ "..x.." "..z.." "..y)
  471.   goto(x,z,y)
  472. --move()
  473. end
  474.  
  475. function fuelCheckStart()
  476.   TotalDepl = DeplQuary + Depl
  477.   FuelActuel = turtle.getFuelLevel()
  478.   FuelNeed = FuelActuel - TotalDepl
  479.  
  480.   while TotalDepl - turtle.getFuelLevel() >= 0 do
  481.     manque = TotalDepl - turtle.getFuelLevel()
  482.     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
  483.     -- print("Veuillez placer "..(manqueCoal).." et validez avec ENTER")
  484.     print("TotalDepl="..TotalDepl.." FuelActuel="..FuelActuel)
  485.     read()
  486.     purge()
  487.     if manqueCoal < 64 then
  488.       refuel()
  489.     else
  490.       turtle.select(1)
  491.       turtle.suckDown()
  492.       if turtle.getItemCount(1) = 64 else
  493.         turtle.refuel(64)
  494.       else
  495.         print("Stock épuisé... remplir le coffre et valider avec ENTER")
  496.         read()
  497.         fuelCheckStart()
  498.       end
  499.       turtle.suckDown()
  500.       reste = manqueCoal - 64
  501.  
  502.       if turtle.getItemCount(1) > reste else
  503.         turtle.refuel(reste)
  504.         qt = 64 - reste
  505.         turtle.dropDown(qt)
  506.       else
  507.         print("Stock épuisé... remplir le coffre et valider avec ENTER")
  508.         read()
  509.         fuelCheckStart()
  510.       end
  511.     end
  512.     print("Plein effectué, au travail")
  513.     fuelLevel = turtle.getFuelLevel()
  514.     print("Information : fuel level="..fuelLevel)
  515.     sleep(2)
  516.     shell.run('clear') 
  517.    
  518.    
  519.    
  520.     -- if turtle.getItemCount(1) > 0 then
  521.       -- if manqueCoal > 64 then
  522.       -- print("le fuel necessaire etant > 64 utiliser egalement le slot 2")
  523.         -- turtle.select(1)
  524.         -- turtle.suckDown(manqueCoal)
  525.         -- turtle.refuel(64)
  526.         -- turtle.select(2)
  527.         -- reste = manqueCoal - 64
  528.         -- turtle.refuel(reste)
  529.       -- else
  530.         -- turtle.select(1)
  531.         -- turtle.suckDown(manqueCoal)
  532.         -- turtle.refuel(manqueCoal)
  533.       -- end
  534.     -- end
  535.     -- print("Plein effectué, au travail")
  536.     -- fuelLevel = turtle.getFuelLevel()
  537.     -- print("Information : fuel level="..fuelLevel)
  538.     -- sleep(2)
  539.     -- shell.run('clear')  
  540.   end
  541. end
  542.  
  543. function fuelCheck()
  544.   nbDeplacement ()
  545.   manqueCoal = math.ceil(Deplacement/80) + 1 -- 1 coal = 80?? 96 deplacements; 1 lava cell = 1200 déplacements
  546.  
  547.   purge() -- purge de l'inventaire
  548.   refuel()
  549. end
  550.  
  551. function purge()
  552.   for i = 1,16 do
  553.     turtle.select(i)
  554.     turtle.drop()
  555.   end
  556. end
  557.  
  558. function refuel() -- for fuel need < 64 items
  559.   turtle.select(1)
  560.   turtle.suckDown(manqueCoal)
  561.  
  562.   if turtle.getItemCount(1) >= manqueCoal then
  563.     qty = turtle.getItemCount(1) - manqueCoal
  564.     turtle.refuel(manqueCoal)
  565.     print("Refuel de :"..manqueCoal)
  566.     turtle.dropDown(qty)
  567.     print("Rendu :"..qty)
  568.     print("Plein effectué, au travail")
  569.     fuelLevel = turtle.getFuelLevel()
  570.     print("Information : fuel level="..fuelLevel)
  571.     sleep(2)
  572.     shell.run('clear')
  573.   else
  574.     print("Stock épuisé... remplir le coffre et valider avec ENTER")
  575.     read()
  576.     sleep(2)
  577.     shell.run('clear')
  578.     fuelCheck()    
  579.   end
  580. end
  581.  
  582. --[[ PROGRAMME ]]--
  583. getPos()
  584. getJob()
Advertisement
Add Comment
Please, Sign In to add comment