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
This commit is contained in:
Loïc Blot 2017-08-19 11:30:46 +02:00 committed by GitHub
parent 4a1265ceb5
commit 1992db1395
12 changed files with 207 additions and 287 deletions

@ -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;
}

@ -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);
}
}

@ -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);
}
}
}

@ -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;

@ -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<v3s16, NodeMetadata*>::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;

@ -60,12 +60,10 @@ void NodeTimerList::serialize(std::ostream &os, u8 map_format_version) const
writeU16(os, m_timers.size());
}
for (std::multimap<double, NodeTimer>::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;

@ -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<NodeTimer> step(float dtime);

@ -23,10 +23,10 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <math.h>
#include <cmath>
#include "noise.h"
#include <iostream>
#include <string.h> // memset
#include <cstring> // memset
#include "debug.h"
#include "util/numeric.h"
#include "util/string.h"
@ -262,7 +262,7 @@ 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);
}
@ -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);
}
}
@ -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

@ -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);
};

@ -43,7 +43,7 @@ enum ObjDefType {
class ObjDef {
public:
virtual ~ObjDef() {}
virtual ~ObjDef() = default;
u32 index;
u32 uid;

@ -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="<<mesh;
os<<", visual_size="<<PP2(visual_size);
os<<", textures=[";
for(u32 i=0; i<textures.size(); i++){
os<<"\""<<textures[i]<<"\" ";
for (const std::string &texture : textures) {
os<<"\""<< texture <<"\" ";
}
os<<"]";
os<<", colors=[";
for(u32 i=0; i<colors.size(); i++){
os<<"\""<<colors[i].getAlpha()<<","<<colors[i].getRed()<<","<<colors[i].getGreen()<<","<<colors[i].getBlue()<<"\" ";
for (const video::SColor &color : colors) {
os << "\"" << color.getAlpha() << "," << color.getRed() << ","
<< color.getGreen() << "," << color.getBlue() << "\" ";
}
os<<"]";
os<<", spritediv="<<PP2(spritediv);
@ -74,8 +75,8 @@ void ObjectProperties::serialize(std::ostream &os) const
os<<serializeString(visual);
writeV2F1000(os, visual_size);
writeU16(os, textures.size());
for(u32 i=0; i<textures.size(); i++){
os<<serializeString(textures[i]);
for (const std::string &texture : textures) {
os << serializeString(texture);
}
writeV2S16(os, spritediv);
writeV2S16(os, initial_sprite_basepos);
@ -85,8 +86,8 @@ void ObjectProperties::serialize(std::ostream &os) const
// Added in protocol version 14
os<<serializeString(mesh);
writeU16(os, colors.size());
for(u32 i=0; i<colors.size(); i++){
writeARGB8(os, colors[i]);
for (video::SColor color : colors) {
writeARGB8(os, color);
}
writeU8(os, collideWithObjects);
writeF1000(os,stepheight);

@ -68,7 +68,7 @@ class PathCost {
public:
/** default constructor */
PathCost();
PathCost() = default;
/** copy constructor */
PathCost(const PathCost &b);
@ -76,10 +76,10 @@ public:
/** assignment operator */
PathCost &operator= (const PathCost &b);
bool valid; /**< movement is possible */
int value; /**< cost of movement */
int direction; /**< y-direction of movement */
bool updated; /**< this cost has ben calculated */
bool valid = false; /**< movement is possible */
int value = 0; /**< cost of movement */
int direction = 0; /**< y-direction of movement */
bool updated = false; /**< this cost has ben calculated */
};
@ -89,7 +89,7 @@ class PathGridnode {
public:
/** default constructor */
PathGridnode();
PathGridnode() = default;
/** copy constructor */
PathGridnode(const PathGridnode &b);
@ -113,17 +113,17 @@ public:
*/
void setCost(v3s16 dir, const PathCost &cost);
bool valid; /**< node is on surface */
bool target; /**< node is target position */
bool source; /**< node is stating position */
int totalcost; /**< cost to move here from starting point */
bool valid = false; /**< node is on surface */
bool target = false; /**< node is target position */
bool source = false; /**< node is stating position */
int totalcost = -1; /**< cost to move here from starting point */
v3s16 sourcedir; /**< origin of movement for current cost */
v3s16 pos; /**< real position of node */
PathCost directions[4]; /**< cost in different directions */
/* debug values */
bool is_element; /**< node is element of path detected */
char type; /**< type of node */
bool is_element = false; /**< node is element of path detected */
char type = 'u'; /**< type of node */
};
class Pathfinder;
@ -132,7 +132,8 @@ class Pathfinder;
class GridNodeContainer {
public:
virtual PathGridnode &access(v3s16 p)=0;
virtual ~GridNodeContainer() {}
virtual ~GridNodeContainer() = default;
protected:
Pathfinder *m_pathf;
@ -141,7 +142,8 @@ protected:
class ArrayGridNodeContainer : public GridNodeContainer {
public:
virtual ~ArrayGridNodeContainer() {}
virtual ~ArrayGridNodeContainer() = default;
ArrayGridNodeContainer(Pathfinder *pathf, v3s16 dimensions);
virtual PathGridnode &access(v3s16 p);
private:
@ -154,7 +156,8 @@ private:
class MapGridNodeContainer : public GridNodeContainer {
public:
virtual ~MapGridNodeContainer() {}
virtual ~MapGridNodeContainer() = default;
MapGridNodeContainer(Pathfinder *pathf);
virtual PathGridnode &access(v3s16 p);
private:
@ -168,7 +171,7 @@ public:
/**
* default constructor
*/
Pathfinder();
Pathfinder() = default;
~Pathfinder();
@ -281,7 +284,7 @@ private:
* @param level current recursion depth
* @return true/false path to destination has been found
*/
bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int total_cost, int level);
bool updateAllCosts(v3s16 ipos, v3s16 srcdir, int current_cost, int level);
/**
* recursive try to find a patrh to destionation
@ -302,17 +305,17 @@ private:
void buildPath(std::vector<v3s16> &path, v3s16 pos, int level);
/* variables */
int m_max_index_x; /**< max index of search area in x direction */
int m_max_index_y; /**< max index of search area in y direction */
int m_max_index_z; /**< max index of search area in z direction */
int m_max_index_x = 0; /**< max index of search area in x direction */
int m_max_index_y = 0; /**< max index of search area in y direction */
int m_max_index_z = 0; /**< max index of search area in z direction */
int m_searchdistance; /**< max distance to search in each direction */
int m_maxdrop; /**< maximum number of blocks a path may drop */
int m_maxjump; /**< maximum number of blocks a path may jump */
int m_min_target_distance; /**< current smalest path to target */
int m_searchdistance = 0; /**< max distance to search in each direction */
int m_maxdrop = 0; /**< maximum number of blocks a path may drop */
int m_maxjump = 0; /**< maximum number of blocks a path may jump */
int m_min_target_distance = 0; /**< current smalest path to target */
bool m_prefetch; /**< prefetch cost data */
bool m_prefetch = true; /**< prefetch cost data */
v3s16 m_start; /**< source position */
v3s16 m_destination; /**< destination position */
@ -322,9 +325,9 @@ private:
/** contains all map data already collected and analyzed.
Access it via the getIndexElement/getIdxElem methods. */
friend class GridNodeContainer;
GridNodeContainer *m_nodes_container;
GridNodeContainer *m_nodes_container = nullptr;
ServerEnvironment *m_env; /**< minetest environment pointer */
ServerEnvironment *m_env = 0; /**< minetest environment pointer */
#ifdef PATHFINDER_DEBUG
@ -394,16 +397,6 @@ std::vector<v3s16> get_path(ServerEnvironment* env,
searchdistance, max_jump, max_drop, algo);
}
/******************************************************************************/
PathCost::PathCost()
: valid(false),
value(0),
direction(0),
updated(false)
{
//intentionaly empty
}
/******************************************************************************/
PathCost::PathCost(const PathCost &b)
{
@ -424,20 +417,6 @@ PathCost &PathCost::operator= (const PathCost &b)
return *this;
}
/******************************************************************************/
PathGridnode::PathGridnode()
: valid(false),
target(false),
source(false),
totalcost(-1),
sourcedir(v3s16(0, 0, 0)),
pos(v3s16(0, 0, 0)),
is_element(false),
type('u')
{
//intentionaly empty
}
/******************************************************************************/
PathGridnode::PathGridnode(const PathGridnode &b)
: valid(b.valid),
@ -728,9 +707,8 @@ std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
//finalize path
std::vector<v3s16> full_path;
for (std::vector<v3s16>::iterator i = path.begin();
i != path.end(); ++i) {
full_path.push_back(getIndexElement(*i).pos);
for (const v3s16 &i : path) {
full_path.push_back(getIndexElement(i).pos);
}
#ifdef PATHFINDER_DEBUG
@ -763,24 +741,6 @@ std::vector<v3s16> Pathfinder::getPath(ServerEnvironment *env,
return retval;
}
/******************************************************************************/
Pathfinder::Pathfinder() :
m_max_index_x(0),
m_max_index_y(0),
m_max_index_z(0),
m_searchdistance(0),
m_maxdrop(0),
m_maxjump(0),
m_min_target_distance(0),
m_prefetch(true),
m_start(0, 0, 0),
m_destination(0, 0, 0),
m_nodes_container(NULL),
m_env(0)
{
//intentionaly empty
}
Pathfinder::~Pathfinder()
{
delete m_nodes_container;
@ -970,19 +930,19 @@ bool Pathfinder::updateAllCosts(v3s16 ipos,
std::vector<v3s16> 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);
for (unsigned int i=0; i < directions.size(); i++) {
if (directions[i] != srcdir) {
PathCost cost = g_pos.getCost(directions[i]);
for (v3s16 &direction : directions) {
if (direction != srcdir) {
PathCost cost = g_pos.getCost(direction);
if (cost.valid) {
directions[i].Y = cost.direction;
direction.Y = cost.direction;
v3s16 ipos2 = ipos + directions[i];
v3s16 ipos2 = ipos + direction;
if (!isValidIndex(ipos2)) {
DEBUG_OUT(LVL " Pathfinder: " << PP(ipos2) <<
@ -1013,7 +973,7 @@ bool Pathfinder::updateAllCosts(v3s16 ipos,
DEBUG_OUT(LVL "Pathfinder: updating path at: "<<
PP(ipos2) << " from: " << g_pos2.totalcost << " to "<<
new_cost << std::endl);
if (updateAllCosts(ipos2, invert(directions[i]),
if (updateAllCosts(ipos2, invert(direction),
new_cost, level)) {
retval = true;
}
@ -1054,18 +1014,16 @@ v3s16 Pathfinder::getDirHeuristic(std::vector<v3s16> &directions, PathGridnode &
DEBUG_OUT("Pathfinder: remaining dirs at beginning:"
<< directions.size() << std::endl);
for (std::vector<v3s16>::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<v3s16> &directions, PathGridnode &
if ((minscore < 0)|| (score < minscore)) {
minscore = score;
retdir = *iter;
retdir = direction;
}
}
}
@ -1123,10 +1081,10 @@ bool Pathfinder::updateCostHeuristic( v3s16 ipos,
std::vector<v3s16> 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);