Advertisement
neuroticfox

Untitled

Sep 5th, 2019
1,007
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 38.77 KB | None | 0 0
  1. --dercc (Draconic Evolution Reactor Complete Control) V1.1
  2. --Official Stamper (c) Aug 2018
  3.  
  4. --Changelog V1.0.1 added maxFuel = 10368 to use as factor in getOutFlux function
  5. --          V1.0.2 added tempDrainFactor to info. and fuelUseRate to getOutFlux, but made no difference to low fuel calcs :(
  6. --          V1.1.0 added OpenComputers layer
  7.  
  8. -- *** REQUIRED *** --
  9. local modemTx = 21              -- Must be a unique channel not used by any other computer on the server and MUST be the same as the reactor modem channel(s)
  10. local modemRx = 21              -- Must be a unique channel not used by any other computer on the server and MUST be the same as the reactor modem channel(s)
  11.  
  12. -- *** REQUIRED FOR COMPUTERCRAFT *** --
  13. local modemSide = 'back'
  14. local monitorSide = 'top'
  15. local inFluxSide = 'right'                                   -- required for CC (ComputerCraft) only, Side of the Flux Gate used as Input into the reactor to power the Containment Field and warm up the reactor
  16. local outFluxSide = 'left'                                   -- required for CC (ComputerCraft) only, Side of the Flux Gate used as Output of RF from reactor into your RF Storeage solution
  17.  
  18. -- *** REQUIRED FOR OPENCOMPUTERS *** --
  19. local inFluxAddr = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'    -- required for OC (OpenComputers) only, Address of the Flux Gate used as Input into the reactor to power the Containment Field and warm up the reactor
  20. local outFluxAddr = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'   -- required for OC (OpenComputers) only, Address of the Flux Gate used as Output of RF from reactor into your RF Storage solution
  21.  
  22. -- *** STARTUP VALUES *** --
  23. local reqTemp = 6000                                         -- Default starting required temperature (this is used if error caused reboot or server restarts)
  24. local reqField = 10                                          -- Default starting required field (this is used if error caused reboot or server restarts)    
  25.  
  26. -- Recommended no other values below are changed
  27. -- Debug only
  28. local skip = 0
  29.  
  30. -- Startup Values
  31. local waitTime = 0.05       -- time/ticks between event operations (0.05 = 1 tick @ 20TPS) - time to wait for modem to respond, recommended value 0.10
  32. local minTemp = 2500        -- minimum temperature allowed (also used for fail-safe, do not change)
  33. local maxTemp = 20000000        -- maximum temperature allowed
  34. local cutoffTemp = 20000000     -- shutdown reactor if temp exceeds this value
  35. local warmUpRF = 20000000   -- how much RF/t to inject to warm reactor up to get to over 2000C
  36. local minField = 1          -- minimum field percentage allowed
  37. local maxField = 100            -- maximum field percentaged allowed
  38. local inFluxRF = 0          -- default value for offline, cooling or cold
  39. local outFluxRF = 25000         -- default value for offline, cooling or cold
  40. local graphLen = 44         -- width graphs will take on screen
  41. local screenWidth = 50      -- width of screen
  42.    
  43. -- Tweaks
  44. local outTrimPct = 1        -- 2 = 200%, 1 = 100%, 0.5 = 50%    the percentage of the genRate difference to trim the outFluxRF
  45.    
  46. -- other variable initialisation (do not change these values as declare and/or constants only)
  47. local info = {}                 -- table for holding reactor.info
  48. local debugMode = false         -- flag used to indicate if the monitor is on the debug screen or normal screen array
  49. local msg = ''                  -- for sending adhoc messages to screen
  50. local timerId = 0               -- event timer
  51. local oc_id = 0                 -- because OpenComputers timer Id's do not work
  52. local maxId = 65535             -- maximun number that can be sent as a modem id
  53. local maxFuel = 10368           -- maximum possible fuel
  54. local maxInteger = 2147483647   -- maximun integer allowed (math.maxInteger doesn't seem to work here)
  55.  
  56. --Peripherals and components (do not initialise to values)
  57. local monitor
  58. local modem
  59. local outFlux = {}
  60. local inFlux = {}
  61. local serialization
  62. local fileIO
  63.  
  64. --check os version
  65. local OSVer = ""
  66. local OC = false
  67. local CC = false
  68.  
  69. if _OSVERSION then
  70.     OSVer = _OSVERSION
  71.     OC = true
  72. elseif os.version() then
  73.     OSVer = os.version()
  74.     CC = true
  75. else
  76.     print("OS not supported!")
  77.     return
  78. end
  79.  
  80. print("OSVer: "..OSVer)
  81. print("OpenComputers: "..tostring(OC))
  82. print("ComputerCraft: "..tostring(CC))
  83.  
  84. --ComputerCraft - wrap the peripherals
  85. if CC then
  86.     fileIO = fs
  87.     monitor = peripheral.wrap(monitorSide)
  88.     modem = peripheral.wrap(modemSide)
  89.     if not modem then print("No modem found, please attach an 'Ender Modem'"); return; end
  90.     outFlux = peripheral.wrap(outFluxSide)
  91.     if not outFlux then print("No Flux Gate found for side '"..outFluxSide.."', please attach a Flux Gate"); return; end
  92.     inFlux = peripheral.wrap(inFluxSide)
  93.     if not inFlux then print("No Flux Gate found for side '"..inFluxSide.."', please attach a Flux Gate"); return; end
  94.     print('inFlux='..inFluxSide)
  95.     print('outFlux='..outFluxSide)
  96. end
  97.  
  98. --OpenComputers - Override ComputerCraft API's to make compatable with OpenComputers
  99. if OC then
  100.     local component = require("component")
  101.     local computer = require("computer")
  102.     local event = require("event")
  103.     local term = require("term")
  104.     fileIO = require("filesystem")
  105.     serialization = require("serialization")
  106.    
  107.     --GPU check
  108.     if not component.gpu or component.gpu.getDepth() ~= 8 then
  109.         print("No GPU or incorrect Tier GPU present, please install Graphics Card (Tier 3)")
  110.         os.exit()
  111.     end
  112.     local gpu = component.gpu
  113.    
  114.     --Modem Check
  115.     if not component.modem or not component.modem.isWireless() then
  116.         print("No wireless modem present, please install Wireless Network Card (Tier 2)")
  117.         os.exit()
  118.     end
  119.     modem = component.modem
  120.  
  121.     --Flux gate checks
  122.     for i, v in pairs(component.list("flux_gate")) do
  123.         print("Found: "..v.." : "..i)
  124.         if i == inFluxAddr then inFlux = component.proxy(inFluxAddr) end
  125.         if i == outFluxAddr then outFlux = component.proxy(outFluxAddr) end
  126.     end
  127.     if not inFlux.address or not outFlux.address then
  128.         print("Flux Gate(s) are not present or incorrect address specified, please check flux gates and address(s) specified")
  129.         print("inFluxAddr: "..inFluxAddr.." Found: "..tostring(inFlux.address))
  130.         print("outFluxAddr: "..outFluxAddr.." Found: "..tostring(outFlux.address))
  131.         os.exit()
  132.     end
  133.  
  134.     -- Overrides
  135.     modem.transmit = modem.broadcast
  136.     colors = {
  137.         white       =0xF0F0F0,
  138.         orange      =0xF2B233,
  139.         magenta     =0xE57FD8,
  140.         lightBlue   =0x99B2F2,
  141.         yellow      =0xDEDE6C,
  142.         lime        =0x7FCC19,
  143.         pink        =0xF2B2CC,
  144.         gray        =0x4C4C4C,
  145.         lightGray   =0x999999,
  146.         cyan        =0x4C99B2,
  147.         purple      =0xB266E5,
  148.         blue        =0x3366CC,
  149.         brown       =0x7F664C,
  150.         green       =0x57A64E,
  151.         red         =0xCC4C4C,
  152.         black       =0x191919,
  153.         ['0']       =0xF0F0F0,
  154.         ['1']       =0xF2B233,
  155.         ['2']       =0xE57FD8,
  156.         ['3']       =0x99B2F2,
  157.         ['4']       =0xDEDE6C,
  158.         ['5']       =0x7FCC19,
  159.         ['6']       =0xF2B2CC,
  160.         ['7']       =0x4C4C4C,
  161.         ['8']       =0x999999,
  162.         ['9']       =0x4C99B2,
  163.         a           =0xB266E5,
  164.         b           =0x3366CC,
  165.         c           =0x7F664C,
  166.         d           =0x57A64E,
  167.         e           =0xCC4C4C,
  168.         f           =0x191919
  169.     }
  170.     monitor = {
  171.         setResolution       = function(inX, inY)    gpu.setResolution(inX, inY) end,
  172.         resetResolution     = function()            local rx, ry = gpu.maxResolution(); gpu.setResolution(rx, ry) end,
  173.         setBackgroundColor  = function(inCol)       gpu.setBackground(inCol) end,
  174.         setTextColor        = function(inCol)       gpu.setForeground(inCol) end,
  175.         setCursorPos        = function(inX, inY)    term.setCursor(inX, inY) end,
  176.         write               = function(inStr)       term.write(inStr, false) end,
  177.         blit                = function(inStr, fCol, bCol)  
  178.                                     local _f = string.sub(fCol, 1, 1)
  179.                                     local _b = string.sub(bCol, 1, 1)
  180.                                     local _p = 1
  181.                                     local _l = string.len(inStr)
  182.                                     for i = 1, _l do
  183.                                         if string.sub(fCol, i, i) ~= _f or string.sub(bCol, i, i) ~= _b or i == _l then
  184.                                             gpu.setBackground(colors[_b])
  185.                                             gpu.setForeground(colors[_f])
  186.                                             term.write(string.sub(inStr, _p, i - 1 + ((i == _l) and 1 or 0)))
  187.                                             _f = string.sub(fCol, i, i)
  188.                                             _b = string.sub(bCol, i, i)
  189.                                             _p = i
  190.                                         end
  191.                                     end
  192.                                 end,
  193.         clear               = function()                    term.clear() end
  194.     }
  195.     os.getComputerID = computer.address
  196.     os.pullEvent = event.pull
  197.     os.queueEvent = event.push
  198.  
  199.     local function ocTimer()
  200.         event.push('timer', timerId)
  201.     end
  202.  
  203.     os.startTimer = function(inWaitTime)
  204.                         --OpenComputers does not return a unique Event Id, so have to manage our own
  205.                         local _id = timerId + 1
  206.                         if _id >= maxInteger - 1 then _id = 1 end
  207.                         oc_id = event.timer(inWaitTime, ocTimer)
  208.                         return _id
  209.                     end
  210.     os.cancelTimer = function()
  211.                         event.cancel(oc_id)
  212.                      end
  213.     os.reboot = function()
  214.                         computer.shutdown(true)
  215.                     end
  216.    
  217. end
  218.  
  219. local function modTemp(val)
  220.     reqTemp = reqTemp + val
  221.     if reqTemp > maxTemp then reqTemp = maxTemp end
  222.     if reqTemp < minTemp then reqTemp = minTemp end
  223. end
  224.  
  225. local function modField(val)
  226.     reqField = reqField + val
  227.     if reqField > maxField then reqField = maxField end
  228.     if reqField < minField then reqField = minField end
  229. end
  230.  
  231. local state = {
  232.     active      = "active",
  233.     disabled    = "disabled",
  234.     enabled     = "enabled",
  235.     off         = "off"
  236. }
  237.  
  238. local buttonColors = {
  239.     active      = function()
  240.                         monitor.setBackgroundColor(colors.green)
  241.                         monitor.setTextColor(colors.white)
  242.                     end,
  243.     disabled    = function()
  244.                         monitor.setBackgroundColor(colors.gray)
  245.                         monitor.setTextColor(colors.black)
  246.                     end,
  247.     enabled     = function()
  248.                         monitor.setBackgroundColor(colors.lightGray)
  249.                         monitor.setTextColor(colors.white)
  250.                     end,
  251.     off         = function()
  252.                         monitor.setBackgroundColor(colors.black)
  253.                         monitor.setTextColor(colors.lightGray)
  254.                     end
  255. }
  256.  
  257. local buttonType = {
  258.     label = {
  259.         id = 'label',
  260.         setColors = function()
  261.                 monitor.setBackgroundColor(colors.black)
  262.                 monitor.setTextColor(colors.white)
  263.             end
  264.         },
  265.     button = {
  266.         id = 'button',
  267.         setColors = function(inState)
  268.                 buttonColors[inState]()
  269.             end
  270.         },
  271.     val1 = {
  272.         id = 'val1',
  273.         setColors = function()
  274.                 monitor.setBackgroundColor(colors.gray)
  275.                 monitor.setTextColor(colors.white)
  276.             end
  277.         },
  278.     val10 = {
  279.         id = 'val10',
  280.         setColors = function()
  281.                 monitor.setBackgroundColor(colors.lightGray)
  282.                 monitor.setTextColor(colors.white)
  283.             end
  284.         }
  285. }
  286.  
  287. local function printStatic(inTable)
  288.     monitor.clear()
  289.     monitor.setBackgroundColor(inTable.bCol)
  290.     monitor.setTextColor(inTable.fCol)
  291.     for i, v in pairs(inTable.array) do
  292.         monitor.setCursorPos(1, i)
  293.         monitor.write(v)
  294.     end
  295.     monitor.setCursorPos(1,2)       --display any std out on line 2
  296. end
  297.  
  298. local function printUpdates(inTable)
  299.     for i, v in pairs(inTable) do
  300.         monitor.setCursorPos(v.x, v.y)
  301.         if v.blit then
  302.             monitor.blit(v.txt(), v.fCol(), v.bCol())
  303.         else
  304.             if v.bCol then monitor.setBackgroundColor(v.bCol()) end
  305.             if v.fCol then monitor.setTextColor(v.fCol()) end
  306.             monitor.write(v.txt())
  307.         end
  308.     end
  309.     monitor.setCursorPos(1,2)       --display any std out on line 2
  310. end
  311.  
  312. local function printButtons(inTable)
  313.     for n, b in pairs(inTable) do
  314.         if b.depressed then
  315.             monitor.setBackgroundColor(colors.green)
  316.             monitor.setTextColor(colors.red)
  317.             b.depressed = b.depressed - 1
  318.             if b.depressed == 0 then b.depressed = nil; end
  319.         else
  320.             buttonType[b.type].setColors(b.state)
  321.         end
  322.         monitor.setCursorPos(b.x,b.y)
  323.         monitor.write(b.txt)
  324.     end
  325.     monitor.setCursorPos(1,2)       --display any std out on line 2
  326. end
  327.  
  328. local infoStatus = {
  329.     ['cold']        = function() return colors.lightBlue; end,
  330.     ['running']     = function() return colors.green; end,
  331.     ['stopping']    = function() return colors.yellow; end,
  332.     ['warming_up']  = function() return colors.yellow; end,
  333.     ['beyond_hope'] = function() return colors.red; end,
  334.     ['cooling']     = function() return colors.orange; end
  335. }
  336.  
  337. local function callReactor(request)
  338.     if request == 'getReactorInfo' then
  339.         timerId = os.startTimer(waitTime)
  340.     end
  341.     local int, _ = math.modf(timerId / maxId)
  342.     local _id = timerId - (maxId * int)
  343.     -- Transmit variables for use by a.n.other computer sniffing the wifi to monitor taffic and/or reactor(s)
  344.     local tRequest = {req = request, inRF = inFluxRF, outRF = outFluxRF, reqTemp = reqTemp, reqField = reqField}
  345.     if OC then tRequest = serialization.serialize(tRequest) end  --stupid OpenComputers can't handle tables, have to serialize
  346.     modem.transmit(modemTx, _id, tRequest)
  347. end
  348.  
  349. local function getInFlux()
  350.     -- Field(shield) Calculation
  351.     local reqFieldStrength = (info.maxFieldStrength / 100) * reqField
  352.  
  353.     if info.temperature > 8000 then
  354.         info.tempDrainFactor = 1 + ((info.temperature - 8000)^2 * 0.0000025)
  355.     elseif info.temperature > 2000 then
  356.         info.tempDrainFactor = 1
  357.     elseif info.temperature > 1000 then
  358.         info.tempDrainFactor = (info.temperature - 1000) / 1000
  359.     else
  360.         info.tempDrainFactor = 0
  361.     end
  362.  
  363.     --fieldDrainRate calculation needs to be calculated on startup, else we can use info.fieldDrainRate
  364.     if not info.fieldDrainRate or info.fieldDrainRate == 0 then
  365.         local baseMaxRFt = (3 * info.maxEnergySaturation) / 2000 -- converted for integer maths
  366.         local drainMax = math.max(0.01, (1 - info.satPct))
  367.         info.fieldDrainRate = math.ceil(math.min(info.tempDrainFactor * drainMax * (baseMaxRFt / 10.923556), maxInteger))  -- 2147483647 = math.maxinteger which doesnt work here :(
  368.     else
  369.         --info.fieldDrainRate = info.fieldDrainRate
  370.     end
  371.    
  372.     --fieldCharge calculation based on the required field strength
  373.     local fieldNegPercent = 1 - (reqFieldStrength / info.maxFieldStrength)
  374.     local fieldInputRate = info.fieldDrainRate / fieldNegPercent
  375.     info.fieldCharge = reqFieldStrength - math.min(info.fieldDrainRate , reqFieldStrength)
  376.  
  377.     -- injectEnergy  (happens after update)
  378.     local tempFactor = 1
  379.     if info.temperature > 15000 then
  380.       tempFactor = 1 - math.min(1, (info.temperature - 15000) / 10000)
  381.     end
  382.  
  383.     local rf = inFluxRF
  384.     if inFluxRF == 0 then rf = 1 end
  385.    
  386.     info.fieldCharge = info.fieldCharge + math.min(rf * (1 - (info.fieldCharge / info.maxFieldStrength)) , info.maxFieldStrength - info.fieldCharge) * tempFactor
  387.     info.reqInDiff = reqFieldStrength - info.fieldCharge
  388.     local inFlux = rf + ((info.maxFieldStrength * info.reqInDiff) / (info.maxFieldStrength - info.fieldCharge))
  389.  
  390.     --to speed things up, subtract the current field from the reqField and add the difference to the rf (divided by 10)
  391.     inFlux = inFlux + ((reqFieldStrength - info.fieldStrength) / 10)
  392.  
  393.     if inFlux < 0 then inFlux = 0 end
  394.    
  395.     return inFlux
  396.  
  397. end
  398.  
  399. local function getRiseAmt()
  400.     -- Calculate the current resist and expo(nential)
  401.     local t50 = info.temperature / 10000 * 50
  402.     local tResist = (t50^4) / (100 - t50)
  403.     local tfResist = ((1.3 * info.fuelPct * tResist) + (1300 * info.fuelPct) - (1.3 * tResist) - 300) / 10000
  404.     local negCSat = (1 - info.satPct) * 99
  405.     local tExpo = (((negCSat^3) / (100 - negCSat)) + 444.7) / 10000
  406.     local tRiseAmt = (tfResist + tExpo) * 100 --correct format for display on debug screen
  407.    
  408.     return tRiseAmt
  409.    
  410. end
  411.  
  412. local function getOutFlux()
  413.     --fuel use rate
  414.     if info.fuelPct > 0 then
  415.         local fuelUseRate = info.tempDrainFactor * (1 - info.satPct) * 0.001
  416.         info.fuelPct = (info.fuelConversion + fuelUseRate) / info.maxFuelConversion
  417.     end
  418.  
  419.     -- RF out calculation
  420.     info.outDiff = reqTemp - info.temperature
  421.     --calculate the required resist
  422.     local reqTResist = -(reqTemp^4 / (8000000 * (reqTemp - 20000)))
  423.     local reqResist = ((1.3 * info.fuelPct * reqTResist) + (1300 * info.fuelPct) - (1.3 * reqTResist) - 300) / 10000
  424.     -- The required Expo will be a reversed sign of reqResist
  425.     local reqExpo = ((reqResist * -1) * 10000) - 444.7  
  426.     local _E = reqExpo
  427.     --reverse the expo to get the expected required saturation%
  428.     local revNegCSat =(((math.sqrt(3)*(math.sqrt((_E^3)+(67500*(_E^2)))))+(450*_E))^(1/3))/(3^(2/3))-(_E/((3^(1/3))*(((math.sqrt(3)*(math.sqrt((_E^3)+(67500*(_E^2)))))+(450*_E))^(1/3))))
  429.     --calculate the required genRate
  430.     info.reqGenRate = (revNegCSat / 99) * (((info.maxEnergySaturation / 1000) * 1.5) * (1 + (((info.fuelPct * 1.3) - 0.3) * 2)))
  431.     --subtract the current genRate from the required genRate to get the new outGenRate
  432.     info.outGenRate = info.reqGenRate - info.generationRate
  433.    
  434.     if math.abs(info.outDiff) > 1 then
  435.         info.outSpeed = info.outDiff * (info.temperature / reqTemp ) * 10 * (info.maxFuelConversion / maxFuel)
  436.         info.outIncrease = info.outGenRate + info.outSpeed
  437.         info.outGenRateTrim = 0
  438.     elseif math.abs(info.outDiff) == 1 then
  439.         info.outSpeed = 0
  440.         info.outIncrease = info.outGenRate
  441.         info.outGenRateTrim = info.outGenRate * outTrimPct
  442.     else
  443.         info.outSpeed = 0
  444.         info.outIncrease = 0
  445.         info.outGenRateTrim = info.outGenRate * outTrimPct
  446.     end
  447.    
  448.     local outFlux = info.reqGenRate + info.outIncrease + info.outSpeed + info.outGenRateTrim
  449.    
  450.     if outFlux < 0 or info.generationRate == 0 or outFlux == (1 / 0) or outFlux ~= outFlux then outFlux = 1000 end
  451.    
  452.     return outFlux
  453.  
  454. end
  455.  
  456. local function getGraph(inVal, inMax, inSize)
  457.     local strGraph = ''
  458.     local val = (((inVal / inMax) * inSize) - 1)    --divide the width of screen by the colored chunks required
  459.     for i = 1, val do                               -- build the '='
  460.         strGraph = strGraph.."="
  461.     end
  462.     strGraph = strGraph..">"                        -- concat the '>'
  463.     for i = val, inSize do                          -- set the remaining width to spaces
  464.         strGraph = strGraph.." "
  465.     end
  466.    
  467.     --In case we have a divide by zero, thus no data, pad the string out with spaces
  468.     strGraph = strGraph..string.rep(" ",inSize)
  469.     strGraph = string.sub(strGraph,1,inSize)
  470.     return strGraph
  471.    
  472. end
  473.  
  474. local function fmtNum( inVal, inWidth, inDec, boolC )
  475.     --because string.format is broken in ComputerCraft, need own format function
  476.     local rtn = inVal
  477.     if type(rtn) == 'number' then
  478.         --Round and get the modulus padded with zeros
  479.         local mod
  480.         if inDec > 0 then                                           -- number of decimal places
  481.             inVal = (math.floor(inVal * (10^(inDec)) + 0.5) / 10^(inDec))   -- round to the specified decimal places
  482.             -- multiply by 10 to the power of decimals places
  483.             mod = math.floor((inVal % 1) * ( 10 ^ (inDec) ))
  484.             -- pad the decimal with zeros
  485.             mod = string.rep(
  486.                 "0",(
  487.                         ( (inDec-string.len(mod)>=0)  and 1 or 0)
  488.                         * (inDec-string.len(mod))
  489.                     )
  490.                 )..mod
  491.         end
  492.         -- get the integral part
  493.         local int = math.floor(inVal)
  494.  
  495.         -- add the commas
  496.         while boolC do
  497.             local k
  498.             int, k = string.gsub(int, "^(-?%d+)(%d%d%d)", '%1,%2')
  499.             if (k==0) then
  500.                 break
  501.             end
  502.         end
  503.  
  504.         if mod then
  505.             -- concatonate integral with modulus and decimal point
  506.             rtn = int.."."..mod
  507.         else
  508.             rtn = int  
  509.         end
  510.        
  511.         -- pad with leading spaces
  512.         local pad = inWidth - string.len(rtn)
  513.  
  514.         if pad > 0 then rtn = string.rep(" ",(pad) )..rtn end
  515.     else
  516.         rtn = tostring(rtn)
  517.         rtn = string.rep(" ",inWidth - string.len(rtn))..rtn
  518.     end
  519.     rtn = string.sub(rtn,1,inWidth)
  520.     return rtn
  521. end
  522.  
  523. local scrnStatic = {
  524.     main = {
  525.         bCol = colors.black,
  526.         fCol = colors.lightBlue,
  527.         x = 1,
  528.         y = 1,
  529.         array = {
  530.             '                                                  ',
  531.             '                                                  ',
  532.             'Temp:                                             ',
  533.             '                                                  ',
  534.             'Fld:                                              ',
  535.             '                                                  ',
  536.             'Satn:                                             ',
  537.             '                                                  ',
  538.             'Fuel:                                             ',
  539.             '                                                  ',
  540.             'FldDrainRate:            GenRate:                 ',
  541.             'In(Field):           Gain:           Out:         '
  542.             }
  543.         },
  544.     debug = {
  545.         bCol = colors.black,
  546.         fCol = colors.lightBlue,
  547.         x = 1,
  548.         y = 1,
  549.         array = {
  550.             'Status             Debug Screen                   ',
  551.             '                                                  ',
  552.             'Fld:                                 Temp:        ',
  553.             'FldChrg:                  outDiff:                ',
  554.             '                          riseAmt:                ',
  555.             ' reqInDiff:              tRiseAmt:                ',
  556.             '                       reqGenRate:                ',
  557.             '                       outGenRate:                ',
  558.             '                      outIncrease:                ',
  559.             'fieldDrain:            speed/trim:                ',
  560.             'fldDrnRate:               GenRate:                ',
  561.             'In(Field):           Gain:           Out:         '
  562.         }
  563.     }
  564. }
  565.  
  566. local scrnDynamic = {
  567.     main = {
  568.         [1] = { --status = {
  569.             x = 8,
  570.             y = 1,
  571.             fCol = function() return infoStatus[info.status](); end,
  572.             bCol = function() return colors.black; end,
  573.             txt = function() return string.sub(info.status..'      ',1,10) end
  574.             },
  575.         [2] = { --tempVal = {
  576.             x = 7,
  577.             y = 3,
  578.             fCol = function()
  579.                         if math.floor(info.temperature) > reqTemp and info.temperature > 2500 then
  580.                             return colors.red
  581.                         else
  582.                             return colors.white
  583.                         end
  584.                     end,
  585.             bCol = function() return colors.black; end,
  586.             txt = function() return fmtNum(math.floor(info.temperature),4,0,false).."C" end
  587.             },
  588.         [3] = { --reqTemp = {
  589.             x = 37,
  590.             y = 3,
  591.             fCol = function() return colors.blue; end,
  592.             bCol = function() return colors.black; end,
  593.             txt = function() return fmtNum(reqTemp,4,0,false).."C" end
  594.             },
  595.         [4] = { --tempVal2 = {
  596.             x = 1,
  597.             y = 4,
  598.             fCol = function()
  599.                         if math.floor(info.temperature) > reqTemp and info.temperature > 2500 then
  600.                             return colors.red
  601.                         else
  602.                             return colors.white
  603.                         end
  604.                     end,
  605.             bCol = function() return colors.black; end,
  606.             txt = function() return fmtNum(math.floor(info.temperature),5,0,false).."C" end
  607.             },
  608.         [5] = { --tempBar = {
  609.             x = 7,
  610.             y = 4,
  611.             blit = true,
  612.             fCol = function() return string.rep("f",graphLen); end,
  613.             bCol = function() return "eeeeeeeeeee111111111114444444444400000000000"; end,
  614.             txt = function() return getGraph(info.temperature, cutoffTemp, graphLen) end
  615.             },
  616.         [6] = { --field = {
  617.             x = 7,
  618.             y = 5,
  619.             fCol = function() return colors.white; end,
  620.             bCol = function() return colors.black; end,
  621.             txt = function() return fmtNum(math.floor(info.fieldStrength),9,0,false).."/"..fmtNum(info.maxFieldStrength,9,0,false) end
  622.             },
  623.         [7] = { --reqField = {
  624.             x = 37,
  625.             y = 5,
  626.             fCol = function() return colors.blue; end,
  627.             bCol = function() return colors.black; end,
  628.             txt = function() return fmtNum(reqField,3,0,false).."%" end
  629.             },
  630.         [8] = { --fieldPct = {
  631.             x = 1,
  632.             y = 6,
  633.             fCol = function()
  634.                         if info.status == 'cold' or info.status == 'offline' or info.status == 'cooling' then
  635.                             return colors.white;
  636.                         else
  637.                             local fieldPct = info.fieldPct * 100
  638.                             if     fieldPct < reqField - 0.5 then return colors.red;
  639.                             elseif fieldPct < reqField - 0.2 then return colors.orange;
  640.                             elseif fieldPct < reqField - 0.1 then return colors.yellow;
  641.                             else return colors.white;  
  642.                             end
  643.                         end
  644.                     end,
  645.             bCol = function() return colors.black; end,
  646.             txt = function() return fmtNum(info.fieldPct*100,5,2,false).."%" end
  647.             },
  648.         [9] = { --fieldBar = {
  649.             x = 7,
  650.             y = 6,
  651.             blit = true,
  652.             fCol = function() return string.rep("f",graphLen); end,
  653.             bCol = function() return "eeeeeeeeeaaaaaaaaa222222222bbbbbbbbb99999999"; end,
  654.             txt = function() return getGraph(info.fieldStrength, info.maxFieldStrength, graphLen) end
  655.             },
  656.         [10] = { --sat = {
  657.             x = 7,
  658.             y = 7,
  659.             fCol = function() return colors.white; end,
  660.             bCol = function() return colors.black; end,
  661.             txt = function() return fmtNum(math.floor(info.energySaturation),9,0,false).."/"..fmtNum(info.maxEnergySaturation,10,0,false) end
  662.             },
  663.         [11] = { --satPct = {
  664.             x = 1,
  665.             y = 8,
  666.             fCol = function()
  667.                         local satPct = info.satPct*100
  668.                         if     satPct > 99 then return colors.red;
  669.                         elseif satPct > 95 then return colors.orange;
  670.                         elseif satPct > 90 then return colors.yellow;
  671.                         else return colors.white;
  672.                         end
  673.                     end,
  674.             bCol = function() return colors.black; end,
  675.             txt = function() return fmtNum(info.satPct*100,5,2,false).."%" end
  676.             },
  677.         [12] = { --satBar = {
  678.             x = 7,
  679.             y = 8,
  680.             blit = true,
  681.             fCol = function() return string.rep("f",graphLen); end,
  682.             bCol = function() return "eeeeeeeeeeeeeeeddddddddddddddd99999999999999"; end,
  683.             txt = function() return getGraph(info.energySaturation, info.maxEnergySaturation, graphLen) end
  684.             },
  685.         [13] = { --fuel = {
  686.             x = 7,
  687.             y = 9,
  688.             fCol = function() return colors.white; end,
  689.             bCol = function() return colors.black; end,
  690.             txt = function() return fmtNum(info.fuelConversion,9,3,false).."/"..fmtNum(info.maxFuelConversion,5,0,false) end
  691.             },
  692.         [14] = { --fuelPct = {
  693.             x = 1,
  694.             y = 10,
  695.             fCol = function()
  696.                         local fuelPct = info.fuelPct*100
  697.                         if     fuelPct > 95 then return colors.red;
  698.                         elseif fuelPct > 90 then return colors.orange;
  699.                         elseif fuelPct > 85 then return colors.yellow;
  700.                         else return colors.white;
  701.                         end
  702.                     end,
  703.             bCol = function() return colors.black; end,
  704.             txt = function() return fmtNum(info.fuelPct*100,5,2,false).."%" end
  705.             },
  706.         [15] = { --fuelBar = {
  707.             x = 7,
  708.             y = 10,
  709.             blit = true,
  710.             fCol = function() return string.rep("f",graphLen); end,
  711.             bCol = function() return "44444444444ddddddddddd22222222222eeeeeeeeeee"; end,
  712.             txt = function() return getGraph(info.fuelConversion, info.maxFuelConversion, graphLen) end
  713.             },
  714.         [16] = { --fieldDrainRate = {
  715.             x = 14,
  716.             y = 11,
  717.             fCol = function() return colors.white; end,
  718.             bCol = function() return colors.black; end,
  719.             txt = function() return fmtNum(info.fieldDrainRate,9,0,false) end
  720.             },
  721.         [17] = { --generationRate = {
  722.             x = 34,
  723.             y = 11,
  724.             fCol = function() return colors.white; end,
  725.             bCol = function() return colors.black; end,
  726.             txt = function() return fmtNum(info.generationRate,9,0,false) end
  727.             },
  728.         [18] = { --inFluxRF = {
  729.             x = 11,
  730.             y = 12,
  731.             fCol = function() return colors.white; end,
  732.             bCol = function() return colors.black; end,
  733.             txt = function() return fmtNum(inFluxRF,8,0,false) end
  734.             },
  735.         [19] = { --gain = {
  736.             x = 28,
  737.             y = 12,
  738.             fCol = function() return colors.white; end,
  739.             bCol = function() return colors.black; end,
  740.             txt = function() return fmtNum(outFluxRF-inFluxRF,8,0,false) end
  741.             },
  742.         [20] = { --outFluxRF = {
  743.             x = 43,
  744.             y = 12,
  745.             fCol = function() return colors.white; end,
  746.             bCol = function() return colors.black; end,
  747.             txt = function() return fmtNum(outFluxRF,8,0,false) end
  748.             }
  749.         },
  750.     debug = {
  751.         [1] = { --skip = {
  752.             x = 1,
  753.             y = 2,
  754.             txt = function() return 'Ticks lost: '..skip.."  TimerId: "..timerId; end
  755.             },
  756.         [2] = { --status = {
  757.             x = 8,
  758.             y = 1,
  759.             txt = function() return string.sub(info.status..'      ',1,10); end
  760.             },
  761.         [3] = { --fldPct = {
  762.             x = 5,
  763.             y = 3,
  764.             txt = function() return fmtNum(info.fieldPct*100,5,2,false); end
  765.             },
  766.         [4] = { --field = {
  767.             x = 12,
  768.             y = 3,
  769.             txt = function() return fmtNum(info.fieldStrength,9,0,false).."/"; end
  770.             },
  771.         [5] = { --maxField = {
  772.             x = 22,
  773.             y = 3,
  774.             txt = function() return fmtNum(info.maxFieldStrength,9,0,false); end
  775.             },
  776.         [6] = { --temperature = {
  777.             x = 43,
  778.             y = 3,
  779.             txt = function() return fmtNum(info.temperature,4,0,false).."C"; end
  780.             },
  781.         [7] = { --fieldCharge = {
  782.             x = 10,
  783.             y = 4,
  784.             txt = function() return fmtNum(info.fieldCharge,11,0,false); end
  785.             },
  786.         [8] = { --outDiff = {
  787.             x = 35,
  788.             y = 4,
  789.             txt = function() return fmtNum(info.outDiff,8,0,false); end
  790.             },
  791.         [9] = { --riseAmount = {
  792.             x = 35,
  793.             y = 5,
  794.             txt = function() if info.riseAmount then return info.riseAmount * 10; else return 'N/A'; end; end
  795.             },
  796.         [10] = { --tRiseAmount = {
  797.             x = 35,
  798.             y = 6,
  799.             txt = function() return getRiseAmt(); end
  800.             },
  801.         [11] = { --reqInDiff = {
  802.             x = 12,
  803.             y = 6,
  804.             txt = function() return fmtNum(info.reqInDiff,10,0,false); end
  805.             },
  806.         [12] = { --reqGenRate = {
  807.             x = 35,
  808.             y = 7,
  809.             txt = function() return fmtNum(info.reqGenRate,11,2,false); end
  810.             },
  811.         [13] = { --outGenRate = {
  812.             x = 35,
  813.             y = 8,
  814.             txt = function() return fmtNum(info.outGenRate,11,2,false); end
  815.             },
  816.         [14] = { --outIncrease = {
  817.             x = 35,
  818.             y = 9,
  819.             txt = function() return fmtNum(info.outIncrease,11,2,false) end
  820.             },
  821.         [15] = { --fieldDrain = {
  822.             x = 12,
  823.             y = 10,
  824.             txt = function() return fmtNum(info.fieldDrain,10,2,false); end
  825.             },
  826.         [16] = { --speedTrim = {
  827.             x = 35,
  828.             y = 10,
  829.             txt = function() return fmtNum(info.outSpeed,11,2,false).."/"..tostring(info.outGenRateTrim).."       "; end
  830.             },
  831.         [17] = { --fieldDrainRate = {
  832.             x = 12,
  833.             y = 11,
  834.             txt = function() return fmtNum(info.fieldDrainRate,10,2,false); end
  835.             },
  836.         [18] = { --generationRate = {
  837.             x = 35,
  838.             y = 11,
  839.             txt = function() return fmtNum(info.generationRate,8,0,false); end
  840.             },
  841.         [19] = { --inFluxRF = {
  842.             x = 11,
  843.             y = 12,
  844.             txt = function() return fmtNum(inFluxRF,8,0,false); end
  845.             },
  846.         [20] = { --gain = {
  847.             x = 28,
  848.             y = 12,
  849.             txt = function() return fmtNum(outFluxRF-inFluxRF,8,0,false); end
  850.             },
  851.         [21] = { --outFluxRF = {
  852.             x = 43,
  853.             y = 12,
  854.             txt = function() return fmtNum(outFluxRF,8,0,false); end
  855.             }
  856.         }
  857. }
  858.  
  859. local buttons = {
  860.     [1] = { --status = {
  861.         x = 1,
  862.         y = 1,
  863.         txt = 'Status',
  864.         type = buttonType.label.id,
  865.         state = state.enabled,
  866.         action = function() debugMode = not debugMode; if debugMode then printStatic(scrnStatic.debug); else printStatic(scrnStatic.main); end; end
  867.     },
  868.     [2] = { --charge = {
  869.         x = 18,
  870.         y = 1,
  871.         txt = '[Charge]',
  872.         type = buttonType.button.id,
  873.         state = state.off,
  874.         action = function(self) if self.state == state.enabled then callReactor('chargeReactor'); end; end
  875.     },
  876.     [3] = { --activate = {
  877.         x = 28,
  878.         y = 1,
  879.         txt = '[Activate]',
  880.         type = buttonType.button.id,
  881.         state = state.off,
  882.         action = function(self) if self.state == state.enabled then callReactor('activateReactor'); end; end
  883.     },
  884.     [4] = { --stop = {
  885.         x = 40,
  886.         y = 1,
  887.         txt = '[Shutdown]',
  888.         type = buttonType.button.id,
  889.         state = state.off, 
  890.         action = function(self) if self.state == state.enabled then callReactor('stopReactor'); end; end
  891.     },
  892.     [5] = { --tempUp1 = {
  893.         x = 42,
  894.         y = 3,
  895.         txt = '[]',
  896.         type = buttonType.val1.id,
  897.         action=function() modTemp(1); end,
  898.         state = state.enabled  
  899.     },
  900.     [6] = { --tempUp10 = {
  901.         x = 44,
  902.         y = 3,
  903.         txt = '[>]',
  904.         type = buttonType.val10.id,
  905.         action=function() modTemp(10); end,
  906.         state = state.enabled
  907.     },
  908.     [7] = { --tempUp100 = {
  909.         x = 47,
  910.         y = 3,
  911.         txt = '[>>]',
  912.         type = buttonType.val1.id,
  913.         action=function() modTemp(100); end,
  914.         state = state.enabled
  915.     },
  916.     [8] = { --tempDown1 = {
  917.         x = 35,
  918.         y = 3,
  919.         txt = '[]',
  920.         type = buttonType.val1.id,
  921.         action=function() modTemp(-1); end,
  922.         state = state.enabled
  923.     },
  924.     [9] = { --tempDown10 = {
  925.         x = 32,
  926.         y = 3,
  927.         txt = '[<]',
  928.         type = buttonType.val10.id,
  929.         action=function() modTemp(-10); end,
  930.         state = state.enabled
  931.     },
  932.     [10] = { --tempDown100 = {
  933.         x = 28,
  934.         y = 3,
  935.         txt = '[<<]',
  936.         type = buttonType.val1.id,
  937.         action=function() modTemp(-100); end,
  938.         state = state.enabled
  939.     },
  940.     [11] = { --fieldUp1 = {
  941.         x = 42,
  942.         y = 5,
  943.         txt = '[]',
  944.         type = buttonType.val1.id,
  945.         action=function() modField(1); end,
  946.         state = state.enabled
  947.     },
  948.     [12] = { --fieldUp10 = {
  949.         x = 44,
  950.         y = 5,
  951.         txt = '[>]',
  952.         type = buttonType.val10.id,
  953.         action=function() modField(10); end,
  954.         state = state.enabled
  955.     },
  956.     [13] = { --fieldDown1 = {
  957.         x = 35,
  958.         y = 5,
  959.         txt = '[]',
  960.         type = buttonType.val1.id,
  961.         action=function() modField(-1); end,
  962.         state = state.enabled
  963.     },
  964.     [14] = { --fieldDown10 = {
  965.         x = 32,
  966.         y = 5,
  967.         txt = '[<]',
  968.         type = buttonType.val10.id,
  969.         action=function() modField(-10); end,
  970.         state = state.enabled
  971.     }
  972. }
  973.  
  974. local setStatus = {
  975.     ["running"] =   function()
  976.                         buttons[2].state = state.disabled
  977.                         buttons[3].state = state.active
  978.                         buttons[4].state = state.enabled
  979.                     end,
  980.     ["cold"] =      function()
  981.                         buttons[2].state = state.enabled
  982.                         buttons[3].state = state.disabled
  983.                         buttons[4].state = state.disabled
  984.                     end,
  985.     ["stopping"] =  function()
  986.                         if info.temperature > 2000 then
  987.                             buttons[2].state = state.disabled
  988.                             buttons[3].state = state.enabled
  989.                         else
  990.                             buttons[2].state = state.enabled
  991.                             buttons[3].state = state.disabled
  992.                         end
  993.                         buttons[4].state = state.active
  994.                     end,
  995.     ["warming_up"] =  function()
  996.                         if info.temperature > 2000 then
  997.                             buttons[2].state = state.active
  998.                             buttons[3].state = state.enabled
  999.                         else
  1000.                             buttons[2].state = state.active
  1001.                             buttons[3].state = state.disabled
  1002.                         end
  1003.                         buttons[4].state = state.enabled
  1004.                     end,
  1005.     ["cooling"] =  function()
  1006.                         if info.satPct >= 0.99 then
  1007.                             buttons[2].state = state.disabled
  1008.                         else
  1009.                             buttons[2].state = state.enabled
  1010.                         end
  1011.                         buttons[3].state = state.disabled
  1012.                         buttons[4].state = state.active
  1013.                     end,
  1014.     ['beyond_hope'] = function()
  1015.                         buttons[2].state = state.off
  1016.                         buttons[3].state = state.off
  1017.                         buttons[4].state = state.off
  1018.                     end
  1019. }
  1020.  
  1021. local function userAction(op1, op2)
  1022.     local x = op1  -- x coord of where click happened
  1023.     local y = op2  -- y coord of where click happened
  1024.     --Loop through all the buttons and compare x/y's to see if button was pressed, if so, execute the buttons action function
  1025.     for n, b in pairs(buttons) do
  1026.         if (x >= b.x and x <= b.x + string.len(b.txt) and y == b.y and b.state == state.enabled) then
  1027.             b:action()
  1028.             b.depressed = 3
  1029.         end
  1030.     end
  1031.  
  1032. end
  1033.  
  1034. local function core()
  1035.     if info.fieldStrength    then info.fieldPct = info.fieldStrength    / info.maxFieldStrength;    else info.fieldPct = 0; end
  1036.     if info.energySaturation then info.satPct   = info.energySaturation / info.maxEnergySaturation; else info.satPct   = 0; end
  1037.     if info.fuelConversion   then info.fuelPct  = info.fuelConversion   / info.maxFuelConversion;   else info.fuelPct  = 0; end
  1038.     -- test for Nan and Inf
  1039.     if info.fieldPct == (1 / 0) or info.fieldPct ~= info.fieldPct then info.fieldPct = 0; end
  1040.     if info.satPct   == (1 / 0) or info.satPct   ~= info.satPct   then info.satPct   = 0; end
  1041.     if info.fuelPct  == (1 / 0) or info.fuelPct  ~= info.fuelPct  then info.fuelPct  = 0; end
  1042.    
  1043.     --Safty Checks
  1044.     if info.temperature > cutoffTemp or (info.temperature < minTemp and info.satPct >= 0.99) then
  1045.         --**** STOP REACTOR ****
  1046.         if info.status ~= state.stopping then callReactor("stopReactor"); end
  1047.     end
  1048.  
  1049.     --print("set status")      
  1050.     setStatus[info.status]()
  1051.    
  1052.     --print("--Calculate and Set Flux Gate Values")
  1053.     if info.status == 'cold' or info.status == 'offline' or info.status == 'cooling' then
  1054.         inFluxRF = 0
  1055.         outFluxRF = info.generationRate
  1056.        
  1057.     elseif info.status == 'warming_up' then
  1058.         if info.temperature < 2000 then
  1059.             inFluxRF = warmUpRF
  1060.         else
  1061.             inFluxRF = 0
  1062.         end
  1063.        
  1064.     elseif info.status == 'stopping' then
  1065.         --print("Stopping, so just calculate inFluxRF to maintain containment field, set outFlux to the info.generationRate")
  1066.         if reqField < 10 then reqField = 10; end  -- set the containment field to 10% for safety reasons
  1067.         inFluxRF = getInFlux()
  1068.         outFluxRF = info.generationRate
  1069.        
  1070.     else
  1071.         --print("--Calculate in and out flux requirements")    
  1072.         inFluxRF = getInFlux()
  1073.         outFluxRF = getOutFlux()
  1074.     end
  1075.    
  1076.     --print("--Update Flux Gates with requirements")
  1077.     inFlux.setFlowOverride(inFluxRF)
  1078.     outFlux.setFlowOverride(outFluxRF)
  1079.  
  1080.     --print("update screen")
  1081.     if debugMode then
  1082.         monitor.setTextColor(colors.white)
  1083.         printUpdates(scrnDynamic.debug)
  1084.     else
  1085.         printUpdates(scrnDynamic.main)
  1086.         printButtons(buttons)
  1087.     end
  1088.     monitor.setCursorPos(1,2)
  1089.  
  1090. end
  1091.  
  1092. local eventHandler = {
  1093.     timer               = function(id, op1, op2, op3, op4, op5)
  1094.                                 if id == timerId then
  1095.                                     callReactor('getReactorInfo')
  1096.                                     skip = skip + 1                           --Debug Only
  1097.                                 end
  1098.                             end,
  1099.     modem_message       = function(id, op1, op2, op3, op4, op5)
  1100.                                 if OC then
  1101.                                     op2 = op4
  1102.                                     op3 = serialization.unserialize(op5)
  1103.                                 end
  1104.                                 local int, _ = math.modf(timerId / maxId)
  1105.                                 if op2 + (maxId * int) == timerId then
  1106.                                     os.cancelTimer(timerId)             -- Cancel the timer as we have the message
  1107.                                     info = op3   -- set reactor info table as returned in op3/message
  1108.                                     core()
  1109.                                     callReactor('getReactorInfo')
  1110.                                 end
  1111.                             end,
  1112.     mouse_click         = function(id, op1, op2, op3, op4) userAction(op1, op2); end,
  1113.     eventmouse_click    = function(id, op1, op2, op3, op4) userAction(op1, op2); end,
  1114.     monitor_touch       = function(id, op1, op2, op3, op4) userAction(op1, op2); end,
  1115.     touch               = function(id, op1, op2, op3, op4) userAction(op1, op2); end,
  1116.     interrupted         = function() end,   -- TO DO
  1117.     ['*']               = function() end
  1118. }
  1119.  
  1120. local function errorHandler(errNo, errMsg)
  1121.     if not errNo then
  1122.         print(tostring(errNo).."    "..tostring(errMsg))
  1123.         if errMsg == "Terminated" or errMsg == "interrupted" then
  1124.             return
  1125.         else
  1126.             local f = fileIO.open("log","a")
  1127.             f:write("#"..os.getComputerID().." :: "..errMsg.."\n")
  1128.             f:close()
  1129.             os.reboot()
  1130. --print("Reboot commented")
  1131.         end
  1132.     end
  1133. end
  1134.  
  1135. local function initialise()
  1136.     --Initialise Flux Gates
  1137.     inFlux.setFlowOverride(0)
  1138.     outFlux.setFlowOverride(0)
  1139.     inFlux.setOverrideEnabled(true)
  1140.     outFlux.setOverrideEnabled(true)
  1141.  
  1142.     --Initialise Monitor
  1143.     if CC then
  1144.         if monitor == nil then
  1145.             print("setting monitor to term")
  1146.             monitor = term
  1147.         else   
  1148.             monitor.setTextScale(1)
  1149.         end
  1150.     elseif OC then
  1151.         monitor.setResolution(50, 12)
  1152.     else
  1153.         --Doh, we have no recognised OS
  1154.     end
  1155.     print("Monitor initialised")
  1156.    
  1157.     monitor.clear()
  1158.     printStatic(scrnStatic.main)
  1159.  
  1160.     --redirect std out to monitor
  1161.     if monitor ~= term and CC then
  1162.         term.redirect(monitor)      --redirct all future std out to monitor instead of local computer screen
  1163.     end
  1164.    
  1165.     -- open modem on listen channel
  1166.     modem.open(modemRx)  
  1167.  
  1168.     print("request initial reactor info")
  1169.     callReactor('getReactorInfo')
  1170.     local try = 0
  1171.  
  1172.     --Loop until we get a response from reactor modem or have info.*
  1173.     while info.status == nil do
  1174.         monitor.setCursorPos(1, 2)
  1175.         try = try + 1
  1176.         monitor.write("waiting for response from reactor modem Try:"..try)
  1177.  
  1178.         local event, id, op1, op2, op3, op4, op5 = os.pullEvent()
  1179.        
  1180.         if type(eventHandler[event]) == 'function' then
  1181.             eventHandler[event](id, op1, op2, op3, op4, op5)
  1182.         else
  1183.             eventHandler['*'](event, id, op1, op2, op3, op4, op5)
  1184.         end
  1185.     end -- end while loop
  1186.    
  1187.     monitor.setBackgroundColor(colors.black)
  1188.     monitor.setCursorPos(1, 2)
  1189.     monitor.write(string.rep(" ", screenWidth))
  1190.  
  1191. end
  1192.  
  1193. local function main()
  1194.     --**************************
  1195.     --****** START MAIN ********
  1196.     --**************************
  1197.     --print("start main while loop")
  1198.     while true do
  1199.         --print("wait for the timer event or some other event")
  1200.         local event, id, op1, op2, op3, op4, op5 = os.pullEvent()
  1201.        
  1202.         if type(eventHandler[event]) == 'function' then
  1203.             eventHandler[event](id, op1, op2, op3, op4, op5)
  1204.         else
  1205.             eventHandler['*'](event, id, op1, op2, op3, op4, op5)
  1206.         end
  1207.     end -- end while true loop
  1208. end
  1209.  
  1210. -- initialise()
  1211. -- main()
  1212.  
  1213. local errNo, errMsg = pcall(initialise)
  1214. errorHandler(errNo, errMsg)
  1215. if errNo then
  1216.     local errNo, errMsg = pcall(main)
  1217.     errorHandler(errNo, errMsg)
  1218. end
  1219.  
  1220. --Reset the stupid OpenComputers screen
  1221. if OC then
  1222.     monitor.resetResolution()
  1223. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement