[10687] UInt32 timer class copies instead using time_t timers for mstime cases.

Also cleanup weather update code and some random code cleanups.
This commit is contained in:
VladimirMangos 2010-11-06 21:54:05 +03:00
parent 478bf3b367
commit 464908f453
15 changed files with 87 additions and 67 deletions

View file

@ -78,6 +78,8 @@ struct MANGOS_DLL_DECL Cell
bool NoCreate() const { return data.Part.nocreate; } bool NoCreate() const { return data.Part.nocreate; }
void SetNoCreate() { data.Part.nocreate = 1; } void SetNoCreate() { data.Part.nocreate = 1; }
GridPair gridPair() const { return GridPair(GridX(),GridY()); }
CellPair cellPair() const CellPair cellPair() const
{ {
return CellPair( return CellPair(

View file

@ -30,7 +30,7 @@ class Map;
template<typename TRAVELLER> template<typename TRAVELLER>
class MANGOS_DLL_DECL DestinationHolder class MANGOS_DLL_DECL DestinationHolder
{ {
TimeTrackerSmall i_tracker; ShortTimeTracker i_tracker;
uint32 i_totalTravelTime; uint32 i_totalTravelTime;
uint32 i_timeElapsed; uint32 i_timeElapsed;
bool i_destSet; bool i_destSet;

View file

@ -246,10 +246,10 @@ MapManager::Update(uint32 diff)
return; return;
for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter)
iter->second->Update((uint32)i_timer.GetCurrent()); iter->second->Update(i_timer.GetCurrent());
for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter)
(*iter)->Update((uint32)i_timer.GetCurrent()); (*iter)->Update(i_timer.GetCurrent());
i_timer.SetCurrent(0); i_timer.SetCurrent(0);
} }

View file

@ -171,7 +171,7 @@ class MANGOS_DLL_DECL MapManager : public MaNGOS::Singleton<MapManager, MaNGOS::
typedef MaNGOS::ClassLevelLockable<MapManager, ACE_Thread_Mutex>::Lock Guard; typedef MaNGOS::ClassLevelLockable<MapManager, ACE_Thread_Mutex>::Lock Guard;
uint32 i_gridCleanUpDelay; uint32 i_gridCleanUpDelay;
MapMapType i_maps; MapMapType i_maps;
IntervalTimer i_timer; ShortIntervalTimer i_timer;
}; };
#define sMapMgr MapManager::Instance() #define sMapMgr MapManager::Instance()

View file

@ -44,7 +44,7 @@ class MANGOS_DLL_SPEC RandomMovementGenerator
bool GetResetPosition(T&, float& x, float& y, float& z); bool GetResetPosition(T&, float& x, float& y, float& z);
private: private:
TimeTrackerSmall i_nextMoveTime; ShortTimeTracker i_nextMoveTime;
DestinationHolder< Traveller<T> > i_destinationHolder; DestinationHolder< Traveller<T> > i_destinationHolder;
uint32 i_nextMove; uint32 i_nextMove;

View file

@ -215,7 +215,7 @@ class MANGOS_DLL_SPEC ThreatManager
private: private:
HostileReference* iCurrentVictim; HostileReference* iCurrentVictim;
Unit* iOwner; Unit* iOwner;
TimeTrackerSmall iUpdateTimer; ShortTimeTracker iUpdateTimer;
bool iUpdateNeed; bool iUpdateNeed;
ThreatContainer iThreatContainer; ThreatContainer iThreatContainer;
ThreatContainer iThreatOfflineContainer; ThreatContainer iThreatOfflineContainer;

View file

