Posted 21 March 2012 - 12:50 AM
Origins
Hey guys! I'm brand new to coding of any sort, but I knew when I saw ComputerCraft that I just -had- to learn!
So, I installed RedWorks and ran through Casper's in-game Tutorial program (which is just great, btw!) and immediately went on to try and code some stuff myself.
Here's one of those attempts, certainly the most polished of the lot. I call it "TarnChat" (My name's Tarnia, Tarn's my nickname xD), and it's a rednet-based instant messaging program. It's sort of like a location-limited IRC client in-game.
About
I'm an admin on a fairly well-established public server, which is about to undertake a huge move- we're changing the server to a Tekkit server, and making it the best we possibly could. One of the things that we thought would be cool would be to use ComputerCraft to have a city-based chat service within the spawn city and other major settlements. Even though I had no experience, I took it on myself, and here's the result- TarnChat!
TarnChat uses wireless modems and rednet to send messages to other computers with a user-friendly interface. It has several built in commands, which are as follows:
/help - lists programs
/name <name> - changes username
/list - lists connected users, and console IDs
/me - used as emote
/clear - clears the screen
/msg <ID> <message> - sends a private message [You can also use /m]
/exit - exits the program [You can also use /quit]
What Next?
Well…to be honest…I have no idea! I'm really very very bad at looking at something I create and saying "it's done!", so I would -love- ideas for more stuff to add! Something I'm thinking of adding in the next release is a way to stop people from taking the same username. If you have any ideas that you'd like me to add- new commands (functional, or just fun), new features, just let me know and I'll do my best!
Installation
Just copy the code below into a new program (on more than one computer, obviously, duh xD), put a wireless modem on to the side of your computer (left or right) and follow the in-game prompts :)/>/> It should be pretty well self-explanitory, though if you have any problems let me know, and I'll try and sort it out!
I apologise in advance for the messy code, like I said- this is the first thing I've ever coded, really. Also, my indentation and formatting got a little screwed up when I copied it here, but oh well. It should still work! xD
Credits
A big thanks to Liraal for his "yes/no" menu tutorial. I used that for the side selection at the start.
Bigger thanks to kyoshiku for "MC-Chat", elements of which are used in this. I didn't take any code directly, but I definitely used a lot of his methods to get started (since I've never done anything like this before, I didn't really know where to begin!). The things I used which are based on his code are: Getting text input without using "read()" and using string.sub() to recognise user commands.
Thanks to Casper7526 for his "How to Use Modems" tutorial, as well :D/>/>
Also big thanks to Hwaet, one of my fellow admins, for being the bestest ever, and encouraging me! :3
Hey guys! I'm brand new to coding of any sort, but I knew when I saw ComputerCraft that I just -had- to learn!
So, I installed RedWorks and ran through Casper's in-game Tutorial program (which is just great, btw!) and immediately went on to try and code some stuff myself.
Here's one of those attempts, certainly the most polished of the lot. I call it "TarnChat" (My name's Tarnia, Tarn's my nickname xD), and it's a rednet-based instant messaging program. It's sort of like a location-limited IRC client in-game.
About
I'm an admin on a fairly well-established public server, which is about to undertake a huge move- we're changing the server to a Tekkit server, and making it the best we possibly could. One of the things that we thought would be cool would be to use ComputerCraft to have a city-based chat service within the spawn city and other major settlements. Even though I had no experience, I took it on myself, and here's the result- TarnChat!
TarnChat uses wireless modems and rednet to send messages to other computers with a user-friendly interface. It has several built in commands, which are as follows:
/help - lists programs
/name <name> - changes username
/list - lists connected users, and console IDs
/me - used as emote
/clear - clears the screen
/msg <ID> <message> - sends a private message [You can also use /m]
/exit - exits the program [You can also use /quit]
What Next?
Well…to be honest…I have no idea! I'm really very very bad at looking at something I create and saying "it's done!", so I would -love- ideas for more stuff to add! Something I'm thinking of adding in the next release is a way to stop people from taking the same username. If you have any ideas that you'd like me to add- new commands (functional, or just fun), new features, just let me know and I'll do my best!
Installation
Just copy the code below into a new program (on more than one computer, obviously, duh xD), put a wireless modem on to the side of your computer (left or right) and follow the in-game prompts :)/>/> It should be pretty well self-explanitory, though if you have any problems let me know, and I'll try and sort it out!
I apologise in advance for the messy code, like I said- this is the first thing I've ever coded, really. Also, my indentation and formatting got a little screwed up when I copied it here, but oh well. It should still work! xD
Spoiler
term.clear()
term.setCursorPos(1,1)
function LR()
local n=1
while true do
local x, y=term.getCursorPos()
term.clearLine()
if n==1 then write(">LEFT< RIGHT") else write (" LEFT >RIGHT<") end
term.setCursorPos(x, y)
a, b=os.pullEventRaw()
while a~="key" do a, b=os.pullEventRaw() end
if b==203 and n==2 then n=1 end
if b==205 and n==1 then n=2 end
if b==28 then print("") break end
end
if n==1 then return true end
if n==2 then return false end
return false
end
local chatTable = { [1] = "Welcome to TarnChat! (/help for commands)" }
local chatLine = 1
local message = ""
local user = "???"
local compID = os.computerID()
local toID = ""
local msgMessage = ""
local msgSent = ""
function drawLine()
term.setCursorPos( 1, 17 )
term.clearLine()
for n=1,49 do
io.write("=")
end
term.setCursorPos(1,18)
term.clearLine()
write(username)
write(": ")
write(message)
end
function addChat(str)
if chatLine < 16 then
table.insert(chatTable, str)
chatLine = chatLine + 1
else
for n=1,15 do
chatTable[n] = chatTable[n+1]
end
chatTable[16] = str
end
end
function quit()
os.exit()
end
function showChat()
term.setCursorPos( 1, 1 )
for n=1,chatLine do
term.clearLine()
print(chatTable[n])
end
end
print("Is your modem on the Left or the Right?")
term.setCursorPos(1,18)
if LR() == true then
rednet.open("left")
else
rednet.open("right")
end
term.clear()
repeat
term.setCursorPos(1,1)
print("Enter a username for yourself.")
term.setCursorPos(1,18)
user = read()
if string.len(user) > 9 then
term.setCursorPos(1,2)
print("Name too long! 9 character maximum.")
term.setCursorPos(1,18)
elseif string.len(user) > 0 then
write("Your username is ")
write(user)
print("")
print("Entering Chat...")
username = user
end
until string.len(user) ~=0
sleep(2.0)
term.clear()
drawLine()
rednet.broadcast("")
rednet.broadcast(username .. " enters the room!")
repeat
term.setCursorPos(1,1)
write(chatTable[chatLine] or "")
drawLine()
showChat()
term.setCursorPos((3 + string.len(username) + string.len(message)),18)
term.setCursorBlink( true )
event, param = os.pullEventRaw()
if event == "key" then
if param == 28 then
if string.sub(message,1,1) == "/" then
if string.sub(message,1,6) == "/name " then
if string.len(message) > 6 then
if string.len(message) < 16 then
user = username
username = string.sub(message,7,string.len(message))
message = "User '" .. user .. "' is now called '" .. username .. "'!"
rednet.broadcast("")
rednet.broadcast(message)
addChat(message)
message = ""
drawLine()
else
addChat("<SYS> Name too long! Maximum 9 characters.")
message = ""
end
end
elseif string.sub(message,1,4) == "/me " then
local meMessage = string.sub(message,4,string.len(message))
message = "*" .. username .. meMessage
rednet.broadcast("")
rednet.broadcast(message)
addChat(message)
message = ""
drawLine()
elseif string.sub(message,1,5) == "/list" then
rednet.broadcast("")
rednet.broadcast(message)
message = ""
elseif string.sub(message,1,5) == "/exit" then
addChat("<SYS> Goodbye!")
rednet.broadcast("")
rednet.broadcast("User '" .. username .. "' exits the room.")
sleep(1.0)
term.clear()
term.setCursorPos(1,1)
break
elseif string.sub(message,1,5) == "/quit" then
addChat("<SYS> Goodbye!")
rednet.broadcast("")
rednet.broadcast("User '" .. username .. "' exits the room.")
sleep(1.0)
term.clear()
term.setCursorPos(1,1)
break
elseif string.sub(message,1,5) == "/help" then
addChat("/help - lists programs")
addChat("/name <name> - changes username")
addChat("/list - lists connected users, and console IDs")
addChat("/me - used as emote")
addChat("/clear - clears the screen")
addChat("/msg <ID> <message> - sends a private message")
addChat("/exit - exits the program")
message = ""
elseif string.sub(message,1,5) == "/msg " then
if string.len(message) > 5 then
if string.sub(message,7,7) == " " then
toID = string.sub(message,6,6)
msgMessage = string.sub(message,8,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message, 8,8) == " " then
toID = string.sub(message,6,7)
msgMessage = string.sub(message,9,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message, 9,9) == " " then
toID = string.sub(message,6,8)
msgMessage = string.sub(message,10,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message,10,10) == " " then
toID = string.sub(message,6,9)
msgMessage = string.sub(message,11,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
end
end
elseif string.sub(message,1,3) == "/m " then
if string.len(message) > 3 then
if string.sub(message,5,5) == " " then
toID = string.sub(message,4,4)
msgMessage = string.sub(message,6,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message, 6,6) == " " then
toID = string.sub(message,4,5)
msgMessage = string.sub(message,7,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message, 7,7) == " " then
toID = string.sub(message,4,6)
msgMessage = string.sub(message,8,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
elseif string.sub(message,8,8) == " " then
toID = string.sub(message,4,7)
msgMessage = string.sub(message,9,string.len(message))
addChat("You whisper to #" .. toID .. ": " .. msgMessage)
message = username .. " whispers to you: " .. msgMessage
toID = tonumber(toID)
rednet.send(toID,"")
rednet.send(toID,message)
message = ""
end
end
elseif string.sub(message,1,6) == "/clear" then
term.clear()
for n=1,16 do
addChat(" ")
end
chatTable = {}
chatLine = 1
message = ""
else
message = ""
end
else
if string.len(message) > 0 then
username = tostring(username)
message = tostring(message)
message = "<" .. username .. "> " .. message
rednet.broadcast("")
rednet.broadcast(message)
addChat(message)
message = ""
drawLine()
end
end
elseif param == 14 then
message = string.sub(message,1,string.len(message)-1)
drawLine()
end
end
if event == "char" then
if string.len(message) < 40 then
message = message .. param
end
drawLine()
end
if event == "rednet_message" then
id, incoming = rednet.receive(0.5)
if incoming == "/list" then
rednet.send(id,"")
rednet.send(id,"User '" .. username .. "' is connected at terminal #" .. compID)
else
addChat(incoming)
end
end
until message == "override"
Credits
A big thanks to Liraal for his "yes/no" menu tutorial. I used that for the side selection at the start.
Bigger thanks to kyoshiku for "MC-Chat", elements of which are used in this. I didn't take any code directly, but I definitely used a lot of his methods to get started (since I've never done anything like this before, I didn't really know where to begin!). The things I used which are based on his code are: Getting text input without using "read()" and using string.sub() to recognise user commands.
Thanks to Casper7526 for his "How to Use Modems" tutorial, as well :D/>/>
Also big thanks to Hwaet, one of my fellow admins, for being the bestest ever, and encouraging me! :3