Fix players spawned at (0,0,0) in some rare cases instead of static_spawnpoint, if set

Approved by: @kwoelkr
This commit is contained in:
Loic Blot 2015-04-03 09:04:02 +02:00
parent aa340fd857
commit 470de10de3
2 changed files with 17 additions and 25 deletions

@ -2522,7 +2522,7 @@ void Server::RespawnPlayer(u16 peer_id)
bool repositioned = m_script->on_respawnplayer(playersao); bool repositioned = m_script->on_respawnplayer(playersao);
if(!repositioned){ if(!repositioned){
v3f pos = findSpawnPos(m_env->getServerMap()); v3f pos = findSpawnPos();
// setPos will send the new position to client // setPos will send the new position to client
playersao->setPos(pos); playersao->setPos(pos);
} }
@ -3179,23 +3179,24 @@ std::string Server::getBuiltinLuaPath()
return porting::path_share + DIR_DELIM + "builtin"; return porting::path_share + DIR_DELIM + "builtin";
} }
v3f findSpawnPos(ServerMap &map) v3f Server::findSpawnPos()
{ {
//return v3f(50,50,50)*BS; ServerMap &map = m_env->getServerMap();
v3f nodeposf;
if (g_settings->getV3FNoEx("static_spawnpoint", nodeposf)) {
return nodeposf * BS;
}
v3s16 nodepos; // Default position is static_spawnpoint
// We will return it if we don't found a good place
v3s16 nodepos(nodeposf.X, nodeposf.Y, nodeposf.Z);
#if 0
nodepos = v2s16(0,0);
groundheight = 20;
#endif
#if 1
s16 water_level = map.getWaterLevel(); s16 water_level = map.getWaterLevel();
bool is_good = false;
// Try to find a good place a few times // Try to find a good place a few times
for(s32 i=0; i<1000; i++) for(s32 i = 0; i < 1000 && !is_good; i++) {
{
s32 range = 1 + i; s32 range = 1 + i;
// We're going to try to throw the player to this position // We're going to try to throw the player to this position
v2s16 nodepos2d = v2s16( v2s16 nodepos2d = v2s16(
@ -3210,7 +3211,7 @@ v3f findSpawnPos(ServerMap &map)
continue; continue;
nodepos = v3s16(nodepos2d.X, groundheight, nodepos2d.Y); nodepos = v3s16(nodepos2d.X, groundheight, nodepos2d.Y);
bool is_good = false;
s32 air_count = 0; s32 air_count = 0;
for (s32 i = 0; i < 10; i++) { for (s32 i = 0; i < 10; i++) {
v3s16 blockpos = getNodeBlockPos(nodepos); v3s16 blockpos = getNodeBlockPos(nodepos);
@ -3225,13 +3226,7 @@ v3f findSpawnPos(ServerMap &map)
} }
nodepos.Y++; nodepos.Y++;
} }
if(is_good){
// Found a good place
//infostream<<"Searched through "<<i<<" places."<<std::endl;
break;
} }
}
#endif
return intToFloat(nodepos, BS); return intToFloat(nodepos, BS);
} }
@ -3274,7 +3269,7 @@ PlayerSAO* Server::emergePlayer(const char *name, u16 peer_id)
// Set player position // Set player position
infostream<<"Server: Finding spawn place for player \"" infostream<<"Server: Finding spawn place for player \""
<<name<<"\""<<std::endl; <<name<<"\""<<std::endl;
v3f pos = findSpawnPos(m_env->getServerMap()); v3f pos = findSpawnPos();
player->setPosition(pos); player->setPosition(pos);
// Make sure the player is saved // Make sure the player is saved

@ -63,11 +63,6 @@ enum ClientDeletionReason {
CDR_DENY CDR_DENY
}; };
/*
Some random functions
*/
v3f findSpawnPos(ServerMap &map);
class MapEditEventIgnorer class MapEditEventIgnorer
{ {
public: public:
@ -474,6 +469,8 @@ private:
void DeleteClient(u16 peer_id, ClientDeletionReason reason); void DeleteClient(u16 peer_id, ClientDeletionReason reason);
void UpdateCrafting(Player *player); void UpdateCrafting(Player *player);
v3f findSpawnPos();
// When called, connection mutex should be locked // When called, connection mutex should be locked
RemoteClient* getClient(u16 peer_id,ClientState state_min=CS_Active); RemoteClient* getClient(u16 peer_id,ClientState state_min=CS_Active);
RemoteClient* getClientNoEx(u16 peer_id,ClientState state_min=CS_Active); RemoteClient* getClientNoEx(u16 peer_id,ClientState state_min=CS_Active);