Advertisement
Guest User

pawno\include\YSI\y_iterate.inc

a guest
Mar 20th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.31 KB | None | 0 0
  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. Version: MPL 1.1
  15.  
  16. The contents of this file are subject to the Mozilla Public License Version
  17. 1.1 (the "License"); you may not use this file except in compliance with
  18. the License. You may obtain a copy of the License at
  19. [url]http://www.mozilla.org/MPL/[/url]
  20.  
  21. Software distributed under the License is distributed on an "AS IS" basis,
  22. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  23. for the specific language governing rights and limitations under the
  24. License.
  25.  
  26. The Original Code is the YSI foreach include.
  27.  
  28. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  29. Portions created by the Initial Developer are Copyright (C) 2011
  30. the Initial Developer. All Rights Reserved.
  31.  
  32. Contributors:
  33. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  34.  
  35. Thanks:
  36. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  37. ZeeX - Very productive conversations.
  38. koolk - IsPlayerinAreaEx code.
  39. TheAlpha - Danish translation.
  40. breadfish - German translation.
  41. Fireburn - Dutch translation.
  42. yom - French translation.
  43. 50p - Polish translation.
  44. Zamaroht - Spanish translation.
  45. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  46. for me to strive to better.
  47. Pixels^ - Running XScripters where the idea was born.
  48. Matite - Pestering me to release it and using it.
  49.  
  50. Very special thanks to:
  51. Thiadmer - PAWN, whose limits continue to amaze me!
  52. Kye/Kalcor - SA:MP.
  53. SA:MP Team past, present and future - SA:MP.
  54.  
  55. Version:
  56. 0.4
  57. Changelog:
  58. 17/10/12:
  59. Fixed a bug that was here but not in "foreach.inc".
  60. 04/10/12:
  61. Added a tiny tweak to detect tag-returning iterator functions.
  62. Added Iter_InternalSize.
  63. 13/01/12:
  64. Fixed the count thanks to AndreT.
  65. 05/01/12:
  66. Fixed multi-dimensional iterators.
  67. Fixed "FOREACH_NO_BOTS".
  68. Made "Iterator:" support multi-dimensional arrays.
  69. 24/12/11:
  70. Added _YSI_SPECIAL_DEBUG support.
  71. Added fix for function iterators.
  72. Ported back to YSI.
  73. Changed to use internal YSI "__" natives.
  74. Fixed calls order by using ALS.
  75. 31/10/11:
  76. Changed the underlying loop code to be slightly faster.
  77. Added support for Iter_SafeRemove, prompting refactoring.
  78. 17/09/11:
  79. Fixed arrays under the new syntax.
  80. 28/04/11:
  81. Moved iterator identifiers to end of variables.
  82. Rewrote "foreach" to accept two syntaxes for "foreach (new i : Iter)".
  83. 16/08/10:
  84. Removed all the "2" versions of the functions.
  85. 14/08/10:
  86. Added Iter_Clear to reset an array.
  87. 06/08/10:
  88. Added special array declaration format.
  89. 18/12/09:
  90. Added Itter_Func2 functions for multi-dimensional iterators.
  91. Renamed foreact et al as keywords in the documentation.
  92. Changed licensing from GPL to MPL.
  93. 02/09/09:
  94. Fixed (again) for 0.3.
  95. Added free slot finding.
  96. 21/08/09:
  97. Updated to include random functions.
  98. Made entirely stand alone.
  99. Ported to 0.3 (separate version).
  100. Added automatic callback hook code.
  101. Removed debug information from stand alone version.
  102. 06/01/08:
  103. Added debug information.
  104. 09/10/07:
  105. Moved to system.
  106. 16/09/07:
  107. Added list sorting.
  108. Made this part of Y SeRver Includes, not Y Sever Includes.
  109. Made list sorting optional.
  110. Fixed version number.
  111. 08/09/07:
  112. First version.
  113. Functions:
  114. Public:
  115. OnPlayerDisconnect - Called when a player leaves to remove them.
  116. OnPlayerConnect - Called when a player connects to add them.
  117. Core:
  118. -
  119. Stock:
  120. Itter_ShowArray - Displays the contents of the array.
  121. Itter_AddInternal - Add a value to an itterator.
  122. Itter_RemoveInternal - Remove a value from an itterator.
  123. Itter_RandomInternal - Get a random item from an itterator.
  124. Itter_FreeInternal - Gets the first free slot in the itterator.
  125. Itter_InitInternal - Initialises a multi-dimensional itterator.
  126. Static:
  127. -
  128. Inline:
  129. Itter_Create - Create a new itterator value set.
  130. Itter_Add - Wraps Itter_AddInternal.
  131. Itter_Remove - Wraps Itter_RemoveInternal.
  132. Itter_Random - Wraps Itter_RandomInternal.
  133. Itter_Count - Gets the number of items in an itterator.
  134. Itter_Debug - Wraps around Itter_ShowArray.
  135. Itter_Free - Wraps around Itter_FreeInternal.
  136. Itter_Create2 - Create a new itterator array value set.
  137. Itter_Add2 - Wraps Itter_AddInternal for arrays.
  138. Itter_Remove2 - Wraps Itter_RemoveInternal for arrays.
  139. Itter_Random2 - Wraps Itter_RandomInternal for arrays.
  140. Itter_Count2 - Gets the number of items in an itterator array.
  141. Itter_Debug2 - Wraps around Itter_ShowArray for arrays.
  142. Itter_Free2 - Wraps around Itter_FreeInternal for arrays.
  143. API:
  144. -
  145. Callbacks:
  146. -
  147. Hooks:
  148. Itter_OnPlayerConnect - Hook for the OnPlayerConnect callback.
  149. Itter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback.
  150. Itter_OnGameModeInit - Only exists to make the code compile correctly...
  151. Definitions:
  152. -
  153. Enums:
  154. -
  155. Macros:
  156. -
  157. Keywords:
  158. foreach - Command to loop an iterator.
  159. foreachex - Like foreach but without a new variable.
  160. foreach2 - Command to loop through an iterator array.
  161. foreachex - Like foreach2 but without a new variable.
  162. Tags:
  163. Iterator - Declare an iterator.
  164. Variables:
  165. Global:
  166. -
  167. Static:
  168. YSI_g_OPC - Records wether Itter_OnPlayerConnect exists for speed.
  169. YSI_g_OPDC - Records wether Itter_OnPlayerDisconnect exists for speed.
  170. Commands:
  171. -
  172. Compile options:
  173. YSI_ITTER_NO_SORT - Removed.
  174. FOREACH_NO_BOTS - Remove the bot iterators for smaller code.
  175. FOREACH_NO_PLAYERS - Remove all default code for player itteration.
  176. Operators:
  177. -
  178. Iterators:
  179. Player - List of all players connected.
  180. Bot - List of all bots (npcs) connected.
  181. NPC - Alias of Bot.
  182. Character - All players and bots.
  183. </remarks>
  184. \**--------------------------------------------------------------------------**/
  185.  
  186. // "y_iterate" is always higher than "foreach".
  187. #define _Y_ITERATE_LOCAL_VERSION 20
  188.  
  189. // Foreach is testing us.
  190. #if defined _FOREACH_INC_TEST
  191. #define _FOREACH_CUR_VERSION _Y_ITERATE_LOCAL_VERSION
  192. #endinput
  193. #endif
  194. #if !defined _FOREACH_NO_TEST
  195. #define _FOREACH_INC_TEST
  196. #tryinclude <foreach>
  197. #undef _FOREACH_INC_TEST
  198. // <foreach> exists - test which is newer.
  199. #if defined _inc_foreach
  200. #if !defined _FOREACH_CUR_VERSION
  201. // Foreach exists, but it's an old version - don't try use this
  202. // system or the variables will conflict.
  203. #error "Old foreach.inc files are no longer compatible with YSI."
  204. #endinput
  205. #endif
  206. #if _FOREACH_CUR_VERSION > _Y_ITERATE_LOCAL_VERSION
  207. // Foreach is newer.
  208. #undef _inc_foreach
  209. #define _FOREACH_NO_TEST
  210. #include <foreach>
  211. #endinput
  212. #endif
  213. #endif
  214. #endif
  215.  
  216. #if !defined _samp_included
  217. #error "Please include a_samp or a_npc before foreach"
  218. #endif
  219.  
  220. #if defined _YSI_SPECIAL_DEBUG
  221. #define PS_IS_PLAYER_CONNECTED(%0) (%0 != INVALID_PLAYER_ID)
  222. #else
  223. #define PS_IS_PLAYER_CONNECTED IsPlayerConnected
  224. #endif
  225.  
  226. //#include "y_debug"
  227. //#include "y_hooks"
  228.  
  229. #include "internal\y_natives"
  230. #include "y_debug"
  231. //#include "y_als"
  232.  
  233. #if defined SendChat || defined FOREACH_NO_PLAYERS
  234. #define BOTSYNC_IS_BOT (true)
  235. #endif
  236.  
  237. #if defined IsPlayerNPC
  238. #define _FOREACH_BOT
  239. #endif
  240.  
  241. #if defined YSI_ITTER_NO_SORT
  242. #error YSI_ITTER_NO_SORT is no longer supported by foreach.
  243. #endif
  244.  
  245. #define _Y_ITER_ARRAY: _:_Y_ITER_C0:
  246. #define _Y_ITER_ARRAY_SIZE(%1) _:_Y_ITER_C1:_Y_ITER_C2:sizeof %1@YSII_Ag-1
  247.  
  248. #define _Y_ITER_C0:%0[%1]@YSII_%4g%3) %0@YSII_%4g[%1]%3)
  249. #define _Y_ITER_C1:_Y_ITER_C2:%0[%1]@YSII_Ag%3) %0@YSII_Ag[]%3)
  250. #define _Y_ITER_C2:sizeof%0(%1)@YSII_Ag-1;_:(%2=_Y_ITER_ARRAY:%3(%4)@YSII_Ag[%5])!=%9_Y_ITER_ARRAY_SIZE(%6);) -1;_:(%2=%3@YSII_Ag(%4,%5))!=-1;)
  251.  
  252. #define _Y_ITER_C3:%0[%1]@YSII_Cg,%2[%3]@YSII_Ag[%4]={%5} _Y_ITER_C3:%0@YSII_Cg[%4-1],%0@YSII_Ag[%1][%4]
  253.  
  254. //#if !defined BOTSYNC_IS_BOT
  255. // static stock
  256. // YSI_g_sCallbacks = 0;
  257. //#endif
  258.  
  259. #if !defined BOTSYNC_IS_BOT
  260. forward Itter_OPDCInternal(playerid);
  261. #endif
  262.  
  263. /**--------------------------------------------------------------------------**\
  264. <summary>Itter_Create2</summary>
  265. <param name="name">Itterator identifier.</param>
  266. <param name="size0">Number of iterators.</param>
  267. <param name="size1">Number of items per iterator.</param>
  268. <returns>
  269. -
  270. </returns>
  271. <remarks>
  272. Creates a new array of itterator start/array pair.
  273. </remarks>
  274. \**--------------------------------------------------------------------------**/
  275.  
  276. // If this ever changes, update the size reference in y_users.
  277. /*#define Iter_Create2 Itter_Create2
  278. #define Itter_Create2(%1,%2,%3) \
  279. new \
  280. %1@YSII_Sg[%2] = {-1, ...}, \
  281. %1@YSII_Cg[%2] = {0}, \
  282. %1@YSII_Ag[%2][%3]*/
  283.  
  284. #define IteratorArray:%1[%2]<%3> %1@YSII_Cg[%2],%1@YSII_Ag[%2][%3+1]//,%1@YSII_Rg[%2][%3+1]
  285.  
  286. /**--------------------------------------------------------------------------**\
  287. Array:
  288. Iterator
  289. </returns>
  290. <remarks>
  291. Creates a new itterator start/array pair.
  292. </remarks>
  293. \**--------------------------------------------------------------------------**/
  294.  
  295. #define Iterator:%1<%2> _Y_ITER_C3:%1@YSII_Cg,%1@YSII_Ag[(%2)+1]={(%2)*2,(%2)*2-1,...}
  296. #define iterator%0<%1> Iterator:%0<%1>
  297.  
  298. /**--------------------------------------------------------------------------**\
  299. <summary>Itter_Init2</summary>
  300. <param name="itter">Name of the itterator array to initialise.</param>
  301. <returns>
  302. -
  303. </returns>
  304. <remarks>
  305. Wrapper for Itter_InitInternal.
  306.  
  307. native Iter_Init(IteratorArray:Name[]<>);
  308.  
  309. </remarks>
  310. \**--------------------------------------------------------------------------**/
  311.  
  312. #define Iter_Init Itter_Init
  313. #define Itter_Init(%1) \
  314. Itter_InitInternal(%1@YSII_Ag,sizeof %1@YSII_Ag,sizeof %1@YSII_Ag[]-1)
  315.  
  316. /**--------------------------------------------------------------------------**\
  317. <summary>Itter_Create</summary>
  318. <param name="name">Itterator identifier.</param>
  319. <param name="size">Number of values.</param>
  320. <returns>
  321. -
  322. </returns>
  323. <remarks>
  324. Creates a new itterator start/array pair.
  325. </remarks>
  326. \**--------------------------------------------------------------------------**/
  327.  
  328. // If this ever changes, update the size reference in y_users.
  329. /*#define Iter_Create Itter_Create
  330. #define Itter_Create(%1,%2) \
  331. new \
  332. %1@YSII_Sg = -1, \
  333. %1@YSII_Cg = 0, \
  334. %1@YSII_Ag[%2] = {-1, ...}*/
  335.  
  336. /**--------------------------------------------------------------------------**\
  337. <summary>Itter_Add</summary>
  338. <param name="itter">Name of the itterator to add the data to.</param>
  339. <param name="value">Value to add to the itterator.</param>
  340. <returns>
  341. -
  342. </returns>
  343. <remarks>
  344. Wrapper for Itter_AddInternal.
  345.  
  346. native Iter_Add(Iterator:Name<>, value);
  347.  
  348. </remarks>
  349. \**--------------------------------------------------------------------------**/
  350.  
  351. #define Iter_Add Itter_Add
  352. #define Itter_Add(%1,%2) Itter_AddInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
  353.  
  354. /*#define Iter_AddSafe Itter_AddSafe
  355. #define Itter_AddSafe(%1,%2) \
  356. Itter_AddSafeInternal(%1@YSII_Sg, %1@YSII_Cg, %1@YSII_Ag, %1@YSII_Rg, %2)*/
  357.  
  358. /**--------------------------------------------------------------------------**\
  359. <summary>Itter_Free</summary>
  360. <param name="itter">Name of the itterator to get the first free slot in.</param>
  361. <returns>
  362. -
  363. </returns>
  364. <remarks>
  365. Wrapper for Itter_FreeInternal.
  366.  
  367. native Iter_Free(Iterator:Name<>);
  368.  
  369. </remarks>
  370. \**--------------------------------------------------------------------------**/
  371.  
  372. #define Iter_Free Itter_Free
  373. #define Itter_Free(%1) Itter_FreeInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
  374.  
  375. /**--------------------------------------------------------------------------**\
  376. <summary>Itter_Remove</summary>
  377. <param name="itter">Name of the itterator to remove data from.</param>
  378. <param name="value">Data to remove.</param>
  379. <returns>
  380. -
  381. </returns>
  382. <remarks>
  383. Wrapper for Itter_RemoveInternal.
  384.  
  385. native Iter_Remove(Iterator:Name<>, value);
  386.  
  387. </remarks>
  388. \**--------------------------------------------------------------------------**/
  389.  
  390. #define Iter_Remove Itter_Remove
  391. #define Itter_Remove(%1,%2) Itter_RemoveInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
  392.  
  393. /**--------------------------------------------------------------------------**\
  394. <summary>Itter_Contains</summary>
  395. <param name="itter">Name of the itterator to check membership of.</param>
  396. <param name="value">Value to check.</param>
  397. <returns>
  398. -
  399. </returns>
  400. <remarks>
  401. Checks if the given value is in the given iterator.
  402.  
  403. native Iter_Remove(Iterator:Name<>, value);
  404.  
  405. </remarks>
  406. \**--------------------------------------------------------------------------**/
  407.  
  408. #define Iter_Contains Itter_Contains
  409. #define Itter_Contains(%1,%2) Itter_ContainsInternal(_Y_ITER_ARRAY:%1@YSII_Ag,%2,_Y_ITER_ARRAY_SIZE(%1))
  410.  
  411. /**--------------------------------------------------------------------------**\
  412. <summary>Itter_SafeRemove</summary>
  413. <param name="itter">Name of the itterator to remove data from.</param>
  414. <param name="value">Data to remove.</param>
  415. <param name="next">Container for the pointer to the next element.</param>
  416. <returns>
  417. -
  418. </returns>
  419. <remarks>
  420. Wrapper for Itter_SafeRemoveInternal. Common use:
  421.  
  422. Iter_SafeRemove(iter, i, i);
  423.  
  424. native Iter_SafeRemove(Iterator:Name<>, value, &next);
  425.  
  426. </remarks>
  427. \**--------------------------------------------------------------------------**/
  428.  
  429. #define Iter_SafeRemove Itter_SafeRemove
  430. #define Itter_SafeRemove(%1,%2,%3) Itter_SafeRemoveInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,%2,%3,_Y_ITER_ARRAY_SIZE(%1))
  431.  
  432. /**--------------------------------------------------------------------------**\
  433. <summary>Itter_Random</summary>
  434. <param name="itter">Name of the itterator to get a random slot from.</param>
  435. <returns>
  436. -
  437. </returns>
  438. <remarks>
  439. Wrapper for Itter_RandomInternal.
  440.  
  441. native Iter_Random(Iterator:Name<>);
  442.  
  443. </remarks>
  444. \**--------------------------------------------------------------------------**/
  445.  
  446. #define Iter_Random Itter_Random
  447. #define Itter_Random(%1) Itter_RandomInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
  448.  
  449. /**--------------------------------------------------------------------------**\
  450. <summary>Itter_Debug</summary>
  451. <param name="itter">Name of the itterator to output debug information from.</param>
  452. <returns>
  453. -
  454. </returns>
  455. <remarks>
  456. Wrapper for Itter_ShowArray.
  457. </remarks>
  458. \**--------------------------------------------------------------------------**/
  459.  
  460. //#define Iter_Debug Itter_Debug
  461. //#define Itter_Debug(%1) Itter_ShowArray(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag)
  462.  
  463. /**--------------------------------------------------------------------------**\
  464. <summary>Itter_Count</summary>
  465. <param name="itter">Name of the itterator to get a random slot from4.</param>
  466. <returns>
  467. -
  468. </returns>
  469. <remarks>
  470. Returns the number of items in this itterator.
  471.  
  472. native Iter_Count(Iterator:Name<>);
  473.  
  474. </remarks>
  475. \**--------------------------------------------------------------------------**/
  476.  
  477. #define Iter_Count Itter_Count
  478. #define Itter_Count(%1) (_Y_ITER_ARRAY:%1@YSII_Cg)
  479.  
  480. /**--------------------------------------------------------------------------**\
  481. <summary>Itter_Clear</summary>
  482. <param name="itter">Name of the itterator empty.</param>
  483. <returns>
  484. -
  485. </returns>
  486. <remarks>
  487. Wrapper for Itter_ClearInternal.
  488.  
  489. native Iter_Clear(IteratorArray:Name[]<>);
  490.  
  491. </remarks>
  492. \**--------------------------------------------------------------------------**/
  493.  
  494. #define Iter_Clear Itter_Clear
  495. #define Itter_Clear(%1) Itter_ClearInternal(_Y_ITER_ARRAY:%1@YSII_Cg,_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1))
  496.  
  497. /**--------------------------------------------------------------------------**\
  498. Create the internal itterators.
  499. </remarks>
  500. \**--------------------------------------------------------------------------**/
  501.  
  502. #if !defined BOTSYNC_IS_BOT
  503. new
  504. Iterator:Player<MAX_PLAYERS>;
  505.  
  506. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  507. new
  508. Iterator:Bot<MAX_PLAYERS>,
  509. Iterator:Character<MAX_PLAYERS>;
  510.  
  511. //#define NPC@YSII_Sg Bot@YSII_Sg
  512. #define NPC@YSII_Cg Bot@YSII_Cg
  513. #define NPC@YSII_Ag Bot@YSII_Ag
  514. //#define NPC@YSII_Eg Bot@YSII_Eg
  515. //#define NPC@YSII_Rg Bot@YSII_Rg
  516. #endif
  517. #endif
  518.  
  519. /**--------------------------------------------------------------------------**\
  520. Variables to optimise memory usage by only having one copy of each string.
  521. Note that only strings used more than once are put here because only they
  522. have any gain to being located in only one place.
  523. </remarks>
  524. \**--------------------------------------------------------------------------**/
  525.  
  526. static stock
  527. YSI_gsOnGameModeInit[] = "Itter_OnGameModeInit",
  528. YSI_gsSpecifier@[] = "";
  529. /* YSI_gsOnPlayerConnect[] = "Itter_OnPlayerConnect",
  530. YSI_gsOnPlayerDisconnect[] = "Itter_OnPlayerDisconnect",
  531. YSI_gsOnGameModeInit[] = "Itter_OnGameModeInit",
  532. YSI_gsSpecifier@i[] = "i",
  533. YSI_gsSpecifier@[] = "";*/
  534.  
  535. /**--------------------------------------------------------------------------**\
  536. <summary>foreach</summary>
  537. <param name="data">Data to itterate through.</param>
  538. <param name="as">Variable to set value to.</param>
  539. <returns>
  540. -
  541. </returns>
  542. <remarks>
  543. Not exactly the same as PHP foreach, just itterates through a list and
  544. returns the value of the current slot but uses that slot as the next index
  545. too. Variables must be in the form @YSII_<gname>S for the start index and
  546. @YSII_<gname>A for the data array where <name> is what's entered in data.
  547. </remarks>
  548. \**--------------------------------------------------------------------------**/
  549.  
  550. //#define foreach(%1,%2)
  551. // for (new %2 = %1@YSII_Sg; _:%2 != -1; %2 = %1@YSII_Ag[%2])
  552. #define foreach%1(%0) for(new Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%0|||)
  553. // This allows us to use "new" multiple times - stripping off ONLY whole words.
  554. #define new%0|||%9|||%1:%2||| %9|||%0|||%1|||%2|||
  555.  
  556. // This one is called if the new syntax is required, but the state of "new" is
  557. // as-yet unknown. This attempts to call "%1" as a macro, if it starts with
  558. // "new" as a whole word then it will (and will also helpfully strip off the
  559. // "new" keyword for us).
  560. #define Y_FOREACH_THIRD|||%0|||%1|||%2||| %1=Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2|||
  561.  
  562. // This is called if the "new" macro is called for a second time.
  563. #define Y_FOREACH_FOURTH|||%0=Y_FOREACH_FIFTH|||%1|||%2||| new Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2|||
  564.  
  565. // This is called when there are tags on the "new" declaration.
  566. #define Y_FOREACH_SEVENTH|||%9Y_FOREACH_SIXTH;%0|||%1|||%2||| new %0:%1=%0:_Y_ITER_ARRAY_SIZE(%2);_:(%1=_Y_ITER_ARRAY:%2@YSII_Ag[%1])!=%0:_Y_ITER_ARRAY_SIZE(%2);
  567.  
  568. // This is called when there aren't.
  569. #define Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2||| %0=_Y_ITER_ARRAY_SIZE(%2);_:(%0=_Y_ITER_ARRAY:%2@YSII_Ag[%0])!=_Y_ITER_ARRAY_SIZE(%2);
  570. //hta:%0=hta:%2@YSII_Sg;_:%0!=-1;%0=hta:%2@YSII_Ag[%0]
  571. //#define Y_FOREACH_FOURTH|||%0=Y_FOREACH_FIFTH|||%1|||%2||| new hta:%0=hta:%2@YSII_Sg;_:%0!=-1;%0=hta:%2@YSII_Ag[%0]
  572.  
  573. // Move any tags from the second half to the first half.
  574. //#define hta:%0=hta:%1:%2;_:%3!=-1;%4=hta:%5:%6[%7] %0:%1=%2;_:%1!=-1;%1=%6[%1]
  575.  
  576. // This is called if "%1" didn't have "new" at the start.
  577. #define Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2||| _Y_ITER_ARRAY_SIZE(%2);_:(%1=_Y_ITER_ARRAY:%2@YSII_Ag[%1])!=_Y_ITER_ARRAY_SIZE(%2);
  578.  
  579. //%1=%2@YSII_Sg;_:Y_FOREACH_NONEW:%1!=-1;%1=%2@YSII_Ag[%1]
  580. // This is the old version, but DON'T add "new" because that already exists from
  581. // the failed "new" macro call above.
  582. #define Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%1,%2||| %2=_Y_ITER_ARRAY_SIZE(%1);_:(%2=_Y_ITER_ARRAY:%1@YSII_Ag[%2])!=_Y_ITER_ARRAY_SIZE(%1);
  583. //#define Y_FOREACH_NONEW:new%0!=-1;new%1=%2[new%3] %0!=-1;%1=%2[%3]
  584.  
  585. //#define Y_FOREACH_EIGHTH:%0[%1]@YSII_Sg;%2;%3=%4[%5]@YSII_Ag[%6] %0@YSII_Sg[%1];%2;%3=%4@YSII_Ag[%5][%6]
  586.  
  587. /**--------------------------------------------------------------------------**\
  588. <summary>foreachex</summary>
  589. <param name="data">Data to itterate through.</param>
  590. <param name="as">Variable to set value to.</param>
  591. <returns>
  592. -
  593. </returns>
  594. <remarks>
  595. Similar to foreach but doesn't declare a new variable for the itterator.
  596. </remarks>
  597. \**--------------------------------------------------------------------------**/
  598.  
  599. #define foreachex(%1,%2) foreach(%2:%1)
  600. //for (%2=_Y_ITER_ARRAY_SIZE(%1);(%2=_Y_ITER_ARRAY:%1@YSII_Ag[%2])!=_Y_ITER_ARRAY_SIZE(%1);)
  601.  
  602. /**--------------------------------------------------------------------------**\
  603. <summary>Itter_OnPlayerConnect</summary>
  604. <param name="playerid">Player who joined.</param>
  605. <returns>
  606. -
  607. </returns>
  608. <remarks>
  609. Adds a player to the loop data. Now sorts the list too. Note that I found
  610. the most bizzare bug ever (I *think* it may be a compiler but, but it
  611. requires further investigation), basically it seems that multiple variables
  612. were being treated as the same variable (namely @YSII_EgotS and
  613. @YSII_CgharacterS were the same and @YSII_EgotC and @YSII_CgharacterC were the
  614. same). Adding print statements which reference these variables seem to fix
  615. the problem, and I've tried to make sure that the values will never actually
  616. get printed.
  617. </remarks>
  618. \**--------------------------------------------------------------------------**/
  619.  
  620. #if !defined BOTSYNC_IS_BOT
  621. public OnPlayerConnect(playerid)
  622. {
  623. P:2("Iter_OnPlayerConnect: %d", playerid);
  624. #if defined _FOREACH_BOT
  625. if (!IsPlayerNPC(playerid))
  626. {
  627. Itter_Add(Player, playerid);
  628. }
  629. #if !defined FOREACH_NO_BOTS
  630. else
  631. {
  632. Itter_Add(Bot, playerid);
  633. }
  634. #pragma tabsize 4
  635. Itter_Add(Character, playerid);
  636. #endif
  637. #else
  638. Itter_Add(Player, playerid);
  639. #endif
  640. //if (YSI_g_sCallbacks & 2)
  641. //{
  642. // CallLocalFunction(YSI_gsOnPlayerConnect, YSI_gsSpecifier@i, playerid);
  643. //}
  644. P:2("Iter_OnPlayerConnect end");
  645. #if defined Itter_OnPlayerConnect
  646. return Itter_OnPlayerConnect(playerid);
  647. #else
  648. return 1;
  649. #endif
  650. }
  651.  
  652. #if defined _ALS_OnPlayerConnect
  653. #undef OnPlayerConnect
  654. #else
  655. #define _ALS_OnPlayerConnect
  656. #endif
  657. #define OnPlayerConnect Itter_OnPlayerConnect
  658. #if defined Itter_OnPlayerConnect
  659. forward Itter_OnPlayerConnect(playerid);
  660. #endif
  661. #endif
  662.  
  663. /**--------------------------------------------------------------------------**\
  664. <summary>Itter_OnFilterScriptInit</summary>
  665. <returns>
  666. -
  667. </returns>
  668. <remarks>
  669. Fixes a bug where callbacks are not detected when "loadfs" is used after the
  670. GM has already started. If this is a GM this is just never used called.
  671. </remarks>
  672. \**--------------------------------------------------------------------------**/
  673.  
  674. #if !defined BOTSYNC_IS_BOT
  675. public OnFilterScriptInit()
  676. {
  677. P:1("Iter_OnFilterScriptInit start: %d", MAX_PLAYERS);
  678. //if (funcidx(YSI_gsOnPlayerDisconnect) != -1)
  679. //{
  680. // YSI_g_sCallbacks |= 1;
  681. //}
  682. //if (funcidx(YSI_gsOnPlayerConnect) != -1)
  683. //{
  684. // YSI_g_sCallbacks |= 2;
  685. //}
  686. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  687. Bot@YSII_Cg = _Y_ITER_C3:0;
  688. Character@YSII_Cg = _Y_ITER_C3:0;
  689. new
  690. lastBot = MAX_PLAYERS,
  691. lastCharacter = MAX_PLAYERS;
  692. #endif
  693. Player@YSII_Cg = _Y_ITER_C3:0;
  694. new
  695. lastPlayer = MAX_PLAYERS;
  696. for (new i = 0; i != MAX_PLAYERS; ++i)
  697. {
  698. if (IsPlayerConnected(i))
  699. {
  700. #if defined _FOREACH_BOT
  701. // Had to do "if ! else" due to compile options.
  702. if (!IsPlayerNPC(i))
  703. {
  704. Player@YSII_Ag[lastPlayer] = i;
  705. ++Player@YSII_Cg;
  706. lastPlayer = i;
  707. }
  708. #if !defined FOREACH_NO_BOTS
  709. else
  710. {
  711. Bot@YSII_Ag[lastBot] = i;
  712. ++Bot@YSII_Cg;
  713. lastBot = i;
  714. }
  715. #pragma tabsize 4
  716. Character@YSII_Ag[lastCharacter] = i;
  717. ++Character@YSII_Cg;
  718. lastCharacter = i;
  719. #endif
  720. #else
  721. Player@YSII_Ag[lastPlayer] = i;
  722. ++Player@YSII_Cg;
  723. lastPlayer = i;
  724. #endif
  725. }
  726. else
  727. {
  728. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  729. Bot@YSII_Ag[i] = MAX_PLAYERS + 1;
  730. //Bot@YSII_Rg[i] = -1;
  731. Character@YSII_Ag[i] = MAX_PLAYERS + 1;
  732. //Character@YSII_Rg[i] = -1;
  733. #endif
  734. Player@YSII_Ag[i] = MAX_PLAYERS + 1;
  735. //Player@YSII_Rg[i] = -1;
  736. }
  737. }
  738. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  739. Bot@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  740. Character@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  741. #endif
  742. Player@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  743. //CallLocalFunction("Itter_OnFilterScriptInit", YSI_gsSpecifier@);
  744. #if defined Itter_OnFilterScriptInit
  745. return Itter_OnFilterScriptInit();
  746. #else
  747. return 1;
  748. #endif
  749. }
  750.  
  751. #if defined _ALS_OnFilterScriptInit
  752. #undef OnFilterScriptInit
  753. #else
  754. #define _ALS_OnFilterScriptInit
  755. #endif
  756. #define OnFilterScriptInit Itter_OnFilterScriptInit
  757. #if defined Itter_OnFilterScriptInit
  758. forward Itter_OnFilterScriptInit();
  759. #endif
  760. #endif
  761.  
  762. /**--------------------------------------------------------------------------**\
  763. <summary>Itter_OnGameModeInit</summary>
  764. <returns>
  765. -
  766. </returns>
  767. <remarks>
  768. There are WIERD bugs in this script, seemingly caused by the compiler, so
  769. this hopefully fixes them. The OnFilterScriptInit code is written to be
  770. very fast by utilising the internal array structure instead of the regular
  771. Add functions.
  772. </remarks>
  773. \**--------------------------------------------------------------------------**/
  774.  
  775. #if !defined BOTSYNC_IS_BOT
  776. public OnGameModeInit()
  777. {
  778. P:1("Iter_OnGameModeInit start: %d", MAX_PLAYERS);
  779. // Clear everything.
  780. //if (funcidx(YSI_gsOnPlayerDisconnect) != -1)
  781. //{
  782. // YSI_g_sCallbacks |= 1;
  783. //}
  784. //if (funcidx(YSI_gsOnPlayerConnect) != -1)
  785. //{
  786. // YSI_g_sCallbacks |= 2;
  787. //}
  788. if (!Player@YSII_Cg)
  789. {
  790. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  791. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@, Bot@YSII_Cg, Character@YSII_Cg, Player@YSII_Cg);
  792. #else
  793. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@, Player@YSII_Cg);
  794. #endif
  795. #if defined _YSI_SPECIAL_DEBUG
  796. for (new i = 0; i != MAX_PLAYERS; ++i)
  797. {
  798. Player@YSII_Ag[i] = i + 1;
  799. }
  800. Player@YSII_Ag[MAX_PLAYERS] = 0;
  801. Player@YSII_Cg = _Y_ITER_C3:MAX_PLAYERS;
  802. #endif
  803. P:1("Iter_OnGameModeInit: first");
  804. return 1;
  805. }
  806. // Do the forward iterator list.
  807. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  808. Bot@YSII_Cg = _Y_ITER_C3:0;
  809. Bot@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  810. Character@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  811. Character@YSII_Cg = _Y_ITER_C3:0;
  812. new
  813. lastBot = MAX_PLAYERS,
  814. lastCharacter = MAX_PLAYERS;
  815. #endif
  816. Player@YSII_Cg = _Y_ITER_C3:0;
  817. Player@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  818. new
  819. lastPlayer = MAX_PLAYERS;
  820. for (new i = 0; i != MAX_PLAYERS; ++i)
  821. {
  822. if (IsPlayerConnected(i))
  823. {
  824. #if defined _FOREACH_BOT
  825. // Had to do "if ! else" due to compile options.
  826. if (!IsPlayerNPC(i))
  827. {
  828. Player@YSII_Ag[lastPlayer] = i;
  829. ++Player@YSII_Cg;
  830. lastPlayer = i;
  831. }
  832. #if !defined FOREACH_NO_BOTS
  833. else
  834. {
  835. Bot@YSII_Ag[lastBot] = i;
  836. ++Bot@YSII_Cg;
  837. lastBot = i;
  838. }
  839. #pragma tabsize 4
  840. Character@YSII_Ag[lastCharacter] = i;
  841. ++Character@YSII_Cg;
  842. lastCharacter = i;
  843. #endif
  844. #else
  845. Player@YSII_Ag[lastPlayer] = i;
  846. ++Player@YSII_Cg;
  847. lastPlayer = i;
  848. #endif
  849. }
  850. else
  851. {
  852. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  853. Bot@YSII_Ag[i] = MAX_PLAYERS + 1;
  854. //Bot@YSII_Rg[i] = -1;
  855. Character@YSII_Ag[i] = MAX_PLAYERS + 1;
  856. //Character@YSII_Rg[i] = -1;
  857. #endif
  858. Player@YSII_Ag[i] = MAX_PLAYERS + 1;
  859. //Player@YSII_Rg[i] = -1;
  860. }
  861. }
  862. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  863. Bot@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  864. Character@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  865. #endif
  866. Player@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  867. P:1("Iter_OnGameModeInit: lastplayer = %d", lastPlayer);
  868. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@);
  869. #if defined _YSI_SPECIAL_DEBUG
  870. for (new i = 0; i != MAX_PLAYERS; ++i)
  871. {
  872. Player@YSII_Ag[i] = i + 1;
  873. }
  874. Player@YSII_Ag[MAX_PLAYERS] = 0;
  875. Player@YSII_Cg = _Y_ITER_C3:MAX_PLAYERS;
  876. #endif
  877. return 1;
  878. }
  879.  
  880. #if defined _ALS_OnGameModeInit
  881. #undef OnGameModeInit
  882. #else
  883. #define _ALS_OnGameModeInit
  884. #endif
  885. #define OnGameModeInit Itter_OnGameModeInit
  886. #if defined Itter_OnGameModeInit
  887. forward Itter_OnGameModeInit();
  888. #endif
  889. #endif
  890.  
  891. /**--------------------------------------------------------------------------**\
  892. <summary>Itter_OnPlayerDisconnect</summary>
  893. <param name="playerid">Player who left.</param>
  894. <returns>
  895. -
  896. </returns>
  897. <remarks>
  898. Removes a player from the loop data. No longer uses "hook" to ENSURE that
  899. this is always last. Previously I think that the order of evaluation in
  900. y_hooks meant that this got called before the user "OnPlayerDisconnect".
  901. </remarks>
  902. \**--------------------------------------------------------------------------**/
  903.  
  904. #if !defined BOTSYNC_IS_BOT
  905. public OnPlayerDisconnect(playerid, reason)
  906. {
  907. SetTimerEx("Itter_OPDCInternal", 0, false, "i", playerid);
  908. #if defined Itter_OnPlayerDisconnect
  909. return Itter_OnPlayerDisconnect(playerid, reason);
  910. #else
  911. return 1;
  912. #endif
  913. }
  914.  
  915. #if defined _ALS_OnPlayerDisconnect
  916. #undef OnPlayerDisconnect
  917. #else
  918. #define _ALS_OnPlayerDisconnect
  919. #endif
  920. #define OnPlayerDisconnect Itter_OnPlayerDisconnect
  921. #if defined Itter_OnPlayerDisconnect
  922. forward Itter_OnPlayerDisconnect(playerid, reason);
  923. #endif
  924. #endif
  925.  
  926. /**--------------------------------------------------------------------------**\
  927. <summary>Itter_OPDCInternal</summary>
  928. <param name="playerid">Player who left.</param>
  929. <returns>
  930. -
  931. </returns>
  932. <remarks>
  933. Called AFTER "OnPlayerDisconnect" so that using "Kick" inside a "foreach"
  934. loop doesn't crash the server due to an OOB error.
  935. </remarks>
  936. \**--------------------------------------------------------------------------**/
  937.  
  938. #if !defined BOTSYNC_IS_BOT
  939. public Itter_OPDCInternal(playerid)
  940. {
  941. if (IsPlayerConnected(playerid))
  942. {
  943. return;
  944. }
  945. #if defined _FOREACH_BOT
  946. if (!IsPlayerNPC(playerid))
  947. {
  948. Itter_Remove(Player, playerid);
  949. }
  950. #if !defined FOREACH_NO_BOTS
  951. else
  952. {
  953. Itter_Remove(Bot, playerid);
  954. }
  955. #pragma tabsize 4
  956. Itter_Remove(Character, playerid);
  957. #endif
  958. #else
  959. Itter_Remove(Player, playerid);
  960. #endif
  961. }
  962. #endif
  963.  
  964. /**--------------------------------------------------------------------------**\
  965. <summary>Itter_ShowArray</summary>
  966. <param name="start">Itterator start point.</param>
  967. <param name="members[]">Itterator contents.</param>
  968. <param name="size">Number of itterator values</param>
  969. <returns>
  970. -
  971. </returns>
  972. <remarks>
  973. Pure debug function. Has regular prints not debug prints
  974. as it's only called when debug is on.
  975. </remarks>
  976. \**--------------------------------------------------------------------------**/
  977.  
  978. /*stock
  979. Itter_ShowArray(size, members[])
  980. {
  981. static
  982. sString[61];
  983. new
  984. i,
  985. j = 10;
  986. printf("Start: %d", start);
  987. printf("Size: %d", size);
  988. while (i < size)
  989. {
  990. sString[0] = '\0';
  991. while (i < j && i < size)
  992. {
  993. format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  994. i++;
  995. }
  996. printf("Array (%d): %s", j, sString);
  997. j += 10;
  998. }
  999. }*/
  1000.  
  1001. /**--------------------------------------------------------------------------**\
  1002. <summary>Itter_RandomInternal</summary>
  1003. <param name="count">Number of items in the itterator.</param>
  1004. <param name="array[]">Itterator data.</param>
  1005. <param name="size">Size of the iterator.</param>
  1006. <returns>
  1007. -
  1008. </returns>
  1009. <remarks>
  1010. Returns a random value from an iterator.
  1011. </remarks>
  1012. \**--------------------------------------------------------------------------**/
  1013.  
  1014. stock
  1015. Itter_RandomInternal(count, array[], size)
  1016. {
  1017. if (count == 0)
  1018. {
  1019. return -1;
  1020. }
  1021. new
  1022. rnd = random(count),
  1023. cur = array[size];
  1024. while (cur != size)
  1025. {
  1026. if (rnd-- == 0)
  1027. {
  1028. return cur;
  1029. }
  1030. cur = array[cur];
  1031. }
  1032. return -1;
  1033. }
  1034.  
  1035. /**--------------------------------------------------------------------------**\
  1036. <summary>Itter_FreeInternal</summary>
  1037. <param name="count">Number of items in the itterator.</param>
  1038. <param name="array[]">Itterator data.</param>
  1039. <param name="size">Size of the itterator.</param>
  1040. <returns>
  1041. -
  1042. </returns>
  1043. <remarks>
  1044. Finds the first free slot in the itterator. Itterators now HAVE to be
  1045. sorted for this function to work correctly as it uses that fact to decide
  1046. wether a slot is unused or the last one. If you want to use the slot
  1047. straight after finding it the itterator will need to re-find it to add in
  1048. the data.
  1049. </remarks>
  1050. \**--------------------------------------------------------------------------**/
  1051.  
  1052. stock
  1053. Itter_FreeInternal(array[], size)
  1054. {
  1055. for (new i = 0; i != size; ++i)
  1056. {
  1057. if (array[i] > size)
  1058. {
  1059. return i;
  1060. }
  1061. }
  1062. return -1;
  1063. }
  1064.  
  1065. /**--------------------------------------------------------------------------**\
  1066. <summary>Itter_AddInternal</summary>
  1067. <param name="&start">Array start index.</param>
  1068. <param name="&count">Number of items in the itterator.</param>
  1069. <param name="array[]">Itterator data.</param>
  1070. <param name="value">Item to add.</param>
  1071. <returns>
  1072. -
  1073. </returns>
  1074. <remarks>
  1075. Adds a value to a given itterator set. Now detects when you try and add the
  1076. last item multiple times, as well as all the other items. Now simplified even
  1077. further with the new internal representation.
  1078. </remarks>
  1079. \**--------------------------------------------------------------------------**/
  1080.  
  1081. stock
  1082. Itter_AddInternal(&count, array[], value, size)
  1083. {
  1084. if (0 <= value < size && array[value] > size)
  1085. {
  1086. new
  1087. last = size,
  1088. next = array[last];
  1089. while (next < value)
  1090. {
  1091. last = next;
  1092. next = array[last];
  1093. }
  1094. array[last] = value;
  1095. array[value] = next;
  1096. ++count;
  1097. return 1;
  1098. }
  1099. return 0;
  1100. }
  1101.  
  1102. /**--------------------------------------------------------------------------**\
  1103. <summary>Itter_RemoveInternal</summary>
  1104. <param name="&count">Number of items in the itterator.</param>
  1105. <param name="array[]">Itterator data.</param>
  1106. <param name="value">Item to remove.</param>
  1107. <returns>
  1108. -
  1109. </returns>
  1110. <remarks>
  1111. Removes a value from an itterator.
  1112. </remarks>
  1113. \**--------------------------------------------------------------------------**/
  1114.  
  1115. stock
  1116. Itter_RemoveInternal(&count, array[], value, size)
  1117. {
  1118. new
  1119. last;
  1120. return Itter_SafeRemoveInternal(count, array, value, last, size);
  1121. }
  1122.  
  1123. /**--------------------------------------------------------------------------**\
  1124. <summary>Itter_SafeRemoveInternal</summary>
  1125. <param name="&count">Number of items in the itterator.</param>
  1126. <param name="array[]">Iterator data.</param>
  1127. <param name="back[]">Reverse iterator data.</param>
  1128. <param name="value">Item to remove.</param>
  1129. <param name="&last">Pointer in which to store the last pointer.</param>
  1130. <returns>
  1131. -
  1132. </returns>
  1133. <remarks>
  1134. Removes a value from an itterator safely.
  1135. </remarks>
  1136. \**--------------------------------------------------------------------------**/
  1137.  
  1138. stock
  1139. Itter_SafeRemoveInternal(&count, array[], value, &last, size)
  1140. {
  1141. if (0 <= value < size && array[value] <= size)
  1142. {
  1143. last = size;
  1144. new
  1145. next = array[last];
  1146. while (next != value)
  1147. {
  1148. last = next;
  1149. next = array[last];
  1150. }
  1151. array[last] = array[value];
  1152. array[value] = size + 1;
  1153. --count;
  1154. return 1;
  1155. }
  1156. return 0;
  1157. }
  1158.  
  1159. /**--------------------------------------------------------------------------**\
  1160. <summary>Itter_ContainsInternal</summary>
  1161. <param name="array[]">Itterator data.</param>
  1162. <param name="value">Item to check.</param>
  1163. <param name="size">Size of the iterator.</param>
  1164. <returns>
  1165. -
  1166. </returns>
  1167. <remarks>
  1168. Checks if this item is in the iterator.
  1169. </remarks>
  1170. \**--------------------------------------------------------------------------**/
  1171.  
  1172. stock
  1173. Itter_ContainsInternal(array[], value, size)
  1174. {
  1175. return 0 <= value < size && array[value] <= size;
  1176. }
  1177.  
  1178. /**--------------------------------------------------------------------------**\
  1179. <summary>Itter_ClearInternal</summary>
  1180. <param name="&count">Number of items in the itterator.</param>
  1181. <param name="array[]">Itterator data.</param>
  1182. <param name="back[]">Reverse data.</param>
  1183. <param name="size">Size of the iterator.</param>
  1184. <returns>
  1185. -
  1186. </returns>
  1187. <remarks>
  1188. Resets an iterator.
  1189. </remarks>
  1190. \**--------------------------------------------------------------------------**/
  1191.  
  1192. stock
  1193. Itter_ClearInternal(&count, array[], size)
  1194. {
  1195. for (new i = 0, t = size + 1; i < size; ++i)
  1196. {
  1197. array[i] = t;
  1198. }
  1199. array[size] = size;
  1200. count = 0;
  1201. }
  1202.  
  1203. /**--------------------------------------------------------------------------**\
  1204. <summary>Itter_InitInternal</summary>
  1205. <param name="array[][]">Itterator array to initialise.</param>
  1206. <param name="s0">Size of first dimension.</param>
  1207. <param name="s1">Size of second dimension.</param>
  1208. <returns>
  1209. -
  1210. </returns>
  1211. <remarks>
  1212. Multi-dimensional arrays can't be initialised at compile time, so need to be
  1213. done at run time, which is slightly annoying.
  1214. </remarks>
  1215. \**--------------------------------------------------------------------------**/
  1216.  
  1217. stock
  1218. Itter_InitInternal(arr[][], s0, s1)
  1219. {
  1220. for (new i = 0, t = s1 + 1; i < s0; ++i)
  1221. {
  1222. for (new j = 0; j < s1; ++j)
  1223. {
  1224. arr[i][j] = t;
  1225. }
  1226. arr[i][s1] = s1;
  1227. }
  1228. }
  1229.  
  1230. /**--------------------------------------------------------------------------**\
  1231. <summary>Itter_PrevInternal</summary>
  1232. <param name="array[]">Itterator data.</param>
  1233. <param name="size">Size of the iterator.</param>
  1234. <param name="slot">The current slot.</param>
  1235. <returns>
  1236. -
  1237. </returns>
  1238. <remarks>
  1239. Gets the element in an iterator that points to the current element.
  1240. </remarks>
  1241. \**--------------------------------------------------------------------------**/
  1242.  
  1243. stock
  1244. Itter_PrevInternal(array[], size, slot)
  1245. {
  1246. if (0 <= slot <= size && array[slot] <= size)
  1247. {
  1248. for (new last = slot; last--; )
  1249. {
  1250. if (array[last] == slot)
  1251. {
  1252. return last;
  1253. }
  1254. }
  1255. }
  1256. return size;
  1257. }
  1258.  
  1259. /**--------------------------------------------------------------------------**\
  1260. <summary>Iter_Begin</summary>
  1261. <param name="iter">Name of the iterator to get the start of.</param>
  1262. <returns>
  1263. -
  1264. </returns>
  1265. <remarks>
  1266. Gets a point BEFORE the start of the iterator (the theoretical beginning).
  1267. </remarks>
  1268. \**--------------------------------------------------------------------------**/
  1269.  
  1270. #define Iter_Begin(%1) (_Y_ITER_ARRAY_SIZE(%1))
  1271. #define Itter_Begin(%1) (_Y_ITER_ARRAY_SIZE(%1))
  1272.  
  1273. /**--------------------------------------------------------------------------**\
  1274. <summary>Iter_End</summary>
  1275. <param name="iter">Name of the iterator to get the end of.</param>
  1276. <returns>
  1277. -
  1278. </returns>
  1279. <remarks>
  1280. Gets a point AFTER the end of the iterator (think "MAX_PLAYERS").
  1281. </remarks>
  1282. \**--------------------------------------------------------------------------**/
  1283.  
  1284. #define Iter_End(%1) (_Y_ITER_ARRAY_SIZE(%1))
  1285. #define Itter_End(%1) (_Y_ITER_ARRAY_SIZE(%1))
  1286.  
  1287. /**--------------------------------------------------------------------------**\
  1288. <summary>Iter_First</summary>
  1289. <param name="iter">Name of the iterator to get the first valid element in.</param>
  1290. <returns>
  1291. -
  1292. </returns>
  1293. <remarks>
  1294. Gets the first element in an iterator.
  1295. </remarks>
  1296. \**--------------------------------------------------------------------------**/
  1297.  
  1298. #define Iter_First(%1) (_Y_ITER_ARRAY:%1@YSII_Ag[_Y_ITER_ARRAY_SIZE(%1)])
  1299. #define Itter_First(%1) (_Y_ITER_ARRAY:%1@YSII_Ag[_Y_ITER_ARRAY_SIZE(%1)])
  1300.  
  1301. /**--------------------------------------------------------------------------**\
  1302. <summary>Iter_Last</summary>
  1303. <param name="iter">Name of the iterator to</param>
  1304. <returns>
  1305. -
  1306. </returns>
  1307. <remarks>
  1308. Gets the last element in an iterator.
  1309. </remarks>
  1310. \**--------------------------------------------------------------------------**/
  1311.  
  1312. #define Iter_Last(%1) Itter_PrevInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),_Y_ITER_ARRAY_SIZE(%1))
  1313. #define Itter_Last(%1) Itter_PrevInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),_Y_ITER_ARRAY_SIZE(%1))
  1314.  
  1315. /**--------------------------------------------------------------------------**\
  1316. <summary>Iter_Next</summary>
  1317. <param name="iter">Name of the iterator to get the next element in.</param>
  1318. <param name="cur">The current element.</param>
  1319. <returns>
  1320. -
  1321. </returns>
  1322. <remarks>
  1323. Gets the element in an interator after the current one.
  1324. </remarks>
  1325. \**--------------------------------------------------------------------------**/
  1326.  
  1327. #define Iter_Next(%1,%2) (_Y_ITER_ARRAY:%1@YSII_Ag[(%2)])
  1328. #define Itter_Next(%1,%2) (_Y_ITER_ARRAY:%1@YSII_Ag[(%2)])
  1329.  
  1330. /**--------------------------------------------------------------------------**\
  1331. <summary>Iter_Prev</summary>
  1332. <param name="iter">Name of the iterator to get the previous element in.</param>
  1333. <param name="cur">The current element.</param>
  1334. <returns>
  1335. -
  1336. </returns>
  1337. <remarks>
  1338. Gets the element in an iterator before the current one. Slow.
  1339. </remarks>
  1340. \**--------------------------------------------------------------------------**/
  1341.  
  1342. #define Iter_Prev(%1,%2) Itter_PrevInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),(%2))
  1343. #define Itter_Prev(%1,%2) Itter_PrevInternal(_Y_ITER_ARRAY:%1@YSII_Ag,_Y_ITER_ARRAY_SIZE(%1),(%2))
  1344.  
  1345. /**--------------------------------------------------------------------------**\
  1346. <summary>Iter_InternalArray</summary>
  1347. <param name="iter">Name of the iterator to get the true name of.</param>
  1348. <returns>
  1349. -
  1350. </returns>
  1351. <remarks>
  1352. Accesses the internal array of an iterator.
  1353. </remarks>
  1354. \**--------------------------------------------------------------------------**/
  1355.  
  1356. #define Iter_InternalArray(%1) (_Y_ITER_ARRAY:%1@YSII_Ag)
  1357. #define Itter_InternalArray(%1) (_Y_ITER_ARRAY:%1@YSII_Ag)
  1358.  
  1359. /**--------------------------------------------------------------------------**\
  1360. <summary>Iter_InternalSize</summary>
  1361. <param name="iter">Name of the iterator to get the true size of.</param>
  1362. <returns>
  1363. -
  1364. </returns>
  1365. <remarks>
  1366. Accesses the internal size of an iterator.
  1367. </remarks>
  1368. \**--------------------------------------------------------------------------**/
  1369.  
  1370. #define _Y_ITER_INT_SIZE:%0(%2[%1]@YSII_Ag)) %0(%2@YSII_Ag[]))
  1371.  
  1372. #define Iter_InternalSize(%1) (_:_Y_ITER_INT_SIZE:sizeof (%1@YSII_Ag))
  1373. #define Itter_InternalSize(%1) (_:_Y_ITER_INT_SIZE:sizeof (%1@YSII_Ag))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement