/*
* Copyright (C) 2005-2010 MaNGOS
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "Map.h"
#include "MapManager.h"
#include "Player.h"
#include "Vehicle.h"
#include "GridNotifiers.h"
#include "Log.h"
#include "GridStates.h"
#include "CellImpl.h"
#include "InstanceData.h"
#include "GridNotifiersImpl.h"
#include "Transports.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "World.h"
#include "ScriptCalls.h"
#include "Group.h"
#include "MapRefManager.h"
#include "DBCEnums.h"
#include "MapInstanced.h"
#include "InstanceSaveMgr.h"
#include "VMapFactory.h"
#include "BattleGroundMgr.h"
struct ScriptAction
{
uint64 sourceGUID;
uint64 targetGUID;
uint64 ownerGUID; // owner of source if source is item
ScriptInfo const* script; // pointer to static script data
};
Map::~Map()
{
ObjectAccessor::DelinkMap(this);
UnloadAll(true);
if(!m_scriptSchedule.empty())
sWorld.DecreaseScheduledScriptCount(m_scriptSchedule.size());
if (m_instanceSave)
m_instanceSave->SetUsedByMapState(false); // field pointer can be deleted after this
}
void Map::LoadVMap(int gx,int gy)
{
// x and y are swapped !!
VMAP::VMAPLoadResult vmapLoadResult = VMAP::VMapFactory::createOrGetVMapManager()->loadMap((sWorld.GetDataPath()+ "vmaps").c_str(), GetId(), gx,gy);
switch(vmapLoadResult)
{
case VMAP::VMAP_LOAD_RESULT_OK:
DETAIL_LOG("VMAP loaded name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break;
case VMAP::VMAP_LOAD_RESULT_ERROR:
DETAIL_LOG("Could not load VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break;
case VMAP::VMAP_LOAD_RESULT_IGNORED:
DEBUG_LOG("Ignored VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)", GetMapName(), GetId(), gx,gy,gx,gy);
break;
}
}
void Map::LoadMap(int gx,int gy, bool reload)
{
if( i_InstanceId != 0 )
{
if(GridMaps[gx][gy])
return;
// load grid map for base map
if (!m_parentMap->GridMaps[gx][gy])
m_parentMap->EnsureGridCreated(GridPair(63-gx,63-gy));
((MapInstanced*)(m_parentMap))->AddGridMapReference(GridPair(gx,gy));
GridMaps[gx][gy] = m_parentMap->GridMaps[gx][gy];
return;
}
if(GridMaps[gx][gy] && !reload)
return;
//map already load, delete it before reloading (Is it necessary? Do we really need the ability the reload maps during runtime?)
if(GridMaps[gx][gy])
{
DETAIL_LOG("Unloading already loaded map %u before reloading.",i_id);
delete (GridMaps[gx][gy]);
GridMaps[gx][gy]=NULL;
}
// map file name
char *tmp=NULL;
int len = sWorld.GetDataPath().length()+strlen("maps/%03u%02u%02u.map")+1;
tmp = new char[len];
snprintf(tmp, len, (char *)(sWorld.GetDataPath()+"maps/%03u%02u%02u.map").c_str(),i_id,gx,gy);
DETAIL_LOG("Loading map %s",tmp);
// loading data
GridMaps[gx][gy] = new GridMap();
if (!GridMaps[gx][gy]->loadData(tmp))
{
sLog.outError("Error load map file: \n %s\n", tmp);
}
delete [] tmp;
}
void Map::LoadMapAndVMap(int gx,int gy)
{
LoadMap(gx,gy);
if(i_InstanceId == 0)
LoadVMap(gx, gy); // Only load the data for the base map
}
Map::Map(uint32 id, time_t expiry, uint32 InstanceId, uint8 SpawnMode, Map* _parent)
: i_mapEntry (sMapStore.LookupEntry(id)), i_spawnMode(SpawnMode),
i_id(id), i_InstanceId(InstanceId), m_unloadTimer(0),
m_VisibleDistance(DEFAULT_VISIBILITY_DISTANCE), m_instanceSave(NULL),
m_activeNonPlayersIter(m_activeNonPlayers.end()),
i_gridExpiry(expiry), m_parentMap(_parent ? _parent : this),
m_lastUpdateTime(getMSTime()) // expected that next update call will not long after map creating
{
for(unsigned int idx=0; idx < MAX_NUMBER_OF_GRIDS; ++idx)
{
for(unsigned int j=0; j < MAX_NUMBER_OF_GRIDS; ++j)
{
//z code
GridMaps[idx][j] =NULL;
setNGrid(NULL, idx, j);
}
}
ObjectAccessor::LinkMap(this);
//lets initialize visibility distance for map
Map::InitVisibilityDistance();
}
void Map::InitVisibilityDistance()
{
//init visibility for continents
m_VisibleDistance = World::GetMaxVisibleDistanceOnContinents();
}
// Template specialization of utility methods
template
void Map::AddToGrid(T* obj, NGridType *grid, Cell const& cell)
{
(*grid)(cell.CellX(), cell.CellY()).template AddGridObject(obj);
}
template<>
void Map::AddToGrid(Player* obj, NGridType *grid, Cell const& cell)
{
(*grid)(cell.CellX(), cell.CellY()).AddWorldObject(obj);
}
template<>
void Map::AddToGrid(Corpse *obj, NGridType *grid, Cell const& cell)
{
// add to world object registry in grid
if(obj->GetType()!=CORPSE_BONES)
{
(*grid)(cell.CellX(), cell.CellY()).AddWorldObject(obj);
}
// add to grid object store
else
{
(*grid)(cell.CellX(), cell.CellY()).AddGridObject(obj);
}
}
template<>
void Map::AddToGrid(Creature* obj, NGridType *grid, Cell const& cell)
{
// add to world object registry in grid
if(obj->IsPet() || obj->IsVehicle())
{
(*grid)(cell.CellX(), cell.CellY()).AddWorldObject(obj);
obj->SetCurrentCell(cell);
}
// add to grid object store
else
{
(*grid)(cell.CellX(), cell.CellY()).AddGridObject(obj);
obj->SetCurrentCell(cell);
}
}
template
void Map::RemoveFromGrid(T* obj, NGridType *grid, Cell const& cell)
{
(*grid)(cell.CellX(), cell.CellY()).template RemoveGridObject(obj);
}
template<>
void Map::RemoveFromGrid(Player* obj, NGridType *grid, Cell const& cell)
{
(*grid)(cell.CellX(), cell.CellY()).RemoveWorldObject(obj);
}
template<>
void Map::RemoveFromGrid(Corpse *obj, NGridType *grid, Cell const& cell)
{
// remove from world object registry in grid
if(obj->GetType()!=CORPSE_BONES)
{
(*grid)(cell.CellX(), cell.CellY()).RemoveWorldObject(obj);
}
// remove from grid object store
else
{
(*grid)(cell.CellX(), cell.CellY()).RemoveGridObject(obj);
}
}
template<>
void Map::RemoveFromGrid(Creature* obj, NGridType *grid, Cell const& cell)
{
// remove from world object registry in grid
if(obj->IsPet() || obj->IsVehicle())
{
(*grid)(cell.CellX(), cell.CellY()).RemoveWorldObject(obj);
}
// remove from grid object store
else
{
(*grid)(cell.CellX(), cell.CellY()).RemoveGridObject(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* pl)
{
sObjectAccessor.RemoveObject(pl);
delete pl;
}
template
void Map::AddNotifier(T* , Cell const& , CellPair const& )
{
}
template<>
void Map::AddNotifier(Player* obj, Cell const& cell, CellPair const& cellpair)
{
PlayerRelocationNotify(obj,cell,cellpair);
}
template<>
void Map::AddNotifier(Creature* obj, Cell const&, CellPair const&)
{
obj->SetNeedNotify();
}
void
Map::EnsureGridCreated(const GridPair &p)
{
if(!getNGrid(p.x_coord, p.y_coord))
{
Guard guard(*this);
if(!getNGrid(p.x_coord, p.y_coord))
{
setNGrid(new NGridType(p.x_coord*MAX_NUMBER_OF_GRIDS + p.y_coord, p.x_coord, p.y_coord, i_gridExpiry, sWorld.getConfig(CONFIG_BOOL_GRID_UNLOAD)),
p.x_coord, p.y_coord);
// build a linkage between this map and NGridType
buildNGridLinkage(getNGrid(p.x_coord, p.y_coord));
getNGrid(p.x_coord, p.y_coord)->SetGridState(GRID_STATE_IDLE);
//z coord
int gx = (MAX_NUMBER_OF_GRIDS - 1) - p.x_coord;
int gy = (MAX_NUMBER_OF_GRIDS - 1) - p.y_coord;
if(!GridMaps[gx][gy])
LoadMapAndVMap(gx,gy);
}
}
}
void
Map::EnsureGridLoadedAtEnter(const Cell &cell, Player *player)
{
NGridType *grid;
if(EnsureGridLoaded(cell))
{
grid = getNGrid(cell.GridX(), cell.GridY());
if (player)
{
DEBUG_FILTER_LOG(LOG_FILTER_PLAYER_MOVES, "Player %s enter cell[%u,%u] triggers of loading grid[%u,%u] on map %u", player->GetName(), cell.CellX(), cell.CellY(), cell.GridX(), cell.GridY(), i_id);
}
else
{
DEBUG_FILTER_LOG(LOG_FILTER_PLAYER_MOVES, "Active object nearby triggers of loading grid [%u,%u] on map %u", cell.GridX(), cell.GridY(), i_id);
}
ResetGridExpiry(*getNGrid(cell.GridX(), cell.GridY()), 0.1f);
grid->SetGridState(GRID_STATE_ACTIVE);
}
else
grid = getNGrid(cell.GridX(), cell.GridY());
if (player)
AddToGrid(player,grid,cell);
}
bool Map::EnsureGridLoaded(const Cell &cell)
{
EnsureGridCreated(GridPair(cell.GridX(), cell.GridY()));
NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
MANGOS_ASSERT(grid != NULL);
if( !isGridObjectDataLoaded(cell.GridX(), cell.GridY()) )
{
//it's important to set it loaded before loading!
//otherwise there is a possibility of infinity chain (grid loading will be called many times for the same grid)
//possible scenario:
//active object A(loaded with loader.LoadN call and added to the map)
//summons some active object B, while B added to map grid loading called again and so on..
setGridObjectDataLoaded(true,cell.GridX(), cell.GridY());
ObjectGridLoader loader(*grid, this, cell);
loader.LoadN();
// Add resurrectable corpses to world object list in grid
sObjectAccessor.AddCorpsesToGrid(GridPair(cell.GridX(),cell.GridY()),(*grid)(cell.CellX(), cell.CellY()), this);
return true;
}
return false;
}
void Map::LoadGrid(const Cell& cell, bool no_unload)
{
EnsureGridLoaded(cell);
if(no_unload)
getNGrid(cell.GridX(), cell.GridY())->setUnloadExplicitLock(true);
}
bool Map::Add(Player *player)
{
player->GetMapRef().link(this, player);
player->SetMap(this);
// update player state for other player and visa-versa
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
Cell cell(p);
EnsureGridLoadedAtEnter(cell, player);
player->AddToWorld();
SendInitSelf(player);
SendInitTransports(player);
NGridType* grid = getNGrid(cell.GridX(), cell.GridY());
player->GetViewPoint().Event_AddedToWorld(&(*grid)(cell.CellX(), cell.CellY()));
UpdateObjectVisibility(player,cell,p);
AddNotifier(player,cell,p);
return true;
}
template
void
Map::Add(T *obj)
{
MANGOS_ASSERT(obj);
CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::Add: Object (GUID: %u TypeId: %u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), p.x_coord, p.y_coord);
return;
}
obj->SetMap(this);
Cell cell(p);
if(obj->isActiveObject())
EnsureGridLoadedAtEnter(cell);
else
EnsureGridCreated(GridPair(cell.GridX(), cell.GridY()));
NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
MANGOS_ASSERT( grid != NULL );
AddToGrid(obj,grid,cell);
obj->AddToWorld();
if(obj->isActiveObject())
AddToActive(obj);
DEBUG_LOG("Object %u enters grid[%u,%u]", GUID_LOPART(obj->GetGUID()), cell.GridX(), cell.GridY());
obj->GetViewPoint().Event_AddedToWorld(&(*grid)(cell.CellX(), cell.CellY()));
UpdateObjectVisibility(obj,cell,p);
AddNotifier(obj,cell,p);
}
void Map::MessageBroadcast(Player *player, WorldPacket *msg, bool to_self)
{
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::MessageBroadcast: Player (GUID: %u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", player->GetGUIDLow(), player->GetPositionX(), player->GetPositionY(), p.x_coord, p.y_coord);
return;
}
Cell cell(p);
cell.SetNoCreate();
if( !loaded(GridPair(cell.data.Part.grid_x, cell.data.Part.grid_y)) )
return;
MaNGOS::MessageDeliverer post_man(*player, msg, to_self);
TypeContainerVisitor message(post_man);
cell.Visit(p, message, *this, *player, GetVisibilityDistance());
}
void Map::MessageBroadcast(WorldObject *obj, WorldPacket *msg)
{
CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::MessageBroadcast: Object (GUID: %u TypeId: %u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), p.x_coord, p.y_coord);
return;
}
Cell cell(p);
cell.SetNoCreate();
if( !loaded(GridPair(cell.data.Part.grid_x, cell.data.Part.grid_y)) )
return;
//TODO: currently on continents when Visibility.Distance.InFlight > Visibility.Distance.Continents
//we have alot of blinking mobs because monster move packet send is broken...
MaNGOS::ObjectMessageDeliverer post_man(*obj,msg);
TypeContainerVisitor message(post_man);
cell.Visit(p, message, *this, *obj, GetVisibilityDistance());
}
void Map::MessageDistBroadcast(Player *player, WorldPacket *msg, float dist, bool to_self, bool own_team_only)
{
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::MessageBroadcast: Player (GUID: %u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", player->GetGUIDLow(), player->GetPositionX(), player->GetPositionY(), p.x_coord, p.y_coord);
return;
}
Cell cell(p);
cell.SetNoCreate();
if( !loaded(GridPair(cell.data.Part.grid_x, cell.data.Part.grid_y)) )
return;
MaNGOS::MessageDistDeliverer post_man(*player, msg, dist, to_self, own_team_only);
TypeContainerVisitor message(post_man);
cell.Visit(p, message, *this, *player, dist);
}
void Map::MessageDistBroadcast(WorldObject *obj, WorldPacket *msg, float dist)
{
CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::MessageBroadcast: Object (GUID: %u TypeId: %u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), p.x_coord, p.y_coord);
return;
}
Cell cell(p);
cell.SetNoCreate();
if( !loaded(GridPair(cell.data.Part.grid_x, cell.data.Part.grid_y)) )
return;
MaNGOS::ObjectMessageDistDeliverer post_man(*obj, msg, dist);
TypeContainerVisitor message(post_man);
cell.Visit(p, message, *this, *obj, dist);
}
bool Map::loaded(const GridPair &p) const
{
return ( getNGrid(p.x_coord, p.y_coord) && isGridObjectDataLoaded(p.x_coord, p.y_coord) );
}
void Map::Update(uint32 time_, uint32 diff)
{
m_lastUpdateTime = time_;
/// update players at tick
for(m_mapRefIter = m_mapRefManager.begin(); m_mapRefIter != m_mapRefManager.end(); ++m_mapRefIter)
if (Player* plr = m_mapRefIter->getSource())
if (plr->IsInWorld())
plr->Update(diff, diff);
/// update active cells around players and active objects
resetMarkedCells();
// the player iterator is stored in the map object
// to make sure calls to Map::Remove don't invalidate it
for(m_mapRefIter = m_mapRefManager.begin(); m_mapRefIter != m_mapRefManager.end(); ++m_mapRefIter)
{
Player* plr = m_mapRefIter->getSource();
if(!plr->IsInWorld())
continue;
CellPair standing_cell(MaNGOS::ComputeCellPair(plr->GetPositionX(), plr->GetPositionY()));
// Check for correctness of standing_cell, it also avoids problems with update_cell
if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
continue;
// the overloaded operators handle range checking
// so ther's no need for range checking inside the loop
CellPair begin_cell(standing_cell), end_cell(standing_cell);
//lets update mobs/objects in ALL visible cells around player!
CellArea area = Cell::CalculateCellArea(plr->GetPositionX(), plr->GetPositionY(), GetVisibilityDistance());
area.ResizeBorders(begin_cell, end_cell);
for(uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x)
{
for(uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
{
// marked cells are those that have been visited
// don't visit the same cell twice
uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x;
if(!isCellMarked(cell_id))
{
markCell(cell_id);
CellPair pair(x,y);
Cell cell(pair);
cell.SetNoCreate();
uint32 realdiff = diff;
if (loaded(cell.gridPair()) )
realdiff = (*getNGrid(cell.GridX(),cell.GridY()))(cell.CellX(),cell.CellY()).SetLastUpdateTimeAndReturnDiff(time_);
MaNGOS::ObjectUpdater updater(realdiff, diff);
// for creature
TypeContainerVisitor grid_object_update(updater);
// for pets
TypeContainerVisitor world_object_update(updater);
Visit(cell, grid_object_update);
Visit(cell, world_object_update);
}
}
}
}
// non-player active objects
if(!m_activeNonPlayers.empty())
{
for(m_activeNonPlayersIter = m_activeNonPlayers.begin(); m_activeNonPlayersIter != m_activeNonPlayers.end(); )
{
// skip not in world
WorldObject* obj = *m_activeNonPlayersIter;
// step before processing, in this case if Map::Remove remove next object we correctly
// step to next-next, and if we step to end() then newly added objects can wait next update.
++m_activeNonPlayersIter;
if(!obj->IsInWorld())
continue;
CellPair standing_cell(MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY()));
// Check for correctness of standing_cell, it also avoids problems with update_cell
if (standing_cell.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
continue;
// the overloaded operators handle range checking
// so ther's no need for range checking inside the loop
CellPair begin_cell(standing_cell), end_cell(standing_cell);
begin_cell << 1; begin_cell -= 1; // upper left
end_cell >> 1; end_cell += 1; // lower right
for(uint32 x = begin_cell.x_coord; x <= end_cell.x_coord; ++x)
{
for(uint32 y = begin_cell.y_coord; y <= end_cell.y_coord; ++y)
{
// marked cells are those that have been visited
// don't visit the same cell twice
uint32 cell_id = (y * TOTAL_NUMBER_OF_CELLS_PER_MAP) + x;
if(!isCellMarked(cell_id))
{
markCell(cell_id);
CellPair pair(x,y);
Cell cell(pair);
cell.SetNoCreate();
uint32 realdiff = diff;
if (loaded(cell.gridPair()) )
realdiff = (*getNGrid(cell.GridX(),cell.GridY()))(cell.CellX(),cell.CellY()).SetLastUpdateTimeAndReturnDiff(time_);
MaNGOS::ObjectUpdater updater(realdiff, diff);
// for creature
TypeContainerVisitor grid_object_update(updater);
// for pets
TypeContainerVisitor world_object_update(updater);
Visit(cell, grid_object_update);
Visit(cell, world_object_update);
}
}
}
}
}
// Send world objects and item update field changes
SendObjectUpdates();
// Don't unload grids if it's battleground, since we may have manually added GOs,creatures, those doesn't load from DB at grid re-load !
// This isn't really bother us, since as soon as we have instanced BG-s, the whole map unloads as the BG gets ended
if (!IsBattleGroundOrArena())
{
for (GridRefManager::iterator i = GridRefManager::begin(); i != GridRefManager::end(); )
{
NGridType *grid = i->getSource();
GridInfo *info = i->getSource()->getGridInfoRef();
++i; // The update might delete the map and we need the next map before the iterator gets invalid
MANGOS_ASSERT(grid->GetGridState() >= 0 && grid->GetGridState() < MAX_GRID_STATE);
sMapMgr.UpdateGridState(grid->GetGridState(), *this, *grid, *info, grid->getX(), grid->getY(), diff);
}
}
///- Process necessary scripts
if (!m_scriptSchedule.empty())
ScriptsProcess();
}
void Map::Remove(Player *player, bool remove)
{
if(remove)
player->CleanupsBeforeDelete();
else
player->RemoveFromWorld();
// this may be called during Map::Update
// after decrement+unlink, ++m_mapRefIter will continue correctly
// when the first element of the list is being removed
// nocheck_prev will return the padding element of the RefManager
// instead of NULL in the case of prev
if(m_mapRefIter == player->GetMapRef())
m_mapRefIter = m_mapRefIter->nocheck_prev();
player->GetMapRef().unlink();
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
{
// invalid coordinates
player->ResetMap();
if( remove )
DeleteFromWorld(player);
return;
}
Cell cell(p);
if( !getNGrid(cell.data.Part.grid_x, cell.data.Part.grid_y) )
{
sLog.outError("Map::Remove() i_grids was NULL x:%d, y:%d",cell.data.Part.grid_x,cell.data.Part.grid_y);
return;
}
DEBUG_FILTER_LOG(LOG_FILTER_PLAYER_MOVES, "Remove player %s from grid[%u,%u]", player->GetName(), cell.GridX(), cell.GridY());
NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
MANGOS_ASSERT(grid != NULL);
RemoveFromGrid(player,grid,cell);
SendRemoveTransports(player);
UpdateObjectVisibility(player,cell,p);
player->ResetMap();
if( remove )
DeleteFromWorld(player);
}
template
void
Map::Remove(T *obj, bool remove)
{
CellPair p = MaNGOS::ComputeCellPair(obj->GetPositionX(), obj->GetPositionY());
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog.outError("Map::Remove: Object (GUID: %u TypeId:%u) have invalid coordinates X:%f Y:%f grid cell [%u:%u]", obj->GetGUIDLow(), obj->GetTypeId(), obj->GetPositionX(), obj->GetPositionY(), p.x_coord, p.y_coord);
return;
}
Cell cell(p);
if( !loaded(GridPair(cell.data.Part.grid_x, cell.data.Part.grid_y)) )
return;
DEBUG_LOG("Remove object (GUID: %u TypeId:%u) from grid[%u,%u]", obj->GetGUIDLow(), obj->GetTypeId(), cell.data.Part.grid_x, cell.data.Part.grid_y);
NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
MANGOS_ASSERT( grid != NULL );
if(obj->isActiveObject())
RemoveFromActive(obj);
if(remove)
obj->CleanupsBeforeDelete();
else
obj->RemoveFromWorld();
UpdateObjectVisibility(obj,cell,p); // i think will be better to call this function while object still in grid, this changes nothing but logically is better(as for me)
RemoveFromGrid(obj,grid,cell);
obj->ResetMap();
if( remove )
{
// if option set then object already saved at this moment
if(!sWorld.getConfig(CONFIG_BOOL_SAVE_RESPAWN_TIME_IMMEDIATLY))
obj->SaveRespawnTime();
DeleteFromWorld(obj);
}
}
void
Map::PlayerRelocation(Player *player, float x, float y, float z, float orientation)
{
MANGOS_ASSERT(player);
CellPair old_val = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
CellPair new_val = MaNGOS::ComputeCellPair(x, y);
Cell old_cell(old_val);
Cell new_cell(new_val);
bool same_cell = (new_cell == old_cell);
player->Relocate(x, y, z, orientation);
if( old_cell.DiffGrid(new_cell) || old_cell.DiffCell(new_cell) )
{
DEBUG_FILTER_LOG(LOG_FILTER_PLAYER_MOVES, "Player %s relocation grid[%u,%u]cell[%u,%u]->grid[%u,%u]cell[%u,%u]", player->GetName(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.GridX(), new_cell.GridY(), new_cell.CellX(), new_cell.CellY());
NGridType* oldGrid = getNGrid(old_cell.GridX(), old_cell.GridY());
RemoveFromGrid(player, oldGrid,old_cell);
if( !old_cell.DiffGrid(new_cell) )
AddToGrid(player, oldGrid,new_cell);
else
EnsureGridLoadedAtEnter(new_cell, player);
NGridType* newGrid = getNGrid(new_cell.GridX(), new_cell.GridY());
player->GetViewPoint().Event_GridChanged(&(*newGrid)(new_cell.CellX(),new_cell.CellY()));
}
player->GetViewPoint().Call_UpdateVisibilityForOwner();
// if move then update what player see and who seen
UpdateObjectVisibility(player, new_cell, new_val);
PlayerRelocationNotify(player,new_cell,new_val);
NGridType* newGrid = getNGrid(new_cell.GridX(), new_cell.GridY());
if( !same_cell && newGrid->GetGridState()!= GRID_STATE_ACTIVE )
{
ResetGridExpiry(*newGrid, 0.1f);
newGrid->SetGridState(GRID_STATE_ACTIVE);
}
}
void
Map::CreatureRelocation(Creature *creature, float x, float y, float z, float ang)
{
MANGOS_ASSERT(CheckGridIntegrity(creature,false));
Cell old_cell = creature->GetCurrentCell();
CellPair new_val = MaNGOS::ComputeCellPair(x, y);
Cell new_cell(new_val);
// delay creature move for grid/cell to grid/cell moves
if (old_cell.DiffCell(new_cell) || old_cell.DiffGrid(new_cell))
{
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) added to moving list from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u].", creature->GetGUIDLow(), creature->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.GridX(), new_cell.GridY(), new_cell.CellX(), new_cell.CellY());
// do move or do move to respawn or remove creature if previous all fail
if(CreatureCellRelocation(creature,new_cell))
{
// update pos
creature->Relocate(x, y, z, ang);
// in diffcell/diffgrid case notifiers called in Creature::Update
creature->SetNeedNotify();
}
else
{
// if creature can't be move in new cell/grid (not loaded) move it to repawn cell/grid
// creature coordinates will be updated and notifiers send
if(!CreatureRespawnRelocation(creature))
{
// ... or unload (if respawn grid also not loaded)
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u ) can't be move to unloaded respawn grid.",creature->GetGUIDLow(),creature->GetEntry());
creature->SetNeedNotify();
}
}
}
else
{
creature->Relocate(x, y, z, ang);
creature->SetNeedNotify();
}
creature->GetViewPoint().Call_UpdateVisibilityForOwner();
MANGOS_ASSERT(CheckGridIntegrity(creature,true));
}
bool Map::CreatureCellRelocation(Creature *c, Cell new_cell)
{
Cell const& old_cell = c->GetCurrentCell();
if(!old_cell.DiffGrid(new_cell) ) // in same grid
{
// if in same cell then none do
if(old_cell.DiffCell(new_cell))
{
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) moved in grid[%u,%u] from cell[%u,%u] to cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.CellX(), new_cell.CellY());
RemoveFromGrid(c,getNGrid(old_cell.GridX(), old_cell.GridY()),old_cell);
NGridType* new_grid = getNGrid(new_cell.GridX(), new_cell.GridY());
AddToGrid(c,new_grid,new_cell);
c->GetViewPoint().Event_GridChanged( &(*new_grid)(new_cell.CellX(),new_cell.CellY()) );
}
else
{
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) move in same grid[%u,%u]cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY());
}
return true;
}
// in diff. grids but active creature
if(c->isActiveObject())
{
EnsureGridLoadedAtEnter(new_cell);
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Active creature (GUID: %u Entry: %u) moved from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.GridX(), new_cell.GridY(), new_cell.CellX(), new_cell.CellY());
RemoveFromGrid(c,getNGrid(old_cell.GridX(), old_cell.GridY()),old_cell);
NGridType* new_grid = getNGrid(new_cell.GridX(), new_cell.GridY());
AddToGrid(c,new_grid,new_cell);
c->GetViewPoint().Event_GridChanged( &(*new_grid)(new_cell.CellX(),new_cell.CellY()) );
return true;
}
// in diff. loaded grid normal creature
if(loaded(GridPair(new_cell.GridX(), new_cell.GridY())))
{
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) moved from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.GridX(), new_cell.GridY(), new_cell.CellX(), new_cell.CellY());
RemoveFromGrid(c,getNGrid(old_cell.GridX(), old_cell.GridY()),old_cell);
{
EnsureGridCreated(GridPair(new_cell.GridX(), new_cell.GridY()));
NGridType* new_grid = getNGrid(new_cell.GridX(), new_cell.GridY());
AddToGrid(c,new_grid,new_cell);
c->GetViewPoint().Event_GridChanged( &(*new_grid)(new_cell.CellX(),new_cell.CellY()) );
}
return true;
}
// fail to move: normal creature attempt move to unloaded grid
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) attempt move from grid[%u,%u]cell[%u,%u] to unloaded grid[%u,%u]cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), old_cell.GridX(), old_cell.GridY(), old_cell.CellX(), old_cell.CellY(), new_cell.GridX(), new_cell.GridY(), new_cell.CellX(), new_cell.CellY());
return false;
}
bool Map::CreatureRespawnRelocation(Creature *c)
{
float resp_x, resp_y, resp_z, resp_o;
c->GetRespawnCoord(resp_x, resp_y, resp_z, &resp_o);
CellPair resp_val = MaNGOS::ComputeCellPair(resp_x, resp_y);
Cell resp_cell(resp_val);
c->CombatStop();
c->GetMotionMaster()->Clear();
DEBUG_FILTER_LOG(LOG_FILTER_CREATURE_MOVES, "Creature (GUID: %u Entry: %u) will moved from grid[%u,%u]cell[%u,%u] to respawn grid[%u,%u]cell[%u,%u].", c->GetGUIDLow(), c->GetEntry(), c->GetCurrentCell().GridX(), c->GetCurrentCell().GridY(), c->GetCurrentCell().CellX(), c->GetCurrentCell().CellY(), resp_cell.GridX(), resp_cell.GridY(), resp_cell.CellX(), resp_cell.CellY());
// teleport it to respawn point (like normal respawn if player see)
if(CreatureCellRelocation(c,resp_cell))
{
c->Relocate(resp_x, resp_y, resp_z, resp_o);
c->GetMotionMaster()->Initialize(); // prevent possible problems with default move generators
c->SetNeedNotify();
return true;
}
else
return false;
}
bool Map::UnloadGrid(const uint32 &x, const uint32 &y, bool pForce)
{
NGridType *grid = getNGrid(x, y);
MANGOS_ASSERT( grid != NULL);
{
if(!pForce && ActiveObjectsNearGrid(x, y) )
return false;
DEBUG_LOG("Unloading grid[%u,%u] for map %u", x,y, i_id);
ObjectGridUnloader unloader(*grid);
// Finish remove and delete all creatures with delayed remove before moving to respawn grids
// Must know real mob position before move
RemoveAllObjectsInRemoveList();
// move creatures to respawn grids if this is diff.grid or to remove list
unloader.MoveToRespawnN();
// Finish remove and delete all creatures with delayed remove before unload
RemoveAllObjectsInRemoveList();
unloader.UnloadN();
delete getNGrid(x, y);
setNGrid(NULL, x, y);
}
int gx = (MAX_NUMBER_OF_GRIDS - 1) - x;
int gy = (MAX_NUMBER_OF_GRIDS - 1) - y;
// delete grid map, but don't delete if it is from parent map (and thus only reference)
//+++if (GridMaps[gx][gy]) don't check for GridMaps[gx][gy], we might have to unload vmaps
{
if (i_InstanceId == 0)
{
if(GridMaps[gx][gy])
{
GridMaps[gx][gy]->unloadData();
delete GridMaps[gx][gy];
}
VMAP::VMapFactory::createOrGetVMapManager()->unloadMap(GetId(), gx, gy);
}
else
((MapInstanced*)m_parentMap)->RemoveGridMapReference(GridPair(gx, gy));
GridMaps[gx][gy] = NULL;
}
DEBUG_LOG("Unloading grid[%u,%u] for map %u finished", x,y, i_id);
return true;
}
void Map::UnloadAll(bool pForce)
{
for (GridRefManager::iterator i = GridRefManager::begin(); i != GridRefManager::end(); )
{
NGridType &grid(*i->getSource());
++i;
UnloadGrid(grid.getX(), grid.getY(), pForce); // deletes the grid and removes it from the GridRefManager
}
}
MapDifficulty const* Map::GetMapDifficulty() const
{
return GetMapDifficultyData(GetId(),GetDifficulty());
}
uint32 Map::GetMaxPlayers() const
{
if(MapDifficulty const* mapDiff = GetMapDifficulty())
{
if(mapDiff->maxPlayers || IsRegularDifficulty()) // Normal case (expect that regular difficulty always have correct maxplayers)
return mapDiff->maxPlayers;
else // DBC have 0 maxplayers for heroic instances with expansion < 2
{ // The heroic entry exists, so we don't have to check anything, simply return normal max players
MapDifficulty const* normalDiff = GetMapDifficultyData(i_id, REGULAR_DIFFICULTY);
return normalDiff ? normalDiff->maxPlayers : 0;
}
}
else // I'd rather ASSERT(false);
return 0;
}
uint32 Map::GetMaxResetDelay() const
{
return InstanceResetScheduler::GetMaxResetTimeFor(GetMapDifficulty());
}
inline GridMap *Map::GetGrid(float x, float y)
{
// half opt method
int gx=(int)(32-x/SIZE_OF_GRIDS); //grid x
int gy=(int)(32-y/SIZE_OF_GRIDS); //grid y
// ensure GridMap is loaded
EnsureGridCreated(GridPair(63-gx,63-gy));
return GridMaps[gx][gy];
}
float Map::GetHeight(float x, float y, float z, bool pUseVmaps, float maxSearchDist) const
{
// find raw .map surface under Z coordinates
float mapHeight;
float z2 = z + 2.f;
if (GridMap *gmap = const_cast