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

Menu with multiple choiches.

Started by Kustus, 29 March 2012 - 09:15 PM
Kustus #1
Posted 29 March 2012 - 11:15 PM
HI

Ok so in technic pack I've built myself a HUGE oilrig thingy with lots of oiltanks and pumps.

I'm making a program to controll all the pumps from 1 terminal.

I made a program for 2 pumps, and it works fine.

but adding mulitple tanks and pumps makes to many possible choiches and variables.

I was hoping to make a nice menu also, but reading the tutorials they are often for 2-3 choices and I 6.

So the way I wanted this was pick a tank, choice to pump into it or from it, and lastly how many seconds it should pump.

came up with this for 1 tank.


Spoiler
local function pass()

local A = "1"
local B = "2"
local S = "Pump for how many seconds?"

  term.clear()
  term.setCursorPos(1, 1)
  print("Tank System 1.0\n")
  sleep(1)
  print("Options:\n")
  sleep(1)
  print("1. Pump from Tank1 to BaseTank")
  print("2. Pimp from BaseTank to Tank1\n")
  write("Press 1 or 2 to select: ")
	input = read()

  if input == A then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	t1 = io.read()
	sleep(1)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping for")
	print(t1)
	print("seconds...")
	rs.setBundledOutput("right", colors.white)
	sleep(t1)
	rs.setBundledOutput("right", 0)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping complete!")
	sleep(3)
  end
--line 37
  if input == B then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	sleep(1)
	t2 = io.read()
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping for ")
	print(t2)
	print("seconds...")
	rs.setOutput("back", true)
	sleep(t2)
	rs.setOutput("back", false)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping Complete!")
	sleep(3)
  else
	term.clear()
	term.setCursorPos(1, 1)
	print("Invalid Selection!")
  end
end
while true do
	pass()
end
Advert #2
Posted 30 March 2012 - 12:12 AM
You can use tables, which let you reference multiple things with one varaible:


local data = {}

while true do
 print("Input key")
 local key = read()
 if not data[key] then
  print("Input data:")
  data[key] = read()
 else
  print("Data: " .. data[key])
 end
end

You can also use it with functions; I'll find a post and link it, as it has a pretty nice example.

Edit: Here's the example (follow the pastebin link):
http://www.computercraft.info/forums2/index.php?/topic/398-help-improving/page__view__findpost__p__2503
Edited by
Kustus #3
Posted 30 March 2012 - 07:01 AM
Thanks alot! Will do a test of it once I get home from work.
Kustus #4
Posted 30 March 2012 - 04:30 PM
ok, I've been playing around with the script you linked me to.

But i've got another question (suprised?" :o/>/>

What whould be the best/easiest way to code?

the 4 (for now) oil tanks all have the same options, to either pump to the main oil tank or pump from the main oil tank to the chosen oiltank.

the maintank has the option to pump into the oil refinery (makes fuel in technic pack) and to pump into 3 remote tanks placed closed to some contraptions around the map.
it also has the choice to pump from the Input tank (where all the new oil I find is pump into).

so whats the best way?

1. pick a tank first and then choose the options?
2. pick an option first and then chose witch tank to perform the task to?

my thoughts where that option 1 whould make alot of doubled code (same code over and over).
but im not shure how to go around number 2.

also, its the timing thing I need help with. new to lua and not shure how i can use the table thing to store multiple values to be used to enable the correct redpower signal?

also when I make 4 selections in the menu, (making 5 the auto shutdown) it gives me an error saying Out of Bounds.
Advert #5
Posted 30 March 2012 - 06:37 PM
ok, I've been playing around with the script you linked me to.

But i've got another question (suprised?" :o/>/>

What whould be the best/easiest way to code?

the 4 (for now) oil tanks all have the same options, to either pump to the main oil tank or pump from the main oil tank to the chosen oiltank.

the maintank has the option to pump into the oil refinery (makes fuel in technic pack) and to pump into 3 remote tanks placed closed to some contraptions around the map.
it also has the choice to pump from the Input tank (where all the new oil I find is pump into).

so whats the best way?

1. pick a tank first and then choose the options?
2. pick an option first and then chose witch tank to perform the task to?

my thoughts where that option 1 whould make alot of doubled code (same code over and over).
but im not shure how to go around number 2.

also, its the timing thing I need help with. new to lua and not shure how i can use the table thing to store multiple values to be used to enable the correct redpower signal?

also when I make 4 selections in the menu, (making 5 the auto shutdown) it gives me an error saying Out of Bounds.

You should post your code when you get an error, I have an idea of what it is, but that's just an idea until I see the code :o/>/>

