From bb1c71158613c1f17c57e53ad489b076a11e3828 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Fri, 11 Aug 2017 09:57:27 +0200 Subject: ServerEnvironment::step: modernize loops Use various ranged-based for loops in ServerEnvironment::step Also set ServerObject::getBasePosition const to be compliant ServerEnvironment::deleteParticleSpawner: use a const iterator --- src/serverenvironment.cpp | 95 ++++++++++++++--------------------------------- src/serverobject.h | 2 +- 2 files changed, 28 insertions(+), 69 deletions(-) diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 1bc7c6eb5..2fb12692b 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -1169,17 +1169,13 @@ void ServerEnvironment::step(float dtime) */ { ScopeProfiler sp(g_profiler, "SEnv: handle players avg", SPT_AVG); - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - RemotePlayer *player = dynamic_cast(*i); - assert(player); - + for (RemotePlayer *player : m_players) { // Ignore disconnected players - if(player->peer_id == 0) + if (player->peer_id == 0) continue; // Move - player->move(dtime, this, 100*BS); + player->move(dtime, this, 100 * BS); } } @@ -1192,11 +1188,7 @@ void ServerEnvironment::step(float dtime) Get player block positions */ std::vector players_blockpos; - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - RemotePlayer *player = dynamic_cast(*i); - assert(player); - + for (RemotePlayer *player: m_players) { // Ignore disconnected players if (player->peer_id == 0) continue; @@ -1204,9 +1196,8 @@ void ServerEnvironment::step(float dtime) PlayerSAO *playersao = player->getPlayerSAO(); assert(playersao); - v3s16 blockpos = getNodeBlockPos( - floatToInt(playersao->getBasePosition(), BS)); - players_blockpos.push_back(blockpos); + players_blockpos.push_back( + getNodeBlockPos(floatToInt(playersao->getBasePosition(), BS))); } /* @@ -1226,16 +1217,9 @@ void ServerEnvironment::step(float dtime) // Convert active objects that are no more in active blocks to static deactivateFarObjects(false); - for(std::set::iterator - i = blocks_removed.begin(); - i != blocks_removed.end(); ++i) { - v3s16 p = *i; - - /* infostream<<"Server: Block " << PP(p) - << " became inactive"<getBlockNoCreateNoEx(p); - if(block==NULL) + if (!block) continue; // Set current time as timestamp (and let it set ChangedFlag) @@ -1246,21 +1230,14 @@ void ServerEnvironment::step(float dtime) Handle added blocks */ - for(std::set::iterator - i = blocks_added.begin(); - i != blocks_added.end(); ++i) - { - v3s16 p = *i; - + for (const v3s16 &p: blocks_added) { MapBlock *block = m_map->getBlockOrEmerge(p); - if(block==NULL){ + if (!block) { m_active_blocks.m_list.erase(p); continue; } activateBlock(block); - /* infostream<<"Server: Block " << PP(p) - << " became active"<::iterator - i = m_active_blocks.m_list.begin(); - i != m_active_blocks.m_list.end(); ++i) - { - v3s16 p = *i; - - /*infostream<<"Server: Block ("<getBlockNoCreateNoEx(p); - if(block==NULL) + if (!block) continue; // Reset block usage timer @@ -1297,17 +1266,16 @@ void ServerEnvironment::step(float dtime) MOD_REASON_BLOCK_EXPIRED); // Run node timers - std::vector elapsed_timers = - block->m_node_timers.step((float)dtime); + std::vector elapsed_timers = block->m_node_timers.step(dtime); if (!elapsed_timers.empty()) { MapNode n; - for (std::vector::iterator i = elapsed_timers.begin(); - i != elapsed_timers.end(); ++i) { - n = block->getNodeNoEx(i->position); - p = i->position + block->getPosRelative(); - if (m_script->node_on_timer(p, n, i->elapsed)) { + v3s16 p2; + for (const NodeTimer &elapsed_timer: elapsed_timers) { + n = block->getNodeNoEx(elapsed_timer.position); + p2 = elapsed_timer.position + block->getPosRelative(); + if (m_script->node_on_timer(p2, n, elapsed_timer.elapsed)) { block->setNodeTimer(NodeTimer( - i->timeout, 0, i->position)); + elapsed_timer.timeout, 0, elapsed_timer.position)); } } } @@ -1315,8 +1283,8 @@ void ServerEnvironment::step(float dtime) } if (m_active_block_modifier_interval.step(dtime, m_cache_abm_interval)) - do{ // breakable - if(m_active_block_interval_overload_skip > 0){ + do { // breakable + if (m_active_block_interval_overload_skip > 0) { ScopeProfiler sp(g_profiler, "SEnv: ABM overload skips"); m_active_block_interval_overload_skip--; break; @@ -1327,17 +1295,9 @@ void ServerEnvironment::step(float dtime) // Initialize handling of ActiveBlockModifiers ABMHandler abmhandler(m_abms, m_cache_abm_interval, this, true); - for(std::set::iterator - i = m_active_blocks.m_list.begin(); - i != m_active_blocks.m_list.end(); ++i) - { - v3s16 p = *i; - - /*infostream<<"Server: Block ("<getBlockNoCreateNoEx(p); - if(block == NULL) + if (!block) continue; // Set current time as timestamp @@ -1349,7 +1309,7 @@ void ServerEnvironment::step(float dtime) u32 time_ms = timer.stop(true); u32 max_time_ms = 200; - if(time_ms > max_time_ms){ + if (time_ms > max_time_ms) { warningstream<<"active block modifiers took " <::iterator it = m_particle_spawner_attachments.find(id); + const auto &it = m_particle_spawner_attachments.find(id); if (it != m_particle_spawner_attachments.end()) { - u16 obj_id = (*it).second; + u16 obj_id = it->second; ServerActiveObject *sao = getActiveObject(obj_id); if (sao != NULL && remove_from_object) { sao->detachParticleSpawner(id); diff --git a/src/serverobject.h b/src/serverobject.h index cd1922c87..8acbd6a19 100644 --- a/src/serverobject.h +++ b/src/serverobject.h @@ -78,7 +78,7 @@ public: /* Some simple getters/setters */ - v3f getBasePosition(){ return m_base_position; } + v3f getBasePosition() const { return m_base_position; } void setBasePosition(v3f pos){ m_base_position = pos; } ServerEnvironment* getEnv(){ return m_env; } -- cgit v1.2.3