Advertisement
sammie287

PowergridNuclearSide.lua

Aug 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.00 KB | None | 0 0
  1. local component = require("component")
  2. local reactor = component.br_reactor
  3. local keyboard = require("keyboard")
  4.  
  5. local isConnected = reactor.getConnected()
  6. local isActive
  7. local energyStored
  8. local maxEnergyStored = 10000000
  9. local percentEnergyStored
  10. local fuelAmount
  11. local wasteAmount
  12. local fuelTemp
  13. local caseTemp
  14.  
  15. local modem = component.modem
  16. local event = require("event")
  17. local fs = require("filesystem") --I don't think this one is needed
  18.  
  19. local port = 70
  20. local timeout = 15
  21. local pullArray = {}
  22. local timeoutPull = {}
  23. --local serverAddress = "9955471e-b28f-4001-9d66-79c28c75be2d" whos address is this??????
  24. local serverAddress = "defb88c6-fe9b-4fd7-a236-c1b956c9023e"
  25.  
  26. local kyleTransformer
  27. local mattTransformer
  28. local powerString
  29.  
  30. local function GetComponent()
  31.   i = 1
  32.   local componentArray = {}
  33.   for k,v in component.list("ie_current_transformer") do
  34.     componentArray[i] = k
  35.     i = i + 1
  36.   end
  37.   return componentArray
  38. end
  39.  
  40. local function OpenModem(port)
  41.   modem.open(port)
  42.   if modem.isOpen(port) then
  43.     print("Successfully opened port " .. port)
  44.   else
  45.     print("Could not open port " .. port .. ", closing program.")
  46.     os.quit()
  47.   end
  48. end
  49.  
  50. local function Quit(port)
  51.   modem.close(port)
  52.   if modem.isOpen(port) == false then
  53.     print("Successfully closed port " .. port .. ", closing program.")
  54.   else
  55.     print("Could not close port " .. port .. ", contact admin.")
  56.   end
  57.   os.quit()
  58. end
  59.  
  60. local function EventPull(timeout)
  61.   local type, _, foreignAddress, port, distance, message = event.pull(timeout)
  62.   local pullArray = {}
  63.   if type == "key_down" then
  64.     if keyboard.isAltDown() then
  65.       Quit()
  66.     end
  67.   elseif type == "modem_message" then
  68.     pullArray[1] = type
  69.     pullArray[2] = foreignAddress
  70.     pullArray[3] = port
  71.     pullArray[4] = distance
  72.     pullArray[5] = message
  73.     print("Message '" .. message .. "' has been recieved from the DNS.")
  74.   end
  75.   return pullArray
  76. end
  77.  
  78. local function SendACK(remoteAddress, port)
  79.   modem.send(remoteAddress, port, "ACK")
  80.   print("ACK has been sent to " .. remoteAddress .. " on port " .. port)
  81. end
  82.  
  83. local function SendTimeout(remoteAddress, port, message, timeout)
  84.   timeoutPull = {}
  85.   while true do
  86.     modem.send(remoteAddress, port, message)
  87.     timeoutPull = EventPull(timeout)
  88.     if timeoutPull[5] == "ACK" then
  89.       print(message)
  90.       break
  91.     end
  92.   end
  93. end
  94.  
  95. local function PowerReactor(percentEnergyStored, isActive, fuelAmount, remoteAddress, port, timeout)
  96.   local offMessage = "Internal bank full, shutting off reactor."
  97.   local onMessage = "Internal bank empty, turning reactor on."
  98.   local fuelMessage = "The reactor is out of fuel and cannot be turned on."
  99.   if percentEnergyStored > 95 and isActive and fuelAmount >= 2000 then
  100.     reactor.setActive(false)
  101.     SendTimeout(remoteAddress, port, offMessage, timeout)
  102.   elseif percentEnergyStored < 15 and isActive == false and fuelAmount >= 2000 then
  103.     reactor.setActive(true)
  104.     SendTimeout(remoteAddress, port, onMessage, timeout)
  105.   elseif fuelAmount < 2000 then
  106.     SendTimeout(remoteAddress, port, fuelMessage, timeout)
  107.   end
  108. end
  109.  
  110. local function CheckConnection(isConnected, remoteAddress, port, timeout)
  111.   if isConnected == false then
  112.     SendTimeout(remoteAddress, port, "Nuclear computer is not connected to the reactor.", timeout)
  113.     Quit()
  114.   end
  115. end
  116.  
  117. local function TransformerSetup()
  118.   local transformerArray = GetComponent()
  119.   local kyle = transformerArray[2]
  120.   print("DEBUG KYLE: " .. kyle)
  121.   local matt = transformerArray[1]
  122.   print("DEBUG MATT: " .. matt)
  123.   mattTransformer = component.proxy(matt)
  124.   kyleTransformer = component.proxy(kyle)
  125. end
  126.  
  127. local function PowerRelay(kyle, matt)
  128.   local kylePower = kyle.getAvgEnergy() * 20
  129.   local mattPower = matt.getAvgEnergy() * 20
  130.   local stringThing = "REACTOR ON/OFF: " .. tostring(isActive) .. "\n" .. "Kyle: " .. kylePower .. " RF/s" .. "\n" .. "Matt: " .. mattPower .. " RF/s"
  131.   return stringThing
  132. end
  133.  
  134. OpenModem(port)
  135. TransformerSetup()
  136. CheckConnection(isConnected, serverAddress, port, timeout)
  137. print("Press Alt to quit. If that doesn't work hit that DANK power button.")
  138. while true do
  139.   energyStored = reactor.getEnergyStored()
  140.   percentEnergyStored = (energyStored/maxEnergyStored)*100
  141.   isActive = reactor.getActive()
  142.   fuelAmount = reactor.getFuelAmount()
  143.   wasteAmount = reactor.getWasteAmount()
  144.   fuelTemp = reactor.getFuelTemperature()
  145.   caseTemp = reactor.getCasingTemperature()
  146.   fuelIngots = fuelAmount/1000
  147.   if fuelIngots < 10 then
  148.     SendTimeout(serverAddress, port, "Only " .. fuelIngots .. " rods remain in the reactor.", timeout)
  149.   end
  150.   if caseTemp > 2000 or fuelTemp > 2000 then
  151.     SendTimeout(serverAddress, port, "Reactor overheating, shutting off.", timeout)
  152.     reactor.setActive(false)
  153.   end
  154.   PowerReactor(percentEnergyStored, isActive, fuelAmount, serverAddress, port, timeout)
  155.   powerString = PowerRelay(kyleTransformer, mattTransformer, isActive)
  156.   SendTimeout(serverAddress, port, powerString, timeout)
  157.   os.sleep(0.5)
  158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement