Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Counter_System.nut
  3. //
  4. // Version:
  5. // 1.0
  6. //
  7. // Modified:
  8. // August 29, 2010
  9. //
  10. // Authors:
  11. // Seung Hahm aka Juicebox360 <logarithmmm@gmail.com>
  12. // Cris Crawford aka 8e8 <chris.j.crawford@hotmail.com>
  13. // Jason Spafford aka NullSoldier <nullprogrammer@gmail.com>
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. // PURPOSE: This system serves as scoring/monetary handler for any HL2 game
  33. ///////////////////////////////////////////////////////////////////////////
  34.  
  35.  
  36. // Initialize the Counter Sytem
  37. function InitCounterSystem()
  38. {
  39.  
  40. // Initialize Classes
  41. ::functions <- Functions();
  42. ::display <- Display();
  43. ::shop <- Shop();
  44.  
  45. functions.toConsole("Initializing Counter System...");
  46. functions.toConsole("Initializing User Variables...");
  47.  
  48. // User Defined Variables
  49. //////////////////////////////////////////////////
  50. ::value <- 0; // Value to add
  51. ::operation <- 0; // Operation to perform(0 - Add, 1 - Subtract, 2 - Multiply, 3 - Divide)
  52. // Display related //
  53. ::digitName <- "digit"; // Prefix of display digits
  54. ::counterName <- "team"; // Prefix of counter index
  55. // Spawn related //
  56. ::spawnName <- "dispenser_spawn"; // Prefix of env_entitiy_maker
  57. ::soundName <- "dispenser_deny"; // Prefix of ambient_generic
  58. ::templateName <- "template_item"; // Prefix of point_template
  59. // Shop related //
  60. ::item <- 0; // Item ID#
  61. // Misc //
  62. ::developer <- 1; // Enable debugging
  63. //////////////////////////////////////////////////
  64.  
  65. functions.toConsole("User Variables Initialized!");
  66. functions.toConsole("Initializing Counter Objects...");
  67.  
  68. // Initialize counter objects
  69. // ADD COUNTERS HERE
  70. ::counter <- [];
  71. counter.append(0);
  72. counter.append(Counter(0, 0, 999)); // Create counter[1] with default value of 0, minimum value of 0, and a maximum value of 999
  73.  
  74. functions.toConsole("Counter Objects Initialized!");
  75. functions.toConsole("Counter System Initialized!");
  76. }
  77.  
  78. /////////////////
  79. // CLASS: Counter
  80. class Counter
  81. {
  82. constructor(counter_def, counter_min, counter_max)
  83. { // Initialize counter
  84. functions.toConsole("Creating Counter Object...");
  85.  
  86. local defaultValue = counter_def;
  87. functions.toConsole("defaultValue = " + defaultValue);
  88. local minimumValue = counter_min;
  89. functions.toConsole("minimumValue = " + minimumValue);
  90. local maximumValue = counter_max;
  91. functions.toConsole("maximumValue = " + maximumValue);
  92. local currentValue = defaultValue;
  93. functions.toConsole("currentValue = " + currentValue);
  94. // Set maximum digits for counter display
  95. local digits = maximumValue.tostring().len();
  96. functions.toConsole("digits = " + digits);
  97. functions.toConsole("Counter Object Created!");
  98. }
  99.  
  100. // Perform operation on counter
  101. function DoOperation ()
  102. {
  103. // Determine operation to perform
  104. switch(operation)
  105. {
  106. case 0: // Add
  107. currentValue = Clamp(currentValue + value, minimumValue, maximumValue);
  108. break;
  109. case 1: // Subtract
  110. currentValue = Clamp(currentValue - value, minimumValue, maximumValue);
  111. break;
  112. case 2: // Multiply
  113. currentValue = Clamp(currentValue * value, minimumValue, maximumValue);
  114. break;
  115. case 3: // Divide
  116. currentValue = Clamp(currentValue / value, minimumValue, maximumValue);
  117. break;
  118. }
  119. // Update display
  120. display.SetDisplay(v)
  121. }
  122.  
  123. // Reset the counter to the default value
  124. function Reset()
  125. {
  126. currentValue = defaultValue;
  127.  
  128. toConsole("Value: " + defaultValue);
  129. }
  130.  
  131. // Constrain counter on operation
  132. function Clamp (v)
  133. {
  134. if(v < minimumValue)
  135. {
  136. toConsole("Value below legal limit, set to minimum value: " + minimumValue);
  137. return minimumValue;
  138. }
  139. else if(v > maximumValue)
  140. {
  141. toConsole("Value above legal limit, set to maximum value: " + maximumValue);
  142. return maximumValue;
  143. }
  144. else
  145. return v;
  146. }
  147. }
  148.  
  149. /////////////////
  150. // CLASS: Display
  151. class Display
  152. {
  153. constructor()
  154. {
  155. // Initialize Display class
  156. }
  157.  
  158. // Set the LCD display for counter v
  159. function SetDisplay(v)
  160. {
  161. placeValue <- 0;
  162. localValue <- counter[v].currentValue;
  163.  
  164. for (p <- 1; p <= counter[v].digits; p++)
  165. {
  166. placeValue = localValue % 10;
  167. localValue = (localValue - placeValue) / 10;
  168.  
  169. toConsole("Place: " + p + " :: Value: " + placeValue);
  170.  
  171. switch (placeValue)
  172. {
  173. case 0:
  174. SetBrushState(v, p, [1, 2, 3, 4, 5, 6]);
  175. break;
  176. case 1:
  177. SetBrushState(v, p, [2, 3]);
  178. break;
  179. case 2:
  180. SetBrushState(v, p, [1, 2, 4, 5, 7]);
  181. break;
  182. case 3:
  183. SetBrushState(v, p, [1, 2, 3, 4, 7]);
  184. break;;
  185. case 4:
  186. SetBrushState(v, p, [2, 3, 6, 7]);
  187. break;
  188. case 5:
  189. SetBrushState(v, p, [1, 3, 4, 6, 7]);
  190. break;
  191. case 6:
  192. SetBrushState(v, p, [1, 3, 4, 5, 6, 7]);
  193. break;
  194. case 7:
  195. SetBrushState(v, p, [1, 2, 3]);
  196. break;
  197. case 8:
  198. SetBrushState(v, p, [1, 2, 3, 4, 5, 6, 7]);
  199. break;
  200. case 9:
  201. SetBrushState(v, p, [1, 2, 3, 4, 6, 7]);
  202. break;
  203. }
  204. }
  205. }
  206.  
  207. // Enables or disables the brushes to display digit v
  208. function SetBrushState(v, place, brushes)
  209. {
  210. for (b <- 1; b <= 7; b++)
  211. {
  212. enable <- 0;
  213. brush <- digitName + "_" + place + "_" + b + "_" + counterName + v;
  214.  
  215. foreach (key, id in brushes)
  216. {
  217. if (b == id)
  218. enable <- 1;
  219. }
  220.  
  221. if (enable)
  222. EntFire(brush, "Enable");
  223. else
  224. EntFire(brush, "Disable");
  225. }
  226. }
  227. }
  228. //////////////
  229. // CLASS: Shop
  230. class Shop
  231. {
  232. // Compares counter[v]'s value to the price of the item
  233. function CheckFunds(v)
  234. {
  235. if (counter[v].currentValue >= value)
  236. return true; // Sufficient funds
  237. else
  238. return false; // Insufficient funds
  239. }
  240.  
  241. // Set's the current shop item's ID and price
  242. function SetItem(id, price)
  243. {
  244. item <- id;
  245. functions.SetValue(price);
  246.  
  247. functions.toConsole("Item: " + id + " :: Price: " + price);
  248. }
  249.  
  250. // Buy's current item
  251. function Buy(v)
  252. {
  253. if (CheckFunds(v))
  254. { // Deduct funds & Spawn item
  255. counter[v].doOperation();
  256. EntFire(spawnName + v, "AddOutput", "EntityTemplate " + templateName + item);
  257. EntFire(spawnName + v, "ForceSpawn", 0);
  258.  
  259. functions.toConsole("Item: " + templateName + item + ":: Spawn Location: " + spawnName + v);
  260. }
  261. else
  262. { // Denied Sound
  263. EntFire(sound + i, "PlaySound", 0);
  264.  
  265. functions.toConsole("Insufficient funds for Item: " + templateName + item);
  266. }
  267. }
  268. }
  269.  
  270. ///////////////////
  271. // CLASS: Functions
  272. class Functions
  273. {
  274. constructor()
  275. {
  276. // Initialize Functions class
  277. }
  278. // Sends debugging output to console
  279. function toConsole(output)
  280. {
  281. if (developer)
  282. printl(output);
  283. }
  284.  
  285. // Sets the operation to perform on the counter
  286. function SetOperation(o)
  287. {
  288. operation = o;
  289.  
  290. functions.toConsole("Operation: " + o);
  291. }
  292. // Sets the value to used against the counter
  293. function SetValue(v)
  294. {
  295. value = v;
  296.  
  297. functions.toConsole("Value: " + v);
  298. }
  299. }
  300. // End of Counter System
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement