Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 KB | None | 0 0
  1. (*
  2. Time
  3. ====
  4.  
  5. The time file includes any functions that have anything to do with time: timers,
  6. the time of day, conversions, etc.
  7.  
  8. The source for this file can be found `here <https://github.com/SRL/SRL-6/blob/master/lib/utilities/time.simba>`_.
  9.  
  10. *)
  11.  
  12. {$f-}
  13.  
  14. (*
  15. const Date/Time
  16. ~~~~~~~~~~~~~~~
  17.  
  18. Integer constants that represent the different time formats that can be used
  19. to display the time and dates in a string.
  20.  
  21. Example:
  22.  
  23. .. code-block:: pascal
  24.  
  25. writeln(500000, TIME_SHORT);
  26.  
  27. *)
  28. const
  29. TIME_FORMAL = 0;
  30. TIME_SHORT = 1;
  31. TIME_ABBREV = 2;
  32. TIME_BARE = 3;
  33. TIME_FSTOP = 4;
  34. TIME_FORMAL_LONG = 5;
  35.  
  36. DATE_FORMAL = 1;
  37. DATE_MONTH = 2;
  38. DATE_DAY = 3;
  39.  
  40. (*
  41. msToTime
  42. ~~~~~~~~
  43.  
  44. .. code-block:: pascal
  45.  
  46. function msToTime(MS, StrType: Integer): string;
  47.  
  48. Takes MS in milliseconds and outputs a string with hours, mins and
  49. seconds. Different styles can be created with different StrType values:
  50. Str Type:
  51.  
  52. - TIME_FORMAL: 2 Hours, 47 Minutes and 28 Seconds
  53. - TIME_SHORT: 02h, 47m, 28s
  54. - TIME_ABBREV: 2 hr, 47 min, 28 sec
  55. - TIME_BARE: 02:47:28
  56. - TIME_FSTOP: 12.04.40
  57. - TIME_FORMAL_LONG: 1 Years, 3 Months, 2 Weeks, 4 Days, 13 Hours, 3 Minutes, and 20 Seconds
  58.  
  59. .. note::
  60.  
  61. - by Zephyrsfury, Nava2 and Rasta Magician.
  62. - Last updated: 4/12/2013 by Ashaman88
  63.  
  64. Example:
  65.  
  66. .. code-block:: pascal
  67.  
  68. writeln(500000, TIME_SHORT);
  69.  
  70. *)
  71. function msToTime(MS, TheType: Integer): string;
  72. var
  73. STA: array [0..5] of TVariantArray;
  74. Time: array of Integer;
  75. i, t, tl: Integer;
  76. begin
  77. Result := '';
  78.  
  79. if (not(InRange(TheType, 0, High(STA)))) then
  80. TheType := TIME_BARE;
  81.  
  82. if TheType = TIME_FORMAL_LONG then
  83. begin
  84. tl := 7;
  85. t := 6;
  86. SetLength(Time,tl);
  87. ConvertTime64(MS, Time[0], Time[1], Time[2], Time[3], Time[4], Time[5], Time[6]);
  88. end else
  89. begin
  90. tl := 3;
  91. t := 2;
  92. SetLength(Time,tl);
  93. ConvertTime(MS, Time[0], Time[1], Time[2]);
  94. end;
  95.  
  96. STA[TIME_FORMAL] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
  97. STA[TIME_SHORT] := ['h ', 'm ', 's', False, 2];
  98. STA[TIME_ABBREV] := [' hr ', ' min ', ' sec', False, 0];
  99. STA[TIME_BARE] := [':', ':', '', True, 2];
  100. STA[TIME_FSTOP] := ['.', '.', '', True, 2];
  101. STA[TIME_FORMAL_LONG] := [' Years, ', ' Months, ', ' Weeks, ', ' Days, ', ' Hours, ', ' Minutes and ', ' Seconds', False, 0];
  102.  
  103. case theType of
  104. TIME_FORMAL:
  105. begin
  106. if time[0]= 1 then
  107. sta[TIME_FORMAL][0]:=' Hour, ';
  108.  
  109. if time[1]= 1 then
  110. sta[TIME_FORMAL][1]:=' Minute and ';
  111.  
  112. if time[2]= 1 then
  113. sta[TIME_FORMAL][2]:=' Second';
  114. end;
  115. TIME_FORMAL_LONG:
  116. begin
  117. if time[0]= 1 then
  118. sta[TIME_FORMAL_LONG][0]:=' Year, ';
  119.  
  120. if time[1]= 1 then
  121. sta[TIME_FORMAL_LONG][1]:=' Month, ';
  122.  
  123. if time[2]= 1 then
  124. sta[TIME_FORMAL_LONG][2]:=' Week, ';
  125.  
  126. if time[3]= 1 then
  127. sta[TIME_FORMAL_LONG][3]:=' Day, ';
  128.  
  129. if time[4]= 1 then
  130. sta[TIME_FORMAL_LONG][4]:=' Hour, ';
  131.  
  132. if time[5]= 1 then
  133. sta[TIME_FORMAL_LONG][5]:=' Minute and ';
  134.  
  135. if time[6]= 1 then
  136. sta[TIME_FORMAL_LONG][6]:=' Second';
  137. end;
  138. end;
  139.  
  140.  
  141. for i := 0 to t do
  142. if (Time[i] > 0) or (STA[TheType][tl]) or (i = t) then
  143. Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][tl+1]) + STA[TheType][i];
  144. end;
  145.  
  146. (*
  147. timeRunning
  148. ~~~~~~~~~~~
  149.  
  150. .. code-block:: pascal
  151.  
  152. function timeRunning: String;
  153.  
  154. Returns Time since the script was started (GetTimeRunning).
  155.  
  156. .. note::
  157.  
  158. - by Rasta Magician.
  159. - Last updated: 4/12/2013 by Ashaman88
  160.  
  161. Example:
  162.  
  163. .. code-block:: pascal
  164.  
  165. writeln('Script time: '+timeRunning);
  166.  
  167. *)
  168. function timeRunning(TheType: Integer = TIME_FORMAL_LONG): string;
  169. begin
  170. Result := msToTime(GetTimeRunning(), TheType);
  171. end;
  172.  
  173. (*
  174. theTime
  175. ~~~~~~~~
  176.  
  177. .. code-block:: pascal
  178.  
  179. function theTime: string;
  180.  
  181. Returns current time as a string.
  182.  
  183. .. note::
  184.  
  185. - by RsN (fixed by Ron and Markus)
  186.  
  187. Example:
  188.  
  189. .. code-block:: pascal
  190.  
  191. writeln(theTime);
  192.  
  193. *)
  194. function theTime: string;
  195. var
  196. Hour, Mins, Sec, MSec: Word;
  197. PAM: string;
  198. begin
  199. DecodeTime(Now(), Hour, Mins, Sec, MSec);
  200. PAM := 'AM';
  201. if (Hour > 12) then
  202. begin
  203. Hour := Hour - 12;
  204. PAM := 'PM';
  205. end else if (Hour = 12) then
  206. PAM := 'PM'
  207. else if (Hour = 0) then
  208. Hour := 12;
  209. Result := (Padz(IntToStr(Hour), 2) + ':' + Padz(IntToStr(Mins), 2) + ':' + Padz(IntToStr(Sec), 2) + ' ' + PAM);
  210. end;
  211.  
  212. (*
  213. theDate
  214. ~~~~~~~
  215.  
  216. .. code-block:: pascal
  217.  
  218. function theDate(DateFormat: Integer): String;
  219.  
  220. Returns the current date. DateFormats can be:
  221.  
  222. - DATE_FORMAL = April 2nd, 2007 Month Day, Year
  223. - DATE_MONTH = 04/02/07 Month/Day/Year
  224. - DATE_DAY = 02-04-07 Day-Month-Year
  225.  
  226. .. note::
  227.  
  228. - by Ron, Nava2 & Narcle
  229.  
  230. Example:
  231.  
  232. .. code-block:: pascal
  233.  
  234. writeln(theDate);
  235.  
  236. *)
  237. function theDate(DateFormat: Integer): string;
  238. var
  239. Year, Month, Day: Word;
  240. D: string;
  241. Mnths, sfx: TStringArray;
  242. begin
  243. DecodeDate(Date(), Year, Month, Day);
  244. Mnths := ['January', 'February', 'March', 'April', 'May', 'June', 'July',
  245. 'August', 'September', 'October', 'November', 'December'];
  246. sfx := ['st', 'nd', 'rd'];
  247.  
  248. case DateFormat of
  249. DATE_FORMAL:
  250. begin
  251. if InRange(Day mod 10, 1, 3) and not InRange(Day, 11, 13) then
  252. D := sfx[(Day mod 10)-1]
  253. else
  254. D := 'th';
  255. Result := Mnths[Month - 1] + ' ' + IntToStr(Day) + D +', ' + IntToStr(Year);
  256. end;
  257.  
  258. DATE_MONTH:
  259. Result := Padz(IntToStr(Month), 2) + '/' + Padz(IntToStr(Day), 2) + '/' + Copy(IntToStr(Year), 3, 4);
  260.  
  261. DATE_DAY:
  262. Result := Padz(IntToStr(Day), 2) + '-' + Padz(IntToStr(Month), 2) + '-' + Copy(IntToStr(Year), 3, 4);
  263. end;
  264. end;
  265.  
  266. (*
  267. waitFunc
  268. ~~~~~~~~
  269.  
  270. .. code-block:: pascal
  271.  
  272. function waitFunc(Func: Function: Boolean; WaitPerLoop, MaxTime: Integer; Value: Boolean = true): Boolean;
  273.  
  274. Waits for function Func to be true or false (default true). WaitPerLoop is how often you
  275. want to call "Func" function.
  276. Example: "waitFunc(@BankScreen, 10 + Random(15), 750);" will check if BankScreen
  277. is open every 10-25th millisecond, for a maximum of 750 milliseconds.
  278. Notice the '@'.
  279.  
  280. .. note::
  281.  
  282. - by Rasta Magician, small edit by EvilChicken!
  283. - Last Updated: 17 July 2016 by BMWxi
  284.  
  285. Example:
  286.  
  287. .. code-block:: pascal
  288.  
  289. waitFunc(@funcName, 50, 5000);
  290. *)
  291. function waitFunc(Func: function: Boolean; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
  292. var
  293. T: UInt64;
  294. begin
  295. T := GetTickCount64() + MaxTime;
  296. while (GetTickCount64() < T) do
  297. begin
  298. if (Func() = value) then
  299. begin
  300. exit(True);
  301. end;
  302. wait(WaitPerLoop);
  303. end;
  304. end;
  305.  
  306. (*
  307. waitTypeFunc
  308. ~~~~~~~~~~~~
  309.  
  310. .. code-block:: pascal
  311.  
  312. function waitTypeFunc(Func: function: boolean of object; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
  313.  
  314. Waits for a type function Func to be true or false (default true). WaitPerLoop is how often you
  315. want to call "Func" function.
  316. Example: "waitTypeFunc(@bankScreen.isOpen, 10 + Random(15), 750);" will check if bankScreen
  317. is open every 10-25th millisecond, for a maximum of 750 milliseconds.
  318. Notice the '@'.
  319.  
  320. .. note::
  321.  
  322. - by Olly
  323. - Last Updated: 17 July 2016 by BMWxi
  324.  
  325. Example:
  326.  
  327. .. code-block:: pascal
  328.  
  329. waitTypeFunc(@minimap.isResting, 50, 5000);
  330. *)
  331. function waitTypeFunc(Func: function: Boolean of object; WaitPerLoop, MaxTime: Integer; Value: Boolean = True): Boolean;
  332. var
  333. T: UInt64;
  334. begin
  335. T := GetTickCount64() + MaxTime;
  336. while (GetTickCount64() < T) do
  337. begin
  338. if (Func() = Value) then
  339. begin
  340. exit(True);
  341. end;
  342. wait(WaitPerLoop);
  343. end;
  344. end;
  345.  
  346. (*
  347. TTimeMarker
  348. ~~~~~~~~~~~
  349.  
  350. .. code-block:: pascal
  351.  
  352. type TTimeMarker = record
  353. time, startTime: LongWord;
  354. paused: Boolean;
  355. end;
  356.  
  357. Timer type which is useful for loops, timing and writing progress reports.
  358.  
  359. .. note::
  360.  
  361. - by Bart de Boer
  362. *)
  363. type
  364. TTimeMarker = record
  365. name: string;
  366. time, startTime, __prevMark: UInt64;
  367. paused: Boolean;
  368. end;
  369.  
  370. (*
  371. TTimeMarker.start
  372. ~~~~~~~~~~~~~~~~~
  373.  
  374. .. code-block pascal
  375.  
  376. procedure TTimeMarker.start();
  377.  
  378. Starts the timer. Can also be used when paused to continue where it left.
  379.  
  380. .. note::
  381.  
  382. - by Bart de Boer
  383.  
  384. Example:
  385.  
  386. .. code-block:: pascal
  387.  
  388. MyScriptTimer.start();
  389. *)
  390. procedure TTimeMarker.start();
  391. begin
  392. Self.__prevMark := getTickCount64(); // used for adding time after a pause
  393.  
  394. // if it hasn't been started yet
  395. if (not Self.paused) then
  396. begin
  397. Self.startTime := getTickCount64();
  398. Self.time := 0;
  399. end;
  400.  
  401. Self.paused := False;
  402. end;
  403.  
  404. (*
  405. TTimeMarker.reset
  406. ~~~~~~~~~~~~~~~~~
  407.  
  408. .. code-block pascal
  409.  
  410. procedure TTimeMarker.reset();
  411.  
  412. Stops the timer and resets it to zero.
  413.  
  414. .. note::
  415.  
  416. - by Bart de Boer
  417.  
  418. Example:
  419.  
  420. .. code-block:: pascal
  421.  
  422. MyScriptTimer.reset();
  423. *)
  424. procedure TTimeMarker.reset();
  425. begin
  426. Self.paused := False;
  427. Self.time := 0;
  428. Self.startTime := 0;
  429. Self.__prevMark := 0;
  430. end;
  431.  
  432. (*
  433. TTimeMarker.pause
  434. ~~~~~~~~~~~~~~~~~
  435.  
  436. .. code-block pascal
  437.  
  438. procedure TTimeMarker.pause();
  439.  
  440. Pauses the timer. It can be continued with start().
  441.  
  442. .. note::
  443.  
  444. - by Bart de Boer
  445.  
  446. Example:
  447.  
  448. .. code-block:: pascal
  449.  
  450. MyScriptTimer.pause();
  451. TakeABreak(90000);
  452. MyScriptTimer.start();
  453. *)
  454. procedure TTimeMarker.pause();
  455. begin
  456. Self.time := Self.time + (getTickCount64() - Self.__prevMark);
  457. Self.paused := True;
  458. end;
  459.  
  460. (*
  461. TTimeMarker.getTime
  462. ~~~~~~~~~~~~~~~~~~~
  463.  
  464. .. code-block pascal
  465.  
  466. function TTimeMarker.getTime(): LongWord;
  467.  
  468. Gets the time from the timer. Returns zero if the timer was not set.
  469.  
  470. .. note::
  471.  
  472. - by Bart de Boer
  473.  
  474. Example:
  475.  
  476. .. code-block:: pascal
  477.  
  478. MyScriptTimer.start();
  479. repeat
  480. DoStuff;
  481. until(MyScriptTimer.getTime() > 60000);
  482.  
  483. *)
  484. function TTimeMarker.getTime(): LongWord;
  485. begin
  486. if not Self.paused then
  487. Result := Self.time + (getTickCount64() - Self.__prevMark)
  488. else
  489. Result := Self.time;
  490. end;
  491.  
  492. (*
  493. TTimeMarker.getTotalTime
  494. ~~~~~~~~~~~~~~~~~~~~~~~~
  495.  
  496. .. code-block pascal
  497.  
  498. function TTimeMarker.getTotalTime(): LongWord;
  499.  
  500. Gets the time from the timer including the time it was paused. Returns zero if
  501. the timer was not set.
  502.  
  503. .. note::
  504.  
  505. - by Bart de Boer
  506.  
  507. Example:
  508.  
  509. .. code-block:: pascal
  510.  
  511. BreakTime := MyTimer.getTotalTime() - MyTimer.getTime();
  512.  
  513. *)
  514. function TTimeMarker.getTotalTime(): LongWord;
  515. begin
  516. if (Self.startTime > 0) then
  517. Result := getTickCount64() - Self.startTime;
  518. end;
  519.  
  520. (*
  521. TCountDown
  522. ~~~~~~~~~~
  523.  
  524. .. code-block:: pascal
  525.  
  526. type
  527. TCountDown = type UInt64;
  528.  
  529. Timer type which is useful for loops, timing and writing progress reports.
  530.  
  531. .. note::
  532.  
  533. - by Obscurity
  534. *)
  535. type
  536. TCountDown = type UInt64;
  537.  
  538. function TCountDown.setTime(Time: UInt64): UInt64;
  539. begin
  540. self := GetTickCount64() + Time;
  541. Result := self;
  542. end;
  543.  
  544. function TCountDown.timeRemaining(): uInt32;
  545. begin
  546. if not self.isFinished() then
  547. Result := self - GetTickCount64();
  548. end;
  549.  
  550. function TCountDown.isFinished(): Boolean;
  551. begin
  552. result := GetTickCount64() >= self;
  553. end;
  554.  
  555. {$f+}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement