resaloli

Reactor TND

Oct 17th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. mon = peripheral.wrap("left")
  2. reactor = peripheral.wrap("BigReactors-Reactor_0")
  3. capacitor = peripheral.wrap("tile_blockcapacitorbank_name_0")
  4.  
  5. local workingMode = 1
  6. local button = {}
  7.  
  8. mon.setBackgroundColor(colors.black)
  9. mon.setTextScale(0.5)
  10. mon.clear()
  11.  
  12. local reactorInfo = {}
  13. function getReactorInfo()
  14.     reactorInfo["active"] = reactor.getActive()
  15.     reactorInfo["nRods"] = reactor.getNumberOfControlRods()
  16.     reactorInfo["storedEnergy"] = reactor.getEnergyStored()
  17.     reactorInfo["internalTemp"] = reactor.getFuelTemperature()
  18.     reactorInfo["externalTemp"] = reactor.getCasingTemperature()
  19.     reactorInfo["fuel"] = reactor.getFuelAmount()
  20.     reactorInfo["waste"] = reactor.getWasteAmount()
  21.     reactorInfo["fuelmax"] = reactor.getFuelAmountMax()
  22.     reactorInfo["controlRodLvl"] = reactor.getControlRodLevel(1)
  23.     reactorInfo["producedLastTick"] = reactor.getEnergyProducedLastTick()
  24. end
  25.  
  26. local capacitorInfo = {}
  27.  function getCapacitorInfo()
  28.     capacitorInfo["storedEnergy"] = capacitor.getEnergyStored()
  29.     capacitorInfo["maxStoredEnergy"] = capacitor.getMaxEnergyStored()
  30.  end
  31.  
  32. function center(str,width)
  33.     while string.len(str) < width do
  34.         str = " "..str.." "
  35.     end
  36.     return str
  37. end
  38.  
  39. function round(num,dec)
  40.     local mult = 10^(dec or 0)
  41.     return math.floor(num + mult + 0.5)/mult
  42. end
  43.  
  44. function Percentage(toCalculate,maxToCalculate)
  45.     return round((toCalculate*100)/maxToCalculate,2)*100-100
  46. end
  47. function autoControl()
  48.     local storedEnergy = capacitorInfo["storedEnergy"]
  49.     local maxStoredEnergy = capacitorInfo["maxStoredEnergy"]
  50.     if Percentage(storedEnergy,maxStoredEnergy) < 25.00 then
  51.         reactor.setActive(true)
  52.     elseif Percentage(storedEnergy,maxStoredEnergy) > 80.00 then
  53.         reactor.setActive(false)
  54.     end
  55. end
  56.  
  57. function drawBoxes(xmin,ymin,width,height)
  58.     mon.setBackgroundColor(colors.white)
  59.     mon.setCursorPos(xmin,ymin)
  60.     mon.write(string.rep(" ",width))
  61.     mon.setCursorPos(xmin,ymin+height)
  62.     mon.write(string.rep(" ",width))
  63.    
  64.     for i = 0, height do
  65.         mon.setCursorPos(xmin,ymin+i)
  66.         mon.write(string.rep(" ",2))
  67.     end
  68.    
  69.     for i = 0, height do
  70.         mon.setCursorPos(xmin+width,ymin+i)
  71.         mon.write(string.rep(" ",2))
  72.     end
  73.     mon.setBackgroundColor(colors.black)
  74. end
  75.  
  76. function drawRod(x,y,per)
  77.     for i = 0,10 do
  78.         mon.setCursorPos(x,y+i)
  79.         mon.setBackgroundColor(colors.yellow)
  80.         mon.write(string.rep(" ",3))
  81.     end
  82.     if per ~= nil then
  83.         for i = 0, math.floor((per/100)*10) do
  84.             mon.setCursorPos(x+1,y+i)
  85.             mon.setBackgroundColor(colors.blue)
  86.             mon.write(" ")
  87.         end
  88.     end
  89.     mon.setBackgroundColor(colors.black)
  90. end
  91.  
  92. function drawGrafic(x,y,per)
  93.     mon.setBackgroundColor(colors.red)
  94.     for i = 0,3 do
  95.         mon.setCursorPos(x,y+i)
  96.         mon.write(string.rep(" ",35))
  97.     end
  98.     mon.setBackgroundColor(colors.green)
  99.     for i = 0, 3 do
  100.         mon.setCursorPos(x,y+i)
  101.         mon.write(string.rep(" ",math.floor((per/100)*35)))
  102.     end
  103.     mon.setBackgroundColor(colors.black)
  104. end
  105.  
  106. function drawButtons()
  107.     for name,data in pairs(button) do
  108.         if data["state"] == 1 then
  109.         mon.setBackgroundColor(data["onColor"])
  110.         else
  111.         mon.setBackgroundColor(data["offColor"])
  112.         end
  113.         mon.setCursorPos(data["xmin"],data["ymin"])
  114.         for i = 0, data["height"] do
  115.             mon.setCursorPos(data["xmin"],data["ymin"]+i)
  116.             mon.write(string.rep(" ",data["width"]))
  117.         end
  118.         mon.setCursorPos(data["xmin"],data["ymin"]+(data["height"]/2))
  119.         mon.write(center(data["display"],data["width"]))
  120.     end
  121.     mon.setBackgroundColor(colors.black)
  122. end
  123.  
  124. function drawText(x,y,str,color)
  125.     color = color or color.white
  126.     mon.setBackgroundColor(colors.black)
  127.     mon.setCursorPos(x,y)
  128.     mon.setTextColor(color)
  129.     mon.write(str)
  130. end
  131.  
  132. function drawGui()
  133.     mon.clear()
  134. --Boxes
  135. --Reactor Info Box
  136.     drawBoxes(3,2,45,20)
  137. --Activate Controller
  138.     drawBoxes(3,25,45,12)
  139. --Control Rod Controller
  140.     drawBoxes(53,2,44,16)
  141. --Capacitor Info Box
  142.     drawBoxes(53,21,44,16)
  143.  
  144. --Text
  145. --Info Box
  146.     drawText(24-(string.len(" Reactor Info ")/2),2, " Reactor Info ", colors.green)
  147. --Activate Controller
  148.     drawText(24-(string.len(" Activate ")/2),25, " Activate ", colors.green)
  149. --Control Rod Controller
  150.     drawText((53+22)-(string.len(" Rods ")/2),2, " Rods ", colors.green)
  151. --Info Box
  152.     drawText((53+22)-(string.len(" Capacitor Info ")/2),21, " Capacitor Info ", colors.green)
  153.    
  154. --Draw all buttons
  155.     drawButtons()
  156.  
  157. --Reactor Info Panel
  158. --Reactor isActive
  159.     if workingMode == 0 then
  160.         drawText(6,5,"Activated: "..tostring(reactorInfo["active"]), colors.red)
  161.     elseif workingMode == 1 then
  162.         drawText(6,5,"Activated: "..tostring(reactorInfo["active"]), colors.green)
  163.     else
  164.         drawText(6,5,"Activated: "..tostring(reactorInfo["active"]), colors.yellow)
  165.     end
  166. --StoredEnergy
  167.     drawText(6,7, "StoredEnergy: "..tostring(reactorInfo["storedEnergy"]).."RF", colors.green)
  168. --Core Temp.
  169.     drawText(6,9, "Core Temp: "..tostring(round(reactorInfo["internalTemp"]),2).."c", colors.green)
  170. --Case Temp.
  171.     drawText(6,11, "Case Temp: "..tostring(round(reactorInfo["externalTemp"]),2).."c", colors.green)
  172. --Fuel Level
  173.     drawText(6,13, "Fuel: "..tostring(Percentage(reactorInfo["fuel"],reactorInfo["fuelmax"])).."%",colors.green)
  174. --Control Rods Level
  175.     drawText(6,15, "Control Rod Level: "..tostring(reactorInfo["controlRodLvl"]).."%", colors.green)
  176. --Produced Last Tick
  177.     drawText(6,17, "Produced Last Tick: "..tostring(round(reactorInfo["producedLastTick"]),2).."RF/t", colors.green)
  178. --Credits
  179.     drawText(24-(string.len("Coded by ResaloliPT")/2)+2,20,"Coded by ResaloliPT",colors.lime)
  180.  
  181. --Rods Panel
  182. --Labels
  183.     drawText(74-(string.len("Rod Lvl")/2)+2,4, "Rod Lvl", colors.green)
  184.     drawText(63-(string.len("Lower")/2)+2,4, "Lower", colors.green)
  185.     drawText(87-(string.len("Higher")/2)+2,4, "Higher", colors.green)
  186. --Rod
  187.     drawRod(74,6,reactorInfo["controlRodLvl"])
  188.    
  189. --Capacitor Panel
  190. --StoredEnergy
  191.     drawText(57,26, "StoredEnergyPer: "..tostring(Percentage(capacitorInfo["storedEnergy"],capacitorInfo["maxStoredEnergy"])).."%", colors.green)
  192. --Grafic
  193.     drawGrafic(57,29,Percentage(capacitorInfo["storedEnergy"],capacitorInfo["maxStoredEnergy"]))
  194. end
  195.  
  196. function addButton(name,func,display,xmin,ymin,width,height,offColor,onColor,state)
  197.     button[name] = {}
  198.     button[name]["name"] = name
  199.     button[name]["func"] = func
  200.     button[name]["display"] = display
  201.     button[name]["xmin"] = xmin
  202.     button[name]["ymin"] = ymin
  203.     button[name]["width"] = width
  204.     button[name]["height"] = height
  205.     button[name]["offColor"] = offColor
  206.     button[name]["onColor"] = onColor
  207.     button[name]["state"] = state
  208. end
  209.  
  210. function activate()
  211.     button["inactive"]["state"] = 0
  212.     if button["active"]["state"] == 1 then
  213.         button["active"]["state"] = 0
  214.         button["auto"]["state"] = 1
  215.         workingMode = 0
  216.     else
  217.         button["active"]["state"] = 1
  218.         button["auto"]["state"] = 0
  219.         workingMode = 2
  220.     end
  221. end
  222.  
  223. function auto()
  224.     button["active"]["state"] = 0
  225.     if button["auto"]["state"] == 1 then
  226.         button["auto"]["state"] = 0
  227.         workingMode = 0
  228.         button["inactive"]["state"] = 1
  229.     else
  230.         button["auto"]["state"] = 1
  231.         button["inactive"]["state"] = 0
  232.         workingMode = 1
  233.     end
  234. end
  235.  
  236. function deactivate()
  237.     button["active"]["state"] = 0
  238.     if button["inactive"]["state"] == 1 then
  239.         button["inactive"]["state"] = 0
  240.         button["auto"]["state"] = 1
  241.         workingMode = 2
  242.     else
  243.         button["inactive"]["state"] = 1
  244.         button["auto"]["state"] = 0
  245.         workingMode = 0
  246.     end
  247. end
  248.  
  249. function lower1()
  250.     if reactorInfo["controlRodLvl"] + 1 <= 100 then
  251.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]+1)
  252.     end
  253. end
  254.  
  255. function lower5()
  256.     if reactorInfo["controlRodLvl"] + 5 <= 100 then
  257.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]+5)
  258.     end
  259. end
  260.  
  261. function lower10()
  262.     if reactorInfo["controlRodLvl"] + 10 <= 100 then
  263.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]+10)
  264.     end
  265. end
  266.  
  267. function higher1()
  268.     if reactorInfo["controlRodLvl"] - 1 >= 0 then
  269.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]-1)
  270.     end
  271. end
  272.  
  273. function higher5()
  274.     if reactorInfo["controlRodLvl"] - 5 >= 0 then
  275.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]-5)
  276.     end
  277. end
  278.  
  279. function higher10()
  280.     if reactorInfo["controlRodLvl"] - 10 >= 0 then
  281.         reactor.setAllControlRodLevels(reactorInfo["controlRodLvl"]-10)
  282.     end
  283. end
  284.  
  285. --Activate Panel
  286. addButton("active",activate,"Activate",8,30,10,2,colors.red,colors.lime,0)
  287. addButton("auto",auto,"Automatic",20,30,11,2,colors.red,colors.lime,1)
  288. addButton("inactive",deactivate,"Deactivate",33,30,12,2,colors.red,colors.lime,0)
  289. --Rod Panel
  290. --Lower
  291. addButton("+1",lower1,"+1",60,6,12,2,colors.red,colors.lime,0)
  292. addButton("+5",lower5,"+5",60,10,12,2,colors.red,colors.lime,0)
  293. addButton("+10",lower10,"+10 ",60,14,12,2,colors.red,colors.lime,0)
  294. --Higher
  295. addButton("-1",higher1,"-1",79,6,12,2,colors.red,colors.lime,0)
  296. addButton("-5",higher5,"-5",79,10,12,2,colors.red,colors.lime,0)
  297. addButton("-10",higher10,"-10 ",79,14,12,2,colors.red,colors.lime,0)
  298.  
  299.  
  300. function checkxy(x, y)
  301.     for id,data in pairs(button) do
  302.         if x < data["xmin"]+data["width"] and x > data["xmin"] then
  303.             if y < data["ymin"]+data["height"] and y > data["ymin"] then
  304.             data["func"]()
  305.             end
  306.         end
  307.     end
  308.    return false
  309. end
  310.  
  311. function getClick()
  312.     event,side,x,y = os.pullEvent("monitor_touch")
  313.     checkxy(x, y)
  314. end
  315.  
  316. function mainLoop()
  317.     sleep(0.2)
  318.     getReactorInfo()
  319.     getCapacitorInfo()
  320.     if workingMode == 1 then
  321.         autoControl()
  322.     elseif workingMode == 2 then
  323.         reactor.setActive(true)
  324.     else
  325.         reactor.setActive(false)
  326.     end
  327.     drawGui()
  328. end
  329.  
  330. while true do
  331.     parallel.waitForAny(mainLoop,getClick)
  332. end
Add Comment
Please, Sign In to add comment