Advertisement
sshikamaru

fermenter/squeezer

Oct 16th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.58 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. print("Requires... ok")
  13. os.sleep(0,5)
  14.  
  15. --Définitions des variables
  16. local init = 0 --valeur avant initialisation
  17. local table_fermenter = {name_oc="fermenter",name="    Fermenteur    ",item=" Cannes à sucre",item_color=0xb4f6a3,product="    Ethanol    ",product_color=0xcccaa5}
  18. local table_squeezer = {name_oc="squeezer",name="    Extracteur    ",item="Graines de melon",item_color=0xdf9891,product="Huile de plante",product_color=0x6a6837}
  19. local table_generale = {table_fermenter,table_squeezer}
  20. local tableau_envoi = {}
  21. local tableau_mode_auto = {1,1} -- mode_item,mode_produit  /  1 = OFF / 2 = Auto / 3 = ON
  22. local tableau_old_mode_auto = {3,3} -- mode_item,mode_produit  / 1  = OFF / 2 = Auto / 3 = ON
  23. local tableau_etat_sortie = {false,false,false}  -- 1 = item / 2 = produit / 3 = alim_elec
  24. local tableau_old_etat_sortie = {true,true,true} -- 1 = item / 2 = produit / 3 = alim_elec
  25. local tableau_bit = {{false,true},{true,false}} -- 1 = item / 2 = produit
  26. local ntable = 0
  27. local arg = {...}
  28. local groupement_machine = tonumber(arg[1])
  29. local nb_machine = tonumber(arg[2])
  30. local nom_machine = tostring(arg[3])
  31. local machine
  32. local noir = 0x000000
  33. local blanc = 0xFFFFFF
  34. local rouge = 0xFF0000
  35. local vert = 0x00FF00
  36. local orange = 0xFFFA000
  37. local i = 0
  38. local j = 0
  39. local Idcodes
  40. local top = sides.top
  41. local volume = 0
  42. local volume_max = 0
  43. local old_volume = 50000000000
  44. local stock_item = 0
  45. local item_max = 0
  46. local old_item = 50000000000
  47. local niveau_mini = 7
  48. local niveau_maxi = 93
  49. print("Initialisation variables... ok")
  50. os.sleep(0,5)
  51.  
  52. --Récupération du matériel
  53. local screen = component.getPrimary("screen")
  54. local gpu = component.gpu
  55. gpu.setResolution(60,30)
  56. local modem = component.modem
  57. local rs = component.redstone
  58. local port_modem = 10001
  59. local pompe = component.immersiveengineering_fluidpump
  60. if nom_machine == "f" then
  61.   ntable = 1
  62.   machine = component.proxy(component.get("","ie_fermenter"))
  63. elseif nom_machine == "s" then
  64.   ntable = 2
  65.   machine = component.proxy(component.get("","ie_squeezer"))
  66. end
  67. print("Récupération materiel... ok")
  68. os.sleep(0,5)
  69.  
  70. --Fonctions
  71. local function setColor(bg,fg)
  72.   gpu.setBackground(bg)
  73.   gpu.setForeground(fg)
  74. end
  75.  
  76. local function unitmb(qty)
  77.   if qty >= 1000000000 then
  78.     return string.format("%.02f MB",qty/10000000)
  79.   elseif qty >=1000000 then
  80.     return string.format("%.02f KB",qty/1000000)
  81.   elseif qty >=1000 then
  82.     return string.format("%.02f B ",qty/1000)
  83.   else
  84.     return string.format("%d mB",qty)
  85.   end
  86. end
  87.  
  88. local function loadbar(x,y,width,cur,text,bg)
  89.   local raw = " " .. text ..string.rep(" ", width - unicode.len(text) - 2) .. " "
  90.   local oldbg = gpu.setBackground(bg)
  91.   local oldfg = gpu.setForeground(blanc - bg)
  92.   gpu.set(x,y,unicode.sub(raw,1,cur))
  93.   gpu.setBackground(oldbg)
  94.   gpu.setForeground(oldfg)
  95.   gpu.set(x+cur,y,unicode.sub(raw,cur+1,width))
  96. end
  97.  
  98. function niveau_sens(amount,old_amount)
  99.   if old_amount < amount then
  100.     return "►"
  101.   elseif old_amount > amount then
  102.     return "◄"
  103.   elseif old_amount == amount then
  104.    return "="
  105.   end
  106. end
  107.  
  108. local function barre_verticale(x,y,hauteur,largeur,amount,capacity,old,color,text1,text2,text3)
  109.   local k = 0
  110.   local cur = math.floor((amount / capacity) * hauteur)
  111.   local dir = string.rep(" ",largeur)
  112.   for k = 1, hauteur do
  113.     local vy = y + hauteur - k
  114.     if k <= cur then
  115.       setColor(color,blanc-color)
  116.     elseif k > cur then
  117.      setColor(noir,blanc)  
  118.     end
  119.       gpu.set(x,vy,dir)
  120.     if k == hauteur - 11 then
  121.       gpu.set(x,vy,dir)
  122.       gpu.set(x,vy,string.format("     %06.02f%%  ",(amount/capacity)*100)) --Pourcentage
  123.     elseif k == hauteur - 10 then
  124.       gpu.set(x,vy,dir)
  125.       gpu.set(x,vy,"    Pourcentage")
  126.     elseif k == hauteur - 8 then
  127.       gpu.set(x,vy,dir)
  128.       gpu.set(x,vy,text1)
  129.     elseif k == hauteur - 7 then
  130.       gpu.set(x,vy,dir)
  131.       gpu.set(x,vy," Valeur Maximale")
  132.     elseif k == hauteur - 5 then
  133.       gpu.set(x,vy,dir)
  134.       gpu.set(x,vy,text2)
  135.     elseif k == hauteur - 4 then
  136.       gpu.set(x,vy,dir)
  137.       gpu.set(x,vy," Valeur actuelle")
  138.     elseif k == hauteur - 1 then
  139.       gpu.set(x,vy,dir)
  140.       gpu.set(x,vy,text3)
  141.     end
  142.     setColor(noir,blanc)
  143.   end  
  144. end
  145.  
  146. local function double_barre(x,y,hauteur,largeur)
  147.   local inv1 = 0
  148.   local inv2 = 0
  149.   item_max = 64*9
  150.   for i = 1, 9 do
  151.     if machine.getInputStack(i) == nil then
  152.       inv2 = 0
  153.     else
  154.       inv2 = tonumber(machine.getInputStack(i).size)
  155.     end
  156.     inv1 = inv1 + inv2
  157.   end
  158.   stock_item = inv1
  159.   if old_item == stock_item then
  160.   else
  161.     barre_verticale(x,y,hauteur,largeur,stock_item,item_max,old_item,table_generale[ntable].item_color,string.format(" Quantité % 3d",item_max),string.format("    % 3d",stock_item).."      "..niveau_sens(stock_item,old_item),table_generale[ntable].item)
  162.   end
  163.  
  164.   local volume1
  165.   local volume2
  166.   volume_max = 12000 + 4000
  167.   if pompe.getTankInfo(1)[1].contents == nil then
  168.     volume1 = 0
  169.   else
  170.     volume1 = pompe.getTankInfo(1)[1].contents.amount
  171.   end
  172.   if machine.getFluid().amount == nil then
  173.     volume2 = 0
  174.   else
  175.     volume2 = machine.getFluid().amount
  176.   end
  177.   volume = volume1 + volume2
  178.   if old_volume == volume then
  179.   else
  180.     barre_verticale(x+1+largeur,y,hauteur,largeur,volume,volume_max,old_volume,table_generale[ntable].product_color,"    "..unitmb(volume_max),"    "..unitmb(volume).." "..niveau_sens(volume,old_volume),table_generale[ntable].product)
  181.   end
  182.  
  183.   old_volume = volume
  184.   old_item = stock_item
  185. end
  186.  
  187. local function on_auto_off(x,y,n)
  188.   if tableau_mode_auto[n] == 1 then
  189.     setColor(rouge, noir)
  190.     gpu.set(x+4,y," OFF ")
  191.     setColor(noir, orange)
  192.     gpu.set(x+3,y+1,"Auto")
  193.     setColor(noir, vert)
  194.     gpu.set(x,y," ON ")  
  195.   elseif tableau_mode_auto[n] == 2 then
  196.     setColor(noir, rouge)
  197.     gpu.set(x+4,y," OFF ")
  198.     setColor(orange, noir)
  199.     gpu.set(x+3,y+1,"Auto")
  200.     setColor(noir, vert)
  201.     gpu.set(x,y," ON ")
  202.   elseif tableau_mode_auto[n] == 3 then
  203.     setColor(noir, rouge)
  204.     gpu.set(x+4,y," OFF ")
  205.     setColor(noir, orange)
  206.     gpu.set(x+3,y+1,"Auto")
  207.     setColor(vert, noir)
  208.     gpu.set(x,y," ON ")
  209.   end
  210.   setColor(noir, blanc)
  211. end
  212.  
  213. local function on_off(x,y,n)
  214.   if tableau_etat_sortie[n] == false then
  215.     setColor(rouge, noir)
  216.     gpu.set(x+4,y," OFF ")
  217.     setColor(noir, vert)
  218.     gpu.set(x,y," ON ")
  219.     rs.setBundledOutput(top,n-1,255)
  220.   elseif tableau_etat_sortie[n] == true then
  221.     setColor(noir, rouge)
  222.     gpu.set(x+4,y," OFF ")
  223.     setColor(vert, noir)
  224.     gpu.set(x,y," ON ")
  225.     rs.setBundledOutput(top,n-1,0)
  226.   end
  227.   setColor(noir, blanc)
  228. end
  229.  
  230. local function etat_vanne(x,y,n)
  231.   if tableau_mode_auto[n] == 1 then
  232.     setColor(noir, rouge)
  233.     gpu.set(x,y,"Fermée ")  
  234.   elseif tableau_mode_auto[n] == 2 then
  235.     setColor(noir, orange)
  236.     gpu.set(x,y," Auto  ")
  237.   elseif tableau_mode_auto[n] == 3 then
  238.     setColor(noir, vert)
  239.     gpu.set(x,y,"Ouverte")
  240.   end
  241.   setColor(noir, blanc)  
  242. end
  243.  
  244. local function vanne(x,y,n)
  245.   if tableau_etat_sortie[n] == false then
  246.     setColor(noir,rouge)
  247.     gpu.set(x,y+0,"  ║  ")
  248.     gpu.set(x,y+1,"  ║  ")
  249.     gpu.set(x,y+2,"  ║  ")
  250.     rs.setBundledOutput(top,n-1,255)
  251.   elseif tableau_etat_sortie[n] == true then
  252.     setColor(noir,vert)
  253.     gpu.set(x,y+0,"     ")
  254.     gpu.set(x,y+1,"═════")
  255.     gpu.set(x,y+2,"     ")
  256.     rs.setBundledOutput(top,n-1,0)
  257.   end
  258.   setColor(noir,blanc)
  259. end
  260.  
  261. local function fonction_auto(amount,capacity,x,y,ty,n)
  262.   local pct = amount/capacity
  263.   if tableau_mode_auto[n] == 1 then
  264.     tableau_etat_sortie[n] = false
  265.   elseif tableau_mode_auto[n] == 2 then
  266.     if pct*100 > niveau_maxi then
  267.       tableau_etat_sortie[n] = tableau_bit[n][1]
  268.     elseif pct*100 < niveau_mini then
  269.       tableau_etat_sortie[n] = tableau_bit[n][2]
  270.     end
  271.   elseif tableau_mode_auto[n] == 3 then
  272.     tableau_etat_sortie[n] = true
  273.   end
  274.   if tableau_old_mode_auto[n] == tableau_mode_auto[n] then
  275.   else
  276.     etat_vanne(x-1,y+ty,n)
  277.   end
  278.   if tableau_old_etat_sortie[n] == tableau_etat_sortie[n] then
  279.   else
  280.     vanne(x,y,n)
  281.   end
  282.   if init == 1 then
  283.     tableau_old_etat_sortie[n] = tableau_etat_sortie[n]
  284.     tableau_old_mode_auto[n] = tableau_mode_auto[n]
  285.   end
  286. end
  287.  
  288. local function send(port)
  289.   tableau_envoi[1]  = groupement_machine              --groupe de machine n°
  290.   tableau_envoi[2]  = ntable                          --type de  machine -- 1 : fermenter / 2 : squeezer
  291.   tableau_envoi[3]  = nb_machine                      --n° machine
  292.   tableau_envoi[4]  = volume                          --volume produit
  293.   tableau_envoi[5]  = volume_max                      --voluùme max produit
  294.   tableau_envoi[6]  = stock_item                      --Quantité item
  295.   tableau_envoi[7]  = item_max                        --quantité max item
  296.   tableau_envoi[8]  = tableau_etat_sortie[1]          --etat sortie item
  297.   tableau_envoi[9]  = tableau_etat_sortie[2]          --etat sortie produit
  298.   tableau_envoi[10] = tableau_etat_sortie[3]          --etat sortie elec
  299.   local table_s = serialization.serialize(tableau_envoi)
  300.   local IDcodes = "machine"
  301.   modem.broadcast(port,IDcodes,table_s)
  302.   local texte_vide = ""
  303.   j = j + 1
  304.   if j > 9 then
  305.     j = 0
  306.     texte_vide = "               "
  307.   end
  308.   local text = " Envoi n°"..string.format("%s", j+1).." Confirmé"..texte_vide
  309.   gpu.set(2+j,29,text)
  310. end
  311. print("Fonctions... ok")
  312. os.sleep(0,5)
  313. print("Initialisation programme...")
  314. os.sleep(1)
  315.  
  316. --Trame de fond
  317.  gpu.set(1,1,[[╔══════════════════════════════════════════════════════════╗]])
  318.  gpu.set(1,2,[[║                                                          ║]])
  319.  gpu.set(1,3,[[╠════════════════════════════════════════════════╦═════════╣]])
  320.  gpu.set(1,4,[[║                                                ║  Vanne  ║]])
  321.  gpu.set(1,5,[[║                       ║                        ║ Energie ║]])
  322.  gpu.set(1,6,[[║               ╔═════  ║  ══════════<           ║ ON  OFF ║]])
  323.  gpu.set(1,7,[[║               ║       ║                        ║         ║]])
  324.  gpu.set(1,8,[[║               ║                                ╠═════════╣]])
  325.  gpu.set(1,9,[[║     ┌─────────╨────────┬──────────────────┐    ║  Vanne  ║]])
  326. gpu.set(1,10,[[║     │      Objet       │     Produit      │    ║Composant║]])
  327. gpu.set(1,11,[[║     │                  │                  │    ║ ON  OFF ║]])
  328. gpu.set(1,12,[[║     │                  │                  │    ║   Auto  ║]])
  329. gpu.set(1,13,[[║     │                  │                  │    ╠═════════╣]])
  330. gpu.set(1,14,[[║     │                  │                  │    ║  Vanne  ║]])
  331. gpu.set(1,15,[[║     │                  │                  │    ║ Produit ║]])
  332. gpu.set(1,16,[[║     │                  │                  │    ║ ON  OFF ║]])
  333. gpu.set(1,17,[[║     │                  │                  │    ║   Auto  ║]])
  334. gpu.set(1,18,[[║     │                  │                  │    ╠═════════╣]])
  335. gpu.set(1,19,[[║     │                  │                  │    ║         ║]])
  336. gpu.set(1,20,[[║     │                  │                  │    ║         ║]])
  337. gpu.set(1,21,[[║     │                  │                  │    ║         ║]])
  338. gpu.set(1,22,[[║     │                  │                  │    ║         ║]])
  339. gpu.set(1,23,[[║     └──────────────────┴────────╥─────────┘    ║         ║]])
  340. gpu.set(1,24,[[║                                 ║              ║         ║]])
  341. gpu.set(1,25,[[║                    ║            ║              ║         ║]])
  342. gpu.set(1,26,[[║            <═════  ║  ══════════╝              ║         ║]])
  343. gpu.set(1,27,[[║                    ║                           ║         ║]])
  344. gpu.set(1,28,[[╠════════════════════════════════════════════════╣         ║]])
  345. gpu.set(1,29,[[║                                                ║         ║]])
  346. gpu.set(1,30,[[╚════════════════════════════════════════════════╩═════════╝]])
  347.  
  348. fonction_auto(stock_item,item_max,23,5,3,1)
  349. fonction_auto(volume,volume_max,20,25,-1,2)
  350. on_off(51,6,3)
  351. on_auto_off(51,11,1)
  352. on_auto_off(51,16,2)
  353. init = 1
  354.  
  355. local function onTouch(_,address,x,y,clic,pseudo)
  356.   local tclic
  357.   if clic == 0  then
  358.     tclic = "Clic gauche"
  359.   elseif clic == 1 then
  360.     tclic = "Clic droit"
  361.   else
  362.     tclic = "Clic inconnu"
  363.   end
  364.   gpu.set(4,2,tclic.." "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
  365.  
  366.   if x~=1 and y~=1 then
  367.     if y==6 then
  368.       if x>50 and x<55 then
  369.         if tableau_etat_sortie[3] == false then
  370.           tableau_etat_sortie[3] = true
  371.         end
  372.       elseif x>54 and x<60 then
  373.         if tableau_etat_sortie[3] == true then
  374.           tableau_etat_sortie[3] = false
  375.         end
  376.       end
  377.       on_off(51,6,3)
  378.     elseif x>50 and x<60 and y>10 and y<13 then
  379.       if x>50 and x<55 and y==11 then
  380.         if tableau_mode_auto[1] ~= 3 then
  381.           tableau_mode_auto[1] = 3
  382.         end
  383.       elseif x>54 and x<60 and y==11 then
  384.         if tableau_mode_auto[1] ~= 1 then
  385.           tableau_mode_auto[1] = 1
  386.         end
  387.       elseif x>53 and x<58 and y==12 then
  388.         if tableau_mode_auto[1] ~= 2 then
  389.           tableau_mode_auto[1] = 2
  390.         end
  391.       end
  392.       on_auto_off(51,11,1)
  393.     elseif x>50 and x<60 and y>15 and y<18 then
  394.       if x>50 and x<55 and y==16 then
  395.         if tableau_mode_auto[2] ~= 3 then
  396.           tableau_mode_auto[2] = 3
  397.         end
  398.       elseif x>54 and x<60 and y==16 then
  399.         if tableau_mode_auto[2] ~= 1 then
  400.           tableau_mode_auto[2] = 1
  401.         end
  402.       elseif x>53 and x<58 and y==17 then
  403.         if tableau_mode_auto[2] ~= 2 then
  404.           tableau_mode_auto[2] = 2
  405.         end
  406.       end
  407.       on_auto_off(51,16,2)
  408.     end
  409.  
  410.   elseif x==1 and y==1 then
  411.     computer.pushSignal("quit")
  412.     gpu.fill(1,1,160,50," ")
  413.     term.setCursor(1,1)
  414.     return false
  415.   end
  416. end
  417.  
  418. local function onTimer(_,timer)
  419.   double_barre(8,10,13,18)
  420.   fonction_auto(stock_item,item_max,23,5,3,1)
  421.   fonction_auto(volume,volume_max,20,25,-1,2)
  422.   send(port_modem)
  423.   return true
  424. end
  425.  
  426. event.listen("touch",onTouch)
  427. local timer = event.timer(.1,onTimer,math.huge)
  428. event.pull("quit")
  429. event.cancel(timer)
  430. rs.setBundledOutput(top,0,255)
  431. rs.setBundledOutput(top,1,255)
  432. rs.setBundledOutput(top,2,255)
  433. event.ignore("touch",onTouch)
  434. gpu.fill(1,1,160,50," ")
  435. gpu.setResolution(160,40)
  436.  
  437. --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