You can use functions to do the repetetive stuff, then send them arguments to make them do it on different stuff.


*Not tested, but should look something like this. You'd have to add the redstone functions, too.

function toggleTankValve(tank)
 tank.valve = not tank.valve
end
local tanks = {alpha = {valve = false}, beta = {valve = true}, gamma = {valve = true}}

print("Choose a tank")
local tank = tanks[read()]
if not tank then
print("Couldn't find tank.")
return
end
print("Tank status: ", tank.valve)
print("Toggle?")
if read() == "y" then
toggleTankValve(tank)
end
Kustus #6
Posted 30 March 2012 - 07:25 PM
You my good sir are somewhat of a genius. :o/>/>

Will try to make the entire program now.

but for the error. I did a bad thing. copied and pasted the code from pastebin that u linked.
then i altered the meny options.


    local tChoices = {}
    tChoices[0] = 'Pump from Main Oil Tank'
    tChoices[1] = 'Pump to Main Oil Tank'
    tChoices[2] = 'Pump to a remote remote location'
    tChoices[3] = 'Pump to refinery'
    tChoices[4] = 'Pump from the Fresh Oil Tank'
     
     
    table.insert(tChoices, 'Shutdown') -- If you add more stuff, the shutdown will always come at the bottom, without you having to change its #.

then when I ran the program and tried to go down to the number 5 selection (the shutdown) it said.
selection out of bounds.

dont know if I did a boo boo or if something from the original code is buggy. (I expect the reason for the error is a PEBKAC.)
Advert #7
Posted 30 March 2012 - 07:29 PM
If you go down a bit, you'll see that each index (0-4 in your case) are mapped to functions that do stuff:


local tActions = {} -- Twin table of tChouices, we'll put functions in here to do stuff.



tActions[0] = function() -- Stuff a function in here!
	term.clear()
	term.setCursorPos(1, 1) -- Reset the cursor's position to be safe
	print(sSeperator)
	print("|                          Terminal Password                             |")
	print(sSeperator)
	print("Information Deleted By User")
	term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
	write("    [Press enter to return to main menu]")
	read()
end

tActions[1] = function()
	term.clear()
	term.setCursorPos(1, 1)
	print(sSeperator)
	print "|                                Mining Reports                           |"
	print(sSeperator)
	print "Today I mined 32 iron in a deep mine under my"
	print "house. I got attacked by a zombie on my way out"
	print "of the mine. I had to drop all my iron so I could"
	print "Run fast enough to outrun the zombie."

	term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
	write("    [Press enter to return to main menu]")
	read()
end
...

You'll need to modify those to do what you want with your pump, and add any missing ones.
Kustus #8
Posted 30 March 2012 - 09:41 PM
ok the thing is coming together nicely now.

But I got one last question, (no promise)

While the tanking takes place, I have a sleep time defined by a variable.
Is it possible to make a "progress" bar based on the sleep-time variable?

like say for x seconds it whould show a progress bar that used x seconds to go to full?

Reason for this is that if you pump a long time u might want to se that something actually is happening rather then just a blank screen or whatever with no indication that something is happening?

Just think it whould make the program look a while lot nicer. :o/>/>

and again, thanks for all the help so far!
Kustus #9
Posted 30 March 2012 - 10:02 PM
ok I think I might know how it could work, but dont know how to code it.

something like this?


write("pump for how many seconds?: ")
  t = io.read()

  t = tonumber(t)

but now I was thinking dividing the t variable by 10 and then make a function that repeated a loop 10 times with a print and a sleep where the total sleep time was equal to the t variable? and just make a print with that adds a line each time its run.
something like this maybe:



print("|-		 |")
print("|--		|")
print("|---	   |")
print("|----	  |")
print("|-----	 |")
print("|------	|")
print("|-------   |")
print("|--------  |")
print("|--------- |")
print("|----------|")

edit: ok the print is all screwed up here. but you get the idea..

but how to make it add in each print and how to make the sleep time and all that? no idea.
Advert #10
Posted 30 March 2012 - 10:10 PM
How long does the pump pump?

You could try something like this:

-- Don't use this, it's broken and all wrong :o/>/>
function sleepWithPercentOutput(n)
 local startTime = os.clock() -- get the tick we start in
 local timer = os.startTimer(n) -- start the timer we'll use for determining when to exit
 local cursorx, cursory = term.getCursorPos()
 for sEvent, t in os.pullEvent() do
  term.setCursorPos(cursorx, cursory) -- reset the cursor position
  term.write("    ") -- clearing the space
  term.setCursorPos(cursorx, cursory) -- get into position for printing below; doing this before checking if to exit to clear the %  sign
  if sEvent == "timer" and t == timer then
   break
  end
  local percent = tostring(os.clock() - startTime / n)
  percent = #percent == 3 and percent or (#percent == 2 and ("0" .. percent) or "00" .. percent) -- make it 3 characters long the weird way
  term.write(percent .. "%")
 end
end

Testing the code I wrote now, but I think it should work.

*Won't actually show a progress bar, but will tell you how many %. You could add in the remaining seconds if you wanted to, too.

Edit: I derped; this is completely broken :o/>/>.

Will fix it in a few.

Edit: Fixed:


function sleepWithPercent(n)
 local startTime = os.clock() -- Get the starting time
 local timer = os.startTimer(n) -- Create a timer so we know when to stop sleeping
 local cursorx, cursory = term.getCursorPos() -- Save the cursor position to avoid spamming the terminal
 local tick = os.startTimer(0) -- Create another timer so we can update the % shown on the terminal
 for sEvent, t in os.pullEvent do -- Loop, with the results of os.pullEvent as sEvent and t
  if sEvent == "timer" and t == timer then -- Check for the exit timer
   term.setCursorPos(cursorx, cursory) -- Clean the screen
   term.write("    ")
   term.setCursorPos(cursorx, cursory)
   break -- Break out of the loop
  elseif sEvent == "timer" and t == tick then -- Check for the update timer
   term.setCursorPos(cursorx, cursory)
   tick = os.startTimer(0.1) -- Re-start the timer since timers only run once
   local p = tostring(math.floor(((os.clock() - startTime) / n) * 100)) -- Calculate the %age
   p = #p == 3 and p or (#p == 2 and ("0" .. p) or "00" .. p) -- Pad with 0s infront if it's not long enough
   term.write(p .. "%")
  end
 end
end

-- Use it like so:
print("Taking over world, please wait...")
term.write("Progress: ")
sleepWithPercent(20)
print("Attempt failed. Your local authorities have been notified.")
Kustus #11
Posted 30 March 2012 - 10:27 PM
the pumps go for about 30 seconds to 2 minutes?



Ok i tested putting this into my original tank program to se how it works, it dosn't. :o/>/>

I called it test and adjusted it to fit the program (I think?)

get the error:

test:3: bad argument: double excpected, got nil

here's the program.
Spoiler

function sleepWithPercentOutput(n)
local startTime = os.clock() -- get the tick we start in
local timer = os.startTimer(n) -- start the timer we'll use for determining when to exit
local cursorx, cursory = term.getCursorPos()
for sEvent, t in os.pullEvent() do
  term.setCursorPos(cursorx, cursory) -- reset the cursor position
  term.write("	") -- clearing the space
  if sEvent == "timer" and t == timer then
   break
  end
  local percent = tostring(os.clock() - startTime / n)
  percent = #percent == 3 and percent or (#percent == 2 and ("0" .. percent) or "00" .. percent) -- make it 3 characters long the weird way
  term.write(percent .. "%")
end
end

local function pass()

local A = "1"
local B = "2"
local S = "Pump for how many seconds?"

  term.clear()
  term.setCursorPos(1, 1)
  print("Tank System 1.0n")
  sleep(1)
  print("Options:n")
  sleep(1)
  print("1. Pump from Tank1 to BaseTank")
  print("2. Pimp from BaseTank to Tank1n")
  write("Press 1 or 2 to select: ")
	input = read()

  if input == A then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	t = io.read()
	sleep(1)
	term.clear()
	term.setCursorPos(1, 1)
	rs.setOutput("right", true)
	sleepWithPercentOutput()
	rs.setOutput("right", false)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping complete!")
	sleep(3)
  end
--line 37
  if input == B then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	sleep(1)
	t = io.read()
	term.clear()
	term.setCursorPos(1, 1)
	rs.setOutput("back", true)
	sleepWithPercentOutput()
	rs.setOutput("back", false)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping Complete!")
	sleep(3)
  else
	term.clear()
	term.setCursorPos(1, 1)
	print("Invalid Selection!")
  end
end
while true do
	pass()
end



Do you see the error? couse I read trough the function you made, Don't grasp it completly but understand how it works, copied it right into my program.



edit: aha, a derp. :o/>/> no wonder it didn't work. :)/>/> thought i had messed up a copy and paste code. XD
Advert #12
Posted 30 March 2012 - 10:35 PM
the pumps go for about 30 seconds to 2 minutes?



Ok i tested putting this into my original tank program to se how it works, it dosn't. :o/>/>

I called it test and adjusted it to fit the program (I think?)

get the error:

test:3: bad argument: double excpected, got nil

here's the program.
Spoiler

