y_playerarray.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. \*----------------------------------------------------------------------------*/
  51. #include "internal\y_version"
  52. #include <a_samp>
  53. #include "y_debug"
  54. #include "y_bit"
  55. #define PA_TYPE_NONE (-1)
  56. #define PA_TYPE_BOOL (-2)
  57. #define PA_TYPE_ID (-3)
  58. #define PA_TYPE_PA (-4)
  59. #if defined _YSI_SPECIAL_DEBUG
  60. #define PS_IS_PLAYER_CONNECTED(%0) (%0 != INVALID_PLAYER_ID)
  61. #else
  62. #define PS_IS_PLAYER_CONNECTED IsPlayerConnected
  63. #endif
  64. #define PlayerArray:%0<%1> Bit:%0[%1] //={Bit:PA_TYPE_PA,Bit:0,Bit:0,...}
  65. //#define Bit:%0[%1,%2] Bit:%0[%1]={Bit:PA_TYPE_PA,Bit:(-1*_:%2),Bit:(-1*_:%2),...}
  66. #define PA_INIT:%1, Bit:PA_TYPE_PA,Bit:(-1*_:%1),Bit:(-1*_:%1),
  67. stock PA_Init(Bit:a[], bool:init = false, s = bits<MAX_PLAYERS>)
  68. {
  69. P:3("PA_Init called: %i, %i, %i", _:a, _:init, s);
  70. //--s;
  71. new
  72. Bit:m = init ? (Bit:-1) : (Bit:0);
  73. a[0] = Bit:PA_TYPE_PA;
  74. while (s)
  75. {
  76. a[s--] = m;
  77. }
  78. }
  79. #define PA_FastInit(%0) %0[0]=Bit:PA_TYPE_PA
  80. #define PA_GetBit(%1,%2) (%1[((%2)>>>CELLSHIFT)+1]&Bit:(1<<((%2)&cellbits-1)))
  81. #define PA_Get(%1,%2) bool:PA_GetBit(Bit:%1,_:%2)
  82. #define PA_Let(%1,%2) %1[((%2)>>>CELLSHIFT)+1]|=Bit:(1<<((%2)&cellbits-1))
  83. #define PA_Vet(%1,%2) %1[((%2)>>>CELLSHIFT)+1]&=Bit:~(1<<((%2)&cellbits-1))
  84. #define PA_GetCount(%1) Bit_GetCount(%1[1],bits<MAX_PLAYERS>)
  85. //#define PA_Set(%0,%1,%2) ((%2)?Bit_Let(%0,(%1)):Bit_Vet(%0,(%1)))
  86. stock PA_Set(PlayerArray:d<>, slot, bool:set)
  87. {
  88. P:3("PA_Set called: %s, %i, %i", Bit_Display(Bit:d[1]), slot, _:set);
  89. if (set)
  90. {
  91. PA_Let(d, slot);
  92. return 1;
  93. }
  94. else
  95. {
  96. PA_Vet(d, slot);
  97. return 0;
  98. }
  99. }
  100. /*stock YSI_gSPAFunc(PlayerArray:data<>)
  101. {
  102. P:3("YSI_gSPA called: %s", Bit_Display(Bit:data[1]));
  103. static const
  104. scDeBruijn[] =
  105. {
  106. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  107. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  108. };
  109. new
  110. cur;
  111. for (new i = 1; i != bits<MAX_PLAYERS> + 1; ++i)
  112. {
  113. if ((cur = _:data[i]))
  114. {
  115. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  116. new
  117. ret = ((i - 1) * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  118. if (PS_IS_PLAYER_CONNECTED(ret))
  119. {
  120. return ret;
  121. }
  122. else
  123. {
  124. return YSI_gAPAFunc(data, ret);
  125. }
  126. }
  127. }
  128. return -1;
  129. }*/
  130. stock PA@YSII_Ag(PlayerArray:data<>, start)
  131. {
  132. P:3("YSI_gAPAFunc called: %s, %i", Bit_Display(Bit:data[1]), start);
  133. static const
  134. scDeBruijn[] =
  135. {
  136. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  137. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  138. };
  139. ++start;
  140. YSI_gAPAFunc_loop:
  141. new
  142. cur,
  143. i = Bit_Slot(start) + 1;
  144. if ((cur = (_:data[i] & (~((1 << start) - 1)))))
  145. {
  146. new
  147. ret = ((i - 1) * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  148. // I'd like to replace this with code which doesn't call
  149. // IsPlayerConnected and doesn't use "goto", but it avoids massive tail
  150. // recursion (well, it is tail recursion which I've inlined).
  151. if (PS_IS_PLAYER_CONNECTED(ret))
  152. {
  153. return ret;
  154. }
  155. else
  156. {
  157. //return YSI_gAPAFunc(data, ret);
  158. start = ret + 1;
  159. goto YSI_gAPAFunc_loop;
  160. }
  161. }
  162. ++i;
  163. while (i != bits<MAX_PLAYERS> + 1)
  164. {
  165. if ((cur = _:data[i]))
  166. {
  167. new
  168. ret = ((i - 1) * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  169. if (PS_IS_PLAYER_CONNECTED(ret))
  170. {
  171. return ret;
  172. }
  173. else
  174. {
  175. //return YSI_gAPAFunc(data, ret);
  176. start = ret + 1;
  177. goto YSI_gAPAFunc_loop;
  178. }
  179. }
  180. ++i;
  181. }
  182. return -1;
  183. }
  184. //#define YSI_gAPA(%0)[%1] YSI_gAPAFunc(%0,%1)
  185. //#define YSI_gSPS YSI_gSPA
  186. //#define YSI_gAPS YSI_gAPA
  187. //#define PA(%0)_YSII_Sg;_:%1!=-1;%2=_Y_ITER_ARRAY:%8PA(%3)_YSII_Ag[%4] YSI_gSPAFunc(%0);_:%1!=-1;%2=YSI_gAPAFunc(%3,%4)
  188. #undef PlayerArray
  189. #define PlayerArray:%0<%1> Bit:%0[bits<%1>+1]//={Bit:-1}