Posted 11 April 2014 - 12:12 PM
Hello. So I will say that I am a total noob when it comes to lua. I have modified code here and there with some trial and error, but finally sat down and wrote something from scratch. It works as intended, but I have decided I want to improve its functionality.
Purpose of the code. It reads the energy in my energycells and sends a redstone signal to my laser drill when power gets below a certain level. When the power recovers it shuts off the signal and allows the drill to activate again.
Here is my code. All but the round() function are originally by myself. I am actually proud of the work I did, but I have hit a wall with how I want to modify this code.
So, my code is in the spoiler (took some time to copy since http is off on the server I play).
My goals are to find a way to allow the program to detect new energy cells when they are placed down, set them to a callable variable of sorts, and then collect info as required to display the values as needed, and to activate or deactivate the laser drill at certain percentage points.
Any help you can offer will be appreciated.
Purpose of the code. It reads the energy in my energycells and sends a redstone signal to my laser drill when power gets below a certain level. When the power recovers it shuts off the signal and allows the drill to activate again.
Here is my code. All but the round() function are originally by myself. I am actually proud of the work I did, but I have hit a wall with how I want to modify this code.
Spoiler
-- * Variables *
c00 = peripheral.wrap("cofh_thermal_expansion_energycell_2") -- wrap first energycell
c01 = peripheral.wrap("cofh_thermal_expansion_energycell_3") -- wrap second energycell
local cur0 = 0 -- variable for later use
local cur1 = 0 -- variable for later use
local pct0 = 0 -- variable for later use
local pct1 = 0 -- variable for later use
local overallPCT = 0 -- variable for later use
local max0 = c00.getMaxEnergyStored("unknown") -- variable for finding the % of energy
local max1 = c01.getNaxEnergyStored("unknown") -- see above
-- * Define functions
local function getStored() -- updates
cur0 = c00.getEnergyStored("unknown")
cur1 = c01.getEnergyStored("unknown")
end
local function round(num, idp) -- round function I found on the lua pages
if idp and idp > 0 then
local mult = 10^idp
return math.floor(num * mult + 0.5) / mult
end
return math.floor(num + 0.5)
end
local function pct() -- calculates the percent of energy stored in the cells
getStored()
pct0 = round(cur0 / max0 * 100, 2)
pct1 = round(cur1 / max1 * 100, 2)
overallPCT = round((pct0 + pct1)) / 2, 2)
end
local function print0() -- prints the info for energy cell 1
term.setCursorPos(1,2)
print("energycell 1: ")
term.setCursorPos(15,2)
print(cur0)
term.setCursorPos(23,2)
print("/")
term.setCursorPos(25,2)
print(max0)
term.setCursorPos(40,2)
print(pct0.."% ")
end
local function print1() -- prints the info for energy cell 2
term.setCursorPos(1,3)
print("energycell 2: ")
term.setCursorPos(15,3)
print(cur1)
term.setCursorPos(23,3)
print("/")
term.setCursorPos(25,3)
print(max1)
term.setCursorPos(40,3)
print(pct1.."% ")
end
local function printA() -- inform of laser drill setting
term.setCursorPos(1,7)
if overallPCT<75 then
print("Power is currently low, laser drill deactivated.")
elseif overallPCT>75 then
print("Power is sufficient. Laser drill currently active.")
end
end
local function printO() -- print overall percentage
term.setCursorPos(1,5)
print("overall percentage full: "..overallPCT.."%")
end
-- main program
rs.setBundledOutout("bottom, 0) -- deactivates redstone signal in case it was on
term.clear()
term.setCursorPos(15,1) -- these lines give display labels
print("Current)
term.setCursorPos(23,1)
print("/")
term.setCursorPos(25,1)
print("Max")
term.setCursorPos(40,1)
print("Percent")
while true do -- main loop
pct()
print0()
print1()
printO()
printA()
if overallPCT<25 then
if rs.getBundledOutput("bottom") == 0 then
rs.setBundledOutput("bottom", colors.yellow) -- if power lower than 25% it deactivates the laser drill
end
while overallPCT<75 do -- locks the main loop to this section till power is again above 75%
pct()
print0()
print1()
printO()
printA()
sleep(15)
end
end
if rs.getBundledOutputo("bottom") ~= 0 then
rs.setBundledOutput("bottom", 0) -- activates laser drill when power is high enough
end
sleep(10)
end
So, my code is in the spoiler (took some time to copy since http is off on the server I play).
My goals are to find a way to allow the program to detect new energy cells when they are placed down, set them to a callable variable of sorts, and then collect info as required to display the values as needed, and to activate or deactivate the laser drill at certain percentage points.
Any help you can offer will be appreciated.