Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.65 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////
  2. // Counter_System.nut
  3. //
  4. // Version:
  5. // 1.0
  6. //
  7. // Modified:
  8. // August 30, 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
  33. // Left 4 Dead 2, and Aliens Swarm: Infestation
  34. ///////////////////////////////////////////////////////////////////////////
  35.  
  36.  
  37. // Initialize the Counter Sytem
  38. function InitCounterSystem()
  39. {
  40. functions.toConsole("Initializing Counter System...");
  41.  
  42. // Initialize Globals
  43. ::operation <- 0; // Operation to perform(0 - Add | 1 - Subtract | 2 - Multiply | 3 - Divide)
  44. ::value <- 0; // Value to perform operation on
  45. ::item <- 0; // Item ID#
  46. ::playersInGame <- 0; // Players in the game
  47. // Initialize Classes
  48. ::functions <- Functions(); // Functions handler
  49. ::display <- Display(); // Display handler
  50. ::shop <- Shop(); // Shop handler
  51. // Initialize Arrays
  52. ::counter <- []; // Create the counter object array
  53. counter.append(0); // and an empty key
  54. ::players <- []; // Create the players array
  55. ::characters<- [ // Create the characters array
  56. "sarge",
  57. "wildcat",
  58. "faith",
  59. "crash",
  60. "jaeger",
  61. "wolfe",
  62. "bastille",
  63. "vegas"
  64. ];
  65.  
  66. // User Defined Variables ////////////////
  67. ::developer <- 1; // (0 - Disable | 1 - Enable) debugging
  68. ::digitPrefix <- "digit"; // Prefix of display digits
  69. ::counterPrefix <- "team"; // Prefix of counter index
  70. ::spawnPrefix <- "dispenser_spawn"; // Prefix of env_entitiy_maker
  71. ::soundPrefix <- "dispenser_deny"; // Prefix of ambient_generic
  72. ::templatePrefix<- "template_item"; // Prefix of point_template
  73. //////////////////////////////////////////
  74.  
  75. functions.toConsole("User Variables Initialized!");
  76. functions.toConsole("Initializing Counter Objects...");
  77.  
  78. // Initialize counter objects
  79. // ADD COUNTERS HERE
  80. counter.append(Counter(1, 0, 0, 999, 1)); // Creates counter[1], id = 1, default = 0, minimum = 0, maximum = 999, multiplier = 1
  81. counter.append(Counter(2, 0, 0, 999, 1)); // Create counter[2], id = 1, default = 0, minimum = 0, maximum = 999, multiplier = 1
  82.  
  83. functions.toConsole("Counter Objects Initialized!");
  84. functions.toConsole("Counter System Initialized!");
  85. }
  86.  
  87. /////////////////
  88. // CLASS: Counter
  89. class Counter
  90. {
  91. counterId = 0;
  92. defaultValue = 0;
  93. minimumValue = 0;
  94. maximumValue = 0;
  95. currentValue = 0;
  96. multiplierValue = 1;
  97. digits = 0;
  98.  
  99. constructor(counter_id, counter_def, counter_min, counter_max, counter_multi)
  100. {
  101. functions.toConsole("Creating Counter Object...");
  102. //
  103. counterId = counter_id;
  104. defaultValue = counter_def;
  105. minimumValue = counter_min;
  106. maximumValue = counter_max;
  107. currentValue = defaultValue;
  108. multiplierValue = counter_multi;
  109. digits = maximumValue.tostring().len();
  110.  
  111. // Output counter to console
  112. functions.toConsole("counterId = " + counterId);
  113. functions.toConsole("Counter[" + counterId + "] defaultValue = " + defaultValue);
  114. functions.toConsole("Counter[" + counterId + "] minimumValue = " + minimumValue);
  115. functions.toConsole("Counter[" + counterId + "] maximumValue = " + maximumValue);
  116. functions.toConsole("Counter[" + counterId + "] currentValue = " + currentValue);
  117. functions.toConsole("Counter[" + counterId + "] multiplierValue = " + multiplierValue);
  118. functions.toConsole("Counter[" + counterId + "] digits = " + digits);
  119. //
  120. functions.toConsole("Counter Object Created!");
  121. }
  122.  
  123. // Perform operation on counter
  124. function DoOperation()
  125. {
  126. local v = currentValue;
  127. // Determine operation to perform
  128. switch(operation)
  129. {
  130. case 0: // Add
  131. currentValue = Clamp(v + (value * multiplierValue));
  132. functions.toConsole("Counter[" + counterId + "] " + v + "(+" + value + ") = " + currentValue);
  133. break;
  134. case 1: // Subtract
  135. currentValue = Clamp(currentValue - (value * multiplierValue));
  136. functions.toConsole("Counter[" + counterId + "] " + v + "(-" + value + ") = " + currentValue);
  137. break;
  138. case 2: // Multiply
  139. currentValue = Clamp(currentValue * (value * multiplierValue));
  140. functions.toConsole("Counter[" + counterId + "] " + v + "(*" + value + ") = " + currentValue);
  141. break;
  142. case 3: // Divide
  143. currentValue = Clamp(currentValue / (value * multiplierValue));
  144. functions.toConsole("Counter[" + counterId + "] " + v + "(/" + value + ") = " + currentValue);
  145. break;
  146. }
  147. // Update display
  148. display.SetDisplay(counterId);
  149. }
  150. // Sets the counter to v
  151. function SetCounter(v)
  152. {
  153. currentValue = v;
  154. functions.toConsole("Counter[" + counterId + "] Value: " + v);
  155. // Update display
  156. display.SetDisplay(counterId);
  157. }
  158. // Reset the counter to the default value
  159. function Reset()
  160. {
  161. currentValue = defaultValue;
  162.  
  163. functions.toConsole("Counter[" + counterId + "] Value: " + defaultValue);
  164. // Update display
  165. display.SetDisplay(counterId);
  166. }
  167.  
  168. // Sets the counter's multiplier value
  169. function SetMultiplier(v)
  170. {
  171. multiplierValue = v;
  172.  
  173. functions.toConsole("Counter[" + counterId + "] Multiplier: " + v);
  174. }
  175.  
  176. // Constrain counter on operation
  177. function Clamp (v)
  178. {
  179. if(v < minimumValue)
  180. {
  181. functions.toConsole("Counter[" + counterId + "] Value below legal limit, set to minimum value: " + minimumValue);
  182. return minimumValue;
  183. }
  184. else if(v > maximumValue)
  185. {
  186. functions.toConsole("Counter[" + counterId + "] Value above legal limit, set to maximum value: " + maximumValue);
  187. return maximumValue;
  188. }
  189. else
  190. {
  191. functions.toConsole("Counter[" + counterId + "] No clamping has been performed on value: " + v);
  192. return v;
  193. }
  194. }
  195. }
  196.  
  197. /////////////////
  198. // CLASS: Display
  199. class Display
  200. {
  201. constructor()
  202. {
  203. // Initialize Display class
  204. }
  205.  
  206. // Set the LCD display for counter id
  207. function SetDisplay(id)
  208. {
  209. local placeValue = 0;
  210. local v = counter[id].currentValue;
  211.  
  212. // For each digit
  213. for (local place = 1; place <= counter[id].digits; p++)
  214. {
  215. // Determine placeValue of digit place
  216. placeValue = v % 10;
  217. v = (v - placeValue) / 10;
  218.  
  219. functions.toConsole("Place: " + place + " :: Value: " + placeValue);
  220.  
  221. // Enable/Disable brushes to display placeValue on digit place
  222. switch (placeValue)
  223. {
  224. case 0:
  225. SetBrushState(id, place, [1, 2, 3, 4, 5, 6]);
  226. break;
  227. case 1:
  228. SetBrushState(id, place, [2, 3]);
  229. break;
  230. case 2:
  231. SetBrushState(id, place, [1, 2, 4, 5, 7]);
  232. break;
  233. case 3:
  234. SetBrushState(id, place, [1, 2, 3, 4, 7]);
  235. break;;
  236. case 4:
  237. SetBrushState(id, place, [2, 3, 6, 7]);
  238. break;
  239. case 5:
  240. SetBrushState(id, place, [1, 3, 4, 6, 7]);
  241. break;
  242. case 6:
  243. SetBrushState(id, place, [1, 3, 4, 5, 6, 7]);
  244. break;
  245. case 7:
  246. SetBrushState(id, place, [1, 2, 3]);
  247. break;
  248. case 8:
  249. SetBrushState(id, place, [1, 2, 3, 4, 5, 6, 7]);
  250. break;
  251. case 9:
  252. SetBrushState(id, place, [1, 2, 3, 4, 6, 7]);
  253. break;
  254. }
  255. }
  256. }
  257.  
  258. // Enables or disables the brushes to display digit place
  259. function SetBrushState(id, place, brushes)
  260. {
  261. for (local b = 1; b <= 7; b++)
  262. {
  263. local enable = 0;
  264. local brush = digitPrefix + "_" + place + "_" + b + "_" + counterPrefix + id;
  265. // Is b in the array?
  266. foreach (key, val in brushes)
  267. {
  268. if (b == val)
  269. enable = 1;
  270. }
  271. if (enable)
  272. EntFire(brush, "Enable"); // Yes, enable brush b
  273. else
  274. EntFire(brush, "Disable");// No, disable brush b
  275. }
  276. }
  277. }
  278. //////////////
  279. // CLASS: Shop
  280. class Shop
  281. {
  282. // Compares counter[id]'s value to the price of the item
  283. function CheckFunds(id)
  284. {
  285. if (counter[id].currentValue >= value)
  286. return true; // Sufficient funds
  287. else
  288. return false; // Insufficient funds
  289. }
  290.  
  291. // Set's the current shop item's ID and price
  292. function SetItem(id, price)
  293. {
  294. item = id;
  295. functions.SetValue(price);
  296.  
  297. functions.toConsole("Item: " + id + " :: Price: " + price);
  298. }
  299.  
  300. // Buy's current item
  301. function Buy(id)
  302. {
  303. if (CheckFunds(id))
  304. { // Deduct funds & Spawn item
  305. counter[id].DoOperation();
  306. EntFire(spawnPrefix + id, "AddOutput", "EntityTemplate " + templatePrefix + item);
  307. EntFire(spawnPrefix + id, "ForceSpawn", 0);
  308. functions.toConsole("Item: " + templatePrefix + item + ":: Spawn Location: " + spawnPrefix + id);
  309. }
  310. else
  311. { // Denied Sound
  312. EntFire(soundPrefix + id, "PlaySound", 0);
  313.  
  314. functions.toConsole("Insufficient funds for Item: " + templatePrefix + item);
  315. }
  316. }
  317. }
  318.  
  319. ///////////////////
  320. // CLASS: Functions
  321. class Functions
  322. {
  323. constructor()
  324. {
  325. // Initialize Functions class
  326. }
  327. // Sends debugging output to console
  328. function toConsole(output)
  329. {
  330. if (developer)
  331. printl(output);
  332. }
  333.  
  334. // Sets the operation to perform on the counter
  335. function SetOperation(o)
  336. {
  337. operation = o;
  338.  
  339. toConsole("Operation: " + o);
  340. }
  341. // Sets the value to used against the counter
  342. function SetValue(v)
  343. {
  344. value = v;
  345.  
  346. toConsole("Value: " + v);
  347. }
  348. // Gets players currently ingame
  349. function GetPlayers()
  350. {
  351. local character;
  352. foreach(i, n in characters)
  353. {
  354. character = Entities.FindByName(null, "#asw_name_" + n)
  355. if (character)
  356. {
  357. toConsole("player" + i + " = " + character);
  358. players.append(n = character);
  359. }
  360. }
  361. playersInGame = players.len();
  362. }
  363. // Display's player count in a hint
  364. function PlayerCount()
  365. {
  366. EntFire("hint_players", "AddOutput", "hint_caption " + playersInGame + " player(s) are currently in the game.");
  367. EntFire("hint_players", "ShowHint", 0);
  368. }
  369. }
  370. // End of Counter System
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement