WIP node metadata, node timers
[oweals/minetest.git] / src / nodemetadata.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "nodemetadata.h"
21 #include "utility.h"
22 #include "exceptions.h"
23 #include "gamedef.h"
24 #include "inventory.h"
25 #include <sstream>
26 #include "log.h"
27
28 /*
29         NodeMetadata
30 */
31
32 NodeMetadata::NodeMetadata(IGameDef *gamedef):
33         m_stringvars(),
34         m_inventory(new Inventory(gamedef->idef())),
35         m_inventorydrawspec(""),
36         m_formspec(""),
37         m_infotext(""),
38         m_allow_removal(true)
39 {
40 }
41
42 NodeMetadata::~NodeMetadata()
43 {
44         delete m_inventory;
45 }
46
47 void NodeMetadata::serialize(std::ostream &os) const
48 {
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);
55         }
56
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);
62 }
63
64 void NodeMetadata::deSerialize(std::istream &is)
65 {
66         m_stringvars.clear();
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;
72         }
73
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);
79 }
80
81 void NodeMetadata::clear()
82 {
83         m_stringvars.clear();
84         m_inventory->clear();
85         m_inventorydrawspec = "";
86         m_formspec = "";
87         m_infotext = "";
88         m_allow_removal = true;
89 }
90
91 /*
92         NodeMetadataList
93 */
94
95 void NodeMetadataList::serialize(std::ostream &os) const
96 {
97         /*
98                 Version 0 is a placeholder for "nothing to see here; go away."
99         */
100
101         if(m_data.size() == 0){
102                 writeU8(os, 0); // version
103                 return;
104         }
105
106         writeU8(os, 1); // version
107
108         u16 count = m_data.size();
109         writeU16(os, count);
110
111         for(std::map<v3s16, NodeMetadata*>::const_iterator
112                         i = m_data.begin();
113                         i != m_data.end(); i++)
114         {
115                 v3s16 p = i->first;
116                 NodeMetadata *data = i->second;
117
118                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
119                 writeU16(os, p16);
120
121                 data->serialize(os);
122         }
123 }
124
125 void NodeMetadataList::deSerialize(std::istream &is, IGameDef *gamedef)
126 {
127         m_data.clear();
128
129         u8 version = readU8(is);
130         
131         if(version == 0){
132                 // Nothing
133                 return;
134         }
135
136         if(version != 1){
137                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
138                                 <<std::endl;
139                 throw SerializationError("NodeMetadataList::deSerialize");
140         }
141
142         u16 count = readU16(is);
143
144         for(u16 i=0; i<count; i++)
145         {
146                 u16 p16 = readU16(is);
147
148                 v3s16 p(0,0,0);
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;
153                 p.X += p16;
154
155                 if(m_data.find(p) != m_data.end())
156                 {
157                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
158                                         <<"already set data at position"
159                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
160                                         <<std::endl;
161                         continue;
162                 }
163
164                 NodeMetadata *data = new NodeMetadata(gamedef);
165                 data->deSerialize(is);
166                 m_data[p] = data;
167         }
168 }
169
170 NodeMetadataList::~NodeMetadataList()
171 {
172         clear();
173 }
174
175 NodeMetadata* NodeMetadataList::get(v3s16 p)
176 {
177         std::map<v3s16, NodeMetadata*>::const_iterator n = m_data.find(p);
178         if(n == m_data.end())
179                 return NULL;
180         return n->second;
181 }
182
183 void NodeMetadataList::remove(v3s16 p)
184 {
185         NodeMetadata *olddata = get(p);
186         if(olddata)
187         {
188                 delete olddata;
189                 m_data.erase(p);
190         }
191 }
192
193 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
194 {
195         remove(p);
196         m_data.insert(std::make_pair(p, d));
197 }
198
199 void NodeMetadataList::clear()
200 {
201         for(std::map<v3s16, NodeMetadata*>::iterator
202                         i = m_data.begin();
203                         i != m_data.end(); i++)
204         {
205                 delete i->second;
206         }
207         m_data.clear();
208 }