foreach.inc 36 KB

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