y_iterate - experimental.inc 27 KB

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