function sleepWithPercentOutput(n)
local startTime = os.clock() -- get the tick we start in
local timer = os.startTimer(n) -- start the timer we'll use for determining when to exit
local cursorx, cursory = term.getCursorPos()
for sEvent, t in os.pullEvent() do
  term.setCursorPos(cursorx, cursory) -- reset the cursor position
  term.write("	") -- clearing the space
  if sEvent == "timer" and t == timer then
   break
  end
  local percent = tostring(os.clock() - startTime / n)
  percent = #percent == 3 and percent or (#percent == 2 and ("0" .. percent) or "00" .. percent) -- make it 3 characters long the weird way
  term.write(percent .. "%")
end
end

local function pass()

local A = "1"
local B = "2"
local S = "Pump for how many seconds?"

  term.clear()
  term.setCursorPos(1, 1)
  print("Tank System 1.0n")
  sleep(1)
  print("Options:n")
  sleep(1)
  print("1. Pump from Tank1 to BaseTank")
  print("2. Pimp from BaseTank to Tank1n")
  write("Press 1 or 2 to select: ")
	input = read()

  if input == A then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	t = io.read()
	sleep(1)
	term.clear()
	term.setCursorPos(1, 1)
	rs.setOutput("right", true)
	sleepWithPercentOutput()
	rs.setOutput("right", false)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping complete!")
	sleep(3)
  end
--line 37
  if input == B then
	term.clear()
	term.setCursorPos(1, 1)
	print(S)
	sleep(1)
	t = io.read()
	term.clear()
	term.setCursorPos(1, 1)
	rs.setOutput("back", true)
	sleepWithPercentOutput()
	rs.setOutput("back", false)
	term.clear()
	term.setCursorPos(1, 1)
	print("Pumping Complete!")
	sleep(3)
  else
	term.clear()
	term.setCursorPos(1, 1)
	print("Invalid Selection!")
  end
end
while true do
	pass()
end



Do you see the error? couse I read trough the function you made, Don't grasp it completly but understand how it works, copied it right into my program.



edit: aha, a derp. :o/>/> no wonder it didn't work. :)/>/> thought i had messed up a copy and paste code. XD

Indeed, a major one. I've fixed it now, though :)/>/>
Kustus #13
Posted 30 March 2012 - 10:41 PM
Ok, MY GOD you are awesome!

Not going to hassle you, but the timing is off. a input of 10 seconds took 21 seconds to complete.

and input of 20 seconds took 21 seconds to complete,

So i thought it was just to much code to keep up. BUT

input of 30 seconds also took 21 seconds. :o/>/>

but it looked AWESOME!

edit: me so stupid… I'm guessing the line
sleepWithPercent(20)

means 20 seconds.. so the t variable should be instead of the numbers? right?



second edit: nope, stil takes 21 seconds to complete, no matter what I type. :o/>/>
Kustus #14
Posted 30 March 2012 - 10:47 PM
feels like I'm standing in front of a beautifull nude lady just waiting for me, but I keep bumping into an invisible wall.
know where u want to go and what to do, just pondering howto. :o/>/>


Edit: Ok I tried the code for 2 minutes, and its pretty close to accurate,

but below a certain point it wont go, trying to find the threshold
, BUT IT WORKS!

a sincere thank you from norway!

it was the sleepWithPercent(20) that fooled me to long. asumption is the mother of all f-ups.

works like a charm.

adding you to the credits list at the end of the movie about me when I become famous. :o/>/>
Advert #15
Posted 30 March 2012 - 10:55 PM
Hmm, it's dependant on minecraft time, so the timing might be off a bit (this runs assuming 20 ticks per second – the server may 'skip' some, but I'm not sure how that works, exactly.)

I'll test some.

Another thought: Could you paste the code you're using?

Edit: I've timed it twice, once at 20 seconds, and another at 2, and I got 20.04 seconds on the first (using a stopwatch), 1.94x on the second, so it seems to be working for me. Judging from your example, I think you're forgetting to pass the number of seconds to sleep to the function (you use it just like sleep(), but it'll print a 4-letter long string with the %age done)
Advert #16
Posted 30 March 2012 - 11:06 PM
feels like I'm standing in front of a beautifull nude lady just waiting for me, but I keep bumping into an invisible wall.
know where u want to go and what to do, just pondering howto. :o/>/>


Edit: Ok I tried the code for 2 minutes, and its pretty close to accurate,

but below a certain point it wont go, trying to find the threshold
, BUT IT WORKS!

a sincere thank you from norway!

it was the sleepWithPercent(20) that fooled me to long. asumption is the mother of all f-ups.

works like a charm.

adding you to the credits list at the end of the movie about me when I become famous. :o/>/>

Happens to the best of us :)/>/>

