Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.39 KB | None | 0 0
  1. //Keypad lock script
  2. //By: Flyguy
  3.  
  4. /*
  5. Entities:
  6. EntityGroup[0-9] = Keys 0-9
  7. EntityGroup[10] = Reset
  8. EntityGroup[11-14] = Display digits
  9. EntityGroup[15] = Papers
  10.  
  11. Outputs:
  12. OnUser1 = Correct code
  13. OnUser2 = Wrong code
  14. */
  15.  
  16. //Constants
  17. NUM_KEYS <- 10; //Number of numeric keys on the keypad.
  18. CODE_LENGTH <- 4; //Number of digits in the code.
  19. KEY_TIMEOUT <- 5.0; //Reset after this many seconds if code isn't complete.
  20. VALIDATION_DELAY <- 0.3; //Time to wait before validating a code.
  21. THINK_INTERVAL <- 0.1;
  22. PAPER_WILDCARD <- "keypad_paper*";
  23.  
  24. //Keypad keys.
  25. keys <- array(NUM_KEYS);
  26. resetKey <- EntityGroup[10];
  27.  
  28. //Display digits
  29. digits <- array(CODE_LENGTH);
  30.  
  31. //Hint papers.
  32. papers <- array(CODE_LENGTH);
  33.  
  34. validating <- false;
  35. validationTime <- 0.0;
  36. keyTime <- 0.0;
  37. curDigit <- 0;
  38. enteredCode <- array(CODE_LENGTH);
  39.  
  40. validCode <- array(CODE_LENGTH);
  41.  
  42. function OnPostSpawn()
  43. {
  44. for(local i = 0;i < NUM_KEYS;i++)
  45. {
  46. keys[i] = EntityGroup[i];
  47. }
  48. for(local i = 0;i < CODE_LENGTH;i++)
  49. {
  50. digits[i] = EntityGroup[i + 11];
  51. }
  52.  
  53. //Setup key inputs.
  54. for(local i = 0;i < NUM_KEYS;i++)
  55. {
  56. keys[i].ValidateScriptScope();
  57.  
  58. local scope = keys[i].GetScriptScope();
  59.  
  60. scope["Keypad"] <- this;
  61. scope["KeyNum"] <- i;
  62. scope["InputUse"] <- function()
  63. {
  64. Keypad.KeyPressed(KeyNum); return true;
  65. }
  66. }
  67.  
  68. //Setup reset key
  69. resetKey.ValidateScriptScope();
  70.  
  71. local scope = resetKey.GetScriptScope();
  72.  
  73. scope["Keypad"] <- this;
  74. scope["InputUse"] <- function()
  75. {
  76. Keypad.Reset(); return true;
  77. }
  78.  
  79. //Find the papers
  80. local paper = null;
  81. for(local i = 0; paper = Entities.FindByName(paper, PAPER_WILDCARD); i++)
  82. {
  83. papers[i] = paper;
  84. }
  85.  
  86. //Blanks the display (will show garbage otherwise).
  87. Reset();
  88.  
  89. //Generate a random code.
  90. GenerateCode();
  91. }
  92.  
  93. function Think()
  94. {
  95. //Reset after timeout
  96. if(Time() > keyTime + KEY_TIMEOUT && curDigit > 0)
  97. {
  98. Reset();
  99. }
  100.  
  101. //If the validating state is true, wait for the validation time.
  102. if(validating && Time() >= validationTime)
  103. {
  104. CheckCode();
  105. Reset();
  106.  
  107. validating = false;
  108. }
  109.  
  110. return THINK_INTERVAL;
  111. }
  112.  
  113. //Called when a numeric key is pressed (key = key number)
  114. function KeyPressed(key)
  115. {
  116. if(!validating)
  117. {
  118. //Set the current code digit.
  119. enteredCode[curDigit] = key;
  120.  
  121. //Set the current display digit.
  122. EntFireByHandle(digits[curDigit], "SetMaterialVar", "" + key, 0, null, null);
  123.  
  124. if(curDigit == CODE_LENGTH - 1)
  125. {
  126. //Set validation state and delay upon reaching last digit.
  127. validationTime = Time() + VALIDATION_DELAY;
  128. validating = true;
  129. }
  130. else
  131. {
  132. //Increment the digit position.
  133. curDigit++
  134. }
  135.  
  136. //Set the last time a key was pressed (for timeout)
  137. keyTime = Time();
  138. }
  139. }
  140.  
  141. //Checks if the entered code and valid code match and fires the relevant outputs.
  142. function CheckCode()
  143. {
  144. local passed = true;
  145.  
  146. for(local i = 0;i < CODE_LENGTH;i++)
  147. {
  148. if(enteredCode[i] != validCode[i])
  149. {
  150. passed = false;
  151. break;
  152. }
  153. }
  154.  
  155. if(passed)
  156. {
  157. EntFireByHandle(self, "FireUser1", "", 0.0, null, null);
  158. }
  159. else
  160. {
  161. EntFireByHandle(self, "FireUser2", "", 0.0, null, null);
  162. }
  163. }
  164.  
  165. //Resets the digit index, entered code, and display.
  166. function Reset()
  167. {
  168. curDigit = 0;
  169. enteredCode = array(CODE_LENGTH);
  170.  
  171. for(local i = 0;i < CODE_LENGTH;i++)
  172. {
  173. EntFireByHandle(digits[i], "SetMaterialVar", "10", 0, null, null);;
  174. }
  175. }
  176.  
  177. //Generates a random code with no repeating digits.
  178. function GenerateCode()
  179. {
  180. validCode = array(CODE_LENGTH, -1);
  181.  
  182. for(local i = 0;i < CODE_LENGTH;i++)
  183. {
  184. local foundDigit = false;
  185. local digit = 0;
  186.  
  187. //Ensure each digit is unique.
  188. while(!foundDigit)
  189. {
  190. //Get a random digit.
  191. digit = RandomInt(0, 9);
  192.  
  193. //Check if the digit is already in the code.
  194. foundDigit = true;
  195. for(local i = 0;i < CODE_LENGTH;i++)
  196. {
  197. if(validCode[i] == digit)
  198. {
  199. foundDigit = false;
  200. break;
  201. }
  202. }
  203. }
  204.  
  205. validCode[i] = digit;
  206. }
  207.  
  208. ShufflePapers();
  209. }
  210.  
  211. //Shuffles the papers and sets their skins.
  212. function ShufflePapers()
  213. {
  214. local maxIdx = CODE_LENGTH - 1;
  215. local temp = clone validCode;
  216.  
  217. for(local i = 0;i < CODE_LENGTH;i++)
  218. {
  219. local idx = RandomInt(0, maxIdx);
  220.  
  221. local skin = (temp[idx] + 9) % 10;
  222. EntFireByHandle(papers[i], "skin", "" + skin, 0.0, null, null);
  223.  
  224. temp.remove(idx);
  225. maxIdx--;
  226.  
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement