Bimgo

Minecraft-Computercraft-Turtle quary program-LOCAL

Jan 16th, 2013
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.61 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 : 23.01.2013  -  15:20
  5.  
  6. VERSION LOCALE
  7. ]]--
  8.  
  9. -- Position et orientation initales
  10. xCoord = 999
  11. zCoord = 999
  12. yCoord = 150
  13.  
  14. xQuary = 999
  15. zQuary = 999
  16. yQuary = 150
  17.  
  18. xProgress = 999
  19. zProgress = 999
  20. yProgress = 150
  21. oProgress = 1
  22.  
  23.  
  24.  
  25. yTravel = 68
  26. yMin = 999
  27. yMinEnd = 7
  28.  
  29. orientation = 2
  30. orientations = {"Nord", "Est", "Sud", "Ouest"}
  31.  
  32. zDif = {-1, 0, 1, 0}
  33. xDif = {0, 1, 0, -1}
  34.  
  35. longeurLigne = 10
  36. lignes = 10
  37.  
  38. JobDispo = true
  39.  
  40. manager = 7
  41.  
  42. --[[ FONCTIONS DEFINIES :
  43. getPos : gps localisation
  44. gauche : tourne a gauche en modifiant l orientation
  45. droite : tourne a droite en modifiant l orientation
  46. avance : avance en minant avec gestion des genes en cas d'obstacle mobile (personne, mob, npc, ...)
  47. haut : monte en minant avec gestion des genes en cas d obstacle mobile
  48. bas : descente en minant avec gestion des genes en cas d obstacle mobile
  49. look : pointage dans la bonne direction par une rotation a droite
  50. digLine : minage d une ligne
  51. digLayer : minage de la couche
  52. digQuary : minage complet
  53. nbDepl : calcul du nombre de déplacement pour arriver sur zone
  54. goto : fonction goto destination
  55. InventairePlein : verifie si le dernier slot a un item ou pas si oui -> vidage
  56. returnItems : retours des items une fois la turtle full et reprise du job ou il a ete laisse
  57. getJob : requete d'un job à la station
  58. nbDeplQuary : calcul du nombre de deplacement pour faire le quary + complément à nbDepl pour aller monter du fond du quary
  59. ndDeplacement : nombre de deplacement lors du retours des items (car le calcul inital ne le prévois pas)
  60. move : introduction destination + calcul nb deplacement
  61. fuelCheckStart : verification carbu necessaire au départ
  62. fuelCheck : verification carbu necessaire
  63. consomableCheck :
  64.  
  65. FUNCTION                       DONNEES DEFINIES                        DONNES UTILISEES
  66. getPos                      >> iCoord et iHome                  >>
  67. getJob                      >> iQuary                           >>
  68.   nbDeplQuary               >> DeplQuary                        >> longeurLigne, lignes, yQuary, yMinEnd
  69.   digQuary                  >>                                  >> iQuary, iHome, yTravel, yMin, yMinEnd
  70.     nbDepl                  >> Depl                             >> iCoord
  71.     fuelCheckStart          >> TotalDepl, FuelActuel, FuelNeed  >> Depl, DeplQuary
  72.     goto                    >> iTarget                          >> iCoord
  73.     digLayer                >>                                  >>
  74.       digLine               >>                                  >>
  75.         InventairePlein     >>                                  >>
  76.         returnItems         >> iProgress, oProgress             >> iCoord
  77.           fuelCheck         >>                                  >>
  78.             nbDeplacement   >>                                  >> iProgress, iCoord, yTravel
  79.  
  80. -------------------------------------------------------------------------------------------
  81.  
  82. shell.run('clear')
  83. turtle.suckUp(quantity)    
  84.  
  85. _REDSTONE IMPULSE      
  86. redstone.setOutput("back", true)
  87. sleep(1)
  88. redstone.setOutput("back", false)
  89.  
  90.  
  91.  
  92. ]]--
  93.  
  94. --function getPos(posX,posY,posZ)
  95. --  rednet.close("right")
  96. --  rednet.open("right")
  97. --  posX, posY, posZ = gps.locate(3) -- posX, posY, posZ = gps.locate(2, false)
  98. --  print("Position : X="..posX.." Y="..posY.." Z="..posZ)
  99. --  xCoord = posX
  100. --  yCoord = posY
  101. --  zCoord = posZ
  102.  
  103. --  xHome = xCoord
  104. --  zHome = zCoord
  105. --  yHome = yCoord
  106. --end
  107.  
  108. function gauche()
  109.   orientation = orientation - 1
  110.   orientation = (orientation - 1) % 4
  111.   orientation = orientation + 1
  112.  
  113.   turtle.turnLeft()
  114. end
  115.  
  116. function droite()
  117.   orientation = orientation - 1
  118.   orientation = (orientation + 1) % 4
  119.   orientation = orientation + 1
  120.  
  121.   turtle.turnRight()
  122. end
  123.  
  124. function avance()
  125.   xCoord = xCoord + xDif[orientation]  
  126.   zCoord = zCoord + zDif[orientation]
  127.  
  128.   while turtle.detect() do
  129.     turtle.dig()
  130.   end
  131.  
  132.   moved = false
  133.   while not(moved) do
  134.     moved = turtle.forward()
  135.   end
  136. end
  137.  
  138. function haut()
  139.   yCoord = yCoord + 1
  140.  
  141.   turtle.digUp()
  142.  
  143.   moved = false
  144.   while not(moved) do
  145.     moved = turtle.up()
  146.   end
  147. end
  148.  
  149. function bas()
  150.   yCoord = yCoord - 1
  151.  
  152.   turtle.digDown()
  153.  
  154.   moved = false
  155.   while not(moved) do
  156.     moved = turtle.down()
  157.   end
  158.  
  159.   if yMin > yCoord then
  160.     yMin = yCoord
  161.   end
  162. end
  163.  
  164. function look(direction)
  165.   while direction ~= orientations[orientation] do
  166.     droite()
  167.   end      
  168. end
  169.  
  170. function digLine()
  171.   for i = 1,longeurLigne do
  172.     if InventairePlein() then
  173.       returnItems()
  174.     end
  175.     avance()
  176.   end
  177. end
  178.  
  179. function digLayer()
  180.   for i = 1, lignes do
  181.     digLine()
  182.     if (i%2) == 1 and i < lignes then
  183.       droite()
  184.       avance()
  185.       droite()
  186.     elseif i < lignes then
  187.       gauche()
  188.       avance()
  189.       gauche()
  190.     end
  191.   end
  192.   goto(xQuary, zQuary, yCoord)
  193.   look(3)
  194.   bas()
  195. end
  196.  
  197. function digQuary(xTarget, zTarget, yTarget)
  198. --  xQuary = xTarget
  199. --  zQuary = zTarget
  200. --  yQuary = yTarget
  201.  
  202.   nbDepl()
  203.   fuelCheckStart()
  204.  
  205.   goto(xQuary, zQuary, yTravel)
  206.   goto(xQuary, zQuary, yQuary)
  207.   look(3)
  208.   while yMin > yMinEnd do
  209.     digLayer()
  210.   end
  211.   goto(xQuary, zQuary, yQuary)
  212.   goto(xHome, zHome, yTravel)
  213.   goto(xHome, zHome, yHome)
  214.   yMin = 999
  215. end
  216.  
  217. function nbDepl()
  218.   if yQuary < yCoord then
  219.     yDepl = yTravel - yTarget
  220.   else
  221.     yDepl = yQuary - yCoord
  222.   end
  223.   if xQuary < xCoord then
  224.     xDepl = xCoord - xQuary
  225.   else
  226.     xDepl = xQuary - xCoord
  227.   end
  228.   if zQuary < zCoord then
  229.     zDepl = zCoord - zQuary
  230.   else
  231.     zDepl = zQuary - zCoord
  232.   end
  233.   Depl = 2 * (yDepl + xDepl + zDepl)
  234. --  print("nombre de deplacement = "..Depl)
  235. end
  236.  
  237. function goto(xTarget, zTarget, yTarget)
  238. -- déplacement  
  239.   while yTarget < yCoord do
  240.     bas()
  241.   end
  242.   while yTarget > yCoord do
  243.     haut()
  244.   end
  245.  
  246.   if xTarget < xCoord then
  247.     look("Ouest")
  248.     while xTarget < xCoord do
  249.       avance()
  250.     end
  251.   end
  252.   if xTarget > xCoord then
  253.     look("Est")
  254.     while xTarget > xCoord do
  255.       avance()
  256.     end
  257.   end
  258.  
  259.   if zTarget < zCoord then
  260.     look("Nord")
  261.     while zTarget < zCoord do
  262.       avance()
  263.     end
  264.   end
  265.   if zTarget > zCoord then
  266.     look("Sud")
  267.     while zTarget > zCoord do
  268.       avance()
  269.     end
  270.   end
  271. end
  272.  
  273. function InventairePlein()
  274.   turtle.select(16)
  275.   full = turtle.getItemCount(16) > 0
  276.   turtle.select(1)
  277.   return full
  278. end
  279.  
  280. function returnItems()
  281.   xProgress = xCoord
  282.   zProgress = zCoord
  283.   yProgress = yCoord
  284.   oProgress = orientation
  285.   print("Progress: X="..xProgress.." Y="..yProgress.." Z="..zProgress.." o="..oProgress)
  286.   print("@ Home: "..xHome.." "..zHome)
  287.   goto(xHome, zHome, yTravel)
  288.   goto(xHome, zHome, yHome)
  289.   look(2)
  290.   for i = 1,16 do
  291.     turtle.select(i)
  292.     turtle.drop()
  293.   end
  294.   turtle.select(1)
  295.  
  296.   fuelCheck()
  297.  
  298.   goto(xProgress,zProgress,yTravel)
  299.   goto(xProgress,zProgress,yProgress)
  300.   look(orientations[oProgress])
  301. end
  302.  
  303.  
  304. function getJob()
  305. --  while JobDispo do
  306. --    print("Requete d'un job...")
  307. --    rednet.send(manager, "getJob")
  308. --    id, message, dis = rednet.receive()
  309. --    
  310. --    if message == "Oui" then
  311. --      id, xRec, dis = rednet.receive()
  312. --      id, zRec, dis = rednet.receive()
  313. --      id, yRec, dis = rednet.receive()
  314. --      
  315. --    xQuary = tonumber(xRec)
  316. --    zQuary = tonumber(zRec)
  317. --    yQuary = tonumber(yRec)
  318. --     
  319.  
  320.   print("Coordonnée départ : X=")
  321.   xCoord = tonumber(read())
  322.   print("Coordonnée départ : Y=")
  323.   yCoord = tonumber(read())
  324.   print("Coordonnée départ : Z=")
  325.   zCoord = tonumber(read())
  326.  
  327.   print("Orientation : O[1=N; 2=E; 3=S; 4=O]=")
  328.   orientation = tonumber(read())
  329.  
  330.   xHome = xCoord
  331.   zHome = zCoord
  332.   yHome = yCoord
  333.  
  334.   print("Coordonnée quary : X=")
  335.   xQuary = tonumber(read())
  336.   print("Coordonnée dquary : Y=")
  337.   yQuary = tonumber(read())
  338.   print("Coordonnée quary : Z=")
  339.   zQuary = tonumber(read())
  340.  
  341.   -- print("Ejection des materiaux : O[1=N; 2=E; 3=S; 4=O]=")
  342.   -- ori2 = tonumber(read())
  343.  
  344.  
  345. --  print("Job dispo : "..xQuary.." "..zQuary)
  346.  
  347.   print("Zone: dimensions=")
  348.   longeurLigne = tonumber(read())
  349.  
  350.   lignes = longeurLigne
  351.  
  352.   print("yTravel=")
  353.   yTravel = tonumber(read())
  354.  
  355.   print("yMin=")
  356.   yMin = tonumber(read())
  357.  
  358.   print("yMinEnd=")
  359.   yMinEnd = tonumber(read())
  360.  
  361.   nbDeplQuary()
  362.          
  363.   digQuary(xQuary, zQuary, yQuary)        
  364.      
  365.   print("Job termine")
  366. --  elseif message == "Non" then
  367. --      print("Pas de job dispo... en attente nouvelle mission.")
  368. --      JobDispo = false
  369. --    end        
  370. --  end
  371. end
  372.  
  373. function nbDeplQuary()
  374.   DeplQuary = tonumber(longeurLigne) * tonumber(lignes) * (tonumber(yQuary) - tonumber(yMinEnd)) + (tonumber(yQuary) - tonumber(yMinEnd))
  375.   -- y compris ajouts déplacement du yQuary au yMinEnd
  376. end
  377.  
  378. function nbDeplacement()
  379. --FONCTION A RACCOURCIR... nbDeplacement (xDest yDest zDest xStart yStart zStart)
  380. --  if yDest < yStart then
  381. --    yDepl = yTravel - yDest
  382. --  else
  383. --    yDepl = yDest - yStart
  384. --  end
  385.   if yProgress < yCoord then
  386.     yDeplacement = yTravel - yProgress
  387.   else
  388.     yDeplacement = yProgress - yCoord
  389.   end
  390.   if xProgress < xCoord then
  391.     xDeplacement = xCoord - xProgress
  392.   else
  393.     xDeplacement = xProgress - xCoord
  394.   end
  395.   if zProgress < zCoord then
  396.     zDeplacement = zCoord - zProgress
  397.   else
  398.     zDeplacement = zProgress - zCoord
  399.   end
  400.   Deplacement = 2 * (yDeplacement + xDeplacement + zDeplacement) -- 2x car aller et retour
  401. --  print("nombre de deplacement = "..Deplacement)
  402. end
  403.  
  404. function move()
  405.   print("Position actuelle : X="..xCoord.." Z="..zCoord.." Y="..yCoord.." Orientation="..orientations[orientation])
  406.   xHome = xCoord
  407.   zHome = zCoord
  408.   yHome = yCoord
  409.  
  410.   print("Destination ?")
  411.   print("X :")
  412.   x = tonumber(read ())
  413.   print("Y :")
  414.   y = tonumber(read ())
  415.   print("Z :")
  416.   z = tonumber(read())
  417.  
  418.   print("@ "..x.." "..z.." "..y)
  419.   goto(x,z,y)
  420. --move()
  421. end
  422.  
  423. function fuelCheckStart()
  424.   TotalDepl = DeplQuary + Depl
  425.   FuelActuel = turtle.getFuelLevel()
  426.   FuelNeed = FuelActuel - TotalDepl
  427.  
  428.   while TotalDepl - turtle.getFuelLevel() >= 0 do
  429.     manque = TotalDepl - turtle.getFuelLevel()
  430.     manqueCoal = math.ceil(manque/80) -- 1 coal = 80??? 96 deplacements; 1 lava cell = 1200 déplacements
  431.     print("Veuillez placer "..(manqueCoal).." et validez avec ENTER")
  432.     print("TotalDepl="..TotalDepl.." FuelActuel="..FuelActuel)
  433.     read()
  434.     if turtle.getItemCount(1) > 0 then
  435.       if manqueCoal > 64 then
  436.         print("le fuel necessaire etant > 64 utiliser egalement le slot 2")
  437.         turtle.select(1)
  438.         turtle.refuel(64)
  439.         turtle.select(2)
  440.         reste = manqueCoal - 64
  441.         turtle.refuel(reste)
  442.       else
  443.       turtle.refuel(manqueCoal)
  444.       end
  445.     end
  446.   end
  447. end
  448.  
  449. function fuelCheck()
  450.   nbDeplacement ()
  451.   manqueCoal = math.ceil(Deplacement/80) -- 1 coal = 80?? 96 deplacements; 1 lava cell = 1200 déplacements
  452.   print("Veuillez placer "..(manqueCoal).." et validez avec ENTER")
  453.     read()
  454.     if turtle.getItemCount(1) >= manqueCoal then
  455.       turtle.refuel(manqueCoal)
  456.     end
  457. end
  458.  
  459.  
  460. --[[ PROGRAMME ]]--
  461.  
  462. getJob()
Advertisement
Add Comment
Please, Sign In to add comment