3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
22 #include "exceptions.h"
24 #include "inventory.h"
32 NodeMetadata::NodeMetadata(IGameDef *gamedef):
34 m_inventory(new Inventory(gamedef->idef()))
38 NodeMetadata::~NodeMetadata()
43 void NodeMetadata::serialize(std::ostream &os) const
45 int num_vars = m_stringvars.size();
46 writeU32(os, num_vars);
47 for(std::map<std::string, std::string>::const_iterator
48 i = m_stringvars.begin(); i != m_stringvars.end(); i++){
49 os<<serializeString(i->first);
50 os<<serializeLongString(i->second);
53 m_inventory->serialize(os);
56 void NodeMetadata::deSerialize(std::istream &is)
59 int num_vars = readU32(is);
60 for(int i=0; i<num_vars; i++){
61 std::string name = deSerializeString(is);
62 std::string var = deSerializeLongString(is);
63 m_stringvars[name] = var;
66 m_inventory->deSerialize(is);
69 void NodeMetadata::clear()
79 void NodeMetadataList::serialize(std::ostream &os) const
82 Version 0 is a placeholder for "nothing to see here; go away."
85 if(m_data.size() == 0){
86 writeU8(os, 0); // version
90 writeU8(os, 1); // version
92 u16 count = m_data.size();
95 for(std::map<v3s16, NodeMetadata*>::const_iterator
97 i != m_data.end(); i++)
100 NodeMetadata *data = i->second;
102 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
109 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
113 u8 version = readU8(is);
121 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
123 throw SerializationError("NodeMetadataList::deSerialize");
126 u16 count = readU16(is);
128 for(u16 i=0; i<count; i++)
130 u16 p16 = readU16(is);
133 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
134 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
135 p.Y += p16 / MAP_BLOCKSIZE;
136 p16 -= p.Y * MAP_BLOCKSIZE;
139 if(m_data.find(p) != m_data.end())
141 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
142 <<"already set data at position"
143 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
148 NodeMetadata *data = new NodeMetadata(gamedef);
149 data->deSerialize(is);
154 NodeMetadataList::~NodeMetadataList()
159 NodeMetadata* NodeMetadataList::get(v3s16 p)
161 std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
162 if(n == m_data.end())
167 void NodeMetadataList::remove(v3s16 p)
169 NodeMetadata *olddata = get(p);
177 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
180 m_data.insert(std::make_pair(p, d));
183 void NodeMetadataList::clear()
185 for(std::map<v3s16, NodeMetadata*>::iterator
187 i != m_data.end(); i++)