@ -8717,11 +8717,11 @@ DiminishingLevels Unit::GetDiminishing(DiminishingGroup group)
if(!i->hitCount) if(!i->hitCount)
return DIMINISHING_LEVEL_1; return DIMINISHING_LEVEL_1;
if(!i->hitTime) if (!i->hitTime)
return DIMINISHING_LEVEL_1; return DIMINISHING_LEVEL_1;
// If last spell was casted more than 15 seconds ago - reset the count. // If last spell was casted more than 15 seconds ago - reset the count.
if(i->stack==0 && getMSTimeDiff(i->hitTime,getMSTime()) > 15000) if (i->stack==0 && getMSTimeDiff(i->hitTime,getMSTime()) > 15*IN_MILLISECONDS)
{ {
i->hitCount = DIMINISHING_LEVEL_1; i->hitCount = DIMINISHING_LEVEL_1;
return DIMINISHING_LEVEL_1; return DIMINISHING_LEVEL_1;

View file

@ -99,7 +99,7 @@ public PathMovementBase<Creature, WaypointPath const*>
bool GetResetPosition(Creature&, float& x, float& y, float& z); bool GetResetPosition(Creature&, float& x, float& y, float& z);
private: private:
TimeTrackerSmall i_nextMoveTime; ShortTimeTracker i_nextMoveTime;
bool m_isArrivalDone; bool m_isArrivalDone;
bool m_isStoppedByPlayer; bool m_isStoppedByPlayer;
}; };

View file

@ -35,15 +35,13 @@ Weather::Weather(uint32 zone, WeatherZoneChances const* weatherChances) : m_zone
m_type = WEATHER_TYPE_FINE; m_type = WEATHER_TYPE_FINE;
m_grade = 0; m_grade = 0;
DETAIL_FILTER_LOG(LOG_FILTER_WEATHER, "WORLD: Starting weather system for zone %u (change every %u minutes).", m_zone, (uint32)(m_timer.GetInterval() / (MINUTE*IN_MILLISECONDS)) ); DETAIL_FILTER_LOG(LOG_FILTER_WEATHER, "WORLD: Starting weather system for zone %u (change every %u minutes).", m_zone, (m_timer.GetInterval() / (MINUTE*IN_MILLISECONDS)) );
} }
/// Launch a weather update /// Launch a weather update
bool Weather::Update(time_t diff) bool Weather::Update(time_t diff)
{ {
if (m_timer.GetCurrent()>=0) m_timer.Update(diff);
m_timer.Update(diff);
else m_timer.SetCurrent(0);
///- If the timer has passed, ReGenerate the weather ///- If the timer has passed, ReGenerate the weather
if(m_timer.Passed()) if(m_timer.Passed())

View file

@ -66,7 +66,7 @@ class Weather
uint32 m_zone; uint32 m_zone;
WeatherType m_type; WeatherType m_type;
float m_grade; float m_grade;
IntervalTimer m_timer; ShortIntervalTimer m_timer;
WeatherZoneChances const* m_weatherChances; WeatherZoneChances const* m_weatherChances;
}; };
#endif #endif

View file

@ -1286,7 +1286,7 @@ void World::SetInitialWorldSettings()
//one second is 1000 -(tested on win system) //one second is 1000 -(tested on win system)
mail_timer = uint32((((localtime( &m_gameTime )->tm_hour + 20) % 24)* HOUR * IN_MILLISECONDS) / m_timers[WUPDATE_AUCTIONS].GetInterval() ); mail_timer = uint32((((localtime( &m_gameTime )->tm_hour + 20) % 24)* HOUR * IN_MILLISECONDS) / m_timers[WUPDATE_AUCTIONS].GetInterval() );
//1440 //1440
mail_timer_expires = uint32( (DAY * IN_MILLISECONDS) / (m_timers[WUPDATE_AUCTIONS].GetInterval())); mail_timer_expires = (DAY * IN_MILLISECONDS) / m_timers[WUPDATE_AUCTIONS].GetInterval();
DEBUG_LOG("Mail timer set to: %u, mail return is called every %u minutes", mail_timer, mail_timer_expires); DEBUG_LOG("Mail timer set to: %u, mail return is called every %u minutes", mail_timer, mail_timer_expires);
///- Initialize static helper structures ///- Initialize static helper structures
@ -1384,12 +1384,7 @@ void World::Update(uint32 diff)
{ {
///- Update the different timers ///- Update the different timers
for(int i = 0; i < WUPDATE_COUNT; ++i) for(int i = 0; i < WUPDATE_COUNT; ++i)
{ m_timers[i].Update(diff);
if (m_timers[i].GetCurrent()>=0)
m_timers[i].Update(diff);
else
m_timers[i].SetCurrent(0);
}
///- Update the game time and check for shutdown time ///- Update the game time and check for shutdown time
_UpdateGameTime(); _UpdateGameTime();
@ -1434,23 +1429,21 @@ void World::Update(uint32 diff)
/// <li> Handle weather updates when the timer has passed /// <li> Handle weather updates when the timer has passed
if (m_timers[WUPDATE_WEATHERS].Passed()) if (m_timers[WUPDATE_WEATHERS].Passed())
{ {
m_timers[WUPDATE_WEATHERS].Reset();
///- Send an update signal to Weather objects ///- Send an update signal to Weather objects
WeatherMap::iterator itr, next; for (WeatherMap::iterator itr = m_weathers.begin(); itr != m_weathers.end(); )
for (itr = m_weathers.begin(); itr != m_weathers.end(); itr = next)
{ {
next = itr;
++next;
///- and remove Weather objects for zones with no player ///- and remove Weather objects for zones with no player
//As interval > WorldTick //As interval > WorldTick
if(!itr->second->Update(m_timers[WUPDATE_WEATHERS].GetInterval())) if(!itr->second->Update(m_timers[WUPDATE_WEATHERS].GetInterval()))
{ {
delete itr->second; delete itr->second;
m_weathers.erase(itr); m_weathers.erase(itr++);
} }
else
++itr;
} }
m_timers[WUPDATE_WEATHERS].SetCurrent(0);
} }
/// <li> Update uptime table /// <li> Update uptime table
if (m_timers[WUPDATE_UPTIME].Passed()) if (m_timers[WUPDATE_UPTIME].Passed())

View file

@ -616,7 +616,7 @@ class World
time_t m_startTime; time_t m_startTime;
time_t m_gameTime; time_t m_gameTime;
IntervalTimer m_timers[WUPDATE_COUNT]; ShortIntervalTimer m_timers[WUPDATE_COUNT];
uint32 mail_timer; uint32 mail_timer;
uint32 mail_timer_expires; uint32 mail_timer_expires;

View file

@ -76,32 +76,17 @@ public:
uint32 curtime = getMSTime(); uint32 curtime = getMSTime();
//DEBUG_LOG("anti-freeze: time=%u, counters=[%u; %u]",curtime,Master::m_masterLoopCounter,World::m_worldLoopCounter); //DEBUG_LOG("anti-freeze: time=%u, counters=[%u; %u]",curtime,Master::m_masterLoopCounter,World::m_worldLoopCounter);
// There is no Master anymore
// TODO: clear the rest of the code
// // normal work
// if(m_loops != Master::m_masterLoopCounter)
// {
// m_lastchange = curtime;
// m_loops = Master::m_masterLoopCounter;
// }
// // possible freeze
// else if(getMSTimeDiff(m_lastchange,curtime) > _delaytime)
// {
// sLog.outError("Main/Sockets Thread hangs, kicking out server!");
// *((uint32 volatile*)NULL) = 0; // bang crash
// }
// normal work // normal work
if(w_loops != World::m_worldLoopCounter) if (w_loops != World::m_worldLoopCounter)
{ {
w_lastchange = curtime; w_lastchange = curtime;
w_loops = World::m_worldLoopCounter; w_loops = World::m_worldLoopCounter;
} }
// possible freeze // possible freeze
else if(getMSTimeDiff(w_lastchange,curtime) > _delaytime) else if (getMSTimeDiff(w_lastchange, curtime) > _delaytime)
{ {
sLog.outError("World Thread hangs, kicking out server!"); sLog.outError("World Thread hangs, kicking out server!");
*((uint32 volatile*)NULL) = 0; // bang crash *((uint32 volatile*)NULL) = 0; // bang crash
} }
} }
sLog.outString("Anti-freeze thread exiting without problems."); sLog.outString("Anti-freeze thread exiting without problems.");

