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

Help improving

Started by polraudio, 02 March 2012 - 08:07 AM
polraudio #1
Posted 02 March 2012 - 09:07 AM
Im very new to lua but have used other programming languages.

Theres a few things i seen people do but dont really know how to implement it into my system.
Im not sure how to add a little selector.

EX: instead of typing 00 to read the "Terminal Password" i have something like this.

>[00]Terminal Password
[01]Mining Reports
[02]New Base of Operations
[03]Energy Project

I tried reading tutorials but its still a little confusing. If someone could show me how to do it with this system it will help me lots in learning.

If you have any ideas on how to improve this further please tell. This is only ment to be a log thing. Kind of like fallout if anyone played it. Its not ment to be an os in any way even knowing it starts up wht the computer.

term.clear()
print "-------------------------------------------------"
print "|		   Polraudio's Personal Logs		   |"
print "-------------------------------------------------"
print"[00]Terminal Password"
print"[01]Mining Reports"
print"[02]New Base of Operations"
print"[03]Energy Project"
print "-------------------------------------------------"
write "Section: "
local status, pass = pcall(read)
if pass == "00" then
	term.clear()
	print "-------------------------------------------------"
	print "|			  Terminal Password				|"
	print "-------------------------------------------------"
	print "Information Deleted By User"
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print "	   [Press enter to return to main menu]"
	local pass = io.read()
	if pass == "" then
		term.clear()
		os.reboot()
	end
end

if pass == "01" then
	term.clear()
	print "-------------------------------------------------"
	print "|				Mining Reports				 |"
	print "-------------------------------------------------"
	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."
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print "	   [Press enter to return to main menu]"
	local pass = io.read()
  
	if pass == "" then
		term.clear()
		os.reboot()
	end
end

if pass == "02" then
	term.clear()
	print "-------------------------------------------------"
	print "|			New Base of Operations			 |"
	print "-------------------------------------------------"
	print "I have moved out of my home into a new base in"
	print "the mountain up north. I will be studying new"
	print "ways to produce energy to power my technology."
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print ""
	print "	   [Press enter to return to main menu]"
	local pass = io.read()
  
	if pass == "" then
		term.clear()
		os.reboot()
	end
end

if pass == "03" then
	term.clear()
	print "-------------------------------------------------"
	print "|				Energy Project				 |"
	print "-------------------------------------------------"
	print "I have discovered a new type of energy that has"
	print "the power output to power 10 cities all at once."
	print ""
	print "I ran into one problem once i discovered this new"
	print "type of energy. I couldnt find a way to get it"
	print "stable enough to be safe. I had to flee the area."
	print "After i left the area my base exploded and made"
	print "the mountain into...well....lets just say its not"
	print "a mountain anymore....."
	print ""
	print ""
	print ""
	print ""
	print "	   [Press enter to return to main menu]"
	local pass = io.read()
  
	if pass == "" then
		term.clear()
		os.reboot()
	end
end

if pass == "shutdown" then
	os.shutdown()
else
	os.reboot()
end

Thanks in advance :unsure:/>/>

EDIT: some reason it messed up my spaces so i put it on pastebin so the spaces work.
http://pastebin.com/zTc9Rsda
Advert #2
Posted 02 March 2012 - 10:15 AM
I rewrote it; this is not the best example (and has a few quirks), but I've tried to make it easy to read: http://pastebin.com/fnmr395x

I'd recommend using more functions, and looping instead of rebooting!

Note that the example is NOT ctrl-t safe; you'll have to use os.pullEventRaw for that, and pcall all the read()s.

Post away if you have any questions :unsure:/>/>

Also, my suggestions: Log in function (Guest, you), Use files instead of hardcoding the journal and entries (you can use the built-in editor program, and shell.run it, though you will probably have to touch file handles, and some fs functions.)

Feel free to do whatever with the code I pasted, though I think I'll modify it sometime later, since I think your idea for this was pretty cool B)/>/>
polraudio #3
Posted 02 March 2012 - 07:58 PM
OK! Now i see how to do this. Still kind of confusing but i will get it by playing around with the commands and values.
I never knew there was a print(sSeperator) to put a line across the screen.

This is only going to be used in the Technic pack so ctrl-t doesnt work on terminals(from what i tried).
No need for the password on these standard terminals.

Im going to make an adventure map using computercraft and all the goodies that come with technic pack. As of now i cant use 1.3

The adventure is going to be to find his new base/factory and hack your way in(with password clues tho).

Thank you very much on helping me with this. The comments you left are helping lots. :unsure:/>/>

Who knows i may use this to make a fallout type hacking game.
Advert #4
Posted 02 March 2012 - 08:03 PM
I defined the sSeperator myself; it's at line 10:


local nTermX, nTermY = term.getSize() -- Line 9: We get the size of the terminal
local sSeperator = ("-"):rep(nTermX) -- Create a seperator string with the size of the terminal
-- This is equivalent to: local sSeperator = string.rep(" ", nTermX)

I'm quite interested to see what you come up with, make sure to make a post when you're done with this!
polraudio #5
Posted 02 March 2012 - 08:10 PM
I see how you did it. Very clever.

Will do :unsure:/>/>
Advert #6
Posted 02 March 2012 - 08:18 PM
On another note, since you're not using 1.3:

After this line:

        local sEvent, nKey = os.pullEvent("key") -- Using the 1.3 filtering; this will mean only "key" events will pass

You'll want to put everything down there in an if:

...

if sEvent == "key" then
 ... -- Put the if statements checking nKey in here
end
-- Below already exists; just here to show where to end the statement
until false 

This is because the os.pullEvent() function changed in 1.3, and you can filter events with it.