2010-10-07 15:35:36 +02:00
/*
2012-01-01 00:32:13 +01:00
* Copyright (C) 2008-2012 TrinityCore <http://www.trinitycore.org/>
2010-10-07 15:35:36 +02:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* 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, see <http://www.gnu.org/licenses/>.
2008-10-02 16:23:55 -05:00
*/
2009-10-17 15:51:44 -07:00
2010-09-22 18:13:57 +06:00
#include "Map.h"
2008-10-02 16:23:55 -05:00
#include "GridStates.h"
2010-09-22 18:13:57 +06:00
#include "ScriptMgr.h"
#include "VMapFactory.h"
2012-04-09 15:11:39 +02:00
#include "MMapFactory.h"
2010-09-22 18:13:57 +06:00
#include "MapInstanced.h"
2008-10-02 16:23:55 -05:00
#include "CellImpl.h"
2010-09-22 18:13:57 +06:00
#include "GridNotifiers.h"
2008-10-02 16:23:55 -05:00
#include "GridNotifiersImpl.h"
2010-06-07 15:59:21 -06:00
#include "Transport.h"
2010-09-22 18:13:57 +06:00
#include "InstanceScript.h"
2008-10-02 16:23:55 -05:00
#include "ObjectAccessor.h"
2010-09-22 18:13:57 +06:00
#include "MapManager.h"
2008-10-02 16:23:55 -05:00
#include "ObjectMgr.h"
2010-11-29 07:50:31 +01:00
#include "Group.h"
2011-12-18 22:09:03 +01:00
#include "LFGMgr.h"
2012-02-03 16:03:52 -05:00
#include "DynamicTree.h"
2012-03-01 17:06:41 +01:00
#include "Vehicle.h"
2009-10-17 15:51:44 -07:00
2011-07-27 11:00:39 +02:00
union u_map_magic
{
char asChar [ 4 ];
uint32 asUInt ;
};
u_map_magic MapMagic = { { 'M' , 'A' , 'P' , 'S' } };
2012-03-07 13:09:35 +01:00
u_map_magic MapVersionMagic = { { 'v' , '1' , '.' , '2' } };
2011-07-27 11:00:39 +02:00
u_map_magic MapAreaMagic = { { 'A' , 'R' , 'E' , 'A' } };
u_map_magic MapHeightMagic = { { 'M' , 'H' , 'G' , 'T' } };
u_map_magic MapLiquidMagic = { { 'M' , 'L' , 'I' , 'Q' } };
2008-10-02 16:23:55 -05:00
#define DEFAULT_GRID_EXPIRY 300
#define MAX_GRID_LOAD_TIME 50
2010-12-23 23:25:44 +01:00
#define MAX_CREATURE_ATTACK_RADIUS (45.0f * sWorld->getRate(RATE_CREATURE_AGGRO))
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
GridState * si_GridStates [ MAX_GRID_STATE ];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Map ::~ Map ()
{
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnDestroyMap ( this );
2010-08-06 19:23:43 +02:00
2009-02-25 17:53:05 -06:00
UnloadAll ();
2009-10-17 15:51:44 -07:00
2010-04-07 19:13:19 +02:00
while ( ! i_worldObjects . empty ())
2009-08-12 15:33:12 -05:00
{
2011-09-15 14:08:17 +02:00
WorldObject * obj = * i_worldObjects . begin ();
2011-12-20 17:05:08 -05:00
ASSERT ( obj -> IsWorldObject ());
2010-07-30 20:04:28 +02:00
//ASSERT(obj->GetTypeId() == TYPEID_CORPSE);
2009-08-12 15:33:12 -05:00
obj -> RemoveFromWorld ();
2009-08-12 23:12:33 -05:00
obj -> ResetMap ();
2009-08-12 15:33:12 -05:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! m_scriptSchedule . empty ())
2011-04-13 12:07:20 +06:00
sScriptMgr -> DecreaseScheduledScriptCount ( m_scriptSchedule . size ());
2012-04-09 15:11:39 +02:00
MMAP :: MMapFactory :: createOrGetMMapManager () -> unloadMapInstance ( GetId (), i_InstanceId );
2012-08-22 17:39:25 -05:00
MMAP :: MMapFactory :: createOrGetMMapManager () -> unloadMap ( GetId ());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
bool Map :: ExistMap ( uint32 mapid , int gx , int gy )
2008-10-02 16:23:55 -05:00
{
2010-12-23 23:25:44 +01:00
int len = sWorld -> GetDataPath (). length () + strlen ( "maps/%03u%02u%02u.map" ) + 1 ;
2008-10-02 16:23:55 -05:00
char * tmp = new char [ len ];
2011-04-29 20:47:02 +02:00
snprintf ( tmp , len , ( char * )( sWorld -> GetDataPath () + "maps/%03u%02u%02u.map" ). c_str (), mapid , gx , gy );
2009-10-17 15:51:44 -07:00
2010-08-31 10:23:22 +02:00
bool ret = false ;
2011-09-15 14:08:17 +02:00
FILE * pf = fopen ( tmp , "rb" );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! pf )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map file '%s': does not exist!" , tmp );
2010-08-31 10:23:22 +02:00
else
2008-10-02 16:23:55 -05:00
{
2010-08-31 10:23:22 +02:00
map_fileheader header ;
if ( fread ( & header , sizeof ( header ), 1 , pf ) == 1 )
{
2011-07-27 11:00:39 +02:00
if ( header . mapMagic != MapMagic . asUInt || header . versionMagic != MapVersionMagic . asUInt )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map file '%s' is from an incompatible clientversion. Please recreate using the mapextractor." , tmp );
2010-08-31 10:23:22 +02:00
else
ret = true ;
2010-09-12 01:40:27 +02:00
}
2011-07-27 11:00:39 +02:00
fclose ( pf );
2008-10-02 16:23:55 -05:00
}
delete [] tmp ;
2010-08-31 10:23:22 +02:00
return ret ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
bool Map :: ExistVMap ( uint32 mapid , int gx , int gy )
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ())
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( vmgr -> isMapLoadingEnabled ())
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
bool exists = vmgr -> existsMap (( sWorld -> GetDataPath () + "vmaps" ). c_str (), mapid , gx , gy );
2010-04-07 19:14:10 +02:00
if ( ! exists )
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
std :: string name = vmgr -> getDirFileName ( mapid , gx , gy );
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "VMap file '%s' is missing or points to wrong version of vmap file. Redo vmaps with latest version of vmap_assembler.exe." , ( sWorld -> GetDataPath () + "vmaps/" + name ). c_str ());
2008-10-02 16:23:55 -05:00
return false ;
}
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2012-04-09 15:11:39 +02:00
void Map :: LoadMMap ( int gx , int gy )
{
// DONT CHANGE "gy" and "gx" - Its necessary !
2012-04-09 18:05:35 +02:00
int mmapLoadResult = MMAP :: MMapFactory :: createOrGetMMapManager () -> loadMap (( sWorld -> GetDataPath () + "mmaps" ). c_str (), GetId (), gy , gx );
2012-04-09 15:11:39 +02:00
switch ( mmapLoadResult )
{
case MMAP :: MMAP_LOAD_RESULT_OK :
sLog -> outDetail ( "MMAP loaded name:%s, id:%d, x:%d, y:%d (mmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
break ;
case MMAP :: MMAP_LOAD_RESULT_ERROR :
sLog -> outDetail ( "Could not load MMAP name:%s, id:%d, x:%d, y:%d (mmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
break ;
case MMAP :: MMAP_LOAD_RESULT_IGNORED :
sLog -> outStaticDebug ( "Ignored MMAP name:%s, id:%d, x:%d, y:%d (mmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
break ;
}
}
2011-04-29 20:47:02 +02:00
void Map :: LoadVMap ( int gx , int gy )
2008-10-02 16:23:55 -05:00
{
2008-11-07 09:36:46 -06:00
// x and y are swapped !!
2011-04-29 20:47:02 +02:00
int vmapLoadResult = VMAP :: VMapFactory :: createOrGetVMapManager () -> loadMap (( sWorld -> GetDataPath () + "vmaps" ). c_str (), GetId (), gx , gy );
2011-09-29 12:43:05 +02:00
switch ( vmapLoadResult )
2008-10-02 16:23:55 -05:00
{
case VMAP :: VMAP_LOAD_RESULT_OK :
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "VMAP loaded name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
2008-10-02 16:23:55 -05:00
break ;
case VMAP :: VMAP_LOAD_RESULT_ERROR :
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "Could not load VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
2008-10-02 16:23:55 -05:00
break ;
case VMAP :: VMAP_LOAD_RESULT_IGNORED :
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Ignored VMAP name:%s, id:%d, x:%d, y:%d (vmap rep.: x:%d, y:%d)" , GetMapName (), GetId (), gx , gy , gx , gy );
2008-10-02 16:23:55 -05:00
break ;
}
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
void Map :: LoadMap ( int gx , int gy , bool reload )
2008-10-02 16:23:55 -05:00
{
2010-04-07 22:59:46 +02:00
if ( i_InstanceId != 0 )
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( GridMaps [ gx ][ gy ])
2008-10-02 16:23:55 -05:00
return ;
2009-10-17 15:51:44 -07:00
2009-04-02 16:33:35 -06:00
// load grid map for base map
2009-07-16 11:49:00 +08:00
if ( ! m_parentMap -> GridMaps [ gx ][ gy ])
2011-10-18 10:53:34 -04:00
m_parentMap -> EnsureGridCreated ( GridCoord ( 63 - gx , 63 - gy ));
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
(( MapInstanced * )( m_parentMap )) -> AddGridMapReference ( GridCoord ( gx , gy ));
2009-07-16 11:49:00 +08:00
GridMaps [ gx ][ gy ] = m_parentMap -> GridMaps [ gx ][ gy ];
2008-10-02 16:23:55 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( GridMaps [ gx ][ gy ] && ! reload )
2009-05-13 18:24:35 -05:00
return ;
2009-10-17 15:51:44 -07:00
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?)
2010-04-07 19:14:10 +02:00
if ( GridMaps [ gx ][ gy ])
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "Unloading previously loaded map %u before reloading." , GetId ());
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnUnloadGridMap ( this , GridMaps [ gx ][ gy ], gx , gy );
2010-08-07 13:07:18 +02:00
2009-04-02 16:33:35 -06:00
delete ( GridMaps [ gx ][ gy ]);
GridMaps [ gx ][ gy ] = NULL ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// map file name
char * tmp = NULL ;
2010-12-23 23:25:44 +01:00
int len = sWorld -> GetDataPath (). length () + strlen ( "maps/%03u%02u%02u.map" ) + 1 ;
2008-10-02 16:23:55 -05:00
tmp = new char [ len ];
2011-04-29 20:47:02 +02:00
snprintf ( tmp , len , ( char * )( sWorld -> GetDataPath () + "maps/%03u%02u%02u.map" ). c_str (), GetId (), gx , gy );
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "Loading map %s" , tmp );
2008-10-02 16:23:55 -05:00
// loading data
2009-04-02 16:33:35 -06:00
GridMaps [ gx ][ gy ] = new GridMap ();
if ( ! GridMaps [ gx ][ gy ] -> loadData ( tmp ))
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Error loading map file: \n %s \n " , tmp );
2008-10-02 16:23:55 -05:00
}
2009-03-09 18:06:19 -06:00
delete [] tmp ;
2010-08-06 19:23:43 +02:00
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnLoadGridMap ( this , GridMaps [ gx ][ gy ], gx , gy );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
void Map :: LoadMapAndVMap ( int gx , int gy )
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
LoadMap ( gx , gy );
2012-08-22 17:39:25 -05:00
// Only load the data for the base map
2010-04-07 19:14:10 +02:00
if ( i_InstanceId == 0 )
2012-08-22 17:39:25 -05:00
{
LoadVMap ( gx , gy );
LoadMMap ( gx , gy );
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
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 ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
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 ];
}
2009-10-17 15:51:44 -07:00
2010-08-21 20:08:47 +02:00
Map :: Map ( uint32 id , time_t expiry , uint32 InstanceId , uint8 SpawnMode , Map * _parent ) :
2011-10-18 12:39:01 +02:00
_creatureToMoveLock ( false ), i_mapEntry ( sMapStore . LookupEntry ( id )), i_spawnMode ( SpawnMode ), i_InstanceId ( InstanceId ),
2010-08-21 20:08:47 +02:00
m_unloadTimer ( 0 ), m_VisibleDistance ( DEFAULT_VISIBILITY_DISTANCE ),
m_VisibilityNotifyPeriod ( DEFAULT_VISIBILITY_NOTIFY_PERIOD ),
2011-10-07 17:36:23 -04:00
m_activeNonPlayersIter ( m_activeNonPlayers . end ()), i_gridExpiry ( expiry ),
2011-10-18 12:39:01 +02:00
i_scriptLock ( false )
2008-10-02 16:23:55 -05:00
{
2010-09-03 21:55:57 -07:00
m_parentMap = ( _parent ? _parent : this );
2009-12-17 11:30:18 +01:00
for ( unsigned int idx = 0 ; idx < MAX_NUMBER_OF_GRIDS ; ++ idx )
2008-10-02 16:23:55 -05:00
{
2009-12-17 11:30:18 +01:00
for ( unsigned int j = 0 ; j < MAX_NUMBER_OF_GRIDS ; ++ j )
2008-10-02 16:23:55 -05:00
{
//z code
2009-12-17 11:30:18 +01:00
GridMaps [ idx ][ j ] = NULL ;
2008-10-02 16:23:55 -05:00
setNGrid ( NULL , idx , j );
}
}
2009-10-17 16:20:24 -07:00
2009-09-27 02:24:25 -07:00
//lets initialize visibility distance for map
Map :: InitVisibilityDistance ();
2010-08-06 19:23:43 +02:00
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnCreateMap ( this );
2009-09-27 02:24:25 -07:00
}
2009-10-17 16:20:24 -07:00
2009-09-27 02:24:25 -07:00
void Map :: InitVisibilityDistance ()
{
//init visibility for continents
2009-11-22 19:56:48 -08:00
m_VisibleDistance = World :: GetMaxVisibleDistanceOnContinents ();
2010-02-27 15:25:14 +02:00
m_VisibilityNotifyPeriod = World :: GetVisibilityNotifyPeriodOnContinents ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Template specialization of utility methods
template < class T >
2011-10-18 11:51:30 -04:00
void Map :: AddToGrid ( T * obj , Cell const & cell )
2008-10-02 16:23:55 -05:00
{
2011-10-18 11:51:30 -04:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
2011-12-20 17:05:08 -05:00
if ( obj -> IsWorldObject ())
2011-10-22 09:36:05 -04:00
grid -> GetGridType ( cell . CellX (), cell . CellY ()). template AddWorldObject < T > ( obj );
2009-08-11 16:14:45 -05:00
else
2011-10-22 09:36:05 -04:00
grid -> GetGridType ( cell . CellX (), cell . CellY ()). template AddGridObject < T > ( obj );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template <>
2011-10-18 11:51:30 -04:00
void Map :: AddToGrid ( Creature * obj , Cell const & cell )
2008-10-02 16:23:55 -05:00
{
2011-10-18 11:51:30 -04:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
2011-12-20 17:05:08 -05:00
if ( obj -> IsWorldObject ())
2011-10-22 09:36:05 -04:00
grid -> GetGridType ( cell . CellX (), cell . CellY ()). AddWorldObject ( obj );
2008-10-02 16:23:55 -05:00
else
2011-10-22 09:36:05 -04:00
grid -> GetGridType ( cell . CellX (), cell . CellY ()). AddGridObject ( obj );
2009-10-17 15:51:44 -07:00
2009-02-17 21:50:18 -06:00
obj -> SetCurrentCell ( cell );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-20 17:05:08 -05:00
void Map :: SwitchGridContainers ( Creature * obj , bool on )
2008-11-05 20:51:05 -06:00
{
2011-12-20 17:05:08 -05:00
ASSERT ( ! obj -> IsPermanentWorldObject ());
2011-10-18 10:53:34 -04:00
CellCoord p = Trinity :: ComputeCellCoord ( obj -> GetPositionX (), obj -> GetPositionY ());
2011-10-18 11:05:32 -04:00
if ( ! p . IsCoordValid ())
2009-04-08 12:20:39 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map::SwitchGridContainers: Object " UI64FMTD " has invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
2009-04-08 12:20:39 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2009-04-08 12:20:39 -05:00
Cell cell ( p );
2011-10-18 11:51:30 -04:00
if ( ! IsGridLoaded ( GridCoord ( cell . data . Part . grid_x , cell . data . Part . grid_y )))
2009-04-08 12:20:39 -05:00
return ;
2009-10-17 15:51:44 -07:00
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Switch object " UI64FMTD " 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 ());
2010-07-30 20:04:28 +02:00
ASSERT ( ngrid != NULL );
2009-10-17 15:51:44 -07:00
2011-10-22 09:36:05 -04:00
GridType & grid = ngrid -> GetGridType ( cell . CellX (), cell . CellY ());
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
obj -> RemoveFromGrid (); //This step is not really necessary but we want to do ASSERT in remove/add
2010-04-07 19:14:10 +02:00
if ( on )
2011-12-20 17:05:08 -05:00
{
grid . AddWorldObject ( obj );
AddWorldObject ( obj );
}
2009-02-17 21:50:18 -06:00
else
2011-12-20 17:05:08 -05:00
{
grid . AddGridObject ( obj );
RemoveWorldObject ( obj );
}
obj -> m_isTempWorldObject = on ;
2008-11-05 20:51:05 -06:00
}
2009-10-17 15:51:44 -07: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 ;
}
2009-10-17 15:51:44 -07:00
2009-07-16 11:49:00 +08:00
template <>
2011-11-07 11:18:00 -06:00
void Map :: DeleteFromWorld ( Player * player )
2009-07-16 11:49:00 +08:00
{
2011-11-07 11:18:00 -06:00
sObjectAccessor -> RemoveObject ( player );
sObjectAccessor -> RemoveUpdateObject ( player ); //TODO: I do not know why we need this, it should be removed in ~Object anyway
delete player ;
2009-07-16 11:49:00 +08:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 11:51:30 -04:00
//Create NGrid so the object can be added to it
//But object data is not loaded here
void Map :: EnsureGridCreated ( const GridCoord & p )
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( ! getNGrid ( p . x_coord , p . y_coord ))
2008-10-02 16:23:55 -05:00
{
2011-11-08 10:48:49 -05:00
TRINITY_GUARD ( ACE_Thread_Mutex , Lock );
2010-04-07 19:14:10 +02:00
if ( ! getNGrid ( p . x_coord , p . y_coord ))
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creating grid[%u, %u] for map %u instance %u" , p . x_coord , p . y_coord , GetId (), i_InstanceId );
2009-10-17 15:51:44 -07:00
2010-12-23 23:25:44 +01:00
setNGrid ( new NGridType ( p . x_coord * MAX_NUMBER_OF_GRIDS + p . y_coord , p . x_coord , p . y_coord , i_gridExpiry , sWorld -> getBoolConfig ( CONFIG_GRID_UNLOAD )),
2008-10-02 16:23:55 -05:00
p . x_coord , p . y_coord );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// build a linkage between this map and NGridType
buildNGridLinkage ( getNGrid ( p . x_coord , p . y_coord ));
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
getNGrid ( p . x_coord , p . y_coord ) -> SetGridState ( GRID_STATE_IDLE );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
//z coord
2009-09-27 02:24:25 -07:00
int gx = ( MAX_NUMBER_OF_GRIDS - 1 ) - p . x_coord ;
int gy = ( MAX_NUMBER_OF_GRIDS - 1 ) - p . y_coord ;
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! GridMaps [ gx ][ gy ])
2011-04-29 20:47:02 +02:00
LoadMapAndVMap ( gx , gy );
2008-10-02 16:23:55 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
2011-10-18 11:51:30 -04:00
//Load NGrid and make it active
void Map :: EnsureGridLoadedForActiveObject ( const Cell & cell , WorldObject * object )
2008-10-02 16:23:55 -05:00
{
2010-01-16 11:43:47 +02:00
EnsureGridLoaded ( cell );
2010-04-07 18:09:10 +02:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
2010-07-30 20:04:28 +02:00
ASSERT ( grid != NULL );
2009-10-17 15:51:44 -07:00
2010-01-16 11:43:47 +02:00
// refresh grid state & timer
2010-04-07 22:59:46 +02:00
if ( grid -> GetGridState () != GRID_STATE_ACTIVE )
2010-01-16 11:43:47 +02:00
{
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Active object " UI64FMTD " triggers loading of grid [%u, %u] on map %u" , object -> GetGUID (), cell . GridX (), cell . GridY (), GetId ());
2010-01-16 11:43:47 +02:00
ResetGridExpiry ( * grid , 0.1f );
grid -> SetGridState ( GRID_STATE_ACTIVE );
}
2009-04-02 16:33:35 -06:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 11:51:30 -04:00
//Create NGrid and load the object data in it
2009-04-02 16:33:35 -06:00
bool Map :: EnsureGridLoaded ( const Cell & cell )
{
2011-10-18 10:53:34 -04:00
EnsureGridCreated ( GridCoord ( cell . GridX (), cell . GridY ()));
2009-04-02 16:33:35 -06:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
2009-10-17 15:51:44 -07:00
2010-07-30 20:04:28 +02:00
ASSERT ( grid != NULL );
2010-04-07 22:59:46 +02:00
if ( ! isGridObjectDataLoaded ( cell . GridX (), cell . GridY ()))
2009-04-02 16:33:35 -06:00
{
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Loading grid[%u, %u] for map %u instance %u" , cell . GridX (), cell . GridY (), GetId (), i_InstanceId );
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
setGridObjectDataLoaded ( true , cell . GridX (), cell . GridY ());
2010-10-22 03:39:27 +02:00
2008-10-02 16:23:55 -05:00
ObjectGridLoader loader ( * grid , this , cell );
loader . LoadN ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// Add resurrectable corpses to world object list in grid
2011-10-22 09:36:05 -04:00
sObjectAccessor -> AddCorpsesToGrid ( GridCoord ( cell . GridX (), cell . GridY ()), grid -> GetGridType ( cell . CellX (), cell . CellY ()), this );
2012-02-03 16:03:52 -05:00
Balance ();
2009-04-02 16:33:35 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-04-02 16:33:35 -06:00
return false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-12-20 14:10:53 -06:00
void Map :: LoadGrid ( float x , float y )
{
2011-10-18 11:18:49 -04:00
EnsureGridLoaded ( Cell ( x , y ));
2008-12-20 14:10:53 -06:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
bool Map :: AddPlayerToMap ( Player * player )
2008-10-02 16:23:55 -05:00
{
2011-11-11 17:50:26 -05:00
CellCoord cellCoord = Trinity :: ComputeCellCoord ( player -> GetPositionX (), player -> GetPositionY ());
if ( ! cellCoord . IsCoordValid ())
2010-01-16 11:43:47 +02:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map::Add: Player (GUID: %u) has invalid coordinates X:%f Y:%f grid cell [%u:%u]" , player -> GetGUIDLow (), player -> GetPositionX (), player -> GetPositionY (), cellCoord . x_coord , cellCoord . y_coord );
2010-01-16 11:43:47 +02:00
return false ;
}
2011-11-11 17:50:26 -05:00
Cell cell ( cellCoord );
2011-10-18 11:51:30 -04:00
EnsureGridLoadedForActiveObject ( cell , player );
AddToGrid ( player , cell );
2010-01-16 11:43:47 +02:00
2011-11-11 17:50:26 -05:00
// Check if we are adding to correct map
ASSERT ( player -> GetMap () == this );
player -> SetMap ( this );
2008-10-02 16:23:55 -05:00
player -> AddToWorld ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
SendInitSelf ( player );
SendInitTransports ( player );
2009-10-17 15:51:44 -07:00
2009-03-27 09:06:26 -06:00
player -> m_clientGUIDs . clear ();
2010-11-13 17:18:09 +01:00
player -> UpdateObjectVisibility ( false );
2009-10-17 15:51:44 -07:00
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnPlayerEnterMap ( this , player );
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2011-10-07 17:36:23 -04:00
template < class T >
2011-10-18 12:39:01 +02:00
void Map :: InitializeObject ( T * /*obj*/ )
2011-10-07 17:36:23 -04:00
{
}
template <>
void Map :: InitializeObject ( Creature * obj )
{
obj -> _moveState = CREATURE_CELL_MOVE_NONE ;
}
2008-10-02 16:23:55 -05:00
template < class T >
2011-11-28 13:23:20 -05:00
bool Map :: AddToMap ( T * obj )
2008-10-02 16:23:55 -05:00
{
2011-11-11 17:50:26 -05:00
//TODO: Needs clean up. An object should not be added to map twice.
if ( obj -> IsInWorld ())
2008-10-02 16:23:55 -05:00
{
2011-11-11 17:50:26 -05:00
ASSERT ( obj -> IsInGrid ());
obj -> UpdateObjectVisibility ( true );
2011-11-28 13:23:20 -05:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
CellCoord cellCoord = Trinity :: ComputeCellCoord ( obj -> GetPositionX (), obj -> GetPositionY ());
2011-11-28 11:32:52 -05:00
//It will create many problems (including crashes) if an object is not added to grid after creation
//The correct way to fix it is to make AddToMap return false and delete the object if it is not added to grid
//But now AddToMap is used in too many places, I will just see how many ASSERT failures it will cause
ASSERT ( cellCoord . IsCoordValid ());
2011-11-11 17:50:26 -05:00
if ( ! cellCoord . IsCoordValid ())
2009-08-11 10:01:39 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map::Add: Object " UI64FMTD " has invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), cellCoord . x_coord , cellCoord . y_coord );
2011-11-28 13:23:20 -05:00
return false ; //Should delete object
2009-08-11 10:01:39 -05:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
Cell cell ( cellCoord );
2010-04-07 19:14:10 +02:00
if ( obj -> isActiveObject ())
2011-10-18 11:51:30 -04:00
EnsureGridLoadedForActiveObject ( cell , obj );
2009-02-25 20:52:20 -06:00
else
2011-10-18 10:53:34 -04:00
EnsureGridCreated ( GridCoord ( cell . GridX (), cell . GridY ()));
2011-10-18 11:51:30 -04:00
AddToGrid ( obj , cell );
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Object %u enters grid[%u, %u]" , GUID_LOPART ( obj -> GetGUID ()), cell . GridX (), cell . GridY ());
2011-11-11 17:50:26 -05:00
//Must already be set before AddToMap. Usually during obj->Create.
2009-08-11 18:23:54 -05:00
//obj->SetMap(this);
2008-10-02 16:23:55 -05:00
obj -> AddToWorld ();
2011-11-11 17:50:26 -05:00
2011-10-07 17:36:23 -04:00
InitializeObject ( obj );
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( obj -> isActiveObject ())
2009-02-25 20:52:20 -06:00
AddToActive ( obj );
2009-10-17 15:51:44 -07:00
2009-04-01 22:40:12 -06:00
//something, such as vehicle, needs to be update immediately
2009-05-03 11:06:48 -05:00
//also, trigger needs to cast spell, if not update, cannot see visual
2010-02-27 15:25:14 +02:00
obj -> UpdateObjectVisibility ( true );
2011-11-28 13:23:20 -05:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 11:51:30 -04:00
bool Map :: IsGridLoaded ( const GridCoord & p ) const
2008-10-02 16:23:55 -05:00
{
2010-04-07 22:59:46 +02:00
return ( getNGrid ( p . x_coord , p . y_coord ) && isGridObjectDataLoaded ( p . x_coord , p . y_coord ));
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-11-13 17:18:09 +01:00
void Map :: VisitNearbyCellsOf ( WorldObject * obj , TypeContainerVisitor < Trinity :: ObjectUpdater , GridTypeMapContainer > & gridVisitor , TypeContainerVisitor < Trinity :: ObjectUpdater , WorldTypeMapContainer > & worldVisitor )
2008-12-27 11:26:22 -06:00
{
2011-10-19 17:40:44 +01:00
// Check for valid position
if ( ! obj -> IsPositionValid ())
2010-11-13 17:18:09 +01:00
return ;
2011-10-19 17:40:44 +01:00
// Update mobs/objects in ALL visible cells around object!
CellArea area = Cell :: CalculateCellArea ( obj -> GetPositionX (), obj -> GetPositionY (), obj -> GetGridActivationRange ());
2010-11-13 17:18:09 +01:00
2011-10-19 17:40:44 +01:00
for ( uint32 x = area . low_bound . x_coord ; x <= area . high_bound . x_coord ; ++ x )
2009-12-17 07:34:00 +01:00
{
2011-10-19 17:40:44 +01:00
for ( uint32 y = area . low_bound . y_coord ; y <= area . high_bound . y_coord ; ++ y )
2010-11-13 17:18:09 +01:00
{
// 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 ))
continue ;
markCell ( cell_id );
2011-10-18 10:53:34 -04:00
CellCoord pair ( x , y );
2010-11-13 17:18:09 +01:00
Cell cell ( pair );
2012-03-12 04:33:24 +01:00
cell . SetNoCreate ();
2011-10-19 17:40:44 +01:00
Visit ( cell , gridVisitor );
Visit ( cell , worldVisitor );
2010-11-13 17:18:09 +01:00
}
2009-12-17 07:34:00 +01:00
}
2010-11-13 17:18:09 +01:00
}
2009-10-17 15:51:44 -07:00
2011-08-01 14:23:27 +02:00
void Map :: Update ( const uint32 t_diff )
2010-11-13 17:18:09 +01:00
{
2012-02-11 19:23:18 -05:00
_dynamicTree . update ( t_diff );
2010-12-13 22:37:56 +01:00
/// update worldsessions for existing players
2011-06-12 02:30:32 +02:00
for ( m_mapRefIter = m_mapRefManager . begin (); m_mapRefIter != m_mapRefManager . end (); ++ m_mapRefIter )
2010-12-13 22:37:56 +01:00
{
2011-11-07 11:06:39 -06:00
Player * player = m_mapRefIter -> getSource ();
if ( player && player -> IsInWorld ())
2010-12-13 22:37:56 +01:00
{
2011-11-07 11:06:39 -06:00
//player->Update(t_diff);
2012-03-12 04:33:24 +01:00
WorldSession * session = player -> GetSession ();
MapSessionFilter updater ( session );
session -> Update ( t_diff , updater );
2010-12-13 22:37:56 +01:00
}
}
2009-06-17 18:37:53 -05:00
/// update active cells around players and active objects
2008-11-21 15:41:18 -06:00
resetMarkedCells ();
2009-10-17 15:51:44 -07:00
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-10-17 15:51:44 -07:00
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
2009-10-17 16:20:24 -07:00
for ( m_mapRefIter = m_mapRefManager . begin (); m_mapRefIter != m_mapRefManager . end (); ++ m_mapRefIter )
2008-11-21 15:41:18 -06:00
{
2011-11-07 11:06:39 -06:00
Player * player = m_mapRefIter -> getSource ();
2009-10-17 15:51:44 -07:00
2011-11-07 11:06:39 -06:00
if ( ! player || ! player -> IsInWorld ())
2009-02-25 20:52:20 -06:00
continue ;
2009-10-17 15:51:44 -07:00
2010-11-13 17:18:09 +01:00
// update players at tick
2011-11-07 11:06:39 -06:00
player -> Update ( t_diff );
2009-10-17 15:51:44 -07:00
2011-11-07 11:06:39 -06:00
VisitNearbyCellsOf ( player , grid_object_update , world_object_update );
2009-02-25 20:52:20 -06:00
}
2009-10-17 15:51:44 -07:00
2010-11-13 17:18:09 +01:00
// non-player active objects, increasing iterator in the loop in case of object removal
for ( m_activeNonPlayersIter = m_activeNonPlayers . begin (); m_activeNonPlayersIter != m_activeNonPlayers . end ();)
2009-02-25 20:52:20 -06:00
{
2010-11-13 17:18:09 +01:00
WorldObject * obj = * m_activeNonPlayersIter ;
++ m_activeNonPlayersIter ;
2009-10-17 15:51:44 -07:00
2011-08-04 19:41:11 +02:00
if ( ! obj || ! obj -> IsInWorld ())
2010-11-13 17:18:09 +01:00
continue ;
2009-10-17 15:51:44 -07:00
2010-11-13 17:18:09 +01:00
VisitNearbyCellsOf ( obj , grid_object_update , world_object_update );
2008-11-21 15:41:18 -06:00
}
2009-10-17 15:51:44 -07:00
2009-07-31 10:48:34 +08:00
///- Process necessary scripts
if ( ! m_scriptSchedule . empty ())
2009-08-19 17:42:56 -05:00
{
i_scriptLock = true ;
2009-07-31 10:48:34 +08:00
ScriptsProcess ();
2009-08-19 17:42:56 -05:00
i_scriptLock = false ;
}
2009-10-17 15:51:44 -07:00
2009-08-25 15:12:22 -05:00
MoveAllCreaturesInMoveList ();
2009-12-19 10:12:40 +01:00
2010-04-07 19:14:10 +02:00
if ( ! m_mapRefManager . isEmpty () || ! m_activeNonPlayers . empty ())
2010-02-27 15:25:14 +02:00
ProcessRelocationNotifies ( t_diff );
2010-08-06 19:23:43 +02:00
2010-12-22 21:25:23 +01:00
sScriptMgr -> OnMapUpdate ( this , t_diff );
2009-12-19 10:12:40 +01:00
}
2010-02-27 15:25:14 +02:00
struct ResetNotifier
2009-12-19 10:12:40 +01:00
{
2010-02-27 15:25:14 +02:00
template < class T > inline void resetNotify ( GridRefManager < T > & m )
2009-12-19 10:12:40 +01:00
{
2010-04-07 19:12:44 +02:00
for ( typename GridRefManager < T >:: iterator iter = m . begin (); iter != m . end (); ++ iter )
2010-02-27 15:25:14 +02:00
iter -> getSource () -> ResetAllNotifies ();
2009-12-19 10:12:40 +01:00
}
2010-02-27 15:25:14 +02:00
template < class T > void Visit ( GridRefManager < T > & ) {}
void Visit ( CreatureMapType & m ) { resetNotify < Creature > ( m );}
void Visit ( PlayerMapType & m ) { resetNotify < Player > ( m );}
};
2009-12-19 10:12:40 +01:00
2011-08-01 14:23:27 +02:00
void Map :: ProcessRelocationNotifies ( const uint32 diff )
2009-12-19 10:12:40 +01:00
{
2010-04-07 19:12:44 +02:00
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end (); ++ i )
2009-12-19 10:12:40 +01:00
{
NGridType * grid = i -> getSource ();
2010-02-27 15:25:14 +02:00
if ( grid -> GetGridState () != GRID_STATE_ACTIVE )
continue ;
grid -> getGridInfoRef () -> getRelocationTimer (). TUpdate ( diff );
if ( ! grid -> getGridInfoRef () -> getRelocationTimer (). TPassed ())
2009-12-19 10:12:40 +01:00
continue ;
uint32 gx = grid -> getX (), gy = grid -> getY ();
2011-10-18 10:53:34 -04:00
CellCoord cell_min ( gx * MAX_NUMBER_OF_CELLS , gy * MAX_NUMBER_OF_CELLS );
CellCoord cell_max ( cell_min . x_coord + MAX_NUMBER_OF_CELLS , cell_min . y_coord + MAX_NUMBER_OF_CELLS );
2009-12-19 10:12:40 +01:00
2010-04-07 19:12:44 +02:00
for ( uint32 x = cell_min . x_coord ; x < cell_max . x_coord ; ++ x )
2009-12-19 10:12:40 +01:00
{
2010-04-07 19:12:44 +02:00
for ( uint32 y = cell_min . y_coord ; y < cell_max . y_coord ; ++ y )
2009-12-19 10:12:40 +01:00
{
uint32 cell_id = ( y * TOTAL_NUMBER_OF_CELLS_PER_MAP ) + x ;
2010-04-07 19:14:10 +02:00
if ( ! isCellMarked ( cell_id ))
2009-12-19 10:12:40 +01:00
continue ;
2011-10-18 10:53:34 -04:00
CellCoord pair ( x , y );
2009-12-19 10:12:40 +01:00
Cell cell ( pair );
cell . SetNoCreate ();
2010-11-13 17:18:09 +01:00
Trinity :: DelayedUnitRelocation cell_relocation ( cell , pair , * this , MAX_VISIBILITY_DISTANCE );
2009-12-19 10:12:40 +01:00
TypeContainerVisitor < Trinity :: DelayedUnitRelocation , GridTypeMapContainer > grid_object_relocation ( cell_relocation );
TypeContainerVisitor < Trinity :: DelayedUnitRelocation , WorldTypeMapContainer > world_object_relocation ( cell_relocation );
2010-02-27 15:25:14 +02:00
Visit ( cell , grid_object_relocation );
Visit ( cell , world_object_relocation );
2009-12-19 10:12:40 +01:00
}
}
}
2010-02-27 15:25:14 +02:00
ResetNotifier reset ;
TypeContainerVisitor < ResetNotifier , GridTypeMapContainer > grid_notifier ( reset );
TypeContainerVisitor < ResetNotifier , WorldTypeMapContainer > world_notifier ( reset );
2010-04-07 19:12:44 +02:00
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end (); ++ i )
2009-12-19 10:12:40 +01:00
{
NGridType * grid = i -> getSource ();
2010-02-27 15:25:14 +02:00
if ( grid -> GetGridState () != GRID_STATE_ACTIVE )
continue ;
if ( ! grid -> getGridInfoRef () -> getRelocationTimer (). TPassed ())
2009-12-19 10:12:40 +01:00
continue ;
2010-02-27 15:25:14 +02:00
grid -> getGridInfoRef () -> getRelocationTimer (). TReset ( diff , m_VisibilityNotifyPeriod );
2009-12-19 10:12:40 +01:00
uint32 gx = grid -> getX (), gy = grid -> getY ();
2011-10-18 10:53:34 -04:00
CellCoord cell_min ( gx * MAX_NUMBER_OF_CELLS , gy * MAX_NUMBER_OF_CELLS );
CellCoord cell_max ( cell_min . x_coord + MAX_NUMBER_OF_CELLS , cell_min . y_coord + MAX_NUMBER_OF_CELLS );
2009-12-19 10:12:40 +01:00
2010-04-07 19:12:44 +02:00
for ( uint32 x = cell_min . x_coord ; x < cell_max . x_coord ; ++ x )
2009-12-19 10:12:40 +01:00
{
2010-04-07 19:12:44 +02:00
for ( uint32 y = cell_min . y_coord ; y < cell_max . y_coord ; ++ y )
2009-12-19 10:12:40 +01:00
{
uint32 cell_id = ( y * TOTAL_NUMBER_OF_CELLS_PER_MAP ) + x ;
2010-04-07 19:14:10 +02:00
if ( ! isCellMarked ( cell_id ))
2009-12-19 10:12:40 +01:00
continue ;
2011-10-18 10:53:34 -04:00
CellCoord pair ( x , y );
2009-12-19 10:12:40 +01:00
Cell cell ( pair );
cell . SetNoCreate ();
2010-02-27 15:25:14 +02:00
Visit ( cell , grid_notifier );
Visit ( cell , world_notifier );
2009-12-19 10:12:40 +01:00
}
}
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
void Map :: RemovePlayerFromMap ( Player * player , bool remove )
2008-10-02 16:23:55 -05:00
{
2009-08-14 14:09:57 -05:00
player -> RemoveFromWorld ();
SendRemoveTransports ( player );
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
player -> UpdateObjectVisibility ( true );
2011-11-12 08:16:52 -05:00
if ( player -> IsInGrid ())
player -> RemoveFromGrid ();
else
ASSERT ( remove ); //maybe deleted in logoutplayer when player is not in a map
2009-10-17 15:51:44 -07:00
2009-10-31 16:00:47 -07:00
if ( remove )
2012-02-16 16:17:56 +01:00
{
2008-10-02 16:23:55 -05:00
DeleteFromWorld ( player );
2010-08-06 19:23:43 +02:00
2012-02-10 12:43:13 +01:00
sScriptMgr -> OnPlayerLeaveMap ( this , player );
2012-02-16 16:17:56 +01:00
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
template < class T >
2011-11-28 13:23:20 -05:00
void Map :: RemoveFromMap ( T * obj , bool remove )
2008-10-02 16:23:55 -05:00
{
obj -> RemoveFromWorld ();
2009-10-31 16:00:47 -07:00
if ( obj -> isActiveObject ())
2009-02-25 20:52:20 -06:00
RemoveFromActive ( obj );
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
obj -> UpdateObjectVisibility ( true );
obj -> RemoveFromGrid ();
2009-10-17 15:51:44 -07:00
2009-07-16 11:49:00 +08:00
obj -> ResetMap ();
2009-10-17 15:51:44 -07:00
2009-10-31 16:00:47 -07:00
if ( remove )
2008-10-02 16:23:55 -05:00
{
// if option set then object already saved at this moment
2010-12-23 23:25:44 +01:00
if ( ! sWorld -> getBoolConfig ( CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY ))
2008-10-02 16:23:55 -05:00
obj -> SaveRespawnTime ();
DeleteFromWorld ( obj );
}
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
void Map :: PlayerRelocation ( Player * player , float x , float y , float z , float orientation )
2008-10-02 16:23:55 -05:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( player );
2009-10-17 15:51:44 -07:00
2011-10-18 11:18:49 -04:00
Cell old_cell ( player -> GetPositionX (), player -> GetPositionY ());
Cell new_cell ( x , y );
2009-10-17 15:51:44 -07:00
2012-03-12 00:45:58 +01:00
//! If hovering, always increase our server-side Z position
//! Client automatically projects correct position based on Z coord sent in monster move
//! and UNIT_FIELD_HOVERHEIGHT sent in object updates
if ( player -> HasUnitMovementFlag ( MOVEMENTFLAG_HOVER ))
z += player -> GetFloatValue ( UNIT_FIELD_HOVERHEIGHT );
2008-10-02 16:23:55 -05:00
player -> Relocate ( x , y , z , orientation );
2012-03-01 17:06:41 +01:00
if ( player -> IsVehicle ())
player -> GetVehicleKit () -> RelocatePassengers ( x , y , z , orientation );
2009-10-17 15:51:44 -07:00
2009-10-31 16:00:47 -07:00
if ( old_cell . DiffGrid ( new_cell ) || old_cell . DiffCell ( new_cell ))
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "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 ());
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
player -> RemoveFromGrid ();
2010-01-16 11:43:47 +02:00
2010-04-07 22:59:46 +02:00
if ( old_cell . DiffGrid ( new_cell ))
2011-10-18 11:51:30 -04:00
EnsureGridLoadedForActiveObject ( new_cell , player );
2010-01-16 11:43:47 +02:00
2011-10-18 11:51:30 -04:00
AddToGrid ( player , new_cell );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-02-27 15:25:14 +02:00
player -> UpdateObjectVisibility ( false );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
void Map :: CreatureRelocation ( Creature * creature , float x , float y , float z , float ang , bool respawnRelocationOnFail )
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
ASSERT ( CheckGridIntegrity ( creature , false ));
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
Cell old_cell = creature -> GetCurrentCell ();
2011-10-18 11:18:49 -04:00
Cell new_cell ( x , y );
2009-10-17 15:51:44 -07:00
2010-10-25 02:30:50 +02:00
if ( ! respawnRelocationOnFail && ! getNGrid ( new_cell . GridX (), new_cell . GridY ()))
return ;
2012-03-12 00:45:58 +01:00
//! If hovering, always increase our server-side Z position
//! Client automatically projects correct position based on Z coord sent in monster move
//! and UNIT_FIELD_HOVERHEIGHT sent in object updates
if ( creature -> HasUnitMovementFlag ( MOVEMENTFLAG_HOVER ))
z += creature -> GetFloatValue ( UNIT_FIELD_HOVERHEIGHT );
2008-10-02 16:23:55 -05:00
// delay creature move for grid/cell to grid/cell moves
2009-10-31 16:00:47 -07:00
if ( old_cell . DiffCell ( new_cell ) || old_cell . DiffGrid ( new_cell ))
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "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 ());
2008-10-02 16:23:55 -05:00
#endif
2009-10-31 16:00:47 -07:00
AddCreatureToMoveList ( creature , x , y , z , ang );
2008-10-02 16:23:55 -05:00
// in diffcell/diffgrid case notifiers called at finishing move creature in Map::MoveAllCreaturesInMoveList
}
else
{
creature -> Relocate ( x , y , z , ang );
2012-03-01 17:06:41 +01:00
if ( creature -> IsVehicle ())
creature -> GetVehicleKit () -> RelocatePassengers ( x , y , z , ang );
2010-02-27 15:25:14 +02:00
creature -> UpdateObjectVisibility ( false );
2011-10-07 17:36:23 -04:00
RemoveCreatureFromMoveList ( creature );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
ASSERT ( CheckGridIntegrity ( creature , true ));
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
void Map :: AddCreatureToMoveList ( Creature * c , float x , float y , float z , float ang )
2008-10-02 16:23:55 -05:00
{
2011-10-07 17:36:23 -04:00
if ( _creatureToMoveLock ) //can this happen?
return ;
2011-10-18 11:42:40 +02:00
if ( c -> _moveState == CREATURE_CELL_MOVE_NONE )
2011-10-07 17:36:23 -04:00
_creaturesToMove . push_back ( c );
c -> SetNewCellPosition ( x , y , z , ang );
}
void Map :: RemoveCreatureFromMoveList ( Creature * c )
{
if ( _creatureToMoveLock ) //can this happen?
2008-10-02 16:23:55 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-10-18 11:42:40 +02:00
if ( c -> _moveState == CREATURE_CELL_MOVE_ACTIVE )
2011-10-07 17:36:23 -04:00
c -> _moveState = CREATURE_CELL_MOVE_INACTIVE ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void Map :: MoveAllCreaturesInMoveList ()
{
2011-10-07 17:36:23 -04:00
_creatureToMoveLock = true ;
2012-04-08 23:59:52 +02:00
for ( std :: vector < Creature *>:: iterator itr = _creaturesToMove . begin (); itr != _creaturesToMove . end (); ++ itr )
2008-10-02 16:23:55 -05:00
{
2011-10-07 17:36:23 -04:00
Creature * c = * itr ;
2012-04-08 23:59:52 +02:00
if ( c -> FindMap () != this ) //pet is teleported to another map
2011-10-07 17:36:23 -04:00
continue ;
2012-04-08 23:59:52 +02:00
if ( c -> _moveState != CREATURE_CELL_MOVE_ACTIVE )
2011-10-07 17:36:23 -04:00
{
c -> _moveState = CREATURE_CELL_MOVE_NONE ;
continue ;
}
c -> _moveState = CREATURE_CELL_MOVE_NONE ;
2012-04-08 23:59:52 +02:00
if ( ! c -> IsInWorld ())
2011-10-07 17:36:23 -04:00
continue ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// do move or do move to respawn or remove creature if previous all fail
2011-10-18 11:18:49 -04:00
if ( CreatureCellRelocation ( c , Cell ( c -> _newPosition . m_positionX , c -> _newPosition . m_positionY )))
2008-10-02 16:23:55 -05:00
{
// update pos
2011-10-07 17:36:23 -04:00
c -> Relocate ( c -> _newPosition );
2011-10-18 10:53:34 -04:00
//CreatureRelocationNotify(c, new_cell, new_cell.cellCoord());
2010-02-27 15:25:14 +02:00
c -> UpdateObjectVisibility ( false );
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
2011-10-07 17:36:23 -04:00
if ( ! CreatureRespawnRelocation ( c , false ))
2008-10-02 16:23:55 -05:00
{
// ... or unload (if respawn grid also not loaded)
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creature (GUID: %u Entry: %u) cannot be move to unloaded respawn grid." , c -> GetGUIDLow (), c -> GetEntry ());
2008-10-02 16:23:55 -05:00
#endif
2011-09-13 11:54:55 -04:00
//AddObjectToRemoveList(Pet*) should only be called in Pet::Remove
//This may happen when a player just logs in and a pet moves to a nearby unloaded cell
//To avoid this, we can load nearby cells when player log in
//But this check is always needed to ensure safety
//TODO: pets will disappear if this is outside CreatureRespawnRelocation
//need to check why pet is frequently relocated to an unloaded cell
if ( c -> isPet ())
(( Pet * ) c ) -> Remove ( PET_SAVE_NOT_IN_SLOT , true );
else
AddObjectToRemoveList ( c );
2008-10-02 16:23:55 -05:00
}
}
}
2011-10-07 17:36:23 -04:00
_creaturesToMove . clear ();
_creatureToMoveLock = false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
bool Map :: CreatureCellRelocation ( Creature * c , Cell new_cell )
2008-10-02 16:23:55 -05:00
{
Cell const & old_cell = c -> GetCurrentCell ();
2009-10-31 16:00:47 -07:00
if ( ! old_cell . DiffGrid ( new_cell )) // in same grid
2008-10-02 16:23:55 -05:00
{
// if in same cell then none do
2009-10-31 16:00:47 -07:00
if ( old_cell . DiffCell ( new_cell ))
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "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 ());
2008-10-02 16:23:55 -05:00
#endif
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
c -> RemoveFromGrid ();
2011-10-18 11:51:30 -04:00
AddToGrid ( c , new_cell );
2008-10-02 16:23:55 -05:00
}
else
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creature (GUID: %u Entry: %u) moved in same grid[%u, %u]cell[%u, %u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY ());
2008-10-02 16:23:55 -05:00
#endif
}
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
// in diff. grids but active creature
2009-10-31 16:00:47 -07:00
if ( c -> isActiveObject ())
2009-02-25 20:52:20 -06:00
{
2011-10-18 11:51:30 -04:00
EnsureGridLoadedForActiveObject ( new_cell , c );
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "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 ());
2009-02-25 20:52:20 -06:00
#endif
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
c -> RemoveFromGrid ();
2011-10-18 11:51:30 -04:00
AddToGrid ( c , new_cell );
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
// in diff. loaded grid normal creature
2011-10-18 11:51:30 -04:00
if ( IsGridLoaded ( GridCoord ( new_cell . GridX (), new_cell . GridY ())))
2008-10-02 16:23:55 -05:00
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "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 ());
2008-10-02 16:23:55 -05:00
#endif
2009-10-17 15:51:44 -07:00
2011-11-10 17:31:45 -05:00
c -> RemoveFromGrid ();
2011-10-18 10:53:34 -04:00
EnsureGridCreated ( GridCoord ( new_cell . GridX (), new_cell . GridY ()));
2011-10-18 11:51:30 -04:00
AddToGrid ( c , new_cell );
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
// fail to move: normal creature attempt move to unloaded grid
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creature (GUID: %u Entry: %u) attempted to 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 ());
2009-02-25 20:52:20 -06:00
#endif
return false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-10-07 17:36:23 -04:00
bool Map :: CreatureRespawnRelocation ( Creature * c , bool diffGridOnly )
2008-10-02 16:23:55 -05:00
{
float resp_x , resp_y , resp_z , resp_o ;
2011-10-18 10:53:34 -04:00
c -> GetRespawnPosition ( resp_x , resp_y , resp_z , & resp_o );
2011-10-18 11:18:49 -04:00
Cell resp_cell ( resp_x , resp_y );
2009-10-17 15:51:44 -07:00
2011-10-07 17:36:23 -04:00
//creature will be unloaded with grid
2011-10-18 11:42:40 +02:00
if ( diffGridOnly && ! c -> GetCurrentCell (). DiffGrid ( resp_cell ))
2011-10-07 17:36:23 -04:00
return true ;
2008-10-02 16:23:55 -05:00
c -> CombatStop ();
c -> GetMotionMaster () -> Clear ();
2009-10-17 15:51:44 -07:00
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creature (GUID: %u Entry: %u) 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 ());
2008-10-02 16:23:55 -05:00
#endif
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// teleport it to respawn point (like normal respawn if player see)
2011-04-29 20:47:02 +02:00
if ( CreatureCellRelocation ( c , resp_cell ))
2008-10-02 16:23:55 -05:00
{
c -> Relocate ( resp_x , resp_y , resp_z , resp_o );
c -> GetMotionMaster () -> Initialize (); // prevent possible problems with default move generators
2011-10-18 10:53:34 -04:00
//CreatureRelocationNotify(c, resp_cell, resp_cell.GetCellCoord());
2010-02-27 15:25:14 +02:00
c -> UpdateObjectVisibility ( false );
2008-10-02 16:23:55 -05:00
return true ;
}
2011-09-13 11:54:55 -04:00
return false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-10-22 10:01:29 -04:00
bool Map :: UnloadGrid ( NGridType & ngrid , bool unloadAll )
2008-10-02 16:23:55 -05:00
{
2011-10-22 10:01:29 -04:00
const uint32 x = ngrid . getX ();
const uint32 y = ngrid . getY ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
{
2011-12-20 17:05:08 -05:00
if ( ! unloadAll )
{
//pets, possessed creatures (must be active), transport passengers
2011-12-22 10:15:17 -05:00
if ( ngrid . GetWorldObjectCountInNGrid < Creature > ())
2011-12-20 17:05:08 -05:00
return false ;
if ( ActiveObjectsNearGrid ( ngrid ))
return false ;
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Unloading grid[%u, %u] for map %u" , x , y , GetId ());
2009-10-17 15:51:44 -07:00
2010-04-07 19:14:10 +02:00
if ( ! unloadAll )
2009-02-25 17:53:05 -06:00
{
// 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-29 11:58:19 -06:00
MoveAllCreaturesInMoveList ();
2009-10-17 15:51:44 -07:00
2009-02-25 17:53:05 -06:00
// move creatures to respawn grids if this is diff.grid or to remove list
2011-10-18 14:31:34 -04:00
ObjectGridEvacuator worker ;
TypeContainerVisitor < ObjectGridEvacuator , GridTypeMapContainer > visitor ( worker );
2011-10-22 10:01:29 -04:00
ngrid . VisitAllGrids ( visitor );
2009-10-17 15:51:44 -07:00
2009-02-25 17:53:05 -06:00
// Finish creature moves, remove and delete all creatures with delayed remove before unload
2009-03-29 11:58:19 -06:00
MoveAllCreaturesInMoveList ();
2009-02-25 17:53:05 -06:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 14:31:34 -04:00
{
ObjectGridCleaner worker ;
TypeContainerVisitor < ObjectGridCleaner , GridTypeMapContainer > visitor ( worker );
2011-10-22 10:01:29 -04:00
ngrid . VisitAllGrids ( visitor );
2011-10-18 14:31:34 -04:00
}
2009-10-17 15:51:44 -07:00
2009-03-29 11:58:19 -06:00
RemoveAllObjectsInRemoveList ();
2009-10-17 15:51:44 -07:00
2011-10-18 14:31:34 -04:00
{
ObjectGridUnloader worker ;
TypeContainerVisitor < ObjectGridUnloader , GridTypeMapContainer > visitor ( worker );
2011-10-22 10:01:29 -04:00
ngrid . VisitAllGrids ( visitor );
2011-10-18 14:31:34 -04:00
}
2009-10-17 15:51:44 -07:00
2010-07-30 20:04:28 +02:00
ASSERT ( i_objectsToRemove . empty ());
2009-10-17 15:51:44 -07:00
2011-10-22 10:01:29 -04:00
delete & ngrid ;
2008-10-02 16:23:55 -05:00
setNGrid ( NULL , x , y );
}
2009-09-27 02:24:25 -07:00
int gx = ( MAX_NUMBER_OF_GRIDS - 1 ) - x ;
int gy = ( MAX_NUMBER_OF_GRIDS - 1 ) - y ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// 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 )
{
2010-04-07 19:14:10 +02:00
if ( GridMaps [ gx ][ gy ])
2009-03-09 18:06:19 -06:00
{
GridMaps [ gx ][ gy ] -> unloadData ();
delete GridMaps [ gx ][ gy ];
}
2008-12-12 11:21:28 -06:00
// x and y are swapped
2009-09-15 20:55:14 -04:00
VMAP :: VMapFactory :: createOrGetVMapManager () -> unloadMap ( GetId (), gx , gy );
2012-04-09 15:11:39 +02:00
MMAP :: MMapFactory :: createOrGetMMapManager () -> unloadMap ( GetId (), gx , gy );
2008-10-02 16:23:55 -05:00
}
else
2011-10-18 10:53:34 -04:00
(( MapInstanced * ) m_parentMap ) -> RemoveGridMapReference ( GridCoord ( gx , gy ));
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
GridMaps [ gx ][ gy ] = NULL ;
}
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Unloading grid[%u, %u] for map %u finished" , x , y , GetId ());
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-08-06 16:14:13 -05:00
void Map :: RemoveAllPlayers ()
{
2010-04-07 19:14:10 +02:00
if ( HavePlayers ())
2009-08-06 16:14:13 -05:00
{
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2009-08-06 16:14:13 -05:00
{
2011-11-07 11:06:39 -06:00
Player * player = itr -> getSource ();
if ( ! player -> IsBeingTeleportedFar ())
2009-08-29 15:29:14 -05:00
{
// this is happening for bg
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map::UnloadAll: player %s is still in map %u during unload, this should not happen!" , player -> GetName (), GetId ());
2011-11-07 11:06:39 -06:00
player -> TeleportTo ( player -> m_homebindMapId , player -> m_homebindX , player -> m_homebindY , player -> m_homebindZ , player -> GetOrientation ());
2009-08-29 15:29:14 -05:00
}
2009-08-06 16:14:13 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
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.
2011-10-07 17:36:23 -04:00
_creaturesToMove . clear ();
2009-10-17 15:51:44 -07:00
2009-11-20 19:11:28 -08:00
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end ();)
2008-10-02 16:23:55 -05:00
{
NGridType & grid ( * i -> getSource ());
++ i ;
2011-10-22 10:01:29 -04:00
UnloadGrid ( grid , true ); // deletes the grid and removes it from the GridRefManager
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2011-01-16 12:23:17 +01:00
// *****************************
2009-03-09 18:06:19 -06:00
// Grid function
2011-01-16 12:23:17 +01:00
// *****************************
2009-03-09 18:06:19 -06:00
GridMap :: GridMap ()
2008-10-02 16:23:55 -05:00
{
2012-02-14 12:46:26 -05:00
_flags = 0 ;
2009-03-09 18:06:19 -06:00
// Area data
2012-02-14 12:46:26 -05:00
_gridArea = 0 ;
_areaMap = NULL ;
2009-03-09 18:06:19 -06:00
// Height level data
2012-02-14 12:46:26 -05:00
_gridHeight = INVALID_HEIGHT ;
_gridGetHeight = & GridMap :: getHeightFromFlat ;
2009-03-09 18:06:19 -06:00
m_V9 = NULL ;
m_V8 = NULL ;
// Liquid data
2012-02-14 12:46:26 -05:00
_liquidType = 0 ;
_liquidOffX = 0 ;
_liquidOffY = 0 ;
_liquidWidth = 0 ;
_liquidHeight = 0 ;
_liquidLevel = INVALID_HEIGHT ;
2012-03-07 13:09:35 +01:00
_liquidEntry = NULL ;
_liquidFlags = NULL ;
2012-02-14 12:46:26 -05:00
_liquidMap = NULL ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
GridMap ::~ GridMap ()
{
unloadData ();
}
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
bool GridMap :: loadData ( char * filename )
{
// Unload old data if exist
unloadData ();
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
map_fileheader header ;
// Not return error if file not found
2011-09-15 14:08:17 +02:00
FILE * in = fopen ( filename , "rb" );
2009-03-09 18:06:19 -06:00
if ( ! in )
return true ;
2010-08-31 10:23:22 +02:00
2011-04-29 20:47:02 +02:00
if ( fread ( & header , sizeof ( header ), 1 , in ) != 1 )
2010-08-31 10:23:22 +02:00
{
fclose ( in );
return false ;
}
2011-07-27 11:00:39 +02:00
if ( header . mapMagic == MapMagic . asUInt && header . versionMagic == MapVersionMagic . asUInt )
2009-03-09 18:06:19 -06:00
{
// loadup area data
if ( header . areaMapOffset && ! loadAreaData ( in , header . areaMapOffset , header . areaMapSize ))
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Error loading map area data \n " );
2009-03-09 18:06:19 -06:00
fclose ( in );
return false ;
}
// loadup height data
if ( header . heightMapOffset && ! loadHeihgtData ( in , header . heightMapOffset , header . heightMapSize ))
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Error loading map height data \n " );
2009-03-09 18:06:19 -06:00
fclose ( in );
return false ;
}
// loadup liquid data
if ( header . liquidMapOffset && ! loadLiquidData ( in , header . liquidMapOffset , header . liquidMapSize ))
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Error loading map liquids data \n " );
2009-03-09 18:06:19 -06:00
fclose ( in );
return false ;
}
fclose ( in );
return true ;
}
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Map file '%s' is from an incompatible clientversion. Please recreate using the mapextractor." , filename );
2009-03-09 18:06:19 -06:00
fclose ( in );
return false ;
}
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
void GridMap :: unloadData ()
{
2012-02-14 12:46:26 -05:00
delete [] _areaMap ;
2010-06-21 23:20:58 +02:00
delete [] m_V9 ;
delete [] m_V8 ;
2012-03-07 13:09:35 +01:00
delete [] _liquidEntry ;
delete [] _liquidFlags ;
2012-02-14 12:46:26 -05:00
delete [] _liquidMap ;
_areaMap = NULL ;
2009-03-09 18:06:19 -06:00
m_V9 = NULL ;
m_V8 = NULL ;
2012-03-07 13:09:35 +01:00
_liquidEntry = NULL ;
_liquidFlags = NULL ;
2012-02-14 12:46:26 -05:00
_liquidMap = NULL ;
_gridGetHeight = & GridMap :: getHeightFromFlat ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
bool GridMap :: loadAreaData ( FILE * in , uint32 offset , uint32 /*size*/ )
2009-03-09 18:06:19 -06:00
{
map_areaHeader header ;
fseek ( in , offset , SEEK_SET );
2010-08-31 10:23:22 +02:00
2011-07-27 11:00:39 +02:00
if ( fread ( & header , sizeof ( header ), 1 , in ) != 1 || header . fourcc != MapAreaMagic . asUInt )
2009-03-09 18:06:19 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
_gridArea = header . gridArea ;
2009-03-12 14:49:12 -06:00
if ( ! ( header . flags & MAP_AREA_NO_AREA ))
2009-03-09 18:06:19 -06:00
{
2012-02-14 12:46:26 -05:00
_areaMap = new uint16 [ 16 * 16 ];
if ( fread ( _areaMap , sizeof ( uint16 ), 16 * 16 , in ) != 16 * 16 )
2010-08-31 10:23:22 +02:00
return false ;
2009-03-09 18:06:19 -06:00
}
return true ;
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
bool GridMap :: loadHeihgtData ( FILE * in , uint32 offset , uint32 /*size*/ )
2009-03-09 18:06:19 -06:00
{
map_heightHeader header ;
fseek ( in , offset , SEEK_SET );
2010-09-12 01:40:27 +02:00
2011-07-27 11:00:39 +02:00
if ( fread ( & header , sizeof ( header ), 1 , in ) != 1 || header . fourcc != MapHeightMagic . asUInt )
2009-03-09 18:06:19 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
_gridHeight = header . gridHeight ;
2009-07-31 11:25:28 +08:00
if ( ! ( header . flags & MAP_HEIGHT_NO_HEIGHT ))
2009-03-09 18:06:19 -06:00
{
2009-03-12 14:49:12 -06:00
if (( header . flags & MAP_HEIGHT_AS_INT16 ))
2009-02-17 15:42:04 -06:00
{
2009-03-09 18:06:19 -06:00
m_uint16_V9 = new uint16 [ 129 * 129 ];
m_uint16_V8 = new uint16 [ 128 * 128 ];
2010-08-31 10:23:22 +02:00
if ( fread ( m_uint16_V9 , sizeof ( uint16 ), 129 * 129 , in ) != 129 * 129 ||
fread ( m_uint16_V8 , sizeof ( uint16 ), 128 * 128 , in ) != 128 * 128 )
return false ;
2012-02-14 12:46:26 -05:00
_gridIntHeightMultiplier = ( header . gridMaxHeight - header . gridHeight ) / 65535 ;
_gridGetHeight = & GridMap :: getHeightFromUint16 ;
2009-03-09 18:06:19 -06:00
}
2009-03-12 14:49:12 -06:00
else if (( header . flags & MAP_HEIGHT_AS_INT8 ))
2009-03-09 18:06:19 -06:00
{
m_uint8_V9 = new uint8 [ 129 * 129 ];
m_uint8_V8 = new uint8 [ 128 * 128 ];
2010-08-31 10:23:22 +02:00
if ( fread ( m_uint8_V9 , sizeof ( uint8 ), 129 * 129 , in ) != 129 * 129 ||
fread ( m_uint8_V8 , sizeof ( uint8 ), 128 * 128 , in ) != 128 * 128 )
return false ;
2012-02-14 12:46:26 -05:00
_gridIntHeightMultiplier = ( header . gridMaxHeight - header . gridHeight ) / 255 ;
_gridGetHeight = & GridMap :: getHeightFromUint8 ;
2009-02-17 15:42:04 -06:00
}
else
{
2009-03-09 18:06:19 -06:00
m_V9 = new float [ 129 * 129 ];
m_V8 = new float [ 128 * 128 ];
2010-08-31 10:23:22 +02:00
if ( fread ( m_V9 , sizeof ( float ), 129 * 129 , in ) != 129 * 129 ||
fread ( m_V8 , sizeof ( float ), 128 * 128 , in ) != 128 * 128 )
return false ;
2012-02-14 12:46:26 -05:00
_gridGetHeight = & GridMap :: getHeightFromFloat ;
2009-02-17 15:42:04 -06:00
}
2009-03-09 18:06:19 -06:00
}
else
2012-02-14 12:46:26 -05:00
_gridGetHeight = & GridMap :: getHeightFromFlat ;
2009-03-09 18:06:19 -06:00
return true ;
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
bool GridMap :: loadLiquidData ( FILE * in , uint32 offset , uint32 /*size*/ )
2009-03-09 18:06:19 -06:00
{
map_liquidHeader header ;
fseek ( in , offset , SEEK_SET );
2010-09-12 01:40:27 +02:00
2011-07-27 11:00:39 +02:00
if ( fread ( & header , sizeof ( header ), 1 , in ) != 1 || header . fourcc != MapLiquidMagic . asUInt )
2009-03-09 18:06:19 -06:00
return false ;
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
_liquidType = header . liquidType ;
_liquidOffX = header . offsetX ;
_liquidOffY = header . offsetY ;
_liquidWidth = header . width ;
2012-03-07 13:09:35 +01:00
_liquidHeight = header . height ;
2012-02-14 12:46:26 -05:00
_liquidLevel = header . liquidLevel ;
2009-10-17 15:51:44 -07:00
2009-07-31 11:25:28 +08:00
if ( ! ( header . flags & MAP_LIQUID_NO_TYPE ))
2009-03-09 18:06:19 -06:00
{
2012-03-07 13:09:35 +01:00
_liquidEntry = new uint16 [ 16 * 16 ];
if ( fread ( _liquidEntry , sizeof ( uint16 ), 16 * 16 , in ) != 16 * 16 )
return false ;
_liquidFlags = new uint8 [ 16 * 16 ];
if ( fread ( _liquidFlags , sizeof ( uint8 ), 16 * 16 , in ) != 16 * 16 )
2010-08-31 10:23:22 +02:00
return false ;
2009-03-09 18:06:19 -06:00
}
2009-07-31 11:25:28 +08:00
if ( ! ( header . flags & MAP_LIQUID_NO_HEIGHT ))
2009-03-09 18:06:19 -06:00
{
2012-03-07 13:09:35 +01:00
_liquidMap = new float [ _liquidWidth * _liquidHeight ];
2012-02-14 12:46:26 -05:00
if ( fread ( _liquidMap , sizeof ( float ), _liquidWidth * _liquidHeight , in ) != _liquidWidth * _liquidHeight )
2010-08-31 10:23:22 +02:00
return false ;
2009-03-09 18:06:19 -06:00
}
return true ;
}
2009-10-17 15:51:44 -07:00
2012-06-21 21:05:45 +03:00
uint16 GridMap :: getArea ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
2012-02-14 12:46:26 -05:00
if ( ! _areaMap )
return _gridArea ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = 16 * ( 32 - x / SIZE_OF_GRIDS );
y = 16 * ( 32 - y / SIZE_OF_GRIDS );
int lx = ( int ) x & 15 ;
int ly = ( int ) y & 15 ;
2012-02-14 12:46:26 -05:00
return _areaMap [ lx * 16 + ly ];
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
float GridMap :: getHeightFromFlat ( float /*x*/ , float /*y*/ ) const
2009-03-09 18:06:19 -06:00
{
2012-02-14 12:46:26 -05:00
return _gridHeight ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
float GridMap :: getHeightFromFloat ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
if ( ! m_V8 || ! m_V9 )
2012-02-14 12:46:26 -05:00
return _gridHeight ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int x_int = ( int ) x ;
int y_int = ( int ) y ;
x -= x_int ;
y -= y_int ;
x_int &= ( MAP_RESOLUTION - 1 );
y_int &= ( MAP_RESOLUTION - 1 );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Height stored as: h5 - its v8 grid, h1-h4 - its v9 grid
// +--------------> X
// | h1-------h2 Coordinates is:
2011-04-29 20:47:02 +02:00
// | | \ 1 / | h1 0, 0
// | | \ / | h2 0, 1
// | | 2 h5 3 | h3 1, 0
// | | / \ | h4 1, 1
// | | / 4 \ | h5 1/2, 1/2
2009-03-09 18:06:19 -06:00
// | h3-------h4
// V Y
// For find height need
// 1 - detect triangle
// 2 - solve linear equation from triangle points
// Calculate coefficients for solve h = a*x + b*y + c
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
float a , b , c ;
2009-03-09 18:06:19 -06:00
// Select triangle:
if ( x + y < 1 )
{
if ( x > y )
{
// 1 triangle (h1, h2, h5 points)
2010-04-07 22:59:46 +02:00
float h1 = m_V9 [( x_int ) * 129 + y_int ];
2009-03-09 18:06:19 -06:00
float h2 = m_V9 [( x_int + 1 ) * 129 + y_int ];
float h5 = 2 * m_V8 [ x_int * 128 + y_int ];
a = h2 - h1 ;
b = h5 - h1 - h2 ;
c = h1 ;
}
else
{
// 2 triangle (h1, h3, h5 points)
float h1 = m_V9 [ x_int * 129 + y_int ];
float h3 = m_V9 [ x_int * 129 + y_int + 1 ];
float h5 = 2 * m_V8 [ x_int * 128 + y_int ];
a = h5 - h1 - h3 ;
b = h3 - h1 ;
c = h1 ;
}
}
else
{
if ( x > y )
{
// 3 triangle (h2, h4, h5 points)
float h2 = m_V9 [( x_int + 1 ) * 129 + y_int ];
float h4 = m_V9 [( x_int + 1 ) * 129 + y_int + 1 ];
float h5 = 2 * m_V8 [ x_int * 128 + y_int ];
a = h2 + h4 - h5 ;
b = h4 - h2 ;
c = h5 - h4 ;
}
else
{
// 4 triangle (h3, h4, h5 points)
2010-04-07 22:59:46 +02:00
float h3 = m_V9 [( x_int ) * 129 + y_int + 1 ];
2009-03-09 18:06:19 -06:00
float h4 = m_V9 [( x_int + 1 ) * 129 + y_int + 1 ];
float h5 = 2 * m_V8 [ x_int * 128 + y_int ];
a = h4 - h3 ;
b = h3 + h4 - h5 ;
c = h5 - h4 ;
}
}
// Calculate height
return a * x + b * y + c ;
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
float GridMap :: getHeightFromUint8 ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
if ( ! m_uint8_V8 || ! m_uint8_V9 )
2012-02-14 12:46:26 -05:00
return _gridHeight ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int x_int = ( int ) x ;
int y_int = ( int ) y ;
x -= x_int ;
y -= y_int ;
x_int &= ( MAP_RESOLUTION - 1 );
y_int &= ( MAP_RESOLUTION - 1 );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int32 a , b , c ;
uint8 * V9_h1_ptr = & m_uint8_V9 [ x_int * 128 + x_int + y_int ];
if ( x + y < 1 )
{
if ( x > y )
{
// 1 triangle (h1, h2, h5 points)
int32 h1 = V9_h1_ptr [ 0 ];
int32 h2 = V9_h1_ptr [ 129 ];
int32 h5 = 2 * m_uint8_V8 [ x_int * 128 + y_int ];
a = h2 - h1 ;
b = h5 - h1 - h2 ;
c = h1 ;
}
else
{
// 2 triangle (h1, h3, h5 points)
int32 h1 = V9_h1_ptr [ 0 ];
int32 h3 = V9_h1_ptr [ 1 ];
int32 h5 = 2 * m_uint8_V8 [ x_int * 128 + y_int ];
a = h5 - h1 - h3 ;
b = h3 - h1 ;
c = h1 ;
}
}
else
{
if ( x > y )
{
// 3 triangle (h2, h4, h5 points)
int32 h2 = V9_h1_ptr [ 129 ];
int32 h4 = V9_h1_ptr [ 130 ];
int32 h5 = 2 * m_uint8_V8 [ x_int * 128 + y_int ];
a = h2 + h4 - h5 ;
b = h4 - h2 ;
c = h5 - h4 ;
}
else
{
// 4 triangle (h3, h4, h5 points)
int32 h3 = V9_h1_ptr [ 1 ];
int32 h4 = V9_h1_ptr [ 130 ];
int32 h5 = 2 * m_uint8_V8 [ x_int * 128 + y_int ];
a = h4 - h3 ;
b = h3 + h4 - h5 ;
c = h5 - h4 ;
}
}
// Calculate height
2012-02-14 12:46:26 -05:00
return ( float )(( a * x ) + ( b * y ) + c ) * _gridIntHeightMultiplier + _gridHeight ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
float GridMap :: getHeightFromUint16 ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
if ( ! m_uint16_V8 || ! m_uint16_V9 )
2012-02-14 12:46:26 -05:00
return _gridHeight ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int x_int = ( int ) x ;
int y_int = ( int ) y ;
x -= x_int ;
y -= y_int ;
x_int &= ( MAP_RESOLUTION - 1 );
y_int &= ( MAP_RESOLUTION - 1 );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int32 a , b , c ;
uint16 * V9_h1_ptr = & m_uint16_V9 [ x_int * 128 + x_int + y_int ];
if ( x + y < 1 )
{
if ( x > y )
{
// 1 triangle (h1, h2, h5 points)
int32 h1 = V9_h1_ptr [ 0 ];
int32 h2 = V9_h1_ptr [ 129 ];
int32 h5 = 2 * m_uint16_V8 [ x_int * 128 + y_int ];
a = h2 - h1 ;
b = h5 - h1 - h2 ;
c = h1 ;
}
else
{
// 2 triangle (h1, h3, h5 points)
int32 h1 = V9_h1_ptr [ 0 ];
int32 h3 = V9_h1_ptr [ 1 ];
int32 h5 = 2 * m_uint16_V8 [ x_int * 128 + y_int ];
a = h5 - h1 - h3 ;
b = h3 - h1 ;
c = h1 ;
}
}
else
{
if ( x > y )
{
// 3 triangle (h2, h4, h5 points)
int32 h2 = V9_h1_ptr [ 129 ];
int32 h4 = V9_h1_ptr [ 130 ];
int32 h5 = 2 * m_uint16_V8 [ x_int * 128 + y_int ];
a = h2 + h4 - h5 ;
b = h4 - h2 ;
c = h5 - h4 ;
}
else
{
// 4 triangle (h3, h4, h5 points)
int32 h3 = V9_h1_ptr [ 1 ];
int32 h4 = V9_h1_ptr [ 130 ];
int32 h5 = 2 * m_uint16_V8 [ x_int * 128 + y_int ];
a = h4 - h3 ;
b = h3 + h4 - h5 ;
c = h5 - h4 ;
}
}
// Calculate height
2012-02-14 12:46:26 -05:00
return ( float )(( a * x ) + ( b * y ) + c ) * _gridIntHeightMultiplier + _gridHeight ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2012-06-21 21:05:45 +03:00
float GridMap :: getLiquidLevel ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
2012-02-14 12:46:26 -05:00
if ( ! _liquidMap )
return _liquidLevel ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
int cx_int = (( int ) x & ( MAP_RESOLUTION - 1 )) - _liquidOffY ;
int cy_int = (( int ) y & ( MAP_RESOLUTION - 1 )) - _liquidOffX ;
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
if ( cx_int < 0 || cx_int >= _liquidHeight )
2009-03-09 18:06:19 -06:00
return INVALID_HEIGHT ;
2012-02-14 12:46:26 -05:00
if ( cy_int < 0 || cy_int >= _liquidWidth )
2009-03-09 18:06:19 -06:00
return INVALID_HEIGHT ;
2009-10-17 15:51:44 -07:00
2012-02-14 12:46:26 -05:00
return _liquidMap [ cx_int * _liquidWidth + cy_int ];
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2012-03-07 13:09:35 +01:00
// Why does this return LIQUID data?
2012-06-21 21:05:45 +03:00
uint8 GridMap :: getTerrainType ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
2012-03-07 13:09:35 +01:00
if ( ! _liquidFlags )
2010-08-30 23:07:14 +02:00
return 0 ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
x = 16 * ( 32 - x / SIZE_OF_GRIDS );
y = 16 * ( 32 - y / SIZE_OF_GRIDS );
int lx = ( int ) x & 15 ;
int ly = ( int ) y & 15 ;
2012-03-07 13:09:35 +01:00
return _liquidFlags [ lx * 16 + ly ];
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Get water state on map
2011-09-15 14:08:17 +02:00
inline ZLiquidStatus GridMap :: getLiquidStatus ( float x , float y , float z , uint8 ReqLiquidType , LiquidData * data )
2009-03-09 18:06:19 -06:00
{
// Check water type (if no water return)
2012-03-07 13:09:35 +01:00
if ( ! _liquidType && ! _liquidFlags )
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_NO_WATER ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Get cell
float cx = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
float cy = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
int x_int = ( int ) cx & ( MAP_RESOLUTION - 1 );
int y_int = ( int ) cy & ( MAP_RESOLUTION - 1 );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Check water type in cell
2012-03-07 13:09:35 +01:00
int idx = ( x_int >> 3 ) * 16 + ( y_int >> 3 );
uint8 type = _liquidFlags ? _liquidFlags [ idx ] : _liquidType ;
uint32 entry = 0 ;
if ( _liquidEntry )
{
if ( LiquidTypeEntry const * liquidEntry = sLiquidTypeStore . LookupEntry ( _liquidEntry [ idx ]))
{
entry = liquidEntry -> Id ;
type &= MAP_LIQUID_TYPE_DARK_WATER ;
uint32 liqTypeIdx = liquidEntry -> Type ;
if ( entry < 21 )
{
if ( AreaTableEntry const * area = GetAreaEntryByAreaFlagAndMap ( getArea ( x , y ), MAPID_INVALID ))
{
uint32 overrideLiquid = area -> LiquidTypeOverride [ liquidEntry -> Type ];
if ( ! overrideLiquid && area -> zone )
2012-03-24 15:37:54 +01:00
{
2012-03-24 15:54:45 +01:00
area = GetAreaEntryByAreaID ( area -> zone );
2012-03-24 15:50:18 +01:00
if ( area )
2012-03-07 13:09:35 +01:00
overrideLiquid = area -> LiquidTypeOverride [ liquidEntry -> Type ];
2012-03-24 15:37:54 +01:00
}
2012-03-07 13:09:35 +01:00
if ( LiquidTypeEntry const * liq = sLiquidTypeStore . LookupEntry ( overrideLiquid ))
{
entry = overrideLiquid ;
liqTypeIdx = liq -> Type ;
}
}
}
type |= 1 << liqTypeIdx ;
}
}
2009-03-09 18:06:19 -06:00
if ( type == 0 )
return LIQUID_MAP_NO_WATER ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Check req liquid type mask
if ( ReqLiquidType && ! ( ReqLiquidType & type ))
return LIQUID_MAP_NO_WATER ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Check water level:
// Check water height map
2012-02-14 12:46:26 -05:00
int lx_int = x_int - _liquidOffY ;
int ly_int = y_int - _liquidOffX ;
if ( lx_int < 0 || lx_int >= _liquidHeight )
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_NO_WATER ;
2012-02-14 12:46:26 -05:00
if ( ly_int < 0 || ly_int >= _liquidWidth )
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_NO_WATER ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Get water level
2012-02-14 12:46:26 -05:00
float liquid_level = _liquidMap ? _liquidMap [ lx_int * _liquidWidth + ly_int ] : _liquidLevel ;
2009-03-09 18:06:19 -06:00
// Get ground level (sub 0.2 for fix some errors)
float ground_level = getHeight ( x , y );
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// Check water level and ground level
if ( liquid_level < ground_level || z < ground_level - 2 )
return LIQUID_MAP_NO_WATER ;
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// All ok in water -> store data
if ( data )
{
2012-03-07 13:09:35 +01:00
data -> entry = entry ;
data -> type_flags = type ;
2009-03-09 18:06:19 -06:00
data -> level = liquid_level ;
data -> depth_level = ground_level ;
}
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// For speed check as int values
2012-03-07 13:09:35 +01:00
float delta = liquid_level - z ;
2009-10-17 15:51:44 -07:00
2012-03-07 13:09:35 +01:00
if ( delta > 2.0f ) // Under water
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_UNDER_WATER ;
2012-03-07 13:09:35 +01:00
if ( delta > 0.0f ) // In water
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_IN_WATER ;
2012-03-07 13:09:35 +01:00
if ( delta > - 0.1f ) // Walk on water
2009-03-09 18:06:19 -06:00
return LIQUID_MAP_WATER_WALK ;
// Above water
return LIQUID_MAP_ABOVE_WATER ;
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
inline GridMap * Map :: GetGrid ( float x , float y )
2009-03-09 18:06:19 -06: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-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
// ensure GridMap is loaded
2011-10-18 10:53:34 -04:00
EnsureGridCreated ( GridCoord ( 63 - gx , 63 - gy ));
2009-10-17 15:51:44 -07:00
2009-03-09 18:06:19 -06:00
return GridMaps [ gx ][ gy ];
}
2009-10-17 15:51:44 -07:00
2012-02-22 09:15:57 +01:00
float Map :: GetWaterOrGroundLevel ( float x , float y , float z , float * ground /*= NULL*/ , bool /*swim = false*/ ) const
2012-01-14 15:34:41 +01:00
{
if ( const_cast < Map *> ( this ) -> GetGrid ( x , y ))
{
// we need ground level (including grid height version) for proper return water level in point
2012-02-03 16:03:52 -05:00
float ground_z = GetHeight ( PHASEMASK_NORMAL , x , y , z , true , 50.0f );
2012-01-14 15:34:41 +01:00
if ( ground )
* ground = ground_z ;
LiquidData liquid_status ;
ZLiquidStatus res = getLiquidStatus ( x , y , ground_z , MAP_ALL_LIQUIDS , & liquid_status );
2012-02-17 14:25:08 -05:00
return res ? liquid_status . level : ground_z ;
2012-01-14 15:34:41 +01:00
}
return VMAP_INVALID_HEIGHT_VALUE ;
}
2011-12-13 18:19:25 +01:00
float Map :: GetHeight ( float x , float y , float z , bool checkVMap /*= true*/ , float maxSearchDist /*= DEFAULT_HEIGHT_SEARCH*/ ) const
2009-03-09 18:06:19 -06:00
{
// find raw .map surface under Z coordinates
2011-12-13 18:19:25 +01:00
float mapHeight = VMAP_INVALID_HEIGHT_VALUE ;
2011-09-15 14:08:17 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2009-03-09 18:06:19 -06:00
{
2011-12-13 18:19:25 +01:00
float gridHeight = gmap -> getHeight ( x , y );
2008-10-02 16:23:55 -05:00
// look from a bit higher pos to find the floor, ignore under surface case
2011-12-13 18:19:25 +01:00
if ( z + 2.0f > gridHeight )
mapHeight = gridHeight ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-13 18:19:25 +01:00
float vmapHeight = VMAP_INVALID_HEIGHT_VALUE ;
if ( checkVMap )
2008-10-02 16:23:55 -05:00
{
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
2010-04-07 19:14:10 +02:00
if ( vmgr -> isHeightCalcEnabled ())
2011-12-13 18:19:25 +01:00
vmapHeight = vmgr -> getHeight ( GetId (), x , y , z + 2.0f , maxSearchDist ); // look from a bit higher pos to find the floor
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// mapHeight set for any above raw ground Z or <= INVALID_HEIGHT
// vmapheight set for any under Z value or <= INVALID_HEIGHT
2010-04-07 22:59:46 +02:00
if ( vmapHeight > INVALID_HEIGHT )
2008-10-02 16:23:55 -05:00
{
2010-04-07 22:59:46 +02:00
if ( mapHeight > INVALID_HEIGHT )
2008-10-02 16:23:55 -05:00
{
// we have mapheight and vmapheight and must select more appropriate
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// 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
2010-04-07 22:59:46 +02:00
if ( z < mapHeight || vmapHeight > mapHeight || fabs ( mapHeight - z ) > fabs ( vmapHeight - z ))
2008-10-02 16:23:55 -05:00
return vmapHeight ;
else
return mapHeight ; // better use .map surface height
}
else
return vmapHeight ; // we have only vmapHeight (if have)
}
2011-12-13 18:19:25 +01:00
return mapHeight ; // explicitly use map data
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-21 23:08:54 +02:00
inline bool IsOutdoorWMO ( uint32 mogpFlags , int32 /*adtId*/ , int32 /*rootId*/ , int32 /*groupId*/ , WMOAreaTableEntry const * wmoEntry , AreaTableEntry const * atEntry )
2009-01-16 19:05:30 -06:00
{
2010-06-05 00:59:25 +02:00
bool outdoor = true ;
2010-01-06 12:15:02 +02:00
2011-06-12 02:30:32 +02:00
if ( wmoEntry && atEntry )
2010-06-05 00:59:25 +02:00
{
2011-06-12 02:30:32 +02:00
if ( atEntry -> flags & AREA_FLAG_OUTSIDE )
2010-06-05 00:59:25 +02:00
return true ;
2011-06-12 02:30:32 +02:00
if ( atEntry -> flags & AREA_FLAG_INSIDE )
2010-06-05 00:59:25 +02:00
return false ;
}
2010-01-06 12:15:02 +02:00
2010-06-05 00:59:25 +02:00
outdoor = mogpFlags & 0x8 ;
2010-01-06 12:15:02 +02:00
2011-06-12 02:30:32 +02:00
if ( wmoEntry )
2010-06-05 00:59:25 +02:00
{
2011-06-12 02:30:32 +02:00
if ( wmoEntry -> Flags & 4 )
2010-06-05 00:59:25 +02:00
return true ;
2011-06-12 02:30:32 +02:00
if (( wmoEntry -> Flags & 2 ) != 0 )
2010-06-05 00:59:25 +02:00
outdoor = false ;
}
return outdoor ;
2009-01-16 19:05:30 -06:00
}
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
bool Map :: IsOutdoors ( float x , float y , float z ) const
2008-10-02 16:23:55 -05:00
{
2010-06-05 00:59:25 +02:00
uint32 mogpFlags ;
int32 adtId , rootId , groupId ;
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
// no wmo found? -> outside by default
2011-06-12 02:30:32 +02:00
if ( ! GetAreaInfo ( x , y , z , mogpFlags , adtId , rootId , groupId ))
2010-06-05 00:59:25 +02:00
return true ;
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
AreaTableEntry const * atEntry = 0 ;
WMOAreaTableEntry const * wmoEntry = GetWMOAreaTableEntryByTripple ( rootId , adtId , groupId );
2011-06-12 02:30:32 +02:00
if ( wmoEntry )
2010-06-05 00:59:25 +02:00
{
2012-08-03 14:20:18 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Got WMOAreaTableEntry! flag %u, areaid %u" , wmoEntry -> Flags , wmoEntry -> areaId );
2010-06-05 00:59:25 +02:00
atEntry = GetAreaEntryByAreaID ( wmoEntry -> areaId );
}
return IsOutdoorWMO ( mogpFlags , adtId , rootId , groupId , wmoEntry , atEntry );
}
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
bool Map :: GetAreaInfo ( float x , float y , float z , uint32 & flags , int32 & adtId , int32 & rootId , int32 & groupId ) const
{
float vmap_z = z ;
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
if ( vmgr -> getAreaInfo ( GetId (), x , y , vmap_z , flags , adtId , rootId , groupId ))
{
// check if there's terrain between player height and object height
2011-09-15 14:08:17 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2010-06-05 00:59:25 +02:00
{
2011-04-29 20:47:02 +02:00
float _mapheight = gmap -> getHeight ( x , y );
2010-06-05 00:59:25 +02:00
// z + 2.0f condition taken from GetHeight(), not sure if it's such a great choice...
2011-06-12 02:30:32 +02:00
if ( z + 2.0f > _mapheight && _mapheight > vmap_z )
2010-06-05 00:59:25 +02:00
return false ;
}
return true ;
}
return false ;
}
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
uint16 Map :: GetAreaFlag ( float x , float y , float z , bool * isOutdoors ) const
{
uint32 mogpFlags ;
int32 adtId , rootId , groupId ;
WMOAreaTableEntry const * wmoEntry = 0 ;
AreaTableEntry const * atEntry = 0 ;
bool haveAreaInfo = false ;
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
if ( GetAreaInfo ( x , y , z , mogpFlags , adtId , rootId , groupId ))
{
haveAreaInfo = true ;
2010-08-21 21:54:41 +02:00
wmoEntry = GetWMOAreaTableEntryByTripple ( rootId , adtId , groupId );
if ( wmoEntry )
2010-06-05 00:59:25 +02:00
atEntry = GetAreaEntryByAreaID ( wmoEntry -> areaId );
}
uint16 areaflag ;
if ( atEntry )
areaflag = atEntry -> exploreFlag ;
else
{
2011-09-15 14:08:17 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2010-06-05 00:59:25 +02:00
areaflag = gmap -> getArea ( x , y );
// this used while not all *.map files generated (instances)
else
areaflag = GetAreaFlagByMapId ( i_mapEntry -> MapID );
2009-01-02 11:03:43 -06:00
}
2009-10-17 15:51:44 -07:00
2010-06-05 00:59:25 +02:00
if ( isOutdoors )
{
if ( haveAreaInfo )
* isOutdoors = IsOutdoorWMO ( mogpFlags , adtId , rootId , groupId , wmoEntry , atEntry );
else
* isOutdoors = true ;
}
2009-01-02 11:03:43 -06:00
return areaflag ;
2010-06-05 00:59:25 +02:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
uint8 Map :: GetTerrainType ( float x , float y ) const
2008-10-02 16:23:55 -05:00
{
2011-09-15 14:08:17 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2009-03-09 18:06:19 -06:00
return gmap -> getTerrainType ( x , y );
2008-10-02 16:23:55 -05:00
else
return 0 ;
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
ZLiquidStatus Map :: getLiquidStatus ( float x , float y , float z , uint8 ReqLiquidType , LiquidData * data ) const
2008-10-02 16:23:55 -05:00
{
2010-06-05 00:59:25 +02:00
ZLiquidStatus result = LIQUID_MAP_NO_WATER ;
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
2012-03-07 13:09:35 +01:00
float liquid_level = INVALID_HEIGHT ;
float ground_level = INVALID_HEIGHT ;
uint32 liquid_type = 0 ;
2010-06-05 00:59:25 +02:00
if ( vmgr -> GetLiquidLevel ( GetId (), x , y , z , ReqLiquidType , liquid_level , ground_level , liquid_type ))
{
2011-02-20 20:16:34 +01:00
sLog -> outDebug ( LOG_FILTER_MAPS , "getLiquidStatus(): vmap liquid level: %f ground: %f type: %u" , liquid_level , ground_level , liquid_type );
2010-06-05 00:59:25 +02:00
// Check water level and ground level
if ( liquid_level > ground_level && z > ground_level - 2 )
{
// All ok in water -> store data
if ( data )
{
2012-03-07 13:09:35 +01:00
// hardcoded in client like this
if ( GetId () == 530 && liquid_type == 2 )
liquid_type = 15 ;
uint32 liquidFlagType = 0 ;
if ( LiquidTypeEntry const * liq = sLiquidTypeStore . LookupEntry ( liquid_type ))
liquidFlagType = liq -> Type ;
if ( liquid_type && liquid_type < 21 )
{
if ( AreaTableEntry const * area = GetAreaEntryByAreaFlagAndMap ( GetAreaFlag ( x , y , z ), GetId ()))
{
uint32 overrideLiquid = area -> LiquidTypeOverride [ liquidFlagType ];
if ( ! overrideLiquid && area -> zone )
2012-03-24 15:37:54 +01:00
{
2012-03-24 15:54:45 +01:00
area = GetAreaEntryByAreaID ( area -> zone );
2012-03-24 15:50:18 +01:00
if ( area )
2012-03-07 13:09:35 +01:00
overrideLiquid = area -> LiquidTypeOverride [ liquidFlagType ];
2012-03-24 15:37:54 +01:00
}
2012-03-07 13:09:35 +01:00
if ( LiquidTypeEntry const * liq = sLiquidTypeStore . LookupEntry ( overrideLiquid ))
{
liquid_type = overrideLiquid ;
liquidFlagType = liq -> Type ;
}
}
}
2010-06-05 00:59:25 +02:00
data -> level = liquid_level ;
data -> depth_level = ground_level ;
2012-03-07 13:09:35 +01:00
data -> entry = liquid_type ;
data -> type_flags = 1 << liquidFlagType ;
2010-06-05 00:59:25 +02:00
}
2012-03-07 13:09:35 +01:00
float delta = liquid_level - z ;
2010-06-05 00:59:25 +02:00
// Get position delta
2012-03-07 13:09:35 +01:00
if ( delta > 2.0f ) // Under water
2010-06-05 00:59:25 +02:00
return LIQUID_MAP_UNDER_WATER ;
2012-03-07 13:09:35 +01:00
if ( delta > 0.0f ) // In water
2010-06-05 00:59:25 +02:00
return LIQUID_MAP_IN_WATER ;
2012-03-07 13:09:35 +01:00
if ( delta > - 0.1f ) // Walk on water
2010-06-05 00:59:25 +02:00
return LIQUID_MAP_WATER_WALK ;
result = LIQUID_MAP_ABOVE_WATER ;
}
}
2011-06-12 02:30:32 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2010-06-05 00:59:25 +02:00
{
LiquidData map_data ;
ZLiquidStatus map_result = gmap -> getLiquidStatus ( x , y , z , ReqLiquidType , & map_data );
// Not override LIQUID_MAP_ABOVE_WATER with LIQUID_MAP_NO_WATER:
if ( map_result != LIQUID_MAP_NO_WATER && ( map_data . level > ground_level ))
{
if ( data )
2012-03-07 13:09:35 +01:00
{
// hardcoded in client like this
if ( GetId () == 530 && map_data . entry == 2 )
map_data . entry = 15 ;
2010-06-05 00:59:25 +02:00
* data = map_data ;
2012-03-07 13:09:35 +01:00
}
2010-06-05 00:59:25 +02:00
return map_result ;
}
}
return result ;
2009-03-09 18:06:19 -06:00
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
float Map :: GetWaterLevel ( float x , float y ) const
2009-03-09 18:06:19 -06:00
{
2010-04-07 19:14:10 +02:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2009-03-09 18:06:19 -06:00
return gmap -> getLiquidLevel ( x , y );
2008-10-02 16:23:55 -05:00
else
return 0 ;
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
uint32 Map :: GetAreaIdByAreaFlag ( uint16 areaflag , uint32 map_id )
2008-10-02 16:23:55 -05:00
{
2011-09-15 14:08:17 +02:00
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
if ( entry )
return entry -> ID ;
else
return 0 ;
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
uint32 Map :: GetZoneIdByAreaFlag ( uint16 areaflag , uint32 map_id )
2008-10-02 16:23:55 -05:00
{
2011-09-15 14:08:17 +02:00
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
if ( entry )
return ( entry -> zone != 0 ) ? entry -> zone : entry -> ID ;
2008-10-02 16:23:55 -05:00
else
return 0 ;
}
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02:00
void Map :: GetZoneAndAreaIdByAreaFlag ( uint32 & zoneid , uint32 & areaid , uint16 areaflag , uint32 map_id )
2009-03-11 16:17:37 -06:00
{
2011-09-15 14:08:17 +02:00
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
2009-10-17 15:51:44 -07:00
2009-03-11 16:17:37 -06:00
areaid = entry ? entry -> ID : 0 ;
2010-04-07 22:59:46 +02:00
zoneid = entry ? (( entry -> zone != 0 ) ? entry -> zone : entry -> ID ) : 0 ;
2009-03-11 16:17:37 -06:00
}
2009-10-17 15:51:44 -07:00
2012-02-03 16:03:52 -05:00
bool Map :: isInLineOfSight ( float x1 , float y1 , float z1 , float x2 , float y2 , float z2 , uint32 phasemask ) const
{
return VMAP :: VMapFactory :: createOrGetVMapManager () -> isInLineOfSight ( GetId (), x1 , y1 , z1 , x2 , y2 , z2 )
2012-02-11 19:23:18 -05:00
&& _dynamicTree . isInLineOfSight ( x1 , y1 , z1 , x2 , y2 , z2 , phasemask );
}
bool Map :: getObjectHitPos ( uint32 phasemask , float x1 , float y1 , float z1 , float x2 , float y2 , float z2 , float & rx , float & ry , float & rz , float modifyDist )
{
Vector3 startPos = Vector3 ( x1 , y1 , z1 );
Vector3 dstPos = Vector3 ( x2 , y2 , z2 );
Vector3 resultPos ;
bool result = _dynamicTree . getObjectHitPos ( phasemask , startPos , dstPos , resultPos , modifyDist );
rx = resultPos . x ;
ry = resultPos . y ;
rz = resultPos . z ;
return result ;
2012-02-03 16:03:52 -05:00
}
float Map :: GetHeight ( uint32 phasemask , float x , float y , float z , bool vmap /*=true*/ , float maxSearchDist /*=DEFAULT_HEIGHT_SEARCH*/ ) const
{
2012-02-11 19:23:18 -05:00
return std :: max < float > ( GetHeight ( x , y , z , vmap , maxSearchDist ), _dynamicTree . getHeight ( x , y , z , maxSearchDist , phasemask ));
2012-02-03 16:03:52 -05:00
}
2011-09-15 14:08:17 +02:00
bool Map :: IsInWater ( float x , float y , float pZ , LiquidData * data ) const
2008-10-02 16:23:55 -05:00
{
2009-03-09 18:06:19 -06:00
// Check surface in x, y point for liquid
2009-09-18 14:10:37 -07:00
if ( const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2009-03-09 18:06:19 -06:00
{
LiquidData liquid_status ;
2011-09-15 14:08:17 +02:00
LiquidData * liquid_ptr = data ? data : & liquid_status ;
2010-06-05 00:59:25 +02:00
if ( getLiquidStatus ( x , y , pZ , MAP_ALL_LIQUIDS , liquid_ptr ))
2011-10-19 17:40:44 +01:00
return true ;
2009-03-09 18:06:19 -06:00
}
return false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool Map :: IsUnderWater ( float x , float y , float z ) const
{
2009-09-18 14:10:37 -07:00
if ( const_cast < Map *> ( this ) -> GetGrid ( x , y ))
2009-03-09 18:06:19 -06:00
{
if ( getLiquidStatus ( x , y , z , MAP_LIQUID_TYPE_WATER | MAP_LIQUID_TYPE_OCEAN ) & LIQUID_MAP_UNDER_WATER )
return true ;
}
return false ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
bool Map :: CheckGridIntegrity ( Creature * c , bool moved ) const
{
Cell const & cur_cell = c -> GetCurrentCell ();
2011-10-18 11:18:49 -04:00
Cell xy_cell ( c -> GetPositionX (), c -> GetPositionY ());
2010-04-07 19:14:10 +02:00
if ( xy_cell != cur_cell )
2008-10-02 16:23:55 -05:00
{
2011-04-29 20:47:02 +02:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Creature (GUID: %u) X: %f Y: %f (%s) is in grid[%u, %u]cell[%u, %u] instead of grid[%u, %u]cell[%u, %u]" ,
2009-03-25 16:07:19 -06:00
c -> GetGUIDLow (),
2011-04-29 20:47:02 +02:00
c -> GetPositionX (), c -> GetPositionY (), ( moved ? "final" : "original" ),
2008-10-02 16:23:55 -05:00
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
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
char const * Map :: GetMapName () const
2008-10-02 16:23:55 -05:00
{
2010-12-23 23:25:44 +01:00
return i_mapEntry ? i_mapEntry -> name [ sWorld -> GetDefaultDbcLocale ()] : "UNNAMEDMAP\x0" ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
void Map :: UpdateObjectVisibility ( WorldObject * obj , Cell cell , CellCoord cellpair )
2008-10-02 16:23:55 -05:00
{
cell . SetNoCreate ();
2008-10-14 11:57:03 -05:00
Trinity :: VisibleChangesNotifier notifier ( * obj );
TypeContainerVisitor < Trinity :: VisibleChangesNotifier , WorldTypeMapContainer > player_notifier ( notifier );
2010-11-13 17:18:09 +01:00
cell . Visit ( cellpair , player_notifier , * this , * obj , obj -> GetVisibilityRange ());
2009-09-27 02:24:25 -07:00
}
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
void Map :: UpdateObjectsVisibilityFor ( Player * player , Cell cell , CellCoord cellpair )
2009-09-27 02:24:25 -07:00
{
2010-02-27 15:25:14 +02:00
Trinity :: VisibleNotifier notifier ( * player );
2009-10-17 16:20:24 -07:00
2009-09-27 02:24:25 -07:00
cell . SetNoCreate ();
2009-12-19 10:12:40 +01:00
TypeContainerVisitor < Trinity :: VisibleNotifier , WorldTypeMapContainer > world_notifier ( notifier );
TypeContainerVisitor < Trinity :: VisibleNotifier , GridTypeMapContainer > grid_notifier ( notifier );
2011-08-08 13:16:03 +02:00
cell . Visit ( cellpair , world_notifier , * this , * player , player -> GetSightRange ());
cell . Visit ( cellpair , grid_notifier , * this , * player , player -> GetSightRange ());
2009-10-17 16:20:24 -07:00
2009-09-27 02:24:25 -07:00
// send data
2009-12-19 10:12:40 +01:00
notifier . SendToSelf ();
2009-09-27 02:24:25 -07:00
}
2009-10-17 15:51:44 -07:00
2011-06-11 22:35:29 +02:00
void Map :: SendInitSelf ( Player * player )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "Creating player data for himself %u" , player -> GetGUIDLow ());
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
UpdateData data ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// attach to player data current transport data
2010-04-07 19:14:10 +02:00
if ( Transport * transport = player -> GetTransport ())
2008-10-02 16:23:55 -05:00
{
transport -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// build data for self presence in world at own client (one time for map)
player -> BuildCreateUpdateBlockForPlayer ( & data , player );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// build other passengers at transport also (they always visible and marked as visible and will not send at visibility update at add to map
2010-04-07 19:14:10 +02:00
if ( Transport * transport = player -> GetTransport ())
2008-10-02 16:23:55 -05:00
{
2010-04-08 08:20:08 +02:00
for ( Transport :: PlayerSet :: const_iterator itr = transport -> GetPassengers (). begin (); itr != transport -> GetPassengers (). end (); ++ itr )
2008-10-02 16:23:55 -05:00
{
2010-04-08 08:20:08 +02:00
if ( player != ( * itr ) && player -> HaveAtClient ( * itr ))
2008-10-02 16:23:55 -05:00
{
( * itr ) -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
WorldPacket packet ;
2009-05-31 15:56:51 -05:00
data . BuildPacket ( & packet );
2008-10-02 16:23:55 -05:00
player -> GetSession () -> SendPacket ( & packet );
}
2009-10-17 15:51:44 -07:00
2011-06-11 22:35:29 +02:00
void Map :: SendInitTransports ( Player * player )
2008-10-02 16:23:55 -05:00
{
// Hack to send out transports
2010-12-22 21:25:23 +01:00
MapManager :: TransportMap & tmap = sMapMgr -> m_TransportsByMap ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
UpdateData transData ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
2009-10-17 15:51:44 -07:00
2009-04-29 00:26:07 -05:00
for ( MapManager :: TransportSet :: const_iterator i = tset . begin (); i != tset . end (); ++ i )
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:49:29 -06:00
// send data for current transport in other place
2010-04-07 23:25:02 +02:00
if (( * i ) != player -> GetTransport () && ( * i ) -> GetMapId () == GetId ())
2008-10-02 16:23:55 -05:00
{
( * i ) -> BuildCreateUpdateBlockForPlayer ( & transData , player );
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
WorldPacket packet ;
2009-05-31 15:56:51 -05:00
transData . BuildPacket ( & packet );
2008-10-02 16:23:55 -05:00
player -> GetSession () -> SendPacket ( & packet );
}
2009-10-17 15:51:44 -07:00
2011-06-11 22:35:29 +02:00
void Map :: SendRemoveTransports ( Player * player )
2008-10-02 16:23:55 -05:00
{
// Hack to send out transports
2010-12-22 21:25:23 +01:00
MapManager :: TransportMap & tmap = sMapMgr -> m_TransportsByMap ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
UpdateData transData ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// except used transport
2009-04-29 00:26:07 -05:00
for ( MapManager :: TransportSet :: const_iterator i = tset . begin (); i != tset . end (); ++ i )
2010-04-08 08:20:08 +02:00
if (( * i ) != player -> GetTransport () && ( * i ) -> GetMapId () != GetId ())
2008-10-02 16:23:55 -05:00
( * i ) -> BuildOutOfRangeUpdateBlock ( & transData );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
WorldPacket packet ;
transData . BuildPacket ( & packet );
player -> GetSession () -> SendPacket ( & packet );
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
inline void Map :: setNGrid ( NGridType * grid , uint32 x , uint32 y )
{
2010-04-07 19:14:10 +02:00
if ( x >= MAX_NUMBER_OF_GRIDS || y >= MAX_NUMBER_OF_GRIDS )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "map::setNGrid() Invalid grid coordinates found: %d, %d!" , x , y );
2010-07-30 20:04:28 +02:00
ASSERT ( false );
2008-10-02 16:23:55 -05:00
}
i_grids [ x ][ y ] = grid ;
}
2009-10-17 15:51:44 -07:00
2009-08-25 15:12:22 -05:00
void Map :: DelayedUpdate ( const uint32 t_diff )
{
RemoveAllObjectsInRemoveList ();
2009-10-17 15:51:44 -07:00
2011-04-29 20:47:02 +02: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 !
2009-08-25 15:12:22 -05:00
// This isn't really bother us, since as soon as we have instanced BG-s, the whole map unloads as the BG gets ended
2010-08-08 04:37:24 +02:00
if ( ! IsBattlegroundOrArena ())
2009-08-25 15:12:22 -05:00
{
2009-11-20 19:11:28 -08:00
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end ();)
2009-08-25 15:12:22 -05:00
{
NGridType * grid = i -> getSource ();
2011-09-15 14:08:17 +02:00
GridInfo * info = i -> getSource () -> getGridInfoRef ();
2009-08-25 15:12:22 -05:00
++ i ; // The update might delete the map and we need the next map before the iterator gets invalid
2010-07-30 20:04:28 +02:00
ASSERT ( grid -> GetGridState () >= 0 && grid -> GetGridState () < MAX_GRID_STATE );
2011-10-22 10:01:29 -04:00
si_GridStates [ grid -> GetGridState ()] -> Update ( * this , * grid , * info , t_diff );
2009-08-25 15:12:22 -05:00
}
}
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
void Map :: AddObjectToRemoveList ( WorldObject * obj )
2008-10-02 16:23:55 -05:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
2009-10-17 15:51:44 -07:00
2010-01-22 20:03:47 +01:00
obj -> CleanupsBeforeDelete ( false ); // remove or simplify at least cross referenced links
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
i_objectsToRemove . insert ( obj );
2011-04-29 20:47:02 +02:00
//sLog->outDebug(LOG_FILTER_MAPS, "Object (GUID: %u TypeId: %u) added to removing list.", obj->GetGUIDLow(), obj->GetTypeId());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
void Map :: AddObjectToSwitchList ( WorldObject * obj , bool on )
2009-02-26 17:01:02 -06:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
2009-10-17 15:51:44 -07:00
2009-02-26 17:01:02 -06:00
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . find ( obj );
2010-04-07 19:14:10 +02:00
if ( itr == i_objectsToSwitch . end ())
2009-02-26 17:01:02 -06:00
i_objectsToSwitch . insert ( itr , std :: make_pair ( obj , on ));
2010-04-07 19:14:10 +02:00
else if ( itr -> second != on )
2009-02-26 17:01:02 -06:00
i_objectsToSwitch . erase ( itr );
else
2010-07-30 20:04:28 +02:00
ASSERT ( false );
2009-02-26 17:01:02 -06:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void Map :: RemoveAllObjectsInRemoveList ()
{
2010-04-07 19:13:19 +02:00
while ( ! i_objectsToSwitch . empty ())
2009-02-26 17:01:02 -06:00
{
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . begin ();
2011-09-15 14:08:17 +02:00
WorldObject * obj = itr -> first ;
2009-02-26 17:01:02 -06:00
bool on = itr -> second ;
i_objectsToSwitch . erase ( itr );
2009-10-17 15:51:44 -07:00
2011-12-20 17:05:08 -05:00
if ( obj -> GetTypeId () == TYPEID_UNIT && ! obj -> IsPermanentWorldObject ())
SwitchGridContainers ( obj -> ToCreature (), on );
2009-02-26 17:01:02 -06:00
}
2009-10-17 15:51:44 -07:00
2011-02-20 20:16:34 +01:00
//sLog->outDebug(LOG_FILTER_MAPS, "Object remover 1 check.");
2010-04-07 19:13:19 +02:00
while ( ! i_objectsToRemove . empty ())
2008-10-02 16:23:55 -05:00
{
2009-03-24 17:55:05 -06:00
std :: set < WorldObject *>:: iterator itr = i_objectsToRemove . begin ();
WorldObject * obj = * itr ;
2009-10-17 15:51:44 -07:00
2011-09-29 12:43:05 +02:00
switch ( obj -> GetTypeId ())
2008-10-02 16:23:55 -05:00
{
case TYPEID_CORPSE :
{
2010-12-23 23:25:44 +01:00
Corpse * corpse = ObjectAccessor :: GetCorpse ( * obj , obj -> GetGUID ());
2008-10-02 16:23:55 -05:00
if ( ! corpse )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Tried to delete corpse/bones %u that is not in map." , obj -> GetGUIDLow ());
2008-10-02 16:23:55 -05:00
else
2011-10-10 17:39:34 -04:00
RemoveFromMap ( corpse , true );
2008-10-02 16:23:55 -05:00
break ;
}
case TYPEID_DYNAMICOBJECT :
2011-10-10 17:39:34 -04:00
RemoveFromMap (( DynamicObject * ) obj , true );
2008-10-02 16:23:55 -05:00
break ;
case TYPEID_GAMEOBJECT :
2011-10-10 17:39:34 -04:00
RemoveFromMap (( GameObject * ) obj , true );
2008-10-02 16:23:55 -05:00
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
2010-03-07 18:30:53 +01:00
obj -> ToCreature () -> CleanupsBeforeDelete ();
2011-10-10 17:39:34 -04:00
RemoveFromMap ( obj -> ToCreature (), true );
2008-10-02 16:23:55 -05:00
break ;
default :
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Non-grid object (TypeId: %u) is in grid object remove list, ignored." , obj -> GetTypeId ());
2008-10-02 16:23:55 -05:00
break ;
}
2009-10-17 15:51:44 -07:00
2009-03-24 17:55:05 -06:00
i_objectsToRemove . erase ( itr );
2008-10-02 16:23:55 -05:00
}
2009-12-17 11:30:18 +01:00
2011-02-20 20:16:34 +01:00
//sLog->outDebug(LOG_FILTER_MAPS, "Object remover 2 check.");
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-18 19:40:06 -06:00
uint32 Map :: GetPlayersCountExceptGMs () const
{
uint32 count = 0 ;
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2010-04-07 19:14:10 +02:00
if ( ! itr -> getSource () -> isGameMaster ())
2008-11-18 19:40:06 -06:00
++ count ;
return count ;
}
2009-10-17 15:51:44 -07:00
2008-11-18 19:40:06 -06:00
void Map :: SendToPlayers ( WorldPacket const * data ) const
{
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-11-18 19:40:06 -06:00
itr -> getSource () -> GetSession () -> SendPacket ( data );
}
2009-10-17 15:51:44 -07:00
2011-10-22 10:01:29 -04:00
bool Map :: ActiveObjectsNearGrid ( NGridType const & ngrid ) const
2008-11-18 19:40:06 -06:00
{
2011-10-22 10:01:29 -04:00
CellCoord cell_min ( ngrid . getX () * MAX_NUMBER_OF_CELLS , ngrid . getY () * MAX_NUMBER_OF_CELLS );
2011-10-18 10:53:34 -04:00
CellCoord cell_max ( cell_min . x_coord + MAX_NUMBER_OF_CELLS , cell_min . y_coord + MAX_NUMBER_OF_CELLS );
2009-10-17 16:20:24 -07:00
2009-09-27 02:24:25 -07:00
//we must find visible range in cells so we unload only non-visible cells...
2010-11-13 17:18:09 +01:00
float viewDist = GetVisibilityRange ();
2009-09-27 02:24:25 -07:00
int cell_range = ( int ) ceilf ( viewDist / SIZE_OF_GRID_CELL ) + 1 ;
2009-10-17 16:20:24 -07:00
2011-10-18 11:42:40 +02:00
cell_min . dec_x ( cell_range );
cell_min . dec_y ( cell_range );
cell_max . inc_x ( cell_range );
cell_max . inc_y ( cell_range );
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: const_iterator iter = m_mapRefManager . begin (); iter != m_mapRefManager . end (); ++ iter )
2008-11-18 19:40:06 -06:00
{
2011-11-07 11:06:39 -06:00
Player * player = iter -> getSource ();
2009-10-17 15:51:44 -07:00
2011-11-07 11:06:39 -06:00
CellCoord p = Trinity :: ComputeCellCoord ( player -> GetPositionX (), player -> GetPositionY ());
2009-11-22 19:56:48 -08: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 ))
2008-11-18 19:40:06 -06:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-10-17 16:20:24 -07: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 ;
2009-10-17 15:51:44 -07:00
2011-10-18 10:53:34 -04:00
CellCoord p = Trinity :: ComputeCellCoord ( obj -> GetPositionX (), obj -> GetPositionY ());
2009-11-22 19:56:48 -08: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 ))
2008-11-21 15:41:18 -06:00
return true ;
}
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
return false ;
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
void Map :: AddToActive ( Creature * c )
2009-02-25 20:52:20 -06:00
{
AddToActiveHelper ( c );
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
// also not allow unloading spawn grid to prevent creating creature clone at load
2010-04-07 19:14:10 +02:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2008-11-21 15:41:18 -06:00
{
2011-04-29 20:47:02 +02:00
float x , y , z ;
2011-10-18 10:53:34 -04:00
c -> GetRespawnPosition ( x , y , z );
GridCoord p = Trinity :: ComputeGridCoord ( x , y );
2010-04-07 19:14:10 +02:00
if ( getNGrid ( p . x_coord , p . y_coord ))
2009-02-25 20:52:20 -06:00
getNGrid ( p . x_coord , p . y_coord ) -> incUnloadActiveLock ();
else
{
2011-10-18 10:53:34 -04:00
GridCoord p2 = Trinity :: ComputeGridCoord ( c -> GetPositionX (), c -> GetPositionY ());
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Active creature (GUID: %u Entry: %u) added to grid[%u, %u] but spawn grid[%u, %u] was not loaded." ,
2009-02-25 20:52:20 -06:00
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
}
2009-10-17 15:51:44 -07:00
2010-04-07 22:59:46 +02:00
void Map :: RemoveFromActive ( Creature * c )
2009-02-25 20:52:20 -06:00
{
RemoveFromActiveHelper ( c );
2009-10-17 15:51:44 -07:00
2009-02-25 20:52:20 -06:00
// also allow unloading spawn grid
2010-04-07 19:14:10 +02:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2009-02-25 20:52:20 -06:00
{
2011-04-29 20:47:02 +02:00
float x , y , z ;
2011-10-18 10:53:34 -04:00
c -> GetRespawnPosition ( x , y , z );
GridCoord p = Trinity :: ComputeGridCoord ( x , y );
2010-04-07 19:14:10 +02:00
if ( getNGrid ( p . x_coord , p . y_coord ))
2009-02-25 20:52:20 -06:00
getNGrid ( p . x_coord , p . y_coord ) -> decUnloadActiveLock ();
else
{
2011-10-18 10:53:34 -04:00
GridCoord p2 = Trinity :: ComputeGridCoord ( c -> GetPositionX (), c -> GetPositionY ());
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Active creature (GUID: %u Entry: %u) removed from grid[%u, %u] but spawn grid[%u, %u] was not loaded." ,
2009-02-25 20:52:20 -06:00
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-10-17 15:51:44 -07:00
2011-11-28 13:23:20 -05:00
template bool Map :: AddToMap ( Corpse * );
template bool Map :: AddToMap ( Creature * );
template bool Map :: AddToMap ( GameObject * );
template bool Map :: AddToMap ( DynamicObject * );
2009-10-17 15:51:44 -07:00
2011-10-10 17:39:34 -04:00
template void Map :: RemoveFromMap ( Corpse * , bool );
template void Map :: RemoveFromMap ( Creature * , bool );
template void Map :: RemoveFromMap ( GameObject * , bool );
template void Map :: RemoveFromMap ( DynamicObject * , bool );
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* ******* Dungeon Instance Maps ******* */
2009-10-17 15:51:44 -07:00
2009-07-16 11:49:00 +08:00
InstanceMap :: InstanceMap ( uint32 id , time_t expiry , uint32 InstanceId , uint8 SpawnMode , Map * _parent )
: Map ( id , expiry , InstanceId , SpawnMode , _parent ),
2009-02-24 11:14:28 -06:00
m_resetAfterUnload ( false ), m_unloadWhenEmpty ( false ),
i_data ( NULL ), i_script_id ( 0 )
2008-10-02 16:23:55 -05:00
{
2009-09-27 02:24:25 -07:00
//lets initialize visibility distance for dungeons
InstanceMap :: InitVisibilityDistance ();
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// 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
2010-12-23 23:25:44 +01:00
m_unloadTimer = std :: max ( sWorld -> getIntConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
InstanceMap ::~ InstanceMap ()
{
2010-06-21 23:20:58 +02:00
delete i_data ;
i_data = NULL ;
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-09-27 02:24:25 -07:00
void InstanceMap :: InitVisibilityDistance ()
{
//init visibility distance for instances
2009-11-22 19:56:48 -08:00
m_VisibleDistance = World :: GetMaxVisibleDistanceInInstances ();
2010-02-27 15:25:14 +02:00
m_VisibilityNotifyPeriod = World :: GetVisibilityNotifyPeriodInInstances ();
2009-09-27 02:24:25 -07:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/*
Do map specific checks to see if the player can enter
*/
2011-06-12 01:47:45 +02:00
bool InstanceMap :: CanEnter ( Player * player )
2008-10-02 16:23:55 -05:00
{
2010-04-07 19:14:10 +02:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::CanEnter - player %s(%u) already in map %d, %d, %d!" , player -> GetName (), player -> GetGUIDLow (), GetId (), GetInstanceId (), GetSpawnMode ());
2010-07-30 20:04:28 +02:00
ASSERT ( false );
2008-10-02 16:23:55 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2009-10-31 19:34:02 +01:00
// allow GM's to enter
if ( player -> isGameMaster ())
return Map :: CanEnter ( player );
2008-10-02 16:23:55 -05:00
// cannot enter if the instance is full (player cap), GMs don't count
2009-01-22 22:44:27 -06:00
uint32 maxPlayers = GetMaxPlayers ();
2009-10-31 19:34:02 +01:00
if ( GetPlayersCountExceptGMs () >= maxPlayers )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "MAP: Instance '%u' of map '%s' cannot have more than '%u' players. Player '%s' rejected" , GetInstanceId (), GetMapName (), maxPlayers , player -> GetName ());
2008-10-02 16:23:55 -05:00
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_MAX_PLAYERS );
return false ;
}
2009-10-17 15:51:44 -07:00
2009-10-31 19:34:02 +01:00
// cannot enter while an encounter is in progress on raids
2011-10-07 10:54:53 -05:00
/*Group* group = player->GetGroup();
if (!player->isGameMaster() && group && group->InCombatToInstance(GetInstanceId()) && player->GetMapId() != GetId())*/
2010-08-08 22:54:58 +06:00
if ( IsRaid () && GetInstanceScript () && GetInstanceScript () -> IsEncounterInProgress ())
2008-10-02 16:23:55 -05:00
{
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_ZONE_IN_COMBAT );
return false ;
}
2009-10-17 15:51:44 -07:00
2010-07-07 22:22:16 +02:00
// cannot enter if instance is in use by another party/soloer that have a
// permanent save in the same instance id
PlayerList const & playerList = GetPlayers ();
if ( ! playerList . isEmpty ())
for ( PlayerList :: const_iterator i = playerList . begin (); i != playerList . end (); ++ i )
2011-09-15 14:08:17 +02:00
if ( Player * iPlayer = i -> getSource ())
2010-07-07 22:22:16 +02:00
{
if ( iPlayer -> isGameMaster ()) // bypass GMs
continue ;
if ( ! player -> GetGroup ()) // player has not group and there is someone inside, deny entry
{
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_MAX_PLAYERS );
return false ;
}
// player inside instance has no group or his groups is different to entering player's one, deny entry
2012-03-29 14:43:34 +08:00
if ( ! iPlayer -> GetGroup () || iPlayer -> GetGroup () != player -> GetGroup ())
2010-07-07 22:22:16 +02:00
{
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_MAX_PLAYERS );
return false ;
}
break ;
}
2008-10-02 16:23:55 -05:00
return Map :: CanEnter ( player );
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/*
Do map specific checks and add the player to the map if successful.
*/
2011-11-11 17:50:26 -05:00
bool InstanceMap :: AddPlayerToMap ( Player * player )
2008-10-02 16:23:55 -05:00
{
// TODO: Not sure about checking player level: already done in HandleAreaTriggerOpcode
// GMs still can teleport player in instance.
// Is it needed?
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
{
2011-11-08 10:48:49 -05:00
TRINITY_GUARD ( ACE_Thread_Mutex , Lock );
2009-08-01 00:20:32 +02:00
// Check moved to void WorldSession::HandleMoveWorldportAckOpcode()
2010-04-07 19:14:10 +02:00
//if (!CanEnter(player))
2009-08-01 00:20:32 +02:00
//return false;
2009-10-17 15:51:44 -07:00
2009-03-18 20:41:49 -06:00
// Dungeon only code
2010-04-07 19:14:10 +02:00
if ( IsDungeon ())
2008-10-02 16:23:55 -05:00
{
2011-06-12 01:47:45 +02:00
Group * group = player -> GetGroup ();
2011-01-24 11:20:30 +01:00
// increase current instances (hourly limit)
if ( ! group || ! group -> isLFGGroup ())
player -> AddInstanceEnterTime ( GetInstanceId (), time ( NULL ));
2009-03-18 20:41:49 -06:00
// get or create an instance save for the map
2011-09-15 14:08:17 +02:00
InstanceSave * mapSave = sInstanceSaveMgr -> GetInstanceSave ( GetInstanceId ());
2010-04-07 19:14:10 +02:00
if ( ! mapSave )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "InstanceMap::Add: creating instance save for map %d spawnmode %d with instance id %d" , GetId (), GetSpawnMode (), GetInstanceId ());
2010-12-22 21:25:23 +01:00
mapSave = sInstanceSaveMgr -> AddInstanceSave ( GetId (), GetInstanceId (), Difficulty ( GetSpawnMode ()), 0 , true );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-03-18 20:41:49 -06:00
// check for existing instance binds
2011-09-15 14:08:17 +02:00
InstancePlayerBind * playerBind = player -> GetBoundInstance ( GetId (), Difficulty ( GetSpawnMode ()));
2010-04-07 19:14:10 +02:00
if ( playerBind && playerBind -> perm )
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
// cannot enter other instances if bound permanently
2010-04-07 19:14:10 +02:00
if ( playerBind -> save != mapSave )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::Add: player %s(%d) is permanently bound to instance %d, %d, %d, %d, %d, %d but he is being put into 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 ());
2009-12-17 06:49:36 +01:00
return false ;
2008-10-02 16:23:55 -05:00
}
2009-03-18 20:41:49 -06:00
}
else
{
2011-01-24 11:20:30 +01:00
if ( group )
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
// solo saves should be reset when entering a group
2011-09-15 14:08:17 +02:00
InstanceGroupBind * groupBind = group -> GetBoundInstance ( this );
2010-04-07 19:14:10 +02:00
if ( playerBind )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::Add: player %s(%d) is being put into 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 ( group -> GetLeaderGUID ()), playerBind -> save -> GetMapId (), playerBind -> save -> GetInstanceId (), playerBind -> save -> GetDifficulty (), playerBind -> save -> GetPlayerCount (), playerBind -> save -> GetGroupCount (), playerBind -> save -> CanReset ());
2011-01-24 11:20:30 +01:00
if ( groupBind )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::Add: the group is bound to the 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 ());
2010-07-30 20:04:28 +02:00
//ASSERT(false);
2009-03-23 17:01:26 -06:00
return false ;
2008-10-02 16:23:55 -05:00
}
2009-03-18 20:41:49 -06:00
// bind to the group or keep using the group save
2010-04-07 19:14:10 +02:00
if ( ! groupBind )
2011-01-24 11:20:30 +01:00
group -> BindToInstance ( mapSave , false );
2009-03-18 20:41:49 -06:00
else
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
// cannot jump to a different instance without resetting it
2010-04-07 19:14:10 +02:00
if ( groupBind -> save != mapSave )
2009-03-18 20:41:49 -06:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::Add: player %s(%d) is being put into 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 ( group -> GetLeaderGUID ()), groupBind -> save -> GetMapId (), groupBind -> save -> GetInstanceId (), groupBind -> save -> GetDifficulty ());
2010-04-07 19:14:10 +02:00
if ( mapSave )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "MapSave players: %d, group count: %d" , mapSave -> GetPlayerCount (), mapSave -> GetGroupCount ());
2009-03-18 20:41:49 -06:00
else
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "MapSave NULL" );
2010-04-07 19:14:10 +02:00
if ( groupBind -> save )
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "GroupBind save players: %d, group count: %d" , groupBind -> save -> GetPlayerCount (), groupBind -> save -> GetGroupCount ());
2009-03-18 20:41:49 -06:00
else
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "GroupBind save NULL" );
2009-12-17 06:49:36 +01:00
return false ;
2009-03-18 20:41:49 -06:00
}
// if the group/leader is permanently bound to the instance
// players also become permanently bound when they enter
2010-04-07 19:14:10 +02:00
if ( groupBind -> perm )
2009-03-18 20:41:49 -06:00
{
2011-01-24 11:20:30 +01:00
WorldPacket data ( SMSG_INSTANCE_LOCK_WARNING_QUERY , 9 );
data << uint32 ( 60000 );
data << uint32 ( i_data ? i_data -> GetCompletedEncounterMask () : 0 );
data << uint8 ( 0 );
2009-03-18 20:41:49 -06:00
player -> GetSession () -> SendPacket ( & data );
2011-09-15 15:29:52 -04:00
player -> SetPendingBind ( mapSave -> GetInstanceId (), 60000 );
2009-03-18 20:41:49 -06:00
}
2008-10-02 16:23:55 -05:00
}
}
else
2009-03-18 20:41:49 -06:00
{
// set up a solo bind or continue using it
2010-04-07 19:14:10 +02:00
if ( ! playerBind )
2009-03-18 20:41:49 -06:00
player -> BindToInstance ( mapSave , false );
else
// cannot jump to a different instance without resetting it
2010-07-30 20:04:28 +02:00
ASSERT ( playerBind -> save == mapSave );
2009-03-18 20:41:49 -06:00
}
2008-10-02 16:23:55 -05:00
}
2011-12-18 22:09:03 +01:00
if ( group && group -> isLFGGroup ())
if ( uint32 dungeonId = sLFGMgr -> GetDungeon ( group -> GetGUID (), true ))
if ( LFGDungeonEntry const * dungeon = sLFGDungeonStore . LookupEntry ( dungeonId ))
if ( LFGDungeonEntry const * randomDungeon = sLFGDungeonStore . LookupEntry ( * ( sLFGMgr -> GetSelectedDungeons ( player -> GetGUID ()). begin ())))
2012-05-20 17:58:35 +01:00
if ( uint32 ( dungeon -> map ) == GetId () && dungeon -> difficulty == uint32 ( GetDifficulty ()) && randomDungeon -> type == uint32 ( LFG_TYPE_RANDOM ))
2011-12-18 22:09:03 +01:00
player -> CastSpell ( player , LFG_SPELL_LUCK_OF_THE_DRAW , true );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-28 11:38:28 -06:00
// for normal instances cancel the reset schedule when the
// first player enters (no players yet)
2011-04-10 21:17:46 +02:00
SetResetSchedule ( false );
2009-10-17 15:51:44 -07:00
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "MAP: Player '%s' entered instance '%u' of map '%s'" , player -> GetName (), GetInstanceId (), GetMapName ());
2008-10-02 16:23:55 -05:00
// initialize unload state
m_unloadTimer = 0 ;
m_resetAfterUnload = false ;
m_unloadWhenEmpty = false ;
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// this will acquire the same mutex so it cannot be in the previous block
2011-11-11 17:50:26 -05:00
Map :: AddPlayerToMap ( player );
2010-02-14 19:13:14 -07:00
if ( i_data )
i_data -> OnPlayerEnter ( player );
2008-10-02 16:23:55 -05:00
return true ;
}
2009-10-17 15:51:44 -07:00
2011-08-01 14:23:27 +02:00
void InstanceMap :: Update ( const uint32 t_diff )
2008-11-02 16:53:46 -06:00
{
2008-11-07 09:36:46 -06:00
Map :: Update ( t_diff );
2009-10-17 15:51:44 -07:00
2009-10-30 22:52:37 -07:00
if ( i_data )
2008-11-07 09:36:46 -06:00
i_data -> Update ( t_diff );
2008-11-02 16:53:46 -06:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
void InstanceMap :: RemovePlayerFromMap ( Player * player , bool remove )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "MAP: Removing player '%s' from instance '%u' of map '%s' before relocating to another map" , player -> GetName (), GetInstanceId (), GetMapName ());
2008-11-18 19:40:06 -06:00
//if last player set unload timer
2009-10-30 22:52:37 -07:00
if ( ! m_unloadTimer && m_mapRefManager . getSize () == 1 )
2010-12-23 23:25:44 +01:00
m_unloadTimer = m_unloadWhenEmpty ? MIN_UNLOAD_DELAY : std :: max ( sWorld -> getIntConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
2011-11-11 17:50:26 -05:00
Map :: RemovePlayerFromMap ( player , remove );
2008-11-28 11:38:28 -06:00
// for normal instances schedule the reset after all players have left
2011-04-10 21:17:46 +02:00
SetResetSchedule ( true );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
void InstanceMap :: CreateInstanceData ( bool load )
{
2009-10-30 22:52:37 -07:00
if ( i_data != NULL )
2008-10-02 16:23:55 -05:00
return ;
2009-10-17 15:51:44 -07:00
2011-04-28 22:16:13 +02:00
InstanceTemplate const * mInstance = sObjectMgr -> GetInstanceTemplate ( GetId ());
2008-10-02 16:23:55 -05:00
if ( mInstance )
{
2011-04-28 22:16:13 +02:00
i_script_id = mInstance -> ScriptId ;
2010-12-22 21:25:23 +01:00
i_data = sScriptMgr -> CreateInstanceData ( this );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2009-10-30 22:52:37 -07:00
if ( ! i_data )
2008-10-02 16:23:55 -05:00
return ;
2009-10-17 15:51:44 -07:00
2009-05-01 11:38:53 -05:00
i_data -> Initialize ();
2009-10-17 15:51:44 -07:00
2009-10-30 22:52:37 -07:00
if ( load )
2008-10-02 16:23:55 -05:00
{
// TODO: make a global storage for this
2012-03-24 01:25:08 +01:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_INSTANCE );
stmt -> setUInt16 ( 0 , uint16 ( GetId ()));
stmt -> setUInt32 ( 1 , i_InstanceId );
PreparedQueryResult result = CharacterDatabase . Query ( stmt );
2008-10-02 16:23:55 -05:00
if ( result )
{
Field * fields = result -> Fetch ();
2010-05-01 20:59:56 +02:00
std :: string data = fields [ 0 ]. GetString ();
2011-02-03 22:20:40 +01:00
i_data -> SetCompletedEncountersMask ( fields [ 1 ]. GetUInt32 ());
2010-05-01 20:59:56 +02:00
if ( data != "" )
2008-10-02 16:23:55 -05:00
{
2011-02-20 20:16:34 +01:00
sLog -> outDebug ( LOG_FILTER_MAPS , "Loading instance data for `%s` with id %u" , sObjectMgr -> GetScriptName ( i_script_id ), i_InstanceId );
2010-05-01 20:59:56 +02:00
i_data -> Load ( data . c_str ());
2008-10-02 16:23:55 -05:00
}
}
}
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/*
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
2009-10-17 15:51:44 -07:00
2009-10-30 22:52:37 -07:00
if ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
2010-11-07 23:53:26 +01:00
if ( method == INSTANCE_RESET_ALL || method == INSTANCE_RESET_CHANGE_DIFFICULTY )
2008-10-02 16:23:55 -05:00
{
// notify the players to leave the instance so it can be reset
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-11-18 19:40:06 -06:00
itr -> getSource () -> SendResetFailedNotify ( GetId ());
2008-10-02 16:23:55 -05:00
}
else
{
2009-10-30 22:52:37 -07:00
if ( method == INSTANCE_RESET_GLOBAL )
2008-10-02 16:23:55 -05:00
// set the homebind timer for players inside (1 minute)
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-11-18 19:40:06 -06:00
itr -> getSource () -> m_InstanceValid = false ;
2009-10-17 15:51:44 -07:00
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 ;
}
2009-10-17 15:51:44 -07:00
2008-11-18 19:40:06 -06:00
return m_mapRefManager . isEmpty ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-11-23 19:17:33 +01:00
void InstanceMap :: PermBindAllPlayers ( Player * source )
2008-10-02 16:23:55 -05:00
{
2009-10-30 22:52:37 -07:00
if ( ! IsDungeon ())
2009-03-18 20:41:49 -06:00
return ;
2009-10-17 15:51:44 -07:00
2011-09-15 14:08:17 +02:00
InstanceSave * save = sInstanceSaveMgr -> GetInstanceSave ( GetInstanceId ());
2009-10-30 22:52:37 -07:00
if ( ! save )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "Cannot bind player (GUID: %u, Name: %s), because no instance save is available for instance map (Name: %s, Entry: %u, InstanceId: %u)!" , source -> GetGUIDLow (), source -> GetName (), source -> GetMap () -> GetMapName (), source -> GetMapId (), GetInstanceId ());
2008-10-02 16:23:55 -05:00
return ;
}
2009-10-17 15:51:44 -07:00
2011-11-23 19:17:33 +01:00
Group * group = source -> GetGroup ();
2008-10-02 16:23:55 -05:00
// group members outside the instance group don't get bound
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-10-02 16:23:55 -05:00
{
2011-11-07 11:06:39 -06:00
Player * player = itr -> getSource ();
2008-11-18 19:40:06 -06:00
// players inside an instance cannot be bound to other instances
// some players may already be permanently bound, in this case nothing happens
2011-11-07 11:06:39 -06:00
InstancePlayerBind * bind = player -> GetBoundInstance ( save -> GetMapId (), save -> GetDifficulty ());
2009-10-30 22:52:37 -07:00
if ( ! bind || ! bind -> perm )
2008-10-02 16:23:55 -05:00
{
2011-11-07 11:06:39 -06:00
player -> BindToInstance ( save , true );
2008-11-18 19:40:06 -06:00
WorldPacket data ( SMSG_INSTANCE_SAVE_CREATED , 4 );
data << uint32 ( 0 );
2011-11-07 11:06:39 -06:00
player -> GetSession () -> SendPacket ( & data );
2012-02-24 19:06:53 +01:00
2012-02-24 19:31:40 +01:00
player -> GetSession () -> SendCalendarRaidLockout ( save , true );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07: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
2011-11-07 11:06:39 -06:00
if ( group && group -> GetLeaderGUID () == player -> GetGUID ())
2008-11-18 19:40:06 -06:00
group -> BindToInstance ( save , true );
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2009-02-25 17:53:05 -06:00
void InstanceMap :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
2010-07-30 20:04:28 +02:00
ASSERT ( ! HavePlayers ());
2009-10-17 15:51:44 -07:00
2009-10-30 22:52:37 -07:00
if ( m_resetAfterUnload == true )
2012-06-07 23:20:57 +02:00
DeleteRespawnTimes ();
2009-10-17 15:51:44 -07:00
2009-02-25 17:53:05 -06:00
Map :: UnloadAll ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2008-11-18 19:40:06 -06:00
void InstanceMap :: SendResetWarnings ( uint32 timeLeft ) const
2008-10-02 16:23:55 -05:00
{
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2009-12-17 11:30:18 +01:00
itr -> getSource () -> SendInstanceResetWarning ( GetId (), itr -> getSource () -> GetDifficulty ( IsRaid ()), timeLeft );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-04-10 21:17:46 +02:00
void InstanceMap :: SetResetSchedule ( bool on )
2008-10-02 16:23:55 -05:00
{
// 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
2010-04-07 19:14:10 +02:00
if ( IsDungeon () && ! HavePlayers () && ! IsRaidOrHeroicDungeon ())
2008-10-02 16:23:55 -05:00
{
2011-09-15 14:08:17 +02:00
if ( InstanceSave * save = sInstanceSaveMgr -> GetInstanceSave ( GetInstanceId ()))
2011-01-24 11:20:30 +01:00
sInstanceSaveMgr -> ScheduleReset ( on , save -> GetResetTime (), InstanceSaveManager :: InstResetEvent ( 0 , GetId (), Difficulty ( GetSpawnMode ()), GetInstanceId ()));
2011-02-17 19:47:08 +06:00
else
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "InstanceMap::SetResetSchedule: cannot turn schedule %s, there is no save information for instance (map [id: %u, name: %s], instance id: %u, difficulty: %u)" ,
2011-08-19 10:47:56 +02:00
on ? "on" : "off" , GetId (), GetMapName (), GetInstanceId (), Difficulty ( GetSpawnMode ()));
2008-10-02 16:23:55 -05:00
}
}
2009-10-17 15:51:44 -07:00
2010-05-16 04:03:53 +02:00
MapDifficulty const * Map :: GetMapDifficulty () const
2009-12-17 11:30:18 +01:00
{
2011-04-29 20:47:02 +02:00
return GetMapDifficultyData ( GetId (), GetDifficulty ());
2009-12-17 11:30:18 +01:00
}
2009-01-22 22:44:27 -06:00
uint32 InstanceMap :: GetMaxPlayers () const
{
2010-04-07 19:14:10 +02:00
if ( MapDifficulty const * mapDiff = GetMapDifficulty ())
2009-12-17 11:30:18 +01:00
{
2010-04-07 19:14:10 +02:00
if ( mapDiff -> maxPlayers || IsRegularDifficulty ()) // Normal case (expect that regular difficulty always have correct maxplayers)
2009-12-17 11:30:18 +01:00
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 ( GetId (), REGULAR_DIFFICULTY );
return normalDiff ? normalDiff -> maxPlayers : 0 ;
}
}
2010-07-30 20:04:28 +02:00
else // I'd rather ASSERT(false);
2009-01-22 22:44:27 -06:00
return 0 ;
2009-12-17 11:30:18 +01:00
}
uint32 InstanceMap :: GetMaxResetDelay () const
{
MapDifficulty const * mapDiff = GetMapDifficulty ();
return mapDiff ? mapDiff -> resetTime : 0 ;
2009-01-22 22:44:27 -06:00
}
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
/* ******* Battleground Instance Maps ******* */
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
BattlegroundMap :: BattlegroundMap ( uint32 id , time_t expiry , uint32 InstanceId , Map * _parent , uint8 spawnMode )
2009-12-17 11:30:18 +01:00
: Map ( id , expiry , InstanceId , spawnMode , _parent )
2008-10-02 16:23:55 -05:00
{
2009-09-27 02:24:25 -07:00
//lets initialize visibility distance for BG/Arenas
2010-08-08 04:37:24 +02:00
BattlegroundMap :: InitVisibilityDistance ();
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
BattlegroundMap ::~ BattlegroundMap ()
2008-10-02 16:23:55 -05:00
{
2011-09-29 12:43:05 +02:00
if ( m_bg )
2011-09-27 17:26:13 -04:00
{
//unlink to prevent crash, always unlink all pointer reference before destruction
m_bg -> SetBgMap ( NULL );
m_bg = NULL ;
}
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
void BattlegroundMap :: InitVisibilityDistance ()
2009-09-27 02:24:25 -07:00
{
//init visibility distance for BG/Arenas
2009-11-22 19:56:48 -08:00
m_VisibleDistance = World :: GetMaxVisibleDistanceInBGArenas ();
2010-02-27 15:25:14 +02:00
m_VisibilityNotifyPeriod = World :: GetVisibilityNotifyPeriodInBGArenas ();
2009-09-27 02:24:25 -07:00
}
2009-10-17 15:51:44 -07:00
2011-06-11 22:35:29 +02:00
bool BattlegroundMap :: CanEnter ( Player * player )
2008-10-02 16:23:55 -05:00
{
2009-10-30 22:52:37 -07:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outError ( LOG_FILTER_MAPS , "BGMap::CanEnter - player %u is already in map!" , player -> GetGUIDLow ());
2010-07-30 20:04:28 +02:00
ASSERT ( false );
2008-10-02 16:23:55 -05:00
return false ;
}
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
if ( player -> GetBattlegroundId () != GetInstanceId ())
2008-10-02 16:23:55 -05:00
return false ;
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
// player number limit is checked in bgmgr, no need to do it here
2009-10-17 15:51:44 -07:00
2008-10-02 16:23:55 -05:00
return Map :: CanEnter ( player );
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
bool BattlegroundMap :: AddPlayerToMap ( Player * player )
2008-10-02 16:23:55 -05:00
{
{
2011-11-08 10:48:49 -05:00
TRINITY_GUARD ( ACE_Thread_Mutex , Lock );
2009-08-01 00:20:32 +02:00
//Check moved to void WorldSession::HandleMoveWorldportAckOpcode()
2010-04-07 19:14:10 +02:00
//if (!CanEnter(player))
2009-08-01 00:20:32 +02:00
//return false;
2008-10-02 16:23:55 -05:00
// reset instance validity, battleground maps do not homebind
player -> m_InstanceValid = true ;
}
2011-11-11 17:50:26 -05:00
return Map :: AddPlayerToMap ( player );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-11-11 17:50:26 -05:00
void BattlegroundMap :: RemovePlayerFromMap ( Player * player , bool remove )
2008-10-02 16:23:55 -05:00
{
2012-08-03 14:20:18 +02:00
sLog -> outInfo ( LOG_FILTER_MAPS , "MAP: Removing player '%s' from bg '%u' of map '%s' before relocating to another map" , player -> GetName (), GetInstanceId (), GetMapName ());
2011-11-11 17:50:26 -05:00
Map :: RemovePlayerFromMap ( player , remove );
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
void BattlegroundMap :: SetUnload ()
2008-10-02 16:23:55 -05:00
{
m_unloadTimer = MIN_UNLOAD_DELAY ;
}
2009-10-17 15:51:44 -07:00
2010-08-08 04:37:24 +02:00
void BattlegroundMap :: RemoveAllPlayers ()
2008-10-02 16:23:55 -05:00
{
2009-10-30 22:52:37 -07:00
if ( HavePlayers ())
2009-10-17 16:20:24 -07:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2011-11-07 11:06:39 -06:00
if ( Player * player = itr -> getSource ())
if ( ! player -> IsBeingTeleportedFar ())
player -> TeleportTo ( player -> GetBattlegroundEntryPoint ());
2008-10-02 16:23:55 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
Creature * Map :: GetCreature ( uint64 guid )
2009-04-20 20:28:19 -05:00
{
2010-07-28 00:08:21 +02:00
return ObjectAccessor :: GetObjectInMap ( guid , this , ( Creature * ) NULL );
2009-04-20 20:28:19 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
GameObject * Map :: GetGameObject ( uint64 guid )
2009-04-20 20:28:19 -05:00
{
2010-07-28 00:08:21 +02:00
return ObjectAccessor :: GetObjectInMap ( guid , this , ( GameObject * ) NULL );
2009-04-20 20:28:19 -05:00
}
2009-10-17 15:51:44 -07:00
2011-12-10 13:48:41 +01:00
DynamicObject * Map :: GetDynamicObject ( uint64 guid )
2009-04-20 20:28:19 -05:00
{
2010-07-28 00:08:21 +02:00
return ObjectAccessor :: GetObjectInMap ( guid , this , ( DynamicObject * ) NULL );
2009-04-20 20:28:19 -05:00
}
2009-10-17 15:51:44 -07:00
2011-06-12 01:47:45 +02:00
void Map :: UpdateIteratorBack ( Player * player )
2009-08-01 00:20:32 +02:00
{
2010-04-07 19:14:10 +02:00
if ( m_mapRefIter == player -> GetMapRef ())
2009-08-01 00:20:32 +02:00
m_mapRefIter = m_mapRefIter -> nocheck_prev ();
}
2012-06-07 23:20:57 +02:00
void Map :: SaveCreatureRespawnTime ( uint32 dbGuid , time_t respawnTime )
{
if ( ! respawnTime )
{
// Delete only
RemoveCreatureRespawnTime ( dbGuid );
return ;
}
_creatureRespawnTimes [ dbGuid ] = respawnTime ;
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_REP_CREATURE_RESPAWN );
stmt -> setUInt32 ( 0 , dbGuid );
stmt -> setUInt32 ( 1 , uint32 ( respawnTime ));
stmt -> setUInt16 ( 2 , GetId ());
stmt -> setUInt32 ( 3 , GetInstanceId ());
CharacterDatabase . Execute ( stmt );
}
void Map :: RemoveCreatureRespawnTime ( uint32 dbGuid )
{
_creatureRespawnTimes . erase ( dbGuid );
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_DEL_CREATURE_RESPAWN );
stmt -> setUInt32 ( 0 , dbGuid );
stmt -> setUInt16 ( 1 , GetId ());
stmt -> setUInt32 ( 2 , GetInstanceId ());
CharacterDatabase . Execute ( stmt );
}
void Map :: SaveGORespawnTime ( uint32 dbGuid , time_t respawnTime )
{
if ( ! respawnTime )
{
// Delete only
RemoveGORespawnTime ( dbGuid );
return ;
}
_goRespawnTimes [ dbGuid ] = respawnTime ;
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_REP_GO_RESPAWN );
stmt -> setUInt32 ( 0 , dbGuid );
stmt -> setUInt32 ( 1 , uint32 ( respawnTime ));
stmt -> setUInt16 ( 2 , GetId ());
stmt -> setUInt32 ( 3 , GetInstanceId ());
CharacterDatabase . Execute ( stmt );
}
void Map :: RemoveGORespawnTime ( uint32 dbGuid )
{
_goRespawnTimes . erase ( dbGuid );
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_DEL_GO_RESPAWN );
stmt -> setUInt32 ( 0 , dbGuid );
stmt -> setUInt16 ( 1 , GetId ());
stmt -> setUInt32 ( 2 , GetInstanceId ());
CharacterDatabase . Execute ( stmt );
}
void Map :: LoadRespawnTimes ()
{
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_CREATURE_RESPAWNS );
stmt -> setUInt16 ( 0 , GetId ());
stmt -> setUInt32 ( 1 , GetInstanceId ());
if ( PreparedQueryResult result = CharacterDatabase . Query ( stmt ))
{
do
{
Field * fields = result -> Fetch ();
uint32 loguid = fields [ 0 ]. GetUInt32 ();
uint32 respawnTime = fields [ 1 ]. GetUInt32 ();
_creatureRespawnTimes [ loguid ] = time_t ( respawnTime );
} while ( result -> NextRow ());
}
stmt = CharacterDatabase . GetPreparedStatement ( CHAR_SEL_GO_RESPAWNS );
stmt -> setUInt16 ( 0 , GetId ());
stmt -> setUInt32 ( 1 , GetInstanceId ());
if ( PreparedQueryResult result = CharacterDatabase . Query ( stmt ))
{
do
{
Field * fields = result -> Fetch ();
uint32 loguid = fields [ 0 ]. GetUInt32 ();
uint32 respawnTime = fields [ 1 ]. GetUInt32 ();
_goRespawnTimes [ loguid ] = time_t ( respawnTime );
} while ( result -> NextRow ());
}
}
void Map :: DeleteRespawnTimes ()
{
_creatureRespawnTimes . clear ();
_goRespawnTimes . clear ();
2012-06-08 14:52:52 +02:00
DeleteRespawnTimesInDB ( GetId (), GetInstanceId ());
}
void Map :: DeleteRespawnTimesInDB ( uint16 mapId , uint32 instanceId )
{
2012-06-07 23:20:57 +02:00
PreparedStatement * stmt = CharacterDatabase . GetPreparedStatement ( CHAR_DEL_CREATURE_RESPAWN_BY_INSTANCE );
2012-06-08 14:52:52 +02:00
stmt -> setUInt16 ( 0 , mapId );
stmt -> setUInt32 ( 1 , instanceId );
2012-06-07 23:20:57 +02:00
CharacterDatabase . Execute ( stmt );
stmt = CharacterDatabase . GetPreparedStatement ( CHAR_DEL_GO_RESPAWN_BY_INSTANCE );
2012-06-08 14:52:52 +02:00
stmt -> setUInt16 ( 0 , mapId );
stmt -> setUInt32 ( 1 , instanceId );
2012-06-07 23:20:57 +02:00
CharacterDatabase . Execute ( stmt );
}
time_t Map :: GetLinkedRespawnTime ( uint64 guid ) const
{
uint64 linkedGuid = sObjectMgr -> GetLinkedRespawnGuid ( guid );
switch ( GUID_HIPART ( linkedGuid ))
{
case HIGHGUID_UNIT :
return GetCreatureRespawnTime ( GUID_LOPART ( linkedGuid ));
case HIGHGUID_GAMEOBJECT :
return GetGORespawnTime ( GUID_LOPART ( linkedGuid ));
default :
break ;
}
return time_t ( 0 );
}
2012-06-08 14:52:52 +02:00