y_iterate.inc 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /**--------------------------------------------------------------------------**\
  2. ===========================
  3. foreach efficient looping
  4. ===========================
  5. Description:
  6. Provides efficient looping through sparse data sets, such as connected
  7. players. Significantly improved from the original version to be a generic
  8. loop system, rather then purely a player loop system. When used for
  9. players this has constant time O(n) for number of connected players (n),
  10. unlike standard player loops which are O(MAX_PLAYERS), regardless of the
  11. actual number of connected players. Even when n is MAX_PLAYERS this is
  12. still faster.
  13. For extensive documentation on writing and using iterators, see this topic:
  14. http://forum.sa-mp.com/showthread.php?t=481877
  15. Legal:
  16. Version: MPL 1.1
  17. The contents of this file are subject to the Mozilla Public License Version
  18. 1.1 (the "License"); you may not use this file except in compliance with
  19. the License. You may obtain a copy of the License at
  20. [url]http://www.mozilla.org/MPL/[/url]
  21. Software distributed under the License is distributed on an "AS IS" basis,
  22. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  23. for the specific language governing rights and limitations under the
  24. License.
  25. The Original Code is the YSI foreach include.
  26. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  27. Portions created by the Initial Developer are Copyright (C) 2011
  28. the Initial Developer. All Rights Reserved.
  29. Contributors:
  30. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  31. Thanks:
  32. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  33. ZeeX - Very productive conversations.
  34. koolk - IsPlayerinAreaEx code.
  35. TheAlpha - Danish translation.
  36. breadfish - German translation.
  37. Fireburn - Dutch translation.
  38. yom - French translation.
  39. 50p - Polish translation.
  40. Zamaroht - Spanish translation.
  41. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  42. for me to strive to better.
  43. Pixels^ - Running XScripters where the idea was born.
  44. Matite - Pestering me to release it and using it.
  45. Very special thanks to:
  46. Thiadmer - PAWN, whose limits continue to amaze me!
  47. Kye/Kalcor - SA:MP.
  48. SA:MP Team past, present and future - SA:MP.
  49. Version:
  50. 0.4
  51. Changelog:
  52. 26/12/13:
  53. Added sections.
  54. 17/10/12:
  55. Fixed a bug that was here but not in "foreach.inc".
  56. 04/10/12:
  57. Added a tiny tweak to detect tag-returning iterator functions.
  58. Added Iter_InternalSize.
  59. 13/01/12:
  60. Fixed the count thanks to AndreT.
  61. 05/01/12:
  62. Fixed multi-dimensional iterators.
  63. Fixed "FOREACH_NO_BOTS".
  64. Made "Iterator:" support multi-dimensional arrays.
  65. 24/12/11:
  66. Added _YSI_SPECIAL_DEBUG support.
  67. Added fix for function iterators.
  68. Ported back to YSI.
  69. Changed to use internal YSI "__" natives.
  70. Fixed calls order by using ALS.
  71. 31/10/11:
  72. Changed the underlying loop code to be slightly faster.
  73. Added support for Iter_SafeRemove, prompting refactoring.
  74. 17/09/11:
  75. Fixed arrays under the new syntax.
  76. 28/04/11:
  77. Moved iterator identifiers to end of variables.
  78. Rewrote "foreach" to accept two syntaxes for "foreach (new i : Iter)".
  79. 16/08/10:
  80. Removed all the "2" versions of the functions.
  81. 14/08/10:
  82. Added Iter_Clear to reset an array.
  83. 06/08/10:
  84. Added special array declaration format.
  85. 18/12/09:
  86. Added Itter_Func2 functions for multi-dimensional iterators.
  87. Renamed foreact et al as keywords in the documentation.
  88. Changed licensing from GPL to MPL.
  89. 02/09/09:
  90. Fixed (again) for 0.3.
  91. Added free slot finding.
  92. 21/08/09:
  93. Updated to include random functions.
  94. Made entirely stand alone.
  95. Ported to 0.3 (separate version).
  96. Added automatic callback hook code.
  97. Removed debug information from stand alone version.
  98. 06/01/08:
  99. Added debug information.
  100. 09/10/07:
  101. Moved to system.
  102. 16/09/07:
  103. Added list sorting.
  104. Made this part of Y SeRver Includes, not Y Sever Includes.
  105. Made list sorting optional.
  106. Fixed version number.
  107. 08/09/07:
  108. First version.
  109. Functions:
  110. Public:
  111. OnPlayerDisconnect - Called when a player leaves to remove them.
  112. OnPlayerConnect - Called when a player connects to add them.
  113. Core:
  114. -
  115. Stock:
  116. Itter_ShowArray - Displays the contents of the array.
  117. Itter_AddInternal - Add a value to an itterator.
  118. Itter_RemoveInternal - Remove a value from an itterator.
  119. Itter_RandomInternal - Get a random item from an itterator.
  120. Itter_FreeInternal - Gets the first free slot in the itterator.
  121. Itter_InitInternal - Initialises a multi-dimensional itterator.
  122. Static:
  123. -
  124. Inline:
  125. Itter_Create - Create a new itterator value set.
  126. Itter_Add - Wraps Itter_AddInternal.
  127. Itter_Remove - Wraps Itter_RemoveInternal.
  128. Itter_Random - Wraps Itter_RandomInternal.
  129. Itter_Count - Gets the number of items in an itterator.
  130. Itter_Debug - Wraps around Itter_ShowArray.
  131. Itter_Free - Wraps around Itter_FreeInternal.
  132. Itter_Create2 - Create a new itterator array value set.
  133. Itter_Add2 - Wraps Itter_AddInternal for arrays.
  134. Itter_Remove2 - Wraps Itter_RemoveInternal for arrays.
  135. Itter_Random2 - Wraps Itter_RandomInternal for arrays.
  136. Itter_Count2 - Gets the number of items in an itterator array.
  137. Itter_Debug2 - Wraps around Itter_ShowArray for arrays.
  138. Itter_Free2 - Wraps around Itter_FreeInternal for arrays.
  139. API:
  140. -
  141. Callbacks:
  142. -
  143. Hooks:
  144. Itter_OnPlayerConnect - Hook for the OnPlayerConnect callback.
  145. Itter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback.
  146. Itter_OnGameModeInit - Only exists to make the code compile correctly...
  147. Definitions:
  148. -
  149. Enums:
  150. -
  151. Macros:
  152. -
  153. Keywords:
  154. foreach - Command to loop an iterator.
  155. foreachex - Like foreach but without a new variable.
  156. foreach2 - Command to loop through an iterator array.
  157. foreachex - Like foreach2 but without a new variable.
  158. Tags:
  159. Iterator - Declare an iterator.
  160. Variables:
  161. Global:
  162. -
  163. Static:
  164. YSI_g_OPC - Records wether Itter_OnPlayerConnect exists for speed.
  165. YSI_g_OPDC - Records wether Itter_OnPlayerDisconnect exists for speed.
  166. Commands:
  167. -
  168. Compile options:
  169. YSI_ITTER_NO_SORT - Removed.
  170. FOREACH_NO_BOTS - Remove the bot iterators for smaller code.
  171. FOREACH_NO_PLAYERS - Remove all default code for player itteration.
  172. Operators:
  173. -
  174. Iterators:
  175. Player - List of all players connected.
  176. Bot - List of all bots (npcs) connected.
  177. NPC - Alias of Bot.
  178. Character - All players and bots.
  179. \**--------------------------------------------------------------------------**/
  180. /*
  181. ad88888ba
  182. d8" "8b ,d
  183. Y8, 88
  184. `Y8aaaaa, ,adPPYba, MM88MMM 88 88 8b,dPPYba,
  185. `"""""8b, a8P_____88 88 88 88 88P' "8a
  186. `8b 8PP""""""" 88 88 88 88 d8
  187. Y8a a8P "8b, ,aa 88, "8a, ,a88 88b, ,a8"
  188. "Y88888P" `"Ybbd8"' "Y888 `"YbbdP'Y8 88`YbbdP"'
  189. 88
  190. 88
  191. */
  192. // "y_iterate" is always higher than "foreach".
  193. #define _Y_ITERATE_LOCAL_VERSION 25
  194. // Foreach is testing us.
  195. #if defined _FOREACH_INC_TEST
  196. #define _FOREACH_CUR_VERSION _Y_ITERATE_LOCAL_VERSION
  197. #endinput
  198. #endif
  199. #if !defined _FOREACH_NO_TEST
  200. #define _FOREACH_INC_TEST
  201. #tryinclude <foreach>
  202. #undef _FOREACH_INC_TEST
  203. // <foreach> exists - test which is newer.
  204. #if defined _FOREACH_LOCAL_VERSION
  205. #if _FOREACH_LOCAL_VERSION > _Y_ITERATE_LOCAL_VERSION
  206. // Foreach is newer.
  207. #if defined _inc_foreach
  208. #undef _inc_foreach
  209. #endif
  210. #define _FOREACH_NO_TEST
  211. #include <foreach>
  212. #endinput
  213. #endif
  214. #elseif defined foreach
  215. #if __Pawn == 0x030A
  216. #include "y_iterate/warning" // Backwards-compatible directive.
  217. #endinput
  218. #else
  219. #error Old foreach.inc files are no longer compatible with YSI.
  220. #endif
  221. #endif
  222. #endif
  223. #define _FOREACH_INC_TEST
  224. #if !defined _samp_included
  225. #error "Please include a_samp or a_npc before foreach"
  226. #endif
  227. #if defined _YSI_SPECIAL_DEBUG
  228. #define PS_IS_PLAYER_CONNECTED(%0) (%0 != INVALID_PLAYER_ID)
  229. #else
  230. #define PS_IS_PLAYER_CONNECTED IsPlayerConnected
  231. #endif
  232. #include "..\YSI_Internal\y_natives"
  233. #include "..\YSI_Core\y_debug"
  234. #if defined SendChat || defined FOREACH_NO_PLAYERS
  235. #define BOTSYNC_IS_BOT (true)
  236. #endif
  237. #if defined IsPlayerNPC
  238. #define _FOREACH_BOT
  239. #endif
  240. #if defined YSI_ITTER_NO_SORT
  241. #error YSI_ITTER_NO_SORT is no longer supported by foreach.
  242. #endif
  243. #if !defined BOTSYNC_IS_BOT
  244. forward Itter_OPDCInternal(playerid);
  245. #endif
  246. /*
  247. 88 a8P 88
  248. 88 ,88' 88
  249. 88 ,88" 88
  250. 88,d88' ,adPPYba, 8b d8 8b db d8 ,adPPYba, 8b,dPPYba, ,adPPYb,88 ,adPPYba,
  251. 8888"88, a8P_____88 `8b d8' `8b d88b d8' a8" "8a 88P' "Y8 a8" `Y88 I8[ ""
  252. 88P Y8b 8PP""""""" `8b d8' `8b d8'`8b d8' 8b d8 88 8b 88 `"Y8ba,
  253. 88 "88, "8b, ,aa `8b,d8' `8bd8' `8bd8' "8a, ,a8" 88 "8a, ,d88 aa ]8I
  254. 88 Y8b `"Ybbd8"' Y88' YP YP `"YbbdP"' 88 `"8bbdP"Y8 `"YbbdP"'
  255. d8'
  256. d8'
  257. */
  258. /**--------------------------------------------------------------------------**\
  259. Array:
  260. Iterator
  261. </returns>
  262. <remarks>
  263. Creates a new itterator start/array pair.
  264. </remarks>
  265. \**--------------------------------------------------------------------------**/
  266. #define Iterator:%1<%2> _Y_ITER_C9:_Y_ITER_C3:%1$YSII_Cg,_Y_ITER_C3:%1$YSII_Ag[(%2)+1]={(%2)*2,(%2)*2-1,...}
  267. #define iterator%0<%1> new Iterator:%0<%1>
  268. /**--------------------------------------------------------------------------**\
  269. <summary>foreach</summary>
  270. <param name="data">Data to itterate through.</param>
  271. <param name="as">Variable to set value to.</param>
  272. <returns>
  273. -
  274. </returns>
  275. <remarks>
  276. Not exactly the same as PHP foreach, just itterates through a list and
  277. returns the value of the current slot but uses that slot as the next index
  278. too. Variables must be in the form @YSII_<gname>S for the start index and
  279. @YSII_<gname>A for the data array where <name> is what's entered in data.
  280. </remarks>
  281. \**--------------------------------------------------------------------------**/
  282. #define foreach%1(%0) for(new Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%0|||)
  283. /*
  284. 88b d88 88 db 88888888ba 88
  285. 888b d888 "" d88b 88 "8b 88
  286. 88`8b d8'88 d8'`8b 88 ,8P 88
  287. 88 `8b d8' 88 ,adPPYYba, 88 8b,dPPYba, d8' `8b 88aaaaaa8P' 88
  288. 88 `8b d8' 88 "" `Y8 88 88P' `"8a d8YaaaaY8b 88""""""' 88
  289. 88 `8b d8' 88 ,adPPPPP88 88 88 88 d8""""""""8b 88 88
  290. 88 `888' 88 88, ,88 88 88 88 d8' `8b 88 88
  291. 88 `8' 88 `"8bbdP"Y8 88 88 88 d8' `8b 88 88
  292. */
  293. /**--------------------------------------------------------------------------**\
  294. <summary>Itter_Init2</summary>
  295. <param name="itter">Name of the itterator array to initialise.</param>
  296. <returns>
  297. -
  298. </returns>
  299. <remarks>
  300. Wrapper for Itter_InitInternal.
  301. native Iter_Init(IteratorArray:Name[]<>);
  302. </remarks>
  303. \**--------------------------------------------------------------------------**/
  304. #define Iter_Init Itter_Init
  305. #define Itter_Init(%1) Itter_InitInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1)+1,_Y_ITER_MAYBE_ARRAY(%1[]))
  306. /**--------------------------------------------------------------------------**\
  307. <summary>Itter_Add</summary>
  308. <param name="itter">Name of the itterator to add the data to.</param>
  309. <param name="value">Value to add to the itterator.</param>
  310. <returns>
  311. -
  312. </returns>
  313. <remarks>
  314. Wrapper for Itter_AddInternal.
  315. native Iter_Add(Iterator:Name<>, value);
  316. </remarks>
  317. \**--------------------------------------------------------------------------**/
  318. #define Iter_Add Itter_Add
  319. #define Itter_Add(%1,%2) Itter_AddInternal(_Y_ITER_ARRAY$%1$YSII_Cg,_Y_ITER_ARRAY$%1$YSII_Ag,%2,_Y_ITER_MAYBE_ARRAY(%1))
  320. /**--------------------------------------------------------------------------**\
  321. <summary>Itter_Remove</summary>
  322. <param name="itter">Name of the itterator to remove data from.</param>
  323. <param name="value">Data to remove.</param>
  324. <returns>
  325. -
  326. </returns>
  327. <remarks>
  328. Wrapper for Itter_RemoveInternal.
  329. native Iter_Remove(Iterator:Name<>, value);
  330. </remarks>
  331. \**--------------------------------------------------------------------------**/
  332. #define Iter_Remove Itter_Remove
  333. #define Itter_Remove(%1,%2) Itter_RemoveInternal(_Y_ITER_ARRAY$%1$YSII_Cg,_Y_ITER_ARRAY$%1$YSII_Ag,%2,_Y_ITER_MAYBE_ARRAY(%1))
  334. /**--------------------------------------------------------------------------**\
  335. <summary>Itter_Free</summary>
  336. <param name="itter">Name of the itterator to get the first free slot in.</param>
  337. <returns>
  338. -
  339. </returns>
  340. <remarks>
  341. Wrapper for Itter_FreeInternal.
  342. native Iter_Free(Iterator:Name<>);
  343. </remarks>
  344. \**--------------------------------------------------------------------------**/
  345. #define Iter_Free Itter_Free
  346. #define Itter_Free(%1) Itter_FreeInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1))
  347. /**--------------------------------------------------------------------------**\
  348. <summary>Itter_Contains</summary>
  349. <param name="itter">Name of the itterator to check membership of.</param>
  350. <param name="value">Value to check.</param>
  351. <returns>
  352. -
  353. </returns>
  354. <remarks>
  355. Checks if the given value is in the given iterator.
  356. native Iter_Remove(Iterator:Name<>, value);
  357. </remarks>
  358. \**--------------------------------------------------------------------------**/
  359. #define Iter_Contains Itter_Contains
  360. #define Itter_Contains(%1,%2) Itter_ContainsInternal(_Y_ITER_ARRAY$%1$YSII_Ag,%2,_Y_ITER_MAYBE_ARRAY(%1))
  361. /**--------------------------------------------------------------------------**\
  362. <summary>Itter_SafeRemove</summary>
  363. <param name="itter">Name of the itterator to remove data from.</param>
  364. <param name="value">Data to remove.</param>
  365. <param name="next">Container for the pointer to the next element.</param>
  366. <returns>
  367. -
  368. </returns>
  369. <remarks>
  370. Wrapper for Itter_SafeRemoveInternal. Common use:
  371. Iter_SafeRemove(iter, i, i);
  372. native Iter_SafeRemove(Iterator:Name<>, value, &next);
  373. </remarks>
  374. \**--------------------------------------------------------------------------**/
  375. #define Iter_SafeRemove Itter_SafeRemove
  376. #define Itter_SafeRemove(%1,%2,%3) Itter_SafeRemoveInternal(_Y_ITER_ARRAY$%1$YSII_Cg,_Y_ITER_ARRAY$%1$YSII_Ag,%2,%3,_Y_ITER_MAYBE_ARRAY(%1))
  377. /**--------------------------------------------------------------------------**\
  378. <summary>Itter_Random</summary>
  379. <param name="itter">Name of the itterator to get a random slot from.</param>
  380. <returns>
  381. -
  382. </returns>
  383. <remarks>
  384. Wrapper for Itter_RandomInternal.
  385. native Iter_Random(Iterator:Name<>);
  386. </remarks>
  387. \**--------------------------------------------------------------------------**/
  388. #define Iter_Random Itter_Random
  389. #define Itter_Random(%1) Itter_RandomInternal(_Y_ITER_ARRAY$%1$YSII_Cg,_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1))
  390. /**--------------------------------------------------------------------------**\
  391. <summary>Itter_Count</summary>
  392. <param name="itter">Name of the itterator to get a random slot from4.</param>
  393. <returns>
  394. -
  395. </returns>
  396. <remarks>
  397. Returns the number of items in this itterator.
  398. native Iter_Count(Iterator:Name<>);
  399. </remarks>
  400. \**--------------------------------------------------------------------------**/
  401. #define Iter_Count Itter_Count
  402. #define Itter_Count(%1) (_Y_ITER_ARRAY$%1$YSII_Cg)
  403. /**--------------------------------------------------------------------------**\
  404. <summary>Itter_Clear</summary>
  405. <param name="iter">Name of the iterator to empty.</param>
  406. <returns>
  407. -
  408. </returns>
  409. <remarks>
  410. Wrapper for Itter_ClearInternal.
  411. native Iter_Clear(IteratorArray:Name[]<>);
  412. </remarks>
  413. \**--------------------------------------------------------------------------**/
  414. #define Iter_Clear Itter_Clear
  415. #define Itter_Clear(%1) Itter_ClearInternal(_Y_ITER_ARRAY$%1$YSII_Cg,_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1))
  416. /**--------------------------------------------------------------------------**\
  417. <summary>Itter_FastClear</summary>
  418. <param name="iter">Name of the iterator to empty.</param>
  419. <returns>
  420. -
  421. </returns>
  422. <remarks>
  423. Uses a static array copy to blank the iterator instead of a loop.
  424. native Iter_FastClear(IteratorArray:Name[]<>);
  425. </remarks>
  426. \**--------------------------------------------------------------------------**/
  427. #define Iter_FastClear Itter_FastClear
  428. #define Itter_FastClear(%1); {static __CLEAR[_:_Y_ITER_C1:(sizeof %1@YSII_Ag+1-1)]={(_:_Y_ITER_C1:(sizeof %1@YSII_Ag-1))*2,(_:_Y_ITER_C1:(sizeof %1@YSII_Ag-1))*2-1,...};(_Y_ITER_ARRAY$%1$YSII_Cg=0),(_Y_ITER_ARRAY$%1$YSII_Ag=__CLEAR);}
  429. /*
  430. 88888888888 db 88888888ba 88
  431. 88 ,d d88b 88 "8b 88
  432. 88 88 d8'`8b 88 ,8P 88
  433. 88aaaaa 8b, ,d8 MM88MMM 8b,dPPYba, ,adPPYYba, d8' `8b 88aaaaaa8P' 88
  434. 88""""" `Y8, ,8P' 88 88P' "Y8 "" `Y8 d8YaaaaY8b 88""""""' 88
  435. 88 )888( 88 88 ,adPPPPP88 d8""""""""8b 88 88
  436. 88 ,d8" "8b, 88, 88 88, ,88 d8' `8b 88 88
  437. 88888888888 8P' `Y8 "Y888 88 `"8bbdP"Y8 d8' `8b 88 88
  438. */
  439. /**--------------------------------------------------------------------------**\
  440. <summary>Iter_Begin</summary>
  441. <param name="iter">Name of the iterator to get the start of.</param>
  442. <returns>
  443. -
  444. </returns>
  445. <remarks>
  446. Gets a point BEFORE the start of the iterator (the theoretical beginning).
  447. </remarks>
  448. \**--------------------------------------------------------------------------**/
  449. #define Iter_Begin(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  450. #define Itter_Begin(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  451. /**--------------------------------------------------------------------------**\
  452. <summary>Iter_End</summary>
  453. <param name="iter">Name of the iterator to get the end of.</param>
  454. <returns>
  455. -
  456. </returns>
  457. <remarks>
  458. Gets a point AFTER the end of the iterator (think "MAX_PLAYERS").
  459. </remarks>
  460. \**--------------------------------------------------------------------------**/
  461. #define Iter_End(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  462. #define Itter_End(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  463. /**--------------------------------------------------------------------------**\
  464. <summary>Iter_First</summary>
  465. <param name="iter">Name of the iterator to get the first valid element in.</param>
  466. <returns>
  467. -
  468. </returns>
  469. <remarks>
  470. Gets the first element in an iterator.
  471. </remarks>
  472. \**--------------------------------------------------------------------------**/
  473. #define Iter_First(%1) (_Y_ITER_ARRAY$%1$YSII_Ag[_Y_ITER_MAYBE_ARRAY(%1)])
  474. #define Itter_First(%1) (_Y_ITER_ARRAY$%1$YSII_Ag[_Y_ITER_MAYBE_ARRAY(%1)])
  475. /**--------------------------------------------------------------------------**\
  476. <summary>Iter_Last</summary>
  477. <param name="iter">Name of the iterator to</param>
  478. <returns>
  479. -
  480. </returns>
  481. <remarks>
  482. Gets the last element in an iterator. Works by getting the previous item
  483. from the one BEFORE the first element (i.e. the one before the sentinel).
  484. </remarks>
  485. \**--------------------------------------------------------------------------**/
  486. #define Iter_Last(%1) Itter_PrevInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1),_Y_ITER_MAYBE_ARRAY(%1))
  487. #define Itter_Last(%1) Itter_PrevInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1),_Y_ITER_MAYBE_ARRAY(%1))
  488. /**--------------------------------------------------------------------------**\
  489. <summary>Iter_Next</summary>
  490. <param name="iter">Name of the iterator to get the next element in.</param>
  491. <param name="cur">The current element.</param>
  492. <returns>
  493. -
  494. </returns>
  495. <remarks>
  496. Gets the element in an interator after the current one.
  497. </remarks>
  498. \**--------------------------------------------------------------------------**/
  499. #define Iter_Next(%1,%2) (_Y_ITER_ARRAY$%1$YSII_Ag[(%2)])
  500. #define Itter_Next(%1,%2) (_Y_ITER_ARRAY$%1$YSII_Ag[(%2)])
  501. /**--------------------------------------------------------------------------**\
  502. <summary>Iter_Prev</summary>
  503. <param name="iter">Name of the iterator to get the previous element in.</param>
  504. <param name="cur">The current element.</param>
  505. <returns>
  506. -
  507. </returns>
  508. <remarks>
  509. Gets the element in an iterator before the current one. Slow.
  510. </remarks>
  511. \**--------------------------------------------------------------------------**/
  512. #define Iter_Prev(%1,%2) Itter_PrevInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1),(%2))
  513. #define Itter_Prev(%1,%2) Itter_PrevInternal(_Y_ITER_ARRAY$%1$YSII_Ag,_Y_ITER_MAYBE_ARRAY(%1),(%2))
  514. /**--------------------------------------------------------------------------**\
  515. <summary>Iter_InternalArray</summary>
  516. <param name="iter">Name of the iterator to get the true name of.</param>
  517. <returns>
  518. -
  519. </returns>
  520. <remarks>
  521. Accesses the internal array of an iterator.
  522. </remarks>
  523. \**--------------------------------------------------------------------------**/
  524. #define Iter_InternalArray(%1) (_Y_ITER_ARRAY$%1$YSII_Ag)
  525. #define Itter_InternalArray(%1) (_Y_ITER_ARRAY$%1$YSII_Ag)
  526. /**--------------------------------------------------------------------------**\
  527. <summary>Iter_InternalSize</summary>
  528. <param name="iter">Name of the iterator to get the true size of.</param>
  529. <returns>
  530. -
  531. </returns>
  532. <remarks>
  533. Accesses the internal size of an iterator.
  534. </remarks>
  535. \**--------------------------------------------------------------------------**/
  536. #define Iter_InternalSize(%1) (_Y_ITER_MAYBE_ARRAY(%1)+1)
  537. #define Itter_InternalSize(%1) (_Y_ITER_MAYBE_ARRAY(%1)+1)
  538. /**--------------------------------------------------------------------------**\
  539. <summary>Iter_Size</summary>
  540. <param name="iter">Name of the iterator to get the size of.</param>
  541. <returns>
  542. -
  543. </returns>
  544. <remarks>
  545. Accesses the internal size of an iterator.
  546. </remarks>
  547. \**--------------------------------------------------------------------------**/
  548. #define Iter_Size(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  549. #define Itter_Size(%1) (_Y_ITER_MAYBE_ARRAY(%1))
  550. /*
  551. 88b d88
  552. 888b d888
  553. 88`8b d8'88
  554. 88 `8b d8' 88 ,adPPYYba, ,adPPYba, 8b,dPPYba, ,adPPYba, ,adPPYba,
  555. 88 `8b d8' 88 "" `Y8 a8" "" 88P' "Y8 a8" "8a I8[ ""
  556. 88 `8b d8' 88 ,adPPPPP88 8b 88 8b d8 `"Y8ba,
  557. 88 `888' 88 88, ,88 "8a, ,aa 88 "8a, ,a8" aa ]8I
  558. 88 `8' 88 `"8bbdP"Y8 `"Ybbd8"' 88 `"YbbdP"' `"YbbdP"'
  559. */
  560. // This now uses "$" not ":" because it is ALWAYS stripped out (it isn't a tag
  561. // based macro that may not be detected), and that way we can detect the "$" in
  562. // "C2" even when there is a custom tag involved. The second "$" allows us to
  563. // pick up all array subscripts at once without recursion. I'm not overly happy
  564. // about the trailing ")" on the macros, but I can't see any other way atm.
  565. #define _Y_ITER_ARRAY$ _:_Y_ITER_C0:_Y_ITER_C3:
  566. #define _Y_ITER_C0:%0[%1$YSII_%4g%5) %0$YSII_%4g[%1%5)
  567. #define _Y_ITER_C3:%0$ %0@
  568. // Find out if the passed iterator is multi-dimensional or an old function.
  569. #define _Y_ITER_FOREACH_SIZE(%1) _:_Y_ITER_C1:_Y_ITER_C2:(sizeof %1@YSII_Ag-1)
  570. // This macro detects multi-dimensional iterator arrays and moves the square
  571. // brackets to the correct location AFTER the iterator name (not in the middle).
  572. // If we get anything in "%8" don't bother putting it back as we can no longer
  573. // match against it because we have matched against "C1" instead.
  574. #define _Y_ITER_C1:%8(%0[%1]%2@YSII_Ag%9-1) _Y_ITER_C1:(%0%2@YSII_Ag[]%9-1)
  575. // I will deprecate these later. The code to do so is in revision c9ca7fd.
  576. #define _Y_ITER_C2:(sizeof%0(%1)@YSII_Ag-1);_:(%2=%9$%3(%4)$YSII_Ag[%5])!=_Y_ITER_MAYBE_ARRAY(%6);) _Y_ITER_C4:(sizeof %3@YSII_Ag-1);(sizeof %3@YSII_Ag-1)!=_:(%2=%3@YSII_Ag[%4][%5]);)
  577. #define _Y_ITER_C4:%9;%8!=_:(%2=%3[][%5]);) %9;%9!=_:(%2=%3[%5]);)
  578. #define _Y_ITER_C5:%9[%1]%2(%0(%5));) (%0(%5%1));)
  579. #define _Y_ITER_C6:_Y_ITER_C5:%9[%1]%2[%3]%4(%0(%5));) _Y_ITER_C6:_Y_ITER_C5:%2[%3]%4(%0(%5%1,));)
  580. #define @iterfunc%1()%7:(%0=%8)%9) YSI_NULL_ITERATOR-3);-1!=_:_Y_ITER_C6:_Y_ITER_C5:%8(%0=%1@YSII_Ag());)
  581. #define iterfunc%0(%1) %0@YSII_Ag(%1)
  582. // Check if this is an array or a single variable.
  583. #define _Y_ITER_MAYBE_ARRAY(%1) _:_Y_ITER_C1:(sizeof %1@YSII_Ag-1)
  584. // Used in array declaration. Need to keep the "_Y_ITER_C9:" as a tag, not as a
  585. // recursive macro. "_Y_ITER_C3:" strips the "$" from normal declarations.
  586. #define _Y_ITER_C9:_Y_ITER_C3:%0[%1$YSII_Cg,_Y_ITER_C3:%2[%3$YSII_Ag[%4]={%5} _Y_ITER_C9:%0@YSII_Cg[%1,%0@YSII_Ag[%1[%4]
  587. /**--------------------------------------------------------------------------**\
  588. <summary>Itter_Create2</summary>
  589. <param name="name">Itterator identifier.</param>
  590. <param name="size0">Number of iterators.</param>
  591. <param name="size1">Number of items per iterator.</param>
  592. <returns>
  593. -
  594. </returns>
  595. <remarks>
  596. Creates a new array of itterator start/array pair.
  597. </remarks>
  598. \**--------------------------------------------------------------------------**/
  599. // If this ever changes, update the size reference in y_users.
  600. #define IteratorArray Iterator //:%1[%2]<%3> %1@YSII_Cg[%2],%1@YSII_Ag[%2][%3+1]
  601. /**--------------------------------------------------------------------------**\
  602. The workings of these macros are very extensively documented at:
  603. http://forum.sa-mp.com/showpost.php?p=2823668
  604. \**--------------------------------------------------------------------------**/
  605. // This allows us to use "new" multiple times - stripping off ONLY whole words.
  606. #define new%0|||%9|||%1:%2||| %9|||%0|||%1|||%2|||
  607. // This one is called if the new syntax is required, but the state of "new" is
  608. // as-yet unknown. This attempts to call "%1" as a macro, if it starts with
  609. // "new" as a whole word then it will (and will also helpfully strip off the
  610. // "new" keyword for us).
  611. #define Y_FOREACH_THIRD|||%0|||%1|||%2||| %1=Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2|||
  612. // This is called if the "new" macro is called for a second time.
  613. #define Y_FOREACH_FOURTH|||%0=Y_FOREACH_FIFTH|||%1|||%2||| new Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2|||
  614. // This is called when there are tags on the "new" declaration.
  615. #define Y_FOREACH_SEVENTH|||%9Y_FOREACH_SIXTH;%0|||%1|||%2||| new %0:%1=%0:_Y_ITER_FOREACH_SIZE(%2);_:(%1=%0:_Y_ITER_ARRAY$%2$YSII_Ag[%1])!=_Y_ITER_MAYBE_ARRAY(%2);
  616. // This is called when there aren't.
  617. #define Y_FOREACH_SIXTH;%0|||Y_FOREACH_SEVENTH|||%2||| %0=_Y_ITER_FOREACH_SIZE(%2);_:(%0=_Y_ITER_ARRAY$%2$YSII_Ag[%0])!=_Y_ITER_MAYBE_ARRAY(%2);
  618. // This is called if "%1" didn't have "new" at the start.
  619. #define Y_FOREACH_FIFTH|||Y_FOREACH_FOURTH|||%1:%2||| _Y_ITER_FOREACH_SIZE(%2);_:(%1=_Y_ITER_ARRAY$%2$YSII_Ag[%1])!=_Y_ITER_MAYBE_ARRAY(%2);
  620. // This is the old version, but DON'T add "new" because that already exists from
  621. // the failed "new" macro call above.
  622. #define Y_FOREACH_SECOND|||Y_FOREACH_THIRD|||%1,%2||| %2=_Y_ITER_FOREACH_SIZE(%1);_:(%2=_Y_ITER_ARRAY$%1$YSII_Ag[%2])!=_Y_ITER_MAYBE_ARRAY(%1);
  623. /**--------------------------------------------------------------------------**\
  624. <summary>foreachex</summary>
  625. <param name="data">Data to itterate through.</param>
  626. <param name="as">Variable to set value to.</param>
  627. <returns>
  628. -
  629. </returns>
  630. <remarks>
  631. Similar to foreach but doesn't declare a new variable for the itterator.
  632. </remarks>
  633. \**--------------------------------------------------------------------------**/
  634. #define foreachex(%1,%2) foreach(%2:%1)
  635. /*
  636. 88
  637. 88 ,d ,d
  638. 88 88 88
  639. 88 MM88MMM ,adPPYba, 8b,dPPYba, ,adPPYYba, MM88MMM ,adPPYba, 8b,dPPYba, ,adPPYba,
  640. 88 88 a8P_____88 88P' "Y8 "" `Y8 88 a8" "8a 88P' "Y8 I8[ ""
  641. 88 88 8PP""""""" 88 ,adPPPPP88 88 8b d8 88 `"Y8ba,
  642. 88 88, "8b, ,aa 88 88, ,88 88, "8a, ,a8" 88 aa ]8I
  643. 88 "Y888 `"Ybbd8"' 88 `"8bbdP"Y8 "Y888 `"YbbdP"' 88 `"YbbdP"'
  644. */
  645. /**--------------------------------------------------------------------------**\
  646. Create the internal itterators.
  647. \**--------------------------------------------------------------------------**/
  648. #if !defined BOTSYNC_IS_BOT
  649. new
  650. Iterator:Player<MAX_PLAYERS>;
  651. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  652. new
  653. Iterator:Bot<MAX_PLAYERS>,
  654. Iterator:Character<MAX_PLAYERS>;
  655. //#define NPC@YSII_Sg Bot@YSII_Sg
  656. #define NPC@YSII_Cg Bot@YSII_Cg
  657. #define NPC@YSII_Ag Bot@YSII_Ag
  658. //#define NPC@YSII_Eg Bot@YSII_Eg
  659. //#define NPC@YSII_Rg Bot@YSII_Rg
  660. #endif
  661. #endif
  662. /*
  663. 88 88
  664. 88 ,d 88
  665. 88 88 88
  666. 88 8b,dPPYba, MM88MMM ,adPPYba, 8b,dPPYba, 8b,dPPYba, ,adPPYYba, 88
  667. 88 88P' `"8a 88 a8P_____88 88P' "Y8 88P' `"8a "" `Y8 88
  668. 88 88 88 88 8PP""""""" 88 88 88 ,adPPPPP88 88
  669. 88 88 88 88, "8b, ,aa 88 88 88 88, ,88 88
  670. 88 88 88 "Y888 `"Ybbd8"' 88 88 88 `"8bbdP"Y8 88
  671. */
  672. /**--------------------------------------------------------------------------**\
  673. Variables to optimise memory usage by only having one copy of each string.
  674. Note that only strings used more than once are put here because only they
  675. have any gain to being located in only one place.
  676. \**--------------------------------------------------------------------------**/
  677. static stock
  678. YSI_gsOnGameModeInit[] = "Itter_OnGameModeInit",
  679. YSI_gsSpecifier@[] = "";
  680. // This is a hack to build the constant number "-1" with "sizeof".
  681. stock const
  682. YSI_NULL_ITERATOR[2]; // NULL target only used for "sizeof".
  683. /**--------------------------------------------------------------------------**\
  684. <summary>Itter_OPDCInternal</summary>
  685. <param name="playerid">Player who left.</param>
  686. <returns>
  687. -
  688. </returns>
  689. <remarks>
  690. Called AFTER "OnPlayerDisconnect" so that using "Kick" inside a "foreach"
  691. loop doesn't crash the server due to an OOB error.
  692. </remarks>
  693. \**--------------------------------------------------------------------------**/
  694. #if !defined BOTSYNC_IS_BOT
  695. public Itter_OPDCInternal(playerid)
  696. {
  697. if (IsPlayerConnected(playerid))
  698. {
  699. return;
  700. }
  701. #if defined _FOREACH_BOT
  702. if (!IsPlayerNPC(playerid))
  703. {
  704. Itter_Remove(Player, playerid);
  705. }
  706. #if !defined FOREACH_NO_BOTS
  707. else
  708. {
  709. Itter_Remove(Bot, playerid);
  710. }
  711. #pragma tabsize 4
  712. Itter_Remove(Character, playerid);
  713. #endif
  714. #else
  715. Itter_Remove(Player, playerid);
  716. #endif
  717. }
  718. #endif
  719. /*
  720. 88 88 88
  721. 88 88 88
  722. 88 88 88
  723. 88aaaaaaaa88 ,adPPYba, ,adPPYba, 88 ,d8 ,adPPYba,
  724. 88""""""""88 a8" "8a a8" "8a 88 ,a8" I8[ ""
  725. 88 88 8b d8 8b d8 8888[ `"Y8ba,
  726. 88 88 "8a, ,a8" "8a, ,a8" 88`"Yba, aa ]8I
  727. 88 88 `"YbbdP"' `"YbbdP"' 88 `Y8a `"YbbdP"'
  728. */
  729. /**--------------------------------------------------------------------------**\
  730. <summary>Itter_OnFilterScriptInit</summary>
  731. <returns>
  732. -
  733. </returns>
  734. <remarks>
  735. Fixes a bug where callbacks are not detected when "loadfs" is used after the
  736. GM has already started. If this is a GM this is just never used called.
  737. </remarks>
  738. \**--------------------------------------------------------------------------**/
  739. #if !defined BOTSYNC_IS_BOT
  740. public OnFilterScriptInit()
  741. {
  742. P:1("Iter_OnFilterScriptInit start: %d", MAX_PLAYERS);
  743. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  744. Bot@YSII_Cg = _Y_ITER_C9:0;
  745. Character@YSII_Cg = _Y_ITER_C9:0;
  746. new
  747. lastBot = MAX_PLAYERS,
  748. lastCharacter = MAX_PLAYERS;
  749. #endif
  750. Player@YSII_Cg = _Y_ITER_C9:0;
  751. new
  752. lastPlayer = MAX_PLAYERS;
  753. for (new i = 0; i != MAX_PLAYERS; ++i)
  754. {
  755. if (IsPlayerConnected(i))
  756. {
  757. #if defined _FOREACH_BOT
  758. // Had to do "if ! else" due to compile options.
  759. if (!IsPlayerNPC(i))
  760. {
  761. Player@YSII_Ag[lastPlayer] = i;
  762. ++Player@YSII_Cg;
  763. lastPlayer = i;
  764. }
  765. #if !defined FOREACH_NO_BOTS
  766. else
  767. {
  768. Bot@YSII_Ag[lastBot] = i;
  769. ++Bot@YSII_Cg;
  770. lastBot = i;
  771. }
  772. #pragma tabsize 4
  773. Character@YSII_Ag[lastCharacter] = i;
  774. ++Character@YSII_Cg;
  775. lastCharacter = i;
  776. #endif
  777. #else
  778. Player@YSII_Ag[lastPlayer] = i;
  779. ++Player@YSII_Cg;
  780. lastPlayer = i;
  781. #endif
  782. }
  783. else
  784. {
  785. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  786. Bot@YSII_Ag[i] = MAX_PLAYERS + 1;
  787. Character@YSII_Ag[i] = MAX_PLAYERS + 1;
  788. #endif
  789. Player@YSII_Ag[i] = MAX_PLAYERS + 1;
  790. }
  791. }
  792. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  793. Bot@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  794. Character@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  795. #endif
  796. Player@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  797. #if defined Itter_OnFilterScriptInit
  798. Itter_OnFilterScriptInit();
  799. #endif
  800. return 1;
  801. }
  802. #if defined _ALS_OnFilterScriptInit
  803. #undef OnFilterScriptInit
  804. #else
  805. #define _ALS_OnFilterScriptInit
  806. #endif
  807. #define OnFilterScriptInit Itter_OnFilterScriptInit
  808. #if defined Itter_OnFilterScriptInit
  809. forward Itter_OnFilterScriptInit();
  810. #endif
  811. #endif
  812. /**--------------------------------------------------------------------------**\
  813. <summary>Itter_OnGameModeInit</summary>
  814. <returns>
  815. -
  816. </returns>
  817. <remarks>
  818. There are WIERD bugs in this script, seemingly caused by the compiler, so
  819. this hopefully fixes them. The OnFilterScriptInit code is written to be
  820. very fast by utilising the internal array structure instead of the regular
  821. Add functions.
  822. </remarks>
  823. \**--------------------------------------------------------------------------**/
  824. #if !defined BOTSYNC_IS_BOT
  825. public OnGameModeInit()
  826. {
  827. P:1("Iter_OnGameModeInit start: %d", MAX_PLAYERS);
  828. if (!Player@YSII_Cg)
  829. {
  830. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  831. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@, Bot@YSII_Cg, Character@YSII_Cg, Player@YSII_Cg);
  832. #else
  833. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@, Player@YSII_Cg);
  834. #endif
  835. #if defined _YSI_SPECIAL_DEBUG
  836. for (new i = 0; i != MAX_PLAYERS; ++i)
  837. {
  838. Player@YSII_Ag[i] = i + 1;
  839. }
  840. Player@YSII_Ag[MAX_PLAYERS] = 0;
  841. Player@YSII_Cg = _Y_ITER_C9:MAX_PLAYERS;
  842. #endif
  843. P:1("Iter_OnGameModeInit: first");
  844. return 1;
  845. }
  846. // Do the forward iterator list.
  847. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  848. Bot@YSII_Cg = _Y_ITER_C9:0;
  849. Bot@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  850. Character@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  851. Character@YSII_Cg = _Y_ITER_C9:0;
  852. new
  853. lastBot = MAX_PLAYERS,
  854. lastCharacter = MAX_PLAYERS;
  855. #endif
  856. Player@YSII_Cg = _Y_ITER_C9:0;
  857. Player@YSII_Ag[MAX_PLAYERS] = MAX_PLAYERS;
  858. new
  859. lastPlayer = MAX_PLAYERS;
  860. for (new i = 0; i != MAX_PLAYERS; ++i)
  861. {
  862. if (IsPlayerConnected(i))
  863. {
  864. #if defined _FOREACH_BOT
  865. // Had to do "if ! else" due to compile options.
  866. if (!IsPlayerNPC(i))
  867. {
  868. Player@YSII_Ag[lastPlayer] = i;
  869. ++Player@YSII_Cg;
  870. lastPlayer = i;
  871. }
  872. #if !defined FOREACH_NO_BOTS
  873. else
  874. {
  875. Bot@YSII_Ag[lastBot] = i;
  876. ++Bot@YSII_Cg;
  877. lastBot = i;
  878. }
  879. #pragma tabsize 4
  880. Character@YSII_Ag[lastCharacter] = i;
  881. ++Character@YSII_Cg;
  882. lastCharacter = i;
  883. #endif
  884. #else
  885. Player@YSII_Ag[lastPlayer] = i;
  886. ++Player@YSII_Cg;
  887. lastPlayer = i;
  888. #endif
  889. }
  890. else
  891. {
  892. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  893. Bot@YSII_Ag[i] = MAX_PLAYERS + 1;
  894. Character@YSII_Ag[i] = MAX_PLAYERS + 1;
  895. #endif
  896. Player@YSII_Ag[i] = MAX_PLAYERS + 1;
  897. }
  898. }
  899. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  900. Bot@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  901. Character@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  902. #endif
  903. Player@YSII_Ag[lastPlayer] = MAX_PLAYERS;
  904. P:1("Iter_OnGameModeInit: lastplayer = %d", lastPlayer);
  905. CallLocalFunction(YSI_gsOnGameModeInit, YSI_gsSpecifier@);
  906. #if defined _YSI_SPECIAL_DEBUG
  907. for (new i = 0; i != MAX_PLAYERS; ++i)
  908. {
  909. Player@YSII_Ag[i] = i + 1;
  910. }
  911. Player@YSII_Ag[MAX_PLAYERS] = 0;
  912. Player@YSII_Cg = _Y_ITER_C9:MAX_PLAYERS;
  913. #endif
  914. return 1;
  915. }
  916. #if defined _ALS_OnGameModeInit
  917. #undef OnGameModeInit
  918. #else
  919. #define _ALS_OnGameModeInit
  920. #endif
  921. #define OnGameModeInit Itter_OnGameModeInit
  922. #if defined Itter_OnGameModeInit
  923. forward Itter_OnGameModeInit();
  924. #endif
  925. #endif
  926. /*
  927. 88888888ba 88
  928. 88 "8b 88
  929. 88 ,8P 88
  930. 88aaaaaa8P' 88 ,adPPYYba, 8b d8 ,adPPYba, 8b,dPPYba, ,adPPYba,
  931. 88""""""' 88 "" `Y8 `8b d8' a8P_____88 88P' "Y8 I8[ ""
  932. 88 88 ,adPPPPP88 `8b d8' 8PP""""""" 88 `"Y8ba,
  933. 88 88 88, ,88 `8b,d8' "8b, ,aa 88 aa ]8I
  934. 88 88 `"8bbdP"Y8 Y88' `"Ybbd8"' 88 `"YbbdP"'
  935. d8'
  936. d8'
  937. */
  938. /**--------------------------------------------------------------------------**\
  939. <summary>Itter_OnPlayerConnect</summary>
  940. <param name="playerid">Player who joined.</param>
  941. <returns>
  942. -
  943. </returns>
  944. <remarks>
  945. Adds a player to the loop data. Now sorts the list too. Note that I found
  946. the most bizzare bug ever (I *think* it may be a compiler but, but it
  947. requires further investigation), basically it seems that multiple variables
  948. were being treated as the same variable (namely @YSII_EgotS and
  949. @YSII_CgharacterS were the same and @YSII_EgotC and @YSII_CgharacterC were the
  950. same). Adding print statements which reference these variables seem to fix
  951. the problem, and I've tried to make sure that the values will never actually
  952. get printed.
  953. </remarks>
  954. \**--------------------------------------------------------------------------**/
  955. #if !defined BOTSYNC_IS_BOT
  956. public OnPlayerConnect(playerid)
  957. {
  958. P:1("Iter_OnPlayerConnect called: %d, %d", playerid, IsPlayerNPC(playerid));
  959. #if defined _FOREACH_BOT
  960. if (!IsPlayerNPC(playerid))
  961. {
  962. Itter_Add(Player, playerid);
  963. }
  964. #if !defined FOREACH_NO_BOTS
  965. else
  966. {
  967. Itter_Add(Bot, playerid);
  968. }
  969. #pragma tabsize 4
  970. Itter_Add(Character, playerid);
  971. #endif
  972. #else
  973. Itter_Add(Player, playerid);
  974. #endif
  975. P:2("Iter_OnPlayerConnect end");
  976. #if defined Itter_OnPlayerConnect
  977. Itter_OnPlayerConnect(playerid);
  978. #endif
  979. return 1;
  980. }
  981. #if defined _ALS_OnPlayerConnect
  982. #undef OnPlayerConnect
  983. #else
  984. #define _ALS_OnPlayerConnect
  985. #endif
  986. #define OnPlayerConnect Itter_OnPlayerConnect
  987. #if defined Itter_OnPlayerConnect
  988. forward Itter_OnPlayerConnect(playerid);
  989. #endif
  990. #endif
  991. /**--------------------------------------------------------------------------**\
  992. <summary>Itter_OnPlayerDisconnect</summary>
  993. <param name="playerid">Player who left.</param>
  994. <returns>
  995. -
  996. </returns>
  997. <remarks>
  998. Removes a player from the loop data. No longer uses "hook" to ENSURE that
  999. this is always last. Previously I think that the order of evaluation in
  1000. y_hooks meant that this got called before the user "OnPlayerDisconnect".
  1001. </remarks>
  1002. \**--------------------------------------------------------------------------**/
  1003. #if !defined BOTSYNC_IS_BOT
  1004. public OnPlayerDisconnect(playerid, reason)
  1005. {
  1006. SetTimerEx("Itter_OPDCInternal", 0, false, "i", playerid);
  1007. #if defined Itter_OnPlayerDisconnect
  1008. Itter_OnPlayerDisconnect(playerid, reason);
  1009. #endif
  1010. return 1;
  1011. }
  1012. #if defined _ALS_OnPlayerDisconnect
  1013. #undef OnPlayerDisconnect
  1014. #else
  1015. #define _ALS_OnPlayerDisconnect
  1016. #endif
  1017. #define OnPlayerDisconnect Itter_OnPlayerDisconnect
  1018. #if defined Itter_OnPlayerDisconnect
  1019. forward Itter_OnPlayerDisconnect(playerid, reason);
  1020. #endif
  1021. #endif
  1022. /*
  1023. db 88888888ba 88 ,ad8888ba, 88
  1024. d88b 88 "8b 88 d8"' `"8b 88
  1025. d8'`8b 88 ,8P 88 d8' 88
  1026. d8' `8b 88aaaaaa8P' 88 88 ,adPPYba, ,adPPYb,88 ,adPPYba,
  1027. d8YaaaaY8b 88""""""' 88 88 a8" "8a a8" `Y88 a8P_____88
  1028. d8""""""""8b 88 88 Y8, 8b d8 8b 88 8PP"""""""
  1029. d8' `8b 88 88 Y8a. .a8P "8a, ,a8" "8a, ,d88 "8b, ,aa
  1030. d8' `8b 88 88 `"Y8888Y"' `"YbbdP"' `"8bbdP"Y8 `"Ybbd8"'
  1031. */
  1032. /**--------------------------------------------------------------------------**\
  1033. <summary>Itter_RandomInternal</summary>
  1034. <param name="count">Number of items in the itterator.</param>
  1035. <param name="array[]">Itterator data.</param>
  1036. <param name="size">Size of the iterator.</param>
  1037. <returns>
  1038. -
  1039. </returns>
  1040. <remarks>
  1041. Returns a random value from an iterator.
  1042. </remarks>
  1043. \**--------------------------------------------------------------------------**/
  1044. stock
  1045. Itter_RandomInternal(count, array[], size)
  1046. {
  1047. if (count == 0)
  1048. {
  1049. return -1;
  1050. }
  1051. new
  1052. rnd = random(count),
  1053. cur = array[size];
  1054. while (cur != size)
  1055. {
  1056. if (rnd-- == 0)
  1057. {
  1058. return cur;
  1059. }
  1060. cur = array[cur];
  1061. }
  1062. return -1;
  1063. }
  1064. /**--------------------------------------------------------------------------**\
  1065. <summary>Itter_FreeInternal</summary>
  1066. <param name="count">Number of items in the itterator.</param>
  1067. <param name="array[]">Itterator data.</param>
  1068. <param name="size">Size of the itterator.</param>
  1069. <returns>
  1070. -
  1071. </returns>
  1072. <remarks>
  1073. Finds the first free slot in the itterator. Itterators now HAVE to be
  1074. sorted for this function to work correctly as it uses that fact to decide
  1075. wether a slot is unused or the last one. If you want to use the slot
  1076. straight after finding it the itterator will need to re-find it to add in
  1077. the data.
  1078. </remarks>
  1079. \**--------------------------------------------------------------------------**/
  1080. stock
  1081. Itter_FreeInternal(array[], size)
  1082. {
  1083. for (new i = 0; i != size; ++i)
  1084. {
  1085. if (array[i] > size)
  1086. {
  1087. return i;
  1088. }
  1089. }
  1090. return -1;
  1091. }
  1092. /**--------------------------------------------------------------------------**\
  1093. <summary>Itter_AddInternal</summary>
  1094. <param name="&start">Array start index.</param>
  1095. <param name="&count">Number of items in the itterator.</param>
  1096. <param name="array[]">Itterator data.</param>
  1097. <param name="value">Item to add.</param>
  1098. <returns>
  1099. -
  1100. </returns>
  1101. <remarks>
  1102. Adds a value to a given itterator set. Now detects when you try and add the
  1103. last item multiple times, as well as all the other items. Now simplified even
  1104. further with the new internal representation.
  1105. </remarks>
  1106. \**--------------------------------------------------------------------------**/
  1107. stock
  1108. Itter_AddInternal(&count, array[], value, size)
  1109. {
  1110. if (0 <= value < size && array[value] > size)
  1111. {
  1112. new
  1113. last = size,
  1114. next = array[last];
  1115. while (next < value)
  1116. {
  1117. last = next;
  1118. next = array[last];
  1119. }
  1120. array[last] = value;
  1121. array[value] = next;
  1122. ++count;
  1123. return 1;
  1124. }
  1125. return 0;
  1126. }
  1127. /**--------------------------------------------------------------------------**\
  1128. <summary>Itter_RemoveInternal</summary>
  1129. <param name="&count">Number of items in the itterator.</param>
  1130. <param name="array[]">Itterator data.</param>
  1131. <param name="value">Item to remove.</param>
  1132. <returns>
  1133. -
  1134. </returns>
  1135. <remarks>
  1136. Removes a value from an itterator.
  1137. </remarks>
  1138. \**--------------------------------------------------------------------------**/
  1139. stock
  1140. Itter_RemoveInternal(&count, array[], value, size)
  1141. {
  1142. new
  1143. last;
  1144. return Itter_SafeRemoveInternal(count, array, value, last, size);
  1145. }
  1146. /**--------------------------------------------------------------------------**\
  1147. <summary>Itter_SafeRemoveInternal</summary>
  1148. <param name="&count">Number of items in the itterator.</param>
  1149. <param name="array[]">Iterator data.</param>
  1150. <param name="back[]">Reverse iterator data.</param>
  1151. <param name="value">Item to remove.</param>
  1152. <param name="&last">Pointer in which to store the last pointer.</param>
  1153. <returns>
  1154. -
  1155. </returns>
  1156. <remarks>
  1157. Removes a value from an itterator safely.
  1158. </remarks>
  1159. \**--------------------------------------------------------------------------**/
  1160. stock
  1161. Itter_SafeRemoveInternal(&count, array[], value, &last, size)
  1162. {
  1163. if (0 <= value < size && array[value] <= size)
  1164. {
  1165. last = size;
  1166. new
  1167. next = array[last];
  1168. while (next != value)
  1169. {
  1170. last = next;
  1171. next = array[last];
  1172. }
  1173. array[last] = array[value];
  1174. array[value] = size + 1;
  1175. --count;
  1176. return 1;
  1177. }
  1178. return 0;
  1179. }
  1180. /**--------------------------------------------------------------------------**\
  1181. <summary>Itter_ContainsInternal</summary>
  1182. <param name="array[]">Itterator data.</param>
  1183. <param name="value">Item to check.</param>
  1184. <param name="size">Size of the iterator.</param>
  1185. <returns>
  1186. -
  1187. </returns>
  1188. <remarks>
  1189. Checks if this item is in the iterator.
  1190. </remarks>
  1191. \**--------------------------------------------------------------------------**/
  1192. stock
  1193. Itter_ContainsInternal(array[], value, size)
  1194. {
  1195. return 0 <= value < size && array[value] <= size;
  1196. }
  1197. /**--------------------------------------------------------------------------**\
  1198. <summary>Itter_ClearInternal</summary>
  1199. <param name="&count">Number of items in the itterator.</param>
  1200. <param name="array[]">Itterator data.</param>
  1201. <param name="back[]">Reverse data.</param>
  1202. <param name="size">Size of the iterator.</param>
  1203. <returns>
  1204. -
  1205. </returns>
  1206. <remarks>
  1207. Resets an iterator.
  1208. </remarks>
  1209. \**--------------------------------------------------------------------------**/
  1210. stock
  1211. Itter_ClearInternal(&count, array[], size)
  1212. {
  1213. for (new i = 0, t = size * 2; i < size; )
  1214. {
  1215. array[i++] = t--;
  1216. }
  1217. array[size] = size;
  1218. count = 0;
  1219. }
  1220. /**--------------------------------------------------------------------------**\
  1221. <summary>Itter_InitInternal</summary>
  1222. <param name="array[][]">Itterator array to initialise.</param>
  1223. <param name="s0">Size of first dimension.</param>
  1224. <param name="s1">Size of second dimension.</param>
  1225. <returns>
  1226. -
  1227. </returns>
  1228. <remarks>
  1229. Multi-dimensional arrays can't be initialised at compile time, so need to be
  1230. done at run time, which is slightly annoying.
  1231. </remarks>
  1232. \**--------------------------------------------------------------------------**/
  1233. stock
  1234. Itter_InitInternal(arr[][], s0, s1)
  1235. {
  1236. for (new i = 0, t = s1 + 1; i < s0; ++i)
  1237. {
  1238. for (new j = 0; j < s1; ++j)
  1239. {
  1240. arr[i][j] = t;
  1241. }
  1242. arr[i][s1] = s1;
  1243. }
  1244. }
  1245. /**--------------------------------------------------------------------------**\
  1246. <summary>Itter_PrevInternal</summary>
  1247. <param name="array[]">Itterator data.</param>
  1248. <param name="size">Size of the iterator.</param>
  1249. <param name="slot">The current slot.</param>
  1250. <returns>
  1251. -
  1252. </returns>
  1253. <remarks>
  1254. Gets the element in an iterator that points to the current element.
  1255. </remarks>
  1256. \**--------------------------------------------------------------------------**/
  1257. stock
  1258. Itter_PrevInternal(array[], size, slot)
  1259. {
  1260. if (0 <= slot <= size && array[slot] <= size)
  1261. {
  1262. for (new last = slot; last--; )
  1263. {
  1264. if (array[last] == slot)
  1265. {
  1266. return last;
  1267. }
  1268. }
  1269. }
  1270. return size;
  1271. }