fca4e5b845b14881d52d907022952f327755d87d
[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 "mapnode.h"
23 #include "exceptions.h"
24
25 /*
26         NodeMetadata
27 */
28
29 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
30
31 NodeMetadata::NodeMetadata()
32 {
33 }
34
35 NodeMetadata::~NodeMetadata()
36 {
37 }
38
39 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
40 {
41         u8 buf[2];
42         is.read((char*)buf, 2);
43         s16 id = readS16(buf);
44
45         core::map<u16, Factory>::Node *n;
46         n = m_types.find(id);
47         if(n == NULL)
48         {
49                 dstream<<"NodeMetadata(): No factory for typeId="<<id<<std::endl;
50                 throw SerializationError("Unknown metadata id");
51         }
52         
53         Factory f = n->getValue();
54         NodeMetadata *meta = (*f)(is);
55         return meta;
56 }
57
58 void NodeMetadata::serialize(std::ostream &os)
59 {
60         u8 buf[2];
61         writeU16(buf, typeId());
62         os.write((char*)buf, 2);
63
64         serializeBody(os);
65 }
66
67 void NodeMetadata::registerType(u16 id, Factory f)
68 {
69         core::map<u16, Factory>::Node *n;
70         n = m_types.find(id);
71         if(n)
72                 return;
73         m_types.insert(id, f);
74 }
75
76 /*
77         SignNodeMetadata
78 */
79
80 SignNodeMetadata::SignNodeMetadata(std::string text):
81         m_text(text)
82 {
83         NodeMetadata::registerType(typeId(), create);
84 }
85 u16 SignNodeMetadata::typeId() const
86 {
87         return CONTENT_SIGN_WALL;
88 }
89 NodeMetadata* SignNodeMetadata::create(std::istream &is)
90 {
91         std::string text = deSerializeString(is);
92         return new SignNodeMetadata(text);
93 }
94 NodeMetadata* SignNodeMetadata::clone()
95 {
96         return new SignNodeMetadata(m_text);
97 }
98 void SignNodeMetadata::serializeBody(std::ostream &os)
99 {
100         os<<serializeString(m_text);
101 }
102 std::string SignNodeMetadata::infoText()
103 {
104         return std::string("\"")+m_text+"\"";
105 }
106
107 /*
108         ChestNodeMetadata
109 */
110
111 ChestNodeMetadata::ChestNodeMetadata()
112 {
113         NodeMetadata::registerType(typeId(), create);
114 }
115 u16 ChestNodeMetadata::typeId() const
116 {
117         return CONTENT_CHEST;
118 }
119 NodeMetadata* ChestNodeMetadata::create(std::istream &is)
120 {
121         return new ChestNodeMetadata();
122 }
123 NodeMetadata* ChestNodeMetadata::clone()
124 {
125         return new ChestNodeMetadata();
126 }
127 void ChestNodeMetadata::serializeBody(std::ostream &os)
128 {
129 }
130 std::string ChestNodeMetadata::infoText()
131 {
132         return "Chest";
133 }
134
135 /*
136         NodeMetadatalist
137 */
138
139 void NodeMetadataList::serialize(std::ostream &os)
140 {
141         u8 buf[6];
142         
143         u16 count = m_data.size();
144         writeU16(buf, count);
145         os.write((char*)buf, 2);
146
147         for(core::map<v3s16, NodeMetadata*>::Iterator
148                         i = m_data.getIterator();
149                         i.atEnd()==false; i++)
150         {
151                 v3s16 p = i.getNode()->getKey();
152                 NodeMetadata *data = i.getNode()->getValue();
153                 
154                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
155                 writeU16(buf, p16);
156                 os.write((char*)buf, 2);
157
158                 data->serialize(os);
159         }
160         
161 }
162 void NodeMetadataList::deSerialize(std::istream &is)
163 {
164         m_data.clear();
165
166         u8 buf[6];
167         
168         is.read((char*)buf, 2);
169         u16 count = readU16(buf);
170         
171         for(u16 i=0; i<count; i++)
172         {
173                 is.read((char*)buf, 2);
174                 u16 p16 = readU16(buf);
175
176                 v3s16 p(0,0,0);
177                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
178                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
179                 p.Y += p16 / MAP_BLOCKSIZE;
180                 p16 -= p.Y * MAP_BLOCKSIZE;
181                 p.X += p16;
182                 
183                 if(m_data.find(p))
184                 {
185                         dstream<<"ERROR: NodeMetadataList::deSerialize(): "
186                                         <<"already set data at position"
187                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<")"
188                                         <<std::endl;
189                         throw SerializationError("NodeMetadataList::deSerialize()");
190                 }
191
192                 NodeMetadata *data = NodeMetadata::deSerialize(is);
193                 
194                 m_data.insert(p, data);
195         }
196 }
197         
198 NodeMetadataList::~NodeMetadataList()
199 {
200         for(core::map<v3s16, NodeMetadata*>::Iterator
201                         i = m_data.getIterator();
202                         i.atEnd()==false; i++)
203         {
204                 delete i.getNode()->getValue();
205         }
206 }
207
208 NodeMetadata* NodeMetadataList::get(v3s16 p)
209 {
210         core::map<v3s16, NodeMetadata*>::Node *n;
211         n = m_data.find(p);
212         if(n == NULL)
213                 return NULL;
214         return n->getValue();
215 }
216
217 void NodeMetadataList::remove(v3s16 p)
218 {
219         NodeMetadata *olddata = get(p);
220         if(olddata)
221         {
222                 delete olddata;
223                 m_data.remove(p);
224         }
225 }
226
227 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
228 {
229         remove(p);
230         m_data.insert(p, d);
231 }
232