y_iterate_3b.inc 50 KB

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