savior67

FalloutLock

Feb 26th, 2013
1,326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --=================================
  2. --FalloutLock created by Savior67
  3. --Do not Redistribute
  4. --=================================
  5. --CONFIG OPTIONS-------------------
  6. local output = "bottom"   --output side
  7. local wordFile = "codes"  --where the words are stored
  8. local textColor = colors.lime
  9. local attempts = 4        --how many attempts to guess the word?
  10. local permLockout = true  --lock the terminal permanently?
  11. --only way to enter if permLockout==true is with disk
  12. --then delete lock_status, or edit and set Locked=false
  13. ----------------------------------
  14.  
  15. local usedWords = {}
  16. local status = {}
  17.  
  18. --check for advanced monitor
  19. if not term.isColor() then
  20.   print("This program needs an advanced monitor.")
  21.   return
  22. end
  23. --check for wordFile
  24. if not fs.exists(wordFile) then
  25.   print("The word file \""..wordFile.."\" is not populated.")
  26.   return
  27. end
  28. --centers the x value of a string
  29. function center(str,y)
  30.   local maxX,maxY = term.getSize()
  31.   term.setCursorPos(math.floor(maxX/2)-math.floor(string.len(str)/2),y)
  32.   term.write(str)
  33. end
  34. --locks the terminal
  35. function lockout()
  36.   term.clear()
  37.   term.setTextColor(textColor)
  38.   center("TERMINAL LOCKED",8)
  39.   center("PLEASE CONTACT AN ADMINISTRATOR",10)
  40.   local h = fs.open("lock_status","w")
  41.   h.writeLine("Locked: true")
  42.   h.close()
  43.   while true do
  44.     sleep(1000)
  45.   end
  46.   os.reboot()
  47. end
  48. --check for permLock
  49. if not fs.exists("lock_status") then
  50.   local h = fs.open("lock_status","w")
  51.   h.writeLine("Locked: false")
  52.   h.close()
  53. else
  54.   local h = fs.open("lock_status","r")
  55.   local isLocked = string.sub(h.readLine(),9)
  56.   if isLocked=="true" and permLockout then
  57.     lockout()
  58.   end
  59. end
  60.  
  61. function main()
  62.   local rows=12
  63.   local cols=10
  64.   term.clear()
  65.   term.setCursorPos(1,1)
  66.   local dict = readWords(wordFile)
  67.   local eString1 = getEncoded(rows*cols,dict)
  68.   local eString2 = getEncoded(rows*cols,dict)
  69.   drawTitle()
  70.   drawAttempts(attempts)
  71.   drawHex(2,7)
  72.   drawHex(20,7)
  73.   drawEncoded(9,7,rows,cols,eString1)
  74.   drawEncoded(27,7,rows,cols,eString2)
  75.   local password=getPass()
  76.   eventListener(eString1,eString2,password)
  77.   term.setCursorPos(1,1)
  78. end
  79.  
  80. function getStatus(choice,pass)
  81.   local sameChars = commonLetters(choice,pass)
  82.   table.insert(status,string.upper(choice))
  83.   if sameChars==#pass and string.len(choice)==string.len(pass) then
  84.      table.insert(status,"Exact match!")
  85.      table.insert(status,"Please wait")
  86.      table.insert(status,"while system")
  87.      table.insert(status,"is accessed.")
  88.   else
  89.      table.insert(status,"Entry denied")
  90.      table.insert(status,sameChars.."/"..string.len(pass).." correct.")
  91.      attempts=attempts-1
  92.   end
  93.  
  94.   if attempts==0 then
  95.     lockout()
  96.   end  
  97. end
  98.  
  99.  
  100.  
  101. function drawStatus()
  102.   local startX=38
  103.   local startY=16
  104.   local currentStat=#status
  105.   repeat
  106.     if status[currentStat]==nil then break end
  107.     clearFrom(startX,startY)
  108.     term.setCursorPos(startX,startY)
  109.     term.write(">"..status[currentStat])
  110.     currentStat=currentStat-1
  111.     startY=startY-1
  112.   until startY<=6
  113. end
  114.  
  115. --highlights part of an eString
  116.  
  117. --detects keypresses
  118. function eventListener(leftStr,rightStr,pass)
  119.   local side="left"     --which eString is it
  120.   local charPos=1       --which  number character
  121.   local cmdLineX,cmdLineY=38,18
  122.   local selection = string.sub(leftStr,1,1)
  123.   local onWord=false
  124.   local cursorX,cursorY=9,7
  125.   local upperX,upperY=36,18 --upper limits
  126.   local lowerX,lowerY=9,7   --lower limits
  127.   local keys = {200,208,203,205}
  128.  
  129.   highlight(cursorX,cursorY,selection)
  130.   while true do
  131.     local ev,p1,p2,p3 = os.pullEventRaw()
  132.     if ev=="key" then
  133.       if inTable(p1,keys) and not onWord then
  134.         lowlight(cursorX,cursorY,selection)
  135.       elseif inTable(p1,keys) and onWord then
  136.         if side=="left" then
  137.           lowWord(selection,leftStr,side)
  138.         else
  139.           lowWord(selection,rightStr,side)
  140.         end
  141.       end
  142.      
  143.       if p1==200 and cursorY>lowerY then  --up arrow
  144.          cursorY=cursorY-1
  145.       elseif p1==208 and cursorY<upperY then --down arrow
  146.          cursorY=cursorY+1
  147.       elseif p1==203 and cursorX>lowerX then --left arrow
  148.          if cursorX==27 then
  149.            side="left"
  150.            cursorX=cursorX-9
  151.          else
  152.            cursorX=cursorX-1
  153.          end
  154.       elseif p1==205 and cursorX<upperX then --right arrow
  155.          if cursorX==18 then
  156.            side="right"
  157.            cursorX=cursorX+9
  158.          else
  159.            cursorX=cursorX+1
  160.          end
  161.       elseif p1==28 then                     --enter key
  162.         getStatus(selection,pass)
  163.         drawStatus()
  164.         drawAttempts(attempts)
  165.     if selection==pass then
  166.           rs.setOutput(output,true)
  167.           sleep(3)
  168.           rs.setOutput(output,false)
  169.       sleep(1)
  170.       os.shutdown()
  171.         end
  172.       end
  173.     --elseif ev=="mouse_click" and p1==1 then --left mouse button
  174.     --  if lowerX<=p2 and p2<=upperX and lowerY<=p3 and p3<=upperY then
  175.     --    cursorX,cursorY=p2,p3
  176.     --  end
  177.     end
  178.    
  179.     if side=="left" then
  180.       charPos=math.floor(((cursorY-7)*10)+(cursorX-9)+1)
  181.       selection=string.sub(leftStr,charPos,charPos)
  182.     else
  183.       charPos=math.floor(((cursorY-7)*10)+(cursorX-27)+1)
  184.       selection=string.sub(rightStr,charPos,charPos)
  185.     end
  186.    
  187.     if isalpha(selection) and side=="left" then
  188.       selection = findWord(cursorX,cursorY,charPos,leftStr,side)
  189.       lightWord(selection,leftStr,side)
  190.       onWord=true
  191.     elseif isalpha(selection) and side=="right" then
  192.       selection = findWord(cursorX,cursorY,charPos,rightStr,side)
  193.       lightWord(selection,rightStr,side)
  194.       onWord=true
  195.     else
  196.       onWord=false
  197.       highlight(cursorX,cursorY,selection)
  198.     end
  199.     clearFrom(cmdLineX,cmdLineY)
  200.     term.setCursorPos(cmdLineX,cmdLineY)
  201.     term.write(">"..selection)
  202.   end
  203. end
  204.  
  205. function lightWord(sel,eString,side)
  206.   local j=1
  207.   local l,r = string.find(eString,sel)
  208.   for i=l,r do
  209.     local x,y=posToCoords(i,side)
  210.     highlight(x,y,string.sub(sel,j,j))
  211.     j=j+1
  212.   end
  213. end
  214.  
  215. function lowWord(sel,eString,side)
  216.   local j=1
  217.   local l,r = string.find(eString,sel)
  218.   for i=l,r do
  219.     local x,y = posToCoords(i,side)
  220.     lowlight(x,y,string.sub(sel,j,j))
  221.     j=j+1
  222.   end
  223. end
  224.  
  225. function findWord(x,y,charPos,eString,side)
  226.   --check left and right
  227.   local left = charPos
  228.   local right = charPos
  229.   while isalpha(string.sub(eString,left-1,left-1)) do
  230.     left=left-1
  231.   end
  232.   while isalpha(string.sub(eString,right+1,right+1)) do
  233.     right=right+1
  234.   end
  235.   --highlight the word
  236.   for i=left,right do
  237.     local x,y = posToCoords(i,side)
  238.     highlight(x,y,string.sub(eString,i,i))
  239.   end
  240.   return(string.sub(eString,left,right))  
  241. end
  242.  
  243. function posToCoords(pos,side)
  244.   local x,y=0,0
  245.   if side=="left" then
  246.     if pos%10~=0 then
  247.       x=pos%10+8
  248.       y=math.floor(pos/10)+7
  249.     else
  250.       x=10+8
  251.       y=math.floor(pos/10)+6
  252.     end
  253.   else
  254.     if pos%10~=0 then
  255.       x=pos%10+26
  256.       y=math.floor(pos/10)+7
  257.     else
  258.       x=10+26
  259.       y=math.floor(pos/10)+6
  260.     end
  261.   end
  262.   return x,y
  263. end
  264.  
  265. function highlight(x,y,sel)
  266.   term.setBackgroundColor(textColor)
  267.   term.setTextColor(colors.black)
  268.   term.setCursorPos(x,y)
  269.   term.write(sel)
  270.   term.setBackgroundColor(colors.black)
  271.   term.setTextColor(textColor)
  272. end
  273.  
  274. function lowlight(x,y,sel)
  275.   term.setBackgroundColor(colors.black)
  276.   term.setTextColor(textColor)
  277.   term.setCursorPos(x,y)
  278.   term.write(sel)
  279. end
  280.  
  281. --returns a password from the usedWords list
  282. function getPass()
  283.   return(usedWords[math.random(1,#usedWords)])
  284. end
  285.  
  286. function drawTitle()
  287.   term.setCursorPos(2,2)
  288.   term.setTextColor(textColor)
  289.   term.write("ROBCO INDUSTRIES (TM) TERMLINK PROTOCOL")
  290.   term.setCursorPos(2,3)
  291.   term.write("ENTER PASSWORD NOW")
  292. end
  293.  
  294. function drawAttempts(a)
  295.   term.setCursorPos(2,5)
  296.   term.setTextColor(textColor)
  297.   term.clearLine()
  298.   term.write(a.." ATTEMPT(S) LEFT: ")
  299.   local x = 21
  300.   for i=1,a do
  301.     term.setBackgroundColor(textColor)
  302.     term.setCursorPos(x,5)
  303.     term.write(" ")
  304.     x = x + 2
  305.   end
  306.   term.setBackgroundColor(colors.black)
  307. end
  308.  
  309. function drawHex(x,y)
  310.   local rows = 12
  311.   term.setTextColor(textColor)
  312.   for i=1,rows do
  313.     term.setCursorPos(x,y)
  314.     local hstring="0x"
  315.     for j=1,4 do hstring=hstring..randomHex() end
  316.     term.write(hstring)
  317.     y = y + 1
  318.   end    
  319. end
  320.  
  321. --returns string containing random nonletter characters
  322. --and various words. does not have '\n' included
  323. function getEncoded(size,words)
  324.   local step = 1
  325.   local totalWords=0
  326.   local curWord = nil
  327.   local onWord = false
  328.   local charIter = 1 --what letter in curWord are we at?
  329.   local chance = 0
  330.   local encodedString=nonletter()
  331.   local lastIsWord=false
  332.  
  333.   --fills encodedString with nonletter chars and words
  334.   repeat
  335.     step=step+1
  336.     chance = math.random(1,13)  
  337.     if chance==6 and not onWord and not lastIsWord and totalWords<=8 then
  338.       local reps=0
  339.       repeat
  340.         if reps>20 then break end
  341.         curWord=words[math.random(1,#words)]
  342.         reps=reps+1
  343.       until not inTable(curWord,usedWords) and step+#curWord<size
  344.       if reps<=20 then onWord=true end
  345.     end
  346.     --determines whether to add random nonletter or char from curWord
  347.     if onWord then
  348.       encodedString=encodedString..string.sub(curWord,charIter,charIter)
  349.       charIter=charIter+1
  350.       if charIter>#curWord then
  351.          table.insert(usedWords,curWord)
  352.          onWord=false
  353.          totalWords=totalWords+1
  354.          lastIsWord=true
  355.          charIter=1
  356.        end
  357.     else
  358.       encodedString=encodedString..nonletter()
  359.       lastIsWord=false
  360.     end
  361.   until string.len(encodedString)==size
  362.   return encodedString
  363. end
  364.  
  365. function isalpha(char)
  366.   local alpha="abcdefghijklmnopqrstuvwxyz"
  367.   for i=1,string.len(alpha) do
  368.     if string.sub(alpha,i,i)==char then return true end
  369.   end
  370.   return false
  371. end
  372. --lightS is a table {x1,y1} and lightE is table {x2,y2}
  373. function drawEncoded(x,y,rows,cols,eString,lightS,lightE)
  374.   term.setTextColor(textColor)
  375.   local curX=x
  376.   local curY=y
  377.   for i=1,rows*cols do
  378.     term.setCursorPos(curX,curY)
  379.     term.write(string.sub(eString,i,i))
  380.     if i%cols==0 then
  381.        curY=curY+1
  382.        curX=x
  383.     else
  384.        curX=curX+1
  385.     end
  386.   end
  387. end
  388.  
  389. --read the codewords from the file
  390. --returns list
  391. function readWords(fileName)
  392.   local dict = {}
  393.   local h = fs.open(fileName,"r")
  394.   for word in string.gmatch(h.readAll(), "%a+") do table.insert(dict,word) end
  395.   return dict
  396. end
  397.  
  398.  
  399. function findPattern(text,pattern,start)
  400.   return string.sub(text,string.find(text,pattern,start))
  401. end
  402.  
  403. --returns one nonletter char (random)
  404. function nonletter()
  405.   local possible = {'!','@','#','$','%','^','&','*',
  406.                    '(',')','_','-','+','=','[','{',
  407.                    ']','|',',','\'','\"','}',
  408.                    ';',':','.','>','<','?'}
  409.   return(possible[math.random(1,#possible)])
  410. end  
  411.  
  412. --returns one valid hex num (random)
  413. function randomHex()
  414.   local possible = {'A','B','C','D','E','F'}
  415.   for i=0,9 do
  416.     table.insert(possible,i)
  417.   end
  418.   return(possible[math.random(1,#possible)])
  419. end
  420.  
  421. --returns int of same letters in same positions in two strings.
  422. function commonLetters(s1,s2)
  423.   charsInCommon=0
  424.   local long,short=nil,nil
  425.   if string.len(s1)>=string.len(s2) then long,short=s1,s2
  426.   else long,short=s2,s1 end
  427.   for i=1,#long do
  428.     if string.sub(long,i,i)==string.sub(short,i,i) then
  429.       charsInCommon=charsInCommon+1
  430.     elseif string.sub(short,i,i)==nil then
  431.       break
  432.     end
  433.   end
  434.   return charsInCommon
  435. end
  436.  
  437. --clears a line from x,y till the end of the screen
  438. function clearFrom(x,y)
  439.   term.setCursorPos(x,y)
  440.   for i=0,51-x do
  441.     term.setCursorPos(x+i,y)
  442.     term.write(" ")
  443.   end
  444. end
  445.  
  446. --checks if a value is in a given table
  447. function inTable(val,t)
  448.   for k,v in ipairs(t) do
  449.     if v==val then
  450.       return true
  451.     end
  452.   end
  453.   return false
  454. end
  455.  
  456. main()
Advertisement
Add Comment
Please, Sign In to add comment