Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/game/Object/CorpseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ void CorpseManager::AddCorpsesToGrid(GridPair const& gridpair, GridType& grid, M
return;
}

// the same grid coordinates exist on every map
if (corpse->GetMapId() != map->GetId())
{
return;
}

// On an instanceable map take only the corpses belonging to this instance.
if (map->Instanceable())
{
Expand Down
3 changes: 2 additions & 1 deletion src/game/Object/Creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2429,7 +2429,8 @@ bool Creature::IsVisibleInGridForPlayer(Player* pl) const
if (IsAlive())
{
Corpse* corpse = pl->GetCorpse();
if (corpse)
// a corpse can be out of the world (its grid unloaded, its battleground ended): no map to compare
if (corpse && corpse->IsInWorld())
{
// 20 - aggro distance for same level, 25 - max additional distance if player level less that creature level
if (InReach(*corpse, *this, (20 + 25)*sWorld.getConfig(CONFIG_FLOAT_RATE_CREATURE_AGGRO)))
Expand Down
5 changes: 5 additions & 0 deletions src/game/Object/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,11 @@ class WorldObject : public Object
// used to check all object's GetMap() calls when object is not in world!
void ResetMap();

/// Drops the map pointer of an object that outlives its map: a player corpse stays in the
/// CorpseManager after its grid unloaded, and its map can be destroyed (an ended battleground,
/// an unloaded instance). FindMap() is NULL afterwards; the grid loader sets the map again.
void ForgetMap() { m_currMap = NULL; }

TerrainInfo const* GetTerrain() const;

void AddToClientUpdateList() override;
Expand Down
4 changes: 3 additions & 1 deletion src/game/Object/PlayerVisibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ bool Player::IsVisibleInGridForPlayer(Player* pl) const
// Dead player see live players near own corpse
if (IsAlive())
{
if (Corpse* corpse = pl->GetCorpse())
Corpse* corpse = pl->GetCorpse();
// a corpse can be out of the world (its grid unloaded, its battleground ended): no map to compare
if (corpse && corpse->IsInWorld())
{
// 20 - aggro distance for same level, 25 - max additional distance if player level less that creature level
if (InReach(*corpse, *this, (20 + 25) * sWorld.getConfig(CONFIG_FLOAT_RATE_CREATURE_AGGRO)))
Expand Down
5 changes: 3 additions & 2 deletions src/game/Object/WorldObjectPosition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,9 @@ static bool InCommonFrame(WorldObject const& a, WorldObject const& b,
return true;
}

TransportMap* va = a.GetMap() ? a.GetMap()->AsTransport() : NULL;
TransportMap* vb = b.GetMap() ? b.GetMap()->AsTransport() : NULL;
// FindMap, not GetMap: a player corpse whose grid unloaded has no map any more (GetMap asserts)
TransportMap* va = a.FindMap() ? a.FindMap()->AsTransport() : NULL;
TransportMap* vb = b.FindMap() ? b.FindMap()->AsTransport() : NULL;

if (!va && !vb)
{
Expand Down
3 changes: 2 additions & 1 deletion src/game/WorldHandlers/MiscHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,8 @@ void WorldSession::HandleReclaimCorpseOpcode(WorldPacket& recv_data)

Corpse* corpse = GetPlayer()->GetCorpse();

if (!corpse)
// out of the world, the corpse has no map to reach it in (its grid unloaded, its battleground ended)
if (!corpse || !corpse->IsInWorld())
{
return;
}
Expand Down
35 changes: 35 additions & 0 deletions src/game/WorldHandlers/ObjectGridLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,41 @@ ObjectGridUnloader::Unload(GridType& grid)
grid.Visit(unloader);
}

void ObjectGridUnloader::DetachCorpsesN()
{
// the resurrectable player corpses are world objects and not deleted with the grid; only a whole
// grid detaches them - AddCorpsesToGrid puts every corpse of the grid in the cell that loaded it,
// so a single cell unload would also detach corpses of cells that stay loaded
ObjectWorldUnloader worldUnloader;
TypeContainerVisitor<ObjectWorldUnloader, WorldTypeMapContainer > corpses(worldUnloader);
for (unsigned int x = 0; x < MAX_NUMBER_OF_CELLS; ++x)
{
for (unsigned int y = 0; y < MAX_NUMBER_OF_CELLS; ++y)
{
i_grid(x, y).Visit(corpses);
}
}
}

void ObjectWorldUnloader::Visit(CorpseMapType& m)
{
// collect first: RemoveFromWorld must not run while the references are walked
std::vector<Corpse*> corpses;
for (CorpseMapType::iterator iter = m.begin(); iter != m.end(); ++iter)
{
corpses.push_back(iter->getSource());
}

for (Corpse* corpse : corpses)
{
if (corpse->IsInWorld())
{
corpse->RemoveFromWorld();
}
corpse->ForgetMap();
Comment on lines +475 to +477

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, and that matching was already wrong before this change (a corpse could be linked into another map's grid with the same coordinates). Fixed in 30fedf4: CorpseManager::AddCorpsesToGrid() now skips corpses whose map ID is not the loading map's, before the instance check.

}
}

template<class T>
void ObjectGridUnloader::Visit(GridRefManager<T>& m)
{
Expand Down
14 changes: 14 additions & 0 deletions src/game/WorldHandlers/ObjectGridLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,28 @@ class ObjectGridUnloader
Unload(i_grid(x, y));
}
}
DetachCorpsesN();
}

void Unload(GridType& grid);
void DetachCorpsesN();
template<class T> void Visit(GridRefManager<T>& m);
private:
NGridType& i_grid;
};

/**
* Takes the player corpses of a grid that unloads out of the world. The CorpseManager owns them and
* they outlive the grid - and the map, which an ended battleground or an unloaded instance deletes -
* so they must not keep pointing at it; LoadHelper puts them back when the grid loads again.
*/
class ObjectWorldUnloader
{
public:
void Visit(CorpseMapType& m);
template<class T> void Visit(GridRefManager<T>&) {}
};

class ObjectGridStoper
{
public:
Expand Down