2
0

y_iterate.inc 24 KB

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