blackrabt

BRM version 0.1.4

Mar 3rd, 2015
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.29 KB | None | 0 0
  1.     --version 0.1.4
  2.   --fixed if true then statements to be cleaner
  3.   --supports 2 turbines
  4.   --detects if second turbine is present
  5.   --fixed stall on coroutine.yield()
  6.   --improved formatting
  7.   --checks for connections before attempting updateData()
  8.  
  9. function loadPeripherals()
  10. if peripheral.isPresent("left") then
  11.   local wireless = peripheral.wrap("left")
  12.   --wireless.open(4)
  13.   wireless.open(65535)
  14. end
  15. --if peripheral.isPresent("BigReactors-Reactor_1") == false then
  16. --  term.write("No Reactor Present!")
  17.   --shell.exit()
  18. --end
  19.   modem = peripheral.wrap("back")
  20.   monitor = peripheral.wrap("monitor_2")
  21.   if peripheral.isPresent("BigReactors-Reactor_1") then
  22.     reactor = peripheral.wrap("BigReactors-Reactor_1")
  23.     reactorPresent = true
  24.   end
  25.   if peripheral.isPresent("BigReactors-Turbine_0") then
  26.    turbine = peripheral.wrap("BigReactors-Turbine_0")
  27.    firstTurbinePresent = true
  28.   end
  29.   if peripheral.isPresent("BigReactors-Turbine_1") then
  30.     secondTurbinePresent = true
  31.     turbine2 = peripheral.wrap("BigReactors-Turbine_1")
  32.   end
  33.   term.clear()
  34.   monitor.setBackgroundColor(32768)
  35.   monitor.clear()
  36.   term.setCursorPos(3,1)
  37.   wirelessSide = "left"
  38.   modemSide = "top"
  39.   rednet.open(wirelessSide)
  40. end
  41.  
  42. function round2(num, places)
  43.   num = tostring(num)
  44.   local inc = false
  45.     if num == nil then
  46.       num = 1
  47.     end --testing this piece out to avoid errors when the reactor doesn't report fuel usage for a tick
  48.     local decimal = string.find(num, "%.")
  49.     if decimal == nil then
  50.       decimal = 1
  51.     end
  52.     if num:len() - decimal <= places then
  53.       return tonumber(num)
  54.     end --already rounded, nothing to do.
  55.     local digit = tonumber(num:sub(decimal + places + 1))
  56.     num = num:sub(1, decimal + places)
  57.     if digit <= 4 then
  58.       return tonumber(num)
  59.     end --no incrementation needed, return truncated number
  60.     local newNum = ""
  61.     for i=num:len(), 1, -1 do
  62.       digit = tonumber(num:sub(i))
  63.       if digit == 9 then
  64.         if i > 1 then
  65.           newNum = "0"..newNum
  66.         else
  67.           newNum = "10"..newNum
  68.         end
  69.       elseif digit == nil then
  70.         newNum = "."..newNum
  71.       else
  72.         if i > 1 then
  73.           newNum = num:sub(1,i-1)..(digit + 1)..newNum
  74.         else
  75.           newNum = (digit + 1)..newNum
  76.         end
  77.                     return tonumber(newNum) --No more 9s found, so we are done incrementing. Copy remaining digits, then return number.
  78.                   end
  79.                 end
  80.                 return tonumber(newNum)
  81.               end
  82.  
  83.               function updateData()
  84.   --declare some reactor variables sucka!
  85.   if reactorPresent then
  86.     fuelTempFloat = reactor.getFuelTemperature()
  87.     if fuelTempFloat == nil then
  88.       fuelTempFloat = 1
  89.     end
  90.     fuelTemp = math.floor(fuelTempFloat)
  91.     reactorIsConnected = reactor.getConnected()
  92.     reactorIsActive = reactor.getActive()
  93.     controlRodCount = reactor.getNumberOfControlRods()
  94.     reactorCasingTempFloat = reactor.getCasingTemperature()
  95.     if reactorCasingTempFloat == nil then
  96.       reactorCasingTempFloat = 0
  97.     end
  98.     reactorCasingTemp = math.floor(reactorCasingTempFloat) --use math.floor to keep it a whole integer otherwise it has too many sigfigs.
  99.     reactorFuelAmount = reactor.getFuelAmount()
  100.     reactorWasteAmount = reactor.getWasteAmount()
  101.     reactorCoolantType = reactor.getCoolantType()
  102.     controlRodDesiredLevel = 0
  103.     reactorFuelUsed = reactor.getFuelConsumedLastTick()
  104.     reactorSteamAmount = reactor.getHotFluidAmount()
  105.     reactorSteamMade = reactor.getHotFluidProducedLastTick()
  106.     controlRodLevelOne = reactor.getControlRodLevel(0) --remember that the index is one less than rod count. find a way to let this adjust for number of control rods automat
  107.     if controlRodLevelOne == nil then
  108.       controlRodLevelOne = 0
  109.     end
  110.     controlRodLevelTwo = reactor.getControlRodLevel(1)
  111.     if controlRodLevelTwo == nil then
  112.       controlRodLevelTwo = 0
  113.     end
  114.     controlRodLevelThree = reactor.getControlRodLevel(2)
  115.     if controlRodLevelThree == nil then
  116.       controlRodLevelThree = 0
  117.     end
  118.     controlRodLevelFour = reactor.getControlRodLevel(3)
  119.     if controlRodLevelFour == nil then
  120.       controlRodLevelFour = 0
  121.     end
  122.     controlRodAverage = ((controlRodLevelOne + controlRodLevelTwo + controlRodLevelThree + controlRodLevelFour)) / (4)
  123.   end
  124.   --turbine variables will go here if this turns out to be beneficial
  125.   if firstTurbinePresent then
  126.     idealRotorRPM = 1800
  127.     turbineIsConnected = turbine.getConnected()
  128.     turbineIsActive = turbine.getActive()
  129.     turbineRotorSpeedFloat = turbine.getRotorSpeed()
  130.     if turbineRotorSpeedFloat == nil then
  131.       turbineRotorSpeedFloat = 0
  132.     end
  133.     turbineRotorSpeed = math.floor(turbineRotorSpeedFloat)
  134.     turbineFluidRate = turbine.getFluidFlowRate()
  135.     turbineEngaged = turbine.getInductorEngaged()
  136.     turbineEnergyFloat = turbine.getEnergyProducedLastTick()
  137.     if turbineEnergyFloat == nil then
  138.       turbineEnergyFloat = 0
  139.     end
  140.     turbineEnergy = math.floor(turbineEnergyFloat)
  141.   end
  142.   --turbine 2
  143.   if secondTurbinePresent then
  144.     turbine2IsConnected = turbine2.getConnected(1)
  145.     idealRotorRPM2 = 1800
  146.     turbine2IsActive = turbine2.getActive()
  147.     turbine2RotorSpeedFloat = turbine2.getRotorSpeed()
  148.     if turbine2RotorSpeedFloat == nil then
  149.       turbine2RotorSpeedFloat = 0
  150.     end
  151.     turbine2RotorSpeed = math.floor(turbine2RotorSpeedFloat)
  152.     turbine2FluidRate = turbine2.getFluidFlowRate()
  153.     turbine2Engaged = turbine2.getInductorEngaged()
  154.     turbine2EnergyFloat = turbine2.getEnergyProducedLastTick()
  155.     if turbine2EnergyFloat == nil then
  156.       turbine2EnergyFloat = 0
  157.     end
  158.     turbine2Energy = math.floor(turbine2EnergyFloat)
  159.   end
  160. end
  161.  
  162. function tabletRequest()
  163.   wirelessSide = "left"
  164.   modemSide = "top"
  165.   local wireless = peripheral.wrap(wirelessSide)
  166.   local modem = peripheral.wrap(modemSide)
  167.   rednet.open(wirelessSide)
  168.   if wirelessSide == nil then
  169.     print("No Modem")
  170.     print("Will shutdown in 3 seconds")
  171.     sleep(3)
  172.   --os.shutdown()
  173.   else
  174.     print("Opened modem on ".. wirelessSide)
  175.   end
  176.   os.sleep(0.2)
  177.   id, message = rednet.receive()
  178.   term.setCursorPos(1,1)
  179.   print ("Device " .. id .. " sent a " .. message .. " ")
  180.   os.sleep(1)
  181.   term.setCursorPos(1,2)
  182.   print ("Sending reactor info...")
  183.   local reactor
  184.     local reactor = peripheral.wrap("BigReactors-Reactor_0")
  185.     if peripheral.wrap("back") then
  186.        term.setCursorPos(1,3)
  187.        print ("Found Reactor")
  188.     end
  189.     active = reactor.getActive(1)
  190.     if active then
  191.       term.setCursorPos(1,5)
  192.       print ("The Reactor is: ")
  193.       term.setTextColor(colors.green)
  194.       print ("ON")
  195.     else term.setTextColor(colors.white)
  196.       print ("The Reactor is: ")
  197.       term.setTextColor(colors.red)
  198.       print ("OFF")
  199.       rednet.send(id, "The Reactor is: OFF")
  200.       sleep(.1)
  201.       rednet.send(id, "The Reactor is: OFF")
  202.     --os.reboot()
  203.     --runProgram()
  204.     --exit() --stops whole program
  205.     --break --only in a loop, at the end
  206.     return --i think this did it
  207.   end
  208.   term.setTextColor(colors.white)
  209.   lvl = reactor.getControlRodLevel(0)
  210.   if lvl then
  211.     print ("Control rod level is " .. lvl .. "% closed")
  212.   end
  213.   turbineEnergy = turbine.getEnergyProducedLastTick()
  214.      tick = turbineEnergy  --reactor.getEnergyProducedLastTick(1)
  215.      if tick then
  216.        print (" " .. tick .. " RF/t ")
  217.      end
  218.      rednet.send(id, "The Reactor is: ON")
  219.      os.sleep(0.2)
  220.      rednet.send(id, "Control rod level is " .. lvl .. "% closed")
  221.      os.sleep(0.2)
  222.      rednet.send(id, " Producing " .. tick .. " RF/t ")
  223.      return
  224.   --os.reboot()
  225.   -- The Pocket computer program is at /bkc8M1Bi
  226.   -- Original code by Dukrobber
  227. -- Updates by blackrabt
  228. end
  229.  
  230. function createDisplayReactor()
  231.   --reactor display stuff
  232.   monitor.setBackgroundColor(32768) --black
  233.   monitor.setTextColor(512) --cyan
  234.   monitor.clear()
  235.   monitor.setCursorPos(4,1)
  236.   --set text color based on reactor status?
  237.   monitor.write("Reactor")
  238.   --term.redirect(monitor)
  239.   --paintutils.drawLine(3,3,7,3,1)
  240.   --term.redirect(term.native())
  241.   monitor.setCursorPos(2,2)
  242.   monitor.write("-----------") --___________
  243.   monitor.setCursorPos(3,3)
  244.   if reactorPresent then
  245.     if reactorIsConnected then
  246.       monitor.setTextColor(8192)
  247.       monitor.write("Connected")
  248.     end
  249.   --skip the rest of the reactor display sections in this event
  250.   monitor.setCursorPos(3,4)
  251.   if reactorIsActive then
  252.     monitor.setTextColor(32)
  253.     monitor.write("Active")
  254.   --if peripheral.isPresent("left") then
  255.   --  testPDA.print(reactorIsActive)
  256.   --  testPDA.print("test")
  257.   --end
  258.   monitor.setTextColor(512)
  259.   monitor.setCursorPos(3,5)
  260.  
  261.   monitor.setTextColor(16) --yellow
  262.   monitor.write("Casing: "..reactorCasingTemp.."C")
  263.   monitor.setCursorPos(3,6)
  264.   if fuelTemp > 170 and fuelTemp < 230 then
  265.     monitor.setTextColor(32)
  266.   end
  267.   if fuelTemp < 170 and fuelTemp > 100 then
  268.     monitor.setTextColor(16)
  269.   end
  270.   if fuelTemp < 100 then
  271.     monitor.setTextColor(16384)
  272.   end
  273.   if fuelTemp > 230 and fuelTemp < 300 then
  274.     monitor.setTextColor(16)
  275.   end
  276.   if fuelTemp > 3000 then
  277.     monitor.setTextColor(16384)
  278.     monitor.setCursorPos(11,6)
  279.     monitor.write("CAUTION!")
  280.     --while fuelTemp > 300 do --this makes a flashing caution but it halts the whole program
  281.       --monitor.setTextColor(16384)
  282.       --monitor.setCursorPos(3,11)
  283.       --monitor.write("CAUTION!")
  284.       --sleep(0.4)
  285.       --monitor.setTextColor(64)
  286.       --monitor.setCursorPos(3,11)
  287.       --monitor.write("CAUTION!")
  288.       --sleep(0.4)
  289.       --monitor.setTextColor(2)
  290.       --monitor.setCursorPos(3,11)
  291.       --monitor.write("CAUTION!")
  292.     --end
  293.   end
  294.   monitor.write("Core:   "..fuelTemp.."C")
  295.   monitor.setCursorPos(3,7)
  296.   monitor.setTextColor(256)
  297.   monitor.write("HFO:    "..reactorSteamMade.."mB/t")
  298.   monitor.setCursorPos(3,9)
  299.   controlRods = math.floor(controlRodAverage)
  300.   monitor.write("Avg. Control Rod: "..controlRodAverage.."% ")
  301.   monitor.setCursorPos(3,8)
  302.   --monitor.write("FBR:    "..reactorFuelUsed.."mB/t")
  303.   --  f = tostring(reactorFuelUsed)
  304.   --f = reactorFuelUsed
  305.   --fuelFormatted = (string.format("%.4f", reactor.getFuelConsumedLastTick()))
  306.   fuelFormatted = round2(reactorFuelUsed, 2)
  307.   -- monitor.write(fuelFormatted)
  308.   monitor.write("FBR:    "..fuelFormatted.."mB/t")
  309.   elseif reactorIsConnected == false then
  310.     monitor.setTextColor(4)
  311.     monitor.write("Disconnected")
  312.     return--skip the rest
  313.   end
  314.   elseif reactorIsActive == false then
  315.     monitor.setCursorPos(3,4)
  316.     monitor.setTextColor(16)
  317.     monitor.write("Inactive")
  318.     --return
  319.   end
  320. end
  321.  
  322. function createDisplayTurbine1()
  323.  
  324.     --turbine display arranged for a shorter and wider display
  325.   -- monitor.setTextColor(512) --cyan
  326.   -- monitor.setCursorPos(4,11)
  327.   -- monitor.write("Turbine Status    1           2")
  328.   -- monitor.setCursorPos(2,12)
  329.   -- monitor.write("-------------------------------")
  330.   -- --term.redirect(monitor)
  331.   -- --paintutils.drawLine(18,3,25,3,512)
  332.   -- --term.redirect(term.native())
  333.   -- monitor.setCursorPos(1,13)
  334.   -- monitor.setTextColor(64)
  335.   -- monitor.write("Network Status")
  336.  
  337.   -- monitor.setCursorPos(3,13)
  338.   -- if turbineIsConnected then
  339.   --   monitor.setTextColor(8192)
  340.   --   monitor.write("Connected") --should say Connected
  341.   -- end
  342.   -- if turbineIsConnected == false then
  343.   --   monitor.setTextColor(16384)
  344.   --   monitor.write("Disconnected")
  345.   -- end
  346.   -- monitor.write(turbineIsConnected)
  347.   -- monitor.setCursorPos(1,14)
  348.   -- monitor.setTextColor(64)
  349.   -- monitor.write("Device Status: ")
  350.  
  351.   -- monitor.setCursorPos(1,15)
  352.   -- monitor.setTextColor(64)
  353.   -- monitor.write("Coil Status: ")
  354.  
  355.   -- monitor.setCursorPos(1,16)
  356.   -- monitor.setTextColor(64)
  357.   -- monitor.write("Generation: ")
  358.  
  359.   -- monitor.setCursorPos(1,17)
  360.   -- monitor.setTextColor(64)
  361.   -- monitor.write("Rotor Speed: ")
  362.  
  363.   monitor.setTextColor(512) --cyan
  364.   monitor.setCursorPos(4,11)
  365.   monitor.write("Turbine 1")
  366.   monitor.setCursorPos(2,12)
  367.   monitor.write("-----------")
  368.   if firstTurbinePresent then
  369.     monitor.setCursorPos(3,13)
  370.     if turbineIsConnected then
  371.       monitor.setTextColor(8192)
  372.       monitor.write("Connected") --should say Connected
  373.     else
  374.       monitor.setTextColor(16384)
  375.       monitor.write("Disconnected")
  376.     end
  377.   end
  378.  
  379.   monitor.setCursorPos(3,14)
  380.   if turbineIsActive then
  381.     monitor.setTextColor(8192)
  382.     monitor.write("Active")
  383.   end
  384.   if turbineIsActive == false then
  385.     monitor.setTextColor(16)
  386.     monitor.write("Inactive")
  387.   end
  388.   monitor.setCursorPos(3,15)
  389.   if turbineEngaged then
  390.     monitor.setTextColor(32)
  391.     monitor.write("Coils Engaged")
  392.   end
  393.   if turbineEngaged == false then
  394.     monitor.setTextColor(16)
  395.     monitor.write("Not Engaged")
  396.   end
  397.   --monitor.setCursorPos(15,15)
  398.   --monitor.write("15,15")
  399.   monitor.setCursorPos(3,16)
  400.   if turbineEnergy > 1000 and turbineEnergy < 10000 then
  401.     monitor.setTextColor(16)
  402.   end
  403.   if turbineEnergy > 10000 then
  404.     monitor.setTextColor(32)
  405.   end
  406.   if turbineEnergy < 1000 then
  407.     monitor.setTextColor(16384)
  408.   end
  409.   monitor.write("Generating:  "..turbineEnergy.."RF")
  410.   monitor.setCursorPos(3,17)
  411.   if turbineRotorSpeed > 0 then
  412.  
  413.     monitor.setTextColor(32)
  414.     monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  415.   end
  416.   if turbineRotorSpeed <1 then
  417.     monitor.setTextColor(16384)
  418.     monitor.write("Rotor speed: "..turbineRotorSpeed.."RPM")
  419.   end
  420. end --ends createDisplayTurbine1() don't forget to update runProgram()
  421.     --TURBINE 2 GOES HERE
  422. function createDisplayTurbine2()
  423.   monitor.setTextColor(512) --cyan
  424.   monitor.setCursorPos(4,20)
  425.   monitor.write("Turbine 2")
  426.   monitor.setCursorPos(2,21)
  427.   monitor.write("-----------")
  428.  
  429.  
  430.   monitor.setCursorPos(3,22)
  431.   if secondTurbinePresent then  
  432.     if turbine2IsConnected then
  433.       monitor.setTextColor(8192)
  434.       monitor.write("Connected") --should say Connected
  435.     end
  436.   --monitor.write(turbineIsConnected)
  437.   monitor.setCursorPos(3,23)
  438.   if turbine2IsActive then
  439.     monitor.setTextColor(8192)
  440.     monitor.write("Active")
  441.   end
  442.   if turbine2IsActive == false then
  443.     monitor.setTextColor(16)
  444.     monitor.write("Inactive")
  445.   end
  446.   monitor.setCursorPos(3,24)
  447.   if turbineEngaged then
  448.     monitor.setTextColor(32)
  449.     monitor.write("Coils Engaged")
  450.   end
  451.   if turbine2Engaged == false then
  452.     monitor.setTextColor(16)
  453.     monitor.write("Not Engaged")
  454.   end
  455.   --monitor.setCursorPos(15,15)
  456.   --monitor.write("15,15")
  457.   monitor.setCursorPos(3,25)
  458.   if turbine2Energy > 1000 and turbine2Energy < 10000 then
  459.     monitor.setTextColor(16)
  460.   end
  461.   if turbine2Energy > 10000 then
  462.     monitor.setTextColor(32)
  463.   end
  464.   if turbine2Energy < 1000 then
  465.     monitor.setTextColor(16384)
  466.   end
  467.   monitor.write("Generating:  "..turbine2Energy.."RF")
  468.   monitor.setCursorPos(3,26)
  469.   if turbine2RotorSpeed > 0 then
  470.     monitor.setTextColor(32)
  471.     monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
  472.   end
  473.   if turbine2RotorSpeed <1 then
  474.     monitor.setTextColor(16384)
  475.     monitor.write("Rotor speed: "..turbine2RotorSpeed.."RPM")
  476.   end
  477.   else
  478.     if turbine2IsConnected == false then
  479.       monitor.setTextColor(16384)
  480.       monitor.write("Disconnected")
  481.     end
  482.     return
  483. end--end if statement
  484. end--end function
  485.  
  486.  
  487. function runProgram()
  488.   loadPeripherals()
  489.   updateData()
  490.   createDisplayReactor()
  491.   createDisplayTurbine1()
  492.   createDisplayTurbine2()
  493.   --coroutine.yield()
  494.   --tabletRequest()
  495.   sleep(1.5)
  496. end
  497.  
  498. -- function runAsCoroutine()
  499. --   runProgram()
  500. -- end
  501.  
  502. -- local theReactorCoroutine = coroutine.create(runProgram)
  503. -- local theTabletCoroutinr = coroutine.create(tabletRequest)
  504.  
  505. -- while true do
  506. --   coroutine.resume(theReactorCoroutine)
  507. --   if os.pullEvent(rednet.receive()) then
  508. --     coroutine.yield(theReactorCoroutine)
  509. --     coroutine.resume(theTabletCoroutinr)
  510. --     coroutine.resume(theReactorCoroutine)
  511. --   end
  512. -- end
  513.  
  514. --[[need to pcall(round2)?]]
  515. --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.
  516. --if pcall(loadPeripherals) then
  517. --  while true do
  518. --    runProgram()
  519. --  end
  520.   --runProgram()
  521. --else
  522.   --there was a problem
  523. --end
  524.  
  525. while true do
  526.   runProgram()
  527.   if rednet.receive(2) then --not nil then
  528.     tabletRequest()
  529.   end
  530. end
Advertisement
Add Comment
Please, Sign In to add comment