Advertisement
SirMongoose

SirChat

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