Advertisement
Lorenc

Foreach - Old Version (re-upped)

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