Advertisement
Y_Less

foreach.inc

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