No problem at all; glad I could help :)/>/>
Kustus #17
Posted 30 March 2012 - 11:08 PM
Coming right up, just gotta get in the last few bits and pieces. :o/>/>

OK, I went over the code.
The main menu works, but the second menu I made comes up, but when I select a tank it goes back to the main menu. Can you se the error? (kinda like where's Waldo?"

but this is how its looking so far.
Spoiler

-- Big round of applause to Advert at Computercraft Forums for pretty mutch making everything in here...  :o/>/>



function sleepWithPercent(n)
 local startTime = os.clock() -- Get the starting time
 local timer = os.startTimer(n) -- Create a timer so we know when to stop sleeping
 local cursorx, cursory = term.getCursorPos() -- Save the cursor position to avoid spamming the terminal
 local tick = os.startTimer(0) -- Create another timer so we can update the % shown on the terminal
 for sEvent, t in os.pullEvent do -- Loop, with the results of os.pullEvent as sEvent and t
  if sEvent == "timer" and t == timer then -- Check for the exit timer
   term.setCursorPos(cursorx, cursory) -- Clean the screen
   term.write("    ")
   term.setCursorPos(cursorx, cursory)
   break -- Break out of the loop
  elseif sEvent == "timer" and t == tick then -- Check for the update timer
   term.setCursorPos(cursorx, cursory)
   tick = os.startTimer(0.1) -- Re-start the timer since timers only run once
   local p = tostring(math.floor(((os.clock() - startTime) / n) * 100)) -- Calculate the %age
   p = #p == 3 and p or (#p == 2 and ("0" .. p) or "00" .. p) -- Pad with 0s infront if it's not long enough
   term.write(p .. "%")
  end
 end
end

-- clear screen function
local function clear()
    term.setCursorPos(1,1)
    term.clear()
end
	    local tChoices = {}
    tChoices[0] = 'Pump from Main Oil Tank'
    tChoices[1] = 'Pump to Main Oil Tank'
    tChoices[2] = 'Pump to a remote remote location'
    tChoices[3] = 'Pump to refinery'
    tChoices[4] = 'Pump from Buffer Tank to Main Oil Tank'
     
     
    table.insert(tChoices, 'Shutdown') -- If you add more stuff, the shutdown will always come at the bottom, without you having to change its #.
    local nTermX, nTermY = term.getSize()
    local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal
     
    local tActions = {} -- Twin table of tChoices, we'll put functions in here to do stuff. !!!(still dont know what to do here?)!!!
     
     
     
    tActions[0] = function(MpT) -- mpt = Main pump Tank
		    clear()
		    print(sSeperator)
		    print("|					  Pumping from Main Oil Tank				  |")
		    print(sSeperator)
		    print("Tank menu:")
		    print("1. Oil Tank 1")
		    print("2. Oil Tank 2")
		    print("3. Oil Tank 3")
		    print("4. Oil Tank 4")
		    term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
		    write("Select a Oil Tank to pump to: ")
			  select = read()
		    
		    if select == 1 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 1 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping to Oil Tank 1 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.blue)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		     
			 if select == 2 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 2 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping to Oil Tank 2 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.lightblue)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		    
		    if select == 3 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 3 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping to Oil Tank 3 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.purple)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		    if select == 4 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 4 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping to Oil Tank 4 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.cyan)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			  else
			    clear()
			    print("!!! Invalid Selection !!!")
			 end    
    end
     
