y_iterate.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. /*
  84. ad88888ba
  85. d8" "8b ,d
  86. Y8, 88
  87. `Y8aaaaa, ,adPPYba, MM88MMM 88 88 8b,dPPYba,
  88. `"""""8b, a8P_____88 88 88 88 88P' "8a
  89. `8b 8PP""""""" 88 88 88 88 d8
  90. Y8a a8P "8b, ,aa 88, "8a, ,a88 88b, ,a8"
  91. "Y88888P" `"Ybbd8"' "Y888 `"YbbdP'Y8 88`YbbdP"'
  92. 88
  93. 88
  94. */
  95. #include "..\YSI_Internal\y_compilerdata"
  96. // "y_iterate" is always higher than "foreach".
  97. #define _Y_ITERATE_LOCAL_VERSION 43
  98. #if __Pawn != 0x030A
  99. #define NO_CUSTOM_WARNINGS
  100. #endif
  101. #if !defined _inc_y_iterate
  102. // The stand-alone version looks for this symbol, even on the new compiler.
  103. #define _inc_y_iterate
  104. #endif
  105. // Extra macro so that those paranoid about accidentally including YSI can
  106. // ensure it never happens.
  107. #if defined NEVER_Y_ITERATE
  108. // Set `FOREACH_VERSION` as equal to whatever the version of the stand-alone
  109. // include is.
  110. #define FOREACH_VERSION _FOREACH_LOCAL_VERSION
  111. #undef _inc_y_iterate
  112. #undef _Y_ITERATE_LOCAL_VERSION
  113. #if defined _FOREACH_INC_TEST
  114. // This is `foreach` testing our version - nothing more need doing.
  115. #endinput
  116. #endif
  117. // Otherwise, include `foreach`. Don't use `#tryinclude`, since they want
  118. // a version of `foreach` and it would be an error to not give them one.
  119. #define _FOREACH_NO_TEST
  120. #include <foreach>
  121. #endinput
  122. #endif
  123. // Foreach is testing us.
  124. #if defined _FOREACH_INC_TEST
  125. #if !defined _Y_ITERATE_FOUND
  126. // Set up so that you can use `FOREACH_VERSION` even if the older
  127. // stand-alone version doesn't support it.
  128. #define FOREACH_VERSION _FOREACH_LOCAL_VERSION
  129. #define _FOREACH_CUR_VERSION _Y_ITERATE_LOCAL_VERSION
  130. #endif
  131. #endinput
  132. #endif
  133. #if !defined _FOREACH_NO_TEST
  134. #define _FOREACH_INC_TEST
  135. // Ignore the broken stand-alone version.
  136. #define _foreach_included
  137. #tryinclude <foreach>
  138. #undef _FOREACH_INC_TEST
  139. // <foreach> exists - test which is newer.
  140. #if defined _FOREACH_LOCAL_VERSION
  141. #if _FOREACH_LOCAL_VERSION > _Y_ITERATE_LOCAL_VERSION
  142. // Foreach is newer.
  143. #if defined _inc_foreach
  144. #undef _inc_foreach
  145. #endif
  146. #define _FOREACH_NO_TEST
  147. // Actually include the code.
  148. #include <foreach>
  149. // Mark the include as found and exit.
  150. #define FOREACH_VERSION _FOREACH_LOCAL_VERSION
  151. #define _FOREACH_INC_TEST
  152. #define _Y_ITERATE_FOUND
  153. #endinput
  154. #endif
  155. #elseif defined foreach && __COMPILER_FIRST_PASS
  156. // I made this a warning on compilers that supported them, but that's a
  157. // silly idea!
  158. #error The old <foreach> include is no longer compatible with YSI.
  159. #endif
  160. #endif
  161. /**
  162. * <library name="y_iterate">
  163. * <section>
  164. * Description
  165. * </section>
  166. * Provides efficient looping through sparse data sets, such as connected
  167. * players. Significantly improved from the original version to be a generic
  168. * loop system, rather then purely a player loop system. When used for
  169. * players this has constant time O(n) for number of connected players (n),
  170. * unlike standard player loops which are O(MAX_PLAYERS), regardless of the
  171. * actual number of connected players. Even when n is MAX_PLAYERS this is
  172. * still faster.
  173. *
  174. * For extensive documentation on writing and using iterators, see this topic:
  175. *
  176. * <a href="http://forum.sa-mp.com/showthread.php?t=481877" />
  177. *
  178. * <section>
  179. * Version
  180. * </section>
  181. * 0.4
  182. * <section>
  183. * Functions
  184. * </section>
  185. * <subsection>
  186. * Public
  187. * </subsection><ul>
  188. * <symbol name="OnPlayerDisconnect">Called when a player leaves to remove them.</symbol>
  189. * <symbol name="OnPlayerConnect">Called when a player connects to add them.</symbol>
  190. * </ul><subsection>
  191. * Stock
  192. * </subsection><ul>
  193. * <symbol name="Iter_ShowArray">Displays the contents of the array.</symbol>
  194. * <symbol name="Iter_AddInternal">Add a value to an iterator.</symbol>
  195. * <symbol name="Iter_RemoveInternal">Remove a value from an iterator.</symbol>
  196. * <symbol name="Iter_RandomInternal">Get a random item from an iterator.</symbol>
  197. * <symbol name="Iter_FreeInternal">Gets the first free slot in the iterator.</symbol>
  198. * <symbol name="Iter_InitInternal">Initialises a multi-dimensional iterator.</symbol>
  199. * </ul><subsection>
  200. * Inline
  201. * </subsection><ul>
  202. * <symbol name="Iter_Create">Create a new iterator value set.</symbol>
  203. * <symbol name="Iter_Add">Wraps Iter_AddInternal.</symbol>
  204. * <symbol name="Iter_Remove">Wraps Iter_RemoveInternal.</symbol>
  205. * <symbol name="Iter_Random">Wraps Iter_RandomInternal.</symbol>
  206. * <symbol name="Iter_Count">Gets the number of items in an iterator.</symbol>
  207. * <symbol name="Iter_Debug">Wraps around Iter_ShowArray.</symbol>
  208. * <symbol name="Iter_Free">Wraps around Iter_FreeInternal.</symbol>
  209. * <symbol name="Iter_Create2">Create a new iterator array value set.</symbol>
  210. * <symbol name="Iter_Add2">Wraps Iter_AddInternal for arrays.</symbol>
  211. * <symbol name="Iter_Remove2">Wraps Iter_RemoveInternal for arrays.</symbol>
  212. * <symbol name="Iter_Random2">Wraps Iter_RandomInternal for arrays.</symbol>
  213. * <symbol name="Iter_Count2">Gets the number of items in an iterator array.</symbol>
  214. * <symbol name="Iter_Debug2">Wraps around Iter_ShowArray for arrays.</symbol>
  215. * <symbol name="Iter_Free2">Wraps around Iter_FreeInternal for arrays.</symbol>
  216. * </ul><section>
  217. * Hooks
  218. * </section><ul>
  219. * <symbol name="Iter_OnPlayerConnect">Hook for the OnPlayerConnect callback.</symbol>
  220. * <symbol name="Iter_OnPlayerDisconnect">Hook for the OnPlayerDisconnect callback.</symbol>
  221. * <symbol name="Iter_OnGameModeInit">Only exists to make the code compile correctly...</symbol>
  222. * </ul><section>
  223. * Keywords
  224. * </section><ul>
  225. * <symbol name="foreach">Command to loop an iterator.</symbol>
  226. * <symbol name="foreachex">Like foreach but without a new variable.</symbol>
  227. * <symbol name="foreach2">Command to loop through an iterator array.</symbol>
  228. * <symbol name="foreachex">Like foreach2 but without a new variable.</symbol>
  229. * </ul><section>
  230. * Tags
  231. * </section><ul>
  232. * <symbol name="Iterator">Declare an iterator.</symbol>
  233. * </ul><section>
  234. * Variables
  235. * </section>
  236. * <subsection>
  237. * Static
  238. * </subsection><ul>
  239. * <symbol name="YSI_g_OPC">Records wether Iter_OnPlayerConnect exists for speed.</symbol>
  240. * <symbol name="YSI_g_OPDC">Records wether Iter_OnPlayerDisconnect exists for speed.</symbol>
  241. * </ul><section>Compile options</section><ul>
  242. * <symbol name="YSI_ITTER_NO_SORT">Removed.</symbol>
  243. * <symbol name="FOREACH_NO_BOTS">Remove the bot iterators for smaller code.</symbol>
  244. * <symbol name="FOREACH_NO_PLAYERS">Remove all default code for player itteration.</symbol>
  245. * </ul><section>
  246. * Iterators
  247. * </section><ul>
  248. * <symbol name="Player">List of all players connected.</symbol>
  249. * <symbol name="Bot">List of all bots (npcs) connected.</symbol>
  250. * <symbol name="NPC">Alias of Bot.</symbol>
  251. * <symbol name="Character">All players and bots.</symbol>
  252. * </ul><section>
  253. * Examples
  254. * </section>
  255. *
  256. *
  257. *
  258. *
  259. * <subsection>Basic Iterators</subsection>
  260. *
  261. * <p>
  262. * Basic iterators are simply collections of numbers - little more than an array.
  263. * A number is either in the array, or not in the array, <em>y_iterate</em> loops
  264. * through only the <em>in</em> numbers.</p>
  265. *
  266. * <p><em>Players</em><br /><br />
  267. *
  268. * This code will loop through every player connected to the server.
  269. *
  270. * <code>
  271. * foreach (new i : Player) <br />
  272. * { <br /><indent />
  273. * printf("player %d is connected", i); <br />
  274. * }
  275. * </code>
  276. * </p>
  277. *
  278. * <p><em>Vehicles</em><br /><br />
  279. *
  280. * This code will loop through all the created vehicles on the server (including
  281. * those made in other running scripts).
  282. *
  283. * <code>
  284. * foreach (new vid : Vehicle) <br />
  285. * { <br /><indent />
  286. * printf("vehicleid %d has been created", vid); <br />
  287. * }
  288. * </code>
  289. * </p>
  290. *
  291. * <p><em>Create An Iterator</em><br /><br />
  292. *
  293. * To create your own iterator, first declare it, then add things to it, then loop
  294. * over it:
  295. *
  296. * <code>
  297. * new <br /><indent />
  298. * Iterator:MyIter&lt;100&gt;; // First declare it (this has room for 100 items numbered 0-99). <br />
  299. * // Then add things to it. <br />
  300. * Iter_Add(MyIter, 0); // Fine. <br />
  301. * Iter_Add(MyIter, 55); // Fine. <br />
  302. * Iter_Add(MyIter, 100); // Will fail. <br />
  303. * // Then loop over it. <br />
  304. * foreach (new i : MyIter) <br />
  305. * { <br /><indent />
  306. * printf("%d", i); // Will print "0" then "55". <br />
  307. * }
  308. * </code>
  309. * </p>
  310. *
  311. * <subsection>Special Iterators</subsection>
  312. *
  313. * </library>
  314. *//** */
  315. // Set `FOREACH_VERSION` as this file's version.
  316. #if defined FOREACH_VERSION
  317. #undef FOREACH_VERSION
  318. #endif
  319. #define FOREACH_VERSION _Y_ITERATE_LOCAL_VERSION
  320. #define _FOREACH_INC_TEST
  321. #define _Y_ITERATE_FOUND
  322. // Remove `_FOREACH_CUR_VERSION` incase someone includes an older version of
  323. // `foreach` later on that doesn't check if this symbol is already defined.
  324. #if defined _FOREACH_CUR_VERSION
  325. #undef _FOREACH_CUR_VERSION
  326. #endif
  327. #if !defined _samp_included
  328. #error "Please include a_samp or a_npc before foreach"
  329. #endif
  330. #if defined _YSI_SPECIAL_DEBUG
  331. #define PS_IS_PLAYER_CONNECTED(%0) (%0 != INVALID_PLAYER_ID)
  332. #else
  333. #define PS_IS_PLAYER_CONNECTED IsPlayerConnected
  334. #endif
  335. #define INVALID_ITERATOR_SLOT (cellmin)
  336. #define ITER_NONE (cellmin)
  337. //
  338. // _FOREACH_BOTS
  339. //
  340. // Should the "NPC", "Bot", and "Character" iterators be included by default?
  341. // Disabled by declaring "FOREACH_NO_BOTS".
  342. //
  343. #define _FOREACH_BOTS 0
  344. #if defined IsPlayerNPC
  345. #define _FOREACH_BOT
  346. #if !defined FOREACH_NO_BOTS
  347. #undef _FOREACH_BOTS
  348. #define _FOREACH_BOTS 1
  349. #endif
  350. #endif
  351. //
  352. // _FOREACH_LOCALS
  353. //
  354. // Should the "LocalActor" and "LocalVehicle" iterators be included? These only
  355. // loop through ones created by the current script, instead of through ones
  356. // created in any script.
  357. //
  358. #define _FOREACH_LOCALS 1
  359. #if defined SendChat || defined FOREACH_NO_LOCALS
  360. #undef _FOREACH_LOCALS
  361. #define _FOREACH_LOCALS 0
  362. #endif
  363. //
  364. // _FOREACH_VEHICLES
  365. //
  366. // Should the "Vehicle" iterator be included? "Vehicle" loops over all vehicles
  367. // created on the server, "LocalVehicle" iterates over vehicles created only in
  368. // the current script. They are the same when "YSI_NO_MASTER" is declared.
  369. // Disabled by declaring "FOREACH_NO_VEHICLES".
  370. //
  371. #define _FOREACH_VEHICLES 1
  372. #if defined SendChat || defined FOREACH_NO_VEHICLES
  373. #undef _FOREACH_VEHICLES
  374. #define _FOREACH_VEHICLES 0
  375. #endif
  376. //
  377. // _FOREACH_ACTORS
  378. //
  379. // Should the "Actor" iterator be included? "Actor" loops over all actors
  380. // created on the server, "LocalActor" iterates over actors created only in the
  381. // current script. They are the same when "YSI_NO_MASTER" is declared.
  382. // Disabled by declaring "FOREACH_NO_ACTORS".
  383. //
  384. #define _FOREACH_ACTORS 0
  385. #if defined GetActorPos
  386. #if !defined FOREACH_NO_ACTORS
  387. #undef _FOREACH_ACTORS
  388. #define _FOREACH_ACTORS 1
  389. #endif
  390. #endif
  391. //
  392. // _FOREACH_PLAYERS
  393. //
  394. // Should the "Player" iterator be included? If "_FOREACH_BOTS" is set, the
  395. // "Player" iterator only loops over human players, "NPC" and "Bot" loop over
  396. // computer players, and "Character" loops over them all. If "_FOREACH_BOTS" is
  397. // not set, then the "Player" iterator loops over every player in the server,
  398. // the same as "Character" would do otherwise, since it has no way to know if a
  399. // player is human or not. Disabled by declaring "FOREACH_NO_PLAYERS".
  400. //
  401. #define _FOREACH_PLAYERS 1
  402. #if defined SendChat || defined FOREACH_NO_PLAYERS
  403. #undef _FOREACH_PLAYERS
  404. #define _FOREACH_PLAYERS 0
  405. #endif
  406. //
  407. // _FOREACH_CHARACTERS
  408. //
  409. // Bot or Player iterators included.
  410. //
  411. #define _FOREACH_CHARACTERS (_FOREACH_BOTS || _FOREACH_PLAYERS)
  412. #if !defined BOTSYNC_IS_BOT
  413. forward Iter_OPDCInternal(playerid);
  414. #endif
  415. #include "y_foreach/macros"
  416. #include "..\YSI_Internal\y_natives"
  417. #include "..\YSI_Internal\y_shortfunc"
  418. #include "..\YSI_Internal\y_funcinc"
  419. #include "..\YSI_Core\y_debug"
  420. //#include "..\YSI_Coding\y_malloc"
  421. #include "y_foreach/impl"
  422. #include "..\YSI_Coding\y_hooks"
  423. #include "y_foreach/yield"
  424. #if _FOREACH_CHARACTERS || _FOREACH_VEHICLES || _FOREACH_ACTORS
  425. #include "..\YSI_Coding\y_hooks"
  426. #endif
  427. #if _FOREACH_VEHICLES || _FOREACH_ACTORS
  428. #include "..\YSI_Coding\y_remote"
  429. #endif
  430. #include "y_foreach/iterators"
  431. #if defined YSI_TESTS
  432. #include "..\YSI_Core\y_testing"
  433. #include "y_foreach/tests"
  434. #endif