SHOW:
|
|
- or go back to the newest paste.
| 1 | - | -- A very basic 'hello, steve' program. |
| 1 | + | local mon = peripheral.find("monitor")
|
| 2 | local speaker = peripheral.wrap("left")
| |
| 3 | - | local sFile, sName = "histeve.txt", "" -- Our data file's name, and our user's name. |
| 3 | + | local sFile = "data.txt" |
| 4 | - | -- We won't worry about finding the right directory to save our data in; we'll just use / |
| 4 | + | local accent,range = "en",100 |
| 5 | ||
| 6 | - | if fs.exists(sFile) then -- Open the file if it exists, and get the user's name from it. |
| 6 | + | function writemon(msg) |
| 7 | - | local hRead = assert(fs.open(sFile, "r")) -- assert will error for us if we can't open the file. |
| 7 | + | if mon then |
| 8 | - | -- the 'h' in the varaible name stands for 'handle', and 'read' is to remind me that I can only read from that handle. |
| 8 | + | mon.write(msg) |
| 9 | - | sName = hRead.readLine() -- Read a line of data from the handle |
| 9 | + | mX,mY = mon.getCursorPos() |
| 10 | - | hRead.close() -- Close the handle! This is extremely important, as not doing so can cause errors when writin/opening the file. |
| 10 | + | mon.setCursorPos(1,mY+1) |
| 11 | end | |
| 12 | end | |
| 13 | - | if sName == "" then -- If the user's name doesn't exist, ask them for it. |
| 13 | + | |
| 14 | - | print("Hello there, what's your name?")
|
| 14 | + | function writeln(msg) |
| 15 | - | local sInput = read() |
| 15 | + | print(msg) |
| 16 | - | if sInput ~= "" then |
| 16 | + | tX,tY = term.getCursorPos() |
| 17 | - | print("Hi, " .. sInput .. "!")
|
| 17 | + | writemon(msg) |
| 18 | - | sName = sInput |
| 18 | + | |
| 19 | - | -- Now, let's save the name. |
| 19 | + | |
| 20 | - | hWrite = fs.open(sFile, "w") -- w for write! Note that this will also clear the file. |
| 20 | + | function clear() |
| 21 | - | -- Note that if the above errors, you'll get an error message somewhere else. Use the example below to catch the error. |
| 21 | + | term.clear() |
| 22 | - | hWrite.write(sName) -- Write the user's name |
| 22 | + | term.setCursorPos(1,1) |
| 23 | - | hWrite.close() -- Close it; still very important. |
| 23 | + | if mon then |
| 24 | mon.clear() | |
| 25 | - | print("I'm currently calculating Pi to 1" .. string.rep("0", 20) .. "digits.") -- Banter. :)
|
| 25 | + | mon.setCursorPos(1,1) |
| 26 | - | print("I'll see you again soon, " .. sName .. "!")
|
| 26 | + | end |
| 27 | - | return |
| 27 | + | |
| 28 | - | else |
| 28 | + | |
| 29 | - | print("I'm sorry, I'm not allowed to speak to strangers, stranger.")
|
| 29 | + | function getUsername() |
| 30 | - | return -- exit :D |
| 30 | + | if fs.exists(sFile) then |
| 31 | - | end |
| 31 | + | local hRead = assert(fs.open(sFile, "r")) |
| 32 | username = hRead.readAll() | |
| 33 | - | -- We don't need to use an else here, since we're exiting in both branches of the above if statement. |
| 33 | + | hRead.close() |
| 34 | - | print("Hi again, " .. sName .. "!\nHow are you doing today?") -- \n is a newline.
|
| 34 | + | end |
| 35 | - | read() -- Discard the input, this is just an example, after all :) |
| 35 | + | |
| 36 | - | print("Damnit! You made me garbagecollect all that work on my Pi digits!") |
| 36 | + | |
| 37 | rednet.open("back")
| |
| 38 | username = "Steve" | |
| 39 | ||
| 40 | function help() | |
| 41 | writeln("Welcome to some shit chat")
| |
| 42 | writeln("Press H to set your Handle")
| |
| 43 | writeln("Press A to change the Accent")
| |
| 44 | writeln("Press T to Type in the chat")
| |
| 45 | writeln("Press E to Exit the program")
| |
| 46 | writeln("Press R to adjust the Range")
| |
| 47 | end | |
| 48 | ||
| 49 | clear() | |
| 50 | getUsername() | |
| 51 | help() | |
| 52 | ||
| 53 | while true do | |
| 54 | event, id, msg = os.pullEvent() | |
| 55 | ||
| 56 | if event == "rednet_message" then | |
| 57 | writeln(msg) | |
| 58 | spk = msg:gsub('%<[^)]*%>', "")
| |
| 59 | speaker.speak(spk, range, accent) | |
| 60 | ||
| 61 | elseif event == "char" then | |
| 62 | if id == "t" then | |
| 63 | write("<"..username.."> ")
| |
| 64 | msgsent = read() | |
| 65 | speaker.speak(msgsent, range, accent) | |
| 66 | writemon("<"..username.."> "..msgsent)
| |
| 67 | rednet.broadcast("<"..username.."> "..msgsent)
| |
| 68 | ||
| 69 | elseif id == "h" then | |
| 70 | write("New username: ")
| |
| 71 | username = read() | |
| 72 | hWrite = fs.open(sFile, "w") | |
| 73 | hWrite.write(username) | |
| 74 | hWrite.close() | |
| 75 | ||
| 76 | elseif id == "a" then | |
| 77 | write("New accent: ")
| |
| 78 | accent = read() | |
| 79 | ||
| 80 | elseif id == "r" then | |
| 81 | write("New range: ")
| |
| 82 | range = read() | |
| 83 | ||
| 84 | elseif id == "e" then | |
| 85 | error("Quit program")
| |
| 86 | ||
| 87 | end | |
| 88 | ||
| 89 | end | |
| 90 | ||
| 91 | end |