Advertisement
Guest User

secondsun.lua

a guest
Feb 17th, 2020
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.42 KB | None | 0 0
  1. local com = require("component")
  2. local evt = require("event")
  3. local thr = require("thread")
  4. local term = require("term")
  5. local shell = require("shell")
  6. local fs = require("filesystem")
  7. local cwd = shell.getWorkingDirectory()
  8. local reactor = com.draconic_reactor
  9. term.clear()
  10. if not fs.exists(cwd.."/secondsun.cfg") then
  11.   -- create config file
  12.   local _ = io.open("secondsun.cfg", "w")
  13.   print("Entering config utility...")
  14.   io.write("Max temperature: ")
  15.   _:write("MaxTemp "..io.read().."\n")
  16.   io.write("Max energy saturation percentage: ")
  17.   _:write("MaxSatPercent "..io.read().."\n")
  18.   io.write("Minmum field strength percentage: ")
  19.   _:write("MinStrengthPercent "..io.read().."\n")
  20.   io.write("Input flux gate address: ")
  21.   _:write("InputGate "..io.read().."\n")
  22.   io.write("Output flux gate address: ")
  23.   _:write("OutputGate "..io.read().."\n")
  24.   io.write("Input kRF/t: ")
  25.   _:write("InputEnergy "..io.read().."\n")
  26.   io.write("Output kRF/t: ")
  27.   _:write("OutputEnergy "..io.read().."\n")
  28.   _:close()
  29. end
  30. local configFile = io.open("secondsun.cfg")
  31. local configData = configFile:read("*a")
  32. local maxTemp = configData:match("MaxTemp%s%d*"):gsub("%D", "")
  33. local maxSatPercentage = configData:match("MaxSatPercent%s%d*"):gsub("%D", "")
  34. local maxSat = maxSatPercentage * 10000000
  35. local minStrPercentage = configData:match("MinStrengthPercent%s%d*"):gsub("%D", "")
  36. local minStr = minStrPercentage * 1000000
  37. local inGateAddr = configData:match("InputGate%s[%x%-]*"):gsub("InputGate%s", "")
  38. local outGateAddr = configData:match("OutputGate%s[%x%-]*"):gsub("OutputGate%s", "")
  39. local inAmount = configData:match("InputEnergy%s%d*"):gsub("%D", "") * 1000
  40. local outAmount = configData:match("OutputEnergy%s%d*"):gsub("%D", "") * 1000
  41. local inGate = component.proxy(inGateAddr)
  42. local outGate = component.proxy(outGateAddr)
  43. inGate.setOverrideEnabled(true)
  44. inGate.setFlowOverride(inAmount)
  45. outGate.setOverrideEnabled(true)
  46. outGate.setFlowOverride(outAmount)
  47. term.clear()
  48. local interruptThread = thr.create(function()
  49.   evt.pull("interrupted")
  50.   io.write("Are you sure you want to shutdown SecondSun (Y/N)? ")
  51.   local input = io.read()
  52.   if input:lower() == "y" then
  53.     print("Exiting..")
  54.     os.exit()
  55.   else
  56.     print("Abort.")
  57.     return
  58.   end
  59. end)
  60. local function getTempData(_, _, newTemp, oldTemp)
  61.   if newTemp > 8000 then
  62.     -- try and stop reactor
  63.     print("Warning: Temperature above max temperature of "..maxTemp..".")
  64.     print("Attempting to stop..")
  65.     local result = reactor.stopReactor()
  66.     if result == false then
  67.       print("Stop failed! RUN!")
  68.       for component in component.list("colorful_lamp") do
  69.         component.invoke(component, "setLampColor", tonumber("111110000000000", 2))
  70.       end
  71.       for component in component.list("os_alarm") do
  72.         component.invoke(component, "setAlarm", "klaxon2")
  73.         component.invoke(component, "activate")
  74.       end
  75.     end
  76.   end
  77. end
  78. local function getStatus(_, addr, newStatus, oldStatus)
  79.   print("Status of reactor "..addr.." changed from "..oldStatus.." to "..newStatus..".")
  80. end
  81. evt.listen("reactor_temp_change", getTempData)
  82. evt.listen("reactor_status_change", getStatus)
  83. print("SecondSun loaded into memory!")
  84. if reactor.getReactorInfo()["status"] == "cold" then
  85.   io.write("Start reactor (Y/N)? ")
  86.   local input = io.read()
  87.   if input:lower() == "y" then
  88.     reactor.chargeReactor()
  89.     print("Reactor started!")
  90.   else
  91.     print("Reactor not started. Please manually start reactor.")
  92.   end
  93. end
  94. thr.waitForAll(interruptThread)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement