Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Keypad lock script
- //By: Flyguy
- /*
- Entities:
- EntityGroup[0-9] = Keys 0-9
- EntityGroup[10] = Reset
- EntityGroup[11-14] = Display digits
- EntityGroup[15] = Papers
- Outputs:
- OnUser1 = Correct code
- OnUser2 = Wrong code
- */
- //Constants
- NUM_KEYS <- 10; //Number of numeric keys on the keypad.
- CODE_LENGTH <- 4; //Number of digits in the code.
- KEY_TIMEOUT <- 5.0; //Reset after this many seconds if code isn't complete.
- VALIDATION_DELAY <- 0.3; //Time to wait before validating a code.
- THINK_INTERVAL <- 0.1;
- PAPER_WILDCARD <- "keypad_paper*";
- //Keypad keys.
- keys <- array(NUM_KEYS);
- resetKey <- EntityGroup[10];
- //Display digits
- digits <- array(CODE_LENGTH);
- //Hint papers.
- papers <- array(CODE_LENGTH);
- validating <- false;
- validationTime <- 0.0;
- keyTime <- 0.0;
- curDigit <- 0;
- enteredCode <- array(CODE_LENGTH);
- validCode <- array(CODE_LENGTH);
- function OnPostSpawn()
- {
- for(local i = 0;i < NUM_KEYS;i++)
- {
- keys[i] = EntityGroup[i];
- }
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- digits[i] = EntityGroup[i + 11];
- }
- //Setup key inputs.
- for(local i = 0;i < NUM_KEYS;i++)
- {
- keys[i].ValidateScriptScope();
- local scope = keys[i].GetScriptScope();
- scope["Keypad"] <- this;
- scope["KeyNum"] <- i;
- scope["InputUse"] <- function()
- {
- Keypad.KeyPressed(KeyNum); return true;
- }
- }
- //Setup reset key
- resetKey.ValidateScriptScope();
- local scope = resetKey.GetScriptScope();
- scope["Keypad"] <- this;
- scope["InputUse"] <- function()
- {
- Keypad.Reset(); return true;
- }
- //Find the papers
- local paper = null;
- for(local i = 0; paper = Entities.FindByName(paper, PAPER_WILDCARD); i++)
- {
- papers[i] = paper;
- }
- //Blanks the display (will show garbage otherwise).
- Reset();
- //Generate a random code.
- GenerateCode();
- }
- function Think()
- {
- //Reset after timeout
- if(Time() > keyTime + KEY_TIMEOUT && curDigit > 0)
- {
- Reset();
- }
- //If the validating state is true, wait for the validation time.
- if(validating && Time() >= validationTime)
- {
- CheckCode();
- Reset();
- validating = false;
- }
- return THINK_INTERVAL;
- }
- //Called when a numeric key is pressed (key = key number)
- function KeyPressed(key)
- {
- if(!validating)
- {
- //Set the current code digit.
- enteredCode[curDigit] = key;
- //Set the current display digit.
- EntFireByHandle(digits[curDigit], "SetMaterialVar", "" + key, 0, null, null);
- if(curDigit == CODE_LENGTH - 1)
- {
- //Set validation state and delay upon reaching last digit.
- validationTime = Time() + VALIDATION_DELAY;
- validating = true;
- }
- else
- {
- //Increment the digit position.
- curDigit++
- }
- //Set the last time a key was pressed (for timeout)
- keyTime = Time();
- }
- }
- //Checks if the entered code and valid code match and fires the relevant outputs.
- function CheckCode()
- {
- local passed = true;
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- if(enteredCode[i] != validCode[i])
- {
- passed = false;
- break;
- }
- }
- if(passed)
- {
- EntFireByHandle(self, "FireUser1", "", 0.0, null, null);
- }
- else
- {
- EntFireByHandle(self, "FireUser2", "", 0.0, null, null);
- }
- }
- //Resets the digit index, entered code, and display.
- function Reset()
- {
- curDigit = 0;
- enteredCode = array(CODE_LENGTH);
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- EntFireByHandle(digits[i], "SetMaterialVar", "10", 0, null, null);;
- }
- }
- //Generates a random code with no repeating digits.
- function GenerateCode()
- {
- validCode = array(CODE_LENGTH, -1);
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- local foundDigit = false;
- local digit = 0;
- //Ensure each digit is unique.
- while(!foundDigit)
- {
- //Get a random digit.
- digit = RandomInt(0, 9);
- //Check if the digit is already in the code.
- foundDigit = true;
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- if(validCode[i] == digit)
- {
- foundDigit = false;
- break;
- }
- }
- }
- validCode[i] = digit;
- }
- ShufflePapers();
- }
- //Shuffles the papers and sets their skins.
- function ShufflePapers()
- {
- local maxIdx = CODE_LENGTH - 1;
- local temp = clone validCode;
- for(local i = 0;i < CODE_LENGTH;i++)
- {
- local idx = RandomInt(0, maxIdx);
- local skin = (temp[idx] + 9) % 10;
- EntFireByHandle(papers[i], "skin", "" + skin, 0.0, null, null);
- temp.remove(idx);
- maxIdx--;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement