Advertisement
Nightmare129

EcranDeControle

Feb 8th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.50 KB | None | 0 0
  1. --/////////////////////////////////////////////////
  2. --//--------- PROGRMAME COMPUTEUR CRAFT ---------//
  3. --//--------- ************************* ---------//
  4. --//----- AFFICHAGE DYNAMIQUE ET INTERACTIF -----//
  5. --//---- DE TUBINE ET REATEUR DE BIGREACTOR -----//
  6. --/////////////////////////////////////////////////
  7.  
  8. --Confguration
  9. rednet.open("left")
  10. local ecran=peripheral.wrap("top")
  11. local idTurbine = "MasterEnergie"
  12.  
  13. --//////////////////////////////////////////////////
  14. --//------------ Variables Globale ---------------//
  15. --//////////////////////////////////////////////////
  16. local nbm = 8 --Nombre de turbine afficher
  17. local timeOut = 5 --temps maximal entre deux trames envoyees par un ordi
  18. local k=0
  19. local data={}
  20. local data100={}
  21.  
  22. --/////////////////////////////////////////////////
  23. --//------------------ Main ---------------------//
  24. --/////////////////////////////////////////////////
  25.  
  26. -- Fonction de recuperation des informations envoyees en wifi
  27. function Ecoute()
  28.     --Init de data[*]["time"] a 0
  29.     for i=1,nbm do
  30.         data[i]={}
  31.         data[i]["time"] = 0
  32.     end
  33.     data[100]={}
  34.     data100["time"] = 0
  35.        
  36.     while true do
  37.         local event, id, ID, TablData = os.pullEvent("rednet_message")     
  38.         if ID>=1 and  ID<=8 then
  39.             data[ID]=textutils.unserialize(TablData)
  40.             data[ID]["time"]=os.clock()
  41.         elseif ID==100 then
  42.             data100=textutils.unserialize(TablData)
  43.             data100["time"]=os.clock()
  44.         end
  45.     end
  46. end
  47.  
  48. -- Fonction du main d'affichage
  49. function Affichage()
  50.     local i
  51.     Init()
  52.     while true do --main
  53.         k=k+1
  54.         AffichageTurbine()
  55.         AffichageReactor()
  56.         DetectionPCoff()
  57.         os.sleep(0.8)
  58.     end
  59. end
  60.  
  61. -- Tout est dans le nom
  62. function Tactille()
  63.     local Mdata={}  
  64.    
  65.     while true do
  66.         local e, bt, yp, xp = os.pullEvent("monitor_touch")
  67.        
  68.         --Pour les turbines
  69.         local i=1
  70.         for i=1,nbm do
  71.             --Choix des positions
  72.             local x=1 y=1
  73.             if     i==1 then x=1  y=3
  74.             elseif i==2 then x=1  y=52
  75.             elseif i==3 then x=11 y=3
  76.             elseif i==4 then x=11 y=52
  77.             elseif i==5 then x=21 y=3
  78.             elseif i==6 then x=21 y=52
  79.             elseif i==7 then x=31 y=3
  80.             elseif i==8 then x=31 y=52
  81.             end
  82.            
  83.             --Activee/Desactivee
  84.             if yp>=y+6 and yp<=y+11 and xp>=x+2 and xp<=x+5 then
  85.                 Mdata["numberTurbine"] = i
  86.                 Mdata["sInductor"] = data[i]["gInductor"]
  87.                 if data[i]["gActive"] == true then
  88.                     Mdata["sActive"]=false
  89.                 else
  90.                     Mdata["sActive"]=true
  91.                  end
  92.             end
  93.            
  94.             --Engagement/Desengagement
  95.             if xp==x+1 and yp>=y+6 and yp<=y+12  then
  96.                 Mdata["numberTurbine"] = i
  97.                 Mdata["sActive"] = data[i]["gActive"]
  98.                 if data[i]["gInductor"] == true then
  99.                     Mdata["sInductor"]=false
  100.                 else
  101.                     Mdata["sInductor"]=true
  102.                 end
  103.             end
  104.         end
  105.        
  106.         --Pour le reacteur
  107.         --Activee/Desactivee
  108.         if yp>=31 and yp<=40  and xp>=10 and xp<=28 then
  109.             Mdata["numberTurbine"] = 100
  110.             if data100["gActive"] == true then
  111.                 Mdata["sActive"]=false
  112.             else
  113.                 Mdata["sActive"]=true
  114.             end
  115.         end
  116.         rednet.broadcast(idTurbine,textutils.serialize(Mdata))
  117.         os.sleep(0.5)
  118.     end
  119. end
  120.  
  121. --/////////////////////////////////////////////////
  122. --//---- Les fonctions dediees a Affichage() ----//q
  123. --/////////////////////////////////////////////////
  124.  
  125. -- Fonction d'initialisation
  126. function Init()
  127.     local cpt=1
  128.     windows = {} --pour les images (paint)
  129.     windows[1] = window.create(ecran, 3 , 2 , 18, 8, true)--Tubines
  130.     windows[2] = window.create(ecran, 52, 2 , 18, 8, true)
  131.     windows[3] = window.create(ecran, 3 , 12, 18, 8, true)
  132.     windows[4] = window.create(ecran, 52, 12, 18, 8, true)
  133.     windows[5] = window.create(ecran, 3 , 22, 18, 8, true)
  134.     windows[6] = window.create(ecran, 52, 22, 18, 8, true)
  135.     windows[7] = window.create(ecran, 3 , 32, 18, 8, true)
  136.     windows[8] = window.create(ecran, 52, 32, 18, 8, true)
  137.     windows[100] = window.create(ecran, 25, 10, 21, 35, true)--Reacteur
  138.     windows[101] = window.create(ecran, 23, 2, 26, 6, true)--Titre
  139.    
  140.     --Chargement
  141.     ecran.setBackgroundColor(colors.blue)
  142.     ecran.clear()
  143.     line(10 ,20,50 ,4, colors.black)
  144.     for cpt=1, 50 do
  145.         line(10 ,20 ,cpt,4, colors.green)
  146.         os.sleep(0.05)
  147.     end
  148.     os.sleep(0.5)
  149.     ecran.setBackgroundColor(colors.black)
  150.     ecran.clear()
  151.    
  152.     Titre = paintutils.loadImage("Titre")
  153.     windows[101].setBackgroundColor(colors.black)
  154.     windows[101].setTextColor(colors.black)
  155.     windows[101].clear()
  156.     term.redirect(windows[101])
  157.     paintutils.drawImage(Titre, 1, 1)
  158. end
  159.  
  160. -- Fonction d'affichage des donnees des turbines
  161. function AffichageTurbine()
  162.     local turb --l'image charger
  163.     local i=1
  164.    
  165.     if k == 5 then k = 1 end --pour les images
  166.    
  167.     for i=1,nbm do --data[1] a data[nbm]       
  168.         --choix de la position
  169.         local x=1 y=1
  170.         if     i==1 then x=1  y=3
  171.         elseif i==2 then x=1  y=52
  172.         elseif i==3 then x=11 y=3
  173.         elseif i==4 then x=11 y=52
  174.         elseif i==5 then x=21 y=3
  175.         elseif i==6 then x=21 y=52
  176.         elseif i==7 then x=31 y=3
  177.         elseif i==8 then x=31 y=52
  178.         end
  179.  
  180.         --Debut de l'affichage
  181.         ecran.setBackgroundColor(colors.black)
  182.         ecran.setTextColor(colors.white)
  183.         ecran.setCursorPos(y+4,x)   --**--
  184.         ecran.write("Turbine "..i)
  185.        
  186.         if data[i]["PCon"] == true then
  187.             --L'activite
  188.             if data[i]["gActive"] == true then
  189.                 turb = paintutils.loadImage("turb"..k)
  190.             else
  191.                 turb = paintutils.loadImage("turb0")
  192.             end
  193.             windows[i].setBackgroundColor(colors.black)
  194.             windows[i].setTextColor(colors.black)
  195.             windows[i].clear()
  196.             term.redirect(windows[i])
  197.             paintutils.drawImage(turb, 7, 2)
  198.  
  199.             --L'engagement
  200.             if data[i]["gInductor"]==true then
  201.                 ecran.setCursorPos(y+4,x+1)  
  202.                 ecran.setBackgroundColor(colors.black)
  203.                 ecran.write(" ")
  204.                 line(y+5 ,x+1 ,7 ,1, colors.blue)
  205.                 ecran.setCursorPos(y+5,x+1)
  206.                 ecran.write("Engager")
  207.                 ecran.setBackgroundColor(colors.black)
  208.                 ecran.write("   ")
  209.             else
  210.                 line(y+4 ,x+1 ,7 ,1, colors.red)
  211.                 ecran.setCursorPos(y+4,x+1)
  212.                 ecran.write("Desengager")
  213.                 ecran.setBackgroundColor(colors.black)
  214.             end
  215.  
  216.             ecran.setTextColor(colors.white)
  217.             ecran.setCursorPos(y+1,x+7)
  218.             ecran.write("Rotor: ")
  219.    
  220.             if data[i]["gSpeed"]<800 then
  221.                 ecran.setTextColor(colors.red)
  222.             elseif data[i]["gSpeed"]<1000 then
  223.                 ecran.setTextColor(colors.blue)
  224.             elseif data[i]["gSpeed"]<1700 then
  225.                 ecran.setTextColor(colors.red)
  226.             elseif data[i]["gSpeed"]<1900 then
  227.                 ecran.setTextColor(colors.blue)  
  228.             else
  229.                 ecran.setTextColor(colors.red)
  230.             end
  231.  
  232.             ecran.setCursorPos(y+8,x+7)
  233.             ecran.write(number_format(data[i]["gSpeed"]).." RPM     ")
  234.             ecran.setTextColor(colors.white)
  235.    
  236.             ecran.setCursorPos(y,x+8)
  237.             ecran.write("Prod : "..number_format(data[i]["gProductLastTick"]).." RF/t     ")
  238.            
  239.         else
  240.             turb = paintutils.loadImage("PCoff")
  241.             windows[i].setBackgroundColor(colors.black)
  242.             windows[i].setTextColor(colors.black)
  243.             windows[i].clear()
  244.             term.redirect(windows[i])
  245.             paintutils.drawImage(turb, 5, 1)
  246.         end
  247.     end
  248. end
  249.  
  250. --Fonction d'affichage des donnees du reacteur
  251. function AffichageReactor()
  252.     local reactor --L'image charger
  253.     local y=25 x=9
  254.    
  255.     --Affichage de l'activite
  256.     ecran.setBackgroundColor(colors.black)
  257.     ecran.setTextColor(colors.white)
  258.     ecran.setCursorPos(y+7,x)
  259.     ecran.write("Reactor")
  260.  
  261.     if data100["PCon"] == true then
  262.  
  263.         if data100["gActive"]==true then
  264.             reactor = paintutils.loadImage("reactor")
  265.         else
  266.             reactor = paintutils.loadImage("reactoroff")
  267.         end
  268.         windows[100].setBackgroundColor(colors.black)
  269.         windows[100].setTextColor(colors.black)
  270.         windows[100].clear()
  271.         term.redirect(windows[100])
  272.         paintutils.drawImage(reactor, 7, 1)
  273.         ecran.setBackgroundColor(colors.black)
  274.        
  275.         --Affichage des donnees
  276.         ecran.setTextColor(colors.white)
  277.         ecran.setCursorPos(y,x+20) --le stock de yell en ingot
  278.         ecran.write("Fuel: "..number_format(data100["gFuelAmount"]).." yell  ")
  279.         ecran.setCursorPos(y+1,x+21)
  280.         ecran.write("Decher: "..number_format(data100["gWasteAmount"]).." cyanit  ")
  281.         ecran.setCursorPos(y+5,x+22) --le stock de yell en %
  282.         ecran.write("Stock: "..math.ceil(data100["gFuelAmount"]/data100["gFuelAmountMax"]*100).."%")
  283.         ecran.setCursorPos(y+1,x+23) --production en mb/t
  284.         ecran.write("Prod: "..number_format(data100["gHotFluidProducedLastTick"]).." mb/t   ")
  285.         ecran.setCursorPos(y,x+24) --consomation de yell en mb
  286.         ecran.write("Conso: "..number_format(data100["gFuelConsumedLastTick"]))
  287.         ecran.setCursorPos(y+9,x+24)
  288.         ecran.write("mb/t de yell")
  289.         ecran.setCursorPos(y+4,x+25) --temperature en C
  290.         ecran.write("Temp: "..number_format(data100["gCasingtemperature"]).." C  ")
  291.         ecran.setCursorPos(y+4,x+26) --radiations %
  292.         ecran.write("Reacto: "..number_format(data100["gFuelReactivity"]).." %  ")
  293.     else
  294.         reactor = paintutils.loadImage("PCroff")
  295.         windows[100].setBackgroundColor(colors.black)
  296.         windows[100].setTextColor(colors.black)
  297.         windows[100].clear()
  298.         term.redirect(windows[100])
  299.         paintutils.drawImage(reactor, 7, 1)
  300.     end
  301. end
  302.  
  303. --Detection d'un PC eteint
  304. function DetectionPCoff()
  305.     local heure = os.clock()
  306.     for i=1,nbm do
  307.         if((heure - data[i]["time"]) > timeOut) then
  308.             data[i]["PCon"] = false
  309.         end
  310.     end
  311.         if((heure - data100["time"]) > timeOut) then
  312.         data100["PCon"] = false
  313.     end
  314. end
  315.  
  316. --/////////////////////////////////////////////////
  317. --//---------- Les fonctions divers -------------//
  318. --/////////////////////////////////////////////////
  319.  
  320. -- Gestion de rectangle de couleur
  321. function line(x ,y ,long ,larg, color_font)
  322.     ecran.setBackgroundColor(color_font)
  323.     for yp=y, y+larg-1 do
  324.         ecran.setCursorPos(x,yp)
  325.         ecran.write(string.rep(" ", long))
  326.     end
  327. end
  328.  
  329. -- Bare de progression
  330. function prog(x ,y ,long ,larg, valmin, valmax ,color_bar ,color_font)
  331.     line(x ,y ,long ,larg, color_font)
  332.     local bar = math.floor((valmin/valmax)*long)
  333.     line(x ,y ,bar ,larg, color_bar)
  334. end
  335.  
  336. -- Fonction pour transformer 9876543 en 9 876 543
  337. function number_format(n)
  338.   local test=mille(n)
  339.   if test.Mille==0 then
  340.     return math.floor(n)
  341.   else
  342.     test.Mille=mille(test.Mille)
  343.     if test.Mille.Mille==0 then
  344.       return test.Mille.Cent.." "..cent(test.Cent)
  345.     else
  346.       return test.Mille.Mille.." "..cent(test.Mille.Cent).." "..cent(test.Cent)
  347.     end
  348.   end
  349. end
  350.  
  351. -- Fonction pour etre sur d'avoir trois chiffres (99 devient 099, 9 devient 009)
  352. function cent(n)
  353.   n=math.floor(n)
  354.   if n>99 then
  355.     return n
  356.    elseif n>9 then
  357.     return "0"..n
  358.    else
  359.     return "00"..n
  360.    end
  361. end
  362.  
  363. -- Creer un objet avec les centaines d'un cotee les milliers de l'autre
  364. function mille(n)
  365.   if n<1000 then
  366.     return {Mille=0,Cent=n}
  367.   else
  368.     return {Mille=math.floor(n/1000),Cent=n%1000}
  369.   end
  370. end
  371.  
  372. -- Execute 3 fontions en paralleles (Main)
  373. parallel.waitForAll(Ecoute,Affichage,Tactille)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement