Files
TCPlayerbotCore/src/server/game/Instances/InstanceSaveMgr.h
T

199 lines
7.9 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
2012-01-01 00:32:13 +01:00
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:54:07 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
#ifndef _INSTANCESAVEMGR_H
#define _INSTANCESAVEMGR_H
2009-10-17 15:51:44 -07:00
2010-06-07 12:08:15 -06:00
#include "Define.h"
#include <ace/Singleton.h>
#include <ace/Thread_Mutex.h>
2008-10-02 16:23:55 -05:00
#include <list>
#include <map>
#include "UnorderedMap.h"
#include "DatabaseEnv.h"
#include "DBCEnums.h"
#include "ObjectDefines.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
struct InstanceTemplate;
struct MapEntry;
class Player;
class Group;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/*
Holds the information necessary for creating a new map for an existing instance
Is referenced in three cases:
- player-instance binds for solo players (not in group)
- player-instance binds for permanent heroic/raid saves
- group-instance binds (both solo and permanent) cache the player binds for the group leader
*/
class InstanceSave
{
friend class InstanceSaveManager;
public:
/* Created either when:
- any new instance is being generated
- the first time a player bound to InstanceId logs in
- when a group bound to the instance is loaded */
InstanceSave(uint16 MapId, uint32 InstanceId, Difficulty difficulty, time_t resetTime, bool canReset);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* Unloaded when m_playerList and m_groupList become empty
or when the instance is reset */
~InstanceSave();
2009-10-17 15:51:44 -07:00
2011-09-01 22:38:32 +03:00
uint8 GetPlayerCount() const { return m_playerList.size(); }
uint8 GetGroupCount() const { return m_groupList.size(); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* A map corresponding to the InstanceId/MapId does not always exist.
InstanceSave objects may be created on player logon but the maps are
created and loaded only when a player actually enters the instance. */
2010-12-06 02:07:53 +01:00
uint32 GetInstanceId() const { return m_instanceid; }
uint32 GetMapId() const { return m_mapid; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* Saved when the instance is generated for the first time */
void SaveToDB();
/* When the instance is being reset (permanently deleted) */
void DeleteFromDB();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* for normal instances this corresponds to max(creature respawn time) + X hours
for raid/heroic instances this caches the global respawn time for the map */
2010-12-06 02:07:53 +01:00
time_t GetResetTime() const { return m_resetTime; }
2008-10-02 16:23:55 -05:00
void SetResetTime(time_t resetTime) { m_resetTime = resetTime; }
time_t GetResetTimeForDB();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
InstanceTemplate const* GetTemplate();
MapEntry const* GetMapEntry();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* online players bound to the instance (perm/solo)
does not include the members of the group unless they have permanent saves */
2011-11-08 10:48:49 -05:00
void AddPlayer(Player* player) { TRINITY_GUARD(ACE_Thread_Mutex, _lock); m_playerList.push_back(player); }
bool RemovePlayer(Player* player) { TRINITY_GUARD(ACE_Thread_Mutex, _lock); m_playerList.remove(player); return UnloadIfEmpty(); }
2008-10-02 16:23:55 -05:00
/* all groups bound to the instance */
2011-06-12 02:00:43 +02:00
void AddGroup(Group* group) { m_groupList.push_back(group); }
bool RemoveGroup(Group* group) { m_groupList.remove(group); return UnloadIfEmpty(); }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* instances cannot be reset (except at the global reset time)
if there are players permanently bound to it
this is cached for the case when those players are offline */
2010-12-06 02:07:53 +01:00
bool CanReset() const { return m_canReset; }
2008-10-02 16:23:55 -05:00
void SetCanReset(bool canReset) { m_canReset = canReset; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* currently it is possible to omit this information from this structure
but that would depend on a lot of things that can easily change in future */
2010-12-06 02:07:53 +01:00
Difficulty GetDifficulty() const { return m_difficulty; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
typedef std::list<Player*> PlayerListType;
typedef std::list<Group*> GroupListType;
private:
bool UnloadIfEmpty();
/* the only reason the instSave-object links are kept is because
the object-instSave links need to be broken at reset time
TODO: maybe it's enough to just store the number of players/groups */
PlayerListType m_playerList;
GroupListType m_groupList;
time_t m_resetTime;
uint32 m_instanceid;
uint32 m_mapid;
Difficulty m_difficulty;
2008-10-02 16:23:55 -05:00
bool m_canReset;
ACE_Thread_Mutex _lock;
2008-10-02 16:23:55 -05:00
};
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
typedef UNORDERED_MAP<uint32 /*PAIR32(map, difficulty)*/, time_t /*resetTime*/> ResetTimeByMapDifficultyMap;
class InstanceSaveManager
2008-10-02 16:23:55 -05:00
{
friend class ACE_Singleton<InstanceSaveManager, ACE_Thread_Mutex>;
2008-10-02 16:23:55 -05:00
friend class InstanceSave;
2011-09-28 11:11:38 +02:00
private:
InstanceSaveManager() : lock_instLists(false) {};
2008-10-02 16:23:55 -05:00
~InstanceSaveManager();
public:
typedef UNORDERED_MAP<uint32 /*InstanceId*/, InstanceSave*> InstanceSaveHashMap;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* resetTime is a global propery of each (raid/heroic) map
all instances of that map reset at the same time */
struct InstResetEvent
{
uint8 type;
Difficulty difficulty:8;
2008-10-02 16:23:55 -05:00
uint16 mapid;
uint16 instanceId;
InstResetEvent() : type(0), difficulty(DUNGEON_DIFFICULTY_NORMAL), mapid(0), instanceId(0) {}
InstResetEvent(uint8 t, uint32 _mapid, Difficulty d, uint16 _instanceid)
: type(t), difficulty(d), mapid(_mapid), instanceId(_instanceid) {}
2011-09-01 22:38:32 +03:00
bool operator == (const InstResetEvent& e) const { return e.instanceId == instanceId; }
2008-10-02 16:23:55 -05:00
};
typedef std::multimap<time_t /*resetTime*/, InstResetEvent> ResetTimeQueue;
2009-10-17 15:51:44 -07:00
void LoadInstances();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void LoadResetTimes();
time_t GetResetTimeFor(uint32 mapid, Difficulty d) const
{
2011-04-29 20:47:02 +02:00
ResetTimeByMapDifficultyMap::const_iterator itr = m_resetTimeByMapDifficulty.find(MAKE_PAIR32(mapid, d));
return itr != m_resetTimeByMapDifficulty.end() ? itr->second : 0;
}
2011-09-28 11:11:38 +02:00
void SetResetTimeFor(uint32 mapid, Difficulty d, time_t t)
{
2011-04-29 20:47:02 +02:00
m_resetTimeByMapDifficulty[MAKE_PAIR32(mapid, d)] = t;
}
2011-09-28 11:11:38 +02:00
ResetTimeByMapDifficultyMap const& GetResetTimeMap() const
{
return m_resetTimeByMapDifficulty;
}
2008-10-02 16:23:55 -05:00
void ScheduleReset(bool add, time_t time, InstResetEvent event);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void Update();
2009-10-17 15:51:44 -07:00
2011-09-28 11:11:38 +02:00
InstanceSave* AddInstanceSave(uint32 mapId, uint32 instanceId, Difficulty difficulty, time_t resetTime,
bool canReset, bool load = false);
2008-10-02 16:23:55 -05:00
void RemoveInstanceSave(uint32 InstanceId);
static void DeleteInstanceFromDB(uint32 instanceid);
2009-10-17 15:51:44 -07:00
InstanceSave* GetInstanceSave(uint32 InstanceId);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* statistics */
uint32 GetNumInstanceSaves() { return m_instanceSaveById.size(); }
uint32 GetNumBoundPlayersTotal();
uint32 GetNumBoundGroupsTotal();
2009-10-17 15:51:44 -07:00
protected:
static uint16 ResetTimeDelay[];
2008-10-02 16:23:55 -05:00
private:
void _ResetOrWarnAll(uint32 mapid, Difficulty difficulty, bool warn, time_t resetTime);
2008-10-02 16:23:55 -05:00
void _ResetInstance(uint32 mapid, uint32 instanceId);
void _ResetSave(InstanceSaveHashMap::iterator &itr);
// used during global instance resets
bool lock_instLists;
// fast lookup by instance id
InstanceSaveHashMap m_instanceSaveById;
// fast lookup for reset times (always use existed functions for access/set)
ResetTimeByMapDifficultyMap m_resetTimeByMapDifficulty;
2008-10-02 16:23:55 -05:00
ResetTimeQueue m_resetTimeQueue;
};
2009-10-17 15:51:44 -07:00
#define sInstanceSaveMgr ACE_Singleton<InstanceSaveManager, ACE_Thread_Mutex>::instance()
2008-10-02 16:23:55 -05:00
#endif