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 "MapInstanced.h"
20 #include "ObjectMgr.h"
21 #include "MapManager.h"
22 #include "BattleGround.h"
23 #include "VMapFactory.h"
24 #include "InstanceSaveMgr.h"
27 MapInstanced::MapInstanced(uint32 id
, time_t expiry
) : Map(id
, expiry
, 0, DUNGEON_DIFFICULTY_NORMAL
)
29 // initialize instanced maps list
30 m_InstancedMaps
.clear();
32 memset(&GridMapReference
, 0, MAX_NUMBER_OF_GRIDS
*MAX_NUMBER_OF_GRIDS
*sizeof(uint16
));
35 void MapInstanced::InitVisibilityDistance()
37 if(m_InstancedMaps
.empty())
39 //initialize visibility distances for all instance copies
40 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
42 (*i
).second
->InitVisibilityDistance();
46 void MapInstanced::Update(const uint32
& t
)
48 // take care of loaded GridMaps (when unused, unload it!)
51 // update the instanced maps
52 InstancedMaps::iterator i
= m_InstancedMaps
.begin();
54 while (i
!= m_InstancedMaps
.end())
56 if(i
->second
->CanUnload(t
))
58 DestroyInstance(i
); // iterator incremented
62 // update only here, because it may schedule some bad things before delete
69 void MapInstanced::MoveAllCreaturesInMoveList()
71 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
73 i
->second
->MoveAllCreaturesInMoveList();
76 Map::MoveAllCreaturesInMoveList();
79 void MapInstanced::RemoveAllObjectsInRemoveList()
81 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
83 i
->second
->RemoveAllObjectsInRemoveList();
86 Map::RemoveAllObjectsInRemoveList();
89 bool MapInstanced::RemoveBones(uint64 guid
, float x
, float y
)
91 bool remove_result
= false;
93 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
95 remove_result
= remove_result
|| i
->second
->RemoveBones(guid
, x
, y
);
98 return remove_result
|| Map::RemoveBones(guid
,x
,y
);
101 void MapInstanced::UnloadAll(bool pForce
)
103 // Unload instanced maps
104 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
105 i
->second
->UnloadAll(pForce
);
107 // Delete the maps only after everything is unloaded to prevent crashes
108 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
111 m_InstancedMaps
.clear();
113 // Unload own grids (just dummy(placeholder) grids, neccesary to unload GridMaps!)
114 Map::UnloadAll(pForce
);
118 - return the right instance for the object, based on its InstanceId
119 - create the instance if it's not created already
120 - the player is not actually added to the instance (only in InstanceMap::Add)
122 Map
* MapInstanced::CreateInstance(const uint32 mapId
, Player
* player
)
124 if(GetId() != mapId
|| !player
)
128 uint32 NewInstanceId
= 0; // instanceId of the resulting map
130 if(IsBattleGroundOrArena())
132 // instantiate or find existing bg map for player
133 // the instance id is set in battlegroundid
134 NewInstanceId
= player
->GetBattleGroundId();
135 ASSERT(NewInstanceId
);
136 map
= _FindMap(NewInstanceId
);
138 map
= CreateBattleGroundMap(NewInstanceId
, player
->GetBattleGround());
142 InstancePlayerBind
*pBind
= player
->GetBoundInstance(GetId(), player
->GetDifficulty(IsRaid()));
143 InstanceSave
*pSave
= pBind
? pBind
->save
: NULL
;
145 // the player's permanent player bind is taken into consideration first
146 // then the player's group bind and finally the solo bind.
147 if(!pBind
|| !pBind
->perm
)
149 InstanceGroupBind
*groupBind
= NULL
;
150 Group
*group
= player
->GetGroup();
151 // use the player's difficulty setting (it may not be the same as the group's)
152 if(group
&& (groupBind
= group
->GetBoundInstance(this)))
153 pSave
= groupBind
->save
;
159 NewInstanceId
= pSave
->GetInstanceId();
160 map
= _FindMap(NewInstanceId
);
161 // it is possible that the save exists but the map doesn't
163 map
= CreateInstance(NewInstanceId
, pSave
, pSave
->GetDifficulty());
167 // if no instanceId via group members or instance saves is found
168 // the instance will be created for the first time
169 NewInstanceId
= sMapMgr
.GenerateInstanceId();
171 Difficulty diff
= player
->GetGroup() ? player
->GetGroup()->GetDifficulty(IsRaid()) : player
->GetDifficulty(IsRaid());
172 map
= CreateInstance(NewInstanceId
, NULL
, diff
);
179 InstanceMap
* MapInstanced::CreateInstance(uint32 InstanceId
, InstanceSave
*save
, Difficulty difficulty
)
184 // make sure we have a valid map id
185 if (!sMapStore
.LookupEntry(GetId()))
187 sLog
.outError("CreateInstance: no entry for map %d", GetId());
190 if (!ObjectMgr::GetInstanceTemplate(GetId()))
192 sLog
.outError("CreateInstance: no instance template for map %d", GetId());
196 // some instances only have one difficulty
197 if (!GetMapDifficultyData(GetId(),difficulty
))
198 difficulty
= DUNGEON_DIFFICULTY_NORMAL
;
200 sLog
.outDebug("MapInstanced::CreateInstance: %s map instance %d for %d created with difficulty %s", save
?"":"new ", InstanceId
, GetId(), difficulty
?"heroic":"normal");
202 InstanceMap
*map
= new InstanceMap(GetId(), GetGridExpiry(), InstanceId
, difficulty
, this);
203 ASSERT(map
->IsDungeon());
205 bool load_data
= save
!= NULL
;
206 map
->CreateInstanceData(load_data
);
208 m_InstancedMaps
[InstanceId
] = map
;
212 BattleGroundMap
* MapInstanced::CreateBattleGroundMap(uint32 InstanceId
, BattleGround
* bg
)
217 sLog
.outDebug("MapInstanced::CreateBattleGroundMap: instance:%d for map:%d and bgType:%d created.", InstanceId
, GetId(), bg
->GetTypeID());
219 PvPDifficultyEntry
const* bracketEntry
= GetBattlegroundBracketByLevel(bg
->GetMapId(),bg
->GetMinLevel());
221 uint8 spawnMode
= bracketEntry
? bracketEntry
->difficulty
: REGULAR_DIFFICULTY
;
223 BattleGroundMap
*map
= new BattleGroundMap(GetId(), GetGridExpiry(), InstanceId
, this, spawnMode
);
224 ASSERT(map
->IsBattleGroundOrArena());
228 m_InstancedMaps
[InstanceId
] = map
;
232 void MapInstanced::DestroyInstance(uint32 InstanceId
)
234 InstancedMaps::iterator itr
= m_InstancedMaps
.find(InstanceId
);
235 if(itr
!= m_InstancedMaps
.end())
236 DestroyInstance(itr
);
239 // increments the iterator after erase
240 void MapInstanced::DestroyInstance(InstancedMaps::iterator
&itr
)
242 itr
->second
->UnloadAll(true);
243 // should only unload VMaps if this is the last instance and grid unloading is enabled
244 if(m_InstancedMaps
.size() <= 1 && sWorld
.getConfig(CONFIG_GRID_UNLOAD
))
246 VMAP::VMapFactory::createOrGetVMapManager()->unloadMap(itr
->second
->GetId());
247 // in that case, unload grids of the base map, too
248 // so in the next map creation, (EnsureGridCreated actually) VMaps will be reloaded
249 Map::UnloadAll(true);
253 m_InstancedMaps
.erase(itr
++);