View file

@ -49,7 +49,7 @@ inline uint32 getMSTimeDiff(uint32 oldMSTime, uint32 newMSTime)
{ {
// getMSTime() have limited data range and this is case when it overflow in this tick // getMSTime() have limited data range and this is case when it overflow in this tick
if (oldMSTime > newMSTime) if (oldMSTime > newMSTime)
return (0xFFFFFFFF - oldMSTime) + newMSTime; return (uint32(0xFFFFFFFF) - oldMSTime) + newMSTime;
else else
return newMSTime - oldMSTime; return newMSTime - oldMSTime;
} }
@ -59,9 +59,18 @@ class IntervalTimer
public: public:
IntervalTimer() : _interval(0), _current(0) {} IntervalTimer() : _interval(0), _current(0) {}
void Update(time_t diff) { _current += diff; if(_current<0) _current=0;} void Update(time_t diff)
bool Passed() { return _current >= _interval; } {
void Reset() { if(_current >= _interval) _current -= _interval; } _current += diff;
if (_current < 0)
_current = 0;
}
bool Passed() const { return _current >= _interval; }
void Reset()
{
if (_current >= _interval)
_current -= _interval;
}
void SetCurrent(time_t current) { _current = current; } void SetCurrent(time_t current) { _current = current; }
void SetInterval(time_t interval) { _interval = interval; } void SetInterval(time_t interval) { _interval = interval; }
@ -73,24 +82,57 @@ class IntervalTimer
time_t _current; time_t _current;
}; };
struct TimeTracker class ShortIntervalTimer
{ {
TimeTracker(time_t expiry) : i_expiryTime(expiry) {} public:
void Update(time_t diff) { i_expiryTime -= diff; } ShortIntervalTimer() : _interval(0), _current(0) {}
bool Passed(void) const { return (i_expiryTime <= 0); }
void Reset(time_t interval) { i_expiryTime = interval; } void Update(uint32 diff)
time_t GetExpiry(void) const { return i_expiryTime; } {
time_t i_expiryTime; _current += diff;
}
bool Passed() const { return _current >= _interval; }
void Reset()
{
if (_current >= _interval)
_current -= _interval;
}
void SetCurrent(uint32 current) { _current = current; }
void SetInterval(uint32 interval) { _interval = interval; }
uint32 GetInterval() const { return _interval; }
uint32 GetCurrent() const { return _current; }
private:
uint32 _interval;
uint32 _current;
}; };
struct TimeTrackerSmall struct TimeTracker
{ {
TimeTrackerSmall(int32 expiry) : i_expiryTime(expiry) {} public:
void Update(int32 diff) { i_expiryTime -= diff; } TimeTracker(time_t expiry) : i_expiryTime(expiry) {}
bool Passed(void) const { return (i_expiryTime <= 0); } void Update(time_t diff) { i_expiryTime -= diff; }
void Reset(int32 interval) { i_expiryTime = interval; } bool Passed() const { return (i_expiryTime <= 0); }
int32 GetExpiry(void) const { return i_expiryTime; } void Reset(time_t interval) { i_expiryTime = interval; }
int32 i_expiryTime; time_t GetExpiry() const { return i_expiryTime; }
private:
time_t i_expiryTime;
};
struct ShortTimeTracker
{
public:
ShortTimeTracker(int32 expiry) : i_expiryTime(expiry) {}
void Update(int32 diff) { i_expiryTime -= diff; }
bool Passed() const { return (i_expiryTime <= 0); }
void Reset(int32 interval) { i_expiryTime = interval; }
int32 GetExpiry() const { return i_expiryTime; }
private:
int32 i_expiryTime;
}; };
#endif #endif

View file

@ -1,4 +1,4 @@
#ifndef __REVISION_NR_H__ #ifndef __REVISION_NR_H__
#define __REVISION_NR_H__ #define __REVISION_NR_H__
#define REVISION_NR "10686" #define REVISION_NR "10687"
#endif // __REVISION_NR_H__ #endif // __REVISION_NR_H__