y_playerarray.inc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**--------------------------------------------------------------------------**\
  2. ========================================
  3. y_playerarray - Bit arrays of players!
  4. ========================================
  5. Description:
  6. This code provides arrays of players who can do things. This is for support
  7. of the text system which can take arrays of player ids, bit arrays or just a
  8. single ID.
  9. Legal:
  10. Version: MPL 1.1
  11. The contents of this file are subject to the Mozilla Public License Version
  12. 1.1 (the "License"); you may not use this file except in compliance with
  13. the License. You may obtain a copy of the License at
  14. http://www.mozilla.org/MPL/
  15. Software distributed under the License is distributed on an "AS IS" basis,
  16. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  17. for the specific language governing rights and limitations under the
  18. License.
  19. The Original Code is the YSI playerarray include.
  20. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  21. Portions created by the Initial Developer are Copyright (C) 2011
  22. the Initial Developer. All Rights Reserved.
  23. Contributors:
  24. ZeeX, koolk
  25. Thanks:
  26. Peter, Cam - Support.
  27. ZeeX - Very productive conversations.
  28. koolk - IsPlayerinAreaEx code.
  29. TheAlpha - Danish translation.
  30. breadfish - German translation.
  31. Fireburn - Dutch translation.
  32. yom - French translation.
  33. 50p - Polish translation.
  34. Zamaroht - Spanish translation.
  35. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  36. for me to strive to better.
  37. Pixels^ - Running XScripters where the idea was born.
  38. Matite - Pestering me to release it and using it.
  39. Very special thanks to:
  40. Thiadmer - PAWN.
  41. Kye/Kalcor - SA:MP.
  42. SA:MP Team past, present and future - SA:MP.
  43. Version:
  44. 1.0
  45. Changelog:
  46. 25/02/11:
  47. Scrapped pointless "playing" code I was unhappy with.
  48. 29/11/10:
  49. First version
  50. </remarks>
  51. \**--------------------------------------------------------------------------**/
  52. #if defined _INC_y_playerarray
  53. #endinput
  54. #endif
  55. #define _INC_y_playerarray
  56. #include "..\YSI_Internal\y_version"
  57. #include <a_samp>
  58. #include "..\YSI_Core\y_debug"
  59. #include "..\YSI_Data\y_bit"
  60. #define PA_TYPE_NONE (-1)
  61. #define PA_TYPE_BOOL (-2)
  62. #define PA_TYPE_ID (-3)
  63. #define PA_TYPE_PA (-4)
  64. #if defined _YSI_SPECIAL_DEBUG
  65. #define PS_IS_PLAYER_CONNECTED(%0) (%0 != INVALID_PLAYER_ID)
  66. #else
  67. #define PS_IS_PLAYER_CONNECTED IsPlayerConnected
  68. #endif
  69. // Redefined later on. This is just for internal use.
  70. #define PlayerArray:%0<%1> Bit:%0[%1]
  71. #define PA_INIT:%1, Bit:PA_TYPE_PA,Bit:(-1*_:%1),Bit:(-1*_:%1),
  72. stock PA_Init(Bit:a[], bool:init = false, s = bits<MAX_PLAYERS>)
  73. {
  74. P:3("PA_Init called: %i, %i, %i", _:a, _:init, s);
  75. //--s;
  76. new
  77. Bit:m = init ? (Bit:-1) : (Bit:0);
  78. a[0] = Bit:PA_TYPE_PA;
  79. while (s)
  80. {
  81. a[s--] = m;
  82. }
  83. }
  84. #define PA_FastInit(%0) %0[0]=Bit:PA_TYPE_PA
  85. #define PA_GetBit(%1,%2) (%1[((%2)>>>CELLSHIFT)+1]&Bit:(1<<((%2)&cellbits-1)))
  86. #define PA_Get(%1,%2) bool:PA_GetBit(Bit:%1,_:%2)
  87. #define PA_Let(%1,%2) %1[((%2)>>>CELLSHIFT)+1]|=Bit:(1<<((%2)&cellbits-1))
  88. #define PA_Vet(%1,%2) %1[((%2)>>>CELLSHIFT)+1]&=Bit:~(1<<((%2)&cellbits-1))
  89. #define PA_GetCount(%1) Bit_GetCount(%1[1],bits<MAX_PLAYERS>)
  90. #define PA_Slot(%0) (Bit_Slot(%0) + 1)
  91. #define PA_Mask(%0) (Bit_Mask(%0))
  92. //#define PA_Set(%0,%1,%2) ((%2)?Bit_Let(%0,(%1)):Bit_Vet(%0,(%1)))
  93. stock PA_Set(PlayerArray:d<>, slot, bool:set)
  94. {
  95. P:3("PA_Set called: %s, %i, %i", Bit_Display(Bit:d[1]), slot, _:set);
  96. if (set)
  97. {
  98. PA_Let(d, slot);
  99. return 1;
  100. }
  101. else
  102. {
  103. PA_Vet(d, slot);
  104. return 0;
  105. }
  106. }
  107. stock Iter_Func@PA(start, PlayerArray:data<>)
  108. {
  109. P:3("YSI_gAPAFunc called: %s, %i", Bit_Display(Bit:data[1]), start);
  110. ++start;
  111. YSI_gAPAFunc_loop:
  112. new
  113. cur,
  114. i = Bit_Slot(start) + 1;
  115. if ((cur = (_:data[i] & (~((1 << start) - 1)))))
  116. {
  117. new
  118. ret = ((i - 1) * cellbits) + Cell_GetLowestBit(cur);
  119. // I'd like to replace this with code which doesn't call
  120. // IsPlayerConnected and doesn't use "goto", but it avoids massive tail
  121. // recursion (well, it is tail recursion which I've inlined).
  122. if (PS_IS_PLAYER_CONNECTED(ret))
  123. {
  124. return ret;
  125. }
  126. else
  127. {
  128. //return YSI_gAPAFunc(data, ret);
  129. start = ret + 1;
  130. goto YSI_gAPAFunc_loop;
  131. }
  132. }
  133. ++i;
  134. while (i != bits<MAX_PLAYERS> + 1)
  135. {
  136. if ((cur = _:data[i]))
  137. {
  138. new
  139. ret = ((i - 1) * cellbits) + Cell_GetLowestBit(cur);
  140. if (PS_IS_PLAYER_CONNECTED(ret))
  141. {
  142. return ret;
  143. }
  144. else
  145. {
  146. //return YSI_gAPAFunc(data, ret);
  147. start = ret + 1;
  148. goto YSI_gAPAFunc_loop;
  149. }
  150. }
  151. ++i;
  152. }
  153. return cellmin;
  154. }
  155. #define iterstart@PA -1
  156. #undef PlayerArray
  157. #define PlayerArray:%0<%1> Bit:%0[bits<%1>+1]//={Bit:-1}