From: Loic Blot Date: Tue, 17 Feb 2015 15:49:27 +0000 (+0100) Subject: Change many useless std::list into Environment class (m_players), ABMHandler and... X-Git-Tag: 0.4.13~596 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=2079462e62b478555b1b614a90692b6bc9bb5d34;p=oweals%2Fminetest.git Change many useless std::list into Environment class (m_players), ABMHandler and ActiveBlockList::update This improve looping performances. --- diff --git a/src/environment.cpp b/src/environment.cpp index e805a59a5..bafc91c00 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -61,9 +61,8 @@ Environment::Environment(): Environment::~Environment() { // Deallocate players - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + for(std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { delete (*i); } } @@ -89,7 +88,7 @@ void Environment::removePlayer(u16 peer_id) { DSTACK(__FUNCTION_NAME); - for(std::list::iterator i = m_players.begin(); + for(std::vector::iterator i = m_players.begin(); i != m_players.end();) { Player *player = *i; @@ -104,7 +103,7 @@ void Environment::removePlayer(u16 peer_id) void Environment::removePlayer(const char *name) { - for (std::list::iterator it = m_players.begin(); + for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { if (strcmp((*it)->getName(), name) == 0) { delete *it; @@ -116,9 +115,8 @@ void Environment::removePlayer(const char *name) Player * Environment::getPlayer(u16 peer_id) { - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + for(std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { Player *player = *i; if(player->peer_id == peer_id) return player; @@ -128,9 +126,8 @@ Player * Environment::getPlayer(u16 peer_id) Player * Environment::getPlayer(const char *name) { - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + for(std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { Player *player = *i; if(strcmp(player->getName(), name) == 0) return player; @@ -140,15 +137,13 @@ Player * Environment::getPlayer(const char *name) Player * Environment::getRandomConnectedPlayer() { - std::list connected_players = getPlayers(true); + std::vector connected_players = getPlayers(true); u32 chosen_one = myrand() % connected_players.size(); u32 j = 0; - for(std::list::iterator + for(std::vector::iterator i = connected_players.begin(); - i != connected_players.end(); ++i) - { - if(j == chosen_one) - { + i != connected_players.end(); ++i) { + if(j == chosen_one) { Player *player = *i; return player; } @@ -159,17 +154,15 @@ Player * Environment::getRandomConnectedPlayer() Player * Environment::getNearestConnectedPlayer(v3f pos) { - std::list connected_players = getPlayers(true); + std::vector connected_players = getPlayers(true); f32 nearest_d = 0; Player *nearest_player = NULL; - for(std::list::iterator + for(std::vector::iterator i = connected_players.begin(); - i != connected_players.end(); ++i) - { + i != connected_players.end(); ++i) { Player *player = *i; f32 d = player->getPosition().getDistanceFrom(pos); - if(d < nearest_d || nearest_player == NULL) - { + if(d < nearest_d || nearest_player == NULL) { nearest_d = d; nearest_player = player; } @@ -177,22 +170,20 @@ Player * Environment::getNearestConnectedPlayer(v3f pos) return nearest_player; } -std::list Environment::getPlayers() +std::vector Environment::getPlayers() { return m_players; } -std::list Environment::getPlayers(bool ignore_disconnected) +std::vector Environment::getPlayers(bool ignore_disconnected) { - std::list newlist; - for(std::list::iterator + std::vector newlist; + for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + i != m_players.end(); ++i) { Player *player = *i; - if(ignore_disconnected) - { + if(ignore_disconnected) { // Ignore disconnected players if(player->peer_id == 0) continue; @@ -287,7 +278,7 @@ void fillRadiusBlock(v3s16 p0, s16 r, std::set &list) } } -void ActiveBlockList::update(std::list &active_positions, +void ActiveBlockList::update(std::vector &active_positions, s16 radius, std::set &blocks_removed, std::set &blocks_added) @@ -296,7 +287,7 @@ void ActiveBlockList::update(std::list &active_positions, Create the new list */ std::set newlist = m_forceloaded_list; - for(std::list::iterator i = active_positions.begin(); + for(std::vector::iterator i = active_positions.begin(); i != active_positions.end(); ++i) { fillRadiusBlock(*i, radius, newlist); @@ -395,8 +386,8 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 //calculate normalized direction vector v3f normalized_vector = v3f((pos2.X - pos1.X)/distance, - (pos2.Y - pos1.Y)/distance, - (pos2.Z - pos1.Z)/distance); + (pos2.Y - pos1.Y)/distance, + (pos2.Z - pos1.Z)/distance); //find out if there's a node on path between pos1 and pos2 for (float i = 1; i < distance; i += stepsize) { @@ -421,7 +412,7 @@ void ServerEnvironment::saveLoadedPlayers() std::string players_path = m_path_world + DIR_DELIM "players"; fs::CreateDir(players_path); - for (std::list::iterator it = m_players.begin(); + for (std::vector::iterator it = m_players.begin(); it != m_players.end(); ++it) { RemotePlayer *player = static_cast(*it); @@ -549,7 +540,7 @@ class ABMHandler { private: ServerEnvironment *m_env; - std::map > m_aabms; + std::map > m_aabms; public: ABMHandler(std::list &abms, float dtime_s, ServerEnvironment *env, @@ -604,10 +595,10 @@ public: k != ids.end(); k++) { content_t c = *k; - std::map >::iterator j; + std::map >::iterator j; j = m_aabms.find(c); if(j == m_aabms.end()){ - std::list aabmlist; + std::vector aabmlist; m_aabms[c] = aabmlist; j = m_aabms.find(c); } @@ -664,14 +655,13 @@ public: content_t c = n.getContent(); v3s16 p = p0 + block->getPosRelative(); - std::map >::iterator j; + std::map >::iterator j; j = m_aabms.find(c); if(j == m_aabms.end()) continue; - for(std::list::iterator - i = j->second.begin(); i != j->second.end(); i++) - { + for(std::vector::iterator + i = j->second.begin(); i != j->second.end(); i++) { if(myrand() % i->chance != 0) continue; @@ -1008,7 +998,7 @@ void ServerEnvironment::step(float dtime) */ { ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); - for(std::list::iterator i = m_players.begin(); + for(std::vector::iterator i = m_players.begin(); i != m_players.end(); ++i) { Player *player = *i; @@ -1031,15 +1021,15 @@ void ServerEnvironment::step(float dtime) /* Get player block positions */ - std::list players_blockpos; - for(std::list::iterator + std::vector players_blockpos; + for(std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + i != m_players.end(); ++i) { Player *player = *i; // Ignore disconnected players if(player->peer_id == 0) continue; + v3s16 blockpos = getNodeBlockPos( floatToInt(player->getPosition(), BS)); players_blockpos.push_back(blockpos); @@ -1686,7 +1676,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) } // Activate stored objects - std::list new_stored; + std::vector new_stored; for(std::list::iterator i = block->m_static_objects.m_stored.begin(); i != block->m_static_objects.m_stored.end(); ++i) { @@ -1715,10 +1705,9 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) // Clear stored list block->m_static_objects.m_stored.clear(); // Add leftover failed stuff to stored list - for(std::list::iterator + for(std::vector::iterator i = new_stored.begin(); - i != new_stored.end(); ++i) - { + i != new_stored.end(); ++i) { StaticObject &s_obj = *i; block->m_static_objects.m_stored.push_back(s_obj); } @@ -1758,11 +1747,10 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) */ void ServerEnvironment::deactivateFarObjects(bool force_delete) { - std::list objects_to_remove; + std::vector objects_to_remove; for(std::map::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; assert(obj); @@ -1979,9 +1967,8 @@ void ServerEnvironment::deactivateFarObjects(bool force_delete) } // Remove references from m_active_objects - for(std::list::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) - { + for(std::vector::iterator i = objects_to_remove.begin(); + i != objects_to_remove.end(); ++i) { m_active_objects.erase(*i); } } @@ -2052,9 +2039,8 @@ void ClientEnvironment::addPlayer(Player *player) LocalPlayer * ClientEnvironment::getLocalPlayer() { - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + for(std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { Player *player = *i; if(player->isLocal()) return (LocalPlayer*)player; @@ -2310,16 +2296,14 @@ void ClientEnvironment::step(float dtime) /* Stuff that can be done in an arbitarily large dtime */ - for(std::list::iterator i = m_players.begin(); - i != m_players.end(); ++i) - { + for(std::vector::iterator i = m_players.begin(); + i != m_players.end(); ++i) { Player *player = *i; /* Handle non-local players */ - if(player->isLocal() == false) - { + if(player->isLocal() == false) { // Move player->move(dtime, this, 100*BS); diff --git a/src/environment.h b/src/environment.h index 13c0c0efe..9eb440c42 100644 --- a/src/environment.h +++ b/src/environment.h @@ -75,8 +75,8 @@ public: Player * getPlayer(const char *name); Player * getRandomConnectedPlayer(); Player * getNearestConnectedPlayer(v3f pos); - std::list getPlayers(); - std::list getPlayers(bool ignore_disconnected); + std::vector getPlayers(); + std::vector getPlayers(bool ignore_disconnected); u32 getDayNightRatio(); @@ -110,7 +110,7 @@ public: protected: // peer_ids in here should be unique, except that there may be many 0s - std::list m_players; + std::vector m_players; // Time of day in milli-hours (0-23999); determines day and night u32 m_time_of_day; // Time of day in 0...1 @@ -182,7 +182,7 @@ struct ABMWithState class ActiveBlockList { public: - void update(std::list &active_positions, + void update(std::vector &active_positions, s16 radius, std::set &blocks_removed, std::set &blocks_added);