Advertisement
sshikamaru

Contrôle Diesel

Mar 31st, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.51 KB | None | 0 0
  1. --Requires
  2. local computer = require("computer")
  3. local component = require("component")
  4. local term = require("term")
  5. local math = require("math")
  6. local event = require("event")
  7. local string = require("string")
  8. local unicode = require("unicode")
  9. local colors = require("colors")
  10. local side = require("sides")
  11. local serialization = require("serialization")
  12.  
  13. --Récupération des composants
  14. local screen = component.getPrimary("screen")
  15. local gpu = component.gpu
  16. local rs1 = component.proxy(component.get("5C48","redstone"))
  17. local rs2 = component.proxy(component.get("6531","redstone"))
  18. gpu.setResolution(91,50)
  19. local modem = component.modem
  20.  
  21. --Variables
  22. local i = 0
  23. local tableau_general = {}
  24. local bit_diesel = {}
  25.  
  26. --Ouverture des ports
  27. modem.open(10001)
  28. modem.open(10003)
  29.  
  30. --Fonctions
  31. local function setColor(bg,fg)
  32.   gpu.setBackground(bg)
  33.   gpu.setForeground(fg)
  34. end
  35.  
  36. local function unitrf(qty)
  37.   if qty >= 1000000000000000 or qty <= -1000000000000000 then
  38.     return string.format("%.03f PRF/t",qty/1000000000000000)
  39.   elseif qty >= 1000000000000 or qty <= -1000000000000 then
  40.     return string.format("%.03f TRF/t",qty/1000000000000)
  41.   elseif qty >=1000000000 or qty <= -1000000000 then
  42.     return string.format("%.03f GRF/t",qty/1000000000)
  43.   elseif qty >=1000000 or qty <= -1000000 then
  44.     return string.format("%.03f MRF/t",qty/1000000)
  45.   elseif qty >=1000 or qty <= -1000 then
  46.     return string.format("%.03f kRF/t",qty/1000)
  47.   else
  48.     return string.format("%.03f RF/t ",qty)
  49.   end
  50. end
  51.  
  52. local function barre_verticale(x,y,hauteur,largeur,valeur,valmax)
  53.   local k = 0
  54.   local cur = math.floor((valeur / valmax) * hauteur)
  55.   local dir = string.rep(" ",largeur)
  56.   for k = 1, hauteur do
  57.     local vy = y + hauteur - k
  58.     if k <= cur then
  59.       setColor(0x00FF00,0x0000FF)
  60.     elseif k > cur then
  61.       setColor(0x000000,0xFFFFFF)  
  62.     end
  63.       gpu.set(x,vy,dir)
  64.     setColor(0x000000,0xFFFFFF)
  65.   end
  66. end
  67.  
  68. local function loadbar(x,y,longueur,valeur,text,bg,fg)
  69.   local raw = " " .. text ..string.rep(" ", longueur - unicode.len(text) - 2) .. " "
  70.   local oldbg = gpu.setBackground(bg)
  71.   local oldfg = gpu.setForeground(fg)
  72.   gpu.set(x,y,unicode.sub(raw,1,valeur))
  73.   gpu.setBackground(oldbg)
  74.   gpu.setForeground(oldfg)
  75.   gpu.set(x+valeur,y,unicode.sub(raw,valeur+1,longueur))
  76. end
  77.  
  78. local function affenergie(x,y,length,tableau)
  79.   local amount = tableau[1]
  80.   local capacity = tableau[2]
  81.   local pct = tableau[3]
  82.   local entree = tableau[4]
  83.   local sortie = tableau[5]
  84.   local delta = tableau[6]
  85.   local color = 0x00FFFF
  86.   local color2 = 0xFF0000
  87.   local cur = math.floor(pct * length)
  88.   local textfrac = string.format("%s / %s", unitrf(amount), unitrf(capacity))
  89.   local textpct = string.format("%.02f%%", pct*100)
  90.   local text1 = "        ENERGIE DANS LA BATTERIE MEKANISM " .. tableau[7]
  91.   local text2 = "I: " .. unitrf(entree) .. "  O: " .. unitrf(sortie) .. "  D: " .. unitrf(delta)
  92.   local text3 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  93.   loadbar(x,y,length,cur,text1,color,color2)
  94.   loadbar(x,y+1,length,cur,text2,color,color2)
  95.   loadbar(x,y+2,length,cur,text3,color,color2)
  96. end
  97.  
  98. local function bar_bank(x,y,length,val,valmax)
  99.   local pct = val/valmax
  100.   local color = 0xafa50d
  101.   local color2 = 0x0000FF
  102.   local cur = math.floor(pct * length)
  103.   local textfrac = unitrf(val) .. " / " .. unitrf(valmax)
  104.   local textpct = string.format("%.02f%%", pct*100)
  105.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  106.   loadbar(x,y,length,cur,text,color,color2)
  107. end
  108.  
  109. local function bar_fuel(x,y,length,val,valmax)
  110.   local pct = val/valmax
  111.   local color = 0x0aFF0a
  112.   local color2 = 0xFFFFFF - 0xafa50d
  113.   local cur = math.floor(pct * length)
  114.   local textfrac = string.format("Fuel : %s mB/ %s mB", val, valmax)
  115.   local textpct = string.format("%.02f%%", pct*100)
  116.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  117.   loadbar(x,y,length,cur,text,color,color2)
  118. end
  119.  
  120. local function onoff(x,y,etat)
  121.   if etat == true then
  122.     setColor(0x00FF00, 0x0)
  123.     gpu.set(x,y," ON ")
  124.   else
  125.     setColor(0xFF0000, 0x0)
  126.     gpu.set(x,y," OFF")    
  127.   end
  128.   setColor(0x0, 0xFFFFFF)
  129. end
  130.  
  131. local function connect(x,y,etat)
  132.   if etat == false then
  133.     setColor(0x0000FF, 0x00FFFF)
  134.     gpu.set(x,y,"Déconnecté")
  135.   else
  136.     setColor(0x00FFFF, 0x0000FF)
  137.     gpu.set(x,y," Connecté ")  
  138.   end
  139.   setColor(0x0, 0xFFFFFF)  
  140. end
  141.  
  142. local function mode(x,y,etat)
  143.   if etat == 1 then
  144.     setColor(0x0, 0xFF0000)
  145.     gpu.set(x,y,"MARCHE")
  146.   elseif etat == 2 then
  147.     setColor(0x0, 0xFFFA000)
  148.     gpu.set(x,y," AUTO ")
  149.   elseif etat == 3 then
  150.     setColor(0x0, 0xFF0000)
  151.     gpu.set(x,y," STOP ")  
  152.   end
  153.   setColor(0x0, 0xFFFFFF)  
  154. end
  155.  
  156. local function diesel(x,y,tableau)  
  157.   gpu.set(x+10,y,string.format("%02s",tableau[1]))
  158.   onoff(x+18,y,tableau[2])
  159.   connect(x+31,y,tableau[3])
  160.   gpu.set(x+16,y+1,string.format("%07.3s",tableau[4]))
  161.   mode(x+35,y+1,tableau[5])
  162.   bar_fuel(x,y+2,44,tableau[6],tableau[7])
  163.   bar_bank(x,y+3,44,tableau[8],tableau[9])
  164. end
  165.  
  166. local function smoke()
  167.   if bit_diesel[1] = true or bit_diesel[2] = true or bit_diesel[3] = true or bit_diesel[4] = true or  bit_diesel[9] = true or bit_diesel[10] = true or bit_diesel[11] = true or bit_diesel[12] = true then
  168.     rs1.setOutput(side.top,15)
  169.   else
  170.     rs1.setOutput(side.top,0)
  171.   end
  172.   if bit_diesel[5] = true or bit_diesel[6] = true or bit_diesel[7] = true or bit_diesel[8] = true or  bit_diesel[13] = true or bit_diesel[14] = true or bit_diesel[15] = true or bit_diesel[16] = true then
  173.     rs1.setOutput(side.top,15)
  174.   else
  175.     rs1.setOutput(side.top,0)
  176.   end
  177. end
  178.  
  179. --trame
  180.  gpu.set(1,1,"╔═════════════════════════════════════════════════════════════════════════════════════════╗")
  181.  gpu.set(1,2,"║                                                                                         ║")
  182.  gpu.set(1,3,"╠════════════════════════════════════════════╦════════════════════════════════════════════╣")
  183.  gpu.set(1,4,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  184.  gpu.set(1,5,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  185.  gpu.set(1,6,"║ fuel : 8000mB / 8000mB             100.00% ║ fuel : 8000mB / 8000mB             100.00% ║")
  186.  gpu.set(1,7,"║ XXX.XXX mrf/t / XXX.XXX mrf/t      100.00% ║                                            ║")
  187.  gpu.set(1,8,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  188.  gpu.set(1,9,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  189. gpu.set(1,10,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  190. gpu.set(1,11,"║                                            ║                                            ║")
  191. gpu.set(1,12,"║                                            ║                                            ║")
  192. gpu.set(1,13,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  193. gpu.set(1,14,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  194. gpu.set(1,15,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  195. gpu.set(1,16,"║                                            ║                                            ║")
  196. gpu.set(1,17,"║                                            ║                                            ║")
  197. gpu.set(1,18,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  198. gpu.set(1,19,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  199. gpu.set(1,20,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  200. gpu.set(1,21,"║                                            ║                                            ║")
  201. gpu.set(1,22,"║                                            ║                                            ║")
  202. gpu.set(1,23,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  203. gpu.set(1,24,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  204. gpu.set(1,25,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  205. gpu.set(1,26,"║                                            ║                                            ║")
  206. gpu.set(1,27,"║                                            ║                                            ║")
  207. gpu.set(1,28,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  208. gpu.set(1,29,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  209. gpu.set(1,30,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  210. gpu.set(1,31,"║                                            ║                                            ║")
  211. gpu.set(1,32,"║                                            ║                                            ║")
  212. gpu.set(1,33,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  213. gpu.set(1,34,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  214. gpu.set(1,35,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  215. gpu.set(1,36,"║                                            ║                                            ║")
  216. gpu.set(1,37,"║                                            ║                                            ║")
  217. gpu.set(1,38,"╠════════════════════════════════════════════╬════════════════════════════════════════════╣")
  218. gpu.set(1,39,"║ Diesel n°    --   XX   --     déconnecté   ║ Diesel n°    --   XX   --     déconnecté   ║")
  219. gpu.set(1,40,"║ Déclenchement 000.000% -- Mode :  marche   ║ Déclenchement 000.000% -- Mode :  marche   ║")
  220. gpu.set(1,41,"║                                            ║                                            ║")
  221. gpu.set(1,42,"║                                            ║                                            ║")
  222. gpu.set(1,43,"╠════════════════════════════════════════════╩════════════════════════════════════════════╣")
  223. gpu.set(1,44,"╠═════════════════════════════════════════════════════════════════════════════════════════╣")
  224. gpu.set(1,45,"║                                                                                         ║")
  225. gpu.set(1,46,"╠═════════════════════════════════════════════════════════════════════════════════════════╣")
  226. gpu.set(1,47,"║bargraph energie                                                                         ║")
  227. gpu.set(1,48,"║                                                                                         ║")
  228. gpu.set(1,49,"║                                                                                         ║")
  229. gpu.set(1,50,"╚═════════════════════════════════════════════════════════════════════════════════════════╝")
  230.  
  231. local function onModem(_,_,from,port,_,IDcodes,message)
  232.   local texte_vide = ""
  233.   i = i + 1
  234.   if i > 45 then
  235.     i = 0
  236.     texte_vide = "                                               "
  237.   end
  238.   local texte_port = " Réception n°" .. i+1 .. " / port n°".. port .. texte_vide
  239.   gpu.set(i+2,45,texte_port)
  240.   if port == 10026 then
  241.   gpu.set(1,4,tostring(port))
  242.   end
  243.   if port == 10001 then
  244.     value_receive = serialization.unserialize(message)
  245.     affenergie(2,47,89,value_receive)
  246.   elseif port == 10003 then
  247.     local vx
  248.     local vy
  249.     for vdiesel = 1, 16 do
  250.       ndiesel = "diesel " .. vdiesel
  251.       if IDcodes == ndiesel then
  252.         value_receive = serialization.unserialize(message)
  253.         tableau_general[vdiesel] = value_receive
  254.         bit_diesel[vdiesel] = tableau_general[2]
  255.         if (math.floor(vdiesel/2)*2 ~= vdiesel) then
  256.           vx = 2
  257.           vy = 4 + (vdiesel - 1) * 2.5
  258.         else
  259.           vx = 47
  260.           vy = 4 + (vdiesel - 2) * 2.5
  261.         end
  262.         diesel(vx,vy,tableau_general[vdiesel])
  263.       end
  264.     end
  265.   end
  266.   smoke()
  267. end
  268.  
  269. local function onTouch(event,adress,x,y,clic,pseudo)
  270.   local tclic
  271.   if clic == 0  then
  272.     tclic = "Clic gauche"
  273.   elseif clic == 1 then
  274.     tclic = "Clic droit"
  275.   else
  276.     tclic = "Clic inconnu"
  277.   end
  278.   gpu.set(2,2," "..tclic.." de la part de "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
  279.   if x==1 and y==1 then
  280.     computer.pushSignal("quit")
  281.     term.setCursor(1,1)
  282.     return false
  283.   end
  284. end
  285.  
  286. event.listen("touch",onTouch)
  287. event.listen("modem_message",onModem)
  288. event.pull("quit")
  289. event.ignore("touch",onTouch)
  290. event.ignore("modem_message",onModem)
  291. component.gpu.setResolution(160,50)
  292.  
  293. --Fermeture des ports
  294. modem.close(10001)
  295. modem.close(10003)
  296. term.clear()
  297.  
  298. --Créé par sshikamaru. Vous avez le droit de l'utiliser mais pas de le distribuer.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement