Environment cleanup

* Move client list to ServerEnvironment and use RemotePlayer members instead of Player
* ClientEnvironment only use setLocalPlayer to specify the current player
* Remove ClientEnvironment dead code on player list (in fact other players are CAO not Player objects)
* Drop LocalPlayer::getPlayer(xxx) functions which aren't used.
* Improve a little bit performance by using const ref list for ClientEnvironment::getPlayerNames() & Client::getConnectedPlayerNames()
* Drop isLocal() function from (Local)Player which is not needed anymore because of previous changes

This change permits to cleanup shared client list which is very old code.
ClientEnvironment doesn't use player list anymore, it only contains the local player, as addPlayer is only called from Client constructor client side.
Clients are only CAO on client side, this cleanup permit to remove confusion about player list.
This commit is contained in:
Loic Blot 2016-10-08 23:13:38 +02:00 committed by Ner'zhul
parent b3fc133442
commit 70f104be07
8 changed files with 97 additions and 162 deletions

@ -257,7 +257,7 @@ Client::Client(
m_localdb(NULL) m_localdb(NULL)
{ {
// Add local player // Add local player
m_env.addPlayer(new LocalPlayer(this, playername)); m_env.setLocalPlayer(new LocalPlayer(this, playername));
m_mapper = new Mapper(device, this); m_mapper = new Mapper(device, this);
m_cache_save_interval = g_settings->getU16("server_map_save_interval"); m_cache_save_interval = g_settings->getU16("server_map_save_interval");
@ -1418,8 +1418,9 @@ Inventory* Client::getInventory(const InventoryLocation &loc)
break; break;
case InventoryLocation::PLAYER: case InventoryLocation::PLAYER:
{ {
LocalPlayer *player = m_env.getPlayer(loc.name.c_str()); // Check if we are working with local player inventory
if(!player) LocalPlayer *player = m_env.getLocalPlayer();
if (!player || strcmp(player->getName(), loc.name.c_str()) != 0)
return NULL; return NULL;
return &player->inventory; return &player->inventory;
} }
@ -1500,11 +1501,6 @@ ClientActiveObject * Client::getSelectedActiveObject(
return NULL; return NULL;
} }
std::list<std::string> Client::getConnectedPlayerNames()
{
return m_env.getPlayerNames();
}
float Client::getAnimationTime() float Client::getAnimationTime()
{ {
return m_animation_time; return m_animation_time;
@ -1664,7 +1660,7 @@ void Client::addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server, bool ur
ClientEvent Client::getClientEvent() ClientEvent Client::getClientEvent()
{ {
ClientEvent event; ClientEvent event;
if(m_client_event_queue.size() == 0) { if (m_client_event_queue.empty()) {
event.type = CE_NONE; event.type = CE_NONE;
} }
else { else {

@ -452,7 +452,10 @@ public:
core::line3d<f32> shootline_on_map core::line3d<f32> shootline_on_map
); );
std::list<std::string> getConnectedPlayerNames(); const std::list<std::string> &getConnectedPlayerNames()
{
return m_env.getPlayerNames();
}
float getAnimationTime(); float getAnimationTime();

@ -653,10 +653,10 @@ void GenericCAO::initialize(const std::string &data)
pos_translator.init(m_position); pos_translator.init(m_position);
updateNodePos(); updateNodePos();
if(m_is_player) if (m_is_player) {
{ // Check if it's the current player
LocalPlayer *player = m_env->getPlayer(m_name.c_str()); LocalPlayer *player = m_env->getLocalPlayer();
if (player && player->isLocal()) { if (player && strcmp(player->getName(), m_name.c_str()) == 0) {
m_is_local_player = true; m_is_local_player = true;
m_is_visible = false; m_is_visible = false;
LocalPlayer* localplayer = player; LocalPlayer* localplayer = player;

@ -69,62 +69,6 @@ Environment::Environment():
Environment::~Environment() Environment::~Environment()
{ {
// Deallocate players
for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
delete (*i);
}
}
void Environment::addPlayer(Player *player)
{
DSTACK(FUNCTION_NAME);
/*
Check that peer_ids are unique.
Also check that names are unique.
Exception: there can be multiple players with peer_id=0
*/
// If peer id is non-zero, it has to be unique.
if(player->peer_id != 0)
FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique");
// Name has to be unique.
FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique");
// Add.
m_players.push_back(player);
}
void Environment::removePlayer(Player* player)
{
for (std::vector<Player*>::iterator it = m_players.begin();
it != m_players.end(); ++it) {
if ((*it) == player) {
delete *it;
m_players.erase(it);
return;
}
}
}
Player *Environment::getPlayer(u16 peer_id)
{
for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
Player *player = *i;
if (player->peer_id == peer_id)
return player;
}
return NULL;
}
Player *Environment::getPlayer(const char *name)
{
for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
Player *player = *i;
if(strcmp(player->getName(), name) == 0)
return player;
}
return NULL;
} }
u32 Environment::getDayNightRatio() u32 Environment::getDayNightRatio()
@ -549,6 +493,12 @@ ServerEnvironment::~ServerEnvironment()
i = m_abms.begin(); i != m_abms.end(); ++i){ i = m_abms.begin(); i != m_abms.end(); ++i){
delete i->abm; delete i->abm;
} }
// Deallocate players
for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
delete (*i);
}
} }
Map & ServerEnvironment::getMap() Map & ServerEnvironment::getMap()
@ -563,12 +513,53 @@ ServerMap & ServerEnvironment::getServerMap()
RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id)
{ {
return dynamic_cast<RemotePlayer *>(Environment::getPlayer(peer_id)); for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
RemotePlayer *player = *i;
if (player->peer_id == peer_id)
return player;
}
return NULL;
} }
RemotePlayer *ServerEnvironment::getPlayer(const char* name) RemotePlayer *ServerEnvironment::getPlayer(const char* name)
{ {
return dynamic_cast<RemotePlayer *>(Environment::getPlayer(name)); for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
RemotePlayer *player = *i;
if (strcmp(player->getName(), name) == 0)
return player;
}
return NULL;
}
void ServerEnvironment::addPlayer(RemotePlayer *player)
{
DSTACK(FUNCTION_NAME);
/*
Check that peer_ids are unique.
Also check that names are unique.
Exception: there can be multiple players with peer_id=0
*/
// If peer id is non-zero, it has to be unique.
if (player->peer_id != 0)
FATAL_ERROR_IF(getPlayer(player->peer_id) != NULL, "Peer id not unique");
// Name has to be unique.
FATAL_ERROR_IF(getPlayer(player->getName()) != NULL, "Player name not unique");
// Add.
m_players.push_back(player);
}
void ServerEnvironment::removePlayer(RemotePlayer *player)
{
for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
it != m_players.end(); ++it) {
if ((*it) == player) {
delete *it;
m_players.erase(it);
return;
}
}
} }
bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p) bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 *p)
@ -601,7 +592,7 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16
void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason,
const std::string &str_reason, bool reconnect) const std::string &str_reason, bool reconnect)
{ {
for (std::vector<Player*>::iterator it = m_players.begin(); for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
it != m_players.end(); ++it) { it != m_players.end(); ++it) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*it); RemotePlayer *player = dynamic_cast<RemotePlayer *>(*it);
((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id, ((Server*)m_gamedef)->DenyAccessVerCompliant(player->peer_id,
@ -614,7 +605,7 @@ void ServerEnvironment::saveLoadedPlayers()
std::string players_path = m_path_world + DIR_DELIM "players"; std::string players_path = m_path_world + DIR_DELIM "players";
fs::CreateDir(players_path); fs::CreateDir(players_path);
for (std::vector<Player*>::iterator it = m_players.begin(); for (std::vector<RemotePlayer *>::iterator it = m_players.begin();
it != m_players.end(); it != m_players.end();
++it) { ++it) {
RemotePlayer *player = static_cast<RemotePlayer*>(*it); RemotePlayer *player = static_cast<RemotePlayer*>(*it);
@ -1253,7 +1244,7 @@ void ServerEnvironment::step(float dtime)
*/ */
{ {
ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG);
for (std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i); RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
assert(player); assert(player);
@ -1276,7 +1267,7 @@ void ServerEnvironment::step(float dtime)
Get player block positions Get player block positions
*/ */
std::vector<v3s16> players_blockpos; std::vector<v3s16> players_blockpos;
for (std::vector<Player*>::iterator i = m_players.begin(); for (std::vector<RemotePlayer *>::iterator i = m_players.begin();
i != m_players.end(); ++i) { i != m_players.end(); ++i) {
RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i); RemotePlayer *player = dynamic_cast<RemotePlayer *>(*i);
assert(player); assert(player);
@ -2255,6 +2246,7 @@ ClientEnvironment::ClientEnvironment(ClientMap *map, scene::ISceneManager *smgr,
ITextureSource *texturesource, IGameDef *gamedef, ITextureSource *texturesource, IGameDef *gamedef,
IrrlichtDevice *irr): IrrlichtDevice *irr):
m_map(map), m_map(map),
m_local_player(NULL),
m_smgr(smgr), m_smgr(smgr),
m_texturesource(texturesource), m_texturesource(texturesource),
m_gamedef(gamedef), m_gamedef(gamedef),
@ -2291,37 +2283,16 @@ ClientMap & ClientEnvironment::getClientMap()
return *m_map; return *m_map;
} }
LocalPlayer *ClientEnvironment::getPlayer(const u16 peer_id) void ClientEnvironment::setLocalPlayer(LocalPlayer *player)
{
return dynamic_cast<LocalPlayer *>(Environment::getPlayer(peer_id));
}
LocalPlayer *ClientEnvironment::getPlayer(const char* name)
{
return dynamic_cast<LocalPlayer *>(Environment::getPlayer(name));
}
void ClientEnvironment::addPlayer(LocalPlayer *player)
{ {
DSTACK(FUNCTION_NAME); DSTACK(FUNCTION_NAME);
/* /*
It is a failure if already is a local player It is a failure if already is a local player
*/ */
FATAL_ERROR_IF(getLocalPlayer() != NULL, FATAL_ERROR_IF(m_local_player != NULL,
"Player is local but there is already a local player"); "Local player already allocated");
Environment::addPlayer(player); m_local_player = player;
}
LocalPlayer *ClientEnvironment::getLocalPlayer()
{
for(std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
Player *player = *i;
if (player->isLocal())
return (LocalPlayer*)player;
}
return NULL;
} }
void ClientEnvironment::step(float dtime) void ClientEnvironment::step(float dtime)
@ -2558,24 +2529,6 @@ void ClientEnvironment::step(float dtime)
} }
} }
/*
Stuff that can be done in an arbitarily large dtime
*/
for (std::vector<Player*>::iterator i = m_players.begin();
i != m_players.end(); ++i) {
Player *player = *i;
assert(player);
/*
Handle non-local players
*/
if (!player->isLocal()) {
// Move
player->move(dtime, this, 100*BS);
}
}
// Update lighting on local player (used for wield item) // Update lighting on local player (used for wield item)
u32 day_night_ratio = getDayNightRatio(); u32 day_night_ratio = getDayNightRatio();
{ {

@ -72,9 +72,6 @@ public:
virtual Map & getMap() = 0; virtual Map & getMap() = 0;
virtual void addPlayer(Player *player);
void removePlayer(Player *player);
u32 getDayNightRatio(); u32 getDayNightRatio();
// 0-23999 // 0-23999
@ -94,12 +91,6 @@ public:
u32 m_added_objects; u32 m_added_objects;
protected: protected:
Player *getPlayer(u16 peer_id);
Player *getPlayer(const char *name);
// peer_ids in here should be unique, except that there may be many 0s
std::vector<Player*> m_players;
GenericAtomic<float> m_time_of_day_speed; GenericAtomic<float> m_time_of_day_speed;
/* /*
@ -325,6 +316,8 @@ public:
void saveLoadedPlayers(); void saveLoadedPlayers();
void savePlayer(RemotePlayer *player); void savePlayer(RemotePlayer *player);
RemotePlayer *loadPlayer(const std::string &playername); RemotePlayer *loadPlayer(const std::string &playername);
void addPlayer(RemotePlayer *player);
void removePlayer(RemotePlayer *player);
/* /*
Save and load time of day and game timer Save and load time of day and game timer
@ -520,6 +513,9 @@ private:
// Can raise to high values like 15s with eg. map generation mods. // Can raise to high values like 15s with eg. map generation mods.
float m_max_lag_estimate; float m_max_lag_estimate;
// peer_ids in here should be unique, except that there may be many 0s
std::vector<RemotePlayer*> m_players;
// Particles // Particles
IntervalLimiter m_particle_management_interval; IntervalLimiter m_particle_management_interval;
UNORDERED_MAP<u32, float> m_particle_spawners; UNORDERED_MAP<u32, float> m_particle_spawners;
@ -579,8 +575,8 @@ public:
void step(f32 dtime); void step(f32 dtime);
virtual void addPlayer(LocalPlayer *player); virtual void setLocalPlayer(LocalPlayer *player);
LocalPlayer * getLocalPlayer(); LocalPlayer *getLocalPlayer() { return m_local_player; }
/* /*
ClientSimpleObjects ClientSimpleObjects
@ -630,21 +626,15 @@ public:
u16 attachement_parent_ids[USHRT_MAX + 1]; u16 attachement_parent_ids[USHRT_MAX + 1];
std::list<std::string> getPlayerNames() const std::list<std::string> &getPlayerNames() { return m_player_names; }
{ return m_player_names; } void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
void addPlayerName(std::string name) void removePlayerName(const std::string &name) { m_player_names.remove(name); }
{ m_player_names.push_back(name); }
void removePlayerName(std::string name)
{ m_player_names.remove(name); }
void updateCameraOffset(v3s16 camera_offset) void updateCameraOffset(v3s16 camera_offset)
{ m_camera_offset = camera_offset; } { m_camera_offset = camera_offset; }
v3s16 getCameraOffset() const { return m_camera_offset; } v3s16 getCameraOffset() const { return m_camera_offset; }
LocalPlayer *getPlayer(const u16 peer_id);
LocalPlayer *getPlayer(const char* name);
private: private:
ClientMap *m_map; ClientMap *m_map;
LocalPlayer *m_local_player;
scene::ISceneManager *m_smgr; scene::ISceneManager *m_smgr;
ITextureSource *m_texturesource; ITextureSource *m_texturesource;
IGameDef *m_gamedef; IGameDef *m_gamedef;

@ -38,11 +38,6 @@ public:
LocalPlayer(Client *gamedef, const char *name); LocalPlayer(Client *gamedef, const char *name);
virtual ~LocalPlayer(); virtual ~LocalPlayer();
bool isLocal() const
{
return true;
}
ClientActiveObject *parent; ClientActiveObject *parent;
bool got_teleported; bool got_teleported;

@ -183,22 +183,6 @@ public:
return size; return size;
} }
void setLocalAnimations(v2s32 frames[4], float frame_speed)
{
for (int i = 0; i < 4; i++)
local_animations[i] = frames[i];
local_animation_speed = frame_speed;
}
void getLocalAnimations(v2s32 *frames, float *frame_speed)
{
for (int i = 0; i < 4; i++)
frames[i] = local_animations[i];
*frame_speed = local_animation_speed;
}
virtual bool isLocal() const { return false; }
bool camera_barely_in_ceiling; bool camera_barely_in_ceiling;
v3f eye_offset_first; v3f eye_offset_first;
v3f eye_offset_third; v3f eye_offset_third;

@ -142,6 +142,20 @@ public:
Player::setYaw(yaw); Player::setYaw(yaw);
} }
void setLocalAnimations(v2s32 frames[4], float frame_speed)
{
for (int i = 0; i < 4; i++)
local_animations[i] = frames[i];
local_animation_speed = frame_speed;
}
void getLocalAnimations(v2s32 *frames, float *frame_speed)
{
for (int i = 0; i < 4; i++)
frames[i] = local_animations[i];
*frame_speed = local_animation_speed;
}
u16 protocol_version; u16 protocol_version;
private: private:
/* /*