Y_Less

YSI_itterown

Jun 24th, 2008
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.26 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_Random
  193. Params:
  194.     itter - Itterator to get size of.
  195. Return:
  196.     -
  197. Notes:
  198.     Returns a random itterator item.
  199. -*----------------------------------------------------------------------------*/
  200.  
  201. #define Iter_Random Itter_Random
  202. #define Iter_Rand Itter_Random
  203. #define Itter_Rand Itter_Random
  204. #define Itter_Random(%1) \
  205.     Itter_RandInternal(YSI_g%1S, YSI_g%1A, YSI_g%1C)
  206.  
  207. /*----------------------------------------------------------------------------*-
  208. Function:
  209.     Itter_OnPlayerConnect
  210. Params:
  211.     playerid - Player who joined.
  212. Return:
  213.     -
  214. Notes:
  215.     Adds a player to the loop data.  Now sorts the list too.
  216. -*----------------------------------------------------------------------------*/
  217.  
  218. Itter_OnPlayerConnect(playerid)
  219. {
  220.     DBGP2("Itter_OnPlayerConnect() start");
  221.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  222.     Itter_Add(Player, playerid);
  223.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  224.     DBGP2("Itter_OnPlayerConnect() end");
  225.     return 1;
  226. }
  227.  
  228. /*----------------------------------------------------------------------------*-
  229. Function:
  230.     Itter_OnPlayerDisconnect
  231. Params:
  232.     playerid - Player who left.
  233. Return:
  234.     -
  235. Notes:
  236.     Removes a player from the loop data.
  237. -*----------------------------------------------------------------------------*/
  238.  
  239. Itter_OnPlayerDisconnect(playerid)
  240. {
  241.     DBGP2("Itter_OnPlayerDisconnect() start");
  242.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  243.     Itter_Remove(Player, playerid);
  244.     DBGC3(Itter_ShowArray(YSI_gPlayerS, YSI_gPlayerA, MAX_PLAYERS););
  245.     DBGP2("Itter_OnPlayerDisconnect() end");
  246.     return 1;
  247. }
  248.  
  249. /*----------------------------------------------------------------------------*-
  250. Function:
  251.     Itter_ShowArray
  252. Params:
  253.     start - Itterator start point.
  254.     members[] - Itterator contents.
  255.     size - Number of itterator values
  256. Return:
  257.     -
  258. Notes:
  259.     Pure debug function.  Has regular prints not debug prints
  260.     as it's only called when debug is on.
  261. -*----------------------------------------------------------------------------*/
  262.  
  263. stock Itter_ShowArray(start, members[], size)
  264. {
  265.     static
  266.         sString[61];
  267.     new
  268.         i,
  269.         j = 10;
  270.     DBGP2("Itter_ShowArray() start");
  271.     printf("Start: %d", start);
  272.     while (i < size)
  273.     {
  274.         sString[0] = '\0';
  275.         while (i < j && i < size)
  276.         {
  277.             format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  278.             i++;
  279.         }
  280.         printf("Array (%d): %s", j, sString);
  281.         j += 10;
  282.     }
  283. }
  284.  
  285. /*----------------------------------------------------------------------------*-
  286. Function:
  287.     Itter_AddInternal
  288. Params:
  289.     &start - Array start index.
  290.     array[] - Itterator data.
  291.     value - Item to add.
  292.     &count - Itterator size.
  293. Return:
  294.     -
  295. Notes:
  296.     Adds a value to a given itterator set.
  297. -*----------------------------------------------------------------------------*/
  298.  
  299. stock Itter_AddInternal(&start, array[], value, &count)
  300. {
  301.     if (array[value] != -1)
  302.     {
  303.         DBGP1("Itter_Add() Value error.");
  304.         return 0;
  305.     }
  306.     count++;
  307.     #if defined YSI_ITTER_NO_SORT
  308.         array[value] = start;
  309.         start = value;
  310.     #else
  311.         if (start == -1)
  312.         {
  313.             start = value;
  314.         }
  315.         else if (start > value)
  316.         {
  317.             array[value] = start;
  318.             start = value;
  319.         }
  320.         else
  321.         {
  322.             new
  323.                 cur = start,
  324.                 last;
  325.             do
  326.             {
  327.                 last = cur;
  328.                 cur = array[cur];
  329.                 if (cur > value)
  330.                 {
  331.                     array[value] = cur;
  332.                     array[last] = value;
  333.                     return 1;
  334.                 }
  335.             }
  336.             while (cur != -1);
  337.             array[last] = value;
  338.         }
  339.     #endif
  340.     return 1;
  341. }
  342.  
  343. /*----------------------------------------------------------------------------*-
  344. Function:
  345.     Itter_AddInternal
  346. Params:
  347.     &start - Array start index.
  348.     array[] - Itterator data.
  349.     value - Item to remove.
  350.     &count - Itterator size.
  351. Return:
  352.     -
  353. Notes:
  354.     Removes a value from an itterator.
  355. -*----------------------------------------------------------------------------*/
  356.  
  357. stock Itter_RemoveInternal(&start, array[], value, &count)
  358. {
  359.     if (start == -1)
  360.     {
  361.         return 0;
  362.     }
  363.     if (start == value)
  364.     {
  365.         start = array[value];
  366.     }
  367.     else
  368.     {
  369.         new
  370.             cur = start;
  371.         while (array[cur] != value)
  372.         {
  373.             cur = array[cur];
  374.             if (cur == -1)
  375.             {
  376.                 return 0;
  377.             }
  378.         }
  379.         array[cur] = array[value];
  380.     }
  381.     array[value] = -1;
  382.     count--;
  383.     return 1;
  384. }
  385.  
  386. /*----------------------------------------------------------------------------*-
  387. Function:
  388.     Itter_RandInternal
  389. Params:
  390.     start - Array start index.
  391.     array[] - Itterator data.
  392.     count - Itterator size.
  393. Return:
  394.     -
  395. Notes:
  396.     Gets a random item from the itterator.
  397. -*----------------------------------------------------------------------------*/
  398.  
  399. stock Itter_RandInternal(start, array[], count)
  400. {
  401.     new
  402.         rand = random(count),
  403.         i = start,
  404.         j = 0;
  405.     while (i != -1 && j++ < rand)
  406.     {
  407.         i = array[i];
  408.     }
  409.     return i;
  410. }
Advertisement
Add Comment
Please, Sign In to add comment