Skip to content

Commit

Permalink
Modernize src/c* src/d* and src/e* files (#6263)
Browse files Browse the repository at this point in the history
* Modernize src/c* src/d* and src/e* files

* default operator
* redundant init
* delete default constructors on CraftDefinition childs (never used)
* fix some missing init values
* const ref fix reported by clang-tidy
* ranged-based for loops
* simple conditions & returns
* empty stl function instead of size
* emplace_back stl function instead of push_back + construct temp obj
* auto for some iterators
* code style fixes
* c++ stl headers instead of C stl headers (stdio.h -> cstdio)
  • Loading branch information
nerzhul committed Aug 17, 2017
1 parent 921151d commit 13e995b
Show file tree
Hide file tree
Showing 25 changed files with 298 additions and 343 deletions.
110 changes: 46 additions & 64 deletions src/content_cao.cpp
Expand Up @@ -119,7 +119,7 @@ class TestCAO : public ClientActiveObject
{
public:
TestCAO(Client *client, ClientEnvironment *env);
virtual ~TestCAO();
virtual ~TestCAO() = default;

ActiveObjectType getType() const
{
Expand Down Expand Up @@ -155,10 +155,6 @@ TestCAO::TestCAO(Client *client, ClientEnvironment *env):
ClientActiveObject::registerType(getType(), create);
}

TestCAO::~TestCAO()
{
}

ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
{
return new TestCAO(client, env);
Expand Down Expand Up @@ -261,7 +257,7 @@ class ItemCAO : public ClientActiveObject
{
public:
ItemCAO(Client *client, ClientEnvironment *env);
virtual ~ItemCAO();
virtual ~ItemCAO() = default;

ActiveObjectType getType() const
{
Expand Down Expand Up @@ -323,10 +319,6 @@ ItemCAO::ItemCAO(Client *client, ClientEnvironment *env):
}
}

ItemCAO::~ItemCAO()
{
}

ClientActiveObject* ItemCAO::create(Client *client, ClientEnvironment *env)
{
return new ItemCAO(client, env);
Expand Down Expand Up @@ -563,7 +555,7 @@ void GenericCAO::initialize(const std::string &data)
player->setCAO(this);
}
if (m_client->getProtoVersion() < 33)
m_env->addPlayerName(m_name.c_str());
m_env->addPlayerName(m_name);
}
}

Expand Down Expand Up @@ -607,7 +599,7 @@ void GenericCAO::processInitData(const std::string &data)
GenericCAO::~GenericCAO()
{
if (m_is_player && m_client->getProtoVersion() < 33) {
m_env->removePlayerName(m_name.c_str());
m_env->removePlayerName(m_name);
}
removeFromScene(true);
}
Expand All @@ -628,8 +620,8 @@ v3f GenericCAO::getPosition()
scene::ISceneNode *node = getSceneNode();
if (node)
return node->getAbsolutePosition();
else
return m_position;

return m_position;
}
return pos_translator.vect_show;
}
Expand All @@ -638,11 +630,17 @@ scene::ISceneNode* GenericCAO::getSceneNode()
{
if (m_meshnode) {
return m_meshnode;
} else if (m_animated_meshnode) {
}

if (m_animated_meshnode) {
return m_animated_meshnode;
} else if (m_wield_meshnode) {
}

if (m_wield_meshnode) {
return m_wield_meshnode;
} else if (m_spritenode) {
}

if (m_spritenode) {
return m_spritenode;
}
return NULL;
Expand All @@ -655,8 +653,8 @@ scene::IAnimatedMeshSceneNode* GenericCAO::getAnimatedMeshSceneNode()

void GenericCAO::setChildrenVisible(bool toset)
{
for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
GenericCAO *obj = m_env->getGenericCAO(m_children[i]);
for (u16 cao_id : m_children) {
GenericCAO *obj = m_env->getGenericCAO(cao_id);
if (obj) {
obj->setVisible(toset);
}
Expand Down Expand Up @@ -686,8 +684,7 @@ void GenericCAO::removeFromScene(bool permanent)
// Should be true when removing the object permanently and false when refreshing (eg: updating visuals)
if((m_env != NULL) && (permanent))
{
for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
u16 ci = m_children[i];
for (u16 ci : m_children) {
if (m_env->attachement_parent_ids[ci] == getId()) {
m_env->attachement_parent_ids[ci] = 0;
}
Expand Down Expand Up @@ -858,14 +855,13 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
}
else
errorstream<<"GenericCAO::addToScene(): Could not load mesh "<<m_prop.mesh<<std::endl;
}
else if(m_prop.visual == "wielditem") {
} else if (m_prop.visual == "wielditem") {
ItemStack item;
infostream << "GenericCAO::addToScene(): wielditem" << std::endl;
if (m_prop.wield_item == "") {
if (m_prop.wield_item.empty()) {
// Old format, only textures are specified.
infostream << "textures: " << m_prop.textures.size() << std::endl;
if (m_prop.textures.size() >= 1) {
if (!m_prop.textures.empty()) {
infostream << "textures[0]: " << m_prop.textures[0]
<< std::endl;
IItemDefManager *idef = m_client->idef();
Expand Down Expand Up @@ -894,7 +890,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
updateTextures(m_current_texture_modifier);

scene::ISceneNode *node = getSceneNode();
if (node && m_prop.nametag != "" && !m_is_local_player) {
if (node && !m_prop.nametag.empty() && !m_is_local_player) {
// Add nametag
v3f pos;
pos.Y = m_prop.collisionbox.MaxEdge.Y + 0.3f;
Expand All @@ -919,8 +915,8 @@ void GenericCAO::updateLight(u8 light_at_pos)
updateLightNoCheck(light_at_pos);

// Update light of all children
for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
for (u16 i : m_children) {
ClientActiveObject *obj = m_env->getActiveObject(i);
if (obj) {
obj->updateLightNoCheck(light_at_pos);
}
Expand Down Expand Up @@ -1046,9 +1042,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)

// Attachments, part 1: All attached objects must be unparented first,
// or Irrlicht causes a segmentation fault
for(std::vector<u16>::iterator ci = m_children.begin();
ci != m_children.end();)
{
for (auto ci = m_children.begin(); ci != m_children.end();) {
if (m_env->attachement_parent_ids[*ci] != getId()) {
ci = m_children.erase(ci);
continue;
Expand All @@ -1066,9 +1060,9 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
addToScene(m_client->tsrc());

// Attachments, part 2: Now that the parent has been refreshed, put its attachments back
for (std::vector<u16>::size_type i = 0; i < m_children.size(); i++) {
for (u16 cao_id : m_children) {
// Get the object of the child
ClientActiveObject *obj = m_env->getActiveObject(m_children[i]);
ClientActiveObject *obj = m_env->getActiveObject(cao_id);
if (obj)
obj->setAttachments();
}
Expand Down Expand Up @@ -1248,12 +1242,10 @@ void GenericCAO::updateTextures(std::string mod)
m_previous_texture_modifier = m_current_texture_modifier;
m_current_texture_modifier = mod;

if(m_spritenode)
{
if(m_prop.visual == "sprite")
{
if (m_spritenode) {
if (m_prop.visual == "sprite") {
std::string texturestring = "unknown_node.png";
if(m_prop.textures.size() >= 1)
if (!m_prop.textures.empty())
texturestring = m_prop.textures[0];
texturestring += mod;
m_spritenode->setMaterialTexture(0,
Expand All @@ -1262,8 +1254,7 @@ void GenericCAO::updateTextures(std::string mod)
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
// has directional lighting, it should work automatically.
if(m_prop.colors.size() >= 1)
{
if (!m_prop.colors.empty()) {
m_spritenode->getMaterial(0).AmbientColor = m_prop.colors[0];
m_spritenode->getMaterial(0).DiffuseColor = m_prop.colors[0];
m_spritenode->getMaterial(0).SpecularColor = m_prop.colors[0];
Expand All @@ -1274,20 +1265,17 @@ void GenericCAO::updateTextures(std::string mod)
m_spritenode->getMaterial(0).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
}
}
if(m_animated_meshnode)
{
if(m_prop.visual == "mesh")
{

if (m_animated_meshnode) {
if (m_prop.visual == "mesh") {
for (u32 i = 0; i < m_prop.textures.size() &&
i < m_animated_meshnode->getMaterialCount(); ++i)
{
i < m_animated_meshnode->getMaterialCount(); ++i) {
std::string texturestring = m_prop.textures[i];
if(texturestring == "")
if (texturestring.empty())
continue; // Empty texture string means don't modify that material
texturestring += mod;
video::ITexture* texture = tsrc->getTextureForMesh(texturestring);
if(!texture)
{
if (!texture) {
errorstream<<"GenericCAO::updateTextures(): Could not load texture "<<texturestring<<std::endl;
continue;
}
Expand Down Expand Up @@ -1351,13 +1339,11 @@ void GenericCAO::updateTextures(std::string mod)
m_meshnode->getMaterial(i).setFlag(video::EMF_BILINEAR_FILTER, use_bilinear_filter);
m_meshnode->getMaterial(i).setFlag(video::EMF_ANISOTROPIC_FILTER, use_anisotropic_filter);
}
}
else if(m_prop.visual == "upright_sprite")
{
} else if (m_prop.visual == "upright_sprite") {
scene::IMesh *mesh = m_meshnode->getMesh();
{
std::string tname = "unknown_object.png";
if(m_prop.textures.size() >= 1)
if (!m_prop.textures.empty())
tname = m_prop.textures[0];
tname += mod;
scene::IMeshBuffer *buf = mesh->getMeshBuffer(0);
Expand All @@ -1367,8 +1353,7 @@ void GenericCAO::updateTextures(std::string mod)
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
// has directional lighting, it should work automatically.
if(m_prop.colors.size() >= 1)
{
if(!m_prop.colors.empty()) {
buf->getMaterial().AmbientColor = m_prop.colors[0];
buf->getMaterial().DiffuseColor = m_prop.colors[0];
buf->getMaterial().SpecularColor = m_prop.colors[0];
Expand All @@ -1380,9 +1365,9 @@ void GenericCAO::updateTextures(std::string mod)
}
{
std::string tname = "unknown_object.png";
if(m_prop.textures.size() >= 2)
if (m_prop.textures.size() >= 2)
tname = m_prop.textures[1];
else if(m_prop.textures.size() >= 1)
else if (!m_prop.textures.empty())
tname = m_prop.textures[0];
tname += mod;
scene::IMeshBuffer *buf = mesh->getMeshBuffer(1);
Expand All @@ -1392,14 +1377,11 @@ void GenericCAO::updateTextures(std::string mod)
// This allows setting per-material colors. However, until a real lighting
// system is added, the code below will have no effect. Once MineTest
// has directional lighting, it should work automatically.
if(m_prop.colors.size() >= 2)
{
if (m_prop.colors.size() >= 2) {
buf->getMaterial().AmbientColor = m_prop.colors[1];
buf->getMaterial().DiffuseColor = m_prop.colors[1];
buf->getMaterial().SpecularColor = m_prop.colors[1];
}
else if(m_prop.colors.size() >= 1)
{
} else if (!m_prop.colors.empty()) {
buf->getMaterial().AmbientColor = m_prop.colors[0];
buf->getMaterial().DiffuseColor = m_prop.colors[0];
buf->getMaterial().SpecularColor = m_prop.colors[0];
Expand Down Expand Up @@ -1476,7 +1458,7 @@ void GenericCAO::updateAttachments()
scene::ISceneNode *parent_node = getParent()->getSceneNode();
scene::IAnimatedMeshSceneNode *parent_animated_mesh_node =
getParent()->getAnimatedMeshSceneNode();
if (parent_animated_mesh_node && m_attachment_bone != "") {
if (parent_animated_mesh_node && !m_attachment_bone.empty()) {
parent_node = parent_animated_mesh_node->getJointNode(m_attachment_bone.c_str());
}

Expand Down Expand Up @@ -1519,7 +1501,7 @@ void GenericCAO::processMessage(const std::string &data)
player->setCollisionbox(m_selection_box);
}

if ((m_is_player && !m_is_local_player) && m_prop.nametag == "")
if ((m_is_player && !m_is_local_player) && m_prop.nametag.empty())
m_prop.nametag = m_name;

expireVisuals();
Expand Down
4 changes: 2 additions & 2 deletions src/content_cao.h
Expand Up @@ -40,11 +40,11 @@ struct SmoothTranslator
v3f vect_show;
v3f vect_aim;
f32 anim_counter = 0;
f32 anim_time;
f32 anim_time = 0;
f32 anim_time_counter = 0;
bool aim_is_end = true;

SmoothTranslator() {};
SmoothTranslator() = default;

void init(v3f vect);

Expand Down
2 changes: 1 addition & 1 deletion src/content_cso.cpp
Expand Up @@ -30,7 +30,7 @@ class SmokePuffCSO: public ClientSimpleObject
scene::IBillboardSceneNode *m_spritenode = nullptr;
public:
SmokePuffCSO(scene::ISceneManager *smgr,
ClientEnvironment *env, v3f pos, v2f size)
ClientEnvironment *env, const v3f &pos, const v2f &size)
{
infostream<<"SmokePuffCSO: constructing"<<std::endl;
m_spritenode = smgr->addBillboardSceneNode(
Expand Down

0 comments on commit 13e995b

Please sign in to comment.