/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
#include "Map.h"
#include "Battleground.h"
#include "CellImpl.h"
#include "Chat.h"
#include "DisableMgr.h"
#include "DynamicTree.h"
#include "GameTime.h"
#include "Geometry.h"
#include "GridNotifiers.h"
#include "Group.h"
#include "InstanceScript.h"
#include "IVMapMgr.h"
#include "LFGMgr.h"
#include "MapGrid.h"
#include "MapInstanced.h"
#include "Metric.h"
#include "MiscPackets.h"
#include "MMapFactory.h"
#include "Object.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Pet.h"
#include "ScriptMgr.h"
#include "Transport.h"
#include "VMapFactory.h"
#include "Vehicle.h"
#include "VMapMgr2.h"
#include "Weather.h"
#define MAP_INVALID_ZONE 0xFFFFFFFF
ZoneDynamicInfo::ZoneDynamicInfo() : MusicId(0), WeatherId(WEATHER_STATE_FINE),
WeatherGrade(0.0f), OverrideLightId(0), LightFadeInTime(0) { }
Map::~Map()
{
// UnloadAll must be called before deleting the map
sScriptMgr->OnDestroyMap(this);
if (!m_scriptSchedule.empty())
sScriptMgr->DecreaseScheduledScriptCount(m_scriptSchedule.size());
MMAP::MMapFactory::createOrGetMMapMgr()->unloadMapInstance(GetId(), i_InstanceId);
}
Map::Map(uint32 id, uint32 InstanceId, uint8 SpawnMode, Map* _parent) :
_mapGridManager(this), i_mapEntry(sMapStore.LookupEntry(id)), i_spawnMode(SpawnMode), i_InstanceId(InstanceId),
m_unloadTimer(0), m_VisibleDistance(DEFAULT_VISIBILITY_DISTANCE), _instanceResetPeriod(0),
_transportsUpdateIter(_transports.end()), i_scriptLock(false), _defaultLight(GetDefaultMapLight(id))
{
m_parentMap = (_parent ? _parent : this);
_zonePlayerCountMap.clear();
_updatableObjectListRecheckTimer.SetInterval(UPDATABLE_OBJECT_LIST_RECHECK_TIMER);
//lets initialize visibility distance for map
Map::InitVisibilityDistance();
_corpseUpdateTimer.SetInterval(20 * MINUTE * IN_MILLISECONDS);
}
// Hook called after map is created AND after added to map list
void Map::OnCreateMap()
{
// Instances load all grids by default (both base map and child maps)
if (GetInstanceId())
LoadAllGrids();
sScriptMgr->OnCreateMap(this);
}
void Map::InitVisibilityDistance()
{
//init visibility for continents
m_VisibleDistance = World::GetMaxVisibleDistanceOnContinents();
switch (GetId())
{
case MAP_EBON_HOLD: // Scarlet Enclave (DK starting zone)
m_VisibleDistance = 125.0f;
break;
case MAP_SCOTT_TEST: // (box map)
m_VisibleDistance = 200.0f;
break;
}
}
// Template specialization of utility methods
template
void Map::AddToGrid(T* obj, Cell const& cell)
{
MapGridType* grid = GetMapGrid(cell.GridX(), cell.GridY());
grid->AddGridObject(cell.CellX(), cell.CellY(), obj);
obj->SetCurrentCell(cell);
}
template<>
void Map::AddToGrid(Creature* obj, Cell const& cell)
{
MapGridType* grid = GetMapGrid(cell.GridX(), cell.GridY());
grid->AddGridObject(cell.CellX(), cell.CellY(), obj);
if (obj->IsFarVisible())
grid->AddFarVisibleObject(cell.CellX(), cell.CellY(), obj);
obj->SetCurrentCell(cell);
}
template<>
void Map::AddToGrid(GameObject* obj, Cell const& cell)
{
MapGridType* grid = GetMapGrid(cell.GridX(), cell.GridY());
grid->AddGridObject(cell.CellX(), cell.CellY(), obj);
if (obj->IsFarVisible())
grid->AddFarVisibleObject(cell.CellX(), cell.CellY(), obj);
obj->SetCurrentCell(cell);
}
template<>
void Map::AddToGrid(Player* obj, Cell const& cell)
{
MapGridType* grid = GetMapGrid(cell.GridX(), cell.GridY());
grid->AddGridObject(cell.CellX(), cell.CellY(), obj);
}
template<>
void Map::AddToGrid(Corpse* obj, Cell const& cell)
{
MapGridType* grid = GetMapGrid(cell.GridX(), cell.GridY());
// Corpses are a special object type - they can be added to grid via a call to AddToMap
// or loaded through ObjectGridLoader.
// Both corpses loaded from database and these freshly generated by Player::CreateCoprse are added to _corpsesByCell
// ObjectGridLoader loads all corpses from _corpsesByCell even if they were already added to grid before it was loaded
// so we need to explicitly check it here (Map::AddToGrid is only called from Player::BuildPlayerRepop, not from ObjectGridLoader)
// to avoid failing an assertion in GridObject::AddToGrid
if (grid->IsObjectDataLoaded())
grid->AddGridObject(cell.CellX(), cell.CellY(), obj);
}
template
void Map::DeleteFromWorld(T* obj)
{
// Note: In case resurrectable corpse and pet its removed from global lists in own destructor
delete obj;
}
template<>
void Map::DeleteFromWorld(Player* player)
{
ObjectAccessor::RemoveObject(player);
RemoveUpdateObject(player); //TODO: I do not know why we need this, it should be removed in ~Object anyway
delete player;
}
void Map::EnsureGridCreated(GridCoord const& gridCoord)
{
_mapGridManager.CreateGrid(gridCoord.x_coord, gridCoord.y_coord);
}
bool Map::EnsureGridLoaded(Cell const& cell)
{
EnsureGridCreated(GridCoord(cell.GridX(), cell.GridY()));
if (_mapGridManager.LoadGrid(cell.GridX(), cell.GridY()))
{
Balance();
return true;
}
return false;
}
MapGridType* Map::GetMapGrid(uint16 const x, uint16 const y)
{
return _mapGridManager.GetGrid(x, y);
}
bool Map::IsGridLoaded(GridCoord const& gridCoord) const
{
return _mapGridManager.IsGridLoaded(gridCoord.x_coord, gridCoord.y_coord);
}
bool Map::IsGridCreated(GridCoord const& gridCoord) const
{
return _mapGridManager.IsGridCreated(gridCoord.x_coord, gridCoord.y_coord);
}
void Map::LoadGrid(float x, float y)
{
EnsureGridLoaded(Cell(x, y));
}
void Map::LoadAllGrids()
{
for (uint32 cellX = 0; cellX < TOTAL_NUMBER_OF_CELLS_PER_MAP; cellX++)
for (uint32 cellY = 0; cellY < TOTAL_NUMBER_OF_CELLS_PER_MAP; cellY++)
LoadGrid((cellX + 0.5f - CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL, (cellY + 0.5f - CENTER_GRID_CELL_ID) * SIZE_OF_GRID_CELL);
}
void Map::LoadGridsInRange(Position const& center, float radius)
{
if (_mapGridManager.IsGridsFullyLoaded())
return;
float const x = center.GetPositionX();
float const y = center.GetPositionY();
CellCoord cellCoord(Acore::ComputeCellCoord(x, y));
if (!cellCoord.IsCoordValid())
return;
if (radius > SIZE_OF_GRIDS)
radius = SIZE_OF_GRIDS;
CellArea area = Cell::CalculateCellArea(x, y, radius);
if (!area)
return;
for (uint32 x = area.low_bound.x_coord; x <= area.high_bound.x_coord; ++x)
{
for (uint32 y = area.low_bound.y_coord; y <= area.high_bound.y_coord; ++y)
{
CellCoord cellCoord(x, y);
Cell cell(cellCoord);
EnsureGridLoaded(cell);
}
}
}
bool Map::AddPlayerToMap(Player* player)
{
CellCoord cellCoord = Acore::ComputeCellCoord(player->GetPositionX(), player->GetPositionY());
if (!cellCoord.IsCoordValid())
{
LOG_ERROR("maps", "Map::Add: Player ({}) has invalid coordinates X:{} Y:{} grid cell [{}:{}]",
player->GetGUID().ToString(), player->GetPositionX(), player->GetPositionY(), cellCoord.x_coord, cellCoord.y_coord);
return false;
}
Cell cell(cellCoord);
LoadGridsInRange(*player, MAX_VISIBILITY_DISTANCE);
AddToGrid(player, cell);
// Check if we are adding to correct map
ASSERT (player->GetMap() == this);
player->SetMap(this);
player->AddToWorld();
SendInitTransports(player);
SendInitSelf(player);
SendZoneDynamicInfo(player);
player->UpdateObjectVisibility(false);
if (player->IsAlive())
ConvertCorpseToBones(player->GetGUID());
sScriptMgr->OnPlayerEnterMap(this, player);
return true;
}
template
void Map::InitializeObject(T* /*obj*/)
{
}
template<>
void Map::InitializeObject(Creature* /*obj*/)
{
//obj->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
}
template<>
void Map::InitializeObject(GameObject* /*obj*/)
{
//obj->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
}
template
bool Map::AddToMap(T* obj, bool checkTransport)
{
//TODO: Needs clean up. An object should not be added to map twice.
if (obj->IsInWorld())
{
ASSERT(obj->IsInGrid());
obj->UpdateObjectVisibilityOnCreate();
return true;
}
CellCoord cellCoord = Acore::ComputeCellCoord(obj->GetPositionX(), obj->GetPositionY());
//It will create many problems (including crashes) if an object is not added to grid after creation
//The correct way to fix it is to make AddToMap return false and delete the object if it is not added to grid
//But now AddToMap is used in too many places, I will just see how many ASSERT failures it will cause
ASSERT(cellCoord.IsCoordValid());
if (!cellCoord.IsCoordValid())
{
LOG_ERROR("maps", "Map::AddToMap: Object {} has invalid coordinates X:{} Y:{} grid cell [{}:{}]",
obj->GetGUID().ToString(), obj->GetPositionX(), obj->GetPositionY(), cellCoord.x_coord, cellCoord.y_coord);
return false; //Should delete object
}
Cell cell(cellCoord);
if (obj->isActiveObject())
EnsureGridLoaded(cell);
else
EnsureGridCreated(GridCoord(cell.GridX(), cell.GridY()));
AddToGrid(obj, cell);
//Must already be set before AddToMap. Usually during obj->Create.
//obj->SetMap(this);
obj->AddToWorld();
if (checkTransport)
if (!(obj->IsGameObject() && obj->ToGameObject()->IsTransport())) // dont add transport to transport ;d
if (Transport* transport = GetTransportForPos(obj->GetPhaseMask(), obj->GetPositionX(), obj->GetPositionY(), obj->GetPositionZ(), obj))
transport->AddPassenger(obj, true);
InitializeObject(obj);
//something, such as vehicle, needs to be update immediately
//also, trigger needs to cast spell, if not update, cannot see visual
obj->UpdateObjectVisibility(true);
// Xinef: little hack for vehicles, accessories have to be added after visibility update so they wont fall off the vehicle, moved from Creature::AIM_Initialize
// Initialize vehicle, this is done only for summoned npcs, DB creatures are handled by grid loaders
if (obj->IsCreature())
if (Vehicle* vehicle = obj->ToCreature()->GetVehicleKit())
vehicle->Reset();
return true;
}
template<>
bool Map::AddToMap(Transport* obj, bool /*checkTransport*/)
{
//TODO: Needs clean up. An object should not be added to map twice.
if (obj->IsInWorld())
return true;
CellCoord cellCoord = Acore::ComputeCellCoord(obj->GetPositionX(), obj->GetPositionY());
if (!cellCoord.IsCoordValid())
{
LOG_ERROR("maps", "Map::Add: Object {} has invalid coordinates X:{} Y:{} grid cell [{}:{}]",
obj->GetGUID().ToString(), obj->GetPositionX(), obj->GetPositionY(), cellCoord.x_coord, cellCoord.y_coord);
return false; //Should delete object
}
Cell cell(cellCoord);
EnsureGridLoaded(cell);
obj->AddToWorld();
_transports.insert(obj);
// Broadcast creation to players
for (Map::PlayerList::const_iterator itr = GetPlayers().begin(); itr != GetPlayers().end(); ++itr)
{
if (itr->GetSource()->GetTransport() != obj)
{
UpdateData data;
obj->BuildCreateUpdateBlockForPlayer(&data, itr->GetSource());
WorldPacket packet;
data.BuildPacket(packet);
itr->GetSource()->SendDirectMessage(&packet);
}
}
return true;
}
void Map::MarkNearbyCellsOf(WorldObject* obj)
{
// Check for valid position
if (!obj->IsPositionValid())
return;
// Update mobs/objects in ALL visible cells around object!
CellArea area = Cell::CalculateCellArea(obj->GetPositionX(), obj->GetPositionY(), obj->GetGridActivationRange());
for (uint32 x = area.low_bound.x_coord; x <= area.high_bound.x_coord; ++x)
{
for (uint32 y = area.low_bound.y_coord; y <= area.high_bound.y_coord; ++y)
{
// marked cells are those that have been visited
uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x;
markCell(cell_id);
}
}
}
void Map::UpdatePlayerZoneStats(uint32 oldZone, uint32 newZone)
{
// Nothing to do if no change
if (oldZone == newZone)
return;
if (oldZone != MAP_INVALID_ZONE)
{
uint32& oldZoneCount = _zonePlayerCountMap[oldZone];
if (!oldZoneCount)
LOG_ERROR("maps", "A player left zone {} (went to {}) - but there were no players in the zone!", oldZone, newZone);
else
--oldZoneCount;
}
++_zonePlayerCountMap[newZone];
}
void Map::Update(const uint32 t_diff, const uint32 s_diff, bool /*thread*/)
{
if (t_diff)
_dynamicTree.update(t_diff);
// Update world sessions and players
for (m_mapRefIter = m_mapRefMgr.begin(); m_mapRefIter != m_mapRefMgr.end(); ++m_mapRefIter)
{
Player* player = m_mapRefIter->GetSource();
if (player && player->IsInWorld())
{
// Update session
WorldSession* session = player->GetSession();
MapSessionFilter updater(session);
session->Update(s_diff, updater);
// update players at tick
if (!t_diff)
player->Update(s_diff);
}
}
_creatureRespawnScheduler.Update(t_diff);
if (!t_diff)
{
HandleDelayedVisibility();
return;
}
_updatableObjectListRecheckTimer.Update(t_diff);
resetMarkedCells();
// Update players
for (m_mapRefIter = m_mapRefMgr.begin(); m_mapRefIter != m_mapRefMgr.end(); ++m_mapRefIter)
{
Player* player = m_mapRefIter->GetSource();
if (!player || !player->IsInWorld())
continue;
player->Update(s_diff);
if (_updatableObjectListRecheckTimer.Passed())
{
MarkNearbyCellsOf(player);
// If player is using far sight, update viewpoint
if (WorldObject* viewPoint = player->GetViewpoint())
{
if (Creature* viewCreature = viewPoint->ToCreature())
MarkNearbyCellsOf(viewCreature);
else if (DynamicObject* viewObject = viewPoint->ToDynObject())
MarkNearbyCellsOf(viewObject);
}
}
}
UpdateNonPlayerObjects(t_diff);
SendObjectUpdates();
///- Process necessary scripts
if (!m_scriptSchedule.empty())
{
i_scriptLock = true;
ScriptsProcess();
i_scriptLock = false;
}
MoveAllCreaturesInMoveList();
MoveAllGameObjectsInMoveList();
MoveAllDynamicObjectsInMoveList();
HandleDelayedVisibility();
UpdateExpiredCorpses(t_diff);
sScriptMgr->OnMapUpdate(this, t_diff);
METRIC_VALUE("map_creatures", uint64(GetObjectsStore().Size()),
METRIC_TAG("map_id", std::to_string(GetId())),
METRIC_TAG("map_instanceid", std::to_string(GetInstanceId())));
METRIC_VALUE("map_gameobjects", uint64(GetObjectsStore().Size()),
METRIC_TAG("map_id", std::to_string(GetId())),
METRIC_TAG("map_instanceid", std::to_string(GetInstanceId())));
}
void Map::UpdateNonPlayerObjects(uint32 const diff)
{
for (WorldObject* obj : _pendingAddUpdatableObjectList)
_AddObjectToUpdateList(obj);
_pendingAddUpdatableObjectList.clear();
if (_updatableObjectListRecheckTimer.Passed())
{
for (uint32 i = 0; i < _updatableObjectList.size();)
{
WorldObject* obj = _updatableObjectList[i];
if (!obj->IsInWorld())
{
++i;
continue;
}
obj->Update(diff);
if (!obj->IsUpdateNeeded())
{
_RemoveObjectFromUpdateList(obj);
// Intentional no iteration here, obj is swapped with last element in
// _updatableObjectList so next loop will update that object at the same index
}
else
++i;
}
_updatableObjectListRecheckTimer.Reset();
}
else
{
for (uint32 i = 0; i < _updatableObjectList.size(); ++i)
{
WorldObject* obj = _updatableObjectList[i];
if (!obj->IsInWorld())
continue;
obj->Update(diff);
}
}
}
void Map::AddObjectToPendingUpdateList(WorldObject* obj)
{
if (!obj->CanBeAddedToMapUpdateList())
return;
UpdatableMapObject* mapUpdatableObject = dynamic_cast(obj);
if (mapUpdatableObject->GetUpdateState() != UpdatableMapObject::UpdateState::NotUpdating)
return;
_pendingAddUpdatableObjectList.insert(obj);
mapUpdatableObject->SetUpdateState(UpdatableMapObject::UpdateState::PendingAdd);
}
// Internal use only
void Map::_AddObjectToUpdateList(WorldObject* obj)
{
UpdatableMapObject* mapUpdatableObject = dynamic_cast(obj);
ASSERT(mapUpdatableObject && mapUpdatableObject->GetUpdateState() == UpdatableMapObject::UpdateState::PendingAdd);
mapUpdatableObject->SetUpdateState(UpdatableMapObject::UpdateState::Updating);
mapUpdatableObject->SetMapUpdateListOffset(_updatableObjectList.size());
_updatableObjectList.push_back(obj);
}
// Internal use only
void Map::_RemoveObjectFromUpdateList(WorldObject* obj)
{
UpdatableMapObject* mapUpdatableObject = dynamic_cast(obj);
ASSERT(mapUpdatableObject && mapUpdatableObject->GetUpdateState() == UpdatableMapObject::UpdateState::Updating);
if (obj != _updatableObjectList.back())
{
dynamic_cast(_updatableObjectList.back())->SetMapUpdateListOffset(mapUpdatableObject->GetMapUpdateListOffset());
std::swap(_updatableObjectList[mapUpdatableObject->GetMapUpdateListOffset()], _updatableObjectList.back());
}
_updatableObjectList.pop_back();
mapUpdatableObject->SetUpdateState(UpdatableMapObject::UpdateState::NotUpdating);
}
void Map::RemoveObjectFromMapUpdateList(WorldObject* obj)
{
if (!obj->CanBeAddedToMapUpdateList())
return;
UpdatableMapObject* mapUpdatableObject = dynamic_cast(obj);
if (mapUpdatableObject->GetUpdateState() == UpdatableMapObject::UpdateState::PendingAdd)
_pendingAddUpdatableObjectList.erase(obj);
else if (mapUpdatableObject->GetUpdateState() == UpdatableMapObject::UpdateState::Updating)
_RemoveObjectFromUpdateList(obj);
}
// Used in VisibilityDistanceType::Large and VisibilityDistanceType::Gigantic
void Map::AddWorldObjectToFarVisibleMap(WorldObject* obj)
{
if (Creature* creature = obj->ToCreature())
{
if (!creature->IsInGrid())
return;
Cell curr_cell = creature->GetCurrentCell();
MapGridType* grid = GetMapGrid(curr_cell.GridX(), curr_cell.GridY());
grid->AddFarVisibleObject(curr_cell.CellX(), curr_cell.CellY(), creature);
}
else if (GameObject* go = obj->ToGameObject())
{
if (!go->IsInGrid())
return;
Cell curr_cell = go->GetCurrentCell();
MapGridType* grid = GetMapGrid(curr_cell.GridX(), curr_cell.GridY());
grid->AddFarVisibleObject(curr_cell.CellX(), curr_cell.CellY(), go);
}
}
void Map::RemoveWorldObjectFromFarVisibleMap(WorldObject* obj)
{
if (Creature* creature = obj->ToCreature())
{
Cell curr_cell = creature->GetCurrentCell();
MapGridType* grid = GetMapGrid(curr_cell.GridX(), curr_cell.GridY());
grid->RemoveFarVisibleObject(curr_cell.CellX(), curr_cell.CellY(), creature);
}
else if (GameObject* go = obj->ToGameObject())
{
Cell curr_cell = go->GetCurrentCell();
MapGridType* grid = GetMapGrid(curr_cell.GridX(), curr_cell.GridY());
grid->RemoveFarVisibleObject(curr_cell.CellX(), curr_cell.CellY(), go);
}
}
// Used in VisibilityDistanceType::Infinite
void Map::AddWorldObjectToZoneWideVisibleMap(uint32 zoneId, WorldObject* obj)
{
_zoneWideVisibleWorldObjectsMap[zoneId].insert(obj);
}
void Map::RemoveWorldObjectFromZoneWideVisibleMap(uint32 zoneId, WorldObject* obj)
{
ZoneWideVisibleWorldObjectsMap::iterator itr = _zoneWideVisibleWorldObjectsMap.find(zoneId);
if (itr == _zoneWideVisibleWorldObjectsMap.end())
return;
itr->second.erase(obj);
}
ZoneWideVisibleWorldObjectsSet const* Map::GetZoneWideVisibleWorldObjectsForZone(uint32 zoneId) const
{
ZoneWideVisibleWorldObjectsMap::const_iterator itr = _zoneWideVisibleWorldObjectsMap.find(zoneId);
if (itr == _zoneWideVisibleWorldObjectsMap.end())
return nullptr;
return &itr->second;
}
void Map::HandleDelayedVisibility()
{
if (i_objectsForDelayedVisibility.empty())
return;
for (std::unordered_set::iterator itr = i_objectsForDelayedVisibility.begin(); itr != i_objectsForDelayedVisibility.end(); ++itr)
(*itr)->ExecuteDelayedUnitRelocationEvent();
i_objectsForDelayedVisibility.clear();
}
struct ResetNotifier
{
templateinline void resetNotify(GridRefMgr& m)
{
for (typename GridRefMgr::iterator iter = m.begin(); iter != m.end(); ++iter)
iter->GetSource()->ResetAllNotifies();
}
template void Visit(GridRefMgr&) {}
void Visit(CreatureMapType& m) { resetNotify(m);}
void Visit(PlayerMapType& m) { resetNotify(m);}
};
void Map::RemovePlayerFromMap(Player* player, bool remove)
{
// Before leaving map, update zone/area for stats
player->UpdateZone(MAP_INVALID_ZONE, 0);
player->getHostileRefMgr().deleteReferences(true); // pussywizard: multithreading crashfix
player->RemoveFromWorld();
SendRemoveTransports(player);
if (player->IsInGrid())
player->RemoveFromGrid();
else
ASSERT(remove); //maybe deleted in logoutplayer when player is not in a map
sScriptMgr->OnPlayerLeaveMap(this, player);
if (remove)
{
DeleteFromWorld(player);
}
}
void Map::AfterPlayerUnlinkFromMap()
{
}
template
void Map::RemoveFromMap(T* obj, bool remove)
{
obj->RemoveFromWorld();
obj->RemoveFromGrid();
if (obj->IsFarVisible())
RemoveWorldObjectFromFarVisibleMap(obj);
obj->ResetMap();
if (remove)
{
RemoveObjectFromMapUpdateList(obj);
DeleteFromWorld(obj);
}
}
template<>
void Map::RemoveFromMap(Transport* obj, bool remove)
{
obj->RemoveFromWorld();
Map::PlayerList const& players = GetPlayers();
if (!players.IsEmpty())
{
UpdateData data;
obj->BuildOutOfRangeUpdateBlock(&data);
WorldPacket packet;
data.BuildPacket(packet);
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (itr->GetSource()->GetTransport() != obj)
itr->GetSource()->SendDirectMessage(&packet);
}
if (_transportsUpdateIter != _transports.end())
{
TransportsContainer::iterator itr = _transports.find(obj);
if (itr == _transports.end())
return;
if (itr == _transportsUpdateIter)
++_transportsUpdateIter;
_transports.erase(itr);
}
else
_transports.erase(obj);
obj->ResetMap();
RemoveObjectFromMapUpdateList(obj);
if (remove)
{
// if option set then object already saved at this moment
if (!sWorld->getBoolConfig(CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY))
obj->SaveRespawnTime();
DeleteFromWorld(obj);
}
}
void Map::PlayerRelocation(Player* player, float x, float y, float z, float o)
{
Cell old_cell(player->GetPositionX(), player->GetPositionY());
Cell new_cell(x, y);
if (old_cell.DiffGrid(new_cell) || old_cell.DiffCell(new_cell))
{
player->RemoveFromGrid();
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddToGrid(player, new_cell);
}
player->Relocate(x, y, z, o);
if (player->IsVehicle())
player->GetVehicleKit()->RelocatePassengers();
player->UpdatePositionData();
player->UpdateObjectVisibility(false);
}
void Map::CreatureRelocation(Creature* creature, float x, float y, float z, float o)
{
Cell old_cell = creature->GetCurrentCell();
Cell new_cell(x, y);
if (old_cell.DiffGrid(new_cell) || old_cell.DiffCell(new_cell))
{
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddCreatureToMoveList(creature);
}
else
RemoveCreatureFromMoveList(creature);
creature->Relocate(x, y, z, o);
if (creature->IsVehicle())
creature->GetVehicleKit()->RelocatePassengers();
creature->UpdatePositionData();
creature->UpdateObjectVisibility(false);
}
void Map::GameObjectRelocation(GameObject* go, float x, float y, float z, float o)
{
Cell old_cell = go->GetCurrentCell();
Cell new_cell(x, y);
if (old_cell.DiffGrid(new_cell) || old_cell.DiffCell(new_cell))
{
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddGameObjectToMoveList(go);
}
else
RemoveGameObjectFromMoveList(go);
go->Relocate(x, y, z, o);
go->UpdateModelPosition();
go->SetPositionDataUpdate();
go->UpdateObjectVisibility(false);
}
void Map::DynamicObjectRelocation(DynamicObject* dynObj, float x, float y, float z, float o)
{
Cell old_cell = dynObj->GetCurrentCell();
Cell new_cell(x, y);
if (old_cell.DiffGrid(new_cell) || old_cell.DiffCell(new_cell))
{
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddDynamicObjectToMoveList(dynObj);
}
else
RemoveDynamicObjectFromMoveList(dynObj);
dynObj->Relocate(x, y, z, o);
dynObj->SetPositionDataUpdate();
dynObj->UpdateObjectVisibility(false);
}
void Map::AddCreatureToMoveList(Creature* c)
{
if (c->_moveState == MAP_OBJECT_CELL_MOVE_NONE)
_creaturesToMove.push_back(c);
c->_moveState = MAP_OBJECT_CELL_MOVE_ACTIVE;
}
void Map::RemoveCreatureFromMoveList(Creature* c)
{
if (c->_moveState == MAP_OBJECT_CELL_MOVE_ACTIVE)
c->_moveState = MAP_OBJECT_CELL_MOVE_INACTIVE;
}
void Map::AddGameObjectToMoveList(GameObject* go)
{
if (go->_moveState == MAP_OBJECT_CELL_MOVE_NONE)
_gameObjectsToMove.push_back(go);
go->_moveState = MAP_OBJECT_CELL_MOVE_ACTIVE;
}
void Map::RemoveGameObjectFromMoveList(GameObject* go)
{
if (go->_moveState == MAP_OBJECT_CELL_MOVE_ACTIVE)
go->_moveState = MAP_OBJECT_CELL_MOVE_INACTIVE;
}
void Map::AddDynamicObjectToMoveList(DynamicObject* dynObj)
{
if (dynObj->_moveState == MAP_OBJECT_CELL_MOVE_NONE)
_dynamicObjectsToMove.push_back(dynObj);
dynObj->_moveState = MAP_OBJECT_CELL_MOVE_ACTIVE;
}
void Map::RemoveDynamicObjectFromMoveList(DynamicObject* dynObj)
{
if (dynObj->_moveState == MAP_OBJECT_CELL_MOVE_ACTIVE)
dynObj->_moveState = MAP_OBJECT_CELL_MOVE_INACTIVE;
}
void Map::MoveAllCreaturesInMoveList()
{
for (std::vector::iterator itr = _creaturesToMove.begin(); itr != _creaturesToMove.end(); ++itr)
{
Creature* c = *itr;
if (c->FindMap() != this)
continue;
if (c->_moveState != MAP_OBJECT_CELL_MOVE_ACTIVE)
{
c->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
continue;
}
c->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
if (!c->IsInWorld())
continue;
Cell const& old_cell = c->GetCurrentCell();
Cell new_cell(c->GetPositionX(), c->GetPositionY());
if (c->IsFarVisible())
{
// Removes via GetCurrentCell, added back in AddToGrid
RemoveWorldObjectFromFarVisibleMap(c);
}
c->RemoveFromGrid();
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddToGrid(c, new_cell);
}
_creaturesToMove.clear();
}
void Map::MoveAllGameObjectsInMoveList()
{
for (std::vector::iterator itr = _gameObjectsToMove.begin(); itr != _gameObjectsToMove.end(); ++itr)
{
GameObject* go = *itr;
if (go->FindMap() != this)
continue;
if (go->_moveState != MAP_OBJECT_CELL_MOVE_ACTIVE)
{
go->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
continue;
}
go->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
if (!go->IsInWorld())
continue;
Cell const& old_cell = go->GetCurrentCell();
Cell new_cell(go->GetPositionX(), go->GetPositionY());
if (go->IsFarVisible())
{
// Removes via GetCurrentCell, added back in AddToGrid
RemoveWorldObjectFromFarVisibleMap(go);
}
go->RemoveFromGrid();
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddToGrid(go, new_cell);
}
_gameObjectsToMove.clear();
}
void Map::MoveAllDynamicObjectsInMoveList()
{
for (std::vector::iterator itr = _dynamicObjectsToMove.begin(); itr != _dynamicObjectsToMove.end(); ++itr)
{
DynamicObject* dynObj = *itr;
if (dynObj->FindMap() != this)
continue;
if (dynObj->_moveState != MAP_OBJECT_CELL_MOVE_ACTIVE)
{
dynObj->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
continue;
}
dynObj->_moveState = MAP_OBJECT_CELL_MOVE_NONE;
if (!dynObj->IsInWorld())
continue;
Cell const& old_cell = dynObj->GetCurrentCell();
Cell new_cell(dynObj->GetPositionX(), dynObj->GetPositionY());
dynObj->RemoveFromGrid();
if (old_cell.DiffGrid(new_cell))
EnsureGridLoaded(new_cell);
AddToGrid(dynObj, new_cell);
}
_dynamicObjectsToMove.clear();
}
bool Map::UnloadGrid(MapGridType& grid)
{
_mapGridManager.UnloadGrid(grid.GetX(), grid.GetY());
ASSERT(i_objectsToRemove.empty());
LOG_DEBUG("maps", "Unloading grid[{}, {}] for map {} finished", grid.GetX(), grid.GetY(), GetId());
return true;
}
void Map::RemoveAllPlayers()
{
if (HavePlayers())
{
for (MapRefMgr::iterator itr = m_mapRefMgr.begin(); itr != m_mapRefMgr.end(); ++itr)
{
Player* player = itr->GetSource();
if (!player->IsBeingTeleportedFar())
{
// this is happening for bg
LOG_ERROR("maps", "Map::UnloadAll: player {} is still in map {} during unload, this should not happen!", player->GetName(), GetId());
player->TeleportTo(player->m_homebindMapId, player->m_homebindX, player->m_homebindY, player->m_homebindZ, player->GetOrientation());
}
}
}
}
void Map::UnloadAll()
{
// clear all delayed moves, useless anyway do this moves before map unload.
_creaturesToMove.clear();
_gameObjectsToMove.clear();
for (GridRefMgr::iterator i = GridRefMgr::begin(); i != GridRefMgr::end();)
{
MapGridType& grid(*i->GetSource());
++i;
UnloadGrid(grid); // deletes the grid and removes it from the GridRefMgr
}
// pussywizard: crashfix, some npc can be left on transport (not a default passenger)
if (!AllTransportsEmpty())
AllTransportsRemovePassengers();
for (TransportsContainer::iterator itr = _transports.begin(); itr != _transports.end();)
{
Transport* transport = *itr;
++itr;
RemoveFromMap(transport, true);
}
_transports.clear();
for (auto& cellCorpsePair : _corpsesByGrid)
{
for (Corpse* corpse : cellCorpsePair.second)
{
corpse->RemoveFromWorld();
corpse->ResetMap();
delete corpse;
}
}
_corpsesByGrid.clear();
_corpsesByPlayer.clear();
_corpseBones.clear();
}
std::shared_ptr Map::GetGridTerrainDataSharedPtr(GridCoord const& gridCoord)
{
// ensure GridMap is created
EnsureGridCreated(gridCoord);
return _mapGridManager.GetGrid(gridCoord.x_coord, gridCoord.y_coord)->GetTerrainDataSharedPtr();
}
GridTerrainData* Map::GetGridTerrainData(GridCoord const& gridCoord)
{
if (!MapGridManager::IsValidGridCoordinates(gridCoord.x_coord, gridCoord.y_coord))
return nullptr;
// ensure GridMap is created
EnsureGridCreated(gridCoord);
return _mapGridManager.GetGrid(gridCoord.x_coord, gridCoord.y_coord)->GetTerrainData();
}
GridTerrainData* Map::GetGridTerrainData(float x, float y)
{
GridCoord const gridCoord = Acore::ComputeGridCoord(x, y);
return GetGridTerrainData(gridCoord);
}
float Map::GetWaterOrGroundLevel(uint32 phasemask, float x, float y, float z, float* ground /*= nullptr*/, bool /*swim = false*/, float collisionHeight) const
{
// we need ground level (including grid height version) for proper return water level in point
float ground_z = GetHeight(phasemask, x, y, z + Z_OFFSET_FIND_HEIGHT, true, 50.0f);
if (ground)
*ground = ground_z;
LiquidData const& liquidData = const_cast