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