Advertisement
sshikamaru

Diesel unité

Mar 24th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.04 KB | None | 0 0
  1. local computer = require("computer")
  2. local component = require("component")
  3. local term = require("term")
  4. local math = require("math")
  5. local event = require("event")
  6. local string = require("string")
  7. local unicode = require("unicode")
  8. local colors = require("colors")
  9. local side = require("sides")
  10. local serialization = require("serialization")
  11.  
  12. --Récupération des composants
  13. local screen = component.getPrimary("screen")
  14. local gpu = component.gpu
  15. gpu.setResolution(63,29)
  16. local modem = component.modem
  17. local rs = component.redstone
  18. local diesel = component.ie_diesel_generator
  19. local bank = component.capacitor_bank
  20.  
  21. --Initialisation variables
  22. local arg = {...}
  23. local nb_diesel = tonumber(arg[1])
  24. local value_receive = {}
  25. local palier_mini = 50 --pourcent
  26. local palier_maxi = 80 --pourcent
  27. local tank_mini = 35
  28. local tank_maxi = 95
  29. local mode = 3
  30. local i = 0
  31. local j = 0
  32. local vconnect = false
  33. local mode_fuel = 3
  34. local palier_diesel = palier_mini+(nb_diesel * (palier_maxi - palier_mini) / 16)
  35.  
  36. --Ouverture des ports du modem
  37. modem.open(10001)
  38.  
  39. local function setColor(bg,fg)
  40.   gpu.setBackground(bg)
  41.   gpu.setForeground(fg)
  42. end
  43.  
  44. --Fonctions
  45. local function unitrf(qty)
  46.   if qty >= 1000000000000000 or qty <= -1000000000000000 then
  47.     return string.format("%.03f PRF/t",qty/1000000000000000)
  48.   elseif qty >= 1000000000000 or qty <= -1000000000000 then
  49.     return string.format("%.03f TRF/t",qty/1000000000000)
  50.   elseif qty >=1000000000 or qty <= -1000000000 then
  51.     return string.format("%.03f GRF/t",qty/1000000000)
  52.   elseif qty >=1000000 or qty <= -1000000 then
  53.     return string.format("%.03f MRF/t",qty/1000000)
  54.   elseif qty >=1000 or qty <= -1000 then
  55.     return string.format("%.03f kRF/t",qty/1000)
  56.   else
  57.     return string.format("%.03f RF/t ",qty)
  58.   end
  59. end
  60.  
  61. local function barre_verticale(x,y,hauteur,largeur,valeur,valmax)
  62.   local k = 0
  63.   local cur = math.floor((valeur / valmax) * hauteur)
  64.   local dir = string.rep(" ",largeur)
  65.   for k = 1, hauteur do
  66.     local vy = y + hauteur - k
  67.     if k <= cur then
  68.       setColor(0xafa50d,0xFFFFFF-0xafa50d)
  69.     elseif k > cur then
  70.       setColor(0x000000,0xFFFFFF)  
  71.     end
  72.       gpu.set(x,vy,dir)
  73.     if k == 1 then
  74.       gpu.set(x,vy,dir)
  75.       gpu.set(x,vy,string.format("Max %4s mB",valmax))
  76.     elseif k == 3 then
  77.       gpu.set(x,vy,dir)
  78.       gpu.set(x,vy,string.format("Cur %4s mB",valeur))
  79.     elseif k == 4 then
  80.       gpu.set(x,vy,dir)
  81.       gpu.set(x,vy,string.format("  %06.02f%%  ",(valeur/valmax)*100))
  82.     elseif k == 6 then
  83.       gpu.set(x,vy," Carburant ")
  84.     elseif k == 7 then
  85.       gpu.set(x,vy,"  Réserve  ")
  86.     end
  87.     setColor(0x000000,0xFFFFFF)
  88.   end
  89. end
  90.  
  91. local function loadbar(x,y,longueur,valeur,text,bg,fg)
  92.   local raw = " " .. text ..string.rep(" ", longueur - unicode.len(text) - 2) .. " "
  93.   local oldbg = gpu.setBackground(bg)
  94.   local oldfg = gpu.setForeground(fg)
  95.   gpu.set(x,y,unicode.sub(raw,1,valeur))
  96.   gpu.setBackground(oldbg)
  97.   gpu.setForeground(oldfg)
  98.   gpu.set(x+valeur,y,unicode.sub(raw,valeur+1,longueur))
  99. end
  100.  
  101. local function affenergie(x,y,length,tableau)
  102.   local amount = tableau[1]
  103.   local capacity = tableau[2]
  104.   local pct = tableau[3]
  105.   local entree = tableau[4]
  106.   local sortie = tableau[5]
  107.   local delta = tableau[6]
  108.   local color = 0x00FFFF
  109.   local color2 = 0xFF0000
  110.   local cur = math.floor(pct * length)
  111.   local textfrac = string.format("%s / %s", unitrf(amount), unitrf(capacity))
  112.   local textpct = string.format("%.02f%%", pct*100)
  113.   local text1 = "        ENERGIE DANS LA BATTERIE MEKANISM " .. tableau[7]
  114.   local text2 = "I: " .. unitrf(entree) .. "  O: " .. unitrf(sortie) .. "  D: " .. unitrf(delta)
  115.   local text3 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  116.   loadbar(x,y,length,cur,text1,color,color2)
  117.   loadbar(x,y+1,length,cur,text2,color,color2)
  118.   loadbar(x,y+2,length,cur,text3,color,color2)
  119. end
  120.  
  121. local function mode_fonctionnement(x,y)
  122.   if mode == 1 then
  123.     setColor(0x00FF00, 0x0)
  124.     gpu.set(x,y,"MARCHE")
  125.     setColor(0x0, 0xFFFA000)
  126.     gpu.set(x+8,y," AUTO ")
  127.     setColor(0x0, 0xFF0000)
  128.     gpu.set(x+15,y," STOP ")
  129.   elseif mode == 2 then
  130.     setColor(0x0, 0x00FF00)
  131.     gpu.set(x,y,"MARCHE")
  132.     setColor(0xFFFA000, 0x0)
  133.     gpu.set(x+8,y," AUTO ")
  134.     setColor(0x0, 0xFF0000)
  135.     gpu.set(x+15,y," STOP ")
  136.   elseif mode == 3 then
  137.     setColor(0x0, 0x00FF00)
  138.     gpu.set(x,y,"MARCHE")
  139.     setColor(0x0, 0xFFFA000)
  140.     gpu.set(x+8,y," AUTO ")
  141.     setColor(0xFF0000, 0x0)
  142.     gpu.set(x+15,y," STOP ")
  143.   end
  144. setColor(0x0, 0xFFFFFF)
  145. end
  146.  
  147. local function fdiesel(tableau)
  148.   local amount = diesel.getTankInfo().amount
  149.   local capacity = diesel.getTankInfo().capacity
  150.   local pct = amount/capacity
  151.   if mode_fuel == 1 then
  152.       rs.setOutput(side.bottom,15)
  153.   elseif mode_fuel == 2 then
  154.     if pct*100 > tank_maxi then
  155.       rs.setOutput(side.bottom,15)
  156.       setColor(0x0, 0xFF0000)
  157.       gpu.set(22,14,"║")
  158.       gpu.set(20,15,"  ║  ")
  159.       gpu.set(22,16,"║")
  160.     elseif pct*100 < tank_mini then
  161.       rs.setOutput(side.bottom,0)
  162.       setColor(0x0, 0x00FF00)
  163.       gpu.set(22,14," ")
  164.       gpu.set(20,15,"═════")
  165.       gpu.set(22,16," ")
  166.     end
  167.   elseif mode_fuel == 3 then
  168.     rs.setOutput(side.bottom,0)
  169.   end  
  170.   setColor(0x0, 0xFFFFFF)
  171.   local color = 0xafa50d
  172.   local color2 = 0xFFFFFF - color
  173.   barre_verticale(7,7,7,11,amount,capacity)
  174.   if diesel.isActive() == true then
  175.     gpu.set(45,11,unitrf(4096))
  176.     setColor(0x00FF00, 0x0)
  177.     gpu.set(21,5," ON ")
  178.   else
  179.     gpu.set(45,11,unitrf(0))
  180.     setColor(0xFF0000, 0x0)
  181.     gpu.set(21,5," OFF")    
  182.   end
  183.   setColor(0x0, 0xFFFFFF)
  184.   local pct = tableau[3]
  185.   if mode == 3 then
  186.     diesel.setEnabled(false)
  187.   elseif mode == 1 then
  188.     diesel.setEnabled(true)
  189.   elseif mode == 2 and palier_diesel > pct*100 then
  190.     diesel.setEnabled(true)
  191.   end
  192. end
  193.  
  194. local function connect(x,y)
  195.   if vconnect == false then
  196.     setColor(0x0000FF, 0x00FFFF)
  197.     gpu.set(x,y,"Déconnecté")
  198.       bank.setOutputMode("on")
  199.   elseif vconnect == true then
  200.     setColor(0x00FFFF, 0x0000FF)
  201.     gpu.set(x,y," Connecté ")
  202.       bank.setOutputMode("off")
  203.   end
  204.   setColor(0x0, 0xFFFFFF)  
  205. end
  206.  
  207. local function alimentation_fuel(x,y)
  208.   if mode_fuel == 1 then
  209.     setColor(0x0, 0x0000FF)
  210.     gpu.set(x,y,"Ouverte")
  211.   elseif mode_fuel == 2 then
  212.     setColor(0x0, 0xFFFA000)
  213.     gpu.set(x,y," Auto  ")
  214.   elseif mode_fuel == 3 then
  215.     setColor(0x0, 0xFF0000)
  216.     gpu.set(x,y,"Fermée ")  
  217.   end
  218.   setColor(0x0, 0xFFFFFF)  
  219. end
  220.  
  221. local function send(port)
  222.   local tableau_valeur = {}
  223.   tableau_valeur[1] = nb_diesel                      --n° diesel
  224.   tableau_valeur[2] = diesel.isActive()              --ON/OFF
  225.   tableau_valeur[3] = vconnect                       --Connecté/Déconnecté
  226.   tableau_valeur[4] = palier_diesel                  --Palier de déclenchement
  227.   tableau_valeur[5] = mode                           --Mode de focntionnement
  228.   tableau_valeur[6] = diesel.getTankInfo().amount    --Quantité de fuel
  229.   tableau_valeur[7] = diesel.getTankInfo().capacity  --Quantité de fuel max
  230.   tableau_valeur[8] = bank.getEnergyStored()
  231.   tableau_valeur[9] = bank.getMaxEnergyStored()
  232.   local table_s = serialization.serialize(tableau_valeur)
  233.   local IDcodes = "diesel " .. nb_diesel
  234.   modem.broadcast(port,IDcodes,table_s)
  235.   local texte_vide = ""
  236.   j = j + 1
  237.   if j > 9 then
  238.     j = 0
  239.     texte_vide = "               "
  240.   end
  241.   local text = " Envoi n°"..string.format("%s", j+1).." envoie vers le port "..port .. texte_vide
  242.   gpu.set(j+2,22,text)
  243. end
  244.  
  245. local function affbank(x,y,length)
  246.   local amount = bank.getEnergyStored()
  247.   local capacity = bank.getMaxEnergyStored()
  248.   local pct = amount / capacity
  249.   local color = 0x0aFF0a
  250.   local color2 = 0x0000FF
  251.   local cur = math.floor(pct * length)
  252.   local textfrac = string.format("%s / %s", unitrf(amount), unitrf(capacity))
  253.   local textpct = string.format("%.02f%%", pct*100)
  254.   local text1 = "Enérgie stockée dans le diesel"
  255.   local text2 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  256.   loadbar(x,y,length,cur,text1,color,color2)
  257.   loadbar(x,y+1,length,cur,text2,color,color2)
  258. end
  259.  
  260. --Trame de fond
  261. setColor(0x0,0xFFFFFF)
  262.  gpu.set(1,1,"╔═════════════════════════════════════════════════════════════╗")
  263.  gpu.set(1,2,"║                                                             ║")
  264.  gpu.set(1,3,"╠═════════════════════════════════════════════════════════════╣")
  265.  gpu.set(1,4,"║ FUEL >═══╗       ┌────┐        ┌─Mode de fonctionnement──┐  ║")
  266.  gpu.set(1,5,"║          ║       │ OFF│        │   MARCHE   AUTO   STOP  │  ║")
  267.  gpu.set(1,6,"║    ┌─────╨─────┐ └────┘ ┌─■─┐  └─────────────────────────┘  ║")
  268.  gpu.set(1,7,"║    │  Réserve  │        │   │      DIESEL n°                ║")
  269.  gpu.set(1,8,"║    │ Carburant │        │   │          ╥                    ║")
  270.  gpu.set(1,9,"║    │           │        │   └─────■──■─╨─■──■──■──┐         ║")
  271. gpu.set(1,10,"║    │           │        │                         └──────┐  ║")
  272. gpu.set(1,11,"║    │cur 8000 mb│        │    Production : X.XXX kRF/T    │  ║")
  273. gpu.set(1,12,"║    │           │        │                                │  ║")
  274. gpu.set(1,13,"║    │max 8000 mb│        │ Réseau électrique  Déconnecté  │  ║")
  275. gpu.set(1,14,"║    └─────╥─────┘        │                                │  ║")
  276. gpu.set(1,15,"║          ╚═══════  ║  ══╡ Palier de démarrage :  00.000% │  ║")
  277. gpu.set(1,16,"║                         └────────────────────────────────┘  ║")
  278. gpu.set(1,17,"║  Alimentation : ouverte                                     ║")
  279. gpu.set(1,18,"╠═════════════════════════════════════════════════════════════╣")
  280. gpu.set(1,19,"║                                                             ║")
  281. gpu.set(1,20,"║                                                             ║")
  282. gpu.set(1,21,"╠═════════════════════════════════════════════════════════════╣")
  283. gpu.set(1,22,"║                                                             ║")
  284. gpu.set(1,23,"╠═════════════════════════════════════════════════════════════╣")
  285. gpu.set(1,24,"║                                                             ║")
  286. gpu.set(1,25,"╠═════════════════════════════════════════════════════════════╣")
  287. gpu.set(1,26,"║bargraph energie                                             ║")
  288. gpu.set(1,27,"║                                                             ║")
  289. gpu.set(1,28,"║                                                             ║")
  290. gpu.set(1,29,"╚═════════════════════════════════════════════════════════════╝")
  291.  
  292. gpu.set(47,7,string.format("%s",nb_diesel))
  293. gpu.set(52,15,string.format("%.03f%%", palier_diesel))
  294. mode_fonctionnement(38,5)
  295. connect(48,13)
  296. alimentation_fuel(19,17)
  297.  
  298. --Main
  299. local function onModem(_,_,from,port,_,IDcodes,message)
  300.   local texte_vide = ""
  301.   i = i + 1
  302.   if i > 25 then
  303.     i = 0
  304.     texte_vide = "                              "
  305.   end
  306.   local texte_port = " Réception n°" .. i+1 .. " / port n°".. port .. texte_vide
  307.   gpu.set(i+2,24,texte_port)
  308.   if port == 10001 then
  309.     value_receive = serialization.unserialize(message)
  310.     affenergie(2,26,61,value_receive)
  311.   end    
  312. end
  313.  
  314. local function onTouch(event,adress,x,y,clic,pseudo)
  315.   local tclic
  316.   if clic == 0  then
  317.     tclic = "Clic gauche"
  318.   elseif clic == 1 then
  319.     tclic = "Clic droit"
  320.   else
  321.     tclic = "Clic inconnu"
  322.   end
  323.   gpu.set(2,2," "..tclic.." de la part de "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
  324.   if x==1 and y==1 then
  325.     computer.pushSignal("quit")
  326.     term.setCursor(1,1)
  327.     return false
  328.   elseif y == 13 and x>47 and x<58 then
  329.     if vconnect == true then
  330.       vconnect = false
  331.     elseif vconnect == false then
  332.       vconnect = true
  333.       rs.setOutput(side.top, 0)
  334.     end
  335.     connect(48,13)
  336.   elseif y == 5 then
  337.     if x>37 and x<44 then
  338.       mode = 1
  339.     elseif x>45 and x<52 then
  340.       mode = 2
  341.     elseif x>53 and x<59 then
  342.       mode = 3
  343.     end
  344.     mode_fonctionnement(38,5)
  345.   elseif x>19 and x<25 and y>13 and y<17 then
  346.     if mode_fuel == 1 then
  347.       mode_fuel = 2
  348.     elseif mode_fuel == 2 then
  349.       mode_fuel = 3
  350.     elseif mode_fuel == 3 then
  351.       mode_fuel = 1
  352.     end
  353.   alimentation_fuel(19,17)
  354.   elseif 1 == 0 then
  355.   end
  356. end
  357.  
  358. local function onTimer(_,timer)
  359.   fdiesel(value_receive)
  360.   send(10003)
  361.   affbank(2,19,61)
  362.   return true
  363. end
  364.  
  365. event.listen("touch",onTouch)
  366. event.listen("modem_message",onModem)
  367. local timer = event.timer(0.5,onTimer,math.huge)
  368. event.pull("quit")
  369. event.cancel(timer)
  370. event.ignore("touch",onTouch)
  371. event.ignore("modem_message",onModem)
  372. component.gpu.setResolution(160,50)
  373. modem.close(10001)
  374. term.clear()
  375.  
  376. --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