[9290] Some cleanups in realmd, no functional changes
[getmangos.git] / src / game / ObjectAccessor.cpp
blobaf35512e425c15bfbdb75572504223a7fba86f35
1 /*
2 * Copyright (C) 2005-2010 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 #include "ObjectAccessor.h"
20 #include "ObjectMgr.h"
21 #include "Policies/SingletonImp.h"
22 #include "Player.h"
23 #include "Creature.h"
24 #include "GameObject.h"
25 #include "DynamicObject.h"
26 #include "Vehicle.h"
27 #include "WorldPacket.h"
28 #include "Item.h"
29 #include "Corpse.h"
30 #include "GridNotifiers.h"
31 #include "MapManager.h"
32 #include "Map.h"
33 #include "CellImpl.h"
34 #include "GridNotifiersImpl.h"
35 #include "Opcodes.h"
36 #include "ObjectDefines.h"
37 #include "MapInstanced.h"
38 #include "World.h"
40 #include <cmath>
42 #define CLASS_LOCK MaNGOS::ClassLevelLockable<ObjectAccessor, ACE_Thread_Mutex>
43 INSTANTIATE_SINGLETON_2(ObjectAccessor, CLASS_LOCK);
44 INSTANTIATE_CLASS_MUTEX(ObjectAccessor, ACE_Thread_Mutex);
46 ObjectAccessor::ObjectAccessor() {}
47 ObjectAccessor::~ObjectAccessor()
49 for(Player2CorpsesMapType::const_iterator itr = i_player2corpse.begin(); itr != i_player2corpse.end(); ++itr)
51 itr->second->RemoveFromWorld();
52 delete itr->second;
56 Unit*
57 ObjectAccessor::GetUnit(WorldObject const &u, uint64 guid)
59 if(!guid)
60 return NULL;
62 if(IS_PLAYER_GUID(guid))
63 return FindPlayer(guid);
65 if (!u.IsInWorld())
66 return NULL;
68 return u.GetMap()->GetCreatureOrPetOrVehicle(guid);
71 Corpse* ObjectAccessor::GetCorpseInMap( uint64 guid, uint32 mapid )
73 Corpse * ret = HashMapHolder<Corpse>::Find(guid);
74 if(!ret)
75 return NULL;
76 if(ret->GetMapId() != mapid)
77 return NULL;
79 return ret;
82 Object* ObjectAccessor::GetObjectByTypeMask(WorldObject const &p, uint64 guid, uint32 typemask)
84 switch(GUID_HIPART(guid))
86 case HIGHGUID_ITEM:
87 if(typemask & TYPEMASK_ITEM && p.GetTypeId() == TYPEID_PLAYER)
88 return ((Player const &)p).GetItemByGuid( guid );
89 break;
90 case HIGHGUID_PLAYER:
91 if(typemask & TYPEMASK_PLAYER)
92 return FindPlayer(guid);
93 break;
94 case HIGHGUID_GAMEOBJECT:
95 if(typemask & TYPEMASK_GAMEOBJECT)
96 return p.GetMap()->GetGameObject(guid);
97 break;
98 case HIGHGUID_UNIT:
99 if(typemask & TYPEMASK_UNIT)
100 return p.GetMap()->GetCreature(guid);
101 break;
102 case HIGHGUID_PET:
103 if(typemask & TYPEMASK_UNIT)
104 return p.GetMap()->GetPet(guid);
105 break;
106 case HIGHGUID_VEHICLE:
107 if(typemask & TYPEMASK_UNIT)
108 return p.GetMap()->GetVehicle(guid);
109 break;
110 case HIGHGUID_DYNAMICOBJECT:
111 if(typemask & TYPEMASK_DYNAMICOBJECT)
112 return p.GetMap()->GetDynamicObject(guid);
113 break;
114 case HIGHGUID_TRANSPORT:
115 case HIGHGUID_CORPSE:
116 case HIGHGUID_MO_TRANSPORT:
117 break;
120 return NULL;
123 Player*
124 ObjectAccessor::FindPlayer(uint64 guid)
126 Player * plr = HashMapHolder<Player>::Find(guid);;
127 if(!plr || !plr->IsInWorld())
128 return NULL;
130 return plr;
133 Player*
134 ObjectAccessor::FindPlayerByName(const char *name)
136 //TODO: Player Guard
137 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
138 HashMapHolder<Player>::MapType::iterator iter = m.begin();
139 for(; iter != m.end(); ++iter)
140 if(iter->second->IsInWorld() && ( ::strcmp(name, iter->second->GetName()) == 0 ))
141 return iter->second;
142 return NULL;
145 void
146 ObjectAccessor::SaveAllPlayers()
148 Guard guard(*HashMapHolder<Player>::GetLock());
149 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
150 HashMapHolder<Player>::MapType::iterator itr = m.begin();
151 for(; itr != m.end(); ++itr)
152 itr->second->SaveToDB();
155 void ObjectAccessor::KickPlayer(uint64 guid)
157 if (Player* p = HashMapHolder<Player>::Find(guid))
159 WorldSession* s = p->GetSession();
160 s->KickPlayer(); // mark session to remove at next session list update
161 s->LogoutPlayer(false); // logout player without waiting next session list update
165 Corpse*
166 ObjectAccessor::GetCorpseForPlayerGUID(uint64 guid)
168 Guard guard(i_corpseGuard);
170 Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid);
171 if( iter == i_player2corpse.end() ) return NULL;
173 assert(iter->second->GetType() != CORPSE_BONES);
175 return iter->second;
178 void
179 ObjectAccessor::RemoveCorpse(Corpse *corpse)
181 assert(corpse && corpse->GetType() != CORPSE_BONES);
183 Guard guard(i_corpseGuard);
184 Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
185 if( iter == i_player2corpse.end() )
186 return;
188 // build mapid*cellid -> guid_set map
189 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
190 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
192 sObjectMgr.DeleteCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID());
193 corpse->RemoveFromWorld();
195 i_player2corpse.erase(iter);
198 void
199 ObjectAccessor::AddCorpse(Corpse *corpse)
201 assert(corpse && corpse->GetType() != CORPSE_BONES);
203 Guard guard(i_corpseGuard);
204 assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
205 i_player2corpse[corpse->GetOwnerGUID()] = corpse;
207 // build mapid*cellid -> guid_set map
208 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
209 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
211 sObjectMgr.AddCorpseCellData(corpse->GetMapId(), cell_id, corpse->GetOwnerGUID(), corpse->GetInstanceId());
214 void
215 ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
217 Guard guard(i_corpseGuard);
218 for(Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
219 if(iter->second->GetGrid() == gridpair)
221 // verify, if the corpse in our instance (add only corpses which are)
222 if (map->Instanceable())
224 if (iter->second->GetInstanceId() == map->GetInstanceId())
226 grid.AddWorldObject(iter->second, iter->second->GetGUID());
229 else
231 grid.AddWorldObject(iter->second, iter->second->GetGUID());
236 Corpse*
237 ObjectAccessor::ConvertCorpseForPlayer(uint64 player_guid, bool insignia)
239 Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
240 if(!corpse)
242 //in fact this function is called from several places
243 //even when player doesn't have a corpse, not an error
244 //sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
245 return NULL;
248 DEBUG_LOG("Deleting Corpse and spawning bones.");
250 // remove corpse from player_guid -> corpse map
251 RemoveCorpse(corpse);
253 // remove resurrectable corpse from grid object registry (loaded state checked into call)
254 // do not load the map if it's not loaded
255 Map *map = sMapMgr.FindMap(corpse->GetMapId(), corpse->GetInstanceId());
256 if(map)
257 map->Remove(corpse, false);
259 // remove corpse from DB
260 corpse->DeleteFromDB();
262 Corpse *bones = NULL;
263 // create the bones only if the map and the grid is loaded at the corpse's location
264 // ignore bones creating option in case insignia
265 if (map && (insignia ||
266 (map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_DEATH_BONES_WORLD))) &&
267 !map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
269 // Create bones, don't change Corpse
270 bones = new Corpse;
271 bones->Create(corpse->GetGUIDLow());
273 for (int i = 3; i < CORPSE_END; ++i) // don't overwrite guid and object type
274 bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
276 bones->SetGrid(corpse->GetGrid());
277 // bones->m_time = m_time; // don't overwrite time
278 // bones->m_inWorld = m_inWorld; // don't overwrite world state
279 // bones->m_type = m_type; // don't overwrite type
280 bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
281 bones->SetPhaseMask(corpse->GetPhaseMask(), false);
283 bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
284 bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
286 for (int i = 0; i < EQUIPMENT_SLOT_END; ++i)
288 if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
289 bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
292 // add bones in grid store if grid loaded where corpse placed
293 map->Add(bones);
296 // all references to the corpse should be removed at this point
297 delete corpse;
299 return bones;
302 /// Define the static member of HashMapHolder
304 template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
305 template <class T> ACE_Thread_Mutex HashMapHolder<T>::i_lock;
307 /// Global definitions for the hashmap storage
309 template class HashMapHolder<Player>;
310 template class HashMapHolder<Corpse>;
312 /// Define the static member of ObjectAccessor
313 std::list<Map*> ObjectAccessor::i_mapList;