Savage_Me55iah

CC_REACTOR_LIST

Apr 18th, 2016
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.38 KB | None | 0 0
  1. --Script by Savage_Me55iah of CraftAU.com.au and Nuqube.co--
  2. local scriptName, version, pastebinCode = "REACTOR_LIST", "v1.0", "0Vp4jNhG"
  3.  
  4. --Other Savage_Me55iah scripts @ http://pastebin.com/u/Savage_Me55iah
  5.  
  6. --INSTRUCTIONS ON USE--
  7. --Instruction video on http://youtube.com/savagemessiah5
  8.  
  9. --Contact Savage_Me55iah @ CraftAU.com.au or Nuqube.co or http://www.computercraft.info/forums2/ with suggestions/fixes/bugs
  10.  
  11. -- CHANGELOG --
  12. --V1.0
  13.  
  14.  
  15. -- SETUP --
  16.  
  17. local tArgs = {...}
  18. local update_request = tostring(tArgs[1]) or nil
  19.  
  20. do
  21.     update = function(script_name, pastebincode)
  22.         print("UPDATING") shell.run("pastebin get "..pastebincode.." update")
  23.         if fs.exists("update") then
  24.             fs.delete(script_name) fs.copy("update", script_name) fs.delete("update") print("UPDATE SUCCESS")
  25.         else
  26.             print("UPDATE FAILED")
  27.         end
  28.         sleep(2) os.reboot()
  29.     end
  30. end
  31.  
  32. if tostring(update_request) == "update" then update(shell.getRunningProgram(), pastebinCode) end
  33.  
  34. --  label_check(script) --
  35. label_check = function(param_scriptName)
  36.     local param_labelA, param_text, param_labelB = os.getComputerLabel() or false, "Computer label, blank for random label:", nil
  37.     while tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" do
  38.         term.clear() term.setCursorPos(1,1) print(param_text)
  39.         param_labelB = tostring(read())
  40.         if param_labelB == "nil" then param_labelB = tostring(param_scriptName.."-"..math.random(1000, 9999)) end
  41.         os.setComputerLabel(param_labelB) param_labelA = os.getComputerLabel() or false
  42.         if tostring(param_labelA) == "false" or tostring(param_labelA) == "nil" then param_text = "Computer label too long, try again:" end
  43.     end
  44. end
  45. label_check(scriptName)
  46.  
  47. id = os.getComputerID()
  48. label = os.getComputerLabel() or false
  49. if label == false then
  50.     print("Please Type label here: ")
  51.     os.setComputerLabel(tostring(read()))
  52. end
  53.  
  54. function boolean_check(params)
  55.     local answer = nil
  56.     if tostring(params) == "true" then
  57.         answer = true
  58.     else
  59.         answer = false
  60.     end
  61.     return answer
  62. end
  63.  
  64. local settings_file = "config"
  65.  
  66. local settings_template = [[-- adjust these configuration options as necessary.
  67. -- IMPORTANT!, API won't load if there are spaces in the values
  68. COMPUTER_LABEL = "${label}"
  69. COMPUTER_ID = ${id}
  70.  
  71. TEXTSCALE = ${textscale}
  72. UPDATE_TIME = ${update_time}
  73. ]]
  74.  
  75. local function interpolate(s, params)
  76.     return s:gsub('($%b{})', function(w) return params[w:sub(3, -2)] or w end)
  77. end
  78.  
  79. do  
  80.     save_settings = function()
  81.         local filehandler = fs.open(tostring(settings_file), "w")
  82.        
  83.         if tostring(CONFIG) == "nil" then
  84.             settings = {
  85.                 label = os.getComputerLabel(),
  86.                 id = os.getComputerID(),
  87.                 textscale = 1,
  88.                 update_time = 5
  89.             }
  90.         else
  91.             settings = {
  92.                 label = CONFIG.COMPUTER_LABEL or os.getComputerLabel(),
  93.                 id = os.getComputerID(),
  94.                 textscale = CONFIG.TEXTSCALE or 1,
  95.                 update_time = CONFIG.UPDATE_TIME or 5
  96.             }
  97.         end
  98.        
  99.         filehandler.write(interpolate(settings_template, settings))
  100.         filehandler.close()
  101.     end
  102. end
  103.  
  104. if fs.exists(settings_file) == false then
  105.     save_settings()
  106.     return
  107. else
  108.     os.unloadAPI(settings_file)
  109.     if os.loadAPI(settings_file) == false then print("ERROR: Could not load the settings file!") end
  110.     CONFIG = nil
  111.     for k, v in pairs(_G) do
  112.         if k == settings_file then CONFIG = v break end
  113.     end
  114.     if CONFIG == nil then log_add("CONFIG came up nil", true) end
  115. end
  116.  
  117. do
  118. --  modems = peripheral_find_type("modem", "wireless") .. returns a table of all peripherals of the same type --
  119.     peripheral_find_type = function(peripheral_type, modem_type)
  120.         local peripheralList, returnTable, modem_type = peripheral.getNames() or false, {}, modem_type or false
  121.         if type(peripheralList) == "table" then
  122.             for a,b in pairs(peripheralList) do
  123.                 local testType = peripheral.getType(b)
  124.                 if testType == tostring(peripheral_type) then
  125.                     if tostring(modem_type) == false then
  126.                         table.insert(returnTable, b)
  127.                     else
  128.                         local Peripheral = peripheral.wrap(b)
  129. --                      local wireless = boolean_check(Peripheral.isWireless())
  130.                         if modem_type == "wireless" and wireless then table.insert(returnTable, b)
  131.                         elseif modem_type ~= "wireless" and not wireless then table.insert(returnTable, b) end
  132.             end end end
  133.         if tostring(returnTable[1]) == "nil" then returnTable = false end
  134.         return returnTable
  135.     end end
  136. end
  137.  
  138. do
  139.     percentage = function(params_varibleA, params_varibleB)
  140.         local result = math.floor((tonumber(params_varibleA) or 0) / (tonumber(params_varibleB) or 0) * 10000)/100
  141.         return result
  142.     end
  143.    
  144.     getDetails = function()
  145.         local results = {["EUTotal"] = 0}
  146.         if tostring(reactorList) ~= "nil" then
  147.             for a,b in pairs(reactorList) do
  148.                 local temp_wrap = peripheral.wrap(b)
  149.                 results[string.sub(tostring(b), 17, string.len(tostring(b)))] = {
  150.                     ["isActive"] = temp_wrap.isActive() or false,
  151.                     ["Heat"] = percentage((temp_wrap.getHeat() or 0), (temp_wrap.getMaxHeat() or 0)),
  152.                     ["EUOutput"] = (temp_wrap.getEUOutput() / 2) * 10
  153.                 }
  154.             end
  155.             for a,b in pairs(results) do
  156.                 if a ~= "EUTotal" then
  157.                     results["EUTotal"] = results["EUTotal"] + results[a]["EUOutput"]
  158.                 end
  159.             end
  160.         return results
  161.         end
  162.     end
  163.    
  164.     decimal = function(number)
  165.         if type(number) == "number" then
  166.             local text, num = ".", number
  167.             if num >= 1000 then text = "K" end
  168.             if num >= 1000000 then text = "M" end
  169.             if num >= 1000000000 then text = "B" end
  170.             if num >= 1000000000000 then text = "T" end
  171.             while num >= 1000 do
  172.                 num = num / 1000
  173.             end
  174.             num = math.floor(num * 100) / 100
  175.             if text ~= "." then
  176.                 text = tostring(num..text) or 0
  177.             else
  178.                 text = tostring(num) or 0
  179.             end
  180.             return text
  181.         else
  182.             return number
  183.         end
  184.     end
  185.  
  186. --  event = eventFilter("timer", "monitor_touch", "mouse_click", "key", "etc") .. returns a table only when the event matches the arg --
  187.     eventFilter = function(...)
  188.         local param, whitelist, event, fileHandler = {...}, {}, {}, fs.open("eventLog", 'a')
  189.         for a,b in pairs(param) do whitelist[b] = true end
  190.         while tostring(whitelist[event[1]]) ~= "true" do
  191.             event = {os.pullEvent()}
  192.             fileHandler.write(textutils.serialize(event))
  193.         end
  194.         fileHandler.close()
  195.         return event
  196.     end
  197.    
  198. -- screenColor(colors.green, colors.blue)
  199.     screenColor = function(param_commandb, param_colorBackground, param_colorText)
  200.         local colorBackground, colorText, commandc = param_colorBackground or colors.black, param_colorText or colors.white, param_commandb
  201.         if temp_wrap.isColor() == false then
  202.             if param_colorBackground ~= colors.black then colorBackground, colorText = colors.white, colors.black
  203.             else colorBackground, colorText = colors.black, colors.white end
  204.         end
  205.         temp_wrap.setBackgroundColor(colorBackground) temp_wrap.setTextColor(colorText)
  206.     end
  207.  
  208. --  screenWrite(mon[1], "hello world":upper(), 5, 3, colors.red, colors.black)
  209.     screenWrite = function(param_command, param_text, param_x, param_y, param_colorBackground, param_colorText)
  210.         local commandb = param_command
  211.         param_command.setCursorPos(param_x, param_y)
  212.         screenColor(commandb, param_colorBackground, param_colorText)
  213.         param_command.write(param_text)
  214.         screenColor()
  215.     end
  216.    
  217.     monitor = function(command)
  218.         local x, y, backg = 2, 2, " "
  219.         command.clear() command.setCursorPos(x, y)
  220.         screenWrite(command, " # HEAT   OUTPUT ", x, y, colors.cyan, colors.white)
  221.         y = y + 1
  222.         for num=2,17 do backg = backg.." " end
  223.         for a,b in pairs(reactorDetails) do
  224.             local colorBackground = colors.red
  225.             if a ~= "EUTotal" then
  226.                 if reactorDetails[a]["isActive"] and reactorDetails[a]["EUOutput"] ~= 0 then colorBackground = colors.green end
  227.                 screenWrite(command, backg, 2, y, colorBackground, colors.white)
  228.                 screenWrite(command, a, 3, y, colorBackground, colors.white)
  229.                 screenWrite(command, tostring(reactorDetails[a]["Heat"]).."%", 5, y, colorBackground, colors.white)
  230.                 screenWrite(command, decimal(reactorDetails[a]["EUOutput"]), 12, y, colorBackground, colors.white)
  231.                 y = y + 1
  232.             end
  233.         end
  234.         screenWrite(command, backg, 2, y, colors.cyan, colors.white)
  235.         screenWrite(command, " TOTAL EU: "..decimal(reactorDetails["EUTotal"]), x, y, colors.cyan, colors.white)
  236.     end
  237.    
  238.     display = function()
  239.         temp_wrap = term
  240.         monitor(term)
  241.         if tostring(peripheralList) ~= "nil" then
  242.             for a,b in pairs(peripheralList) do
  243.                 local methodList = peripheral.getMethods(b)
  244.                 for c,d in pairs(methodList) do
  245.                     if tostring(d) == "setCursorPos" then
  246.                         temp_wrap = peripheral.wrap(b)
  247.                         temp_wrap.setTextScale(CONFIG.TEXTSCALE)
  248.                         monitor(temp_wrap)
  249.     end end end end end
  250. end
  251.  
  252. Event, reactorList, temp_wrap = {[1] = "timer"}, nil, nil, nil
  253.  
  254.  
  255. while true do
  256.     if tostring(Event[1]) == "timer" then os.startTimer(tonumber(CONFIG.UPDATE_TIME)) end
  257.     if tostring(Event[1]) == "peripheral" or tostring(Event[1]) == "peripheral_detach" or tostring(reactorList) == "nil" then reactorList = {} reactorList = peripheral_find_type("nuclear_reactor") peripheralList = peripheral.getNames() end
  258.     reactorDetails = getDetails()
  259.     display()
  260.     Event = eventFilter("timer", "monitor_touch", "key", "peripheral_detach", "peripheral")
  261. end
Advertisement
Add Comment
Please, Sign In to add comment