Skip to content

Commit

Permalink
Store and use Map* pointer in WorldObject instead map ids for speedup
Browse files Browse the repository at this point in the history
Also some code logic cleanups.
Changes let make more cleanups in base map access and other places,
but this chnages not inlcuded in patch.

(based on commit b4431f2)
(based on commit 59fa774)
(based on commit 2552914)
(based on commit a2e3c59)

Thanks to mns for preparing and testing backport diff :)

Signed-off-by: VladimirMangos <vladimir@getmangos.com>
  • Loading branch information
Ambal authored and VladimirMangos committed May 12, 2010
1 parent 3bedcd8 commit 2fc3f59
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 201 deletions.
13 changes: 8 additions & 5 deletions src/game/Corpse.cpp
Expand Up @@ -74,12 +74,15 @@ bool Corpse::Create( uint32 guidlow )

bool Corpse::Create( uint32 guidlow, Player *owner)
{
SetInstanceId(owner->GetInstanceId());

WorldObject::_Create(guidlow, HIGHGUID_CORPSE, owner->GetMapId());
ASSERT(owner);

WorldObject::_Create(guidlow, HIGHGUID_CORPSE);
Relocate(owner->GetPositionX(), owner->GetPositionY(), owner->GetPositionZ(), owner->GetOrientation());

//we need to assign owner's map for corpse
//in other way we will get a crash in Corpse::SaveToDB()
SetMap(owner->GetMap());

if(!IsPositionValid())
{
sLog.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
Expand Down Expand Up @@ -204,8 +207,8 @@ bool Corpse::LoadFromDB(uint32 guid, Field *fields)
SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE));

// place
SetInstanceId(instanceid);
SetMapId(mapid);
SetLocationInstanceId(instanceid);
SetLocationMapId(mapid);
Relocate(positionX, positionY, positionZ, ort);

if(!IsPositionValid())
Expand Down
19 changes: 8 additions & 11 deletions src/game/Creature.cpp
Expand Up @@ -602,14 +602,20 @@ bool Creature::AIM_Initialize()

