2008-10-02 16:23:55 -05:00
/*
2009-03-31 14:40:54 -06:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2009-03-31 14:40:54 -06:00
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* 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
2008-11-07 09:36:46 -06:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* 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
2008-11-07 09:36:46 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2008-10-20 12:23:56 -05:00
#include "MapManager.h"
2008-10-02 16:23:55 -05:00
#include "Player.h"
#include "GridNotifiers.h"
#include "WorldSession.h"
#include "Log.h"
#include "GridStates.h"
#include "CellImpl.h"
#include "InstanceData.h"
#include "Map.h"
#include "GridNotifiersImpl.h"
#include "Config/ConfigEnv.h"
#include "Transports.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "World.h"
#include "ScriptCalls.h"
#include "Group.h"
2008-11-18 19:40:06 -06:00
#include "MapRefManager.h"
2008-10-02 16:23:55 -05:00
#include "MapInstanced.h"
#include "InstanceSaveMgr.h"
#include "VMapFactory.h"
#define DEFAULT_GRID_EXPIRY 300
#define MAX_GRID_LOAD_TIME 50
// magic *.map header
2009-02-21 08:48:08 -05:00
const char MAP_MAGIC [] = "MAP_2.50" ;
2008-10-02 16:23:55 -05:00
GridState * si_GridStates [ MAX_GRID_STATE ];
Map ::~ Map ()
{
2009-02-25 17:53:05 -06:00
UnloadAll ();
2008-10-02 16:23:55 -05:00
}
bool Map :: ExistMap ( uint32 mapid , int x , int y )
{
int len = sWorld . GetDataPath (). length () + strlen ( "maps/%03u%02u%02u.map" ) + 1 ;
char * tmp = new char [ len ];
snprintf ( tmp , len , ( char * )( sWorld . GetDataPath () + "maps/%03u%02u%02u.map" ). c_str (), mapid , x , y );
FILE * pf = fopen ( tmp , "rb" );
if ( ! pf )
{
sLog . outError ( "Check existing of map file '%s': not exist!" , tmp );
delete [] tmp ;
return false ;
}
char magic [ 8 ];
fread ( magic , 1 , 8 , pf );
if ( strncmp ( MAP_MAGIC , magic , 8 ))
{
sLog . outError ( "Map file '%s' is non-compatible version (outdated?). Please, create new using ad.exe program." , tmp );
delete [] tmp ;
fclose ( pf ); //close file before return
return false ;
}
delete [] tmp ;
fclose ( pf );
return true ;
}
bool Map :: ExistVMap ( uint32 mapid , int x , int y )
{
if ( VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ())
{
if ( vmgr -> isMapLoadingEnabled ())
{
2008-11-07 09:36:46 -06:00
// x and y are swapped !! => fixed now
2008-10-02 16:23:55 -05:00
bool exists = vmgr -> existsMap (( sWorld . GetDataPath () + "vmaps" ). c_str (), mapid , x , y );
if ( ! exists )
{
std :: string name = vmgr -> getDirFileName ( mapid , x , y );
sLog . outError ( "VMap file '%s' is missing or point to wrong version vmap file, redo vmaps with latest vmap_assembler.exe program" , ( sWorld . GetDataPath () + "vmaps/" + name ). c_str ());
return false ;
}
}
}
return true ;
}
void Map :: LoadVMap ( int x , int y )
{
2008-11-07 09:36:46 -06:00
// x and y are swapped !!
2008-10-02 16:23:55 -05:00
int vmapLoadResult = VMAP :: VMapFactory :: createOrGetVMapManager () -> loadMap (( sWorld . GetDataPath () + "vmaps" ). c_str (), GetId (), x , y );
switch ( vmapLoadResult )
{
case VMAP :: VMAP_LOAD_RESULT_OK :
sLog . outDetail ( "VMAP loaded name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), x , y , x , y );
break ;
case VMAP :: VMAP_LOAD_RESULT_ERROR :
sLog . outDetail ( "Could not load VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), x , y , x , y );
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 (), x , y , x , y );
break ;
}
}
void Map :: LoadMap ( uint32 mapid , uint32 instanceid , int x , int y )
{
if ( instanceid != 0 )
{
if ( GridMaps [ x ][ y ])
return ;
Map * baseMap = const_cast < Map *> ( MapManager :: Instance (). GetBaseMap ( mapid ));
// load gridmap for base map
if ( ! baseMap -> GridMaps [ x ][ y ])
baseMap -> EnsureGridCreated ( GridPair ( 63 - x , 63 - y ));
//+++ if (!baseMap->GridMaps[x][y]) don't check for GridMaps[gx][gy], we need the management for vmaps
// return;
(( MapInstanced * )( baseMap )) -> AddGridMapReference ( GridPair ( x , y ));
GridMaps [ x ][ y ] = baseMap -> GridMaps [ x ][ y ];
return ;
}
2008-11-07 09:36:46 -06:00
//map already load, delete it before reloading (Is it necessary? Do we really need the ability the reload maps during runtime?)
2008-10-02 16:23:55 -05:00
if ( GridMaps [ x ][ y ])
{
sLog . outDetail ( "Unloading already loaded map %u before reloading." , mapid );
delete ( GridMaps [ x ][ y ]);
GridMaps [ x ][ y ] = NULL ;
}
// map file name
char * tmp = NULL ;
// Pihhan: dataPath length + "maps/" + 3+2+2+ ".map" length may be > 32 !
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 (), mapid , x , y );
sLog . outDetail ( "Loading map %s" , tmp );
// loading data
FILE * pf = fopen ( tmp , "rb" );
if ( ! pf )
{
delete [] tmp ;
return ;
}
char magic [ 8 ];
fread ( magic , 1 , 8 , pf );
if ( strncmp ( MAP_MAGIC , magic , 8 ))
{
sLog . outError ( "Map file '%s' is non-compatible version (outdated?). Please, create new using ad.exe program." , tmp );
delete [] tmp ;
fclose ( pf ); //close file before return
return ;
}
delete [] tmp ;
GridMap * buf = new GridMap ;
fread ( buf , 1 , sizeof ( GridMap ), pf );
fclose ( pf );
GridMaps [ x ][ y ] = buf ;
}
void Map :: LoadMapAndVMap ( uint32 mapid , uint32 instanceid , int x , int y )
{
LoadMap ( mapid , instanceid , x , y );
if ( instanceid == 0 )
LoadVMap ( x , y ); // Only load the data for the base map
}
void Map :: InitStateMachine ()
{
si_GridStates [ GRID_STATE_INVALID ] = new InvalidState ;
si_GridStates [ GRID_STATE_ACTIVE ] = new ActiveState ;
si_GridStates [ GRID_STATE_IDLE ] = new IdleState ;
si_GridStates [ GRID_STATE_REMOVAL ] = new RemovalState ;
}
void Map :: DeleteStateMachine ()
{
delete si_GridStates [ GRID_STATE_INVALID ];
delete si_GridStates [ GRID_STATE_ACTIVE ];
delete si_GridStates [ GRID_STATE_IDLE ];
delete si_GridStates [ GRID_STATE_REMOVAL ];
}
Map :: Map ( uint32 id , time_t expiry , uint32 InstanceId , uint8 SpawnMode )
2009-02-25 20:52:20 -06:00
: i_mapEntry ( sMapStore . LookupEntry ( id )), i_spawnMode ( SpawnMode ),
i_id ( id ), i_InstanceId ( InstanceId ), m_unloadTimer ( 0 ), i_gridExpiry ( expiry ),
m_activeNonPlayersIter ( m_activeNonPlayers . end ())
2009-04-02 20:27:10 -06:00
, i_lock ( true )
2008-10-02 16:23:55 -05:00
{
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 );
}
}
}
// Template specialization of utility methods
template < class T >
void Map :: AddToGrid ( T * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). template AddGridObject < T > ( obj , obj -> GetGUID ());
}
template <>
void Map :: AddToGrid ( Player * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject ( obj , obj -> GetGUID ());
}
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 , obj -> GetGUID ());
}
// add to grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject ( obj , obj -> GetGUID ());
}
}
template <>
void Map :: AddToGrid ( Creature * obj , NGridType * grid , Cell const & cell )
{
// add to world object registry in grid
2009-03-02 18:01:27 -06:00
if ( obj -> isPet () || obj -> IsTempWorldObject )
2008-10-02 16:23:55 -05:00
{
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject < Creature > ( obj , obj -> GetGUID ());
}
// add to grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject < Creature > ( obj , obj -> GetGUID ());
}
2009-02-17 21:50:18 -06:00
obj -> SetCurrentCell ( cell );
2008-10-02 16:23:55 -05:00
}
2009-02-26 16:29:55 -06:00
template <>
void Map :: AddToGrid ( DynamicObject * obj , NGridType * grid , Cell const & cell )
{
if ( obj -> isActiveObject ()) // only farsight
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject < DynamicObject > ( obj , obj -> GetGUID ());
else
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject < DynamicObject > ( obj , obj -> GetGUID ());
}
2008-10-02 16:23:55 -05:00
template < class T >
void Map :: RemoveFromGrid ( T * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). template RemoveGridObject < T > ( obj , obj -> GetGUID ());
}
template <>
void Map :: RemoveFromGrid ( Player * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject ( obj , obj -> GetGUID ());
}
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 , obj -> GetGUID ());
}
// remove from grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject ( obj , obj -> GetGUID ());
}
}
template <>
void Map :: RemoveFromGrid ( Creature * obj , NGridType * grid , Cell const & cell )
{
// remove from world object registry in grid
2009-03-02 18:01:27 -06:00
if ( obj -> isPet () || obj -> IsTempWorldObject )
2008-10-02 16:23:55 -05:00
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject < Creature > ( obj , obj -> GetGUID ());
}
// remove from grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject < Creature > ( obj , obj -> GetGUID ());
}
}
2009-02-26 16:29:55 -06:00
template <>
void Map :: RemoveFromGrid ( DynamicObject * obj , NGridType * grid , Cell const & cell )
{
if ( obj -> isActiveObject ()) // only farsight
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject < DynamicObject > ( obj , obj -> GetGUID ());
else
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject < DynamicObject > ( obj , obj -> GetGUID ());
}
2008-11-05 20:51:05 -06:00
template < class T >
2009-02-26 17:01:02 -06:00
void Map :: SwitchGridContainers ( T * obj , bool on )
2008-11-05 20:51:05 -06:00
{
2009-04-08 12:20:39 -05:00
CellPair p = Trinity :: 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::SwitchGridContainers: Object " I64FMT " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), 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 ( "Switch object " I64FMT " from grid[%u,%u] %u" , obj -> GetGUID (), cell . data . Part . grid_x , cell . data . Part . grid_y , on );
2008-11-14 20:40:35 -06:00
NGridType * ngrid = getNGrid ( cell . GridX (), cell . GridY ());
2009-04-08 12:20:39 -05:00
assert ( ngrid != NULL );
2008-11-14 20:40:35 -06:00
GridType & grid = ( * ngrid )( cell . CellX (), cell . CellY ());
2008-11-05 20:51:05 -06:00
2009-02-26 17:01:02 -06:00
if ( on )
2008-11-05 20:51:05 -06:00
{
2009-03-03 19:07:52 -06:00
grid . RemoveGridObject < T > ( obj , obj -> GetGUID ());
grid . AddWorldObject < T > ( obj , obj -> GetGUID ());
/*if(!grid.RemoveGridObject<T>(obj, obj->GetGUID())
2009-02-26 16:29:55 -06:00
|| !grid.AddWorldObject<T>(obj, obj->GetGUID()))
2008-11-14 20:40:35 -06:00
{
2009-02-26 16:29:55 -06:00
assert(false);
2009-03-03 19:07:52 -06:00
}*/
2009-02-17 21:50:18 -06:00
}
else
2008-11-05 20:51:05 -06:00
{
2009-03-03 19:07:52 -06:00
grid . RemoveWorldObject < T > ( obj , obj -> GetGUID ());
grid . AddGridObject < T > ( obj , obj -> GetGUID ());
/*if(!grid.RemoveWorldObject<T>(obj, obj->GetGUID())
2009-02-26 16:29:55 -06:00
|| !grid.AddGridObject<T>(obj, obj->GetGUID()))
2008-11-14 20:40:35 -06:00
{
2009-02-26 16:29:55 -06:00
assert(false);
2009-03-03 19:07:52 -06:00
}*/
2008-11-05 20:51:05 -06:00
}
2009-03-02 18:01:27 -06:00
obj -> IsTempWorldObject = on ;
2008-11-05 20:51:05 -06:00
}
template void Map :: SwitchGridContainers ( Creature * , bool );
2008-11-14 20:40:35 -06:00
template void Map :: SwitchGridContainers ( DynamicObject * , bool );
2008-11-05 20:51:05 -06:00
2008-10-02 16:23:55 -05:00
template < class T >
void Map :: DeleteFromWorld ( T * obj )
{
2008-11-07 09:36:46 -06:00
// Note: In case resurrectable corpse and pet its removed from global lists in own destructor
2008-10-02 16:23:55 -05:00
delete obj ;
}
template < class T >
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( T * )
2008-10-02 16:23:55 -05:00
{
}
template <>
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( Player * obj )
2008-10-02 16:23:55 -05:00
{
2009-04-04 13:08:14 -06:00
obj -> m_Notified = false ;
2009-04-03 14:44:34 -06:00
obj -> m_IsInNotifyList = false ;
2009-02-17 21:50:18 -06:00
AddUnitToNotify ( obj );
2008-10-02 16:23:55 -05:00
}
template <>
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( Creature * obj )
2008-10-02 16:23:55 -05:00
{
2009-04-04 13:08:14 -06:00
obj -> m_Notified = false ;
2009-04-03 14:44:34 -06:00
obj -> m_IsInNotifyList = false ;
2009-02-17 21:50:18 -06:00
AddUnitToNotify ( obj );
2008-10-02 16:23:55 -05:00
}
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 ))
{
2009-04-12 16:44:44 -05:00
sLog . outDebug ( "Loading grid[%u,%u] for map %u" , p . x_coord , p . y_coord , i_id );
2008-10-02 16:23:55 -05:00
setNGrid ( new NGridType ( p . x_coord * MAX_NUMBER_OF_GRIDS + p . y_coord , p . x_coord , p . y_coord , i_gridExpiry , sWorld . getConfig ( CONFIG_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 = 63 - p . x_coord ;
int gy = 63 - p . y_coord ;
if ( ! GridMaps [ gx ][ gy ])
Map :: LoadMapAndVMap ( i_id , i_InstanceId , gx , gy );
}
}
}
void
2009-02-25 20:52:20 -06:00
Map :: EnsureGridLoaded ( const Cell & cell , Player * player )
2008-10-02 16:23:55 -05:00
{
EnsureGridCreated ( GridPair ( cell . GridX (), cell . GridY ()));
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
2009-02-25 20:52:20 -06:00
if ( ! isGridObjectDataLoaded ( cell . GridX (), cell . GridY ()))
2008-10-02 16:23:55 -05:00
{
2009-02-25 20:52:20 -06:00
if ( player )
2008-10-02 16:23:55 -05:00
{
player -> SendDelayResponse ( MAX_GRID_LOAD_TIME );
DEBUG_LOG ( "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
{
2009-02-25 20:52:20 -06:00
DEBUG_LOG ( "Active object nearby triggers of loading grid [%u,%u] on map %u" , cell . GridX (), cell . GridY (), i_id );
2008-10-02 16:23:55 -05:00
}
ObjectGridLoader loader ( * grid , this , cell );
loader . LoadN ();
setGridObjectDataLoaded ( true , cell . GridX (), cell . GridY ());
// Add resurrectable corpses to world object list in grid
ObjectAccessor :: Instance (). AddCorpsesToGrid ( GridPair ( cell . GridX (), cell . GridY ()),( * grid )( cell . CellX (), cell . CellY ()), this );
ResetGridExpiry ( * getNGrid ( cell . GridX (), cell . GridY ()), 0.1f );
grid -> SetGridState ( GRID_STATE_ACTIVE );
}
2009-02-25 20:52:20 -06:00
if ( player )
2008-10-02 16:23:55 -05:00
AddToGrid ( player , grid , cell );
}
2008-12-20 14:10:53 -06:00
void Map :: LoadGrid ( float x , float y )
{
CellPair pair = Trinity :: ComputeCellPair ( x , y );
Cell cell ( pair );
2009-02-26 16:29:55 -06:00
EnsureGridLoaded ( cell );
2008-12-20 14:10:53 -06:00
}
2008-10-02 16:23:55 -05:00
bool Map :: Add ( Player * player )
{
2008-11-18 19:40:06 -06:00
player -> GetMapRef (). link ( this , player );
2008-11-07 09:36:46 -06:00
player -> SetInstanceId ( GetInstanceId ());
2008-10-02 16:23:55 -05:00
// update player state for other player and visa-versa
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
Cell cell ( p );
2009-02-25 20:52:20 -06:00
EnsureGridLoaded ( cell , player );
2008-10-02 16:23:55 -05:00
player -> AddToWorld ();
SendInitSelf ( player );
SendInitTransports ( player );
2009-03-27 09:06:26 -06:00
player -> m_clientGUIDs . clear ();
2009-04-08 17:23:57 -05:00
//AddNotifier(player);
2008-10-02 16:23:55 -05:00
return true ;
}
template < class T >
void
Map :: Add ( T * obj )
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
assert ( obj );
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog . outError ( "Map::Add: Object " I64FMTD " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
return ;
}
Cell cell ( p );
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
EnsureGridLoaded ( cell );
else
EnsureGridCreated ( GridPair ( cell . GridX (), cell . GridY ()));
2008-10-02 16:23:55 -05:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
AddToGrid ( obj , grid , cell );
obj -> AddToWorld ();
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
AddToActive ( obj );
2008-10-02 16:23:55 -05:00
DEBUG_LOG ( "Object %u enters grid[%u,%u]" , GUID_LOPART ( obj -> GetGUID ()), cell . GridX (), cell . GridY ());
2009-04-02 20:27:10 -06:00
UpdateObjectVisibility ( obj , cell , p );
2008-10-02 16:23:55 -05:00
2009-04-08 17:23:57 -05:00
//AddNotifier(obj);
2008-10-02 16:23:55 -05:00
}
2008-11-05 20:51:05 -06:00
void Map :: MessageBroadcast ( Player * player , WorldPacket * msg , bool to_self , bool to_possessor )
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
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 . data . Part . reserved = ALL_DISTRICT ;
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-11-05 20:51:05 -06:00
Trinity :: MessageDeliverer post_man ( * player , msg , to_possessor , to_self );
2008-10-14 11:57:03 -05:00
TypeContainerVisitor < Trinity :: MessageDeliverer , WorldTypeMapContainer > message ( post_man );
2008-10-02 16:23:55 -05:00
CellLock < ReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , message , * this );
}
2008-11-05 20:51:05 -06:00
void Map :: MessageBroadcast ( WorldObject * obj , WorldPacket * msg , bool to_possessor )
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog . outError ( "Map::MessageBroadcast: Object " I64FMTD " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
return ;
}
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
cell . SetNoCreate ();
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-11-05 20:51:05 -06:00
Trinity :: ObjectMessageDeliverer post_man ( * obj , msg , to_possessor );
2008-10-14 11:57:03 -05:00
TypeContainerVisitor < Trinity :: ObjectMessageDeliverer , WorldTypeMapContainer > message ( post_man );
2008-10-02 16:23:55 -05:00
CellLock < ReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , message , * this );
}
2008-11-12 16:21:08 -06:00
void Map :: MessageDistBroadcast ( Player * player , WorldPacket * msg , float dist , bool to_self , bool to_possessor , bool own_team_only )
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
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 . data . Part . reserved = ALL_DISTRICT ;
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-11-05 20:51:05 -06:00
Trinity :: MessageDistDeliverer post_man ( * player , msg , to_possessor , dist , to_self , own_team_only );
2008-10-14 11:57:03 -05:00
TypeContainerVisitor < Trinity :: MessageDistDeliverer , WorldTypeMapContainer > message ( post_man );
2008-10-02 16:23:55 -05:00
CellLock < ReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , message , * this );
}
2008-11-05 20:51:05 -06:00
void Map :: MessageDistBroadcast ( WorldObject * obj , WorldPacket * msg , float dist , bool to_possessor )
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog . outError ( "Map::MessageBroadcast: Object " I64FMTD " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
return ;
}
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
cell . SetNoCreate ();
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-11-05 20:51:05 -06:00
Trinity :: ObjectMessageDistDeliverer post_man ( * obj , msg , to_possessor , dist );
2008-10-14 11:57:03 -05:00
TypeContainerVisitor < Trinity :: ObjectMessageDistDeliverer , WorldTypeMapContainer > message ( post_man );
2008-10-02 16:23:55 -05:00
CellLock < ReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , message , * this );
}
bool Map :: loaded ( const GridPair & p ) const
{
return ( getNGrid ( p . x_coord , p . y_coord ) && isGridObjectDataLoaded ( p . x_coord , p . y_coord ) );
}
2008-12-27 11:26:22 -06:00
void Map :: RelocationNotify ()
2008-11-21 15:41:18 -06:00
{
2009-04-02 20:27:10 -06:00
//Move backlog to notify list
for ( std :: vector < uint64 >:: iterator iter = i_unitsToNotifyBacklog . begin (); iter != i_unitsToNotifyBacklog . end (); ++ iter )
{
if ( Unit * unit = ObjectAccessor :: GetObjectInWorld ( * iter , ( Unit * ) NULL ))
{
i_unitsToNotify . push_back ( unit );
}
}
i_unitsToNotifyBacklog . clear ();
2009-01-05 16:03:45 -06:00
//Notify
2009-04-02 20:27:10 -06:00
for ( std :: vector < Unit *>:: iterator iter = i_unitsToNotify . begin (); iter != i_unitsToNotify . end (); ++ iter )
2008-11-29 13:08:23 -06:00
{
2009-04-02 20:27:10 -06:00
Unit * unit = * iter ;
2009-04-04 15:14:19 -06:00
if ( unit -> m_Notified || ! unit -> IsInWorld () || unit -> GetMapId () != GetId ())
2009-01-05 16:03:45 -06:00
continue ;
2008-11-29 23:07:23 -06:00
unit -> m_Notified = true ;
2009-04-04 20:31:29 -06:00
unit -> m_IsInNotifyList = false ;
2009-01-05 16:03:45 -06:00
2009-04-18 11:03:27 -05:00
float dist = abs ( unit -> GetPositionX () - unit -> oldX ) + abs ( unit -> GetPositionY () - unit -> oldY );
if ( dist > 10.0f )
{
Trinity :: VisibleChangesNotifier notifier ( * unit );
VisitWorld ( unit -> oldX , unit -> oldY , World :: GetMaxVisibleDistance (), notifier );
dist = 0 ;
}
2008-11-29 23:07:23 -06:00
if ( unit -> GetTypeId () == TYPEID_PLAYER )
2008-12-19 23:31:02 -06:00
{
Trinity :: PlayerRelocationNotifier notifier ( * (( Player * ) unit ));
2009-04-18 11:03:27 -05:00
VisitAll ( unit -> GetPositionX (), unit -> GetPositionY (), World :: GetMaxVisibleDistance () + dist , notifier );
2009-03-08 11:11:21 -06:00
notifier . Notify ();
2008-12-19 23:31:02 -06:00
}
2008-11-29 13:08:23 -06:00
else
2008-12-19 23:31:02 -06:00
{
Trinity :: CreatureRelocationNotifier notifier ( * (( Creature * ) unit ));
2009-04-18 11:03:27 -05:00
VisitAll ( unit -> GetPositionX (), unit -> GetPositionY (), World :: GetMaxVisibleDistance () + dist , notifier );
2008-12-19 23:31:02 -06:00
}
2008-11-29 13:08:23 -06:00
}
2009-04-04 13:02:57 -06:00
for ( std :: vector < Unit *>:: iterator iter = i_unitsToNotify . begin (); iter != i_unitsToNotify . end (); ++ iter )
{
( * iter ) -> m_Notified = false ;
}
2009-04-02 20:27:10 -06:00
i_unitsToNotify . clear ();
}
2009-01-05 16:03:45 -06:00
2009-04-02 20:27:10 -06:00
void Map :: AddUnitToNotify ( Unit * u )
{
if ( u -> m_IsInNotifyList )
return ;
u -> m_IsInNotifyList = true ;
2009-04-18 11:03:27 -05:00
u -> oldX = u -> GetPositionX ();
u -> oldY = u -> GetPositionY ();
2009-04-02 20:27:10 -06:00
if ( i_lock )
i_unitsToNotifyBacklog . push_back ( u -> GetGUID ());
else
i_unitsToNotify . push_back ( u );
2008-12-27 11:26:22 -06:00
}
2008-11-29 13:08:23 -06:00
2008-12-27 11:26:22 -06:00
void Map :: Update ( const uint32 & t_diff )
{
2009-04-02 20:27:10 -06:00
i_lock = false ;
2008-11-21 15:41:18 -06:00
resetMarkedCells ();
2008-11-23 11:01:26 -06:00
Trinity :: ObjectUpdater updater ( t_diff );
// for creature
TypeContainerVisitor < Trinity :: ObjectUpdater , GridTypeMapContainer > grid_object_update ( updater );
// for pets
TypeContainerVisitor < Trinity :: ObjectUpdater , WorldTypeMapContainer > world_object_update ( updater );
2009-02-25 20:52:20 -06:00
// 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 )
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
Player * plr = m_mapRefIter -> getSource ();
if ( ! plr -> IsInWorld ())
continue ;
CellPair standing_cell ( Trinity :: ComputeCellPair ( plr -> GetPositionX (), plr -> GetPositionY ()));
2008-11-23 11:01:26 -06:00
// 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 . data . Part . reserved = CENTER_DISTRICT ;
2008-12-27 11:26:22 -06:00
//cell.SetNoCreate();
2008-11-23 11:01:26 -06:00
CellLock < NullGuard > cell_lock ( cell , pair );
cell_lock -> Visit ( cell_lock , grid_object_update , * this );
cell_lock -> Visit ( cell_lock , world_object_update , * this );
}
}
}
2009-02-25 20:52:20 -06:00
}
// 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 ;
2008-11-23 11:01:26 -06:00
2009-03-12 08:58:28 -06:00
// Update bindsight players
2009-03-12 10:56:22 -06:00
/*if(obj->isType(TYPEMASK_UNIT))
2009-03-12 08:58:28 -06:00
{
if(!((Unit*)obj)->GetSharedVisionList().empty())
for(SharedVisionList::const_iterator itr = ((Unit*)obj)->GetSharedVisionList().begin(); itr != ((Unit*)obj)->GetSharedVisionList().end(); ++itr)
{
if(!*itr)
{
sLog.outError("unit %u has invalid shared vision player, list size %u", obj->GetEntry(), ((Unit*)obj)->GetSharedVisionList().size());
continue;
}
Trinity::PlayerVisibilityNotifier notifier(**itr);
VisitAll(obj->GetPositionX(), obj->GetPositionY(), World::GetMaxVisibleDistance(), notifier);
notifier.Notify();
}
}
2009-03-12 10:56:22 -06:00
else */ if ( obj -> GetTypeId () == TYPEID_DYNAMICOBJECT )
2009-03-12 08:58:28 -06:00
{
if ( Unit * caster = (( DynamicObject * ) obj ) -> GetCaster ())
if ( caster -> GetTypeId () == TYPEID_PLAYER && caster -> GetUInt64Value ( PLAYER_FARSIGHT ) == obj -> GetGUID ())
{
Trinity :: PlayerVisibilityNotifier notifier ( * (( Player * ) caster ));
VisitAll ( obj -> GetPositionX (), obj -> GetPositionY (), World :: GetMaxVisibleDistance (), notifier );
notifier . Notify ();
}
}
2009-02-25 20:52:20 -06:00
CellPair standing_cell ( Trinity :: 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 . data . Part . reserved = CENTER_DISTRICT ;
//cell.SetNoCreate();
CellLock < NullGuard > cell_lock ( cell , pair );
cell_lock -> Visit ( cell_lock , grid_object_update , * this );
cell_lock -> Visit ( cell_lock , world_object_update , * this );
}
}
}
}
2008-11-21 15:41:18 -06:00
}
2008-11-18 19:40:06 -06:00
2009-04-02 20:27:10 -06:00
i_lock = true ;
2009-04-21 11:18:46 -05:00
MoveAllCreaturesInMoveList ();
2008-12-27 11:26:22 -06:00
RelocationNotify ();
2009-04-21 11:18:46 -05:00
RemoveAllObjectsInRemoveList ();
2008-12-27 11:26:22 -06:00
2008-10-02 16:23:55 -05:00
// 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 ())
return ;
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: 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
assert ( grid -> GetGridState () >= 0 && grid -> GetGridState () < MAX_GRID_STATE );
si_GridStates [ grid -> GetGridState ()] -> Update ( * this , * grid , * info , grid -> getX (), grid -> getY (), t_diff );
}
}
void Map :: Remove ( Player * player , bool remove )
{
2008-11-28 11:38:28 -06:00
// 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 ();
2008-11-18 19:40:06 -06:00
player -> GetMapRef (). unlink ();
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
// invalid coordinates
player -> RemoveFromWorld ();
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_LOG ( "Remove player %s from grid[%u,%u]" , player -> GetName (), cell . GridX (), cell . GridY ());
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
player -> RemoveFromWorld ();
RemoveFromGrid ( player , grid , cell );
SendRemoveTransports ( player );
2009-03-27 09:06:26 -06:00
UpdateObjectVisibility ( player , cell , p );
2008-10-02 16:23:55 -05:00
if ( remove )
DeleteFromWorld ( player );
}
bool Map :: RemoveBones ( uint64 guid , float x , float y )
{
if ( IsRemovalGrid ( x , y ))
{
Corpse * corpse = ObjectAccessor :: Instance (). GetObjectInWorld ( GetId (), x , y , guid , ( Corpse * ) NULL );
if ( corpse && corpse -> GetTypeId () == TYPEID_CORPSE && corpse -> GetType () == CORPSE_BONES )
corpse -> DeleteBonesFromWorld ();
else
return false ;
}
return true ;
}
template < class T >
void
Map :: Remove ( T * obj , bool remove )
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
2008-12-12 11:21:28 -06:00
sLog . outError ( "Map::Remove: Object " I64FMT " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
2008-10-02 16:23:55 -05:00
return ;
}
Cell cell ( p );
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-12-12 11:21:28 -06:00
DEBUG_LOG ( "Remove object " I64FMT " from grid[%u,%u]" , obj -> GetGUID (), cell . data . Part . grid_x , cell . data . Part . grid_y );
2008-10-02 16:23:55 -05:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
2009-02-26 10:42:37 -06:00
obj -> RemoveFromWorld ();
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
RemoveFromActive ( obj );
2008-10-02 16:23:55 -05:00
RemoveFromGrid ( obj , grid , cell );
UpdateObjectVisibility ( obj , cell , p );
if ( remove )
{
// if option set then object already saved at this moment
2008-11-14 16:28:45 -06:00
if ( ! sWorld . getConfig ( CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY ))
2008-10-02 16:23:55 -05:00
obj -> SaveRespawnTime ();
DeleteFromWorld ( obj );
}
}
void
Map :: PlayerRelocation ( Player * player , float x , float y , float z , float orientation )
{
assert ( player );
2008-10-14 11:57:03 -05:00
CellPair old_val = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
CellPair new_val = Trinity :: ComputeCellPair ( x , y );
2008-10-02 16:23:55 -05:00
Cell old_cell ( old_val );
Cell new_cell ( new_val );
new_cell |= old_cell ;
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_LOG ( "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 ());
// update player position for group at taxi flight
if ( player -> GetGroup () && player -> isInFlight ())
player -> SetGroupUpdateFlag ( GROUP_UPDATE_FLAG_POSITION );
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 );
2009-02-17 21:50:18 -06:00
else
2009-02-25 20:52:20 -06:00
EnsureGridLoaded ( new_cell , player );
2008-10-02 16:23:55 -05:00
}
2008-11-29 13:08:23 -06:00
AddUnitToNotify ( player );
2009-02-17 21:50:18 -06:00
2008-10-02 16:23:55 -05:00
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 )
{
assert ( CheckGridIntegrity ( creature , false ));
Cell old_cell = creature -> GetCurrentCell ();
2008-10-14 11:57:03 -05:00
CellPair new_val = Trinity :: ComputeCellPair ( x , y );
2008-10-02 16:23:55 -05:00
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 ) )
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
AddCreatureToMoveList ( creature , x , y , z , ang );
// in diffcell/diffgrid case notifiers called at finishing move creature in Map::MoveAllCreaturesInMoveList
}
else
{
creature -> Relocate ( x , y , z , ang );
2008-11-29 13:08:23 -06:00
AddUnitToNotify ( creature );
2008-10-02 16:23:55 -05:00
}
assert ( CheckGridIntegrity ( creature , true ));
}
void Map :: AddCreatureToMoveList ( Creature * c , float x , float y , float z , float ang )
{
if ( ! c )
return ;
i_creaturesToMove [ c ] = CreatureMover ( x , y , z , ang );
}
void Map :: MoveAllCreaturesInMoveList ()
{
while ( ! i_creaturesToMove . empty ())
{
// get data and remove element;
CreatureMoveList :: iterator iter = i_creaturesToMove . begin ();
Creature * c = iter -> first ;
CreatureMover cm = iter -> second ;
i_creaturesToMove . erase ( iter );
// calculate cells
2008-10-14 11:57:03 -05:00
CellPair new_val = Trinity :: ComputeCellPair ( cm . x , cm . y );
2008-10-02 16:23:55 -05:00
Cell new_cell ( new_val );
// do move or do move to respawn or remove creature if previous all fail
if ( CreatureCellRelocation ( c , new_cell ))
{
// update pos
c -> Relocate ( cm . x , cm . y , cm . z , cm . ang );
2008-11-29 13:08:23 -06:00
//CreatureRelocationNotify(c,new_cell,new_cell.cellPair());
AddUnitToNotify ( c );
2008-10-02 16:23:55 -05:00
}
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 ( c ))
{
// ... or unload (if respawn grid also not loaded)
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u ) can't be move to unloaded respawn grid." , c -> GetGUIDLow (), c -> GetEntry ());
#endif
c -> CleanupsBeforeDelete ();
AddObjectToRemoveList ( c );
}
}
}
}
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 ))
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
2009-02-17 21:50:18 -06:00
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
2008-10-02 16:23:55 -05:00
}
else
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
}
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-02-25 20:52:20 -06:00
// in diff. grids but active creature
if ( c -> isActiveObject ())
{
EnsureGridLoaded ( new_cell );
#ifdef TRINITY_DEBUG
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
return true ;
}
// in diff. loaded grid normal creature
2008-10-02 16:23:55 -05:00
if ( loaded ( GridPair ( new_cell . GridX (), new_cell . GridY ())))
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
2009-03-04 09:33:15 -06:00
EnsureGridCreated ( GridPair ( new_cell . GridX (), new_cell . GridY ()));
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-02-25 20:52:20 -06:00
// fail to move: normal creature attempt move to unloaded grid
#ifdef TRINITY_DEBUG
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
return false ;
2008-10-02 16:23:55 -05:00
}
bool Map :: CreatureRespawnRelocation ( Creature * c )
{
float resp_x , resp_y , resp_z , resp_o ;
c -> GetRespawnCoord ( resp_x , resp_y , resp_z , & resp_o );
2008-10-14 11:57:03 -05:00
CellPair resp_val = Trinity :: ComputeCellPair ( resp_x , resp_y );
2008-10-02 16:23:55 -05:00
Cell resp_cell ( resp_val );
c -> CombatStop ();
c -> GetMotionMaster () -> Clear ();
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "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 ());
#endif
// 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
2008-11-29 13:08:23 -06:00
//CreatureRelocationNotify(c,resp_cell,resp_cell.cellPair());
AddUnitToNotify ( c );
2008-10-02 16:23:55 -05:00
return true ;
}
else
return false ;
}
2009-02-25 17:53:05 -06:00
bool Map :: UnloadGrid ( const uint32 & x , const uint32 & y , bool unloadAll )
2008-10-02 16:23:55 -05:00
{
NGridType * grid = getNGrid ( x , y );
assert ( grid != NULL );
{
2009-02-25 20:52:20 -06:00
if ( ! unloadAll && ActiveObjectsNearGrid ( x , y ) )
return false ;
2008-10-02 16:23:55 -05:00
2009-04-12 16:44:44 -05:00
sLog . outDebug ( "Unloading grid[%u,%u] for map %u" , x , y , i_id );
2009-03-31 14:40:54 -06:00
2008-10-02 16:23:55 -05:00
ObjectGridUnloader unloader ( * grid );
2009-02-25 17:53:05 -06:00
if ( ! unloadAll )
{
// Finish creature moves, remove and delete all creatures with delayed remove before moving to respawn grids
// Must know real mob position before move
2009-03-31 14:40:54 -06:00
MoveAllCreaturesInMoveList ();
2008-10-02 16:23:55 -05:00
2009-02-25 17:53:05 -06:00
// move creatures to respawn grids if this is diff.grid or to remove list
unloader . MoveToRespawnN ();
2008-10-02 16:23:55 -05:00
2009-02-25 17:53:05 -06:00
// Finish creature moves, remove and delete all creatures with delayed remove before unload
2009-03-31 14:40:54 -06:00
MoveAllCreaturesInMoveList ();
2009-02-25 17:53:05 -06:00
}
2008-10-02 16:23:55 -05:00
2009-03-31 14:40:54 -06:00
ObjectGridCleaner cleaner ( * grid );
cleaner . CleanN ();
RemoveAllObjectsInRemoveList ();
2008-10-02 16:23:55 -05:00
unloader . UnloadN ();
2009-03-31 14:40:54 -06:00
assert ( i_objectsToRemove . empty ());
2009-02-25 17:53:05 -06:00
delete grid ;
2008-10-02 16:23:55 -05:00
setNGrid ( NULL , x , y );
}
int gx = 63 - x ;
int gy = 63 - 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 ]) delete ( GridMaps [ gx ][ gy ]);
2008-12-12 11:21:28 -06:00
// x and y are swapped
2008-10-02 16:23:55 -05:00
VMAP :: VMapFactory :: createOrGetVMapManager () -> unloadMap ( GetId (), gy , gx );
}
else
(( MapInstanced * )( MapManager :: Instance (). GetBaseMap ( i_id ))) -> RemoveGridMapReference ( GridPair ( gx , gy ));
GridMaps [ gx ][ gy ] = NULL ;
}
DEBUG_LOG ( "Unloading grid[%u,%u] for map %u finished" , x , y , i_id );
return true ;
}
2009-02-25 17:53:05 -06:00
void Map :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
// clear all delayed moves, useless anyway do this moves before map unload.
i_creaturesToMove . clear ();
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end (); )
{
NGridType & grid ( * i -> getSource ());
++ i ;
2009-02-25 17:53:05 -06:00
UnloadGrid ( grid . getX (), grid . getY (), true ); // deletes the grid and removes it from the GridRefManager
2008-10-02 16:23:55 -05:00
}
}
float Map :: GetHeight ( float x , float y , float z , bool pUseVmaps ) const
{
2008-10-14 11:57:03 -05:00
GridPair p = Trinity :: ComputeGridPair ( x , y );
2008-10-02 16:23:55 -05:00
// half opt method
int gx = ( int )( 32 - x / SIZE_OF_GRIDS ); //grid x
int gy = ( int )( 32 - y / SIZE_OF_GRIDS ); //grid y
2009-02-20 16:12:47 -06:00
float lx = 128 * ( 32 - x / SIZE_OF_GRIDS - gx );
float ly = 128 * ( 32 - y / SIZE_OF_GRIDS - gy );
2008-10-02 16:23:55 -05:00
// ensure GridMap is loaded
const_cast < Map *> ( this ) -> EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
// find raw .map surface under Z coordinates
float mapHeight ;
if ( GridMap * gmap = GridMaps [ gx ][ gy ])
{
int lx_int = ( int ) lx ;
int ly_int = ( int ) ly ;
2009-02-20 16:12:47 -06:00
lx -= lx_int ;
ly -= ly_int ;
float a , b , c ;
if ( lx + ly < 1 )
{
if ( lx > ly )
{
// 1
float h1 = gmap -> v9 [ lx_int ][ ly_int ];
float h2 = gmap -> v9 [ lx_int + 1 ][ ly_int ];
float h5 = 2 * gmap -> v8 [ lx_int ][ ly_int ];
a = h2 - h1 ;
b = h5 - h1 - h2 ;
c = h1 ;
}
else
{
// 2
float h1 = gmap -> v9 [ lx_int ][ ly_int ];
float h3 = gmap -> v9 [ lx_int ][ ly_int + 1 ];
float h5 = 2 * gmap -> v8 [ lx_int ][ ly_int ];
a = h5 - h1 - h3 ;
b = h3 - h1 ;
c = h1 ;
}
}
else
{
if ( lx > ly )
{
// 3
float h2 = gmap -> v9 [ lx_int + 1 ][ ly_int ];
float h4 = gmap -> v9 [ lx_int + 1 ][ ly_int + 1 ];
float h5 = 2 * gmap -> v8 [ lx_int ][ ly_int ];
a = h2 + h4 - h5 ;
b = h4 - h2 ;
c = h5 - h4 ;
}
else
{
// 4
float h3 = gmap -> v9 [ lx_int ][ ly_int + 1 ];
float h4 = gmap -> v9 [ lx_int + 1 ][ ly_int + 1 ];
float h5 = 2 * gmap -> v8 [ lx_int ][ ly_int ];
a = h4 - h3 ;
b = h3 + h4 - h5 ;
c = h5 - h4 ;
}
}
float _mapheight = a * lx + b * ly + c ;
2008-12-06 10:55:18 -06:00
// In some very rare case this will happen. Need find a better way.
if ( lx_int == MAP_RESOLUTION ) -- lx_int ;
if ( ly_int == MAP_RESOLUTION ) -- ly_int ;
2009-02-20 16:12:47 -06:00
/*
2008-10-02 16:23:55 -05:00
float zi[4];
// Probe 4 nearest points (except border cases)
zi[0] = gmap->Z[lx_int][ly_int];
zi[1] = lx < MAP_RESOLUTION-1 ? gmap->Z[lx_int+1][ly_int] : zi[0];
zi[2] = ly < MAP_RESOLUTION-1 ? gmap->Z[lx_int][ly_int+1] : zi[0];
zi[3] = lx < MAP_RESOLUTION-1 && ly < MAP_RESOLUTION-1 ? gmap->Z[lx_int+1][ly_int+1] : zi[0];
// Recalculate them like if their x,y positions were in the range 0,1
float b[4];
b[0] = zi[0];
b[1] = zi[1]-zi[0];
b[2] = zi[2]-zi[0];
b[3] = zi[0]-zi[1]-zi[2]+zi[3];
// Normalize the dx and dy to be in range 0..1
float fact_x = lx - lx_int;
float fact_y = ly - ly_int;
// Use the simplified bilinear equation, as described in [url="http://en.wikipedia.org/wiki/Bilinear_interpolation"]http://en.wikipedia.org/wiki/Bilinear_interpolation[/url]
float _mapheight = b[0] + (b[1]*fact_x) + (b[2]*fact_y) + (b[3]*fact_x*fact_y);
2009-02-20 16:12:47 -06:00
*/
2008-10-02 16:23:55 -05:00
// look from a bit higher pos to find the floor, ignore under surface case
if ( z + 2.0f > _mapheight )
mapHeight = _mapheight ;
else
mapHeight = VMAP_INVALID_HEIGHT_VALUE ;
}
else
mapHeight = VMAP_INVALID_HEIGHT_VALUE ;
float vmapHeight ;
if ( pUseVmaps )
{
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
if ( vmgr -> isHeightCalcEnabled ())
{
// look from a bit higher pos to find the floor
vmapHeight = vmgr -> getHeight ( GetId (), x , y , z + 2.0f );
}
else
vmapHeight = VMAP_INVALID_HEIGHT_VALUE ;
}
else
vmapHeight = VMAP_INVALID_HEIGHT_VALUE ;
// mapHeight set for any above raw ground Z or <= INVALID_HEIGHT
// vmapheight set for any under Z value or <= INVALID_HEIGHT
if ( vmapHeight > INVALID_HEIGHT )
{
if ( mapHeight > INVALID_HEIGHT )
{
// we have mapheight and vmapheight and must select more appropriate
// we are already under the surface or vmap height above map heigt
// or if the distance of the vmap height is less the land height distance
if ( z < mapHeight || vmapHeight > mapHeight || fabs ( mapHeight - z ) > fabs ( vmapHeight - z ) )
return vmapHeight ;
else
return mapHeight ; // better use .map surface height
}
else
return vmapHeight ; // we have only vmapHeight (if have)
}
else
{
if ( ! pUseVmaps )
return mapHeight ; // explicitly use map data (if have)
else if ( mapHeight > INVALID_HEIGHT && ( z < mapHeight + 2 || z == MAX_HEIGHT ))
return mapHeight ; // explicitly use map data if original z < mapHeight but map found (z+2 > mapHeight)
else
return VMAP_INVALID_HEIGHT_VALUE ; // we not have any height
}
}
2009-01-16 19:05:30 -06:00
float Map :: GetVmapHeight ( float x , float y , float z , bool useMaps ) const
{
2009-02-09 08:16:34 -05:00
float mapHeight ;
float vmapHeight ;
if ( useMaps )
{
mapHeight = GetHeight ( x , y , z , false );
if ( fabs ( mapHeight - z ) < 0.1 )
return mapHeight ;
}
else
mapHeight = INVALID_HEIGHT ;
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
if ( vmgr -> isLineOfSightCalcEnabled ())
bool result = vmgr -> getObjectHitPos ( GetId (), x , y , z + 2.0f , x , y , mapHeight , x , y , vmapHeight , 0 );
else
return INVALID_HEIGHT ;
return vmapHeight ;
2009-01-16 19:05:30 -06:00
}
2008-10-02 16:23:55 -05:00
uint16 Map :: GetAreaFlag ( float x , float y ) const
{
//local x,y coords
float lx , ly ;
int gx , gy ;
2008-10-14 11:57:03 -05:00
GridPair p = Trinity :: ComputeGridPair ( x , y );
2008-10-02 16:23:55 -05:00
// half opt method
gx = ( int )( 32 - x / SIZE_OF_GRIDS ) ; //grid x
gy = ( int )( 32 - y / SIZE_OF_GRIDS ); //grid y
lx = 16 * ( 32 - x / SIZE_OF_GRIDS - gx );
ly = 16 * ( 32 - y / SIZE_OF_GRIDS - gy );
//DEBUG_LOG("my %d %d si %d %d",gx,gy,p.x_coord,p.y_coord);
// ensure GridMap is loaded
const_cast < Map *> ( this ) -> EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
if ( GridMaps [ gx ][ gy ])
return GridMaps [ gx ][ gy ] -> area_flag [( int )( lx )][( int )( ly )];
// this used while not all *.map files generated (instances)
else
return GetAreaFlagByMapId ( i_id );
}
uint8 Map :: GetTerrainType ( float x , float y ) const
{
//local x,y coords
float lx , ly ;
int gx , gy ;
// half opt method
gx = ( int )( 32 - x / SIZE_OF_GRIDS ) ; //grid x
gy = ( int )( 32 - y / SIZE_OF_GRIDS ); //grid y
lx = 16 * ( 32 - x / SIZE_OF_GRIDS - gx );
ly = 16 * ( 32 - y / SIZE_OF_GRIDS - gy );
// ensure GridMap is loaded
const_cast < Map *> ( this ) -> EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
if ( GridMaps [ gx ][ gy ])
return GridMaps [ gx ][ gy ] -> terrain_type [( int )( lx )][( int )( ly )];
else
return 0 ;
}
float Map :: GetWaterLevel ( float x , float y ) const
{
//local x,y coords
float lx , ly ;
int gx , gy ;
// half opt method
gx = ( int )( 32 - x / SIZE_OF_GRIDS ) ; //grid x
gy = ( int )( 32 - y / SIZE_OF_GRIDS ); //grid y
lx = 128 * ( 32 - x / SIZE_OF_GRIDS - gx );
ly = 128 * ( 32 - y / SIZE_OF_GRIDS - gy );
// ensure GridMap is loaded
const_cast < Map *> ( this ) -> EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
if ( GridMaps [ gx ][ gy ])
return GridMaps [ gx ][ gy ] -> liquid_level [( int )( lx )][( int )( ly )];
else
return 0 ;
}
uint32 Map :: GetAreaId ( uint16 areaflag , uint32 map_id )
{
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
if ( entry )
return entry -> ID ;
else
return 0 ;
}
uint32 Map :: GetZoneId ( uint16 areaflag , uint32 map_id )
{
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
if ( entry )
return ( entry -> zone != 0 ) ? entry -> zone : entry -> ID ;
else
return 0 ;
}
bool Map :: IsInWater ( float x , float y , float pZ ) const
{
// This method is called too often to use vamps for that (4. parameter = false).
// The pZ pos is taken anyway for future use
float z = GetHeight ( x , y , pZ , false ); // use .map base surface height
// underground or instance without vmap
if ( z <= INVALID_HEIGHT )
return false ;
float water_z = GetWaterLevel ( x , y );
uint8 flag = GetTerrainType ( x , y );
return ( z < ( water_z - 2 )) && ( flag & 0x01 );
}
bool Map :: IsUnderWater ( float x , float y , float z ) const
{
float water_z = GetWaterLevel ( x , y );
uint8 flag = GetTerrainType ( x , y );
return ( z < ( water_z - 2 )) && ( flag & 0x01 );
}
bool Map :: CheckGridIntegrity ( Creature * c , bool moved ) const
{
Cell const & cur_cell = c -> GetCurrentCell ();
2008-10-14 11:57:03 -05:00
CellPair xy_val = Trinity :: ComputeCellPair ( c -> GetPositionX (), c -> GetPositionY ());
2008-10-02 16:23:55 -05:00
Cell xy_cell ( xy_val );
if ( xy_cell != cur_cell )
{
2009-04-04 20:31:29 -06:00
sLog . outDebug ( "Creature (GUIDLow: %u) X: %f Y: %f (%s) in grid[%u,%u]cell[%u,%u] instead grid[%u,%u]cell[%u,%u]" ,
c -> GetGUIDLow (),
2008-10-02 16:23:55 -05:00
c -> GetPositionX (), c -> GetPositionY (),( moved ? "final" : "original" ),
cur_cell . GridX (), cur_cell . GridY (), cur_cell . CellX (), cur_cell . CellY (),
xy_cell . GridX (), xy_cell . GridY (), xy_cell . CellX (), xy_cell . CellY ());
return true ; // not crash at error, just output error in debug mode
}
return true ;
}
const char * Map :: GetMapName () const
{
return i_mapEntry ? i_mapEntry -> name [ sWorld . GetDefaultDbcLocale ()] : "UNNAMEDMAP\x0" ;
}
void Map :: UpdateObjectVisibility ( WorldObject * obj , Cell cell , CellPair cellpair )
{
cell . data . Part . reserved = ALL_DISTRICT ;
cell . SetNoCreate ();
2008-10-14 11:57:03 -05:00
Trinity :: VisibleChangesNotifier notifier ( * obj );
TypeContainerVisitor < Trinity :: VisibleChangesNotifier , WorldTypeMapContainer > player_notifier ( notifier );
2008-10-02 16:23:55 -05:00
CellLock < GridReadGuard > cell_lock ( cell , cellpair );
cell_lock -> Visit ( cell_lock , player_notifier , * this );
}
void Map :: SendInitSelf ( Player * player )
{
sLog . outDetail ( "Creating player data for himself %u" , player -> GetGUIDLow ());
UpdateData data ;
bool hasTransport = false ;
// attach to player data current transport data
if ( Transport * transport = player -> GetTransport ())
{
hasTransport = true ;
transport -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
// build data for self presence in world at own client (one time for map)
player -> BuildCreateUpdateBlockForPlayer ( & data , player );
// build other passengers at transport also (they always visible and marked as visible and will not send at visibility update at add to map
if ( Transport * transport = player -> GetTransport ())
{
for ( Transport :: PlayerSet :: const_iterator itr = transport -> GetPassengers (). begin (); itr != transport -> GetPassengers (). end (); ++ itr )
{
if ( player != ( * itr ) && player -> HaveAtClient ( * itr ))
{
hasTransport = true ;
( * itr ) -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
}
}
WorldPacket packet ;
data . BuildPacket ( & packet , hasTransport );
player -> GetSession () -> SendPacket ( & packet );
}
void Map :: SendInitTransports ( Player * player )
{
// Hack to send out transports
MapManager :: TransportMap & tmap = MapManager :: Instance (). m_TransportsByMap ;
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
UpdateData transData ;
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
bool hasTransport = false ;
for ( MapManager :: TransportSet :: iterator i = tset . begin (); i != tset . end (); ++ i )
{
if (( * i ) != player -> GetTransport ()) // send data for current transport in other place
{
hasTransport = true ;
( * i ) -> BuildCreateUpdateBlockForPlayer ( & transData , player );
}
}
WorldPacket packet ;
transData . BuildPacket ( & packet , hasTransport );
player -> GetSession () -> SendPacket ( & packet );
}
void Map :: SendRemoveTransports ( Player * player )
{
// Hack to send out transports
MapManager :: TransportMap & tmap = MapManager :: Instance (). m_TransportsByMap ;
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
UpdateData transData ;
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
// except used transport
for ( MapManager :: TransportSet :: iterator i = tset . begin (); i != tset . end (); ++ i )
if ( player -> GetTransport () != ( * i ))
( * i ) -> BuildOutOfRangeUpdateBlock ( & transData );
WorldPacket packet ;
transData . BuildPacket ( & packet );
player -> GetSession () -> SendPacket ( & packet );
}
inline void Map :: setNGrid ( NGridType * grid , uint32 x , uint32 y )
{
if ( x >= MAX_NUMBER_OF_GRIDS || y >= MAX_NUMBER_OF_GRIDS )
{
sLog . outError ( "map::setNGrid() Invalid grid coordinates found: %d, %d!" , x , y );
assert ( false );
}
i_grids [ x ][ y ] = grid ;
}
void Map :: DoDelayedMovesAndRemoves ()
{
MoveAllCreaturesInMoveList ();
RemoveAllObjectsInRemoveList ();
}
void Map :: AddObjectToRemoveList ( WorldObject * obj )
{
assert ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
i_objectsToRemove . insert ( obj );
//sLog.outDebug("Object (GUID: %u TypeId: %u ) added to removing list.",obj->GetGUIDLow(),obj->GetTypeId());
}
2009-02-26 17:01:02 -06:00
void Map :: AddObjectToSwitchList ( WorldObject * obj , bool on )
{
assert ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . find ( obj );
if ( itr == i_objectsToSwitch . end ())
i_objectsToSwitch . insert ( itr , std :: make_pair ( obj , on ));
else if ( itr -> second != on )
i_objectsToSwitch . erase ( itr );
else
assert ( false );
}
2008-10-02 16:23:55 -05:00
void Map :: RemoveAllObjectsInRemoveList ()
{
2009-02-26 17:01:02 -06:00
while ( ! i_objectsToSwitch . empty ())
{
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . begin ();
WorldObject * obj = itr -> first ;
bool on = itr -> second ;
i_objectsToSwitch . erase ( itr );
switch ( obj -> GetTypeId ())
{
case TYPEID_UNIT :
if ( ! (( Creature * ) obj ) -> isPet ())
SwitchGridContainers (( Creature * ) obj , on );
break ;
}
}
2008-10-02 16:23:55 -05:00
//sLog.outDebug("Object remover 1 check.");
while ( ! i_objectsToRemove . empty ())
{
2009-03-31 14:40:54 -06:00
std :: set < WorldObject *>:: iterator itr = i_objectsToRemove . begin ();
WorldObject * obj = * itr ;
2008-10-02 16:23:55 -05:00
switch ( obj -> GetTypeId ())
{
case TYPEID_CORPSE :
{
Corpse * corpse = ObjectAccessor :: Instance (). GetCorpse ( * obj , obj -> GetGUID ());
if ( ! corpse )
2009-03-31 14:40:54 -06:00
sLog . outError ( "Try delete corpse/bones %u that not in map" , obj -> GetGUIDLow ());
2008-10-02 16:23:55 -05:00
else
Remove ( corpse , true );
break ;
}
case TYPEID_DYNAMICOBJECT :
Remove (( DynamicObject * ) obj , true );
break ;
case TYPEID_GAMEOBJECT :
Remove (( GameObject * ) obj , true );
break ;
case TYPEID_UNIT :
2008-11-07 09:36:46 -06:00
// in case triggered sequence some spell can continue casting after prev CleanupsBeforeDelete call
2008-11-05 20:10:19 -06:00
// make sure that like sources auras/etc removed before destructor start
(( Creature * ) obj ) -> CleanupsBeforeDelete ();
2008-10-02 16:23:55 -05:00
Remove (( Creature * ) obj , true );
break ;
default :
sLog . outError ( "Non-grid object (TypeId: %u) in grid object removing list, ignored." , obj -> GetTypeId ());
break ;
}
2009-03-31 14:40:54 -06:00
i_objectsToRemove . erase ( itr );
2008-10-02 16:23:55 -05:00
}
//sLog.outDebug("Object remover 2 check.");
}
2008-11-18 19:40:06 -06:00
uint32 Map :: GetPlayersCountExceptGMs () const
{
uint32 count = 0 ;
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
if ( ! itr -> getSource () -> isGameMaster ())
++ count ;
return count ;
}
void Map :: SendToPlayers ( WorldPacket const * data ) const
{
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> GetSession () -> SendPacket ( data );
}
2009-02-25 20:52:20 -06:00
bool Map :: ActiveObjectsNearGrid ( uint32 x , uint32 y ) const
2008-11-18 19:40:06 -06:00
{
CellPair cell_min ( x * MAX_NUMBER_OF_CELLS , y * MAX_NUMBER_OF_CELLS );
CellPair cell_max ( cell_min . x_coord + MAX_NUMBER_OF_CELLS , cell_min . y_coord + MAX_NUMBER_OF_CELLS );
cell_min << 2 ;
cell_min -= 2 ;
cell_max >> 2 ;
cell_max += 2 ;
for ( MapRefManager :: const_iterator iter = m_mapRefManager . begin (); iter != m_mapRefManager . end (); ++ iter )
{
Player * plr = iter -> getSource ();
CellPair p = Trinity :: ComputeCellPair ( plr -> GetPositionX (), plr -> GetPositionY ());
if ( ( cell_min . x_coord <= p . x_coord && p . x_coord <= cell_max . x_coord ) &&
( cell_min . y_coord <= p . y_coord && p . y_coord <= cell_max . y_coord ) )
return true ;
}
2009-02-25 20:52:20 -06:00
for ( ActiveNonPlayers :: const_iterator iter = m_activeNonPlayers . begin (); iter != m_activeNonPlayers . end (); ++ iter )
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
WorldObject * obj = * iter ;
2008-11-21 15:41:18 -06:00
2009-02-25 20:52:20 -06:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-11-21 15:41:18 -06:00
if ( ( cell_min . x_coord <= p . x_coord && p . x_coord <= cell_max . x_coord ) &&
( cell_min . y_coord <= p . y_coord && p . y_coord <= cell_max . y_coord ) )
return true ;
}
2009-02-25 20:52:20 -06:00
return false ;
}
void Map :: AddToActive ( Creature * c )
{
AddToActiveHelper ( c );
// also not allow unloading spawn grid to prevent creating creature clone at load
2009-03-07 00:53:01 -06:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
float x , y , z ;
c -> GetRespawnCoord ( x , y , z );
GridPair p = Trinity :: ComputeGridPair ( x , y );
if ( getNGrid ( p . x_coord , p . y_coord ))
getNGrid ( p . x_coord , p . y_coord ) -> incUnloadActiveLock ();
else
{
GridPair p2 = Trinity :: ComputeGridPair ( c -> GetPositionX (), c -> GetPositionY ());
sLog . outError ( "Active creature (GUID: %u Entry: %u) added to grid[%u,%u] but spawn grid[%u,%u] not loaded." ,
c -> GetGUIDLow (), c -> GetEntry (), p . x_coord , p . y_coord , p2 . x_coord , p2 . y_coord );
}
2008-11-21 15:41:18 -06:00
}
2009-02-25 20:52:20 -06:00
}
2008-11-21 15:41:18 -06:00
2009-02-25 20:52:20 -06:00
void Map :: RemoveFromActive ( Creature * c )
{
RemoveFromActiveHelper ( c );
// also allow unloading spawn grid
2009-03-07 00:53:01 -06:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2009-02-25 20:52:20 -06:00
{
float x , y , z ;
c -> GetRespawnCoord ( x , y , z );
GridPair p = Trinity :: ComputeGridPair ( x , y );
if ( getNGrid ( p . x_coord , p . y_coord ))
getNGrid ( p . x_coord , p . y_coord ) -> decUnloadActiveLock ();
else
{
GridPair p2 = Trinity :: ComputeGridPair ( c -> GetPositionX (), c -> GetPositionY ());
sLog . outError ( "Active creature (GUID: %u Entry: %u) removed from grid[%u,%u] but spawn grid[%u,%u] not loaded." ,
c -> GetGUIDLow (), c -> GetEntry (), p . x_coord , p . y_coord , p2 . x_coord , p2 . y_coord );
}
}
2008-11-21 15:41:18 -06:00
}
2008-10-02 16:23:55 -05:00
template void Map :: Add ( Corpse * );
template void Map :: Add ( Creature * );
template void Map :: Add ( GameObject * );
template void Map :: Add ( DynamicObject * );
template void Map :: Remove ( Corpse * , bool );
template void Map :: Remove ( Creature * , bool );
template void Map :: Remove ( GameObject * , bool );
template void Map :: Remove ( DynamicObject * , bool );
/* ******* Dungeon Instance Maps ******* */
InstanceMap :: InstanceMap ( uint32 id , time_t expiry , uint32 InstanceId , uint8 SpawnMode )
: Map ( id , expiry , InstanceId , SpawnMode ), i_data ( NULL ),
m_resetAfterUnload ( false ), m_unloadWhenEmpty ( false )
{
// the timer is started by default, and stopped when the first player joins
// this make sure it gets unloaded if for some reason no player joins
m_unloadTimer = std :: max ( sWorld . getConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
}
InstanceMap ::~ InstanceMap ()
{
if ( i_data )
{
delete i_data ;
i_data = NULL ;
}
}
/*
Do map specific checks to see if the player can enter
*/
bool InstanceMap :: CanEnter ( Player * player )
{
2008-11-18 19:40:06 -06:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "InstanceMap::CanEnter - player %s(%u) already in map %d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), GetId (), GetInstanceId (), GetSpawnMode ());
assert ( false );
return false ;
}
// cannot enter if the instance is full (player cap), GMs don't count
InstanceTemplate const * iTemplate = objmgr . GetInstanceTemplate ( GetId ());
if ( ! player -> isGameMaster () && GetPlayersCountExceptGMs () >= iTemplate -> maxPlayers )
{
sLog . outDetail ( "MAP: Instance '%u' of map '%s' cannot have more than '%u' players. Player '%s' rejected" , GetInstanceId (), GetMapName (), iTemplate -> maxPlayers , player -> GetName ());
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_MAX_PLAYERS );
return false ;
}
// cannot enter while players in the instance are in combat
Group * pGroup = player -> GetGroup ();
2009-02-13 23:53:06 +01:00
if ( ! player -> isGameMaster () && pGroup && pGroup -> InCombatToInstance ( GetInstanceId ()) && player -> GetMapId () != GetId ())
2008-10-02 16:23:55 -05:00
{
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_ZONE_IN_COMBAT );
return false ;
}
return Map :: CanEnter ( player );
}
/*
Do map specific checks and add the player to the map if successful.
*/
bool InstanceMap :: Add ( Player * player )
{
// TODO: Not sure about checking player level: already done in HandleAreaTriggerOpcode
// GMs still can teleport player in instance.
// Is it needed?
{
Guard guard ( * this );
if ( ! CanEnter ( player ))
return false ;
// get or create an instance save for the map
InstanceSave * mapSave = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! mapSave )
{
sLog . outDetail ( "InstanceMap::Add: creating instance save for map %d spawnmode %d with instance id %d" , GetId (), GetSpawnMode (), GetInstanceId ());
mapSave = sInstanceSaveManager . AddInstanceSave ( GetId (), GetInstanceId (), GetSpawnMode (), 0 , true );
}
// check for existing instance binds
InstancePlayerBind * playerBind = player -> GetBoundInstance ( GetId (), GetSpawnMode ());
if ( playerBind && playerBind -> perm )
{
// cannot enter other instances if bound permanently
if ( playerBind -> save != mapSave )
{
sLog . outError ( "InstanceMap::Add: player %s(%d) is permanently bound to instance %d,%d,%d,%d,%d,%d but he is being put in instance %d,%d,%d,%d,%d,%d" , player -> GetName (), player -> GetGUIDLow (), playerBind -> save -> GetMapId (), playerBind -> save -> GetInstanceId (), playerBind -> save -> GetDifficulty (), playerBind -> save -> GetPlayerCount (), playerBind -> save -> GetGroupCount (), playerBind -> save -> CanReset (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), mapSave -> GetPlayerCount (), mapSave -> GetGroupCount (), mapSave -> CanReset ());
assert ( false );
}
}
else
{
Group * pGroup = player -> GetGroup ();
if ( pGroup )
{
// solo saves should be reset when entering a group
InstanceGroupBind * groupBind = pGroup -> GetBoundInstance ( GetId (), GetSpawnMode ());
if ( playerBind )
{
sLog . outError ( "InstanceMap::Add: player %s(%d) is being put in instance %d,%d,%d,%d,%d,%d but he is in group %d and is bound to instance %d,%d,%d,%d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), mapSave -> GetPlayerCount (), mapSave -> GetGroupCount (), mapSave -> CanReset (), GUID_LOPART ( pGroup -> GetLeaderGUID ()), playerBind -> save -> GetMapId (), playerBind -> save -> GetInstanceId (), playerBind -> save -> GetDifficulty (), playerBind -> save -> GetPlayerCount (), playerBind -> save -> GetGroupCount (), playerBind -> save -> CanReset ());
if ( groupBind ) sLog . outError ( "InstanceMap::Add: the group is bound to instance %d,%d,%d,%d,%d,%d" , groupBind -> save -> GetMapId (), groupBind -> save -> GetInstanceId (), groupBind -> save -> GetDifficulty (), groupBind -> save -> GetPlayerCount (), groupBind -> save -> GetGroupCount (), groupBind -> save -> CanReset ());
2009-01-01 22:28:53 -06:00
sLog . outError ( "InstanceMap::Add: do not let player %s enter instance otherwise crash will happen" , player -> GetName ());
return false ;
//player->UnbindInstance(GetId(), GetSpawnMode());
2008-12-30 16:49:45 -06:00
//assert(false);
2008-10-02 16:23:55 -05:00
}
// bind to the group or keep using the group save
if ( ! groupBind )
pGroup -> BindToInstance ( mapSave , false );
else
{
// cannot jump to a different instance without resetting it
if ( groupBind -> save != mapSave )
{
sLog . outError ( "InstanceMap::Add: player %s(%d) is being put in instance %d,%d,%d but he is in group %d which is bound to instance %d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), GUID_LOPART ( pGroup -> GetLeaderGUID ()), groupBind -> save -> GetMapId (), groupBind -> save -> GetInstanceId (), groupBind -> save -> GetDifficulty ());
if ( mapSave )
sLog . outError ( "MapSave players: %d, group count: %d" , mapSave -> GetPlayerCount (), mapSave -> GetGroupCount ());
else
sLog . outError ( "MapSave NULL" );
if ( groupBind -> save )
sLog . outError ( "GroupBind save players: %d, group count: %d" , groupBind -> save -> GetPlayerCount (), groupBind -> save -> GetGroupCount ());
else
sLog . outError ( "GroupBind save NULL" );
assert ( false );
}
// if the group/leader is permanently bound to the instance
// players also become permanently bound when they enter
if ( groupBind -> perm )
{
WorldPacket data ( SMSG_INSTANCE_SAVE_CREATED , 4 );
data << uint32 ( 0 );
player -> GetSession () -> SendPacket ( & data );
player -> BindToInstance ( mapSave , true );
}
}
}
else
{
// set up a solo bind or continue using it
if ( ! playerBind )
player -> BindToInstance ( mapSave , false );
else
// cannot jump to a different instance without resetting it
assert ( playerBind -> save == mapSave );
}
}
if ( i_data ) i_data -> OnPlayerEnter ( player );
2008-11-28 11:38:28 -06:00
// for normal instances cancel the reset schedule when the
// first player enters (no players yet)
2008-10-02 16:23:55 -05:00
SetResetSchedule ( false );
player -> SendInitWorldStates ();
sLog . outDetail ( "MAP: Player '%s' entered the instance '%u' of map '%s'" , player -> GetName (), GetInstanceId (), GetMapName ());
// initialize unload state
m_unloadTimer = 0 ;
m_resetAfterUnload = false ;
m_unloadWhenEmpty = false ;
}
// this will acquire the same mutex so it cannot be in the previous block
Map :: Add ( player );
return true ;
}
2008-11-02 16:53:46 -06:00
void InstanceMap :: Update ( const uint32 & t_diff )
{
2008-11-07 09:36:46 -06:00
Map :: Update ( t_diff );
2008-11-02 16:53:46 -06:00
2008-11-07 09:36:46 -06:00
if ( i_data )
i_data -> Update ( t_diff );
2008-11-02 16:53:46 -06:00
}
2008-10-02 16:23:55 -05:00
void InstanceMap :: Remove ( Player * player , bool remove )
{
sLog . outDetail ( "MAP: Removing player '%s' from instance '%u' of map '%s' before relocating to other map" , player -> GetName (), GetInstanceId (), GetMapName ());
2008-11-18 19:40:06 -06:00
//if last player set unload timer
if ( ! m_unloadTimer && m_mapRefManager . getSize () == 1 )
2008-10-02 16:23:55 -05:00
m_unloadTimer = m_unloadWhenEmpty ? MIN_UNLOAD_DELAY : std :: max ( sWorld . getConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
Map :: Remove ( player , remove );
2008-11-28 11:38:28 -06:00
// for normal instances schedule the reset after all players have left
SetResetSchedule ( true );
2008-10-02 16:23:55 -05:00
}
2008-10-19 11:07:10 -05:00
Creature * Map :: GetCreatureInMap ( uint64 guid )
{
Creature * obj = HashMapHolder < Creature >:: Find ( guid );
if ( obj && obj -> GetInstanceId () != GetInstanceId ()) obj = NULL ;
return obj ;
}
GameObject * Map :: GetGameObjectInMap ( uint64 guid )
{
GameObject * obj = HashMapHolder < GameObject >:: Find ( guid );
if ( obj && obj -> GetInstanceId () != GetInstanceId ()) obj = NULL ;
return obj ;
}
2008-10-02 16:23:55 -05:00
void InstanceMap :: CreateInstanceData ( bool load )
{
if ( i_data != NULL )
return ;
InstanceTemplate const * mInstance = objmgr . GetInstanceTemplate ( GetId ());
if ( mInstance )
{
2008-11-20 16:16:57 -06:00
i_script_id = mInstance -> script_id ;
2008-10-02 16:23:55 -05:00
i_data = Script -> CreateInstanceData ( this );
}
if ( ! i_data )
return ;
if ( load )
{
// TODO: make a global storage for this
QueryResult * result = CharacterDatabase . PQuery ( "SELECT data FROM instance WHERE map = '%u' AND id = '%u'" , GetId (), i_InstanceId );
if ( result )
{
Field * fields = result -> Fetch ();
const char * data = fields [ 0 ]. GetString ();
if ( data )
{
2008-11-20 16:16:57 -06:00
sLog . outDebug ( "Loading instance data for `%s` with id %u" , objmgr . GetScriptName ( i_script_id ), i_InstanceId );
2008-10-02 16:23:55 -05:00
i_data -> Load ( data );
}
delete result ;
}
}
else
{
2008-11-20 16:16:57 -06:00
sLog . outDebug ( "New instance data, \" %s \" ,initialized!" , objmgr . GetScriptName ( i_script_id ));
2008-10-02 16:23:55 -05:00
i_data -> Initialize ();
}
}
/*
Returns true if there are no players in the instance
*/
bool InstanceMap :: Reset ( uint8 method )
{
// note: since the map may not be loaded when the instance needs to be reset
// the instance must be deleted from the DB by InstanceSaveManager
2008-11-18 19:40:06 -06:00
if ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
if ( method == INSTANCE_RESET_ALL )
{
// notify the players to leave the instance so it can be reset
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> SendResetFailedNotify ( GetId ());
2008-10-02 16:23:55 -05:00
}
else
{
if ( method == INSTANCE_RESET_GLOBAL )
{
// set the homebind timer for players inside (1 minute)
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> m_InstanceValid = false ;
2008-10-02 16:23:55 -05:00
}
// the unload timer is not started
// instead the map will unload immediately after the players have left
m_unloadWhenEmpty = true ;
m_resetAfterUnload = true ;
}
}
else
{
// unloaded at next update
m_unloadTimer = MIN_UNLOAD_DELAY ;
m_resetAfterUnload = true ;
}
2008-11-18 19:40:06 -06:00
return m_mapRefManager . isEmpty ();
2008-10-02 16:23:55 -05:00
}
void InstanceMap :: PermBindAllPlayers ( Player * player )
{
InstanceSave * save = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! save )
{
sLog . outError ( "Cannot bind players, no instance save available for map! \n " );
return ;
}
Group * group = player -> GetGroup ();
// group members outside the instance group don't get bound
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
Player * plr = itr -> getSource ();
// players inside an instance cannot be bound to other instances
// some players may already be permanently bound, in this case nothing happens
InstancePlayerBind * bind = plr -> GetBoundInstance ( save -> GetMapId (), save -> GetDifficulty ());
if ( ! bind || ! bind -> perm )
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
plr -> BindToInstance ( save , true );
WorldPacket data ( SMSG_INSTANCE_SAVE_CREATED , 4 );
data << uint32 ( 0 );
plr -> GetSession () -> SendPacket ( & data );
2008-10-02 16:23:55 -05:00
}
2008-11-18 19:40:06 -06:00
// if the leader is not in the instance the group will not get a perm bind
if ( group && group -> GetLeaderGUID () == plr -> GetGUID ())
group -> BindToInstance ( save , true );
2008-10-02 16:23:55 -05:00
}
}
time_t InstanceMap :: GetResetTime ()
{
InstanceSave * save = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
return save ? save -> GetDifficulty () : DIFFICULTY_NORMAL ;
}
2009-02-25 17:53:05 -06:00
void InstanceMap :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
if ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "InstanceMap::UnloadAll: there are still players in the instance at unload, should not happen!" );
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
{
Player * plr = itr -> getSource ();
plr -> TeleportTo ( plr -> m_homebindMapId , plr -> m_homebindX , plr -> m_homebindY , plr -> m_homebindZ , plr -> GetOrientation ());
}
2008-10-02 16:23:55 -05:00
}
if ( m_resetAfterUnload == true )
objmgr . DeleteRespawnTimeForInstance ( GetInstanceId ());
2009-02-25 17:53:05 -06:00
Map :: UnloadAll ();
2008-10-02 16:23:55 -05:00
}
2008-11-18 19:40:06 -06:00
void InstanceMap :: SendResetWarnings ( uint32 timeLeft ) const
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> SendInstanceResetWarning ( GetId (), timeLeft );
2008-10-02 16:23:55 -05:00
}
void InstanceMap :: SetResetSchedule ( bool on )
{
// only for normal instances
// the reset time is only scheduled when there are no payers inside
// it is assumed that the reset time will rarely (if ever) change while the reset is scheduled
2008-11-18 19:40:06 -06:00
if ( ! HavePlayers () && ! IsRaid () && ! IsHeroic ())
2008-10-02 16:23:55 -05:00
{
InstanceSave * save = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! save ) sLog . outError ( "InstanceMap::SetResetSchedule: cannot turn schedule %s, no save available for instance %d of %d" , on ? "on" : "off" , GetInstanceId (), GetId ());
else sInstanceSaveManager . ScheduleReset ( on , save -> GetResetTime (), InstanceSaveManager :: InstResetEvent ( 0 , GetId (), GetInstanceId ()));
}
}
/* ******* Battleground Instance Maps ******* */
BattleGroundMap :: BattleGroundMap ( uint32 id , time_t expiry , uint32 InstanceId )
: Map ( id , expiry , InstanceId , DIFFICULTY_NORMAL )
{
}
BattleGroundMap ::~ BattleGroundMap ()
{
}
bool BattleGroundMap :: CanEnter ( Player * player )
{
2008-11-18 19:40:06 -06:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "BGMap::CanEnter - player %u already in map!" , player -> GetGUIDLow ());
assert ( false );
return false ;
}
if ( player -> GetBattleGroundId () != GetInstanceId ())
return false ;
// player number limit is checked in bgmgr, no need to do it here
return Map :: CanEnter ( player );
}
bool BattleGroundMap :: Add ( Player * player )
{
{
Guard guard ( * this );
if ( ! CanEnter ( player ))
return false ;
// reset instance validity, battleground maps do not homebind
player -> m_InstanceValid = true ;
}
return Map :: Add ( player );
}
void BattleGroundMap :: Remove ( Player * player , bool remove )
{
sLog . outDetail ( "MAP: Removing player '%s' from bg '%u' of map '%s' before relocating to other map" , player -> GetName (), GetInstanceId (), GetMapName ());
Map :: Remove ( player , remove );
}
void BattleGroundMap :: SetUnload ()
{
m_unloadTimer = MIN_UNLOAD_DELAY ;
}
2009-02-25 17:53:05 -06:00
void BattleGroundMap :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
while ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
2009-04-04 20:31:29 -06:00
if ( Player * plr = m_mapRefManager . getFirst () -> getSource ())
{
plr -> TeleportTo ( plr -> m_homebindMapId , plr -> m_homebindX , plr -> m_homebindY , plr -> m_homebindZ , plr -> GetOrientation ());
// TeleportTo removes the player from this map (if the map exists) -> calls BattleGroundMap::Remove -> invalidates the iterator.
// just in case, remove the player from the list explicitly here as well to prevent a possible infinite loop
// note that this remove is not needed if the code works well in other places
plr -> GetMapRef (). unlink ();
}
2008-10-02 16:23:55 -05:00
}
2009-02-25 17:53:05 -06:00
Map :: UnloadAll ();
2008-10-02 16:23:55 -05:00
}
2008-11-29 13:08:23 -06:00
/*--------------------------TRINITY-------------------------*/