1
0

impl.inc 43 KB

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