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