impl.inc 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  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. 1.0
  44. Changelog:
  45. 04/10/12:
  46. Major update to better distribute group data. Now all script have it.
  47. Changed the code to favour common cases of element creation not groups.
  48. Added group hierarchys.
  49. 29/11/10:
  50. First version
  51. \**--------------------------------------------------------------------------**/
  52. #define _GROUP_STOP_INCLUDES
  53. /**--------------------------------------------------------------------------**\
  54. <summary>GROUP_CHAIN</summary>
  55. <param name="%9">Check if the function exists?</param>
  56. <param name="%0">Variable holding the function.</param>
  57. <param name="%1">Parameters.</param>
  58. <returns>
  59. -
  60. </returns>
  61. <remarks>
  62. Calls the correct function to chain a function with the given number of
  63. parameters, calls the function stored in a variable and passes parameters
  64. BY REFERENCE - ALL of them!
  65. </remarks>
  66. \**--------------------------------------------------------------------------**/
  67. /*
  68. New groups check:
  69. if (ALL A == ALL B)
  70. {
  71. if (ALL A == P)
  72. {
  73. // Add.
  74. }
  75. else
  76. {
  77. // Remove.
  78. }
  79. }
  80. else
  81. {
  82. if (ANY A == P && NO B == P)
  83. {
  84. // Add.
  85. }
  86. else
  87. {
  88. // Remove.
  89. }
  90. }
  91. */
  92. enum e_GROUP_FLAGS (<<= 1)
  93. {
  94. e_GROUP_FLAGS_NONE = 0,
  95. e_GROUP_FLAGS_GANG = 1,
  96. // I can't remember why I had this!
  97. //e_GROUP_FLAGS_CHAT,
  98. e_GROUP_FLAGS_ACTIVE,
  99. // Only ever set for one group.
  100. e_GROUP_FLAGS_GLOBAL,
  101. // Has no name.
  102. e_GROUP_FLAGS_TEMP,
  103. e_GROUP_FLAGS_COLOR = 0xFFFFFF00
  104. }
  105. enum E_GROUP_DATA
  106. {
  107. E_GROUP_DATA_NAME[MAX_GROUP_NAME char],
  108. //E_GROUP_DATA_COUNT,
  109. E_GROUP_DATA_HASH,
  110. e_GROUP_FLAGS:E_GROUP_DATA_FLAGS,
  111. }
  112. stock PlayerGroups(a, b)
  113. {
  114. return 0;
  115. }
  116. // Define the groups iterator in terms of the underlying bits, but without the
  117. // need for users to know about the "YSI_gGroupPlayers" array, just the player.
  118. #define PlayerGroups(%0,%1) GROUP_MANGLE(Bits@YSII_Ag(YSI_gGroupPlayers[%0],_:%1))
  119. // Detect missing parameters.
  120. #define PlayerGroups@YSII_Ag-1);_:(%0=%8(%5)%7)%9) YSI_NULL_ITERATOR-3);_:(%0=PlayerGroups(%5))!=-1;)
  121. static
  122. // Function pointers for chaining.
  123. YSI_g_sNextInitFunc,
  124. YSI_g_sNextUpdFunc,
  125. YSI_g_sNextAddFunc,
  126. YSI_g_sGroupCount;
  127. static stock const
  128. YSI_g_scGlobalName[] = "__GLOBAL",
  129. BitArray:RG@<_MAX_GROUPS_G>;
  130. MASTER_DATA<_MAX_GROUPS_G>
  131. static stock __declspec(dist_master) YSI_gGroupData[_MAX_GROUPS_G][E_GROUP_DATA];
  132. static stock __declspec(dist_tagged) Bit:YSI_g_sChildGroups[_MAX_GROUPS_G][bits<_MAX_GROUPS_G>];
  133. static stock
  134. BitArray:YSI_g_sDefaultGroups<_MAX_GROUPS_G>,
  135. // Create group hierarchys.
  136. Iterator:GroupPlayers[_MAX_GROUPS_G]<MAX_PLAYERS>;
  137. //#define GROUP_BITS<%0> BitArray:%0<_MAX_GROUPS_G>,
  138. #define _Group_HasPlayer(%0,%1) \
  139. Bit_Get(YSI_gGroupPlayers[(%1)],(%0)) //, _MAX_GROUPS_G)
  140. //PA_Get(YSI_gGroupPlayers[(%0)],(%1))
  141. //Bit_Get(YSI_gGroupPlayers[(%0)], (%1), MAX_PLAYERS)
  142. #define _Group_GetColor(%0) \
  143. (_:(YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_COLOR) | 0xAA)
  144. #define _Group_SetColor(%0,%1) \
  145. (YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] = (YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] & ~e_GROUP_FLAGS_COLOR) | (e_GROUP_FLAGS:(%1) & e_GROUP_FLAGS_COLOR))
  146. #define _Group_GetGang(%0) \
  147. (bool:(YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_GANG))
  148. #define _Group_LetGang(%0) \
  149. (YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] |= e_GROUP_FLAGS_GANG)
  150. #define _Group_VetGang(%0) \
  151. (YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] &= ~e_GROUP_FLAGS_GANG)
  152. #define _Group_IsActive(%0) \
  153. (YSI_gGroupData[_:(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_ACTIVE)
  154. //#define _Group_IsValid(%0)
  155. // (0 <= (%0) < _MAX_GROUPS && Group_IsActive(%0))
  156. #define _Group_IsValid(%0) \
  157. ((_:GROUP_MASK<=_:(%0)<=_:GROUP_GLOBAL)&&(_Group_IsActive(GROUP_TEMP_FIX(%0))))
  158. //#define Group_IsActive(%0)
  159. // (YSI_gGroupData[_:GROUP_FIX(%0)][E_GROUP_DATA_FLAGS] & e_GROUP_FLAGS_ACTIVE)
  160. foreign void:_Group_ReInitPlayers();
  161. global void:_Group_ReInitPlayers()
  162. {
  163. Iter_Init(GroupPlayers);
  164. for (new i = 0; i != _MAX_GROUPS_G; ++i) if (_Group_IsActive(i))
  165. {
  166. foreach (new playerid : Player)
  167. {
  168. if (_Group_HasPlayer(i, playerid)) Iter_Add(GroupPlayers[i], playerid);
  169. }
  170. }
  171. }
  172. HANDOFF()
  173. {
  174. P:1("y_groups HANDOFF called");
  175. DISTRIBUTE(YSI_gGroupData);
  176. DISTRIBUTE(YSI_g_sChildGroups);
  177. _Group_ReInitPlayers();
  178. P:1("y_groups HANDOFF ended");
  179. }
  180. /**--------------------------------------------------------------------------**\
  181. <summary>GROUP_ADD</summary>
  182. <param name="Group:g">Group to add the following items to.</param>
  183. <returns>
  184. -
  185. </returns>
  186. <remarks>
  187. This sets up a temporary environment, during which all items are ONLY added
  188. to the specified group and no others, with no complex extra code required.
  189. </remarks>
  190. \**--------------------------------------------------------------------------**/
  191. #define GROUP_ADD<%0> for(J@=_Group_AddInternal(1,(%0));J@;J@=_Group_AddInternal(0,(%0)))
  192. static remotefunc void:__Group_AddInternal(a,g)
  193. {
  194. GROUP_CHAIN?<YSI_g_sNextInitFunc>(-1, a, g);
  195. }
  196. // Can't use "stock remotefunc".
  197. #pragma unused __Group_AddInternal
  198. stock _Group_AddInternal(a, Group:g)
  199. {
  200. //printf("_Group_AddInternal %d %d", a, _:g);
  201. if (_Group_IsValid(g))
  202. {
  203. broadcastfunc __Group_AddInternal(a, _:GROUP_TEMP_FIX(g));
  204. return a;
  205. }
  206. else
  207. {
  208. return 0;
  209. }
  210. }
  211. /**--------------------------------------------------------------------------**\
  212. <summary>_Group_FakePlayer</summary>
  213. <param name="p">Player who connected.</param>
  214. <param name="Bit:g[]">The player's new groups.</param>
  215. <param name="s">Size of "g".</param>
  216. <returns>
  217. -
  218. </returns>
  219. <remarks>
  220. Pretend the player is in all the groups they won't be in shortly so that
  221. they have a complete blank slate when they connect - the system KNOWS they
  222. are not in some groups and are in others.
  223. Uses an unusual size in "g" to reduce the string length.
  224. </remarks>
  225. \**--------------------------------------------------------------------------**/
  226. static remotefunc void:_Group_FakePlayer(p,Bit:g[sizeof RG@],s)
  227. {
  228. for (new i = 0; i != sizeof (g); ++i)
  229. {
  230. // Set their current groups to the inverse of what they are now in.
  231. YSI_gGroupPlayers[p][i] = ~g[i];
  232. }
  233. _Group_SetPlayer(p, g, s);
  234. }
  235. /**--------------------------------------------------------------------------**\
  236. <summary>Group_IsValid</summary>
  237. <param name="Group:g">Group to check.</param>
  238. <returns>
  239. bool: - Is the group active and valid?
  240. </returns>
  241. <remarks>
  242. -
  243. </remarks>
  244. \**--------------------------------------------------------------------------**/
  245. foreign bool:Group_IsValid(Group:g);
  246. global bool:Group_IsValid(Group:g)
  247. {
  248. //GROUP_FIX(g);
  249. return _Group_IsValid(g);
  250. }
  251. /**--------------------------------------------------------------------------**\
  252. <summary>OnScriptInit</summary>
  253. <returns>
  254. -
  255. </returns>
  256. <remarks>
  257. Finds three functions by prefix:
  258. _yGI - An init function to set up a script using groups.
  259. _yGA - An add function called when a new group is created.
  260. _yGU - An update function called when a player's groups change.
  261. </remarks>
  262. \**--------------------------------------------------------------------------**/
  263. hook OnScriptInit()
  264. {
  265. P:1("Group_OnScriptInit called");
  266. //state YSI_has_groups:y;
  267. // Set up the global groups.
  268. #if !(YSIM_HAS_MASTER && (_YSIM_IS_STUB || _YSIM_IS_CLIENT))
  269. #if _YSIM_IS_CLOUD
  270. if (_YCM@y)
  271. #endif
  272. {
  273. P:5("Group_OnScriptInit: Master");
  274. Iter_Init(GroupPlayers);
  275. strpack(YSI_gGroupData[_MAX_GROUPS][E_GROUP_DATA_NAME], YSI_g_scGlobalName, MAX_GROUP_NAME char);
  276. YSI_gGroupData[_MAX_GROUPS][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS_ACTIVE | e_GROUP_FLAGS_GLOBAL;
  277. YSI_gGroupData[_MAX_GROUPS][E_GROUP_DATA_HASH] = YHash(YSI_g_scGlobalName);
  278. // Store the default groups away from the actual global group.
  279. // The default groups include the global group, but it itself
  280. // doesn't.
  281. Bit_Let(YSI_g_sDefaultGroups, _:_MAX_GROUPS);
  282. }
  283. #endif
  284. // Call the other group init functions.
  285. new
  286. ni = AMX_GetPublicPointerPrefix(0, YSI_g_sNextInitFunc, _A<_yGI>),
  287. na = AMX_GetPublicPointerPrefix(0, YSI_g_sNextAddFunc, _A<_yGA>),
  288. nu = AMX_GetPublicPointerPrefix(0, YSI_g_sNextUpdFunc, _A<_yGU>);
  289. //printf("Group_OnScriptInit: %d, %d, %d", ni, na, nu);
  290. P:4("Group_OnScriptInit: %d, %d, %d", ni, na, nu);
  291. P:4("Group_OnScriptInit: %d, %d, %d", AMX_HEADER_COD - AMX_BASE_ADDRESS, YSI_g_sNextInitFunc, AMX_HEADER_DAT - AMX_BASE_ADDRESS);
  292. GROUP_CHAIN?<YSI_g_sNextInitFunc>(ni, na, nu);
  293. //CallLocalFunction("_Group_SpecialInit", "");
  294. heapspace(); // I'm sure there's a reason this is here. But I forgot it!
  295. #if defined _GROUP_INCLUDE_ALL_PREVIOUS
  296. if (FALSE)
  297. {
  298. // Include, but never call, this function.
  299. _GROUP_UNIQUE_FUNCTION();
  300. }
  301. #endif
  302. P:1("Group_OnScriptInit end");
  303. return 1;
  304. }
  305. /**--------------------------------------------------------------------------**\
  306. <summary>OnScriptExit</summary>
  307. <returns>
  308. -
  309. </returns>
  310. <remarks>
  311. Destroy all this script's groups. This is an instance of "_YSI_SpecialExit"
  312. which can't be y_hooked and is called after (almost) every other callback,
  313. at least after every one controlled via y_scriptinit.
  314. </remarks>
  315. \**--------------------------------------------------------------------------**/
  316. public OnScriptExit()
  317. {
  318. #if defined _Group_SpecialExit
  319. _Group_SpecialExit();
  320. #endif
  321. _Group_TryRemove();
  322. return 1;
  323. }
  324. #undef OnScriptExit
  325. #define OnScriptExit _Group_SpecialExit
  326. #if defined _Group_SpecialExit
  327. forward _Group_SpecialExit();
  328. #endif
  329. /**--------------------------------------------------------------------------**\
  330. <summary>Group_Destroy</summary>
  331. <param name="Group:group">Group to destroy from the system.</param>
  332. <returns>
  333. -
  334. </returns>
  335. <remarks>
  336. -
  337. </remarks>
  338. \**--------------------------------------------------------------------------**/
  339. static remotefunc void:_Group_Destroy(g, gp@YSII_Ag[MAX_PLAYERS + 1], s)
  340. {
  341. #pragma unused s
  342. new
  343. ps = Bit_Slot(g),
  344. Bit:pm = ~Bit_Mask(g);
  345. if (YSI_g_sNextUpdFunc)
  346. {
  347. // Update all players who were in this group as they may have lost
  348. // some permissions. This is the simplest update because we don't
  349. // need to worry about the effect of removing this group on the
  350. // players' memberships in other groups. Maybe they were added to
  351. // that other group as a direct result of being added to this group,
  352. // but maybe they weren't - there's no way to know!
  353. // Check it! Local parameter iterator!
  354. foreach (new p : gp)
  355. {
  356. YSI_gTempGroups = YSI_gGroupPlayers[p];
  357. // If these parameters were the other way round I could do this:
  358. //GROUP_CHAIN<YSI_g_sNextUpdFunc>(p, (YSI_gGroupPlayers[p], YSI_gGroupPlayers[p][ps] &= pm), YSI_gGroupPlayers[p]);
  359. // But they're currently not so I can't.
  360. GROUP_CHAIN<YSI_g_sNextUpdFunc>(p, YSI_gTempGroups, YSI_gGroupPlayers[p]);
  361. YSI_gGroupPlayers[p][ps] &= pm;
  362. }
  363. }
  364. else
  365. {
  366. foreach (new p : gp)
  367. {
  368. // Remove all players from this group.
  369. YSI_gGroupPlayers[p][ps] &= pm;
  370. }
  371. }
  372. }
  373. // Added for the fun of it.
  374. foreign void:Group_Destroy(Group:group);
  375. global void:Group_Destroy(Group:group)
  376. {
  377. P:2("Group_Destroy called: %i", _:group);
  378. P:2("Group_Destroy called in %d", _@);
  379. // You can't destroy the global group.
  380. if (_Group_IsValid(group) && group != GROUP_GLOBAL)
  381. {
  382. GROUP_FIX(group);
  383. broadcastfunc _Group_Destroy(_:group, Iter_InternalArray(GroupPlayers[_:group]), Iter_InternalSize(GroupPlayers[_:group]));
  384. YSI_gGroupData[_:group][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS:0;
  385. YSI_gGroupData[_:group][E_GROUP_DATA_HASH] = 0;
  386. Iter_Clear(GroupPlayers[_:group]);
  387. MASTER_RESET<_:group>
  388. // This group no longer exists, so can't be the child of another group.
  389. new
  390. ps = Bit_Slot(_:group),
  391. Bit:pm = ~Bit_Mask(_:group);
  392. for (new i = 0; i != _MAX_GROUPS; ++i)
  393. {
  394. YSI_g_sChildGroups[i][ps] &= pm;
  395. }
  396. // Nor can it have groups (saves some later checks).
  397. YSI_g_sChildGroups[_: group] = YSI_g_cEmptyGroups;
  398. }
  399. }
  400. /**--------------------------------------------------------------------------**\
  401. <summary>_Group_TryRemove</summary>
  402. <returns>
  403. -
  404. </returns>
  405. <remarks>
  406. Removes all groups purely owned by the calling script.
  407. </remarks>
  408. \**--------------------------------------------------------------------------**/
  409. @foreign void:_Group_TryRemove();
  410. @global void:_Group_TryRemove()
  411. {
  412. for (new i = 0; i != _MAX_GROUPS; ++i) if (_Group_IsActive(i))
  413. {
  414. MASTER_REMOVE<i>
  415. MASTER_EMPTY<i>
  416. {
  417. //printf("removing i");
  418. broadcastfunc _Group_Destroy(i, Iter_InternalArray(GroupPlayers[i]), Iter_InternalSize(GroupPlayers[i]));
  419. YSI_gGroupData[i][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS:0;
  420. YSI_gGroupData[i][E_GROUP_DATA_HASH] = 0;
  421. Iter_Clear(GroupPlayers[i]);
  422. }
  423. }
  424. //return 1;
  425. }
  426. /**--------------------------------------------------------------------------**\
  427. <summary>OnPlayerDisconnect</summary>
  428. <param name="playerid">The player that left.</param>
  429. <param name="reason">Why they left (unused).</param>
  430. <returns>
  431. -
  432. </returns>
  433. <remarks>
  434. Removes this player from all groups. Unfortunately there's no way to
  435. combine multiple iterator removals to improve their efficiency - currently
  436. they have to loop through all previous ones to find the one to modify to
  437. skip over that player. I did debate updating foreach to doubly-linked
  438. lists for this reason - that would make reverse traversal and removal faster
  439. by doubling memory consumption but only very slightly affecting adds.
  440. </remarks>
  441. \**--------------------------------------------------------------------------**/
  442. hook OnPlayerDisconnect(playerid, reason)
  443. {
  444. #pragma unused reason
  445. P:2("hook OnPlayerDisconnect called: %d, %d", playerid, reason);
  446. // Can't - can't what? Don't stop typing mid comment!
  447. //foreach (new Group:g : PlayerGroups(playerid))
  448. #if !(YSIM_HAS_MASTER && (_YSIM_IS_STUB || _YSIM_IS_CLIENT))
  449. #if _YSIM_IS_CLOUD
  450. if (_YCM@y)
  451. #endif
  452. {
  453. foreach (new g : Bits(YSI_gGroupPlayers[playerid]))
  454. {
  455. Iter_Remove(GroupPlayers[g], playerid);
  456. Bit_Vet(YSI_gGroupPlayers[playerid], g);
  457. }
  458. }
  459. #endif
  460. // Remove them from everything ever.
  461. //static const
  462. // BitArray:scEmpty<_MAX_GROUPS_G>;
  463. //GROUP_CHAIN?<YSI_g_sNextUpdFunc>(playerid, YSI_gGroupPlayers[p], scEmpty);
  464. YSI_gGroupPlayers[playerid] = YSI_g_cEmptyGroups;
  465. return 1;
  466. }
  467. /**--------------------------------------------------------------------------**\
  468. <summary>OnPlayerConnect</summary>
  469. <param name="playerid">The player that joined.</param>
  470. <returns>
  471. -
  472. </returns>
  473. <remarks>
  474. The player may not have ACTUALLY joined the server, they may have just been
  475. added to this newly loaded script. In that case we need to initialise the
  476. locally stored group data to the new data. Of course, if this script is the
  477. group master, then we need to do significantly more! This is more complex
  478. than other scripts with master as they don't have some things to do in non-
  479. master scripts as well, whereas this one does.
  480. </remarks>
  481. \**--------------------------------------------------------------------------**/
  482. hook OnPlayerConnect@9(playerid)
  483. {
  484. P:1("Groups_OnPlayerConnect called: %d, %d", playerid, !(YSIM_HAS_MASTER && (_YSIM_IS_STUB || _YSIM_IS_CLIENT)));
  485. #if !(YSIM_HAS_MASTER && (_YSIM_IS_STUB || _YSIM_IS_CLIENT))
  486. #if _YSIM_IS_CLOUD
  487. if (_YCM@y)
  488. #endif
  489. {
  490. //printf("0");
  491. P:6("Group_OnPlayerConnect: Master %d", playerid);
  492. // Is master. ALWAYS reset all players - if they start screwing
  493. // around with loading orders then that's their own fault!
  494. broadcastfunc _Group_FakePlayer(playerid, YSI_g_sDefaultGroups, bits<_MAX_GROUPS_G>);
  495. //printf("1");
  496. }
  497. #if _YSIM_IS_CLOUD
  498. else
  499. #endif
  500. #endif
  501. #if YSIM_HAS_MASTER
  502. {
  503. P:6("Group_OnPlayerConnect: Slave %d", playerid);
  504. static const
  505. BitArray:scF<_MAX_GROUPS_G> = {Bit:-1, ...};
  506. _GROUPS_CHECK_ANY(scF, YSI_gGroupPlayers[playerid])
  507. {
  508. // Already been set up - return.
  509. return 1;
  510. }
  511. //_Group_
  512. _Group_InitPlayer(playerid, _@);
  513. }
  514. #endif
  515. return 1;
  516. }
  517. /**--------------------------------------------------------------------------**\
  518. <summary>_Group_InitPlayer</summary>
  519. <param name="p">The player that joined.</param>
  520. <param name="master">The script that just started.</param>
  521. <returns>
  522. -
  523. </returns>
  524. <remarks>
  525. Request all a player's groups from the master system.
  526. </remarks>
  527. \**--------------------------------------------------------------------------**/
  528. foreign void:_Group_InitPlayer(p, master);
  529. global void:_Group_InitPlayer(p, master)
  530. {
  531. targetfunc _Group_FakePlayer<master>(p, YSI_gGroupPlayers[p], bits<_MAX_GROUPS_G>);
  532. }
  533. /**--------------------------------------------------------------------------**\
  534. <summary>_Group_SetSome</summary>
  535. <param name="playerid">Player to add to multiple groups.</param>
  536. <param name="group">Parent group to add to.</param>
  537. <returns>
  538. A new array of groups.
  539. </returns>
  540. <remarks>
  541. Takes a group and adds a player to that group and every child group of
  542. which they are not already a member.
  543. </remarks>
  544. \**--------------------------------------------------------------------------**/
  545. // If you overflow this stack there is something VERY wrong! The manual stack
  546. // is intended to help us combat very long inheritance chains. This code also
  547. // neatly ignores recursion by only doing any one group if the player doesn't
  548. // already have it.
  549. static
  550. YSI_g_sRecursionStack[_MAX_GROUPS_G][2];
  551. static stock Bit:_Group_SetSome(playerid, group)
  552. {
  553. new
  554. g = -1,
  555. s = 0;
  556. YSI_gTempGroups = YSI_gGroupPlayers[playerid];
  557. Bit_Let(YSI_gTempGroups, group);
  558. for ( ; ; )
  559. {
  560. g = Bits@YSII_Ag(YSI_g_sChildGroups[group], g, bits<_MAX_GROUPS_G>);
  561. if (g == -1)
  562. {
  563. // Finished this child group, pop off the stack.
  564. if (s--)
  565. {
  566. g = YSI_g_sRecursionStack[s][0];
  567. group = YSI_g_sRecursionStack[s][1];
  568. }
  569. else break;
  570. }
  571. else if (!Bit_Get(YSI_gTempGroups, g))
  572. {
  573. // This player isn't in this group, add them.
  574. Bit_Let(YSI_gTempGroups, g);
  575. YSI_g_sRecursionStack[s][0] = g;
  576. YSI_g_sRecursionStack[s][1] = group;
  577. ++s;
  578. group = g;
  579. g = -1;
  580. }
  581. }
  582. return YSI_gTempGroups;
  583. }
  584. /**--------------------------------------------------------------------------**\
  585. <summary>Group_SetPlayer</summary>
  586. <param name="Group:g">Group to modify.</param>
  587. <param name="p">Player to modify the group for.</param>
  588. <param name="bool:s">Add the player (true) or remove them (false).</param>
  589. <returns>
  590. -
  591. </returns>
  592. <remarks>
  593. Will in some cases update all settings, unless they are being added through
  594. a recursive call due to being added to child groups.
  595. There is an internal version that ONLY adds them to the group and DOES NOT
  596. update any of their other settings. As a result it has less checks in it.
  597. </remarks>
  598. \**--------------------------------------------------------------------------**/
  599. foreign void:Group_SetPlayer(Group:g,p,bool:s);
  600. global void:Group_SetPlayer(Group:g,p,bool:s)
  601. {
  602. if (_Group_IsValid(g) && VALID_PLAYERID(p))
  603. {
  604. GROUP_FIX(g);
  605. //static
  606. // BitArray:n<_MAX_GROUPS_G>;
  607. if (s)
  608. {
  609. if (!_Group_HasPlayer(g, p))
  610. {
  611. // Added to a group they don't already have. Make a new list of
  612. // groups they're in, taking in to account the new groups
  613. // hierarchy and pass that new array about.
  614. broadcastfunc _Group_SetPlayer(p, _Group_SetSome(p, _:g), sizeof (YSI_gTempGroups));
  615. return; //1;
  616. }
  617. }
  618. else
  619. {
  620. if (_Group_HasPlayer(g, p))
  621. {
  622. YSI_gTempGroups = YSI_gGroupPlayers[p];
  623. Bit_Vet(YSI_gTempGroups, _:g);
  624. broadcastfunc _Group_SetPlayer(p, YSI_gTempGroups, sizeof (YSI_gTempGroups));
  625. return; //1;
  626. }
  627. }
  628. }
  629. //return 0;
  630. }
  631. /**--------------------------------------------------------------------------**\
  632. <summary>_Group_SetPlayer</summary>
  633. <param name="p">Player to set.</param>
  634. <param name="Bit:g[bits<_MAX_GROUPS_G>]">The player's new groups.</param>
  635. <param name="s">Size of "g".</param>
  636. <returns>
  637. -
  638. </returns>
  639. <remarks>
  640. Pass a list of groups that the player is now in or not in. This is a
  641. complete replacement for their existing list of groups, and so multiple
  642. updates can be done at once.
  643. </remarks>
  644. \**--------------------------------------------------------------------------**/
  645. remotefunc void:_Group_SetPlayer(p, Bit:g[sizeof RG@], s)
  646. {
  647. #pragma unused s
  648. #if !(YSIM_HAS_MASTER && (_YSIM_IS_STUB || _YSIM_IS_CLIENT))
  649. #if _YSIM_IS_CLOUD
  650. if (_YCM@y)
  651. #endif
  652. {
  653. P:2("_Group_SetPlayer called: %i, %s, %s, %i", p, Bit_Display(YSI_gGroupPlayers[p]), Bit_Display(g), s);
  654. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  655. new
  656. Bit:cur,
  657. Bit:exor,
  658. Bit:bit,
  659. group;
  660. for (new i = 0; i != sizeof (g); ++i)
  661. {
  662. // Get the difference between the old and new groups.
  663. cur = YSI_gGroupPlayers[p][i];
  664. exor = g[i] ^ cur;
  665. P:6("_Group_SetPlayer: Loop %d 0x%04x%04x 0x%04x%04x", i, _:cur >>> 16, _:cur & 0xFFFF, _:exor >>> 16, _:exor & 0xFFFF);
  666. // Low-level bit twiddling.
  667. while ((bit = Bit:Cell_GetLowestComponent(exor)))
  668. {
  669. // group = i * cellbits + (scDeBruijn[(_:bit * 0x077CB531) >>> 27]);
  670. group = i * cellbits + Cell_GetLowestBit(exor);
  671. P:7("_Group_SetPlayer: %d %s %d", p, _:(cur & bit) ? ("remove from") : ("add to"), group);
  672. if (cur & bit)
  673. {
  674. // Used to have this group, now don't.
  675. Iter_Remove(GroupPlayers[group], p);
  676. }
  677. else
  678. {
  679. // Didn't have this group, now do.
  680. Iter_Add(GroupPlayers[group], p);
  681. }
  682. exor ^= bit;
  683. }
  684. }
  685. }
  686. #endif
  687. #pragma tabsize 4
  688. GROUP_CHAIN?<YSI_g_sNextUpdFunc>(p, YSI_gGroupPlayers[p], g);
  689. // NOW save the new version, after everything else has had a chance to
  690. // update according to the changes.
  691. YSI_gGroupPlayers[p] = g;
  692. }
  693. /**--------------------------------------------------------------------------**\
  694. <summary>Group_SetBalancedInternal</summary>
  695. <param name="p">Player to add to the smallest group.</param>
  696. <param name="Group:gs[]">An array of possible groups.</param>
  697. <param name="c">The number of USED items in the array.</param>
  698. <returns>
  699. The group they have been added to.
  700. </returns>
  701. <remarks>
  702. Chains the call with "Group_SetPlayer" to use its hierarchy code.
  703. </remarks>
  704. \**--------------------------------------------------------------------------**/
  705. foreign Group:Group_SetBalancedInternal(p,Group:gs[],c);
  706. global Group:Group_SetBalancedInternal(p,Group:gs[],c)
  707. {
  708. // Find which of the listed groups has the least players in.
  709. //#pragma unused s
  710. new
  711. count = cellmax,
  712. Group:id = INVALID_GROUP;
  713. for (new i = 0; i != c; ++i)
  714. {
  715. // Find the group with the least players.
  716. new
  717. gi = _:gs[i];
  718. if (_Group_IsValid(Group:gi))
  719. {
  720. gi = _:GROUP_TEMP_FIX(Group:gi);
  721. //GROUP_FIX(gi);
  722. new
  723. cc = Iter_Count(GroupPlayers[gi]);
  724. if (Bit_Get(YSI_gGroupPlayers[p], gi))
  725. {
  726. // The player is already in this group - prefer it to others.
  727. --cc;
  728. }
  729. if (cc < count)
  730. {
  731. count = cc;
  732. id = Group:gi;
  733. }
  734. }
  735. }
  736. Group_SetPlayer(id, p, true);
  737. return id;
  738. }
  739. /**--------------------------------------------------------------------------**\
  740. <summary>Group_SetBalanced</summary>
  741. <param name="playerid">Player to put in one of a number of groups</param>
  742. <param name="{Group, _}:...">Either an array size then array, or a list of groups.</param>
  743. <returns>
  744. -
  745. </returns>
  746. <remarks>
  747. Puts a player in whichever of the given groups currently has the least
  748. players.
  749. </remarks>
  750. \**--------------------------------------------------------------------------**/
  751. stock Group:Group_SetBalanced(playerid, {Group, _}:...)
  752. {
  753. if (!(0 < p <= MAX_PLAYERS))
  754. {
  755. return INVALID_GROUP;
  756. }
  757. new
  758. Group:possible[_MAX_GROUPS_G],
  759. count = numargs();
  760. switch (count)
  761. {
  762. case 1, 2:
  763. {
  764. P:W("Group_SetBalanced requires at least 2 groups or an array.");
  765. }
  766. case 3:
  767. {
  768. new
  769. third = getarg(2);
  770. if (third & _:GROUP_MASK)
  771. {
  772. // Two groups.
  773. possible[0] = Group:getarg(1);
  774. possible[1] = Group:third;
  775. return Group_SetBalancedInternal(playerid, possible, 2);
  776. }
  777. else
  778. {
  779. third = min(third, _MAX_GROUPS_G);
  780. // Array of groups.
  781. for (new i = 0; i != third; ++i)
  782. {
  783. possible[i] = getarg(1, i);
  784. }
  785. return Group_SetBalancedInternal(playerid, possible, third);
  786. }
  787. }
  788. default:
  789. {
  790. for (new i = 1; i != count; ++i)
  791. {
  792. possible[i - 1] = getarg(i);
  793. }
  794. return Group_SetBalancedInternal(playerid, possible, count - 1);
  795. }
  796. }
  797. return INVALID_GROUP;
  798. }
  799. /**--------------------------------------------------------------------------**\
  800. <summary>Group_GetPlayer</summary>
  801. <param name="Group:g">Group to check.</param>
  802. <param name="p">Player to check.</param>
  803. <returns>
  804. Is this player in this group?
  805. </returns>
  806. <remarks>
  807. -
  808. </remarks>
  809. \**--------------------------------------------------------------------------**/
  810. foreign bool:Group_GetPlayer(Group:g,p);
  811. global bool:Group_GetPlayer(Group:g,p)
  812. {
  813. P:2("bool:Group_GetPlayer called: %i, %i", _:g, p);
  814. if (_Group_IsValid(g) && VALID_PLAYERID(p))
  815. {
  816. GROUP_FIX(g);
  817. return Bit_Get(YSI_gGroupPlayers[p], _:g);
  818. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  819. }
  820. return false;
  821. }
  822. /**--------------------------------------------------------------------------**\
  823. <summary></summary>
  824. <param name="Group:g">Group to set the name of.</param>
  825. <param name="string:n[]">The new name of the group.</param>
  826. <returns>
  827. -
  828. </returns>
  829. <remarks>
  830. Sets the name of a group.
  831. </remarks>
  832. \**--------------------------------------------------------------------------**/
  833. foreign void:Group_SetName(Group:g,string:n[]);
  834. global void:Group_SetName(Group:g,string:n[])
  835. {
  836. P:2("Group_SetName called: %i, \"%s\"", _:g, n);
  837. if (_Group_IsValid(g))
  838. {
  839. GROUP_FIX(g);
  840. if (isnull(n))
  841. {
  842. YSI_gGroupData[_:g][E_GROUP_DATA_NAME][0] = 0;
  843. YSI_gGroupData[_:g][E_GROUP_DATA_HASH] = 0;
  844. }
  845. else
  846. {
  847. strpack(YSI_gGroupData[_:g][E_GROUP_DATA_NAME], n, MAX_GROUP_NAME char);
  848. YSI_gGroupData[_:g][E_GROUP_DATA_HASH] = YHash(n);
  849. }
  850. }
  851. //return 1;
  852. }
  853. /**--------------------------------------------------------------------------**\
  854. <summary>Group_GetName</summary>
  855. <param name="Group:g">Group to get the name of.</param>
  856. <returns>
  857. string:
  858. </returns>
  859. <remarks>
  860. Gets the name of a group.
  861. </remarks>
  862. \**--------------------------------------------------------------------------**/
  863. foreign string:Group_GetName(Group:g);
  864. global string:Group_GetName(Group:g)
  865. {
  866. //printf("Group_GetName called: %04x%04x", _:g >>> 16, _:g & 0xFFFF);
  867. P:2("Group_GetName called: %04x%04x", _:g >>> 16, _:g & 0xFFFF);
  868. static
  869. ret[YSI_MAX_STRING];
  870. if (_Group_IsValid(g))
  871. {
  872. //printf("Is Valid");
  873. //return Bit_Get(YSI_gGroupPlayers[g], p);
  874. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  875. strunpack(ret, YSI_gGroupData[_:GROUP_TEMP_FIX(g)][E_GROUP_DATA_NAME], YSI_MAX_STRING);
  876. //printf("Is Valid: %s", ret);
  877. }
  878. return ret;
  879. }
  880. /**--------------------------------------------------------------------------**\
  881. <summary>Group_GetID</summary>
  882. <param name="string:name[]">Name of a group to get.</param>
  883. <returns>
  884. Group: - The ID of the group with this name.
  885. </returns>
  886. <remarks>
  887. -
  888. </remarks>
  889. \**--------------------------------------------------------------------------**/
  890. foreign Group:Group_GetID(string:name[]);
  891. global Group:Group_GetID(string:name[])
  892. {
  893. P:2("Group_GetID called: %s", name);
  894. new
  895. i,
  896. hash = YHash(name);
  897. while (i != _MAX_GROUPS)
  898. {
  899. if (_Group_IsActive(i) && YSI_gGroupData[i][E_GROUP_DATA_HASH] == hash)
  900. {
  901. break;
  902. }
  903. ++i;
  904. }
  905. if (i == _MAX_GROUPS) return INVALID_GROUP;
  906. return GROUP_MANGLE(i);
  907. }
  908. /**--------------------------------------------------------------------------**\
  909. <summary>Group_SetGane</summary>
  910. <param name="Group:g">Group to set for.</param>
  911. <param name="bool:n">Set or not?</param>
  912. <returns>
  913. -
  914. </returns>
  915. <remarks>
  916. I actually can't remember what the "Gang" setting on a group does - I don't
  917. think it actually does anything in the latest code version!
  918. </remarks>
  919. \**--------------------------------------------------------------------------**/
  920. foreign void:Group_SetGang(Group:g,bool:n);
  921. global void:Group_SetGang(Group:g,bool:n)
  922. {
  923. P:2("Group_SetGang called: %i, %i", _:g, n);
  924. if (_Group_IsValid(g))
  925. {
  926. if (n)
  927. {
  928. _Group_LetGang(GROUP_TEMP_FIX(g));
  929. }
  930. else
  931. {
  932. _Group_VetGang(GROUP_TEMP_FIX(g));
  933. }
  934. }
  935. //return 1;
  936. }
  937. /**--------------------------------------------------------------------------**\
  938. <summary>Group_GetGang</summary>
  939. <param name="Group:g">Group to get the gang status of.</param>
  940. <returns>
  941. bool:
  942. </returns>
  943. <remarks>
  944. I still don't remember what this once did!
  945. </remarks>
  946. \**--------------------------------------------------------------------------**/
  947. foreign bool:Group_GetGang(Group:g);
  948. global bool:Group_GetGang(Group:g)
  949. {
  950. P:2("bool:Group_GetGang called: %i", _:g);
  951. if (_Group_IsValid(g))
  952. {
  953. return _Group_GetGang(GROUP_TEMP_FIX(g));
  954. }
  955. return false;
  956. }
  957. /**--------------------------------------------------------------------------**\
  958. <summary>
  959. Group_SetColor
  960. Group_SetColour
  961. </summary>
  962. <param name="Group:g">The group to set the colour of.</param>
  963. <param name="c">An RGBA colour.</param>
  964. <returns>
  965. -
  966. </returns>
  967. <remarks>
  968. This colour is not actually currently used anywhere.
  969. </remarks>
  970. \**--------------------------------------------------------------------------**/
  971. foreign void:Group_SetColour(Group:g,c);
  972. global void:Group_SetColour(Group:g,c)
  973. {
  974. P:2("Group_SetColour called: %i, %i", _:g, c);
  975. if (_Group_IsValid(g))
  976. {
  977. GROUP_FIX(g);
  978. _Group_SetColor(g, c);
  979. }
  980. //return 1;
  981. }
  982. /**--------------------------------------------------------------------------**\
  983. <summary>
  984. Group_GetColor
  985. Group_GetColour
  986. </summary>
  987. <param name="Group:g">The group to get the colour of.</param>
  988. <returns>
  989. An RGBA colour.
  990. </returns>
  991. <remarks>
  992. This colour is not actually currently used anywhere.
  993. </remarks>
  994. \**--------------------------------------------------------------------------**/
  995. foreign Group_GetColour(Group:g);
  996. global Group_GetColour(Group:g)
  997. {
  998. P:2("Group_GetColour called: %i", _:g);
  999. if (_Group_IsValid(g))
  1000. {
  1001. return _Group_GetColor(GROUP_TEMP_FIX(g));
  1002. //Bit_Set(YSI_gGroupPlayers[g], p, s, bits<MAX_PLAYERS>);
  1003. }
  1004. return 0;
  1005. }
  1006. #define Group_SetColor Group_SetColour
  1007. #define Group_GetColor Group_GetColour
  1008. /**--------------------------------------------------------------------------**\
  1009. <summary>Group_GetCount</summary>
  1010. <param name="Group:g">The group to get the membership for.</param>
  1011. <returns>
  1012. The number of players in the group.
  1013. </returns>
  1014. <remarks>
  1015. -
  1016. </remarks>
  1017. \**--------------------------------------------------------------------------**/
  1018. foreign Group_GetCount(Group:g);
  1019. global Group_GetCount(Group:g)
  1020. {
  1021. P:2("Group_GetCount called: %i", _:g);
  1022. if (_Group_IsValid(g))
  1023. {
  1024. GROUP_FIX(g);
  1025. return Iter_Count(GroupPlayers[_:g]);
  1026. }
  1027. return 0;
  1028. }
  1029. /**--------------------------------------------------------------------------**\
  1030. <summary>Group_Create</summary>
  1031. <param name="string:name[]">The optional name of the new group.</param>
  1032. <returns>
  1033. The group ID with a tag of "Group:", or "INVALID_GROUP".
  1034. </returns>
  1035. <remarks>
  1036. Group_Create - Local function to detect and "NULL"ify empty strings.
  1037. _Group_Create - Global function that does most of the work.
  1038. _Group_CreateChain - Remote function that updates all master scripts with
  1039. the new group's existence.
  1040. </remarks>
  1041. \**--------------------------------------------------------------------------**/
  1042. static remotefunc void:_Group_CreateChain(Group:g)
  1043. {
  1044. GROUP_CHAIN?<YSI_g_sNextAddFunc>(g);
  1045. }
  1046. @foreign Group:_Group_Create(string:name[]);
  1047. @global Group:_Group_Create(string:name[])
  1048. {
  1049. P:2("Group:Group_Create called: \"%s\"", name);
  1050. P:2("Group_Create called in %d", _@);
  1051. new
  1052. i;
  1053. if (!isnull(name))
  1054. {
  1055. // Get a group of the same name if it already exists.
  1056. i = _:Group_GetID(name);
  1057. if (i != _:INVALID_GROUP)
  1058. {
  1059. MASTER_ADD<_:GROUP_TEMP_FIX(Group:i)>
  1060. return Group:i;
  1061. }
  1062. }
  1063. i = 0;
  1064. while (i != _MAX_GROUPS && _Group_IsActive(i))
  1065. {
  1066. ++i;
  1067. }
  1068. if (i == _MAX_GROUPS) return INVALID_GROUP;
  1069. if (isnull(name))
  1070. {
  1071. YSI_gGroupData[i][E_GROUP_DATA_NAME][0] = 0;
  1072. YSI_gGroupData[i][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS_TEMP | e_GROUP_FLAGS_ACTIVE;
  1073. YSI_gGroupData[i][E_GROUP_DATA_HASH] = 0;
  1074. }
  1075. else
  1076. {
  1077. strpack(YSI_gGroupData[i][E_GROUP_DATA_NAME], name, MAX_GROUP_NAME char);
  1078. YSI_gGroupData[i][E_GROUP_DATA_FLAGS] = e_GROUP_FLAGS_ACTIVE;
  1079. if (!(YSI_gGroupData[i][E_GROUP_DATA_HASH] = YHash(name)))
  1080. {
  1081. P:E("Group %d has hash 0.", name);
  1082. }
  1083. }
  1084. MASTER_SET<i>
  1085. ++YSI_g_sGroupCount;
  1086. // Set the child groups to only this group.
  1087. //Bit_Let(YSI_g_sChildGroups[i], i);
  1088. // Add this group to other groups.
  1089. broadcastfunc _Group_CreateChain(Group:i);
  1090. return GROUP_MANGLE(i);
  1091. }
  1092. stock Group:Group_Create(string:name[] = "")
  1093. {
  1094. if (name[0])
  1095. {
  1096. return _Group_Create(name);
  1097. }
  1098. else
  1099. {
  1100. return _Group_Create(NULL);
  1101. }
  1102. }
  1103. /**--------------------------------------------------------------------------**\
  1104. <summary>Group@YSII_Ag</summary>
  1105. <param name="Group:group">Group to loop over.</param>
  1106. <param name="start">Last value.</param>
  1107. <returns>
  1108. The next player.
  1109. </returns>
  1110. <remarks>
  1111. Internal implementation of the "Group()" iterator for "foreach". Returns
  1112. all the players in a group one at a time. Now just wraps the "GroupPlayers"
  1113. iterator that is only stored in the GROUP master script.
  1114. </remarks>
  1115. \**--------------------------------------------------------------------------**/
  1116. #pragma deprecated use "GroupMember[]"
  1117. stock Group@brackets()
  1118. {
  1119. P:E("Old style \"Group()\" iterator called. Use \"GroupMember[]\".");
  1120. return 0;
  1121. }
  1122. #define Group@YSII_Ag%0- @iterfunc Group()
  1123. #pragma deprecated use "GroupMember"
  1124. iterfunc stock Group(Group:group, start)
  1125. {
  1126. static
  1127. sOnce = true;
  1128. if (sOnce)
  1129. {
  1130. P:I("The \"Group\" iterator is deprecated, please use \"GroupMember\" instead.");
  1131. sOnce = false;
  1132. }
  1133. return GroupMember@YSII_Ag(Group:group, start);
  1134. }
  1135. #pragma deprecated use "GroupMember[]"
  1136. stock GroupMember@brackets()
  1137. {
  1138. P:E("Old style \"GroupMember()\" iterator called. Use \"GroupMember[]\".");
  1139. return 0;
  1140. }
  1141. #define GroupMember@YSII_Ag%0- @iterfunc GroupMember()
  1142. iterfunc foreign GroupMember(Group:group, start);
  1143. iterfunc global GroupMember(Group:group, start)
  1144. {
  1145. GROUP_FIX(group);
  1146. if (start == -1)
  1147. {
  1148. //start = Iter_Begin(GroupPlayers[]);
  1149. start = Iter_First(GroupPlayers[_:GROUP_TEMP_FIX(group)]);
  1150. }
  1151. else
  1152. {
  1153. start = Iter_Next(GroupPlayers[_:GROUP_TEMP_FIX(group)], start);
  1154. }
  1155. if (start == Iter_End(GroupPlayers[]))
  1156. {
  1157. return -1;
  1158. }
  1159. return start;
  1160. }
  1161. /**--------------------------------------------------------------------------**\
  1162. <summary>CreatedGroup@YSII_Ag</summary>
  1163. <param name="start">Last value.</param>
  1164. <returns>
  1165. The next group.
  1166. </returns>
  1167. <remarks>
  1168. Internal implementation of the "CreatedGroup()" iterator for "foreach".
  1169. Returns all the groups that exist.
  1170. </remarks>
  1171. \**--------------------------------------------------------------------------**/
  1172. #pragma deprecated use "CreatedGroup[]"
  1173. stock CreatedGroup@brackets()
  1174. {
  1175. P:E("Old style \"CreatedGroup()\" iterator called. Use \"CreatedGroup\".");
  1176. return 0;
  1177. }
  1178. // Bypass y_iterate restrictions.
  1179. #define CreatedGroup@YSII_Ag%0- @iterfunc CreatedGroup()
  1180. iterfunc foreign Group:CreatedGroup(Group:group);
  1181. iterfunc global Group:CreatedGroup(Group:group)
  1182. {
  1183. // In most iterators the inital value is implicitly handled by the first
  1184. // "++", but it isn't here because we need to correct for the "Group" flag.
  1185. if (group != INVALID_GROUP) GROUP_FIX(group);
  1186. while (++group < Group:(_MAX_GROUPS_G - 1))
  1187. {
  1188. // DO NOT include the global group in this loop!
  1189. if (_Group_IsActive(group)) return GROUP_MANGLE(group);
  1190. }
  1191. return INVALID_GROUP;
  1192. }
  1193. /**--------------------------------------------------------------------------**\
  1194. <summary>ChildGroup@YSII_Ag</summary>
  1195. <param name="parent">Group to get the children of.</param>
  1196. <param name="start">Last value.</param>
  1197. <returns>
  1198. The next group.
  1199. </returns>
  1200. <remarks>
  1201. Internal implementation of the "ChildGroup()" iterator for "foreach".
  1202. Returns all the groups that are a child of the provided group.
  1203. </remarks>
  1204. \**--------------------------------------------------------------------------**/
  1205. #pragma deprecated use "GroupChild[]"
  1206. stock GroupChild@brackets()
  1207. {
  1208. P:E("Old style \"GroupChild()\" iterator called. Use \"GroupChild[]\".");
  1209. return 0;
  1210. }
  1211. #define GroupChild@YSII_Ag%0- @iterfunc GroupChild()
  1212. iterfunc foreign Group:GroupChild(Group:parent, Group:start);
  1213. iterfunc global Group:GroupChild(Group:parent, Group:start)
  1214. {
  1215. if (!_Group_IsValid(parent)) return INVALID_GROUP;
  1216. // In most iterators the inital value is implicitly handled by the first
  1217. // "++", but it isn't here because we need to correct for the "Group" flag.
  1218. if (start != INVALID_GROUP) GROUP_FIX(start);
  1219. // Use the existing "Bits" iterator with fixed identifiers.
  1220. return GROUP_MANGLE(Bits@YSII_Ag(YSI_g_sChildGroups[_:GROUP_TEMP_FIX(parent)], _:start, bits<_MAX_GROUPS_G>));
  1221. }
  1222. /**--------------------------------------------------------------------------**\
  1223. <summary>
  1224. _Y_G@C_0
  1225. _Y_G@C_1
  1226. _Y_G@C_2
  1227. _Y_G@C_3
  1228. </summary>
  1229. <param name="func">Function to call.</param>
  1230. <param name="...">0, 1, 2, or 3 parameters.</param>
  1231. <returns>
  1232. 0 - ALWAYS zero to make "_gchain" work properly.
  1233. </returns>
  1234. <remarks>
  1235. Basically function indirection, call a function through a pointer and use
  1236. the compiler to figure out the assembly to generate instead of having a
  1237. run-time loop inside a single instance of this function. Using just one
  1238. macro ("GROUP_CHAIN") one of these four functions are selected and called.
  1239. Adding more is fairly trivial too, the parameters are just:
  1240. #emit PUSH.S <16 + n * 4>
  1241. #emit PUSH.S ...
  1242. #emit PUSH.S 20
  1243. #emit PUSH.S 16
  1244. #emit PUSH.C <n * 4>
  1245. </remarks>
  1246. \**--------------------------------------------------------------------------**/
  1247. stock _Y_G@C_0(func)
  1248. {
  1249. #emit PUSH.C 0
  1250. #emit LCTRL 6
  1251. #emit ADD.C 28
  1252. #emit PUSH.pri
  1253. #emit LOAD.S.pri func
  1254. #emit SCTRL 6
  1255. // ALWAYS return 0;
  1256. return 0;
  1257. }
  1258. stock _Y_G@C_1(func, GLOBAL_TAG_TYPES:...)
  1259. {
  1260. #emit PUSH.S 16
  1261. #emit PUSH.C 4
  1262. #emit LCTRL 6
  1263. #emit ADD.C 28
  1264. #emit PUSH.pri
  1265. #emit LOAD.S.pri func
  1266. #emit SCTRL 6
  1267. //#emit RETN
  1268. return 0;
  1269. }
  1270. stock _Y_G@C_2(func, GLOBAL_TAG_TYPES:...)
  1271. {
  1272. #emit PUSH.S 20
  1273. #emit PUSH.S 16
  1274. #emit PUSH.C 8
  1275. #emit LCTRL 6
  1276. #emit ADD.C 28
  1277. #emit PUSH.pri
  1278. #emit LOAD.S.pri func
  1279. #emit SCTRL 6
  1280. //#emit RETN
  1281. return 0;
  1282. }
  1283. stock _Y_G@C_3(func, GLOBAL_TAG_TYPES:...)
  1284. {
  1285. #emit PUSH.S 24
  1286. #emit PUSH.S 20
  1287. #emit PUSH.S 16
  1288. #emit PUSH.C 12
  1289. #emit LCTRL 6
  1290. #emit ADD.C 28
  1291. #emit PUSH.pri
  1292. #emit LOAD.S.pri func
  1293. #emit SCTRL 6
  1294. //#emit RETN
  1295. return 0;
  1296. }
  1297. /**--------------------------------------------------------------------------**\
  1298. <summary>Group_SetGroup</summary>
  1299. <param name="Group:g">Group to set for.</param>
  1300. <param name="el">Element to set.</param>
  1301. <param name="bool:s">Set or unset?</param>
  1302. <returns>
  1303. -
  1304. </returns>
  1305. <remarks>
  1306. IMPORTANT NOTE: Groups are SLIGHTLY different to other systems - if you
  1307. REMOVE a group from another group then players WILL NOT be removed from
  1308. that second group, or any child groups.
  1309. </remarks>
  1310. \**--------------------------------------------------------------------------**/
  1311. foreign void:Group_SetGroup(Group:g,Group:el,bool:s);
  1312. global void:Group_SetGroup(Group:g,Group:el,bool:s)
  1313. {
  1314. P:2(#Group_SetGroup " called: %i, %i, %i", _:g, _:el, s);
  1315. // Set wether a group can use this item.
  1316. if (g != el && _Group_IsValid(g) && _Group_IsValid(el))
  1317. {
  1318. GROUP_FIX(g);
  1319. GROUP_FIX(el);
  1320. if (s)
  1321. {
  1322. if (Bit_Get(YSI_g_sChildGroups[_:g], _:el))
  1323. {
  1324. // Child is already part of parent.
  1325. return;
  1326. }
  1327. Bit_Let(YSI_g_sChildGroups[_:g], _:el);
  1328. if (g == Group:_MAX_GROUPS) Bit_Let(YSI_g_sDefaultGroups, _:el);
  1329. foreach (new p : GroupPlayers[_:g])
  1330. {
  1331. if (!_Group_HasPlayer(el, p))
  1332. {
  1333. broadcastfunc _Group_SetPlayer(p, _Group_SetSome(p, _:el), bits<_MAX_GROUPS_G>);
  1334. }
  1335. }
  1336. }
  1337. else
  1338. {
  1339. if (Bit_Get(YSI_g_sChildGroups[_:g], _:el))
  1340. {
  1341. // Child is a part of parent (don't remove players though).
  1342. Bit_Vet(YSI_g_sChildGroups[_:g], _:el);
  1343. if (g == Group:_MAX_GROUPS) Bit_Vet(YSI_g_sDefaultGroups, _:el);
  1344. }
  1345. }
  1346. }
  1347. }
  1348. /**--------------------------------------------------------------------------**\
  1349. <summary>Group_SetGlobalGroup</summary>
  1350. <param name="el">Element to set.</param>
  1351. <param name="bool:s">Set or unset?</param>
  1352. <returns>
  1353. -
  1354. </returns>
  1355. <remarks>
  1356. If "s" is true, then one element is added to the global group. False it is
  1357. removed.
  1358. </remarks>
  1359. \**--------------------------------------------------------------------------**/
  1360. foreign void:Group_SetGlobalGroup(Group:el,bool:s);
  1361. global void:Group_SetGlobalGroup(Group:el,bool:s)
  1362. {
  1363. P:2(#Group_SetGlobalGroup " called: %i, %i", _:el, s);
  1364. Group_SetGroup(GROUP_GLOBAL, el, s);
  1365. }
  1366. /**--------------------------------------------------------------------------**\
  1367. <summary>Group_Get...</summary>
  1368. <param name="Group:g">Group to get from.</param>
  1369. <param name="Group:el">Element to get.</param>
  1370. <returns>
  1371. bool: Does the group have the element?
  1372. </returns>
  1373. <remarks>
  1374. This has no "active" checks on the groups as if they aren't active but are
  1375. in range, then "YSI_g_sChildGroups" will return false anyway. Extra checks
  1376. are therefore just a waste of time.
  1377. </remarks>
  1378. \**--------------------------------------------------------------------------**/
  1379. foreign bool:Group_GetGroup(Group:g,Group:el);
  1380. global bool:Group_GetGroup(Group:g,Group:el)
  1381. {
  1382. P:2(#Group_GetGroup " called: %i, %i", _:g, _:el);
  1383. return (GROUP_MASK <= el <= GROUP_GLOBAL && GROUP_MASK <= g <= GROUP_GLOBAL && Bit_Get(YSI_g_sChildGroups[_:GROUP_TEMP_FIX(g)], _:GROUP_TEMP_FIX(el)));
  1384. }
  1385. /**--------------------------------------------------------------------------**\
  1386. <summary>Group_GetGlobal...</summary>
  1387. <param name="Group:el">Element to get.</param>
  1388. <returns>
  1389. bool: Does the global group have the element?
  1390. </returns>
  1391. <remarks>
  1392. -
  1393. </remarks>
  1394. \**--------------------------------------------------------------------------**/
  1395. foreign Group_GetGlobalGroup(Group:el);
  1396. global Group_GetGlobalGroup(Group:el)
  1397. {
  1398. P:2(#Group_GetGlobalGroup " called: %i", _:el);
  1399. return Group_GetGroup(GROUP_GLOBAL, el);
  1400. }
  1401. #define Group_AddChild(%0,%1) Group_SetGroup((%0), (%1), true)
  1402. #define Group_RemoveChild(%0,%1) Group_SetGroup((%0), (%1), false)
  1403. #define Group_IsChild(%0,%1) Group_GetGroup((%0), (%1))
  1404. /**--------------------------------------------------------------------------**\
  1405. <summary>Group_IsDescendant</summary>
  1406. <param name="Group:p">Ancestor group to check.</param>
  1407. <param name="Group:c">Other group to check.</param>
  1408. <returns>
  1409. bool: Is the second group related to the first?
  1410. </returns>
  1411. <remarks>
  1412. Now uses implicit, not explicit, recursion.
  1413. </remarks>
  1414. \**--------------------------------------------------------------------------**/
  1415. static stock
  1416. BitArray:YSI_g_sGroupDone<_MAX_GROUPS_G>;
  1417. foreign bool:Group_IsDescendant(Group:p, Group:c);
  1418. global bool:Group_IsDescendant(Group:p, Group:c)
  1419. {
  1420. // Groups that don't exist CAN'T be related, even if they're the same group.
  1421. if (_Group_IsValid(p) && _Group_IsValid(c))
  1422. {
  1423. if (p == c) return true; // Any group is related to itself.
  1424. GROUP_FIX(p);
  1425. GROUP_FIX(c);
  1426. // "g" = current child. "s" = current stack position. "c" = target
  1427. // child group. "p" = current parent.
  1428. Bit_SetAll(YSI_g_sGroupDone, false);
  1429. Bit_Let(YSI_g_sGroupDone, _:p);
  1430. new
  1431. g = -1,
  1432. s = 0;
  1433. for ( ; ; )
  1434. {
  1435. g = Bits@YSII_Ag(YSI_g_sChildGroups[_:p], g, bits<_MAX_GROUPS_G>);
  1436. if (g == -1)
  1437. {
  1438. // Finished this child group, pop off the stack.
  1439. if (s--)
  1440. {
  1441. g = YSI_g_sRecursionStack[s][0];
  1442. p = Group:YSI_g_sRecursionStack[s][1];
  1443. }
  1444. else return false;
  1445. }
  1446. else if (!Bit_Get(YSI_g_sGroupDone, g))
  1447. {
  1448. if (Group:g == c) return true;
  1449. // We haven't tested this group yet.
  1450. Bit_Let(YSI_g_sGroupDone, g);
  1451. YSI_g_sRecursionStack[s][0] = g;
  1452. YSI_g_sRecursionStack[s][1] = _:p;
  1453. ++s;
  1454. p = Group:g;
  1455. g = -1;
  1456. }
  1457. }
  1458. }
  1459. return false;
  1460. }