krezi1212123

BigReactor

Apr 4th, 2016 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local term = require("term")
  2. local component = require("component")
  3. local computer = require("computer")
  4. local math = require("math")
  5. local event = require("event")
  6. local string = require("string")
  7. local unicode = require("unicode")
  8. local os = require("os")
  9. local fs = require('filesystem')
  10.  
  11. local gpu = component.gpu
  12. gpu.setResolution(100,50)
  13. br = component.getPrimary("br_reactor")
  14.  
  15. local boucle2 = 0
  16. local a = 80
  17. local b = 1
  18. local lvl
  19. local val = 0
  20. local rod = 1
  21. local rodmax = br.getNumberOfControlRods()
  22. local maxcon = rodmax*100
  23. local termset = 0
  24. local stco=false
  25. local kp=1
  26. local ki=0.1
  27. local int=0
  28.  
  29. local function claim(x,a,b)
  30. if x<a then
  31. return a
  32. end
  33. if x>b then
  34. return b
  35. end
  36. return x
  37. end
  38.  
  39. local function rodcon(num,rod)
  40. local x= (num-num%100)/100
  41. local y=num%100
  42. if rod<x then
  43. return 100
  44. elseif rod>x then
  45. return 0
  46. elseif rod==x then
  47. return y
  48. end
  49. end
  50.  
  51. local function rodcontrol()
  52. local err = termset-br.getCasingTemperature()
  53. local pid_p = err*kp
  54. local pid_i = int*ki
  55. local pid = claim((pid_p+pid_i),0,maxcon)
  56. int=claim((int+err),0,maxcon/ki)
  57. pid=math.floor(pid)
  58. pid= ( (pid - 0) / (maxcon - 0) ) * (0 - maxcon) + maxcon
  59. for i=1,maxcon,1 do
  60. br.setControlRodLevel(i,rodcon(pid,i))
  61. end
  62. gpu.set(22,6, string.format("%10d", pid))
  63. gpu.set(22,11,string.format("%10d",int))
  64. end
  65.  
  66. local function setColor(bg,fg)
  67.   gpu.setBackground(bg)
  68.   gpu.setForeground(fg)
  69. end
  70.  
  71. local function loadbar(x,y,width,cur,text,bg,fg)
  72.   local raw = " " .. text ..string.rep(" ", width - unicode.len(text) - 2) .. " "
  73.   local oldbg = gpu.setBackground(bg)
  74.   local oldfg = gpu.setForeground(fg)
  75.   gpu.set(x,y,unicode.sub(raw,1,cur))
  76.   gpu.setBackground(oldbg)
  77.   gpu.setForeground(oldfg)
  78.   gpu.set(x+cur,y,unicode.sub(raw,cur+1,width))
  79. end
  80.  
  81. local function hexa(dec)
  82. local B,K,OUT,I,D = 16,"0123456789ABCDEF","",0
  83.   if dec <= 0 then
  84.     OUT = "00"
  85.   elseif dec > 0 and dec < 16 then
  86.     D = dec + 1
  87.     OUT = "0"..string.sub(K,D,D)
  88.   elseif dec > 16 and dec < 256 then
  89.     V1 = math.modf(dec/B)+1
  90.     V2 = dec-V1*16
  91.     OUT = string.sub(K,V1,V1)..string.sub(K,V2,V2)
  92.   elseif dec >= 255 then
  93.     OUT = "FF"
  94.   end
  95.   return OUT
  96. end
  97.  
  98. local function round(num, dec)
  99.   local mult = 10 ^ (dec or 0)
  100.   return math.floor(num * mult + 0.5) / mult
  101. end
  102.  
  103. local function bargraph(x,y,length,am,cap,na,col,colpol)
  104.   local amount = am
  105.   local capacity = cap
  106.   local pct = amount / capacity
  107.   local cur = math.floor(pct * length)
  108.   local color = col
  109.   local color2 = colpol
  110.   local name = na
  111.   local textfrac = string.format("%s / %s", amount, capacity)
  112.   local textpct = string.format("%.02f%%", pct*100)
  113.   local text = textfrac .. string.rep(" ", length - string.len(textfrac) - string.len(textpct) - 6) .. "   " .. textpct .. " "
  114.   local text1 = "              Уровень заполнения : ("..name..")"
  115.   loadbar(x,y,length,cur,text1,color,color2)
  116.   loadbar(x,y+1,length,cur,text,color,color2)
  117. end
  118.  
  119. function drawbars()
  120.   amFuel = br.getFuelAmount()
  121.   capFuel = br.getFuelAmountMax()
  122.   naFuel = "ТОПЛИВО - Yellorium"
  123.   colFuel = 0xFFFF00
  124.   colpolFuel = 0x0000FF
  125.   bargraph(2,22,98,amFuel,capFuel,naFuel,colFuel,colpolFuel)
  126.   amWaste = br.getWasteAmount()
  127.   capWaste = 64000
  128.   naWaste = "ОТХОДЫ - Cyanite"
  129.   colWaste = 0x00FFFF
  130.   colpolWaste = 0xFF00FF
  131.   bargraph(2,25,98,amWaste,capWaste,naWaste,colWaste,colpolWaste)
  132.   amSteam = br.getHotFluidAmount()
  133.   capSteam = br.getHotFluidAmountMax()
  134.   naSteam = "ПАР"
  135.   colSteam = 0x8F8F8F
  136.   colpolSteam = 0xFFFF00
  137.   bargraph(2,28,98,amSteam,capSteam,naSteam,colSteam,colpolSteam)
  138.   amWater = br.getCoolantAmount()
  139.   capWater = br.getCoolantAmountMax()
  140.   naWater = "ВОДА"
  141.   colWater = 0x71b6cb
  142.   colpolWater = 0x0000FF
  143.   bargraph(2,31,98,amWater,capWater,naWater,colWater,colpolWater)
  144.   consot = br.getFuelConsumedLastTick()
  145.   consos = consot * 20
  146.   consoh = consos * 3.6
  147.   consoj = consoh * 24  
  148.   gpu.set(2,35,string.format("% 1.03f",consot).."mb/t =>"..string.format("% 1.02f",consos).."mb/s =>"..string.format("% 3.02f",consoh).."b/h =>"..string.format("% 5.02f",consoj).."b/d")
  149.   Tcore = br.getCasingTemperature()
  150.   Tfuel = br.getFuelTemperature()
  151.   Reactivity = br.getFuelReactivity()
  152.   Prod = br.getHotFluidProducedLastTick()
  153.   gpu.set(35,38,string.format("% 4.2f",Tcore))
  154.   gpu.set(35,40,string.format("% 4.2f",Tfuel))
  155.   gpu.set(35,42,string.format("% 4.2f",Reactivity))
  156.   gpu.set(35,44,string.format("% 4.2f",Prod))
  157.   if stco then
  158.     rodcontrol()
  159.   end
  160. end
  161.  
  162. local function waste()
  163. local waste = br.getWasteAmount()
  164.   if waste > 48000 then
  165.     br.doEjectWaste()
  166.   end
  167. end
  168.  
  169. local function marche(x,y)
  170.   gpu.set(x,y,"              ")
  171. gpu.set(x,y+1,"   ВКЛЮЧИТЬ   ")
  172. gpu.set(x,y+2,"              ")
  173. end
  174.  
  175. local function arret(x,y)
  176.   gpu.set(x,y,"              ")
  177. gpu.set(x,y+1,"   ВЫКЛЮЧИТЬ  ")
  178. gpu.set(x,y+2,"              ")
  179. end
  180.  
  181. local function start()
  182. local ON = br.getActive()
  183.   if ON == true then
  184.     setColor(0x00FF00,0x0)
  185.     marche(60,11)
  186.     setColor(0x0,0xFF0000)
  187.     arret(60,15)
  188.     setColor(0x0,0xFFFFFF)
  189.   elseif ON == false then
  190.     setColor(0x0,0x00FF00)
  191.     marche(60,11)
  192.     setColor(0xFF0000,0xFFFFFF)
  193.     arret(60,15)
  194.     setColor(0x0,0xFFFFFF)
  195.   end
  196. end
  197.  
  198. local function scon()
  199.   if stco == true then
  200.     setColor(0x00FF00,0x0)
  201.     marche(5,46)
  202.     setColor(0x0,0xFF0000)
  203.     arret(27,46)
  204.     setColor(0x0,0xFFFFFF)
  205.   elseif stco == false then
  206.     setColor(0x0,0x00FF00)
  207.     marche(5,46)
  208.     setColor(0xFF0000,0xFFFFFF)
  209.     arret(27,46)
  210.     setColor(0x0,0xFFFFFF)
  211.   end
  212. end
  213.  
  214. term.clear()
  215.  gpu.set(1,1,"╔══════════════════════════════════════════════════════════════════════════════════════════════════╗")
  216.  gpu.set(1,2,"║                                                                                                  ║")
  217.  gpu.set(1,3,"║   ███████████████████████████████████████████████       ███████████████████                      ║")
  218.  gpu.set(1,4,"║   ██         установка температуры             ██       █ Ручной контроль █                      ║")
  219.  gpu.set(1,5,"║   ██                                           ██       █       всех      █                      ║")
  220.  gpu.set(1,6,"║   ██                                           ██       █     стержней    █                      ║")
  221.  gpu.set(1,7,"║   ██   +100                         -100       ██       █                 █                      ║")
  222.  gpu.set(1,8,"║   ██    +10                         -10        ██       ███████████████████                      ║")
  223.  gpu.set(1,9,"║   ██     +1                         -1         ██                                                ║")
  224. gpu.set(1,10,"║   ██                                           ██                                                ║")
  225. gpu.set(1,11,"║   ██                                           ██        █████████████                           ║")
  226. gpu.set(1,12,"║   ██                                           ██        █ВКЛЮЧИТЬ  ██                           ║")
  227. gpu.set(1,13,"║   ██                                           ██        █████████████                           ║")
  228. gpu.set(1,14,"║   ██                                           ██                                                ║")
  229. gpu.set(1,15,"║   ██                                           ██        █████████████                           ║")
  230. gpu.set(1,16,"║   ██                                           ██        █ВЫКЛЮЧИТЬ ██                           ║")
  231. gpu.set(1,17,"║   ███████████████████████████████████████████████        █████████████                           ║")
  232. gpu.set(1,18,"║                                                                                                  ║")
  233. gpu.set(1,19,"╠══════════════════════════════════════════════════════════════════════════════════════════════════╣")
  234. gpu.set(1,20,"║                                                                                                  ║")
  235. gpu.set(1,21,"╠══════════════════════════════════════════════════════════════════════════════════════════════════╣")
  236. gpu.set(1,22,"║                                                                                                  ║")
  237. gpu.set(1,23,"║                                                                                                  ║")
  238. gpu.set(1,24,"╠══════════════════════════════════════════════════════════════════════════════════════════════════╣")
  239. gpu.set(1,25,"║                                                                                                  ║")
  240. gpu.set(1,26,"║                                                                                                  ║")
  241. gpu.set(1,27,"╠══════════════════════════════════════════════════════════════════════════════════════════════════╣")
  242. gpu.set(1,28,"║                                                                                                  ║")
  243. gpu.set(1,29,"║                                                                                                  ║")
  244. gpu.set(1,30,"╠══════════════════════════════════════════════════════════════════════════════════════════════════╣")
  245. gpu.set(1,31,"║                                                                                                  ║")
  246. gpu.set(1,32,"║                                                                                                  ║")
  247. gpu.set(1,33,"╠════════════════════════════════════════════════╦═════════════════════════════════════════════════╣")
  248. gpu.set(1,34,"║ РАСХОД ТОПЛИВА                                 ║  MiliBukket/second => mb/s   Bukket/Hour => b/h ║")
  249. gpu.set(1,35,"║                                                ║  MiliBukket/tick   => mb/t   Bukket/Day  => b/d ║")
  250. gpu.set(1,36,"╠════════════════════════════════════════════════╩══════════════════════╦══════════════════════════╣")
  251. gpu.set(1,37,"║                                                                       ║                          ║")
  252. gpu.set(1,38,"║    ТЕМПЕРАТУРА РЕАКТОРА       :           °C                          ║                          ║")
  253. gpu.set(1,39,"║                                                                       ║    ██████████████████    ║")
  254. gpu.set(1,40,"║    ТЕМПЕРАТУРА ТОПЛИВА        :           °C                          ║    ██   ИЗВЛЕЧЬ    ██    ║")
  255. gpu.set(1,41,"║                                                                       ║    ██   CYANITE    ██    ║")
  256. gpu.set(1,42,"║    РАДИОАКТИВНОСТЬ ТОПЛИВА    :           %                           ║    ██████████████████    ║")
  257. gpu.set(1,43,"║                                                                       ║                          ║")
  258. gpu.set(1,44,"║    ПЕРЕРАБОТКА ПАРА           :           mb/t                        ║    ██████████████████    ║")
  259. gpu.set(1,45,"╠═══════════════════════════════════════════════════════════════════════╣    ██   ИЗВЛЕЧЬ    ██    ║")
  260. gpu.set(1,46,"║    █████████████         █████████████       АВТО ТКМПЕРАТУРА         ║    ██  YELLORIUM   ██    ║")
  261. gpu.set(1,47,"║    █ВКЛЮЧИТЬ  ██         █ВЫКЛЮЧИТЬ ██                                ║    ██████████████████    ║")
  262. gpu.set(1,48,"║    █████████████         █████████████                                ║                          ║")
  263. gpu.set(1,49,"║                                                                       ║                          ║")
  264. gpu.set(1,50,"╚═══════════════════════════════════════════════════════════════════════╩══════════════════════════╝")
  265.  
  266. start()
  267. scon()
  268.  
  269. local function onTouch(event,adress,x,y,clic,pseudo)
  270.   local tclic
  271.   if clic == 0  then
  272.     tclic = "НАЖИТО ЛКМ"
  273.   elseif clic == 1 then
  274.     tclic = "НАЖИТО ПКМ"
  275.   else
  276.     tclic = "НАЖИТО НЕИЗВЕСТНО"
  277.   end
  278.   gpu.set(6,20,"                "..tclic.." . ИГРОКОМ: "..pseudo.." ( X : "..string.format("% 3s",x).." Y: "..string.format("% 3s",y)..")")
  279.  
  280.   if x==1 and y==1 then
  281.       computer.pushSignal("quit")
  282.       term.setCursor(1,1)
  283.       return false
  284.     elseif x > 77 and x < 96 and y > 38 and y < 43 then
  285.       br.doEjectWaste()
  286.     elseif x > 77 and x < 96 and y > 43 and y < 48 then
  287.       br.doEjectFuel()  
  288.     elseif x > 58 and x < 73 and y > 10 and y < 14 then
  289.       br.setActive(true)
  290.       start()
  291.     elseif x >= 5 and x <= 17 and y >= 46 and y <= 48 then
  292.      stco=true
  293.       scon()
  294.     elseif x >= 27 and x <= 39 and y >= 46 and y <= 48 then
  295.       stco=false
  296.       scon()
  297.     elseif x >= 58 and x <= 73 and y >= 14 and y <= 18 then
  298.       br.setActive(false)
  299.       start()
  300.      gpu.set(22,5, string.format("%10d", termset))
  301.      elseif x >= 9 and x <= 13 and y == 9 then
  302.       if termset >= 2999 then
  303.        termset = 3000
  304.       else
  305.        termset = termset + 1
  306.       end
  307.      gpu.set(22,5, string.format("%10d", termset))
  308.      elseif x >= 9 and x <= 13 and y == 8 then
  309.       if termset >= 2990 then
  310.        termset = 3000
  311.       else
  312.        termset = termset + 10
  313.       end
  314.      gpu.set(22,5, string.format("%10d", termset))
  315.      elseif x >=9 and x <= 13 and y == 7 then
  316.       if termset >= 2900 then
  317.        termset = 3000
  318.       else
  319.        termset = termset + 100
  320.       end
  321.     gpu.set(22,5, string.format("%10d", termset))
  322.      elseif x >= 38 and x <= 43 and y == 9 then
  323.       if termset <= 1 then
  324.        termset = 0
  325.       else
  326.        termset = termset - 1
  327.       end
  328.      gpu.set(22,5, string.format("%10d", termset))
  329.      elseif x >= 38 and x <= 43 and y == 8 then
  330.       if termset <= 10 then
  331.        termset = 0
  332.       else
  333.        termset = termset - 10
  334.       end
  335.      gpu.set(22,5, string.format("%10d", termset))
  336.      elseif x >= 38 and x <= 43 and y == 7 then
  337.       if termset <= 100 then
  338.        termset = 0
  339.       else
  340.        termset = termset - 100
  341.       end  
  342.     gpu.set(22,5, string.format("%10d", termset))
  343.   end
  344. end
  345.  
  346. local function onTimer(_,timer)
  347.   waste()
  348.   drawbars()
  349.   return true
  350. end
  351.  
  352. event.listen("touch",onTouch)
  353. local timer = event.timer(0,onTimer,math.huge)
  354. event.pull("quit")
  355. event.cancel(timer)
  356. event.ignore("touch",onTouch)
  357. component.gpu.setResolution(160,50)
  358. term.clear()
Add Comment
Please, Sign In to add comment