From 7528986e4449febead9b18b6118f0b096f7cf800 Mon Sep 17 00:00:00 2001 From: Loïc Blot Date: Sat, 19 Aug 2017 14:25:35 +0200 Subject: Code modernization: src/p*, src/q*, src/r*, src/s* (partial) (#6282) * Code modernization: src/p*, src/q*, src/r*, src/s* (partial) * empty function * default constructor/destructor * for range-based loops * use emplace_back instead of push_back * C++ STL header style * Spelling: vertice -> vertex --- src/serverenvironment.cpp | 253 ++++++++++++++++++---------------------------- 1 file changed, 98 insertions(+), 155 deletions(-) (limited to 'src/serverenvironment.cpp') diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 4cc76b96f..b552ad72f 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -129,7 +129,7 @@ LBMManager::~LBMManager() void LBMManager::addLBMDef(LoadingBlockModifierDef *lbm_def) { // Precondition, in query mode the map isn't used anymore - FATAL_ERROR_IF(m_query_mode == true, + FATAL_ERROR_IF(m_query_mode, "attempted to modify LBMManager in query mode"); if (!string_allowed(lbm_def->name, LBM_NAME_ALLOWED_CHARS)) { @@ -160,7 +160,7 @@ void LBMManager::loadIntroductionTimes(const std::string ×, size_t idx = 0; size_t idx_new; - while ((idx_new = times.find(";", idx)) != std::string::npos) { + while ((idx_new = times.find(';', idx)) != std::string::npos) { std::string entry = times.substr(idx, idx_new - idx); std::vector components = str_split(entry, '~'); if (components.size() != 2) @@ -224,7 +224,7 @@ void LBMManager::loadIntroductionTimes(const std::string ×, std::string LBMManager::createIntroductionTimesString() { // Precondition, we must be in query mode - FATAL_ERROR_IF(m_query_mode == false, + FATAL_ERROR_IF(!m_query_mode, "attempted to query on non fully set up LBMManager"); std::ostringstream oss; @@ -246,7 +246,7 @@ std::string LBMManager::createIntroductionTimesString() void LBMManager::applyLBMs(ServerEnvironment *env, MapBlock *block, u32 stamp) { // Precondition, we need m_lbm_lookup to be initialized - FATAL_ERROR_IF(m_query_mode == false, + FATAL_ERROR_IF(!m_query_mode, "attempted to query on non fully set up LBMManager"); v3s16 pos_of_block = block->getPosRelative(); v3s16 pos; @@ -300,22 +300,17 @@ void ActiveBlockList::update(std::vector &active_positions, Create the new list */ std::set newlist = m_forceloaded_list; - for(std::vector::iterator i = active_positions.begin(); - i != active_positions.end(); ++i) - { - fillRadiusBlock(*i, radius, newlist); + for (const v3s16 &active_position : active_positions) { + fillRadiusBlock(active_position, radius, newlist); } /* Find out which blocks on the old list are not on the new list */ // Go through old list - for(std::set::iterator i = m_list.begin(); - i != m_list.end(); ++i) - { - v3s16 p = *i; + for (v3s16 p : m_list) { // If not on new list, it's been removed - if(newlist.find(p) == newlist.end()) + if (newlist.find(p) == newlist.end()) blocks_removed.insert(p); } @@ -323,10 +318,7 @@ void ActiveBlockList::update(std::vector &active_positions, Find out which blocks on the new list are not on the old list */ // Go through new list - for(std::set::iterator i = newlist.begin(); - i != newlist.end(); ++i) - { - v3s16 p = *i; + for (v3s16 p : newlist) { // If not on old list, it's been added if(m_list.find(p) == m_list.end()) blocks_added.insert(p); @@ -336,10 +328,7 @@ void ActiveBlockList::update(std::vector &active_positions, Update m_list */ m_list.clear(); - for(std::set::iterator i = newlist.begin(); - i != newlist.end(); ++i) - { - v3s16 p = *i; + for (v3s16 p : newlist) { m_list.insert(p); } } @@ -375,7 +364,7 @@ ServerEnvironment::ServerEnvironment(ServerMap *map, } } - std::string name = ""; + std::string name; conf.getNoEx("player_backend", name); m_player_database = openPlayerDatabase(name, path_world, conf); } @@ -393,15 +382,13 @@ ServerEnvironment::~ServerEnvironment() m_map->drop(); // Delete ActiveBlockModifiers - for (std::vector::iterator - i = m_abms.begin(); i != m_abms.end(); ++i){ - delete i->abm; + for (ABMWithState &m_abm : m_abms) { + delete m_abm.abm; } // Deallocate players - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - delete (*i); + for (RemotePlayer *m_player : m_players) { + delete m_player; } delete m_player_database; @@ -419,9 +406,7 @@ ServerMap & ServerEnvironment::getServerMap() RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) { - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - RemotePlayer *player = *i; + for (RemotePlayer *player : m_players) { if (player->peer_id == peer_id) return player; } @@ -430,9 +415,7 @@ RemotePlayer *ServerEnvironment::getPlayer(const u16 peer_id) RemotePlayer *ServerEnvironment::getPlayer(const char* name) { - for (std::vector::iterator i = m_players.begin(); - i != m_players.end(); ++i) { - RemotePlayer *player = *i; + for (RemotePlayer *player : m_players) { if (strcmp(player->getName(), name) == 0) return player; } @@ -503,9 +486,7 @@ bool ServerEnvironment::line_of_sight(v3f pos1, v3f pos2, float stepsize, v3s16 void ServerEnvironment::kickAllPlayers(AccessDeniedCode reason, const std::string &str_reason, bool reconnect) { - for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); ++it) { - RemotePlayer *player = dynamic_cast(*it); + for (RemotePlayer *player : m_players) { m_server->DenyAccessVerCompliant(player->peer_id, player->protocol_version, reason, str_reason, reconnect); } @@ -516,15 +497,13 @@ void ServerEnvironment::saveLoadedPlayers() std::string players_path = m_path_world + DIR_DELIM + "players"; fs::CreateDir(players_path); - for (std::vector::iterator it = m_players.begin(); - it != m_players.end(); - ++it) { - if ((*it)->checkModified() || - ((*it)->getPlayerSAO() && (*it)->getPlayerSAO()->extendedAttributesModified())) { + for (RemotePlayer *player : m_players) { + if (player->checkModified() || (player->getPlayerSAO() && + player->getPlayerSAO()->extendedAttributesModified())) { try { - m_player_database->savePlayer(*it); + m_player_database->savePlayer(player); } catch (DatabaseException &e) { - errorstream << "Failed to save player " << (*it)->getName() << " exception: " + errorstream << "Failed to save player " << player->getName() << " exception: " << e.what() << std::endl; throw; } @@ -641,7 +620,7 @@ void ServerEnvironment::loadMeta() // If missing, do as if clearObjects was never called args.getU64("last_clear_objects_time") : 0; - std::string lbm_introduction_times = ""; + std::string lbm_introduction_times; try { u64 ver = args.getU64("lbm_introduction_times_version"); if (ver == 1) { @@ -685,18 +664,17 @@ public: if(dtime_s < 0.001) return; INodeDefManager *ndef = env->getGameDef()->ndef(); - for(std::vector::iterator - i = abms.begin(); i != abms.end(); ++i) { - ActiveBlockModifier *abm = i->abm; + for (ABMWithState &abmws : abms) { + ActiveBlockModifier *abm = abmws.abm; float trigger_interval = abm->getTriggerInterval(); if(trigger_interval < 0.001) trigger_interval = 0.001; float actual_interval = dtime_s; if(use_timers){ - i->timer += dtime_s; - if(i->timer < trigger_interval) + abmws.timer += dtime_s; + if(abmws.timer < trigger_interval) continue; - i->timer -= trigger_interval; + abmws.timer -= trigger_interval; actual_interval = trigger_interval; } float chance = abm->getTriggerChance(); @@ -718,20 +696,16 @@ public: // Trigger neighbors const std::set &required_neighbors_s = abm->getRequiredNeighbors(); - for (std::set::iterator rn = required_neighbors_s.begin(); - rn != required_neighbors_s.end(); ++rn) { - ndef->getIds(*rn, aabm.required_neighbors); + for (const std::string &required_neighbor_s : required_neighbors_s) { + ndef->getIds(required_neighbor_s, aabm.required_neighbors); } // Trigger contents const std::set &contents_s = abm->getTriggerContents(); - for (std::set::iterator cs = contents_s.begin(); - cs != contents_s.end(); ++cs) { + for (const std::string &content_s : contents_s) { std::set ids; - ndef->getIds(*cs, ids); - for (std::set::const_iterator k = ids.begin(); - k != ids.end(); ++k) { - content_t c = *k; + ndef->getIds(content_s, ids); + for (content_t c : ids) { if (c >= m_aabms.size()) m_aabms.resize(c + 256, NULL); if (!m_aabms[c]) @@ -744,8 +718,8 @@ public: ~ABMHandler() { - for (size_t i = 0; i < m_aabms.size(); i++) - delete m_aabms[i]; + for (auto &aabms : m_aabms) + delete aabms; } // Find out how many objects the given block and its neighbours contain. @@ -799,14 +773,12 @@ public: continue; v3s16 p = p0 + block->getPosRelative(); - for(std::vector::iterator - i = m_aabms[c]->begin(); i != m_aabms[c]->end(); ++i) { - if(myrand() % i->chance != 0) + for (ActiveABM &aabm : *m_aabms[c]) { + if (myrand() % aabm.chance != 0) continue; // Check neighbors - if(!i->required_neighbors.empty()) - { + if (!aabm.required_neighbors.empty()) { v3s16 p1; for(p1.X = p0.X-1; p1.X <= p0.X+1; p1.X++) for(p1.Y = p0.Y-1; p1.Y <= p0.Y+1; p1.Y++) @@ -826,8 +798,8 @@ public: c = n.getContent(); } std::set::const_iterator k; - k = i->required_neighbors.find(c); - if(k != i->required_neighbors.end()){ + k = aabm.required_neighbors.find(c); + if(k != aabm.required_neighbors.end()){ goto neighbor_found; } } @@ -837,8 +809,8 @@ public: neighbor_found: // Call all the trigger variations - i->abm->trigger(m_env, p, n); - i->abm->trigger(m_env, p, n, + aabm.abm->trigger(m_env, p, n); + aabm.abm->trigger(m_env, p, n, active_object_count, active_object_count_wider); // Count surrounding objects again if the abms added any @@ -894,13 +866,12 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) block->m_node_timers.step((float)dtime_s); if (!elapsed_timers.empty()) { MapNode n; - for (std::vector::iterator - i = elapsed_timers.begin(); - i != elapsed_timers.end(); ++i){ - n = block->getNodeNoEx(i->position); - v3s16 p = i->position + block->getPosRelative(); - if (m_script->node_on_timer(p, n, i->elapsed)) - block->setNodeTimer(NodeTimer(i->timeout, 0, i->position)); + for (const NodeTimer &elapsed_timer : elapsed_timers) { + n = block->getNodeNoEx(elapsed_timer.position); + v3s16 p = elapsed_timer.position + block->getPosRelative(); + if (m_script->node_on_timer(p, n, elapsed_timer.elapsed)) + block->setNodeTimer(NodeTimer(elapsed_timer.timeout, 0, + elapsed_timer.position)); } } @@ -911,7 +882,7 @@ void ServerEnvironment::activateBlock(MapBlock *block, u32 additional_dtime) void ServerEnvironment::addActiveBlockModifier(ActiveBlockModifier *abm) { - m_abms.push_back(ABMWithState(abm)); + m_abms.emplace_back(abm); } void ServerEnvironment::addLoadingBlockModifierDef(LoadingBlockModifierDef *lbm) @@ -985,10 +956,9 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n) void ServerEnvironment::getObjectsInsideRadius(std::vector &objects, v3f pos, float radius) { - for (ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { - ServerActiveObject* obj = i->second; - u16 id = i->first; + for (auto &activeObject : m_active_objects) { + ServerActiveObject* obj = activeObject.second; + u16 id = activeObject.first; v3f objectpos = obj->getBasePosition(); if (objectpos.getDistanceFrom(pos) > radius) continue; @@ -1001,12 +971,11 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) infostream << "ServerEnvironment::clearObjects(): " << "Removing all active objects" << std::endl; std::vector objects_to_remove; - for (ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { - ServerActiveObject* obj = i->second; + for (auto &it : m_active_objects) { + ServerActiveObject* obj = it.second; if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) continue; - u16 id = i->first; + u16 id = it.first; // Delete static object if block is loaded if (obj->m_static_exists) { MapBlock *block = m_map->getBlockNoCreateNoEx(obj->m_static_block); @@ -1037,9 +1006,8 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) } // Remove references from m_active_objects - for (std::vector::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) { - m_active_objects.erase(*i); + for (u16 i : objects_to_remove) { + m_active_objects.erase(i); } // Get list of loaded blocks @@ -1069,9 +1037,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) << " blocks" << std::endl; // Grab a reference on each loaded block to avoid unloading it - for (std::vector::iterator i = loaded_blocks.begin(); - i != loaded_blocks.end(); ++i) { - v3s16 p = *i; + for (v3s16 p : loaded_blocks) { MapBlock *block = m_map->getBlockNoCreateNoEx(p); assert(block != NULL); block->refGrab(); @@ -1087,7 +1053,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) u32 num_blocks_checked = 0; u32 num_blocks_cleared = 0; u32 num_objs_cleared = 0; - for (std::vector::iterator i = loadable_blocks.begin(); + for (auto i = loadable_blocks.begin(); i != loadable_blocks.end(); ++i) { v3s16 p = *i; MapBlock *block = m_map->emergeBlock(p, false); @@ -1124,9 +1090,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) m_map->unloadUnreferencedBlocks(); // Drop references that were added above - for (std::vector::iterator i = loaded_blocks.begin(); - i != loaded_blocks.end(); ++i) { - v3s16 p = *i; + for (v3s16 p : loaded_blocks) { MapBlock *block = m_map->getBlockNoCreateNoEx(p); assert(block); block->refDrop(); @@ -1341,9 +1305,8 @@ void ServerEnvironment::step(float dtime) send_recommended = true; } - for (ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { - ServerActiveObject* obj = i->second; + for (auto &ao_it : m_active_objects) { + ServerActiveObject* obj = ao_it.second; // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) continue; @@ -1491,12 +1454,11 @@ void ServerEnvironment::getAddedActiveObjects(PlayerSAO *playersao, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for (ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { - u16 id = i->first; + for (auto &ao_it : m_active_objects) { + u16 id = ao_it.first; // Get object - ServerActiveObject *object = i->second; + ServerActiveObject *object = ao_it.second; if (object == NULL) continue; @@ -1545,11 +1507,7 @@ void ServerEnvironment::getRemovedActiveObjects(PlayerSAO *playersao, s16 radius - object has m_removed=true, or - object is too far away */ - for(std::set::iterator - i = current_objects.begin(); - i != current_objects.end(); ++i) - { - u16 id = *i; + for (u16 id : current_objects) { ServerActiveObject *object = getActiveObject(id); if (object == NULL) { @@ -1583,11 +1541,9 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock( if (!block) return; - for (std::map::iterator - so_it = block->m_static_objects.m_active.begin(); - so_it != block->m_static_objects.m_active.end(); ++so_it) { + for (auto &so_it : block->m_static_objects.m_active) { // Get the ServerActiveObject counterpart to this StaticObject - ServerActiveObjectMap::const_iterator ao_it = m_active_objects.find(so_it->first); + ServerActiveObjectMap::const_iterator ao_it = m_active_objects.find(so_it.first); if (ao_it == m_active_objects.end()) { // If this ever happens, there must be some kind of nasty bug. errorstream << "ServerEnvironment::setStaticForObjectsInBlock(): " @@ -1621,8 +1577,8 @@ void ServerEnvironment::getSelectedActiveObjects( shootline_on_map.getLength() + 10.0f); const v3f line_vector = shootline_on_map.getVector(); - for (u32 i = 0; i < objectIds.size(); i++) { - ServerActiveObject* obj = getActiveObject(objectIds[i]); + for (u16 objectId : objectIds) { + ServerActiveObject* obj = getActiveObject(objectId); aabb3f selection_box; if (!obj->getSelectionBox(&selection_box)) @@ -1637,9 +1593,9 @@ void ServerEnvironment::getSelectedActiveObjects( v3s16 current_normal; if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector, ¤t_intersection, ¤t_normal)) { - objects.push_back(PointedThing( - (s16) objectIds[i], current_intersection, current_normal, - (current_intersection - shootline_on_map.start).getLengthSQ())); + objects.emplace_back( + (s16) objectId, current_intersection, current_normal, + (current_intersection - shootline_on_map.start).getLengthSQ()); } } } @@ -1707,7 +1663,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, { // Add static object to active static list of the block v3f objectpos = object->getBasePosition(); - std::string staticdata = ""; + std::string staticdata; object->getStaticData(&staticdata); StaticObject s_obj(object->getType(), objectpos, staticdata); // Add to the block where the object is located in @@ -1738,10 +1694,9 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, void ServerEnvironment::removeRemovedObjects() { std::vector objects_to_remove; - for(ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { - u16 id = i->first; - ServerActiveObject* obj = i->second; + for (auto &ao_it : m_active_objects) { + u16 id = ao_it.first; + ServerActiveObject* obj = ao_it.second; // This shouldn't happen but check it if(obj == NULL) { @@ -1814,9 +1769,8 @@ void ServerEnvironment::removeRemovedObjects() objects_to_remove.push_back(id); } // Remove references from m_active_objects - for(std::vector::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) { - m_active_objects.erase(*i); + for (u16 i : objects_to_remove) { + m_active_objects.erase(i); } } @@ -1885,11 +1839,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) // Activate stored objects std::vector new_stored; - for (std::vector::iterator - i = block->m_static_objects.m_stored.begin(); - i != block->m_static_objects.m_stored.end(); ++i) { - StaticObject &s_obj = *i; - + for (const StaticObject &s_obj : block->m_static_objects.m_stored) { // Create an active object from the data ServerActiveObject *obj = ServerActiveObject::create ((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data); @@ -1913,20 +1863,14 @@ 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::vector::iterator - i = new_stored.begin(); - i != new_stored.end(); ++i) { - StaticObject &s_obj = *i; + for (const StaticObject &s_obj : new_stored) { block->m_static_objects.m_stored.push_back(s_obj); } // Turn the active counterparts of activated objects not pending for // deactivation - for(std::map::iterator - i = block->m_static_objects.m_active.begin(); - i != block->m_static_objects.m_active.end(); ++i) - { - u16 id = i->first; + for (auto &i : block->m_static_objects.m_active) { + u16 id = i.first; ServerActiveObject *object = getActiveObject(id); assert(object); object->m_pending_deactivation = false; @@ -1956,12 +1900,11 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) void ServerEnvironment::deactivateFarObjects(bool _force_delete) { std::vector objects_to_remove; - for (ServerActiveObjectMap::iterator i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) { + for (auto &ao_it : m_active_objects) { // force_delete might be overriden per object bool force_delete = _force_delete; - ServerActiveObject* obj = i->second; + ServerActiveObject* obj = ao_it.second; assert(obj); // Do not deactivate if static data creation not allowed @@ -1972,7 +1915,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete) if(!force_delete && obj->m_pending_deactivation) continue; - u16 id = i->first; + u16 id = ao_it.first; v3f objectpos = obj->getBasePosition(); // The block in which the object resides in @@ -1997,7 +1940,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete) <getStaticData(&staticdata_new); StaticObject s_obj(obj->getType(), objectpos, staticdata_new); block->m_static_objects.insert(id, s_obj); @@ -2038,7 +1981,7 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete) if(obj->isStaticAllowed()) { // Create new static object - std::string staticdata_new = ""; + std::string staticdata_new; obj->getStaticData(&staticdata_new); StaticObject s_obj(obj->getType(), objectpos, staticdata_new); @@ -2179,9 +2122,8 @@ void ServerEnvironment::deactivateFarObjects(bool _force_delete) } // Remove references from m_active_objects - for(std::vector::iterator i = objects_to_remove.begin(); - i != objects_to_remove.end(); ++i) { - m_active_objects.erase(*i); + for (u16 i : objects_to_remove) { + m_active_objects.erase(i); } } @@ -2191,19 +2133,20 @@ PlayerDatabase *ServerEnvironment::openPlayerDatabase(const std::string &name, if (name == "sqlite3") return new PlayerDatabaseSQLite3(savedir); - else if (name == "dummy") + + if (name == "dummy") return new Database_Dummy(); #if USE_POSTGRESQL - else if (name == "postgresql") { - std::string connect_string = ""; + if (name == "postgresql") { + std::string connect_string; conf.getNoEx("pgsql_player_connection", connect_string); return new PlayerDatabasePostgreSQL(connect_string); } #endif - else if (name == "files") + if (name == "files") return new PlayerDatabaseFiles(savedir + DIR_DELIM + "players"); - else - throw BaseException(std::string("Database backend ") + name + " not supported."); + + throw BaseException(std::string("Database backend ") + name + " not supported."); } bool ServerEnvironment::migratePlayersDatabase(const GameParams &game_params, -- cgit v1.2.3