y_iterate.inc 42 KB

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