THEJean_Kevin

redstone ordi

Mar 1st, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 KB | None | 0 0
  1. local libGUI = require("libGUI")
  2. local frame_base = require("libGUI/frame")
  3. local window_base = require("libGUI/window")
  4. local bar_base = require("libGUI/bar")
  5. local colors = require("libGUI/colors")
  6. local draconic_control = require("draconic_control")
  7.  
  8. local oop = require("oop")
  9. local component = require("component")
  10.  
  11. local DraconicControllerSummaryFrame = {
  12. mController = nil,
  13. mReactorInfo = nil
  14. }
  15.  
  16. local draconic_rf = component.proxy(component.get("7640"))
  17. local maxEnergie = draconic_rf.getMaxEnergyStored()
  18.  
  19. oop.inherit(DraconicControllerSummaryFrame, frame_base)
  20.  
  21. function DraconicControllerSummaryFrame:construct(controller)
  22. frame_base.construct(self)
  23. self.mController = controller
  24. self.mReactorInfo = {
  25. status = "unknown",
  26. temperature = 0,
  27. fieldStrength = 0,
  28. fieldDrainRate = 0,
  29. generationRate = 0,
  30. fuelConversionRate = 0,
  31. energySaturation = 0,
  32. maxFuelConversion = 0,
  33. fuelConversion = 0,
  34. maxFieldStrength = 0,
  35. maxEnergySaturation = 0
  36. }
  37. end
  38.  
  39. function DraconicControllerSummaryFrame:update(reactorInfo)
  40. self.mReactorInfo = reactorInfo
  41. end
  42.  
  43. function formatNumber(number)
  44. local output = string.format("%.2f", number)
  45. local unitVal = {1000000000000000, 1000000000000, 1000000000, 1000000, 1000}
  46. local unit = {" billiard", " billion", " milliard", " million", " mille"}
  47. local units = 5
  48.  
  49. local toConvert = math.abs(number)
  50. local sign = ""
  51.  
  52. for i = 1, units do
  53. if toConvert >= unitVal[i] then
  54. local x = toConvert / unitVal[i]
  55. if number < 0 then
  56. sign = "-"
  57. end
  58. output = sign .. string.format("%.2f", x) .. unit[i]
  59. break
  60. end
  61. end
  62.  
  63. return output
  64. end
  65.  
  66. function DraconicControllerSummaryFrame:onDraw()
  67. frame_base.onDraw(self)
  68. self:setBackground(colors.black)
  69. self:setForeground(colors.white)
  70.  
  71. self:fill(1, 1, self.mWidth, self.mHeight, ' ')
  72. local statusText = string.gsub(self.mReactorInfo.status, '_', ' ')
  73.  
  74. local captions = {
  75. {"Reactor Status:", string.upper(statusText), 3},
  76. {"Temperature Load Factor:", string.format("%6.2f%%", math.max(1, self.mController.calculateTempDrainFactor(self.mReactorInfo.temperature)) * 100), 21},
  77. {"Core Mass:", string.format("%3.1f m^3", self.mReactorInfo.maxFuelConversion / 1296), 18},
  78. {"Generation Rate:", string.format("%d RF/t", self.mReactorInfo.generationRate), 12},
  79. {"Field Drain Rate:", string.format("%d RF/t", self.mReactorInfo.fieldDrainRate), 15},
  80. {"Output Rate:", string.format("%d RF/t", self.mController.outputLast), 6},
  81. {"Fuel Conversion Rate:", string.format("%d nB/t", self.mReactorInfo.fuelConversionRate), 9},
  82. {"Boule draconic"," ",99},
  83. {"Energie stored:",formatNumber(draconic_rf.getEnergyStored()).. " / " .. formatNumber(maxEnergie).." (".. string.format("%.2f", math.floor(draconic_rf.getEnergyStored() / maxEnergie) * 100).."%)",9},
  84. {"Energie transfert:",formatNumber(draconic_rf.getTransferPerTick()),9}
  85. }
  86.  
  87. local offset_Y = 1
  88. for _, v in pairs(captions) do
  89. if v[3] == 99 then
  90. self:set((self.mWidth - string.len(v[2])) / 2, offset_Y, v[2])
  91. offset_Y = offset_Y + 2
  92. elseif self.mHeight >= v[3] then
  93. self:set(1, offset_Y, v[1])
  94. self:set(self.mWidth - string.len(v[2]), offset_Y , v[2])
  95. offset_Y = offset_Y + 2
  96. end
  97.  
  98. end
  99.  
  100. self:setBackground(0xFFA500)
  101. self:setForeground(0x008000)
  102. self:fill(1, offset_Y, self.mWidth, 2, " ")
  103. local proz = math.floor(draconic_rf.getEnergyStored() / maxEnergie) * 100
  104. if proz > 100 then
  105. proz = 100
  106. end
  107. local pos = math.floor((self.mWidth - 2) / 100 * proz)
  108. self:setBackground(0x008000)
  109. self:fill(1, offset_Y, pos, 2, " ")
  110.  
  111.  
  112. end
  113.  
  114. local DraconicControllerHealthFrame = {
  115. mController = nil,
  116. mReactorInfo = nil,
  117. mFieldPercentage = 0,
  118. mFieldPercentageLast = 0,
  119. mSaturation = 0,
  120. mMaxRFt = 0,
  121. mDrawTicks = 0,
  122. }
  123. oop.inherit(DraconicControllerHealthFrame, frame_base)
  124. function DraconicControllerHealthFrame:construct(controller)
  125. frame_base.construct(self)
  126. self.mController = controller
  127. self.mShutdownHits = 0
  128. self.mReactorInfo = {
  129. status = "unknown",
  130. temperature = 0,
  131. fieldStrength = 0,
  132. fieldDrainRate = 0,
  133. generationRate = 0,
  134. fuelConversionRate = 0,
  135. energySaturation = 0,
  136. maxFuelConversion = 0,
  137. fuelConversion = 0,
  138. maxFieldStrength = 0,
  139. maxEnergySaturation = 0
  140. }
  141. end
  142.  
  143. function DraconicControllerHealthFrame:update(reactorInfo)
  144. self.mReactorInfo = reactorInfo
  145. self.mFieldPercentageLast = self.mFieldPercentage
  146. self.mFieldPercentage = reactorInfo.fieldStrength / reactorInfo.maxFieldStrength
  147. self.mSaturation = reactorInfo.energySaturation / reactorInfo.maxEnergySaturation
  148. self.mMaxRFt = self.mController.calculateReactorMaxRFT(reactorInfo)
  149. self.mShutdownHits = 0
  150. end
  151.  
  152. function DraconicControllerHealthFrame:onDraw()
  153. frame_base.onDraw(self)
  154. local warning = false
  155. local danger = false
  156. local throttled = false
  157.  
  158. if self.mReactorInfo.temperature > self.mController.limitTemperature then
  159. danger = true
  160. elseif self.mReactorInfo.temperature > (self.mController.limitTemperature-self.mController.throttleTemperature) / 2 + self.mController.throttleTemperature then
  161. warning = true
  162. elseif self.mReactorInfo.temperature > self.mController.throttleTemperature then
  163. throttled = true
  164. end
  165.  
  166. local burnmode = throttled and self.mController:isConnected() and self.mController.throttleLast > 0
  167.  
  168. if self.mFieldPercentage < 0.05 then
  169. danger = true
  170. elseif self.mFieldPercentage < 0.09 and (not burnmode or self.mFieldPercentageLast < self.mFieldPercentage) then
  171. warning = true
  172. end
  173.  
  174. if self.mSaturation < 0.07 then
  175. danger = true
  176. elseif self.mSaturation < math.min(0.09, self.mController.targetSaturation) then
  177. warning = true
  178. end
  179.  
  180. if self.mMaxRFt * 0.33 < self.mReactorInfo.fieldDrainRate then
  181. danger = true
  182. elseif self.mMaxRFt * 0.25 < self.mReactorInfo.fieldDrainRate then
  183. warning = true
  184. end
  185.  
  186. local displayString
  187. if not self.mController:isConnected() then
  188. if self.mDrawTicks % 2 == 0 then
  189. self:setBackground(colors.red)
  190. self:setForeground(colors.white)
  191. else
  192. self:setBackground(colors.black)
  193. self:setForeground(colors.red)
  194. end
  195. displayString = "Connection Failure"
  196. elseif danger then
  197. if self.mDrawTicks % 2 == 0 then
  198. self:setBackground(colors.red)
  199. self:setForeground(colors.white)
  200. else
  201. self:setBackground(colors.black)
  202. self:setForeground(colors.red)
  203. end
  204. displayString = "danger"
  205. elseif warning then
  206. if self.mDrawTicks % 2 == 0 then
  207. self:setBackground(colors.yellow)
  208. self:setForeground(colors.black)
  209. else
  210. self:setBackground(colors.black)
  211. self:setForeground(colors.yellow)
  212. end
  213. displayString = "warning"
  214. elseif throttled then
  215. self:setBackground(colors.gray)
  216. self:setForeground(colors.orange)
  217. if self.mController.throttleLast > 0 then
  218. displayString = "throttled"
  219. else
  220. displayString = "burn mode"
  221. end
  222. else
  223. self:setBackground(colors.green)
  224. self:setForeground(colors.black)
  225. displayString = "healthy"
  226. end
  227.  
  228. self:fill(1, 1, self.mWidth, self.mHeight, ' ')
  229. self:set(1+math.ceil((self.mWidth-string.len(displayString))/2), math.ceil(self.mHeight/2), string.upper(displayString))
  230. self.mDrawTicks = self.mDrawTicks + 1
  231. end
  232.  
  233. function DraconicControllerHealthFrame:onTouch(x, y, button, playerName)
  234. if self.mController.reactorInfoLast ~= nil and (self.mController.reactorInfoLast.status == self.mController.STATE_ONLINE or self.mController.reactorInfoLast.status == self.mController.STATE_SHUTDOWN) then
  235. self.mShutdownHits = self.mShutdownHits + 1
  236. if self.mShutdownHits == 5 then
  237. self.mController:toggleState()
  238. component.computer.beep(1000, 0.05)
  239. end
  240. end
  241. end
  242.  
  243. local DraconicControllerGUI = {
  244. mController = nil,
  245. mBarTemperature = nil,
  246. mBarContainment = nil,
  247. mBarSaturation = nil,
  248. mBarFuel = nil,
  249. mSummaryFrame = nil,
  250. mHealthFrame = nil
  251. }
  252. oop.inherit(DraconicControllerGUI, frame_base)
  253.  
  254. function DraconicControllerGUI:construct(controller)
  255. frame_base.construct(self)
  256. self.mController = controller
  257.  
  258. self.mBarTemperature = bar_base()
  259. self.mBarTemperature:setBarPalette({
  260. {2000 / controller.limitTemperature, colors.cyan},
  261. {(controller.throttleTemperature / 2 - 1000) / controller.limitTemperature, colors.lightBlue},
  262. {controller.throttleTemperature / controller.limitTemperature, colors.green},
  263. {((controller.limitTemperature-controller.throttleTemperature) / 2 + controller.throttleTemperature) / controller.limitTemperature, colors.orange},
  264. {1, colors.red}
  265. })
  266. self:addChild(self.mBarTemperature)
  267.  
  268. self.mBarContainment = bar_base()
  269. self.mBarContainment:setBarPalette({
  270. {0.1, colors.red},
  271. {0.25, colors.purple},
  272. {0.66, colors.blue},
  273. {1, colors.lightBlue}
  274. })
  275. self:addChild(self.mBarContainment)
  276.  
  277. self.mBarSaturation = bar_base()
  278. self.mBarSaturation:setBarPalette({
  279. {0.15, colors.red},
  280. {0.85, colors.green},
  281. {1, colors.lightBlue}
  282. })
  283. self:addChild(self.mBarSaturation)
  284.  
  285. self.mBarFuel = bar_base()
  286. self.mBarFuel:setBarPalette({
  287. {0.04, colors.yellow},
  288. {0.41, colors.green},
  289. {0.9, colors.orange},
  290. {1, colors.red}
  291. })
  292. self:addChild(self.mBarFuel)
  293.  
  294. self.mSummaryFrame = DraconicControllerSummaryFrame(controller)
  295. self:addChild(self.mSummaryFrame)
  296.  
  297. self.mHealthFrame = DraconicControllerHealthFrame(controller)
  298. self:addChild(self.mHealthFrame)
  299. end
  300.  
  301. function DraconicControllerGUI:update(reactorInfo)
  302. self.mBarTemperature:setPercentage(reactorInfo.temperature / self.mController.limitTemperature, false)
  303. self.mBarContainment:setPercentage(reactorInfo.fieldStrength / reactorInfo.maxFieldStrength, false)
  304. self.mBarSaturation:setPercentage(reactorInfo.energySaturation / reactorInfo.maxEnergySaturation, false)
  305. self.mBarFuel:setPercentage(reactorInfo.fuelConversion / reactorInfo.maxFuelConversion, false)
  306. self.mSummaryFrame:update(reactorInfo)
  307. self.mHealthFrame:update(reactorInfo)
  308. end
  309.  
  310. function DraconicControllerGUI:setParent(parent)
  311. frame_base.setParent(self, parent)
  312. local width, height = parent:getSize()
  313. self:setSize(width, height)
  314. end
  315.  
  316. function DraconicControllerGUI:getRootFrame()
  317. return self.mWindow
  318. end
  319.  
  320. function DraconicControllerGUI:onResize()
  321. self.mBarTemperature:setRegion(1, 1, 4, self.mHeight)
  322. self.mBarContainment:setRegion(6, 1, 4, self.mHeight)
  323. self.mBarSaturation:setRegion(self.mWidth - 8, 1, 4, self.mHeight)
  324. self.mBarFuel:setRegion(self.mWidth - 3, 1, 4, self.mHeight)
  325. self.mSummaryFrame:setRegion(11, 1, self.mWidth - 19, self.mHeight - 3)
  326. self.mHealthFrame:setRegion(11, self.mHeight - 2, self.mWidth - 20, 3)
  327. end
  328.  
  329. local reactorInfoFake = {
  330. status = "unknown",
  331. temperature = 20,
  332. fieldStrength = 0,
  333. fieldDrainRate = 0,
  334. generationRate = 0,
  335. fuelConversionRate = 0,
  336. energySaturation = 0,
  337. maxFuelConversion = 0,
  338. fuelConversion = 0,
  339. maxFieldStrength = 0,
  340. maxEnergySaturation = 0
  341. }
  342.  
  343. function DraconicControllerGUI:onDraw()
  344. self:update(self.mController.reactorInfoLast or reactorInfoFake)
  345. frame_base.onDraw(self)
  346. end
  347.  
  348. if not draconic_control.isRunning() then
  349. draconic_control.loadConfig()
  350. draconic_control.start()
  351. end
  352.  
  353. if #draconic_control.controllers < 1 then
  354. print("Draconic Control has not been configured yet.")
  355. print("Please make sure everything is set up correctly and start this program again.")
  356. draconic_control.stop()
  357. return 1
  358. end
  359.  
  360. local controller = draconic_control.controllers[1]
  361.  
  362. libGUI.init()
  363. libGUI.setOptimalResolution(0.65)
  364.  
  365. local window = window_base()
  366. local dcGUI = DraconicControllerGUI(controller)
  367. local terminateGUI = false
  368.  
  369. function window:onTouch(x, y, button, playerName)
  370. window_base.onTouch(self, x, y, button, playerName)
  371. if y == 1 then
  372. libGUI.exit()
  373. end
  374. end
  375.  
  376. function window:onResize()
  377. window_base.onResize(self)
  378.  
  379. local width, height = self.mClientFrame:getSize()
  380. dcGUI:setRegion(1, 1, width, height)
  381.  
  382. if self:getGPU() then
  383. local oldColor, oldPalette = self:getBackground()
  384. self:setBackground(colors.black)
  385. self:fill(1, 1, self.mWidth, self.mHeight, ' ')
  386. self:setBackground(oldColor, oldPalette)
  387. self:onDraw()
  388. end
  389. end
  390.  
  391. window:setTitle("Draconic Control v" .. tostring(draconic_control.getVersion()) .. " by XyFreak")
  392. window:addChild(dcGUI)
  393.  
  394. libGUI.setRootFrame(window)
  395. libGUI.setRedrawInterval(1)
  396. libGUI.runOrFork()
  397.  
  398. print("Please keep in mind that the draconic control service will not shut down automatically if the gui is closed.")
Advertisement
Add Comment
Please, Sign In to add comment