From 613797a3048907275ceebe29582b9fc2761b1f25 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Oct 2016 09:03:55 +0200 Subject: Replace various std::map with UNORDERED_MAP + various cleanups This is part 2 for 5f084cd98d7b3326b51320455364337539710efd Other improvements: * Use the defined ItemGroupList when used * make Client::checkPrivilege const * inline some trivial functions * Add ActiveObjectMap typedef * Add SettingsEntries typedef --- src/environment.cpp | 60 ++++++++++++++++++----------------------------------- 1 file changed, 20 insertions(+), 40 deletions(-) (limited to 'src/environment.cpp') diff --git a/src/environment.cpp b/src/environment.cpp index eea264699..34b3c34f4 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -1124,14 +1124,12 @@ bool ServerEnvironment::swapNode(v3s16 p, const MapNode &n) void ServerEnvironment::getObjectsInsideRadius(std::vector &objects, v3f pos, float radius) { - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for (ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; u16 id = i->first; v3f objectpos = obj->getBasePosition(); - if(objectpos.getDistanceFrom(pos) > radius) + if (objectpos.getDistanceFrom(pos) > radius) continue; objects.push_back(id); } @@ -1142,8 +1140,7 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) infostream << "ServerEnvironment::clearObjects(): " << "Removing all active objects" << std::endl; std::vector objects_to_remove; - for (std::map::iterator - i = m_active_objects.begin(); + for (ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; if (obj->getType() == ACTIVEOBJECT_TYPE_PLAYER) @@ -1518,10 +1515,8 @@ void ServerEnvironment::step(float dtime) send_recommended = true; } - for(std::map::iterator - i = m_active_objects.begin(); - i != m_active_objects.end(); ++i) - { + for(ActiveObjectMap::iterator i = m_active_objects.begin(); + i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; // Don't step if is to be removed or stored statically if(obj->m_removed || obj->m_pending_deactivation) @@ -1554,7 +1549,7 @@ void ServerEnvironment::step(float dtime) Manage particle spawner expiration */ if (m_particle_management_interval.step(dtime, 1.0)) { - for (std::map::iterator i = m_particle_spawners.begin(); + for (UNORDERED_MAP::iterator i = m_particle_spawners.begin(); i != m_particle_spawners.end(); ) { //non expiring spawners if (i->second == PARTICLE_SPAWNER_NO_EXPIRY) { @@ -1579,8 +1574,7 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) u32 id = 0; for (;;) { // look for unused particlespawner id id++; - std::map::iterator f; - f = m_particle_spawners.find(id); + UNORDERED_MAP::iterator f = m_particle_spawners.find(id); if (f == m_particle_spawners.end()) { m_particle_spawners[id] = time; break; @@ -1589,31 +1583,21 @@ u32 ServerEnvironment::addParticleSpawner(float exptime) return id; } -void ServerEnvironment::deleteParticleSpawner(u32 id) -{ - m_particle_spawners.erase(id); -} - ServerActiveObject* ServerEnvironment::getActiveObject(u16 id) { - std::map::iterator n; - n = m_active_objects.find(id); - if(n == m_active_objects.end()) - return NULL; - return n->second; + ActiveObjectMap::iterator n = m_active_objects.find(id); + return (n != m_active_objects.end() ? n->second : NULL); } -bool isFreeServerActiveObjectId(u16 id, - std::map &objects) +bool isFreeServerActiveObjectId(u16 id, ActiveObjectMap &objects) { - if(id == 0) + if (id == 0) return false; return objects.find(id) == objects.end(); } -u16 getFreeServerActiveObjectId( - std::map &objects) +u16 getFreeServerActiveObjectId(ActiveObjectMap &objects) { //try to reuse id's as late as possible static u16 last_used_id = 0; @@ -1659,8 +1643,7 @@ void ServerEnvironment::getAddedActiveObjects(Player *player, s16 radius, - discard objects that are found in current_objects. - add remaining objects to added_objects */ - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; @@ -1756,8 +1739,7 @@ void ServerEnvironment::setStaticForActiveObjectsInBlock( so_it = block->m_static_objects.m_active.begin(); so_it != block->m_static_objects.m_active.end(); ++so_it) { // Get the ServerActiveObject counterpart to this StaticObject - std::map::iterator ao_it; - ao_it = m_active_objects.find(so_it->first); + ActiveObjectMap::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(): " @@ -1806,8 +1788,8 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, verbosestream<<"ServerEnvironment::addActiveObjectRaw(): " <<"supplied with id "<getId()<getId(), m_active_objects) == false) - { + + if(!isFreeServerActiveObjectId(object->getId(), m_active_objects)) { errorstream<<"ServerEnvironment::addActiveObjectRaw(): " <<"id is not free ("<getId()<<")"<environmentDeletes()) @@ -1875,8 +1857,7 @@ u16 ServerEnvironment::addActiveObjectRaw(ServerActiveObject *object, void ServerEnvironment::removeRemovedObjects() { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { u16 id = i->first; ServerActiveObject* obj = i->second; @@ -1894,7 +1875,7 @@ void ServerEnvironment::removeRemovedObjects() We will delete objects that are marked as removed or thatare waiting for deletion after deactivation */ - if(obj->m_removed == false && obj->m_pending_deactivation == false) + if (!obj->m_removed && !obj->m_pending_deactivation) continue; /* @@ -2094,8 +2075,7 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s) void ServerEnvironment::deactivateFarObjects(bool force_delete) { std::vector objects_to_remove; - for(std::map::iterator - i = m_active_objects.begin(); + for(ActiveObjectMap::iterator i = m_active_objects.begin(); i != m_active_objects.end(); ++i) { ServerActiveObject* obj = i->second; assert(obj); -- cgit v1.2.3