[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / ObjectAccessor.cpp
blobd2330fc789d1bf8097690c9ad9029f4366f28a39
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 "ObjectGuid.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, ObjectGuid guid)
59 if(guid.IsEmpty())
60 return NULL;
62 if(guid.IsPlayer())
63 return FindPlayer(guid);
65 if (!u.IsInWorld())
66 return NULL;
68 return u.GetMap()->GetCreatureOrPetOrVehicle(guid);
71 Corpse* ObjectAccessor::GetCorpseInMap(ObjectGuid 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 Player*
83 ObjectAccessor::FindPlayer(ObjectGuid guid)
85 Player * plr = HashMapHolder<Player>::Find(guid);;
86 if(!plr || !plr->IsInWorld())
87 return NULL;
89 return plr;
92 Player*
93 ObjectAccessor::FindPlayerByName(const char *name)
95 //TODO: Player Guard
96 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
97 HashMapHolder<Player>::MapType::iterator iter = m.begin();
98 for(; iter != m.end(); ++iter)
99 if(iter->second->IsInWorld() && ( ::strcmp(name, iter->second->GetName()) == 0 ))
100 return iter->second;
101 return NULL;
104 void
105 ObjectAccessor::SaveAllPlayers()
107 Guard guard(*HashMapHolder<Player>::GetLock());
108 HashMapHolder<Player>::MapType& m = HashMapHolder<Player>::GetContainer();
109 HashMapHolder<Player>::MapType::iterator itr = m.begin();
110 for(; itr != m.end(); ++itr)
111 itr->second->SaveToDB();
114 void ObjectAccessor::KickPlayer(uint64 guid)
116 if (Player* p = HashMapHolder<Player>::Find(guid))
118 WorldSession* s = p->GetSession();
119 s->KickPlayer(); // mark session to remove at next session list update
120 s->LogoutPlayer(false); // logout player without waiting next session list update
124 Corpse*
125 ObjectAccessor::GetCorpseForPlayerGUID(ObjectGuid guid)
127 Guard guard(i_corpseGuard);
129 Player2CorpsesMapType::iterator iter = i_player2corpse.find(guid.GetRawValue());
130 if( iter == i_player2corpse.end() ) return NULL;
132 assert(iter->second->GetType() != CORPSE_BONES);
134 return iter->second;
137 void
138 ObjectAccessor::RemoveCorpse(Corpse *corpse)
140 assert(corpse && corpse->GetType() != CORPSE_BONES);
142 Guard guard(i_corpseGuard);
143 Player2CorpsesMapType::iterator iter = i_player2corpse.find(corpse->GetOwnerGUID());
144 if( iter == i_player2corpse.end() )
145 return;
147 // build mapid*cellid -> guid_set map
148 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
149 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
151 sObjectMgr.DeleteCorpseCellData(corpse->GetMapId(), cell_id, GUID_LOPART(corpse->GetOwnerGUID()));
152 corpse->RemoveFromWorld();
154 i_player2corpse.erase(iter);
157 void
158 ObjectAccessor::AddCorpse(Corpse *corpse)
160 assert(corpse && corpse->GetType() != CORPSE_BONES);
162 Guard guard(i_corpseGuard);
163 assert(i_player2corpse.find(corpse->GetOwnerGUID()) == i_player2corpse.end());
164 i_player2corpse[corpse->GetOwnerGUID()] = corpse;
166 // build mapid*cellid -> guid_set map
167 CellPair cell_pair = MaNGOS::ComputeCellPair(corpse->GetPositionX(), corpse->GetPositionY());
168 uint32 cell_id = (cell_pair.y_coord*TOTAL_NUMBER_OF_CELLS_PER_MAP) + cell_pair.x_coord;
170 sObjectMgr.AddCorpseCellData(corpse->GetMapId(), cell_id, GUID_LOPART(corpse->GetOwnerGUID()), corpse->GetInstanceId());
173 void
174 ObjectAccessor::AddCorpsesToGrid(GridPair const& gridpair,GridType& grid,Map* map)
176 Guard guard(i_corpseGuard);
177 for(Player2CorpsesMapType::iterator iter = i_player2corpse.begin(); iter != i_player2corpse.end(); ++iter)
178 if(iter->second->GetGrid() == gridpair)
180 // verify, if the corpse in our instance (add only corpses which are)
181 if (map->Instanceable())
183 if (iter->second->GetInstanceId() == map->GetInstanceId())
185 grid.AddWorldObject(iter->second);
188 else
190 grid.AddWorldObject(iter->second);
195 Corpse*
196 ObjectAccessor::ConvertCorpseForPlayer(ObjectGuid player_guid, bool insignia)
198 Corpse *corpse = GetCorpseForPlayerGUID(player_guid);
199 if(!corpse)
201 //in fact this function is called from several places
202 //even when player doesn't have a corpse, not an error
203 //sLog.outError("Try remove corpse that not in map for GUID %ul", player_guid);
204 return NULL;
207 DEBUG_LOG("Deleting Corpse and spawning bones.");
209 // remove corpse from player_guid -> corpse map
210 RemoveCorpse(corpse);
212 // remove resurrectable corpse from grid object registry (loaded state checked into call)
213 // do not load the map if it's not loaded
214 Map *map = sMapMgr.FindMap(corpse->GetMapId(), corpse->GetInstanceId());
215 if(map)
216 map->Remove(corpse, false);
218 // remove corpse from DB
219 corpse->DeleteFromDB();
221 Corpse *bones = NULL;
222 // create the bones only if the map and the grid is loaded at the corpse's location
223 // ignore bones creating option in case insignia
224 if (map && (insignia ||
225 (map->IsBattleGroundOrArena() ? sWorld.getConfig(CONFIG_BOOL_DEATH_BONES_BG_OR_ARENA) : sWorld.getConfig(CONFIG_BOOL_DEATH_BONES_WORLD))) &&
226 !map->IsRemovalGrid(corpse->GetPositionX(), corpse->GetPositionY()))
228 // Create bones, don't change Corpse
229 bones = new Corpse;
230 bones->Create(corpse->GetGUIDLow());
232 for (int i = 3; i < CORPSE_END; ++i) // don't overwrite guid and object type
233 bones->SetUInt32Value(i, corpse->GetUInt32Value(i));
235 bones->SetGrid(corpse->GetGrid());
236 // bones->m_time = m_time; // don't overwrite time
237 // bones->m_inWorld = m_inWorld; // don't overwrite world state
238 // bones->m_type = m_type; // don't overwrite type
239 bones->Relocate(corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetOrientation());
240 bones->SetPhaseMask(corpse->GetPhaseMask(), false);
242 bones->SetUInt32Value(CORPSE_FIELD_FLAGS, CORPSE_FLAG_UNK2 | CORPSE_FLAG_BONES);
243 bones->SetUInt64Value(CORPSE_FIELD_OWNER, 0);
245 for (int i = 0; i < EQUIPMENT_SLOT_END; ++i)
247 if(corpse->GetUInt32Value(CORPSE_FIELD_ITEM + i))
248 bones->SetUInt32Value(CORPSE_FIELD_ITEM + i, 0);
251 // add bones in grid store if grid loaded where corpse placed
252 map->Add(bones);
255 // all references to the corpse should be removed at this point
256 delete corpse;
258 return bones;
261 /// Define the static member of HashMapHolder
263 template <class T> UNORDERED_MAP< uint64, T* > HashMapHolder<T>::m_objectMap;
264 template <class T> ACE_Thread_Mutex HashMapHolder<T>::i_lock;
266 /// Global definitions for the hashmap storage
268 template class HashMapHolder<Player>;
269 template class HashMapHolder<Corpse>;
271 /// Define the static member of ObjectAccessor
272 std::list<Map*> ObjectAccessor::i_mapList;