foreach.inc 43 KB

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