I've got an energy storage room with 125 Resonant Energy Cells all hooked up to a CC network (with wired modems) and to a computer that calculates the energy stored in said cells. Today (technically yesterday) I decided that I wanted to update my program to show me what my energy usage (or gain) was per tick.
1. My main bug - and my main reason for coming here - was that I was seeing my current energy stored every update… Major fail on my part as I realised - when I was copying my code to paste here - that I was setting one of my variables to a math.abs() of a different variable and not itself. So that's been fixed.
2. Are there any obvious bugs in my code that I've not seen? I'm reasonably confident with something like this, but I'm still a bit of a newbie at coding so I'd just like to check.
3. Timing… I had "sleep(2)" before but it always seemed a little longer than two seconds. Thinking that it must be the system slowing by iterating through 126 peripherals every 2 seconds, I changed it to 5 seconds. Now it seems nearer to 13 seconds to me. Is this a bug, my bad coding or something else?
4. Is it possible to calculate the energy used by my machines per tick? I guess not since I can only see what the storage is now and what it was before. I thought I would be able to use a "getEnergyOutputThisTick()" function or something like that, but only the energyStored and maxEnergyStored functions show up when I use getMethods().
5. Finally, in what ways could I optimise my program to a) run more efficiently and b ) for the code to look nicer and be easier to read? I like how easy it is now, but I constantly see code written by pros that's just so much "fancier" than anything I can write.
Here's the code in question:
Note: there is an 8 wide by 3 high monitor attached to the system too.
local ec = {}
local maxStore, curStore, storePercent, curUsed, lastStore = 0
local energyGained = true
function comma_value(n) -- credit http://richard.warburton.it
local left,num,right = string.match(n,'^([^%d]*%d)(%d*)(.-)$')
return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right
end
function calcEnergyUsage()
if lastStore == nil then
lastStore = 1
end
curUsed = (curStore - lastStore)/100
if curUsed < 0 then
energyGained = false
elseif curUsed > 0 then
energyGained = true
end
curUsed = math.abs(curUsed)
end
while true do
maxStore = 0
curStore = 0
storePercent = 0
curUsed = 0
for a,b in pairs(peripheral.getNames()) do
if peripheral.getType(B)/>/>/> == "cofh_thermalexpansion_energycell" then
ec[#ec+1] = peripheral.wrap(B)/>/>/>
curStore = curStore + ec[#ec].getEnergyStored("")
maxStore = maxStore + ec[#ec].getMaxEnergyStored("")
end
if peripheral.getType(B)/>/>/> == "monitor" then
m = peripheral.wrap(B)/>/>/>
term.redirect(m)
end
end
storePercent = curStore/maxStore*100
term.clear()
term.setCursorPos(1, 1)
local percentColour = 0
if tonumber(storePercent) < 30 then
percentColour = 16384
elseif tonumber(storePercent) < 50 and tonumber(storePercent) > 30 then
percentColour = 2
elseif tonumber(storePercent) < 70 and tonumber(storePercent) > 50 then
percentColour = 16
elseif tonumber(storePercent) < 90 and tonumber(storePercent) > 70 then
percentColour = 32
elseif tonumber(storePercent) > 90 then
percentColour = 2048
end
m.setTextScale(4)
term.setTextColour(colors.white)
print("Main Power Storage: ")
term.setTextColour(percentColour)
term.write(comma_value(curStore))
term.setTextColour(colors.white)
print(" RF of ")
term.setTextColour(percentColour)
term.write(comma_value(maxStore))
term.setTextColour(colors.white)
print(" RF ")
term.setTextColour(percentColour)
term.write(math.floor(storePercent).."% ")
calcEnergyUsage()
if lastStore ~= 0 then
if energyGained == true then
term.setTextColour(colors.lime)
else
term.setTextColour(colors.red)
end
term.write(comma_value(tostring(curUsed)))
term.setTextColour(colors.white)
print(" RF/t ")
end
lastStore = curStore
sleep(5)
end
Thanks for taking the time to check it (and as you can see by the "comma_value" function's comments, it's not mine).