tActions[1] = function(TpM) -- TpM = Tank Pump Main
		    clear()
		    print(sSeperator)
		    print("|					  Pumping to Main Oil Tank				  |")
		    print(sSeperator)
		    print("Tank menu:")
		    print("1. Oil Tank 1")
		    print("2. Oil Tank 2")
		    print("3. Oil Tank 3")
		    print("4. Oil Tank 4")
		    term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
		    write("Select a Oil Tank to pump from: ")
			  select = read()
		    
		    if select == 1 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 1 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping from Oil Tank 1 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.white)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		     
			 if select == 2 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 2 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping from Oil Tank 2 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.lightgrey)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		    
		    if select == 3 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 3 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping from Oil Tank 3 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.grey)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			 end
		    if select == 4 then
			    clear()
			    print(sSeperator)
			    print "|							  Oil Tank 4 selected						   |"
			    print(sSeperator)
			    write("How many seconds do you want to pump?: ")
				  t = io.read()
			    sleep(1)
			    clear()
			    print("Pumping from Oil Tank 4 for "..t.." Seconds...")
			    rs.setBundledOutput("top", colors.black)
			    term.write("Progress: ")
			    sleepWithPercent(t)
			    rs.setBundledoutput("top", 0)
			    clear()
			    print("Pumping Complete!")
			  else
			    clear()
			    print("!!! Invalid Selection !!!")
			 end    
    end
     
    tActions[2] = function(MpR) -- Main pump Remote
		    term.clear()
		    term.setCursorPos(1, 1)
		    print("Function disabled untill I learn Lua")
		    term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
		    write("    [Press enter to return to main menu]")
		    read()
    end
     
    tActions[3] = function(MpRef) -- Main pump Refinery
		    term.clear()
		    term.setCursorPos(1, 1)
		    print("Function disabled untill I learn Lua")
		    term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
		    write("    [Press enter to return to main menu]")
		    read()
    end
    
    tActions[4] = function(BpM) -- Buffer pump Main
		    term.clear()
		    term.setCursorPos(1, 1)
		    print("Function disabled untill I learn Lua")
     
		    term.setCursorPos(1, nTermY) -- Move the cursor to the bottom
		    write("    [Press enter to return to main menu]")
		    read()
    end     
    table.insert(tActions, os.shutdown) -- Insert the shutdown function at the end to compliment the "Shutdown" menu item :)/>/>
     
    -- Do the above for the remaining
     
    local nSelection = 0 -- The current selection defaults at 0
    repeat
		    term.setCursorPos(1, 1)
		    term.clear()
		    print(sSeperator)
		    print("|				   Oil Pump System Beta 1.0			   |")
		    print(sSeperator)
     
		    for nLine = 0, #tChoices do -- Iterate through the possible options, and print them, marking the chosen one
				    local sLine = " "
				    if nSelection == nLine then
						    sLine = ">"
				    end
				    local sLineNum = tostring(nLine)
				    if #sLineNum < 2 then
						    sLineNum = "0" .. sLineNum -- Prepend a 0 if it's too short
				    end
				    sLine = sLine .. "[" .. sLineNum .. "]" .. " " .. tChoices[nLine] -- Construct the string we're printing
				    print(sLine) -- Print it
		    end
		    -- os.pullEvent keys: up - 200, down - 208, enter - 28
		    local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass
     
		    if nKey == 200 or nKey == 17 then -- Up/w key: move up the menu
				    if tChoices[nSelection - 1] then -- Check if we can move up
						    nSelection = nSelection - 1
				    end
				    -- Ignore it otherwise
		    elseif nKey == 208 or nKey == 31  then -- Down/s key: move down the menu
				    if tChoices[nSelection + 1] then -- Check if we can move down
						    nSelection = nSelection + 1
				    end
		    elseif nKey == 28 then -- Enter key: Selecting a choice
				    if tActions[nSelection] then
						    tActions[nSelection]() -- Run the function associated with the action.
				    else
						    print("Error: Selection out of bounds: ", nSelection)
						    read() -- This error is recoverable.
				    end
		    end
    until false -- Run this loop forever :)/>/>

Advert #18
Posted 31 March 2012 - 12:20 AM
You could convert this menu into a function, that'd make adding sub-menus very easy.

I think I'll have a go at it :o/>/>
Kustus #19
Posted 31 March 2012 - 12:28 AM
nice! if u ever need a kidney, let me know. U just saved my easter holliday. been trying to figure this out for 4 days now. 2 allnighters with 8 hours of work each day.

just saying, looking forward to the result. But I'm hitting the sack now.
Advert #20
Posted 31 March 2012 - 01:56 AM
I have something coming along nicely, but I'm starting to create utility functions, as usual :o/>/>

Here's what I have so far: http://pastebin.com/MsAEDu6C

I think It's working now, I'll make a post with some examples once I'm awake again :o/>/>
Edited by
Evis #21
Posted 31 March 2012 - 07:05 AM
I have something coming along nicely, but I'm starting to create utility functions, as usual :o/>/>

Here's what I have so far: http://pastebin.com/MsAEDu6C

I think It's working now, I'll make a post with some examples once I'm awake again :o/>/>

Just tried this out. It's pretty slick. Going to tinker tomorrow.
Advert #22
Posted 31 March 2012 - 02:11 PM
I have something coming along nicely, but I'm starting to create utility functions, as usual :o/>/>

Here's what I have so far: http://pastebin.com/MsAEDu6C

I think It's working now, I'll make a post with some examples once I'm awake again :o/>/>

Just tried this out. It's pretty slick. Going to tinker tomorrow.

Glad you liked it; I'll make a post in the API section with it when I feel content :)/>/>

