Posted 04 December 2013 - 01:33 AM
First of all, let me thank you for taking the time to read through this. I am a LUA noob, and so far have learned most of what I know through the lua.org website and through this website as well. I appreciate what all of you guys do for the ComputerCraft community.
I want to start off by talking about this program and letting you know that it does work, so we're off to a good start. Whether it works well is subjective to one's knowledge of LUA, hence why I'm posting here. I feel as though what I'm trying to accomplish could be done better if I knew more or if I knew the right syntax to get my point across. I don't have a bunch of comments in my code because I hope I wrote it for the most part as readable (Good code is it's own documentation they say). If not, I can easily break it down into chunks that provide details explaining what each bit does. I'm using CC version 1.57 and OP 0.2.1, but our server is due for mod updates soon.
There are a lot of "~= nil" conditionals in there, because unavoidably I receive nil statements that I haven't quiet ironed out of yet, but I've got most of the bugs squashed and it's been running well for a couple days now.
What I'm looking for here is constructive criticism, and comments like "Hey, why'd you do that when you could have just done this much simpler thing…." Most of the time my answer will be "Holy crap you can do that?!" Any advice you could offer to make this code more streamline/efficient would be greatly appreciated. Again, thank you for taking the time to wade through my programming.
Here are the goals for the program:
1. Pull the stored energy data from the 9 network peripheral attached MFSUs
2. Calculate the total energy stored and total energy percentage and write it
3. Calculate the stored energy data into percentages, color them by percentage, write them on the monitor
4. Draw energy flow chart statics (box and scale text)
5. Find the difference in total energy values for 1 second, divide it down into segments and chart them
6. Have the chart scroll to the left, per second, to monitor changes in energy flow.
This is a animated .jpeg of the working program running: (Alt Link if not displayed below)
I tried to cut it down to 200 lines. I was not going to CODE it here because it was slightly over, but I figured it might save you a bit of trouble. It's not like a 500+ line OS or anything crazy like that. Here's the link to the pastebin, and the code is below.
If it's too confusing or there's something that doesn't make sense to you (which is likely to happen since I'm still a LUA white belt), please quote out a section of code or just say you can't understand any of it, hah, and I can explain what it's "supposed" to do line by line. I did my best to make it easy to read. I appreciate all the help and support.
I want to start off by talking about this program and letting you know that it does work, so we're off to a good start. Whether it works well is subjective to one's knowledge of LUA, hence why I'm posting here. I feel as though what I'm trying to accomplish could be done better if I knew more or if I knew the right syntax to get my point across. I don't have a bunch of comments in my code because I hope I wrote it for the most part as readable (Good code is it's own documentation they say). If not, I can easily break it down into chunks that provide details explaining what each bit does. I'm using CC version 1.57 and OP 0.2.1, but our server is due for mod updates soon.
There are a lot of "~= nil" conditionals in there, because unavoidably I receive nil statements that I haven't quiet ironed out of yet, but I've got most of the bugs squashed and it's been running well for a couple days now.
What I'm looking for here is constructive criticism, and comments like "Hey, why'd you do that when you could have just done this much simpler thing…." Most of the time my answer will be "Holy crap you can do that?!" Any advice you could offer to make this code more streamline/efficient would be greatly appreciated. Again, thank you for taking the time to wade through my programming.
Here are the goals for the program:
1. Pull the stored energy data from the 9 network peripheral attached MFSUs
2. Calculate the total energy stored and total energy percentage and write it
3. Calculate the stored energy data into percentages, color them by percentage, write them on the monitor
4. Draw energy flow chart statics (box and scale text)
5. Find the difference in total energy values for 1 second, divide it down into segments and chart them
6. Have the chart scroll to the left, per second, to monitor changes in energy flow.
This is a animated .jpeg of the working program running: (Alt Link if not displayed below)
I tried to cut it down to 200 lines. I was not going to CODE it here because it was slightly over, but I figured it might save you a bit of trouble. It's not like a 500+ line OS or anything crazy like that. Here's the link to the pastebin, and the code is below.
mfsu = {}
mfsu["1"] = peripheral.wrap("mfsu_3")
mfsu["2"] = peripheral.wrap("mfsu_4")
mfsu["3"] = peripheral.wrap("mfsu_5")
mfsu["4"] = peripheral.wrap("mfsu_6")
mfsu["5"] = peripheral.wrap("mfsu_7")
mfsu["6"] = peripheral.wrap("mfsu_8")
mfsu["7"] = peripheral.wrap("mfsu_9")
mfsu["8"] = peripheral.wrap("mfsu_10")
mfsu["9"] = peripheral.wrap("mfsu_11")
m = peripheral.wrap("monitor_13")
m.clear()
storedEU = {}
storedPerc = {}
plotter = {}
printer = {}
energyTotals = 0
energyFlow = 0
function storedTotal()
energyTotal = 0
for i = 1,9 do
energyTotal = energyTotal + mfsu[tostring(i)].getEUStored()
end
energyFlow = (energyTotal - energyTotals) / 20
energyTotals = energyTotal
energyPercentage = shorten(tostring(energyTotal / 360000000 * 100))
m.setCursorPos(3,3)
m.write("Total Energy Stored: "..energyTotal.." EU")
m.setCursorPos(9,4)
m.write("Total Capacity: ")
colorPerc(energyPercentage)
end
function storedEach()
for i = 1,9 do
storedEU[i] = mfsu[tostring(i)].getEUStored()
storedPerc[i] = shorten(tostring(storedEU[i] / 40000000 * 100))
end
m.setCursorPos(1,6)
for i = 1,3 do
m.write("MFSU"..i..": ")
colorPerc(storedPerc[i])
xPos = i * 14
m.setCursorPos(xPos,6)
end
m.setCursorPos(1,7)
for i = 4,6 do
m.write("MFSU"..i..": ")
colorPerc(storedPerc[i])
xPos = (i-3) * 14
m.setCursorPos(xPos,7)
end
m.setCursorPos(1,8)
for i = 7,9 do
m.write("MFSU"..i..": ")
colorPerc(storedPerc[i])
xPos = (i-6) * 14
m.setCursorPos(xPos,8)
end
end
function shorten(num)
if num ~= nil then
dec = string.find(tostring(num),".",1,true)
if dec ~= nil then
dec = dec + 1
return (string.sub(num,1,dec))
end
else
return ("0")
end
end
function percRound(num)
subW,subR,whole,remainder = 0,0,0,0
subW,subR = string.len(num) - 2 , string.len(num)
whole = tonumber(string.sub(num,1,subW))
remainder = tonumber(string.sub(num,subR,subR))
if remainder >= 5 then
whole = whole + 1
end
return whole
end
function colorPerc(num)
num = tonumber(num)
if num ~= nil then
if num >= 0 then
m.setTextColor(colors.red)
end
if num > 30 then
m.setTextColor(colors.orange)
end
if num > 60 then
m.setTextColor(colors.yellow)
end
if num > 90 then
m.setTextColor(colors.lime)
end
m.write(num.."%")
space = string.len(tostring(num)) - 5 * (-1)
for i = 1,space do
m.write(" ")
end
m.setTextColor(colors.white)
end
end
function flowChart()
for i = 1,15 do
m.setCursorPos(6,(i + 10))
m.write("|")
end
for i = 1,39 do
m.setCursorPos(i,26)
m.write("-")
end
for i = 1,15 do
m.setCursorPos(34,(i + 10))
m.write("|")
end
for i = 1,2 do
if i == 1 then
yP = 1
else
yP = 35
end
m.setCursorPos(yP,11)
m.write("2.1k ")
m.setCursorPos(yP,13)
m.write("1.5k ")
m.setCursorPos(yP,15)
m.write(" 900 ")
m.setCursorPos(yP,17)
m.write(" 300 ")
m.setCursorPos(yP,19)
m.write("-300 ")
m.setCursorPos(yP,21)
m.write("-900 ")
m.setCursorPos(yP,23)
m.write("-1.5k")
m.setCursorPos(yP,25)
m.write("-2.1k")
end
end
function drawPlots()
m.setCursorPos(12,26)
m.write("--("..energyFlow.." EU/t)--------")
yPos = plotPos(energyFlow) + 18
printer[34] = yPos
for i = 7,33 do
j = i + 1
h = i
printer[i] = printer[j]
for i = 11,25 do
m.setCursorPos(h,i)
if i == printer[h] then
if i < 18 then
m.setBackgroundColor(colors.cyan)
else
m.setBackgroundColor(colors.blue)
end
else
m.setBackgroundColor(colors.black)
end
m.write(" ")
m.setBackgroundColor(colors.black)
end
end
end
function plotPos(num)
plotter,plotW,plotR = 0,0,0
plotter = energyFlow / 300
if plotter < 0 then
plotW = tonumber(string.sub(tostring(plotter),2,2))
plotR = tonumber(string.sub(tostring(plotter),4,4))
negative = true
else
plotW = tonumber(string.sub(tostring(plotter),1,1))
plotR = tonumber(string.sub(tostring(plotter),3,3))
negative = false
end
if plotR ~= nil then
if plotR >= 5 then
plotW = plotW + 1
end
end
if negative == false then
plotW = plotW * (-1)
end
return plotW
end
function label()
m.setCursorPos(13,1)
m.write("Energy Monitor")
m.setCursorPos(1,10)
m.write("---------- Energy Flow (EU/t)----------")
end
label()
flowChart()
while true do
storedTotal()
storedEach()
drawPlots()
sleep(1)
end
If it's too confusing or there's something that doesn't make sense to you (which is likely to happen since I'm still a LUA white belt), please quote out a section of code or just say you can't understand any of it, hah, and I can explain what it's "supposed" to do line by line. I did my best to make it easy to read. I appreciate all the help and support.