Advertisement
AdslHouba

Big Reactor 3/3 l'écran de controle

Mar 20th, 2015
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.58 KB | None | 0 0
  1. -- Video : https://youtu.be/mG_4e6uEOy0
  2.  
  3. -- Une ligne par machine {Nom="NOM AFFICHER",Id=XX}
  4. -- XX remplacer par le numero unique choisi sur les autre ordi à id
  5. -- oublier pas la virgule sauf sur la dernier ligne
  6. local config={
  7.   {Nom="Reactor",Id=100},
  8.   {Nom="Turbine 1",Id=0},
  9.   {Nom="Turbine 2",Id=1},
  10.   {Nom="Turbine 3",Id=2}
  11. }
  12. local modem=peripheral.wrap("bottom")
  13. local ecran=peripheral.wrap("top")
  14. local data={}
  15.  
  16. -- Initialisation
  17. for i=1,#config do
  18.   -- Creation de data de base
  19.   data[config[i].Id]="non"
  20.   -- Calcul de la posion a l'ecran
  21.   if i%2==1 then
  22.     config[i].X=1
  23.   else
  24.     config[i].X=21
  25.   end
  26.   config[i].Y=1+((math.ceil(i/2)-1)*6)
  27.   config[i].w1=window.create(ecran, config[i].X, config[i].Y, 20, 2)
  28.   config[i].w1.setTextColor(colors.white)
  29.   config[i].w3=window.create(ecran, config[i].X, config[i].Y+2, 20, 2)
  30.   config[i].w3.setTextColor(colors.white)
  31.   config[i].w4=window.create(ecran, config[i].X, config[i].Y+3, 20, 1)
  32.   config[i].w4.setTextColor(colors.white)
  33.   config[i].w5=window.create(ecran, config[i].X, config[i].Y+4, 20, 1)
  34.   config[i].w5.setTextColor(colors.white)
  35.   config[i].w6=window.create(ecran, config[i].X, config[i].Y+5, 20, 1)
  36.   config[i].w6.setBackgroundColor(colors.black)
  37.   -- ouverture pour ecouter les info
  38.   modem.open(config[i].Id)
  39. end
  40.  
  41. -- function pour transformer 9876543 en 9 876 543
  42. function number_format(n)
  43.   local test=mille(n)
  44.   if test.Mille==0 then
  45.     return math.floor(n)
  46.   else
  47.     test.Mille=mille(test.Mille)
  48.     if test.Mille.Mille==0 then
  49.       return test.Mille.Cent.." "..cent(test.Cent)
  50.     else
  51.       return test.Mille.Mille.." "..cent(test.Mille.Cent).." "..cent(test.Cent)
  52.     end
  53.   end
  54. end
  55. -- function pour etre sur d'avoir trois chiffre (99 devient 099, 9 devient 009)
  56. function cent(n)
  57.   n=math.floor(n)
  58.   if n>99 then
  59.     return n
  60.    elseif n>9 then
  61.     return "0"..n
  62.    else
  63.     return "00"..n
  64.    end
  65. end
  66. -- creer un objet avec les centaines d'un coté les millier de l'autre
  67. function mille(n)
  68.   if n<1000 then
  69.     return {Mille=0,Cent=n}
  70.   else
  71.     return {Mille=math.floor(n/1000),Cent=n%1000}
  72.   end
  73. end
  74.  
  75. -- function de recuperation des infos envoye en wifi
  76. function ecoute()
  77.   while true do
  78.     local e, ms, sc, rp, msg, sd = os.pullEvent("modem_message")
  79.     data[sc]=textutils.unserialize(msg)  
  80.     --print(msg)
  81.   end
  82. end
  83.  
  84. local couleurFond
  85. -- affichage de l ecran
  86. function affiche()
  87.   ecran.setBackgroundColor(colors.black)
  88.   while true do    
  89.     ecran.clear()
  90.     -- boucle sur chaque machine enregistre
  91.     for i=1,#config do
  92.       -- secu si rien recu
  93.       if data[config[i].Id]=="non" then
  94.     couleurFond=colors.red
  95.       else
  96.         -- on change la couleur si actif
  97.         if data[config[i].Id].active then    
  98.            couleurFond=colors.green
  99.         else
  100.            couleurFond=colors.red
  101.         end
  102.    
  103.     if data[config[i].Id].type=="turbine" then
  104.           --config[i].w4.setBackgroundColor(couleurFond)
  105.           --config[i].w5.setBackgroundColor(couleurFond)
  106.         else
  107.       --config[i].w3.setBackgroundColor(couleurFond)
  108.           --config[i].w5.setBackgroundColor(couleurFond)
  109.         end  
  110.       end    
  111.       config[i].w1.setBackgroundColor(couleurFond)
  112.       --config[i].w6.setBackgroundColor(couleurFond)  
  113.       --config[i].w6.setTextColor(couleurFond)
  114.  
  115.       config[i].w1.clear()
  116.       config[i].w1.setCursorPos(1,1)
  117.       config[i].w1.write(" "..config[i].Nom)
  118.      
  119.       config[i].w1.setCursorPos(1,2)
  120.       -- secu si rien recu
  121.       if data[config[i].Id]=="non" then        
  122.         config[i].w1.write(" Aucune info")
  123.         --config[i].w3.setBackgroundColor(couleurFond)
  124.         --config[i].w3.write(" ")
  125.         --config[i].w4.setBackgroundColor(couleurFond)
  126.         --config[i].w4.write(" ")
  127.         --config[i].w5.setBackgroundColor(couleurFond)
  128.         --config[i].w5.write(" ")
  129.       else
  130.         -- affiche active ou non
  131.         if data[config[i].Id].active then
  132.           config[i].w1.write(" Active")
  133.         else
  134.           config[i].w1.write(" Desactive")
  135.         end
  136.         config[i].w3.clear()
  137.         config[i].w3.setCursorPos(1,1)
  138.        
  139.         -- affichage en fonction du type de machine (turbine ou reactor)
  140.         if data[config[i].Id].type=="turbine" then
  141.      
  142.           -- couleur en fonction de la vitesse
  143.           -- bleu optimum
  144.           -- rouge non
  145.       if data[config[i].Id].speed<800 then
  146.         config[i].w3.setBackgroundColor(colors.red)
  147.       elseif data[config[i].Id].speed<1000 then
  148.         config[i].w3.setBackgroundColor(colors.blue)
  149.       elseif data[config[i].Id].speed<1700 then
  150.         config[i].w3.setBackgroundColor(colors.red)
  151.       elseif data[config[i].Id].speed<1900 then
  152.         config[i].w3.setBackgroundColor(colors.blue)
  153.       else
  154.         config[i].w3.setBackgroundColor(colors.red)
  155.       end
  156.           config[i].w3.write(" Rotor  : ")
  157.           -- Affichage des autres infos
  158.       config[i].w3.write(number_format(data[config[i].Id].speed))
  159.           config[i].w4.clear()
  160.       config[i].w4.setCursorPos(1,1)
  161.       config[i].w4.write(" Prod   : "..number_format(data[config[i].Id].prod))
  162.           config[i].w5.clear()
  163.       config[i].w5.setCursorPos(1,1)
  164.       config[i].w5.write(" Buffer : "..number_format(data[config[i].Id].buff))
  165.           config[i].w6.clear()
  166.     else
  167.       config[i].w3.write(" Fuel  : "..number_format(data[config[i].Id].fuel/1000).." yell")
  168.           config[i].w4.clear()
  169.       config[i].w4.setCursorPos(1,1)
  170.      
  171.           -- couleur si pas 100% fuel
  172.       local pourc=math.ceil(data[config[i].Id].fuel/data[config[i].Id].fuelMax*100)
  173.           if pourc<100 then
  174.          config[i].w4.setBackgroundColor(colors.red)
  175.       else
  176.          config[i].w4.setBackgroundColor(colors.blue)
  177.       end
  178.       config[i].w4.write(" Stock   : ")
  179.       config[i].w4.write(pourc.."%")
  180.           config[i].w5.clear()
  181.       config[i].w5.setCursorPos(1,1)
  182.       config[i].w5.write(" Prod : "..number_format(data[config[i].Id].prod))
  183.     end
  184.        
  185.        
  186.       end
  187.       config[i].w6.write("XXXXXXXXXXXXXXXXXXXXXX")
  188.     end
  189.     os.sleep(1)
  190.   end
  191. end
  192. -- ecoute du click
  193. function click()
  194.   while true do
  195.     local e, bt, xp, yp = os.pullEvent("monitor_touch")
  196.     local sortie=-1
  197.     -- calcul en fonction x/y de la clique de la machine
  198.     local i=((math.ceil(yp/6)*2)-1)+(math.ceil(xp/20)-1)
  199.     -- on verifie si y a une machine
  200.     if config[i] then
  201.      sortie=50+config[i].Id
  202.     else
  203.       print("pas de config "..i)
  204.     end  
  205.     -- si trouve on envoi l'ordre
  206.     if sortie==-1 then
  207.     else
  208.       print(sortie)
  209.       modem.transmit(sortie,sortie-50,"c")
  210.     end
  211.   end
  212. end
  213.  
  214. parallel.waitForAll(ecoute,affiche,click)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement