3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "nodemetadata.h"
21 #include "exceptions.h"
23 #include "inventory.h"
25 #include "util/serialize.h"
26 #include "constants.h" // MAP_BLOCKSIZE
33 NodeMetadata::NodeMetadata(IGameDef *gamedef):
35 m_inventory(new Inventory(gamedef->idef()))
39 NodeMetadata::~NodeMetadata()
44 void NodeMetadata::serialize(std::ostream &os) const
46 int num_vars = m_stringvars.size();
47 writeU32(os, num_vars);
48 for(std::map<std::string, std::string>::const_iterator
49 i = m_stringvars.begin(); i != m_stringvars.end(); i++){
50 os<<serializeString(i->first);
51 os<<serializeLongString(i->second);
54 m_inventory->serialize(os);
57 void NodeMetadata::deSerialize(std::istream &is)
60 int num_vars = readU32(is);
61 for(int i=0; i<num_vars; i++){
62 std::string name = deSerializeString(is);
63 std::string var = deSerializeLongString(is);
64 m_stringvars[name] = var;
67 m_inventory->deSerialize(is);
70 void NodeMetadata::clear()
80 void NodeMetadataList::serialize(std::ostream &os) const
83 Version 0 is a placeholder for "nothing to see here; go away."
86 if(m_data.size() == 0){
87 writeU8(os, 0); // version
91 writeU8(os, 1); // version
93 u16 count = m_data.size();
96 for(std::map<v3s16, NodeMetadata*>::const_iterator
98 i != m_data.end(); i++)
101 NodeMetadata *data = i->second;
103 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
110 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
114 u8 version = readU8(is);
122 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
124 throw SerializationError("NodeMetadataList::deSerialize");
127 u16 count = readU16(is);
129 for(u16 i=0; i<count; i++)
131 u16 p16 = readU16(is);
134 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
135 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
136 p.Y += p16 / MAP_BLOCKSIZE;
137 p16 -= p.Y * MAP_BLOCKSIZE;
140 if(m_data.find(p) != m_data.end())
142 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
143 <<"already set data at position"
144 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
149 NodeMetadata *data = new NodeMetadata(gamedef);
150 data->deSerialize(is);
155 NodeMetadataList::~NodeMetadataList()
160 NodeMetadata* NodeMetadataList::get(v3s16 p)
162 std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
163 if(n == m_data.end())
168 void NodeMetadataList::remove(v3s16 p)
170 NodeMetadata *olddata = get(p);
178 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
181 m_data.insert(std::make_pair(p, d));
184 void NodeMetadataList::clear()
186 for(std::map<v3s16, NodeMetadata*>::iterator
188 i != m_data.end(); i++)