Guest User

foreach.inc

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