Last active
July 29, 2022 15:40
-
-
Save bananasov/004dfbec6f1f79ab511c1e6b33008391 to your computer and use it in GitHub Desktop.
Energy monitor for CC: Tweaked (and CC: Restiched) and tech reborn
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local energy = { | |
su = { | |
battery_box = {name = "low_voltage_su"}, | |
mfe = {name = "medium_voltage_su"}, | |
mfsu = {name = "high_voltage_su"} | |
}, | |
solar = { | |
basic = {name = "basic_solar_panel"}, | |
advanced = {name = "advanced_solar_panel"}, | |
industrial = {name = "industrial_solar_panel"}, | |
ultimate = {name = "ultimate_solar_panel"}, | |
quantum = {name = "quantum_solar_panel"}, | |
creative = {name = "creative_solar_panel"} | |
}, | |
generators = { | |
diesel = {name = "diesel_generator"}, | |
gas = {name = "gas_turbine"}, | |
water = {name = "water_mill"}, | |
lightning = {name = "lightning_rod"}, | |
dragon_egg = {name = "dragon_egg_syphon"}, | |
plasma = {name = "plasma_generator"} | |
} | |
} | |
--#region Helpers | |
function energy:getPeripheralsFromType(tType) | |
local tPeripherals = {} | |
if type(tType) == "table" then | |
for _,v in pairs(tType) do | |
for i, sPeriph in pairs(peripheral.getNames()) do | |
if sPeriph:find(v) then | |
tPeripherals[i] = sPeriph | |
end | |
end | |
end | |
else | |
for i, v in pairs(peripheral.getNames()) do | |
if v:find(tType) then | |
tPeripherals[i] = v | |
end | |
end | |
end | |
return tPeripherals | |
end | |
function energy:toRadix(energy) | |
if energy > 1e6 then | |
return tostring(energy / 1e6) .. " ME " | |
elseif energy > 1e3 then | |
return tostring(energy / 1e3) .. " kE " | |
else | |
return tostring(energy) .. " E " | |
end | |
end | |
function energy:getTotalStoredFromType(tType) | |
local iStored = 0 | |
if type(tType) == "table" then | |
for _, v in pairs(tType) do | |
local tPeripherals = energy:getPeripheralsFromType(v) | |
for i, sPeriph in pairs(tPeripherals) do | |
local wrap = peripheral.wrap(sPeriph) | |
iStored = iStored + wrap.getEnergy() | |
end | |
end | |
else | |
local tPeripherals = energy:getPeripheralsFromType(tType) | |
for i, sPeriph in pairs(tPeripherals) do | |
local wrap = peripheral.wrap(sPeriph) | |
iStored = iStored + wrap.getEnergy() | |
end | |
end | |
return iStored | |
end | |
function energy:getTotalCapacityFromType(tType) | |
local iCapacity = 0 | |
if type(tType) == "table" then | |
for _, v in pairs(tType) do | |
local tPeripherals = energy:getPeripheralsFromType(v) | |
for i, sPeriph in pairs(tPeripherals) do | |
local wrap = peripheral.wrap(sPeriph) | |
iCapacity = iCapacity + wrap.getEnergyCapacity() | |
end | |
end | |
else | |
local tPeripherals = energy:getPeripheralsFromType(tType) | |
for i, sPeriph in pairs(tPeripherals) do | |
local wrap = peripheral.wrap(sPeriph) | |
iCapacity = iCapacity + wrap.getEnergyCapacity() | |
end | |
end | |
return iCapacity | |
end | |
function energy:getTableLength(tbl) | |
local iLength = 0 | |
for _, _ in pairs(tbl) do | |
iLength = iLength + 1 | |
end | |
return iLength | |
end | |
--#endregion | |
--#region SU (Battery Boxes and such) | |
function energy.su:getTotalStored() | |
local tTypes = {"low_voltage_su", "medium_voltage_su", "high_voltage_su"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.su:getTotalCapacity() | |
local tTypes = {"low_voltage_su", "medium_voltage_su", "high_voltage_su"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.su:getTotalPeripherals() | |
local tTypes = {"low_voltage_su", "medium_voltage_su", "high_voltage_su"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iTotal = energy:getTableLength(tPeripherals) | |
return iTotal | |
end | |
function energy.su.battery_box:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.su.mfe:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.su.mfsu:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.su.battery_box:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.su.mfe:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.su.mfsu:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
--#endregion | |
--#region Solar Panels | |
function energy.solar:getTotalStored() | |
local tTypes = {"basic_solar_panel", "advanced_solar_panel", "industrial_solar_panel", "ultimate_solar_panel", "quantum_solar_panel", "creative_solar_panel"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar:getTotalCapacity() | |
local tTypes = {"basic_solar_panel", "advanced_solar_panel", "industrial_solar_panel", "ultimate_solar_panel", "quantum_solar_panel", "creative_solar_panel"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar:getTotalPeripherals() | |
local tTypes = {"basic_solar_panel", "advanced_solar_panel", "industrial_solar_panel", "ultimate_solar_panel", "quantum_solar_panel", "creative_solar_panel"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iTotal = energy:getTableLength(tPeripherals) | |
return iTotal | |
end | |
function energy.solar.basic:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.advanced:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.industrial:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.ultimate:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.quantum:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.creative:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.solar.basic:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar.advanced:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar.industrial:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar.ultimate:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar.quantum:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.solar.creative:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
--#endregion | |
--#region Generators (Diesel, wind, etc) | |
function energy.generators:getTotalStored() | |
local tTypes = {"diesel_generator", "gas_turbine", "water_mill", "lightning_rod", "dragon_egg_syphon", "plasma_generator"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators:getTotalCapacity() | |
local tTypes = {"diesel_generator", "gas_turbine", "water_mill", "lightning_rod", "dragon_egg_syphon", "plasma_generator"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators:getTotalPeripherals() | |
local tTypes = {"diesel_generator", "gas_turbine", "water_mill", "lightning_rod", "dragon_egg_syphon", "plasma_generator"} | |
local tPeripherals = energy:getPeripheralsFromType(tTypes) | |
local iTotal = energy:getTableLength(tPeripherals) | |
return iTotal | |
end | |
function energy.generators.diesel:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.gas:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.dragon_egg:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.water:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.plasma:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.lightning:getTotalStored() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iStored = energy:getTotalStoredFromType(tPeripherals) | |
return iStored | |
end | |
function energy.generators.diesel:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators.gas:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators.dragon_egg:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators.water:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators.plasma:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
function energy.generators.lightning:getTotalCapacity() | |
local tPeripherals = energy:getPeripheralsFromType(self.name) | |
local iCapacity = energy:getTotalCapacityFromType(tPeripherals) | |
return iCapacity | |
end | |
--#endregion | |
return energy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local basalt = require("Basalt") | |
local energy = require("energy") | |
local w, h = term.getSize() | |
local main = basalt.createFrame("mainFrame"):show() | |
local solarFrame = main:addFrame("solarFrame"):setPosition(1,2):setBackground(colors.black):setSize(w, h-1):show() | |
local storedFrame = main:addFrame("storedFrame"):setPosition(1,2):setBackground(colors.black):setSize(w, h-1):hide() | |
local settingsFrame = main:addFrame("settingsFrame"):setPosition(1,2):setBackground(colors.black):setSize(w, h-1):hide() | |
local stats = { | |
solar = { | |
total = 0, | |
totalCap = energy.solar:getTotalCapacity(), | |
peripheralAmount = energy.solar:getTotalPeripherals() | |
}, | |
su = { | |
total = 0, | |
totalCap = energy.su:getTotalCapacity(), | |
peripheralAmount = energy.su:getTotalPeripherals() | |
} | |
} | |
local useWiredModem = settings.get("energymon.use_wired_modem") | |
local menuBar = main:addMenubar("mainMenuBar"):addItem("Solar"):addItem("Stored"):addItem("Settings"):setBackground(colors.gray):setSize(w, 1):setSpace(5):setScrollable():show() | |
menuBar:onChange(function(self) | |
solarFrame:hide() | |
storedFrame:hide() | |
settingsFrame:hide() | |
if self:getValue().text == "Solar" then | |
solarFrame:show() | |
elseif self:getValue().text == "Stored" then | |
storedFrame:show() | |
elseif self:getValue().text == "Settings" then | |
settingsFrame:show() | |
end | |
end) | |
--#region Solar Frame | |
local currentStoredSolar = solarFrame:addLabel("currentStoredSolar"):setPosition(2,2):setForeground(colors.white):setText("Currently Stored Energy: " .. energy:toRadix(stats.solar.total)):show() | |
local maxStoredSolar = solarFrame:addLabel("maxStoredSolar"):setPosition(2,3):setForeground(colors.white):setText("Maximum Amount of Stored Energy: " .. energy:toRadix(stats.solar.totalCap)):show() | |
local attachedPeripherals_solarFrame = solarFrame:addLabel("attachedPeripherals"):setPosition(2,4):setForeground(colors.white):setText("Attached Solar Panels: " .. tostring(stats.solar.peripheralAmount)):show() | |
local broadcastInformation_solarFrame = solarFrame:addButton("broadcastInfo"):setPosition(2, 6):setSize(24,3):setBackground(colors.green):setForeground(colors.white):setText("Broadcast Information"):onClick(function (self) | |
self:onClick(function(self) self:setBackground(colors.gray) self:setForeground(colors.white) end) | |
self:onClickUp(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
self:onLoseFocus(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
local modems = {peripheral.find("modem", function(name, modem) | |
return (useWiredModem and modem.isWireless == false) or modem.isWireless() | |
end)} | |
local modem = modems[1] | |
if modem == nil then basalt.debug("No modem connected.") return end | |
modem.transmit(65473, 0, stats.solar) | |
end) | |
--#endregion | |
--#region Stored Frame | |
local currentStoredEnergy = storedFrame:addLabel("currentStoredEnergy"):setPosition(2,2):setForeground(colors.white):setText("Currently Stored Energy: " .. energy:toRadix(stats.su.total)):show() | |
local maxStoredEnergy = storedFrame:addLabel("maxStoredEnergy"):setPosition(2,3):setForeground(colors.white):setText("Maximum Amount of Stored Energy: " .. energy:toRadix(stats.su.totalCap)):show() | |
local attachedPeripherals_storedFrame = storedFrame:addLabel("attachedPeripherals"):setPosition(2,4):setForeground(colors.white):setText("Attached Energy Banks: " .. tostring(stats.su.peripheralAmount)):show() | |
local broadcastInformation_storedFrame = storedFrame:addButton("broadcastInfo"):setPosition(2, 6):setSize(24,3):setBackground(colors.green):setForeground(colors.white):setText("Broadcast Information"):onClick(function (self) | |
self:onClick(function(self) self:setBackground(colors.gray) self:setForeground(colors.white) end) | |
self:onClickUp(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
self:onLoseFocus(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
local modems = {peripheral.find("modem", function(name, modem) | |
return (useWiredModem and modem.isWireless == false) or modem.isWireless() | |
end)} | |
local modem = modems[1] | |
if modem == nil then basalt.debug("No modem connected.") return end | |
modem.transmit(65473, 0, stats.su) | |
end) | |
--#endregion | |
--#region Settings frame | |
local useWiredModemCheckbox = settingsFrame:addCheckbox("useWiredModem"):setPosition(2, 2):setForeground(colors.white):onChange(function(self) | |
local checked = self:getValue() | |
useWiredModem = checked | |
settings.set("energymon.use_wired_modem", checked) | |
basalt.debug("Set `use_wired_modem` to: " .. tostring(useWiredModem)) | |
end):show() | |
useWiredModemCheckbox:setValue(useWiredModem) | |
local useWiredModemModemLabel = settingsFrame:addLabel("useWiredModemLabel"):setPosition(4, 2):setForeground(colors.white):setText("Use Wired modem"):show() | |
settingsFrame:addButton("refreshButton"):setPosition(2, 5):setText("Refresh"):setSize(15,3):setBackground(colors.green):setForeground(colors.white):onClick(function (self) | |
self:onClick(function(self) self:setBackground(colors.gray) self:setForeground(colors.white) end) | |
self:onClickUp(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
self:onLoseFocus(function(self) self:setBackground(colors.green) self:setForeground(colors.white) end) | |
stats.solar.peripheralAmount = energy.solar:getTotalPeripherals() | |
stats.su.peripheralAmount = energy.su:getTotalPeripherals() | |
stats.solar.total = energy.solar:getTotalStored() | |
stats.su.total = energy.su:getTotalStored() | |
stats.solar.totalCap = energy.solar:getTotalCapacity() | |
stats.su.totalCap = energy.su:getTotalCapacity() | |
attachedPeripherals_solarFrame:setText("Attached Solar Panels: " .. tostring(stats.solar.peripheralAmount)) | |
attachedPeripherals_storedFrame:setText("Attached Energy Banks: " .. tostring(stats.su.peripheralAmount)) | |
currentStoredSolar:setText("Currently Stored Energy: " .. energy:toRadix(stats.solar.total)) | |
currentStoredEnergy:setText("Currently Stored Energy: " .. energy:toRadix(stats.su.total)) | |
maxStoredSolar:setText("Maximum Amount of Stored Energy: " .. energy:toRadix(stats.solar.totalCap)) | |
maxStoredEnergy:setText("Maximum Amount of Stored Energy: " .. energy:toRadix(stats.su.totalCap)) | |
basalt.debug("Refreshed peripherals and total capacity.") | |
end) | |
--#endregion | |
--#region Threads | |
local function checkSolarEnergy() | |
stats.solar.total = energy.solar:getTotalStored() | |
while true do | |
stats.solar.total = energy.solar:getTotalStored() | |
currentStoredSolar:setText("Currently Stored Energy: " .. energy:toRadix(stats.solar.total)) | |
os.sleep(1) | |
end | |
end | |
local function checkStoredEnergy() | |
while true do | |
stats.su.total = energy.su:getTotalStored() | |
currentStoredEnergy:setText("Currently Stored Energy: " .. energy:toRadix(stats.su.total)) | |
os.sleep(1) | |
end | |
end | |
main:addThread("solarThread"):start(checkSolarEnergy) | |
main:addThread("storedThread"):start(checkStoredEnergy) | |
--#endregion | |
basalt.autoUpdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment