[2] | 1 | /* |
---|
[102] | 2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
| 3 | * |
---|
[44] | 4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
[2] | 5 | * |
---|
| 6 | * This program is free software; you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[44] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
[2] | 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program; if not, write to the Free Software |
---|
[44] | 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
[2] | 19 | */ |
---|
| 20 | |
---|
| 21 | #include "MapManager.h" |
---|
| 22 | #include "InstanceSaveMgr.h" |
---|
| 23 | #include "Policies/SingletonImp.h" |
---|
| 24 | #include "Database/DatabaseEnv.h" |
---|
| 25 | #include "Log.h" |
---|
| 26 | #include "ObjectAccessor.h" |
---|
| 27 | #include "Transports.h" |
---|
| 28 | #include "GridDefines.h" |
---|
| 29 | #include "MapInstanced.h" |
---|
| 30 | #include "DestinationHolderImp.h" |
---|
| 31 | #include "World.h" |
---|
| 32 | #include "CellImpl.h" |
---|
| 33 | #include "Corpse.h" |
---|
| 34 | #include "ObjectMgr.h" |
---|
| 35 | |
---|
[44] | 36 | #define CLASS_LOCK Trinity::ClassLevelLockable<MapManager, ZThread::Mutex> |
---|
[2] | 37 | INSTANTIATE_SINGLETON_2(MapManager, CLASS_LOCK); |
---|
| 38 | INSTANTIATE_CLASS_MUTEX(MapManager, ZThread::Mutex); |
---|
| 39 | |
---|
| 40 | extern GridState* si_GridStates[]; // debugging code, should be deleted some day |
---|
| 41 | |
---|
| 42 | MapManager::MapManager() : i_gridCleanUpDelay(sWorld.getConfig(CONFIG_INTERVAL_GRIDCLEAN)) |
---|
| 43 | { |
---|
| 44 | i_timer.SetInterval(sWorld.getConfig(CONFIG_INTERVAL_MAPUPDATE)); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | MapManager::~MapManager() |
---|
| 48 | { |
---|
| 49 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
| 50 | delete iter->second; |
---|
| 51 | |
---|
| 52 | for(TransportSet::iterator i = m_Transports.begin(); i != m_Transports.end(); ++i) |
---|
| 53 | delete *i; |
---|
| 54 | |
---|
| 55 | Map::DeleteStateMachine(); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | void |
---|
| 59 | MapManager::Initialize() |
---|
| 60 | { |
---|
| 61 | Map::InitStateMachine(); |
---|
| 62 | |
---|
| 63 | // debugging code, should be deleted some day |
---|
| 64 | { |
---|
| 65 | for(int i=0;i<MAX_GRID_STATE; i++) |
---|
| 66 | { |
---|
| 67 | i_GridStates[i] = si_GridStates[i]; |
---|
| 68 | } |
---|
| 69 | i_GridStateErrorCount = 0; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | InitMaxInstanceId(); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | // debugging code, should be deleted some day |
---|
| 76 | void MapManager::checkAndCorrectGridStatesArray() |
---|
| 77 | { |
---|
| 78 | bool ok = true; |
---|
| 79 | for(int i=0;i<MAX_GRID_STATE; i++) |
---|
| 80 | { |
---|
| 81 | if(i_GridStates[i] != si_GridStates[i]) |
---|
| 82 | { |
---|
| 83 | sLog.outError("MapManager::checkGridStates(), GridState: si_GridStates is currupt !!!"); |
---|
| 84 | ok = false; |
---|
| 85 | si_GridStates[i] = i_GridStates[i]; |
---|
| 86 | } |
---|
[44] | 87 | #ifdef TRINITY_DEBUG |
---|
[2] | 88 | // inner class checking only when compiled with debug |
---|
| 89 | if(!si_GridStates[i]->checkMagic()) |
---|
| 90 | { |
---|
| 91 | ok = false; |
---|
| 92 | si_GridStates[i]->setMagic(); |
---|
| 93 | } |
---|
| 94 | #endif |
---|
| 95 | } |
---|
| 96 | if(!ok) |
---|
| 97 | ++i_GridStateErrorCount; |
---|
| 98 | if(i_GridStateErrorCount > 2) |
---|
| 99 | assert(false); // force a crash. Too many errors |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | Map* |
---|
| 103 | MapManager::_GetBaseMap(uint32 id) |
---|
| 104 | { |
---|
| 105 | Map *m = _findMap(id); |
---|
| 106 | |
---|
| 107 | if( m == NULL ) |
---|
| 108 | { |
---|
| 109 | Guard guard(*this); |
---|
| 110 | |
---|
| 111 | const MapEntry* entry = sMapStore.LookupEntry(id); |
---|
[9] | 112 | if (entry && entry->Instanceable()) |
---|
[2] | 113 | { |
---|
| 114 | m = new MapInstanced(id, i_gridCleanUpDelay, 0); |
---|
| 115 | } |
---|
| 116 | else |
---|
| 117 | { |
---|
| 118 | m = new Map(id, i_gridCleanUpDelay, 0, 0); |
---|
| 119 | } |
---|
| 120 | i_maps[id] = m; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | assert(m != NULL); |
---|
| 124 | return m; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | Map* MapManager::GetMap(uint32 id, const WorldObject* obj) |
---|
| 128 | { |
---|
| 129 | //if(!obj->IsInWorld()) sLog.outError("GetMap: called for map %d with object (typeid %d, guid %d, mapid %d, instanceid %d) who is not in world!", id, obj->GetTypeId(), obj->GetGUIDLow(), obj->GetMapId(), obj->GetInstanceId()); |
---|
| 130 | Map *m = _GetBaseMap(id); |
---|
| 131 | |
---|
| 132 | if (m && obj && m->Instanceable()) m = ((MapInstanced*)m)->GetInstance(obj); |
---|
| 133 | |
---|
| 134 | return m; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | Map* MapManager::FindMap(uint32 mapid, uint32 instanceId) |
---|
| 138 | { |
---|
| 139 | Map *map = FindMap(mapid); |
---|
| 140 | if(!map) return NULL; |
---|
| 141 | if(!map->Instanceable()) return instanceId == 0 ? map : NULL; |
---|
| 142 | return ((MapInstanced*)map)->FindMap(instanceId); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /* |
---|
| 146 | checks that do not require a map to be created |
---|
| 147 | will send transfer error messages on fail |
---|
| 148 | */ |
---|
| 149 | bool MapManager::CanPlayerEnter(uint32 mapid, Player* player) |
---|
| 150 | { |
---|
| 151 | const MapEntry *entry = sMapStore.LookupEntry(mapid); |
---|
| 152 | if(!entry) return false; |
---|
| 153 | const char *mapName = entry->name[player->GetSession()->GetSessionDbcLocale()]; |
---|
| 154 | |
---|
| 155 | if(entry->map_type == MAP_INSTANCE || entry->map_type == MAP_RAID) |
---|
| 156 | { |
---|
| 157 | if (entry->map_type == MAP_RAID) |
---|
| 158 | { |
---|
| 159 | // GMs can avoid raid limitations |
---|
| 160 | if(!player->isGameMaster() && !sWorld.getConfig(CONFIG_INSTANCE_IGNORE_RAID)) |
---|
| 161 | { |
---|
| 162 | // can only enter in a raid group |
---|
| 163 | Group* group = player->GetGroup(); |
---|
| 164 | if (!group || !group->isRaidGroup()) |
---|
| 165 | { |
---|
| 166 | // probably there must be special opcode, because client has this string constant in GlobalStrings.lua |
---|
| 167 | // TODO: this is not a good place to send the message |
---|
| 168 | player->GetSession()->SendAreaTriggerMessage("You must be in a raid group to enter %s instance", mapName); |
---|
| 169 | sLog.outDebug("MAP: Player '%s' must be in a raid group to enter instance of '%s'", player->GetName(), mapName); |
---|
| 170 | return false; |
---|
| 171 | } |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | //The player has a heroic mode and tries to enter into instance which has no a heroic mode |
---|
| 176 | if (!entry->SupportsHeroicMode() && player->GetDifficulty() == DIFFICULTY_HEROIC) |
---|
| 177 | { |
---|
| 178 | player->SendTransferAborted(mapid, TRANSFER_ABORT_DIFFICULTY2); //Send aborted message |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if (!player->isAlive()) |
---|
| 183 | { |
---|
| 184 | if(Corpse *corpse = player->GetCorpse()) |
---|
| 185 | { |
---|
| 186 | // let enter in ghost mode in instance that connected to inner instance with corpse |
---|
| 187 | uint32 instance_map = corpse->GetMapId(); |
---|
| 188 | do |
---|
| 189 | { |
---|
| 190 | if(instance_map==mapid) |
---|
| 191 | break; |
---|
| 192 | |
---|
| 193 | InstanceTemplate const* instance = objmgr.GetInstanceTemplate(instance_map); |
---|
| 194 | instance_map = instance ? instance->parent : 0; |
---|
| 195 | } |
---|
| 196 | while (instance_map); |
---|
| 197 | |
---|
| 198 | if (!instance_map) |
---|
| 199 | { |
---|
| 200 | player->GetSession()->SendAreaTriggerMessage("You cannot enter %s while in a ghost mode", mapName); |
---|
| 201 | sLog.outDebug("MAP: Player '%s' doesn't has a corpse in instance '%s' and can't enter", player->GetName(), mapName); |
---|
| 202 | return false; |
---|
| 203 | } |
---|
| 204 | sLog.outDebug("MAP: Player '%s' has corpse in instance '%s' and can enter", player->GetName(), mapName); |
---|
| 205 | } |
---|
| 206 | else |
---|
| 207 | { |
---|
| 208 | sLog.outDebug("Map::CanEnter - player '%s' is dead but doesn't have a corpse!", player->GetName()); |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
| 212 | // TODO: move this to a map dependent location |
---|
| 213 | /*if(i_data && i_data->IsEncounterInProgress()) |
---|
| 214 | { |
---|
| 215 | sLog.outDebug("MAP: Player '%s' can't enter instance '%s' while an encounter is in progress.", player->GetName(), GetMapName()); |
---|
| 216 | player->SendTransferAborted(GetId(), TRANSFER_ABORT_ZONE_IN_COMBAT); |
---|
| 217 | return(false); |
---|
| 218 | }*/ |
---|
| 219 | return true; |
---|
| 220 | } |
---|
| 221 | else |
---|
| 222 | return true; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | void MapManager::DeleteInstance(uint32 mapid, uint32 instanceId, uint8 mode) |
---|
| 226 | { |
---|
| 227 | Map *m = _GetBaseMap(mapid); |
---|
| 228 | if (m && m->Instanceable()) |
---|
| 229 | ((MapInstanced*)m)->DestroyInstance(instanceId); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | void MapManager::RemoveBonesFromMap(uint32 mapid, uint64 guid, float x, float y) |
---|
| 233 | { |
---|
| 234 | bool remove_result = _GetBaseMap(mapid)->RemoveBones(guid, x, y); |
---|
| 235 | |
---|
| 236 | if (!remove_result) |
---|
| 237 | { |
---|
| 238 | sLog.outDebug("Bones %u not found in world. Delete from DB also.", GUID_LOPART(guid)); |
---|
| 239 | } |
---|
| 240 | } |
---|
| 241 | |
---|
| 242 | void |
---|
| 243 | MapManager::Update(time_t diff) |
---|
| 244 | { |
---|
| 245 | i_timer.Update(diff); |
---|
| 246 | if( !i_timer.Passed() ) |
---|
| 247 | return; |
---|
| 248 | |
---|
| 249 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
| 250 | { |
---|
| 251 | checkAndCorrectGridStatesArray(); // debugging code, should be deleted some day |
---|
| 252 | iter->second->Update(i_timer.GetCurrent()); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | ObjectAccessor::Instance().Update(i_timer.GetCurrent()); |
---|
| 256 | for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) |
---|
| 257 | (*iter)->Update(i_timer.GetCurrent()); |
---|
| 258 | |
---|
| 259 | i_timer.SetCurrent(0); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | void MapManager::DoDelayedMovesAndRemoves() |
---|
| 263 | { |
---|
| 264 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
| 265 | iter->second->DoDelayedMovesAndRemoves(); |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | bool MapManager::ExistMapAndVMap(uint32 mapid, float x,float y) |
---|
| 269 | { |
---|
[44] | 270 | GridPair p = Trinity::ComputeGridPair(x,y); |
---|
[2] | 271 | |
---|
| 272 | int gx=63-p.x_coord; |
---|
| 273 | int gy=63-p.y_coord; |
---|
| 274 | |
---|
| 275 | return Map::ExistMap(mapid,gx,gy) && Map::ExistVMap(mapid,gx,gy); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | bool MapManager::IsValidMAP(uint32 mapid) |
---|
| 279 | { |
---|
| 280 | MapEntry const* mEntry = sMapStore.LookupEntry(mapid); |
---|
| 281 | return mEntry && (!mEntry->Instanceable() || objmgr.GetInstanceTemplate(mapid)); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | void MapManager::LoadGrid(int mapid, float x, float y, const WorldObject* obj, bool no_unload) |
---|
| 285 | { |
---|
[44] | 286 | CellPair p = Trinity::ComputeCellPair(x,y); |
---|
[2] | 287 | Cell cell(p); |
---|
| 288 | GetMap(mapid, obj)->LoadGrid(cell,no_unload); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | void MapManager::UnloadAll() |
---|
| 292 | { |
---|
| 293 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
| 294 | iter->second->UnloadAll(true); |
---|
| 295 | |
---|
| 296 | while(!i_maps.empty()) |
---|
| 297 | { |
---|
| 298 | delete i_maps.begin()->second; |
---|
| 299 | i_maps.erase(i_maps.begin()); |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | void MapManager::InitMaxInstanceId() |
---|
| 304 | { |
---|
| 305 | i_MaxInstanceId = 0; |
---|
| 306 | |
---|
| 307 | QueryResult *result = CharacterDatabase.Query( "SELECT MAX(id) FROM instance" ); |
---|
| 308 | if( result ) |
---|
| 309 | { |
---|
| 310 | i_MaxInstanceId = result->Fetch()[0].GetUInt32(); |
---|
| 311 | delete result; |
---|
| 312 | } |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | uint32 MapManager::GetNumInstances() |
---|
| 316 | { |
---|
| 317 | uint32 ret = 0; |
---|
| 318 | for(MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr) |
---|
| 319 | { |
---|
| 320 | Map *map = itr->second; |
---|
| 321 | if(!map->Instanceable()) continue; |
---|
| 322 | MapInstanced::InstancedMaps &maps = ((MapInstanced *)map)->GetInstancedMaps(); |
---|
| 323 | for(MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr) |
---|
| 324 | if(mitr->second->IsDungeon()) ret++; |
---|
| 325 | } |
---|
| 326 | return ret; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | uint32 MapManager::GetNumPlayersInInstances() |
---|
| 330 | { |
---|
| 331 | uint32 ret = 0; |
---|
| 332 | for(MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr) |
---|
| 333 | { |
---|
| 334 | Map *map = itr->second; |
---|
| 335 | if(!map->Instanceable()) continue; |
---|
| 336 | MapInstanced::InstancedMaps &maps = ((MapInstanced *)map)->GetInstancedMaps(); |
---|
| 337 | for(MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr) |
---|
| 338 | if(mitr->second->IsDungeon()) |
---|
| 339 | ret += ((InstanceMap*)mitr->second)->GetPlayers().size(); |
---|
| 340 | } |
---|
| 341 | return ret; |
---|
| 342 | } |
---|