y_grouponce.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*----------------------------------------------------------------------------*-
  2. =======================================
  3. y_groups - Player group abstractions!
  4. =======================================
  5. Description:
  6. Admin levels, gangs, teams etc - they're all "groups" of people, this
  7. provides an abstraction for all of these collections.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the SA:MP script information include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2008
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk
  24. Thanks:
  25. Peter, Cam - Support.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN.
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 1.0
  44. Changelog:
  45. 29/11/10:
  46. First version
  47. -*----------------------------------------------------------------------------*/
  48. #include <a_samp>
  49. #include <YSI\y_master>
  50. #include <YSI\y_bit>
  51. #include <YSI\y_debug>
  52. //#include <YSI\y_hooks>
  53. #include <YSI\y_scripting>
  54. #include <YSI\y_iterate>
  55. #define ALS_PREFIX Group
  56. #include <YSI\y_als>
  57. //#define _YSI_HAS_GROUP_SYSTEM
  58. //#define _GROUP_MAKE_NAME<%0...%1> %0_%1
  59. // Dummy value
  60. //#define _GROUP_MAKE_LIMIT 0
  61. // Dummy values.
  62. #undef _GROUP_MAKE_NAME
  63. #undef _GROUP_MAKE_LIMIT
  64. #define _GROUP_MAKE_NAME<%0...%1> %0_%1
  65. #define _GROUP_MAKE_LIMIT 0
  66. // Example:
  67. // #define _GROUP_MAKE_NAME<%0...%1> %0Checkpoint%1
  68. /*#if !defined _GROUP_MAKE_NAME
  69. #error Please define _GROUP_MAKE_NAME before including y_groupsone.
  70. #endif
  71. #if !defined _GROUP_MAKE_LIMIT
  72. #error Please define _GROUP_MAKE_LIMIT before including y_groupsone.
  73. #endif*/
  74. // Define local variable names. This will go in the single call file.
  75. //#define _GROUP_LOCAL_NAME _GROUP_MAKE_NAME<i...>
  76. #define _GROUP_GROUP_NAME _GROUP_MAKE_NAME<YSI_gs_Group...Data>
  77. #define _GROUP_GLOBAL_NAME _GROUP_MAKE_NAME<YSI_gs_GroupGlobal...>
  78. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  79. /*#define _GROUP_UPDATE _GROUP_MAKE_NAME<Group_UpdatePlayer...>
  80. #define _GROUP_UPDATE_ALL _GROUP_MAKE_NAME<Group_UpdateAll...>
  81. #define _GROUP_GROUP_FUNC _GROUP_MAKE_NAME<Group_Set...>
  82. #define _GROUP_GLOBAL_FUNC _GROUP_MAKE_NAME<Group_SetGlobal...>
  83. #define _GROUP_GLOBAL_DEF _GROUP_MAKE_NAME<Group_SetGlobal...Default>
  84. #define _GROUP_GROUP_DEF _GROUP_MAKE_NAME<Group_Set...Default>
  85. #define _GROUP_ON_PLAYER_CONNECT _GROUP_MAKE_NAME<@yH_PlayerConnect_...@yG>
  86. #define _GROUP_DEFAULTS _GROUP_MAKE_NAME<Group_...Defaults>
  87. #define _GROUP_INIT _GROUP_MAKE_NAME<Group_...Init>*/
  88. #define _GROUP_CREATE _GROUP_MAKE_NAME<@yG_Init...>
  89. #define _GROUP_UPDATE_PLAYER _GROUP_MAKE_NAME<@yG_Upd...>
  90. //#define _GROUP_ON_PLAYER_CONNECT _GROUP_MAKE_NAME<@yG_..._PlayerConnect>
  91. //#define _GROUP_ON_PLAYER_CONNECT RH:_GROUP_MAKE_NAME<...@yG_OnPlayerConnect>
  92. #define _GROUP_OPC_OTHER_CALLED _GROUP_MAKE_NAME<_@yGPlayerConnect_...>
  93. #define _GROUP_OPC_IS_CALLED _GROUP_MAKE_NAME<_yG@PlayerConnect_...>
  94. #define _GROUP_OPC_PUBLIC _GROUP_MAKE_NAME<@yG_PlayerConnect_...>
  95. #if !defined MAX_GROUP_NAME
  96. #define MAX_GROUP_NAME (24)
  97. #endif
  98. #if !defined MAX_GROUPS
  99. // 1 less than a nice number (with good reason).
  100. #define MAX_GROUPS (Group:127)
  101. #endif
  102. enum e_GROUP_FLAGS (<<= 1)
  103. {
  104. e_GROUP_FLAGS_GANG = 1,
  105. // I can't remember why I had this!
  106. //e_GROUP_FLAGS_CHAT,
  107. e_GROUP_FLAGS_ACTIVE,
  108. e_GROUP_FLAGS_COLOR = 0xFFFFFF00
  109. }
  110. enum E_GROUP_DATA
  111. {
  112. E_GROUP_DATA_NAME[MAX_GROUP_NAME char],
  113. E_GROUP_DATA_HASH,
  114. e_GROUP_FLAGS:E_GROUP_DATA_FLAGS,
  115. }
  116. stock
  117. BitArray:YSI_gGroupPlayers[MAX_GROUPS]<MAX_PLAYERS>,
  118. YSI_gGroupData[MAX_GROUPS][E_GROUP_DATA];
  119. ALS_DATA<>
  120. #define GROUPS_MAX_LIBRARIES 4
  121. static stock
  122. YSI_g_sNextUpdateFunc[32],
  123. YSI_g_sNextInitFunc[32],
  124. YSI_g_sGroupCount,
  125. bool:YSI_g_sHasOPC = false;
  126. #define _Group_HasPlayer(%0,%1) \
  127. Bit_Get(YSI_gGroupPlayers[(%0)], (%1), MAX_PLAYERS)
  128. #define _Group_GetColor(%0) \
  129. (_:(YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_COLOR | e_GROUP_FLAGS:0xAA))
  130. #define _Group_SetColor(%0,%1) \
  131. (YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] |= e_GROUP_FLAGS:(%1) & e_GROUP_FLAGS_COLOR)
  132. #define _Group_GetGang(%0) \
  133. (bool:(YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_GANG))
  134. #define _Group_LetGang(%0) \
  135. (YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] |= e_GROUP_FLAGS_GANG)
  136. #define _Group_VetGang(%0) \
  137. (YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] &= ~e_GROUP_FLAGS_GANG)
  138. #define _Group_IsActive(%0) \
  139. (YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_ACTIVE)
  140. //#define _Group_IsValid(%0)
  141. // (0 <= (%0) < MAX_GROUPS && Group_IsActive(%0))
  142. #define _Group_IsValid(%0) \
  143. (Group:0 <= (%0) < MAX_GROUPS && _Group_IsActive(%0))
  144. #define Group_IsActive(%0) \
  145. (YSI_gGroupData[GROUP_FIX(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_ACTIVE)
  146. /*#define Group_IsValid(%0) \
  147. (GROUP_MASK <= (%0) < (GROUP_MASK + MAX_GROUPS) <= && Group_IsActive(%0))*/
  148. //#define Group_GetColor(%0)
  149. // (YSI_gGroupData[(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_COLOR | 0xAA);
  150. // (0 <= GROUP_FIX(%0) < MAX_GROUPS && Group_IsActive(%0))
  151. #if defined FILTERSCRIPT
  152. public OnFilterScriptInit()
  153. #else
  154. public OnGameModeInit()
  155. #endif
  156. {
  157. state YSI_has_groups:y;
  158. ALS_DETECT<PlayerDisconnect>
  159. ALS_DETECT<PlayerConnect>
  160. YSI_g_sHasOPC = funcidx(#_GROUP_OPC_PUBLIC) != -1;
  161. P:3(#_GROUP_OPC_PUBLIC " does%s exist", YSI_g_sHasOPC ? ("") : (" not"));
  162. P:5("Group_OnScriptInit: OPC = %d", YSI_g_sHasOPC);
  163. new
  164. a = Scripting_GetPublic(0, YSI_g_sNextUpdateFunc, "@yG_Upd"),
  165. idx = Scripting_GetPublic(0, YSI_g_sNextInitFunc, "@yG_Init");
  166. if (idx)
  167. {
  168. CallLocalFunction(YSI_g_sNextInitFunc, "ii", -1, (a << 16) | idx);
  169. }
  170. CallLocalFunction("Group_OnScriptInit", "");
  171. }
  172. #if defined FILTERSCRIPT
  173. #if defined _ALS_OnFilterScriptInit
  174. #undef OnFilterScriptInit
  175. #else
  176. #define _ALS_OnFilterScriptInit
  177. #endif
  178. #define OnFilterScriptInit Group_OnScriptInit
  179. #else
  180. #if defined _ALS_OnGameModeInit
  181. #undef OnGameModeInit
  182. #else
  183. #define _ALS_OnGameModeInit
  184. #endif
  185. #define OnGameModeInit Group_OnScriptInit
  186. #endif
  187. forward Group_OnScriptInit();
  188. // This needs an OnPlayerConnect hook, but an ALS one - I'll sort that later.
  189. RA:Group_OnPlayerDisconnect(playerid, reason)
  190. {
  191. new
  192. s = Bit_Slot(playerid),
  193. Bit:m = ~Bit_Mask(playerid);
  194. for (new Group:i; i != MAX_GROUPS; ++i)
  195. {
  196. // Pre-calculate the slot and mask.
  197. YSI_gGroupPlayers[i][s] &= m;
  198. }
  199. //CallLocalFunction("Group_OnPlayerConnect", "i", playerid);
  200. ALS_CALL<PlayerDisconnect>
  201. }
  202. #if defined _ALS_OnPlayerDisconnect
  203. #undef OnPlayerDisconnect
  204. #else
  205. #define _ALS_OnPlayerDisconnect
  206. #endif
  207. #define OnPlayerDisconnect Group_OnPlayerDisconnect
  208. ALS_FORWARD<PlayerDisconnect>
  209. // This needs an OnPlayerConnect hook, but an ALS one - I'll sort that later.
  210. public OnPlayerConnect(playerid)
  211. {
  212. P:3("Group_OnPlayerConnect called");
  213. if (YSI_g_sHasOPC) CallLocalFunction(#_GROUP_OPC_PUBLIC, "i", playerid);
  214. //CallLocalFunction("Group_OnPlayerConnect", "i", playerid);
  215. ALS_CALL<PlayerConnect>
  216. }
  217. forward _GROUP_OPC_PUBLIC(playerid);
  218. #if defined _ALS_OnPlayerConnect
  219. #undef OnPlayerConnect
  220. #else
  221. #define _ALS_OnPlayerConnect
  222. #endif
  223. #define OnPlayerConnect Group_OnPlayerConnect
  224. ALS_FORWARD<PlayerConnect>
  225. RF@vp:Group_SetPlayer[iii](Group:g,p,bool:s)<g,p,s>
  226. {
  227. //g &= ~GROUP_MASK;
  228. GROUP_FIX(g);
  229. if (_Group_IsValid(g) && 0 <= p < MAX_PLAYERS)
  230. {
  231. Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  232. P:4("Group_SetPlayer: %d %d", g, _:YSI_gGroupPlayers[g][0]);
  233. if (YSI_g_sNextUpdateFunc[0])
  234. {
  235. CallLocalFunction(YSI_g_sNextUpdateFunc, "iii", p, g, s);
  236. }
  237. }
  238. }
  239. RF@pt:bool:Group_GetPlayer[ii](Group:g,p)<g,p>
  240. {
  241. GROUP_FIX(g);
  242. if (_Group_IsValid(g) && 0 <= p < MAX_PLAYERS)
  243. {
  244. return Bit_Get(YSI_gGroupPlayers[g], p);
  245. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  246. }
  247. return false;
  248. }
  249. RF@vp:Group_SetName[is](Group:g,n[])<g,n>
  250. {
  251. GROUP_FIX(g);
  252. if (_Group_IsValid(g))
  253. {
  254. strpack(YSI_gGroupData[g][E_GROUP_DATA_NAME], n, MAX_GROUP_NAME char);
  255. }
  256. }
  257. RS@p:Group_GetName[i](Group:g)<g>
  258. {
  259. new
  260. ret[YSI_MAX_STRING];
  261. GROUP_FIX(g);
  262. if (_Group_IsValid(g))
  263. {
  264. //return Bit_Get(YSI_gGroupPlayers[g], p);
  265. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  266. strunpack(ret, YSI_gGroupData[g][E_GROUP_DATA_NAME], YSI_MAX_STRING);
  267. }
  268. return ret;
  269. }
  270. RF@vp:Group_SetGang[is](Group:g,bool:n)<g,n>
  271. {
  272. GROUP_FIX(g);
  273. if (_Group_IsValid(g))
  274. {
  275. if (n)
  276. {
  277. _Group_LetGang(g);
  278. }
  279. else
  280. {
  281. _Group_VetGang(g);
  282. }
  283. }
  284. }
  285. RF@pt:bool:Group_GetGang[i](Group:g)<g>
  286. {
  287. GROUP_FIX(g);
  288. if (_Group_IsValid(g))
  289. {
  290. return _Group_GetGang(g);
  291. }
  292. return false;
  293. }
  294. RF@vp:Group_SetColour[ii](Group:g,c)<g,c>
  295. {
  296. GROUP_FIX(g);
  297. if (_Group_IsValid(g))
  298. {
  299. _Group_SetColor(g, c);
  300. }
  301. }
  302. RF@p:Group_GetColour[i](Group:g)<g>
  303. {
  304. GROUP_FIX(g);
  305. if (_Group_IsValid(g))
  306. {
  307. return _Group_GetColor(g);
  308. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  309. }
  310. return 0;
  311. }
  312. #define Group_SetColor Group_SetColour
  313. #define Group_GetColor Group_GetColour
  314. RF@pt:Group:Group_Create[s](name[])<name>
  315. {
  316. P:3("Group_Create called in %d", _@);
  317. new
  318. Group:i;
  319. while (i != MAX_GROUPS && _Group_IsActive(i))
  320. {
  321. ++i;
  322. }
  323. if (i == MAX_GROUPS) return GROUP_GLOBAL;
  324. strpack(YSI_gGroupData[i][E_GROUP_DATA_NAME], name, MAX_GROUP_NAME char);
  325. ++YSI_g_sGroupCount;
  326. YSI_gGroupData[i][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS_ACTIVE;
  327. // Now initialise all the little bits. CallREMOTEFunction to get ALL
  328. // scripts which may control part of the group system.
  329. //CallRemoteFunction(#_GROUP_CREATE, "ii", _:i, -1);
  330. if (YSI_g_sNextInitFunc[0])
  331. {
  332. CallLocalFunction(YSI_g_sNextInitFunc, "ii", _:i, -1);
  333. }
  334. return i | GROUP_MASK;
  335. }
  336. //forward _GROUP_CREATE(group);
  337. stock YSI_gSGroup(Group:group)
  338. {
  339. GROUP_FIX(group);
  340. if (!_Group_IsValid(group))
  341. {
  342. return -1;
  343. }
  344. static const
  345. scDeBruijn[] =
  346. {
  347. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  348. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  349. };
  350. new
  351. cur;
  352. for (new i = 0; i != bits<MAX_PLAYERS>; ++i)
  353. {
  354. if ((cur = _:YSI_gGroupPlayers[group][i]))
  355. {
  356. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  357. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  358. }
  359. }
  360. return -1;
  361. }
  362. stock YSI_gAGroupFunc(Group:group, start)
  363. {
  364. GROUP_FIX(group);
  365. if (!_Group_IsValid(group))
  366. {
  367. return -1;
  368. }
  369. static const
  370. scDeBruijn[] =
  371. {
  372. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  373. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  374. };
  375. new
  376. cur,
  377. i = Bit_Slot(start);
  378. if ((cur = (_:YSI_gGroupPlayers[group][i] & (~((1 << (start + 1)) - 1)))))
  379. {
  380. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  381. }
  382. ++i;
  383. while (i != bits<MAX_PLAYERS>)
  384. {
  385. if ((cur = _:YSI_gGroupPlayers[group][i]))
  386. {
  387. return (i * cellbits) + scDeBruijn[((cur & -cur) * 0x077CB531) >>> 27];
  388. }
  389. ++i;
  390. }
  391. return -1;
  392. }
  393. /*stock Group:operator-(Group:group, Group:playerid)
  394. {
  395. Group_SetPlayer(group, playerid, false);
  396. return group;
  397. }
  398. stock Group:operator+(Group:group, playerid)
  399. {
  400. Group_SetPlayer(group, playerid, false);
  401. return group;
  402. }
  403. stock Group:operator==(Group:group, playerid)
  404. {
  405. return Group_GetPlayer(group, playerid);
  406. }*/
  407. #define YSI_gAGroup(%0)[%1] YSI_gAGroupFunc(%0,%1)
  408. #define _YSIM_RESET_USER
  409. #include <YSI\y_master>
  410. #undef ALS_PREFIX