Skip to content

Commit 3e80bf9

Browse files
committedAug 17, 2017
l_server, clientenvironment, clientiface: code modernization
* use range-based for loops * use refs on some exceptions & one setter
1 parent 9c8fec8 commit 3e80bf9

File tree

4 files changed

+37
-58
lines changed

4 files changed

+37
-58
lines changed
 

‎src/clientenvironment.cpp

+26-39
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,12 @@ ClientEnvironment::ClientEnvironment(ClientMap *map,
5252
ClientEnvironment::~ClientEnvironment()
5353
{
5454
// delete active objects
55-
for (ClientActiveObjectMap::iterator i = m_active_objects.begin();
56-
i != m_active_objects.end(); ++i) {
57-
delete i->second;
55+
for (auto &active_object : m_active_objects) {
56+
delete active_object.second;
5857
}
5958

60-
for(std::vector<ClientSimpleObject*>::iterator
61-
i = m_simple_objects.begin(); i != m_simple_objects.end(); ++i) {
62-
delete *i;
59+
for (auto &simple_object : m_simple_objects) {
60+
delete simple_object;
6361
}
6462

6563
// Drop/delete map
@@ -211,22 +209,19 @@ void ClientEnvironment::step(float dtime)
211209

212210
//std::cout<<"Looped "<<loopcount<<" times."<<std::endl;
213211

214-
for(std::vector<CollisionInfo>::iterator i = player_collisions.begin();
215-
i != player_collisions.end(); ++i) {
216-
CollisionInfo &info = *i;
212+
for (const CollisionInfo &info : player_collisions) {
217213
v3f speed_diff = info.new_speed - info.old_speed;;
218214
// Handle only fall damage
219215
// (because otherwise walking against something in fast_move kills you)
220-
if(speed_diff.Y < 0 || info.old_speed.Y >= 0)
216+
if (speed_diff.Y < 0 || info.old_speed.Y >= 0)
221217
continue;
222218
// Get rid of other components
223219
speed_diff.X = 0;
224220
speed_diff.Z = 0;
225221
f32 pre_factor = 1; // 1 hp per node/s
226222
f32 tolerance = BS*14; // 5 without damage
227223
f32 post_factor = 1; // 1 hp per node/s
228-
if(info.type == COLLISION_NODE)
229-
{
224+
if (info.type == COLLISION_NODE) {
230225
const ContentFeatures &f = m_client->ndef()->
231226
get(m_map->getNodeNoEx(info.node_p));
232227
// Determine fall damage multiplier
@@ -343,14 +338,12 @@ void ClientEnvironment::step(float dtime)
343338

344339
g_profiler->avg("CEnv: num of objects", m_active_objects.size());
345340
bool update_lighting = m_active_object_light_update_interval.step(dtime, 0.21);
346-
for (ClientActiveObjectMap::iterator i = m_active_objects.begin();
347-
i != m_active_objects.end(); ++i) {
348-
ClientActiveObject* obj = i->second;
341+
for (auto &ao_it : m_active_objects) {
342+
ClientActiveObject* obj = ao_it.second;
349343
// Step object
350344
obj->step(dtime, this);
351345

352-
if(update_lighting)
353-
{
346+
if (update_lighting) {
354347
// Update lighting
355348
u8 light = 0;
356349
bool pos_ok;
@@ -371,9 +364,8 @@ void ClientEnvironment::step(float dtime)
371364
Step and handle simple objects
372365
*/
373366
g_profiler->avg("CEnv: num of simple objects", m_simple_objects.size());
374-
for(std::vector<ClientSimpleObject*>::iterator
375-
i = m_simple_objects.begin(); i != m_simple_objects.end();) {
376-
std::vector<ClientSimpleObject*>::iterator cur = i;
367+
for (auto i = m_simple_objects.begin(); i != m_simple_objects.end();) {
368+
auto cur = i;
377369
ClientSimpleObject *simple = *cur;
378370

379371
simple->step(dtime);
@@ -397,13 +389,13 @@ GenericCAO* ClientEnvironment::getGenericCAO(u16 id)
397389
ClientActiveObject *obj = getActiveObject(id);
398390
if (obj && obj->getType() == ACTIVEOBJECT_TYPE_GENERIC)
399391
return (GenericCAO*) obj;
400-
else
401-
return NULL;
392+
393+
return NULL;
402394
}
403395

404396
ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
405397
{
406-
ClientActiveObjectMap::iterator n = m_active_objects.find(id);
398+
auto n = m_active_objects.find(id);
407399
if (n == m_active_objects.end())
408400
return NULL;
409401
return n->second;
@@ -412,10 +404,8 @@ ClientActiveObject* ClientEnvironment::getActiveObject(u16 id)
412404
bool isFreeClientActiveObjectId(const u16 id,
413405
ClientActiveObjectMap &objects)
414406
{
415-
if(id == 0)
416-
return false;
407+
return id != 0 && objects.find(id) == objects.end();
417408

418-
return objects.find(id) == objects.end();
419409
}
420410

421411
u16 getFreeClientActiveObjectId(ClientActiveObjectMap &objects)
@@ -580,18 +570,15 @@ void ClientEnvironment::updateLocalPlayerBreath(u16 breath)
580570
void ClientEnvironment::getActiveObjects(v3f origin, f32 max_d,
581571
std::vector<DistanceSortedActiveObject> &dest)
582572
{
583-
for (ClientActiveObjectMap::iterator i = m_active_objects.begin();
584-
i != m_active_objects.end(); ++i) {
585-
ClientActiveObject* obj = i->second;
573+
for (auto &ao_it : m_active_objects) {
574+
ClientActiveObject* obj = ao_it.second;
586575

587576
f32 d = (obj->getPosition() - origin).getLength();
588577

589-
if(d > max_d)
578+
if (d > max_d)
590579
continue;
591580

592-
DistanceSortedActiveObject dso(obj, d);
593-
594-
dest.push_back(dso);
581+
dest.emplace_back(obj, d);
595582
}
596583
}
597584

@@ -614,22 +601,22 @@ void ClientEnvironment::getSelectedActiveObjects(
614601
shootline_on_map.getLength() + 10.0f, allObjects);
615602
const v3f line_vector = shootline_on_map.getVector();
616603

617-
for (u32 i = 0; i < allObjects.size(); i++) {
618-
ClientActiveObject *obj = allObjects[i].obj;
604+
for (const auto &allObject : allObjects) {
605+
ClientActiveObject *obj = allObject.obj;
619606
aabb3f selection_box;
620607
if (!obj->getSelectionBox(&selection_box))
621608
continue;
622-
v3f pos = obj->getPosition();
609+
610+
const v3f &pos = obj->getPosition();
623611
aabb3f offsetted_box(selection_box.MinEdge + pos,
624612
selection_box.MaxEdge + pos);
625613

626614
v3f current_intersection;
627615
v3s16 current_normal;
628616
if (boxLineCollision(offsetted_box, shootline_on_map.start, line_vector,
629617
&current_intersection, &current_normal)) {
630-
objects.push_back(PointedThing(
631-
(s16) obj->getId(), current_intersection, current_normal,
632-
(current_intersection - shootline_on_map.start).getLengthSQ()));
618+
objects.emplace_back((s16) obj->getId(), current_intersection, current_normal,
619+
(current_intersection - shootline_on_map.start).getLengthSQ());
633620
}
634621
}
635622
}

‎src/clientenvironment.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class ClientEnvironment : public Environment
139139
const std::list<std::string> &getPlayerNames() { return m_player_names; }
140140
void addPlayerName(const std::string &name) { m_player_names.push_back(name); }
141141
void removePlayerName(const std::string &name) { m_player_names.remove(name); }
142-
void updateCameraOffset(v3s16 camera_offset)
142+
void updateCameraOffset(const v3s16 &camera_offset)
143143
{ m_camera_offset = camera_offset; }
144144
v3s16 getCameraOffset() const { return m_camera_offset; }
145145
private:

‎src/clientiface.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ enum ClientStateEvent
205205
*/
206206
struct PrioritySortedBlockTransfer
207207
{
208-
PrioritySortedBlockTransfer(float a_priority, v3s16 a_pos, u16 a_peer_id)
208+
PrioritySortedBlockTransfer(float a_priority, const v3s16 &a_pos, u16 a_peer_id)
209209
{
210210
priority = a_priority;
211211
pos = a_pos;
@@ -246,8 +246,8 @@ class RemoteClient
246246
bool isMechAllowed(AuthMechanism mech)
247247
{ return allowed_auth_mechs & mech; }
248248

249-
RemoteClient() {}
250-
~RemoteClient() {}
249+
RemoteClient() = default;
250+
~RemoteClient() = default;
251251

252252
/*
253253
Finds block that should be sent next to the client.
@@ -495,7 +495,6 @@ class ClientInterface {
495495

496496
// Environment
497497
ServerEnvironment *m_env;
498-
std::mutex m_env_mutex;
499498

500499
float m_print_info_timer;
501500

‎src/script/lua_api/l_server.cpp

+7-14
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ int ModApiServer::l_get_player_privs(lua_State *L)
103103
lua_newtable(L);
104104
int table = lua_gettop(L);
105105
std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
106-
for(std::set<std::string>::const_iterator
107-
i = privs_s.begin(); i != privs_s.end(); ++i){
106+
for (const std::string &privs_ : privs_s) {
108107
lua_pushboolean(L, true);
109-
lua_setfield(L, table, i->c_str());
108+
lua_setfield(L, table, privs_.c_str());
110109
}
111110
lua_pushvalue(L, table);
112111
return 1;
@@ -129,9 +128,7 @@ int ModApiServer::l_get_player_ip(lua_State *L)
129128
std::string ip_str = addr.serializeString();
130129
lua_pushstring(L, ip_str.c_str());
131130
return 1;
132-
}
133-
catch(con::PeerNotFoundException) // unlikely
134-
{
131+
} catch (const con::PeerNotFoundException &) {
135132
dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
136133
lua_pushnil(L); // error
137134
return 1;
@@ -154,9 +151,7 @@ int ModApiServer::l_get_player_information(lua_State *L)
154151
try
155152
{
156153
addr = getServer(L)->getPeerAddress(player->peer_id);
157-
}
158-
catch(con::PeerNotFoundException) // unlikely
159-
{
154+
} catch(const con::PeerNotFoundException &) {
160155
dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
161156
lua_pushnil(L); // error
162157
return 1;
@@ -235,7 +230,7 @@ int ModApiServer::l_get_player_information(lua_State *L)
235230
lua_pushstring(L,"protocol_version");
236231
lua_pushnumber(L, prot_vers);
237232
lua_settable(L, table);
238-
233+
239234
#ifndef NDEBUG
240235
lua_pushstring(L,"serialization_version");
241236
lua_pushnumber(L, ser_vers);
@@ -299,9 +294,7 @@ int ModApiServer::l_ban_player(lua_State *L)
299294
dynamic_cast<ServerEnvironment *>(getEnv(L))->getPlayer(name)->peer_id);
300295
std::string ip_str = addr.serializeString();
301296
getServer(L)->setIpBanned(ip_str, name);
302-
}
303-
catch(con::PeerNotFoundException) // unlikely
304-
{
297+
} catch(const con::PeerNotFoundException &) {
305298
dstream << FUNCTION_NAME << ": peer was not found" << std::endl;
306299
lua_pushboolean(L, false); // error
307300
return 1;
@@ -478,7 +471,7 @@ int ModApiServer::l_is_singleplayer(lua_State *L)
478471
int ModApiServer::l_notify_authentication_modified(lua_State *L)
479472
{
480473
NO_MAP_LOCK_REQUIRED;
481-
std::string name = "";
474+
std::string name;
482475
if(lua_isstring(L, 1))
483476
name = lua_tostring(L, 1);
484477
getServer(L)->reportPrivsModified(name);

0 commit comments

Comments
 (0)
Please sign in to comment.