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 (StringMap::const_iterator
49 it = m_stringvars.begin();
50 it != m_stringvars.end(); ++it) {
51 os << serializeString(it->first);
52 os << serializeLongString(it->second);
55 m_inventory->serialize(os);
58 void NodeMetadata::deSerialize(std::istream &is)
61 int num_vars = readU32(is);
62 for(int i=0; i<num_vars; i++){
63 std::string name = deSerializeString(is);
64 std::string var = deSerializeLongString(is);
65 m_stringvars[name] = var;
68 m_inventory->deSerialize(is);
71 void NodeMetadata::clear()
81 void NodeMetadataList::serialize(std::ostream &os) const
84 Version 0 is a placeholder for "nothing to see here; go away."
88 writeU8(os, 0); // version
92 writeU8(os, 1); // version
94 u16 count = m_data.size();
97 for(std::map<v3s16, NodeMetadata*>::const_iterator
99 i != m_data.end(); i++)
102 NodeMetadata *data = i->second;
104 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
111 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
115 u8 version = readU8(is);
123 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
125 throw SerializationError("NodeMetadataList::deSerialize");
128 u16 count = readU16(is);
130 for(u16 i=0; i<count; i++)
132 u16 p16 = readU16(is);
135 p.Z = p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
136 p16 &= MAP_BLOCKSIZE * MAP_BLOCKSIZE - 1;
137 p.Y = p16 / MAP_BLOCKSIZE;
138 p16 &= MAP_BLOCKSIZE - 1;
141 if(m_data.find(p) != m_data.end())
143 infostream<<"WARNING: NodeMetadataList::deSerialize(): "
144 <<"already set data at position"
145 <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
150 NodeMetadata *data = new NodeMetadata(gamedef);
151 data->deSerialize(is);
156 NodeMetadataList::~NodeMetadataList()
161 std::vector<v3s16> NodeMetadataList::getAllKeys()
163 std::vector<v3s16> keys;
165 std::map<v3s16, NodeMetadata *>::const_iterator it;
166 for (it = m_data.begin(); it != m_data.end(); ++it)
167 keys.push_back(it->first);
172 NodeMetadata *NodeMetadataList::get(v3s16 p)
174 std::map<v3s16, NodeMetadata *>::const_iterator n = m_data.find(p);
175 if (n == m_data.end())
180 void NodeMetadataList::remove(v3s16 p)
182 NodeMetadata *olddata = get(p);
189 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
192 m_data.insert(std::make_pair(p, d));
195 void NodeMetadataList::clear()
197 std::map<v3s16, NodeMetadata*>::iterator it;
198 for (it = m_data.begin(); it != m_data.end(); ++it) {
204 std::string NodeMetadata::getString(const std::string &name,
205 unsigned short recursion) const
207 StringMap::const_iterator it = m_stringvars.find(name);
208 if (it == m_stringvars.end())
211 return resolveString(it->second, recursion);
214 void NodeMetadata::setString(const std::string &name, const std::string &var)
217 m_stringvars.erase(name);
219 m_stringvars[name] = var;
223 std::string NodeMetadata::resolveString(const std::string &str,
224 unsigned short recursion) const
229 if (str.substr(0, 2) == "${" && str[str.length() - 1] == '}') {
230 return getString(str.substr(2, str.length() - 3), recursion + 1);