1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Modernize various files (part 2)

* range-based for loops
* emplace_back instead of push_back
* code style
* C++ headers instead of C headers
* Default operators
* empty stl function
This commit is contained in:
Loic Blot 2017-08-18 08:07:59 +02:00
parent 55ab4264dc
commit 1d086aee7c
No known key found for this signature in database
GPG key ID: EFAA458E8C153987
10 changed files with 107 additions and 152 deletions

View file

@ -35,11 +35,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
static content_t content_translate_from_19_to_internal(content_t c_from)
{
for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
{
if(trans_table_19[i][1] == c_from)
{
return trans_table_19[i][0];
for (const auto &tt : trans_table_19) {
if(tt[1] == c_from) {
return tt[0];
}
}
return c_from;
@ -116,7 +114,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
NameIdMapping legacy_nimap;
content_mapnode_get_name_id_mapping(&legacy_nimap);
legacy_nimap.getName(material, name);
if(name == "")
if(name.empty())
name = "unknown_block";
if (itemdef)
name = itemdef->getAlias(name);
@ -136,7 +134,7 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
NameIdMapping legacy_nimap;
content_mapnode_get_name_id_mapping(&legacy_nimap);
legacy_nimap.getName(material, name);
if(name == "")
if(name.empty())
name = "unknown_block";
if (itemdef)
name = itemdef->getAlias(name);
@ -207,21 +205,20 @@ void ItemStack::deSerialize(std::istream &is, IItemDefManager *itemdef)
// Read the count
std::string count_str;
std::getline(is, count_str, ' ');
if(count_str.empty())
{
if (count_str.empty()) {
count = 1;
break;
}
else
count = stoi(count_str);
count = stoi(count_str);
// Read the wear
std::string wear_str;
std::getline(is, wear_str, ' ');
if(wear_str.empty())
break;
else
wear = stoi(wear_str);
wear = stoi(wear_str);
// Read metadata
metadata.deSerialize(is);
@ -388,17 +385,12 @@ InventoryList::InventoryList(const std::string &name, u32 size, IItemDefManager
clearItems();
}
InventoryList::~InventoryList()
{
}
void InventoryList::clearItems()
{
m_items.clear();
for(u32 i=0; i<m_size; i++)
{
m_items.push_back(ItemStack());
for (u32 i=0; i < m_size; i++) {
m_items.emplace_back();
}
//setDirty(true);
@ -427,15 +419,10 @@ void InventoryList::serialize(std::ostream &os) const
os<<"Width "<<m_width<<"\n";
for(u32 i=0; i<m_items.size(); i++)
{
const ItemStack &item = m_items[i];
if(item.empty())
{
for (const auto &item : m_items) {
if (item.empty()) {
os<<"Empty";
}
else
{
} else {
os<<"Item ";
item.serialize(os);
}
@ -464,17 +451,16 @@ void InventoryList::deSerialize(std::istream &is)
std::string name;
std::getline(iss, name, ' ');
if(name == "EndInventoryList")
{
if (name == "EndInventoryList") {
break;
}
// This is a temporary backwards compatibility fix
else if(name == "end")
{
if (name == "end") {
break;
}
else if(name == "Width")
{
if (name == "Width") {
iss >> m_width;
if (iss.fail())
throw SerializationError("incorrect width property");
@ -551,9 +537,8 @@ u32 InventoryList::getWidth() const
u32 InventoryList::getUsedSlots() const
{
u32 num = 0;
for(u32 i=0; i<m_items.size(); i++)
{
if(!m_items[i].empty())
for (const auto &m_item : m_items) {
if (!m_item.empty())
num++;
}
return num;
@ -672,20 +657,17 @@ bool InventoryList::roomForItem(const ItemStack &item_) const
bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
{
u32 count = item.count;
if(count == 0)
if (count == 0)
return true;
for(std::vector<ItemStack>::const_reverse_iterator
i = m_items.rbegin();
i != m_items.rend(); ++i)
{
if(count == 0)
for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
if (count == 0)
break;
if (i->name == item.name
&& (!match_meta || (i->metadata == item.metadata))) {
if (i->name == item.name && (!match_meta || (i->metadata == item.metadata))) {
if (i->count >= count)
return true;
else
count -= i->count;
count -= i->count;
}
}
return false;
@ -694,15 +676,11 @@ bool InventoryList::containsItem(const ItemStack &item, bool match_meta) const
ItemStack InventoryList::removeItem(const ItemStack &item)
{
ItemStack removed;
for(std::vector<ItemStack>::reverse_iterator
i = m_items.rbegin();
i != m_items.rend(); ++i)
{
if(i->name == item.name)
{
for (auto i = m_items.rbegin(); i != m_items.rend(); ++i) {
if (i->name == item.name) {
u32 still_to_remove = item.count - removed.count;
removed.addItem(i->takeItem(still_to_remove), m_itemdef);
if(removed.count == item.count)
if (removed.count == item.count)
break;
}
}
@ -815,9 +793,8 @@ Inventory::~Inventory()
void Inventory::clear()
{
m_dirty = true;
for(u32 i=0; i<m_lists.size(); i++)
{
delete m_lists[i];
for (auto &m_list : m_lists) {
delete m_list;
}
m_lists.clear();
}
@ -825,11 +802,8 @@ void Inventory::clear()
void Inventory::clearContents()
{
m_dirty = true;
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for(u32 j=0; j<list->getSize(); j++)
{
for (InventoryList *list : m_lists) {
for (u32 j=0; j<list->getSize(); j++) {
list->deleteItem(j);
}
}
@ -855,9 +829,8 @@ Inventory & Inventory::operator = (const Inventory &other)
m_dirty = true;
clear();
m_itemdef = other.m_itemdef;
for(u32 i=0; i<other.m_lists.size(); i++)
{
m_lists.push_back(new InventoryList(*other.m_lists[i]));
for (InventoryList *list : other.m_lists) {
m_lists.push_back(new InventoryList(*list));
}
}
return *this;
@ -878,9 +851,7 @@ bool Inventory::operator == (const Inventory &other) const
void Inventory::serialize(std::ostream &os) const
{
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for (InventoryList *list : m_lists) {
os<<"List "<<list->getName()<<" "<<list->getSize()<<"\n";
list->serialize(os);
}
@ -902,17 +873,16 @@ void Inventory::deSerialize(std::istream &is)
std::string name;
std::getline(iss, name, ' ');
if(name == "EndInventory")
{
if (name == "EndInventory") {
break;
}
// This is a temporary backwards compatibility fix
else if(name == "end")
{
if (name == "end") {
break;
}
else if(name == "List")
{
if (name == "List") {
std::string listname;
u32 listsize;
@ -944,15 +914,14 @@ InventoryList * Inventory::addList(const std::string &name, u32 size)
}
return m_lists[i];
}
else
{
//don't create list with invalid name
if (name.find(" ") != std::string::npos) return NULL;
InventoryList *list = new InventoryList(name, size, m_itemdef);
m_lists.push_back(list);
return list;
}
//don't create list with invalid name
if (name.find(' ') != std::string::npos) return NULL;
InventoryList *list = new InventoryList(name, size, m_itemdef);
m_lists.push_back(list);
return list;
}
InventoryList * Inventory::getList(const std::string &name)
@ -966,9 +935,7 @@ InventoryList * Inventory::getList(const std::string &name)
std::vector<const InventoryList*> Inventory::getLists()
{
std::vector<const InventoryList*> lists;
for(u32 i=0; i<m_lists.size(); i++)
{
InventoryList *list = m_lists[i];
for (auto list : m_lists) {
lists.push_back(list);
}
return lists;