minetest/src/nodemetadata.cpp

252 lines
5.4 KiB
C++
Raw Normal View History

2011-04-04 04:13:08 +02:00
/*
2013-02-24 18:40:43 +01:00
Minetest
2013-02-24 19:38:45 +01:00
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
2011-04-04 04:13:08 +02:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
2011-04-04 04:13:08 +02:00
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
2011-04-04 04:13:08 +02:00
You should have received a copy of the GNU Lesser General Public License along
2011-04-04 04:13:08 +02:00
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "nodemetadata.h"
#include "exceptions.h"
2012-03-19 01:08:04 +01:00
#include "gamedef.h"
2011-04-04 14:13:19 +02:00
#include "inventory.h"
#include "log.h"
#include "util/serialize.h"
#include "util/basic_macros.h"
#include "constants.h" // MAP_BLOCKSIZE
#include <sstream>
2011-04-04 04:13:08 +02:00
/*
NodeMetadata
*/
NodeMetadata::NodeMetadata(IItemDefManager *item_def_mgr):
m_inventory(new Inventory(item_def_mgr))
2017-01-31 15:45:28 +01:00
{}
2011-04-04 04:13:08 +02:00
NodeMetadata::~NodeMetadata()
{
2012-03-19 01:08:04 +01:00
delete m_inventory;
2011-04-04 04:13:08 +02:00
}
void NodeMetadata::serialize(std::ostream &os, u8 version, bool disk) const
2011-11-25 14:17:54 +01:00
{
int num_vars = disk ? m_stringvars.size() : countNonPrivate();
2012-03-19 01:08:04 +01:00
writeU32(os, num_vars);
for (StringMap::const_iterator
it = m_stringvars.begin();
it != m_stringvars.end(); ++it) {
bool priv = isPrivate(it->first);
if (!disk && priv)
continue;
os << serializeString(it->first);
os << serializeLongString(it->second);
if (version >= 2)
writeU8(os, (priv) ? 1 : 0);
2011-11-25 14:17:54 +01:00
}
2012-03-19 01:08:04 +01:00
m_inventory->serialize(os);
2011-11-25 14:17:54 +01:00
}
void NodeMetadata::deSerialize(std::istream &is, u8 version)
2011-04-04 04:13:08 +02:00
{
clear();
2012-03-19 01:08:04 +01:00
int num_vars = readU32(is);
for(int i=0; i<num_vars; i++){
std::string name = deSerializeString(is);
std::string var = deSerializeLongString(is);
m_stringvars[name] = var;
if (version >= 2) {
if (readU8(is) == 1)
markPrivate(name, true);
}
}
2011-04-04 04:13:08 +02:00
2012-03-19 01:08:04 +01:00
m_inventory->deSerialize(is);
2011-04-04 04:13:08 +02:00
}
2012-03-19 01:08:04 +01:00
void NodeMetadata::clear()
2011-04-04 04:13:08 +02:00
{
2017-01-31 15:45:28 +01:00
Metadata::clear();
m_privatevars.clear();
2012-03-19 01:08:04 +01:00
m_inventory->clear();
2011-04-04 04:13:08 +02:00
}
bool NodeMetadata::empty() const
{
2017-01-31 15:45:28 +01:00
return Metadata::empty() && m_inventory->getLists().size() == 0;
}
void NodeMetadata::markPrivate(const std::string &name, bool set)
{
if (set)
m_privatevars.insert(name);
else
m_privatevars.erase(name);
}
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)
n++;
}
return n;
}
2011-04-04 04:13:08 +02:00
/*
NodeMetadataList
2011-04-04 04:13:08 +02:00
*/
void NodeMetadataList::serialize(std::ostream &os, u8 blockver, bool disk) const
2011-04-04 04:13:08 +02:00
{
2012-03-19 01:08:04 +01:00
/*
Version 0 is a placeholder for "nothing to see here; go away."
*/
u16 count = countNonEmpty();
if (count == 0) {
2012-03-19 01:08:04 +01:00
writeU8(os, 0); // version
return;
}
u8 version = (blockver > 27) ? 2 : 1;
writeU8(os, version);
2012-03-19 01:08:04 +01:00
writeU16(os, count);
2011-04-04 04:13:08 +02:00
2012-03-19 01:08:04 +01:00
for(std::map<v3s16, NodeMetadata*>::const_iterator
i = m_data.begin();
2015-08-25 22:23:05 +02:00
i != m_data.end(); ++i)
2011-04-04 04:13:08 +02:00
{
2012-03-19 01:08:04 +01:00
v3s16 p = i->first;
NodeMetadata *data = i->second;
if (data->empty())
continue;
2012-03-19 01:08:04 +01:00
u16 p16 = p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE + p.Y * MAP_BLOCKSIZE + p.X;
2012-03-19 01:08:04 +01:00
writeU16(os, p16);
2011-04-04 04:13:08 +02:00
data->serialize(os, version, disk);
2011-04-04 04:13:08 +02:00
}
}
2012-03-19 01:08:04 +01:00
void NodeMetadataList::deSerialize(std::istream &is, IItemDefManager *item_def_mgr)
2011-04-04 04:13:08 +02:00
{
clear();
2011-04-04 04:13:08 +02:00
2012-03-19 01:08:04 +01:00
u8 version = readU8(is);
if (version == 0) {
2012-03-19 01:08:04 +01:00
// Nothing
return;
}
2011-04-05 09:59:48 +02:00
if (version > 2) {
std::string err_str = std::string(FUNCTION_NAME)
+ ": version " + itos(version) + " not supported";
infostream << err_str << std::endl;
throw SerializationError(err_str);
2011-04-05 09:59:48 +02:00
}
2012-03-19 01:08:04 +01:00
u16 count = readU16(is);
for (u16 i = 0; i < count; i++) {
2012-03-19 01:08:04 +01:00
u16 p16 = readU16(is);
2011-04-04 04:13:08 +02:00
v3s16 p;
p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
p.Y = p16 / MAP_BLOCKSIZE;
p16 &= MAP_BLOCKSIZE - 1;
p.X = p16;
if (m_data.find(p) != m_data.end()) {
warningstream << "NodeMetadataList::deSerialize(): "
<< "already set data at position " << PP(p)
<< ": Ignoring." << std::endl;
continue;
2011-04-04 04:13:08 +02:00
}
NodeMetadata *data = new NodeMetadata(item_def_mgr);
data->deSerialize(is, version);
2012-03-19 01:08:04 +01:00
m_data[p] = data;
2011-04-04 04:13:08 +02:00
}
}
2012-03-19 01:08:04 +01:00
2011-04-04 04:13:08 +02:00
NodeMetadataList::~NodeMetadataList()
{
2012-03-19 01:08:04 +01:00
clear();
2011-04-04 04:13:08 +02:00
}
std::vector<v3s16> NodeMetadataList::getAllKeys()
2011-04-04 04:13:08 +02:00
{
std::vector<v3s16> keys;
std::map<v3s16, NodeMetadata *>::const_iterator it;
for (it = m_data.begin(); it != m_data.end(); ++it)
keys.push_back(it->first);
return keys;
}
NodeMetadata *NodeMetadataList::get(v3s16 p)
{
std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
if (n == m_data.end())
2011-04-04 04:13:08 +02:00
return NULL;
2012-03-19 01:08:04 +01:00
return n->second;
2011-04-04 04:13:08 +02:00
}
void NodeMetadataList::remove(v3s16 p)
{
NodeMetadata *olddata = get(p);
if (olddata) {
2011-04-04 04:13:08 +02:00
delete olddata;
2012-03-19 01:08:04 +01:00
m_data.erase(p);
2011-04-04 04:13:08 +02:00
}
}
void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
{
remove(p);
2012-03-19 01:08:04 +01:00
m_data.insert(std::make_pair(p, d));
2011-04-04 04:13:08 +02:00
}
2012-03-19 01:08:04 +01:00
void NodeMetadataList::clear()
2011-04-05 01:56:29 +02:00
{
std::map<v3s16, NodeMetadata*>::iterator it;
for (it = m_data.begin(); it != m_data.end(); ++it) {
delete it->second;
2011-04-05 01:56:29 +02:00
}
2012-03-19 01:08:04 +01:00
m_data.clear();
2011-04-05 01:56:29 +02:00
}
int NodeMetadataList::countNonEmpty() const
{
int n = 0;
std::map<v3s16, NodeMetadata*>::const_iterator it;
for (it = m_data.begin(); it != m_data.end(); ++it) {
if (!it->second->empty())
n++;
}
return n;
}