Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local computer = require("computer")
- local component = require("component")
- local term = require("term")
- local math = require("math")
- local event = require("event")
- local string = require("string")
- local unicode = require("unicode")
- local colors = require("colors")
- local side = require("sides")
- local serialization = require("serialization")
- --Récupération des composants
- local screen = component.getPrimary("screen")
- local gpu = component.gpu
- gpu.setResolution(63,29)
- local modem = component.modem
- local rs = component.redstone
- local diesel = component.ie_diesel_generator
- local bank = component.capacitor_bank
- --Initialisation variables
- local arg = {...}
- local nb_diesel = tonumber(arg[1])
- local value_receive = {}
- local palier_mini = 50 --pourcent
- local palier_maxi = 80 --pourcent
- local tank_mini = 35
- local tank_maxi = 95
- local mode = 3
- local i = 0
- local j = 0
- local vconnect = false
- local mode_fuel = 3
- local palier_diesel = palier_mini+(nb_diesel * (palier_maxi - palier_mini) / 16)
- --Ouverture des ports du modem
- modem.open(10001)
- local function setColor(bg,fg)
- gpu.setBackground(bg)
- gpu.setForeground(fg)
- end
- --Fonctions
- local function unitrf(qty)
- if qty >= 1000000000000000 or qty <= -1000000000000000 then
- return string.format("%.03f PRF/t",qty/1000000000000000)
- elseif qty >= 1000000000000 or qty <= -1000000000000 then
- return string.format("%.03f TRF/t",qty/1000000000000)
- elseif qty >=1000000000 or qty <= -1000000000 then
- return string.format("%.03f GRF/t",qty/1000000000)
- elseif qty >=1000000 or qty <= -1000000 then
- return string.format("%.03f MRF/t",qty/1000000)
- elseif qty >=1000 or qty <= -1000 then
- return string.format("%.03f kRF/t",qty/1000)
- else
- return string.format("%.03f RF/t ",qty)
- end
- end
- local function barre_verticale(x,y,hauteur,largeur,valeur,valmax)
- local k = 0
- local cur = math.floor((valeur / valmax) * hauteur)
- local dir = string.rep(" ",largeur)
- for k = 1, hauteur do
- local vy = y + hauteur - k
- if k <= cur then
- setColor(0xafa50d,0xFFFFFF-0xafa50d)
- elseif k > cur then
- setColor(0x000000,0xFFFFFF)
- end
- gpu.set(x,vy,dir)
- if k == 1 then
- gpu.set(x,vy,dir)
- gpu.set(x,vy,string.format("Max %4s mB",valmax))
- elseif k == 3 then
- gpu.set(x,vy,dir)
- gpu.set(x,vy,string.format("Cur %4s mB",valeur))
- elseif k == 4 then
- gpu.set(x,vy,dir)
- gpu.set(x,vy,string.format(" %06.02f%% ",(valeur/valmax)*100))
- elseif k == 6 then
- gpu.set(x,vy," Carburant ")
- elseif k == 7 then
- gpu.set(x,vy," Réserve ")
- end
- setColor(0x000000,0xFFFFFF)
- end
- end
- local function loadbar(x,y,longueur,valeur,text,bg,fg)
- local raw = " " .. text ..string.rep(" ", longueur - unicode.len(text) - 2) .. " "
- local oldbg = gpu.setBackground(bg)
- local oldfg = gpu.setForeground(fg)
- gpu.set(x,y,unicode.sub(raw,1,valeur))
- gpu.setBackground(oldbg)
- gpu.setForeground(oldfg)
- gpu.set(x+valeur,y,unicode.sub(raw,valeur+1,longueur))
- end
- local function affenergie(x,y,length,tableau)
- local amount = tableau[1]
- local capacity = tableau[2]
- local pct = tableau[3]
- local entree = tableau[4]
- local sortie = tableau[5]
- local delta = tableau[6]
- local color = 0x00FFFF
- local color2 = 0xFF0000
- local cur = math.floor(pct * length)
- local textfrac = string.format("%s / %s", unitrf(amount), unitrf(capacity))
- local textpct = string.format("%.02f%%", pct*100)
- local text1 = " ENERGIE DANS LA BATTERIE MEKANISM " .. tableau[7]
- local text2 = "I: " .. unitrf(entree) .. " O: " .. unitrf(sortie) .. " D: " .. unitrf(delta)
- local text3 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
- loadbar(x,y,length,cur,text1,color,color2)
- loadbar(x,y+1,length,cur,text2,color,color2)
- loadbar(x,y+2,length,cur,text3,color,color2)
- end
- local function mode_fonctionnement(x,y)
- if mode == 1 then
- setColor(0x00FF00, 0x0)
- gpu.set(x,y,"MARCHE")
- setColor(0x0, 0xFFFA000)
- gpu.set(x+8,y," AUTO ")
- setColor(0x0, 0xFF0000)
- gpu.set(x+15,y," STOP ")
- elseif mode == 2 then
- setColor(0x0, 0x00FF00)
- gpu.set(x,y,"MARCHE")
- setColor(0xFFFA000, 0x0)
- gpu.set(x+8,y," AUTO ")
- setColor(0x0, 0xFF0000)
- gpu.set(x+15,y," STOP ")
- elseif mode == 3 then
- setColor(0x0, 0x00FF00)
- gpu.set(x,y,"MARCHE")
- setColor(0x0, 0xFFFA000)
- gpu.set(x+8,y," AUTO ")
- setColor(0xFF0000, 0x0)
- gpu.set(x+15,y," STOP ")
- end
- setColor(0x0, 0xFFFFFF)
- end
- local function fdiesel(tableau)
- local amount = diesel.getTankInfo().amount
- local capacity = diesel.getTankInfo().capacity
- local pct = amount/capacity
- if mode_fuel == 1 then
- rs.setOutput(side.bottom,15)
- elseif mode_fuel == 2 then
- if pct*100 > tank_maxi then
- rs.setOutput(side.bottom,15)
- setColor(0x0, 0xFF0000)
- gpu.set(22,14,"║")
- gpu.set(20,15," ║ ")
- gpu.set(22,16,"║")
- elseif pct*100 < tank_mini then
- rs.setOutput(side.bottom,0)
- setColor(0x0, 0x00FF00)
- gpu.set(22,14," ")
- gpu.set(20,15,"═════")
- gpu.set(22,16," ")
- end
- elseif mode_fuel == 3 then
- rs.setOutput(side.bottom,0)
- end
- setColor(0x0, 0xFFFFFF)
- local color = 0xafa50d
- local color2 = 0xFFFFFF - color
- barre_verticale(7,7,7,11,amount,capacity)
- if diesel.isActive() == true then
- gpu.set(45,11,unitrf(4096))
- setColor(0x00FF00, 0x0)
- gpu.set(21,5," ON ")
- else
- gpu.set(45,11,unitrf(0))
- setColor(0xFF0000, 0x0)
- gpu.set(21,5," OFF")
- end
- setColor(0x0, 0xFFFFFF)
- local pct = tableau[3]
- if mode == 3 then
- diesel.setEnabled(false)
- elseif mode == 1 then
- diesel.setEnabled(true)
- elseif mode == 2 and palier_diesel > pct*100 then
- diesel.setEnabled(true)
- end
- end
- local function connect(x,y)
- if vconnect == false then
- setColor(0x0000FF, 0x00FFFF)
- gpu.set(x,y,"Déconnecté")
- bank.setOutputMode("on")
- elseif vconnect == true then
- setColor(0x00FFFF, 0x0000FF)
- gpu.set(x,y," Connecté ")
- bank.setOutputMode("off")
- end
- setColor(0x0, 0xFFFFFF)
- end
- local function alimentation_fuel(x,y)
- if mode_fuel == 1 then
- setColor(0x0, 0x0000FF)
- gpu.set(x,y,"Ouverte")
- elseif mode_fuel == 2 then
- setColor(0x0, 0xFFFA000)
- gpu.set(x,y," Auto ")
- elseif mode_fuel == 3 then
- setColor(0x0, 0xFF0000)
- gpu.set(x,y,"Fermée ")
- end
- setColor(0x0, 0xFFFFFF)
- end
- local function send(port)
- local tableau_valeur = {}
- tableau_valeur[1] = nb_diesel --n° diesel
- tableau_valeur[2] = diesel.isActive() --ON/OFF
- tableau_valeur[3] = vconnect --Connecté/Déconnecté
- tableau_valeur[4] = palier_diesel --Palier de déclenchement
- tableau_valeur[5] = mode --Mode de focntionnement
- tableau_valeur[6] = diesel.getTankInfo().amount --Quantité de fuel
- tableau_valeur[7] = diesel.getTankInfo().capacity --Quantité de fuel max
- tableau_valeur[8] = bank.getEnergyStored()
- tableau_valeur[9] = bank.getMaxEnergyStored()
- local table_s = serialization.serialize(tableau_valeur)
- local IDcodes = "diesel " .. nb_diesel
- modem.broadcast(port,IDcodes,table_s)
- local texte_vide = ""
- j = j + 1
- if j > 9 then
- j = 0
- texte_vide = " "
- end
- local text = " Envoi n°"..string.format("%s", j+1).." envoie vers le port "..port .. texte_vide
- gpu.set(j+2,22,text)
- end
- local function affbank(x,y,length)
- local amount = bank.getEnergyStored()
- local capacity = bank.getMaxEnergyStored()
- local pct = amount / capacity
- local color = 0x0aFF0a
- local color2 = 0x0000FF
- local cur = math.floor(pct * length)
- local textfrac = string.format("%s / %s", unitrf(amount), unitrf(capacity))
- local textpct = string.format("%.02f%%", pct*100)
- local text1 = "Enérgie stockée dans le diesel"
- local text2 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
- loadbar(x,y,length,cur,text1,color,color2)
- loadbar(x,y+1,length,cur,text2,color,color2)
- end
- --Trame de fond
- setColor(0x0,0xFFFFFF)
- gpu.set(1,1,"╔═════════════════════════════════════════════════════════════╗")
- gpu.set(1,2,"║ ║")
- gpu.set(1,3,"╠═════════════════════════════════════════════════════════════╣")
- gpu.set(1,4,"║ FUEL >═══╗ ┌────┐ ┌─Mode de fonctionnement──┐ ║")
- gpu.set(1,5,"║ ║ │ OFF│ │ MARCHE AUTO STOP │ ║")
- gpu.set(1,6,"║ ┌─────╨─────┐ └────┘ ┌─■─┐ └─────────────────────────┘ ║")
- gpu.set(1,7,"║ │ Réserve │ │ │ DIESEL n° ║")
- gpu.set(1,8,"║ │ Carburant │ │ │ ╥ ║")
- gpu.set(1,9,"║ │ │ │ └─────■──■─╨─■──■──■──┐ ║")
- gpu.set(1,10,"║ │ │ │ └──────┐ ║")
- gpu.set(1,11,"║ │cur 8000 mb│ │ Production : X.XXX kRF/T │ ║")
- gpu.set(1,12,"║ │ │ │ │ ║")
- gpu.set(1,13,"║ │max 8000 mb│ │ Réseau électrique Déconnecté │ ║")
- gpu.set(1,14,"║ └─────╥─────┘ │ │ ║")
- gpu.set(1,15,"║ ╚═══════ ║ ══╡ Palier de démarrage : 00.000% │ ║")
- gpu.set(1,16,"║ └────────────────────────────────┘ ║")
- gpu.set(1,17,"║ Alimentation : ouverte ║")
- gpu.set(1,18,"╠═════════════════════════════════════════════════════════════╣")
- gpu.set(1,19,"║ ║")
- gpu.set(1,20,"║ ║")
- gpu.set(1,21,"╠═════════════════════════════════════════════════════════════╣")
- gpu.set(1,22,"║ ║")
- gpu.set(1,23,"╠═════════════════════════════════════════════════════════════╣")
- gpu.set(1,24,"║ ║")
- gpu.set(1,25,"╠═════════════════════════════════════════════════════════════╣")
- gpu.set(1,26,"║bargraph energie ║")
- gpu.set(1,27,"║ ║")
- gpu.set(1,28,"║ ║")
- gpu.set(1,29,"╚═════════════════════════════════════════════════════════════╝")
- gpu.set(47,7,string.format("%s",nb_diesel))
- gpu.set(52,15,string.format("%.03f%%", palier_diesel))
- mode_fonctionnement(38,5)
- connect(48,13)
- alimentation_fuel(19,17)
- --Main
- local function onModem(_,_,from,port,_,IDcodes,message)
- local texte_vide = ""
- i = i + 1
- if i > 25 then
- i = 0
- texte_vide = " "
- end
- local texte_port = " Réception n°" .. i+1 .. " / port n°".. port .. texte_vide
- gpu.set(i+2,24,texte_port)
- if port == 10001 then
- value_receive = serialization.unserialize(message)
- affenergie(2,26,61,value_receive)
- end
- end
- local function onTouch(event,adress,x,y,clic,pseudo)
- local tclic
- if clic == 0 then
- tclic = "Clic gauche"
- elseif clic == 1 then
- tclic = "Clic droit"
- else
- tclic = "Clic inconnu"
- end
- gpu.set(2,2," "..tclic.." de la part de "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
- if x==1 and y==1 then
- computer.pushSignal("quit")
- term.setCursor(1,1)
- return false
- elseif y == 13 and x>47 and x<58 then
- if vconnect == true then
- vconnect = false
- elseif vconnect == false then
- vconnect = true
- rs.setOutput(side.top, 0)
- end
- connect(48,13)
- elseif y == 5 then
- if x>37 and x<44 then
- mode = 1
- elseif x>45 and x<52 then
- mode = 2
- elseif x>53 and x<59 then
- mode = 3
- end
- mode_fonctionnement(38,5)
- elseif x>19 and x<25 and y>13 and y<17 then
- if mode_fuel == 1 then
- mode_fuel = 2
- elseif mode_fuel == 2 then
- mode_fuel = 3
- elseif mode_fuel == 3 then
- mode_fuel = 1
- end
- alimentation_fuel(19,17)
- elseif 1 == 0 then
- end
- end
- local function onTimer(_,timer)
- fdiesel(value_receive)
- send(10003)
- affbank(2,19,61)
- return true
- end
- event.listen("touch",onTouch)
- event.listen("modem_message",onModem)
- local timer = event.timer(0.5,onTimer,math.huge)
- event.pull("quit")
- event.cancel(timer)
- event.ignore("touch",onTouch)
- event.ignore("modem_message",onModem)
- component.gpu.setResolution(160,50)
- modem.close(10001)
- term.clear()
- --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