_funcs.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 YSI group include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  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, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 2.0
  44. Changelog:
  45. 14/04/13:
  46. Reduced complexity for different include styles.
  47. 21/10/12:
  48. Added Group_Get... functions.
  49. 20/10/12:
  50. Pretty much a ground-up API-compatible rewrite. Shorter and reliabler.
  51. 29/11/10:
  52. First version
  53. \**--------------------------------------------------------------------------**/
  54. /*
  55. ad88888ba
  56. d8" "8b ,d
  57. Y8, 88
  58. `Y8aaaaa, ,adPPYba, MM88MMM 88 88 8b,dPPYba,
  59. `"""""8b, a8P_____88 88 88 88 88P' "8a
  60. `8b 8PP""""""" 88 88 88 88 d8
  61. Y8a a8P "8b, ,aa 88, "8a, ,a88 88b, ,a8"
  62. "Y88888P" `"Ybbd8"' "Y888 `"YbbdP'Y8 88`YbbdP"'
  63. 88
  64. 88
  65. */
  66. #include "..\..\YSI_Internal\y_compilerpass"
  67. #if AUTO_INCLUDE_GUARD
  68. #undef _inc__funcs
  69. #endif
  70. #if !defined _INC_y_groups__funcs
  71. #define _INC_y_groups__funcs
  72. #include "setup"
  73. #include "..\..\YSI_Internal\y_shortfunc"
  74. #include "..\..\YSI_Data\y_iterate"
  75. #endif
  76. #if !defined _GROUP_MAKE_TAG
  77. #define _GROUP_MAKE_TAG _
  78. #endif
  79. // Start of the multi-use file.
  80. static stock
  81. YSI_g_sMaxEncountered = -1,
  82. BitArray:YSI_g_sElementMembership[_GROUP_MAKE_LIMIT]<_:_MAX_GROUPS_G>,
  83. BitArray:YSI_g_sDefaultMembership<_:_MAX_GROUPS_G>,
  84. BitArray:YSI_g_sEmpty<_:_MAX_GROUPS_G>,
  85. _yGI,
  86. _yGU,
  87. _yGA;
  88. // Define the callback chaining for the current
  89. #if defined _YSI_HAS_y_groups
  90. #define gforeign%1(%2); _GROUP_MAKE_NAME<foreign%1>(%2);
  91. #define gglobal%1(%2) _GROUP_MAKE_NAME<global%1>(%2)
  92. #define gpublic master_func
  93. #if YSIM_HAS_MASTER
  94. #if _YSIM_IS_CLIENT
  95. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)
  96. // Should never be needed.
  97. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Client_Set>
  98. #elseif _YSIM_IS_SERVER
  99. #define _gchain%0(%2) _GROUP_MAKE_NAME<%0...>(%2);public _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  100. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  101. #elseif _YSIM_IS_STUB
  102. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)
  103. // Because of the interaction between the various things that the
  104. // groups and master systems this will NEVER be called in a stub
  105. // script - and if it is we now get a nice compile-time error
  106. // telling us so! It may, however, prove difficult to trace!
  107. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Stub_Set>
  108. #else
  109. #define _gchain%0(%2) _GROUP_MAKE_NAME<%0...>(%2);public _GROUP_MAKE_NAME<%0...>(%2)<>GROUP_CHAIN?<%0>(%2);public _GROUP_MAKE_NAME<%0...>(%2)<_YCM:y>for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  110. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  111. #endif
  112. #else
  113. #define _gchain%0(%2) _GROUP_MAKE_NAME<%0...>(%2);public _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  114. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  115. #endif
  116. #elseif COMPILER_1ST_PASS
  117. #if YSIM_HAS_MASTER
  118. #if _YSIM_IS_CLIENT
  119. #define gforeign%0...%1(%2); stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;stock _GROUP_MAKE_NAME<%0...%1>(%2)<>_UNUSED(%2);
  120. #define gglobal%0...%1(%2) stock _GROUP_MAKE_NAME<%0...%1_>(%2)
  121. #define gpublic static stock
  122. #define _gchain%0(%2) stock _GROUP_MAKE_NAME<%0...>(%2)
  123. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Client_Set>
  124. #elseif _YSIM_IS_SERVER
  125. #define gforeign%0...%1(%2); _GROUP_MAKE_NAME<%0...%1>(%2);static stock _GROUP_MAKE_NAME<%0...%1@>(%2)(_GROUP_MAKE_NAME<%0...%1>(%2));
  126. #define gglobal%0...%1(%2) stock _GROUP_MAKE_NAME<%0...%1>(%2)
  127. #define gpublic stock
  128. #define _gchain%0(%2) stock _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  129. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  130. #elseif _YSIM_IS_STUB
  131. #define gforeign%0...%1(%2); stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;stock _GROUP_MAKE_NAME<%0...%1>(%2)<>;
  132. #define gglobal%0...%1(%2)
  133. #define gpublic static stock
  134. #define _gchain%0(%2) stock _GROUP_MAKE_NAME<%0...>(%2)
  135. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Stub_Set>
  136. #else
  137. #define gforeign%0...%1(%2); stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;stock _GROUP_MAKE_NAME<%0...%1>(%2)<>_UNUSED(%2);static stock _GROUP_MAKE_NAME<%0...%1@>(%2)<_YCM:y>(_GROUP_MAKE_NAME<%0...%1>(%2));
  138. #define gglobal%0...%1(%2) static stock _GROUP_MAKE_NAME<%0...%1@>(%2)<>(_GROUP_MAKE_NAME<%0...%1>(%2)); stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:y>
  139. #define gpublic%0(%1)<%9> stock%0(%1)<>{}stock%0(%1)<_YCM:%9>
  140. #define _gchain%0(%2) stock _GROUP_MAKE_NAME<%0...>(%2)<>GROUP_CHAIN?<%0>(%2);stock _GROUP_MAKE_NAME<%0...>(%2)<_YCM:y>for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  141. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  142. #endif
  143. #else
  144. #define gforeign%0...%1(%2);
  145. #define gglobal%0...%1(%2) stock _GROUP_MAKE_NAME<%0...%1>(%2)
  146. #define gglobal%0...%1(%2) stock _GROUP_MAKE_NAME<%0...%1>(%2)
  147. #define gpublic stock
  148. #define _gchain%0(%2) stock _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  149. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  150. #endif
  151. #else
  152. #if YSIM_HAS_MASTER
  153. #if _YSIM_IS_CLIENT
  154. #define gforeign%0...%1(%2); static stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;static stock _GROUP_MAKE_NAME<%0...%1>(%2)<>_UNUSED(%2);
  155. #define gglobal%0...%1(%2) static stock _GROUP_MAKE_NAME<%0...%1_>(%2)
  156. #define gpublic static stock
  157. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)
  158. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Client_Set>
  159. #elseif _YSIM_IS_SERVER
  160. #define gforeign%0...%1(%2); _GROUP_MAKE_NAME<%0...%1>(%2);static stock _GROUP_MAKE_NAME<%0...%1@>(%2)(_GROUP_MAKE_NAME<%0...%1>(%2));
  161. #define gglobal%0...%1(%2) static stock _GROUP_MAKE_NAME<%0...%1>(%2)
  162. #define gpublic static stock
  163. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  164. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  165. #elseif _YSIM_IS_STUB
  166. #define gforeign%0...%1(%2); static stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;static stock _GROUP_MAKE_NAME<%0...%1>(%2)<>;
  167. #define gglobal%0...%1(%2)
  168. #define gpublic static stock
  169. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)
  170. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<_Error_..._Stub_Set>
  171. #else
  172. #define gforeign%0...%1(%2); static stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:m>;static stock _GROUP_MAKE_NAME<%0...%1>(%2)<>_UNUSED(%2);static stock _GROUP_MAKE_NAME<%0...%1@>(%2)<_YCM:y>(_GROUP_MAKE_NAME<%0...%1>(%2));
  173. #define gglobal%0...%1(%2) static stock _GROUP_MAKE_NAME<%0...%1@>(%2)<>(_GROUP_MAKE_NAME<%0...%1>(%2));static stock _GROUP_MAKE_NAME<%0...%1>(%2)<_YCM:y>
  174. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)<>GROUP_CHAIN?<%0>(%2);static stock _GROUP_MAKE_NAME<%0...>(%2)<_YCM:y>for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  175. #define gpublic%0(%1)<%9> static stock%0(%1)<>{}static stock%0(%1)<_YCM:%9>
  176. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  177. #endif
  178. #else
  179. #define gforeign%0...%1(%2);
  180. #define gglobal%0...%1(%2) static stock _GROUP_MAKE_NAME<%0...%1>(%2)
  181. #define gpublic static stock
  182. #define _gchain%0(%2) static stock _GROUP_MAKE_NAME<%0...>(%2)for(J@=1;J@;GROUP_CHAIN?<%0>(%2))
  183. #define _GROUP_SET_PLAYER _GROUP_MAKE_NAME<..._SetPlayer>
  184. #endif
  185. #endif
  186. /**--------------------------------------------------------------------------**\
  187. <summary>_GROUP_INITIALISE</summary>
  188. <param name="x">The element that was added (maybe).</param>
  189. <returns>
  190. -
  191. </returns>
  192. <remarks>
  193. The name is a macro, so this function isn't actually called this. This is
  194. called when a new element is created, and as such it is NOT chained to other
  195. parts of the groups system because each part handles one type of element.
  196. Loop through all players and set up the element for them if they are in a
  197. group that this is also in by default.
  198. If x is "_GROUP_MAKE_LIMIT" then this is the test used in OnPlayerConnect in
  199. various libraries to see if the groups system exists, and if not locally
  200. initialise the player instead of leaving it up to this system.
  201. </remarks>
  202. \**--------------------------------------------------------------------------**/
  203. stock bool:_GROUP_INITIALISE(_GROUP_MAKE_TAG:x = _GROUP_MAKE_TAG:_GROUP_MAKE_LIMIT)
  204. {
  205. #if defined _YSI_HAS_y_groups
  206. P:4(#_GROUP_INITIALISE " called: %i", x);
  207. // A new item has been added to the system - update all players according to
  208. // the default settings for a group.
  209. if (x != _GROUP_MAKE_TAG:_GROUP_MAKE_LIMIT)
  210. {
  211. YSI_g_sElementMembership[_:x] = YSI_g_sDefaultMembership,
  212. YSI_g_sMaxEncountered = max(YSI_g_sMaxEncountered, _:x);
  213. foreach (new playerid : Player)
  214. {
  215. Group_FullPlayerUpdate(playerid, _:x, YSI_g_sEmpty, YSI_g_sDefaultMembership, YSI_gGroupPlayers[playerid]);
  216. }
  217. }
  218. #endif
  219. #pragma unused x
  220. return false;
  221. }
  222. /*
  223. ad88888ba 88 88
  224. d8" "8b "" 88
  225. Y8, 88
  226. `Y8aaaaa, 88 8b,dPPYba, ,adPPYb,d8 88 ,adPPYba,
  227. `"""""8b, 88 88P' `"8a a8" `Y88 88 a8P_____88
  228. `8b 88 88 88 8b 88 88 8PP"""""""
  229. Y8a a8P 88 88 88 "8a, ,d88 88 "8b, ,aa
  230. "Y88888P" 88 88 88 `"YbbdP"Y8 88 `"Ybbd8"'
  231. aa, ,88
  232. "Y8bbdP"
  233. */
  234. /**--------------------------------------------------------------------------**\
  235. <summary>Group_Set...</summary>
  236. <param name="Group:g">Group to set for.</param>
  237. <param name="el">Element to set.</param>
  238. <param name="bool:s">Set or unset?</param>
  239. <returns>
  240. -
  241. </returns>
  242. <remarks>
  243. If "s" is true, then one element is added to the current group. False it is
  244. removed.
  245. </remarks>
  246. \**--------------------------------------------------------------------------**/
  247. gforeign Group_Set...(Group:g,_GROUP_MAKE_TAG:el,bool:s);
  248. gglobal Group_Set...(Group:g,_GROUP_MAKE_TAG:el,bool:s)
  249. {
  250. P:2(#_GROUP_MAKE_NAME<Group_Set...> " called: %i, %i, %i", _:g, _:el, s);
  251. // Set wether a group can use this item.
  252. if (0 <= _:el < _GROUP_MAKE_LIMIT && GROUP_MASK <= g <= GROUP_GLOBAL)
  253. {
  254. // There is now NO validity check for reasons of distruibution.
  255. GROUP_FIX(g);
  256. new
  257. slot = Bit_Slot(g),
  258. Bit:mask = Bit_Mask(g);
  259. if (s)
  260. {
  261. YSI_gTempGroups = YSI_g_sElementMembership[_:el];
  262. if (YSI_gTempGroups[slot] & mask)
  263. {
  264. // No point adding an element to a group that it is already in.
  265. return 1;
  266. }
  267. // Is this element NOT in the current group?
  268. Bit_Let(YSI_gTempGroups, _:g);
  269. }
  270. else
  271. {
  272. YSI_gTempGroups = YSI_g_sElementMembership[_:el];
  273. if (!(YSI_gTempGroups[slot] & mask))
  274. {
  275. return 1;
  276. }
  277. // Is this element in the current group?
  278. Bit_Vet(YSI_gTempGroups, _:g);
  279. }
  280. foreach (new playerid : Player)
  281. {
  282. if (YSI_gGroupPlayers[playerid][slot] & mask)
  283. {
  284. // The player is in the group in question, so they need a full
  285. // update.
  286. Group_FullPlayerUpdate(playerid, _:el, YSI_g_sElementMembership[_:el], YSI_gTempGroups, YSI_gGroupPlayers[playerid]);
  287. }
  288. }
  289. YSI_g_sElementMembership[_:el] = YSI_gTempGroups;
  290. return 1;
  291. }
  292. return 0;
  293. }
  294. /**--------------------------------------------------------------------------**\
  295. <summary>Group_Get...</summary>
  296. <param name="Group:g">Group to get from.</param>
  297. <param name="el">Element to get.</param>
  298. <returns>
  299. bool: Does the group have the element?
  300. </returns>
  301. <remarks>
  302. -
  303. </remarks>
  304. \**--------------------------------------------------------------------------**/
  305. gforeign bool:Group_Get...(Group:g,_GROUP_MAKE_TAG:el);
  306. gglobal bool:Group_Get...(Group:g,_GROUP_MAKE_TAG:el)
  307. {
  308. P:2(#_GROUP_MAKE_NAME<Group_Get...> " called: %i, %i", _:g, _:el);
  309. return (0 <= _:el < _GROUP_MAKE_LIMIT && GROUP_MASK <= g <= GROUP_GLOBAL && Bit_Get(YSI_g_sElementMembership[_:el], _:GROUP_TEMP_FIX(g)));
  310. }
  311. /**--------------------------------------------------------------------------**\
  312. <summary>Group_Set...Default</summary>
  313. <param name="Group:g">Group to set for.</param>
  314. <param name="bool:s">Set or unset?</param>
  315. <returns>
  316. -
  317. </returns>
  318. <remarks>
  319. If "s" is true, then all elements are added to this group (i.e. the default
  320. is set to true and all previous settings are wiped out). If it is false
  321. then all elements are removed and a full update is done.
  322. </remarks>
  323. \**--------------------------------------------------------------------------**/
  324. gforeign Group_Set...Default(Group:g,bool:s);
  325. gglobal Group_Set...Default(Group:g,bool:s)
  326. {
  327. P:2(#_GROUP_MAKE_NAME<Group_Set...Default> " called: %i, %i", _:g, s);
  328. if (GROUP_MASK <= g <= GROUP_GLOBAL)
  329. {
  330. // There is now NO validity check for reasons of distruibution.
  331. GROUP_FIX(g);
  332. new
  333. slot = Bit_Slot(g),
  334. Bit:mask = Bit_Mask(g),
  335. Iterator:GP<MAX_PLAYERS>;
  336. foreach (new playerid : Player)
  337. {
  338. // Do this check here so it is only done once per player. This is a
  339. // good argument for moving iterators to be duplicated in every
  340. // script; however, the default "Group()" iterator implementation is
  341. // a function, not a standard iterator - actually it now isn't...
  342. if (YSI_gGroupPlayers[playerid][slot] & mask)
  343. {
  344. // Make a fast local iterator of all the players.
  345. Iter_Add(GP, playerid);
  346. }
  347. }
  348. static
  349. BitArray:sNext<_MAX_GROUPS_G>;
  350. if (s)
  351. {
  352. Bit_Let(YSI_g_sDefaultMembership, _:g);
  353. for (new el = 0; el <= YSI_g_sMaxEncountered; ++el)
  354. {
  355. sNext = YSI_g_sElementMembership[el];
  356. if (sNext[slot] & mask)
  357. {
  358. continue;
  359. }
  360. // Is this element NOT in the current group?
  361. Bit_Let(sNext, _:g);
  362. foreach (new playerid : GP)
  363. {
  364. Group_FullPlayerUpdate(playerid, el, YSI_g_sElementMembership[el], sNext, YSI_gGroupPlayers[playerid]);
  365. }
  366. YSI_g_sElementMembership[el] = sNext;
  367. }
  368. }
  369. else
  370. {
  371. Bit_Vet(YSI_g_sDefaultMembership, _:g);
  372. for (new el = 0; el <= YSI_g_sMaxEncountered; ++el)
  373. {
  374. sNext = YSI_g_sElementMembership[el];
  375. if (!(sNext[slot] & mask))
  376. {
  377. continue;
  378. }
  379. // Is this element in the current group?
  380. Bit_Vet(sNext, _:g);
  381. foreach (new playerid : GP)
  382. {
  383. Group_FullPlayerUpdate(playerid, el, YSI_g_sElementMembership[el], sNext, YSI_gGroupPlayers[playerid]);
  384. }
  385. YSI_g_sElementMembership[el] = sNext;
  386. }
  387. }
  388. return 1;
  389. }
  390. return 0;
  391. }
  392. /**--------------------------------------------------------------------------**\
  393. <summary>Group_Set...New</summary>
  394. <param name="Group:g">Group to set for.</param>
  395. <param name="bool:s">Set or unset?</param>
  396. <returns>
  397. -
  398. </returns>
  399. <remarks>
  400. Similar to "Group_Set...Default", but doesn't reset all existing elements,
  401. just sets the permissions for any future items.
  402. </remarks>
  403. \**--------------------------------------------------------------------------**/
  404. gforeign Group_Set...New(Group:g,bool:s);
  405. gglobal Group_Set...New(Group:g,bool:s)
  406. {
  407. P:2(#_GROUP_MAKE_NAME<Group_Set...New> " called: %i, %i", _:g, s);
  408. if (GROUP_MASK <= g <= GROUP_GLOBAL)
  409. {
  410. // There is now NO validity check for reasons of distruibution.
  411. Bit_Set(YSI_g_sDefaultMembership, _:GROUP_TEMP_FIX(g), s);
  412. return 1;
  413. }
  414. return 0;
  415. }
  416. /**--------------------------------------------------------------------------**\
  417. <summary>Group_Exclusive...</summary>
  418. <param name="Group:g">Group to add this to.</param>
  419. <param name="_GROUP_MAKE_TAG:el">Element to add.</param>
  420. <returns>
  421. -
  422. </returns>
  423. <remarks>
  424. Add this element to ONLY this group and remove it from any others it might
  425. already be in. This is basically a simplified version of "GROUP_ADD".
  426. </remarks>
  427. \**--------------------------------------------------------------------------**/
  428. gforeign Group_Exclusive...(Group:g, _GROUP_MAKE_TAG:el);
  429. gglobal Group_Exclusive...(Group:g, _GROUP_MAKE_TAG:el)
  430. {
  431. if (GROUP_MASK <= g <= GROUP_GLOBAL)
  432. {
  433. GROUP_FIX(g);
  434. YSI_gTempGroups = YSI_g_sDefaultMembership;
  435. YSI_g_sDefaultMembership = YSI_g_cEmptyGroups;
  436. Bit_Let(YSI_g_sDefaultMembership, _:g);
  437. for (new i = 0; i != bits<_MAX_GROUPS_G>; ++i)
  438. {
  439. YSI_g_sEmpty[i] = ~YSI_g_sDefaultMembership[i];
  440. }
  441. _GROUP_INITIALISE(el);
  442. YSI_g_sDefaultMembership = YSI_gTempGroups;
  443. YSI_g_sEmpty = YSI_g_cEmptyGroups;
  444. return 1;
  445. }
  446. return 0;
  447. }
  448. /*
  449. ,ad8888ba, 88 88 88
  450. d8"' `"8b 88 88 88
  451. d8' 88 88 88
  452. 88 88 ,adPPYba, 88,dPPYba, ,adPPYYba, 88
  453. 88 88888 88 a8" "8a 88P' "8a "" `Y8 88
  454. Y8, 88 88 8b d8 88 d8 ,adPPPPP88 88
  455. Y8a. .a88 88 "8a, ,a8" 88b, ,a8" 88, ,88 88
  456. `"Y88888P" 88 `"YbbdP"' 8Y"Ybbd8"' `"8bbdP"Y8 88
  457. */
  458. /**--------------------------------------------------------------------------**\
  459. <summary>Group_SetGlobal...</summary>
  460. <param name="el">Element to set.</param>
  461. <param name="bool:s">Set or unset?</param>
  462. <returns>
  463. -
  464. </returns>
  465. <remarks>
  466. If "s" is true, then one element is added to the global group. False it is
  467. removed.
  468. </remarks>
  469. \**--------------------------------------------------------------------------**/
  470. gforeign Group_SetGlobal...(_GROUP_MAKE_TAG:el,bool:s);
  471. gglobal Group_SetGlobal...(_GROUP_MAKE_TAG:el,bool:s)
  472. {
  473. P:2(#_GROUP_MAKE_NAME<Group_SetGlobal...> " called: %i, %i", _:el, s);
  474. return _GROUP_MAKE_NAME<Group_Set...>(GROUP_GLOBAL, el, s);
  475. }
  476. /**--------------------------------------------------------------------------**\
  477. <summary>Group_GetGlobal...</summary>
  478. <param name="el">Element to get.</param>
  479. <returns>
  480. bool: Does the global group have the element?
  481. </returns>
  482. <remarks>
  483. -
  484. </remarks>
  485. \**--------------------------------------------------------------------------**/
  486. gforeign bool:Group_GetGlobal...(_GROUP_MAKE_TAG:el);
  487. gglobal bool:Group_GetGlobal...(_GROUP_MAKE_TAG:el)
  488. {
  489. P:2(#_GROUP_MAKE_NAME<Group_GetGlobal...> " called: %i", _:el);
  490. return _GROUP_MAKE_NAME<Group_Get...>(GROUP_GLOBAL, el);
  491. }
  492. /**--------------------------------------------------------------------------**\
  493. <summary>Group_SetGlobal...Default</summary>
  494. <param name="bool:s">Set or unset?</param>
  495. <returns>
  496. -
  497. </returns>
  498. <remarks>
  499. If "s" is true, then all elements are added to the global group (i.e. the
  500. default is set to true and all previous settings are wiped out). If it is
  501. false then all elements are removed and a full update is done.
  502. </remarks>
  503. \**--------------------------------------------------------------------------**/
  504. gforeign Group_SetGlobal...Default(bool:s);
  505. gglobal Group_SetGlobal...Default(bool:s)
  506. {
  507. P:2(#_GROUP_MAKE_NAME<Group_SetGlobal...Default> " called: %i", s);
  508. return _GROUP_MAKE_NAME<Group_Set...Default>(GROUP_GLOBAL, s);
  509. }
  510. /**--------------------------------------------------------------------------**\
  511. <summary>Group_SetGlobal...New</summary>
  512. <param name="bool:s">Set or unset?</param>
  513. <returns>
  514. -
  515. </returns>
  516. <remarks>
  517. All elements created FROM THIS POINT ON will have this default setting.
  518. </remarks>
  519. \**--------------------------------------------------------------------------**/
  520. gforeign Group_SetGlobal...New(bool:s);
  521. gglobal Group_SetGlobal...New(bool:s)
  522. {
  523. P:2(#_GROUP_MAKE_NAME<Group_SetGlobal...New> " called: %i", s);
  524. return _GROUP_MAKE_NAME<Group_Set...New>(GROUP_GLOBAL, s);
  525. }
  526. /**--------------------------------------------------------------------------**\
  527. <summary>Group_GlobalExclusive...</summary>
  528. <param name="_GROUP_MAKE_TAG:el">Element to add.</param>
  529. <returns>
  530. -
  531. </returns>
  532. <remarks>
  533. Add this element to ONLY the global group and remove it from any others it
  534. might already be in.
  535. </remarks>
  536. \**--------------------------------------------------------------------------**/
  537. gforeign Group_GlobalExclusive...(_GROUP_MAKE_TAG:el);
  538. gglobal Group_GlobalExclusive...(_GROUP_MAKE_TAG:el)
  539. {
  540. P:2(#_GROUP_MAKE_NAME<Group_GlobalExclusive...> " called: %i", el);
  541. return _GROUP_MAKE_NAME<Group_Exclusive...>(GROUP_GLOBAL, el);
  542. }
  543. /*
  544. 88 88
  545. 88 ,d 88
  546. 88 88 88
  547. 88 8b,dPPYba, MM88MMM ,adPPYba, 8b,dPPYba, 8b,dPPYba, ,adPPYYba, 88
  548. 88 88P' `"8a 88 a8P_____88 88P' "Y8 88P' `"8a "" `Y8 88
  549. 88 88 88 88 8PP""""""" 88 88 88 ,adPPPPP88 88
  550. 88 88 88 88, "8b, ,aa 88 88 88 88, ,88 88
  551. 88 88 88 "Y888 `"Ybbd8"' 88 88 88 `"8bbdP"Y8 88
  552. */
  553. static stock void:Group_Handoff(i,a[],s)
  554. {
  555. s = min(s, bits<_:_MAX_GROUPS_G>);
  556. if (i == -1)
  557. {
  558. memcpy(_:YSI_g_sDefaultMembership, a, 0, s * 4);
  559. }
  560. else if (i < sizeof (YSI_g_sElementMembership))
  561. {
  562. memcpy(_:YSI_g_sElementMembership[i], a, 0, s * 4);
  563. }
  564. }
  565. gpublic MAKE_YCM<HANDOFF_SOURCE...Group>()<p>
  566. {
  567. // Pass data to the new group controller.
  568. Group_Handoff(-1, _:YSI_g_sDefaultMembership, sizeof (YSI_g_sDefaultMembership));
  569. for (new i = 0; i != sizeof (YSI_g_sElementMembership); ++i)
  570. {
  571. Group_Handoff(i, _:YSI_g_sElementMembership[i], sizeof (YSI_g_sElementMembership[]));
  572. }
  573. }
  574. /**--------------------------------------------------------------------------**\
  575. <summary>Group_FullPlayerUpdate</summary>
  576. <param name="playerid">Player to check.</param>
  577. <param name="el">Element to show or hide.</param>
  578. <param name="previous">(p) The old state of affairs.</param>
  579. <param name="current">(c) The new state of affairs.</param>
  580. <param name="reference">(r) What to compare changes to.</param>
  581. <returns>
  582. -
  583. </returns>
  584. <remarks>
  585. I did have a good reason for calling this "FU", but I forgot it! Anyway,
  586. the state of some groups has changed - either a player's groups or an
  587. elements groups have changed. If the player could previously see the
  588. element but now can't, hide it. If the player previously couldn't see it
  589. but now can, show it. If there is no change do nothing. The old version of
  590. this library would just re-show the element even if they could already see
  591. it, but this was a bad design as it could incur large overheads in other
  592. libraries when they had to do IO to enable or disable something for a
  593. player.
  594. The change can be in either the player's groups or the element's groups,
  595. either way this code will work regardless.
  596. </remarks>
  597. \**--------------------------------------------------------------------------**/
  598. static stock Group_FullPlayerUpdate(playerid, el, Bit:p[], Bit:c[], Bit:r[])
  599. {
  600. // "_GROUPS_CHECK" is a macro that expands to a massive unrolled "if"
  601. // statement checking up to 512 groups at once. Any more than that and this
  602. // code resorts to a loop instead. I say "at once"; it does 32 AT ONCE
  603. // (as in truly in parallel), starting with the most likely match (the
  604. // default group that every player and every element is usually in), and
  605. // loops over all the groups in 32 group chunks. When I say "loop", this
  606. // could be in the form of a huge "if" statement with every iteration put in
  607. // explicitly.
  608. // r = Reference (valid groups).
  609. // c = Current (new groups).
  610. // p = Previous (old groups).
  611. #if _DEBUG >= 7
  612. new debugA[] = "false", debugB[] = "false";
  613. _GROUPS_CHECK_ANY(p,r)
  614. debugA = "true";
  615. _GROUPS_CHECK_ANY(c,r)
  616. debugB = "true";
  617. printf("Group_FullPlayerUpdate ("#_GROUP_MAKE_NAME<...>") called:\r\n\t%d, %d,\r\n\t%s,\r\n\t%s,\r\n\t%s,\r\n\t%s, %s",
  618. playerid, el, Bit_Display(p, bits<_MAX_GROUPS_G>), Bit_Display(c, bits<_MAX_GROUPS_G>), Bit_Display(r, bits<_MAX_GROUPS_G>), debugA, debugB);
  619. #endif
  620. _GROUPS_CHECK_ANY(p,r)
  621. {
  622. // Could previously see this thing. The thing about this design is that
  623. // it can (best case) take just 2 comparisons to end - and that should
  624. // be the common case!
  625. _GROUPS_CHECK_ANY(c,r)
  626. {
  627. // Still can.
  628. P:7("Group_FullPlayerUpdate: %d %d could, still can", playerid, el);
  629. return;
  630. }
  631. // Now can't.
  632. P:7("Group_FullPlayerUpdate: %d %d could, now can't", playerid, el);
  633. _GROUP_SET_PLAYER(_GROUP_MAKE_TAG:el, playerid, false);
  634. return;
  635. }
  636. // Couldn't see it before.
  637. _GROUPS_CHECK_ANY(c,r)
  638. {
  639. P:7("Group_FullPlayerUpdate: %d %d now can", playerid, el);
  640. // They have whatever this thing is. Note that this may be called
  641. // MULTIPLE times for an element, without anything actually changing.
  642. // I.e. this could be set to "true" repeatedly while never being set
  643. // to "false".
  644. _GROUP_SET_PLAYER(_GROUP_MAKE_TAG:el, playerid, true);
  645. // We use "return" here because "_GROUPS_CHECK_ANY" MAY be a loop.
  646. return;
  647. }
  648. P:7("Group_FullPlayerUpdate: %d %d can't", playerid, el);
  649. }
  650. /*
  651. ,ad8888ba, 88 88
  652. d8"' `"8b 88 ""
  653. d8' 88
  654. 88 88,dPPYba, ,adPPYYba, 88 8b,dPPYba, ,adPPYba,
  655. 88 88P' "8a "" `Y8 88 88P' `"8a I8[ ""
  656. Y8, 88 88 ,adPPPPP88 88 88 88 `"Y8ba,
  657. Y8a. .a8P 88 88 88, ,88 88 88 88 aa ]8I
  658. `"Y8888Y"' 88 88 `"8bbdP"Y8 88 88 88 `"YbbdP"'
  659. */
  660. /**--------------------------------------------------------------------------**\
  661. <summary>_yGI</summary>
  662. <param name="&ni">Next init function variable as returned by y_amx.</param>
  663. <param name="&na">Next add function variable as returned by y_amx.</param>
  664. <param name="&nu">Next update function variable as returned by y_amx.</param>
  665. <returns>
  666. -
  667. </returns>
  668. <remarks>
  669. This function is called when the group system first starts up to initialise
  670. the global group and all the various function pointers. The way the
  671. "_gchain" macro works means that the fact that "ni" etc are references is
  672. irrelevant; however, it does make the code LOOK much nicer and like
  673. assigning to the variables does have some wider meaning.
  674. If this is called with "ni = -1", it is special code to temporarily set or
  675. restore the defaults for use with the "GROUP_ADD" macro. So basically, it
  676. is poor design giving two distinct uses to a single function.
  677. </remarks>
  678. \**--------------------------------------------------------------------------**/
  679. _gchain _yGI(&ni, &na, &nu)
  680. {
  681. P:2(#_GROUP_MAKE_NAME<_yGI...> " called: %i, %i, %i", ni, na, nu);
  682. if (ni == -1)
  683. {
  684. P:4(#_GROUP_MAKE_NAME<_yGI...> " INIT");
  685. static
  686. BitArray:sStack<_MAX_GROUPS_G>;
  687. if (na)
  688. {
  689. // Called to "push" the default settings.
  690. sStack = YSI_g_sDefaultMembership;
  691. YSI_g_sDefaultMembership = YSI_g_cEmptyGroups;
  692. Bit_Let(YSI_g_sDefaultMembership, nu);
  693. for (new i = 0; i != bits<_MAX_GROUPS_G>; ++i)
  694. {
  695. YSI_g_sEmpty[i] = ~YSI_g_sDefaultMembership[i];
  696. }
  697. }
  698. else
  699. {
  700. // Called to "pop" the default settings.
  701. YSI_g_sDefaultMembership = sStack;
  702. YSI_g_sEmpty = YSI_g_cEmptyGroups;
  703. }
  704. }
  705. else
  706. {
  707. P:4(#_GROUP_MAKE_NAME<_yGI...> " SETUP");
  708. // Enable the default group. If I'm right, this way is actually better than
  709. // using variables as in most cases because "_MAX_GROUPS" is a constant so
  710. // all the other maths will be constant.
  711. Bit_Let(YSI_g_sDefaultMembership, _MAX_GROUPS);
  712. // Set up the function chaining.
  713. new
  714. x;
  715. ni = AMX_GetPublicPointerPrefix(ni, x, _A<_yGI>);
  716. _yGI = x;
  717. na = AMX_GetPublicPointerPrefix(na, x, _A<_yGA>);
  718. _yGA = x;
  719. nu = AMX_GetPublicPointerPrefix(nu, x, _A<_yGU>);
  720. _yGU = x;
  721. }
  722. }
  723. /**--------------------------------------------------------------------------**\
  724. <summary>_yGA</summary>
  725. <param name="&group">The group that was just created.</param>
  726. <returns>
  727. -
  728. </returns>
  729. <remarks>
  730. The given group was just created, loop over all elements and make sure they
  731. are NOT in this group - only the global group has a "default default" of
  732. true. We don't need to update any players with this as no-one will ever be
  733. in a brand new group.
  734. </remarks>
  735. \**--------------------------------------------------------------------------**/
  736. _gchain _yGA(&group)
  737. {
  738. P:4(#_GROUP_MAKE_NAME<_yGA...> " called: %i", _:group);
  739. // Adding a new group is now a lot harder than it was before, but on the
  740. // other hand, adding and using elements is vastly simpler so that's OK.
  741. new
  742. s = Bit_Slot(group),
  743. Bit:m = ~Bit_Mask(group);
  744. // Set the default "contains" for this group to false.
  745. YSI_g_sDefaultMembership[s] &= m;
  746. // Disable every element in this group. DOESN'T use "YSI_g_sMaxEncountered"
  747. // because we need to set up for the future too.
  748. for (new i = 0; i != _GROUP_MAKE_LIMIT; ++i)
  749. {
  750. YSI_g_sElementMembership[i][s] &= m;
  751. }
  752. }
  753. /**--------------------------------------------------------------------------**\
  754. <summary>_yGU</summary>
  755. <param name="&pid">The player who joined or left groups.</param>
  756. <param name="Bit:p[]">Their previous groups.</param>
  757. <param name="Bit:c[]">Their new groups.</param>
  758. <returns>
  759. -
  760. </returns>
  761. <remarks>
  762. The player "pid" just joined or left a group (or groups - can do multiple).
  763. Update their visibility accordingly. This function is ONLY called if there
  764. is a CHANGE - earlier functions confirm that they weren't already in (or
  765. not) this group(s) before the call.
  766. </remarks>
  767. \**--------------------------------------------------------------------------**/
  768. _gchain _yGU(&pid, Bit:p[], Bit:c[])
  769. {
  770. P:4(#_GROUP_MAKE_NAME<_yGU...> " called: %i, %s, %s", pid, Bit_Display(p, bits<_MAX_GROUPS_G>), Bit_Display(c, bits<_MAX_GROUPS_G>));
  771. // This code loops over every "thing" controlled by this script. For every
  772. // one it checks to see if the player can or can't see something that they
  773. // previously could or couldn't see. If their ability to see it has
  774. // changed then the "_GROUP_SET_PLAYER" function in the controlling library
  775. // is called to do the actual internal function of updating their state.
  776. for (new el = 0; el <= YSI_g_sMaxEncountered; ++el)
  777. {
  778. Group_FullPlayerUpdate(pid, el, p, c, YSI_g_sElementMembership[el]);
  779. }
  780. //printf("Player membership: %s", Bit_Display(c));
  781. }
  782. /*
  783. ,ad8888ba, 88
  784. d8"' `"8b 88
  785. d8' 88
  786. 88 88 ,adPPYba, ,adPPYYba, 8b,dPPYba, 88 88 8b,dPPYba,
  787. 88 88 a8P_____88 "" `Y8 88P' `"8a 88 88 88P' "8a
  788. Y8, 88 8PP""""""" ,adPPPPP88 88 88 88 88 88 d8
  789. Y8a. .a8P 88 "8b, ,aa 88, ,88 88 88 "8a, ,a88 88b, ,a8"
  790. `"Y8888Y"' 88 `"Ybbd8"' `"8bbdP"Y8 88 88 `"YbbdP'Y8 88`YbbdP"'
  791. 88
  792. 88
  793. */
  794. /**--------------------------------------------------------------------------**\
  795. <summary>_GROUP_UNIQUE_FUNCTION</summary>
  796. <returns>
  797. -
  798. </returns>
  799. <remarks>
  800. Calls all functions to correctly include them in the AMX when required.
  801. Also all variables as it turns out they were a problem too.
  802. </remarks>
  803. \**--------------------------------------------------------------------------**/
  804. #if !defined _GROUP_STOP_INCLUDES
  805. static stock _GROUP_PREVIOUS_FUNCTION()
  806. {
  807. #if defined _GROUP_INCLUDE_ALL_PREVIOUS
  808. _GROUP_UNIQUE_FUNCTION();
  809. #endif
  810. }
  811. #include "_unique"
  812. stock _GROUP_UNIQUE_FUNCTION()
  813. {
  814. #if YSIM_HAS_MASTER
  815. // Force the compiler to add the functions correctly.
  816. #if !(_YSIM_IS_CLIENT || _YSIM_IS_STUB)
  817. new
  818. a = _yGI, b = _yGA, c = _yGU;
  819. _GROUP_MAKE_NAME<_yGI...>(a, YSI_g_sMaxEncountered, c);
  820. _GROUP_MAKE_NAME<Group_Exclusive...@>(Group:0, _GROUP_MAKE_TAG:0);
  821. _GROUP_MAKE_NAME<Group_GlobalExclusive...@>(_GROUP_MAKE_TAG:0);
  822. _GROUP_MAKE_NAME<_yGA...>(b);
  823. _GROUP_MAKE_NAME<_yGU...>(c, Bit:"", Bit:"");
  824. _GROUP_MAKE_NAME<Group_Set...Default@>(Group:0, false);
  825. _GROUP_MAKE_NAME<Group_SetGlobal...Default@>(false);
  826. _GROUP_MAKE_NAME<Group_Set...New@>(Group:0, false);
  827. _GROUP_MAKE_NAME<Group_SetGlobal...New@>(false);
  828. _GROUP_MAKE_NAME<Group_Set...@>(Group:0, _GROUP_MAKE_TAG:0, false);
  829. _GROUP_MAKE_NAME<Group_SetGlobal...@>(_GROUP_MAKE_TAG:0, false);
  830. _GROUP_MAKE_NAME<Group_Get...@>(Group:0, _GROUP_MAKE_TAG:0);
  831. _GROUP_MAKE_NAME<Group_GetGlobal...@>(_GROUP_MAKE_TAG:0);
  832. for (new i = 0; i != _GROUP_MAKE_LIMIT; ++i)
  833. {
  834. YSI_g_sElementMembership[i] = Bit:"";
  835. }
  836. YSI_g_sDefaultMembership = Bit:"";
  837. YSI_g_sEmpty = Bit:"";
  838. printf("", _:YSI_g_sElementMembership);
  839. printf("", _yGI);
  840. printf("", _:YSI_g_sDefaultMembership);
  841. printf("", _yGU);
  842. printf("", _:YSI_g_sEmpty);
  843. printf("", _yGA);
  844. _GROUP_SET_PLAYER(_GROUP_MAKE_TAG:0, 0, false);
  845. MAKE_YCM<HANDOFF_SOURCE...Group>();
  846. #endif
  847. #else
  848. new
  849. a = _yGI, b = _yGA, c = _yGU;
  850. _GROUP_MAKE_NAME<_yGI...>(a, YSI_g_sMaxEncountered, c);
  851. _GROUP_MAKE_NAME<_yGA...>(b);
  852. _GROUP_MAKE_NAME<_yGU...>(c, Bit:"", Bit:"");
  853. printf("", YSI_g_sMaxEncountered);
  854. printf("", _:YSI_g_sElementMembership);
  855. printf("", _:YSI_g_sDefaultMembership);
  856. printf("", _:YSI_g_sEmpty);
  857. printf("", _yGI);
  858. printf("", _yGA);
  859. printf("", _yGU);
  860. #endif
  861. _GROUP_PREVIOUS_FUNCTION();
  862. }
  863. #define _GROUP_INCLUDE_ALL_PREVIOUS
  864. #endif
  865. #undef _GROUP_MAKE_TAG
  866. #undef _gchain
  867. #undef _GROUP_SET_PLAYER
  868. #undef gforeign
  869. #undef gglobal
  870. #undef gpublic