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())),
35 m_inventorydrawspec(""),
42 NodeMetadata::~NodeMetadata()
47 void NodeMetadata::serialize(std::ostream &os) const
49 int num_vars = m_stringvars.size();
50 writeU32(os, num_vars);
51 for(std::map<std::string, std::string>::const_iterator
52 i = m_stringvars.begin(); i != m_stringvars.end(); i++){
53 os<<serializeString(i->first);
54 os<<serializeLongString(i->second);
57 m_inventory->serialize(os);
58 os<<serializeString(m_inventorydrawspec);
59 os<<serializeString(m_formspec);
60 os<<serializeString(m_infotext);
61 writeU8(os, m_allow_removal);
64 void NodeMetadata::deSerialize(std::istream &is)
67 int num_vars = readU32(is);
68 for(int i=0; i<num_vars; i++){
69 std::string name = deSerializeString(is);
70 std::string var = deSerializeLongString(is);
71 m_stringvars[name] = var;
74 m_inventory->deSerialize(is);
75 m_inventorydrawspec = deSerializeString(is);
76 m_formspec = deSerializeString(is);
77 m_infotext = deSerializeString(is);
78 m_allow_removal = readU8(is);
81 void NodeMetadata::clear()
85 m_inventorydrawspec = "";
88 m_allow_removal = true;
95 void NodeMetadataList::serialize(std::ostream &os) const
98 Version 0 is a placeholder for "nothing to see here; go away."
101 if(m_data.size() == 0){
102 writeU8(os, 0); // version
106 writeU8(os, 1); // version
108 u16 count = m_data.size();
111 for(std::map<v3s16, NodeMetadata*>::const_iterator
113 i != m_data.end(); i++)
116 NodeMetadata *data = i->second;
118 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
125 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
129 u8 version = readU8(is);
137 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
139 throw SerializationError("NodeMetadataList::deSerialize");
142 u16 count = readU16(is);
144 for(u16 i=0; i<count; i++)
146 u16 p16 = readU16(is);
149 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
150 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
151 p.Y += p16 / MAP_BLOCKSIZE;
152 p16 -= p.Y * MAP_BLOCKSIZE;
155 if(m_data.find(p) != m_data.end())
157 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
158 <<"already set data at position"
159 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
164 NodeMetadata *data = new NodeMetadata(gamedef);
165 data->deSerialize(is);
170 NodeMetadataList::~NodeMetadataList()
175 NodeMetadata* NodeMetadataList::get(v3s16 p)
177 std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
178 if(n == m_data.end())
183 void NodeMetadataList::remove(v3s16 p)
185 NodeMetadata *olddata = get(p);
193 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
196 m_data.insert(std::make_pair(p, d));
199 void NodeMetadataList::clear()
201 for(std::map<v3s16, NodeMetadata*>::iterator
203 i != m_data.end(); i++)