Files
TCPlayerbotCore/src/game/ObjectAccessor.cpp
T

374 lines
12 KiB
C++
Raw Normal View History

2008-10-02 16:23:55 -05:00
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2010-01-16 20:19:18 +03:00
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-10-02 16:23:55 -05:00
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Policies/SingletonImp.h"
#include "Player.h"
#include "Creature.h"
#include "GameObject.h"
#include "DynamicObject.h"
#include "Vehicle.h"
2008-10-02 16:23:55 -05:00
#include "WorldPacket.h"
#include "Item.h"
#include "Corpse.h"
#include "GridNotifiers.h"
#include "MapManager.h"
#include "Map.h"
#include "CellImpl.h"
#include "GridNotifiersImpl.h"
#include "Opcodes.h"
#include "ObjectDefines.h"
#include "MapInstanced.h"
#include "World.h"
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
#include <cmath>
2009-10-17 15:51:44 -07:00
2009-12-20 15:20:04 +01:00
#define CLASS_LOCK Trinity::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex>
2008-10-02 16:23:55 -05:00
INSTANTIATE_SINGLETON_2(ObjectAccessor, CLASS_LOCK);
INSTANTIATE_CLASS_MUTEX(ObjectAccessor, ACE_Thread_Mutex);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
ObjectAccessor::ObjectAccessor()
{
}
ObjectAccessor::~ObjectAccessor()
{
2009-10-17 16:20:24 -07:00
for (Player2CorpsesMapType::const_iterator itr = i_player2corpse.begin(); itr != i_player2corpse.end(); ++itr)
2009-12-17 11:40:23 +01:00
{
itr->second->RemoveFromWorld();
delete itr->second;
2009-12-17 11:40:23 +01:00
}
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Creature* ObjectAccessor::GetCreatureOrPetOrVehicle(WorldObject const& u, uint64 guid)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
if (IS_PLAYER_GUID(guid))
return NULL;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
if (IS_PET_GUID(guid))
return GetPet(guid);
2009-10-17 15:51:44 -07:00
return u.IsInWorld() ? u.GetMap()->GetCreature(guid) : NULL;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Corpse* ObjectAccessor::GetCorpse(WorldObject const& u, uint64 guid)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
Corpse* ret = GetObjectInWorld(guid, (Corpse*)NULL);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
if (!ret)
2009-01-25 16:35:14 -06:00
return NULL;
2010-03-13 16:17:25 +01:00
if (ret->GetMapId() != u.GetMapId())
2009-01-29 19:33:39 -06:00
return NULL;
2010-03-13 16:17:25 +01:00
if (ret->GetInstanceId() != u.GetInstanceId())
2009-01-25 16:35:14 -06:00
return NULL;
2010-03-13 16:17:25 +01:00
2008-10-02 16:23:55 -05:00
return ret;
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
WorldObject* ObjectAccessor::GetWorldObject(WorldObject const& p, uint64 guid)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
switch (GUID_HIPART(guid))
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
case HIGHGUID_PLAYER: return FindPlayer(guid);
case HIGHGUID_GAMEOBJECT: return p.GetMap()->GetGameObject(guid);
case HIGHGUID_VEHICLE:
2010-03-13 16:17:25 +01:00
case HIGHGUID_UNIT: return p.GetMap()->GetCreature(guid);
case HIGHGUID_PET: return GetPet(guid);
case HIGHGUID_DYNAMICOBJECT: return p.GetMap()->GetDynamicObject(guid);
case HIGHGUID_TRANSPORT: return NULL;
case HIGHGUID_CORPSE: return GetCorpse(p,guid);
case HIGHGUID_MO_TRANSPORT: return NULL;
default: return NULL;
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const& p, uint64 guid, uint32 typemask)
{
2010-03-13 16:17:25 +01:00
switch (GUID_HIPART(guid))
2008-10-02 16:23:55 -05:00
{
case HIGHGUID_ITEM:
2010-03-13 16:17:25 +01:00
if (typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
return ((Player const&)p).GetItemByGuid(guid);
break;
case HIGHGUID_PLAYER:
if(typemask & TYPEMASK_PLAYER)
return FindPlayer(guid);
break;
case HIGHGUID_GAMEOBJECT:
if(typemask & TYPEMASK_GAMEOBJECT)
return p.GetMap()->GetGameObject(guid);
break;
case HIGHGUID_UNIT:
case HIGHGUID_VEHICLE:
if(typemask & TYPEMASK_UNIT)
return p.GetMap()->GetCreature(guid);
break;
case HIGHGUID_PET:
if(typemask & TYPEMASK_UNIT)
return GetPet(guid);
break;
case HIGHGUID_DYNAMICOBJECT:
if(typemask & TYPEMASK_DYNAMICOBJECT)
return p.GetMap()->GetDynamicObject(guid);
break;
case HIGHGUID_TRANSPORT:
case HIGHGUID_CORPSE:
case HIGHGUID_MO_TRANSPORT:
break;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
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
Player* ObjectAccessor::FindPlayer(uint64 guid)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
Player* plr = GetObjectInWorld(guid, (Player*)NULL);
if (!plr || !plr->IsInWorld())
return NULL;
2009-10-17 15:51:44 -07:00
return plr;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Player* ObjectAccessor::FindPlayerByName(const char* name)
2008-10-02 16:23:55 -05:00
{
2009-12-17 07:34:00 +01:00
Guard guard(*HashMapHolder<Player>::GetLock());
2008-10-02 16:23:55 -05:00
HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
2010-03-13 16:17:25 +01:00
for (HashMapHolder<Player>::MapType::iterator iter = m.begin(); iter != m.end(); ++iter)
if (iter->second->IsInWorld() && strcmp(name, iter->second->GetName()) == 0)
2008-10-02 16:23:55 -05:00
return iter->second;
2010-03-13 16:17:25 +01:00
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
void ObjectAccessor::SaveAllPlayers()
2008-10-02 16:23:55 -05:00
{
2009-10-23 16:03:22 +02:00
Guard guard(*HashMapHolder<Player>::GetLock());
2008-10-02 16:23:55 -05:00
HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
2010-03-13 16:17:25 +01:00
for (HashMapHolder<Player>::MapType::iterator itr = m.begin(); itr != m.end(); ++itr)
2008-10-02 16:23:55 -05:00
itr->second->SaveToDB();
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Pet* ObjectAccessor::GetPet(uint64 guid)
2008-10-02 16:23:55 -05:00
{
return GetObjectInWorld(guid, (Pet*)NULL);
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Corpse* ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
2008-10-02 16:23:55 -05:00
{
Guard guard(i_corpseGuard);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
2010-03-13 16:17:25 +01:00
if (iter == i_player2corpse.end())
return NULL;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
assert(iter->second->GetType() != CORPSE_BONES);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return iter->second;
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void ObjectAccessor::RemoveCorpse(Corpse* corpse)
2008-10-02 16:23:55 -05:00
{
assert(corpse && corpse->GetType() != CORPSE_BONES);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
if (corpse->FindMap())
2009-08-13 21:36:26 -05:00
corpse->FindMap()->Remove(corpse, false);
else
corpse->RemoveFromWorld();
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// Critical section
{
Guard guard(i_corpseGuard);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
if (iter == i_player2corpse.end()) // TODO: Fix this
return;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
objmgr.DeleteCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID());
i_player2corpse.erase(iter);
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void ObjectAccessor::AddCorpse(Corpse* corpse)
2008-10-02 16:23:55 -05:00
{
assert(corpse && corpse->GetType() != CORPSE_BONES);
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// Critical section
{
Guard guard(i_corpseGuard);
assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
i_player2corpse[corpse->GetOwnerGUID()] = corpse;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
// build mapid*cellid -> guid_set map
CellPair cell_pair = Trinity::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
uint32 cell_id = (cell_pair.y_coord * TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
objmgr.AddCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID(), corpse->GetInstanceId());
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair, GridType& grid, Map* map)
2008-10-02 16:23:55 -05:00
{
Guard guard(i_corpseGuard);
2010-03-13 16:17:25 +01:00
2009-10-17 16:20:24 -07:00
for (Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
if (iter->second->GetGrid() == gridpair)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
// verify, if the corpse in our instance (add only corpses which are)
if (map->Instanceable())
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
if (iter->second->GetInstanceId() == map->GetInstanceId())
grid.AddWorldObject(iter->second);
2008-10-02 16:23:55 -05:00
}
2010-03-13 16:17:25 +01:00
else
grid.AddWorldObject(iter->second);
2008-10-02 16:23:55 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Corpse* ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
Corpse* corpse = GetCorpseForPlayerGUID(player_guid);
2008-10-02 16:23:55 -05:00
if(!corpse)
{
//in fact this function is called from several places
//even when player doesn't have a corpse, not an error
2010-03-13 16:17:25 +01:00
// TODO: really, now...
//sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
2008-10-02 16:23:55 -05:00
return NULL;
}
2009-10-17 15:51:44 -07:00
DEBUG_LOG("Deleting Corpse and spawning bones.");
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
Map* map = corpse->FindMap();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// remove corpse from player_guid -> corpse map
RemoveCorpse(corpse);
2009-10-17 15:51:44 -07:00
2009-08-13 11:35:23 -05:00
// done in removecorpse
2009-06-11 00:45:59 -05:00
// remove resurrectable corpse from grid object registry (loaded state checked into call)
2008-10-02 16:23:55 -05:00
// do not load the map if it's not loaded
2009-08-13 11:35:23 -05:00
//Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId());
//if(map)
// map->Remove(corpse, false);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// remove corpse from DB
corpse->DeleteFromDB();
2009-10-17 15:51:44 -07:00
2009-12-17 11:40:23 +01:00
// we don't want bones to save some cpu.. :)
delete corpse;
return NULL;
2010-03-13 16:17:25 +01:00
Corpse* bones = NULL;
2008-10-02 16:23:55 -05:00
// create the bones only if the map and the grid is loaded at the corpse's location
// ignore bones creating option in case insignia
if (map && (insignia ||
(map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_DEATH_BONES_WORLD))) &&
!map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
2008-10-02 16:23:55 -05:00
{
// Create bones, don't change Corpse
bones = new Corpse;
bones->Create(corpse->GetGUIDLow(), map);
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
for (int i = 3; i < CORPSE_END; ++i) // don't overwrite guid and object type
2008-10-02 16:23:55 -05:00
bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bones->SetGrid(corpse->GetGrid());
// bones->m_time = m_time; // don't overwrite time
2010-03-13 16:17:25 +01:00
// bones->m_inWorld = m_inWorld; // don't overwrite in-world state
2008-10-02 16:23:55 -05:00
// bones->m_type = m_type; // don't overwrite type
bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
2009-06-11 00:45:59 -05:00
bones->SetPhaseMask(corpse->GetPhaseMask(), false);
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
2009-10-17 15:51:44 -07:00
2009-06-11 00:45:59 -05:00
for (int i = 0; i < EQUIPMENT_SLOT_END; ++i)
2008-10-02 16:23:55 -05:00
{
2010-03-13 16:17:25 +01:00
if (corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
2008-10-02 16:23:55 -05:00
bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// add bones in grid store if grid loaded where corpse placed
map->Add(bones);
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// all references to the corpse should be removed at this point
delete corpse;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return bones;
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
void ObjectAccessor::Update(uint32 diff)
2008-10-02 16:23:55 -05:00
{
UpdateDataMapType update_players;
2010-03-13 16:17:25 +01:00
// Critical section
{
Guard guard(i_updateGuard);
2010-03-13 16:17:25 +01:00
while (!i_objects.empty())
{
Object* obj = *i_objects.begin();
2009-04-01 21:17:59 -06:00
assert(obj && obj->IsInWorld());
i_objects.erase(i_objects.begin());
obj->BuildUpdate(update_players);
}
}
2009-10-17 15:51:44 -07:00
WorldPacket packet; // here we allocate a std::vector with a size of 0x10000
2009-10-17 16:20:24 -07:00
for (UpdateDataMapType::iterator iter = update_players.begin(); iter != update_players.end(); ++iter)
{
iter->second.BuildPacket(&packet);
iter->first->GetSession()->SendPacket(&packet);
packet.clear(); // clean the string
}
}
2009-10-17 15:51:44 -07:00
2010-03-13 16:17:25 +01:00
/// Define the static members of HashMapHolder
2009-10-17 15:51:44 -07:00
template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
template <class T> ACE_Thread_Mutex HashMapHolder<T>::i_lock;
2009-10-17 15:51:44 -07:00
2008-12-12 11:21:28 -06:00
/// Global definitions for the hashmap storage
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template class HashMapHolder<Player>;
template class HashMapHolder<Pet>;
template class HashMapHolder<GameObject>;
template class HashMapHolder<DynamicObject>;
template class HashMapHolder<Creature>;
template class HashMapHolder<Corpse>;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template Player* ObjectAccessor::GetObjectInWorld<Player>(uint32 mapid, float x, float y, uint64 guid, Player* /*fake*/);
template Pet* ObjectAccessor::GetObjectInWorld<Pet>(uint32 mapid, float x, float y, uint64 guid, Pet* /*fake*/);
template Creature* ObjectAccessor::GetObjectInWorld<Creature>(uint32 mapid, float x, float y, uint64 guid, Creature* /*fake*/);
template Corpse* ObjectAccessor::GetObjectInWorld<Corpse>(uint32 mapid, float x, float y, uint64 guid, Corpse* /*fake*/);
template GameObject* ObjectAccessor::GetObjectInWorld<GameObject>(uint32 mapid, float x, float y, uint64 guid, GameObject* /*fake*/);
template DynamicObject* ObjectAccessor::GetObjectInWorld<DynamicObject>(uint32 mapid, float x, float y, uint64 guid, DynamicObject* /*fake*/);