Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. API = require("buttonAPI")
  2. local filesystem = require("filesystem")
  3. local component = require("component")
  4. local keyboard = require("keyboard")
  5. local event = require("event")
  6. local gpu = component.gpu
  7. local reactor = component.br_reactor
  8.  
  9.  
  10. local colors = { blue = 0x4286F4, purple = 0xB673d6, red = 0xC14141, green = 0xDA841,
  11. black = 0x000000, white = 0xFFFFFF, grey = 0x47494C, lightGrey = 0xBBBBBB}
  12. -- set size of the screen for lvl 3
  13. gpu.setResolution(132,38)
  14. gpu.setBackground(colors.black)
  15. gpu.fill(1, 1, 132, 38, " ")
  16.  
  17. local sections = {}
  18. local graphs = {}
  19. local infos = {}
  20.  
  21.  
  22.  
  23. -- defninitions
  24. reactor["stats"] = {}
  25. local running = true
  26. local maxRF = 0
  27. local currentRodLevel = 0
  28. local currentRf = 0
  29. local currentRfTick = 0
  30. local currenFuel = 0
  31.  
  32. local minPowerRod = 0
  33. local maxPowerRod = 100
  34.  
  35.  
  36. -- functions
  37.  
  38. function setSections()
  39. sections["graph"] = { x = 5, y = 3, width = 78, height= 33, title = " INFOS "}
  40. sections["controls"] = { x = 88, y = 3, width = 40, height = 20, title = " CONTROLS "}
  41. sections["info"] = { x = 88, y = 26, width = 40, height= 10, title = " NUMBERS "}
  42. end
  43.  
  44. function setGraphs()
  45. graphs["tick"] = { x = 8, y = 6, width = 73, height= 8, title = "ENERGY LAST TICK"}
  46. graphs["stored"] = { x = 8, y = 16, width = 73, height = 8, title = "ENERGY STORED"}
  47. graphs["rods"] = { x = 8, y = 26, width = 73, height= 8, title = "CONTROL RODS LEVEL"}
  48. end
  49.  
  50. function setInfos()
  51. infos["tick"] = { x = 92, y = 28, width = 73, height= 1, title = "RF PER TICK : ", unit = " RF"}
  52. infos["stored"] = { x = 92, y = 30, width = 73, height = 1, title = "ENERGY STORED : ", unit = " RF"}
  53. infos["rods"] = { x = 92, y = 32, width = 73, height= 1, title = "CONTROL ROD LEVEL : ", unit = "%"}
  54. infos["fuel"] = { x = 92, y = 34, width = 73, height= 1, title = "FUEL USAGE : ", unit = " Mb/t"}
  55. end
  56.  
  57. function setButtons()
  58. API.setTable("ON", powerOn, 91, 5, 106, 7,"ON", {on = colors.green, off = colors.green})
  59. API.setTable("OFF", powerOff, 109, 5, 125, 7,"OFF", {on = colors.red, off = colors.red})
  60.  
  61. API.setTable("lowerMinLimit", lowerMinLimit, 91, 15, 106, 17,"-1", {on = colors.blue, off = colors.blue})
  62. API.setTable("lowerMaxLimit", lowerMaxLimit, 109, 15, 125, 17,"-1", {on = colors.purple, off = colors.purple})
  63.  
  64. API.setTable("augmentMinLimit", augmentMinLimit, 91, 19, 106, 21,"+1", {on = colors.blue, off = colors.blue})
  65. API.setTable("augmentMaxLimit", augmentMaxLimit, 109, 19, 125, 21,"+1", {on = colors.purple, off = colors.purple})
  66. end
  67.  
  68. function printBorders(sectionName)
  69. local s = sections[sectionName]
  70.  
  71. -- set border
  72. gpu.setBackground(colors.grey)
  73. gpu.fill(s.x, s.y, s.width, 1, " ")
  74. gpu.fill(s.x, s.y, 1, s.height, " ")
  75. gpu.fill(s.x, s.y + s.height, s.width, 1, " ")
  76. gpu.fill(s.x + s.width, s.y, 1, s.height + 1, " ")
  77.  
  78. -- set title
  79. gpu.setBackground(colors.black)
  80. gpu.set(s.x + 2, s.y, s.title)
  81. end
  82.  
  83. function printGraphs(graphName)
  84. local g = graphs[graphName]
  85.  
  86. -- set graph
  87. gpu.setBackground(colors.lightGrey)
  88. gpu.fill(g.x, g.y, g.width, g.height, " ")
  89.  
  90. -- set title
  91. gpu.setBackground(colors.black)
  92. gpu.set(g.x, g.y - 1, g.title)
  93. end
  94.  
  95. function printActiveGraphs(activeGraph)
  96. local g = activeGraph
  97.  
  98. -- set graph
  99. gpu.setBackground(colors.green)
  100. gpu.fill(g.x, g.y, g.width, g.height, " ")
  101. gpu.setBackground(colors.black)
  102. end
  103.  
  104. function printStaticControlText()
  105. gpu.setForeground(colors.blue)
  106. gpu.set(97,12, "MIN")
  107. gpu.setForeground(colors.purple)
  108. gpu.set(116,12, "MAX")
  109. gpu.setForeground(colors.white)
  110. gpu.set(102,10, "AUTO-CONTROL")
  111. gpu.set(107,13, "--")
  112. end
  113.  
  114. function printControlInfos()
  115. gpu.setForeground(colors.blue)
  116. gpu.set(97,13, minPowerRod .. "% ")
  117. gpu.setForeground(colors.purple)
  118. gpu.set(116,13, maxPowerRod .. "% ")
  119. gpu.setForeground(colors.white)
  120. end
  121.  
  122. function printInfos(infoName)
  123. local maxLength = 15
  124. local i = infos[infoName]
  125. local spaces = string.rep(" ", maxLength - string.len(reactor.stats[infoName] .. i.unit))
  126. gpu.set(i.x, i.y , i.title .. reactor.stats[infoName] .. i.unit .. spaces)
  127. end
  128.  
  129. function getInfoFromReactor()
  130. reactor.stats["tick"] = math.ceil(reactor.getEnergyProducedLastTick())
  131. reactor.stats["stored"] = reactor.getEnergyStored()
  132. reactor.stats["rods"] = math.ceil(reactor.getControlRodLevel(0))
  133. reactor.stats["fuel"] = round(reactor.getFuelConsumedLastTick(), 2);
  134. currentRf = reactor.stats["stored"]
  135. end
  136.  
  137. function augmentMinLimit()
  138. modifyRods("min", 1)
  139. end
  140.  
  141. function lowerMinLimit()
  142. modifyRods("min", -1)
  143. end
  144.  
  145. function augmentMaxLimit()
  146. modifyRods("max", 1)
  147. end
  148.  
  149. function lowerMaxLimit()
  150. modifyRods("max", -1)
  151. end
  152.  
  153. function powerOn()
  154. reactor.setActive(true)
  155. end
  156.  
  157. function powerOff()
  158. reactor.setActive(false)
  159. end
  160.  
  161. function modifyRods(limit, number)
  162. local tempLevel = 0
  163.  
  164. if limit == "min" then
  165. tempLevel = minPowerRod + number
  166. if tempLevel <= 0 then
  167. minPowerRod = 0
  168. end
  169.  
  170. if tempLevel >= maxPowerRod then
  171. minPowerRod = maxPowerRod -1
  172. end
  173.  
  174. if tempLevel < maxPowerRod and tempLevel > 0 then
  175. minPowerRod = tempLevel
  176. end
  177. else
  178. tempLevel = maxPowerRod + number
  179. if tempLevel <= minPowerRod then
  180. maxPowerRod = minPowerRod +1
  181. end
  182.  
  183. if tempLevel >= 100 then
  184. maxPowerRod = 100
  185. end
  186.  
  187. if tempLevel > minPowerRod and tempLevel < 100 then
  188. maxPowerRod = tempLevel
  189. end
  190. end
  191.  
  192. setInfoToFile()
  193. adjustRodsLevel()
  194. end
  195.  
  196. -- Calculate and adjusts the level of the rods
  197. function adjustRodsLevel()
  198. local rfTotalMax = 10000000
  199. currentRf = reactor.stats["stored"]
  200.  
  201. differenceMinMax = maxPowerRod - minPowerRod
  202.  
  203. local maxPower = (rfTotalMax/100) * maxPowerRod
  204. local minPower = (rfTotalMax/100) * minPowerRod
  205.  
  206. if currentRf >= maxPower then
  207. currentRf = maxPower
  208. end
  209.  
  210. if currentRf <= minPower then
  211. currentRf = minPower
  212. end
  213.  
  214. currentRf = currentRf - (rfTotalMax/100) * minPowerRod
  215. local rfInBetween = (rfTotalMax/100) * differenceMinMax
  216. local rodLevel = math.ceil((currentRf/rfInBetween)*100)
  217.  
  218. reactor.setAllControlRodLevels(rodLevel)
  219. end
  220.  
  221. function draw()
  222. if maxRF < reactor.stats["tick"] then
  223. maxRF = reactor.stats["tick"]
  224. end
  225.  
  226. if currentRfTick ~= reactor.stats["tick"] then
  227. currentRfTick = reactor.stats["tick"]
  228. local max = math.ceil(graphs["tick"].width * (currentRfTick/maxRF))
  229. local currentRFTickObj = {x = graphs["tick"].x, y = graphs["tick"].y, width = max, height = graphs["tick"].height}
  230. printInfos("tick")
  231. printGraphs("tick")
  232. printActiveGraphs(currentRFTickObj)
  233. end
  234.  
  235. if currentRF ~= reactor.stats["stored"] then
  236. currentRF = reactor.stats["stored"]
  237. local max = math.ceil(graphs["stored"].width * (currentRF/10000000))
  238. local currentRFObj = {x = graphs["stored"].x, y = graphs["stored"].y, width = max, height = graphs["stored"].height}
  239. printInfos("stored")
  240. printGraphs("stored")
  241. printActiveGraphs(currentRFObj)
  242. end
  243.  
  244. if currentRodLevel ~= reactor.stats["rods"] then
  245. currentRodLevel = reactor.stats["rods"]
  246. local max = math.ceil(graphs["rods"].width * (currentRodLevel/100))
  247. local currentRodObj = {x = graphs["rods"].x, y = graphs["rods"].y, width = max, height = graphs["rods"].height}
  248. printInfos("rods")
  249. printGraphs("rods")
  250. printActiveGraphs(currentRodObj)
  251. end
  252.  
  253. if currenFuel ~= reactor.stats["fuel"] then
  254. currenFuel = reactor.stats["fuel"]
  255. printInfos("fuel")
  256. end
  257. printControlInfos()
  258. end
  259.  
  260. function startup()
  261. getInfoFromFile()
  262. getInfoFromReactor()
  263. setSections()
  264. setGraphs()
  265. setInfos()
  266. setButtons()
  267.  
  268. for name, data in pairs(sections) do
  269. printBorders(name)
  270. end
  271. for name, data in pairs(graphs) do
  272. printGraphs(name)
  273. end
  274. for name, data in pairs(infos) do
  275. printInfos(name)
  276. end
  277. printStaticControlText()
  278.  
  279.  
  280. end
  281.  
  282.  
  283. -- helpers
  284. function round(val, decimal)
  285. if (decimal) then
  286. return math.floor( (val * 10^decimal) + 0.5) / (10^decimal)
  287. else
  288. return math.floor(val+0.5)
  289. end
  290. end
  291.  
  292. function file_exists(name)
  293. local f=io.open(name,"r")
  294. if f~=nil then io.close(f) return false else return true end
  295. end
  296.  
  297. function getInfoFromFile()
  298. if file_exists("reactor.txt") then
  299. file = io.open("reactor.txt","w")
  300. file:write("0")
  301. file:write("\n")
  302. file:write("100")
  303. file:close()
  304. else
  305. file = io.open("reactor.txt","r")
  306. minPowerRod = tonumber(file:read("*l"))
  307. maxPowerRod = tonumber(file:read("*l"))
  308. file:close()
  309. end
  310. end
  311.  
  312. function setInfoToFile()
  313. file = io.open("reactor.txt","w")
  314. file:write(minPowerRod .. "\n" .. maxPowerRod)
  315. file:flush()
  316. file:close()
  317. end
  318.  
  319. -- starting
  320. startup()
  321. API.screen()
  322.  
  323. event.listen("touch", API.checkxy)
  324.  
  325. while event.pull(0.05, "interrupted") == nil do
  326. getInfoFromReactor()
  327. adjustRodsLevel()
  328. draw()
  329. local event, address, arg1, arg2, arg3 = event.pull(1)
  330. if type(address) == "string" and component.isPrimary(address) then
  331. if event == "key_down" and arg2 == keyboard.keys.q then
  332. os.exit()
  333. end
  334. end
  335. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement