Advertisement
sshikamaru

Contrôle turbine

Mar 25th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.14 KB | None | 0 0
  1. --Requires
  2. local term = require("term")
  3. local component = require("component")
  4. local computer = require("computer")
  5. local math = require("math")
  6. local event = require("event")
  7. local string = require("string")
  8. local unicode = require("unicode")
  9. local os = require("os")
  10. local fs = require('filesystem')
  11.  
  12. local gpu = component.gpu
  13. gpu.setResolution(101,50)
  14.  
  15. --récupération des turbines
  16. local i = 0
  17. local bit_marche = {}
  18. local bit_engage = {}
  19. local tableau_bits = {bit_marche, bit_engage}
  20. local tableau_turbine = {}
  21. for address,componentType in pairs(component.list("br_turbine")) do
  22.   i = i + 1
  23.   tableau_turbine[i] = component.proxy(address)
  24.   bit_marche[i] = tableau_turbine[i].getActive()
  25.   bit_engage[i] = tableau_turbine[i].getInductorEngaged()
  26. end
  27.  
  28. --Variables
  29.  
  30. --Functions
  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 >=1000000 or qty <= -1000000 then
  38.     return string.format("%.03f MRF/t",qty/1000000)
  39.   elseif qty >=1000 or qty <= -1000 then
  40.     return string.format("%.03f kRF/t",qty/1000)
  41.   else
  42.     return string.format("%.03f RF/t ",qty)
  43.   end
  44. end
  45.  
  46. local function loadbar(x,y,width,cur,text,bg,fg)
  47.   local raw = " " .. text ..string.rep(" ", width - unicode.len(text) - 2) .. " "
  48.   local oldbg = gpu.setBackground(bg)
  49.   local oldfg = gpu.setForeground(fg)
  50.   gpu.set(x,y,unicode.sub(raw,1,cur))
  51.   gpu.setBackground(oldbg)
  52.   gpu.setForeground(oldfg)
  53.   gpu.set(x+cur,y,unicode.sub(raw,cur+1,width))
  54. end
  55.  
  56. local function bar_horizontale(x,y,length,val,valmax,texte,color1,color2,rfmb)
  57.   local pct = val/valmax
  58.   local cur = math.floor(pct * length)
  59.   local textfrac = texte..string.format(" : %04d / %04d "..rfmb, val, valmax)
  60.   local textpct = string.format("%.02f%%", pct*100)
  61.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  62.   loadbar(x,y,length,cur,text,color1,color2)
  63. end
  64.  
  65. local function bar_horizontalerf(x,y,length,val,valmax,texte,color1,color2)
  66.   local pct = val/valmax
  67.   local cur = math.floor(pct * length)
  68.   local textfrac = texte.." : "..unitrf(val).." / "..unitrf(valmax)
  69.   local textpct = string.format("%.02f%%", pct*100)
  70.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 2) .. textpct
  71.   loadbar(x,y,length,cur,text,color1,color2)
  72. end
  73.  
  74. local function marche(x,y,etat)
  75.   if etat == true then
  76.     setColor(0x0, 0x00FF00)
  77.     gpu.set(x,y,"MARCHE")
  78.   else
  79.     setColor(0x0, 0xFF0000)
  80.     gpu.set(x,y," ARRÊT")    
  81.   end
  82.   setColor(0x0, 0xFFFFFF)
  83. end
  84.  
  85. local function active(x,y,etat)
  86.   if etat == true then
  87.     setColor(0x00FF00, 0x0)
  88.     gpu.set(x,y," Activée  ")
  89.   else
  90.     setColor(0xFF0000, 0x0)
  91.     gpu.set(x,y,"Désactivée")    
  92.   end
  93.   setColor(0x0, 0xFFFFFF)
  94. end
  95.  
  96. local function engage(x,y,etat)
  97.   if etat == false then
  98.     setColor(0x0000FF, 0x00FFFF)
  99.     gpu.set(x,y,"Désengagé")
  100.   else
  101.     setColor(0x00FFFF, 0x0000FF)
  102.     gpu.set(x,y," Engagé  ")  
  103.   end
  104.   setColor(0x0, 0xFFFFFF)  
  105. end
  106.  
  107. local function turbine(x,y,tableau,n)
  108.   tableau.setActive(bit_marche[n])
  109.   tableau.setInductorEngaged(bit_engage[n])
  110.   marche(x+1,y,bit_marche[n])
  111.   active(x+21,y,tableau.getActive())
  112.   gpu.set(x+19,y,tostring(n))
  113.   engage(x+38,y,tableau.getInductorEngaged())
  114.   gpu.set(x+4,y+1,string.format("%04s",tableau.getFluidFlowRate()))
  115.   gpu.set(x+36,y+1,unitrf(tableau.getEnergyProducedLastTick()))
  116.   bar_horizontale(x,y+3,49,tableau.getInputAmount(),tableau.getFluidAmountMax(),"Vapeur",0x8F8F8F,0xFFFF00,"mB/t")
  117.   bar_horizontale(x,y+4,49,tableau.getRotorSpeed(),2200,"Vitesse du rotor",0xff9127,0xFFFFFF-0xff9127,"tr/min")
  118.   bar_horizontalerf(x,y+5,49,tableau.getEnergyStored(),1000000,"Energie",0x00FFFF,0xFF0000)
  119. end
  120.  
  121. local function control()
  122.   local vx
  123.   local vy
  124.   local nturbine = 0
  125.   for nturbine = 1, 8 do
  126.     if math.floor(nturbine/2)*2 ~= nturbine then
  127.       vx = 2
  128.       vy = (nturbine - 2) * 3.5 + 7.5
  129.     else
  130.       vx = 52
  131.       vy = (nturbine - 2) * 3.5 + 4
  132.     end
  133.     turbine(vx,vy,tableau_turbine[nturbine],nturbine)
  134.   end
  135. end
  136.  
  137. local function command(tableau,n)
  138.     if tableau_bits[tableau][n] == true then
  139.       tableau_bits[tableau][n] = false
  140.     elseif tableau_bits[tableau][n] == false then
  141.       tableau_bits[tableau][n] = true
  142.     end
  143. end
  144.  
  145. --trace
  146. term.clear()
  147.  gpu.set(1,1,"╔═══════════════════════════════════════════════════════════════════════════════════════════════════╗")
  148.  gpu.set(1,2,"║                                                                                                   ║")
  149.  gpu.set(1,3,"╠═════════════════════════════════════════════════╦═════════════════════════════════════════════════╣")
  150.  gpu.set(1,4,"║ marche  Turbine n°X désactivée Rotor désengagé  ║ marche  Turbine n°X désactivée Rotor désengagé  ║")
  151.  gpu.set(1,5,"║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║")
  152.  gpu.set(1,6,"╟-------------------------------------------------╫-------------------------------------------------╢")
  153.  gpu.set(1,7,"║ Rotor Speed                                     ║ Rotor Speed                                     ║")
  154.  gpu.set(1,8,"║ Steam                                           ║ Steam                                           ║")
  155.  gpu.set(1,9,"║ Energy                                          ║ Energy                                          ║")
  156. gpu.set(1,10,"╠═════════════════════════════════════════════════╬═════════════════════════════════════════════════╣")
  157. gpu.set(1,11,"║ marche  Turbine n°X désactivée Rotor désengagé  ║ marche  Turbine n°X désactivée Rotor désengagé  ║")
  158. gpu.set(1,12,"║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║")
  159. gpu.set(1,13,"╟-------------------------------------------------╫-------------------------------------------------╢")
  160. gpu.set(1,14,"║ Rotor Speed                                     ║ Rotor Speed                                     ║")
  161. gpu.set(1,15,"║ Steam                                           ║ Steam                                           ║")
  162. gpu.set(1,16,"║ Energy                                          ║ Energy                                          ║")
  163. gpu.set(1,17,"╠═════════════════════════════════════════════════╬═════════════════════════════════════════════════╣")
  164. gpu.set(1,18,"║ marche  Turbine n°X désactivée Rotor désengagé  ║ marche  Turbine n°X désactivée Rotor désengagé  ║")
  165. gpu.set(1,19,"║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║")
  166. gpu.set(1,20,"╟-------------------------------------------------╫-------------------------------------------------╢")
  167. gpu.set(1,21,"║ Rotor Speed                                     ║ Rotor Speed                                     ║")
  168. gpu.set(1,22,"║ Steam                                           ║ Steam                                           ║")
  169. gpu.set(1,23,"║ Energy                                          ║ Energy                                          ║")
  170. gpu.set(1,24,"╠═════════════════════════════════════════════════╬═════════════════════════════════════════════════╣")
  171. gpu.set(1,25,"║ marche  Turbine n°X désactivée Rotor désengagé  ║ marche  Turbine n°X désactivée Rotor désengagé  ║")
  172. gpu.set(1,26,"║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║ Qe XXXX mB / Production énergie :  XX.XXX Krf/t ║")
  173. gpu.set(1,27,"╟-------------------------------------------------╫-------------------------------------------------╢")
  174. gpu.set(1,28,"║ Rotor Speed                                     ║ Rotor Speed                                     ║")
  175. gpu.set(1,29,"║ Steam                                           ║ Steam                                           ║")
  176. gpu.set(1,30,"║ Energy                                          ║ Energy                                          ║")
  177. gpu.set(1,31,"╠═════════════════════════════════════════════════╬═════════════════════════════════════════════════╣")
  178. gpu.set(1,32,"║                                                 ║                                                 ║")
  179. gpu.set(1,33,"║                                                 ║                                                 ║")
  180. gpu.set(1,34,"║                                                 ║                                                 ║")
  181. gpu.set(1,35,"║                                                 ║                                                 ║")
  182. gpu.set(1,36,"║                                                 ║                                                 ║")
  183. gpu.set(1,37,"║                                                 ║                                                 ║")
  184. gpu.set(1,38,"║                                                 ║                                                 ║")
  185. gpu.set(1,39,"║                                                 ║                                                 ║")
  186. gpu.set(1,40,"║                                                 ║                                                 ║")
  187. gpu.set(1,41,"║                                                 ║                                                 ║")
  188. gpu.set(1,42,"║                                                 ║                                                 ║")
  189. gpu.set(1,43,"║                                                 ║                                                 ║")
  190. gpu.set(1,44,"║                                                 ║                                                 ║")
  191. gpu.set(1,45,"║                                                 ║                                                 ║")
  192. gpu.set(1,46,"║                                                 ║                                                 ║")
  193. gpu.set(1,47,"║                                                 ║                                                 ║")
  194. gpu.set(1,48,"║                                                 ║                                                 ║")
  195. gpu.set(1,49,"║                                                 ║                                                 ║")
  196. gpu.set(1,50,"╚═════════════════════════════════════════════════╩═════════════════════════════════════════════════╝")
  197.  
  198. --Main
  199. local function onTouch(event,adress,x,y,clic,pseudo)
  200.   local tclic
  201.   if clic == 0  then
  202.     tclic = "Clic gauche"
  203.   elseif clic == 1 then
  204.     tclic = "Clic droit"
  205.   else
  206.     tclic = "Clic inconnu"
  207.   end
  208.   gpu.set(2,2,"                "..tclic.." de la part de "..pseudo.." / X : "..string.format("% 3s",x).." / Y : "..string.format("% 3s",y))
  209.  
  210.   if x==1 and y==1 then
  211.       computer.pushSignal("quit")
  212.       term.setCursor(1,1)
  213.       return false
  214.   elseif x>2 and x<9 and (y==4 or y==11 or y==18 or y==25) then
  215.     local n = ((y - 7.5) / 3.5) + 2
  216.     command(1,n)
  217.   elseif x>52 and x<59 and (y==4 or y==11 or y==18 or y==25) then
  218.     local n = ((y - 4) / 3.5) + 2
  219.     command(1,n)
  220.   elseif x>39 and x<49 and (y==4 or y==11 or y==18 or y==25) then
  221.     local n = ((y - 7.5) / 3.5) + 2
  222.     command(2,n)
  223.   elseif x>89 and x<99 and (y==4 or y==11 or y==18 or y==25) then
  224.     local n = ((y - 4) / 3.5) + 2
  225.     command(2,n)
  226.   elseif 1 == 0 then
  227.   end
  228. end
  229.  
  230. local function onTimer(_,timer)
  231.   control()
  232.   return true
  233. end
  234.  
  235. event.listen("touch",onTouch)
  236. local timer = event.timer(0,onTimer,math.huge)
  237. event.pull("quit")
  238. event.cancel(timer)
  239. event.ignore("touch",onTouch)
  240. component.gpu.setResolution(160,50)
  241. term.clear()
  242.  
  243. --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