Saereth

Untitled

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