Advertisement
NolanSyKinsley

Untitled

Mar 28th, 2014
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.32 KB | None | 0 0
  1. --[[
  2. Program name: Lolmer's EZ-NUKE reactor control system
  3. Version: v0.3.6
  4. Programmer: Lolmer
  5. Last update: 2014-03-07
  6. Pastebin: http://pastebin.com/fguScPBQ
  7.  
  8. Description:
  9. This program controls a Big Reactors nuclear reactor in Minecraft with a Computercraft computer, using Computercraft's own wired modem connected to the reactors computer control port.
  10.  
  11. This program was designed to work with the mods and versions installed on Never Stop Toasting (NST) Diet http://www.technicpack.net/modpack/details/never-stop-toasting-diet.254882 Endeavour: Never Stop Toasting: Diet official Minecraft server http://forums.somethingawful.com/showthread.php?threadid=3603757
  12.  
  13. To simplify the code and guesswork, I assume the following monitor layout:
  14. 1) One Advanced Monitor for overall status display plus
  15. one or more Reactors plus
  16. none or more Turbines.
  17. 2) One Advanced Monitor for overall status display plus (first found monitor)
  18. one Advanced Monitor for each connected Reactor plus (subsequent found monitors)
  19. one Advanced Monitor for each connected Turbine (last group of monitors found).
  20. If you enable debug mode, add one additional Advanced Monitor for #1 or #2.
  21.  
  22. Notes:
  23. Only one reactor and one, two, and three turbines have been tested with the above, but IN THEORY any number is supported.
  24. Devices are found in the reverse order they are plugged in, so monitor_10 will be found before monitor_9.
  25. Two 15x15x14 Turbines can output 260K RF/t by just one 7^3 (four rods) reactor putting out 4k mB steam
  26.  
  27. When using actively cooled reactors with turbines, keep the following in mind:
  28. - 1 mB steam carries up to 10RF of potential energy to extract in a turbine.
  29. - Actively cooled reactors produce steam, not power.
  30. - You will need about 10 mB of water for each 1 mB of steam that you want to create in a 7^3 reactor.
  31.  
  32. Features:
  33. Configurable min/max energy buffer and min/max temperature via ReactorOptions file.
  34. ReactorOptions is read on start and then current values are saved every program cycle.
  35. Rod Control value in ReactorOptions is only useful for initial start, after that the program saves the current Rod Control average over all Fuel Rods for next boot.
  36. Auto-adjusts control rods per reactor to maintain temperature.
  37. Will display reactor data to all attached monitors of correct dimensions.
  38. For multiple monitors, the first monitor (often last plugged in) is the overall status monitor.
  39. For multiple monitors, the first monitor (often last plugged in) is the overall status monitor.
  40.  
  41. GUI Usage:
  42. The "<" and ">" buttons, when right-clicked with the mouse, will decrease and increase, respectively, the values assigned to the monitor:
  43. "Rod (%)" will lower/raise the Reactor Control Rods for that Reactor
  44. "Flow mB/t" will lower/raise the Turbine Flow Rate maximum for that Turbine
  45. Right-clicking between the "<" and ">" (not on them) will disable auto-adjust of that value for attached device.
  46. Right-clicking on the "Enabled" or "Disabled" text for auto-adjust will do the same.
  47. Right-clicking on "ONLINE" or "OFFLINE" at the top-right will toggle the state of attached device.
  48.  
  49. Default values:
  50. Rod Control: 90% (Let's start off safe and then power up as we can)
  51. Minimum Energy Buffer: 15% (will power on below this value)
  52. Maximum Energy Buffer: 85% (will power off above this value)
  53. Minimum Passive Cooling Temperature: 850^C (will raise control rods below this value)
  54. Maximum Passive Temperature: 950^C (will lower control rods above this value)
  55. Optimal Turbine RPM: 900 or 1,800
  56.  
  57. Requirements:
  58. Advanced Monitor size is X: 29, Y: 12 with a 3x2 size
  59. Computer or Advanced Computer
  60. Modems (not wireless) connecting each of the Computer to both the Advanced Monitor and Reactor Computer Port.
  61. Big Reactors (http://www.big-reactors.com/) 0.3
  62. Computercraft (http://computercraft.info/) 1.57+
  63. Reset the computer any time number of connected devices change.
  64.  
  65. Resources:
  66. This script is available from:
  67. http://pastebin.com/fguScPBQ
  68. https://github.com/sandalle/minecraft_bigreactor_control
  69. Start-up script is available from:
  70. http://pastebin.com/ZTMzRLez
  71. https://github.com/sandalle/minecraft_bigreactor_control
  72. Other reactor control program which I based my program on:
  73. http://pastebin.com/aMAu4X5J (ScatmanJohn)
  74. http://pastebin.com/HjUVNDau (version ScatmanJohn based his on)
  75. A simpler Big Reactor control program is available from:
  76. http://pastebin.com/7S5xCvgL (IronClaymore only for passively cooled reactors)
  77.  
  78. Reactor Computer Port API: http://wiki.technicpack.net/Reactor_Computer_Port
  79. Computercraft API: http://computercraft.info/wiki/Category:APIs
  80. Big Reactors Efficiency, Speculation and Questions! http://www.reddit.com/r/feedthebeast/comments/1vzds0/big_reactors_efficiency_speculation_and_questions/
  81. Big Reactors API code: https://github.com/erogenousbeef/BigReactors/blob/master/erogenousbeef/bigreactors/common/multiblock/tileentity/TileEntityReactorComputerPort.java
  82. Big Reactors API: http://big-reactors.com/cc_api.html
  83.  
  84. ChangeLog:
  85. 0.3.6 - Fix multi-reactors displaying on the correct monitors (thanks HybridFusion).
  86. Fix rod auto-adjust text position.
  87. Reactors store 10M RF and Turbines store 1M RF in their buffer.
  88. Add more colour to displayAllStatus().
  89. Sleep for only two seconds instead of five.
  90. Fix getDeviceStoredEnergyBufferPercent() for Reactors storing 10M RF in buffer.
  91. Keep actively cooled reactors between 0-300^C (non-configurable for now).
  92. 0.3.5 - Do not discover connected devices every loop - nicer on servers. Reset computer anytime number of connected devices change.
  93. Fix multi-reactor setups to display the additional reactors on monitors, rather than the last one found.
  94. Fix passive reactor display having auto-adjust and energy buffer overwrite each other (removes rod count).
  95. 0.3.4 - Fix arithmetic for checking if we have enough monitors for the number of reactors.
  96. Turbines are optimal at 900, 1800, *and* 2700 RPM
  97. Increase loop timer from 1 to 5 to be nicer to servers
  98. 0.3.3 - Add Big Reactor Turbine support
  99. First found monitor (appears to be last connected monitor) is used to display status of all found devices (if more than one valid monitor is found)
  100. Display monitor number on top left of each monitor as "M#" to help find which monitor is which.
  101. Enabling debug will use the last monitor found, if more than one, to print out debug info (also written to file)
  102. Add monitor layout requirements to simplify code
  103. Only clear monitors when we're about to use them (e.g. turbine monitors no longer clear, then wait for all reactors to update)
  104. Fix getDeviceStoredEnergyBufferPercent(), was off by a decimal place
  105. Just use first Control Rod level for entire reactor, they are no longer treated individually in BR 0.3
  106. Allow for one monitor for n number of reactors and m number of turbines
  107. Auto-adjust turbine flow rate by 25 mB to keep rotor speed at 900 or 1,800 RPM.
  108. Clicks on monitors relate to what the monitor is showing (e.g. clicking on reactor 1's display won't modify turbine 1's nor reactor 2's values)
  109. Print monitor name and device (reactor|turbine) name in blue to monitor associated for easier design by users.
  110. Remove version number from monitors to free up space for monitor names.
  111. Add option of right-clicking on "Enabled"/"Disabled" of auto-adjust to toggle it.
  112. 0.3.2 - Allow for rod control to override (disable) auto-adjust via UI (Rhonyn)
  113. 0.3.1 - Add fuel consumption per tick to display
  114. 0.3.0 - Add multi-monitor support! Sends one reactor's data to all monitors.
  115. print function now takes table to support optional specified monitor
  116. Set "numRods" every cycle for some people (mechaet)
  117. Don't redirect terminal output with multiple monitor support
  118. Log troubleshooting data to reactorcontrol.log
  119. FC_API no longer used (copied and modified what I needed)
  120. Multi-reactor support is theoretically implemented, but it is UNTESTED!
  121. Updated for Big Reactor 0.3 (no longer works with 0.2)
  122. BR getFuelTemperature() now returns many significant digits, just use math.ceil()
  123. BR 0.3 removed individual rod temperatures, now it's only reactor-level temperature
  124. 0.2.4 - Simplify math, don't divide by a simple large number and then multiply by 100 (#/10000000*100)
  125. Fix direct-connected (no modem) devices. getDeviceSide -> FC_API.getDeviceSide (simple as that :))
  126. 0.2.3 - Check bounds on reactor.setRodControlLevel(#,#), Big Reactor doesn't check for us.
  127. 0.2.2 - Do not auto-start the reactor if it was manually powered off (autoStart=false)
  128. 0.2.1 - Lower/raise only the hottest/coldest Control Rod while trying to control the reactor temperature.
  129. "<" Rod Control buttons was off by one (to the left)
  130. 0.2.0 - Lolmer Edition :)
  131. Add min/max stored energy percentage (default is 15%/85%), configurable via ReactorOptions file.
  132. No reason to keep burning fuel if our power output is going nowhere. :)
  133. Use variables variable for the title and version.
  134. Try to keep the temperature between configured values (default is 850^C-950^C)
  135. Add Waste and number of Control/Fuel Rods to displayBards()
  136.  
  137. TODO:
  138. - Save parameters per reactor instead of one global set for all reactors
  139. - Add min/max RF/t output and have it override temperature concerns (maybe?)
  140. - Add support for wireless modems, see http://computercraft.info/wiki/Modem_%28API%29, will not be secure (anyone can send/listen to your channels)!
  141. - Add support for any sized monitor (minimum 3x3), dynamic allocation/alignment
  142. - Lookup using pcall for better error handling http://www.computercraft.info/forums2/index.php?/topic/10992-using-pcall/
  143.  
  144. ]]--
  145.  
  146.  
  147. -- Some global variables
  148. local progVer = "0.3.6"
  149. local progName = "EZ-NUKE "
  150. local sideClick, xClick, yClick = nil, 0, 0
  151. local loopTime = 2
  152. local controlRodAdjustAmount = 1 -- Default Reactor Rod Control % adjustment amount
  153. local flowRateAdjustAmount = 25 -- Default Turbine Flow Rate in mB adjustment amount
  154. local debugMode = false
  155. -- These need to be updated for multiple reactors
  156. local baseControlRodLevel = nil
  157. local reactorRodOverride = false -- Rod override for Reactors
  158. -- End multi-reactor cleanup section
  159. local minStoredEnergyPercent = nil -- Max energy % to store before activate
  160. local maxStoredEnergyPercent = nil -- Max energy % to store before shutdown
  161. local minReactorTemp = nil -- Minimum reactor temperature (^C) to maintain
  162. local maxReactorTemp = nil -- Maximum reactor temperature (^C) to maintain
  163. local autoStart = {} -- Array for automatically starting reactors
  164. local monitorList = {} -- Empty monitor array
  165. local monitorNames = {} -- Empty array of monitor names
  166. local reactorList = {} -- Empty reactor array
  167. local reactorNames = {} -- Empty array of reactor names
  168. local turbineList = {} -- Empty turbine array
  169. local trubineNames = {} -- Empty array of turbine names
  170. local turbineFlowRateOverride = {} -- Flow rate override for each Turbine
  171. local turbineMonitorOffset = 0 -- Turbines are assigned monitors after reactors
  172.  
  173. term.clear()
  174. term.setCursorPos(2,1)
  175. term.write("Initializing program...")
  176.  
  177.  
  178. -- File needs to exist for append "a" later and zero it out if it already exists
  179. -- Always initalize this file to avoid confusion with old files and the latest run
  180. local logFile = fs.open("reactorcontrol.log", "w")
  181. if logFile then
  182. logFile.writeLine("Minecraft time: Day "..os.day().." at "..textutils.formatTime(os.time(),true))
  183. logFile.close()
  184. else
  185. error("Could not open file reactorcontrol.log for writing")
  186. end
  187.  
  188.  
  189. -- Helper functions
  190.  
  191.  
  192. -- round() function from
  193. -- http://www.computercraft.info/forums2/index.php?/topic/4023-lua-printformat-with-floating-point-numbers/page__view__findpost__p__31037
  194. local function round(num, places)
  195. num = tostring(num)
  196. local inc = false
  197.  
  198. -- Make sure decimal is a valid integer for later arithmetic
  199. local decimal = string.find(num, "%.") or 0
  200.  
  201. if (num:len() - decimal) <= places then
  202. return tonumber(num)
  203. end --already rounded, nothing to do.
  204.  
  205. local digit = tonumber(num:sub(decimal + places + 1))
  206. num = num:sub(1, decimal + places)
  207.  
  208. if digit <= 4 then
  209. return tonumber(num)
  210. end --no incrementation needed, return truncated number
  211.  
  212. local newNum = ""
  213. for i=num:len(), 1, -1 do
  214. digit = tonumber(num:sub(i))
  215. if digit == 9 then
  216. if i > 1 then
  217. newNum = "0"..newNum
  218. else
  219. newNum = "10"..newNum
  220. end
  221. elseif digit == nil then
  222. newNum = "."..newNum
  223. else
  224. if i > 1 then
  225. newNum = num:sub(1,i-1)..(digit + 1)..newNum
  226. else
  227. newNum = (digit + 1)..newNum
  228. end
  229. return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
  230. end -- if digit == 9 then
  231. end -- for i=num:len(), 1, -1 do
  232. return tonumber(newNum)
  233. end -- function round(num, places
  234.  
  235.  
  236. local function printLog(printStr)
  237. if debugMode then
  238. -- If multiple monitors, use the last monitor for debugging if debug is enabled
  239. if #monitorList > 1 then
  240. term.redirect(monitorList[#monitorList]) -- Redirect to last monitor for debugging
  241. monitorList[#monitorList].setTextScale(0.5) -- Fit more logs on screen
  242. write(printStr.."\n") -- May need to use term.scroll(x) if we output too much, not sure
  243. term.restore()
  244. end -- if #monitorList > 1 then
  245.  
  246. local logFile = fs.open("reactorcontrol.log", "a") -- See http://computercraft.info/wiki/Fs.open
  247. if logFile then
  248. logFile.writeLine(printStr)
  249. logFile.close()
  250. else
  251. error("Cannot open file reactorcontrol.log for appending!")
  252. end -- if logFile then
  253. end -- if debugMode then
  254. end -- function printLog(printStr)
  255.  
  256.  
  257. local function print(printParams)
  258. -- Default to xPos=1, yPos=1, and first monitor
  259. setmetatable(printParams,{__index={xPos=1, yPos=1, monitorIndex=1}})
  260. local printString, xPos, yPos, monitorIndex =
  261. printParams[1], -- Required parameter
  262. printParams[2] or printParams.xPos,
  263. printParams[3] or printParams.yPos,
  264. printParams[4] or printParams.monitorIndex
  265.  
  266. local monitor = nil
  267. monitor = monitorList[monitorIndex]
  268.  
  269. if not monitor then
  270. printLog("monitorList["..monitorIndex.."] in print() was not a valid monitor")
  271. return -- Invalid monitorIndex
  272. end
  273.  
  274. monitor.setCursorPos(xPos, yPos)
  275. monitor.write(printString)
  276. end -- function print(printParams)
  277.  
  278.  
  279. -- Replaces the one from FC_API (http://pastebin.com/A9hcbZWe) and adding multi-monitor support
  280. local function printCentered(printString, yPos, monitorIndex)
  281. local monitor = nil
  282. monitor = monitorList[monitorIndex]
  283.  
  284. if not monitor then
  285. printLog("monitorList["..monitorIndex.."] in printCentered() was not a valid monitor")
  286. return -- Invalid monitorIndex
  287. end
  288.  
  289. local width, height = monitor.getSize()
  290. local monitorNameLength = 0
  291.  
  292. -- Special changes for title bar
  293. if yPos == 1 then
  294. -- Add monitor name to first line
  295. monitorNameLength = monitorNames[monitorIndex]:len()
  296.  
  297. -- Leave room for "offline" and "online" on the right except for overall status display
  298. if (#monitorList ~= 1) and (monitorIndex ~= 1) then
  299. width = width - 7
  300. end
  301. end
  302.  
  303. monitor.setCursorPos(math.floor(width/2) - math.ceil(printString:len()/2) + monitorNameLength/2, yPos)
  304. monitor.clearLine()
  305. monitor.write(printString)
  306.  
  307. monitor.setTextColor(colors.blue)
  308. print{monitorNames[monitorIndex], 1, 1, monitorIndex}
  309. monitor.setTextColor(colors.white)
  310. end -- function printCentered(printString, yPos, monitorIndex)
  311.  
  312.  
  313. -- Print text padded from the left side
  314. -- Clear the left side of the screen
  315. local function printLeft(printString, yPos, monitorIndex)
  316. local monitor = nil
  317. monitor = monitorList[monitorIndex]
  318.  
  319. if not monitor then
  320. printLog("monitorList["..monitorIndex.."] in printLeft() was not a valid monitor")
  321. return -- Invalid monitorIndex
  322. end
  323.  
  324. local gap = 1
  325. local width = monitor.getSize()
  326.  
  327. -- Clear left-half of the monitor
  328.  
  329. for curXPos = 1, (width / 2) do
  330. monitor.setCursorPos(curXPos, yPos)
  331. monitor.write(" ")
  332. end
  333.  
  334. -- Write our string left-aligned
  335. monitor.setCursorPos(1+gap, yPos)
  336. monitor.write(printString)
  337. end
  338.  
  339.  
  340. -- Print text padded from the right side
  341. -- Clear the right side of the screen
  342. local function printRight(printString, yPos, monitorIndex)
  343. local monitor = nil
  344. monitor = monitorList[monitorIndex]
  345.  
  346. if not monitor then
  347. printLog("monitorList["..monitorIndex.."] in printRight() was not a valid monitor")
  348. return -- Invalid monitorIndex
  349. end
  350.  
  351. -- Make sure printString is a string
  352. printString = tostring(printString)
  353.  
  354. local gap = 1
  355. local width = monitor.getSize()
  356.  
  357. -- Clear right-half of the monitor
  358. for curXPos = (width/2), width do
  359. monitor.setCursorPos(curXPos, yPos)
  360. monitor.write(" ")
  361. end
  362.  
  363. -- Write our string right-aligned
  364. monitor.setCursorPos(math.floor(width) - math.ceil(printString:len()+gap), yPos)
  365. monitor.write(printString)
  366. end
  367.  
  368.  
  369. -- Replaces the one from FC_API (http://pastebin.com/A9hcbZWe) and adding multi-monitor support
  370. local function clearMonitor(printString, monitorIndex)
  371. local monitor = nil
  372. monitor = monitorList[monitorIndex]
  373.  
  374. if not monitor then
  375. printLog("monitorList["..monitorIndex.."] in clearMonitor() was not a valid monitor")
  376. return -- Invalid monitorIndex
  377. end
  378.  
  379. local gap = 2
  380. monitor.clear()
  381. local width, height = monitor.getSize()
  382.  
  383. printCentered(printString, 1, monitorIndex)
  384. monitor.setTextColor(colors.blue)
  385. print{monitorNames[monitorIndex], 1, 1, monitorIndex}
  386. monitor.setTextColor(colors.white)
  387.  
  388. for i=1, width do
  389. monitor.setCursorPos(i, gap)
  390. monitor.write("-")
  391. end
  392.  
  393. monitor.setCursorPos(1, gap+1)
  394. end -- function clearMonitor(printString, monitorIndex)
  395.  
  396.  
  397. -- Return a list of all connected (including via wired modems) devices of "deviceType"
  398. local function getDevices(deviceType)
  399. local deviceName = nil
  400. local deviceIndex = 1
  401. local deviceList, deviceNames = {}, {} -- Empty array, which grows as we need
  402. local peripheralList = peripheral.getNames() -- Get table of connected peripherals
  403.  
  404. deviceType = deviceType:lower() -- Make sure we're matching case here
  405.  
  406. for peripheralIndex = 1, #peripheralList do
  407. -- Log every device found
  408. -- printLog("Found "..peripheral.getType(peripheralList[peripheralIndex]).."["..peripheralIndex.."] attached as \""..peripheralList[peripheralIndex].."\".")
  409. if (string.lower(peripheral.getType(peripheralList[peripheralIndex])) == deviceType) then
  410. -- Log devices found which match deviceType and which device index we give them
  411. printLog("Found "..peripheral.getType(peripheralList[peripheralIndex]).."["..peripheralIndex.."] as index \"["..deviceIndex.."]\" attached as \""..peripheralList[peripheralIndex].."\".")
  412. deviceNames[deviceIndex] = peripheralList[peripheralIndex]
  413. deviceList[deviceIndex] = peripheral.wrap(peripheralList[peripheralIndex])
  414. deviceIndex = deviceIndex + 1
  415. end
  416. end -- for peripheralIndex = 1, #peripheralList do
  417.  
  418. return deviceList, deviceNames
  419. end -- function getDevices(deviceType)
  420.  
  421. -- Draw a line across the entire x-axis
  422. local function drawLine(yPos, monitorIndex)
  423. local monitor = nil
  424. monitor = monitorList[monitorIndex]
  425.  
  426. if not monitor then
  427. printLog("monitorList["..monitorIndex.."] in drawLine() was not a valid monitor")
  428. return -- Invalid monitorIndex
  429. end
  430.  
  431. local width, height = monitor.getSize()
  432.  
  433. for i=1, width do
  434. monitor.setCursorPos(i, yPos)
  435. monitor.write("-")
  436. end
  437. end -- function drawLine(yPos,monitorIndex)
  438.  
  439.  
  440. -- Display a solid bar of specified color
  441. local function drawBar(startXPos, startYPos, endXPos, endYPos, color, monitorIndex)
  442. local monitor = nil
  443. monitor = monitorList[monitorIndex]
  444.  
  445. if not monitor then
  446. printLog("monitorList["..monitorIndex.."] in drawBar() was not a valid monitor")
  447. return -- Invalid monitorIndex
  448. end
  449.  
  450. -- PaintUtils only outputs to term., not monitor.
  451. -- See http://www.computercraft.info/forums2/index.php?/topic/15540-paintutils-on-a-monitor/
  452. term.redirect(monitor)
  453. paintutils.drawLine(startXPos, startYPos, endXPos, endYPos, color)
  454. monitor.setBackgroundColor(colors.black) -- PaintUtils doesn't restore the color
  455. term.restore()
  456. end -- function drawBar(startXPos, startYPos,endXPos,endYPos,color,monitorIndex)
  457.  
  458.  
  459. -- Display single pixel color
  460. local function drawPixel(xPos, yPos, color, monitorIndex)
  461. local monitor = nil
  462. monitor = monitorList[monitorIndex]
  463.  
  464. if not monitor then
  465. printLog("monitorList["..monitorIndex.."] in drawPixel() was not a valid monitor")
  466. return -- Invalid monitorIndex
  467. end
  468.  
  469. -- PaintUtils only outputs to term., not monitor.
  470. -- See http://www.computercraft.info/forums2/index.php?/topic/15540-paintutils-on-a-monitor/
  471. term.redirect(monitor)
  472. paintutils.drawPixel(xPos, yPos, color)
  473. monitor.setBackgroundColor(colors.black) -- PaintUtils doesn't restore the color
  474. term.restore()
  475. end -- function drawPixel(xPos, yPos, color, monitorIndex)
  476.  
  477.  
  478. -- End helper functions
  479.  
  480.  
  481. -- Then initialize the monitors
  482. local function findMonitors()
  483. -- Empty out old list of monitors
  484. monitorList = {}
  485.  
  486. printLog("Finding monitors...")
  487. monitorList, monitorNames = getDevices("monitor")
  488.  
  489. if #monitorList == 0 then
  490. printLog("No monitors found!")
  491. error("Can't find any monitors!")
  492. else
  493. for monitorIndex = 1, #monitorList do
  494. local monitor = nil
  495. monitor = monitorList[monitorIndex]
  496.  
  497. if not monitor then
  498. printLog("monitorList["..monitorIndex.."] in findMonitors() was not a valid monitor")
  499. break -- Invalid monitorIndex
  500. end
  501.  
  502. local monitorX, monitorY = monitor.getSize()
  503. printLog("Verifying monitor["..monitorIndex.."] is of size x:"..monitorX.." by y:"..monitorY)
  504.  
  505. -- Check for minimum size to allow for monitor.setTextScale(0.5) to work for 3x2 debugging monitor, changes getSize()
  506. if monitorX < 29 or monitorY < 12 then
  507. term.redirect(monitor)
  508. monitor.clear()
  509. printLog("Removing monitor "..monitorIndex.." for incorrect size")
  510. monitor.setCursorPos(1,2)
  511. write("Monitor is the wrong size!\n")
  512. write("Needs to be 3x2.")
  513. term.restore()
  514.  
  515. table.remove(monitorList, monitorIndex) -- Remove invalid monitor from list
  516. if monitorIndex == #monitorList then -- If we're at the end already, break from loop
  517. break
  518. else
  519. monitorIndex = monitorIndex - 1 -- We just removed an element
  520. end -- if monitorIndex == #monitorList then
  521.  
  522. end -- if monitorX ~= 29 or monitorY ~= 12 then
  523. end -- for monitorIndex = 1, #monitorList do
  524. end -- if #monitorList == 0 then
  525. end -- local function findMonitors()
  526.  
  527.  
  528. -- Initialize all Big Reactors - Reactors
  529. local function findReactors()
  530. -- Empty out old list of reactors
  531. newReactorList = {}
  532.  
  533. printLog("Finding reactors...")
  534. newReactorList, reactorNames = getDevices("BigReactors-Reactor")
  535.  
  536. if #newReactorList == 0 then
  537. printLog("No reactors found!")
  538. error("Can't find any reactors!")
  539. else -- Placeholder
  540. for reactorIndex = 1, #newReactorList do
  541. local reactor = nil
  542. reactor = newReactorList[reactorIndex]
  543.  
  544. if not reactor then
  545. printLog("reactorList["..reactorIndex.."] in findReactors() was not a valid Big Reactor")
  546. return -- Invalid reactorIndex
  547. end
  548.  
  549. -- If number of found reactors changed, re-initialize them all for now
  550. -- For now, initialize reactors to the same baseControlRodLevel
  551. if #newReactorList ~= #reactorList then
  552. reactor.setAllControlRodLevels(baseControlRodLevel)
  553.  
  554. -- Auto-start reactor when needed (e.g. program startup) by default, or use existing value
  555. autoStart[reactorIndex] = true
  556. end -- if #newReactorList ~= #reactorList then
  557. end -- for reactorIndex = 1, #newReactorList do
  558. end -- if #newReactorList == 0 then
  559.  
  560. -- Overwrite old reactor list with the now updated list
  561. reactorList = newReactorList
  562.  
  563. -- Check if we have enough monitors for the number of reactors
  564. if (#reactorList ~= 1) and ((#turbineList + #reactorList) + 1 < #monitorList) then
  565. printLog("You need "..(#reactorList + 1).." monitors for your "..#reactorList.." connected reactors")
  566. end
  567.  
  568. -- Start turbine monitor offset after reactors get monitors
  569. -- This assumes that there is a monitor for each turbine and reactor, plus the overall monitor display
  570. turbineMonitorOffset = #reactorList + 1 -- #turbineList will start at "1" if turbines found and move us just beyond #reactorList and status monitor range
  571. end -- function findReactors()
  572.  
  573.  
  574. -- Initialize all Big Reactors - Turbines
  575. local function findTurbines()
  576. -- Empty out old list of turbines
  577. newTurbineList = {}
  578.  
  579. printLog("Finding turbines...")
  580. newTurbineList, turbineNames = getDevices("BigReactors-Turbine")
  581.  
  582. if #newTurbineList == 0 then
  583. printLog("No turbines found") -- Not an error
  584. else
  585. for turbineIndex = 1, #newTurbineList do
  586. local turbine = nil
  587. turbine = newTurbineList[turbineIndex]
  588.  
  589. if not turbine then
  590. printLog("turbineList["..turbineIndex.."] is not a valid Big Reactors Turbine")
  591. return -- Invalid turbineIndex
  592. end
  593.  
  594. -- If number of found turbines changed, re-initialize them all for now
  595. if #newTurbineList ~= #turbineList then
  596. -- Default is to allow flow rate auto-adjust
  597. turbineFlowRateOverride[turbineIndex] = false
  598. end -- if #newTurbineList ~= #turbineList then
  599. end -- for turbineIndex = 1, #newTurbineList do
  600.  
  601. -- Overwrite old turbine list with the now updated list
  602. turbineList = newTurbineList
  603.  
  604. -- Check if we have enough monitors for the number of turbines
  605. if #monitorList < (#reactorList + #turbineList + 1) then
  606. printLog("You need "..(#reactorList + #turbineList + 1).." monitors for your "..#reactorList.." connected reactors and "..#turbineList.." connected turbines")
  607. end
  608. end -- if #newTurbineList == 0 then
  609. end -- function findTurbines()
  610.  
  611.  
  612. -- Return current energy buffer in a specific reactor by %
  613. local function getDeviceStoredEnergyBufferPercent(device)
  614. if not device then
  615. printLog("getDeviceStoredEnergyBufferPercent() did not receive a valid Big Reactor device")
  616. return -- Invalid reactorIndex
  617. end
  618.  
  619. local energyBufferStorage = device.getEnergyStored()
  620. return (math.floor(energyBufferStorage/100000)) -- (buffer/10000000 RF)*100%
  621. end -- function getDeviceStoredEnergyBufferPercent(reactorIndex)
  622.  
  623.  
  624. -- Modify reactor control rod levels to keep temperature with defined parameters, but
  625. -- wait an in-game half-hour for the temperature to stabalize before modifying again
  626. local function temperatureControl(reactorIndex)
  627. local reactor = nil
  628. reactor = reactorList[reactorIndex]
  629. if not reactor then
  630. printLog("reactorList["..reactorIndex.."] in temperatureControl() was not a valid Big Reactor")
  631. return -- Invalid reactorIndex
  632. end
  633.  
  634. local rodPercentage = math.ceil(reactor.getControlRodLevel(0))
  635. local reactorTemp = math.ceil(reactor.getFuelTemperature())
  636. local localMinReactorTemp, localMaxReactorTemp = minReactorTemp, maxReactorTemp
  637.  
  638. -- No point modifying control rod levels for temperature if the reactor is offline
  639. if reactor.getActive() then
  640. -- Actively cooled reactors should range between 0^C-300^C
  641. if reactor.isActivelyCooled() then
  642. localMinReactorTemp = 0
  643. localMaxReactorTemp = 300
  644. end
  645.  
  646. -- Don't bring us to 100, that's effectively a shutdown
  647. if (reactorTemp > localMaxReactorTemp) and (rodPercentage ~= 99) then
  648. -- If more than double our maximum temperature, increase rodPercentage faster
  649. if reactorTemp > (2 * localMaxReactorTemp) then
  650. -- Check bounds, Big Reactor doesn't do this for us. :)
  651. if (rodPercentage + (10 * controlRodAdjustAmount)) > 99 then
  652. reactor.setAllControlRodLevels(99)
  653. else
  654. reactor.setAllControlRodLevels(rodPercentage + (10 * controlRodAdjustAmount))
  655. end
  656. else
  657. -- Check bounds, Big Reactor doesn't do this for us. :)
  658. if (rodPercentage + controlRodAdjustAmount) > 99 then
  659. reactor.setAllControlRodLevels(99)
  660. else
  661. reactor.setAllControlRodLevels(rodPercentage + controlRodAdjustAmount)
  662. end
  663. end -- if reactorTemp > (2 * localMaxReactorTemp) then
  664. elseif (reactorTemp < localMinReactorTemp) and (rodPercentage ~= 0) then
  665. -- If less than half our minimum temperature, decrease rodPercentage faster
  666. if reactorTemp < (localMinReactorTemp / 2) then
  667. -- Check bounds, Big Reactor doesn't do this for us. :)
  668. if (rodPercentage - (10 * controlRodAdjustAmount)) < 0 then
  669. reactor.setAllControlRodLevels(0)
  670. else
  671. reactor.setAllControlRodLevels(rodPercentage - (10 * controlRodAdjustAmount))
  672. end
  673. else
  674. -- Check bounds, Big Reactor doesn't do this for us. :)
  675. if (rodPercentage - controlRodAdjustAmount) < 0 then
  676. reactor.setAllControlRodLevels(0)
  677. else
  678. reactor.setAllControlRodLevels(rodPercentage - controlRodAdjustAmount)
  679. end
  680. end -- if reactorTemp < (localMinReactorTemp / 2) then
  681.  
  682. baseControlRodLevel = rodPercentage
  683. end -- if (reactorTemp > localMaxReactorTemp) and (rodPercentage < 99) then
  684. end -- if reactor.getActive() then
  685. end -- function temperatureControl(reactorIndex)
  686.  
  687.  
  688. -- Load saved reactor parameters if ReactorOptions file exists
  689. local function loadReactorOptions()
  690. local reactorOptions = fs.open("ReactorOptions", "r") -- See http://computercraft.info/wiki/Fs.open
  691.  
  692. if reactorOptions then
  693. baseControlRodLevel = reactorOptions.readLine()
  694. -- The following values were added by Lolmer
  695. minStoredEnergyPercent = reactorOptions.readLine()
  696. maxStoredEnergyPercent = reactorOptions.readLine()
  697. minReactorTemp = reactorOptions.readLine()
  698. maxReactorTemp = reactorOptions.readLine()
  699. reactorRodOverride = reactorOptions.readLine() -- Should be string "true" or "false"
  700.  
  701. -- If we succeeded in reading a string, convert it to a number
  702. if baseControlRodLevel ~= nil then
  703. baseControlRodLevel = tonumber(baseControlRodLevel)
  704. end
  705.  
  706. if minStoredEnergyPercent ~= nil then
  707. minStoredEnergyPercent = tonumber(minStoredEnergyPercent)
  708. end
  709.  
  710. if maxStoredEnergyPercent ~= nil then
  711. maxStoredEnergyPercent = tonumber(maxStoredEnergyPercent)
  712. end
  713.  
  714. if minReactorTemp ~= nil then
  715. minReactorTemp = tonumber(minReactorTemp)
  716. end
  717.  
  718. if maxReactorTemp ~= nil then
  719. maxReactorTemp = tonumber(maxReactorTemp)
  720. end
  721.  
  722. if reactorRodOverride == "true" then
  723. reactorRodOverride = true
  724. else
  725. reactorRodOverride = false
  726. end
  727.  
  728. reactorOptions.close()
  729. end -- if reactorOptions then
  730.  
  731. -- Set default values if we failed to read any of the above
  732. if baseControlRodLevel == nil then
  733. baseControlRodLevel = 90
  734. end
  735.  
  736. if minStoredEnergyPercent == nil then
  737. minStoredEnergyPercent = 15
  738. end
  739.  
  740. if maxStoredEnergyPercent == nil then
  741. maxStoredEnergyPercent = 85
  742. end
  743.  
  744. if minReactorTemp == nil then
  745. minReactorTemp = 850
  746. end
  747.  
  748. if maxReactorTemp == nil then
  749. maxReactorTemp = 950
  750. end
  751. end -- function loadReactorOptions()
  752.  
  753.  
  754. -- Save our reactor parameters
  755. local function saveReactorOptions()
  756. local reactorOptions = fs.open("ReactorOptions", "w") -- See http://computercraft.info/wiki/Fs.open
  757.  
  758. -- If we can save the files, save them
  759. if reactorOptions then
  760. local reactorIndex = 1
  761. reactorOptions.writeLine(math.ceil(reactorList[1].getControlRodLevel(0))) -- Store just the first reactor for now
  762. -- The following values were added by Lolmer
  763. reactorOptions.writeLine(minStoredEnergyPercent)
  764. reactorOptions.writeLine(maxStoredEnergyPercent)
  765. reactorOptions.writeLine(minReactorTemp)
  766. reactorOptions.writeLine(maxReactorTemp)
  767. reactorOptions.writeLine(reactorRodOverride)
  768. reactorOptions.close()
  769. else
  770. printLog("Failed to open file ReactorOptions for writing!")
  771. end -- if reactorOptions then
  772. end -- function saveReactorOptions()
  773.  
  774.  
  775. local function displayReactorBars(barParams)
  776. -- Default to first reactor and first monitor
  777. setmetatable(barParams,{__index={reactorIndex=1, monitorIndex=1}})
  778. local reactorIndex, monitorIndex =
  779. barParams[1] or barParams.reactorIndex,
  780. barParams[2] or barParams.monitorIndex
  781.  
  782. -- Grab current monitor
  783. local monitor = nil
  784. monitor = monitorList[monitorIndex]
  785. if not monitor then
  786. printLog("monitorList["..monitorIndex.."] in displayReactorBars() was not a valid monitor")
  787. return -- Invalid monitorIndex
  788. end
  789.  
  790. -- Grab current reactor
  791. local reactor = nil
  792. reactor = reactorList[reactorIndex]
  793. if not reactor then
  794. printLog("reactorList["..reactorIndex.."] in displayReactorBars() was not a valid Big Reactor")
  795. return -- Invalid reactorIndex
  796. end
  797.  
  798. -- Draw border lines
  799. local width, height = monitor.getSize()
  800.  
  801. for i=3, 5 do
  802. monitor.setCursorPos(22, i)
  803. monitor.write("|")
  804. end
  805.  
  806. drawLine(2, monitorIndex)
  807. drawLine(6, monitorIndex)
  808.  
  809. -- Draw some text
  810. local fuelString = "Fuel: "
  811. local tempString = "Temp: "
  812. local energyBufferString = "Producing: "
  813.  
  814. local padding = math.max(string.len(fuelString), string.len(tempString), string.len(energyBufferString))
  815.  
  816. local fuelPercentage = math.ceil(reactor.getFuelAmount()/reactor.getFuelAmountMax()*100)
  817. print{fuelString,2,3,monitorIndex}
  818. print{fuelPercentage.." %",padding+2,3,monitorIndex}
  819.  
  820. local reactorTemp = math.ceil(reactor.getFuelTemperature())
  821. print{tempString,2,5,monitorIndex}
  822. print{reactorTemp.." C",padding+2,5,monitorIndex}
  823.  
  824. local rodPercentage = math.ceil(reactor.getControlRodLevel(0))
  825. -- Allow controlling Reactor Control Rod Level from GUI
  826. -- Decrease rod button: 23X, 4Y
  827. -- Increase rod button: 28X, 4Y
  828. if (xClick == 23) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  829. --Decrease rod level by amount
  830. newRodPercentage = rodPercentage - (5 * controlRodAdjustAmount)
  831. if newRodPercentage < 0 then
  832. newRodPercentage = 0
  833. end
  834. sideClick, xClick, yClick = 0, 0, 0
  835.  
  836. reactor.setAllControlRodLevels(newRodPercentage)
  837.  
  838. -- Save updated rod percentage
  839. baseControlRodLevel = newRodPercentage
  840. rodPercentage = newRodPercentage
  841. end -- if (xClick == 23) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  842.  
  843. if (xClick == 29) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  844. --Increase rod level by amount
  845. newRodPercentage = rodPercentage + (5 * controlRodAdjustAmount)
  846. if newRodPercentage > 100 then
  847. newRodPercentage = 100
  848. end
  849. sideClick, xClick, yClick = 0, 0, 0
  850.  
  851. reactor.setAllControlRodLevels(newRodPercentage)
  852.  
  853. -- Save updated rod percentage
  854. baseControlRodLevel = newRodPercentage
  855. rodPercentage = newRodPercentage
  856. end -- if (xClick == 29) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  857.  
  858. print{"Rod (%)",23,3,monitorIndex}
  859. print{"< >",23,4,monitorIndex}
  860. print{rodPercentage,25,4,monitorIndex}
  861.  
  862. -- getEnergyProducedLastTick() is used for both RF/t (passively cooled) and mB/t (actively cooled)
  863. local energyBuffer = reactor.getEnergyProducedLastTick()
  864. print{energyBufferString,2,4,monitorIndex}
  865.  
  866. -- Actively cooled reactors do not produce energy, only hot fluid mB/t to be used in a turbine
  867. -- still uses getEnergyProducedLastTick for mB/t of hot fluid generated
  868. if not reactor.isActivelyCooled() then
  869. -- Draw stored energy buffer bar
  870. drawBar(2,8,28,8,colors.gray,monitorIndex)
  871.  
  872. local curStoredEnergyPercent = getDeviceStoredEnergyBufferPercent(reactor)
  873. if curStoredEnergyPercent > 4 then
  874. drawBar(2, 8, math.floor(26*curStoredEnergyPercent/100)+2, 8, colors.yellow, monitorIndex)
  875. elseif curStoredEnergyPercent > 0 then
  876. drawPixel(2, 8, colors.yellow, monitorIndex)
  877. end -- if curStoredEnergyPercent > 4 then
  878.  
  879. print{"Energy Buffer",2,7,monitorIndex}
  880. print{curStoredEnergyPercent, width-(string.len(curStoredEnergyPercent)+3),7,monitorIndex}
  881. print{"%",28,7,monitorIndex}
  882.  
  883. print{math.ceil(energyBuffer).." RF/t",padding+2,4,monitorIndex}
  884. else
  885. print{math.ceil(energyBuffer).." mB/t",padding+2,4,monitorIndex}
  886. end -- if not reactor.isActivelyCooled() then
  887.  
  888. -- Print rod override status
  889. local reactorRodOverrideStatus = ""
  890.  
  891. print{"Rod Auto-adjust:",2,9,monitorIndex}
  892.  
  893. if not reactorRodOverride then
  894. reactorRodOverrideStatus = "Enabled"
  895. monitor.setTextColor(colors.green)
  896. else
  897. reactorRodOverrideStatus = "Disabled"
  898. monitor.setTextColor(colors.red)
  899. end -- if not reactorRodOverride then
  900.  
  901. print{reactorRodOverrideStatus, width - string.len(reactorRodOverrideStatus) - 1, 9, monitorIndex}
  902. monitor.setTextColor(colors.white)
  903.  
  904. local numRods = reactor.getNumberOfControlRods() - 1 -- Call every time as some people modify their reactor without rebooting the computer
  905.  
  906. print{"Reactivity: "..math.ceil(reactor.getFuelReactivity()).." %", 2, 10, monitorIndex}
  907. print{"Fuel: "..round(reactor.getFuelConsumedLastTick(),3).." mB/t", 2, 11, monitorIndex}
  908. print{"Waste: "..reactor.getWasteAmount().." mB", width-(string.len(reactor.getWasteAmount())+10), 11, monitorIndex}
  909.  
  910. monitor.setTextColor(colors.blue)
  911. printCentered(reactorNames[reactorIndex],12,monitorIndex)
  912. monitor.setTextColor(colors.white)
  913. end -- function displayReactorBars(barParams)
  914.  
  915.  
  916. local function reactorStatus(statusParams)
  917. -- Default to first reactor and first monitor
  918. setmetatable(statusParams,{__index={reactorIndex=1, monitorIndex=1}})
  919. local reactorIndex, monitorIndex =
  920. statusParams[1] or statusParams.reactorIndex,
  921. statusParams[2] or statusParams.monitorIndex
  922.  
  923. -- Grab current monitor
  924. local monitor = nil
  925. monitor = monitorList[monitorIndex]
  926. if not monitor then
  927. printLog("monitorList["..monitorIndex.."] in reactorStatus() was not a valid monitor")
  928. return -- Invalid monitorIndex
  929. end
  930.  
  931. -- Grab current reactor
  932. local reactor = nil
  933. reactor = reactorList[reactorIndex]
  934. if not reactor then
  935. printLog("reactorList["..reactorIndex.."] in reactorStatus() was not a valid Big Reactor")
  936. return -- Invalid reactorIndex
  937. end
  938.  
  939. local width, height = monitor.getSize()
  940. local reactorStatus = ""
  941.  
  942. if reactor.getConnected() then
  943. if reactor.getActive() then
  944. reactorStatus = "ONLINE"
  945. monitor.setTextColor(colors.green)
  946. else
  947. reactorStatus = "OFFLINE"
  948. monitor.setTextColor(colors.red)
  949. end -- if reactor.getActive() then
  950.  
  951. if xClick >= (width - string.len(reactorStatus) - 1) and xClick <= (width-1) and (sideClick == monitorNames[monitorIndex]) then
  952. if yClick == 1 then
  953. reactor.setActive(not reactor.getActive()) -- Toggle reactor status
  954. sideClick, xClick, yClick = 0, 0, 0 -- Reset click after we register it
  955.  
  956. -- If someone offlines the reactor (offline after a status click was detected), then disable autoStart
  957. if not reactor.getActive() then
  958. autoStart[reactorIndex] = false
  959. end
  960. end -- if yClick == 1 then
  961. end -- if (xClick >= (width - string.len(reactorStatus) - 1) and xClick <= (width-1)) and (sideClick == monitorNames[monitorIndex]) then
  962.  
  963. -- Allow disabling rod level auto-adjust and only manual rod level control
  964. if ((xClick > 23 and xClick < 28 and yClick == 4)
  965. or (xClick > 20 and xClick < 27 and yClick == 9))
  966. and (sideClick == monitorNames[monitorIndex]) then
  967. reactorRodOverride = not reactorRodOverride -- Toggle reactor rod override status
  968. sideClick, xClick, yClick = 0, 0, 0 -- Reset click after we register it
  969. end -- if (xClick > 23) and (xClick < 28) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  970.  
  971. else
  972. reactorStatus = "DISCONNECTED"
  973. monitor.setTextColor(colors.red)
  974. end -- if reactor.getConnected() then
  975.  
  976. print{reactorStatus, width - string.len(reactorStatus) - 1, 1, monitorIndex}
  977. monitor.setTextColor(colors.white)
  978. end -- function reactorStatus(statusParams)
  979.  
  980.  
  981. -- Display all found reactors' status to monitor 1
  982. -- This is only called if multiple reactors and/or a reactor plus at least one turbine are found
  983. local function displayAllStatus()
  984. local reactor, turbine = nil, nil
  985. local onlineReactor, onlineTurbine = 0, 0
  986. local totalReactorRF, totalReactorSteam, totalTurbineRF = 0, 0, 0
  987. local totalReactorFuelConsumed = 0
  988. local totalCoolantStored, totalSteamStored, totalEnergy, totalMaxEnergyStored = 0, 0, 0, 0 -- Total turbine and reactor energy buffer and overall capacity
  989. local maxSteamStored = (2000*#turbineList)+(5000*#reactorList)
  990. local maxCoolantStored = (2000*#turbineList)+(5000*#reactorList)
  991.  
  992. local monitor, monitorIndex = nil, 1
  993. monitor = monitorList[monitorIndex]
  994. if not monitor then
  995. printLog("monitorList["..monitorIndex.."] in reactorStatus() was not a valid monitor")
  996. return -- Invalid monitorIndex
  997. end
  998.  
  999. for reactorIndex = 1, #reactorList do
  1000. reactor = reactorList[reactorIndex]
  1001. if not reactor then
  1002. printLog("reactorList["..reactorIndex.."] in main() was not a valid Big Reactor")
  1003. break -- Invalid reactorIndex
  1004. end -- if not reactor then
  1005.  
  1006. if reactor.getConnected() then
  1007. if reactor.getActive() then
  1008. onlineReactor = onlineReactor + 1
  1009. totalReactorFuelConsumed = totalReactorFuelConsumed + reactor.getFuelConsumedLastTick()
  1010. end -- reactor.getActive() then
  1011.  
  1012. -- Actively cooled reactors do not produce or store energy
  1013. if not reactor.isActivelyCooled() then
  1014. totalMaxEnergyStored = totalMaxEnergyStored + 10000000 -- Reactors store 10M RF
  1015. totalEnergy = totalEnergy + reactor.getEnergyStored()
  1016. totalReactorRF = totalReactorRF + reactor.getEnergyProducedLastTick()
  1017. else
  1018. totalReactorSteam = totalReactorSteam + reactor.getEnergyProducedLastTick()
  1019. totalSteamStored = totalSteamStored + reactor.getHotFluidAmount()
  1020. totalCoolantStored = totalCoolantStored + reactor.getCoolantAmount()
  1021. end -- if not reactor.isActivelyCooled() then
  1022. end -- if reactor.getConnected() then
  1023. end -- for reactorIndex = 1, #reactorList do
  1024.  
  1025. for turbineIndex = 1, #turbineList do
  1026. turbine = turbineList[turbineIndex]
  1027. if not turbine then
  1028. printLog("turbineList["..turbineIndex.."] in main() was not a valid Big Reactor")
  1029. break -- Invalid turbineIndex
  1030. end -- if not turbine then
  1031.  
  1032. if turbine.getConnected() then
  1033. if turbine.getActive() then
  1034. onlineTurbine = onlineTurbine + 1
  1035. end
  1036.  
  1037. totalMaxEnergyStored = totalMaxEnergyStored + 1000000 -- Turbines store 1M RF
  1038. totalEnergy = totalEnergy + turbine.getEnergyStored()
  1039. totalTurbineRF = totalTurbineRF + turbine.getEnergyProducedLastTick()
  1040. totalSteamStored = totalSteamStored + turbine.getInputAmount()
  1041. totalCoolantStored = totalCoolantStored + turbine.getOutputAmount()
  1042. end -- if turbine.getConnected() then
  1043. end -- for turbineIndex = 1, #turbineList do
  1044.  
  1045. print{"Reactors online/found: "..onlineReactor.."/"..#reactorList, 2, 3, monitorIndex}
  1046. print{"Turbines online/found: "..onlineTurbine.."/"..#turbineList, 2, 4, monitorIndex}
  1047.  
  1048. if totalReactorRF ~= 0 then
  1049. monitor.setTextColor(colors.blue)
  1050. printRight("Reactor", 9, monitorIndex)
  1051. monitor.setTextColor(colors.white)
  1052. printRight(math.ceil(totalReactorRF).." (RF/t)", 10, monitorIndex)
  1053. end
  1054.  
  1055. if #turbineList then
  1056. -- Display liquids
  1057. monitor.setTextColor(colors.blue)
  1058. printLeft("Steam (mB)", 6, monitorIndex)
  1059. monitor.setTextColor(colors.white)
  1060. printLeft(math.ceil(totalSteamStored).."/"..maxSteamStored, 7, monitorIndex)
  1061. printLeft(math.ceil(totalReactorSteam).." mB/t", 8, monitorIndex)
  1062. monitor.setTextColor(colors.blue)
  1063. printRight("Coolant (mB)", 6, monitorIndex)
  1064. monitor.setTextColor(colors.white)
  1065. printRight(math.ceil(totalCoolantStored).."/"..maxCoolantStored, 7, monitorIndex)
  1066.  
  1067. monitor.setTextColor(colors.blue)
  1068. printLeft("Turbine", 9, monitorIndex)
  1069. monitor.setTextColor(colors.white)
  1070. printLeft(math.ceil(totalTurbineRF).." RF/t", 10, monitorIndex)
  1071. end -- if #turbineList then
  1072.  
  1073. printRight("Fuel: "..round(totalReactorFuelConsumed,3).." mB/t", 11, monitorIndex)
  1074. print{"Buffer: "..math.ceil(totalEnergy,3).."/"..totalMaxEnergyStored.." RF", 2, 12, monitorIndex}
  1075. end -- function displayAllStatus()
  1076.  
  1077.  
  1078. -- Get turbine status
  1079. local function displayTurbineBars(turbineIndex, monitorIndex)
  1080. -- Grab current monitor
  1081. local monitor = nil
  1082. monitor = monitorList[monitorIndex]
  1083. if not monitor then
  1084. printLog("monitorList["..monitorIndex.."] in displayTurbineBars() was not a valid monitor")
  1085. return -- Invalid monitorIndex
  1086. end
  1087.  
  1088. -- Grab current turbine
  1089. local turbine = nil
  1090. turbine = turbineList[turbineIndex]
  1091. if not turbine then
  1092. printLog("turbineList["..turbineIndex.."] in displayTurbineBars() was not a valid Big Turbine")
  1093. return -- Invalid turbineIndex
  1094. end
  1095.  
  1096. -- Draw border lines
  1097. local width, height = monitor.getSize()
  1098.  
  1099. for i=3, 5 do
  1100. monitor.setCursorPos(21, i)
  1101. monitor.write("|")
  1102. end
  1103.  
  1104. drawLine(2,monitorIndex)
  1105. drawLine(6,monitorIndex)
  1106.  
  1107. -- Allow controlling Turbine Flow Rate from GUI
  1108. -- Decrease flow rate button: 22X, 4Y
  1109. -- Increase flow rate button: 28X, 4Y
  1110. local turbineFlowRate = math.ceil(turbine.getFluidFlowRateMax())
  1111. if (xClick == 22) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  1112. --Decrease rod level by amount
  1113. newTurbineFlowRate = turbineFlowRate - flowRateAdjustAmount
  1114. if newTurbineFlowRate < 0 then
  1115. newTurbineFlowRate = 0
  1116. end
  1117. sideClick, xClick, yClick = 0, 0, 0
  1118.  
  1119. -- Check bounds [0,2000]
  1120. if newTurbineFlowRate > 2000 then
  1121. newTurbineFlowRate = 2000
  1122. elseif newTurbineFlowRate < 0 then
  1123. newTurbineFlowRate = 25 -- Don't go to zero, might as well power off
  1124. end
  1125.  
  1126. turbine.setFluidFlowRateMax(newTurbineFlowRate)
  1127.  
  1128. -- Save updated Turbine Flow Rate
  1129. turbineFlowRate = newTurbineFlowRate
  1130. end -- if (xClick == 22) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  1131.  
  1132. if (xClick == 29) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  1133. --Increase rod level by amount
  1134. newTurbineFlowRate = turbineFlowRate + flowRateAdjustAmount
  1135. if newTurbineFlowRate > 2000 then
  1136. newTurbineFlowRate = 2000
  1137. end
  1138. sideClick, xClick, yClick = 0, 0, 0
  1139.  
  1140. -- Check bounds [0,2000]
  1141. if newTurbineFlowRate > 2000 then
  1142. newTurbineFlowRate = 2000
  1143. elseif newTurbineFlowRate < 0 then
  1144. newTurbineFlowRate = 25 -- Don't go to zero, might as well power off
  1145. end
  1146.  
  1147. turbine.setFluidFlowRateMax(newTurbineFlowRate)
  1148.  
  1149. -- Save updated Turbine Flow Rate
  1150. turbineFlowRate = newTurbineFlowRate
  1151. end -- if (xClick == 29) and (yClick == 4) and (sideClick == monitorNames[monitorIndex]) then
  1152.  
  1153. print{" Flow",22,3,monitorIndex}
  1154. print{"< >",22,4,monitorIndex}
  1155. print{turbineFlowRate,23,4,monitorIndex}
  1156. print{" mB/t",22,5,monitorIndex}
  1157.  
  1158. local rotorSpeedString = "Speed: "
  1159. local energyBufferString = "Producing: "
  1160.  
  1161. local padding = math.max(string.len(rotorSpeedString), string.len(energyBufferString))
  1162.  
  1163. local energyBuffer = turbine.getEnergyProducedLastTick()
  1164. print{energyBufferString,1,4,monitorIndex}
  1165. print{math.ceil(energyBuffer).." RF/t",padding+1,4,monitorIndex}
  1166.  
  1167. local rotorSpeed = math.ceil(turbine.getRotorSpeed())
  1168. print{rotorSpeedString,1,5,monitorIndex}
  1169. print{rotorSpeed.." RPM",padding+1,5,monitorIndex}
  1170.  
  1171. -- PaintUtils only outputs to term., not monitor.
  1172. -- See http://www.computercraft.info/forums2/index.php?/topic/15540-paintutils-on-a-monitor/
  1173.  
  1174. -- Draw stored energy buffer bar
  1175. drawBar(1,8,28,8,colors.gray,monitorIndex)
  1176. --paintutils.drawLine(2, 8, 28, 8, colors.gray)
  1177.  
  1178. local energyedit = getDeviceStoredEnergyBufferPercent(turbine)
  1179. local curStoredEnergyPercent = math.floor(10*energyEdit)
  1180. if curStoredEnergyPercent > 4 then
  1181. drawBar(1, 8, math.floor(26*curStoredEnergyPercent/100)+2, 8, colors.yellow,monitorIndex)
  1182. elseif curStoredEnergyPercent > 0 then
  1183. drawPixel(1, 8, colors.yellow, monitorIndex)
  1184. end -- if curStoredEnergyPercent > 4 then
  1185.  
  1186. print{"Energy Buffer",1,7,monitorIndex}
  1187. print{curStoredEnergyPercent, width-(string.len(curStoredEnergyPercent)+3),7,monitorIndex}
  1188. print{"%",28,7,monitorIndex}
  1189.  
  1190. -- Print rod override status
  1191. local turbineFlowRateOverrideStatus = ""
  1192.  
  1193. print{"Flow Auto-adjust:",2,10,monitorIndex}
  1194.  
  1195. if not turbineFlowRateOverride[turbineIndex] then
  1196. turbineFlowRateOverrideStatus = "Enabled"
  1197. monitor.setTextColor(colors.green)
  1198. else
  1199. turbineFlowRateOverrideStatus = "Disabled"
  1200. monitor.setTextColor(colors.red)
  1201. end -- if not reactorRodOverride then
  1202.  
  1203. print{turbineFlowRateOverrideStatus, width - string.len(turbineFlowRateOverrideStatus) - 1, 10, monitorIndex}
  1204. monitor.setTextColor(colors.white)
  1205.  
  1206. monitor.setTextColor(colors.blue)
  1207. printCentered(turbineNames[turbineIndex],12,monitorIndex)
  1208. monitor.setTextColor(colors.white)
  1209.  
  1210. -- Need equation to figure out rotor efficiency and display
  1211. end -- function displayTurbineBars(statusParams)
  1212.  
  1213.  
  1214. -- Display turbine status
  1215. local function turbineStatus(turbineIndex, monitorIndex)
  1216. -- Grab current monitor
  1217. local monitor = nil
  1218. monitor = monitorList[monitorIndex]
  1219. if not monitor then
  1220. printLog("monitorList["..monitorIndex.."] in turbineStatus() was not a valid monitor")
  1221. return -- Invalid monitorIndex
  1222. end
  1223.  
  1224. -- Grab current turbine
  1225. local turbine = nil
  1226. turbine = turbineList[turbineIndex]
  1227. if not turbine then
  1228. printLog("turbineList["..turbineIndex.."] in turbineStatus() was not a valid Big Turbine")
  1229. return -- Invalid turbineIndex
  1230. end
  1231.  
  1232. local width, height = monitor.getSize()
  1233. local turbineStatus = ""
  1234.  
  1235. if turbine.getConnected() then
  1236. if turbine.getActive() then
  1237. turbineStatus = "ONLINE"
  1238. monitor.setTextColor(colors.green)
  1239. else
  1240. turbineStatus = "OFFLINE"
  1241. monitor.setTextColor(colors.red)
  1242. end -- if turbine.getActive() then
  1243.  
  1244. if (xClick >= (width - string.len(turbineStatus) - 1)) and (xClick <= (width-1)) and (sideClick == monitorNames[monitorIndex]) then
  1245. if yClick == 1 then
  1246. turbine.setActive(not turbine.getActive()) -- Toggle turbine status
  1247. sideClick, xClick, yClick = 0, 0, 0 -- Reset click after we register it
  1248. end -- if yClick == 1 then
  1249. end -- if (xClick >= (width - string.len(turbineStatus) - 1)) and (xClick <= (width-1)) and (sideClick == monitorNames[monitorIndex]) then
  1250.  
  1251. -- Allow disabling/enabling flow rate auto-adjust
  1252. if ((xClick > 23 and xClick < 28 and yClick == 4)
  1253. or (xClick > 20 and xClick < 27 and yClick == 10))
  1254. and (sideClick == monitorNames[monitorIndex]) then
  1255. turbineFlowRateOverride[turbineIndex] = not turbineFlowRateOverride[turbineIndex] -- Toggle turbine rod override status
  1256. sideClick, xClick, yClick = 0, 0, 0 -- Reset click after we register it
  1257. end
  1258.  
  1259. else
  1260. turbineStatus = "DISCONNECTED"
  1261. monitor.setTextColor(colors.red)
  1262. end -- if turbine.getConnected() then
  1263.  
  1264. print{turbineStatus, width - string.len(turbineStatus) - 1, 1, monitorIndex}
  1265. monitor.setTextColor(colors.white)
  1266. end -- function function turbineStatus(turbineIndex, monitorIndex)
  1267.  
  1268.  
  1269. -- Maintain Turbine flow rate at 900 or 1,800 RPM
  1270. local function flowRateControl(turbineIndex)
  1271. -- Grab current turbine
  1272. local turbine = nil
  1273. turbine = turbineList[turbineIndex]
  1274. if not turbine then
  1275. printLog("turbineList["..turbineIndex.."] in flowRateControl() was not a valid Big Turbine")
  1276. return -- Invalid turbineIndex
  1277. end
  1278.  
  1279. -- No point modifying control rod levels for temperature if the turbine is offline
  1280. if turbine.getActive() then
  1281. local flowRate = turbine.getFluidFlowRate()
  1282. local flowRateUserMax = math.ceil(turbine.getFluidFlowRateMax())
  1283. local rotorSpeed = math.ceil(turbine.getRotorSpeed())
  1284. local newFlowRate = 0
  1285.  
  1286. -- If we're not at max flow-rate and an optimal RPM, let's do something
  1287. -- also don't do anything if the current flow rate hasn't caught up to the user defined flow rate maximum
  1288. if (((rotorSpeed % 900) ~= 0) and (flowRate ~= 2000) and (flowRate == flowRateUserMax))
  1289. or (flowRate == 0) then
  1290. -- Make sure we are not going too fast
  1291. if rotorSpeed > 2700 then
  1292. newFlowRate = flowRateUserMax - flowRateAdjustAmount
  1293. -- Make sure we're not going too slow
  1294. elseif rotorSpeed < 900 then
  1295. newFlowRate = flowRateUserMax + flowRateAdjustAmount
  1296. -- We're not at optimal RPM or flow-rate and we're not out-of-bounds
  1297. else
  1298. return
  1299. end
  1300.  
  1301. -- Check bounds [0,2000]
  1302. if newFlowRate > 2000 then
  1303. newFlowRate = 2000
  1304. elseif newFlowRate < 0 then
  1305. newFlowRate = 25 -- Don't go to zero, might as well power off
  1306. end
  1307.  
  1308. turbine.setFluidFlowRateMax(newFlowRate)
  1309. end -- if ((rotorSpeed % 900) ~= 0) and (flowRate ~= 2000) and (flowRate == flowRateUserMax) then
  1310. end -- if turbine.getActive() then
  1311. end -- function flowRateControl(turbineIndex)
  1312.  
  1313.  
  1314. function main()
  1315. -- Load reactor parameters and initialize systems
  1316. loadReactorOptions()
  1317.  
  1318. -- Get our initial list of connected monitors and reactors
  1319. -- and initialize every cycle in case the connected devices change
  1320. findMonitors()
  1321. findReactors()
  1322. findTurbines()
  1323.  
  1324. while not finished do
  1325. local reactor = nil
  1326. local monitorIndex = 1
  1327.  
  1328. -- For multiple reactors/monitors, monitor #1 is reserved for overall status
  1329. -- or for multiple reactors/turbines and only one monitor
  1330. if (((#reactorList + #turbineList) > 1) and (#monitorList > 1)) or
  1331. (((#reactorList + #turbineList) > 1) and (#monitorList == 1)) then
  1332. local monitor = nil
  1333. monitor = monitorList[monitorIndex]
  1334. if not monitor then
  1335. printLog("monitorList["..monitorIndex.."] in turbineStatus() was not a valid monitor")
  1336. return -- Invalid monitorIndex
  1337. end
  1338.  
  1339. clearMonitor(progName.." "..progVer, monitorIndex) -- Clear monitor and draw borders
  1340. printCentered(progName.." "..progVer, 1, monitorIndex)
  1341. displayAllStatus()
  1342. monitorIndex = 2
  1343. end
  1344.  
  1345. -- Iterate through reactors
  1346. for reactorIndex = 1, #reactorList do
  1347. local monitor = nil
  1348. monitor = monitorList[monitorIndex]
  1349. if not monitor then
  1350. printLog("monitorList["..monitorIndex.."] in turbineStatus() was not a valid monitor")
  1351. return -- Invalid monitorIndex
  1352. end
  1353.  
  1354. local reactorMonitorIndex = monitorIndex + reactorIndex - 1 -- reactorIndex starts at 1
  1355.  
  1356. clearMonitor(progName, reactorMonitorIndex) -- Clear monitor and draw borders
  1357. printCentered(progName, 1, reactorMonitorIndex)
  1358.  
  1359. -- Display reactor status, includes "Disconnected" but found reactors
  1360. reactorStatus{reactorIndex, reactorMonitorIndex}
  1361.  
  1362. reactor = reactorList[reactorIndex]
  1363. if not reactor then
  1364. printLog("reactorList["..reactorIndex.."] in main() was not a valid Big Reactor")
  1365. break -- Invalid reactorIndex
  1366. end
  1367.  
  1368. if reactor.getConnected() then
  1369. local curStoredEnergyPercent = getDeviceStoredEnergyBufferPercent(reactor)
  1370.  
  1371. -- Shutdown reactor if current stored energy % is >= desired level, otherwise activate
  1372. -- First pass will have curStoredEnergyPercent=0 until displayBars() is run once
  1373. if curStoredEnergyPercent >= maxStoredEnergyPercent then
  1374. reactor.setActive(false)
  1375. -- Do not auto-start the reactor if it was manually powered off (autoStart=false)
  1376. elseif (curStoredEnergyPercent <= minStoredEnergyPercent) and (autoStart[reactorIndex] == true) then
  1377. reactor.setActive(true)
  1378. end
  1379.  
  1380. -- Don't try to auto-adjust control rods if manual control is requested
  1381. if not reactorRodOverride then
  1382. temperatureControl(reactorIndex)
  1383. end
  1384.  
  1385. displayReactorBars{reactorIndex,reactorMonitorIndex}
  1386. end -- if reactor.getConnected() then
  1387. end -- for reactorIndex = 1, #reactorList do
  1388.  
  1389. -- Monitors for turbines start after turbineMonitorOffset
  1390. for turbineIndex = 1, #turbineList do
  1391. local monitor = nil
  1392. monitor = monitorList[monitorIndex]
  1393. if not monitor then
  1394. printLog("monitorList["..monitorIndex.."] in turbineStatus() was not a valid monitor")
  1395. return -- Invalid monitorIndex
  1396. end
  1397.  
  1398. local turbineMonitorIndex = turbineIndex+turbineMonitorOffset
  1399. clearMonitor(progName, turbineMonitorIndex) -- Clear monitor and draw borders
  1400. printCentered(progName, 1, turbineMonitorIndex)
  1401.  
  1402. -- Display turbine status, includes "Disconnected" but found turbines
  1403. turbineStatus(turbineIndex, turbineMonitorIndex)
  1404.  
  1405. turbine = turbineList[turbineIndex]
  1406. if not turbine then
  1407. printLog("turbineList["..turbineIndex.."] in main() was not a valid Big Turbine")
  1408. break -- Invalid turbineIndex
  1409. end
  1410.  
  1411. if turbine.getConnected() then
  1412. if not turbineFlowRateOverride[turbineIndex] then
  1413. flowRateControl(turbineIndex)
  1414. end
  1415.  
  1416. displayTurbineBars(turbineIndex,turbineMonitorIndex)
  1417. end
  1418. end -- for reactorIndex = 1, #reactorList do
  1419.  
  1420. sleep(loopTime) -- Sleep
  1421. saveReactorOptions()
  1422. end -- while not finished do
  1423. end -- main()
  1424.  
  1425.  
  1426. local function eventHandler()
  1427. while not finished do
  1428. -- http://computercraft.info/wiki/Os.pullEvent
  1429. -- http://www.computercraft.info/forums2/index.php?/topic/1516-ospullevent-what-is-it-and-how-is-it-useful/
  1430. event, arg1, arg2, arg3 = os.pullEvent()
  1431.  
  1432. if event == "monitor_touch" then
  1433. sideClick, xClick, yClick = arg1, math.floor(arg2), math.floor(arg3)
  1434. printLog("Side: "..arg1.." Monitor touch X: "..xClick.." Y: "..yClick)
  1435. elseif event == "char" and not inManualMode then
  1436. local ch = string.lower(arg1)
  1437. if ch == "q" then
  1438. finished = true
  1439. elseif ch == "r" then
  1440. finished = true
  1441. os.reboot()
  1442. end -- if ch == "q" then
  1443. end -- if event == "monitor_touch" then
  1444. end -- while not finished do
  1445. end -- function eventHandler()
  1446.  
  1447.  
  1448. while not finished do
  1449. parallel.waitForAny(eventHandler, main)
  1450. sleep(loopTime)
  1451. end -- while not finished do
  1452.  
  1453.  
  1454. -- Clear up after an exit
  1455. term.clear()
  1456. term.setCursorPos(1,1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement