foreach.inc 23 KB

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