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

Missile Launch System

Started by hbomb79, 16 June 2014 - 06:12 AM
hbomb79 #1
Posted 16 June 2014 - 08:12 AM
Hi there, in my code i have a for loop counting down from sixty and i would like the ability to type a password to stop it, how would i do this?

here is the part of the code:

for y = 1, 60 do
    clear()
   x = x - 1
  
   if x < 61 and x > 30 then
	 term.setTextColor(colors.lime)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif  x < 30 and x > 10 then
	 term.setTextColor(colors.orange)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif x < 10 and x > 1 then
   rs.setOutput("right", true)
	 term.setTextColor(colors.red)
	 print ("Missile Silo ONE Launch In T-"..x.."")
   elseif x == 0 then
	  rs.setOutput("back", true)
	 term.setTextColor(colors.red)
	 print "Launching Missile!"
	 sleep(10)
	    rs.setOutput("top",false)
		   rs.setOutput("bottom",false)
		    rs.setOutput("back",false)
		   rs.setOutput("right",false)
		   rs.setOutput("left",false)
   end
   sleep(1)
   clear()
 
  end
Bomb Bloke #2
Posted 16 June 2014 - 09:55 AM
This is where you want the parallel API.

Rig one function to perform the countdown, and the other to take in the password. Every time the countdown function sleeps, it needs to put the cursor back to where it was when it last finished sleeping.
hbomb79 #3
Posted 16 June 2014 - 10:01 AM
I havent used this before, could you give me an example, im a beginner at this…
Bomb Bloke #4
Posted 16 June 2014 - 10:26 AM
I've not tested it, but something along these lines should work.

local password = "panda"
local explode = true

local function counter()
	for x = 100, 1, -1 do
		local oldX, oldY = term.getCursorPos()
		
		term.setCursorPos(1,1)
		term.clearLine()
		term.write(x)
		
		term.setCursorPos(oldX, oldY)
		sleep(1)
	end
end

local function getPassword()
	while explode do
		term.setCursorPos(1,2)
		term.clearLine()
		term.write("Enter the shutdown password: ")
		
		local input = read("*")
		explode = not (input == password)
	end
end

parallel.waitForAny(counter, getPassword)

if explode then detonate() end
hbomb79 #5
Posted 17 June 2014 - 12:33 AM
Why must it go back to where it was and then to 1,1? I don't understand why it can't just go to 1,1 each time? Maybe it's obvious, I'm not sure tho
Bomb Bloke #6
Posted 17 June 2014 - 02:13 AM
The idea is that control switches between the functions running in "parallel" whenever they yield. A yield is triggered on most any action that causes a pause - like when you wait for the user to type something (eg using read()), or when you sleep.

One function is sitting there waiting for you to type a password. When you press a key, you expect the result to show up after the last character you typed. Each press moves the cursor along to the right.

But what if, between keypresses, the cursor gets moved off to 1,1 in order to print the countdown value? Your next keypress will appear next to that, unless you take care to put the cursor back where it was taken from before passing control back to the input reading function!
hbomb79 #7
Posted 17 June 2014 - 03:35 AM
I think I get it know, thank you very much