bool Creature::Create(uint32 guidlow, Map *map, uint32 Entry, uint32 team, const CreatureData *data)
{
SetMapId(map->GetId());
SetInstanceId(map->GetInstanceId());
ASSERT(map);
SetMap(map);

//oX = x; oY = y; dX = x; dY = y; m_moveTime = 0; m_startMove = 0;
const bool bResult = CreateFromProto(guidlow, Entry, team, data);

if (bResult)
{
//Notify the map's instance data.
//Only works if you create the object in it, not if it is moves to that map.
//Normally non-players do not teleport to other maps.
if(map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
((InstanceMap*)map)->GetInstanceData()->OnCreatureCreate(this);

switch (GetCreatureInfo()->rank)
{
case CREATURE_ELITE_RARE:
Expand Down Expand Up @@ -1040,15 +1046,6 @@ bool Creature::CreateFromProto(uint32 guidlow, uint32 Entry, uint32 team, const
if (!UpdateEntry(Entry, team, data, false))
return false;

//Notify the map's instance data.
//Only works if you create the object in it, not if it is moves to that map.
//Normally non-players do not teleport to other maps.
Map *map = MapManager::Instance().FindMap(GetMapId(), GetInstanceId());
if(map && map->IsDungeon() && ((InstanceMap*)map)->GetInstanceData())
{
((InstanceMap*)map)->GetInstanceData()->OnCreatureCreate(this);
}

return true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/game/DynamicObject.cpp
Expand Up @@ -57,9 +57,8 @@ void DynamicObject::RemoveFromWorld()

bool DynamicObject::Create( uint32 guidlow, Unit *caster, uint32 spellId, SpellEffectIndex effIndex, float x, float y, float z, int32 duration, float radius )
{
SetInstanceId(caster->GetInstanceId());

WorldObject::_Create(guidlow, HIGHGUID_DYNAMICOBJECT, caster->GetMapId());
WorldObject::_Create(guidlow, HIGHGUID_DYNAMICOBJECT);
SetMap(caster->GetMap());
Relocate(x, y, z, 0);

if(!IsPositionValid())
Expand Down
4 changes: 2 additions & 2 deletions src/game/GameObject.cpp
Expand Up @@ -98,9 +98,9 @@ void GameObject::RemoveFromWorld()

bool GameObject::Create(uint32 guidlow, uint32 name_id, Map *map, float x, float y, float z, float ang, float rotation0, float rotation1, float rotation2, float rotation3, uint32 animprogress, GOState go_state)
{
ASSERT(map);
Relocate(x,y,z,ang);
SetMapId(map->GetId());
SetInstanceId(map->GetInstanceId());
SetMap(map);

if(!IsPositionValid())
{
Expand Down
58 changes: 35 additions & 23 deletions src/game/Map.cpp
Expand Up @@ -141,14 +141,12 @@ void Map::LoadMap(int gx,int gy, bool reload)
if(GridMaps[gx][gy])
return;

Map* baseMap = const_cast<Map*>(MapManager::Instance().CreateBaseMap(i_id));

// load grid map for base map
if (!baseMap->GridMaps[gx][gy])
baseMap->EnsureGridCreated(GridPair(63-gx,63-gy));
if (!m_parentMap->GridMaps[gx][gy])
m_parentMap->EnsureGridCreated(GridPair(63-gx,63-gy));

((MapInstanced*)(baseMap))->AddGridMapReference(GridPair(gx,gy));
GridMaps[gx][gy] = baseMap->GridMaps[gx][gy];
((MapInstanced*)(m_parentMap))->AddGridMapReference(GridPair(gx,gy));
GridMaps[gx][gy] = m_parentMap->GridMaps[gx][gy];
return;
}

Expand Down Expand Up @@ -203,12 +201,12 @@ void Map::DeleteStateMachine()
delete si_GridStates[GRID_STATE_REMOVAL];
}

Map::Map(uint32 id, time_t expiry, uint32 InstanceId, uint8 SpawnMode)
Map::Map(uint32 id, time_t expiry, uint32 InstanceId, uint8 SpawnMode, Map* _parent)
: i_mapEntry (sMapStore.LookupEntry(id)), i_spawnMode(SpawnMode),
i_id(id), i_InstanceId(InstanceId), m_unloadTimer(0),
m_VisibleDistance(DEFAULT_VISIBILITY_DISTANCE),
m_activeNonPlayersIter(m_activeNonPlayers.end()),
i_gridExpiry(expiry)
i_gridExpiry(expiry), m_parentMap(_parent ? _parent : this)
{
for(unsigned int idx=0; idx < MAX_NUMBER_OF_GRIDS; ++idx)
{
Expand Down Expand Up @@ -325,6 +323,13 @@ void Map::DeleteFromWorld(T* obj)
delete obj;
}

template<>
void Map::DeleteFromWorld(Player* pl)
{
ObjectAccessor::Instance().RemoveObject(pl);
delete pl;
}

template<class T>
void Map::AddNotifier(T* , Cell const& , CellPair const& )
{
Expand Down Expand Up @@ -429,8 +434,7 @@ void Map::LoadGrid(const Cell& cell, bool no_unload)
bool Map::Add(Player *player)
{
player->GetMapRef().link(this, player);

player->SetInstanceId(GetInstanceId());
player->SetMap(this);

// update player state for other player and visa-versa
CellPair p = MaNGOS::ComputeCellPair(player->GetPositionX(), player->GetPositionY());
Expand Down Expand Up @@ -461,6 +465,8 @@ Map::Add(T *obj)
return;
}

obj->SetMap(this);

Cell cell(p);
if(obj->isActiveObject())
EnsureGridLoadedAtEnter(cell);
Expand Down Expand Up @@ -713,6 +719,11 @@ void Map::Update(const uint32 &t_diff)

void Map::Remove(Player *player, bool remove)
{
if(remove)
player->CleanupsBeforeDelete();
else
player->RemoveFromWorld();

// this may be called during Map::Update
// after decrement+unlink, ++m_mapRefIter will continue correctly
// when the first element of the list is being removed
Expand All @@ -725,7 +736,7 @@ void Map::Remove(Player *player, bool remove)
if(p.x_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP || p.y_coord >= TOTAL_NUMBER_OF_CELLS_PER_MAP)
{
// invalid coordinates
player->RemoveFromWorld();
player->ResetMap();

if( remove )
DeleteFromWorld(player);
Expand All @@ -745,13 +756,13 @@ void Map::Remove(Player *player, bool remove)
NGridType *grid = getNGrid(cell.GridX(), cell.GridY());
ASSERT(grid != NULL);

player->RemoveFromWorld();
RemoveFromGrid(player,grid,cell);

SendRemoveTransports(player);
UpdateObjectVisibility(player,cell,p);
UpdateObjectsVisibilityFor(player,cell,p);

player->ResetMap();
if( remove )
DeleteFromWorld(player);
}
Expand All @@ -778,11 +789,16 @@ Map::Remove(T *obj, bool remove)
if(obj->isActiveObject())
RemoveFromActive(obj);

obj->RemoveFromWorld();
if(remove)
obj->CleanupsBeforeDelete();
else
obj->RemoveFromWorld();

RemoveFromGrid(obj,grid,cell);

UpdateObjectVisibility(obj,cell,p);

obj->ResetMap();
if( remove )
{
// if option set then object already saved at this moment
Expand Down Expand Up @@ -1006,7 +1022,8 @@ bool Map::UnloadGrid(const uint32 &x, const uint32 &y, bool pForce)
VMAP::VMapFactory::createOrGetVMapManager()->unloadMap(GetId(), gx, gy);
}
else
((MapInstanced*)(MapManager::Instance().CreateBaseMap(i_id)))->RemoveGridMapReference(GridPair(gx, gy));
((MapInstanced*)m_parentMap)->RemoveGridMapReference(GridPair(gx, gy));

GridMaps[gx][gy] = NULL;
}
DEBUG_LOG("Unloading grid[%u,%u] for map %u finished", x,y, i_id);
Expand Down Expand Up @@ -1940,9 +1957,6 @@ void Map::RemoveAllObjectsInRemoveList()
Remove((GameObject*)obj,true);
break;
case TYPEID_UNIT:
// in case triggered sequence some spell can continue casting after prev CleanupsBeforeDelete call
// make sure that like sources auras/etc removed before destructor start
((Creature*)obj)->CleanupsBeforeDelete ();
Remove((Creature*)obj,true);
break;
default:
Expand Down Expand Up @@ -2062,8 +2076,8 @@ template void Map::Remove(DynamicObject *, bool);

/* ******* Dungeon Instance Maps ******* */

InstanceMap::InstanceMap(uint32 id, time_t expiry, uint32 InstanceId, uint8 SpawnMode)
: Map(id, expiry, InstanceId, SpawnMode),
InstanceMap::InstanceMap(uint32 id, time_t expiry, uint32 InstanceId, uint8 SpawnMode, Map* _parent)
: Map(id, expiry, InstanceId, SpawnMode, _parent),
m_resetAfterUnload(false), m_unloadWhenEmpty(false),
i_data(NULL), i_script_id(0)
{
Expand Down Expand Up @@ -2404,8 +2418,8 @@ void InstanceMap::SetResetSchedule(bool on)

/* ******* Battleground Instance Maps ******* */

BattleGroundMap::BattleGroundMap(uint32 id, time_t expiry, uint32 InstanceId)
: Map(id, expiry, InstanceId, REGULAR_DIFFICULTY)
BattleGroundMap::BattleGroundMap(uint32 id, time_t expiry, uint32 InstanceId, Map* _parent)
: Map(id, expiry, InstanceId, REGULAR_DIFFICULTY, _parent)
{
//lets initialize visibility distance for BG/Arenas
BattleGroundMap::InitVisibilityDistance();
Expand Down Expand Up @@ -3268,8 +3282,6 @@ void Map::SendObjectUpdates()
{
Object* obj = *i_objectsToClientUpdate.begin();
i_objectsToClientUpdate.erase(i_objectsToClientUpdate.begin());
if (!obj)
continue;
obj->BuildUpdateData(update_players);
}

Expand Down
11 changes: 8 additions & 3 deletions src/game/Map.h
Expand Up @@ -217,7 +217,7 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
{
friend class MapReference;
public:
Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
Map(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent = NULL);
virtual ~Map();

// currently unused for normal maps
Expand Down Expand Up @@ -282,6 +282,8 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
static void InitStateMachine();
static void DeleteStateMachine();

Map const * GetParent() const { return m_parentMap; }

// some calls like isInWater should not use vmaps due to processor power
// can return INVALID_HEIGHT if under z+2 z coord not found height
float GetHeight(float x, float y, float z, bool pCheckVMap=true) const;
Expand Down Expand Up @@ -460,6 +462,9 @@ class MANGOS_DLL_SPEC Map : public GridRefManager<NGridType>, public MaNGOS::Obj
ActiveNonPlayers::iterator m_activeNonPlayersIter;
TypeUnorderedMapContainer<AllMapStoredObjectTypes> m_objectsStore;
private:
//used for fast base_map (e.g. MapInstanced class object) search for
//InstanceMaps and BattleGroundMaps...
Map* m_parentMap;

NGridType* i_grids[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
GridMap *GridMaps[MAX_NUMBER_OF_GRIDS][MAX_NUMBER_OF_GRIDS];
Expand Down Expand Up @@ -522,7 +527,7 @@ enum InstanceResetMethod
class MANGOS_DLL_SPEC InstanceMap : public Map
{
public:
InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode);
InstanceMap(uint32 id, time_t, uint32 InstanceId, uint8 SpawnMode, Map* _parent);
~InstanceMap();
bool Add(Player *);
void Remove(Player *, bool);
Expand All @@ -548,7 +553,7 @@ class MANGOS_DLL_SPEC InstanceMap : public Map
class MANGOS_DLL_SPEC BattleGroundMap : public Map
{
public:
BattleGroundMap(uint32 id, time_t, uint32 InstanceId);
BattleGroundMap(uint32 id, time_t, uint32 InstanceId, Map* _parent);
~BattleGroundMap();

bool Add(Player *);
Expand Down

0 comments on commit 2fc3f59

Please sign in to comment.