Advertisement
Apjjm

ATDD Scripting: Password Example

Dec 20th, 2012
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.59 KB | None | 0 0
  1. //Valid button characters (I.e. Characters for which an area exists)
  2. string[] VALID_CHARS = {
  3.     "0","1","2","3",
  4.     "4","5","6","7",
  5.     "8","9","A","B",
  6.     "C","D","E","F",
  7.     "#"};
  8.  
  9. //Called when the map starts                       
  10. void OnStart()
  11. {
  12.     SetupButtons();
  13.    
  14.     //Say the password if you are testing your map!
  15.     if(ScriptDebugOn())
  16.         AddDebugMessage("Password is: " + GetLocalVarString("TargetPassword"),false);
  17. }          
  18.            
  19. //Call this to setup the buttons
  20. void SetupButtons()
  21. {
  22.     //Determine what the password should be at random
  23.     SetLocalVarString("InputPassword","");
  24.     SetLocalVarString("TargetPassword",GetRandomPassword(4));
  25.    
  26.     //Add an interact callback for each button
  27.     for(uint i=0; i<VALID_CHARS.length(); i++)
  28.     {
  29.         //For each valid button character, get it's script area & add the use callback
  30.         string area = "ScriptArea_" + VALID_CHARS[i];
  31.         SetEntityPlayerInteractCallback(area,"cbButtonPress",false);
  32.     }
  33.    
  34.     //Add callback for accept and cancel buttons
  35.     SetEntityPlayerInteractCallback("ScriptArea_Accept","cbButtonEnter",false);
  36.     SetEntityPlayerInteractCallback("ScriptArea_Cancel","cbButtonCancel",false);                   
  37. }
  38.  
  39. //Returns a random password of a given length
  40. string GetRandomPassword(uint passwordLength)
  41. {
  42.     //Password to be generated
  43.     string password = "";
  44.    
  45.     //Calculate the maximum integer to be randomly selected. Error if there are no chars to choose from!
  46.     int maxChar = int(VALID_CHARS.length())-1;
  47.     if(maxChar < 0) { AddDebugMessage("No VALID_CHARS specified!",false); return "";}
  48.    
  49.     //Generate a password until it satisfies the length criteria and then return it.
  50.     while(password.length() < passwordLength)
  51.     {
  52.         password += VALID_CHARS[RandInt(0,maxChar)];
  53.     }
  54.    
  55.     return password;
  56. }
  57.  
  58. //Callback triggered when a button is pressed
  59. void cbButtonPress(string &in asButtonArea)
  60. {
  61.     /* TODO HERE: Any sounds or effects for pressing the button! */
  62.    
  63.     //Split the button area using _ so you have "ScriptArea" and "<ID>"
  64.     string[] buttonSplit = stringSplit(asButtonArea,"_");
  65.     //Button name is fine so get the Button character (Second array element if ScriptArea_<ID>)
  66.     string buttonchar = buttonSplit[1];
  67.    
  68.     //Get the max password length (This is just the length of the target password);
  69.     uint maxPasswordLength = GetLocalVarString("TargetPassword").length();
  70.     //Determine the length of the new password
  71.     uint newPasswordLength = GetLocalVarString("InputPassword").length() + buttonchar.length();
  72.    
  73.     //Accept the button press if the new password is valid
  74.     if(newPasswordLength <= maxPasswordLength)
  75.      {
  76.         //Player has entered password was fine in length, add this character to the password.
  77.         AddLocalVarString("InputPassword",buttonchar);
  78.      }
  79.     else
  80.      {
  81.         //Player has entered a password that was too long - clear the password and enter (reject)
  82.         SetLocalVarString("InputPassword","");
  83.         cbButtonEnter("");
  84.      }
  85. }
  86.  
  87.  
  88. //Callback for if the player has pressed the "enter"/"Accept" button.
  89. void cbButtonEnter(string &in asButtonArea)
  90. {
  91.     //Is the target password the same as the input password?
  92.     if(GetLocalVarString("TargetPassword") == GetLocalVarString("InputPassword"))
  93.     {
  94.         /* Todo: Your code for if the player has got the password correct! (E.g Unlock/Open door) */
  95.        
  96.         //De-activate all the buttons
  97.         for(uint i=0; i<VALID_CHARS.length(); i++) SetEntityActive("ScriptArea_" + VALID_CHARS[i],false);
  98.         SetEntityActive("ScriptArea_Accept",false);
  99.         SetEntityActive("ScriptArea_Cancel",false);
  100.     }
  101.     else
  102.     {
  103.         /* Todo: Some code for if the player got the password wrong! */
  104.     }
  105.    
  106.     //Reset the input password
  107.     SetLocalVarString("InputPassword","");
  108. }
  109.  
  110. //Callback for if the player has pressed the "cancel" button
  111. void cbButtonCancel(string &in asButtonArea)
  112. {
  113.     SetLocalVarString("InputPassword","");
  114. }
  115.  
  116. //Helper Function: This will split a string using a specified delimiter.
  117. //E.g. StringSplit("Hello_World","_");
  118. //     returns an array containing "Hello" and "World"
  119. string[] stringSplit(string asString, string asDelim)
  120.  {
  121.   string[] output = {""};
  122.   //For each character in the input, check against the output.
  123.   int s1 = int(asString.length()); int s2 = int(asDelim.length());
  124.   int lastMatch = 0;
  125.   //Add all but final elements
  126.   for(int i=0; i<=s1 - s2; i++)
  127.    {
  128.     if(StringSub(asString,i,s2) == asDelim)
  129.      {
  130.       //Add element to output    
  131.       output[output.length() - 1] = StringSub(asString,lastMatch,i-lastMatch);
  132.       output.resize(output.length() + 1);
  133.       //Move search along
  134.       i += s2;
  135.       lastMatch = i;
  136.      }
  137.    }
  138.   //Add lastMatch -> final
  139.   output[output.length() - 1] = StringSub(asString,lastMatch,s1 - lastMatch);
  140.   return output;
  141.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement