EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
timer.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Rewrite: Allan
25 */
26 
27 #include <ctime>
28 #include "eve-core.h"
29 
30 #include "utils/timer.h"
31 #include "utils/utils_time.h"
32 
33 static uint32 currentTime = 0;
34 //static float currentSeconds = 0;
35 static int64 lastTime = 0;
36 
37 Timer::Timer(uint32 time/*0*/, bool useAcurateTiming /*false*/) {
38  m_duration = time;
40  m_setAtTrigger = time;
41  m_useAcurateTiming = useAcurateTiming;
42 
43  if (time)
44  m_enabled = true;
45  else
46  m_enabled = false;
47 }
48 
49 Timer::Timer(uint32 startAt, uint32 time, bool useAcurateTiming /*false*/) {
50  m_duration = time;
51  m_startTime = startAt;
52  m_setAtTrigger = time;
53  m_useAcurateTiming = useAcurateTiming;
54 
55  if (time)
56  m_enabled = true;
57  else
58  m_enabled = false;
59 }
60 
61 /* This function checks if the timer triggered */
62 bool Timer::Check(bool reset /*true*/)
63 {
64  if (m_enabled)
66  if (reset) {
68  m_startTime += m_duration; /* set start time to end of last timer */
69  else
70  m_startTime = currentTime; /* set start time to now */
72  } else
73  m_enabled = false;
74  return true;
75  }
76 
77  return false;
78 }
79 
80 /* This function sets the timer and starts it */
81 void Timer::Start(uint32 setTimerTime, bool changeResetTimer /*true*/) {
82  if (setTimerTime == 0) {
83  m_enabled = false;
84  return;
85  }
86 
87  if (m_enabled) { // this will allow resetting of the time without changing the start time
88  if (m_duration != setTimerTime)
89  m_duration = setTimerTime;
90  } else {
92  m_duration = setTimerTime;
93  }
94 
95  if (changeResetTimer)
96  m_setAtTrigger = setTimerTime;
97 
98  m_enabled = true;
99 }
100 
101 /* This updates the timer without restarting it */
102 void Timer::SetTimer(uint32 setTimerTime) {
103  /* If we were disabled before => restart the timer */
104  if (!m_enabled) {
106  m_enabled = true;
107  }
108  if (setTimerTime) {
109  m_duration = setTimerTime;
110  m_setAtTrigger = setTimerTime;
111  }
112 }
113 
115  if (m_enabled)
117  return (m_startTime + m_duration - currentTime);
118 
119  return 0;
120 }
121 
122 void Timer::SetAtTrigger(uint32 setAtTrigger, bool enableIfDisabled) {
123  m_setAtTrigger = setAtTrigger;
124  if (!m_enabled && enableIfDisabled)
125  Enable();
126 }
127 
129  m_enabled = true;
132 }
133 
135  return currentTime;
136 }
137 
139 {
140  int64 tickCount = GetSteadyTime();
141  if (lastTime == 0)
142  currentTime = 0;
143  else
144  currentTime += (tickCount - lastTime);
145  lastTime = tickCount;
146  //currentSeconds = (tickCount / 1000);
147 }
void Trigger()
Definition: timer.cpp:128
uint32 m_setAtTrigger
Definition: timer.h:66
static const void SetCurrentTime()
Definition: timer.cpp:138
void SetTimer(uint32 setTimerTime=0)
Definition: timer.cpp:102
bool m_useAcurateTiming
Definition: timer.h:62
uint32 GetRemainingTime() const
Definition: timer.cpp:114
int64 GetSteadyTime()
Definition: utils_time.cpp:95
bool m_enabled
Definition: timer.h:60
uint32 m_startTime
Definition: timer.h:65
uint32 GetCurrentTime()
Definition: timer.cpp:134
bool Check(bool reset=true)
Definition: timer.cpp:62
static int64 lastTime
Definition: timer.cpp:35
uint32 m_duration
Definition: timer.h:64
void Enable()
Definition: timer.h:38
unsigned __int32 uint32
Definition: eve-compat.h:50
Timer(uint32 time=0, bool useAcurateTiming=false)
Definition: timer.cpp:37
signed __int64 int64
Definition: eve-compat.h:51
void SetAtTrigger(uint32 setAtTrigger, bool enableIfDisabled=false)
Definition: timer.cpp:122
static uint32 currentTime
Definition: timer.cpp:33
void Start(uint32 setTimerTime=0, bool changeResetTimer=true)
Definition: timer.cpp:81