From 1992db1395d9c068327a7c08bac7a24ef7112274 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lo=C3=AFc=20Blot?= Date: Sat, 19 Aug 2017 11:30:46 +0200 Subject: [PATCH] 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 --- src/metadata.cpp | 3 +- src/nameidmapping.cpp | 7 +- src/nodedef.cpp | 222 +++++++++++++++++--------------------- src/nodedef.h | 15 +-- src/nodemetadata.cpp | 27 ++--- src/nodetimer.cpp | 8 +- src/nodetimer.h | 12 +-- src/noise.cpp | 20 ++-- src/noise.h | 5 +- src/objdef.h | 2 +- src/object_properties.cpp | 21 ++-- src/pathfinder.cpp | 152 ++++++++++---------------- 12 files changed, 207 insertions(+), 287 deletions(-) diff --git a/src/metadata.cpp b/src/metadata.cpp index 628e38c15..6ad65e007 100644 --- a/src/metadata.cpp +++ b/src/metadata.cpp @@ -50,8 +50,7 @@ bool Metadata::operator==(const Metadata &other) const return false; for (const auto &sv : m_stringvars) { - if (!other.contains(sv.first) || - other.getString(sv.first) != sv.second) + if (!other.contains(sv.first) || other.getString(sv.first) != sv.second) return false; } diff --git a/src/nameidmapping.cpp b/src/nameidmapping.cpp index 78e4a0a56..bd5bb1fc8 100644 --- a/src/nameidmapping.cpp +++ b/src/nameidmapping.cpp @@ -25,10 +25,9 @@ void NameIdMapping::serialize(std::ostream &os) const { writeU8(os, 0); // version writeU16(os, m_id_to_name.size()); - for (IdToNameMap::const_iterator i = m_id_to_name.begin(); - i != m_id_to_name.end(); ++i) { - writeU16(os, i->first); - os << serializeString(i->second); + for (const auto &i : m_id_to_name) { + writeU16(os, i.first); + os << serializeString(i.second); } } 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::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::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::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 &boxes, aabb3f *box_union) { - for (std::vector::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::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 all; idef->getAll(all); m_name_id_mapping_with_aliases.clear(); - for (std::set::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 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::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::iterator it = f->connects_to.begin(); - it != f->connects_to.end(); ++it) { - getIds(*it, f->connects_to_ids); + + for (std::vector::const_iterator it = f.connects_to.begin(); + it != f.connects_to.end(); ++it) { + getIds(*it, f.connects_to_ids); } } } diff --git a/src/nodedef.h b/src/nodedef.h index f3fb834bc..22ed2f127 100644 --- a/src/nodedef.h +++ b/src/nodedef.h @@ -137,7 +137,7 @@ public: bool enable_mesh_cache; bool enable_minimap; - TextureSettings() {} + TextureSettings() = default; void readSettings(); }; @@ -369,7 +369,7 @@ struct ContentFeatures */ ContentFeatures(); - ~ContentFeatures(); + ~ContentFeatures() = default; void reset(); void serialize(std::ostream &os, u16 protocol_version) const; void deSerialize(std::istream &is); @@ -411,8 +411,9 @@ struct ContentFeatures class INodeDefManager { public: - INodeDefManager(){} - virtual ~INodeDefManager(){} + INodeDefManager() = default; + virtual ~INodeDefManager() = default; + // Get node definition virtual const ContentFeatures &get(content_t c) const=0; virtual const ContentFeatures &get(const MapNode &n) const=0; @@ -438,9 +439,9 @@ public: class IWritableNodeDefManager : public INodeDefManager { public: - IWritableNodeDefManager(){} - virtual ~IWritableNodeDefManager(){} - virtual IWritableNodeDefManager* clone()=0; + IWritableNodeDefManager() = default; + virtual ~IWritableNodeDefManager() = default; + // Get node definition virtual const ContentFeatures &get(content_t c) const=0; virtual const ContentFeatures &get(const MapNode &n) const=0; diff --git a/src/nodemetadata.cpp b/src/nodemetadata.cpp index 0e8195c34..708a08099 100644 --- a/src/nodemetadata.cpp +++ b/src/nodemetadata.cpp @@ -44,15 +44,13 @@ void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const { int num_vars = disk ? m_stringvars.size() : countNonPrivate(); writeU32(os, num_vars); - for (StringMap::const_iterator - it = m_stringvars.begin(); - it != m_stringvars.end(); ++it) { - bool priv = isPrivate(it->first); + for (const auto &sv : m_stringvars) { + bool priv = isPrivate(sv.first); if (!disk && priv) continue; - os << serializeString(it->first); - os << serializeLongString(it->second); + os << serializeString(sv.first); + os << serializeLongString(sv.second); if (version >= 2) writeU8(os, (priv) ? 1 : 0); } @@ -86,7 +84,7 @@ void NodeMetadata::clear() bool NodeMetadata::empty() const { - return Metadata::empty() && m_inventory->getLists().size() == 0; + return Metadata::empty() && m_inventory->getLists().empty(); } @@ -103,10 +101,8 @@ int NodeMetadata::countNonPrivate() const // m_privatevars can contain names not actually present // DON'T: return m_stringvars.size() - m_privatevars.size(); int n = 0; - for (StringMap::const_iterator - it = m_stringvars.begin(); - it != m_stringvars.end(); ++it) { - if (isPrivate(it->first) == false) + for (const auto &sv : m_stringvars) { + if (!isPrivate(sv.first)) n++; } return n; @@ -132,12 +128,9 @@ void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk) const writeU8(os, version); writeU16(os, count); - for(std::map::const_iterator - i = m_data.begin(); - i != m_data.end(); ++i) - { - v3s16 p = i->first; - NodeMetadata *data = i->second; + for (const auto &it : m_data) { + v3s16 p = it.first; + NodeMetadata *data = it.second; if (data->empty()) continue; diff --git a/src/nodetimer.cpp b/src/nodetimer.cpp index 003d08782..ec8611a01 100644 --- a/src/nodetimer.cpp +++ b/src/nodetimer.cpp @@ -60,12 +60,10 @@ void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const writeU16(os, m_timers.size()); } - for (std::multimap::const_iterator - i = m_timers.begin(); - i != m_timers.end(); ++i) { - NodeTimer t = i->second; + for (const auto &timer : m_timers) { + NodeTimer t = timer.second; NodeTimer nt = NodeTimer(t.timeout, - t.timeout - (f32)(i->first - m_time), t.position); + t.timeout - (f32)(timer.first - m_time), t.position); v3s16 p = t.position; u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X; diff --git a/src/nodetimer.h b/src/nodetimer.h index 36eabafda..40e54ccc5 100644 --- a/src/nodetimer.h +++ b/src/nodetimer.h @@ -35,12 +35,12 @@ with this program; if not, write to the Free Software Foundation, Inc., class NodeTimer { public: - NodeTimer() {} + NodeTimer() = default; NodeTimer(const v3s16 &position_): position(position_) {} NodeTimer(f32 timeout_, f32 elapsed_, v3s16 position_): timeout(timeout_), elapsed(elapsed_), position(position_) {} - ~NodeTimer() {} + ~NodeTimer() = default; void serialize(std::ostream &os) const; void deSerialize(std::istream &is); @@ -57,8 +57,8 @@ public: class NodeTimerList { public: - NodeTimerList() {} - ~NodeTimerList() {} + NodeTimerList() = default; + ~NodeTimerList() = default; void serialize(std::ostream &os, u8 map_format_version) const; void deSerialize(std::istream &is, u8 map_format_version); @@ -117,10 +117,6 @@ public: m_next_trigger_time = -1.; } - inline double getNextTriggerTime() { - return m_next_trigger_time; - } - // Move forward in time, returns elapsed timers std::vector step(float dtime); diff --git a/src/noise.cpp b/src/noise.cpp index e6ca8a495..8b84803b3 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -23,10 +23,10 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include +#include #include "noise.h" #include -#include // memset +#include // memset #include "debug.h" #include "util/numeric.h" #include "util/string.h" @@ -262,8 +262,8 @@ float noise2d_gradient(float x, float y, s32 seed, bool eased) // Interpolate if (eased) return biLinearInterpolation(v00, v10, v01, v11, xl, yl); - else - return biLinearInterpolationNoEase(v00, v10, v01, v11, xl, yl); + + return biLinearInterpolationNoEase(v00, v10, v01, v11, xl, yl); } @@ -292,12 +292,12 @@ float noise3d_gradient(float x, float y, float z, s32 seed, bool eased) v000, v100, v010, v110, v001, v101, v011, v111, xl, yl, zl); - } else { - return triLinearInterpolationNoEase( - v000, v100, v010, v110, - v001, v101, v011, v111, - xl, yl, zl); } + + return triLinearInterpolationNoEase( + v000, v100, v010, v110, + v001, v101, v011, v111, + xl, yl, zl); } @@ -778,7 +778,7 @@ float *Noise::perlinMap3D(float x, float y, float z, float *persistence_map) void Noise::updateResults(float g, float *gmap, - float *persistence_map, size_t bufsize) + const float *persistence_map, size_t bufsize) { // This looks very ugly, but it is 50-70% faster than having // conditional statements inside the loop diff --git a/src/noise.h b/src/noise.h index ae97b8740..7b5e83251 100644 --- a/src/noise.h +++ b/src/noise.h @@ -112,7 +112,7 @@ struct NoiseParams { NoiseParams() = default; - NoiseParams(float offset_, float scale_, v3f spread_, s32 seed_, + NoiseParams(float offset_, float scale_, const v3f &spread_, s32 seed_, u16 octaves_, float persist_, float lacunarity_, u32 flags_=NOISE_FLAG_DEFAULTS) { @@ -180,7 +180,8 @@ public: private: void allocBuffers(); void resizeNoiseBuf(bool is3d); - void updateResults(float g, float *gmap, float *persistence_map, size_t bufsize); + void updateResults(float g, float *gmap, const float *persistence_map, + size_t bufsize); }; diff --git a/src/objdef.h b/src/objdef.h index 04f067975..b06d4a85e 100644 --- a/src/objdef.h +++ b/src/objdef.h @@ -43,7 +43,7 @@ enum ObjDefType { class ObjDef { public: - virtual ~ObjDef() {} + virtual ~ObjDef() = default; u32 index; u32 uid; diff --git a/src/object_properties.cpp b/src/object_properties.cpp index af442806b..5534db283 100644 --- a/src/object_properties.cpp +++ b/src/object_properties.cpp @@ -26,8 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., ObjectProperties::ObjectProperties() { - textures.push_back("unknown_object.png"); - colors.push_back(video::SColor(255,255,255,255)); + textures.emplace_back("unknown_object.png"); + colors.emplace_back(255,255,255,255); } std::string ObjectProperties::dump() @@ -42,13 +42,14 @@ std::string ObjectProperties::dump() os<<", mesh="< &directions, PathGridnode & DEBUG_OUT("Pathfinder: remaining dirs at beginning:" << directions.size() << std::endl); - for (std::vector::iterator iter = directions.begin(); - iter != directions.end(); - ++iter) { + for (v3s16 &direction : directions) { - v3s16 pos1 = v3s16(srcpos.X + iter->X, 0, srcpos.Z+iter->Z); + v3s16 pos1 = v3s16(srcpos.X + direction.X, 0, srcpos.Z+ direction.Z); int cur_manhattan = getXZManhattanDist(pos1); - PathCost cost = g_pos.getCost(*iter); + PathCost cost = g_pos.getCost(direction); if (!cost.updated) { - cost = calcCost(g_pos.pos, *iter); - g_pos.setCost(*iter, cost); + cost = calcCost(g_pos.pos, direction); + g_pos.setCost(direction, cost); } if (cost.valid) { @@ -1073,7 +1031,7 @@ v3s16 Pathfinder::getDirHeuristic(std::vector &directions, PathGridnode & if ((minscore < 0)|| (score < minscore)) { minscore = score; - retdir = *iter; + retdir = direction; } } } @@ -1123,10 +1081,10 @@ bool Pathfinder::updateCostHeuristic( v3s16 ipos, std::vector directions; - directions.push_back(v3s16( 1, 0, 0)); - directions.push_back(v3s16(-1, 0, 0)); - directions.push_back(v3s16( 0, 0, 1)); - directions.push_back(v3s16( 0, 0, -1)); + directions.emplace_back(1, 0, 0); + directions.emplace_back(-1, 0, 0); + directions.emplace_back(0, 0, 1); + directions.emplace_back(0, 0, -1); v3s16 direction = getDirHeuristic(directions, g_pos); -- 2.25.1