This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
1shot1kill's profile picture

Help with status screen & control

Started by 1shot1kill, 24 August 2012 - 07:46 AM
1shot1kill #1
Posted 24 August 2012 - 09:46 AM
Hello,

I have been playing Tekkit (singleplayer) for a while now and about 1 and a half weeks ago I decided to try out the computercraft bit of it. Not having any knowledge of Lua or any other programming language I have been fiddling around with other people their scripts and some of my own. There are 2 things I want computercraft to do and that is control my machines with bundled cables and having a status screen show if everey machine is on or off. The first I have tried many scripts that were already available on this forum and I ended up liking this one: http://96.31.75.244/...-programs/]link.

I have modified it to control my machines but the problem is that when I quit the game and load it again all machines are off no matter what state they were in when I quit the game, is there any way to fix this?

code:
Spoilerfunction main1()
while true do
term.clear()
term.setCursorPos( 1, 1)
print("Master Control Unit Page 1")
print("What Machine would you like to access?")
print("_______________________________")
print("")
print("[1] Nuclear Reactor")
print("[2] Quarry 1")
print("[3] Quarry 2")
print("[4] Refinery")
print("[5] Pump fuel to quarry")
print("[6] Geothermal generators")
print("[7] ")
print("[8] ")
print("[9] ")
print("[0] ")
print("Hit [SPACE] for next page")
print("Hit [R] to reset machines on this page")
print("")
event, param1, param2, param3 = os.pullEvent()
if event == "char" and param1 == "1" then one() end
if event == "char" and param1 == "2" then two() end
if event == "char" and param1 == "3" then three() end
if event == "char" and param1 == "4" then four() end
if event == "char" and param1 == "5" then five() end
if event == "char" and param1 == "6" then six() end
if event == "char" and param1 == "7" then seven() end
if event == "char" and param1 == "8" then eight() end
if event == "char" and param1 == "9" then nine() end
if event == "char" and param1 == "0" then zero() end
if event == "char" and param1 == " " then main2() end
if event == "char" and param1 == "r" then reset("back") end
end
end

function main2()
while true do
term.clear()
term.setCursorPos( 1, 1)
print("Master Control Unit Page 2")
print("What Machine would you like to access?")
print("_______________________________")
print("")
print("[1] ")
print("[2] ")
print("[3] ")
print("[4] ")
print("[5] ")
print("[6] ")
print("[7] ")
print("[8] ")
print("[9] ")
print("[0] ")
print("Hit [SPACE] for next page")
print("Hit for previous page")
print("Hit [R] to reset machines on this page")
event, param1, param2, param3 = os.pullEvent()
if event == "char" and param1 == "1" then one1() end
if event == "char" and param1 == "2" then two1() end
if event == "char" and param1 == "3" then three1() end
if event == "char" and param1 == "4" then four1() end
if event == "char" and param1 == "5" then five1() end
if event == "char" and param1 == "6" then six1() end
if event == "char" and param1 == "7" then seven1() end
if event == "char" and param1 == "8" then eight1() end
if event == "char" and param1 == "9" then nine1() end
if event == "char" and param1 == "0" then zero1() end
if event == "char" and param1 == "b" then main1() end
if event == "char" and param1 == " " then main3() end
if event == "char" and param1 == "r" then reset("left") end
end
end

function main3()
while true do
term.clear()
term.setCursorPos( 1, 1)
print("Master Control Unit Page 3")
print("What Machine would you like to access?")
print("_______________________________")
print("")
print("[1] ")
print("[2] ")
print("[3] ")
print("[4] ")
print("[5] ")
print("[6] ")
print("[7] ")
print("[8] ")
print("[9] ")
print("[0] ")
print("Hit for previous page")
print("Hit [R] to reset machines on this page")
print("")
event, param1, param2, param3 = os.pullEvent()
if event == "char" and param1 == "1" then one2() end
if event == "char" and param1 == "2" then two2() end
if event == "char" and param1 == "3" then three2() end
if event == "char" and param1 == "4" then four2() end
if event == "char" and param1 == "5" then five2() end
if event == "char" and param1 == "6" then six2() end
if event == "char" and param1 == "7" then seven2() end
if event == "char" and param1 == "8" then eight2() end
if event == "char" and param1 == "9" then nine2() end
if event == "char" and param1 == "0" then zero2() end
if event == "char" and param1 == "b" then main2() end
if event == "char" and param1 == "r" then reset("right") end
end
end

