Advertisement
Face9000

Foreach Include SAMP

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