Advertisement
sshikamaru

Rafinerie - fermenter x3

Apr 19th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.08 KB | None | 0 0
  1. --Liste des require
  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 sides = require("sides")
  11. local serialization = require("serialization")
  12.  
  13. --Définitions des variables
  14. local noir = 0x000000
  15. local blanc = 0xFFFFFF
  16. local rouge = 0xFF0000
  17. local vert = 0x00FF00
  18. local bg_ethanol = 0xcccaa5
  19. local fg_ethanol = 0xFFFFFF - bg_ethanol
  20. local color_tampon = 0x8cf600
  21. local i = 0
  22. local volume
  23. local volume_max
  24. local old_amount = {0,0,0}
  25. local old_tank = 0
  26. local arg = {...}
  27. local nb_fermenter = tonumber(arg[1])
  28. local Idcodes
  29. local top = sides.top
  30. local table_value = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false}
  31. local value_tampon = 0
  32. local amount = {0,0,0}
  33. local inv_fermenter = {0,0,0}
  34. local j = 0
  35. local tableau_valeur = {}
  36.  
  37. --Récupération du matériel
  38. local screen = component.getPrimary("screen")
  39. local gpu = component.gpu
  40. gpu.setResolution(75,60)
  41. local modem = component.modem
  42. local rs = component.redstone
  43. local port_modem = 10001
  44. local tank = component.getPrimary("thermalexpansion_tank")
  45. local chest = component.diamond
  46. local fermenter1 = component.proxy(component.get("","ie_fermenter"))
  47. local fermenter2 = component.proxy(component.get("","ie_fermenter"))
  48. local fermenter3 = component.proxy(component.get("","ie_fermenter"))
  49. local fermenter = {fermenter1,fermenter2,fermenter3}
  50.  
  51. --Fonctions
  52. local function setColor(bg,fg)
  53.   gpu.setBackground(bg)
  54.   gpu.setForeground(fg)
  55. end
  56.  
  57. local function unitmb(qty)
  58.   if qty >= 1000000000 then
  59.     return string.format("%.02f MB",qty/10000000)
  60.   elseif qty >=1000000 then
  61.     return string.format("%.02f KB",qty/1000000)
  62.   elseif qty >=1000 then
  63.     return string.format("%.02f B ",qty/1000)
  64.   else
  65.     return string.format("%d mB",qty)
  66.   end
  67. end
  68.  
  69. local function loadbar(x,y,width,cur,text,bg)
  70.   local raw = " " .. text ..string.rep(" ", width - unicode.len(text) - 2) .. " "
  71.   local oldbg = gpu.setBackground(bg)
  72.   local oldfg = gpu.setForeground(0xFFFFFF - bg)
  73.   gpu.set(x,y,unicode.sub(raw,1,cur))
  74.   gpu.setBackground(oldbg)
  75.   gpu.setForeground(oldfg)
  76.   gpu.set(x+cur,y,unicode.sub(raw,cur+1,width))
  77. end
  78.  
  79. local function affbar(x,y,length)
  80.   local capacity = tank.getTankInfo()[1].capacity
  81.   local amount
  82.   local text1
  83.   if tank.getTankInfo()[1].contents == nil then
  84.     amount = 0
  85.     text1 = " Stock tampon : " .. "Vide"
  86.   else
  87.     amount = tank.getTankInfo()[1].contents.amount
  88.     text1 = " Stock tampon : " .. tank.getTankInfo()[1].contents.rawName
  89.   end
  90.   tableau_valeur[10] = amount
  91.   tableau_valeur[11] = capacity
  92.   local pct = amount / capacity
  93.   local cur = math.floor(pct * length)
  94.   local textfrac = string.format("%s / %s", unitmb(amount), unitmb(capacity))
  95.   local textpct = string.format("%.02f%%", pct*100)
  96.   local text2
  97.   if old_tank > amount then
  98.     text2 = "  ◄  "
  99.   elseif old_tank < amount then
  100.     text2 = "  ►  "
  101.   elseif old_tank == amount then
  102.    text2 = "  =  "
  103.   end
  104.   local text3 = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  105.   loadbar(x,y,length,cur,text1,bg_ethanol)
  106.   loadbar(x,y+1,length,cur,text2,bg_ethanol)
  107.   loadbar(x,y+2,length,cur,text3,bg_ethanol)
  108.   old_tank = amount
  109. end
  110.  
  111. local function barre_verticale(x,y,hauteur,largeur,amount,capacity,n)
  112.   local inv1 = 0
  113.   local inv2 = 0
  114.   local entete = " Fermenter n°"..n
  115.   for i = 1, 9 do
  116.     if fermenter[n].getInputStack(i) == nil then
  117.       inv2 = 0
  118.     else
  119.       inv2 = tonumber(fermenter[n].getInputStack(i).size)
  120.     end
  121.     inv1 = inv1 + inv2
  122.   end
  123.   inv_fermenter[n] = inv1
  124.   local k = 0
  125.   local text
  126.   if old_amount[n] < amount then
  127.     text = "  ►  "
  128.   elseif old_amount[n] > amount then
  129.     text = "  ◄  "
  130.   elseif old_amount[n] == amount then
  131.    text = "  =  "
  132.   end
  133.   local cur = math.floor((amount / capacity) * hauteur)
  134.   local dir = string.rep(" ",largeur)
  135.   for k = 1, hauteur do
  136.     local vy = y + hauteur - k
  137.     if k <= cur then
  138.       setColor(bg_ethanol,fg_ethanol)
  139.     elseif k > cur then
  140.       setColor(noir,blanc)  
  141.     end
  142.       gpu.set(x,vy,dir)
  143.     if k == 1 then
  144.       gpu.set(x,vy,dir)
  145.     elseif k == hauteur - 22 then
  146.       gpu.set(x,vy,dir)
  147.       gpu.set(x,vy,string.format("    % 6.02f%%",inv1*100/(64*9)))
  148.     elseif k == hauteur - 21 then
  149.       gpu.set(x,vy,dir)
  150.       gpu.set(x,vy,string.format(" Quantité % 3d",inv1))
  151.     elseif k == hauteur - 20 then
  152.       gpu.set(x,vy,dir)
  153.       gpu.set(x,vy," Canne à sucre ")
  154.     elseif k == hauteur - 10 then
  155.       gpu.set(x,vy,dir)
  156.       gpu.set(x,vy,string.format("  Cur %06.02f%%  ",(amount/capacity)*100))
  157.     elseif k == hauteur - 8 then
  158.       gpu.set(x,vy,dir)
  159.       gpu.set(x,vy," Max "..unitmb(capacity))
  160.     elseif k == hauteur - 6 then
  161.       gpu.set(x,vy,dir)
  162.       gpu.set(x,vy," Etat :"..text)
  163.     elseif k == hauteur - 5 then
  164.       gpu.set(x,vy,dir)
  165.       gpu.set(x,vy," Cur "..unitmb(amount))
  166.     elseif k == hauteur - 1 then
  167.       gpu.set(x,vy,entete)
  168.     end
  169.     setColor(noir,blanc)
  170.   end
  171. end
  172.  
  173. local function process(x,y,hauteur,largeur,n)
  174.   local capacity = 12000
  175.   if fermenter[n].getFluid().amount == nil then
  176.     amount[n] = 0
  177.   else
  178.     amount[n] = fermenter[n].getFluid().amount
  179.   end
  180.   barre_verticale(x,y,hauteur,largeur,amount[n],capacity,n)
  181.   old_amount[n] = amount[n]
  182. end
  183.  
  184. local function process_x3(x,y,hauteur,largeur)
  185.   process(x,y,hauteur,largeur,1)
  186.   process(x+16,y,hauteur,largeur,2)
  187.   process(x+32,y,hauteur,largeur,3)
  188. end
  189.  
  190. local function tampon(x,y,largeur)
  191.   value_tampon = 0
  192.   local inv = 0
  193.   local capacity = 108*64
  194.   for i = 1, 108 do
  195.     if chest.getStackInSlot(i) == nil then
  196.       inv = 0
  197.     else
  198.       inv = chest.getStackInSlot(i).qty
  199.     end
  200.   value_tampon = value_tampon + inv
  201.   end
  202.   local pct = value_tampon / capacity
  203.   local cur = math.floor(pct * largeur)
  204.   local text1 = " "..string.format("%s / %s", value_tampon, capacity)
  205.   local text2 = " "..string.format("% 6.02f%%", pct*100)
  206.   loadbar(x,y,largeur,cur,text1,color_tampon)
  207.   loadbar(x,y+1,largeur,cur,text2,color_tampon)
  208. end
  209.  
  210. local function vanne(x,y,tx,ty,n)
  211.   if tx > x-1 and tx < x+5 and ty > y-1 and ty < y+3 then
  212.     if table_value[n] == false then
  213.       table_value[n] = true
  214.     elseif table_value[n] == true then
  215.       table_value[n] = false
  216.     end
  217.   end
  218.   if table_value[n] == true then
  219.     setColor(noir,vert)
  220.     gpu.set(x+2,y+1,"║")
  221.     gpu.set(x,y+2,"  ║  ")
  222.     gpu.set(x+2,y+3,"║")
  223.     rs.setBundledOutput(top,n-1,0)
  224.   elseif table_value[n] == false then
  225.     setColor(noir,rouge)
  226.     gpu.set(x+2,y+1," ")
  227.     gpu.set(x,y+2,"═════")
  228.     gpu.set(x+2,y+3," ")
  229.     rs.setBundledOutput(top,n-1,255)
  230.   end
  231.   setColor(noir,blanc)
  232. end
  233.  
  234. local function on_off(x,y,tx,ty,n)
  235.   if tx > x-1 and tx < x+5 and ty == y then
  236.     if table_value[n] == false then
  237.       table_value[n] = true
  238.     elseif table_value[n] == true then
  239.       table_value[n] = false
  240.     end
  241.   end
  242.   if table_value[n] == true then
  243.     text = "  ON "
  244.     color = vert
  245.     rs.setBundledOutput(top,n-1,0)
  246.   elseif table_value[n] == false then
  247.     text = " OFF "
  248.     color = rouge
  249.     rs.setBundledOutput(top,n-1,255)
  250.   end
  251.   setColor(color,noir)
  252.   gpu.set(x,y,text)
  253.   setColor(noir,blanc)
  254. end
  255.  
  256. local function send(port)
  257.   tableau_valeur[1] = nb_fermenter                    --n° fermenter
  258.   tableau_valeur[2] = amount[1]                       --Quantité fermenter 1
  259.   tableau_valeur[3] = amount[2]                       --Quantité fermenter 2
  260.   tableau_valeur[4] = amount[3]                       --Quantité fermenter 3
  261.   tableau_valeur[5] = 12000                           --quantité max fermenter
  262.   tableau_valeur[6] = inv_fermenter[1]                --Inventaire fermenter 1
  263.   tableau_valeur[7] = inv_fermenter[2]                --Inventaire fermenter 2
  264.   tableau_valeur[8] = inv_fermenter[3]                --Inventaire fermenter 3
  265.   tableau_valeur[9] = 9*64                            --Inventaire max fermenter
  266.   tableau_valeur[12] = value_tampon                   --tampon
  267.   tableau_valeur[13] = 108*64                         --tampon max
  268.   local table_s = serialization.serialize(tableau_valeur)
  269.   local IDcodes = "fermenter"
  270.   modem.broadcast(port,IDcodes,table_s)
  271.   local texte_vide = ""
  272.   j = j + 1
  273.   if j > 9 then
  274.     j = 0
  275.     texte_vide = "               "
  276.   end
  277.   local text = " Envoi n°"..string.format("%s", j+1).." Confirmé"..texte_vide
  278.   gpu.set(34+j,54,text)
  279. end
  280.  
  281. --Trame de fond
  282.   gpu.set(1,1,[[╔═════════════════════════════════════════════════════════════════════════╗]])
  283.   gpu.set(1,2,[[║                                                                         ║]])
  284.   gpu.set(1,3,[[╠═════════════════════════════════════════════════════════════════════════╣]])
  285.   gpu.set(1,4,[[║     _________________________________________                           ║]])
  286.   gpu.set(1,5,[[║    /                                         \                          ║]])
  287.   gpu.set(1,6,[[║   /      COLONNE DE FERMENTATION N°           \                         ║]])
  288.   gpu.set(1,7,[[║  /_____________________________________________\                        ║]])
  289.   gpu.set(1,8,[[║ │               │               │               │                       ║]])
  290.   gpu.set(1,9,[[║ │               │               │               │                       ║]])
  291.  gpu.set(1,10,[[║ │               │               │               │                       ║]])
  292.  gpu.set(1,11,[[║ │               │               │               │                       ║]])
  293.  gpu.set(1,12,[[║ │               │               │               │                       ║]])
  294.  gpu.set(1,13,[[║ │               │               │               │                       ║]])
  295.  gpu.set(1,14,[[║ │               │               │               │                       ║]])
  296.  gpu.set(1,15,[[║ │               │               │               │                       ║]])
  297.  gpu.set(1,16,[[║ │               │               │               │ Alimentation Générale ║]])
  298.  gpu.set(1,17,[[║ │               │               │               │    Energie : ■OFF■    ║]])
  299.  gpu.set(1,18,[[║ │               │               │               │    Produit : ■OFF■    ║]])
  300.  gpu.set(1,19,[[║ │               │               │               │    Tampon  : ■OFF■    ║]])
  301.  gpu.set(1,20,[[║ │               │               │               │                       ║]])
  302.  gpu.set(1,21,[[║ │               │               │               │                       ║]])
  303.  gpu.set(1,22,[[║ │               │               │               │                       ║]])
  304.  gpu.set(1,23,[[║ │               │               │               │                       ║]])
  305.  gpu.set(1,24,[[║ │               │               │               │                       ║]])
  306.  gpu.set(1,25,[[║ │               │               │               │                       ║]])
  307.  gpu.set(1,26,[[║ │               │               │               │                       ║]])
  308.  gpu.set(1,27,[[║ │               │               │               │     ╔══< Alimentation ║]])
  309.  gpu.set(1,28,[[║ │               │               │               │     ║                 ║]])
  310.  gpu.set(1,29,[[║ │               │               │               │                       ║]])
  311.  gpu.set(1,30,[[║ │               │               │               │   ═════               ║]])
  312.  gpu.set(1,31,[[║ │               │               │               │                       ║]])
  313.  gpu.set(1,32,[[║ │               │               │               │   ┌─╨─────────────┐   ║]])
  314.  gpu.set(1,33,[[║ │               │               │               │   │ Stock Tampon  │   ║]])
  315.  gpu.set(1,34,[[║ │               │               │               │   │               │   ║]])
  316.  gpu.set(1,35,[[║ │               │               │               │   │               │   ║]])
  317.  gpu.set(1,36,[[║ │               │               │               │   │               │   ║]])
  318.  gpu.set(1,37,[[║ │               │               │               │   └───────╥───────┘   ║]])
  319.  gpu.set(1,38,[[║ │               │               │               │           ║           ║]])
  320.  gpu.set(1,39,[[║ ├───────────────┼───────────────┼───────────────┤                       ║]])
  321.  gpu.set(1,40,[[║ │ Alimentation  │ Alimentation  │ Alimentation  │         ═════         ║]])
  322.  gpu.set(1,41,[[║ │ Energie : OFF │ Energie : OFF │ Energie : OFF │                       ║]])
  323.  gpu.set(1,42,[[║ │ Produit : OFF │ Produit : OFF │ Produit : OFF ╞═══════════╝           ║]])
  324.  gpu.set(1,43,[[║ │ Pompe   : OFF │ Pompe   : OFF │ Pompe   : OFF │                       ║]])
  325.  gpu.set(1,44,[[║ ├───────────────┴───────────────┴───────────────┤                       ║]])
  326.  gpu.set(1,45,[[║ │                                               │                       ║]])
  327.  gpu.set(1,46,[[║ │                                               │                       ║]])
  328.  gpu.set(1,47,[[║ │                                               │                       ║]])
  329.  gpu.set(1,48,[[║ └────────────────────────╥──────────────────────┘                       ║]])
  330.  gpu.set(1,49,[[║                          ║                                              ║]])
  331.  gpu.set(1,50,[[║                                                                         ║]])
  332.  gpu.set(1,51,[[║                        ═════                                            ║]])
  333.  gpu.set(1,52,[[║                                                                         ║]])
  334.  gpu.set(1,53,[[║                          ║                                              ║]])
  335.  gpu.set(1,54,[[║                       /¯¯║¯¯\                                           ║]])
  336.  gpu.set(1,55,[[║  /¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯       ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯\  ║]])
  337.  gpu.set(1,56,[[║ │ Cuve Ethanol Générale                                               │ ║]])
  338.  gpu.set(1,57,[[║ │                                                                     │ ║]])
  339.  gpu.set(1,58,[[║ │                                                                     │ ║]])
  340.  gpu.set(1,59,[[║  \___________________________________________________________________/  ║]])
  341.  gpu.set(1,60,[[╚═════════════════════════════════════════════════════════════════════════╝]])
  342.  
  343. local function affbouton(x,y)
  344.   on_off(14,41,x,y,1)
  345.   on_off(14,42,x,y,2)
  346.   on_off(14,43,x,y,3)
  347.   on_off(30,41,x,y,4)
  348.   on_off(30,42,x,y,5)
  349.   on_off(30,43,x,y,6)
  350.   on_off(46,41,x,y,7)
  351.   on_off(46,42,x,y,8)
  352.   on_off(46,43,x,y,9)
  353.   on_off(66,17,x,y,10)
  354.   on_off(66,18,x,y,11)
  355.   on_off(66,19,x,y,12)
  356.   vanne(55,28,x,y,13)
  357.   vanne(61,38,x,y,14)
  358.   vanne(26,49,x,y,14)
  359. end
  360.  
  361. affbouton(2,2)
  362. gpu.set(39,6,tostring(nb_fermenter))
  363.  
  364. local function onTouch(_,address,x,y,clic,pseudo)
  365.   local tclic
  366.   if clic == 0  then
  367.     tclic = "Clic gauche"
  368.   elseif clic == 1 then
  369.     tclic = "Clic droit"
  370.   else
  371.     tclic = "Clic inconnu"
  372.   end
  373.   gpu.set(4,2,tclic.." de la part de "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
  374.  
  375.   if x~=1 and y~=1 then
  376.   elseif x==1 and y==1 then
  377.     computer.pushSignal("quit")
  378.     gpu.fill(1,1,160,50," ")
  379.     term.setCursor(1,1)
  380.     return false
  381.   end
  382. end
  383.  
  384. local function onTimer(_,timer)
  385.   process_x3(4,9,30,15)
  386.   affbar(4,45,47)
  387.   tampon(56,34,15)
  388.   send(port_modem)
  389.   return true
  390. end
  391.  
  392. event.listen("touch",onTouch)
  393. local timer = event.timer(.1,onTimer,math.huge)
  394. event.pull("quit")
  395. event.cancel(timer)
  396. event.ignore("touch",onTouch)
  397. gpu.fill(1,1,160,50," ")
  398. gpu.setResolution(160,50)
  399.  
  400. --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