function on(x, y, z)
if rs.testBundledInput(z, x)==true then
print(y.." already on")
sleep(1)
main1()
else
rs.setBundledOutput(z, rs.getBundledOutput(z) + x)
print(y.." on")
sleep(1)
main1()
end
end

function off(x, y, z)
if rs.testBundledInput(z, x)==false then
print(y.." already off")
sleep(1)
main1()
else
rs.setBundledOutput(z, rs.getBundledOutput(z) - x)
print(y.." off")
sleep(1)
main1()
end
end

function check(x, y, z)
if rs.testBundledInput(z, x)==false then
print(y.." off")
sleep(2)
term.clear()
term.setCursorPos( 1, 1)
main1()
end
if rs.testBundledInput(z, x)==true then
print(y.." on")
sleep(2)
term.clear()
term.setCursorPos( 1, 1)
main1()
end
end

function menu(x, y, z)
while true do
term.clear()
term.setCursorPos( 1, 1)
print(y.." Control Selected")
print("")
print("[1] Turn on " ..y)
print("[2] Turn off " ..y)
print("[3] Check Status")
print("[4] Return to Main Menu")
event, param1, param2, param3 = os.pullEvent()
if event == "char" and param1 == "1" then on(x, y, z) end
if event == "char" and param1 == "2" then off(x, y, z) end
if event == "char" and param1 == "3" then check(x, y, z) end
if event == "char" and param1 == "4" then main1() end
end
end

function reset(z)
rs.setBundledOutput(z ,0)
print("Machines reset")
term.setCursorPos( 1, 18)
term.clearLine()
sleep(1)

end

function one()
menu(256, "Nuclear reactor", "bottom")
end

function two()
menu(1, "Quarry 1", "bottom")
end

function three()
menu(16384, "Quarry 2", "bottom")
end

function four()
menu(2048, "Refinery", "bottom")
end

function five()
menu(32, "Pump fuel to quarries", "bottom")
end

function six()
menu(2, "Geothermal generators", "bottom")
end

function seven()
menu(colors.black, "[Machine]", "back")
end

function eight()
menu(colors.green, "[Machine]", "back")
end

function nine()
menu(colors.black, "[Machine]", "back")
end

function zero()
menu(colors.black, "[Machine]", "back")
end


function one1()
menu(colors.black, "[Machine]", "left")
end

function two1()
menu(colors.black, "[Machine]", "left")
end

function three1()
menu(colors.black, "[Machine]", "left")
end

function four1()
menu(colors.black, "[Machine]", "left")
end

function five1()
menu(colors.black, "[Machine]", "left")
end

function six1()
menu(colors.black, "[Machine]", "left")
end

function seven1()
menu(colors.black, "[Machine]", "left")
end

function eight1()
menu(colors.green, "[Machine]", "left")
end

function nine1()
menu(colors.black, "[Machine]", "left")
end

function zero1()
menu(colors.black, "[Machine]", "left")
end


function one2()
menu(colors.black, "[Machine]", "right")
end

function two2()
menu(colors.black, "[Machine]", "right")
end

function three2()
menu(colors.black, "[Machine]", "right")
end

function four2()
menu(colors.black, "[Machine]", "right")
end

function five2()
menu(colors.black, "[Machine]", "right")
end

function six2()
menu(colors.black, "[Machine]", "right")
end

function seven2()
menu(colors.black, "[Machine]", "right")
end

function eight2()
menu(colors.green, "[Machine]", "right")
end

function nine2()
menu(colors.black, "[Machine]", "right")
end

function zero2()
menu(colors.black, "[Machine]", "right")
end
main1()

