y_iterate.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* *
  2. * *
  3. * _____ ______ ______ ____ ______ _ ______ __ ______ ____ _____ *
  4. * / ____| ____| ____| | _ \| ____| | / __ \ \ / / | ____/ __ \| __ \ *
  5. * | (___ | |__ | |__ | |_) | |__ | | | | | \ \ /\ / / | |__ | | | | |__) | *
  6. * \___ \| __| | __| | _ <| __| | | | | | |\ \/ \/ / | __|| | | | _ / *
  7. * ____) | |____| |____ | |_) | |____| |___| |__| | \ /\ / | | | |__| | | \ \ *
  8. * |_____/|______|______| |____/|______|______\____/ \/ \/ |_| \____/|_| \_\ *
  9. * *
  10. * _____ ____ _____ _ _ __ __ ______ _ _ _______ _______ _____ ____ _ _ *
  11. * | __ \ / __ \ / ____| | | | \/ | ____| \ | |__ __|/\|__ __|_ _/ __ \| \ | | *
  12. * | | | | | | | | | | | | \ / | |__ | \| | | | / \ | | | || | | | \| | *
  13. * | | | | | | | | | | | | |\/| | __| | . ` | | | / /\ \ | | | || | | | . ` | *
  14. * | |__| | |__| | |____| |__| | | | | |____| |\ | | |/ ____ \| | _| || |__| | |\ | *
  15. * |_____/ \____/ \_____|\____/|_| |_|______|_| \_| |_/_/ \_\_| |_____\____/|_| \_| *
  16. * *
  17. * This is required for technical reasons - to place it after `#endinput` to not generate *
  18. * multiple copies of it in XML when compiling with `-r`. *
  19. * *
  20. *//*
  21. Legal:
  22. Version: MPL 1.1
  23. The contents of this file are subject to the Mozilla Public License Version
  24. 1.1 the "License"; you may not use this file except in compliance with
  25. the License. You may obtain a copy of the License at
  26. http://www.mozilla.org/MPL/
  27. Software distributed under the License is distributed on an "AS IS" basis,
  28. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  29. for the specific language governing rights and limitations under the
  30. License.
  31. The Original Code is the YSI framework.
  32. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  33. Portions created by the Initial Developer are Copyright C 2011
  34. the Initial Developer. All Rights Reserved.
  35. Contributors:
  36. Y_Less
  37. koolk
  38. JoeBullet/Google63
  39. g_aSlice/Slice
  40. Misiur
  41. samphunter
  42. tianmeta
  43. maddinat0r
  44. spacemud
  45. Crayder
  46. Dayvison
  47. Ahmad45123
  48. Zeex
  49. irinel1996
  50. Yiin-
  51. Chaprnks
  52. Konstantinos
  53. Masterchen09
  54. Southclaws
  55. PatchwerkQWER
  56. m0k1
  57. paulommu
  58. udan111
  59. Thanks:
  60. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  61. ZeeX - Very productive conversations.
  62. koolk - IsPlayerinAreaEx code.
  63. TheAlpha - Danish translation.
  64. breadfish - German translation.
  65. Fireburn - Dutch translation.
  66. yom - French translation.
  67. 50p - Polish translation.
  68. Zamaroht - Spanish translation.
  69. Los - Portuguese translation.
  70. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  71. me to strive to better.
  72. Pixels^ - Running XScripters where the idea was born.
  73. Matite - Pestering me to release it and using it.
  74. Very special thanks to:
  75. Thiadmer - PAWN, whose limits continue to amaze me!
  76. Kye/Kalcor - SA:MP.
  77. SA:MP Team past, present and future - SA:MP.
  78. Optional plugins:
  79. Gamer_Z - GPS.
  80. Incognito - Streamer.
  81. Me - sscanf2, fixes2, Whirlpool.
  82. */
  83. #if !defined _INC_y_iterate
  84. #tryinclude "y_foreach\y_foreach_entry"
  85. #endif
  86. #if !defined _INC_y_iterate
  87. #tryinclude "YSI_Data\y_foreach\y_foreach_entry"
  88. #endif
  89. #if !defined _INC_y_iterate
  90. #tryinclude "YSI\YSI_Data\y_foreach\y_foreach_entry"
  91. #endif
  92. #if !defined _INC_y_iterate
  93. #tryinclude <YSI_Data\y_foreach\y_foreach_entry>
  94. #endif
  95. #if !defined _INC_y_iterate
  96. #tryinclude <YSI\YSI_Data\y_foreach\y_foreach_entry>
  97. #endif
  98. #if !defined _INC_y_iterate
  99. #error Could not find y_iterate
  100. #endif
  101. /**
  102. * <library name="y_iterate">
  103. * <section>
  104. * Description
  105. * </section>
  106. * Provides efficient looping through sparse data sets, such as connected
  107. * players. Significantly improved from the original version to be a generic
  108. * loop system, rather then purely a player loop system. When used for
  109. * players this has constant time O(n) for number of connected players (n),
  110. * unlike standard player loops which are O(MAX_PLAYERS), regardless of the
  111. * actual number of connected players. Even when n is MAX_PLAYERS this is
  112. * still faster.
  113. *
  114. * For extensive documentation on writing and using iterators, see this topic:
  115. *
  116. * <a href="http://forum.sa-mp.com/showthread.php?t=481877" />
  117. *
  118. * <section>
  119. * Version
  120. * </section>
  121. * 0.4
  122. * <section>
  123. * Functions
  124. * </section>
  125. * <subsection>
  126. * Public
  127. * </subsection><ul>
  128. * <symbol name="OnPlayerDisconnect">Called when a player leaves to remove them.</symbol>
  129. * <symbol name="OnPlayerConnect">Called when a player connects to add them.</symbol>
  130. * </ul><subsection>
  131. * Stock
  132. * </subsection><ul>
  133. * <symbol name="Iter_ShowArray">Displays the contents of the array.</symbol>
  134. * <symbol name="Iter_AddInternal">Add a value to an iterator.</symbol>
  135. * <symbol name="Iter_RemoveInternal">Remove a value from an iterator.</symbol>
  136. * <symbol name="Iter_RandomInternal">Get a random item from an iterator.</symbol>
  137. * <symbol name="Iter_FreeInternal">Gets the first free slot in the iterator.</symbol>
  138. * <symbol name="Iter_InitInternal">Initialises a multi-dimensional iterator.</symbol>
  139. * </ul><subsection>
  140. * Inline
  141. * </subsection><ul>
  142. * <symbol name="Iter_Create">Create a new iterator value set.</symbol>
  143. * <symbol name="Iter_Add">Wraps Iter_AddInternal.</symbol>
  144. * <symbol name="Iter_Remove">Wraps Iter_RemoveInternal.</symbol>
  145. * <symbol name="Iter_Random">Wraps Iter_RandomInternal.</symbol>
  146. * <symbol name="Iter_Count">Gets the number of items in an iterator.</symbol>
  147. * <symbol name="Iter_Debug">Wraps around Iter_ShowArray.</symbol>
  148. * <symbol name="Iter_Free">Wraps around Iter_FreeInternal.</symbol>
  149. * <symbol name="Iter_Create2">Create a new iterator array value set.</symbol>
  150. * <symbol name="Iter_Add2">Wraps Iter_AddInternal for arrays.</symbol>
  151. * <symbol name="Iter_Remove2">Wraps Iter_RemoveInternal for arrays.</symbol>
  152. * <symbol name="Iter_Random2">Wraps Iter_RandomInternal for arrays.</symbol>
  153. * <symbol name="Iter_Count2">Gets the number of items in an iterator array.</symbol>
  154. * <symbol name="Iter_Debug2">Wraps around Iter_ShowArray for arrays.</symbol>
  155. * <symbol name="Iter_Free2">Wraps around Iter_FreeInternal for arrays.</symbol>
  156. * </ul><section>
  157. * Hooks
  158. * </section><ul>
  159. * <symbol name="Iter_OnPlayerConnect">Hook for the OnPlayerConnect callback.</symbol>
  160. * <symbol name="Iter_OnPlayerDisconnect">Hook for the OnPlayerDisconnect callback.</symbol>
  161. * <symbol name="Iter_OnGameModeInit">Only exists to make the code compile correctly...</symbol>
  162. * </ul><section>
  163. * Keywords
  164. * </section><ul>
  165. * <symbol name="foreach">Command to loop an iterator.</symbol>
  166. * <symbol name="foreachex">Like foreach but without a new variable.</symbol>
  167. * <symbol name="foreach2">Command to loop through an iterator array.</symbol>
  168. * <symbol name="foreachex">Like foreach2 but without a new variable.</symbol>
  169. * </ul><section>
  170. * Tags
  171. * </section><ul>
  172. * <symbol name="Iterator">Declare an iterator.</symbol>
  173. * </ul><section>
  174. * Variables
  175. * </section>
  176. * <subsection>
  177. * Static
  178. * </subsection><ul>
  179. * <symbol name="YSI_g_OPC">Records wether Iter_OnPlayerConnect exists for speed.</symbol>
  180. * <symbol name="YSI_g_OPDC">Records wether Iter_OnPlayerDisconnect exists for speed.</symbol>
  181. * </ul><section>Compile options</section><ul>
  182. * <symbol name="YSI_ITTER_NO_SORT">Removed.</symbol>
  183. * <symbol name="FOREACH_NO_BOTS">Remove the bot iterators for smaller code.</symbol>
  184. * <symbol name="FOREACH_NO_PLAYERS">Remove all default code for player itteration.</symbol>
  185. * </ul><section>
  186. * Iterators
  187. * </section><ul>
  188. * <symbol name="Player">List of all players connected.</symbol>
  189. * <symbol name="Bot">List of all bots (npcs) connected.</symbol>
  190. * <symbol name="NPC">Alias of Bot.</symbol>
  191. * <symbol name="Character">All players and bots.</symbol>
  192. * </ul><section>
  193. * Examples
  194. * </section>
  195. *
  196. *
  197. *
  198. *
  199. * <subsection>Basic Iterators</subsection>
  200. *
  201. * <p>
  202. * Basic iterators are simply collections of numbers - little more than an array.
  203. * A number is either in the array, or not in the array, <em>y_iterate</em> loops
  204. * through only the <em>in</em> numbers.</p>
  205. *
  206. * <p><em>Players</em><br /><br />
  207. *
  208. * This code will loop through every player connected to the server.
  209. *
  210. * <code>
  211. * foreach (new i : Player) <br />
  212. * { <br /><indent />
  213. * printf("player %d is connected", i); <br />
  214. * }
  215. * </code>
  216. * </p>
  217. *
  218. * <p><em>Vehicles</em><br /><br />
  219. *
  220. * This code will loop through all the created vehicles on the server (including
  221. * those made in other running scripts).
  222. *
  223. * <code>
  224. * foreach (new vid : Vehicle) <br />
  225. * { <br /><indent />
  226. * printf("vehicleid %d has been created", vid); <br />
  227. * }
  228. * </code>
  229. * </p>
  230. *
  231. * <p><em>Create An Iterator</em><br /><br />
  232. *
  233. * To create your own iterator, first declare it, then add things to it, then loop
  234. * over it:
  235. *
  236. * <code>
  237. * new <br /><indent />
  238. * Iterator:MyIter&lt;100&gt;; // First declare it (this has room for 100 items numbered 0-99). <br />
  239. * // Then add things to it. <br />
  240. * Iter_Add(MyIter, 0); // Fine. <br />
  241. * Iter_Add(MyIter, 55); // Fine. <br />
  242. * Iter_Add(MyIter, 100); // Will fail. <br />
  243. * // Then loop over it. <br />
  244. * foreach (new i : MyIter) <br />
  245. * { <br /><indent />
  246. * printf("%d", i); // Will print "0" then "55". <br />
  247. * }
  248. * </code>
  249. * </p>
  250. *
  251. * <subsection>Special Iterators</subsection>
  252. *
  253. * </library>
  254. *//** */