Y_Less

YSI_Itter 02

Apr 18th, 2008
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.80 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*-
  2.                     ==================================
  3.                     Y Server Includes - Itterator Core
  4.                     ==================================
  5. Description:
  6.     Provides functionality to loop efficiently through all connected players.
  7.     Removes reliance on both modification of MAX_PLAYERS for more efficient
  8.     processing on small servers (although still recommended) and
  9.     IsPlayerConnected.
  10. Legal:
  11.     Copyright (C) 2007 Alex "Y_Less" Cole
  12.  
  13.     This program is free software; you can redistribute it and/or
  14.     modify it under the terms of the GNU General Public License
  15.     as published by the Free Software Foundation; either version 2
  16.     of the License, or (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  26.     MA 02110-1301, USA.
  27. Version:
  28.     0.2
  29. Changelog:
  30.     18/04/08:
  31.         Added count information.
  32.         Added random function.
  33.     06/01/08:
  34.         Added debug information.
  35.     09/10/07:
  36.         Moved to system.
  37.     16/09/07:
  38.         Added list sorting.
  39.         Made this part of Y SeRver Includes, not Y Sever Includes.
  40.         Made list sorting optional.
  41.         Fixed version number.
  42.     08/09/07:
  43.         First version.
  44. Functions:
  45.     Public:
  46.         -
  47.     Core:
  48.         Itter_OnPlayerDisconnect - Called when a player leaves to remove them.
  49.         Itter_OnPlayerConnect - Called when a player connects to add them.
  50.     Stock:
  51.         Itter_ShowArray - Displays the contents of the array.
  52.         Itter_AddInternal - Add a value to an itterator.
  53.         Itter_RemoveInternal - Remove a value from an itterator.
  54.     Static:
  55.         -
  56.     Inline:
  57.         Itter_Create - Create a new itterator value set.
  58.         Itter_Add - Wraps Itter_AddInternal.
  59.         Itter_Remove - Wraps Itter_RemoveInternal.
  60.         Itter_Count - Gets the number of items in an itterator.
  61.         Itter_Rand - Gets a random item from an itterator.
  62.     API:
  63.         -
  64. Callbacks:
  65.     -
  66. Definitions:
  67.     -
  68. Enums:
  69.     -
  70. Macros:
  71.     foreach - Command to loop through all connected players efficiently.
  72.     foreachex - Like foreach but without a new variable.
  73. Tags:
  74.     -
  75. Variables:
  76.     Global:
  77.         YSI_gPlayerS - Start index of connected players.
  78.         YSI_gPlayerA - List of connected players.
  79.     Static:
  80.         -
  81. Commands:
  82.     -
  83. Compile options:
  84.     YSI_ITTER_NO_SORT - Doesn't sort the list of players on connect.
  85. Operators:
  86.     -
  87. -*----------------------------------------------------------------------------*/
  88.  
  89. /*----------------------------------------------------------------------------*-
  90. Function:
  91.     Itter_Create
  92. Params:
  93.     name - Itterator identifier.
  94.     size - Number of values.
  95. Return:
  96.     -
  97. Notes:
  98.     Creates a new itterator start/array pair.
  99. -*----------------------------------------------------------------------------*/
  100.  
  101. #define Iter_Create Itter_Create
  102. #define Itter_Create(%1,%2) \
  103.     new \
  104.         YSI_g%1S = -1, \
  105.         YSI_g%1A[%2] = {-1, ...} \
  106.         YSI_g%1C = 0
  107.  
  108. /*----------------------------------------------------------------------------*-
  109. Function:
  110.     Itter_Add
  111. Params:
  112.     itter - Name of the itterator to add the data to.
  113.     value - Value to add to the itterator.
  114. Return:
  115.     -
  116. Notes:
  117.     Wrapper for Itter_AddInternal
  118. -*----------------------------------------------------------------------------*/
  119.  
  120. #define Iter_Add Itter_Add
  121. #define Itter_Add(%1,%2) \
  122.     Itter_AddInternal(YSI_g%1S, YSI_g%1A, %2, YSI_g%1C)
  123.  
  124. /*----------------------------------------------------------------------------*-
  125. Function:
  126.     Itter_Remove
  127. Params:
  128.     itter - Name of the itterator to remove data from.
  129.     value - Data to remove.
  130. Return:
  131.     -
  132. Notes:
  133.     Wrapper for Itter_RemoveInternal
  134. -*----------------------------------------------------------------------------*/
  135.  
  136. #define Iter_Remove Itter_Remove
  137. #define Itter_Remove(%1,%2) \
  138.     Itter_RemoveInternal(YSI_g%1S, YSI_g%1A, %2, YSI_g%1C)
  139.  
  140. Itter_Create(Player, MAX_PLAYERS);
  141.  
  142. /*----------------------------------------------------------------------------*-
  143. Function:
  144.     foreach
  145. Params:
  146.     data - Data to itterate through.
  147.     as - Variable to set value to.
  148. Return:
  149.     -
  150. Notes:
  151.     Not exactly the same as PHP foreach, just itterates through a list and
  152.     returns the value of the current slot but uses that slot as the next index
  153.     too.  Variables must be in the form YSI_g<name>S for the start index and
  154.     YSI_g<name>A for the data array where <name> is what's entered in data.
  155. -*----------------------------------------------------------------------------*/
  156.  
  157. #define foreach(%1,%2) \
  158.     for (new %2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  159.  
  160. /*----------------------------------------------------------------------------*-
  161. Function:
  162.     foreachex
  163. Params:
  164.     data - Data to itterate through.
  165.     as - Variable to set value to.
  166. Return:
  167.     -
  168. Notes:
  169.     Similar to foreach but doesn't declare a new variable for the itterator.
  170. -*----------------------------------------------------------------------------*/
  171.  
  172. #define foreachex(%1,%2) \
  173.     for (%2 = YSI_g%1S; %2 != -1; %2 = YSI_g%1A[%2])
  174.  
  175. /*----------------------------------------------------------------------------*-
  176. Function:
  177.     Itter_Count
  178. Params:
  179.     itter - Itterator to get size of.
  180. Return:
  181.     -
  182. Notes:
  183.     Gets the number of items in the current itterator.
  184. -*----------------------------------------------------------------------------*/
  185.  
  186. #define Iter_Count Itter_Count
  187. #define Itter_Count(%1) \
  188.     (YSI_g%1C)
  189.  
  190. /*----------------------------------------------------------------------------*-
  191. Function:
  192.     Itter_OnPlayerConnect
  193. Params:
  194.     playerid - Player who joined.
  195. Return:
  196.     -
  197. Notes:
  198.     Adds a player to the loop data.  Now sorts the list too.
  199. -*----------------------------------------------------------------------------*/
  200.  
  201. Itter_OnPlayerConnect(playerid)
  202. {
  203.     DBGP2("Itter_OnPlayerConnect() start");
  204.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  205.     Itter_Add(Player, playerid);
  206.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  207.     DBGP2("Itter_OnPlayerConnect() end");
  208.     return 1;
  209. }
  210.  
  211. /*----------------------------------------------------------------------------*-
  212. Function:
  213.     Itter_OnPlayerDisconnect
  214. Params:
  215.     playerid - Player who left.
  216. Return:
  217.     -
  218. Notes:
  219.     Removes a player from the loop data.
  220. -*----------------------------------------------------------------------------*/
  221.  
  222. Itter_OnPlayerDisconnect(playerid)
  223. {
  224.     DBGP2("Itter_OnPlayerDisconnect() start");
  225.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  226.     Itter_Remove(Player, playerid);
  227.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  228.     DBGP2("Itter_OnPlayerDisconnect() end");
  229.     return 1;
  230. }
  231.  
  232. /*----------------------------------------------------------------------------*-
  233. Function:
  234.     Itter_ShowArray
  235. Params:
  236.     start - Itterator start point.
  237.     members[] - Itterator contents.
  238.     size - Number of itterator values
  239. Return:
  240.     -
  241. Notes:
  242.     Pure debug function.  Has regular prints not debug prints
  243.     as it's only called when debug is on.
  244. -*----------------------------------------------------------------------------*/
  245.  
  246. stock Itter_ShowArray(start, members[], size)
  247. {
  248.     static
  249.         sString[61];
  250.     new
  251.         i,
  252.         j = 10;
  253.     DBGP2("Itter_ShowArray() start");
  254.     printf("Start: %d", start);
  255.     while (i < size)
  256.     {
  257.         sString[0] = '\0';
  258.         while (i < j && i < size)
  259.         {
  260.             format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  261.             i++;
  262.         }
  263.         printf("Array (%d): %s", j, sString);
  264.         j += 10;
  265.     }
  266. }
  267.  
  268. /*----------------------------------------------------------------------------*-
  269. Function:
  270.     Itter_AddInternal
  271. Params:
  272.     &start - Array start index.
  273.     array[] - Itterator data.
  274.     value - Item to add.
  275.     &count - Itterator size.
  276. Return:
  277.     -
  278. Notes:
  279.     Adds a value to a given itterator set.
  280. -*----------------------------------------------------------------------------*/
  281.  
  282. stock Itter_AddInternal(&start, array[], value, &count)
  283. {
  284.     if (array[value] != -1)
  285.     {
  286.         DBGP1("Itter_Add() Value error.");
  287.         return 0;
  288.     }
  289.     &count++;
  290.     #if defined YSI_ITTER_NO_SORT
  291.         array[value] = start;
  292.         start = value;
  293.     #else
  294.         if (start == -1)
  295.         {
  296.             start = value;
  297.         }
  298.         else if (start > value)
  299.         {
  300.             array[value] = start;
  301.             start = value;
  302.         }
  303.         else
  304.         {
  305.             new
  306.                 cur = start,
  307.                 last;
  308.             do
  309.             {
  310.                 last = cur;
  311.                 cur = array[cur];
  312.                 if (cur > value)
  313.                 {
  314.                     array[value] = cur;
  315.                     array[last] = value;
  316.                     return 1;
  317.                 }
  318.             }
  319.             while (cur != -1);
  320.             array[last] = value;
  321.         }
  322.     #endif
  323.     return 1;
  324. }
  325.  
  326. /*----------------------------------------------------------------------------*-
  327. Function:
  328.     Itter_AddInternal
  329. Params:
  330.     &start - Array start index.
  331.     array[] - Itterator data.
  332.     value - Item to remove.
  333.     &count - Itterator size.
  334. Return:
  335.     -
  336. Notes:
  337.     Removes a value from an itterator.
  338. -*----------------------------------------------------------------------------*/
  339.  
  340. stock Itter_RemoveInternal(&start, array[], value, &count)
  341. {
  342.     if (start == -1)
  343.     {
  344.         return 0;
  345.     }
  346.     if (start == value)
  347.     {
  348.         start = array[value];
  349.     }
  350.     else
  351.     {
  352.         new
  353.             cur = start;
  354.         while (array[cur] != value)
  355.         {
  356.             cur = array[cur];
  357.             if (cur == -1)
  358.             {
  359.                 return 0;
  360.             }
  361.         }
  362.         array[cur] = array[value];
  363.     }
  364.     array[value] = -1;
  365.     count--;
  366.     return 1;
  367. }
  368.  
  369. /*----------------------------------------------------------------------------*-
  370. Function:
  371.     Itter_RandInternal
  372. Params:
  373.     start - Array start index.
  374.     array[] - Itterator data.
  375.     count - Itterator size.
  376. Return:
  377.     -
  378. Notes:
  379.     Gets a random item from the itterator.
  380. -*----------------------------------------------------------------------------*/
  381.  
  382. stock Itter_RandInternal(start, array[], count)
  383. {
  384.     new
  385.         rand = random(count),
  386.         i = start,
  387.         j = 0;
  388.     while (i != -1 && j++ < rand)
  389.     {
  390.         i = array[i];
  391.     }
  392.     return i;
  393. }
  394.  
Advertisement
Add Comment
Please, Sign In to add comment