icdb

[ComputerCraft] Remote Redstone (with PW) [sender]

Sep 6th, 2014
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.53 KB | None | 0 0
  1. --Sender
  2.  
  3. --Id of the reciver
  4. id = 4
  5. --True if you want that a password is necessary to change the output
  6. pwnecessary = true
  7. --Password (4 symbols)
  8. code = 1234
  9. --Wireless modem (currently at the top)
  10. rednet.open("top")
  11. --Monitor (currently at the back)
  12. monitor = peripheral.wrap("back")
  13.  
  14. function clrmonitor()
  15.     for i=1,7 do
  16.         for o=1,5 do
  17.             monitor.setCursorPos(i,o)
  18.             monitor.setTextColor(1)
  19.             monitor.setBackgroundColor(32768)
  20.             monitor.write("O")
  21.         end
  22.     end
  23.     monitor.clear()
  24.     monitor.setCursorPos(1,1)
  25. end
  26.  
  27. function question()
  28.     choose = false
  29.     rednet.send(id,"question")
  30.     event, id, message, distance = os.pullEvent("rednet_message")
  31.     while choose == false do
  32.         clrmonitor()
  33.         monitor.write("Status:")
  34.         for i=2,6 do
  35.             for o=2,4 do
  36.                 if message == "on" then
  37.                     monitor.setCursorPos(i,o)
  38.                     monitor.setBackgroundColor(8192)
  39.                     monitor.setTextColor(8192)
  40.                     monitor.write("O")
  41.                 else
  42.                     monitor.setCursorPos(i,o)
  43.                     monitor.setBackgroundColor(16384)
  44.                     monitor.setTextColor(16384)
  45.                     monitor.write("O")
  46.                 end
  47.             end
  48.         end
  49.         --monitor.setBackgroundColor(32768)
  50.         monitor.setTextColor(1)
  51.         monitor.setCursorPos(3,3)
  52.         monitor.write(message)
  53.         event, side, x, y = os.pullEvent("monitor_touch")
  54.         if y >= 2 or y <= 4 then
  55.             if x >= 2 and x <= 6 then
  56.                 choose = true
  57.             end
  58.         end
  59.     end
  60. end
  61.  
  62. function setnumber()
  63.     clrmonitor()
  64.    
  65.     monitor.setCursorPos(3,2)
  66.     monitor.write("1")
  67.     monitor.setCursorPos(4,2)
  68.     monitor.write("2")
  69.     monitor.setCursorPos(5,2)
  70.     monitor.write("3")
  71.    
  72.     monitor.setCursorPos(3,3)
  73.     monitor.write("4")
  74.     monitor.setCursorPos(4,3)
  75.     monitor.write("5")
  76.     monitor.setCursorPos(5,3)
  77.     monitor.write("6")
  78.    
  79.     monitor.setCursorPos(3,4)
  80.     monitor.write("7")
  81.     monitor.setCursorPos(4,4)
  82.     monitor.write("8")
  83.     monitor.setCursorPos(5,4)
  84.     monitor.write("9")
  85.    
  86.     monitor.setCursorPos(4,5)
  87.     monitor.write("0")
  88.    
  89.     monitor.setCursorPos(1,1)
  90. end
  91.  
  92. function getinput()
  93.     repeat
  94.         if x == 3 and y == 2 then
  95.             input = 1          
  96.            
  97.         elseif x == 4 and y == 2 then
  98.             input = 2
  99.            
  100.         elseif x == 5 and y == 2 then
  101.             input = 3
  102.            
  103.         elseif x == 3 and y == 3 then
  104.             input = 4
  105.            
  106.         elseif x == 4 and y == 3 then
  107.             input = 5
  108.            
  109.         elseif x == 5 and y == 3 then
  110.             input = 6
  111.            
  112.         elseif x == 3 and y == 4 then
  113.             input = 7
  114.            
  115.         elseif x == 4 and y == 4 then
  116.             input = 8
  117.            
  118.         elseif x == 5 and y == 4 then
  119.             input = 9
  120.            
  121.         elseif x == 4 and y == 5 then
  122.             input = 0
  123.         else
  124.             input = 10
  125.             event, side, x, y = os.pullEvent("monitor_touch")
  126.         end
  127.     until input ~= 10
  128. end
  129.  
  130. while true do
  131.     clrmonitor()
  132.     question()
  133.     if pwnecessary == true then
  134.         setnumber()
  135.         output = 0
  136.         for i=1,4 do
  137.              event, side, x, y = os.pullEvent("monitor_touch")
  138.              if event == "monitor_touch" then
  139.                 getinput()
  140.                 monitor.setTextColor(2048)
  141.                 monitor.setCursorPos(i+2,1)
  142.                 monitor.write(input)
  143.                 monitor.setCursorPos(i+1+2,1)
  144.                 monitor.write(" ")
  145.                 monitor.setCursorPos(i+2+2,1)
  146.                 monitor.write(" ")
  147.                 monitor.setTextColor(1)
  148.                 if i == 1 then
  149.                     output = output + input * 1000
  150.                 elseif i == 2 then
  151.                     output = output + input * 100
  152.                 elseif i == 3 then
  153.                     output = output + input * 10
  154.                 elseif i == 4 then
  155.                     output = output + input
  156.                 end
  157.              end
  158.         end
  159.         if output == code then
  160.             if message == "off" then
  161.                 rednet.send(id,"on")
  162.             else
  163.                 rednet.send(id,"off")
  164.             end
  165.         end
  166.         print(output)
  167.     else
  168.         if message == "off" then
  169.             rednet.send(id,"on")
  170.         else
  171.             rednet.send(id,"off")
  172.         end
  173.     end
  174.     sleep(0.5)
  175.     clrmonitor()
  176. end
Advertisement
Add Comment
Please, Sign In to add comment