y_playerarray.inc 5.6 KB

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