macros.inc 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. /*
  65. 88 a8P 88
  66. 88 ,88' 88
  67. 88 ,88" 88
  68. 88,d88' ,adPPYba, 8b d8 8b db d8 ,adPPYba, 8b,dPPYba, ,adPPYb,88 ,adPPYba,
  69. 8888"88, a8P_____88 `8b d8' `8b d88b d8' a8" "8a 88P' "Y8 a8" `Y88 I8[ ""
  70. 88P Y8b 8PP""""""" `8b d8' `8b d8'`8b d8' 8b d8 88 8b 88 `"Y8ba,
  71. 88 "88, "8b, ,aa `8b,d8' `8bd8' `8bd8' "8a, ,a8" 88 "8a, ,d88 aa ]8I
  72. 88 Y8b `"Ybbd8"' Y88' YP YP `"YbbdP"' 88 `"8bbdP"Y8 `"YbbdP"'
  73. d8'
  74. d8'
  75. */
  76. // Temp definition for documentation. Will be corrected at the end of the file.
  77. #define Iterator:%1<%2> Iterator:%1[]
  78. /*-------------------------------------------------------------------------*//**
  79. * <param name="expr"><code>var : Iterator</code></param>
  80. * <remarks>
  81. * </remarks>
  82. *//*------------------------------------------------------------------------**/
  83. //P:D(foreach(expr));
  84. #define foreach(%0) for(new Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%0|||)
  85. /*-------------------------------------------------------------------------*//**
  86. * <param name="params">The iterator function's parameters.</param>
  87. * <remarks>
  88. * Used to declare a special iterator function. Examples:
  89. *
  90. * <code>
  91. * iterfunc stock OnlyZero(cur) <br />
  92. * { <br /><indent />
  93. * if (cur == -1) <br /><indent /><indent />
  94. * return 0; <br /><indent />
  95. * return -1; <br />
  96. * } <br />
  97. * </code>
  98. *
  99. * <code>
  100. * iterfunc stock AlsoOnlyZero[cellmin](cur) <br />
  101. * { <br /><indent />
  102. * if (cur == cellmin) <br /><indent /><indent />
  103. * return 0; <br /><indent />
  104. * return cellmin; <br />
  105. * } <br />
  106. * </code>
  107. *
  108. * <code>
  109. * iterfunc stock OneToTen[cellmin](cur) <br />
  110. * { <br /><indent />
  111. * if (cur == cellmin) <br /><indent /><indent />
  112. * return 1; <br /><indent />
  113. * if (cur == 10) <br /><indent /><indent />
  114. * return cellmin; <br /><indent />
  115. * return cur + 1; <br />
  116. * } <br />
  117. * </code>
  118. *
  119. * <code>
  120. * iterfunc stock OneToN(cur, n) <br />
  121. * { <br /><indent />
  122. * if (n &lt; 1) <br /><indent /><indent />
  123. * return -1; <br /><indent />
  124. * if (cur == -1) <br /><indent /><indent />
  125. * return 1; <br /><indent />
  126. * if (cur == n) <br /><indent /><indent />
  127. * return -1; <br /><indent />
  128. * return cur + 1; <br />
  129. * } <br />
  130. * </code>
  131. *
  132. * </remarks>
  133. *//*------------------------------------------------------------------------**/
  134. P:D(iterfunc(params));
  135. #define iterfunc%0(%1) F@y:%0(%1)
  136. /*-------------------------------------------------------------------------*//**
  137. * <param name="iter">Iterator to run backwards.</param>
  138. * <remarks>
  139. * Run an iterator backwards.
  140. * </remarks>
  141. *//*------------------------------------------------------------------------**/
  142. P:D(Iterator@Reverse(Iterator:iter<>));
  143. #define Iterator@Reverse%9;_:(%1=%9(%0)%9)%9; F@o)+_:F@p:F@q:$%1,%0$
  144. /*
  145. 88b d88 88 db 88888888ba 88
  146. 888b d888 "" d88b 88 "8b 88
  147. 88`8b d8'88 d8'`8b 88 ,8P 88
  148. 88 `8b d8' 88 ,adPPYYba, 88 8b,dPPYba, d8' `8b 88aaaaaa8P' 88
  149. 88 `8b d8' 88 "" `Y8 88 88P' `"8a d8YaaaaY8b 88""""""' 88
  150. 88 `8b d8' 88 ,adPPPPP88 88 88 88 d8""""""""8b 88 88
  151. 88 `888' 88 88, ,88 88 88 88 d8' `8b 88 88
  152. 88 `8' 88 `"8bbdP"Y8 88 88 88 d8' `8b 88 88
  153. */
  154. /*-------------------------------------------------------------------------*//**
  155. * <param name="iter">Name of the iterator array to initialise.</param>
  156. * <remarks>
  157. * Wrapper for Iter_Init_Internal. When <c>__COMPILER_NESTED_ELLIPSIS</c> is
  158. * set, this isn't needed because multi-dimensional iterators can be
  159. * initialised with the new <c>{{0, 1, ...), ...}</c> feature. In that case
  160. * <c>I@ = 0</c> is called as a <c>void</c> function that does nothing but ends
  161. * in a semi-colon (<c>I@</c> is used a lot in YSI as a <c>do nothing</c>
  162. * enabler).
  163. *
  164. * <code>native Iter_Init(IteratorArray:Name[]&lt;&gt;);</code>
  165. *
  166. * </remarks>
  167. *//*------------------------------------------------------------------------**/
  168. P:D(Iter_Init(Iterator:iter[]<>));
  169. #if __COMPILER_NESTED_ELLIPSIS
  170. #define Iter_Init(%0) (I@=0)
  171. #else
  172. #define Iter_Init(%0) Iter_Init_Internal(F@r(Iterator@%0),F@r(Iterator@%0)[0],F@s(Iterator@%0),F@s(Iterator@%0[]),F@r(Iter_Init@%0))
  173. #endif
  174. /*-------------------------------------------------------------------------*//**
  175. * <param name="iter">Name of the iterator to add the data to.</param>
  176. * <param name="value">Value to add to the iterator.</param>
  177. * <remarks>
  178. * Wrapper for Iter_AddInternal.
  179. *
  180. * native Iter_Add(Iterator:Name&lt;&gt;, value);
  181. *
  182. * </remarks>
  183. *//*------------------------------------------------------------------------**/
  184. P:D(Iter_Add(Iterator:iter<>, value));
  185. #define Iter_Add _ITER<Add>
  186. #define Iter_Add_InternalA(%0,%1,%9) Iter_Add_InternalC(%0,_:%1,F@s(%1)-1,_:F@t:((%1[0]-%1[0])+%9))
  187. #define Iter_Add_InternalB(%0,%2,%1,%9) Iter_Add_InternalD(%0[%2],_:%1,F@s(%1)-1-(%2),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  188. /*-------------------------------------------------------------------------*//**
  189. * <param name="iter">Name of the iterator to remove data from.</param>
  190. * <param name="value">Data to remove.</param>
  191. * <remarks>
  192. * Wrapper for Iter_RemoveInternal.
  193. *
  194. * native Iter_Remove(Iterator:Name&lt;&gt;, value);
  195. *
  196. * </remarks>
  197. *//*------------------------------------------------------------------------**/
  198. P:D(Iter_Remove(Iterator:iter<>, value));
  199. #define Iter_Remove _ITER<Remove>
  200. #define Iter_Remove_InternalA(%0,%1,%9) Iter_Remove_InternalC(%0,_:%1,F@s(%1)-1,_:F@t:((%1[0]-%1[0])+%9))
  201. #define Iter_Remove_InternalB(%0,%2,%1,%9) Iter_Remove_InternalD(%0[%2],_:%1,F@s(%1)-1-(%2),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  202. /*-------------------------------------------------------------------------*//**
  203. * <param name="iter">Name of the iterator to get the first free slot in.</param>
  204. * <remarks>
  205. * Wrapper for Iter_Free_Internal. Returns a slot NOT in the current
  206. * iterator.
  207. *
  208. * native Iter_Free(Iterator:Name&lt;&gt;);
  209. *
  210. * </remarks>
  211. *//*------------------------------------------------------------------------**/
  212. P:D(Iter_Free(Iterator:iter<>));
  213. #define Iter_Free _ITER<Free>
  214. #define Iter_Free_InternalA(%0,%1) Iter_Free_Internal(_:%1,F@s(%1)-F@s(%0))
  215. #define Iter_Free_InternalB(%0,%2,%1) Iter_Free_Internal(_:%1,F@s(%1)-F@s(%0))
  216. /*-------------------------------------------------------------------------*//**
  217. * <param name="iter">Name of the multi-iterator to get the first free slot in.</param>
  218. * <remarks>
  219. * Wrapper for Iter_FreeMulti_Internal. Returns a slot NOT in the current
  220. * multi-iterator.
  221. *
  222. * native Iter_FreeMulti(Iterator:Name&lt;&gt;);
  223. *
  224. * </remarks>
  225. *//*------------------------------------------------------------------------**/
  226. P:D(Iter_FreeMulti(Iterator:iter<,>, ));
  227. #define Iter_FreeMulti _ITER<FreeMulti>
  228. #define Iter_FreeMulti_InternalA(%0,%1) Iter_FreeMulti_Internal(_:%1,F@s(%1),F@s(%1)-F@s(%0))
  229. #define Iter_FreeMulti_InternalB(%0,%2,%1) Iter_FreeMulti_Internal(_:%1,F@s(%1),F@s(%1)-F@s(%0))
  230. /*-------------------------------------------------------------------------*//**
  231. * <param name="iter">Name of the iterator to check membership of.</param>
  232. * <param name="value">Value to check.</param>
  233. * <remarks>
  234. * Checks if the given value is in the given iterator.
  235. *
  236. * native Iter_Contains(Iterator:Name&lt;&gt;, value);
  237. *
  238. * </remarks>
  239. *//*------------------------------------------------------------------------**/
  240. P:D(bool:Iter_Contains(Iterator:iter<>, value));
  241. #define Iter_Contains(%0) (bool:_ITER<Contains>(%0))
  242. #define Iter_Contains_InternalA(%0,%1,%9) Iter_Contains_InternalC(_:%1,F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  243. #define Iter_Contains_InternalB(%0,%2,%1,%9) Iter_Contains_InternalD(%0[%2],_:%1,F@s(%1)-F@s(%0),-%2+F@s(%1)-1,_:F@t:((%1[0]-%1[0])+%9))
  244. #define Iter_Contains_InternalD(%0,%1,%2,-+%8,%3) Iter_Contains_InternalC(%1,%2,%3)
  245. /*-------------------------------------------------------------------------*//**
  246. * <param name="iter">Name of the iterator to check membership of.</param>
  247. * <param name="value">Value to check.</param>
  248. * <returns>
  249. * Index in which the value is contained in the multi-iterator.
  250. * </returns>
  251. * <remarks>
  252. * Checks if the given value is in the given iterator, and if it is return which index it is contained.
  253. *
  254. * native Iter_GetMulti(Iterator:Name&lt;&gt;, value);
  255. *
  256. * </remarks>
  257. *//*------------------------------------------------------------------------**/
  258. P:D(Iter_GetMulti(Iterator:iter<,>, value));
  259. #define Iter_GetMulti _ITER<GetMulti>
  260. #define Iter_GetMulti_InternalA(%0,%1,%9) Iter_GetMulti_Internal(_:%1,F@s(%1),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  261. #define Iter_GetMulti_InternalB(%0,%2,%1,%9) Iter_GetMulti_Internal(_:%1,F@s(%1),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  262. /*-------------------------------------------------------------------------*//**
  263. * <param name="iter">Name of the iterator to remove data from.</param>
  264. * <param name="value">Data to remove.</param>
  265. * <param name="next">Container for the pointer to the next element.</param>
  266. * <remarks>
  267. * Wrapper for Iter_SafeRemoveInternal. Common use:
  268. *
  269. * Iter_SafeRemove(iter, i, i);
  270. *
  271. * native Iter_SafeRemove(Iterator:Name&lt;&gt;, value, &amp;next);
  272. *
  273. * </remarks>
  274. *//*------------------------------------------------------------------------**/
  275. P:D(Iter_SafeRemove(Iterator:iter<>, value, &next));
  276. #define Iter_SafeRemove _ITER<SafeRemove>
  277. #define Iter_SafeRemove_InternalA(%0,%1,%9) Iter_SafeRemove_InternalC(%0,_:%1,F@s(%1)-1,_:F@t:((%1[0]-%1[0])+%9))
  278. #define Iter_SafeRemove_InternalB(%0,%2,%1,%9) Iter_SafeRemove_InternalD(%0[%2],_:%1,F@s(%1)-1-(%2),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  279. /*-------------------------------------------------------------------------*//**
  280. * <param name="iter">Name of the iterator to add a random slot to.</param>
  281. * <remarks>
  282. * Wrapper for Iter_RandomAddInternal.
  283. *
  284. * native Iter_RandomAdd(Iterator:Name&lt;&gt;);
  285. *
  286. * </remarks>
  287. *//*------------------------------------------------------------------------**/
  288. P:D(Iter_RandomAdd(Iterator:iter<>));
  289. #define Iter_RandomAdd _ITER<RandomAdd>
  290. #define Iter_RandomAdd_InternalA(%0,%1) Iter_RandomAdd_InternalC(%0,_:%1,F@s(%1)-1)
  291. #define Iter_RandomAdd_InternalB(%0,%2,%1) Iter_RandomAdd_InternalD(%0,_:%1,F@s(%1)-F@s(%0),F@s(%0),F@s(%1)-1-(%2),%2)
  292. /*-------------------------------------------------------------------------*//**
  293. * <param name="iter">Name of the iterator to remove a random slot from.</param>
  294. * <remarks>
  295. * Wrapper for Iter_RandomRemoveInternal.
  296. *
  297. * native Iter_RandomRemove(Iterator:Name&lt;&gt;);
  298. *
  299. * </remarks>
  300. *//*------------------------------------------------------------------------**/
  301. P:D(Iter_RandomRemove(Iterator:iter<>));
  302. #define Iter_RandomRemove _ITER<RandomRemove>
  303. #define Iter_RandomRemove_InternalA(%0,%1) Iter_RandomRemove_InternalC(%0,_:%1,F@s(%1)-1)
  304. #define Iter_RandomRemove_InternalB(%0,%2,%1) Iter_RandomRemove_InternalD(%0[%2],_:%1,F@s(%1)-F@s(%0),F@s(%1)-1-(%2))
  305. /*-------------------------------------------------------------------------*//**
  306. * <param name="iter">Name of the iterator to get a random unused slot for.</param>
  307. * <remarks>
  308. * Wrapper for Iter_RandomFree_Internal.
  309. *
  310. * native Iter_RandomFree(Iterator:Name&lt;&gt;);
  311. *
  312. * </remarks>
  313. *//*------------------------------------------------------------------------**/
  314. P:D(Iter_RandomFree(Iterator:iter<>));
  315. #define Iter_RandomFree _ITER<RandomFree>
  316. #define Iter_RandomFree_InternalA(%0,%1) Iter_RandomFree_InternalC(%0,_:%1,F@s(%1)-1)
  317. #define Iter_RandomFree_InternalB(%0,%2,%1) Iter_RandomFree_InternalD(%0,_:%1,F@s(%1)-F@s(%0),F@s(%0))
  318. /*-------------------------------------------------------------------------*//**
  319. * <param name="iter">Name of the iterator to get a random slot from.</param>
  320. * <remarks>
  321. * Wrapper for Iter_RandomInternal.
  322. *
  323. * native Iter_Random(Iterator:Name&lt;&gt;);
  324. *
  325. * </remarks>
  326. *//*------------------------------------------------------------------------**/
  327. P:D(Iter_Random(Iterator:iter<>));
  328. #define Iter_Random _ITER<Random>
  329. #define Iter_Random_InternalA(%0,%1) Iter_Random_Internal(%0,_:%1,F@s(%1)-1)
  330. #define Iter_Random_InternalB(%0,%2,%1) Iter_Random_Internal(%0[%2],_:%1,F@s(%1)-1-(%2))
  331. /*-------------------------------------------------------------------------*//**
  332. * <param name="iter">Name of the iterator to get a random slot from.</param>
  333. * <remarks>
  334. * Returns the number of items in this iterator.
  335. *
  336. * native Iter_Count(Iterator:Name&lt;&gt;);
  337. *
  338. * </remarks>
  339. *//*------------------------------------------------------------------------**/
  340. P:D(Iter_Count(Iterator:iter<>));
  341. #define Iter_Count _ITER<Count>
  342. #define Iter_Count_InternalA(%0,%1) (%0)
  343. #define Iter_Count_InternalB(%0,%2,%1) (%0[%2])
  344. /*-------------------------------------------------------------------------*//**
  345. * <param name="iter">Name of the iterator to empty.</param>
  346. * <remarks>
  347. * Wrapper for Iter_Clear_Internal.
  348. *
  349. * Although it doesn't fit my normal strict spacing, the end of "B" is correct,
  350. * namely: "_:F@s(%0),%2)". This uses the "_:%0,)" macro to consume
  351. * a trailing comma when nothing is given in "%2", so I can't have a leading
  352. * space sadly.
  353. *
  354. * "- 2" in place of the normal "- 1" is CORRECT!
  355. *
  356. * native Iter_Clear(IteratorArray:Name[]&lt;&gt;);
  357. *
  358. * </remarks>
  359. *//*------------------------------------------------------------------------**/
  360. P:D(Iter_Clear(Iterator:iter<>));
  361. #define Iter_Clear _ITER<Clear>
  362. #define Iter_Clear_InternalA(%0,%1) Iter_Clear_InternalC(_:%1,F@s(%1),F@s(%0),F@s(%1)-2,%0)
  363. #define Iter_Clear_InternalB(%0,%2,%1) Iter_Clear_InternalD(_:%1,F@s(%1),F@s(%0),F@s(%1)-F@s(%0),%0,%2)
  364. #define Iter_Clear_InternalD(%0,%1,%2,%3,%4,) Iter_Clear_InternalC(%0,%1,%2,%3-1,%4)
  365. /*-------------------------------------------------------------------------*//**
  366. * <param name="iter">Name of the iterator to get the first free slot in.</param>
  367. * <remarks>
  368. * Finds an empty slot in an iterator, adds that slot to the iterator, and
  369. * returns the now added slot.
  370. *
  371. * native Iter_Alloc(Iterator:Name&lt;&gt;);
  372. *
  373. * </remarks>
  374. *//*------------------------------------------------------------------------**/
  375. P:D(Iter_Alloc(Iterator:iter<>));
  376. #define Iter_Alloc _ITER<Alloc>
  377. #define Iter_Alloc_InternalA(%0,%1) Iter_Alloc_InternalC(%0,_:%1,F@s(%1)-1)
  378. #define Iter_Alloc_InternalB(%0,%2,%1) Iter_Alloc_InternalD(%0[%2],_:%1,F@s(%1)-1-(%2),F@s(%1)-F@s(%0))
  379. /*
  380. 88888888888 db 88888888ba 88
  381. 88 ,d d88b 88 "8b 88
  382. 88 88 d8'`8b 88 ,8P 88
  383. 88aaaaa 8b, ,d8 MM88MMM 8b,dPPYba, ,adPPYYba, d8' `8b 88aaaaaa8P' 88
  384. 88""""" `Y8, ,8P' 88 88P' "Y8 "" `Y8 d8YaaaaY8b 88""""""' 88
  385. 88 )888( 88 88 ,adPPPPP88 d8""""""""8b 88 88
  386. 88 ,d8" "8b, 88, 88 88, ,88 d8' `8b 88 88
  387. 88888888888 8P' `Y8 "Y888 88 `"8bbdP"Y8 d8' `8b 88 88
  388. */
  389. /*-------------------------------------------------------------------------*//**
  390. * <param name="iter">Name of the iterator to empty.</param>
  391. * <remarks>
  392. * Uses a static array copy to blank the iterator instead of a loop.
  393. *
  394. * BROKEN!
  395. *
  396. * native Iter_FastClear(IteratorArray:Name[]&lt;&gt;);
  397. *
  398. * </remarks>
  399. *//*------------------------------------------------------------------------**/
  400. P:D(Iter_FastClear(Iterator:iter<>));
  401. #define Iter_FastClear _ITER<FastClear>
  402. #define Iter_FastClear_InternalA(%0,%1); {static YSII_Xg@Clear[F@s(%1)]={0,1,...};%1=YSII_Xg@Clear;%0=0;}
  403. #define Iter_FastClear_InternalB(%0,%2,%1); {static YSII_Xg@Clear[F@s(%1)]={0,1,...},YSII_Yg@Clear[F@s(%0)]={0,0,...};%1=YSII_Xg@Clear;%0=YSII_Yg@Clear;}
  404. /*-------------------------------------------------------------------------*//**
  405. * <param name="iter">Name of the iterator to get the start of.</param>
  406. * <remarks>
  407. * Gets a point BEFORE the start of the iterator (the theoretical beginning).
  408. * </remarks>
  409. *//*------------------------------------------------------------------------**/
  410. P:D(Iter_Begin(Iterator:iter<>));
  411. #define Iter_Begin _ITER<Begin>
  412. #define Iter_Begin_InternalA(%0,%1) (F@s(%1)-F@s(%0))
  413. #define Iter_Begin_InternalB(%0,%2,%1) (F@s(%1)-1-(%2))
  414. /*-------------------------------------------------------------------------*//**
  415. * <param name="iter">Name of the iterator to get the end of.</param>
  416. * <remarks>
  417. * Gets a point AFTER the end of the iterator (think "MAX_PLAYERS").
  418. * </remarks>
  419. *//*------------------------------------------------------------------------**/
  420. P:D(Iter_End(Iterator:iter<>));
  421. #define Iter_End _ITER<End>
  422. #define Iter_End_InternalA(%0,%1) (F@s(%1)-F@s(%0))
  423. #define Iter_End_InternalB(%0,%2,%1) (F@s(%1)-1-(%2))
  424. /*-------------------------------------------------------------------------*//**
  425. * <param name="iter">Name of the iterator to get the first valid element in.</param>
  426. * <remarks>
  427. * Gets the first element in an iterator.
  428. * </remarks>
  429. *//*------------------------------------------------------------------------**/
  430. P:D(Iter_First(Iterator:iter<>));
  431. #define Iter_First _ITER<First>
  432. #define Iter_First_InternalA(%0,%1) (%1[(F@s(%1)- F@s(%0))])
  433. #define Iter_First_InternalB(%0,%2,%1) (%1[(F@s(%1)-1-(%2))])
  434. /*-------------------------------------------------------------------------*//**
  435. * <param name="iter">Name of the iterator to</param>
  436. * <remarks>
  437. * Gets the last element in an iterator. Works by getting the previous item
  438. * from the one BEFORE the first element (i.e. the one before the sentinel).
  439. * </remarks>
  440. *//*------------------------------------------------------------------------**/
  441. P:D(Iter_Last(Iterator:iter<>));
  442. #define Iter_Last _ITER<Last>
  443. #define Iter_Last_InternalA(%0,%1) ((%1[Iter_End_InternalA(%0,%1)-1])-1)
  444. #define Iter_Last_InternalB(%0,%2,%1) (Iter_Prev_InternalB(%0,%2,%1,Iter_End_InternalB(%0,%2,%1)))
  445. /*-------------------------------------------------------------------------*//**
  446. * <param name="iter">Name of the iterator to get the next element in.</param>
  447. * <param name="cur">The current element.</param>
  448. * <remarks>
  449. * Gets the element in an iterator after the current one.
  450. * </remarks>
  451. *//*------------------------------------------------------------------------**/
  452. P:D(Iter_Next(Iterator:iter<>, cur));
  453. #define Iter_Next _ITER<Next>
  454. #define Iter_Next_InternalA(%0,%1,%9) (%1[_:F@t:((%1[0]-%1[0])+%9)])
  455. #define Iter_Next_InternalB(%0,%2,%1,%9) (%1[_:F@t:((%1[0]-%1[0])+%9)])
  456. /*-------------------------------------------------------------------------*//**
  457. * <param name="iter">Name of the iterator to get the previous element in.</param>
  458. * <param name="cur">The current element.</param>
  459. * <remarks>
  460. * Gets the element in an iterator before the current one. Slow.
  461. * </remarks>
  462. *//*------------------------------------------------------------------------**/
  463. P:D(Iter_Prev(Iterator:iter<>, cur));
  464. #define Iter_Prev _ITER<Prev>
  465. #define Iter_Prev_InternalA(%0,%1,%9) (((%1[(_:((%1[0]-%1[0])+%9)-1)%F@s(%1)])-1)%F@s(%1))
  466. #define Iter_Prev_InternalB(%0,%2,%1,%9) Iter_Prev_Internal(%1,F@s(%1)-F@s(%0),F@s(%1),_:F@t:((%1[0]-%1[0])+%9))
  467. /*-------------------------------------------------------------------------*//**
  468. * <param name="iter">Name of the iterator to get the true array of.</param>
  469. * <remarks>
  470. * Accesses the internal array of an iterator.
  471. * </remarks>
  472. *//*------------------------------------------------------------------------**/
  473. P:D(Iter_TrueArray(Iterator:iter<>));
  474. #define Iter_TrueArray(%1) F@r(Iterator@%1)
  475. /*-------------------------------------------------------------------------*//**
  476. * <param name="iter">Name of the iterator to get the true count of.</param>
  477. * <remarks>
  478. * Accesses the internal count of an iterator.
  479. * </remarks>
  480. *//*------------------------------------------------------------------------**/
  481. P:D(Iter_TrueCount(Iterator:iter<>));
  482. #define Iter_TrueCount(%1) F@r(Iter_Single@%1)
  483. /*-------------------------------------------------------------------------*//**
  484. * <param name="iter">Multi iterator to get the true count of.</param>
  485. * <remarks>
  486. * Accesses the internal count of a multi-iterator.
  487. * </remarks>
  488. *//*------------------------------------------------------------------------**/
  489. P:D(Iter_TrueMulti(Iterator:iter<,>));
  490. #define Iter_TrueMulti(%1) F@r(Iter_Multi@%1)
  491. /*-------------------------------------------------------------------------*//**
  492. * <param name="iter">Name of the iterator to get the true size of.</param>
  493. * <remarks>
  494. * Accesses the internal size of an iterator.
  495. * </remarks>
  496. *//*------------------------------------------------------------------------**/
  497. P:D(Iter_TrueSize(Iterator:iter<>));
  498. #define Iter_TrueSize _ITER<TrueSize>
  499. #define Iter_TrueSize_InternalA(%0,%1) (F@s(%1))
  500. #define Iter_TrueSize_InternalB(%0,%2,%1) (F@s(%1))
  501. /*-------------------------------------------------------------------------*//**
  502. * <param name="iter">Name of the iterator to get the true starts of.</param>
  503. * <remarks>
  504. * Accesses the number of starts in a multi-iterator.
  505. * </remarks>
  506. *//*------------------------------------------------------------------------**/
  507. P:D(Iter_Starts(Iterator:iter<>));
  508. #define Iter_Starts _ITER<Starts>
  509. #define Iter_Starts_InternalA(%0,%1) (F@s(%0))
  510. #define Iter_Starts_InternalB(%0,%2,%1) (F@s(%0))
  511. /*-------------------------------------------------------------------------*//**
  512. * <param name="iter">Name of the iterator to get the size of.</param>
  513. * <remarks>
  514. * Accesses the size of an iterator.
  515. * </remarks>
  516. *//*------------------------------------------------------------------------**/
  517. P:D(Iter_Size(Iterator:iter<>));
  518. #define Iter_Size _ITER<Size>
  519. #define Iter_Size_InternalA(%0,%1) (F@s(%1)-F@s(%0))
  520. #define Iter_Size_InternalB(%0,%2,%1) (F@s(%1)-F@s(%0))
  521. /*-------------------------------------------------------------------------*//**
  522. * <param name="iter">Name of the iterator to get a slot in by index.</param>
  523. * <param name="index">Index.</param>
  524. * <param name="wrap">Keep going around until a value is found?</param>
  525. * <remarks>
  526. * Wrapper for Iter_Index_Internal. Returns the Nth value in the iterator
  527. * (requires looping due to the way iterators are stored and optimised for
  528. * loops not direct access).
  529. *
  530. * native Iter_Index(Iterator:Name&lt;&gt;);
  531. *
  532. * </remarks>
  533. *//*------------------------------------------------------------------------**/
  534. P:D(Iter_Index(Iterator:iter<>, index, bool:wrap = false));
  535. #define Iter_Index _ITER<Index>
  536. #define Iter_Index_InternalA(%0,%1,%9) Iter_Index_Internal(%0,%1,F@s(%1)-1,F@s(%1)-1,_:F@t:((%1[0]-%1[0])+%9))
  537. #define Iter_Index_InternalB(%0,%2,%1,%9) Iter_Index_Internal(%0[%2],%1,F@s(%1)-1-(%2),F@s(%1)-F@s(%0),_:F@t:((%1[0]-%1[0])+%9))
  538. /*
  539. 88b d88
  540. 888b d888
  541. 88`8b d8'88
  542. 88 `8b d8' 88 ,adPPYYba, ,adPPYba, 8b,dPPYba, ,adPPYba, ,adPPYba,
  543. 88 `8b d8' 88 "" `Y8 a8" "" 88P' "Y8 a8" "8a I8[ ""
  544. 88 `8b d8' 88 ,adPPPPP88 8b 88 8b d8 `"Y8ba,
  545. 88 `888' 88 88, ,88 "8a, ,aa 88 "8a, ,a8" aa ]8I
  546. 88 `8' 88 `"8bbdP"Y8 `"Ybbd8"' 88 `"YbbdP"' `"YbbdP"'
  547. */
  548. // Remove trailing spaces. This looks really unsafe because of the "%0\32;"
  549. // allowing anything to come before the space, but it would have to be
  550. // "anything" not text immediately following a "YSII_X@" prefix.
  551. #define iterstart@%0\32;%1$ iterstart@%0%1$
  552. #define Iterator@%0\32;%1$ Iterator@%0%1$
  553. #define Iter_Func@%0\32;%1$ Iter_Func@%0%1$
  554. #define Iter_Single@%0\32;%1$ Iter_Single@%0%1$
  555. #define Iter_Multi@%0\32;%1$ Iter_Multi@%0%1$
  556. #define Iter_Init@%0\32;%1$ Iter_Init@%0%1$
  557. // Allow "iterfunc stock", "iterfunc stock", and other varieties.
  558. // "iterfunc stock" is the hardest, because we have to detect it BEFORE our own
  559. // macro, but still use it on the function itself. It does nothing on the
  560. // forward.
  561. #define Iter_Func@stock stock Iter_Func@
  562. #define Iter_Func@static static Iter_Func@
  563. // Must do this manually I think.
  564. //#define Iter_Func@foreign%0(%2) foreign Iter_Func@%0(%2)
  565. //#define Iter_Func@global%0(%2) global Iter_Func@%0(%2)
  566. #define Iter_Func@public public Iter_Func@
  567. #define Iter_Func@forward forward Iter_Func@
  568. // Drop modifiers - the only place these can appear is under `iterfunc` with the
  569. // dummy variable, which we don't want to modify more than we already have.
  570. #define Iterator@stock Iterator@
  571. #define Iterator@static Iterator@
  572. #define Iterator@public Iterator@
  573. #define Iterator@forward Iterator@
  574. #define iterstart@stock iterstart@
  575. #define iterstart@static iterstart@
  576. #define iterstart@public iterstart@
  577. #define iterstart@forward iterstart@
  578. #define ITER_TAG_FUNC(%0)%1(%2) %0:Iter_Func@%1(%2)
  579. // Many tag types that may come up as iterator function return types.
  580. #define Iter_Func@_: ITER_TAG_FUNC(_)
  581. #define Iter_Func@bool: ITER_TAG_FUNC(bool)
  582. #define Iter_Func@PlayerText3D: ITER_TAG_FUNC(PlayerText3D)
  583. #define Iter_Func@Text3D: ITER_TAG_FUNC(Text3D)
  584. #define Iter_Func@DB: ITER_TAG_FUNC(DB)
  585. #define Iter_Func@DBResult: ITER_TAG_FUNC(DBResult)
  586. #define Iter_Func@Language: ITER_TAG_FUNC(Language)
  587. #define Iter_Func@Bit: ITER_TAG_FUNC(Bit)
  588. #define Iter_Func@Text: ITER_TAG_FUNC(Text)
  589. #define Iter_Func@Menu: ITER_TAG_FUNC(Menu)
  590. #define Iter_Func@Style: ITER_TAG_FUNC(Style)
  591. #define Iter_Func@XML: ITER_TAG_FUNC(XML)
  592. #define Iter_Func@Bintree: ITER_TAG_FUNC(Bintree)
  593. #define Iter_Func@Group: ITER_TAG_FUNC(Group)
  594. #define Iter_Func@Timer: ITER_TAG_FUNC(Timer)
  595. #define Iter_Func@File: ITER_TAG_FUNC(File)
  596. #define Iter_Func@Float: ITER_TAG_FUNC(Float)
  597. #define ITER_TAG(%0)%1={%2,%3,...}%4: %0:Iterator@%1={%0:%2,%0:%3,...}F@a$,Iter_Single@
  598. // Many tag types for declared iterators.
  599. #define Iterator@_: ITER_TAG(_)
  600. #define Iterator@bool: ITER_TAG(bool)
  601. #define Iterator@PlayerText3D: ITER_TAG(PlayerText3D)
  602. #define Iterator@Text3D: ITER_TAG(Text3D)
  603. #define Iterator@DB: ITER_TAG(DB)
  604. #define Iterator@DBResult: ITER_TAG(DBResult)
  605. #define Iterator@Language: ITER_TAG(Language)
  606. #define Iterator@Bit: ITER_TAG(Bit)
  607. #define Iterator@Text: ITER_TAG(Text)
  608. #define Iterator@Menu: ITER_TAG(Menu)
  609. #define Iterator@Style: ITER_TAG(Style)
  610. #define Iterator@XML: ITER_TAG(XML)
  611. #define Iterator@Bintree: ITER_TAG(Bintree)
  612. #define Iterator@Timer: ITER_TAG(Timer)
  613. #define Iterator@File: ITER_TAG(File)
  614. #define Iterator@Float: ITER_TAG(Float)
  615. // This iterator is both a special function AND a tag. This is not natively
  616. // supported, so we need to invent some special macros to do so. Fortunately,
  617. // it is not a state function, so some of the existing macros are not needed.
  618. #define Iterator@Group I@ Iterator@GroupElse_(-1)
  619. // Tag.
  620. #define I@%0Iterator@GroupElse_(%1): ITER_TAG(Group)
  621. // The `Iterator@GroupElse_` macro is in "y_groups", so you only get it if you
  622. // include both libraries.
  623. // A nice "match" symbol that we can scan for, then remove after it is used.
  624. #define F@a$
  625. #define F@b|||
  626. // Detect array declarations: "Iterator:X[20]<4>", "Iterator:X<10, 4>", and the
  627. // frankly silly "Iterator:X[20]<10, 4>".
  628. #define F@c:F@d:((%0,%1)+1)]%2$,Iter_Single@%9$%7$ F@d:((%0)+(%1))]={0,1,...}F@a$,Iter_Multi@%9$[%0]%7$
  629. #if __COMPILER_NESTED_ELLIPSIS
  630. #define F@d:%0]%2$,Iter_%8@%9[%3$%7$ %0]={{0,1,...},...},Iter_%8@%9[%3$%7$
  631. #else
  632. #define F@d:%0]%2$,Iter_%8@%9[%3$%7$ %0],Iter_%8@%9[%3$%7$,F@g(Iter_Init@%9,)=F@s(Iter_%8@%9[%3$)
  633. #endif
  634. // Special "sizeof" that strips subscripts. "|||" prevents "sizeof" recursion.
  635. #define F@e:%0|||(%1[%2]%3|||%4)) F@e:%0|||(%1%3|||%4[]))
  636. #define F@f:%0|||(F@g(%1,%5)%3|||) %0|||(%1 %3|||)
  637. #define F@s(%0) (_:F@f:F@e:sizeof F@b|||(%0 F@a$F@b|||))
  638. // Reify macros.
  639. #define F@r(%0) %0 F@a$
  640. #define F@g(%0,%8) %0 F@a$
  641. #define F@h:%0) %0 F@a$)
  642. // Sort out tagged parameters plus extra parameters. Expanded so it will fail
  643. // to match parameters outside the required area.
  644. //#define F@t:(%0,%1) (%0),%1
  645. #define F@t:(%0) MACRO_PARSE<F@B:>((%0))
  646. #define MACRO_PARSE<%0>(%1) %0_MACRO_PARSE:`%1`
  647. #define _MACRO_PARSE:`%0` %0
  648. #define F@B:%9`(%0,%1)` %9`(%0),%1`
  649. // y_iterate parser. Takes an array input, and splits up all the components.
  650. #define _ITER<%5>(%1) (_:F@i:F@j:$Iter_%5_Internal(%1))
  651. // Separate "<>"s. Parameters to the resulting macro/function:
  652. //
  653. // %0 - Count.
  654. // %1 - Multi iterator position.
  655. // %2 - Array.
  656. // ... - All other parameters.
  657. //
  658. #define F@j:%7$%5(%0) %5A(F@g(Iter_Single@%0,),F@h:Iterator@%0)
  659. #define F@i:%7$%5(%0<%2>%1) %5B(F@h:Iter_Multi@%0,%2,F@h:Iterator@%0%1)
  660. #define _Y_ITER_DO_FOREACH(%1,%0) (_:F@n:F@A:F@m:F@a$%1,%0$
  661. // Multi-iterator.
  662. #define F@n:%9$%1,%0<%2>$ F@s(Iterator@%0)-1-(%2));_:(%1=Iterator@%0 F@a$[_:%1])<(F@s(Iterator@%0)-F@s(Iter_Multi@%0));
  663. // Special iterator.
  664. #define iterstart(%1)%9;_:(%9=Iter%9@%0$[_:%2]%9; F@o))-1+(_:%1));_:(%1)!=_:F@u:F@l:(%2=Iter_Func@%0$(_:%2));
  665. #define iterstate iterstart
  666. // Special iterators without extra defines. Default to -1 start value. The
  667. // many extra brackets are needed to ensure that other macros match.
  668. #define F@A:%9$%1,%0(%2)%8$ F@s(Iterator@%0)-1+_:((F@h:iterstart@%0))+_:F@x:F@h:Iterator@%0);_:(%1=Iter_Func@%0(%2)F@a$[_:%1])!=(F@s(Iterator@%0)-1+_:F@h:Iterator@%0+iterstart@%0);
  669. #define F@x:%6);_:(%1=%9@%0(%2)%9[%4])!=%3; %6);_:(%1=Iter_Func@%0(%4,%2)%9)!=%3;
  670. #define F@o))-1+(%5,%6));_:(%9)!=_:%9(%1=%2$(_:%1)); F@o))-1+(%5)),itrst:Iter_State@%1=itrst:(%6);_:(%5)!=_:F@u:F@l:(%1=%2$(_:Iter_State@%1,_:%1));
  671. //#define F@l:%9$%1,%0(%2)%8$ F@r(Iterator@%0));_:(F@r(Iterator@%0))!=_:(%1=F@h:Iter_Func@%0(_:%1,%2));
  672. #define F@u:F@l:(%0(%2)%8$(%1)%9) (%0(%1,%2)%8$%9)
  673. #define F@l:(%0[%2]%8$(%1)%9) (%0(%1,%2)%8$%9)
  674. // Normal iterator.
  675. #define F@m:%9$%1,%0$ F@s(Iterator@%0)-1);_:(%1=Iterator@%0 F@a$[_:%1])!=(F@s(Iterator@%0)-1);
  676. // Reverse multi-iterator.
  677. #define F@p:%9$%1,%0<%2>$ F@s(Iterator@%0)-2-(%2)));_:(%1=Iter_Prev_InternalB(Iter_Multi@%0,,Iterator@%0,%1))<(F@s(Iterator@%0)-F@s(Iter_Multi@%0));
  678. // Reverse normal iterator.
  679. #define F@q:%9$%1,%0$ F@s(Iterator@%0)-2));_:(%1=Iter_Prev_InternalA(,Iterator@%0,%1))!=(F@s(Iterator@%0)-1);
  680. // Special iterator function declaration.
  681. #define F@y:%0(%1) Y_FOREACH_SQ:Iter_Func@%0[]$(%1);Y_FOREACH_STOCK stock const F@z:Iterator@%0=F@z:0,iterstart@%0=-1;Iter_Func@%0(%1)F@a$
  682. #define Y_FOREACH_STOCK __:F@w:$
  683. // Get rid of ALL square brackets after the first function name.
  684. #define Y_FOREACH_SQ:%0[%1$ %0
  685. #define F@w:%8$%0[%3]=%1,%5[%3]=%6;%2[%3](%4) %8$%0=%1,%5=%3;%2(%4)
  686. // Currently unused:
  687. // Part of y_text for some reason.
  688. //#define F@k
  689. // Make the internal state an array if there is more than one value.
  690. #define itrst:%1=itrst:(%6,%7) %1[]={%6,%7}
  691. // Ultimate macro: <a href="http://forum.sa-mp.com/showpost.php?p=3801730" />
  692. #define u@$ 0]);
  693. #define __:%0$ u@(u[_:%0 u@$
  694. // Consume spaces in the iterator state variables.
  695. #define Iter_State@%0\32;%1) Iter_State@%0%1)
  696. /*-------------------------------------------------------------------------*//**
  697. * The workings of these macros are very extensively documented at:
  698. *
  699. * <a href="http://forum.sa-mp.com/showpost.php?p=2823668" />
  700. *//*------------------------------------------------------------------------**/
  701. // This allows us to use "new" multiple times - stripping off ONLY whole words.
  702. #define new%0|||%3|||%1:%2||| %3|||%0|||%1|||%2|||
  703. // This one is called if the new syntax is required, but the state of "new" is
  704. // as-yet unknown. This attempts to call "%1" as a macro, if it starts with
  705. // "new" as a whole word then it will (and will also helpfully strip off the
  706. // "new" keyword for us).
  707. #define Y_FOREACH_THIRD|||%9|||%1|||%2||| %1=Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2|||
  708. // This is called if the "new" macro is called for a second time.
  709. #define Y_FOREACH_FOURTH|||%1=Y_FOREACH_FIFTH|||%9|||%2||| new Y_FOREACH_SIXTH;%1|||Y_FOREACH_SEVENTH|||%2|||
  710. // This is called when there are tags on the "new" declaration (%9 = "new ").
  711. #define Y_FOREACH_SEVENTH|||%9Y_FOREACH_SIXTH;%0|||%1|||%2||| new %0:%1=%0:_Y_ITER_DO_FOREACH(%1,%2)
  712. // This is called when there aren't.
  713. #define Y_FOREACH_SIXTH;%1|||Y_FOREACH_SEVENTH|||%2||| %1=_Y_ITER_DO_FOREACH(%1,%2)
  714. // This is called if "%1" didn't have "new" at the start.
  715. #define Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2||| _Y_ITER_DO_FOREACH(%1,%2)
  716. // This is the old version, but DON'T add "new" because that already exists from
  717. // the failed "new" macro call above.
  718. #define Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%2,%1||| using_deprecated_foreach_syntax,%1=_Y_ITER_DO_FOREACH(%1,%2)++using_deprecated_foreach_syntax
  719. stock const
  720. // This variable is re-declared when you do "foreach (Player, i)" or similar
  721. // so that a warning is generated. The warning will read:
  722. //
  723. // local variable "using_deprecated_foreach_syntax" shadows a variable at a preceding level
  724. //
  725. // This is the best I could do to warn about the old syntax. That code
  726. // should now be "foreach (new i : Player)". It may become an error later.
  727. bool:using_deprecated_foreach_syntax = false,
  728. F@o = 1;
  729. /*-------------------------------------------------------------------------*//**
  730. * <remarks>
  731. * Creates a new iterator start/array pair.
  732. * </remarks>
  733. *//*------------------------------------------------------------------------**/
  734. #undef Iterator
  735. P:D(Iterator:Iterator(name));
  736. #define Iterator:%1<%2> Iterator@%1[_:F@c:F@d:((%2)+1)]={0,1,...}F@a$,Iter_Single@%1 F@a$F@a$
  737. #define IteratorArray Iterator