Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Valid button characters (I.e. Characters for which an area exists)
- string[] VALID_CHARS = {
- "0","1","2","3",
- "4","5","6","7",
- "8","9","A","B",
- "C","D","E","F",
- "#"};
- //Called when the map starts
- void OnStart()
- {
- SetupButtons();
- //Say the password if you are testing your map!
- if(ScriptDebugOn())
- AddDebugMessage("Password is: " + GetLocalVarString("TargetPassword"),false);
- }
- //Call this to setup the buttons
- void SetupButtons()
- {
- //Determine what the password should be at random
- SetLocalVarString("InputPassword","");
- SetLocalVarString("TargetPassword",GetRandomPassword(4));
- //Add an interact callback for each button
- for(uint i=0; i<VALID_CHARS.length(); i++)
- {
- //For each valid button character, get it's script area & add the use callback
- string area = "ScriptArea_" + VALID_CHARS[i];
- SetEntityPlayerInteractCallback(area,"cbButtonPress",false);
- }
- //Add callback for accept and cancel buttons
- SetEntityPlayerInteractCallback("ScriptArea_Accept","cbButtonEnter",false);
- SetEntityPlayerInteractCallback("ScriptArea_Cancel","cbButtonCancel",false);
- }
- //Returns a random password of a given length
- string GetRandomPassword(uint passwordLength)
- {
- //Password to be generated
- string password = "";
- //Calculate the maximum integer to be randomly selected. Error if there are no chars to choose from!
- int maxChar = int(VALID_CHARS.length())-1;
- if(maxChar < 0) { AddDebugMessage("No VALID_CHARS specified!",false); return "";}
- //Generate a password until it satisfies the length criteria and then return it.
- while(password.length() < passwordLength)
- {
- password += VALID_CHARS[RandInt(0,maxChar)];
- }
- return password;
- }
- //Callback triggered when a button is pressed
- void cbButtonPress(string &in asButtonArea)
- {
- /* TODO HERE: Any sounds or effects for pressing the button! */
- //Split the button area using _ so you have "ScriptArea" and "<ID>"
- string[] buttonSplit = stringSplit(asButtonArea,"_");
- //Button name is fine so get the Button character (Second array element if ScriptArea_<ID>)
- string buttonchar = buttonSplit[1];
- //Get the max password length (This is just the length of the target password);
- uint maxPasswordLength = GetLocalVarString("TargetPassword").length();
- //Determine the length of the new password
- uint newPasswordLength = GetLocalVarString("InputPassword").length() + buttonchar.length();
- //Accept the button press if the new password is valid
- if(newPasswordLength <= maxPasswordLength)
- {
- //Player has entered password was fine in length, add this character to the password.
- AddLocalVarString("InputPassword",buttonchar);
- }
- else
- {
- //Player has entered a password that was too long - clear the password and enter (reject)
- SetLocalVarString("InputPassword","");
- cbButtonEnter("");
- }
- }
- //Callback for if the player has pressed the "enter"/"Accept" button.
- void cbButtonEnter(string &in asButtonArea)
- {
- //Is the target password the same as the input password?
- if(GetLocalVarString("TargetPassword") == GetLocalVarString("InputPassword"))
- {
- /* Todo: Your code for if the player has got the password correct! (E.g Unlock/Open door) */
- //De-activate all the buttons
- for(uint i=0; i<VALID_CHARS.length(); i++) SetEntityActive("ScriptArea_" + VALID_CHARS[i],false);
- SetEntityActive("ScriptArea_Accept",false);
- SetEntityActive("ScriptArea_Cancel",false);
- }
- else
- {
- /* Todo: Some code for if the player got the password wrong! */
- }
- //Reset the input password
- SetLocalVarString("InputPassword","");
- }
- //Callback for if the player has pressed the "cancel" button
- void cbButtonCancel(string &in asButtonArea)
- {
- SetLocalVarString("InputPassword","");
- }
- //Helper Function: This will split a string using a specified delimiter.
- //E.g. StringSplit("Hello_World","_");
- // returns an array containing "Hello" and "World"
- string[] stringSplit(string asString, string asDelim)
- {
- string[] output = {""};
- //For each character in the input, check against the output.
- int s1 = int(asString.length()); int s2 = int(asDelim.length());
- int lastMatch = 0;
- //Add all but final elements
- for(int i=0; i<=s1 - s2; i++)
- {
- if(StringSub(asString,i,s2) == asDelim)
- {
- //Add element to output
- output[output.length() - 1] = StringSub(asString,lastMatch,i-lastMatch);
- output.resize(output.length() + 1);
- //Move search along
- i += s2;
- lastMatch = i;
- }
- }
- //Add lastMatch -> final
- output[output.length() - 1] = StringSub(asString,lastMatch,s1 - lastMatch);
- return output;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement