2008-10-02 16:23:55 -05:00
/*
2009-02-04 12:42:26 +01:00
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
2008-10-14 11:57:03 -05:00
*
2009-02-04 12:04:12 +01:00
* Copyright (C) 2008-2009 Trinity <http://www.trinitycore.org/>
2008-10-02 16:23:55 -05:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2008-11-07 09:36:46 -06:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2008-10-02 16:23:55 -05:00
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2008-11-07 09:36:46 -06:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2008-10-02 16:23:55 -05:00
*/
2008-10-20 12:23:56 -05:00
#include "MapManager.h"
2008-10-02 16:23:55 -05:00
#include "Player.h"
2009-07-31 10:48:34 +08:00
#include "Vehicle.h"
2008-10-02 16:23:55 -05:00
#include "GridNotifiers.h"
#include "Log.h"
#include "GridStates.h"
#include "CellImpl.h"
#include "InstanceData.h"
#include "Map.h"
#include "GridNotifiersImpl.h"
#include "Config/ConfigEnv.h"
#include "Transports.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "World.h"
#include "ScriptCalls.h"
#include "Group.h"
2008-11-18 19:40:06 -06:00
#include "MapRefManager.h"
2009-04-17 15:08:58 -05:00
#include "Vehicle.h"
2009-07-31 10:48:34 +08:00
#include "WaypointManager.h"
2008-10-02 16:23:55 -05:00
#include "MapInstanced.h"
#include "InstanceSaveMgr.h"
#include "VMapFactory.h"
#define DEFAULT_GRID_EXPIRY 300
#define MAX_GRID_LOAD_TIME 50
GridState * si_GridStates [ MAX_GRID_STATE ];
2009-07-31 10:48:34 +08:00
struct ScriptAction
{
uint64 sourceGUID ;
uint64 targetGUID ;
uint64 ownerGUID ; // owner of source if source is item
ScriptInfo const * script ; // pointer to static script data
};
2008-10-02 16:23:55 -05:00
Map ::~ Map ()
{
2009-02-25 17:53:05 -06:00
UnloadAll ();
2009-07-31 10:48:34 +08:00
if ( ! m_scriptSchedule . empty ())
sWorld . DecreaseScheduledScriptCount ( m_scriptSchedule . size ());
2008-10-02 16:23:55 -05:00
}
2009-04-02 16:33:35 -06:00
bool Map :: ExistMap ( uint32 mapid , int gx , int gy )
2008-10-02 16:23:55 -05:00
{
int len = sWorld . GetDataPath (). length () + strlen ( "maps/%03u%02u%02u.map" ) + 1 ;
char * tmp = new char [ len ];
2009-04-02 16:33:35 -06:00
snprintf ( tmp , len , ( char * )( sWorld . GetDataPath () + "maps/%03u%02u%02u.map" ). c_str (), mapid , gx , gy );
2008-10-02 16:23:55 -05:00
FILE * pf = fopen ( tmp , "rb" );
if ( ! pf )
{
sLog . outError ( "Check existing of map file '%s': not exist!" , tmp );
delete [] tmp ;
return false ;
}
2009-03-09 18:06:19 -06:00
map_fileheader header ;
fread ( & header , sizeof ( header ), 1 , pf );
2009-03-12 14:49:12 -06:00
if ( header . mapMagic != uint32 ( MAP_MAGIC ) ||
header . versionMagic != uint32 ( MAP_VERSION_MAGIC ))
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "Map file '%s' is non-compatible version (outdated?). Please, create new using ad.exe program." , tmp );
delete [] tmp ;
fclose ( pf ); //close file before return
return false ;
}
delete [] tmp ;
fclose ( pf );
return true ;
}
2009-04-02 16:33:35 -06:00
bool Map :: ExistVMap ( uint32 mapid , int gx , int gy )
2008-10-02 16:23:55 -05:00
{
if ( VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ())
{
if ( vmgr -> isMapLoadingEnabled ())
{
2008-11-07 09:36:46 -06:00
// x and y are swapped !! => fixed now
2009-04-02 16:33:35 -06:00
bool exists = vmgr -> existsMap (( sWorld . GetDataPath () + "vmaps" ). c_str (), mapid , gx , gy );
2008-10-02 16:23:55 -05:00
if ( ! exists )
{
2009-04-02 16:33:35 -06:00
std :: string name = vmgr -> getDirFileName ( mapid , gx , gy );
2008-10-02 16:23:55 -05:00
sLog . outError ( "VMap file '%s' is missing or point to wrong version vmap file, redo vmaps with latest vmap_assembler.exe program" , ( sWorld . GetDataPath () + "vmaps/" + name ). c_str ());
return false ;
}
}
}
return true ;
}
2009-04-02 16:33:35 -06: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 !!
2009-04-02 16:33:35 -06:00
int vmapLoadResult = VMAP :: VMapFactory :: createOrGetVMapManager () -> loadMap (( sWorld . GetDataPath () + "vmaps" ). c_str (), GetId (), gx , gy );
2008-10-02 16:23:55 -05:00
switch ( vmapLoadResult )
{
case VMAP :: VMAP_LOAD_RESULT_OK :
2009-04-02 16:33:35 -06:00
sLog . outDetail ( "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 :
2009-04-02 16:33:35 -06:00
sLog . outDetail ( "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 :
2009-04-02 16:33:35 -06:00
DEBUG_LOG ( "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-05-13 18:24:35 -05:00
void Map :: LoadMap ( int gx , int gy , bool reload )
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:33:35 -06:00
if ( i_InstanceId != 0 )
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:33:35 -06:00
if ( GridMaps [ gx ][ gy ])
2008-10-02 16:23:55 -05:00
return ;
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 ])
m_parentMap -> EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
2008-10-02 16:23:55 -05:00
2009-07-16 11:49:00 +08:00
(( MapInstanced * )( m_parentMap )) -> AddGridMapReference ( GridPair ( gx , gy ));
GridMaps [ gx ][ gy ] = m_parentMap -> GridMaps [ gx ][ gy ];
2008-10-02 16:23:55 -05:00
return ;
}
2009-05-13 18:24:35 -05:00
if ( GridMaps [ gx ][ gy ] && ! reload )
return ;
2008-11-07 09:36:46 -06:00
//map already load, delete it before reloading (Is it necessary? Do we really need the ability the reload maps during runtime?)
2009-04-02 16:33:35 -06:00
if ( GridMaps [ gx ][ gy ])
2008-10-02 16:23:55 -05:00
{
2009-08-01 00:20:32 +02:00
sLog . outDetail ( "Unloading already loaded map %u before reloading." , GetId ());
2009-04-02 16:33:35 -06:00
delete ( GridMaps [ gx ][ gy ]);
GridMaps [ gx ][ gy ] = NULL ;
2008-10-02 16:23:55 -05:00
}
// map file name
char * tmp = NULL ;
int len = sWorld . GetDataPath (). length () + strlen ( "maps/%03u%02u%02u.map" ) + 1 ;
tmp = new char [ len ];
2009-08-01 00:20:32 +02:00
snprintf ( tmp , len , ( char * )( sWorld . GetDataPath () + "maps/%03u%02u%02u.map" ). c_str (), GetId (), gx , gy );
2008-10-02 16:23:55 -05:00
sLog . outDetail ( "Loading map %s" , tmp );
// 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
{
2009-03-09 18:06:19 -06:00
sLog . outError ( "Error load map file: \n %s \n " , tmp );
2008-10-02 16:23:55 -05:00
}
2009-03-09 18:06:19 -06:00
delete [] tmp ;
2008-10-02 16:23:55 -05:00
}
2009-04-02 16:33:35 -06:00
void Map :: LoadMapAndVMap ( int gx , int gy )
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:33:35 -06:00
LoadMap ( gx , gy );
if ( i_InstanceId == 0 )
LoadVMap ( gx , gy ); // Only load the data for the base map
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 ;
}
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-07-16 11:49:00 +08:00
Map :: Map ( uint32 id , time_t expiry , uint32 InstanceId , uint8 SpawnMode , Map * _parent )
2009-08-01 00:20:32 +02:00
: i_mapEntry ( sMapStore . LookupEntry ( id )), i_spawnMode ( SpawnMode ), i_InstanceId ( InstanceId ), m_unloadTimer ( 0 ),
2009-05-03 22:21:46 -05:00
m_activeNonPlayersIter ( m_activeNonPlayers . end ()),
2009-07-16 11:49:00 +08:00
i_gridExpiry ( expiry ), m_parentMap ( _parent ? _parent : this )
2009-04-02 20:27:10 -06:00
, i_lock ( true )
2008-10-02 16:23:55 -05:00
{
2009-06-10 16:33:09 -05:00
m_notifyTimer . SetInterval ( IN_MILISECONDS / 2 );
2008-10-02 16:23:55 -05:00
for ( unsigned int idx = 0 ; idx < MAX_NUMBER_OF_GRIDS ; ++ idx )
{
for ( unsigned int j = 0 ; j < MAX_NUMBER_OF_GRIDS ; ++ j )
{
//z code
GridMaps [ idx ][ j ] = NULL ;
setNGrid ( NULL , idx , j );
}
}
}
// Template specialization of utility methods
template < class T >
void Map :: AddToGrid ( T * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). template AddGridObject < T > ( obj , obj -> GetGUID ());
}
template <>
void Map :: AddToGrid ( Player * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject ( obj , obj -> GetGUID ());
}
template <>
void Map :: AddToGrid ( Corpse * obj , NGridType * grid , Cell const & cell )
{
// add to world object registry in grid
if ( obj -> GetType () != CORPSE_BONES )
{
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject ( obj , obj -> GetGUID ());
}
// add to grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject ( obj , obj -> GetGUID ());
}
}
template <>
void Map :: AddToGrid ( Creature * obj , NGridType * grid , Cell const & cell )
{
// add to world object registry in grid
2009-03-28 22:44:09 -06:00
if ( obj -> isWorldCreature () || obj -> IsTempWorldObject )
2008-10-02 16:23:55 -05:00
{
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject < Creature > ( obj , obj -> GetGUID ());
}
// add to grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject < Creature > ( obj , obj -> GetGUID ());
}
2009-02-17 21:50:18 -06:00
obj -> SetCurrentCell ( cell );
2008-10-02 16:23:55 -05:00
}
2009-02-26 16:29:55 -06:00
template <>
void Map :: AddToGrid ( DynamicObject * obj , NGridType * grid , Cell const & cell )
{
if ( obj -> isActiveObject ()) // only farsight
( * grid )( cell . CellX (), cell . CellY ()). AddWorldObject < DynamicObject > ( obj , obj -> GetGUID ());
else
( * grid )( cell . CellX (), cell . CellY ()). AddGridObject < DynamicObject > ( obj , obj -> GetGUID ());
}
2008-10-02 16:23:55 -05:00
template < class T >
void Map :: RemoveFromGrid ( T * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). template RemoveGridObject < T > ( obj , obj -> GetGUID ());
}
template <>
void Map :: RemoveFromGrid ( Player * obj , NGridType * grid , Cell const & cell )
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject ( obj , obj -> GetGUID ());
}
template <>
void Map :: RemoveFromGrid ( Corpse * obj , NGridType * grid , Cell const & cell )
{
// remove from world object registry in grid
if ( obj -> GetType () != CORPSE_BONES )
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject ( obj , obj -> GetGUID ());
}
// remove from grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject ( obj , obj -> GetGUID ());
}
}
template <>
void Map :: RemoveFromGrid ( Creature * obj , NGridType * grid , Cell const & cell )
{
// remove from world object registry in grid
2009-03-28 22:44:09 -06:00
if ( obj -> isWorldCreature () || obj -> IsTempWorldObject )
2008-10-02 16:23:55 -05:00
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject < Creature > ( obj , obj -> GetGUID ());
}
// remove from grid object store
else
{
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject < Creature > ( obj , obj -> GetGUID ());
}
}
2009-02-26 16:29:55 -06:00
template <>
void Map :: RemoveFromGrid ( DynamicObject * obj , NGridType * grid , Cell const & cell )
{
if ( obj -> isActiveObject ()) // only farsight
( * grid )( cell . CellX (), cell . CellY ()). RemoveWorldObject < DynamicObject > ( obj , obj -> GetGUID ());
else
( * grid )( cell . CellX (), cell . CellY ()). RemoveGridObject < DynamicObject > ( obj , obj -> GetGUID ());
}
2008-11-05 20:51:05 -06:00
template < class T >
2009-02-26 17:01:02 -06:00
void Map :: SwitchGridContainers ( T * obj , bool on )
2008-11-05 20:51:05 -06:00
{
2009-04-08 12:20:39 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
sLog . outError ( "Map::SwitchGridContainers: Object " I64FMT " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
return ;
}
Cell cell ( p );
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
DEBUG_LOG ( "Switch object " I64FMT " from grid[%u,%u] %u" , obj -> GetGUID (), cell . data . Part . grid_x , cell . data . Part . grid_y , on );
2008-11-14 20:40:35 -06:00
NGridType * ngrid = getNGrid ( cell . GridX (), cell . GridY ());
2009-04-08 12:20:39 -05:00
assert ( ngrid != NULL );
2008-11-14 20:40:35 -06:00
GridType & grid = ( * ngrid )( cell . CellX (), cell . CellY ());
2008-11-05 20:51:05 -06:00
2009-02-26 17:01:02 -06:00
if ( on )
2008-11-05 20:51:05 -06:00
{
2009-03-03 19:07:52 -06:00
grid . RemoveGridObject < T > ( obj , obj -> GetGUID ());
grid . AddWorldObject < T > ( obj , obj -> GetGUID ());
/*if(!grid.RemoveGridObject<T>(obj, obj->GetGUID())
2009-02-26 16:29:55 -06:00
|| !grid.AddWorldObject<T>(obj, obj->GetGUID()))
2008-11-14 20:40:35 -06:00
{
2009-02-26 16:29:55 -06:00
assert(false);
2009-03-03 19:07:52 -06:00
}*/
2009-02-17 21:50:18 -06:00
}
else
2008-11-05 20:51:05 -06:00
{
2009-03-03 19:07:52 -06:00
grid . RemoveWorldObject < T > ( obj , obj -> GetGUID ());
grid . AddGridObject < T > ( obj , obj -> GetGUID ());
/*if(!grid.RemoveWorldObject<T>(obj, obj->GetGUID())
2009-02-26 16:29:55 -06:00
|| !grid.AddGridObject<T>(obj, obj->GetGUID()))
2008-11-14 20:40:35 -06:00
{
2009-02-26 16:29:55 -06:00
assert(false);
2009-03-03 19:07:52 -06:00
}*/
2008-11-05 20:51:05 -06:00
}
2009-03-02 18:01:27 -06:00
obj -> IsTempWorldObject = on ;
2008-11-05 20:51:05 -06:00
}
template void Map :: SwitchGridContainers ( Creature * , bool );
2008-11-14 20:40:35 -06:00
template void Map :: SwitchGridContainers ( DynamicObject * , bool );
2008-11-05 20:51:05 -06:00
2008-10-02 16:23:55 -05:00
template < class T >
void Map :: DeleteFromWorld ( T * obj )
{
2008-11-07 09:36:46 -06:00
// Note: In case resurrectable corpse and pet its removed from global lists in own destructor
2008-10-02 16:23:55 -05:00
delete obj ;
}
2009-07-16 11:49:00 +08:00
template <>
void Map :: DeleteFromWorld ( Player * pl )
{
ObjectAccessor :: Instance (). RemoveObject ( pl );
delete pl ;
}
2008-10-02 16:23:55 -05:00
template < class T >
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( T * )
2008-10-02 16:23:55 -05:00
{
}
template <>
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( Player * obj )
2008-10-02 16:23:55 -05:00
{
2009-06-09 16:05:14 -05:00
//obj->m_Notified = false;
//obj->m_IsInNotifyList = false;
2009-02-17 21:50:18 -06:00
AddUnitToNotify ( obj );
2008-10-02 16:23:55 -05:00
}
template <>
2009-02-17 21:50:18 -06:00
void Map :: AddNotifier ( Creature * obj )
2008-10-02 16:23:55 -05:00
{
2009-06-09 16:05:14 -05:00
//obj->m_Notified = false;
//obj->m_IsInNotifyList = false;
2009-02-17 21:50:18 -06:00
AddUnitToNotify ( obj );
2008-10-02 16:23:55 -05:00
}
void
Map :: EnsureGridCreated ( const GridPair & p )
{
if ( ! getNGrid ( p . x_coord , p . y_coord ))
{
Guard guard ( * this );
if ( ! getNGrid ( p . x_coord , p . y_coord ))
{
2009-08-01 00:20:32 +02:00
sLog . outDebug ( "Creating grid[%u,%u] for map %u instance %u" , p . x_coord , p . y_coord , GetId (), i_InstanceId );
2009-04-12 16:44:44 -05:00
2008-10-02 16:23:55 -05:00
setNGrid ( new NGridType ( p . x_coord * MAX_NUMBER_OF_GRIDS + p . y_coord , p . x_coord , p . y_coord , i_gridExpiry , sWorld . getConfig ( CONFIG_GRID_UNLOAD )),
p . x_coord , p . y_coord );
// build a linkage between this map and NGridType
buildNGridLinkage ( getNGrid ( p . x_coord , p . y_coord ));
getNGrid ( p . x_coord , p . y_coord ) -> SetGridState ( GRID_STATE_IDLE );
//z coord
int gx = 63 - p . x_coord ;
int gy = 63 - p . y_coord ;
if ( ! GridMaps [ gx ][ gy ])
2009-04-02 16:33:35 -06:00
LoadMapAndVMap ( gx , gy );
2008-10-02 16:23:55 -05:00
}
}
}
void
2009-04-02 16:33:35 -06:00
Map :: EnsureGridLoadedAtEnter ( const Cell & cell , Player * player )
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:33:35 -06:00
NGridType * grid ;
2008-10-02 16:23:55 -05:00
2009-04-02 16:33:35 -06:00
if ( EnsureGridLoaded ( cell ))
2008-10-02 16:23:55 -05:00
{
2009-04-02 16:33:35 -06:00
grid = getNGrid ( cell . GridX (), cell . GridY ());
2009-02-25 20:52:20 -06:00
if ( player )
2008-10-02 16:23:55 -05:00
{
player -> SendDelayResponse ( MAX_GRID_LOAD_TIME );
2009-08-01 00:20:32 +02:00
DEBUG_LOG ( "Player %s enter cell[%u,%u] triggers of loading grid[%u,%u] on map %u" , player -> GetName (), cell . CellX (), cell . CellY (), cell . GridX (), cell . GridY (), GetId ());
2008-10-02 16:23:55 -05:00
}
else
{
2009-08-01 00:20:32 +02:00
DEBUG_LOG ( "Active object nearby triggers of loading grid [%u,%u] on map %u" , cell . GridX (), cell . GridY (), GetId ());
2008-10-02 16:23:55 -05:00
}
2009-04-02 16:33:35 -06:00
ResetGridExpiry ( * getNGrid ( cell . GridX (), cell . GridY ()), 0.1f );
grid -> SetGridState ( GRID_STATE_ACTIVE );
}
else
grid = getNGrid ( cell . GridX (), cell . GridY ());
if ( player )
AddToGrid ( player , grid , cell );
}
bool Map :: EnsureGridLoaded ( const Cell & cell )
{
EnsureGridCreated ( GridPair ( cell . GridX (), cell . GridY ()));
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
if ( ! isGridObjectDataLoaded ( cell . GridX (), cell . GridY ()) )
{
2009-08-01 00:20:32 +02:00
sLog . outDebug ( "Loading grid[%u,%u] for map %u instance %u" , cell . GridX (), cell . GridY (), GetId (), i_InstanceId );
2009-04-14 18:24:35 -05:00
2008-10-02 16:23:55 -05:00
ObjectGridLoader loader ( * grid , this , cell );
loader . LoadN ();
// Add resurrectable corpses to world object list in grid
ObjectAccessor :: Instance (). AddCorpsesToGrid ( GridPair ( cell . GridX (), cell . GridY ()),( * grid )( cell . CellX (), cell . CellY ()), this );
2009-04-02 16:33:35 -06:00
setGridObjectDataLoaded ( true , cell . GridX (), cell . GridY ());
return true ;
2008-10-02 16:23:55 -05:00
}
2009-04-02 16:33:35 -06:00
return false ;
2008-10-02 16:23:55 -05:00
}
2008-12-20 14:10:53 -06:00
void Map :: LoadGrid ( float x , float y )
{
CellPair pair = Trinity :: ComputeCellPair ( x , y );
Cell cell ( pair );
2009-02-26 16:29:55 -06:00
EnsureGridLoaded ( cell );
2008-12-20 14:10:53 -06:00
}
2008-10-02 16:23:55 -05:00
bool Map :: Add ( Player * player )
{
2009-08-01 00:20:32 +02:00
// Check if we are adding to correct map
assert ( player -> GetMap () == this );
2008-10-02 16:23:55 -05:00
// update player state for other player and visa-versa
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
Cell cell ( p );
2009-04-02 16:33:35 -06:00
EnsureGridLoadedAtEnter ( cell , player );
2008-10-02 16:23:55 -05:00
player -> AddToWorld ();
SendInitSelf ( player );
SendInitTransports ( player );
2009-03-27 09:06:26 -06:00
player -> m_clientGUIDs . clear ();
2009-04-08 17:23:57 -05:00
//AddNotifier(player);
2008-10-02 16:23:55 -05:00
return true ;
}
template < class T >
void
Map :: Add ( T * obj )
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
2009-05-31 16:15:12 -05:00
sLog . outError ( "Map::Add: Object " UI64FMTD " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
2008-10-02 16:23:55 -05:00
return ;
}
2009-07-16 11:49:00 +08:00
obj -> SetMap ( this );
2008-10-02 16:23:55 -05:00
Cell cell ( p );
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
2009-04-02 16:33:35 -06:00
EnsureGridLoadedAtEnter ( cell );
2009-02-25 20:52:20 -06:00
else
EnsureGridCreated ( GridPair ( cell . GridX (), cell . GridY ()));
2008-10-02 16:23:55 -05:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
AddToGrid ( obj , grid , cell );
obj -> AddToWorld ();
2009-04-06 21:14:51 +02:00
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
AddToActive ( obj );
2008-10-02 16:23:55 -05:00
DEBUG_LOG ( "Object %u enters grid[%u,%u]" , GUID_LOPART ( obj -> GetGUID ()), cell . GridX (), cell . GridY ());
2009-04-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
//if(obj->GetTypeId() != TYPEID_UNIT)
2009-05-01 11:26:19 -05:00
UpdateObjectVisibility ( obj , cell , p );
AddNotifier ( obj );
2008-10-02 16:23:55 -05:00
}
2009-05-27 09:09:22 -05:00
void Map :: MessageBroadcast ( Player * player , WorldPacket * msg , bool to_self )
2008-10-02 16:23:55 -05:00
{
2009-06-07 17:38:08 -05:00
Trinity :: MessageDistDeliverer post_man ( player , msg , World :: GetMaxVisibleDistance (), to_self );
2009-04-20 15:07:45 -05:00
VisitWorld ( player -> GetPositionX (), player -> GetPositionY (), World :: GetMaxVisibleDistance (), post_man );
2008-10-02 16:23:55 -05:00
}
2009-05-27 09:09:22 -05:00
void Map :: MessageBroadcast ( WorldObject * obj , WorldPacket * msg )
2008-10-02 16:23:55 -05:00
{
2009-06-07 17:38:08 -05:00
Trinity :: MessageDistDeliverer post_man ( obj , msg , World :: GetMaxVisibleDistance ());
2009-04-20 15:07:45 -05:00
VisitWorld ( obj -> GetPositionX (), obj -> GetPositionY (), World :: GetMaxVisibleDistance (), post_man );
2008-10-02 16:23:55 -05:00
}
2009-05-27 09:09:22 -05:00
void Map :: MessageDistBroadcast ( Player * player , WorldPacket * msg , float dist , bool to_self , bool own_team_only )
2008-10-02 16:23:55 -05:00
{
2009-06-07 17:38:08 -05:00
Trinity :: MessageDistDeliverer post_man ( player , msg , dist , to_self , own_team_only );
2009-05-27 09:09:22 -05:00
VisitWorld ( player -> GetPositionX (), player -> GetPositionY (), dist , post_man );
2008-10-02 16:23:55 -05:00
}
2009-05-27 09:09:22 -05:00
void Map :: MessageDistBroadcast ( WorldObject * obj , WorldPacket * msg , float dist )
2008-10-02 16:23:55 -05:00
{
2009-06-07 17:38:08 -05:00
Trinity :: MessageDistDeliverer post_man ( obj , msg , dist );
2009-05-27 09:09:22 -05:00
VisitWorld ( obj -> GetPositionX (), obj -> GetPositionY (), dist , post_man );
2008-10-02 16:23:55 -05:00
}
bool Map :: loaded ( const GridPair & p ) const
{
return ( getNGrid ( p . x_coord , p . y_coord ) && isGridObjectDataLoaded ( p . x_coord , p . y_coord ) );
}
2008-12-27 11:26:22 -06:00
void Map :: RelocationNotify ()
2008-11-21 15:41:18 -06:00
{
2009-06-09 16:05:14 -05:00
i_lock = true ;
2009-01-05 16:03:45 -06:00
//Notify
2009-04-02 20:27:10 -06:00
for ( std :: vector < Unit *>:: iterator iter = i_unitsToNotify . begin (); iter != i_unitsToNotify . end (); ++ iter )
2008-11-29 13:08:23 -06:00
{
2009-04-02 20:27:10 -06:00
Unit * unit = * iter ;
2009-06-09 16:05:14 -05:00
if ( ! unit )
continue ;
unit -> m_NotifyListPos = - 1 ;
if ( unit -> m_Notified )
2009-01-05 16:03:45 -06:00
continue ;
2008-11-29 23:07:23 -06:00
unit -> m_Notified = true ;
2009-01-05 16:03:45 -06:00
2009-04-18 11:03:27 -05:00
float dist = abs ( unit -> GetPositionX () - unit -> oldX ) + abs ( unit -> GetPositionY () - unit -> oldY );
if ( dist > 10.0f )
{
Trinity :: VisibleChangesNotifier notifier ( * unit );
VisitWorld ( unit -> oldX , unit -> oldY , World :: GetMaxVisibleDistance (), notifier );
dist = 0 ;
}
2008-11-29 23:07:23 -06:00
if ( unit -> GetTypeId () == TYPEID_PLAYER )
2008-12-19 23:31:02 -06:00
{
Trinity :: PlayerRelocationNotifier notifier ( * (( Player * ) unit ));
2009-06-09 16:05:14 -05:00
//if(((Player*)unit)->m_seer != unit)
2009-05-26 15:34:55 -05:00
VisitAll ((( Player * ) unit ) -> m_seer -> GetPositionX (), (( Player * ) unit ) -> m_seer -> GetPositionY (), World :: GetMaxVisibleDistance () + dist , notifier );
2009-06-09 16:05:14 -05:00
//else
//VisitAll(((Player*)unit)->GetPositionX(), ((Player*)unit)->GetPositionY(), World::GetMaxVisibleDistance() + dist, notifier);
2009-03-08 11:11:21 -06:00
notifier . Notify ();
2008-12-19 23:31:02 -06:00
}
2008-11-29 13:08:23 -06:00
else
2008-12-19 23:31:02 -06:00
{
Trinity :: CreatureRelocationNotifier notifier ( * (( Creature * ) unit ));
2009-04-18 11:03:27 -05:00
VisitAll ( unit -> GetPositionX (), unit -> GetPositionY (), World :: GetMaxVisibleDistance () + dist , notifier );
2008-12-19 23:31:02 -06:00
}
2008-11-29 13:08:23 -06:00
}
2009-04-04 13:11:03 -06:00
for ( std :: vector < Unit *>:: iterator iter = i_unitsToNotify . begin (); iter != i_unitsToNotify . end (); ++ iter )
2009-06-09 19:53:25 -05:00
if ( * iter )
( * iter ) -> m_Notified = false ;
2008-11-29 13:08:23 -06:00
i_unitsToNotify . clear ();
2009-06-09 16:05:14 -05:00
i_lock = false ;
if ( ! i_unitsToNotifyBacklog . empty ())
{
i_unitsToNotify . insert ( i_unitsToNotify . end (), i_unitsToNotifyBacklog . begin (), i_unitsToNotifyBacklog . end ());
i_unitsToNotifyBacklog . clear ();
}
2009-04-02 20:27:10 -06:00
}
2009-01-05 16:03:45 -06:00
2009-04-02 20:27:10 -06:00
void Map :: AddUnitToNotify ( Unit * u )
{
2009-06-10 09:53:21 -05:00
if ( u -> m_NotifyListPos < 0 && u -> IsInWorld ())
2009-06-09 16:05:14 -05:00
{
u -> oldX = u -> GetPositionX ();
u -> oldY = u -> GetPositionY ();
2009-04-02 20:27:10 -06:00
2009-06-09 16:05:14 -05:00
if ( i_lock )
{
u -> m_NotifyListPos = i_unitsToNotifyBacklog . size ();
i_unitsToNotifyBacklog . push_back ( u );
}
else
{
u -> m_NotifyListPos = i_unitsToNotify . size ();
i_unitsToNotify . push_back ( u );
}
}
}
2009-04-02 20:27:10 -06:00
2009-06-09 16:05:14 -05:00
void Map :: RemoveUnitFromNotify ( int32 slot )
{
2009-04-02 20:27:10 -06:00
if ( i_lock )
2009-06-09 16:05:14 -05:00
{
assert ( slot < i_unitsToNotifyBacklog . size ());
i_unitsToNotifyBacklog [ slot ] = NULL ;
}
2009-04-02 20:27:10 -06:00
else
2009-06-09 16:05:14 -05:00
{
assert ( slot < i_unitsToNotify . size ());
i_unitsToNotify [ slot ] = NULL ;
}
2008-12-27 11:26:22 -06:00
}
2008-11-29 13:08:23 -06:00
2008-12-27 11:26:22 -06:00
void Map :: Update ( const uint32 & t_diff )
{
2009-06-17 18:37:53 -05:00
/// update players at tick
for ( m_mapRefIter = m_mapRefManager . begin (); m_mapRefIter != m_mapRefManager . end (); ++ m_mapRefIter )
{
Player * plr = m_mapRefIter -> getSource ();
if ( plr && plr -> IsInWorld ())
plr -> Update ( t_diff );
}
/// update active cells around players and active objects
2008-11-21 15:41:18 -06:00
resetMarkedCells ();
2008-11-23 11:01:26 -06:00
Trinity :: ObjectUpdater updater ( t_diff );
// for creature
TypeContainerVisitor < Trinity :: ObjectUpdater , GridTypeMapContainer > grid_object_update ( updater );
// for pets
TypeContainerVisitor < Trinity :: ObjectUpdater , WorldTypeMapContainer > world_object_update ( updater );
2009-02-25 20:52:20 -06:00
// the player iterator is stored in the map object
// to make sure calls to Map::Remove don't invalidate it
for ( m_mapRefIter = m_mapRefManager . begin (); m_mapRefIter != m_mapRefManager . end (); ++ m_mapRefIter )
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
Player * plr = m_mapRefIter -> getSource ();
if ( ! plr -> IsInWorld ())
continue ;
CellPair standing_cell ( Trinity :: ComputeCellPair ( plr -> GetPositionX (), plr -> GetPositionY ()));
2008-11-23 11:01:26 -06:00
// Check for correctness of standing_cell, it also avoids problems with update_cell
if ( standing_cell . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
continue ;
// the overloaded operators handle range checking
// so ther's no need for range checking inside the loop
CellPair begin_cell ( standing_cell ), end_cell ( standing_cell );
begin_cell << 1 ; begin_cell -= 1 ; // upper left
end_cell >> 1 ; end_cell += 1 ; // lower right
for ( uint32 x = begin_cell . x_coord ; x <= end_cell . x_coord ; ++ x )
{
for ( uint32 y = begin_cell . y_coord ; y <= end_cell . y_coord ; ++ y )
{
// marked cells are those that have been visited
// don't visit the same cell twice
uint32 cell_id = ( y * TOTAL_NUMBER_OF_CELLS_PER_MAP ) + x ;
if ( ! isCellMarked ( cell_id ))
{
markCell ( cell_id );
CellPair pair ( x , y );
Cell cell ( pair );
cell . data . Part . reserved = CENTER_DISTRICT ;
2008-12-27 11:26:22 -06:00
//cell.SetNoCreate();
2008-11-23 11:01:26 -06:00
CellLock < NullGuard > cell_lock ( cell , pair );
cell_lock -> Visit ( cell_lock , grid_object_update , * this );
cell_lock -> Visit ( cell_lock , world_object_update , * this );
}
}
}
2009-03-12 14:27:45 -06:00
2009-05-26 15:34:55 -05:00
if ( plr -> m_seer != plr && ! plr -> m_Vehicle )
AddUnitToNotify ( plr );
2009-02-25 20:52:20 -06:00
}
// non-player active objects
if ( ! m_activeNonPlayers . empty ())
{
for ( m_activeNonPlayersIter = m_activeNonPlayers . begin (); m_activeNonPlayersIter != m_activeNonPlayers . end (); )
{
// skip not in world
WorldObject * obj = * m_activeNonPlayersIter ;
// step before processing, in this case if Map::Remove remove next object we correctly
// step to next-next, and if we step to end() then newly added objects can wait next update.
++ m_activeNonPlayersIter ;
if ( ! obj -> IsInWorld ())
continue ;
2008-11-23 11:01:26 -06:00
2009-02-25 20:52:20 -06:00
CellPair standing_cell ( Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ()));
// Check for correctness of standing_cell, it also avoids problems with update_cell
if ( standing_cell . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || standing_cell . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
continue ;
// the overloaded operators handle range checking
// so ther's no need for range checking inside the loop
CellPair begin_cell ( standing_cell ), end_cell ( standing_cell );
begin_cell << 1 ; begin_cell -= 1 ; // upper left
end_cell >> 1 ; end_cell += 1 ; // lower right
2008-11-23 11:01:26 -06:00
2009-02-25 20:52:20 -06:00
for ( uint32 x = begin_cell . x_coord ; x <= end_cell . x_coord ; ++ x )
{
for ( uint32 y = begin_cell . y_coord ; y <= end_cell . y_coord ; ++ y )
{
// marked cells are those that have been visited
// don't visit the same cell twice
uint32 cell_id = ( y * TOTAL_NUMBER_OF_CELLS_PER_MAP ) + x ;
if ( ! isCellMarked ( cell_id ))
{
markCell ( cell_id );
CellPair pair ( x , y );
Cell cell ( pair );
cell . data . Part . reserved = CENTER_DISTRICT ;
//cell.SetNoCreate();
CellLock < NullGuard > cell_lock ( cell , pair );
cell_lock -> Visit ( cell_lock , grid_object_update , * this );
cell_lock -> Visit ( cell_lock , world_object_update , * this );
}
}
}
}
2008-11-21 15:41:18 -06:00
}
2008-11-18 19:40:06 -06:00
2009-04-21 11:18:46 -05:00
MoveAllCreaturesInMoveList ();
RemoveAllObjectsInRemoveList ();
2008-12-27 11:26:22 -06:00
2009-06-10 16:38:19 -05:00
m_notifyTimer . Update ( t_diff );
2009-06-10 16:33:09 -05:00
if ( m_notifyTimer . Passed ())
{
m_notifyTimer . Reset ();
RelocationNotify ();
}
2008-10-02 16:23:55 -05:00
// Don't unload grids if it's battleground, since we may have manually added GOs,creatures, those doesn't load from DB at grid re-load !
// This isn't really bother us, since as soon as we have instanced BG-s, the whole map unloads as the BG gets ended
2009-07-31 10:48:34 +08:00
if ( ! IsBattleGroundOrArena ())
2008-10-02 16:23:55 -05:00
{
2009-07-31 10:48:34 +08:00
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end (); )
{
NGridType * grid = i -> getSource ();
GridInfo * info = i -> getSource () -> getGridInfoRef ();
++ i ; // The update might delete the map and we need the next map before the iterator gets invalid
assert ( grid -> GetGridState () >= 0 && grid -> GetGridState () < MAX_GRID_STATE );
si_GridStates [ grid -> GetGridState ()] -> Update ( * this , * grid , * info , grid -> getX (), grid -> getY (), t_diff );
}
2008-10-02 16:23:55 -05:00
}
2009-07-31 10:48:34 +08:00
///- Process necessary scripts
if ( ! m_scriptSchedule . empty ())
ScriptsProcess ();
2008-10-02 16:23:55 -05:00
}
void Map :: Remove ( Player * player , bool remove )
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
2009-07-16 11:49:00 +08:00
if ( remove )
player -> CleanupsBeforeDelete ();
2008-10-02 16:23:55 -05:00
// invalid coordinates
player -> RemoveFromWorld ();
if ( remove )
2009-08-01 21:39:33 +02:00
{
player -> ResetMap ();
2009-08-01 21:41:15 +02:00
DeleteFromWorld ( player );
2009-08-01 21:39:33 +02:00
}
2008-10-02 16:23:55 -05:00
return ;
}
Cell cell ( p );
if ( ! getNGrid ( cell . data . Part . grid_x , cell . data . Part . grid_y ) )
{
sLog . outError ( "Map::Remove() i_grids was NULL x:%d, y:%d" , cell . data . Part . grid_x , cell . data . Part . grid_y );
return ;
}
DEBUG_LOG ( "Remove player %s from grid[%u,%u]" , player -> GetName (), cell . GridX (), cell . GridY ());
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
2009-07-16 11:49:00 +08:00
if ( remove )
player -> CleanupsBeforeDelete ();
2008-10-02 16:23:55 -05:00
player -> RemoveFromWorld ();
RemoveFromGrid ( player , grid , cell );
SendRemoveTransports ( player );
2009-03-27 09:06:26 -06:00
UpdateObjectVisibility ( player , cell , p );
2008-10-02 16:23:55 -05:00
if ( remove )
DeleteFromWorld ( player );
}
bool Map :: RemoveBones ( uint64 guid , float x , float y )
{
if ( IsRemovalGrid ( x , y ))
{
Corpse * corpse = ObjectAccessor :: Instance (). GetObjectInWorld ( GetId (), x , y , guid , ( Corpse * ) NULL );
if ( corpse && corpse -> GetTypeId () == TYPEID_CORPSE && corpse -> GetType () == CORPSE_BONES )
corpse -> DeleteBonesFromWorld ();
else
return false ;
}
return true ;
}
template < class T >
void
Map :: Remove ( T * obj , bool remove )
{
2008-10-14 11:57:03 -05:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-10-02 16:23:55 -05:00
if ( p . x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p . y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP )
{
2008-12-12 11:21:28 -06:00
sLog . outError ( "Map::Remove: Object " I64FMT " have invalid coordinates X:%f Y:%f grid cell [%u:%u]" , obj -> GetGUID (), obj -> GetPositionX (), obj -> GetPositionY (), p . x_coord , p . y_coord );
2008-10-02 16:23:55 -05:00
return ;
}
Cell cell ( p );
if ( ! loaded ( GridPair ( cell . data . Part . grid_x , cell . data . Part . grid_y )) )
return ;
2008-12-12 11:21:28 -06:00
DEBUG_LOG ( "Remove object " I64FMT " from grid[%u,%u]" , obj -> GetGUID (), cell . data . Part . grid_x , cell . data . Part . grid_y );
2008-10-02 16:23:55 -05:00
NGridType * grid = getNGrid ( cell . GridX (), cell . GridY ());
assert ( grid != NULL );
obj -> RemoveFromWorld ();
2009-02-25 20:52:20 -06:00
if ( obj -> isActiveObject ())
RemoveFromActive ( obj );
2008-10-02 16:23:55 -05:00
RemoveFromGrid ( obj , grid , cell );
UpdateObjectVisibility ( obj , cell , p );
2009-07-16 11:49:00 +08:00
obj -> ResetMap ();
2008-10-02 16:23:55 -05:00
if ( remove )
{
// if option set then object already saved at this moment
2008-11-14 16:28:45 -06:00
if ( ! sWorld . getConfig ( CONFIG_SAVE_RESPAWN_TIME_IMMEDIATELY ))
2008-10-02 16:23:55 -05:00
obj -> SaveRespawnTime ();
DeleteFromWorld ( obj );
}
}
void
Map :: PlayerRelocation ( Player * player , float x , float y , float z , float orientation )
{
assert ( player );
2008-10-14 11:57:03 -05:00
CellPair old_val = Trinity :: ComputeCellPair ( player -> GetPositionX (), player -> GetPositionY ());
CellPair new_val = Trinity :: ComputeCellPair ( x , y );
2008-10-02 16:23:55 -05:00
Cell old_cell ( old_val );
Cell new_cell ( new_val );
new_cell |= old_cell ;
bool same_cell = ( new_cell == old_cell );
player -> Relocate ( x , y , z , orientation );
if ( old_cell . DiffGrid ( new_cell ) || old_cell . DiffCell ( new_cell ) )
{
DEBUG_LOG ( "Player %s relocation grid[%u,%u]cell[%u,%u]->grid[%u,%u]cell[%u,%u]" , player -> GetName (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . GridX (), new_cell . GridY (), new_cell . CellX (), new_cell . CellY ());
// update player position for group at taxi flight
if ( player -> GetGroup () && player -> isInFlight ())
player -> SetGroupUpdateFlag ( GROUP_UPDATE_FLAG_POSITION );
NGridType * oldGrid = getNGrid ( old_cell . GridX (), old_cell . GridY ());
RemoveFromGrid ( player , oldGrid , old_cell );
if ( ! old_cell . DiffGrid ( new_cell ) )
AddToGrid ( player , oldGrid , new_cell );
2009-02-17 21:50:18 -06:00
else
2009-04-02 16:33:35 -06:00
EnsureGridLoadedAtEnter ( new_cell , player );
2008-10-02 16:23:55 -05:00
}
2008-11-29 13:08:23 -06:00
AddUnitToNotify ( player );
2009-02-17 21:50:18 -06:00
2008-10-02 16:23:55 -05:00
NGridType * newGrid = getNGrid ( new_cell . GridX (), new_cell . GridY ());
if ( ! same_cell && newGrid -> GetGridState () != GRID_STATE_ACTIVE )
{
ResetGridExpiry ( * newGrid , 0.1f );
newGrid -> SetGridState ( GRID_STATE_ACTIVE );
}
}
void
Map :: CreatureRelocation ( Creature * creature , float x , float y , float z , float ang )
{
assert ( CheckGridIntegrity ( creature , false ));
Cell old_cell = creature -> GetCurrentCell ();
2008-10-14 11:57:03 -05:00
CellPair new_val = Trinity :: ComputeCellPair ( x , y );
2008-10-02 16:23:55 -05:00
Cell new_cell ( new_val );
// delay creature move for grid/cell to grid/cell moves
if ( old_cell . DiffCell ( new_cell ) || old_cell . DiffGrid ( new_cell ) )
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) added to moving list from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u]." , creature -> GetGUIDLow (), creature -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . GridX (), new_cell . GridY (), new_cell . CellX (), new_cell . CellY ());
#endif
AddCreatureToMoveList ( creature , x , y , z , ang );
// in diffcell/diffgrid case notifiers called at finishing move creature in Map::MoveAllCreaturesInMoveList
}
else
{
creature -> Relocate ( x , y , z , ang );
2008-11-29 13:08:23 -06:00
AddUnitToNotify ( creature );
2008-10-02 16:23:55 -05:00
}
2009-04-17 15:08:58 -05:00
if ( creature -> isVehicle ())
{
for ( SeatMap :: iterator itr = (( Vehicle * ) creature ) -> m_Seats . begin (); itr != (( Vehicle * ) creature ) -> m_Seats . end (); ++ itr )
2009-04-21 12:55:26 -05:00
if ( Unit * passenger = itr -> second . passenger )
{
if ( passenger -> GetTypeId () == TYPEID_PLAYER )
PlayerRelocation (( Player * ) passenger ,
x + passenger -> m_movementInfo . t_x ,
y + passenger -> m_movementInfo . t_y ,
z + passenger -> m_movementInfo . t_z ,
ang + passenger -> m_movementInfo . t_o );
2009-04-17 15:08:58 -05:00
else
2009-04-21 12:55:26 -05:00
CreatureRelocation (( Creature * ) passenger ,
x + passenger -> m_movementInfo . t_x ,
y + passenger -> m_movementInfo . t_y ,
z + passenger -> m_movementInfo . t_z ,
ang + passenger -> m_movementInfo . t_o );
}
2009-04-17 15:08:58 -05:00
}
2008-10-02 16:23:55 -05:00
assert ( CheckGridIntegrity ( creature , true ));
}
void Map :: AddCreatureToMoveList ( Creature * c , float x , float y , float z , float ang )
{
if ( ! c )
return ;
i_creaturesToMove [ c ] = CreatureMover ( x , y , z , ang );
}
void Map :: MoveAllCreaturesInMoveList ()
{
while ( ! i_creaturesToMove . empty ())
{
// get data and remove element;
CreatureMoveList :: iterator iter = i_creaturesToMove . begin ();
Creature * c = iter -> first ;
CreatureMover cm = iter -> second ;
i_creaturesToMove . erase ( iter );
// calculate cells
2008-10-14 11:57:03 -05:00
CellPair new_val = Trinity :: ComputeCellPair ( cm . x , cm . y );
2008-10-02 16:23:55 -05:00
Cell new_cell ( new_val );
// do move or do move to respawn or remove creature if previous all fail
if ( CreatureCellRelocation ( c , new_cell ))
{
// update pos
c -> Relocate ( cm . x , cm . y , cm . z , cm . ang );
2008-11-29 13:08:23 -06:00
//CreatureRelocationNotify(c,new_cell,new_cell.cellPair());
AddUnitToNotify ( c );
2008-10-02 16:23:55 -05:00
}
else
{
// if creature can't be move in new cell/grid (not loaded) move it to repawn cell/grid
// creature coordinates will be updated and notifiers send
if ( ! CreatureRespawnRelocation ( c ))
{
// ... or unload (if respawn grid also not loaded)
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u ) can't be move to unloaded respawn grid." , c -> GetGUIDLow (), c -> GetEntry ());
#endif
AddObjectToRemoveList ( c );
}
}
}
}
bool Map :: CreatureCellRelocation ( Creature * c , Cell new_cell )
{
Cell const & old_cell = c -> GetCurrentCell ();
if ( ! old_cell . DiffGrid ( new_cell ) ) // in same grid
{
// if in same cell then none do
if ( old_cell . DiffCell ( new_cell ))
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) moved in grid[%u,%u] from cell[%u,%u] to cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . CellX (), new_cell . CellY ());
#endif
2009-02-17 21:50:18 -06:00
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
2008-10-02 16:23:55 -05:00
}
else
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) move in same grid[%u,%u]cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY ());
#endif
}
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-02-25 20:52:20 -06:00
// in diff. grids but active creature
if ( c -> isActiveObject ())
{
2009-04-02 16:33:35 -06:00
EnsureGridLoadedAtEnter ( new_cell );
2009-02-25 20:52:20 -06:00
#ifdef TRINITY_DEBUG
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Active creature (GUID: %u Entry: %u) moved from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . GridX (), new_cell . GridY (), new_cell . CellX (), new_cell . CellY ());
#endif
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
return true ;
}
// in diff. loaded grid normal creature
2008-10-02 16:23:55 -05:00
if ( loaded ( GridPair ( new_cell . GridX (), new_cell . GridY ())))
{
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) moved from grid[%u,%u]cell[%u,%u] to grid[%u,%u]cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . GridX (), new_cell . GridY (), new_cell . CellX (), new_cell . CellY ());
#endif
RemoveFromGrid ( c , getNGrid ( old_cell . GridX (), old_cell . GridY ()), old_cell );
2009-03-04 09:33:15 -06:00
EnsureGridCreated ( GridPair ( new_cell . GridX (), new_cell . GridY ()));
AddToGrid ( c , getNGrid ( new_cell . GridX (), new_cell . GridY ()), new_cell );
2009-02-25 20:52:20 -06:00
return true ;
2008-10-02 16:23:55 -05:00
}
2009-02-25 20:52:20 -06:00
// fail to move: normal creature attempt move to unloaded grid
#ifdef TRINITY_DEBUG
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) attempt move from grid[%u,%u]cell[%u,%u] to unloaded grid[%u,%u]cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), old_cell . GridX (), old_cell . GridY (), old_cell . CellX (), old_cell . CellY (), new_cell . GridX (), new_cell . GridY (), new_cell . CellX (), new_cell . CellY ());
#endif
return false ;
2008-10-02 16:23:55 -05:00
}
bool Map :: CreatureRespawnRelocation ( Creature * c )
{
float resp_x , resp_y , resp_z , resp_o ;
c -> GetRespawnCoord ( resp_x , resp_y , resp_z , & resp_o );
2008-10-14 11:57:03 -05:00
CellPair resp_val = Trinity :: ComputeCellPair ( resp_x , resp_y );
2008-10-02 16:23:55 -05:00
Cell resp_cell ( resp_val );
c -> CombatStop ();
c -> GetMotionMaster () -> Clear ();
2008-10-14 11:57:03 -05:00
#ifdef TRINITY_DEBUG
2008-10-02 16:23:55 -05:00
if (( sLog . getLogFilter () & LOG_FILTER_CREATURE_MOVES ) == 0 )
sLog . outDebug ( "Creature (GUID: %u Entry: %u) will moved from grid[%u,%u]cell[%u,%u] to respawn grid[%u,%u]cell[%u,%u]." , c -> GetGUIDLow (), c -> GetEntry (), c -> GetCurrentCell (). GridX (), c -> GetCurrentCell (). GridY (), c -> GetCurrentCell (). CellX (), c -> GetCurrentCell (). CellY (), resp_cell . GridX (), resp_cell . GridY (), resp_cell . CellX (), resp_cell . CellY ());
#endif
// teleport it to respawn point (like normal respawn if player see)
if ( CreatureCellRelocation ( c , resp_cell ))
{
c -> Relocate ( resp_x , resp_y , resp_z , resp_o );
c -> GetMotionMaster () -> Initialize (); // prevent possible problems with default move generators
2008-11-29 13:08:23 -06:00
//CreatureRelocationNotify(c,resp_cell,resp_cell.cellPair());
AddUnitToNotify ( c );
2008-10-02 16:23:55 -05:00
return true ;
}
else
return false ;
}
2009-02-25 17:53:05 -06:00
bool Map :: UnloadGrid ( const uint32 & x , const uint32 & y , bool unloadAll )
2008-10-02 16:23:55 -05:00
{
NGridType * grid = getNGrid ( x , y );
assert ( grid != NULL );
{
2009-02-25 20:52:20 -06:00
if ( ! unloadAll && ActiveObjectsNearGrid ( x , y ) )
return false ;
2008-10-02 16:23:55 -05:00
2009-08-01 00:20:32 +02:00
sLog . outDebug ( "Unloading grid[%u,%u] for map %u" , x , y , GetId ());
2009-03-29 11:58:19 -06:00
2008-10-02 16:23:55 -05:00
ObjectGridUnloader unloader ( * grid );
2009-02-25 17:53:05 -06:00
if ( ! unloadAll )
{
// Finish creature moves, remove and delete all creatures with delayed remove before moving to respawn grids
// Must know real mob position before move
2009-03-29 11:58:19 -06:00
MoveAllCreaturesInMoveList ();
2008-10-02 16:23:55 -05:00
2009-02-25 17:53:05 -06:00
// move creatures to respawn grids if this is diff.grid or to remove list
unloader . MoveToRespawnN ();
2008-10-02 16:23:55 -05:00
2009-02-25 17:53:05 -06:00
// Finish creature moves, remove and delete all creatures with delayed remove before unload
2009-03-29 11:58:19 -06:00
MoveAllCreaturesInMoveList ();
2009-02-25 17:53:05 -06:00
}
2008-10-02 16:23:55 -05:00
2009-03-29 11:58:19 -06:00
ObjectGridCleaner cleaner ( * grid );
cleaner . CleanN ();
RemoveAllObjectsInRemoveList ();
2008-10-02 16:23:55 -05:00
unloader . UnloadN ();
2009-03-29 11:58:19 -06:00
assert ( i_objectsToRemove . empty ());
2009-02-25 17:53:05 -06:00
delete grid ;
2008-10-02 16:23:55 -05:00
setNGrid ( NULL , x , y );
}
int gx = 63 - x ;
int gy = 63 - y ;
// delete grid map, but don't delete if it is from parent map (and thus only reference)
//+++if (GridMaps[gx][gy]) don't check for GridMaps[gx][gy], we might have to unload vmaps
{
if ( i_InstanceId == 0 )
{
2009-03-09 18:06:19 -06:00
if ( GridMaps [ gx ][ gy ])
{
GridMaps [ gx ][ gy ] -> unloadData ();
delete GridMaps [ gx ][ gy ];
}
2008-12-12 11:21:28 -06:00
// x and y are swapped
2008-10-02 16:23:55 -05:00
VMAP :: VMapFactory :: createOrGetVMapManager () -> unloadMap ( GetId (), gy , gx );
}
else
2009-07-16 11:49:00 +08:00
(( MapInstanced * ) m_parentMap ) -> RemoveGridMapReference ( GridPair ( gx , gy ));
2008-10-02 16:23:55 -05:00
GridMaps [ gx ][ gy ] = NULL ;
}
2009-08-01 00:20:32 +02:00
DEBUG_LOG ( "Unloading grid[%u,%u] for map %u finished" , x , y , GetId ());
2008-10-02 16:23:55 -05:00
return true ;
}
2009-02-25 17:53:05 -06:00
void Map :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
// clear all delayed moves, useless anyway do this moves before map unload.
i_creaturesToMove . clear ();
for ( GridRefManager < NGridType >:: iterator i = GridRefManager < NGridType >:: begin (); i != GridRefManager < NGridType >:: end (); )
{
NGridType & grid ( * i -> getSource ());
++ i ;
2009-02-25 17:53:05 -06:00
UnloadGrid ( grid . getX (), grid . getY (), true ); // deletes the grid and removes it from the GridRefManager
2008-10-02 16:23:55 -05:00
}
}
2009-03-09 18:06:19 -06:00
//*****************************
// Grid function
//*****************************
GridMap :: GridMap ()
2008-10-02 16:23:55 -05:00
{
2009-03-09 18:06:19 -06:00
m_flags = 0 ;
// Area data
m_gridArea = 0 ;
m_area_map = NULL ;
// Height level data
m_gridHeight = INVALID_HEIGHT ;
m_gridGetHeight = & GridMap :: getHeightFromFlat ;
m_V9 = NULL ;
m_V8 = NULL ;
// Liquid data
m_liquidType = 0 ;
m_liquid_offX = 0 ;
m_liquid_offY = 0 ;
m_liquid_width = 0 ;
m_liquid_height = 0 ;
m_liquidLevel = INVALID_HEIGHT ;
m_liquid_type = NULL ;
m_liquid_map = NULL ;
}
2008-10-02 16:23:55 -05:00
2009-03-09 18:06:19 -06:00
GridMap ::~ GridMap ()
{
unloadData ();
}
bool GridMap :: loadData ( char * filename )
{
// Unload old data if exist
unloadData ();
2008-10-02 16:23:55 -05:00
2009-03-09 18:06:19 -06:00
map_fileheader header ;
// Not return error if file not found
FILE * in = fopen ( filename , "rb" );
if ( ! in )
return true ;
fread ( & header , sizeof ( header ), 1 , in );
2009-03-12 14:49:12 -06:00
if ( header . mapMagic == uint32 ( MAP_MAGIC ) &&
header . versionMagic == uint32 ( MAP_VERSION_MAGIC ))
2009-03-09 18:06:19 -06:00
{
// loadup area data
if ( header . areaMapOffset && ! loadAreaData ( in , header . areaMapOffset , header . areaMapSize ))
{
sLog . outError ( "Error loading map area data \n " );
fclose ( in );
return false ;
}
// loadup height data
if ( header . heightMapOffset && ! loadHeihgtData ( in , header . heightMapOffset , header . heightMapSize ))
{
sLog . outError ( "Error loading map height data \n " );
fclose ( in );
return false ;
}
// loadup liquid data
if ( header . liquidMapOffset && ! loadLiquidData ( in , header . liquidMapOffset , header . liquidMapSize ))
{
sLog . outError ( "Error loading map liquids data \n " );
fclose ( in );
return false ;
}
fclose ( in );
return true ;
}
sLog . outError ( "Map file '%s' is non-compatible version (outdated?). Please, create new using ad.exe program." , filename );
fclose ( in );
return false ;
}
2008-10-02 16:23:55 -05:00
2009-03-09 18:06:19 -06:00
void GridMap :: unloadData ()
{
if ( m_area_map ) delete [] m_area_map ;
if ( m_V9 ) delete [] m_V9 ;
if ( m_V8 ) delete [] m_V8 ;
if ( m_liquid_type ) delete [] m_liquid_type ;
if ( m_liquid_map ) delete [] m_liquid_map ;
m_area_map = NULL ;
m_V9 = NULL ;
m_V8 = NULL ;
m_liquid_type = NULL ;
m_liquid_map = NULL ;
m_gridGetHeight = & GridMap :: getHeightFromFlat ;
}
2008-10-02 16:23:55 -05:00
2009-03-09 18:06:19 -06:00
bool GridMap :: loadAreaData ( FILE * in , uint32 offset , uint32 size )
{
map_areaHeader header ;
fseek ( in , offset , SEEK_SET );
fread ( & header , sizeof ( header ), 1 , in );
2009-03-12 14:49:12 -06:00
if ( header . fourcc != uint32 ( MAP_AREA_MAGIC ))
2009-03-09 18:06:19 -06:00
return false ;
m_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
{
m_area_map = new uint16 [ 16 * 16 ];
fread ( m_area_map , sizeof ( uint16 ), 16 * 16 , in );
}
return true ;
}
bool GridMap :: loadHeihgtData ( FILE * in , uint32 offset , uint32 size )
{
map_heightHeader header ;
fseek ( in , offset , SEEK_SET );
fread ( & header , sizeof ( header ), 1 , in );
2009-07-31 11:25:28 +08:00
if ( header . fourcc != uint32 ( MAP_HEIGHT_MAGIC ))
2009-03-09 18:06:19 -06:00
return false ;
m_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 ];
fread ( m_uint16_V9 , sizeof ( uint16 ), 129 * 129 , in );
fread ( m_uint16_V8 , sizeof ( uint16 ), 128 * 128 , in );
m_gridIntHeightMultiplier = ( header . gridMaxHeight - header . gridHeight ) / 65535 ;
m_gridGetHeight = & GridMap :: getHeightFromUint16 ;
}
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 ];
fread ( m_uint8_V9 , sizeof ( uint8 ), 129 * 129 , in );
fread ( m_uint8_V8 , sizeof ( uint8 ), 128 * 128 , in );
m_gridIntHeightMultiplier = ( header . gridMaxHeight - header . gridHeight ) / 255 ;
m_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 ];
fread ( m_V9 , sizeof ( float ), 129 * 129 , in );
fread ( m_V8 , sizeof ( float ), 128 * 128 , in );
m_gridGetHeight = & GridMap :: getHeightFromFloat ;
2009-02-17 15:42:04 -06:00
}
2009-03-09 18:06:19 -06:00
}
else
m_gridGetHeight = & GridMap :: getHeightFromFlat ;
return true ;
}
bool GridMap :: loadLiquidData ( FILE * in , uint32 offset , uint32 size )
{
map_liquidHeader header ;
fseek ( in , offset , SEEK_SET );
fread ( & header , sizeof ( header ), 1 , in );
2009-03-12 14:49:12 -06:00
if ( header . fourcc != uint32 ( MAP_LIQUID_MAGIC ))
2009-03-09 18:06:19 -06:00
return false ;
m_liquidType = header . liquidType ;
m_liquid_offX = header . offsetX ;
m_liquid_offY = header . offsetY ;
m_liquid_width = header . width ;
m_liquid_height = header . height ;
m_liquidLevel = header . liquidLevel ;
2009-07-31 11:25:28 +08:00
if ( ! ( header . flags & MAP_LIQUID_NO_TYPE ))
2009-03-09 18:06:19 -06:00
{
m_liquid_type = new uint8 [ 16 * 16 ];
fread ( m_liquid_type , sizeof ( uint8 ), 16 * 16 , in );
}
2009-07-31 11:25:28 +08:00
if ( ! ( header . flags & MAP_LIQUID_NO_HEIGHT ))
2009-03-09 18:06:19 -06:00
{
m_liquid_map = new float [ m_liquid_width * m_liquid_height ];
fread ( m_liquid_map , sizeof ( float ), m_liquid_width * m_liquid_height , in );
}
return true ;
}
uint16 GridMap :: getArea ( float x , float y )
{
if ( ! m_area_map )
return m_gridArea ;
x = 16 * ( 32 - x / SIZE_OF_GRIDS );
y = 16 * ( 32 - y / SIZE_OF_GRIDS );
int lx = ( int ) x & 15 ;
int ly = ( int ) y & 15 ;
return m_area_map [ lx * 16 + ly ];
}
2009-05-03 22:21:46 -05:00
float GridMap :: getHeightFromFlat ( float /*x*/ , float /*y*/ ) const
2009-03-09 18:06:19 -06:00
{
return m_gridHeight ;
}
float GridMap :: getHeightFromFloat ( float x , float y ) const
{
if ( ! m_V8 || ! m_V9 )
return m_gridHeight ;
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
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 );
// Height stored as: h5 - its v8 grid, h1-h4 - its v9 grid
// +--------------> X
// | h1-------h2 Coordinates is:
// | | \ 1 / | h1 0,0
// | | \ / | h2 0,1
// | | 2 h5 3 | h3 1,0
// | | / \ | h4 1,1
// | | / 4 \ | h5 1/2,1/2
// | 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
float a , b , c ;
// Select triangle:
if ( x + y < 1 )
{
if ( x > y )
{
// 1 triangle (h1, h2, h5 points)
float h1 = m_V9 [( x_int ) * 129 + y_int ];
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)
float h3 = m_V9 [( x_int ) * 129 + y_int + 1 ];
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 ;
}
float GridMap :: getHeightFromUint8 ( float x , float y ) const
{
if ( ! m_uint8_V8 || ! m_uint8_V9 )
return m_gridHeight ;
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
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 );
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
return ( float )(( a * x ) + ( b * y ) + c ) * m_gridIntHeightMultiplier + m_gridHeight ;
}
float GridMap :: getHeightFromUint16 ( float x , float y ) const
{
if ( ! m_uint16_V8 || ! m_uint16_V9 )
return m_gridHeight ;
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
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 );
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
return ( float )(( a * x ) + ( b * y ) + c ) * m_gridIntHeightMultiplier + m_gridHeight ;
}
float GridMap :: getLiquidLevel ( float x , float y )
{
if ( ! m_liquid_map )
return m_liquidLevel ;
x = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
y = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
int cx_int = (( int ) x & ( MAP_RESOLUTION - 1 )) - m_liquid_offY ;
int cy_int = (( int ) y & ( MAP_RESOLUTION - 1 )) - m_liquid_offX ;
if ( cx_int < 0 || cx_int >= m_liquid_height )
return INVALID_HEIGHT ;
if ( cy_int < 0 || cy_int >= m_liquid_width )
return INVALID_HEIGHT ;
return m_liquid_map [ cx_int * m_liquid_width + cy_int ];
}
uint8 GridMap :: getTerrainType ( float x , float y )
{
if ( ! m_liquid_type )
return m_liquidType ;
x = 16 * ( 32 - x / SIZE_OF_GRIDS );
y = 16 * ( 32 - y / SIZE_OF_GRIDS );
int lx = ( int ) x & 15 ;
int ly = ( int ) y & 15 ;
return m_liquid_type [ lx * 16 + ly ];
}
// Get water state on map
inline ZLiquidStatus GridMap :: getLiquidStatus ( float x , float y , float z , uint8 ReqLiquidType , LiquidData * data )
{
// Check water type (if no water return)
if ( ! m_liquid_type && ! m_liquidType )
return LIQUID_MAP_NO_WATER ;
// Get cell
float cx = MAP_RESOLUTION * ( 32 - x / SIZE_OF_GRIDS );
float cy = MAP_RESOLUTION * ( 32 - y / SIZE_OF_GRIDS );
int x_int = ( int ) cx & ( MAP_RESOLUTION - 1 );
int y_int = ( int ) cy & ( MAP_RESOLUTION - 1 );
// Check water type in cell
uint8 type = m_liquid_type ? m_liquid_type [( x_int >> 3 ) * 16 + ( y_int >> 3 )] : m_liquidType ;
if ( type == 0 )
return LIQUID_MAP_NO_WATER ;
// Check req liquid type mask
if ( ReqLiquidType && ! ( ReqLiquidType & type ))
return LIQUID_MAP_NO_WATER ;
// Check water level:
// Check water height map
int lx_int = x_int - m_liquid_offY ;
int ly_int = y_int - m_liquid_offX ;
if ( lx_int < 0 || lx_int >= m_liquid_height )
return LIQUID_MAP_NO_WATER ;
if ( ly_int < 0 || ly_int >= m_liquid_width )
return LIQUID_MAP_NO_WATER ;
// Get water level
float liquid_level = m_liquid_map ? m_liquid_map [ lx_int * m_liquid_width + ly_int ] : m_liquidLevel ;
// Get ground level (sub 0.2 for fix some errors)
float ground_level = getHeight ( x , y );
// Check water level and ground level
if ( liquid_level < ground_level || z < ground_level - 2 )
return LIQUID_MAP_NO_WATER ;
// All ok in water -> store data
if ( data )
{
data -> type = type ;
data -> level = liquid_level ;
data -> depth_level = ground_level ;
}
// For speed check as int values
2009-03-12 14:49:12 -06:00
int delta = int (( liquid_level - z ) * 10 );
2009-03-09 18:06:19 -06:00
// Get position delta
if ( delta > 20 ) // Under water
return LIQUID_MAP_UNDER_WATER ;
if ( delta > 0 ) // In water
return LIQUID_MAP_IN_WATER ;
if ( delta > - 1 ) // Walk on water
return LIQUID_MAP_WATER_WALK ;
// Above water
return LIQUID_MAP_ABOVE_WATER ;
}
inline GridMap * Map :: GetGrid ( float x , float y )
{
// half opt method
int gx = ( int )( 32 - x / SIZE_OF_GRIDS ); //grid x
int gy = ( int )( 32 - y / SIZE_OF_GRIDS ); //grid y
// ensure GridMap is loaded
EnsureGridCreated ( GridPair ( 63 - gx , 63 - gy ));
return GridMaps [ gx ][ gy ];
}
float Map :: GetHeight ( float x , float y , float z , bool pUseVmaps ) const
{
// find raw .map surface under Z coordinates
float mapHeight ;
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
{
float _mapheight = 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
if ( z + 2.0f > _mapheight )
mapHeight = _mapheight ;
else
mapHeight = VMAP_INVALID_HEIGHT_VALUE ;
}
else
mapHeight = VMAP_INVALID_HEIGHT_VALUE ;
float vmapHeight ;
if ( pUseVmaps )
{
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
if ( vmgr -> isHeightCalcEnabled ())
{
// look from a bit higher pos to find the floor
vmapHeight = vmgr -> getHeight ( GetId (), x , y , z + 2.0f );
}
else
vmapHeight = VMAP_INVALID_HEIGHT_VALUE ;
}
else
vmapHeight = VMAP_INVALID_HEIGHT_VALUE ;
// mapHeight set for any above raw ground Z or <= INVALID_HEIGHT
// vmapheight set for any under Z value or <= INVALID_HEIGHT
if ( vmapHeight > INVALID_HEIGHT )
{
if ( mapHeight > INVALID_HEIGHT )
{
// we have mapheight and vmapheight and must select more appropriate
// we are already under the surface or vmap height above map heigt
// or if the distance of the vmap height is less the land height distance
if ( z < mapHeight || vmapHeight > mapHeight || fabs ( mapHeight - z ) > fabs ( vmapHeight - z ) )
return vmapHeight ;
else
return mapHeight ; // better use .map surface height
}
else
return vmapHeight ; // we have only vmapHeight (if have)
}
else
{
if ( ! pUseVmaps )
return mapHeight ; // explicitly use map data (if have)
else if ( mapHeight > INVALID_HEIGHT && ( z < mapHeight + 2 || z == MAX_HEIGHT ))
return mapHeight ; // explicitly use map data if original z < mapHeight but map found (z+2 > mapHeight)
else
return VMAP_INVALID_HEIGHT_VALUE ; // we not have any height
}
}
2009-01-16 19:05:30 -06:00
float Map :: GetVmapHeight ( float x , float y , float z , bool useMaps ) const
{
2009-02-09 08:16:34 -05:00
float mapHeight ;
float vmapHeight ;
if ( useMaps )
{
mapHeight = GetHeight ( x , y , z , false );
if ( fabs ( mapHeight - z ) < 0.1 )
return mapHeight ;
}
else
mapHeight = INVALID_HEIGHT ;
VMAP :: IVMapManager * vmgr = VMAP :: VMapFactory :: createOrGetVMapManager ();
if ( vmgr -> isLineOfSightCalcEnabled ())
bool result = vmgr -> getObjectHitPos ( GetId (), x , y , z + 2.0f , x , y , mapHeight , x , y , vmapHeight , 0 );
else
return INVALID_HEIGHT ;
return vmapHeight ;
2009-01-16 19:05:30 -06:00
}
2009-01-02 11:03:43 -06:00
uint16 Map :: GetAreaFlag ( float x , float y , float z ) const
2008-10-02 16:23:55 -05:00
{
2009-01-02 11:03:43 -06:00
uint16 areaflag ;
2009-03-09 18:06:19 -06:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
areaflag = gmap -> getArea ( x , y );
2008-10-02 16:23:55 -05:00
// this used while not all *.map files generated (instances)
else
2009-08-01 00:20:32 +02:00
areaflag = GetAreaFlagByMapId ( GetId ());
2009-01-02 11:03:43 -06:00
//FIXME: some hacks for areas above or underground for ground area
// required for area specific spells/etc, until map/vmap data
// not provided correct areaflag with this hacks
switch ( areaflag )
{
// Acherus: The Ebon Hold (Plaguelands: The Scarlet Enclave)
case 1984 : // Plaguelands: The Scarlet Enclave
case 2076 : // Death's Breach (Plaguelands: The Scarlet Enclave)
case 2745 : // The Noxious Pass (Plaguelands: The Scarlet Enclave)
if ( z > 350.0f ) areaflag = 2048 ; break ;
// Acherus: The Ebon Hold (Eastern Plaguelands)
case 856 : // The Noxious Glade (Eastern Plaguelands)
case 2456 : // Death's Breach (Eastern Plaguelands)
if ( z > 350.0f ) areaflag = 1950 ; break ;
2009-03-07 10:42:54 -06:00
// Dalaran
2009-04-07 19:39:25 -05:00
case 2492 : // Forlorn Woods (Crystalsong Forest)
2009-05-05 14:45:52 -05:00
if ( x > 5568.0f && x < 6116.0f && y > 282.0f && y < 982.0f && z > 563.0f )
{
// Krasus' Landing (Dalaran), fast check
if ( x > 5758.77f && x < 5869.03f && y < 510.46f )
{
// Krasus' Landing (Dalaran), with open east side
if ( y < 449.33f || ( x - 5813.9f ) * ( x - 5813.9f ) + ( y - 449.33f ) * ( y - 449.33f ) < 1864.0f )
{
areaflag = 2533 ; // Note: also 2633, possible one flight allowed and other not allowed case
break ;
}
}
// Dalaran
areaflag = 2153 ;
}
break ;
// The Violet Citadel (Dalaran) or Dalaran
case 2484 : // The Twilight Rivulet (Crystalsong Forest)
case 1593 : // Crystalsong Forest
// Dalaran
if ( x > 5568.0f && x < 6116.0f && y > 282.0f && y < 982.0f && z > 563.0f )
{
// The Violet Citadel (Dalaran), fast check
if ( x > 5721.1f && x < 5884.66f && y > 764.4f && y < 948.0f )
{
// The Violet Citadel (Dalaran)
if (( x - 5803.0f ) * ( x - 5803.0f ) + ( y - 846.18f ) * ( y - 846.18f ) < 6690.0f )
{
areaflag = 2696 ;
break ;
}
}
// Dalaran
areaflag = 2153 ;
}
break ;
// Vargoth's Retreat (Dalaran) or The Violet Citadel (Dalaran) or Dalaran
case 2504 : // Violet Stand (Crystalsong Forest)
// Dalaran
if ( x > 5568.0f && x < 6116.0f && y > 282.0f && y < 982.0f && z > 563.0f )
{
// The Violet Citadel (Dalaran), fast check
if ( x > 5721.1f && x < 5884.66f && y > 764.4f && y < 948.0f )
{
// Vargoth's Retreat (Dalaran), nice slow circle with upper limit
if ( z < 898.0f && ( x - 5765.0f ) * ( x - 5765.0f ) + ( y - 862.4f ) * ( y - 862.4f ) < 262.0f )
{
areaflag = 2748 ;
break ;
}
// The Violet Citadel (Dalaran)
if (( x - 5803.0f ) * ( x - 5803.0f ) + ( y - 846.18f ) * ( y - 846.18f ) < 6690.0f )
{
areaflag = 2696 ;
break ;
}
}
// Dalaran
areaflag = 2153 ;
}
break ;
2009-03-28 16:17:01 -06:00
// Maw of Neltharion (cave)
case 164 : // Dragonblight
case 1797 : // Obsidian Dragonshrine (Dragonblight)
case 1827 : // Wintergrasp
case 2591 : // The Cauldron of Flames (Wintergrasp)
if ( x > 4364.0f && x < 4632.0f && y > 1545.0f && y < 1886.0f && z < 200.0f ) areaflag = 1853 ; break ;
2009-04-07 19:39:25 -05:00
// Undercity (sewers enter and path)
case 179 : // Tirisfal Glades
if ( x > 1595.0f && x < 1699.0f && y > 535.0f && y < 643.5f && z < 30.5f ) areaflag = 685 ; break ;
// Undercity (Royal Quarter)
case 210 : // Silverpine Forest
case 316 : // The Shining Strand (Silverpine Forest)
case 438 : // Lordamere Lake (Silverpine Forest)
if ( x > 1237.0f && x < 1401.0f && y > 284.0f && y < 440.0f && z < - 40.0f ) areaflag = 685 ; break ;
// Undercity (cave and ground zone, part of royal quarter)
case 607 : // Ruins of Lordaeron (Tirisfal Glades)
// ground and near to ground (by city walls)
if ( z > 0.0f )
{
if ( x > 1510.0f && x < 1839.0f && y > 29.77f && y < 433.0f ) areaflag = 685 ;
}
// more wide underground, part of royal quarter
else
{
if ( x > 1299.0f && x < 1839.0f && y > 10.0f && y < 440.0f ) areaflag = 685 ;
}
break ;
2009-04-12 17:41:09 -05:00
// The Makers' Perch (ground) and Makers' Overlook (ground and cave)
case 1335 : // Sholazar Basin
// The Makers' Perch ground (fast box)
if ( x > 6100.0f && x < 6250.0f && y > 5650.0f && y < 5800.0f )
{
// nice slow circle
if (( x - 6183.0f ) * ( x - 6183.0f ) + ( y - 5717.0f ) * ( y - 5717.0f ) < 2500.0f )
areaflag = 2189 ;
}
// Makers' Overlook (ground and cave)
else if ( x > 5634.48f && x < 5774.53f && y < 3475.0f && z > 300.0f )
{
if ( y > 3380.26f || y > 3265.0f && z < 360.0f ) areaflag = 2187 ;
}
break ;
// The Makers' Perch (underground)
case 2147 : // The Stormwright's Shelf (Sholazar Basin)
if ( x > 6199.0f && x < 6283.0f && y > 5705.0f && y < 5817.0f && z < 38.0f ) areaflag = 2189 ; break ;
// Makers' Overlook (deep cave)
case 267 : // Icecrown
if ( x > 5684.0f && x < 5798.0f && y > 3035.0f && y < 3367.0f && z < 358.0f ) areaflag = 2187 ; break ;
// Wyrmrest Temple (Dragonblight)
case 1814 : // Path of the Titans (Dragonblight)
case 1897 : // The Dragon Wastes (Dragonblight)
// fast box
if ( x > 3400.0f && x < 3700.0f && y > 130.0f && y < 420.0f )
{
// nice slow circle
if (( x - 3546.87f ) * ( x - 3546.87f ) + ( y - 272.71f ) * ( y - 272.71f ) < 19600.0f ) areaflag = 1791 ;
}
break ;
2009-01-02 11:03:43 -06:00
}
return areaflag ;
2008-10-02 16:23:55 -05:00
}
uint8 Map :: GetTerrainType ( float x , float y ) const
{
2009-03-09 18:06:19 -06:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
return gmap -> getTerrainType ( x , y );
2008-10-02 16:23:55 -05:00
else
return 0 ;
}
2009-03-09 18:06:19 -06:00
ZLiquidStatus Map :: getLiquidStatus ( float x , float y , float z , uint8 ReqLiquidType , LiquidData * data ) const
2008-10-02 16:23:55 -05:00
{
2009-03-09 18:06:19 -06:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
return gmap -> getLiquidStatus ( x , y , z , ReqLiquidType , data );
else
return LIQUID_MAP_NO_WATER ;
}
2008-10-02 16:23:55 -05:00
2009-03-09 18:06:19 -06:00
float Map :: GetWaterLevel ( float x , float y ) const
{
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
return gmap -> getLiquidLevel ( x , y );
2008-10-02 16:23:55 -05:00
else
return 0 ;
}
2009-03-11 16:17:37 -06:00
uint32 Map :: GetAreaIdByAreaFlag ( uint16 areaflag , uint32 map_id )
2008-10-02 16:23:55 -05:00
{
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
if ( entry )
return entry -> ID ;
else
return 0 ;
}
2009-03-11 16:17:37 -06:00
uint32 Map :: GetZoneIdByAreaFlag ( uint16 areaflag , uint32 map_id )
2008-10-02 16:23:55 -05:00
{
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
if ( entry )
return ( entry -> zone != 0 ) ? entry -> zone : entry -> ID ;
else
return 0 ;
}
2009-03-11 16:17:37 -06:00
void Map :: GetZoneAndAreaIdByAreaFlag ( uint32 & zoneid , uint32 & areaid , uint16 areaflag , uint32 map_id )
{
AreaTableEntry const * entry = GetAreaEntryByAreaFlagAndMap ( areaflag , map_id );
areaid = entry ? entry -> ID : 0 ;
zoneid = entry ? (( entry -> zone != 0 ) ? entry -> zone : entry -> ID ) : 0 ;
}
2009-07-09 18:06:08 +02:00
bool Map :: IsInWater ( float x , float y , float pZ , float min_depth ) 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
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
{
LiquidData liquid_status ;
if ( getLiquidStatus ( x , y , pZ , MAP_ALL_LIQUIDS , & liquid_status ))
{
2009-07-09 18:06:08 +02:00
if ( liquid_status . level - liquid_status . depth_level > min_depth )
2009-03-09 18:06:19 -06:00
return true ;
}
}
return false ;
2008-10-02 16:23:55 -05:00
}
bool Map :: IsUnderWater ( float x , float y , float z ) const
{
2009-03-09 18:06:19 -06:00
if ( GridMap * gmap = const_cast < Map *> ( this ) -> GetGrid ( x , y ))
{
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
}
bool Map :: CheckGridIntegrity ( Creature * c , bool moved ) const
{
Cell const & cur_cell = c -> GetCurrentCell ();
2008-10-14 11:57:03 -05:00
CellPair xy_val = Trinity :: ComputeCellPair ( c -> GetPositionX (), c -> GetPositionY ());
2008-10-02 16:23:55 -05:00
Cell xy_cell ( xy_val );
if ( xy_cell != cur_cell )
{
2009-03-25 16:07:19 -06:00
sLog . outDebug ( "Creature (GUIDLow: %u) X: %f Y: %f (%s) in grid[%u,%u]cell[%u,%u] instead grid[%u,%u]cell[%u,%u]" ,
c -> GetGUIDLow (),
2008-10-02 16:23:55 -05:00
c -> GetPositionX (), c -> GetPositionY (),( moved ? "final" : "original" ),
cur_cell . GridX (), cur_cell . GridY (), cur_cell . CellX (), cur_cell . CellY (),
xy_cell . GridX (), xy_cell . GridY (), xy_cell . CellX (), xy_cell . CellY ());
return true ; // not crash at error, just output error in debug mode
}
return true ;
}
const char * Map :: GetMapName () const
{
return i_mapEntry ? i_mapEntry -> name [ sWorld . GetDefaultDbcLocale ()] : "UNNAMEDMAP\x0" ;
}
void Map :: UpdateObjectVisibility ( WorldObject * obj , Cell cell , CellPair cellpair )
{
cell . data . Part . reserved = ALL_DISTRICT ;
cell . SetNoCreate ();
2008-10-14 11:57:03 -05:00
Trinity :: VisibleChangesNotifier notifier ( * obj );
TypeContainerVisitor < Trinity :: VisibleChangesNotifier , WorldTypeMapContainer > player_notifier ( notifier );
2008-10-02 16:23:55 -05:00
CellLock < GridReadGuard > cell_lock ( cell , cellpair );
cell_lock -> Visit ( cell_lock , player_notifier , * this );
}
void Map :: SendInitSelf ( Player * player )
{
sLog . outDetail ( "Creating player data for himself %u" , player -> GetGUIDLow ());
UpdateData data ;
// attach to player data current transport data
if ( Transport * transport = player -> GetTransport ())
{
transport -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
// build data for self presence in world at own client (one time for map)
player -> BuildCreateUpdateBlockForPlayer ( & data , player );
// build other passengers at transport also (they always visible and marked as visible and will not send at visibility update at add to map
if ( Transport * transport = player -> GetTransport ())
{
for ( Transport :: PlayerSet :: const_iterator itr = transport -> GetPassengers (). begin (); itr != transport -> GetPassengers (). end (); ++ itr )
{
if ( player != ( * itr ) && player -> HaveAtClient ( * itr ))
{
( * itr ) -> BuildCreateUpdateBlockForPlayer ( & data , player );
}
}
}
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 );
}
void Map :: SendInitTransports ( Player * player )
{
// Hack to send out transports
MapManager :: TransportMap & tmap = MapManager :: Instance (). m_TransportsByMap ;
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
UpdateData transData ;
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
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
2009-08-01 00:20:32 +02:00
if (( * i ) != player -> GetTransport () && ( * i ) -> GetMapId () == GetId ())
2008-10-02 16:23:55 -05:00
{
( * i ) -> BuildCreateUpdateBlockForPlayer ( & transData , player );
}
}
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 );
}
void Map :: SendRemoveTransports ( Player * player )
{
// Hack to send out transports
MapManager :: TransportMap & tmap = MapManager :: Instance (). m_TransportsByMap ;
// no transports at map
if ( tmap . find ( player -> GetMapId ()) == tmap . end ())
return ;
UpdateData transData ;
MapManager :: TransportSet & tset = tmap [ player -> GetMapId ()];
// except used transport
2009-04-29 00:26:07 -05:00
for ( MapManager :: TransportSet :: const_iterator i = tset . begin (); i != tset . end (); ++ i )
2009-08-01 00:20:32 +02:00
if (( * i ) != player -> GetTransport () && ( * i ) -> GetMapId () != GetId ())
2008-10-02 16:23:55 -05:00
( * i ) -> BuildOutOfRangeUpdateBlock ( & transData );
WorldPacket packet ;
transData . BuildPacket ( & packet );
player -> GetSession () -> SendPacket ( & packet );
}
inline void Map :: setNGrid ( NGridType * grid , uint32 x , uint32 y )
{
if ( x >= MAX_NUMBER_OF_GRIDS || y >= MAX_NUMBER_OF_GRIDS )
{
sLog . outError ( "map::setNGrid() Invalid grid coordinates found: %d, %d!" , x , y );
assert ( false );
}
i_grids [ x ][ y ] = grid ;
}
2009-06-09 23:52:45 -05:00
//void Map::DoDelayedMovesAndRemoves()
//{
2009-06-09 19:53:25 -05:00
//MoveAllCreaturesInMoveList();
//RemoveAllObjectsInRemoveList();
2009-06-09 23:52:45 -05:00
//}
2008-10-02 16:23:55 -05:00
void Map :: AddObjectToRemoveList ( WorldObject * obj )
{
assert ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
2009-07-01 18:23:36 -05:00
obj -> CleanupsBeforeDelete (); // remove or simplify at least cross referenced links
2008-10-02 16:23:55 -05:00
i_objectsToRemove . insert ( obj );
//sLog.outDebug("Object (GUID: %u TypeId: %u ) added to removing list.",obj->GetGUIDLow(),obj->GetTypeId());
}
2009-02-26 17:01:02 -06:00
void Map :: AddObjectToSwitchList ( WorldObject * obj , bool on )
{
assert ( obj -> GetMapId () == GetId () && obj -> GetInstanceId () == GetInstanceId ());
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . find ( obj );
if ( itr == i_objectsToSwitch . end ())
i_objectsToSwitch . insert ( itr , std :: make_pair ( obj , on ));
else if ( itr -> second != on )
i_objectsToSwitch . erase ( itr );
else
assert ( false );
}
2008-10-02 16:23:55 -05:00
void Map :: RemoveAllObjectsInRemoveList ()
{
2009-02-26 17:01:02 -06:00
while ( ! i_objectsToSwitch . empty ())
{
std :: map < WorldObject * , bool >:: iterator itr = i_objectsToSwitch . begin ();
WorldObject * obj = itr -> first ;
bool on = itr -> second ;
i_objectsToSwitch . erase ( itr );
switch ( obj -> GetTypeId ())
{
case TYPEID_UNIT :
2009-03-28 22:44:09 -06:00
if ( ! (( Creature * ) obj ) -> isWorldCreature ())
2009-02-26 17:01:02 -06:00
SwitchGridContainers (( Creature * ) obj , on );
break ;
}
}
2008-10-02 16:23:55 -05:00
//sLog.outDebug("Object remover 1 check.");
while ( ! i_objectsToRemove . empty ())
{
2009-03-24 17:55:05 -06:00
std :: set < WorldObject *>:: iterator itr = i_objectsToRemove . begin ();
WorldObject * obj = * itr ;
2008-10-02 16:23:55 -05:00
switch ( obj -> GetTypeId ())
{
case TYPEID_CORPSE :
{
Corpse * corpse = ObjectAccessor :: Instance (). GetCorpse ( * obj , obj -> GetGUID ());
if ( ! corpse )
2009-03-21 14:28:02 -06:00
sLog . outError ( "Try delete corpse/bones %u that not in map" , obj -> GetGUIDLow ());
2008-10-02 16:23:55 -05:00
else
Remove ( corpse , true );
break ;
}
case TYPEID_DYNAMICOBJECT :
Remove (( DynamicObject * ) obj , true );
break ;
case TYPEID_GAMEOBJECT :
Remove (( GameObject * ) obj , true );
break ;
case TYPEID_UNIT :
2008-11-07 09:36:46 -06:00
// in case triggered sequence some spell can continue casting after prev CleanupsBeforeDelete call
2008-11-05 20:10:19 -06:00
// make sure that like sources auras/etc removed before destructor start
(( Creature * ) obj ) -> CleanupsBeforeDelete ();
2008-10-02 16:23:55 -05:00
Remove (( Creature * ) obj , true );
break ;
default :
sLog . outError ( "Non-grid object (TypeId: %u) in grid object removing list, ignored." , obj -> GetTypeId ());
break ;
}
2009-03-24 17:55:05 -06:00
i_objectsToRemove . erase ( itr );
2008-10-02 16:23:55 -05:00
}
//sLog.outDebug("Object remover 2 check.");
}
2008-11-18 19:40:06 -06:00
uint32 Map :: GetPlayersCountExceptGMs () const
{
uint32 count = 0 ;
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
if ( ! itr -> getSource () -> isGameMaster ())
++ count ;
return count ;
}
void Map :: SendToPlayers ( WorldPacket const * data ) const
{
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> GetSession () -> SendPacket ( data );
}
2009-02-25 20:52:20 -06:00
bool Map :: ActiveObjectsNearGrid ( uint32 x , uint32 y ) const
2008-11-18 19:40:06 -06:00
{
CellPair cell_min ( x * MAX_NUMBER_OF_CELLS , y * MAX_NUMBER_OF_CELLS );
CellPair cell_max ( cell_min . x_coord + MAX_NUMBER_OF_CELLS , cell_min . y_coord + MAX_NUMBER_OF_CELLS );
cell_min << 2 ;
cell_min -= 2 ;
cell_max >> 2 ;
cell_max += 2 ;
for ( MapRefManager :: const_iterator iter = m_mapRefManager . begin (); iter != m_mapRefManager . end (); ++ iter )
{
Player * plr = iter -> getSource ();
CellPair p = Trinity :: ComputeCellPair ( plr -> GetPositionX (), plr -> GetPositionY ());
if ( ( cell_min . x_coord <= p . x_coord && p . x_coord <= cell_max . x_coord ) &&
( cell_min . y_coord <= p . y_coord && p . y_coord <= cell_max . y_coord ) )
return true ;
}
2009-02-25 20:52:20 -06:00
for ( ActiveNonPlayers :: const_iterator iter = m_activeNonPlayers . begin (); iter != m_activeNonPlayers . end (); ++ iter )
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
WorldObject * obj = * iter ;
2008-11-21 15:41:18 -06:00
2009-02-25 20:52:20 -06:00
CellPair p = Trinity :: ComputeCellPair ( obj -> GetPositionX (), obj -> GetPositionY ());
2008-11-21 15:41:18 -06:00
if ( ( cell_min . x_coord <= p . x_coord && p . x_coord <= cell_max . x_coord ) &&
( cell_min . y_coord <= p . y_coord && p . y_coord <= cell_max . y_coord ) )
return true ;
}
2009-02-25 20:52:20 -06:00
return false ;
}
void Map :: AddToActive ( Creature * c )
{
AddToActiveHelper ( c );
// also not allow unloading spawn grid to prevent creating creature clone at load
2009-03-07 00:53:01 -06:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2008-11-21 15:41:18 -06:00
{
2009-02-25 20:52:20 -06:00
float x , y , z ;
c -> GetRespawnCoord ( x , y , z );
GridPair p = Trinity :: ComputeGridPair ( x , y );
if ( getNGrid ( p . x_coord , p . y_coord ))
getNGrid ( p . x_coord , p . y_coord ) -> incUnloadActiveLock ();
else
{
GridPair p2 = Trinity :: ComputeGridPair ( c -> GetPositionX (), c -> GetPositionY ());
sLog . outError ( "Active creature (GUID: %u Entry: %u) added to grid[%u,%u] but spawn grid[%u,%u] not loaded." ,
c -> GetGUIDLow (), c -> GetEntry (), p . x_coord , p . y_coord , p2 . x_coord , p2 . y_coord );
}
2008-11-21 15:41:18 -06:00
}
2009-02-25 20:52:20 -06:00
}
2008-11-21 15:41:18 -06:00
2009-02-25 20:52:20 -06:00
void Map :: RemoveFromActive ( Creature * c )
{
RemoveFromActiveHelper ( c );
// also allow unloading spawn grid
2009-03-07 00:53:01 -06:00
if ( ! c -> isPet () && c -> GetDBTableGUIDLow ())
2009-02-25 20:52:20 -06:00
{
float x , y , z ;
c -> GetRespawnCoord ( x , y , z );
GridPair p = Trinity :: ComputeGridPair ( x , y );
if ( getNGrid ( p . x_coord , p . y_coord ))
getNGrid ( p . x_coord , p . y_coord ) -> decUnloadActiveLock ();
else
{
GridPair p2 = Trinity :: ComputeGridPair ( c -> GetPositionX (), c -> GetPositionY ());
sLog . outError ( "Active creature (GUID: %u Entry: %u) removed from grid[%u,%u] but spawn grid[%u,%u] not loaded." ,
c -> GetGUIDLow (), c -> GetEntry (), p . x_coord , p . y_coord , p2 . x_coord , p2 . y_coord );
}
}
2008-11-21 15:41:18 -06:00
}
2008-10-02 16:23:55 -05:00
template void Map :: Add ( Corpse * );
template void Map :: Add ( Creature * );
template void Map :: Add ( GameObject * );
template void Map :: Add ( DynamicObject * );
template void Map :: Remove ( Corpse * , bool );
template void Map :: Remove ( Creature * , bool );
template void Map :: Remove ( GameObject * , bool );
template void Map :: Remove ( DynamicObject * , bool );
/* ******* Dungeon Instance Maps ******* */
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
{
// the timer is started by default, and stopped when the first player joins
// this make sure it gets unloaded if for some reason no player joins
m_unloadTimer = std :: max ( sWorld . getConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
}
InstanceMap ::~ InstanceMap ()
{
if ( i_data )
{
delete i_data ;
i_data = NULL ;
}
}
/*
Do map specific checks to see if the player can enter
*/
bool InstanceMap :: CanEnter ( Player * player )
{
2008-11-18 19:40:06 -06:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "InstanceMap::CanEnter - player %s(%u) already in map %d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), GetId (), GetInstanceId (), GetSpawnMode ());
assert ( false );
return false ;
}
// cannot enter if the instance is full (player cap), GMs don't count
2009-01-22 22:44:27 -06:00
uint32 maxPlayers = GetMaxPlayers ();
if ( ! player -> isGameMaster () && GetPlayersCountExceptGMs () >= maxPlayers )
2008-10-02 16:23:55 -05:00
{
2009-01-22 22:44:27 -06:00
sLog . outDetail ( "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 ;
}
// cannot enter while players in the instance are in combat
Group * pGroup = player -> GetGroup ();
2009-02-13 23:53:06 +01:00
if ( ! player -> isGameMaster () && pGroup && pGroup -> InCombatToInstance ( GetInstanceId ()) && player -> GetMapId () != GetId ())
2008-10-02 16:23:55 -05:00
{
player -> SendTransferAborted ( GetId (), TRANSFER_ABORT_ZONE_IN_COMBAT );
return false ;
}
return Map :: CanEnter ( player );
}
/*
Do map specific checks and add the player to the map if successful.
*/
bool InstanceMap :: Add ( Player * player )
{
// TODO: Not sure about checking player level: already done in HandleAreaTriggerOpcode
// GMs still can teleport player in instance.
// Is it needed?
{
Guard guard ( * this );
2009-08-01 00:20:32 +02:00
// Check moved to void WorldSession::HandleMoveWorldportAckOpcode()
//if(!CanEnter(player))
//return false;
2008-10-02 16:23:55 -05:00
2009-03-18 20:41:49 -06:00
// Dungeon only code
if ( IsDungeon ())
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
// get or create an instance save for the map
InstanceSave * mapSave = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! mapSave )
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
sLog . outDetail ( "InstanceMap::Add: creating instance save for map %d spawnmode %d with instance id %d" , GetId (), GetSpawnMode (), GetInstanceId ());
mapSave = sInstanceSaveManager . AddInstanceSave ( GetId (), GetInstanceId (), GetSpawnMode (), 0 , true );
2008-10-02 16:23:55 -05:00
}
2009-03-18 20:41:49 -06:00
// check for existing instance binds
InstancePlayerBind * playerBind = player -> GetBoundInstance ( GetId (), GetSpawnMode ());
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
if ( playerBind -> save != mapSave )
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
sLog . outError ( "InstanceMap::Add: player %s(%d) is permanently bound to instance %d,%d,%d,%d,%d,%d but he is being put in instance %d,%d,%d,%d,%d,%d" , player -> GetName (), player -> GetGUIDLow (), playerBind -> save -> GetMapId (), playerBind -> save -> GetInstanceId (), playerBind -> save -> GetDifficulty (), playerBind -> save -> GetPlayerCount (), playerBind -> save -> GetGroupCount (), playerBind -> save -> CanReset (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), mapSave -> GetPlayerCount (), mapSave -> GetGroupCount (), mapSave -> CanReset ());
assert ( false );
2008-10-02 16:23:55 -05:00
}
2009-03-18 20:41:49 -06:00
}
else
{
Group * pGroup = player -> GetGroup ();
if ( pGroup )
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
InstanceGroupBind * groupBind = pGroup -> GetBoundInstance ( GetId (), GetSpawnMode ());
if ( playerBind )
2008-10-02 16:23:55 -05:00
{
2009-03-18 20:41:49 -06:00
sLog . outError ( "InstanceMap::Add: player %s(%d) is being put in instance %d,%d,%d,%d,%d,%d but he is in group %d and is bound to instance %d,%d,%d,%d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), mapSave -> GetPlayerCount (), mapSave -> GetGroupCount (), mapSave -> CanReset (), GUID_LOPART ( pGroup -> GetLeaderGUID ()), playerBind -> save -> GetMapId (), playerBind -> save -> GetInstanceId (), playerBind -> save -> GetDifficulty (), playerBind -> save -> GetPlayerCount (), playerBind -> save -> GetGroupCount (), playerBind -> save -> CanReset ());
if ( groupBind ) sLog . outError ( "InstanceMap::Add: the group is bound to instance %d,%d,%d,%d,%d,%d" , groupBind -> save -> GetMapId (), groupBind -> save -> GetInstanceId (), groupBind -> save -> GetDifficulty (), groupBind -> save -> GetPlayerCount (), groupBind -> save -> GetGroupCount (), groupBind -> save -> CanReset ());
2009-03-23 17:01:26 -06:00
//assert(false);
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
if ( ! groupBind )
pGroup -> BindToInstance ( mapSave , false );
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
if ( groupBind -> save != mapSave )
{
sLog . outError ( "InstanceMap::Add: player %s(%d) is being put in instance %d,%d,%d but he is in group %d which is bound to instance %d,%d,%d!" , player -> GetName (), player -> GetGUIDLow (), mapSave -> GetMapId (), mapSave -> GetInstanceId (), mapSave -> GetDifficulty (), GUID_LOPART ( pGroup -> GetLeaderGUID ()), groupBind -> save -> GetMapId (), groupBind -> save -> GetInstanceId (), groupBind -> save -> GetDifficulty ());
if ( mapSave )
sLog . outError ( "MapSave players: %d, group count: %d" , mapSave -> GetPlayerCount (), mapSave -> GetGroupCount ());
else
sLog . outError ( "MapSave NULL" );
if ( groupBind -> save )
sLog . outError ( "GroupBind save players: %d, group count: %d" , groupBind -> save -> GetPlayerCount (), groupBind -> save -> GetGroupCount ());
else
sLog . outError ( "GroupBind save NULL" );
assert ( false );
}
// if the group/leader is permanently bound to the instance
// players also become permanently bound when they enter
if ( groupBind -> perm )
{
WorldPacket data ( SMSG_INSTANCE_SAVE_CREATED , 4 );
data << uint32 ( 0 );
player -> GetSession () -> SendPacket ( & data );
player -> BindToInstance ( mapSave , true );
}
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
if ( ! playerBind )
player -> BindToInstance ( mapSave , false );
else
// cannot jump to a different instance without resetting it
assert ( playerBind -> save == mapSave );
}
2008-10-02 16:23:55 -05:00
}
}
if ( i_data ) i_data -> OnPlayerEnter ( player );
2008-11-28 11:38:28 -06:00
// for normal instances cancel the reset schedule when the
// first player enters (no players yet)
2008-10-02 16:23:55 -05:00
SetResetSchedule ( false );
sLog . outDetail ( "MAP: Player '%s' entered the instance '%u' of map '%s'" , player -> GetName (), GetInstanceId (), GetMapName ());
// initialize unload state
m_unloadTimer = 0 ;
m_resetAfterUnload = false ;
m_unloadWhenEmpty = false ;
}
// this will acquire the same mutex so it cannot be in the previous block
Map :: Add ( player );
return true ;
}
2008-11-02 16:53:46 -06:00
void InstanceMap :: Update ( const uint32 & t_diff )
{
2008-11-07 09:36:46 -06:00
Map :: Update ( t_diff );
2008-11-02 16:53:46 -06:00
2008-11-07 09:36:46 -06:00
if ( i_data )
i_data -> Update ( t_diff );
2008-11-02 16:53:46 -06:00
}
2008-10-02 16:23:55 -05:00
void InstanceMap :: Remove ( Player * player , bool remove )
{
sLog . outDetail ( "MAP: Removing player '%s' from instance '%u' of map '%s' before relocating to other map" , player -> GetName (), GetInstanceId (), GetMapName ());
2008-11-18 19:40:06 -06:00
//if last player set unload timer
if ( ! m_unloadTimer && m_mapRefManager . getSize () == 1 )
2008-10-02 16:23:55 -05:00
m_unloadTimer = m_unloadWhenEmpty ? MIN_UNLOAD_DELAY : std :: max ( sWorld . getConfig ( CONFIG_INSTANCE_UNLOAD_DELAY ), ( uint32 ) MIN_UNLOAD_DELAY );
Map :: Remove ( player , remove );
2008-11-28 11:38:28 -06:00
// for normal instances schedule the reset after all players have left
SetResetSchedule ( true );
2008-10-02 16:23:55 -05:00
}
void InstanceMap :: CreateInstanceData ( bool load )
{
if ( i_data != NULL )
return ;
InstanceTemplate const * mInstance = objmgr . GetInstanceTemplate ( GetId ());
if ( mInstance )
{
2008-11-20 16:16:57 -06:00
i_script_id = mInstance -> script_id ;
2008-10-02 16:23:55 -05:00
i_data = Script -> CreateInstanceData ( this );
}
if ( ! i_data )
return ;
2009-05-01 11:38:53 -05:00
i_data -> Initialize ();
2008-10-02 16:23:55 -05:00
if ( load )
{
// TODO: make a global storage for this
QueryResult * result = CharacterDatabase . PQuery ( "SELECT data FROM instance WHERE map = '%u' AND id = '%u'" , GetId (), i_InstanceId );
if ( result )
{
Field * fields = result -> Fetch ();
const char * data = fields [ 0 ]. GetString ();
2009-05-11 03:17:13 -05:00
if ( data && data != "" )
2008-10-02 16:23:55 -05:00
{
2008-11-20 16:16:57 -06:00
sLog . outDebug ( "Loading instance data for `%s` with id %u" , objmgr . GetScriptName ( i_script_id ), i_InstanceId );
2008-10-02 16:23:55 -05:00
i_data -> Load ( data );
}
delete result ;
}
}
}
/*
Returns true if there are no players in the instance
*/
bool InstanceMap :: Reset ( uint8 method )
{
// note: since the map may not be loaded when the instance needs to be reset
// the instance must be deleted from the DB by InstanceSaveManager
2008-11-18 19:40:06 -06:00
if ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
if ( method == INSTANCE_RESET_ALL )
{
// notify the players to leave the instance so it can be reset
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> SendResetFailedNotify ( GetId ());
2008-10-02 16:23:55 -05:00
}
else
{
if ( method == INSTANCE_RESET_GLOBAL )
{
// set the homebind timer for players inside (1 minute)
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
itr -> getSource () -> m_InstanceValid = false ;
2008-10-02 16:23:55 -05:00
}
// the unload timer is not started
// instead the map will unload immediately after the players have left
m_unloadWhenEmpty = true ;
m_resetAfterUnload = true ;
}
}
else
{
// unloaded at next update
m_unloadTimer = MIN_UNLOAD_DELAY ;
m_resetAfterUnload = true ;
}
2008-11-18 19:40:06 -06:00
return m_mapRefManager . isEmpty ();
2008-10-02 16:23:55 -05:00
}
void InstanceMap :: PermBindAllPlayers ( Player * player )
{
2009-03-18 20:41:49 -06:00
if ( ! IsDungeon ())
return ;
2008-10-02 16:23:55 -05:00
InstanceSave * save = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! save )
{
2009-03-09 17:56:51 -06:00
sLog . outError ( "Cannot bind players, no instance save available for map!" );
2008-10-02 16:23:55 -05:00
return ;
}
Group * group = player -> GetGroup ();
// group members outside the instance group don't get bound
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
Player * plr = itr -> getSource ();
// players inside an instance cannot be bound to other instances
// some players may already be permanently bound, in this case nothing happens
InstancePlayerBind * bind = plr -> GetBoundInstance ( save -> GetMapId (), save -> GetDifficulty ());
if ( ! bind || ! bind -> perm )
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
plr -> BindToInstance ( save , true );
WorldPacket data ( SMSG_INSTANCE_SAVE_CREATED , 4 );
data << uint32 ( 0 );
plr -> GetSession () -> SendPacket ( & data );
2008-10-02 16:23:55 -05:00
}
2008-11-18 19:40:06 -06:00
// if the leader is not in the instance the group will not get a perm bind
if ( group && group -> GetLeaderGUID () == plr -> GetGUID ())
group -> BindToInstance ( save , true );
2008-10-02 16:23:55 -05:00
}
}
2009-02-25 17:53:05 -06:00
void InstanceMap :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
2009-08-06 10:21:10 -05:00
while ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "InstanceMap::UnloadAll: there are still players in the instance at unload, should not happen!" );
2009-08-06 10:21:10 -05:00
Player * plr = m_mapRefManager . getFirst () -> getSource ();
plr -> TeleportOutOfMap ( this );
2008-10-02 16:23:55 -05:00
}
if ( m_resetAfterUnload == true )
objmgr . DeleteRespawnTimeForInstance ( GetInstanceId ());
2009-02-25 17:53:05 -06:00
Map :: UnloadAll ();
2008-10-02 16:23:55 -05:00
}
2008-11-18 19:40:06 -06:00
void InstanceMap :: SendResetWarnings ( uint32 timeLeft ) const
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
for ( MapRefManager :: const_iterator itr = m_mapRefManager . begin (); itr != m_mapRefManager . end (); ++ itr )
2009-06-14 10:19:44 -05:00
itr -> getSource () -> SendInstanceResetWarning ( GetId (), itr -> getSource () -> GetDifficulty (), timeLeft );
2008-10-02 16:23:55 -05:00
}
void InstanceMap :: SetResetSchedule ( bool on )
{
// only for normal instances
// the reset time is only scheduled when there are no payers inside
// it is assumed that the reset time will rarely (if ever) change while the reset is scheduled
2009-03-18 20:41:49 -06:00
if ( IsDungeon () && ! HavePlayers () && ! IsRaid () && ! IsHeroic ())
2008-10-02 16:23:55 -05:00
{
InstanceSave * save = sInstanceSaveManager . GetInstanceSave ( GetInstanceId ());
if ( ! save ) sLog . outError ( "InstanceMap::SetResetSchedule: cannot turn schedule %s, no save available for instance %d of %d" , on ? "on" : "off" , GetInstanceId (), GetId ());
else sInstanceSaveManager . ScheduleReset ( on , save -> GetResetTime (), InstanceSaveManager :: InstResetEvent ( 0 , GetId (), GetInstanceId ()));
}
}
2009-01-22 22:44:27 -06:00
uint32 InstanceMap :: GetMaxPlayers () const
{
InstanceTemplate const * iTemplate = objmgr . GetInstanceTemplate ( GetId ());
if ( ! iTemplate )
return 0 ;
return IsHeroic () ? iTemplate -> maxPlayersHeroic : iTemplate -> maxPlayers ;
}
2008-10-02 16:23:55 -05:00
/* ******* Battleground Instance Maps ******* */
2009-07-16 11:49:00 +08:00
BattleGroundMap :: BattleGroundMap ( uint32 id , time_t expiry , uint32 InstanceId , Map * _parent )
: Map ( id , expiry , InstanceId , DIFFICULTY_NORMAL , _parent )
2008-10-02 16:23:55 -05:00
{
}
BattleGroundMap ::~ BattleGroundMap ()
{
}
bool BattleGroundMap :: CanEnter ( Player * player )
{
2008-11-18 19:40:06 -06:00
if ( player -> GetMapRef (). getTarget () == this )
2008-10-02 16:23:55 -05:00
{
sLog . outError ( "BGMap::CanEnter - player %u already in map!" , player -> GetGUIDLow ());
assert ( false );
return false ;
}
if ( player -> GetBattleGroundId () != GetInstanceId ())
return false ;
// player number limit is checked in bgmgr, no need to do it here
return Map :: CanEnter ( player );
}
bool BattleGroundMap :: Add ( Player * player )
{
{
Guard guard ( * this );
2009-08-01 00:20:32 +02:00
//Check moved to void WorldSession::HandleMoveWorldportAckOpcode()
//if(!CanEnter(player))
//return false;
2008-10-02 16:23:55 -05:00
// reset instance validity, battleground maps do not homebind
player -> m_InstanceValid = true ;
}
return Map :: Add ( player );
}
void BattleGroundMap :: Remove ( Player * player , bool remove )
{
sLog . outDetail ( "MAP: Removing player '%s' from bg '%u' of map '%s' before relocating to other map" , player -> GetName (), GetInstanceId (), GetMapName ());
Map :: Remove ( player , remove );
}
void BattleGroundMap :: SetUnload ()
{
m_unloadTimer = MIN_UNLOAD_DELAY ;
}
2009-02-25 17:53:05 -06:00
void BattleGroundMap :: UnloadAll ()
2008-10-02 16:23:55 -05:00
{
2008-11-18 19:40:06 -06:00
while ( HavePlayers ())
2008-10-02 16:23:55 -05:00
{
2009-08-06 10:21:10 -05:00
Player * plr = m_mapRefManager . getFirst () -> getSource ();
plr -> TeleportTo ( plr -> GetBattleGroundEntryPoint ());
plr -> TeleportOutOfMap ( this );
2008-10-02 16:23:55 -05:00
}
2009-02-25 17:53:05 -06:00
Map :: UnloadAll ();
2008-10-02 16:23:55 -05:00
}
2008-11-29 13:08:23 -06:00
2009-07-31 10:48:34 +08:00
/// Put scripts in the execution queue
void Map :: ScriptsStart ( ScriptMapMap const & scripts , uint32 id , Object * source , Object * target )
{
///- Find the script map
ScriptMapMap :: const_iterator s = scripts . find ( id );
if ( s == scripts . end ())
return ;
// prepare static data
uint64 sourceGUID = source ? source -> GetGUID () : ( uint64 ) 0 ; //some script commands doesn't have source
uint64 targetGUID = target ? target -> GetGUID () : ( uint64 ) 0 ;
uint64 ownerGUID = ( source -> GetTypeId () == TYPEID_ITEM ) ? (( Item * ) source ) -> GetOwnerGUID () : ( uint64 ) 0 ;
///- Schedule script execution for all scripts in the script map
ScriptMap const * s2 = & ( s -> second );
bool immedScript = false ;
for ( ScriptMap :: const_iterator iter = s2 -> begin (); iter != s2 -> end (); ++ iter )
{
ScriptAction sa ;
sa . sourceGUID = sourceGUID ;
sa . targetGUID = targetGUID ;
sa . ownerGUID = ownerGUID ;
sa . script = & iter -> second ;
m_scriptSchedule . insert ( std :: pair < time_t , ScriptAction > ( time_t ( sWorld . GetGameTime () + iter -> first ), sa ));
if ( iter -> first == 0 )
immedScript = true ;
sWorld . IncreaseScheduledScriptsCount ();
}
///- If one of the effects should be immediate, launch the script execution
if ( /*start &&*/ immedScript )
ScriptsProcess ();
}
void Map :: ScriptCommandStart ( ScriptInfo const & script , uint32 delay , Object * source , Object * target )
{
// NOTE: script record _must_ exist until command executed
// prepare static data
uint64 sourceGUID = source ? source -> GetGUID () : ( uint64 ) 0 ;
uint64 targetGUID = target ? target -> GetGUID () : ( uint64 ) 0 ;
uint64 ownerGUID = ( source -> GetTypeId () == TYPEID_ITEM ) ? (( Item * ) source ) -> GetOwnerGUID () : ( uint64 ) 0 ;
ScriptAction sa ;
sa . sourceGUID = sourceGUID ;
sa . targetGUID = targetGUID ;
sa . ownerGUID = ownerGUID ;
sa . script = & script ;
m_scriptSchedule . insert ( std :: pair < time_t , ScriptAction > ( time_t ( sWorld . GetGameTime () + delay ), sa ));
sWorld . IncreaseScheduledScriptsCount ();
///- If effects should be immediate, launch the script execution
if ( delay == 0 )
ScriptsProcess ();
}
/// Process queued scripts
void Map :: ScriptsProcess ()
{
if ( m_scriptSchedule . empty ())
return ;
///- Process overdue queued scripts
std :: multimap < time_t , ScriptAction >:: iterator iter = m_scriptSchedule . begin ();
// ok as multimap is a *sorted* associative container
while ( ! m_scriptSchedule . empty () && ( iter -> first <= sWorld . GetGameTime ()))
{
ScriptAction const & step = iter -> second ;
Object * source = NULL ;
if ( step . sourceGUID )
{
switch ( GUID_HIPART ( step . sourceGUID ))
{
case HIGHGUID_ITEM :
// case HIGHGUID_CONTAINER: ==HIGHGUID_ITEM
{
Player * player = HashMapHolder < Player >:: Find ( step . ownerGUID );
if ( player )
source = player -> GetItemByGuid ( step . sourceGUID );
break ;
}
case HIGHGUID_UNIT :
source = HashMapHolder < Creature >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_PET :
source = HashMapHolder < Pet >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_VEHICLE :
source = HashMapHolder < Vehicle >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_PLAYER :
source = HashMapHolder < Player >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_GAMEOBJECT :
source = HashMapHolder < GameObject >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_CORPSE :
source = HashMapHolder < Corpse >:: Find ( step . sourceGUID );
break ;
case HIGHGUID_MO_TRANSPORT :
for ( MapManager :: TransportSet :: iterator iter = MapManager :: Instance (). m_Transports . begin (); iter != MapManager :: Instance (). m_Transports . end (); ++ iter )
{
if (( * iter ) -> GetGUID () == step . sourceGUID )
{
source = reinterpret_cast < Object *> ( * iter );
break ;
}
}
break ;
default :
sLog . outError ( "*_script source with unsupported high guid value %u" , GUID_HIPART ( step . sourceGUID ));
break ;
}
}
//if(source && !source->IsInWorld()) source = NULL;
Object * target = NULL ;
if ( step . targetGUID )
{
switch ( GUID_HIPART ( step . targetGUID ))
{
case HIGHGUID_UNIT :
target = HashMapHolder < Creature >:: Find ( step . targetGUID );
break ;
case HIGHGUID_PET :
target = HashMapHolder < Pet >:: Find ( step . targetGUID );
break ;
case HIGHGUID_VEHICLE :
target = HashMapHolder < Vehicle >:: Find ( step . targetGUID );
break ;
case HIGHGUID_PLAYER : // empty GUID case also
target = HashMapHolder < Player >:: Find ( step . targetGUID );
break ;
case HIGHGUID_GAMEOBJECT :
target = HashMapHolder < GameObject >:: Find ( step . targetGUID );
break ;
case HIGHGUID_CORPSE :
target = HashMapHolder < Corpse >:: Find ( step . targetGUID );
break ;
default :
sLog . outError ( "*_script source with unsupported high guid value %u" , GUID_HIPART ( step . targetGUID ));
break ;
}
}
//if(target && !target->IsInWorld()) target = NULL;
switch ( step . script -> command )
{
case SCRIPT_COMMAND_TALK :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_TALK call for NULL creature." );
break ;
}
if ( source -> GetTypeId () != TYPEID_UNIT )
{
sLog . outError ( "SCRIPT_COMMAND_TALK call for non-creature (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
if ( step . script -> datalong > 3 )
{
sLog . outError ( "SCRIPT_COMMAND_TALK invalid chat type (%u), skipping." , step . script -> datalong );
break ;
}
uint64 unit_target = target ? target -> GetGUID () : 0 ;
//datalong 0=normal say, 1=whisper, 2=yell, 3=emote text
switch ( step . script -> datalong )
{
case 0 : // Say
(( Creature * ) source ) -> Say ( step . script -> dataint , LANG_UNIVERSAL , unit_target );
break ;
case 1 : // Whisper
if ( ! unit_target )
{
sLog . outError ( "SCRIPT_COMMAND_TALK attempt to whisper (%u) NULL, skipping." , step . script -> datalong );
break ;
}
(( Creature * ) source ) -> Whisper ( step . script -> dataint , unit_target );
break ;
case 2 : // Yell
(( Creature * ) source ) -> Yell ( step . script -> dataint , LANG_UNIVERSAL , unit_target );
break ;
case 3 : // Emote text
(( Creature * ) source ) -> TextEmote ( step . script -> dataint , unit_target );
break ;
default :
break ; // must be already checked at load
}
break ;
}
case SCRIPT_COMMAND_EMOTE :
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_EMOTE call for NULL creature." );
break ;
}
if ( source -> GetTypeId () != TYPEID_UNIT )
{
sLog . outError ( "SCRIPT_COMMAND_EMOTE call for non-creature (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
(( Creature * ) source ) -> HandleEmoteCommand ( step . script -> datalong );
break ;
case SCRIPT_COMMAND_FIELD_SET :
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_FIELD_SET call for NULL object." );
break ;
}
if ( step . script -> datalong <= OBJECT_FIELD_ENTRY || step . script -> datalong >= source -> GetValuesCount ())
{
sLog . outError ( "SCRIPT_COMMAND_FIELD_SET call for wrong field %u (max count: %u) in object (TypeId: %u)." ,
step . script -> datalong , source -> GetValuesCount (), source -> GetTypeId ());
break ;
}
source -> SetUInt32Value ( step . script -> datalong , step . script -> datalong2 );
break ;
case SCRIPT_COMMAND_MOVE_TO :
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_MOVE_TO call for NULL creature." );
break ;
}
if ( source -> GetTypeId () != TYPEID_UNIT )
{
sLog . outError ( "SCRIPT_COMMAND_MOVE_TO call for non-creature (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
(( Creature * ) source ) -> SendMonsterMoveWithSpeed ( step . script -> x , step . script -> y , step . script -> z , step . script -> datalong2 );
(( Creature * ) source ) -> GetMap () -> CreatureRelocation ((( Creature * ) source ), step . script -> x , step . script -> y , step . script -> z , 0 );
break ;
case SCRIPT_COMMAND_FLAG_SET :
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_FLAG_SET call for NULL object." );
break ;
}
if ( step . script -> datalong <= OBJECT_FIELD_ENTRY || step . script -> datalong >= source -> GetValuesCount ())
{
sLog . outError ( "SCRIPT_COMMAND_FLAG_SET call for wrong field %u (max count: %u) in object (TypeId: %u)." ,
step . script -> datalong , source -> GetValuesCount (), source -> GetTypeId ());
break ;
}
source -> SetFlag ( step . script -> datalong , step . script -> datalong2 );
break ;
case SCRIPT_COMMAND_FLAG_REMOVE :
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_FLAG_REMOVE call for NULL object." );
break ;
}
if ( step . script -> datalong <= OBJECT_FIELD_ENTRY || step . script -> datalong >= source -> GetValuesCount ())
{
sLog . outError ( "SCRIPT_COMMAND_FLAG_REMOVE call for wrong field %u (max count: %u) in object (TypeId: %u)." ,
step . script -> datalong , source -> GetValuesCount (), source -> GetTypeId ());
break ;
}
source -> RemoveFlag ( step . script -> datalong , step . script -> datalong2 );
break ;
case SCRIPT_COMMAND_TELEPORT_TO :
{
// accept player in any one from target/source arg
if ( ! target && ! source )
{
sLog . outError ( "SCRIPT_COMMAND_TELEPORT_TO call for NULL object." );
break ;
}
// must be only Player
if (( ! target || target -> GetTypeId () != TYPEID_PLAYER ) && ( ! source || source -> GetTypeId () != TYPEID_PLAYER ))
{
sLog . outError ( "SCRIPT_COMMAND_TELEPORT_TO call for non-player (TypeIdSource: %u)(TypeIdTarget: %u), skipping." , source ? source -> GetTypeId () : 0 , target ? target -> GetTypeId () : 0 );
break ;
}
Player * pSource = target && target -> GetTypeId () == TYPEID_PLAYER ? ( Player * ) target : ( Player * ) source ;
pSource -> TeleportTo ( step . script -> datalong , step . script -> x , step . script -> y , step . script -> z , step . script -> o );
break ;
}
case SCRIPT_COMMAND_TEMP_SUMMON_CREATURE :
{
if ( ! step . script -> datalong ) // creature not specified
{
sLog . outError ( "SCRIPT_COMMAND_TEMP_SUMMON_CREATURE call for NULL creature." );
break ;
}
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_TEMP_SUMMON_CREATURE call for NULL world object." );
break ;
}
WorldObject * summoner = dynamic_cast < WorldObject *> ( source );
if ( ! summoner )
{
sLog . outError ( "SCRIPT_COMMAND_TEMP_SUMMON_CREATURE call for non-WorldObject (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
float x = step . script -> x ;
float y = step . script -> y ;
float z = step . script -> z ;
float o = step . script -> o ;
Creature * pCreature = summoner -> SummonCreature ( step . script -> datalong , x , y , z , o , TEMPSUMMON_TIMED_OR_DEAD_DESPAWN , step . script -> datalong2 );
if ( ! pCreature )
{
sLog . outError ( "SCRIPT_COMMAND_TEMP_SUMMON failed for creature (entry: %u)." , step . script -> datalong );
break ;
}
break ;
}
case SCRIPT_COMMAND_RESPAWN_GAMEOBJECT :
{
if ( ! step . script -> datalong ) // gameobject not specified
{
sLog . outError ( "SCRIPT_COMMAND_RESPAWN_GAMEOBJECT call for NULL gameobject." );
break ;
}
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_RESPAWN_GAMEOBJECT call for NULL world object." );
break ;
}
WorldObject * summoner = dynamic_cast < WorldObject *> ( source );
if ( ! summoner )
{
sLog . outError ( "SCRIPT_COMMAND_RESPAWN_GAMEOBJECT call for non-WorldObject (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
GameObject * go = NULL ;
int32 time_to_despawn = step . script -> datalong2 < 5 ? 5 : ( int32 ) step . script -> datalong2 ;
CellPair p ( MaNGOS :: ComputeCellPair ( summoner -> GetPositionX (), summoner -> GetPositionY ()));
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
MaNGOS :: GameObjectWithDbGUIDCheck go_check ( * summoner , step . script -> datalong );
MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > checker ( summoner , go , go_check );
TypeContainerVisitor < MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > , GridTypeMapContainer > object_checker ( checker );
CellLock < GridReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , object_checker , * summoner -> GetMap ());
if ( ! go )
{
sLog . outError ( "SCRIPT_COMMAND_RESPAWN_GAMEOBJECT failed for gameobject(guid: %u)." , step . script -> datalong );
break ;
}
if ( go -> GetGoType () == GAMEOBJECT_TYPE_FISHINGNODE ||
go -> GetGoType () == GAMEOBJECT_TYPE_DOOR ||
go -> GetGoType () == GAMEOBJECT_TYPE_BUTTON ||
go -> GetGoType () == GAMEOBJECT_TYPE_TRAP )
{
sLog . outError ( "SCRIPT_COMMAND_RESPAWN_GAMEOBJECT can not be used with gameobject of type %u (guid: %u)." , uint32 ( go -> GetGoType ()), step . script -> datalong );
break ;
}
if ( go -> isSpawned () )
break ; //gameobject already spawned
go -> SetLootState ( GO_READY );
go -> SetRespawnTime ( time_to_despawn ); //despawn object in ? seconds
go -> GetMap () -> Add ( go );
break ;
}
case SCRIPT_COMMAND_OPEN_DOOR :
{
if ( ! step . script -> datalong ) // door not specified
{
sLog . outError ( "SCRIPT_COMMAND_OPEN_DOOR call for NULL door." );
break ;
}
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_OPEN_DOOR call for NULL unit." );
break ;
}
if ( ! source -> isType ( TYPEMASK_UNIT )) // must be any Unit (creature or player)
{
sLog . outError ( "SCRIPT_COMMAND_OPEN_DOOR call for non-unit (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
Unit * caster = ( Unit * ) source ;
GameObject * door = NULL ;
int32 time_to_close = step . script -> datalong2 < 15 ? 15 : ( int32 ) step . script -> datalong2 ;
CellPair p ( MaNGOS :: ComputeCellPair ( caster -> GetPositionX (), caster -> GetPositionY ()));
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
MaNGOS :: GameObjectWithDbGUIDCheck go_check ( * caster , step . script -> datalong );
MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > checker ( caster , door , go_check );
TypeContainerVisitor < MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > , GridTypeMapContainer > object_checker ( checker );
CellLock < GridReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , object_checker , * caster -> GetMap ());
if ( ! door )
{
sLog . outError ( "SCRIPT_COMMAND_OPEN_DOOR failed for gameobject(guid: %u)." , step . script -> datalong );
break ;
}
if ( door -> GetGoType () != GAMEOBJECT_TYPE_DOOR )
{
sLog . outError ( "SCRIPT_COMMAND_OPEN_DOOR failed for non-door(GoType: %u)." , door -> GetGoType ());
break ;
}
if ( door -> GetGoState () != GO_STATE_READY )
break ; //door already open
door -> UseDoorOrButton ( time_to_close );
if ( target && target -> isType ( TYPEMASK_GAMEOBJECT ) && (( GameObject * ) target ) -> GetGoType () == GAMEOBJECT_TYPE_BUTTON )
(( GameObject * ) target ) -> UseDoorOrButton ( time_to_close );
break ;
}
case SCRIPT_COMMAND_CLOSE_DOOR :
{
if ( ! step . script -> datalong ) // guid for door not specified
{
sLog . outError ( "SCRIPT_COMMAND_CLOSE_DOOR call for NULL door." );
break ;
}
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_CLOSE_DOOR call for NULL unit." );
break ;
}
if ( ! source -> isType ( TYPEMASK_UNIT )) // must be any Unit (creature or player)
{
sLog . outError ( "SCRIPT_COMMAND_CLOSE_DOOR call for non-unit (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
Unit * caster = ( Unit * ) source ;
GameObject * door = NULL ;
int32 time_to_open = step . script -> datalong2 < 15 ? 15 : ( int32 ) step . script -> datalong2 ;
CellPair p ( MaNGOS :: ComputeCellPair ( caster -> GetPositionX (), caster -> GetPositionY ()));
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
MaNGOS :: GameObjectWithDbGUIDCheck go_check ( * caster , step . script -> datalong );
MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > checker ( caster , door , go_check );
TypeContainerVisitor < MaNGOS :: GameObjectSearcher < MaNGOS :: GameObjectWithDbGUIDCheck > , GridTypeMapContainer > object_checker ( checker );
CellLock < GridReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , object_checker , * caster -> GetMap ());
if ( ! door )
{
sLog . outError ( "SCRIPT_COMMAND_CLOSE_DOOR failed for gameobject(guid: %u)." , step . script -> datalong );
break ;
}
if ( door -> GetGoType () != GAMEOBJECT_TYPE_DOOR )
{
sLog . outError ( "SCRIPT_COMMAND_CLOSE_DOOR failed for non-door(GoType: %u)." , door -> GetGoType ());
break ;
}
if ( door -> GetGoState () == GO_STATE_READY )
break ; //door already closed
door -> UseDoorOrButton ( time_to_open );
if ( target && target -> isType ( TYPEMASK_GAMEOBJECT ) && (( GameObject * ) target ) -> GetGoType () == GAMEOBJECT_TYPE_BUTTON )
(( GameObject * ) target ) -> UseDoorOrButton ( time_to_open );
break ;
}
case SCRIPT_COMMAND_QUEST_EXPLORED :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_QUEST_EXPLORED call for NULL source." );
break ;
}
if ( ! target )
{
sLog . outError ( "SCRIPT_COMMAND_QUEST_EXPLORED call for NULL target." );
break ;
}
// when script called for item spell casting then target == (unit or GO) and source is player
WorldObject * worldObject ;
Player * player ;
if ( target -> GetTypeId () == TYPEID_PLAYER )
{
if ( source -> GetTypeId () != TYPEID_UNIT && source -> GetTypeId () != TYPEID_GAMEOBJECT )
{
sLog . outError ( "SCRIPT_COMMAND_QUEST_EXPLORED call for non-creature and non-gameobject (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
worldObject = ( WorldObject * ) source ;
player = ( Player * ) target ;
}
else
{
if ( target -> GetTypeId () != TYPEID_UNIT && target -> GetTypeId () != TYPEID_GAMEOBJECT )
{
sLog . outError ( "SCRIPT_COMMAND_QUEST_EXPLORED call for non-creature and non-gameobject (TypeId: %u), skipping." , target -> GetTypeId ());
break ;
}
if ( source -> GetTypeId () != TYPEID_PLAYER )
{
sLog . outError ( "SCRIPT_COMMAND_QUEST_EXPLORED call for non-player(TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
worldObject = ( WorldObject * ) target ;
player = ( Player * ) source ;
}
// quest id and flags checked at script loading
if ( ( worldObject -> GetTypeId () != TYPEID_UNIT || (( Unit * ) worldObject ) -> isAlive ()) &&
( step . script -> datalong2 == 0 || worldObject -> IsWithinDistInMap ( player , float ( step . script -> datalong2 ))) )
player -> AreaExploredOrEventHappens ( step . script -> datalong );
else
player -> FailQuest ( step . script -> datalong );
break ;
}
case SCRIPT_COMMAND_ACTIVATE_OBJECT :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_ACTIVATE_OBJECT must have source caster." );
break ;
}
if ( ! source -> isType ( TYPEMASK_UNIT ))
{
sLog . outError ( "SCRIPT_COMMAND_ACTIVATE_OBJECT source caster isn't unit (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
if ( ! target )
{
sLog . outError ( "SCRIPT_COMMAND_ACTIVATE_OBJECT call for NULL gameobject." );
break ;
}
if ( target -> GetTypeId () != TYPEID_GAMEOBJECT )
{
sLog . outError ( "SCRIPT_COMMAND_ACTIVATE_OBJECT call for non-gameobject (TypeId: %u), skipping." , target -> GetTypeId ());
break ;
}
Unit * caster = ( Unit * ) source ;
GameObject * go = ( GameObject * ) target ;
go -> Use ( caster );
break ;
}
case SCRIPT_COMMAND_REMOVE_AURA :
{
Object * cmdTarget = step . script -> datalong2 ? source : target ;
if ( ! cmdTarget )
{
sLog . outError ( "SCRIPT_COMMAND_REMOVE_AURA call for NULL %s." , step . script -> datalong2 ? "source" : "target" );
break ;
}
if ( ! cmdTarget -> isType ( TYPEMASK_UNIT ))
{
sLog . outError ( "SCRIPT_COMMAND_REMOVE_AURA %s isn't unit (TypeId: %u), skipping." , step . script -> datalong2 ? "source" : "target" , cmdTarget -> GetTypeId ());
break ;
}
(( Unit * ) cmdTarget ) -> RemoveAurasDueToSpell ( step . script -> datalong );
break ;
}
case SCRIPT_COMMAND_CAST_SPELL :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_CAST_SPELL must have source caster." );
break ;
}
Object * cmdTarget = step . script -> datalong2 & 0x01 ? source : target ;
if ( ! cmdTarget )
{
sLog . outError ( "SCRIPT_COMMAND_CAST_SPELL call for NULL %s." , step . script -> datalong2 & 0x01 ? "source" : "target" );
break ;
}
if ( ! cmdTarget -> isType ( TYPEMASK_UNIT ))
{
sLog . outError ( "SCRIPT_COMMAND_CAST_SPELL %s isn't unit (TypeId: %u), skipping." , step . script -> datalong2 & 0x01 ? "source" : "target" , cmdTarget -> GetTypeId ());
break ;
}
Unit * spellTarget = ( Unit * ) cmdTarget ;
Object * cmdSource = step . script -> datalong2 & 0x02 ? target : source ;
if ( ! cmdSource )
{
sLog . outError ( "SCRIPT_COMMAND_CAST_SPELL call for NULL %s." , step . script -> datalong2 & 0x02 ? "target" : "source" );
break ;
}
if ( ! cmdSource -> isType ( TYPEMASK_UNIT ))
{
sLog . outError ( "SCRIPT_COMMAND_CAST_SPELL %s isn't unit (TypeId: %u), skipping." , step . script -> datalong2 & 0x02 ? "target" : "source" , cmdSource -> GetTypeId ());
break ;
}
Unit * spellSource = ( Unit * ) cmdSource ;
//TODO: when GO cast implemented, code below must be updated accordingly to also allow GO spell cast
spellSource -> CastSpell ( spellTarget , step . script -> datalong , false );
break ;
}
case SCRIPT_COMMAND_LOAD_PATH :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_START_MOVE is tried to apply to NON-existing unit." );
break ;
}
if ( ! source -> isType ( TYPEMASK_UNIT ))
{
sLog . outError ( "SCRIPT_COMMAND_START_MOVE source mover isn't unit (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
if ( ! WaypointMgr . GetPath ( step . script -> datalong ))
{
sLog . outError ( "SCRIPT_COMMAND_START_MOVE source mover has an invallid path, skipping." , step . script -> datalong2 );
break ;
}
dynamic_cast < Unit *> ( source ) -> GetMotionMaster () -> MovePath ( step . script -> datalong , step . script -> datalong2 );
break ;
}
case SCRIPT_COMMAND_CALLSCRIPT_TO_UNIT :
{
if ( ! step . script -> datalong || ! step . script -> datalong2 )
{
sLog . outError ( "SCRIPT_COMMAND_CALLSCRIPT calls invallid db_script_id or lowguid not present: skipping." );
break ;
}
//our target
Creature * target = NULL ;
if ( source ) //using grid searcher
{
CellPair p ( Trinity :: ComputeCellPair ((( Unit * ) source ) -> GetPositionX (), (( Unit * ) source ) -> GetPositionY ()));
Cell cell ( p );
cell . data . Part . reserved = ALL_DISTRICT ;
//sLog.outDebug("Attempting to find Creature: Db GUID: %i", step.script->datalong);
Trinity :: CreatureWithDbGUIDCheck target_check ((( Unit * ) source ), step . script -> datalong );
Trinity :: CreatureSearcher < Trinity :: CreatureWithDbGUIDCheck > checker ((( Unit * ) source ), target , target_check );
TypeContainerVisitor < Trinity :: CreatureSearcher < Trinity :: CreatureWithDbGUIDCheck > , GridTypeMapContainer > unit_checker ( checker );
CellLock < GridReadGuard > cell_lock ( cell , p );
cell_lock -> Visit ( cell_lock , unit_checker , * ((( Unit * ) source ) -> GetMap ()));
}
else //check hashmap holders
{
if ( CreatureData const * data = objmgr . GetCreatureData ( step . script -> datalong ))
target = ObjectAccessor :: GetObjectInWorld < Creature > ( data -> mapid , data -> posX , data -> posY , MAKE_NEW_GUID ( step . script -> datalong , data -> id , HIGHGUID_UNIT ), target );
}
//sLog.outDebug("attempting to pass target...");
if ( ! target )
break ;
//sLog.outDebug("target passed");
//Lets choose our ScriptMap map
ScriptMapMap * datamap = NULL ;
switch ( step . script -> dataint )
{
case 1 : //QUEST END SCRIPTMAP
datamap = & sQuestEndScripts ;
break ;
case 2 : //QUEST START SCRIPTMAP
datamap = & sQuestStartScripts ;
break ;
case 3 : //SPELLS SCRIPTMAP
datamap = & sSpellScripts ;
break ;
case 4 : //GAMEOBJECTS SCRIPTMAP
datamap = & sGameObjectScripts ;
break ;
case 5 : //EVENTS SCRIPTMAP
datamap = & sEventScripts ;
break ;
case 6 : //WAYPOINTS SCRIPTMAP
datamap = & sWaypointScripts ;
break ;
default :
sLog . outError ( "SCRIPT_COMMAND_CALLSCRIPT ERROR: no scriptmap present... ignoring" );
break ;
}
//if no scriptmap present...
if ( ! datamap )
break ;
uint32 script_id = step . script -> datalong2 ;
//insert script into schedule but do not start it
ScriptsStart ( * datamap , script_id , target , NULL /*, false*/ );
break ;
}
case SCRIPT_COMMAND_KILL :
{
if ( ! source || (( Creature * ) source ) -> isDead ())
break ;
(( Creature * ) source ) -> DealDamage ((( Creature * ) source ), (( Creature * ) source ) -> GetHealth (), NULL , DIRECT_DAMAGE , SPELL_SCHOOL_MASK_NORMAL , NULL , false );
switch ( step . script -> dataint )
{
case 0 : break ; //return false not remove corpse
case 1 : (( Creature * ) source ) -> RemoveCorpse (); break ;
}
break ;
}
case SCRIPT_COMMAND_PLAY_SOUND :
{
if ( ! source )
{
sLog . outError ( "SCRIPT_COMMAND_PLAY_SOUND call for NULL creature." );
break ;
}
WorldObject * pSource = dynamic_cast < WorldObject *> ( source );
if ( ! pSource )
{
sLog . outError ( "SCRIPT_COMMAND_PLAY_SOUND call for non-world object (TypeId: %u), skipping." , source -> GetTypeId ());
break ;
}
// bitmask: 0/1=anyone/target, 0/2=with distance dependent
Player * pTarget = NULL ;
if ( step . script -> datalong2 & 1 )
{
if ( ! target )
{
sLog . outError ( "SCRIPT_COMMAND_PLAY_SOUND in targeted mode call for NULL target." );
break ;
}
if ( target -> GetTypeId () != TYPEID_PLAYER )
{
sLog . outError ( "SCRIPT_COMMAND_PLAY_SOUND in targeted mode call for non-player (TypeId: %u), skipping." , target -> GetTypeId ());
break ;
}
pTarget = ( Player * ) target ;
}
// bitmask: 0/1=anyone/target, 0/2=with distance dependent
if ( step . script -> datalong2 & 2 )
pSource -> PlayDistanceSound ( step . script -> datalong , pTarget );
else
pSource -> PlayDirectSound ( step . script -> datalong , pTarget );
break ;
}
default :
sLog . outError ( "Unknown script command %u called." , step . script -> command );
break ;
}
m_scriptSchedule . erase ( iter );
sWorld . DecreaseScheduledScriptCount ();
iter = m_scriptSchedule . begin ();
}
return ;
}
2009-04-20 20:28:19 -05:00
Creature *
Map :: GetCreature ( uint64 guid )
{
2009-05-26 00:53:20 -05:00
Creature * ret = NULL ;
if ( IS_CREATURE_GUID ( guid ))
ret = ObjectAccessor :: GetObjectInWorld ( guid , ( Creature * ) NULL );
else if ( IS_VEHICLE_GUID ( guid ))
ret = ObjectAccessor :: GetObjectInWorld ( guid , ( Vehicle * ) NULL );
2009-04-20 20:28:19 -05:00
if ( ! ret )
return NULL ;
if ( ret -> GetMapId () != GetId ())
return NULL ;
if ( ret -> GetInstanceId () != GetInstanceId ())
return NULL ;
return ret ;
}
GameObject *
Map :: GetGameObject ( uint64 guid )
{
GameObject * ret = ObjectAccessor :: GetObjectInWorld ( guid , ( GameObject * ) NULL );
if ( ! ret )
return NULL ;
if ( ret -> GetMapId () != GetId ())
return NULL ;
if ( ret -> GetInstanceId () != GetInstanceId ())
return NULL ;
return ret ;
}
2008-11-29 13:08:23 -06:00
2009-04-20 20:28:19 -05:00
DynamicObject *
Map :: GetDynamicObject ( uint64 guid )
{
DynamicObject * ret = ObjectAccessor :: GetObjectInWorld ( guid , ( DynamicObject * ) NULL );
if ( ! ret )
return NULL ;
if ( ret -> GetMapId () != GetId ())
return NULL ;
if ( ret -> GetInstanceId () != GetInstanceId ())
return NULL ;
return ret ;
}
2009-08-01 00:20:32 +02:00
void Map :: UpdateIteratorBack ( Player * player )
{
if ( m_mapRefIter == player -> GetMapRef ())
m_mapRefIter = m_mapRefIter -> nocheck_prev ();
}