blackrabt

BigReactors Monitor Server co

Mar 1st, 2015
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.86 KB | None | 0 0
  1.     if peripheral.isPresent("right") == true then
  2.       local wireless = peripheral.wrap("right")
  3.       --wireless.open(4)
  4.       wireless.open(65535)
  5.     end
  6.     --if peripheral.isPresent("BigReactors-Reactor_1") == false then
  7.     --  term.write("No Reactor Present!")
  8.       --shell.exit()
  9.     --end
  10.     --function loadPeripherals()
  11.       local modem = peripheral.wrap("top")
  12.       local monitor = peripheral.wrap("monitor_1")
  13.       local reactor = peripheral.wrap("BigReactors-Reactor_0")
  14.       --local reactor = peripheral.wrap("BigReactors-Reactor_1")
  15.       local turbine = peripheral.wrap("BigReactors-Turbine_0")
  16.       term.clear()
  17.       monitor.setBackgroundColor(32768)
  18.       monitor.clear()
  19.       term.setCursorPos(3,1)
  20.       wirelessSide = "right"
  21.       modemSide = "top"
  22.      
  23.      
  24.       --local wireless = peripheral.wrap(wirelessSide)
  25.       --local modem = peripheral.wrap(modemSide)
  26.       rednet.open(wirelessSide)
  27.       --if unexpected_condition then
  28.       --  error()
  29.       --end
  30.       --print(a[i])
  31.     --end
  32.    
  33.  
  34. function round2(num, places)
  35.         num = tostring(num)
  36.         local inc = false
  37.         if num = nil then num = 1 end --testing this piece out to avoid errors when the reactor doesn't report fuel usage for a tick
  38.         local decimal = string.find(num, "%.")
  39.         if num:len() - decimal <= places then return tonumber(num) end --already rounded, nothing to do.
  40.         local digit = tonumber(num:sub(decimal + places + 1))
  41.         num = num:sub(1, decimal + places)
  42.         if digit <= 4 then return tonumber(num) end --no incrementation needed, return truncated number
  43.         local newNum = ""
  44.         for i=num:len(), 1, -1 do
  45.                 digit = tonumber(num:sub(i))
  46.                 if digit == 9 then
  47.                         if i > 1 then
  48.                                 newNum = "0"..newNum
  49.                         else
  50.                                 newNum = "10"..newNum
  51.                         end
  52.                 elseif digit == nil then
  53.                         newNum = "."..newNum
  54.                 else
  55.                         if i > 1 then
  56.                                 newNum = num:sub(1,i-1)..(digit + 1)..newNum
  57.                         else
  58.                                 newNum = (digit + 1)..newNum
  59.                         end
  60.                         return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
  61.                 end
  62.         end
  63.         return tonumber(newNum)
  64. end
  65.  
  66.     function updateData()
  67.       --declare some reactor variables sucka!
  68.       fuelTemp = math.floor(reactor.getFuelTemperature())
  69.       reactorIsConnected = reactor.getConnected()
  70.       reactorIsActive = reactor.getActive()
  71.       controlRodCount = reactor.getNumberOfControlRods()
  72.       reactorCasingTemp = math.floor(reactor.getCasingTemperature()) --use math.floor to keep it a whole integer otherwise it has too many sigfigs.
  73.       reactorFuelAmount = reactor.getFuelAmount()
  74.       reactorWasteAmount = reactor.getWasteAmount()
  75.       reactorCoolantType = reactor.getCoolantType()
  76.       controlRodDesiredLevel = 0
  77.       reactorFuelUsed = reactor.getFuelConsumedLastTick()
  78.       reactorSteamAmount = reactor.getHotFluidAmount()
  79.       reactorSteamMade = reactor.getHotFluidProducedLastTick()
  80.       controlRodLevelOne = reactor.getControlRodLevel(0) --remember that the index is one less than rod count
  81.       controlRodLevelTwo = reactor.getControlRodLevel(1)
  82.       controlRodLevelThree = reactor.getControlRodLevel(2)
  83.       controlRodLevelFour = reactor.getControlRodLevel(3)
  84.       controlRodAverage = ((controlRodLevelOne + controlRodLevelTwo + controlRodLevelThree + controlRodLevelFour)) / (4)
  85.      
  86.       --turbine variables will go here if this turns out to be beneficial
  87.       idealRotorRPM = 1800
  88.       turbineIsConnected = turbine.getConnected()
  89.       turbineIsActive = turbine.getActive()
  90.       turbineRotorSpeed = math.floor(turbine.getRotorSpeed())
  91.       turbineFluidRate = turbine.getFluidFlowRate()
  92.       turbineEngaged = turbine.getInductorEngaged()
  93.       turbineEnergy = math.floor(turbine.getEnergyProducedLastTick())
  94.     end
  95.      
  96.     function tabletRequest()
  97.      
  98.       --local function openRednet()
  99.      
  100.       wirelessSide = "right"
  101.       modemSide = "top"
  102.      
  103.       local wireless = peripheral.wrap(wirelessSide)
  104.       local modem = peripheral.wrap(modemSide)
  105.       rednet.open(wirelessSide)
  106.       --end
  107.      
  108.      
  109.       --modemOn = openRednet()
  110.       if wirelessSide == nil then
  111.       print("No Modem")
  112.       print("Will shutdown in 3 seconds")
  113.       sleep(3)
  114.       --os.shutdown()
  115.       else
  116.       print("Opened modem on ".. wirelessSide)
  117.       end
  118.       os.sleep(0.2)
  119.       --term.clear() --i added this to make error messages more readable
  120.      
  121.      
  122.      
  123.       id, message = rednet.receive()
  124.       term.setCursorPos(1,1)
  125.       print ("Device " .. id .. " sent a " .. message .. " ")
  126.       os.sleep(1)
  127.       term.setCursorPos(1,2)
  128.       print ("Sending reactor info...")
  129.       local reactor
  130.          local reactor = peripheral.wrap("BigReactors-Reactor_0") --if networked replace with reactor name, this seems to interfere with line 46 at times
  131.          if peripheral.wrap("back") then
  132.          term.setCursorPos(1,3)
  133.          print ("Found Reactor")
  134.          end
  135.          active = reactor.getActive(1)
  136.          if active then
  137.          term.setCursorPos(1,5)
  138.          print ("The Reactor is: ")
  139.          term.setTextColor(colors.green)
  140.          print ("ON")
  141.          else term.setTextColor(colors.white)
  142.          print ("The Reactor is: ")
  143.          term.setTextColor(colors.red)
  144.          print ("OFF")
  145.          rednet.send(id, "The Reactor is: OFF")
  146.          sleep(.1)
  147.          rednet.send(id, "The Reactor is: OFF")
  148.          --os.reboot()
  149.          --runProgram()
  150.          --exit() --stops whole program
  151.          --break --only in a loop, at the end
  152.          return --i think this did it
  153.          end
  154.          term.setTextColor(colors.white)
  155.          lvl = reactor.getControlRodLevel(0)
  156.          if lvl then
  157.           print ("Control rod level is " .. lvl .. "% closed")
  158.          end
  159.          turbineEnergy = turbine.getEnergyProducedLastTick()
  160.          tick = turbineEnergy  --reactor.getEnergyProducedLastTick(1)
  161.          if tick then
  162.          print (" " .. tick .. " RF/t ")
  163.          end
  164.       rednet.send(id, "The Reactor is: ON")
  165.       os.sleep(0.2)
  166.       rednet.send(id, "Control rod level is " .. lvl .. "% closed")
  167.       os.sleep(0.2)
  168.       rednet.send(id, " Producing " .. tick .. " RF/t ")
  169.       return
  170.       --os.reboot()
  171.      
  172.       -- The Pocket computer program is at /bkc8M1Bi
  173.       -- Original code by Dukrobber
  174.     -- Updates by blackrabt
  175.     end
  176.      
  177.     function createDisplay()
  178.       --reactor display stuff
  179.       monitor.setBackgroundColor(32768) --black
  180.       monitor.setTextColor(512) --cyan
  181.       monitor.clear()
  182.       monitor.setCursorPos(4,1)
  183.       --set text color based on reactor status?
  184.       monitor.write("Reactor")
  185.       --term.redirect(monitor)
  186.       --paintutils.drawLine(3,3,7,3,1)
  187.       --term.redirect(term.native())
  188.       monitor.setCursorPos(2,2)
  189.       monitor.write("-----------") --___________
  190.       monitor.setCursorPos(3,3)
  191.       if reactorIsConnected == true then
  192.         monitor.setTextColor(8192)
  193.         monitor.write("Connected")
  194.       end
  195.       if reactorIsConnected == false then
  196.         monitor.setTextColor(4)
  197.         monitor.write("Disconnected")
  198.       end
  199.       monitor.setCursorPos(3,4)
  200.       if reactorIsActive == true then
  201.         monitor.setTextColor(32)
  202.         monitor.write("Active")
  203.       end
  204.       if reactorIsActive == false then
  205.         monitor.setTextColor(16)
  206.         monitor.write("Inactive")
  207.       end
  208.       --if peripheral.isPresent("left") == true then
  209.       --  testPDA.print(reactorIsActive)
  210.       --  testPDA.print("test")
  211.       --end
  212.       monitor.setTextColor(512)
  213.       monitor.setCursorPos(3,5)
  214.      
  215.       monitor.setTextColor(16) --yellow
  216.       monitor.write("Casing: "..reactorCasingTemp.."C")
  217.       monitor.setCursorPos(3,6)
  218.       if fuelTemp > 170 and fuelTemp < 230 then
  219.         monitor.setTextColor(32)
  220.       end
  221.       if fuelTemp < 170 and fuelTemp > 100 then
  222.         monitor.setTextColor(16)
  223.       end
  224.       if fuelTemp < 100 then
  225.         monitor.setTextColor(16384)
  226.       end
  227.       if fuelTemp > 230 and fuelTemp < 300 then
  228.         monitor.setTextColor(16)
  229.       end
  230.       if fuelTemp > 3000 then
  231.         monitor.setTextColor(16384)
  232.         monitor.setCursorPos(11,6)
  233.         monitor.write("CAUTION!")
  234.         --while fuelTemp > 300 do --this makes a flashing caution but it halts the whole program
  235.           --monitor.setTextColor(16384)
  236.           --monitor.setCursorPos(3,11)
  237.           --monitor.write("CAUTION!")
  238.           --sleep(0.4)
  239.           --monitor.setTextColor(64)
  240.           --monitor.setCursorPos(3,11)
  241.           --monitor.write("CAUTION!")
  242.           --sleep(0.4)
  243.           --monitor.setTextColor(2)
  244.           --monitor.setCursorPos(3,11)
  245.           --monitor.write("CAUTION!")
  246.         --end
  247.       end
  248.       monitor.write("Core:   "..fuelTemp.."C")
  249.       monitor.setCursorPos(3,7)
  250.       monitor.setTextColor(256)
  251.       monitor.write("HFO:    "..reactorSteamMade.."mB/t")
  252.       monitor.setCursorPos(3,9)
  253.       controlRods = math.floor(controlRodAverage)
  254.       monitor.write("Avg. Control Rod: "..controlRodAverage.."% ")
  255.       monitor.setCursorPos(3,8)
  256.       --monitor.write("FBR:    "..reactorFuelUsed.."mB/t")
  257.       --  f = tostring(reactorFuelUsed)
  258.       --f = reactorFuelUsed
  259.       --fuelFormatted = (string.format("%.4f", reactor.getFuelConsumedLastTick()))
  260.       fuelFormatted = round2(reactorFuelUsed, 2)
  261.      -- monitor.write(fuelFormatted)
  262.        monitor.write("FBR:    "..fuelFormatted.."mB/t")
  263.      
  264.         --turbine display
  265.       monitor.setTextColor(512) --cyan
  266.       monitor.setCursorPos(4,11)
  267.       monitor.write("Turbine")
  268.       monitor.setCursorPos(2,12)
  269.       monitor.write("-----------")
  270.       --term.redirect(monitor)
  271.       --paintutils.drawLine(18,3,25,3,512)
  272.       --term.redirect(term.native())
  273.       monitor.setCursorPos(3,13)
  274.       if turbineIsConnected == true then
  275.         monitor.setTextColor(8192)
  276.         monitor.write("Connected") --should say Connected
  277.       end
  278.       if turbineIsConnected == false then
  279.         monitor.setTextColor(16384)
  280.         monitor.write("Disconnected")
  281.       end
  282.       --monitor.write(turbineIsConnected)
  283.       monitor.setCursorPos(3,14)
  284.       if turbineIsActive == true then
  285.         monitor.setTextColor(8192)
  286.         monitor.write("Active")
  287.       end
  288.       if turbineIsActive == false then
  289.         monitor.setTextColor(16)
  290.         monitor.write("Inactive")
  291.       end
  292.       monitor.setCursorPos(3,15)
  293.       if turbineEngaged == true then
  294.         monitor.setTextColor(32)
  295.         monitor.write("Coils Engaged")
  296.       end
  297.       if turbineEngaged == false then
  298.         monitor.setTextColor(16)
  299.         monitor.write("Not Engaged")
  300.       end
  301.       --monitor.setCursorPos(15,15)
  302.       --monitor.write("15,15")
  303.       monitor.setCursorPos(3,16)
  304.       if turbineEnergy > 1000 and turbineEnergy < 10000 then
  305.         monitor.setTextColor(16)
  306.       end
  307.       if turbineEnergy > 10000 then
  308.         monitor.setTextColor(32)
  309.       end
  310.       if turbineEnergy < 1000 then
  311.         monitor.setTextColor(16384)
  312.       end
  313.       monitor.write("Generating:  "..turbineEnergy.."RF")
  314.       monitor.setCursorPos(3,17)
  315.       if turbineRotorSpeed > 0 then
  316.        
  317.         monitor.setTextColor(32)
  318.         monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  319.       end
  320.       if turbineRotorSpeed <1 then
  321.         monitor.setTextColor(16384)
  322.         monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  323.       end
  324.     end
  325.      
  326.     function runProgram()
  327.       --loadPeripherals() no need to make this run every time
  328.       updateData()
  329.       createDisplay()
  330.       coroutine.yield()
  331.       --tabletRequest()
  332.      
  333.       sleep(1.5)
  334.     end
  335.  
  336. -- function runAsCoroutine()
  337. --   runProgram()
  338. -- end
  339.  
  340. local theReactorCoroutine = coroutine.create(runProgram)
  341. local theTabletCoroutinr = coroutine.create(tabletRequest)
  342.  
  343. while true do
  344.   coroutine.resume(theReactorCoroutine)
  345.   if os.pullEvent(rednet.receive()) then
  346.     coroutine.yield(theReactorCoroutine)
  347.     coroutine.resume(theTabletCoroutinr)
  348.     coroutine.resume(theReactorCoroutine)
  349.   end
  350. end
  351.  
  352.     --[[need to pcall(round2)?]]
  353.     --this is supposed to run the peripheral loading in a protected call, and throw an error if there is a problem. It doesn't want to do that yet.
  354.     --if pcall(loadPeripherals) then
  355.     --  while true do
  356.     --    runProgram()
  357.     --  end
  358.       --runProgram()
  359.     --else
  360.       --there was a problem
  361.     --end
  362.      
  363.     -- while true do
  364.     --   runProgram()
  365.     --   if rednet.receive(2) then --not nil then
  366.     --     tabletRequest()
  367.     --   end
  368.     -- end
Advertisement
Add Comment
Please, Sign In to add comment