Posted 11 July 2014 - 05:50 PM
Okay, I made this little program for the Tekkit server I'm currently running. It's a program that runs on loop, displaying on a screen the amount of energy stored in one of the 24 Resonant Energy Cells in our facility. Here's the code:
It runs fine, no issues except the fact that it's a bit sluggish. It updates about every 2 seconds or so, and I fear that this may affect the accuracy of the displayed information.
So 2 questions:
1) Would the getLoss() function show inaccurate results? and
2) Is there any way I can clean up this code to make it run faster? It's about 139 lines of code.
From an aspiring programmer, thanks in advance! :D/>
local cells = {
{ p=peripheral.wrap("back"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_0"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_1"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_2"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_3"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_4"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_5"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_6"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_7"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_8"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_9"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_10"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_11"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_12"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_13"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_14"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_15"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_16"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_17"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_18"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_19"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_20"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_21"), n=0, max=0 },
{ p=peripheral.wrap("cofh_thermalexpansion_energycell_22"), n=0, max=0 }
}
function getLoss()
--This function is used to calculate the net flow of energy into the
--energy cells. It does so by measuring the cell energy, sleeping for
--1 tick, and then measuring it again.
local before = 0
local after = 0
for i=1,#cells do
readCell(i)
before = before + cells[i].n
sleep(.05)
readCell(i)
after = after + cells[i].n
end
local net = after - before
x,y = m.getCursorPos()
m.setCursorPos(3,y+4)
if net < 0 then
m.write("ENERGY USAGE: "..net)
m.write(" RF/t")
elseif net > 0 then
m.write("ENERGY GAIN: +"..net)
m.write(" RF/t")
else
m.write("NO NET CHANGE (0 RF/t)")
end
end
function readCell(cell)
--This reads the amount of energy in the given cell and sets it as
--the variable "n", as well as setting the maximum energy storage as the
--variable "max" in the table.
local p = cells[cell].p
cells[cell].n = p.getEnergyStored("unknown")
cells[cell].max = p.getMaxEnergyStored("unknown")
end
function display()
--This prints a list of all 24 cells, separates them into 3 columns, and
--lists the amount of energy stored in each.
for i=1,#cells do
if i <= 10 then
m.setCursorPos(3,1+i)
m.write("CELL #"..i)
m.write(": "..cells[i].n)
m.write(" RF")
elseif i <= 20 and i > 10 then
m.setCursorPos(27,1+i-10)
m.write("CELL #"..i)
m.write(": "..cells[i].n)
m.write(" RF")
elseif i <= 30 and i > 20 then
m.setCursorPos(51,1+i-20)
m.write("CELL #"..i)
m.write(": "..cells[i].n)
m.write(" RF")
end
end
end
function totalPrint()
--Prints the total storage capacity of all the cells, how much energy is
--stored, and the percentage of potential energy.
local total = 0
local totalMax = 0
for i=1,#cells do
total = total + cells[i].n
totalMax = totalMax + cells[i].max
end
local totalPC = ( ( total / totalMax ) * 100 )
local x,y = m.getCursorPos()
m.setCursorPos(3,y+10)
m.write("TOTAL: "..total)
m.write(" RF")
m.setCursorPos(3,y+11)
m.write("MAX: "..totalMax)
m.write(" RF")
m.setCursorPos(3,y+13)
m.write("STORED: "..totalPC)
m.write("%")
end
--Main Command Line
function main()
--Runs all the functions in order.
local isRunning = true
m = peripheral.wrap("monitor_0")
m.clear()
m.setTextScale(1.47)
while isRunning do
for i=1,#cells do
readCell(i)
end
m.clear()
display()
totalPrint()
getLoss()
end
end
main()
It runs fine, no issues except the fact that it's a bit sluggish. It updates about every 2 seconds or so, and I fear that this may affect the accuracy of the displayed information.
So 2 questions:
1) Would the getLoss() function show inaccurate results? and
2) Is there any way I can clean up this code to make it run faster? It's about 139 lines of code.
From an aspiring programmer, thanks in advance! :D/>