foreach.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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. Legal:
  14. Copyright (C) 2009 Alex "Y_Less" Cole
  15. The contents of this file are subject to the Mozilla Public License Version
  16. 1.1 (the "License"); you may not use this file except in compliance with
  17. the License. You may obtain a copy of the License at
  18. [url]http://www.mozilla.org/MPL/[/url]
  19. Software distributed under the License is distributed on an "AS IS" basis,
  20. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  21. for the specific language governing rights and limitations under the
  22. License.
  23. The Original Code is the SA:MP foreach iterator code.
  24. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  25. Version:
  26. 0.1.8
  27. Changelog:
  28. 05/03/11:
  29. Added tag-free code.
  30. Updated to YSI y_iterate compatible code.
  31. Added self-fixing code for internal errors.
  32. 16/08/10:
  33. Removed all the "2" versions of the functions.
  34. 14/08/10:
  35. Added Iter_Clear to reset an array.
  36. 06/08/10:
  37. Added special array declaration format.
  38. 18/12/09:
  39. Added Itter_Func2 functions for multi-dimensional iterators.
  40. Renamed foreact et al as keywords in the documentation.
  41. Changed licensing from GPL to MPL.
  42. 02/09/09:
  43. Fixed (again) for 0.3.
  44. Added free slot finding.
  45. 21/08/09:
  46. Updated to include random functions.
  47. Made entirely stand alone.
  48. Ported to 0.3 (separate version).
  49. Added automatic callback hook code.
  50. Removed debug information from stand alone version.
  51. 06/01/08:
  52. Added debug information.
  53. 09/10/07:
  54. Moved to system.
  55. 16/09/07:
  56. Added list sorting.
  57. Made this part of Y SeRver Includes, not Y Sever Includes.
  58. Made list sorting optional.
  59. Fixed version number.
  60. 08/09/07:
  61. First version.
  62. Functions:
  63. Public:
  64. OnPlayerDisconnect - Called when a player leaves to remove them.
  65. OnPlayerConnect - Called when a player connects to add them.
  66. Core:
  67. -
  68. Stock:
  69. Itter_ShowArray - Displays the contents of the array.
  70. Itter_AddInternal - Add a value to an itterator.
  71. Itter_RemoveInternal - Remove a value from an itterator.
  72. Itter_RandomInternal - Get a random item from an itterator.
  73. Itter_FreeInternal - Gets the first free slot in the itterator.
  74. Itter_InitInternal - Initialises a multi-dimensional itterator.
  75. Static:
  76. -
  77. Inline:
  78. Itter_Create - Create a new itterator value set.
  79. Itter_Add - Wraps Itter_AddInternal.
  80. Itter_Remove - Wraps Itter_RemoveInternal.
  81. Itter_Random - Wraps Itter_RandomInternal.
  82. Itter_Count - Gets the number of items in an itterator.
  83. Itter_Debug - Wraps around Itter_ShowArray.
  84. Itter_Free - Wraps around Itter_FreeInternal.
  85. Itter_Create2 - Create a new itterator array value set.
  86. Itter_Add2 - Wraps Itter_AddInternal for arrays.
  87. Itter_Remove2 - Wraps Itter_RemoveInternal for arrays.
  88. Itter_Random2 - Wraps Itter_RandomInternal for arrays.
  89. Itter_Count2 - Gets the number of items in an itterator array.
  90. Itter_Debug2 - Wraps around Itter_ShowArray for arrays.
  91. Itter_Free2 - Wraps around Itter_FreeInternal for arrays.
  92. API:
  93. -
  94. Callbacks:
  95. -
  96. Hooks:
  97. Itter_OnPlayerConnect - Hook for the OnPlayerConnect callback.
  98. Itter_OnPlayerDisconnect - Hook for the OnPlayerDisconnect callback.
  99. Itter_OnGameModeInit - Only exists to make the code compile correctly...
  100. Definitions:
  101. -
  102. Enums:
  103. -
  104. Macros:
  105. -
  106. Keywords:
  107. foreach - Command to loop an iterator.
  108. foreachex - Like foreach but without a new variable.
  109. foreach2 - Command to loop through an iterator array.
  110. foreachex - Like foreach2 but without a new variable.
  111. Tags:
  112. Iterator - Declare an iterator.
  113. Variables:
  114. Global:
  115. -
  116. Static:
  117. YSI_g_OPC - Records wether Itter_OnPlayerConnect exists for speed.
  118. YSI_g_OPDC - Records wether Itter_OnPlayerDisconnect exists for speed.
  119. Commands:
  120. -
  121. Compile options:
  122. YSI_ITTER_NO_SORT - Removed.
  123. FOREACH_NO_BOTS - Remove the bot iterators for smaller code.
  124. FOREACH_NO_PLAYERS - Remove all default code for player itteration.
  125. Operators:
  126. -
  127. Iterators:
  128. Player - List of all players connected.
  129. Bot - List of all bots (npcs) connected.
  130. NPC - Alias of Bot.
  131. Character - All players and bots.
  132. -*----------------------------------------------------------------------------*/
  133. #define _FOREACH_LOCAL_VERSION 2
  134. // Foreach is testing us.
  135. #if defined _FOREACH_INC_TEST
  136. #define _FOREACH_CUR_VERSION _FOREACH_LOCAL_VERSION
  137. #endinput
  138. #endif
  139. #if !defined _FOREACH_NO_TEST
  140. #define _FOREACH_INC_TEST
  141. #tryinclude <YSI\y_iterate>
  142. #undef _FOREACH_INC_TEST
  143. // <y_iterate> exists - test which is newer.
  144. #if defined _inc_y_iterate
  145. #if !defined _FOREACH_CUR_VERSION
  146. // y_iterate exists, but it's an old version - don't try use this
  147. // system or the variables will conflict.
  148. #endinput
  149. #endif
  150. #if _FOREACH_CUR_VERSION > _FOREACH_LOCAL_VERSION
  151. // y_iterate is newer.
  152. #undef _inc_y_iterate
  153. #define _FOREACH_NO_TEST
  154. #include <YSI\y_iterate>
  155. #endinput
  156. #endif
  157. #endif
  158. #endif
  159. #if !defined _samp_included
  160. #error "Please include a_samp or a_npc before foreach"
  161. #endif
  162. #if defined SendChat || defined FOREACH_NO_PLAYERS
  163. #define BOTSYNC_IS_BOT (true)
  164. #endif
  165. #if defined IsPlayerNPC
  166. #define _FOREACH_BOT
  167. #endif
  168. #if !defined BOTSYNC_IS_BOT
  169. static
  170. bool:YSI_g_OPC = false,
  171. bool:YSI_g_OPDC = false;
  172. #endif
  173. #if defined YSI_ITTER_NO_SORT
  174. #error YSI_ITTER_NO_SORT is no longer supported by foreach.
  175. #endif
  176. /*----------------------------------------------------------------------------*-
  177. Function:
  178. Itter_Create2
  179. Params:
  180. name - Itterator identifier.
  181. size0 - Number of iterators.
  182. size1 - Number of items per iterator.
  183. Return:
  184. -
  185. Notes:
  186. Creates a new array of itterator start/array pair.
  187. -*----------------------------------------------------------------------------*/
  188. #define Iter_Create2 Itter_Create2
  189. #define Itter_Create2(%1,%2,%3) \
  190. new \
  191. YSI_gS%1[%2] = {-1, ...}, \
  192. YSI_gC%1[%2] = {0}, \
  193. YSI_gA%1[%2][%3]
  194. #define IteratorArray:%1[%2]<%3> \
  195. YSI_gS%1[%2] = {-1, ...}, \
  196. YSI_gC%1[%2] = {0}, \
  197. YSI_gA%1[%2][%3]
  198. /*----------------------------------------------------------------------------*-
  199. Function:
  200. Itter_Init2
  201. Params:
  202. itter - Name of the itterator array to initialise.
  203. Return:
  204. -
  205. Notes:
  206. Wrapper for Itter_InitInternal.
  207. native Iter_Init(IteratorArray:Name[]<>);
  208. -*----------------------------------------------------------------------------*/
  209. #define Iter_Init Itter_Init
  210. #define Itter_Init(%1) \
  211. Itter_InitInternal(YSI_gA%1, sizeof (YSI_gA%1), sizeof (YSI_gA%1[]))
  212. /*----------------------------------------------------------------------------*-
  213. Function:
  214. Itter_Create
  215. Params:
  216. name - Itterator identifier.
  217. size - Number of values.
  218. Return:
  219. -
  220. Notes:
  221. Creates a new itterator start/array pair.
  222. -*----------------------------------------------------------------------------*/
  223. #define Iter_Create Itter_Create
  224. #define Itter_Create(%1,%2) \
  225. new \
  226. YSI_gS%1 = -1, \
  227. YSI_gC%1 = 0, \
  228. YSI_gA%1[%2] = {-1, ...}
  229. /*----------------------------------------------------------------------------*-
  230. Array:
  231. Iterator
  232. Notes:
  233. Creates a new itterator start/array pair.
  234. -*----------------------------------------------------------------------------*/
  235. #define Iterator:%1<%2> \
  236. YSI_gS%1 = -1, \
  237. YSI_gC%1 = 0, \
  238. YSI_gA%1[%2] = {-1, ...}
  239. /*----------------------------------------------------------------------------*-
  240. Function:
  241. Itter_Add
  242. Params:
  243. itter - Name of the itterator to add the data to.
  244. value - Value to add to the itterator.
  245. Return:
  246. -
  247. Notes:
  248. Wrapper for Itter_AddInternal.
  249. native Iter_Add(Iterator:Name<>, value);
  250. -*----------------------------------------------------------------------------*/
  251. #define Iter_Add Itter_Add
  252. #define Itter_Add(%1,%2) \
  253. Itter_AddInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, %2)
  254. /*----------------------------------------------------------------------------*-
  255. Function:
  256. Itter_Free
  257. Params:
  258. itter - Name of the itterator to get the first free slot in.
  259. Return:
  260. -
  261. Notes:
  262. Wrapper for Itter_FreeInternal.
  263. native Iter_Free(Iterator:Name<>);
  264. -*----------------------------------------------------------------------------*/
  265. #define Iter_Free Itter_Free
  266. #define Itter_Free(%1) \
  267. Itter_FreeInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, sizeof (YSI_gA%1))
  268. /*----------------------------------------------------------------------------*-
  269. Function:
  270. Itter_Remove
  271. Params:
  272. itter - Name of the itterator to remove data from.
  273. value - Data to remove.
  274. Return:
  275. -
  276. Notes:
  277. Wrapper for Itter_RemoveInternal.
  278. native Iter_Remove(Iterator:Name<>, value);
  279. -*----------------------------------------------------------------------------*/
  280. #define Iter_Remove Itter_Remove
  281. #define Itter_Remove(%1,%2) \
  282. Itter_RemoveInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1, %2)
  283. /*----------------------------------------------------------------------------*-
  284. Function:
  285. Itter_Random
  286. Params:
  287. itter - Name of the itterator to get a random slot from.
  288. Return:
  289. -
  290. Notes:
  291. Wrapper for Itter_RandomInternal.
  292. native Iter_Random(Iterator:Name<>);
  293. -*----------------------------------------------------------------------------*/
  294. #define Iter_Random Itter_Random
  295. #define Itter_Random(%1) \
  296. Itter_RandomInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1)
  297. /*----------------------------------------------------------------------------*-
  298. Function:
  299. Itter_Debug
  300. Params:
  301. itter - Name of the itterator to output debug information from.
  302. Return:
  303. -
  304. Notes:
  305. Wrapper for Itter_ShowArray.
  306. -*----------------------------------------------------------------------------*/
  307. #define Iter_Debug Itter_Debug
  308. #define Itter_Debug(%1) \
  309. Itter_ShowArray(YSI_gS%1, YSI_gA%1, YSI_gC%1)
  310. /*----------------------------------------------------------------------------*-
  311. Function:
  312. Itter_Count
  313. Params:
  314. itter - Name of the itterator to get a random slot from4.
  315. Return:
  316. -
  317. Notes:
  318. Returns the number of items in this itterator.
  319. native Iter_Count(Iterator:Name<>);
  320. -*----------------------------------------------------------------------------*/
  321. #define Iter_Count Itter_Count
  322. #define Itter_Count(%1) \
  323. YSI_gC%1
  324. /*----------------------------------------------------------------------------*-
  325. Function:
  326. Itter_Clear
  327. Params:
  328. itter - Name of the itterator empty.
  329. Return:
  330. -
  331. Notes:
  332. Wrapper for Itter_ClearInternal.
  333. native Iter_Clear(IteratorArray:Name[]<>);
  334. -*----------------------------------------------------------------------------*/
  335. #define Iter_Clear Itter_Clear
  336. #define Itter_Clear(%1) \
  337. Itter_ClearInternal(YSI_gS%1, YSI_gC%1, YSI_gA%1)
  338. /*----------------------------------------------------------------------------*-
  339. Create the internal itterators.
  340. -*----------------------------------------------------------------------------*/
  341. #if !defined BOTSYNC_IS_BOT
  342. new
  343. Iterator:Player<MAX_PLAYERS>;
  344. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  345. new
  346. Iterator:Bot<MAX_PLAYERS>,
  347. Iterator:Character<MAX_PLAYERS>;
  348. #define YSI_gNPCS YSI_gBotS
  349. #define YSI_gNPCC YSI_gBotC
  350. #define YSI_gNPCA YSI_gBotA
  351. #endif
  352. #endif
  353. /*----------------------------------------------------------------------------*-
  354. Function:
  355. foreach
  356. Params:
  357. data - Data to itterate through.
  358. as - Variable to set value to.
  359. Return:
  360. -
  361. Notes:
  362. Not exactly the same as PHP foreach, just itterates through a list and
  363. returns the value of the current slot but uses that slot as the next index
  364. too. Variables must be in the form YSI_g<name>S for the start index and
  365. YSI_g<name>A for the data array where <name> is what's entered in data.
  366. -*----------------------------------------------------------------------------*/
  367. #define foreach(%1,%2) \
  368. for (new %2 = YSI_gS%1; _:%2 != -1; %2 = YSI_gA%1[%2])
  369. /*----------------------------------------------------------------------------*-
  370. Function:
  371. foreachex
  372. Params:
  373. data - Data to itterate through.
  374. as - Variable to set value to.
  375. Return:
  376. -
  377. Notes:
  378. Similar to foreach but doesn't declare a new variable for the itterator.
  379. -*----------------------------------------------------------------------------*/
  380. #define foreachex(%1,%2) \
  381. for (%2 = YSI_gS%1; _:%2 != -1; %2 = YSI_gA%1[%2])
  382. /*----------------------------------------------------------------------------*-
  383. Function:
  384. Itter_OnPlayerConnect
  385. Params:
  386. playerid - Player who joined.
  387. Return:
  388. -
  389. Notes:
  390. Adds a player to the loop data. Now sorts the list too. Note that I found
  391. the most bizzare bug ever (I *think* it may be a compiler but, but it
  392. requires further investigation), basically it seems that multiple variables
  393. were being treated as the same variable (namely YSI_gBotS and
  394. YSI_gCharacterS were the same and YSI_gBotC and YSI_gCharacterC were the
  395. same). Adding print statements which reference these variables seem to fix
  396. the problem, and I've tried to make sure that the values will never actually
  397. get printed.
  398. -*----------------------------------------------------------------------------*/
  399. #if !defined BOTSYNC_IS_BOT
  400. public
  401. OnPlayerConnect(playerid)
  402. {
  403. #if defined _FOREACH_BOT
  404. if (!IsPlayerNPC(playerid))
  405. {
  406. Itter_Add(Player, playerid);
  407. }
  408. #if !defined FOREACH_NO_BOTS
  409. else
  410. {
  411. Itter_Add(Bot, playerid);
  412. }
  413. #pragma tabsize 4
  414. Itter_Add(Character, playerid);
  415. #endif
  416. #else
  417. Itter_Add(Player, playerid);
  418. #endif
  419. if (YSI_g_OPC)
  420. {
  421. return CallLocalFunction("Itter_OnPlayerConnect", "i", playerid);
  422. }
  423. return 1;
  424. }
  425. #if defined _ALS_OnPlayerConnect
  426. #undef OnPlayerConnect
  427. #else
  428. #define _ALS_OnPlayerConnect
  429. #endif
  430. #define OnPlayerConnect Itter_OnPlayerConnect
  431. forward
  432. Itter_OnPlayerConnect(playerid);
  433. #endif
  434. /*----------------------------------------------------------------------------*-
  435. Function:
  436. Itter_OnGameModeInit
  437. Params:
  438. -
  439. Return:
  440. -
  441. Notes:
  442. There are WIERD bugs in this script, seemingly caused by the compiler, so
  443. this hopefully fixes them. The OnFilterScriptInit code is written to be
  444. very fast by utilising the internal array structure instead of the regular
  445. Add functions.
  446. -*----------------------------------------------------------------------------*/
  447. #if !defined BOTSYNC_IS_BOT
  448. #if defined FILTERSCRIPT
  449. public
  450. OnFilterScriptInit()
  451. {
  452. if (YSI_gCPlayer)
  453. {
  454. print("foreach error: Something went wrong again! Please tell Y_less");
  455. // Try reset.
  456. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  457. printf("%d", YSI_gSBot);
  458. printf("%d", YSI_gCBot);
  459. printf("%d", YSI_gSCharacter);
  460. printf("%d", YSI_gCCharacter);
  461. #endif
  462. printf("%d", YSI_gSPlayer);
  463. printf("%d", YSI_gCPlayer);
  464. }
  465. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  466. new
  467. lastBot = -1,
  468. lastCharacter = -1;
  469. #endif
  470. new
  471. lastPlayer = -1;
  472. for (new i = 0; i != MAX_PLAYERS; ++i)
  473. {
  474. if (IsPlayerConnected(i))
  475. {
  476. #if defined _FOREACH_BOT
  477. if (!IsPlayerNPC(i))
  478. {
  479. if (lastPlayer == -1)
  480. {
  481. YSI_gSPlayer = i;
  482. }
  483. else
  484. {
  485. YSI_gAPlayer[lastPlayer] = i;
  486. }
  487. ++YSI_gCPlayer;
  488. lastPlayer = i;
  489. }
  490. #if !defined FOREACH_NO_BOTS
  491. else
  492. {
  493. if (lastBot == -1)
  494. {
  495. YSI_gSBot = i;
  496. }
  497. else
  498. {
  499. YSI_gABot[lastBot] = i;
  500. }
  501. ++YSI_gCBot;
  502. lastBot = i;
  503. }
  504. #pragma tabsize 4
  505. if (lastCharacter == -1)
  506. {
  507. YSI_gSCharacter = i;
  508. }
  509. else
  510. {
  511. YSI_gACharacter[lastCharacter] = i;
  512. }
  513. ++YSI_gCCharacter;
  514. lastCharacter = i;
  515. #endif
  516. #else
  517. if (lastPlayer == -1)
  518. {
  519. YSI_gSPlayer = i;
  520. }
  521. else
  522. {
  523. YSI_gAPlayer[lastPlayer] = i;
  524. }
  525. ++YSI_gCPlayer;
  526. lastPlayer = i;
  527. #endif
  528. }
  529. }
  530. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  531. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  532. CallLocalFunction("Itter_OnFilterScriptInit", "");
  533. }
  534. #if defined _ALS_OnFilterScriptInit
  535. #undef OnFilterScriptInit
  536. #else
  537. #define _ALS_OnFilterScriptInit
  538. #endif
  539. #define OnFilterScriptInit Itter_OnFilterScriptInit
  540. forward Itter_OnFilterScriptInit();
  541. #else
  542. public
  543. OnGameModeInit()
  544. {
  545. if (YSI_gCPlayer)
  546. {
  547. print("foreach error: Something went wrong again! Is this a Filterscript and have you");
  548. print("foreach error: got \"#define FILTERSCRIPT\" above all your includes? Resetting...");
  549. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  550. printf("%d", YSI_gSBot);
  551. printf("%d", YSI_gCBot);
  552. printf("%d", YSI_gSCharacter);
  553. printf("%d", YSI_gCCharacter);
  554. YSI_gCBot = 0;
  555. YSI_gCCharacter = 0;
  556. YSI_gSBot = -1;
  557. YSI_gSCharacter = -1;
  558. #endif
  559. printf("%d", YSI_gSPlayer);
  560. printf("%d", YSI_gCPlayer);
  561. YSI_gCPlayer = 0;
  562. YSI_gSPlayer = -1;
  563. for (new i = 0; i != MAX_PLAYERS; ++i)
  564. {
  565. #if defined _FOREACH_BOT && !defined FOREACH_NO_BOTS
  566. YSI_gABot[i] = -1;
  567. YSI_gACharacter[i] = -1;
  568. #endif
  569. YSI_gAPlayer[i] = -1;
  570. }
  571. }
  572. YSI_g_OPC = (funcidx("Itter_OnPlayerConnect") != -1);
  573. YSI_g_OPDC = (funcidx("Itter_OnPlayerDisconnect") != -1);
  574. CallLocalFunction("Itter_OnGameModeInit", "");
  575. }
  576. #if defined _ALS_OnGameModeInit
  577. #undef OnGameModeInit
  578. #else
  579. #define _ALS_OnGameModeInit
  580. #endif
  581. #define OnGameModeInit Itter_OnGameModeInit
  582. forward
  583. Itter_OnGameModeInit();
  584. #endif
  585. #endif
  586. /*----------------------------------------------------------------------------*-
  587. Function:
  588. Itter_OnPlayerDisconnect
  589. Params:
  590. playerid - Player who left.
  591. Return:
  592. -
  593. Notes:
  594. Removes a player from the loop data.
  595. -*----------------------------------------------------------------------------*/
  596. #if !defined BOTSYNC_IS_BOT
  597. public
  598. OnPlayerDisconnect(playerid, reason)
  599. {
  600. #if defined _FOREACH_BOT
  601. if (!IsPlayerNPC(playerid))
  602. {
  603. Itter_Remove(Player, playerid);
  604. }
  605. #if !defined FOREACH_NO_BOTS
  606. else
  607. {
  608. Itter_Remove(Bot, playerid);
  609. }
  610. #pragma tabsize 4
  611. Itter_Remove(Character, playerid);
  612. #endif
  613. #else
  614. Itter_Remove(Player, playerid);
  615. #endif
  616. if (YSI_g_OPDC)
  617. {
  618. return CallLocalFunction("Itter_OnPlayerDisconnect", "ii", playerid, reason);
  619. }
  620. return 1;
  621. }
  622. #if defined _ALS_OnPlayerDisconnect
  623. #undef OnPlayerDisconnect
  624. #else
  625. #define _ALS_OnPlayerDisconnect
  626. #endif
  627. #define OnPlayerDisconnect Itter_OnPlayerDisconnect
  628. forward
  629. Itter_OnPlayerDisconnect(playerid, reason);
  630. #endif
  631. /*----------------------------------------------------------------------------*-
  632. Function:
  633. Itter_ShowArray
  634. Params:
  635. start - Itterator start point.
  636. members[] - Itterator contents.
  637. size - Number of itterator values
  638. Return:
  639. -
  640. Notes:
  641. Pure debug function. Has regular prints not debug prints
  642. as it's only called when debug is on.
  643. -*----------------------------------------------------------------------------*/
  644. stock
  645. Itter_ShowArray(start, members[], size)
  646. {
  647. static
  648. sString[61];
  649. new
  650. i,
  651. j = 10;
  652. printf("Start: %d", start);
  653. printf("Size: %d", size);
  654. while (i < size)
  655. {
  656. sString[0] = '\0';
  657. while (i < j && i < size)
  658. {
  659. format(sString, sizeof (sString), "%s, %d", sString, members[i]);
  660. i++;
  661. }
  662. printf("Array (%d): %s", j, sString);
  663. j += 10;
  664. }
  665. }
  666. /*----------------------------------------------------------------------------*-
  667. Function:
  668. Itter_RandomInternal
  669. Params:
  670. start - Array start index.
  671. count - Number of items in the itterator.
  672. array[] - Itterator data.
  673. Return:
  674. -
  675. Notes:
  676. Returns a random value from an iterator.
  677. -*----------------------------------------------------------------------------*/
  678. stock
  679. Itter_RandomInternal(start, count, array[])
  680. {
  681. if (count == 0)
  682. {
  683. return -1;
  684. }
  685. new
  686. rnd = random(count),
  687. cur = start;
  688. while (cur != -1)
  689. {
  690. if (rnd--)
  691. {
  692. cur = array[cur];
  693. }
  694. else
  695. {
  696. return cur;
  697. }
  698. }
  699. return -1;
  700. }
  701. /*----------------------------------------------------------------------------*-
  702. Function:
  703. Itter_FreeInternal
  704. Params:
  705. start - Array start index.
  706. count - Number of items in the itterator.
  707. array[] - Itterator data.
  708. size - Size of the itterator.
  709. Return:
  710. -
  711. Notes:
  712. Finds the first free slot in the itterator. Itterators now HAVE to be
  713. sorted for this function to work correctly as it uses that fact to decide
  714. wether a slot is unused or the last one. If you want to use the slot
  715. straight after finding it the itterator will need to re-find it to add in
  716. the data.
  717. -*----------------------------------------------------------------------------*/
  718. stock
  719. Itter_FreeInternal(start, count, array[], size)
  720. {
  721. if (count == size)
  722. {
  723. return -1;
  724. }
  725. else if (count == 0)
  726. {
  727. return 0;
  728. }
  729. new
  730. first = 0;
  731. while (first != -1)
  732. {
  733. if (first == start)
  734. {
  735. start = array[start];
  736. }
  737. else if (array[first] == -1)
  738. {
  739. return first;
  740. }
  741. ++first;
  742. }
  743. return -1;
  744. }
  745. /*----------------------------------------------------------------------------*-
  746. Function:
  747. Itter_AddInternal
  748. Params:
  749. &start - Array start index.
  750. &count - Number of items in the itterator.
  751. array[] - Itterator data.
  752. value - Item to add.
  753. Return:
  754. -
  755. Notes:
  756. Adds a value to a given itterator set.
  757. -*----------------------------------------------------------------------------*/
  758. stock
  759. Itter_AddInternal(&start, &count, array[], value)
  760. {
  761. if (array[value] != -1)
  762. {
  763. return 0;
  764. }
  765. ++count;
  766. if (start == -1)
  767. {
  768. start = value;
  769. }
  770. else if (start > value)
  771. {
  772. array[value] = start;
  773. start = value;
  774. }
  775. else
  776. {
  777. new
  778. cur = start,
  779. last;
  780. do
  781. {
  782. last = cur;
  783. cur = array[cur];
  784. if (cur > value)
  785. {
  786. array[value] = cur;
  787. array[last] = value;
  788. return 1;
  789. }
  790. }
  791. while (cur != -1);
  792. array[last] = value;
  793. }
  794. return 1;
  795. }
  796. /*----------------------------------------------------------------------------*-
  797. Function:
  798. Itter_RemoveInternal
  799. Params:
  800. &start - Array start index.
  801. &count - Number of items in the itterator.
  802. array[] - Itterator data.
  803. value - Item to remove.
  804. Return:
  805. -
  806. Notes:
  807. Removes a value from an itterator.
  808. -*----------------------------------------------------------------------------*/
  809. stock
  810. Itter_RemoveInternal(&start, &count, array[], value)
  811. {
  812. if (start == -1)
  813. {
  814. return 0;
  815. }
  816. if (start == value)
  817. {
  818. start = array[value];
  819. }
  820. else
  821. {
  822. new
  823. cur = start;
  824. while (array[cur] != value)
  825. {
  826. cur = array[cur];
  827. if (cur == -1)
  828. {
  829. return 0;
  830. }
  831. }
  832. array[cur] = array[value];
  833. }
  834. array[value] = -1;
  835. --count;
  836. return 1;
  837. }
  838. /*----------------------------------------------------------------------------*-
  839. Function:
  840. Itter_ClearInternal
  841. Params:
  842. &start - Array start index.
  843. &count - Number of items in the itterator.
  844. array[] - Itterator data.
  845. Return:
  846. -
  847. Notes:
  848. Resets an iterator.
  849. -*----------------------------------------------------------------------------*/
  850. stock
  851. Itter_ClearInternal(&start, &count, array[])
  852. {
  853. if (start != -1)
  854. {
  855. new
  856. cur = start,
  857. next = array[cur];
  858. start = -1;
  859. count = 0;
  860. while (next != -1)
  861. {
  862. array[cur] = -1;
  863. cur = next;
  864. next = array[cur];
  865. }
  866. }
  867. }
  868. /*----------------------------------------------------------------------------*-
  869. Function:
  870. Itter_InitInternal
  871. Params:
  872. array[][] - Itterator array to initialise.
  873. s0 - Size of first dimension.
  874. s1 - Size of second dimension.
  875. Return:
  876. -
  877. Notes:
  878. Multi-dimensional arrays can't be initialised at compile time, so need to be
  879. done at run time, which is slightly annoying.
  880. -*----------------------------------------------------------------------------*/
  881. stock
  882. Itter_InitInternal(arr[][], s0, s1)
  883. {
  884. for (new i = 0; i != s0; ++i)
  885. {
  886. for (new j = 0; j != s1; ++j)
  887. {
  888. arr[i][j] = -1;
  889. }
  890. }
  891. }