I've updated it again; same pastebin link, its example is now using Kustus' menu structure.
Espen #23
Posted 31 March 2012 - 02:11 PM
@Advert:
Very clean and well documented code, props for taking the time to polish code.
Very few people bother to do this, so I feel it should be applauded whenever someone does it. :o/>/>
Advert #24
Posted 31 March 2012 - 02:49 PM
@Advert:
Very clean and well documented code, props for taking the time to polish code.
Very few people bother to do this, so I feel it should be applauded whenever someone does it. :o/>/>
It was quite fun coding it :)/>/>

Trying to get into the habit of documenting and commenting better; I can get some code from a few years ago and you will literally shriek in horror when you see the code :o/>/>.
Kustus #25
Posted 31 March 2012 - 03:34 PM
clearly I need to learn lua bether…

Could not for the sake of my life learn understand how he did that.
to many functions for my brain to comprehend.

but it works like a charm and looks awesome.

But I made the program myself. Its a shitty code and not efficient at all but it works like I want it to.

going to continue to try to use adverts code and tune it the way I want it. but untill its stable enough to run I gotta use this one.

Spoiler


-- sleep with percent function
function sleepWithPercent(n)
local startTime = os.clock() -- Get the starting time
local timer = os.startTimer(n) -- Create a timer so we know when to stop sleeping
local cursorx, cursory = term.getCursorPos() -- Save the cursor position to avoid spamming the terminal
local tick = os.startTimer(0) -- Create another timer so we can update the % shown on the terminal
for sEvent, t in os.pullEvent do -- Loop, with the results of os.pullEvent as sEvent and t
  if sEvent == "timer" and t == timer then -- Check for the exit timer
   term.setCursorPos(cursorx, cursory) -- Clean the screen
   term.write("	")
   term.setCursorPos(cursorx, cursory)
   break -- Break out of the loop
  elseif sEvent == "timer" and t == tick then -- Check for the update timer
   term.setCursorPos(cursorx, cursory)
   tick = os.startTimer(0.1) -- Re-start the timer since timers only run once
   local p = tostring(math.floor(((os.clock() - startTime) / n) * 100)) -- Calculate the %age
   p = #p == 3 and p or (#p == 2 and ("0" .. p) or "00" .. p) -- Pad with 0s infront if it's not long enough
   term.write(p .. "%")
  end
end
end

-- clear screen function
local function clear()
	term.setCursorPos(1,1)
	term.clear()
end

local nTermX, nTermY = term.getSize()
local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal

local function logo()
   clear()
	print(sSeperator)
	print("	Oil Controll 0.1 Beta")
	print(sSeperator)
end

local function tankList()
	print("1. Oil Tank 1")
	print("2. Oil Tank 2")
	print("3. Oil Tank 3")
	print("4. Oil Tank 4n")
end

local function menu()
	clear()
	logo()
	print("		 Main Menun")
	print("	 1. Pump from Main Oil Tank")
	print("	 2. Pump to Main Oil Tank")
	print("	 3. Pump to a Remote Tank")
	print("	 4. Pump from Buffer Tank")
	print("	 5. Start Fuel Refinery")
	print("	 6. Shutdown Systemn")
	write("	 Select an option: ")
		select1 = io.read()

	if select1 == "1" then  
		logo()
		print("	 Pumping from Main Oil Tank n")
		tankList()
		write("	 Select which tank to pump to: ")
		select2 = read()

		if select2 == "1" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 1 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.blue)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "2" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 2 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.lightBlue)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "3" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 3 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.purple)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "4" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 4 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.cyan)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		 end
	  end

	if select1 == "2" then
		logo()
		print("	 Pumping to Main Oil Tank n")
		tankList()
		write("	 Select which tank to pump from: ")
		select2 = read()

		if select2 == "1" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 1 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.white)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "2" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 2 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.lightGray)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "3" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 3 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.gray)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "4" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 4 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.black)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
	  end

	if select1 == "3" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "4" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "5" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "6" then
		logo()
		print("	 Shutting down system!")
		textutils.slowWrite("---------------n")
		print("")
		print(" Goodbye")
		sleep(2)
		os.shutdown()
	  end
  end
while true do
	menu()
end


but thanks for the program Advert! Amazing job and thank you for the close followup on the newbie!

couldn't have done it without you. :o/>/>
Advert #26
Posted 31 March 2012 - 03:43 PM
clearly I need to learn lua bether…

Could not for the sake of my life learn understand how he did that.
to many functions for my brain to comprehend.

but it works like a charm and looks awesome.

But I made the program myself. Its a shitty code and not efficient at all but it works like I want it to.

going to continue to try to use adverts code and tune it the way I want it. but untill its stable enough to run I gotta use this one.

Spoiler


