Advertisement
Guest User

stairctrl

a guest
May 19th, 2013
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.77 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. ------------------------------------------------------------------
  3.  
  4. local codename = 'stairctrl'
  5. local codeversion = '0.4.0.2'
  6. local whoami = os.getComputerID ()
  7.  
  8. term.clear ()
  9. print (">>>"..codename.." Version "..codeversion.."@ID:"..whoami)
  10.  
  11. ------------------------------------------------------------------
  12. ------------------------------------------------------------------
  13.  
  14. local password = 'IAO'
  15.  
  16. local msg_expected_paramcount = 4 -- message format: <SenderID>|<ReceiverID>|<Secret>|<Command>
  17. local shared_secret = "OpenUpMofo"
  18. local sendChannel = 59999
  19. local listenChannel = 60000
  20. local rs_signal_output = 'right'
  21. local current_status = false
  22.  
  23. local monitor = peripheral.wrap('left')
  24. local mon_maxx, mon_maxy = monitor.getSize()
  25.  
  26. local quote = {"Improvise,", "Adapt,", "Overcome."}
  27. local charoffsettable = {}
  28. local stairvisible = false
  29.  
  30. ------------------------------------------------------------------
  31.  
  32. local function parseMessage (msg)
  33.     local msgparam = {}
  34.     for param in string.gmatch (msg, "[^|]+") do
  35.         table.insert (msgparam, param)
  36.     end
  37.    
  38.     return msgparam
  39. end
  40.  
  41. ------------------------------------------------------------------
  42.  
  43. local function showStairs ()
  44.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|2|"..shared_secret.."|on")
  45.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|4|"..shared_secret.."|on")
  46.     sleep (1)
  47.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|3|"..shared_secret.."|on")
  48. end
  49.  
  50. ------------------------------------------------------------------
  51.  
  52. local function hideStairs ()
  53.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|3|"..shared_secret.."|off")
  54.     sleep (1)
  55.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|2|"..shared_secret.."|off")
  56.     calicommAPI.modemtable[1][1].transmit(sendChannel, listenChannel, whoami.."|4|"..shared_secret.."|off")
  57. end
  58.  
  59. local function drawQuote ()
  60.     monitor.clear()
  61.     local ypos = math.ceil((mon_maxy / 2) - (#quote / 2))
  62.    
  63.     for i=1,#quote do
  64.         local xpos = math.ceil((mon_maxx / 2) - (string.len(quote[i]) / 2)) + 1
  65.         ypos = ypos + 1
  66.         table.insert (charoffsettable, {xpos, ypos})
  67.         monitor.setCursorPos(xpos,ypos)
  68.         monitor.write(quote[i])
  69.     end
  70. end
  71.  
  72. local function getKeyFromScreen (xclick, yclick)
  73.     for i=1,#charoffsettable do
  74.         if (yclick == charoffsettable[i][2]) then
  75.             local xoffset = xclick - charoffsettable[i][1]
  76.             if ((xoffset >= 0) and ((xoffset + 1) <= #quote[i])) then
  77.                 --print ("X:"..xclick.." Y:"..yclick.." Key:"..string.sub (quote[i], xoffset + 1, xoffset + 1))
  78.                 return (string.sub (quote[i], xoffset + 1, xoffset + 1))
  79.             end
  80.         end
  81.     end
  82.     print ("X:"..xclick.." Y:"..yclick.." Key:")
  83. end
  84.  
  85. -- MAIN ----------------------------------------------------------
  86.  
  87. os.loadAPI ('calicommAPI')
  88. calicommAPI.enableDebug (false)
  89. calicommAPI.initModems ()
  90.  
  91. calicommAPI.openModemChannel (calicommAPI.modemtable[1][1], listenChannel)
  92.  
  93. while (true) do
  94.     drawQuote ()
  95.    
  96.     if (stairvisible) then
  97.         local event, monside, xpos, ypos = os.pullEvent("monitor_touch")
  98.         print "*closed stairs"
  99.         hideStairs ()
  100.         stairvisible = false
  101.     else   
  102.         local pwentered = ''
  103.         while (tonumber(string.len(pwentered)) < tonumber(string.len(password))) do
  104.             local event, monside, xpos, ypos = os.pullEvent("monitor_touch")
  105.             local pressed = getKeyFromScreen (xpos, ypos)
  106.             if (pressed ~= nil) then
  107.                 pwentered = pwentered .. pressed
  108.             end
  109.         end
  110.    
  111.         if (pwentered == password) then
  112.             print "*Password ok"
  113.             showStairs ()
  114.             stairvisible = true
  115.         else
  116.             print "*Password error"
  117.         end
  118.  
  119.     end
  120. end
  121.  
  122. calicommAPI.closeModemChannel (calicommAPI.modemtable[1][1], listenChannel)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement