]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodemetadata.cpp
Make CONTENT_IGNORE to not block the view when occlusion culling
[dragonfireclient.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 #include "inventory.h"
25 #include <sstream>
26 #include "content_mapnode.h"
27 #include "log.h"
28
29 /*
30         NodeMetadata
31 */
32
33 core::map<u16, NodeMetadata::Factory> NodeMetadata::m_types;
34
35 NodeMetadata::NodeMetadata()
36 {
37 }
38
39 NodeMetadata::~NodeMetadata()
40 {
41 }
42
43 NodeMetadata* NodeMetadata::deSerialize(std::istream &is)
44 {
45         // Read id
46         u8 buf[2];
47         is.read((char*)buf, 2);
48         s16 id = readS16(buf);
49         
50         // Read data
51         std::string data = deSerializeString(is);
52         
53         // Find factory function
54         core::map<u16, Factory>::Node *n;
55         n = m_types.find(id);
56         if(n == NULL)
57         {
58                 // If factory is not found, just return.
59                 infostream<<"WARNING: NodeMetadata: No factory for typeId="
60                                 <<id<<std::endl;
61                 return NULL;
62         }
63         
64         // Try to load the metadata. If it fails, just return.
65         try
66         {
67                 std::istringstream iss(data, std::ios_base::binary);
68                 
69                 Factory f = n->getValue();
70                 NodeMetadata *meta = (*f)(iss);
71                 return meta;
72         }
73         catch(SerializationError &e)
74         {
75                 infostream<<"WARNING: NodeMetadata: ignoring SerializationError"<<std::endl;
76                 return NULL;
77         }
78 }
79
80 void NodeMetadata::serialize(std::ostream &os)
81 {
82         u8 buf[2];
83         writeU16(buf, typeId());
84         os.write((char*)buf, 2);
85         
86         std::ostringstream oss(std::ios_base::binary);
87         serializeBody(oss);
88         os<<serializeString(oss.str());
89 }
90
91 void NodeMetadata::registerType(u16 id, Factory f)
92 {
93         core::map<u16, Factory>::Node *n;
94         n = m_types.find(id);
95         if(n)
96                 return;
97         m_types.insert(id, f);
98 }
99
100 /*
101         NodeMetadataList
102 */
103
104 void NodeMetadataList::serialize(std::ostream &os)
105 {
106         u8 buf[6];
107         
108         u16 version = 1;
109         writeU16(buf, version);
110         os.write((char*)buf, 2);
111
112         u16 count = m_data.size();
113         writeU16(buf, count);
114         os.write((char*)buf, 2);
115
116         for(core::map<v3s16, NodeMetadata*>::Iterator
117                         i = m_data.getIterator();
118                         i.atEnd()==false; i++)
119         {
120                 v3s16 p = i.getNode()->getKey();
121                 NodeMetadata *data = i.getNode()->getValue();
122                 
123                 u16 p16 = p.Z*MAP_BLOCKSIZE*MAP_BLOCKSIZE + p.Y*MAP_BLOCKSIZE + p.X;
124                 writeU16(buf, p16);
125                 os.write((char*)buf, 2);
126
127                 data->serialize(os);
128         }
129         
130 }
131 void NodeMetadataList::deSerialize(std::istream &is)
132 {
133         m_data.clear();
134
135         u8 buf[6];
136         
137         is.read((char*)buf, 2);
138         u16 version = readU16(buf);
139
140         if(version > 1)
141         {
142                 infostream<<__FUNCTION_NAME<<": version "<<version<<" not supported"
143                                 <<std::endl;
144                 throw SerializationError("NodeMetadataList::deSerialize");
145         }
146         
147         is.read((char*)buf, 2);
148         u16 count = readU16(buf);
149         
150         for(u16 i=0; i<count; i++)
151         {
152                 is.read((char*)buf, 2);
153                 u16 p16 = readU16(buf);
154
155                 v3s16 p(0,0,0);
156                 p.Z += p16 / MAP_BLOCKSIZE / MAP_BLOCKSIZE;
157                 p16 -= p.Z * MAP_BLOCKSIZE * MAP_BLOCKSIZE;
158                 p.Y += p16 / MAP_BLOCKSIZE;
159                 p16 -= p.Y * MAP_BLOCKSIZE;
160                 p.X += p16;
161                 
162                 NodeMetadata *data = NodeMetadata::deSerialize(is);
163
164                 if(data == NULL)
165                         continue;
166                 
167                 if(m_data.find(p))
168                 {
169                         infostream<<"WARNING: NodeMetadataList::deSerialize(): "
170                                         <<"already set data at position"
171                                         <<"("<<p.X<<","<<p.Y<<","<<p.Z<<"): Ignoring."
172                                         <<std::endl;
173                         delete data;
174                         continue;
175                 }
176
177                 m_data.insert(p, data);
178         }
179 }
180         
181 NodeMetadataList::~NodeMetadataList()
182 {
183         for(core::map<v3s16, NodeMetadata*>::Iterator
184                         i = m_data.getIterator();
185                         i.atEnd()==false; i++)
186         {
187                 delete i.getNode()->getValue();
188         }
189 }
190
191 NodeMetadata* NodeMetadataList::get(v3s16 p)
192 {
193         core::map<v3s16, NodeMetadata*>::Node *n;
194         n = m_data.find(p);
195         if(n == NULL)
196                 return NULL;
197         return n->getValue();
198 }
199
200 void NodeMetadataList::remove(v3s16 p)
201 {
202         NodeMetadata *olddata = get(p);
203         if(olddata)
204         {
205                 delete olddata;
206                 m_data.remove(p);
207         }
208 }
209
210 void NodeMetadataList::set(v3s16 p, NodeMetadata *d)
211 {
212         remove(p);
213         m_data.insert(p, d);
214 }
215
216 bool NodeMetadataList::step(float dtime)
217 {
218         bool something_changed = false;
219         for(core::map<v3s16, NodeMetadata*>::Iterator
220                         i = m_data.getIterator();
221                         i.atEnd()==false; i++)
222         {
223                 v3s16 p = i.getNode()->getKey();
224                 NodeMetadata *meta = i.getNode()->getValue();
225                 bool changed = meta->step(dtime);
226                 if(changed)
227                         something_changed = true;
228         }
229         return something_changed;
230 }
231