summaryrefslogtreecommitdiff
path: root/src/nodedef.cpp
diff options
context:
space:
mode:
authorLoïc Blot <nerzhul@users.noreply.github.com>2017-08-19 11:30:46 +0200
committerGitHub <noreply@github.com>2017-08-19 11:30:46 +0200
commit1992db1395d9c068327a7c08bac7a24ef7112274 (patch)
tree3fb4982e4662a9519e08b57cab228c6b26d4a57f /src/nodedef.cpp
parent4a1265ceb5919c53efaa8c50bbb0fe7955aa47aa (diff)
downloadminetest-1992db1395d9c068327a7c08bac7a24ef7112274.tar.gz
minetest-1992db1395d9c068327a7c08bac7a24ef7112274.tar.bz2
minetest-1992db1395d9c068327a7c08bac7a24ef7112274.zip
Code modernization: src/n*, src/o* (#6280)
* Code modernization: src/n*, src/o* * empty function * default constructor/destructor * for range-based loops * use emplace_back instead of push_back * remove unused IWritableNodeDefManager::clone() * C++ STL header style * Pointer constness in some functions
Diffstat (limited to 'src/nodedef.cpp')
-rw-r--r--src/nodedef.cpp222
1 files changed, 98 insertions, 124 deletions
diff --git a/src/nodedef.cpp b/src/nodedef.cpp
index 651418cd8..21522955e 100644
--- a/src/nodedef.cpp
+++ b/src/nodedef.cpp
@@ -78,12 +78,9 @@ void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, type);
writeU16(os, fixed.size());
- for (std::vector<aabb3f>::const_iterator
- i = fixed.begin();
- i != fixed.end(); ++i)
- {
- writeV3F1000(os, i->MinEdge);
- writeV3F1000(os, i->MaxEdge);
+ for (const aabb3f &nodebox : fixed) {
+ writeV3F1000(os, nodebox.MinEdge);
+ writeV3F1000(os, nodebox.MaxEdge);
}
break;
case NODEBOX_WALLMOUNTED:
@@ -108,14 +105,12 @@ void NodeBox::serialize(std::ostream &os, u16 protocol_version) const
} else {
writeU8(os, type);
-#define WRITEBOX(box) do { \
+#define WRITEBOX(box) \
writeU16(os, (box).size()); \
- for (std::vector<aabb3f>::const_iterator \
- i = (box).begin(); \
- i != (box).end(); ++i) { \
- writeV3F1000(os, i->MinEdge); \
- writeV3F1000(os, i->MaxEdge); \
- }; } while (0)
+ for (const aabb3f &i: (box)) { \
+ writeV3F1000(os, i.MinEdge); \
+ writeV3F1000(os, i.MaxEdge); \
+ };
WRITEBOX(fixed);
WRITEBOX(connect_top);
@@ -170,7 +165,7 @@ void NodeBox::deSerialize(std::istream &is)
while (count--) { \
v3f min = readV3F1000(is); \
v3f max = readV3F1000(is); \
- (box).push_back(aabb3f(min, max)); }; } while (0)
+ (box).emplace_back(min, max); }; } while (0)
u16 count;
@@ -303,10 +298,6 @@ ContentFeatures::ContentFeatures()
reset();
}
-ContentFeatures::~ContentFeatures()
-{
-}
-
void ContentFeatures::reset()
{
/*
@@ -334,15 +325,15 @@ void ContentFeatures::reset()
drawtype = NDT_NORMAL;
mesh = "";
#ifndef SERVER
- for(u32 i = 0; i < 24; i++)
- mesh_ptr[i] = NULL;
+ for (auto &i : mesh_ptr)
+ i = NULL;
minimap_color = video::SColor(0, 0, 0, 0);
#endif
visual_scale = 1.0;
- for(u32 i = 0; i < 6; i++)
- tiledef[i] = TileDef();
- for(u16 j = 0; j < CF_SPECIAL_COUNT; j++)
- tiledef_special[j] = TileDef();
+ for (auto &i : tiledef)
+ i = TileDef();
+ for (auto &j : tiledef_special)
+ j = TileDef();
alpha = 255;
post_effect_color = video::SColor(0, 0, 0, 0);
param_type = CPT_NONE;
@@ -398,10 +389,9 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
// general
os << serializeString(name);
writeU16(os, groups.size());
- for (ItemGroupList::const_iterator i = groups.begin(); i != groups.end();
- ++i) {
- os << serializeString(i->first);
- writeS16(os, i->second);
+ for (const auto &group : groups) {
+ os << serializeString(group.first);
+ writeS16(os, group.second);
}
writeU8(os, param_type);
writeU8(os, param_type_2);
@@ -411,13 +401,13 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
os << serializeString(mesh);
writeF1000(os, visual_scale);
writeU8(os, 6);
- for (u32 i = 0; i < 6; i++)
- tiledef[i].serialize(os, protocol_version);
- for (u32 i = 0; i < 6; i++)
- tiledef_overlay[i].serialize(os, protocol_version);
+ for (const TileDef &td : tiledef)
+ td.serialize(os, protocol_version);
+ for (const TileDef &td : tiledef_overlay)
+ td.serialize(os, protocol_version);
writeU8(os, CF_SPECIAL_COUNT);
- for (u32 i = 0; i < CF_SPECIAL_COUNT; i++) {
- tiledef_special[i].serialize(os, protocol_version);
+ for (const TileDef &td : tiledef_special) {
+ td.serialize(os, protocol_version);
}
writeU8(os, alpha);
writeU8(os, color.getRed());
@@ -427,9 +417,8 @@ void ContentFeatures::serialize(std::ostream &os, u16 protocol_version) const
writeU8(os, waving);
writeU8(os, connect_sides);
writeU16(os, connects_to_ids.size());
- for (std::set<content_t>::const_iterator i = connects_to_ids.begin();
- i != connects_to_ids.end(); ++i)
- writeU16(os, *i);
+ for (u16 connects_to_id : connects_to_ids)
+ writeU16(os, connects_to_id);
writeU8(os, post_effect_color.getAlpha());
writeU8(os, post_effect_color.getRed());
writeU8(os, post_effect_color.getGreen());
@@ -485,7 +474,7 @@ void ContentFeatures::correctAlpha(TileDef *tiles, int length)
return;
for (int i = 0; i < length; i++) {
- if (tiles[i].name == "")
+ if (tiles[i].name.empty())
continue;
std::stringstream s;
s << tiles[i].name << "^[noalpha^[opacity:" << ((int)alpha);
@@ -500,7 +489,9 @@ void ContentFeatures::deSerialize(std::istream &is)
if (version < 9) {
deSerializeOld(is, version);
return;
- } else if (version > 11) {
+ }
+
+ if (version > 11) {
throw SerializationError("unsupported ContentFeatures version");
}
@@ -522,15 +513,15 @@ void ContentFeatures::deSerialize(std::istream &is)
visual_scale = readF1000(is);
if (readU8(is) != 6)
throw SerializationError("unsupported tile count");
- for (u32 i = 0; i < 6; i++)
- tiledef[i].deSerialize(is, version, drawtype);
+ for (TileDef &td : tiledef)
+ td.deSerialize(is, version, drawtype);
if (version >= 10)
- for (u32 i = 0; i < 6; i++)
- tiledef_overlay[i].deSerialize(is, version, drawtype);
+ for (TileDef &td : tiledef_overlay)
+ td.deSerialize(is, version, drawtype);
if (readU8(is) != CF_SPECIAL_COUNT)
throw SerializationError("unsupported CF_SPECIAL_COUNT");
- for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
- tiledef_special[i].deSerialize(is, version, drawtype);
+ for (TileDef &td : tiledef_special)
+ td.deSerialize(is, version, drawtype);
alpha = readU8(is);
color.setRed(readU8(is));
color.setGreen(readU8(is));
@@ -667,14 +658,14 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings)
{
// minimap pixel color - the average color of a texture
- if (tsettings.enable_minimap && tiledef[0].name != "")
+ if (tsettings.enable_minimap && !tiledef[0].name.empty())
minimap_color = tsrc->getTextureAverageColor(tiledef[0].name);
// Figure out the actual tiles to use
TileDef tdef[6];
for (u32 j = 0; j < 6; j++) {
tdef[j] = tiledef[j];
- if (tdef[j].name == "")
+ if (tdef[j].name.empty())
tdef[j].name = "unknown_node.png";
}
// also the overlay tiles
@@ -739,7 +730,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
visual_solidness = 1;
} else if (tsettings.leaves_style == LEAVES_SIMPLE) {
for (u32 j = 0; j < 6; j++) {
- if (tdef_spec[j].name != "")
+ if (!tdef_spec[j].name.empty())
tdef[j].name = tdef_spec[j].name;
}
drawtype = NDT_GLASSLIKE;
@@ -748,8 +739,8 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
} else {
drawtype = NDT_NORMAL;
solidness = 2;
- for (u32 i = 0; i < 6; i++)
- tdef[i].name += std::string("^[noalpha");
+ for (TileDef &td : tdef)
+ td.name += std::string("^[noalpha");
}
if (waving >= 1)
material_type = TILE_MATERIAL_WAVING_LEAVES;
@@ -805,7 +796,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
fillTileAttribs(tsrc, &tiles[j].layers[0], &tdef[j], tile_shader,
tsettings.use_normal_texture,
tdef[j].backface_culling, material_type);
- if (tdef_overlay[j].name != "")
+ if (!tdef_overlay[j].name.empty())
fillTileAttribs(tsrc, &tiles[j].layers[1], &tdef_overlay[j],
overlay_shader, tsettings.use_normal_texture,
tdef[j].backface_culling, overlay_material);
@@ -832,7 +823,7 @@ void ContentFeatures::updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc
param_type_2 == CPT2_COLORED_WALLMOUNTED)
palette = tsrc->getPalette(palette_name);
- if ((drawtype == NDT_MESH) && (mesh != "")) {
+ if (drawtype == NDT_MESH && !mesh.empty()) {
// Meshnode drawtype
// Read the mesh and apply scale
mesh_ptr[0] = client->getMesh(mesh);
@@ -892,7 +883,7 @@ public:
CNodeDefManager();
virtual ~CNodeDefManager();
void clear();
- virtual IWritableNodeDefManager *clone();
+
inline virtual const ContentFeatures& get(content_t c) const;
inline virtual const ContentFeatures& get(const MapNode &n) const;
virtual bool getId(const std::string &name, content_t &result) const;
@@ -977,11 +968,10 @@ CNodeDefManager::CNodeDefManager()
CNodeDefManager::~CNodeDefManager()
{
#ifndef SERVER
- for (u32 i = 0; i < m_content_features.size(); i++) {
- ContentFeatures *f = &m_content_features[i];
- for (u32 j = 0; j < 24; j++) {
- if (f->mesh_ptr[j])
- f->mesh_ptr[j]->drop();
+ for (ContentFeatures &f : m_content_features) {
+ for (auto &j : f.mesh_ptr) {
+ if (j)
+ j->drop();
}
}
#endif
@@ -1057,14 +1047,6 @@ void CNodeDefManager::clear()
}
-IWritableNodeDefManager *CNodeDefManager::clone()
-{
- CNodeDefManager *mgr = new CNodeDefManager();
- *mgr = *this;
- return mgr;
-}
-
-
inline const ContentFeatures& CNodeDefManager::get(content_t c) const
{
return c < m_content_features.size()
@@ -1116,10 +1098,9 @@ bool CNodeDefManager::getIds(const std::string &name,
return true;
const GroupItems &items = i->second;
- for (GroupItems::const_iterator j = items.begin();
- j != items.end(); ++j) {
- if ((*j).second != 0)
- result.insert((*j).first);
+ for (const auto &item : items) {
+ if (item.second != 0)
+ result.insert(item.first);
}
//printf("getIds: %dus\n", t.stop());
return true;
@@ -1141,10 +1122,10 @@ content_t CNodeDefManager::allocateId()
id >= m_next_id; // overflow?
++id) {
while (id >= m_content_features.size()) {
- m_content_features.push_back(ContentFeatures());
+ m_content_features.emplace_back();
}
const ContentFeatures &f = m_content_features[id];
- if (f.name == "") {
+ if (f.name.empty()) {
m_next_id = id + 1;
return id;
}
@@ -1163,9 +1144,8 @@ content_t CNodeDefManager::allocateId()
*/
void boxVectorUnion(const std::vector<aabb3f> &boxes, aabb3f *box_union)
{
- for (std::vector<aabb3f>::const_iterator it = boxes.begin();
- it != boxes.end(); ++it) {
- box_union->addInternalBox(*it);
+ for (const aabb3f &box : boxes) {
+ box_union->addInternalBox(box);
}
}
@@ -1202,9 +1182,9 @@ void getNodeBoxUnion(const NodeBox &nodebox, const ContentFeatures &features,
fabsf(half_processed.MaxEdge.Y),
fabsf(half_processed.MaxEdge.Z) };
f32 max = 0;
- for (int i = 0; i < 6; i++) {
- if (max < coords[i]) {
- max = coords[i];
+ for (float coord : coords) {
+ if (max < coord) {
+ max = coord;
}
}
// Add the union of all possible rotated boxes
@@ -1226,9 +1206,9 @@ void getNodeBoxUnion(const NodeBox &nodebox, const ContentFeatures &features,
fabsf(nodebox.wall_side.MaxEdge.X),
fabsf(nodebox.wall_side.MaxEdge.Z) };
f32 max = 0;
- for (int i = 0; i < 4; i++) {
- if (max < coords[i]) {
- max = coords[i];
+ for (float coord : coords) {
+ if (max < coord) {
+ max = coord;
}
}
// Add the union of all possible rotated boxes
@@ -1308,18 +1288,16 @@ content_t CNodeDefManager::set(const std::string &name, const ContentFeatures &d
// Add this content to the list of all groups it belongs to
// FIXME: This should remove a node from groups it no longer
// belongs to when a node is re-registered
- for (ItemGroupList::const_iterator i = def.groups.begin();
- i != def.groups.end(); ++i) {
- std::string group_name = i->first;
+ for (const auto &group : def.groups) {
+ const std::string &group_name = group.first;
std::unordered_map<std::string, GroupItems>::iterator
j = m_group_to_items.find(group_name);
if (j == m_group_to_items.end()) {
- m_group_to_items[group_name].push_back(
- std::make_pair(id, i->second));
+ m_group_to_items[group_name].emplace_back(id, group.second);
} else {
GroupItems &items = j->second;
- items.push_back(std::make_pair(id, i->second));
+ items.emplace_back(id, group.second);
}
}
return id;
@@ -1360,7 +1338,7 @@ void CNodeDefManager::removeNode(const std::string &name)
}
// Check if group is empty
- if (items.size() == 0)
+ if (items.empty())
m_group_to_items.erase(iter_groups++);
else
++iter_groups;
@@ -1373,9 +1351,7 @@ void CNodeDefManager::updateAliases(IItemDefManager *idef)
std::set<std::string> all;
idef->getAll(all);
m_name_id_mapping_with_aliases.clear();
- for (std::set<std::string>::const_iterator
- i = all.begin(); i != all.end(); ++i) {
- const std::string &name = *i;
+ for (const std::string &name : all) {
const std::string &convert_to = idef->getAlias(name);
content_t id;
if (m_name_id_mapping.getId(convert_to, id)) {
@@ -1395,7 +1371,7 @@ void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath
int line_c = 0;
while (std::getline(infile, line)) {
line_c++;
- if (trim(line) == "")
+ if (trim(line).empty())
continue;
std::vector<std::string> splitted = str_split(line, ' ');
if (splitted.size() != 3) {
@@ -1424,8 +1400,8 @@ void CNodeDefManager::applyTextureOverrides(const std::string &override_filepath
else if (splitted[1] == "front")
nodedef.tiledef[5].name = splitted[2];
else if (splitted[1] == "all" || splitted[1] == "*")
- for (int i = 0; i < 6; i++)
- nodedef.tiledef[i].name = splitted[2];
+ for (TileDef &i : nodedef.tiledef)
+ i.name = splitted[2];
else if (splitted[1] == "sides")
for (int i = 2; i < 6; i++)
nodedef.tiledef[i].name = splitted[2];
@@ -1475,7 +1451,7 @@ void CNodeDefManager::serialize(std::ostream &os, u16 protocol_version) const
|| i == CONTENT_UNKNOWN)
continue;
const ContentFeatures *f = &m_content_features[i];
- if (f->name == "")
+ if (f->name.empty())
continue;
writeU16(os2, i);
// Wrap it in a string to allow different lengths without
@@ -1517,7 +1493,7 @@ void CNodeDefManager::deSerialize(std::istream &is)
"not changing builtin node " << i << std::endl;
continue;
}
- if (f.name == "") {
+ if (f.name.empty()) {
warningstream << "NodeDefManager::deSerialize(): "
"received empty name" << std::endl;
continue;
@@ -1580,7 +1556,7 @@ void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
TileDef compatible_tiles[6];
for (u8 i = 0; i < 6; i++) {
compatible_tiles[i] = tiledef[i];
- if (tiledef_overlay[i].name != "") {
+ if (!tiledef_overlay[i].name.empty()) {
std::stringstream s;
s << "(" << tiledef[i].name << ")^(" << tiledef_overlay[i].name
<< ")";
@@ -1594,19 +1570,18 @@ void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
os << serializeString(name);
writeU16(os, groups.size());
- for (ItemGroupList::const_iterator i = groups.begin();
- i != groups.end(); ++i) {
- os << serializeString(i->first);
- writeS16(os, i->second);
+ for (const auto &group : groups) {
+ os << serializeString(group.first);
+ writeS16(os, group.second);
}
writeU8(os, drawtype);
writeF1000(os, compatible_visual_scale);
writeU8(os, 6);
- for (u32 i = 0; i < 6; i++)
- compatible_tiles[i].serialize(os, protocol_version);
+ for (const auto &compatible_tile : compatible_tiles)
+ compatible_tile.serialize(os, protocol_version);
writeU8(os, CF_SPECIAL_COUNT);
- for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
- tiledef_special[i].serialize(os, protocol_version);
+ for (const TileDef &i : tiledef_special)
+ i.serialize(os, protocol_version);
writeU8(os, alpha);
writeU8(os, post_effect_color.getAlpha());
writeU8(os, post_effect_color.getRed());
@@ -1646,9 +1621,8 @@ void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version) const
collision_box.serialize(os, protocol_version);
writeU8(os, floodable);
writeU16(os, connects_to_ids.size());
- for (std::set<content_t>::const_iterator i = connects_to_ids.begin();
- i != connects_to_ids.end(); ++i)
- writeU16(os, *i);
+ for (content_t connects_to_id : connects_to_ids)
+ writeU16(os, connects_to_id);
writeU8(os, connect_sides);
} else {
throw SerializationError("ContentFeatures::serialize(): "
@@ -1673,12 +1647,12 @@ void ContentFeatures::deSerializeOld(std::istream &is, int version)
visual_scale = readF1000(is);
if (readU8(is) != 6)
throw SerializationError("unsupported tile count");
- for (u32 i = 0; i < 6; i++)
- tiledef[i].deSerialize(is, version, drawtype);
+ for (TileDef &i : tiledef)
+ i.deSerialize(is, version, drawtype);
if (readU8(is) != CF_SPECIAL_COUNT)
throw SerializationError("unsupported CF_SPECIAL_COUNT");
- for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
- tiledef_special[i].deSerialize(is, version, drawtype);
+ for (TileDef &i : tiledef_special)
+ i.deSerialize(is, version, drawtype);
alpha = readU8(is);
post_effect_color.setAlpha(readU8(is));
post_effect_color.setRed(readU8(is));
@@ -1722,8 +1696,8 @@ void ContentFeatures::deSerializeOld(std::istream &is, int version)
visual_scale = readF1000(is);
if (readU8(is) != 6)
throw SerializationError("unsupported tile count");
- for (u32 i = 0; i < 6; i++)
- tiledef[i].deSerialize(is, version, drawtype);
+ for (TileDef &i : tiledef)
+ i.deSerialize(is, version, drawtype);
// CF_SPECIAL_COUNT in version 6 = 2
if (readU8(is) != 2)
throw SerializationError("unsupported CF_SPECIAL_COUNT");
@@ -1777,12 +1751,12 @@ void ContentFeatures::deSerializeOld(std::istream &is, int version)
visual_scale = readF1000(is);
if (readU8(is) != 6)
throw SerializationError("unsupported tile count");
- for (u32 i = 0; i < 6; i++)
- tiledef[i].deSerialize(is, version, drawtype);
+ for (TileDef &i : tiledef)
+ i.deSerialize(is, version, drawtype);
if (readU8(is) != CF_SPECIAL_COUNT)
throw SerializationError("unsupported CF_SPECIAL_COUNT");
- for (u32 i = 0; i < CF_SPECIAL_COUNT; i++)
- tiledef_special[i].deSerialize(is, version, drawtype);
+ for (TileDef &i : tiledef_special)
+ i.deSerialize(is, version, drawtype);
alpha = readU8(is);
post_effect_color.setAlpha(readU8(is));
post_effect_color.setRed(readU8(is));
@@ -1886,13 +1860,13 @@ void CNodeDefManager::resetNodeResolveState()
void CNodeDefManager::mapNodeboxConnections()
{
- for (u32 i = 0; i < m_content_features.size(); i++) {
- ContentFeatures *f = &m_content_features[i];
- if ((f->drawtype != NDT_NODEBOX) || (f->node_box.type != NODEBOX_CONNECTED))
+ for (ContentFeatures &f : m_content_features) {
+ if (f.drawtype != NDT_NODEBOX || f.node_box.type != NODEBOX_CONNECTED)
continue;
- for (std::vector<std::string>::iterator it = f->connects_to.begin();
- it != f->connects_to.end(); ++it) {
- getIds(*it, f->connects_to_ids);
+
+ for (std::vector<std::string>::const_iterator it = f.connects_to.begin();
+ it != f.connects_to.end(); ++it) {
+ getIds(*it, f.connects_to_ids);
}
}
}