Skip to content

Commit c738d1e

Browse files
authoredAug 17, 2017
clientobject, clouds, collision, clientsimpleobject: code modernization (#6260)
* clientobject, clouds, collision, clientsimpleobject: code modernization * use range-based for loops * simplify some tests * various code style fixes * use emplace_back instead of push_back when necessary * use auto on some iterators * use default operator when needed * unroll v3s16 creation on collisionMoveSimple
1 parent 9bd1887 commit c738d1e

File tree

6 files changed

+41
-44
lines changed

6 files changed

+41
-44
lines changed
 

Diff for: ‎src/clientobject.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ClientActiveObject* ClientActiveObject::create(ActiveObjectType type,
4242
Client *client, ClientEnvironment *env)
4343
{
4444
// Find factory function
45-
std::unordered_map<u16, Factory>::iterator n = m_types.find(type);
45+
auto n = m_types.find(type);
4646
if (n == m_types.end()) {
4747
// If factory is not found, just return.
4848
warningstream << "ClientActiveObject: No factory for type="
@@ -57,8 +57,8 @@ ClientActiveObject* ClientActiveObject::create(ActiveObjectType type,
5757

5858
void ClientActiveObject::registerType(u16 type, Factory f)
5959
{
60-
std::unordered_map<u16, Factory>::iterator n = m_types.find(type);
61-
if(n != m_types.end())
60+
auto n = m_types.find(type);
61+
if (n != m_types.end())
6262
return;
6363
m_types[type] = f;
6464
}

Diff for: ‎src/clientsimpleobject.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class ClientSimpleObject
2929
public:
3030
bool m_to_be_removed = false;
3131

32-
ClientSimpleObject() {}
33-
virtual ~ClientSimpleObject() {}
32+
ClientSimpleObject() = default;
33+
virtual ~ClientSimpleObject() = default;
34+
3435
virtual void step(float dtime) {}
3536
};
3637

Diff for: ‎src/clouds.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void Clouds::render()
208208

209209
u32 i = GETINDEX(xi, zi, m_cloud_radius_i);
210210

211-
if(grid[i] == false)
211+
if (!grid[i])
212212
continue;
213213

214214
v2f p0 = v2f(xi,zi)*cloud_size + world_center_of_drawing_in_noise_f;

Diff for: ‎src/clouds.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Clouds : public scene::ISceneNode
7474

7575
void update(const v3f &camera_p, const video::SColorf &color);
7676

77-
void updateCameraOffset(v3s16 camera_offset)
77+
void updateCameraOffset(const v3s16 &camera_offset)
7878
{
7979
m_camera_offset = camera_offset;
8080
updateBox();

Diff for: ‎src/collision.cpp

+29-35
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,8 @@ bool wouldCollideWithCeiling(
188188

189189
assert(y_increase >= 0); // pre-condition
190190

191-
for (std::vector<NearbyCollisionInfo>::const_iterator it = cinfo.begin();
192-
it != cinfo.end(); ++it) {
193-
const aabb3f &staticbox = it->box;
191+
for (const auto &it : cinfo) {
192+
const aabb3f &staticbox = it.box;
194193
if ((movingbox.MaxEdge.Y - d <= staticbox.MinEdge.Y) &&
195194
(movingbox.MaxEdge.Y + y_increase > staticbox.MinEdge.Y) &&
196195
(movingbox.MinEdge.X < staticbox.MaxEdge.X) &&
@@ -203,7 +202,7 @@ bool wouldCollideWithCeiling(
203202
return false;
204203
}
205204

206-
static inline void getNeighborConnectingFace(v3s16 p, INodeDefManager *nodedef,
205+
static inline void getNeighborConnectingFace(const v3s16 &p, INodeDefManager *nodedef,
207206
Map *map, MapNode n, int v, int *neighbors)
208207
{
209208
MapNode n2 = map->getNodeNoEx(p);
@@ -255,7 +254,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
255254
std::vector<NearbyCollisionInfo> cinfo;
256255
{
257256
//TimeTaker tt2("collisionMoveSimple collect boxes");
258-
ScopeProfiler sp(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
257+
ScopeProfiler sp2(g_profiler, "collisionMoveSimple collect boxes avg", SPT_AVG);
259258

260259
v3f newpos_f = *pos_f + *speed_f * dtime;
261260
v3f minpos_f(
@@ -273,12 +272,10 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
273272

274273
bool any_position_valid = false;
275274

276-
for(s16 x = min.X; x <= max.X; x++)
277-
for(s16 y = min.Y; y <= max.Y; y++)
278-
for(s16 z = min.Z; z <= max.Z; z++)
279-
{
280-
v3s16 p(x,y,z);
281-
275+
v3s16 p;
276+
for (p.X = min.X; p.X <= max.X; p.X++)
277+
for (p.Y = min.Y; p.Y <= max.Y; p.Y++)
278+
for (p.Z = min.Z; p.Z <= max.Z; p.Z++) {
282279
bool is_position_valid;
283280
MapNode n = map->getNodeNoEx(p, &is_position_valid);
284281

@@ -288,12 +285,15 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
288285
any_position_valid = true;
289286
INodeDefManager *nodedef = gamedef->getNodeDefManager();
290287
const ContentFeatures &f = nodedef->get(n);
291-
if(f.walkable == false)
288+
289+
if (!f.walkable)
292290
continue;
291+
293292
int n_bouncy_value = itemgroup_get(f.groups, "bouncy");
294293

295294
int neighbors = 0;
296-
if (f.drawtype == NDT_NODEBOX && f.node_box.type == NODEBOX_CONNECTED) {
295+
if (f.drawtype == NDT_NODEBOX &&
296+
f.node_box.type == NODEBOX_CONNECTED) {
297297
v3s16 p2 = p;
298298

299299
p2.Y++;
@@ -321,20 +321,15 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
321321
}
322322
std::vector<aabb3f> nodeboxes;
323323
n.getCollisionBoxes(gamedef->ndef(), &nodeboxes, neighbors);
324-
for(std::vector<aabb3f>::iterator
325-
i = nodeboxes.begin();
326-
i != nodeboxes.end(); ++i)
327-
{
328-
aabb3f box = *i;
329-
box.MinEdge += v3f(x, y, z)*BS;
330-
box.MaxEdge += v3f(x, y, z)*BS;
331-
cinfo.push_back(NearbyCollisionInfo(false,
332-
false, n_bouncy_value, p, box));
324+
for (auto box : nodeboxes) {
325+
box.MinEdge += intToFloat(p, BS);
326+
box.MaxEdge += intToFloat(p, BS);
327+
cinfo.emplace_back(false, false, n_bouncy_value, p, box);
333328
}
334329
} else {
335330
// Collide with unloaded nodes
336331
aabb3f box = getNodeBox(p, BS);
337-
cinfo.push_back(NearbyCollisionInfo(true, false, 0, p, box));
332+
cinfo.emplace_back(true, false, 0, p, box);
338333
}
339334
}
340335

@@ -349,7 +344,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
349344

350345
if(collideWithObjects)
351346
{
352-
ScopeProfiler sp(g_profiler, "collisionMoveSimple objects avg", SPT_AVG);
347+
ScopeProfiler sp2(g_profiler, "collisionMoveSimple objects avg", SPT_AVG);
353348
//TimeTaker tt3("collisionMoveSimple collect object boxes");
354349

355350
/* add object boxes to cinfo */
@@ -361,9 +356,9 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
361356
f32 distance = speed_f->getLength();
362357
std::vector<DistanceSortedActiveObject> clientobjects;
363358
c_env->getActiveObjects(*pos_f, distance * 1.5, clientobjects);
364-
for (size_t i=0; i < clientobjects.size(); i++) {
365-
if ((self == 0) || (self != clientobjects[i].obj)) {
366-
objects.push_back((ActiveObject*)clientobjects[i].obj);
359+
for (auto &clientobject : clientobjects) {
360+
if (!self || (self != clientobject.obj)) {
361+
objects.push_back((ActiveObject*) clientobject.obj);
367362
}
368363
}
369364
}
@@ -375,9 +370,9 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
375370
f32 distance = speed_f->getLength();
376371
std::vector<u16> s_objects;
377372
s_env->getObjectsInsideRadius(s_objects, *pos_f, distance * 1.5);
378-
for (std::vector<u16>::iterator iter = s_objects.begin(); iter != s_objects.end(); ++iter) {
379-
ServerActiveObject *current = s_env->getActiveObject(*iter);
380-
if ((self == 0) || (self != current)) {
373+
for (u16 obj_id : s_objects) {
374+
ServerActiveObject *current = s_env->getActiveObject(obj_id);
375+
if (!self || (self != current)) {
381376
objects.push_back((ActiveObject*)current);
382377
}
383378
}
@@ -388,11 +383,11 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
388383
iter != objects.end(); ++iter) {
389384
ActiveObject *object = *iter;
390385

391-
if (object != NULL) {
386+
if (object) {
392387
aabb3f object_collisionbox;
393388
if (object->getCollisionBox(&object_collisionbox) &&
394389
object->collideWithObjects()) {
395-
cinfo.push_back(NearbyCollisionInfo(false, true, 0, v3s16(), object_collisionbox));
390+
cinfo.emplace_back(false, true, 0, v3s16(), object_collisionbox);
396391
}
397392
}
398393
}
@@ -417,7 +412,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
417412

418413
while(dtime > BS * 1e-10) {
419414
//TimeTaker tt3("collisionMoveSimple dtime loop");
420-
ScopeProfiler sp(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
415+
ScopeProfiler sp2(g_profiler, "collisionMoveSimple dtime loop avg", SPT_AVG);
421416

422417
// Avoid infinite loop
423418
loopcount++;
@@ -545,8 +540,7 @@ collisionMoveResult collisionMoveSimple(Environment *env, IGameDef *gamedef,
545540
aabb3f box = box_0;
546541
box.MinEdge += *pos_f;
547542
box.MaxEdge += *pos_f;
548-
for (u32 boxindex = 0; boxindex < cinfo.size(); boxindex++) {
549-
NearbyCollisionInfo &box_info = cinfo[boxindex];
543+
for (const auto &box_info : cinfo) {
550544
const aabb3f &cbox = box_info.box;
551545

552546
/*

Diff for: ‎src/collision.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ enum CollisionType
3636

3737
struct CollisionInfo
3838
{
39-
CollisionInfo() {}
39+
CollisionInfo() = default;
40+
4041
CollisionType type = COLLISION_NODE;
4142
v3s16 node_p = v3s16(-32768,-32768,-32768); // COLLISION_NODE
4243
v3f old_speed;
@@ -45,7 +46,8 @@ struct CollisionInfo
4546

4647
struct collisionMoveResult
4748
{
48-
collisionMoveResult() {}
49+
collisionMoveResult() = default;
50+
4951
bool touching_ground = false;
5052
bool collides = false;
5153
bool standing_on_object = false;

0 commit comments

Comments
 (0)
Please sign in to comment.