Files
TCPlayerbotCore/src/server/game/Globals/ObjectAccessor.h
T

273 lines
8.9 KiB
C++
Raw Normal View History

2010-10-07 15:54:07 +02:00
/*
* Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
* 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
2008-10-14 11:57:03 -05:00
#ifndef TRINITY_OBJECTACCESSOR_H
#define TRINITY_OBJECTACCESSOR_H
2009-10-17 15:51:44 -07:00
2010-06-07 09:04:14 -06:00
#include "Define.h"
#include <ace/Singleton.h>
#include <ace/Thread_Mutex.h>
2010-06-08 13:05:47 -06:00
#include "UnorderedMap.h"
2009-10-17 15:51:44 -07:00
2010-06-08 13:05:47 -06:00
#include "UpdateData.h"
2009-10-17 15:51:44 -07:00
2010-06-08 13:05:47 -06:00
#include "GridDefines.h"
2008-10-02 16:23:55 -05:00
#include "Object.h"
#include "Player.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include <set>
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
class Creature;
class Corpse;
class Unit;
class GameObject;
class DynamicObject;
class WorldObject;
class Vehicle;
2008-10-02 16:23:55 -05:00
class Map;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template <class T>
class HashMapHolder
{
public:
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
typedef UNORDERED_MAP<uint64, T*> MapType;
typedef ACE_Thread_Mutex LockType;
2009-10-17 15:51:44 -07:00
2009-12-17 07:34:00 +01:00
static void Insert(T* o)
{
ACE_GUARD(LockType, Guard, i_lock);
2009-12-17 07:34:00 +01:00
m_objectMap[o->GetGUID()] = o;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
static void Remove(T* o)
{
ACE_GUARD(LockType, Guard, i_lock);
2009-01-20 19:59:43 -06:00
m_objectMap.erase(o->GetGUID());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
static T* Find(uint64 guid)
{
ACE_GUARD_RETURN(LockType, Guard, i_lock, NULL);
2008-10-02 16:23:55 -05:00
typename MapType::iterator itr = m_objectMap.find(guid);
return (itr != m_objectMap.end()) ? itr->second : NULL;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
static MapType& GetContainer() { return m_objectMap; }
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
static LockType* GetLock() { return &i_lock; }
2010-03-13 16:17:25 +01:00
2008-10-02 16:23:55 -05:00
private:
2009-10-17 15:51:44 -07:00
2008-12-12 11:21:28 -06:00
//Non instanceable only static
2008-10-02 16:23:55 -05:00
HashMapHolder() {}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
static LockType i_lock;
static MapType m_objectMap;
};
2009-10-17 15:51:44 -07:00
class ObjectAccessor
2008-10-02 16:23:55 -05:00
{
friend class ACE_Singleton<ObjectAccessor, ACE_Thread_Mutex>;
2008-10-02 16:23:55 -05:00
ObjectAccessor();
~ObjectAccessor();
2010-03-13 16:17:25 +01:00
ObjectAccessor(const ObjectAccessor&);
ObjectAccessor& operator=(const ObjectAccessor&);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
public:
2010-03-13 16:17:25 +01:00
typedef UNORDERED_MAP<uint64, Corpse*> Player2CorpsesMapType;
typedef UNORDERED_MAP<Player*, UpdateData>::value_type UpdateDataValueType;
2009-10-17 15:51:44 -07:00
// TODO: override these template functions for each holder type and add assertions
template<class T> static T* GetObjectInOrOutOfWorld(uint64 guid, T* /*typeSpecifier*/)
{
return HashMapHolder<T>::Find(guid);
}
static Unit* GetObjectInOrOutOfWorld(uint64 guid, Unit* /*typeSpecifier*/)
{
if (IS_PLAYER_GUID(guid))
return (Unit*)GetObjectInOrOutOfWorld(guid, (Player*)NULL);
if (IS_PET_GUID(guid))
return (Unit*)GetObjectInOrOutOfWorld(guid, (Pet*)NULL);
return (Unit*)GetObjectInOrOutOfWorld(guid, (Creature*)NULL);
}
// returns object if is in world
template<class T> static T* GetObjectInWorld(uint64 guid, T* /*typeSpecifier*/)
2008-10-02 16:23:55 -05:00
{
return HashMapHolder<T>::Find(guid);
}
2009-10-17 15:51:44 -07:00
// Player may be not in world while in ObjectAccessor
static Player* GetObjectInWorld(uint64 guid, Player* /*typeSpecifier*/)
2008-10-02 16:23:55 -05:00
{
Player * player = HashMapHolder<Player>::Find(guid);
if (player && player->IsInWorld())
return player;
return NULL;
}
2009-10-17 15:51:44 -07:00
static Unit* GetObjectInWorld(uint64 guid, Unit* /*typeSpecifier*/)
{
if (IS_PLAYER_GUID(guid))
return (Unit*)GetObjectInWorld(guid, (Player*)NULL);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
if (IS_PET_GUID(guid))
return (Unit*)GetObjectInWorld(guid, (Pet*)NULL);
2009-10-17 15:51:44 -07:00
return (Unit*)GetObjectInWorld(guid, (Creature*)NULL);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
// returns object if is in map
template<class T> static T* GetObjectInMap(uint64 guid, Map * map, T* /*typeSpecifier*/)
{
ASSERT(map);
if (T * obj = GetObjectInWorld(guid, (T*)NULL))
if (obj->GetMap() == map)
return obj;
return NULL;
}
2008-10-02 16:23:55 -05:00
template<class T> static T* GetObjectInWorld(uint32 mapid, float x, float y, uint64 guid, T* /*fake*/)
{
T* obj = HashMapHolder<T>::Find(guid);
2010-03-13 16:17:25 +01:00
if (!obj || obj->GetMapId() != mapid)
return NULL;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
CellPair p = Trinity::ComputeCellPair(x, y);
2010-04-07 22:59:46 +02:00
if (p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
2008-10-02 16:23:55 -05:00
{
sLog->outError("ObjectAccessor::GetObjectInWorld: invalid coordinates supplied X:%f Y:%f grid cell [%u:%u]", x, y, p.x_coord, p.y_coord);
2008-10-02 16:23:55 -05:00
return NULL;
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
CellPair q = Trinity::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
2010-04-07 22:59:46 +02:00
if (q.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || q.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
2008-10-02 16:23:55 -05:00
{
sLog->outError("ObjectAccessor::GetObjecInWorld: object (GUID: %u TypeId: %u) has invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), q.x_coord, q.y_coord);
2008-10-02 16:23:55 -05:00
return NULL;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
int32 dx = int32(p.x_coord) - int32(q.x_coord);
int32 dy = int32(p.y_coord) - int32(q.y_coord);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
if (dx > -2 && dx < 2 && dy > -2 && dy < 2)
return obj;
else
return NULL;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
// these functions return objects only if in map of specified object
2010-03-13 16:17:25 +01:00
static WorldObject* GetWorldObject(WorldObject const&, uint64);
static Object* GetObjectByTypeMask(WorldObject const&, uint64, uint32 typemask);
static Corpse* GetCorpse(WorldObject const& u, uint64 guid);
static GameObject* GetGameObject(WorldObject const& u, uint64 guid);
static DynamicObject* GetDynamicObject(WorldObject const& u, uint64 guid);
static Unit* GetUnit(WorldObject const&, uint64 guid);
static Creature* GetCreature(WorldObject const& u, uint64 guid);
static Pet* GetPet(WorldObject const&, uint64 guid);
static Player* GetPlayer(WorldObject const&, uint64 guid);
static Creature* GetCreatureOrPetOrVehicle(WorldObject const&, uint64);
2009-10-17 15:51:44 -07:00
// these functions return objects if found in whole world
// ACCESS LIKE THAT IS NOT THREAD SAFE
static Pet * FindPet(uint64);
static Player* FindPlayer(uint64);
static Unit* FindUnit(uint64);
Player* FindPlayerByName(const char* name);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// when using this, you must use the hashmapholder's lock
2009-03-31 19:32:04 -04:00
HashMapHolder<Player>::MapType& GetPlayers()
{
return HashMapHolder<Player>::GetContainer();
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// when using this, you must use the hashmapholder's lock
2009-03-31 19:32:04 -04:00
HashMapHolder<Creature>::MapType& GetCreatures()
{
return HashMapHolder<Creature>::GetContainer();
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// when using this, you must use the hashmapholder's lock
2009-03-31 19:32:04 -04:00
HashMapHolder<GameObject>::MapType& GetGameObjects()
{
return HashMapHolder<GameObject>::GetContainer();
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
template<class T> void AddObject(T* object)
2008-10-02 16:23:55 -05:00
{
HashMapHolder<T>::Insert(object);
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
template<class T> void RemoveObject(T* object)
2008-10-02 16:23:55 -05:00
{
HashMapHolder<T>::Remove(object);
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void RemoveObject(Player* pl)
2008-10-02 16:23:55 -05:00
{
HashMapHolder<Player>::Remove(pl);
2010-03-13 16:17:25 +01:00
RemoveUpdateObject((Object*)pl);
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void SaveAllPlayers();
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void AddUpdateObject(Object* obj)
2009-01-20 19:59:43 -06:00
{
ACE_GUARD(LockType, Guard, i_updateGuard);
2009-01-20 19:59:43 -06:00
i_objects.insert(obj);
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void RemoveUpdateObject(Object* obj)
2009-01-20 19:59:43 -06:00
{
ACE_GUARD(LockType, Guard, i_updateGuard);
2010-04-07 22:59:46 +02:00
i_objects.erase(obj);
2009-01-20 19:59:43 -06:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void Update(uint32 diff);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Corpse* GetCorpseForPlayerGUID(uint64 guid);
2010-03-13 16:17:25 +01:00
void RemoveCorpse(Corpse* corpse);
void AddCorpse(Corpse* corpse);
void AddCorpsesToGrid(GridPair const& gridpair, GridType& grid, Map* map);
Corpse* ConvertCorpseForPlayer(uint64 player_guid, bool insignia = false);
2010-08-06 00:15:29 +06:00
void RemoveOldCorpses();
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
typedef ACE_Thread_Mutex LockType;
2008-10-02 16:23:55 -05:00
private:
2010-03-13 16:17:25 +01:00
Player2CorpsesMapType i_player2corpse;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
static void _buildChangeObjectForPlayer(WorldObject*, UpdateDataMapType&);
static void _buildPacket(Player*, Object*, UpdateDataMapType&);
void _update();
std::set<Object*> i_objects;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
LockType i_updateGuard;
LockType i_corpseGuard;
};
#define sObjectAccessor ACE_Singleton<ObjectAccessor, ACE_Thread_Mutex>::instance()
2008-10-02 16:23:55 -05:00
#endif