y_groups__funcs.inc 30 KB

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