1
0
Fork 0

l_server, clientenvironment, clientiface: code modernization

* use range-based for loops
* use refs on some exceptions & one setter
mutilcraft-mt53
Loic Blot 2017-08-17 08:11:39 +02:00
parent 9c8fec83af
commit 3e80bf933f
No known key found for this signature in database
GPG Key ID: EFAA458E8C153987
4 changed files with 37 additions and 58 deletions

View File

@ -52,14 +52,12 @@ ClientEnvironment::ClientEnvironment(ClientMap *map,
ClientEnvironment::~ClientEnvironment() ClientEnvironment::~ClientEnvironment()
{ {
// delete active objects // delete active objects
for (ClientActiveObjectMap::iterator i = m_active_objects.begin(); for (auto &active_object : m_active_objects) {
i != m_active_objects.end(); ++i) { delete active_object.second;
delete i->second;
} }
for(std::vector<ClientSimpleObject*>::iterator for (auto &simple_object : m_simple_objects) {
i = m_simple_objects.begin(); i != m_simple_objects.end(); ++i) { delete simple_object;
delete *i;
} }
// Drop/delete map // Drop/delete map
@ -211,13 +209,11 @@ void ClientEnvironment::step(float dtime)
//std::cout<<"Looped "<<loopcount<<" times."<<std::endl; //std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
for(std::vector<CollisionInfo>::iterator i = player_collisions.begin(); for (const CollisionInfo &info : player_collisions) {
i != player_collisions.end(); ++i) {
CollisionInfo &info = *i;
v3f speed_diff = info.new_speed - info.old_speed;; v3f speed_diff = info.new_speed - info.old_speed;;
// Handle only fall damage // Handle only fall damage
// (because otherwise walking against something in fast_move kills you) // (because otherwise walking against something in fast_move kills you)
if(speed_diff.Y < 0 || info.old_speed.Y >= 0) if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
continue; continue;
// Get rid of other components // Get rid of other components
speed_diff.X = 0; speed_diff.X = 0;
@ -225,8 +221,7 @@ void ClientEnvironment::step(float dtime)
f32 pre_factor = 1; // 1 hp per node/s f32 pre_factor = 1; // 1 hp per node/s
f32 tolerance = BS*14; // 5 without damage f32 tolerance = BS*14; // 5 without damage
f32 post_factor = 1; // 1 hp per node/s f32 post_factor = 1; // 1 hp per node/s
if(info.type == COLLISION_NODE) if (info.type == COLLISION_NODE) {
{
const ContentFeatures &f = m_client->ndef()-> const ContentFeatures &f = m_client->ndef()->
get(m_map->getNodeNoEx(info.node_p)); get(m_map->getNodeNoEx(info.node_p));
// Determine fall damage multiplier // Determine fall damage multiplier
@ -343,14 +338,12 @@ void ClientEnvironment::step(float dtime)
g_profiler->avg("CEnv: num of objects", m_active_objects.size()); g_profiler->avg("CEnv: num of objects", m_active_objects.size());
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21); bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
for (ClientActiveObjectMap::iterator i = m_active_objects.begin(); for (auto &ao_it : m_active_objects) {
i != m_active_objects.end(); ++i) { ClientActiveObject* obj = ao_it.second;
ClientActiveObject* obj = i->second;
// Step object // Step object
obj->step(dtime, this); obj->step(dtime, this);
if(update_lighting) if (update_lighting) {
{
// Update lighting // Update lighting
u8 light = 0; u8 light = 0;
bool pos_ok; bool pos_ok;
@ -371,9 +364,8 @@ void ClientEnvironment::step(float dtime)
Step and handle simple objects Step and handle simple objects
*/ */
g_profiler->avg("CEnv: num of simple objects", m_simple_objects.size()); g_profiler->avg("CEnv: num of simple objects", m_simple_objects.size());
for(std::vector<ClientSimpleObject*>::iterator for (auto i = m_simple_objects.begin(); i != m_simple_objects.end();) {
i = m_simple_objects.begin(); i != m_simple_objects.end();) { auto cur = i;
std::vector<ClientSimpleObject*>::iterator cur = i;
ClientSimpleObject *simple = *cur; ClientSimpleObject *simple = *cur;
simple->step(dtime); simple->step(dtime);
@ -397,13 +389,13 @@ GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
ClientActiveObject *obj = getActiveObject(id); ClientActiveObject *obj = getActiveObject(id);
if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC) if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
return (GenericCAO*) obj; return (GenericCAO*) obj;
else
return NULL; return NULL;
} }
ClientActiveObject* ClientEnvironment::getActiveObject(u16 id) ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
{ {
ClientActiveObjectMap::iterator n = m_active_objects.find(id); auto n = m_active_objects.find(id);
if (n == m_active_objects.end()) if (n == m_active_objects.end())
return NULL; return NULL;
return n->second; return n->second;
@ -412,10 +404,8 @@ ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
bool isFreeClientActiveObjectId(const u16 id, bool isFreeClientActiveObjectId(const u16 id,
ClientActiveObjectMap &objects) ClientActiveObjectMap &objects)
{ {
if(id == 0) return id != 0 && objects.find(id) == objects.end();
return false;
return objects.find(id) == objects.end();
} }
u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects) u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
@ -580,18 +570,15 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d, void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
std::vector<DistanceSortedActiveObject> &dest) std::vector<DistanceSortedActiveObject> &dest)
{ {
for (ClientActiveObjectMap::iterator i = m_active_objects.begin(); for (auto &ao_it : m_active_objects) {
i != m_active_objects.end(); ++i) { ClientActiveObject* obj = ao_it.second;
ClientActiveObject* obj = i->second;
f32 d = (obj->getPosition() - origin).getLength(); f32 d = (obj->getPosition() - origin).getLength();
if(d > max_d) if (d > max_d)
continue; continue;
DistanceSortedActiveObject dso(obj, d); dest.emplace_back(obj, d);
dest.push_back(dso);
} }
} }
@ -614,12 +601,13 @@ void ClientEnvironment::getSelectedActiveObjects(
shootline_on_map.getLength() + 10.0f, allObjects); shootline_on_map.getLength() + 10.0f, allObjects);
const v3f line_vector = shootline_on_map.getVector(); const v3f line_vector = shootline_on_map.getVector();
for (u32 i = 0; i < allObjects.size(); i++) { for (const auto &allObject : allObjects) {
ClientActiveObject *obj = allObjects[i].obj; ClientActiveObject *obj = allObject.obj;
aabb3f selection_box; aabb3f selection_box;
if (!obj->getSelectionBox(&selection_box)) if (!obj->getSelectionBox(&selection_box))
continue; continue;
v3f pos = obj->getPosition();
const v3f &pos = obj->getPosition();
aabb3f offsetted_box(selection_box.MinEdge + pos, aabb3f offsetted_box(selection_box.MinEdge + pos,
selection_box.MaxEdge + pos); selection_box.MaxEdge + pos);
@ -627,9 +615,8 @@ void ClientEnvironment::getSelectedActiveObjects(
v3s16 current_normal; v3s16 current_normal;
if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector, if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
&current_intersection, &current_normal)) { &current_intersection, &current_normal)) {
objects.push_back(PointedThing( objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
(s16) obj->getId(), current_intersection, current_normal, (current_intersection - shootline_on_map.start).getLengthSQ());
(current_intersection - shootline_on_map.start).getLengthSQ()));
} }
} }
} }

View File

@ -139,7 +139,7 @@ public:
const std::list<std::string> &getPlayerNames() { return m_player_names; } const std::list<std::string> &getPlayerNames() { return m_player_names; }
void addPlayerName(const std::string &name) { m_player_names.push_back(name); } void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
void removePlayerName(const std::string &name) { m_player_names.remove(name); } void removePlayerName(const std::string &name) { m_player_names.remove(name); }
void updateCameraOffset(v3s16 camera_offset) void updateCameraOffset(const 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; }
private: private:

View File

@ -205,7 +205,7 @@ enum ClientStateEvent
*/ */
struct PrioritySortedBlockTransfer struct PrioritySortedBlockTransfer
{ {
PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id) PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, u16 a_peer_id)
{ {
priority = a_priority; priority = a_priority;
pos = a_pos; pos = a_pos;
@ -246,8 +246,8 @@ public:
bool isMechAllowed(AuthMechanism mech) bool isMechAllowed(AuthMechanism mech)
{ return allowed_auth_mechs & mech; } { return allowed_auth_mechs & mech; }
RemoteClient() {} RemoteClient() = default;
~RemoteClient() {} ~RemoteClient() = default;
/* /*
Finds block that should be sent next to the client. Finds block that should be sent next to the client.
@ -495,7 +495,6 @@ private:
// Environment // Environment
ServerEnvironment *m_env; ServerEnvironment *m_env;
std::mutex m_env_mutex;
float m_print_info_timer; float m_print_info_timer;

View File

@ -103,10 +103,9 @@ int ModApiServer::l_get_player_privs(lua_State *L)
lua_newtable(L); lua_newtable(L);
int table = lua_gettop(L); int table = lua_gettop(L);
std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name); std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
for(std::set<std::string>::const_iterator for (const std::string &privs_ : privs_s) {
i = privs_s.begin(); i != privs_s.end(); ++i){
lua_pushboolean(L, true); lua_pushboolean(L, true);
lua_setfield(L, table, i->c_str()); lua_setfield(L, table, privs_.c_str());
} }
lua_pushvalue(L, table); lua_pushvalue(L, table);
return 1; return 1;
@ -129,9 +128,7 @@ int ModApiServer::l_get_player_ip(lua_State *L)
std::string ip_str = addr.serializeString(); std::string ip_str = addr.serializeString();
lua_pushstring(L, ip_str.c_str()); lua_pushstring(L, ip_str.c_str());
return 1; return 1;
} } catch (const con::PeerNotFoundException &) {
catch(con::PeerNotFoundException) // unlikely
{
dstream << FUNCTION_NAME << ": peer was not found" << std::endl; dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
lua_pushnil(L); // error lua_pushnil(L); // error
return 1; return 1;
@ -154,9 +151,7 @@ int ModApiServer::l_get_player_information(lua_State *L)
try try
{ {
addr = getServer(L)->getPeerAddress(player->peer_id); addr = getServer(L)->getPeerAddress(player->peer_id);
} } catch(const con::PeerNotFoundException &) {
catch(con::PeerNotFoundException) // unlikely
{
dstream << FUNCTION_NAME << ": peer was not found" << std::endl; dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
lua_pushnil(L); // error lua_pushnil(L); // error
return 1; return 1;
@ -235,7 +230,7 @@ int ModApiServer::l_get_player_information(lua_State *L)
lua_pushstring(L,"protocol_version"); lua_pushstring(L,"protocol_version");
lua_pushnumber(L, prot_vers); lua_pushnumber(L, prot_vers);
lua_settable(L, table); lua_settable(L, table);
#ifndef NDEBUG #ifndef NDEBUG
lua_pushstring(L,"serialization_version"); lua_pushstring(L,"serialization_version");
lua_pushnumber(L, ser_vers); lua_pushnumber(L, ser_vers);
@ -299,9 +294,7 @@ int ModApiServer::l_ban_player(lua_State *L)
dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->peer_id); dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->peer_id);
std::string ip_str = addr.serializeString(); std::string ip_str = addr.serializeString();
getServer(L)->setIpBanned(ip_str, name); getServer(L)->setIpBanned(ip_str, name);
} } catch(const con::PeerNotFoundException &) {
catch(con::PeerNotFoundException) // unlikely
{
dstream << FUNCTION_NAME << ": peer was not found" << std::endl; dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
lua_pushboolean(L, false); // error lua_pushboolean(L, false); // error
return 1; return 1;
@ -478,7 +471,7 @@ int ModApiServer::l_is_singleplayer(lua_State *L)
int ModApiServer::l_notify_authentication_modified(lua_State *L) int ModApiServer::l_notify_authentication_modified(lua_State *L)
{ {
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
std::string name = ""; std::string name;
if(lua_isstring(L, 1)) if(lua_isstring(L, 1))
name = lua_tostring(L, 1); name = lua_tostring(L, 1);
getServer(L)->reportPrivsModified(name); getServer(L)->reportPrivsModified(name);