-- sleep with percent function
function sleepWithPercent(n)
local startTime = os.clock() -- Get the starting time
local timer = os.startTimer(n) -- Create a timer so we know when to stop sleeping
local cursorx, cursory = term.getCursorPos() -- Save the cursor position to avoid spamming the terminal
local tick = os.startTimer(0) -- Create another timer so we can update the % shown on the terminal
for sEvent, t in os.pullEvent do -- Loop, with the results of os.pullEvent as sEvent and t
  if sEvent == "timer" and t == timer then -- Check for the exit timer
   term.setCursorPos(cursorx, cursory) -- Clean the screen
   term.write("	")
   term.setCursorPos(cursorx, cursory)
   break -- Break out of the loop
  elseif sEvent == "timer" and t == tick then -- Check for the update timer
   term.setCursorPos(cursorx, cursory)
   tick = os.startTimer(0.1) -- Re-start the timer since timers only run once
   local p = tostring(math.floor(((os.clock() - startTime) / n) * 100)) -- Calculate the %age
   p = #p == 3 and p or (#p == 2 and ("0" .. p) or "00" .. p) -- Pad with 0s infront if it's not long enough
   term.write(p .. "%")
  end
end
end

-- clear screen function
local function clear()
	term.setCursorPos(1,1)
	term.clear()
end

local nTermX, nTermY = term.getSize()
local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal

local function logo()
   clear()
	print(sSeperator)
	print("	Oil Controll 0.1 Beta")
	print(sSeperator)
end

local function tankList()
	print("1. Oil Tank 1")
	print("2. Oil Tank 2")
	print("3. Oil Tank 3")
	print("4. Oil Tank 4n")
end

local function menu()
	clear()
	logo()
	print("		 Main Menun")
	print("	 1. Pump from Main Oil Tank")
	print("	 2. Pump to Main Oil Tank")
	print("	 3. Pump to a Remote Tank")
	print("	 4. Pump from Buffer Tank")
	print("	 5. Start Fuel Refinery")
	print("	 6. Shutdown Systemn")
	write("	 Select an option: ")
		select1 = io.read()

	if select1 == "1" then  
		logo()
		print("	 Pumping from Main Oil Tank n")
		tankList()
		write("	 Select which tank to pump to: ")
		select2 = read()

		if select2 == "1" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 1 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.blue)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "2" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 2 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.lightBlue)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "3" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 3 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.purple)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "4" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping to Oil Tank 4 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.cyan)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		 end
	  end

	if select1 == "2" then
		logo()
		print("	 Pumping to Main Oil Tank n")
		tankList()
		write("	 Select which tank to pump from: ")
		select2 = read()

		if select2 == "1" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 1 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.white)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "2" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 2 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.lightGray)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "3" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 3 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.gray)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
		if select2 == "4" then
			logo()
			write("Pump for how many seconds: ")
			t = io.read()

			sleep(1)
			logo()
			print("Pumping from Oil Tank 4 for "..t.." Seconds...")
			rs.setBundledOutput("top", colors.black)
			term.write("Progress: ")
			sleepWithPercent(t)
			rs.setBundledOutput("top", 0)
			clear()
			print("Pumping Complete!")
			sleep(3)
		  end
	  end

	if select1 == "3" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "4" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "5" then
		logo()
		print(" Selection disabled ")
		sleep(3)
	  end
	if select1 == "6" then
		logo()
		print("	 Shutting down system!")
		textutils.slowWrite("---------------n")
		print("")
		print(" Goodbye")
		sleep(2)
		os.shutdown()
	  end
  end
while true do
	menu()
end


but thanks for the program Advert! Amazing job and thank you for the close followup on the newbie!

couldn't have done it without you. :o/>/>

My pleasure.

If you have any other questions, or want have any questions about my code, feel free to ask :o/>/>
Kustus #27
Posted 31 March 2012 - 04:06 PM
that I will do, i guarantee it. :o/>/>

but now the server is running with the program I made. Now I can enjoy your code and fiddle with it untill I understand it. :)/>/>

while the black gold is pumping trough my pipes. :o/>/>
Kustus #28
Posted 01 April 2012 - 10:43 AM
ok, quick question. is there a way to show this program on the screen while using it on the monitor? couldn't find any scripts that worked or a tutorial showing how to do it.

You've seen the code, Is it possible?

something tels me I need to send everything to another computer which is connected to a monitor. or is there a simple way of just dooping the screen onto the monitor?
Advert #29
Posted 01 April 2012 - 10:53 AM
Espen made a nice API for that: http://www.computercraft.info/forums2/index.php?/topic/955-synchronized-terminal-redirection-poc/