1 | /* |
---|
2 | * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/> |
---|
3 | * |
---|
4 | * Copyright (C) 2008 Trinity <http://www.trinitycore.org/> |
---|
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 |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
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 |
---|
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
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 | |
---|
36 | #define CLASS_LOCK Trinity::ClassLevelLockable<MapManager, ZThread::Mutex> |
---|
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 | } |
---|
87 | #ifdef TRINITY_DEBUG |
---|
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); |
---|
112 | if (entry && entry->Instanceable()) |
---|
113 | { |
---|
114 | m = new MapInstanced(id, i_gridCleanUpDelay); |
---|
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(player->GetSession()->GetTrinityString(810), 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(player->GetSession()->GetTrinityString(811), 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) |
---|
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 | ObjectAccessor::Instance().UpdatePlayers(i_timer.GetCurrent()); |
---|
250 | |
---|
251 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
252 | { |
---|
253 | checkAndCorrectGridStatesArray(); // debugging code, should be deleted some day |
---|
254 | iter->second->Update(i_timer.GetCurrent()); |
---|
255 | } |
---|
256 | |
---|
257 | ObjectAccessor::Instance().Update(i_timer.GetCurrent()); |
---|
258 | for (TransportSet::iterator iter = m_Transports.begin(); iter != m_Transports.end(); ++iter) |
---|
259 | (*iter)->Update(i_timer.GetCurrent()); |
---|
260 | |
---|
261 | i_timer.SetCurrent(0); |
---|
262 | } |
---|
263 | |
---|
264 | void MapManager::DoDelayedMovesAndRemoves() |
---|
265 | { |
---|
266 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
267 | iter->second->DoDelayedMovesAndRemoves(); |
---|
268 | } |
---|
269 | |
---|
270 | bool MapManager::ExistMapAndVMap(uint32 mapid, float x,float y) |
---|
271 | { |
---|
272 | GridPair p = Trinity::ComputeGridPair(x,y); |
---|
273 | |
---|
274 | int gx=63-p.x_coord; |
---|
275 | int gy=63-p.y_coord; |
---|
276 | |
---|
277 | return Map::ExistMap(mapid,gx,gy) && Map::ExistVMap(mapid,gx,gy); |
---|
278 | } |
---|
279 | |
---|
280 | bool MapManager::IsValidMAP(uint32 mapid) |
---|
281 | { |
---|
282 | MapEntry const* mEntry = sMapStore.LookupEntry(mapid); |
---|
283 | return mEntry && (!mEntry->Instanceable() || objmgr.GetInstanceTemplate(mapid)); |
---|
284 | } |
---|
285 | |
---|
286 | void MapManager::LoadGrid(int mapid, float x, float y, const WorldObject* obj, bool no_unload) |
---|
287 | { |
---|
288 | CellPair p = Trinity::ComputeCellPair(x,y); |
---|
289 | Cell cell(p); |
---|
290 | GetMap(mapid, obj)->LoadGrid(cell,no_unload); |
---|
291 | } |
---|
292 | |
---|
293 | void MapManager::UnloadAll() |
---|
294 | { |
---|
295 | for(MapMapType::iterator iter=i_maps.begin(); iter != i_maps.end(); ++iter) |
---|
296 | iter->second->UnloadAll(true); |
---|
297 | |
---|
298 | while(!i_maps.empty()) |
---|
299 | { |
---|
300 | delete i_maps.begin()->second; |
---|
301 | i_maps.erase(i_maps.begin()); |
---|
302 | } |
---|
303 | } |
---|
304 | |
---|
305 | void MapManager::InitMaxInstanceId() |
---|
306 | { |
---|
307 | i_MaxInstanceId = 0; |
---|
308 | |
---|
309 | QueryResult *result = CharacterDatabase.Query( "SELECT MAX(id) FROM instance" ); |
---|
310 | if( result ) |
---|
311 | { |
---|
312 | i_MaxInstanceId = result->Fetch()[0].GetUInt32(); |
---|
313 | delete result; |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | uint32 MapManager::GetNumInstances() |
---|
318 | { |
---|
319 | uint32 ret = 0; |
---|
320 | for(MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr) |
---|
321 | { |
---|
322 | Map *map = itr->second; |
---|
323 | if(!map->Instanceable()) continue; |
---|
324 | MapInstanced::InstancedMaps &maps = ((MapInstanced *)map)->GetInstancedMaps(); |
---|
325 | for(MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr) |
---|
326 | if(mitr->second->IsDungeon()) ret++; |
---|
327 | } |
---|
328 | return ret; |
---|
329 | } |
---|
330 | |
---|
331 | uint32 MapManager::GetNumPlayersInInstances() |
---|
332 | { |
---|
333 | uint32 ret = 0; |
---|
334 | for(MapMapType::iterator itr = i_maps.begin(); itr != i_maps.end(); ++itr) |
---|
335 | { |
---|
336 | Map *map = itr->second; |
---|
337 | if(!map->Instanceable()) continue; |
---|
338 | MapInstanced::InstancedMaps &maps = ((MapInstanced *)map)->GetInstancedMaps(); |
---|
339 | for(MapInstanced::InstancedMaps::iterator mitr = maps.begin(); mitr != maps.end(); ++mitr) |
---|
340 | if(mitr->second->IsDungeon()) |
---|
341 | ret += ((InstanceMap*)mitr->second)->GetPlayers().getSize(); |
---|
342 | } |
---|
343 | return ret; |
---|
344 | } |
---|