y_iterate.inc 39 KB

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