Advertisement
SirMongoose

SirChat

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