1
0
Fork 0

Fixed a race condition when adding a player to a world.

This commit is contained in:
madmaxoft 2014-06-10 18:25:53 +02:00
parent 6c43799cc5
commit 366ecf9dfd
3 changed files with 29 additions and 4 deletions

View File

@ -1665,6 +1665,30 @@ void cChunkMap::AddEntity(cEntity * a_Entity)
void cChunkMap::AddEntityIfNotPresent(cEntity * a_Entity)
{
cCSLock Lock(m_CSLayers);
cChunkPtr Chunk = GetChunkNoGen(a_Entity->GetChunkX(), ZERO_CHUNK_Y, a_Entity->GetChunkZ());
if (
(Chunk == NULL) || // Chunk not present at all
(!Chunk->IsValid() && !a_Entity->IsPlayer()) // Chunk present, but no valid data; players need to spawn in such chunks (#953)
)
{
LOGWARNING("Entity at %p (%s, ID %d) spawning in a non-existent chunk, the entity is lost.",
a_Entity, a_Entity->GetClass(), a_Entity->GetUniqueID()
);
return;
}
if (!Chunk->HasEntity(a_Entity->GetUniqueID()))
{
Chunk->AddEntity(a_Entity);
}
}
bool cChunkMap::HasEntity(int a_UniqueID)
{
cCSLock Lock(m_CSLayers);

View File

@ -199,6 +199,10 @@ public:
/** Adds the entity to its appropriate chunk, takes ownership of the entity pointer */
void AddEntity(cEntity * a_Entity);
/** Adds the entity to its appropriate chunk, if the entity is not already added.
Takes ownership of the entity pointer */
void AddEntityIfNotPresent(cEntity * a_Entity);
/** Returns true if the entity with specified ID is present in the chunks */
bool HasEntity(int a_EntityID);

View File

@ -3148,10 +3148,7 @@ void cWorld::AddQueuedPlayers(void)
(*itr)->SetWorld(this);
// Add to chunkmap, if not already there (Spawn vs MoveToWorld):
if (!m_ChunkMap->HasEntity((*itr)->GetUniqueID()))
{
m_ChunkMap->AddEntity(*itr);
}
m_ChunkMap->AddEntityIfNotPresent(*itr);
} // for itr - PlayersToAdd[]
} // Lock(m_CSPlayers)