Use reset time for normal/heroic from new DBC. Improve basic support for raid diffica...
[getmangos.git] / src / game / MapInstanced.cpp
blob0aa985ffe00f3abf33542c99898f13aae8fa19b5
1 /*
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"
25 #include "World.h"
27 MapInstanced::MapInstanced(uint32 id, time_t expiry) : Map(id, expiry, 0, 0)
29 // initialize instanced maps list
30 m_InstancedMaps.clear();
31 // fill with zero
32 memset(&GridMapReference, 0, MAX_NUMBER_OF_GRIDS*MAX_NUMBER_OF_GRIDS*sizeof(uint16));
35 void MapInstanced::InitVisibilityDistance()
37 if(m_InstancedMaps.empty())
38 return;
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!)
49 Map::Update(t);
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
60 else
62 // update only here, because it may schedule some bad things before delete
63 i->second->Update(t);
64 ++i;
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)
109 delete i->second;
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)
125 return NULL;
127 Map* map = NULL;
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);
137 if(!map)
138 map = CreateBattleGroundMap(NewInstanceId, player->GetBattleGround());
140 else
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;
156 if(pSave)
158 // solo/perm/group
159 NewInstanceId = pSave->GetInstanceId();
160 map = _FindMap(NewInstanceId);
161 // it is possible that the save exists but the map doesn't
162 if(!map)
163 map = CreateInstance(NewInstanceId, pSave, pSave->GetDifficulty());
165 else
167 // if no instanceId via group members or instance saves is found
168 // the instance will be created for the first time
169 NewInstanceId = MapManager::Instance().GenerateInstanceId();
170 map = CreateInstance(NewInstanceId, NULL, player->GetDifficulty(IsRaid()));
174 return map;
177 InstanceMap* MapInstanced::CreateInstance(uint32 InstanceId, InstanceSave *save, Difficulty difficulty)
179 // load/create a map
180 Guard guard(*this);
182 // make sure we have a valid map id
183 const MapEntry* entry = sMapStore.LookupEntry(GetId());
184 if(!entry)
186 sLog.outError("CreateInstance: no entry for map %d", GetId());
187 assert(false);
189 const InstanceTemplate * iTemplate = objmgr.GetInstanceTemplate(GetId());
190 if(!iTemplate)
192 sLog.outError("CreateInstance: no instance template for map %d", GetId());
193 assert(false);
196 // some instances only have one difficulty
197 MapDifficulty const* mapDiff = GetMapDifficultyData(GetId(),difficulty);
198 if (!mapDiff)
199 difficulty = DUNGEON_DIFFICULTY_NORMAL;
201 sLog.outDebug("MapInstanced::CreateInstance: %s map instance %d for %d created with difficulty %s", save?"":"new ", InstanceId, GetId(), difficulty?"heroic":"normal");
203 InstanceMap *map = new InstanceMap(GetId(), GetGridExpiry(), InstanceId, difficulty, this);
204 ASSERT(map->IsDungeon());
206 bool load_data = save != NULL;
207 map->CreateInstanceData(load_data);
209 m_InstancedMaps[InstanceId] = map;
210 return map;
213 BattleGroundMap* MapInstanced::CreateBattleGroundMap(uint32 InstanceId, BattleGround* bg)
215 // load/create a map
216 Guard guard(*this);
218 sLog.outDebug("MapInstanced::CreateBattleGroundMap: instance:%d for map:%d and bgType:%d created.", InstanceId, GetId(), bg->GetTypeID());
220 BattleGroundMap *map = new BattleGroundMap(GetId(), GetGridExpiry(), InstanceId, this);
221 ASSERT(map->IsBattleGroundOrArena());
222 map->SetBG(bg);
223 bg->SetBgMap(map);
225 m_InstancedMaps[InstanceId] = map;
226 return map;
229 void MapInstanced::DestroyInstance(uint32 InstanceId)
231 InstancedMaps::iterator itr = m_InstancedMaps.find(InstanceId);
232 if(itr != m_InstancedMaps.end())
233 DestroyInstance(itr);
236 // increments the iterator after erase
237 void MapInstanced::DestroyInstance(InstancedMaps::iterator &itr)
239 itr->second->UnloadAll(true);
240 // should only unload VMaps if this is the last instance and grid unloading is enabled
241 if(m_InstancedMaps.size() <= 1 && sWorld.getConfig(CONFIG_GRID_UNLOAD))
243 VMAP::VMapFactory::createOrGetVMapManager()->unloadMap(itr->second->GetId());
244 // in that case, unload grids of the base map, too
245 // so in the next map creation, (EnsureGridCreated actually) VMaps will be reloaded
246 Map::UnloadAll(true);
248 // erase map
249 delete itr->second;
250 m_InstancedMaps.erase(itr++);