foreach.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. /*----------------------------------------------------------------------------*-
  2. ===========================
  3. foreach efficient looping
  4. ===========================
  5. Description:
  6. Provides efficient looping through sparse data sets, such as connected
  7. players. Significantly improved from the original version to be a generic
  8. loop system, rather then purely a player loop system. When used for
  9. players this has constant time O(n) for number of connected players (n),
  10. unlike standard player loops which are O(MAX_PLAYERS), regardless of the
  11. actual number of connected players. Even when n is MAX_PLAYERS this is
  12. still faster.
  13. Legal:
  14. Copyright (C) 2009 Alex "Y_Less" Cole
  15. The contents of this file are subject to the Mozilla Public License Version
  16. 1.1 (the "License"); you may not use this file except in compliance with
  17. the License. You may obtain a copy of the License at
  18. http://www.mozilla.org/MPL/
  19. Software distributed under the License is distributed on an "AS IS" basis,
  20. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  21. for the specific language governing rights and limitations under the
  22. License.
  23. The Original Code is the SA:MP foreach iterator code.
  24. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  25. Version:
  26. 0.1.5
  27. Changelog:
  28. 18/12/09:
  29. Added Itter_Func2 functions for multi-dimensional iterators.
  30. Renamed foreact et al as keywords in the documentation.
  31. Changed licensing from GPL to MPL.
  32. 02/09/09:
  33. Fixed (again) for 0.3.
  34. Added free slot finding.
  35. 21/08/09:
  36. Updated to include random functions.
  37. Made entirely stand alone.
  38. Ported to 0.3 (separate version).
  39. Added automatic callback hook code.
  40. Removed debug information from stand alone version.
  41. 06/01/08:
  42. Added debug information.
  43. 09/10/07:
  44. Moved to system.
  45. 16/09/07:
  46. Added list sorting.
  47. Made this part of Y SeRver Includes, not Y Sever Includes.
  48. Made list sorting optional.
  49. Fixed version number.
  50. 08/09/07:
  51. First version.
  52. Functions:
  53. Public:
  54. OnPlayerDisconnect - Called when a player leaves to remove them.
  55. OnPlayerConnect - Called when a player connects to add them.
  56. Core:
  57. -
  58. Stock:
  59. Itter_ShowArray - Displays the contents of the array.
  60. Itter_AddInternal - Add a value to an itterator.
  61. Itter_RemoveInternal - Remove a value from an itterator.
  62. Itter_RandomInternal - Get a random item from an itterator.
  63. Itter_FreeInternal - Gets the first free slot in the itterator.
  64. Itter_InitInternal - Initialises a multi-dimensional itterator.
  65. Static:
  66. -
  67. Inline:
  68. Itter_Create - Create a new itterator value set.
  69. Itter_Add - Wraps Itter_AddInternal.
  70. Itter_Remove - Wraps Itter_RemoveInternal.
  71. Itter_Random - Wraps Itter_RandomInternal.
  72. Itter_Count - Gets the number of items in an itterator.
  73. Itter_Debug - Wraps around Itter_ShowArray.
  74. Itter_Free - Wraps around Itter_FreeInternal.
  75. Itter_Create2 - Create a new itterator array value set.
  76. Itter_Add2 - Wraps Itter_AddInternal for arrays.
  77. Itter_Remove2 - Wraps Itter_RemoveInternal for arrays.
  78. Itter_Random2 - Wraps Itter_RandomInternal for arrays.
  79. Itter_Count2 - Gets the number of items in an itterator array.
  80. Itter_Debug2 - Wraps around Itter_ShowArray for arrays.
  81. Itter_Free2 - Wraps around Itter_FreeInternal for arrays.
  82. API:
  83. -
  84. Callbacks:
  85. -
  86. Hooks:
  87. Itter_OnPlayerConnect - Hook for the OnPlayerConnect callback.
  88. Itter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback.
  89. Itter_OnGameModeInit - Only exists to make the code compile correctly...
  90. Definitions:
  91. -
  92. Enums:
  93. -
  94. Macros:
  95. -
  96. Keywords:
  97. foreach - Command to loop an iterator.
  98. foreachex - Like foreach but without a new variable.
  99. foreach2 - Command to loop through an iterator array.
  100. foreachex - Like foreach2 but without a new variable.
  101. Tags:
  102. -
  103. Variables:
  104. Global:
  105. -
  106. Static:
  107. YSI_g_OPC - Records wether Itter_OnPlayerConnect exists for speed.
  108. YSI_g_OPDC - Records wether Itter_OnPlayerDisconnect exists for speed.
  109. Commands:
  110. -
  111. Compile options:
  112. YSI_ITTER_NO_SORT - Removed.
  113. FOREACH_NO_BOTS - Remove the bot iterators for smaller code.
  114. FOREACH_NO_PLAYERS - Remove all default code for player itteration.
  115. Operators:
  116. -
  117. Iterators:
  118. Player - List of all players connected.
  119. Bot - List of all bots (npcs) connected.
  120. NPC - Alias of Bot.
  121. Character - All players and bots.
  122. -*----------------------------------------------------------------------------*/
  123. #if defined _foreach_included
  124. #endinput
  125. #endif
  126. #define _foreach_included
  127. #if !defined _samp_included
  128. #error "Please include a_samp or a_npc before foreach"
  129. #endif
  130. #if defined SendChat || defined FOREACH_NO_PLAYERS
  131. #define BOTSYNC_IS_BOT (true)
  132. #endif
  133. #if defined IsPlayerNPC
  134. #define _FOREACH_BOT
  135. #endif
  136. #if !defined BOTSYNC_IS_BOT
  137. static
  138. bool:YSI_g_OPC = false,
  139. bool:YSI_g_OPDC = false;
  140. #endif
  141. #if defined YSI_ITTER_NO_SORT
  142. #error "YSI_ITTER_NO_SORT is no longer supported by foreach"
  143. #endif
  144. /*----------------------------------------------------------------------------*-
  145. Function:
  146. Itter_Create2
  147. Params:
  148. name - Itterator identifier.
  149. size0 - Number of iterators.
  150. size1 - Number of items per iterator.
  151. Return:
  152. -
  153. Notes:
  154. Creates a new array of itterator start/array pair.
  155. -*----------------------------------------------------------------------------*/
  156. #define Iter_Create2 Itter_Create2
  157. #define Itter_Create2(%1,%2,%3) \
  158. new \
  159. YSI_g%1S[%2] = {-1, ...}, \
  160. YSI_g%1C[%2] = {0}, \
  161. YSI_g%1A[%2][%3]
  162. /*----------------------------------------------------------------------------*-
  163. Function:
  164. Itter_Init2
  165. Params:
  166. itter - Name of the itterator array to initialise.
  167. Return:
  168. -
  169. Notes:
  170. Wrapper for Itter_InitInternal.
  171. -*----------------------------------------------------------------------------*/
  172. #define Iter_Init2 Itter_Init2
  173. #define Itter_Init2(%1) \
  174. Itter_InitInternal(YSI_g%1A, sizeof (YSI_g%1A), sizeof (YSI_g%1A[]))
  175. /*----------------------------------------------------------------------------*-
  176. Function:
  177. Itter_Add2
  178. Params:
  179. itter - Name of the itterator array to add the data to.
  180. index - Index of the iterator.
  181. value - Value to add to the itterator.
  182. Return:
  183. -
  184. Notes:
  185. Wrapper for Itter_AddInternal.
  186. -*----------------------------------------------------------------------------*/
  187. #define Iter_Add2 Itter_Add2
  188. #define Itter_Add2(%1,%2,%3) \
  189. Itter_AddInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], %3)
  190. /*----------------------------------------------------------------------------*-
  191. Function:
  192. Itter_Free2
  193. Params:
  194. itter - Name of the itterator to get the first free slot in.
  195. index - Index of the iterator.
  196. Return:
  197. -
  198. Notes:
  199. Wrapper for Itter_FreeInternal.
  200. -*----------------------------------------------------------------------------*/
  201. #define Iter_Free2 Itter_Free2
  202. #define Itter_Free2(%1,%2) \
  203. Itter_FreeInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], sizeof (YSI_g%1A[]))
  204. /*----------------------------------------------------------------------------*-
  205. Function:
  206. Itter_Remove2
  207. Params:
  208. itter - Name of the itterator to remove data from.
  209. index - Index of the iterator.
  210. value - Data to remove.
  211. Return:
  212. -
  213. Notes:
  214. Wrapper for Itter_RemoveInternal.
  215. -*----------------------------------------------------------------------------*/
  216. #define Iter_Remove2 Itter_Remove2
  217. #define Itter_Remove2(%1,%2,%3) \
  218. Itter_RemoveInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2], %3)
  219. /*----------------------------------------------------------------------------*-
  220. Function:
  221. Itter_Random2
  222. Params:
  223. itter - Name of the itterator to get a random slot from.
  224. index - Index of the iterator.
  225. Return:
  226. -
  227. Notes:
  228. Wrapper for Itter_RandomInternal.
  229. -*----------------------------------------------------------------------------*/
  230. #define Iter_Random2 Itter_Random2
  231. #define Itter_Random2(%1,%2) \
  232. Itter_RandomInternal(YSI_g%1S[%2], YSI_g%1C[%2], YSI_g%1A[%2])
  233. /*----------------------------------------------------------------------------*-
  234. Function:
  235. Itter_Debug2
  236. Params:
  237. itter - Name of the itterator to output debug information from.
  238. index - Index of the iterator.
  239. Return:
  240. -
  241. Notes:
  242. Wrapper for Itter_ShowArray.
  243. -*----------------------------------------------------------------------------*/
  244. #define Iter_Debug2 Itter_Debug2
  245. #define Itter_Debug2(%1,%2) \
  246. Itter_ShowArray(YSI_g%1S[%2], YSI_g%1A[%2], YSI_g%1C[%2])
  247. /*----------------------------------------------------------------------------*-
  248. Function:
  249. Itter_Count2
  250. Params:
  251. itter - Name of the itterator to get a random slot from4.
  252. index - Index of the iterator.
  253. Return:
  254. -
  255. Notes:
  256. Returns the number of items in this itterator.
  257. -*----------------------------------------------------------------------------*/
  258. #define Iter_Count2 Itter_Count2
  259. #define Itter_Count2(%1,%2) \
  260. YSI_g%1C[%2]
  261. /*----------------------------------------------------------------------------*-
  262. Function:
  263. foreach2
  264. Params:
  265. data - Data to itterate through.
  266. index - Index of the iterator.
  267. as - Variable to set value to.
  268. Return:
  269. -
  270. Notes:
  271. Not exactly the same as PHP foreach, just itterates through a list and
  272. returns the value of the current slot but uses that slot as the next index
  273. too. Variables must be in the form YSI_g<name>S for the start index and
  274. YSI_g<name>A for the data array where <name> is what's entered in data.
  275. -*----------------------------------------------------------------------------*/
  276. #define foreach2(%1,%2,%3) \
  277. for (new %3 = YSI_g%1S[%2]; %3 != -1; %3 = YSI_g%1A[%2][%3])
  278. /*----------------------------------------------------------------------------*-
  279. Function:
  280. foreachex2
  281. Params:
  282. data - Data to itterate through.
  283. index - Index of the iterator.
  284. as - Variable to set value to.
  285. Return:
  286. -
  287. Notes:
  288. Similar to foreach but doesn't declare a new variable for the itterator.
  289. -*----------------------------------------------------------------------------*/
  290. #define foreachex2(%1,%2,%3) \
  291. for (%3 = YSI_g%1S[%2]; %3 != -1; %3 = YSI_g%1A[%2][%3])
  292. /*----------------------------------------------------------------------------*-
  293. Function:
  294. Itter_Create
  295. Params:
  296. name - Itterator identifier.
  297. size - Number of values.
  298. Return:
  299. -
  300. Notes:
  301. Creates a new itterator start/array pair.
  302. -*----------------------------------------------------------------------------*/
  303. #define Iter_Create Itter_Create
  304. #define Itter_Create(%1,%2) \
  305. new \
  306. YSI_g%1S = -1, \
  307. YSI_g%1C = 0, \
  308. YSI_g%1A[%2] = {-1, ...}
  309. /*----------------------------------------------------------------------------*-
  310. Function:
  311. Itter_Add
  312. Params:
  313. itter - Name of the itterator to add the data to.
  314. value - Value to add to the itterator.
  315. Return:
  316. -
  317. Notes:
  318. Wrapper for Itter_AddInternal.
  319. -*----------------------------------------------------------------------------*/
  320. #define Iter_Add Itter_Add
  321. #define Itter_Add(%1,%2) \
  322. Itter_AddInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, %2)
  323. /*----------------------------------------------------------------------------*-
  324. Function:
  325. Itter_Free
  326. Params:
  327. itter - Name of the itterator to get the first free slot in.
  328. Return:
  329. -
  330. Notes:
  331. Wrapper for Itter_FreeInternal.
  332. -*----------------------------------------------------------------------------*/
  333. #define Iter_Free Itter_Free
  334. #define Itter_Free(%1) \
  335. Itter_FreeInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, sizeof (YSI_g%1A))
  336. /*----------------------------------------------------------------------------*-
  337. Function:
  338. Itter_Remove
  339. Params:
  340. itter - Name of the itterator to remove data from.
  341. value - Data to remove.
  342. Return:
  343. -
  344. Notes:
  345. Wrapper for Itter_RemoveInternal.
  346. -*----------------------------------------------------------------------------*/
  347. #define Iter_Remove Itter_Remove
  348. #define Itter_Remove(%1,%2) \
  349. Itter_RemoveInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A, %2)
  350. /*----------------------------------------------------------------------------*-
  351. Function:
  352. Itter_Random
  353. Params:
  354. itter - Name of the itterator to get a random slot from.
  355. Return:
  356. -
  357. Notes:
  358. Wrapper for Itter_RandomInternal.
  359. -*----------------------------------------------------------------------------*/
  360. #define Iter_Random Itter_Random
  361. #define Itter_Random(%1) \
  362. Itter_RandomInternal(YSI_g%1S, YSI_g%1C, YSI_g%1A)
  363. /*----------------------------------------------------------------------------*-
  364. Function:
  365. Itter_Debug
  366. Params:
  367. itter - Name of the itterator to output debug information from.
  368. Return:
  369. -
  370. Notes:
  371. Wrapper for Itter_ShowArray.
  372. -*----------------------------------------------------------------------------*/
  373. #define Iter_Debug Itter_Debug
  374. #define Itter_Debug(%1) \
  375. Itter_ShowArray(YSI_g%1S, YSI_g%1A, YSI_g%1C)
  376. /*----------------------------------------------------------------------------*-
  377. Function:
  378. Itter_Count
  379. Params:
  380. itter - Name of the itterator to get a random slot from4.
  381. Return:
  382. -
  383. Notes:
  384. Returns the number of items in this itterator.
  385. -*----------------------------------------------------------------------------*/
  386. #define Iter_Count Itter_Count
  387. #define Itter_Count(%1) \
  388. YSI_g%1C
  389. /*----------------------------------------------------------------------------*-
  390. Create the internal itterators.
  391. -*----------------------------------------------------------------------------*/
  392. #if !defined BOTSYNC_IS_BOT
  393. Itter_Create(Player, MAX_PLAYERS);
  394. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  395. Itter_Create(Bot, MAX_PLAYERS);
  396. Itter_Create(Character, MAX_PLAYERS);
  397. #define YSI_gNPCS YSI_gBotS
  398. #define YSI_gNPCC YSI_gBotC
  399. #define YSI_gNPCA YSI_gBotA
  400. #endif
  401. #endif
  402. /*----------------------------------------------------------------------------*-
  403. Function:
  404. foreach
  405. Params:
  406. data - Data to itterate through.
  407. as - Variable to set value to.
  408. Return:
  409. -
  410. Notes:
  411. Not exactly the same as PHP foreach, just itterates through a list and
  412. returns the value of the current slot but uses that slot as the next index
  413. too. Variables must be in the form YSI_g<name>S for the start index and
  414. YSI_g<name>A for the data array where <name> is what's entered in data.
  415. -*----------------------------------------------------------------------------*/
  416. #define foreach(%1,%2) \
  417. for (new %2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  418. /*----------------------------------------------------------------------------*-
  419. Function:
  420. foreachex
  421. Params:
  422. data - Data to itterate through.
  423. as - Variable to set value to.
  424. Return:
  425. -
  426. Notes:
  427. Similar to foreach but doesn't declare a new variable for the itterator.
  428. -*----------------------------------------------------------------------------*/
  429. #define foreachex(%1,%2) \
  430. for (%2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  431. /*----------------------------------------------------------------------------*-
  432. Function:
  433. Itter_OnPlayerConnect
  434. Params:
  435. playerid - Player who joined.
  436. Return:
  437. -
  438. Notes:
  439. Adds a player to the loop data. Now sorts the list too. Note that I found
  440. the most bizzare bug ever (I *think* it may be a compiler but, but it
  441. requires further investigation), basically it seems that multiple variables
  442. were being treated as the same variable (namely YSI_gBotS and
  443. YSI_gCharacterS were the same and YSI_gBotC and YSI_gCharacterC were the
  444. same). Adding print statements which reference these variables seem to fix
  445. the problem, and I've tried to make sure that the values will never actually
  446. get printed.
  447. -*----------------------------------------------------------------------------*/
  448. #if !defined BOTSYNC_IS_BOT
  449. public
  450. OnPlayerConnect(playerid)
  451. {
  452. #if defined _FOREACH_BOT
  453. if (!IsPlayerNPC(playerid))
  454. {
  455. Itter_Add(Player, playerid);
  456. }
  457. #if !defined FOREACH_NO_BOTS
  458. else
  459. {
  460. Itter_Add(Bot, playerid);
  461. }
  462. #pragma tabsize 4
  463. Itter_Add(Character, playerid);
  464. #endif
  465. #else
  466. Itter_Add(Player, playerid);
  467. #endif
  468. if (YSI_g_OPC)
  469. {
  470. return CallLocalFunction("Itter_OnPlayerConnect", "i", playerid);
  471. }
  472. return 1;
  473. }
  474. #if defined _ALS_OnPlayerConnect
  475. #undef OnPlayerConnect
  476. #else
  477. #define _ALS_OnPlayerConnect
  478. #endif
  479. #define OnPlayerConnect Itter_OnPlayerConnect
  480. forward
  481. Itter_OnPlayerConnect(playerid);
  482. #endif
  483. /*----------------------------------------------------------------------------*-
  484. Function:
  485. Itter_OnGameModeInit
  486. Params:
  487. -
  488. Return:
  489. -
  490. Notes:
  491. There are WIERD bugs in this script, seemingly caused by the compiler, so
  492. this hopefully fixes them. The OnFilterScriptInit code is written to be
  493. very fast by utilising the internal array structure instead of the regular
  494. Add functions.
  495. -*----------------------------------------------------------------------------*/
  496. #if !defined BOTSYNC_IS_BOT
  497. #if defined FILTERSCRIPT
  498. public
  499. OnFilterScriptInit()
  500. {
  501. if (YSI_gPlayerC)
  502. {
  503. print("foreach error: Something went wrong again! Please tell Y_less");
  504. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  505. printf("%d", YSI_gBotS);
  506. printf("%d", YSI_gBotC);
  507. printf("%d", YSI_gCharacterS);
  508. printf("%d", YSI_gCharacterC);
  509. #endif
  510. printf("%d", YSI_gPlayerS);
  511. printf("%d", YSI_gPlayerC);
  512. }
  513. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  514. new
  515. lastBot = -1,
  516. lastCharacter = -1;
  517. #endif
  518. new
  519. lastPlayer = -1;
  520. for (new i = 0; i != MAX_PLAYERS; ++i)
  521. {
  522. if (IsPlayerConnected(i))
  523. {
  524. #if defined _FOREACH_BOT
  525. if (!IsPlayerNPC(i))
  526. {
  527. if (lastPlayer == -1)
  528. {
  529. YSI_gPlayerS = i;
  530. }
  531. else
  532. {
  533. YSI_gPlayerA[lastPlayer] = i;
  534. }
  535. ++YSI_gPlayerC;
  536. lastPlayer = i;
  537. }
  538. #if !defined FOREACH_NO_BOTS
  539. else
  540. {
  541. if (lastBot == -1)
  542. {
  543. YSI_gBotS = i;
  544. }
  545. else
  546. {
  547. YSI_gBotA[lastBot] = i;
  548. }
  549. ++YSI_gBotC;
  550. lastBot = i;
  551. }
  552. #pragma tabsize 4
  553. if (lastCharacter == -1)
  554. {
  555. YSI_gCharacterS = i;
  556. }
  557. else
  558. {
  559. YSI_gCharacterA[lastCharacter] = i;
  560. }
  561. ++YSI_gCharacterC;
  562. lastCharacter = i;
  563. #endif
  564. #else
  565. if (lastPlayer == -1)
  566. {
  567. YSI_gPlayerS = i;
  568. }
  569. else
  570. {
  571. YSI_gPlayerA[lastPlayer] = i;
  572. }
  573. ++YSI_gPlayerC;
  574. lastPlayer = i;
  575. #endif
  576. }
  577. }
  578. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  579. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  580. CallLocalFunction("Itter_OnFilterScriptInit", "");
  581. }
  582. #if defined _ALS_OnFilterScriptInit
  583. #undef OnFilterScriptInit
  584. #else
  585. #define _ALS_OnFilterScriptInit
  586. #endif
  587. #define OnFilterScriptInit Itter_OnFilterScriptInit
  588. forward Itter_OnFilterScriptInit();
  589. #else
  590. public
  591. OnGameModeInit()
  592. {
  593. if (YSI_gPlayerC)
  594. {
  595. print("foreach error: Something went wrong again! Please tell Y_less");
  596. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  597. printf("%d", YSI_gBotS);
  598. printf("%d", YSI_gBotC);
  599. printf("%d", YSI_gCharacterS);
  600. printf("%d", YSI_gCharacterC);
  601. #endif
  602. printf("%d", YSI_gPlayerS);
  603. printf("%d", YSI_gPlayerC);
  604. }
  605. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  606. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  607. CallLocalFunction("Itter_OnGameModeInit", "");
  608. }
  609. #if defined _ALS_OnGameModeInit
  610. #undef OnGameModeInit
  611. #else
  612. #define _ALS_OnGameModeInit
  613. #endif
  614. #define OnGameModeInit Itter_OnGameModeInit
  615. forward
  616. Itter_OnGameModeInit();
  617. #endif
  618. #endif
  619. /*----------------------------------------------------------------------------*-
  620. Function:
  621. Itter_OnPlayerDisconnect
  622. Params:
  623. playerid - Player who left.
  624. Return:
  625. -
  626. Notes:
  627. Removes a player from the loop data.
  628. -*----------------------------------------------------------------------------*/
  629. #if !defined BOTSYNC_IS_BOT
  630. public
  631. OnPlayerDisconnect(playerid, reason)
  632. {
  633. #if defined _FOREACH_BOT
  634. if (!IsPlayerNPC(playerid))
  635. {
  636. Itter_Remove(Player, playerid);
  637. }
  638. #if !defined FOREACH_NO_BOTS
  639. else
  640. {
  641. Itter_Remove(Bot, playerid);
  642. }
  643. #pragma tabsize 4
  644. Itter_Remove(Character, playerid);
  645. #endif
  646. #else
  647. Itter_Remove(Player, playerid);
  648. #endif
  649. if (YSI_g_OPDC)
  650. {
  651. return CallLocalFunction("Itter_OnPlayerDisconnect", "ii", playerid, reason);
  652. }
  653. return 1;
  654. }
  655. #if defined _ALS_OnPlayerDisconnect
  656. #undef OnPlayerDisconnect
  657. #else
  658. #define _ALS_OnPlayerDisconnect
  659. #endif
  660. #define OnPlayerDisconnect Itter_OnPlayerDisconnect
  661. forward
  662. Itter_OnPlayerDisconnect(playerid, reason);
  663. #endif
  664. /*----------------------------------------------------------------------------*-
  665. Function:
  666. Itter_ShowArray
  667. Params:
  668. start - Itterator start point.
  669. members[] - Itterator contents.
  670. size - Number of itterator values
  671. Return:
  672. -
  673. Notes:
  674. Pure debug function. Has regular prints not debug prints
  675. as it's only called when debug is on.
  676. -*----------------------------------------------------------------------------*/
  677. stock
  678. Itter_ShowArray(start, members[], size)
  679. {
  680. static
  681. sString[61];
  682. new
  683. i,
  684. j = 10;
  685. printf("Start: %d", start);
  686. printf("Size: %d", size);
  687. while (i < size)
  688. {
  689. sString[0] = '\0';
  690. while (i < j && i < size)
  691. {
  692. format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  693. i++;
  694. }
  695. printf("Array (%d): %s", j, sString);
  696. j += 10;
  697. }
  698. }
  699. /*----------------------------------------------------------------------------*-
  700. Function:
  701. Itter_RandomInternal
  702. Params:
  703. start - Array start index.
  704. count - Number of items in the itterator.
  705. array[] - Itterator data.
  706. Return:
  707. -
  708. Notes:
  709. Returns a random value from an iterator.
  710. -*----------------------------------------------------------------------------*/
  711. stock
  712. Itter_RandomInternal(start, count, array[])
  713. {
  714. if (count == 0)
  715. {
  716. return -1;
  717. }
  718. new
  719. rnd = random(count),
  720. cur = start;
  721. while (cur != -1)
  722. {
  723. if (rnd--)
  724. {
  725. cur = array[cur];
  726. }
  727. else
  728. {
  729. return cur;
  730. }
  731. }
  732. return -1;
  733. }
  734. /*----------------------------------------------------------------------------*-
  735. Function:
  736. Itter_FreeInternal
  737. Params:
  738. start - Array start index.
  739. count - Number of items in the itterator.
  740. array[] - Itterator data.
  741. size - Size of the itterator.
  742. Return:
  743. -
  744. Notes:
  745. Finds the first free slot in the itterator. Itterators now HAVE to be
  746. sorted for this function to work correctly as it uses that fact to decide
  747. wether a slot is unused or the last one. If you want to use the slot
  748. straight after finding it the itterator will need to re-find it to add in
  749. the data.
  750. -*----------------------------------------------------------------------------*/
  751. stock
  752. Itter_FreeInternal(start, count, array[], size)
  753. {
  754. if (count == size)
  755. {
  756. return -1;
  757. }
  758. else if (count == 0)
  759. {
  760. return 0;
  761. }
  762. new
  763. first = 0;
  764. while (first != -1)
  765. {
  766. if (first == start)
  767. {
  768. start = array[start];
  769. }
  770. else if (array[first] == -1)
  771. {
  772. return first;
  773. }
  774. ++first;
  775. }
  776. return -1;
  777. }
  778. /*----------------------------------------------------------------------------*-
  779. Function:
  780. Itter_AddInternal
  781. Params:
  782. &start - Array start index.
  783. &count - Number of items in the itterator.
  784. array[] - Itterator data.
  785. value - Item to add.
  786. Return:
  787. -
  788. Notes:
  789. Adds a value to a given itterator set.
  790. -*----------------------------------------------------------------------------*/
  791. stock
  792. Itter_AddInternal(&start, &count, array[], value)
  793. {
  794. if (array[value] != -1)
  795. {
  796. return 0;
  797. }
  798. ++count;
  799. if (start == -1)
  800. {
  801. start = value;
  802. }
  803. else if (start > value)
  804. {
  805. array[value] = start;
  806. start = value;
  807. }
  808. else
  809. {
  810. new
  811. cur = start,
  812. last;
  813. do
  814. {
  815. last = cur;
  816. cur = array[cur];
  817. if (cur > value)
  818. {
  819. array[value] = cur;
  820. array[last] = value;
  821. return 1;
  822. }
  823. }
  824. while (cur != -1);
  825. array[last] = value;
  826. }
  827. return 1;
  828. }
  829. /*----------------------------------------------------------------------------*-
  830. Function:
  831. Itter_RemoveInternal
  832. Params:
  833. &start - Array start index.
  834. &count - Number of items in the itterator.
  835. array[] - Itterator data.
  836. value - Item to remove.
  837. Return:
  838. -
  839. Notes:
  840. Removes a value from an itterator.
  841. -*----------------------------------------------------------------------------*/
  842. stock
  843. Itter_RemoveInternal(&start, &count, array[], value)
  844. {
  845. if (start == -1)
  846. {
  847. return 0;
  848. }
  849. if (start == value)
  850. {
  851. start = array[value];
  852. }
  853. else
  854. {
  855. new
  856. cur = start;
  857. while (array[cur] != value)
  858. {
  859. cur = array[cur];
  860. if (cur == -1)
  861. {
  862. return 0;
  863. }
  864. }
  865. array[cur] = array[value];
  866. }
  867. array[value] = -1;
  868. --count;
  869. return 1;
  870. }
  871. /*----------------------------------------------------------------------------*-
  872. Function:
  873. Itter_InitInternal
  874. Params:
  875. array[][] - Itterator array to initialise.
  876. s0 - Size of first dimension.
  877. s1 - Size of second dimension.
  878. Return:
  879. -
  880. Notes:
  881. Multi-dimensional arrays can't be initialised at compile time, so need to be
  882. done at run time, which is slightly annoying.
  883. -*----------------------------------------------------------------------------*/
  884. stock
  885. Itter_InitInternal(arr[][], s0, s1)
  886. {
  887. for (new i = 0; i != s0; ++i)
  888. {
  889. for (new j = 0; j != s1; ++j)
  890. {
  891. arr[i][j] = -1;
  892. }
  893. }
  894. }