Advertisement
Y_Less

foreach 0.1.6

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