The status screen is giving me more of a headache, I was somehow not able to find anything on here that suited my needs (maybe I am using the wrong words to search for it) so I decided to try it out myself but it only does what I want in certain states, if I turn on the quarry the script keeps spamming the 2 lines until it quits with the too long without yielding message. Could somebody fix the script below so I can edit it myself to feature all my machines? I just want it to show my machines with their online or offline status behind it, updating every 1 to 2 seconds.

code:
Spoilerwhile true do
term.clear()
term.setCursorPos( 1, 1)
if rs.testBundledInput("back", 256) == true
then print("Nuclear reactor on")
else print("Nuclear reactor off")
if rs.testBundledInput("back", 2048) == true
then print("Quarry 1 on")
else print("Quarry 1 off")
break
end
end
end

I actually hate to ask for help because I rather do things like this myself but I have tried so many things to fix this that I have lost sight on what could be wrong.
Cranium #2
Posted 24 August 2012 - 01:48 PM
Here, check out this program I created. It should do everything you mentioned, but I am still working on displaying to a monitor. Should be updated soon.
http://96.31.75.244/forums2/index.php?/topic/3250-master-control-system-mcs-ver-15/
1shot1kill #3
Posted 25 August 2012 - 11:42 AM
Hey thanks for your reply, I had already tried your script but it's just way more than I need. As I mentioned I play singleplayer, thus a login is useless unless a smart creeper invades my base and accesses my computer there is no need for it. And I also don't have a need to monitor my energy storages etc. All I need is a script that runs when I start using the pc and that allows me to change the state of any of my machines connected to the bundled cable with 2 key presses. Which the script I currently uses does it just doesn't keep them on when I quit the game and load it again, that is the only problem I have with that one. The status screen is still giving me a headache because of my lack of knowledge of Lua.
Cranium #4
Posted 25 August 2012 - 04:00 PM
Well, what you can do is kinda what I did. I defined my statuses as variables, and put them into a table. I then saved that table into a file, and had the computer read that file and act accordingly on those results. Here is my save/load/restart code:

local controlTable = {}
local function saveState(saveTable)
    local file = fs.open("status", "w")
    if file then
	    for eNum, eInfo in ipairs(saveTable) do
		    file.writeLine(eInfo.type..","..eInfo.color..","..eInfo.status)
	    end
	    file.close()
    end
end
local function loadState()
    local loadTable = {}
    local file = fs.open("status", "r")
    if file then
	    readLine = file.readLine()
	    while readLine do
		    local lineTable = {}
		    lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%w+),(%w+),(%w+)")
		    if lineTable.type then
			    table.insert(loadTable, lineTable)
		    end
		    readLine = file.readLine()
	    end
	    file.close()
    end
    return loadTable
end
function resume()
while true do
controlTable = loadState()
for eNum, dInfo in ipairs(controlTable) do
  if dInfo.type ~= "Admin" and dInfo.type ~= "User" then
   if dInfo.color == "redstone" then
    if dInfo.status == "false" then
	 rs.setOutput("back",false)
    elseif dInfo.status == "true" then
	 rs.setOutput("back",true)
    end
   elseif not dInfo.color == "redstone" then
    if dInfo.status == "false" then
	 rs.setBundledOutput("back",colors.subtract(dInfo.color))
    elseif dInfo.status == "true" then
	 rs.setBundledOutput("back",colors.combine(dInfo.color))
    end
   end
  end
end break
end
end
What you do is, in your code, you can define your systems like this:

local deviceTable={type=macerator}
local deviceTable.color="colors.red"
local deviceTable.status="off"
All you have to do is define which options you use, and use this:

table.insert(controlTable,deviceTable)
saveState(controlTable)
After each of your definitions.
When you change the state of something, you use this:

local controlTable=loadState()
--define the state of the item
deviceTable.status = "on"
table.insert(controlTable,POSITION IN THE TABLE,deviceTable)
table.remove(controlTable,NEXT POSITION IN THE TABLE,deviceTable)
saveState(controlTable)
I hope this information helps. If you have any questions on that code, feel free to let me know.
1shot1kill #5
Posted 25 August 2012 - 08:47 PM
Alright thanks I will try to get that to work, could you tell me what I did wrong in the code on the status screen that makes it spam it over and over again instead of refreshing? That one is what is bugging me the most.