Advertisement
SirMongoose

SirChat

Aug 4th, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.67 KB | None | 0 0
  1. --SirChat v1.2.0 -Created by Sir.Mongoose
  2.  
  3. local IDs = {}
  4. local ex = false
  5. local ID = os.getComputerID()
  6. local cle = false
  7. local cle2 = false
  8. local line = 0
  9. local mLine = 1
  10. local targetID = nil
  11. local x = true
  12. local monitor = term
  13. local w = 1
  14. local p = 2
  15. local c = 3
  16. local cl = 4
  17. local s = 5
  18. local m = 6
  19. local sp = true
  20. local curx = 0
  21. local cury = 0
  22. local he = 0
  23. local ms = 7
  24. local wi = 0
  25. local he = 0
  26.  
  27. function termToMon(a,b,d)  
  28.     if monitor ~= term then
  29.         if a == ms then
  30.             if d == line then
  31.             monitor.setCursorPos(b,mLine)
  32.             else
  33.             monitor.setCursorPos(b,d)
  34.             end
  35.         elseif a == m and b == c and d == c then
  36.             monitor.clear()
  37.         elseif a == m and b == p then
  38.             curx, cury = monitor.getCursorPos()
  39.             if sp == false then
  40.                 monitor.setCursorPos(1,cury+1)
  41.                 monitor.write(d)
  42.             else
  43.                 sp = false
  44.                 monitor.write(d)
  45.             end
  46.        
  47.         elseif a == m and b == w then
  48.             curx, cury = monitor.getCursorPos()
  49.             if sp == false then
  50.                 monitor.setCursorPos(1, cury+1)
  51.                 monitor.write(d)
  52.                 sp = true
  53.             else
  54.                 monitor.write(d)
  55.             end
  56.         elseif a == w and b == w then
  57.             curx, cury = monitor.getCursorPos()
  58.             if sp == false then
  59.                 monitor.setCursorPos(1, cury+1)
  60.                 monitor.write(d)
  61.                 sp = true
  62.             else
  63.                 monitor.write(d)
  64.             end
  65.             write(d)
  66.         elseif a == p and b == p then
  67.                 curx, cury = monitor.getCursorPos()
  68.                 if sp == false then
  69.                     monitor.setCursorPos(1,cury+1)
  70.                     monitor.write(d)
  71.                 else
  72.                     sp = false
  73.                     monitor.write(d)
  74.                 end
  75.             print(d)
  76.         elseif a == c and b == c and d == c then
  77.             monitor.clear()
  78.             term.clear()
  79.         elseif a == cl and b == cl and d == cl then
  80.             monitor.clearLine()
  81.             term.clearLine()
  82.         elseif a == s then
  83.             if d == line then
  84.             monitor.setCursorPos(b,mLine)
  85.             term.setCursorPos(b,d)
  86.             else
  87.             monitor.setCursorPos(b,d)
  88.             term.setCursorPos(b,d)
  89.             end
  90.         end
  91.    
  92.  
  93.     else
  94.         if a == w and b == w then
  95.             write(d)
  96.         elseif a == p and b == p then
  97.             print(d)
  98.         elseif a == c and b == c and d == c then
  99.             term.clear()
  100.         elseif a == cl and b == cl and d == cl then
  101.             term.clearLine()
  102.         elseif a == s then
  103.             term.setCursorPos(b,d)
  104.         end
  105.     end
  106. end
  107.  
  108. function receive()
  109. local senderID, message = rednet.receive()
  110.     if message == "::sirchat" then
  111.         IDs[senderID] = true
  112.         termToMon(cl,cl,cl)
  113.         termToMon(s,1,line)
  114.     elseif IDs[senderID] == true then
  115.         termToMon(cl,cl,cl)
  116.         termToMon(s,1,line)
  117.         termToMon(p,p,"<ID#"..senderID.."> "..(tostring(message)))
  118.         line = line + 1
  119.         mLine = mLine + 1
  120.         table.remove(IDs, senderID)
  121.     else
  122.         termToMon(cl,cl,cl)
  123.         termToMon(s,1,line)
  124.     end
  125. end
  126.    
  127. function send()
  128.     termToMon(w,w,"<ID#"..ID.."> ")
  129.     local mesg = read()
  130.     termToMon(m,p,mesg)
  131.     if mesg == "/exit" then
  132.         ex = true
  133.         termToMon(c,c,c)
  134.         termToMon(s,1,1)
  135.     elseif mesg == "/commands" then
  136.         termToMon(p,p,"> '/exit' terminates the program.")
  137.         termToMon(p,p,"> '/clear' clears chat completely.")
  138.         termToMon(p,p,"> '/connect' connects you to a different ID.")
  139.         termToMon(p,p,"> '/info' Tells you about SirChat.")
  140.         line = line + 5
  141.         mLine = mLine + 5
  142.     elseif mesg == "/clear" then
  143.         cleA = true
  144.     elseif mesg == "/connect" then
  145.         connect()
  146.     elseif mesg == "/info" then
  147.         termToMon(p,p,"> SirChat was a program made by Sir.Mongoose.")
  148.         termToMon(p,p,"> SirChat is currently in version 1.2.0.")
  149.         line = line + 3
  150.         mLine = mLine + 3
  151.     elseif mesg ~= "/exit" or "/commands" or "/clear" or "/connect" or "/info" then
  152.         rednet.send((tonumber(targetID)), "::sirchat")
  153.         rednet.send((tonumber(targetID)), (tostring(mesg)))
  154.         line = line + 1
  155.         mLine = mLine + 1
  156.     end
  157. end
  158.    
  159. function chat()
  160.     x = false
  161.     termToMon(c,c,c)
  162.     termToMon(s,1,1)
  163.     termToMon(p,p,"**Connected with ComputerID#"..(tostring(targetID)).."**")
  164.     termToMon(p,p,"**Type '/commands' for the list of commands**")
  165.     while ex == false do
  166.         parallel.waitForAny(receive, send)
  167.         if line > 17 then
  168.             cle = true
  169.         elseif mLine > he-2 then
  170.             cle2 = true
  171.         end
  172.         if cleA == true then
  173.             line = 1
  174.             mLine = 0
  175.             termToMon(c,c,c)
  176.             termToMon(s,1,line)
  177.             termToMon(p,p,"**Connected with ComputerID#"..(tostring(targetID)).."**")
  178.             termToMon(p,p,"**Type '/commands' for the list of commands**")
  179.             line = 3
  180.             mLine = 4
  181.             cleA = false
  182.         elseif cle == true then
  183.             term.clear()
  184.             term.setCursorPos(1,1)
  185.             print("**Connected with ComputerID#"..(tostring(targetID)).."**")
  186.             print("**Type '/commands' for the list of commands**")
  187.             line = 3
  188.             cle = false
  189.         elseif cle2 == true then
  190.             termToMon(m,c,c)
  191.             termToMon(ms,1,0)
  192.             termToMon(m,p,"**Connected with ComputerID#"..(tostring(targetID)).."**")
  193.             termToMon(m,p,"**Type '/commands' for the list of commands**")
  194.             mLine = 4
  195.             cle2 = false
  196.         end
  197.     end
  198. end
  199.  
  200. function numCheck(a)
  201.   if ((tonumber(a)) == nil) == true then
  202.     return false
  203.   else
  204.     return true
  205.    end
  206. end
  207.  
  208. function connect()
  209.   termToMon(p,p,"**Enter the ComputerID you want to connect to**")
  210.   line = line + 2
  211.   mLine = mLine + 2
  212.   termToMon(s,1,line)
  213.   termToMon(w,w,">>")
  214.   target = read()
  215.  
  216.   if numCheck(target) == true then
  217.     targetID = target
  218.     termToMon(m,w,target)
  219.     line = line + 1
  220.     mLine = mLine + 1
  221.   else
  222.         while numCheck(target) == false do
  223.             termToMon(s,1,line)
  224.             termToMon(cl,cl,cl)
  225.             termToMon(w,w,">>")
  226.             target = read()
  227.         end
  228.     targetID = target
  229.     termToMon(m,w,target)
  230.     line = line + 1
  231.   end
  232.     if x == true then
  233.         x = false
  234.         termToMon(c,c,c)
  235.         line = 3
  236.         mLine = 3
  237.         termToMon(s,1,1)
  238.         chat()
  239.     else
  240.         termToMon(s,1,1)
  241.         termToMon(cl,cl,cl)
  242.         termToMon(p,p,"**Connected with ComputerID#"..(tostring(targetID)).."**")
  243.         termToMon(s,1,line)
  244.   end
  245. end
  246.  
  247. function connectModem()
  248.     if monitor ~= term then
  249.         wi, he = monitor.getSize()
  250.         if wi < 50 then
  251.             monitor.setTextScale(0.9)
  252.             wi, he = monitor.getSize()
  253.         end
  254.     end
  255.     termToMon(c,c,c)
  256.     termToMon(s,1,1)
  257.   if peripheral.isPresent("back") and peripheral.getType("back") == "modem" then
  258.     rednet.open("back")
  259.     connect()
  260.   elseif peripheral.isPresent("front") and peripheral.getType("front") == "modem" then
  261.     rednet.open("front")
  262.     connect()
  263.   elseif peripheral.isPresent("left") and peripheral.getType("left") == "modem" then
  264.     rednet.open("left")
  265.     connect()
  266.   elseif peripheral.isPresent("right") and peripheral.getType("right") == "modem" then
  267.     rednet.open("right")  
  268.     connect()  
  269.   elseif peripheral.isPresent("top") and peripheral.getType("top") == "modem" then
  270.     rednet.open("top")
  271.     connect()
  272.   elseif peripheral.isPresent("bottom") and peripheral.getType("bottom") == "modem" then
  273.     rednet.open("bottom")
  274.     connect()
  275.   else
  276.     termToMon(p,p,"**No modem connected, please place one on an available side and press enter**")
  277.     read()
  278.     termToMon(c,c,c)
  279.     termToMon(s,1,1)
  280.     connectModem()
  281.    end
  282. end
  283.  
  284. function connectMonitor()
  285.     if peripheral.isPresent("back") and peripheral.getType("back") == "monitor" then
  286.         monitor = peripheral.wrap("back")
  287.         connectModem()
  288.     elseif peripheral.isPresent("bottom") and peripheral.getType("bottom") == "monitor" then
  289.         monitor = peripheral.wrap("bottom")
  290.         connectModem()
  291.     elseif peripheral.isPresent("front") and peripheral.getType("front") == "monitor" then
  292.         monitor = peripheral.wrap("front")
  293.         connectModem()
  294.     elseif peripheral.isPresent("left") and peripheral.getType("left") == "monitor" then
  295.         monitor = peripheral.wrap("left")
  296.         connectModem()
  297.     elseif peripheral.isPresent("right") and peripheral.getType("right") == "monitor" then
  298.         monitor = peripheral.wrap("right")
  299.         connectModem()
  300.     elseif peripheral.isPresent("top") and peripheral.getType("top") == "monitor" then
  301.         monitor = peripheral.wrap("top")
  302.         connectModem()
  303.     else
  304.         connectModem()
  305.     end
  306. end
  307.  
  308. connectMonitor()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement