y_iterate.inc 33 KB

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