impl.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*----------------------------------------------------------------------------*\
  2. ====================================
  3. y_timers - Run timers efficiently.
  4. ====================================
  5. Description:
  6. Sets up repeating timers without requiring any SetTimers and arranges them
  7. so that they will be very unlikely to meet (at least for a long time) using
  8. scheduling algorithms to get timers with the same period to be offset. Also
  9. fixes arrays and strings in timers so they can be passed properly.
  10. Legal:
  11. Version: MPL 1.1
  12. The contents of this file are subject to the Mozilla Public License Version
  13. 1.1 (the "License"); you may not use this file except in compliance with
  14. the License. You may obtain a copy of the License at
  15. http://www.mozilla.org/MPL/
  16. Software distributed under the License is distributed on an "AS IS" basis,
  17. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  18. for the specific language governing rights and limitations under the
  19. License.
  20. The Original Code is the YSI timers include.
  21. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  22. Portions created by the Initial Developer are Copyright (C) 2011
  23. the Initial Developer. All Rights Reserved.
  24. Contributors:
  25. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  26. Thanks:
  27. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  28. ZeeX - Very productive conversations.
  29. koolk - IsPlayerinAreaEx code.
  30. TheAlpha - Danish translation.
  31. breadfish - German translation.
  32. Fireburn - Dutch translation.
  33. yom - French translation.
  34. 50p - Polish translation.
  35. Zamaroht - Spanish translation.
  36. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  37. for me to strive to better.
  38. Pixels^ - Running XScripters where the idea was born.
  39. Matite - Pestering me to release it and using it.
  40. Very special thanks to:
  41. Thiadmer - PAWN, whose limits continue to amaze me!
  42. Kye/Kalcor - SA:MP.
  43. SA:MP Team past, present and future - SA:MP.
  44. Version:
  45. 2.0
  46. Changelog:
  47. 29/04/11:
  48. Added version 2 of the code with more advanced options.
  49. 21/03/11:
  50. Added debug printing to timer functions. Uses "P:C" in compiling.
  51. 26/10/10:
  52. Officially added simple calling.
  53. Added "delay" functions.
  54. 12/10/10:
  55. Rewrote for YSI 1.0 using y_scripting.
  56. 11/08/07:
  57. Removed millions of defines to reduce pre-processing.
  58. Added pickups.
  59. 03/08/07:
  60. First version.
  61. \*----------------------------------------------------------------------------*/
  62. // Disable this version!
  63. #include "internal\y_version"
  64. #include "y_amx"
  65. #include "internal\y_shortfunc"
  66. #include "y_malloc"
  67. #include "y_iterate"
  68. #include "y_hooks"
  69. #include "y_debug"
  70. static stock
  71. Alloc:YSI_g_sLastSlot = NO_ALLOC,
  72. Alloc:YSI_g_sFirstSlot = NO_ALLOC,
  73. YSI_g_sPlayerTimers = -1;
  74. hook OnScriptInit()
  75. {
  76. P:1("hook Timers_OnScriptInit called");
  77. new
  78. pointer,
  79. time,
  80. idx,
  81. entry;
  82. while ((idx = AMX_GetPublicEntrySuffix(idx, entry, _A<@yT_>)))
  83. //while ((idx = AMX_GetPublicPointerSuffix(idx, pointer, _A<@yT_>)))
  84. {
  85. P:6("Timer_OnScriptInit: entry: %d", entry);
  86. #emit LREF.S.pri entry
  87. #emit STOR.S.pri pointer
  88. //YSI_g_sCurFunc = pointer;
  89. // Don't bother with the real name, call the function by address to get
  90. // the time the function runs for.
  91. P:7("Timer_OnScriptInit: pointer: %d", pointer);
  92. // Push the address of the current function.
  93. #emit PUSH.S pointer
  94. #emit PUSH.C 0xFFFFFFFF
  95. #emit PUSH.C 8
  96. #emit LCTRL 6
  97. #emit ADD.C 28
  98. #emit PUSH.pri
  99. #emit LOAD.S.pri pointer
  100. #emit SCTRL 6
  101. #emit STOR.S.pri time
  102. //YSI_g_sCurFunc = 0;
  103. P:7("Timer_OnScriptInit: time: %d", time);
  104. if (time != -1)
  105. {
  106. // Find all the functions with the same time. This is less
  107. // efficient than previous implementations (it is O(N^2)), but also
  108. // more robust as it won't fail no matter how many different times
  109. // there are - old ones relied on an array with a finite size.
  110. new
  111. pointer2,
  112. time2,
  113. idx2,
  114. total,
  115. pre;
  116. while ((idx2 = AMX_GetPublicPointerSuffix(idx2, pointer2, _A<@yT_>)))
  117. {
  118. // Call the functions a second time to guarantee getting
  119. #emit PUSH.C 0
  120. #emit PUSH.C 0xFFFFFFFF
  121. #emit PUSH.C 8
  122. #emit LCTRL 6
  123. #emit ADD.C 28
  124. #emit PUSH.pri
  125. #emit LOAD.S.pri pointer2
  126. #emit SCTRL 6
  127. #emit STOR.S.pri time2
  128. // Check if the new time is a FACTOR, SAME, or MULTIPLE of this
  129. // task, so we don't start different timers together.
  130. if (time2 == time || time / time2 * time2 == time || time2 / time * time == time2)
  131. {
  132. ++total;
  133. if (idx2 < idx)
  134. {
  135. ++pre;
  136. }
  137. }
  138. }
  139. P:7("Timer_OnScriptInit: total: %d, time: %d, pre: %d", total, time, pre);
  140. // Now we know what time this function has, how many others have
  141. // that time and how many have already been started.
  142. new
  143. buffer[32];
  144. entry += 4;
  145. #emit LREF.S.pri entry
  146. #emit STOR.S.pri pointer
  147. AMX_ReadString(AMX_BASE_ADDRESS + pointer, buffer);
  148. P:7("Timer_OnScriptInit: %s", unpack(buffer));
  149. // Get the time offset for the current call. This should mean that
  150. // all the functions are nicely spread out.
  151. SetTimerEx(buffer, time * pre / total, 0, "ii", 1, -1);
  152. }
  153. }
  154. P:1("hook Timers_OnScriptInit ended");
  155. }
  156. hook OnPlayerConnect(playerid)
  157. {
  158. P:1("hook Timers_OnPlayerConnect called: %d", playerid);
  159. // Loop through all the per-player timers. Correctly finds them all from a
  160. // linked list hidden in static variables (which are really global).
  161. new
  162. cur = YSI_g_sPlayerTimers,
  163. data;
  164. while (cur != -1)
  165. {
  166. #emit LREF.S.pri cur
  167. #emit STOR.S.pri data
  168. P:6("Timers_OnPlayerConnect: func: %x", data);
  169. // Start this timer for this player.
  170. #emit PUSH.S playerid
  171. #emit PUSH.C 1
  172. // Push the parameter count (in bytes). This is actually passed to
  173. // native functions directly.
  174. #emit PUSH.C 8
  175. // Call the function currently in the list to trigger the repeating
  176. // timer. This involves getting the current "cip" address, modifying it
  177. // to get the return address then modifying "cip" to call the function.
  178. #emit LCTRL 6
  179. #emit ADD.C 28
  180. #emit PUSH.pri
  181. #emit LOAD.S.pri data
  182. #emit SCTRL 6
  183. // Returned, get the next list element.
  184. cur += 4;
  185. #emit LREF.S.pri cur
  186. #emit STOR.S.pri cur
  187. }
  188. P:1("hook Timers_OnPlayerConnect ended");
  189. }
  190. hook OnPlayerDisconnect(playerid, reason)
  191. {
  192. P:1("hook Timers_OnPlayerDisconnect called: %d, %d, playerid, reason");
  193. // Loop through all the per-player timers. Correctly finds them all from a
  194. // linked list hidden in static variables (which are really global).
  195. new
  196. cur = YSI_g_sPlayerTimers,
  197. data;
  198. while (cur != -1)
  199. {
  200. #emit LREF.S.pri cur
  201. #emit STOR.S.pri data
  202. P:6("Timers_OnPlayerDisconnect: func: %x", data);
  203. // End this timer for this player.
  204. #emit PUSH.S playerid
  205. #emit PUSH.C 0
  206. // Push the parameter count (in bytes). This is actually passed to
  207. // native functions directly.
  208. #emit PUSH.C 8
  209. // Call the function currently in the list to trigger the repeating
  210. // timer. This involves getting the current "cip" address, modifying it
  211. // to get the return address then modifying "cip" to call the function.
  212. #emit LCTRL 6
  213. #emit ADD.C 28
  214. #emit PUSH.pri
  215. #emit LOAD.S.pri data
  216. #emit SCTRL 6
  217. // Returned, get the next list element.
  218. cur += 4;
  219. #emit LREF.S.pri cur
  220. #emit STOR.S.pri cur
  221. }
  222. P:1("hook Timers_OnPlayerDisconnect ended");
  223. }
  224. stock _Timer_I(func[], interval, action, &result)
  225. {
  226. P:3("_Timer_I called");
  227. switch (action)
  228. {
  229. case 0:
  230. {
  231. if (result != -1)
  232. {
  233. KillTimer(result),
  234. result =- 1;
  235. }
  236. }
  237. case 1:
  238. {
  239. if (result == -1)
  240. {
  241. result = SetTimer(func, interval, 1);
  242. }
  243. }
  244. }
  245. return interval;
  246. }
  247. // Attempt to stop or start a task, possibly for a single player.
  248. stock _Timer_D(func[], interval, const action, who, results[MAX_PLAYERS], &pp, a[2])
  249. {
  250. P:3("_Timer_D called");
  251. switch (action)
  252. {
  253. case -1:
  254. {
  255. if (who)
  256. {
  257. a[0] = who;
  258. a[1] = YSI_g_sPlayerTimers;
  259. // Store the address of the global array.
  260. #emit LOAD.S.pri a
  261. #emit STOR.pri YSI_g_sPlayerTimers
  262. }
  263. }
  264. case 0:
  265. {
  266. // Stop the timer.
  267. if (who == -1)
  268. {
  269. pp = 0;
  270. }
  271. else if (results[who] != -1)
  272. {
  273. KillTimer(results[who]);
  274. results[who] = -1;
  275. }
  276. }
  277. case 1:
  278. {
  279. // Start the timer.
  280. if (who == -1)
  281. {
  282. pp = 1;
  283. }
  284. else if (results[who] == -1)
  285. {
  286. results[who] = SetTimerEx(func, interval, true, "i", who);
  287. }
  288. }
  289. }
  290. // No global interval for per-player timers.
  291. return -1;
  292. }
  293. static stock Alloc:Timer_GetSingleSlot(len)
  294. {
  295. // Allocates memory and secretly appends data to the start.
  296. P:4("Timer_GetSingleSlot called: %d", len);
  297. new
  298. Alloc:slot = malloc(len + 1);
  299. if (slot == NO_ALLOC)
  300. {
  301. return NO_ALLOC;
  302. }
  303. P:5("Timer_GetSingleSlot: %d, %d, %d", _:YSI_g_sFirstSlot, _:YSI_g_sLastSlot, _:slot);
  304. // Standard linked list.
  305. if (YSI_g_sFirstSlot == NO_ALLOC)
  306. {
  307. YSI_g_sFirstSlot = slot;
  308. }
  309. else
  310. {
  311. mset(YSI_g_sLastSlot, 0, _:slot);
  312. }
  313. YSI_g_sLastSlot = slot;
  314. mset(YSI_g_sLastSlot, 0, -1);
  315. return slot;// + Alloc:1;
  316. }
  317. // Allocate memory to store a string.
  318. stock _Timer_S(string:str[])
  319. {
  320. P:3("_Timer_S called");
  321. new
  322. len = strlen(str);
  323. if (len & 0x0F)
  324. {
  325. len = (len & ~0x0F) + 32;
  326. }
  327. new
  328. Alloc:slot = Timer_GetSingleSlot(len + 1);
  329. if (slot != NO_ALLOC)
  330. {
  331. msets(slot, 1, str);
  332. }
  333. P:5("str: %d", _:slot);
  334. return _:slot + 1;
  335. }
  336. // Allocate memory to store an array.
  337. stock _Timer_A(str[], len)
  338. {
  339. P:3("_Timer_A called");
  340. new
  341. Alloc:slot = Timer_GetSingleSlot(len);
  342. if (slot != NO_ALLOC)
  343. {
  344. mseta(slot, 1, str, len);
  345. }
  346. P:5("str: %d", _:slot);
  347. return _:slot + 1;
  348. }
  349. //stock
  350. // I@ = -1;
  351. // Create the timer setup.
  352. stock _Timer_C(tt, g)
  353. {
  354. P:3("_Timer_C called: %d, %d", tt, g);
  355. //P:3("_Timer_C called: %d", tt);
  356. // This is done here for convenience.
  357. I@ = -1;
  358. // Only repeating timers are freed like this.
  359. // UPDATE: Now all timers with array parameters, regardless of repeat status
  360. // are freed like this. Only timers with no malloc aren't.
  361. if (g)
  362. {
  363. new
  364. Alloc:slot = Timer_GetSingleSlot(1);
  365. P:5("_Timer_C: slot = %d", _:slot);
  366. if (slot == NO_ALLOC)
  367. {
  368. // Not a graceful fail!
  369. return 0;
  370. }
  371. mset(slot, 1, tt);
  372. // Just so it isn't a real timer ID (or possibly isn't).
  373. slot = ~YSI_g_sFirstSlot;// ^ Alloc:-1;
  374. YSI_g_sFirstSlot = NO_ALLOC;
  375. YSI_g_sLastSlot = NO_ALLOC;
  376. return _:slot;
  377. }
  378. // Reset these variables on all timers, including self-cleaning ones.
  379. YSI_g_sFirstSlot = NO_ALLOC;
  380. YSI_g_sLastSlot = NO_ALLOC;
  381. return tt;
  382. }
  383. // Free all timer resources.
  384. stock _Timer_F(slot)
  385. {
  386. P:3("_Timer_F called");
  387. // This is done here for convenience.
  388. if (slot & 0x80000000)
  389. {
  390. new
  391. next;
  392. slot = ~slot; //^= -1;
  393. for ( ; ; )
  394. {
  395. next = mget(Alloc:slot, 0);
  396. P:6("_Timer_F: slot = %d, next = %d", slot, next);
  397. // Out of stored strings and arrays.
  398. if (next == -1)
  399. {
  400. KillTimer(mget(Alloc:slot, 1));
  401. free(Alloc:slot);
  402. break;
  403. }
  404. free(Alloc:slot);
  405. slot = next;
  406. }
  407. }
  408. else
  409. {
  410. KillTimer(slot);
  411. }
  412. return 1;
  413. }
  414. stock _Timer_H(slot)
  415. {
  416. _Timer_F(~(slot - 1));
  417. }
  418. #define task%0[%1](%2) %0@yT_(g,p);public%0@yT_(g,p){static s=-1;return _Timer_I(#%0,%1,g,s);}%0();public%0()
  419. #define ptask%0[%1](%2) stock %0_yT@=1;%0@yT_(g,p);public%0@yT_(g,p){static s[MAX_PLAYERS]={-1,...},a[2];return _Timer_D(#%0@_yT,%1,g,p,s,%0_yT@,a);}%0@_yT(p);public%0@_yT(p)if(%0_yT@)%0(p);%0(%2)
  420. #define pause%0; {J@=_:@Ym:%0@yT_(0,-1);}
  421. #define resume%0; {J@=_:@Ym:%0@yT_(1,-1);}
  422. #define @Ym:%0[%1]@yT_(%2,-1) %0@yT_(%2,%1)
  423. #define timerfunc YSI_timer
  424. #if !defined YSI_NO_timer
  425. #define timer YSI_timer
  426. #endif