[7833] Implement levelup spells for non-hunter pets.
[getmangos.git] / src / shared / Timer.h
blobc258fdcee3a22f55932aeff4637fc6cf1f7fa5e7
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #ifndef MANGOS_TIMER_H
20 #define MANGOS_TIMER_H
22 #include "Platform/CompilerDefs.h"
24 #if PLATFORM == PLATFORM_WINDOWS
25 # include <ace/config-all.h>
26 # include <mmsystem.h>
27 # include <time.h>
28 #else
29 # if defined(__APPLE_CC__)
30 # include <time.h>
31 # endif
32 # include <sys/time.h>
33 # include <sys/timeb.h>
34 #endif
36 #if PLATFORM == PLATFORM_WINDOWS
37 inline uint32 getMSTime() { return GetTickCount(); }
38 #else
39 inline uint32 getMSTime()
41 struct timeval tv;
42 struct timezone tz;
43 gettimeofday( &tv, &tz );
44 return (tv.tv_sec * 1000) + (tv.tv_usec / 1000);
46 #endif
48 inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
50 // getMSTime() have limited data range and this is case when it overflow in this tick
51 if (oldMSTime > newMSTime)
52 return (0xFFFFFFFF - oldMSTime) + newMSTime;
53 else
54 return newMSTime - oldMSTime;
57 class IntervalTimer
59 public:
60 IntervalTimer() : _interval(0), _current(0) {}
62 void Update(time_t diff) { _current += diff; if(_current<0) _current=0;}
63 bool Passed() { return _current >= _interval; }
64 void Reset() { if(_current >= _interval) _current -= _interval; }
66 void SetCurrent(time_t current) { _current = current; }
67 void SetInterval(time_t interval) { _interval = interval; }
68 time_t GetInterval() const { return _interval; }
69 time_t GetCurrent() const { return _current; }
71 private:
72 time_t _interval;
73 time_t _current;
76 struct TimeTracker
78 TimeTracker(time_t expiry) : i_expiryTime(expiry) {}
79 void Update(time_t diff) { i_expiryTime -= diff; }
80 bool Passed(void) const { return (i_expiryTime <= 0); }
81 void Reset(time_t interval) { i_expiryTime = interval; }
82 time_t GetExpiry(void) const { return i_expiryTime; }
83 time_t i_expiryTime;
86 struct TimeTrackerSmall
88 TimeTrackerSmall(int32 expiry) : i_expiryTime(expiry) {}
89 void Update(int32 diff) { i_expiryTime -= diff; }
90 bool Passed(void) const { return (i_expiryTime <= 0); }
91 void Reset(int32 interval) { i_expiryTime = interval; }
92 int32 GetExpiry(void) const { return i_expiryTime; }
93 int32 i_expiryTime;
96 #endif