2 * Copyright (C) 2005-2009 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, 0)
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::Update(const uint32
& t
)
37 // take care of loaded GridMaps (when unused, unload it!)
40 // update the instanced maps
41 InstancedMaps::iterator i
= m_InstancedMaps
.begin();
43 while (i
!= m_InstancedMaps
.end())
45 if(i
->second
->CanUnload(t
))
47 DestroyInstance(i
); // iterator incremented
51 // update only here, because it may schedule some bad things before delete
58 void MapInstanced::MoveAllCreaturesInMoveList()
60 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
62 i
->second
->MoveAllCreaturesInMoveList();
65 Map::MoveAllCreaturesInMoveList();
68 void MapInstanced::RemoveAllObjectsInRemoveList()
70 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
72 i
->second
->RemoveAllObjectsInRemoveList();
75 Map::RemoveAllObjectsInRemoveList();
78 bool MapInstanced::RemoveBones(uint64 guid
, float x
, float y
)
80 bool remove_result
= false;
82 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
84 remove_result
= remove_result
|| i
->second
->RemoveBones(guid
, x
, y
);
87 return remove_result
|| Map::RemoveBones(guid
,x
,y
);
90 void MapInstanced::UnloadAll(bool pForce
)
92 // Unload instanced maps
93 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
94 i
->second
->UnloadAll(pForce
);
96 // Delete the maps only after everything is unloaded to prevent crashes
97 for (InstancedMaps::iterator i
= m_InstancedMaps
.begin(); i
!= m_InstancedMaps
.end(); ++i
)
100 m_InstancedMaps
.clear();
102 // Unload own grids (just dummy(placeholder) grids, neccesary to unload GridMaps!)
103 Map::UnloadAll(pForce
);
107 - return the right instance for the object, based on its InstanceId
108 - create the instance if it's not created already
109 - the player is not actually added to the instance (only in InstanceMap::Add)
111 Map
* MapInstanced::CreateInstance(const uint32 mapId
, Player
* player
)
113 if(GetId() != mapId
|| !player
)
117 uint32 NewInstanceId
= 0; // instanceId of the resulting map
119 if(IsBattleGroundOrArena())
121 // instantiate or find existing bg map for player
122 // the instance id is set in battlegroundid
123 NewInstanceId
= player
->GetBattleGroundId();
124 ASSERT(NewInstanceId
);
125 map
= _FindMap(NewInstanceId
);
127 map
= CreateBattleGround(NewInstanceId
);
131 InstancePlayerBind
*pBind
= player
->GetBoundInstance(GetId(), player
->GetDifficulty());
132 InstanceSave
*pSave
= pBind
? pBind
->save
: NULL
;
134 // the player's permanent player bind is taken into consideration first
135 // then the player's group bind and finally the solo bind.
136 if(!pBind
|| !pBind
->perm
)
138 InstanceGroupBind
*groupBind
= NULL
;
139 Group
*group
= player
->GetGroup();
140 // use the player's difficulty setting (it may not be the same as the group's)
141 if(group
&& (groupBind
= group
->GetBoundInstance(GetId(), player
->GetDifficulty())))
142 pSave
= groupBind
->save
;
148 NewInstanceId
= pSave
->GetInstanceId();
149 map
= _FindMap(NewInstanceId
);
150 // it is possible that the save exists but the map doesn't
152 map
= CreateInstance(NewInstanceId
, pSave
, pSave
->GetDifficulty());
156 // if no instanceId via group members or instance saves is found
157 // the instance will be created for the first time
158 NewInstanceId
= MapManager::Instance().GenerateInstanceId();
159 map
= CreateInstance(NewInstanceId
, NULL
, player
->GetDifficulty());
166 InstanceMap
* MapInstanced::CreateInstance(uint32 InstanceId
, InstanceSave
*save
, uint8 difficulty
)
171 // make sure we have a valid map id
172 const MapEntry
* entry
= sMapStore
.LookupEntry(GetId());
175 sLog
.outError("CreateInstance: no entry for map %d", GetId());
178 const InstanceTemplate
* iTemplate
= objmgr
.GetInstanceTemplate(GetId());
181 sLog
.outError("CreateInstance: no instance template for map %d", GetId());
185 // some instances only have one difficulty
186 if (entry
&& !entry
->SupportsHeroicMode()) difficulty
= DIFFICULTY_NORMAL
;
188 sLog
.outDebug("MapInstanced::CreateInstance: %s map instance %d for %d created with difficulty %s", save
?"":"new ", InstanceId
, GetId(), difficulty
?"heroic":"normal");
190 InstanceMap
*map
= new InstanceMap(GetId(), GetGridExpiry(), InstanceId
, difficulty
, this);
191 ASSERT(map
->IsDungeon());
193 bool load_data
= save
!= NULL
;
194 map
->CreateInstanceData(load_data
);
196 m_InstancedMaps
[InstanceId
] = map
;
200 BattleGroundMap
* MapInstanced::CreateBattleGround(uint32 InstanceId
)
205 sLog
.outDebug("MapInstanced::CreateBattleGround: map bg %d for %d created.", InstanceId
, GetId());
207 BattleGroundMap
*map
= new BattleGroundMap(GetId(), GetGridExpiry(), InstanceId
, this);
208 ASSERT(map
->IsBattleGroundOrArena());
210 m_InstancedMaps
[InstanceId
] = map
;
214 void MapInstanced::DestroyInstance(uint32 InstanceId
)
216 InstancedMaps::iterator itr
= m_InstancedMaps
.find(InstanceId
);
217 if(itr
!= m_InstancedMaps
.end())
218 DestroyInstance(itr
);
221 // increments the iterator after erase
222 void MapInstanced::DestroyInstance(InstancedMaps::iterator
&itr
)
224 itr
->second
->UnloadAll(true);
225 // should only unload VMaps if this is the last instance and grid unloading is enabled
226 if(m_InstancedMaps
.size() <= 1 && sWorld
.getConfig(CONFIG_GRID_UNLOAD
))
228 VMAP::VMapFactory::createOrGetVMapManager()->unloadMap(itr
->second
->GetId());
229 // in that case, unload grids of the base map, too
230 // so in the next map creation, (EnsureGridCreated actually) VMaps will be reloaded
231 Map::UnloadAll(true);
235 m_InstancedMaps
.erase(itr
++);