timer.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. EQ2Emulator: Everquest II Server Emulator
  3. Copyright (C) 2007 EQ2EMulator Development Team (http://www.eq2emulator.net)
  4. This file is part of EQ2Emulator.
  5. EQ2Emulator is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. EQ2Emulator is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with EQ2Emulator. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "../common/debug.h"
  17. // Disgrace: for windows compile
  18. #ifndef WIN32
  19. #include <sys/time.h>
  20. #else
  21. #include <sys/timeb.h>
  22. #endif
  23. #include <iostream>
  24. using namespace std;
  25. #include "timer.h"
  26. int32 started_unix_timestamp = 0;
  27. int32 current_time = 0;
  28. int32 last_time = 0;
  29. Timer::Timer(){
  30. timer_time = 30000; //default to 30 seconds
  31. start_time = current_time;
  32. set_at_trigger = timer_time;
  33. pUseAcurateTiming = false;
  34. enabled = false;
  35. }
  36. Timer::Timer(int32 in_timer_time, bool iUseAcurateTiming) {
  37. timer_time = in_timer_time;
  38. start_time = current_time;
  39. set_at_trigger = timer_time;
  40. pUseAcurateTiming = iUseAcurateTiming;
  41. if (timer_time == 0) {
  42. enabled = false;
  43. }
  44. else {
  45. enabled = true;
  46. }
  47. }
  48. Timer::Timer(int32 start, int32 timer, bool iUseAcurateTiming = false) {
  49. timer_time = timer;
  50. start_time = start;
  51. set_at_trigger = timer_time;
  52. pUseAcurateTiming = iUseAcurateTiming;
  53. if (timer_time == 0) {
  54. enabled = false;
  55. }
  56. else {
  57. enabled = true;
  58. }
  59. }
  60. /* Reimplemented for MSVC - Bounce */
  61. #ifdef WIN32
  62. int gettimeofday (timeval *tp, ...)
  63. {
  64. timeb tb;
  65. ftime (&tb);
  66. tp->tv_sec = tb.time;
  67. tp->tv_usec = tb.millitm * 1000;
  68. return 0;
  69. }
  70. #endif
  71. /* This function checks if the timer triggered */
  72. bool Timer::Check(bool iReset)
  73. {
  74. if (enabled && current_time-start_time > timer_time) {
  75. if (iReset) {
  76. if (pUseAcurateTiming)
  77. start_time += timer_time;
  78. else
  79. start_time = current_time; // Reset timer
  80. timer_time = set_at_trigger;
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. /* This function disables the timer */
  87. void Timer::Disable() {
  88. enabled = false;
  89. }
  90. void Timer::Enable() {
  91. enabled = true;
  92. }
  93. /* This function set the timer and restart it */
  94. void Timer::Start(int32 set_timer_time, bool ChangeResetTimer) {
  95. start_time = current_time;
  96. enabled = true;
  97. if (set_timer_time != 0)
  98. {
  99. timer_time = set_timer_time;
  100. if (ChangeResetTimer)
  101. set_at_trigger = set_timer_time;
  102. }
  103. }
  104. /* This timer updates the timer without restarting it */
  105. void Timer::SetTimer(int32 set_timer_time) {
  106. /* If we were disabled before => restart the timer */
  107. if (!enabled) {
  108. start_time = current_time;
  109. enabled = true;
  110. }
  111. if (set_timer_time != 0) {
  112. timer_time = set_timer_time;
  113. set_at_trigger = set_timer_time;
  114. }
  115. }
  116. int32 Timer::GetElapsedTime(){
  117. if (enabled) {
  118. return current_time - start_time;
  119. }
  120. else {
  121. return 0xFFFFFFFF;
  122. }
  123. }
  124. int32 Timer::GetRemainingTime() {
  125. if (enabled) {
  126. if (current_time-start_time > timer_time)
  127. return 0;
  128. else
  129. return (start_time + timer_time) - current_time;
  130. }
  131. else {
  132. return 0xFFFFFFFF;
  133. }
  134. }
  135. void Timer::SetAtTrigger(int32 in_set_at_trigger, bool iEnableIfDisabled) {
  136. set_at_trigger = in_set_at_trigger;
  137. if (!Enabled() && iEnableIfDisabled) {
  138. Enable();
  139. }
  140. }
  141. void Timer::Trigger()
  142. {
  143. enabled = true;
  144. timer_time = set_at_trigger;
  145. start_time = current_time-timer_time-1;
  146. }
  147. const int32& Timer::GetCurrentTime2()
  148. {
  149. return current_time;
  150. }
  151. const int32& Timer::SetCurrentTime()
  152. {
  153. struct timeval read_time;
  154. int32 this_time;
  155. gettimeofday(&read_time,0);
  156. if(started_unix_timestamp == 0)
  157. started_unix_timestamp = read_time.tv_sec;
  158. this_time = (read_time.tv_sec - started_unix_timestamp) * 1000 + read_time.tv_usec / 1000;
  159. if (last_time == 0)
  160. {
  161. current_time = 0;
  162. }
  163. else
  164. {
  165. current_time += this_time - last_time;
  166. }
  167. last_time = this_time;
  168. // cerr << "Current time:" << current_time << endl;
  169. return current_time;
  170. }
  171. int32 Timer::GetUnixTimeStamp(){
  172. struct timeval read_time;
  173. gettimeofday(&read_time,0);
  174. return read_time.tv_sec;
  175. }