Advertisement
SirMongoose

SirChat

Jul 29th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.25 KB | None | 0 0
  1. --SirChat v1.1.0 -Created by Sir.Mongoose
  2. local IDs = {}
  3. local ex = false
  4. local ID = os.getComputerID()
  5. local cl = false
  6. local line = 3
  7.  
  8. function receive()
  9. local senderID, message = rednet.receive()
  10.     if message == "::sirchat" then
  11.         IDs[senderID] = true
  12.         term.clearLine()
  13.         term.setCursorPos(1,line)
  14.     elseif IDs[senderID] == true then
  15.         term.clearLine()
  16.         term.setCursorPos(1,line)
  17.         print("<ID#"..senderID.."> "..(tostring(message)))
  18.         line = line + 1
  19.         table.remove(IDs, senderID)
  20.     else
  21.         term.clearLine()
  22.         term.setCursorPos(1,line)
  23.     end
  24. end
  25.    
  26. function send()
  27.     term.write("<ID#"..ID.."> ")
  28.     local mesg = read()
  29.     if mesg == "/exit" then
  30.         ex = true
  31.         term.clear()
  32.         term.setCursorPos(1,1)
  33.     elseif mesg == "/commands" then
  34.         print("> '/exit' terminates the program.")
  35.         print("> '/clear' clears chat completely.")
  36.         print("> '/connect' connects you to a different ID.")
  37.         print("> '/info' Tells you about SirChat.")
  38.         line = line + 5
  39.     elseif mesg == "/clear" then
  40.         cl = true
  41.     elseif mesg == "/connect" then
  42.         connect()
  43.     elseif mesg == "/info" then
  44.         print("> SirChat was a program made by Sir.Mongoose.")
  45.         print("> SirChat is currently in version 1.1.")
  46.     elseif mesg ~= "/exit" or "/commands" or "/clear" or "/connect" or "/info" then
  47.         rednet.broadcast("::sirchat")
  48.         rednet.send((tonumber(targetID)), (tostring(mesg)))
  49.         line = line + 1
  50.     end
  51. end
  52.  
  53. function chat()
  54.     term.clear()
  55.     term.setCursorPos(1,1)
  56.     print("**Connected with ComputerID#"..(tostring(targetID)).."**")
  57.     print("**Type '/commands' for the list of commands**")
  58.     while ex == false do
  59.         parallel.waitForAny(receive, send)
  60.         if cl == true then
  61.             term.clear()
  62.             term.setCursorPos(1,1)
  63.             print("**Connected with ComputerID#"..(tostring(targetID)).."**")
  64.             print("**Type '/commands' for the list of commands**")
  65.             line = 3
  66.             cl = false
  67.         end
  68.     end
  69. end
  70.  
  71. function numCheck(a)
  72.   if ((tonumber(a)) == nil) == true then
  73.     return false
  74.   else
  75.     return true
  76.    end
  77. end
  78.  
  79. function connect()
  80.   term.clear()
  81.   term.setCursorPos(1,1)
  82.   print("**Enter the ComputerID you want to connect to**")
  83.   targetID = read()
  84.  
  85.   if numCheck(targetID) == true then
  86.     chat()
  87.   else
  88.     while numCheck(targetID) == false do
  89.         term.setCursorPos(1,2)
  90.         term.clearLine()
  91.         targetID = read()
  92.     end
  93.     chat()
  94.   end
  95. end
  96.  
  97. function connectModem()
  98. term.clear()
  99. term.setCursorPos(1,1)
  100.  
  101.   if peripheral.isPresent("back") and peripheral.getType("back") == "modem" then
  102.     rednet.open("back")
  103.     connect()
  104.   elseif peripheral.isPresent("front") and peripheral.getType("front") == "modem" then
  105.     rednet.open("front")
  106.     connect()
  107.   elseif peripheral.isPresent("left") and peripheral.getType("left") == "modem" then
  108.     rednet.open("left")
  109.     connect()
  110.   elseif peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  111.     rednet.open("right")
  112.     connect()  
  113.   elseif peripheral.isPresent("top") and peripheral.getType("top") == "modem" then
  114.     rednet.open("top")
  115.     connect()
  116.   elseif peripheral.isPresent("bottom") and peripheral.getType("bottom") == "modem" then
  117.     rednet.open("bottom")
  118.     connect()
  119.   else
  120.     print("**No modem connected, please place one on an available side and press enter**")
  121.     read()
  122.     connectModem()
  123.    end
  124. end
  125.  
  126. connectModem()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement