]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.cpp
Use hexadecimal RRGGBB instead of colorkeys, rename getColor to parseColor
[minetest.git] / src / nodedef.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 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.
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 Lesser General Public License for more details.
14
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.
18 */
19
20 #include "nodedef.h"
21
22 #include "main.h" // For g_settings
23 #include "itemdef.h"
24 #ifndef SERVER
25 #include "tile.h"
26 #endif
27 #include "log.h"
28 #include "settings.h"
29 #include "nameidmapping.h"
30 #include "util/serialize.h"
31 //#include "profiler.h" // For TimeTaker
32
33 /*
34         NodeBox
35 */
36
37 void NodeBox::reset()
38 {
39         type = NODEBOX_REGULAR;
40         // default is empty
41         fixed.clear();
42         // default is sign/ladder-like
43         wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
44         wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
45         wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
46 }
47
48 void NodeBox::serialize(std::ostream &os) const
49 {
50         writeU8(os, 1); // version
51         writeU8(os, type);
52
53         if(type == NODEBOX_FIXED)
54         {
55                 writeU16(os, fixed.size());
56                 for(std::vector<aabb3f>::const_iterator
57                                 i = fixed.begin();
58                                 i != fixed.end(); i++)
59                 {
60                         writeV3F1000(os, i->MinEdge);
61                         writeV3F1000(os, i->MaxEdge);
62                 }
63         }
64         else if(type == NODEBOX_WALLMOUNTED)
65         {
66                 writeV3F1000(os, wall_top.MinEdge);
67                 writeV3F1000(os, wall_top.MaxEdge);
68                 writeV3F1000(os, wall_bottom.MinEdge);
69                 writeV3F1000(os, wall_bottom.MaxEdge);
70                 writeV3F1000(os, wall_side.MinEdge);
71                 writeV3F1000(os, wall_side.MaxEdge);
72         }
73 }
74
75 void NodeBox::deSerialize(std::istream &is)
76 {
77         int version = readU8(is);
78         if(version != 1)
79                 throw SerializationError("unsupported NodeBox version");
80
81         reset();
82
83         type = (enum NodeBoxType)readU8(is);
84
85         if(type == NODEBOX_FIXED)
86         {
87                 u16 fixed_count = readU16(is);
88                 while(fixed_count--)
89                 {
90                         aabb3f box;
91                         box.MinEdge = readV3F1000(is);
92                         box.MaxEdge = readV3F1000(is);
93                         fixed.push_back(box);
94                 }
95         }
96         else if(type == NODEBOX_WALLMOUNTED)
97         {
98                 wall_top.MinEdge = readV3F1000(is);
99                 wall_top.MaxEdge = readV3F1000(is);
100                 wall_bottom.MinEdge = readV3F1000(is);
101                 wall_bottom.MaxEdge = readV3F1000(is);
102                 wall_side.MinEdge = readV3F1000(is);
103                 wall_side.MaxEdge = readV3F1000(is);
104         }
105 }
106
107 /*
108         TileDef
109 */
110
111 void TileDef::serialize(std::ostream &os, u16 protocol_version) const
112 {
113         if(protocol_version >= 17)
114                 writeU8(os, 1); 
115         else
116                 writeU8(os, 0);
117         os<<serializeString(name);
118         writeU8(os, animation.type);
119         writeU16(os, animation.aspect_w);
120         writeU16(os, animation.aspect_h);
121         writeF1000(os, animation.length);
122         if(protocol_version >= 17)
123                 writeU8(os, backface_culling);
124 }
125
126 void TileDef::deSerialize(std::istream &is)
127 {
128         int version = readU8(is);
129         name = deSerializeString(is);
130         animation.type = (TileAnimationType)readU8(is);
131         animation.aspect_w = readU16(is);
132         animation.aspect_h = readU16(is);
133         animation.length = readF1000(is);
134         if(version >= 1)
135                 backface_culling = readU8(is);
136 }
137
138 /*
139         SimpleSoundSpec serialization
140 */
141
142 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
143                 std::ostream &os)
144 {
145         os<<serializeString(ss.name);
146         writeF1000(os, ss.gain);
147 }
148 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
149 {
150         ss.name = deSerializeString(is);
151         ss.gain = readF1000(is);
152 }
153
154 /*
155         ContentFeatures
156 */
157
158 ContentFeatures::ContentFeatures()
159 {
160         reset();
161 }
162
163 ContentFeatures::~ContentFeatures()
164 {
165 }
166
167 void ContentFeatures::reset()
168 {
169         /*
170                 Cached stuff
171         */
172 #ifndef SERVER
173         solidness = 2;
174         visual_solidness = 0;
175         backface_culling = true;
176 #endif
177         has_on_construct = false;
178         has_on_destruct = false;
179         has_after_destruct = false;
180         /*
181                 Actual data
182
183                 NOTE: Most of this is always overridden by the default values given
184                       in builtin.lua
185         */
186         name = "";
187         groups.clear();
188         // Unknown nodes can be dug
189         groups["dig_immediate"] = 2;
190         drawtype = NDT_NORMAL;
191         visual_scale = 1.0;
192         for(u32 i=0; i<6; i++)
193                 tiledef[i] = TileDef();
194         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
195                 tiledef_special[j] = TileDef();
196         alpha = 255;
197         post_effect_color = video::SColor(0, 0, 0, 0);
198         param_type = CPT_NONE;
199         param_type_2 = CPT2_NONE;
200         is_ground_content = false;
201         light_propagates = false;
202         sunlight_propagates = false;
203         walkable = true;
204         pointable = true;
205         diggable = true;
206         climbable = false;
207         buildable_to = false;
208         rightclickable = true;
209         liquid_type = LIQUID_NONE;
210         liquid_alternative_flowing = "";
211         liquid_alternative_source = "";
212         liquid_viscosity = 0;
213         liquid_renewable = true;
214         drowning = true;
215         light_source = 0;
216         damage_per_second = 0;
217         node_box = NodeBox();
218         selection_box = NodeBox();
219         legacy_facedir_simple = false;
220         legacy_wallmounted = false;
221         sound_footstep = SimpleSoundSpec();
222         sound_dig = SimpleSoundSpec("__group");
223         sound_dug = SimpleSoundSpec();
224 }
225
226 void ContentFeatures::serialize(std::ostream &os, u16 protocol_version)
227 {
228         if(protocol_version < 14){
229                 serializeOld(os, protocol_version);
230                 return;
231         }
232
233         writeU8(os, 6); // version
234         os<<serializeString(name);
235         writeU16(os, groups.size());
236         for(ItemGroupList::const_iterator
237                         i = groups.begin(); i != groups.end(); i++){
238                 os<<serializeString(i->first);
239                 writeS16(os, i->second);
240         }
241         writeU8(os, drawtype);
242         writeF1000(os, visual_scale);
243         writeU8(os, 6);
244         for(u32 i=0; i<6; i++)
245                 tiledef[i].serialize(os, protocol_version);
246         writeU8(os, CF_SPECIAL_COUNT);
247         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
248                 tiledef_special[i].serialize(os, protocol_version);
249         }
250         writeU8(os, alpha);
251         writeU8(os, post_effect_color.getAlpha());
252         writeU8(os, post_effect_color.getRed());
253         writeU8(os, post_effect_color.getGreen());
254         writeU8(os, post_effect_color.getBlue());
255         writeU8(os, param_type);
256         writeU8(os, param_type_2);
257         writeU8(os, is_ground_content);
258         writeU8(os, light_propagates);
259         writeU8(os, sunlight_propagates);
260         writeU8(os, walkable);
261         writeU8(os, pointable);
262         writeU8(os, diggable);
263         writeU8(os, climbable);
264         writeU8(os, buildable_to);
265         os<<serializeString(""); // legacy: used to be metadata_name
266         writeU8(os, liquid_type);
267         os<<serializeString(liquid_alternative_flowing);
268         os<<serializeString(liquid_alternative_source);
269         writeU8(os, liquid_viscosity);
270         writeU8(os, liquid_renewable);
271         writeU8(os, light_source);
272         writeU32(os, damage_per_second);
273         node_box.serialize(os);
274         selection_box.serialize(os);
275         writeU8(os, legacy_facedir_simple);
276         writeU8(os, legacy_wallmounted);
277         serializeSimpleSoundSpec(sound_footstep, os);
278         serializeSimpleSoundSpec(sound_dig, os);
279         serializeSimpleSoundSpec(sound_dug, os);
280         writeU8(os, rightclickable);
281         // Stuff below should be moved to correct place in a version that otherwise changes
282         // the protocol version
283         writeU8(os, drowning);
284 }
285
286 void ContentFeatures::deSerialize(std::istream &is)
287 {
288         int version = readU8(is);
289         if(version != 6){
290                 deSerializeOld(is, version);
291                 return;
292         }
293
294         name = deSerializeString(is);
295         groups.clear();
296         u32 groups_size = readU16(is);
297         for(u32 i=0; i<groups_size; i++){
298                 std::string name = deSerializeString(is);
299                 int value = readS16(is);
300                 groups[name] = value;
301         }
302         drawtype = (enum NodeDrawType)readU8(is);
303         visual_scale = readF1000(is);
304         if(readU8(is) != 6)
305                 throw SerializationError("unsupported tile count");
306         for(u32 i=0; i<6; i++)
307                 tiledef[i].deSerialize(is);
308         if(readU8(is) != CF_SPECIAL_COUNT)
309                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
310         for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
311                 tiledef_special[i].deSerialize(is);
312         alpha = readU8(is);
313         post_effect_color.setAlpha(readU8(is));
314         post_effect_color.setRed(readU8(is));
315         post_effect_color.setGreen(readU8(is));
316         post_effect_color.setBlue(readU8(is));
317         param_type = (enum ContentParamType)readU8(is);
318         param_type_2 = (enum ContentParamType2)readU8(is);
319         is_ground_content = readU8(is);
320         light_propagates = readU8(is);
321         sunlight_propagates = readU8(is);
322         walkable = readU8(is);
323         pointable = readU8(is);
324         diggable = readU8(is);
325         climbable = readU8(is);
326         buildable_to = readU8(is);
327         deSerializeString(is); // legacy: used to be metadata_name
328         liquid_type = (enum LiquidType)readU8(is);
329         liquid_alternative_flowing = deSerializeString(is);
330         liquid_alternative_source = deSerializeString(is);
331         liquid_viscosity = readU8(is);
332         liquid_renewable = readU8(is);
333         light_source = readU8(is);
334         damage_per_second = readU32(is);
335         node_box.deSerialize(is);
336         selection_box.deSerialize(is);
337         legacy_facedir_simple = readU8(is);
338         legacy_wallmounted = readU8(is);
339         deSerializeSimpleSoundSpec(sound_footstep, is);
340         deSerializeSimpleSoundSpec(sound_dig, is);
341         deSerializeSimpleSoundSpec(sound_dug, is);
342         rightclickable = readU8(is);
343         // If you add anything here, insert it primarily inside the try-catch
344         // block to not need to increase the version.
345         try{
346                 // Stuff below should be moved to correct place in a version that
347                 // otherwise changes the protocol version
348                 drowning = readU8(is);
349         }catch(SerializationError &e) {};
350 }
351
352 /*
353         CNodeDefManager
354 */
355
356 class CNodeDefManager: public IWritableNodeDefManager
357 {
358 public:
359         void clear()
360         {
361                 m_name_id_mapping.clear();
362                 m_name_id_mapping_with_aliases.clear();
363
364                 for(u16 i=0; i<=MAX_CONTENT; i++)
365                 {
366                         ContentFeatures &f = m_content_features[i];
367                         f.reset(); // Reset to defaults
368                 }
369
370                 // Set CONTENT_AIR
371                 {
372                         ContentFeatures f;
373                         f.name = "air";
374                         f.drawtype = NDT_AIRLIKE;
375                         f.param_type = CPT_LIGHT;
376                         f.light_propagates = true;
377                         f.sunlight_propagates = true;
378                         f.walkable = false;
379                         f.pointable = false;
380                         f.diggable = false;
381                         f.buildable_to = true;
382                         // Insert directly into containers
383                         content_t c = CONTENT_AIR;
384                         m_content_features[c] = f;
385                         addNameIdMapping(c, f.name);
386                 }
387                 // Set CONTENT_IGNORE
388                 {
389                         ContentFeatures f;
390                         f.name = "ignore";
391                         f.drawtype = NDT_AIRLIKE;
392                         f.param_type = CPT_NONE;
393                         f.light_propagates = false;
394                         f.sunlight_propagates = false;
395                         f.walkable = false;
396                         f.pointable = false;
397                         f.diggable = false;
398                         // A way to remove accidental CONTENT_IGNOREs
399                         f.buildable_to = true;
400                         // Insert directly into containers
401                         content_t c = CONTENT_IGNORE;
402                         m_content_features[c] = f;
403                         addNameIdMapping(c, f.name);
404                 }
405         }
406         // CONTENT_IGNORE = not found
407         content_t getFreeId()
408         {
409                 for(u32 i=0; i<=0xffff; i++){
410                         const ContentFeatures &f = m_content_features[i];
411                         if(f.name == "")
412                                 return i;
413                 }
414                 return CONTENT_IGNORE;
415         }
416         CNodeDefManager()
417         {
418                 clear();
419         }
420         virtual ~CNodeDefManager()
421         {
422         }
423         virtual IWritableNodeDefManager* clone()
424         {
425                 CNodeDefManager *mgr = new CNodeDefManager();
426                 for(u16 i=0; i<=MAX_CONTENT; i++)
427                 {
428                         mgr->set(i, get(i));
429                 }
430                 return mgr;
431         }
432         virtual const ContentFeatures& get(content_t c) const
433         {
434                 assert(c <= MAX_CONTENT);
435                 return m_content_features[c];
436         }
437         virtual const ContentFeatures& get(const MapNode &n) const
438         {
439                 return get(n.getContent());
440         }
441         virtual bool getId(const std::string &name, content_t &result) const
442         {
443                 std::map<std::string, content_t>::const_iterator
444                         i = m_name_id_mapping_with_aliases.find(name);
445                 if(i == m_name_id_mapping_with_aliases.end())
446                         return false;
447                 result = i->second;
448                 return true;
449         }
450         virtual content_t getId(const std::string &name) const
451         {
452                 content_t id = CONTENT_IGNORE;
453                 getId(name, id);
454                 return id;
455         }
456         virtual void getIds(const std::string &name, std::set<content_t> &result)
457                         const
458         {
459                 //TimeTaker t("getIds", NULL, PRECISION_MICRO);
460                 if(name.substr(0,6) != "group:"){
461                         content_t id = CONTENT_IGNORE;
462                         if(getId(name, id))
463                                 result.insert(id);
464                         return;
465                 }
466                 std::string group = name.substr(6);
467
468 #if 1   // Optimized version, takes less than 1 microsecond at -O1
469                 std::map<std::string, GroupItems>::const_iterator
470                         i = m_group_to_items.find(group);
471                 if (i == m_group_to_items.end())
472                         return;
473
474                 const GroupItems &items = i->second;
475                 for (GroupItems::const_iterator j = items.begin();
476                         j != items.end(); ++j) {
477                         if ((*j).second != 0)
478                                 result.insert((*j).first);
479                 }
480 #else   // Old version, takes about ~150-200us at -O1
481                 for(u16 id=0; id<=MAX_CONTENT; id++)
482                 {
483                         const ContentFeatures &f = m_content_features[id];
484                         if(f.name == "") // Quickly discard undefined nodes
485                                 continue;
486                         if(itemgroup_get(f.groups, group) != 0)
487                                 result.insert(id);
488                 }
489 #endif
490                 //printf("getIds: %dus\n", t.stop());
491         }
492         virtual const ContentFeatures& get(const std::string &name) const
493         {
494                 content_t id = CONTENT_IGNORE;
495                 getId(name, id);
496                 return get(id);
497         }
498         // IWritableNodeDefManager
499         virtual void set(content_t c, const ContentFeatures &def)
500         {
501                 verbosestream<<"registerNode: registering content id \""<<c
502                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
503                 assert(c <= MAX_CONTENT);
504                 // Don't allow redefining CONTENT_IGNORE (but allow air)
505                 if(def.name == "ignore" || c == CONTENT_IGNORE){
506                         infostream<<"registerNode: WARNING: Ignoring "
507                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
508                         return;
509                 }
510                 // Check that the special contents are not redefined as different id
511                 // because it would mess up everything
512                 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
513                         (def.name == "air" && c != CONTENT_AIR)){
514                         errorstream<<"registerNode: IGNORING ERROR: "
515                                         <<"trying to register built-in type \""
516                                         <<def.name<<"\" as different id"<<std::endl;
517                         return;
518                 }
519                 m_content_features[c] = def;
520                 if(def.name != "")
521                         addNameIdMapping(c, def.name);
522
523                 // Add this content to the list of all groups it belongs to
524                 for (ItemGroupList::const_iterator i = def.groups.begin();
525                         i != def.groups.end(); ++i) {
526                         std::string group_name = i->first;
527                         
528                         std::map<std::string, GroupItems>::iterator
529                                 j = m_group_to_items.find(group_name);
530                         if (j == m_group_to_items.end()) {
531                                 m_group_to_items[group_name].push_back(std::make_pair(c, i->second));
532                         } else {
533                                 GroupItems &items = j->second;
534                                 items.push_back(std::make_pair(c, i->second));
535                         }
536                 }
537         }
538         virtual content_t set(const std::string &name,
539                         const ContentFeatures &def)
540         {
541                 assert(name == def.name);
542                 u16 id = CONTENT_IGNORE;
543                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
544                 if(!found){
545                         // Get some id
546                         id = getFreeId();
547                         if(id == CONTENT_IGNORE)
548                                 return CONTENT_IGNORE;
549                         if(name != "")
550                                 addNameIdMapping(id, name);
551                 }
552                 set(id, def);
553                 return id;
554         }
555         virtual content_t allocateDummy(const std::string &name)
556         {
557                 assert(name != "");
558                 ContentFeatures f;
559                 f.name = name;
560                 return set(name, f);
561         }
562         virtual void updateAliases(IItemDefManager *idef)
563         {
564                 std::set<std::string> all = idef->getAll();
565                 m_name_id_mapping_with_aliases.clear();
566                 for(std::set<std::string>::iterator
567                                 i = all.begin(); i != all.end(); i++)
568                 {
569                         std::string name = *i;
570                         std::string convert_to = idef->getAlias(name);
571                         content_t id;
572                         if(m_name_id_mapping.getId(convert_to, id))
573                         {
574                                 m_name_id_mapping_with_aliases.insert(
575                                                 std::make_pair(name, id));
576                         }
577                 }
578         }
579         virtual void updateTextures(ITextureSource *tsrc)
580         {
581 #ifndef SERVER
582                 infostream<<"CNodeDefManager::updateTextures(): Updating "
583                                 <<"textures in node definitions"<<std::endl;
584
585                 bool new_style_water = g_settings->getBool("new_style_water");
586                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
587                 bool opaque_water = g_settings->getBool("opaque_water");
588
589                 for(u16 i=0; i<=MAX_CONTENT; i++)
590                 {
591                         ContentFeatures *f = &m_content_features[i];
592
593                         // Figure out the actual tiles to use
594                         TileDef tiledef[6];
595                         for(u32 j=0; j<6; j++)
596                         {
597                                 tiledef[j] = f->tiledef[j];
598                                 if(tiledef[j].name == "")
599                                         tiledef[j].name = "unknown_node.png";
600                         }
601
602                         bool is_liquid = false;
603                         switch(f->drawtype){
604                         default:
605                         case NDT_NORMAL:
606                                 f->solidness = 2;
607                                 break;
608                         case NDT_AIRLIKE:
609                                 f->solidness = 0;
610                                 break;
611                         case NDT_LIQUID:
612                                 assert(f->liquid_type == LIQUID_SOURCE);
613                                 if(opaque_water)
614                                         f->alpha = 255;
615                                 if(new_style_water){
616                                         f->solidness = 0;
617                                 } else {
618                                         f->solidness = 1;
619                                         f->backface_culling = false;
620                                 }
621                                 is_liquid = true;
622                                 break;
623                         case NDT_FLOWINGLIQUID:
624                                 assert(f->liquid_type == LIQUID_FLOWING);
625                                 f->solidness = 0;
626                                 if(opaque_water)
627                                         f->alpha = 255;
628                                 is_liquid = true;
629                                 break;
630                         case NDT_GLASSLIKE:
631                                 f->solidness = 0;
632                                 f->visual_solidness = 1;
633                                 break;
634                         case NDT_GLASSLIKE_FRAMED:
635                                 f->solidness = 0;
636                                 f->visual_solidness = 1;
637                                 break;
638                         case NDT_ALLFACES:
639                                 f->solidness = 0;
640                                 f->visual_solidness = 1;
641                                 break;
642                         case NDT_ALLFACES_OPTIONAL:
643                                 if(new_style_leaves){
644                                         f->drawtype = NDT_ALLFACES;
645                                         f->solidness = 0;
646                                         f->visual_solidness = 1;
647                                 } else {
648                                         f->drawtype = NDT_NORMAL;
649                                         f->solidness = 2;
650                                         for(u32 i=0; i<6; i++){
651                                                 tiledef[i].name += std::string("^[noalpha");
652                                         }
653                                 }
654                                 break;
655                         case NDT_PLANTLIKE:
656                                 f->solidness = 0;
657                                 f->backface_culling = false;
658                                 break;
659                         case NDT_TORCHLIKE:
660                         case NDT_SIGNLIKE:
661                         case NDT_FENCELIKE:
662                         case NDT_RAILLIKE:
663                         case NDT_NODEBOX:
664                                 f->solidness = 0;
665                                 break;
666                         }
667
668                         u8 material_type;
669                         if (is_liquid)
670                                 material_type = (f->alpha == 255) ? TILE_MATERIAL_LIQUID_OPAQUE : TILE_MATERIAL_LIQUID_TRANSPARENT;
671                         else
672                                 material_type = (f->alpha == 255) ? TILE_MATERIAL_BASIC : TILE_MATERIAL_ALPHA;
673
674                         // Tiles (fill in f->tiles[])
675                         for(u16 j=0; j<6; j++){
676                                 // Texture
677                                 f->tiles[j].texture = tsrc->getTexture(
678                                                 tiledef[j].name,
679                                                 &f->tiles[j].texture_id);
680                                 // Alpha
681                                 f->tiles[j].alpha = f->alpha;
682                                 // Material type
683                                 f->tiles[j].material_type = material_type;
684                                 // Material flags
685                                 f->tiles[j].material_flags = 0;
686                                 if(f->backface_culling)
687                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
688                                 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
689                                         f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
690                                 // Animation parameters
691                                 if(f->tiles[j].material_flags &
692                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
693                                 {
694                                         // Get texture size to determine frame count by
695                                         // aspect ratio
696                                         v2u32 size = f->tiles[j].texture->getOriginalSize();
697                                         int frame_height = (float)size.X /
698                                                         (float)tiledef[j].animation.aspect_w *
699                                                         (float)tiledef[j].animation.aspect_h;
700                                         int frame_count = size.Y / frame_height;
701                                         int frame_length_ms = 1000.0 *
702                                                         tiledef[j].animation.length / frame_count;
703                                         f->tiles[j].animation_frame_count = frame_count;
704                                         f->tiles[j].animation_frame_length_ms = frame_length_ms;
705
706                                         // If there are no frames for an animation, switch
707                                         // animation off (so that having specified an animation
708                                         // for something but not using it in the texture pack
709                                         // gives no overhead)
710                                         if(frame_count == 1){
711                                                 f->tiles[j].material_flags &=
712                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
713                                         }
714                                 }
715                         }
716                         // Special tiles (fill in f->special_tiles[])
717                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
718                                 // Texture
719                                 f->special_tiles[j].texture = tsrc->getTexture(
720                                                 f->tiledef_special[j].name,
721                                                 &f->special_tiles[j].texture_id);
722                                 // Alpha
723                                 f->special_tiles[j].alpha = f->alpha;
724                                 // Material type
725                                 f->special_tiles[j].material_type = material_type;
726                                 // Material flags
727                                 f->special_tiles[j].material_flags = 0;
728                                 if(f->tiledef_special[j].backface_culling)
729                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
730                                 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
731                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
732                                 // Animation parameters
733                                 if(f->special_tiles[j].material_flags &
734                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
735                                 {
736                                         // Get texture size to determine frame count by
737                                         // aspect ratio
738                                         v2u32 size = f->special_tiles[j].texture->getOriginalSize();
739                                         int frame_height = (float)size.X /
740                                                         (float)f->tiledef_special[j].animation.aspect_w *
741                                                         (float)f->tiledef_special[j].animation.aspect_h;
742                                         int frame_count = size.Y / frame_height;
743                                         int frame_length_ms = 1000.0 *
744                                                         f->tiledef_special[j].animation.length / frame_count;
745                                         f->special_tiles[j].animation_frame_count = frame_count;
746                                         f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
747
748                                         // If there are no frames for an animation, switch
749                                         // animation off (so that having specified an animation
750                                         // for something but not using it in the texture pack
751                                         // gives no overhead)
752                                         if(frame_count == 1){
753                                                 f->special_tiles[j].material_flags &=
754                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
755                                         }
756                                 }
757                         }
758                 }
759 #endif
760         }
761         void serialize(std::ostream &os, u16 protocol_version)
762         {
763                 writeU8(os, 1); // version
764                 u16 count = 0;
765                 std::ostringstream os2(std::ios::binary);
766                 for(u16 i=0; i<=MAX_CONTENT; i++)
767                 {
768                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
769                                 continue;
770                         ContentFeatures *f = &m_content_features[i];
771                         if(f->name == "")
772                                 continue;
773                         writeU16(os2, i);
774                         // Wrap it in a string to allow different lengths without
775                         // strict version incompatibilities
776                         std::ostringstream wrapper_os(std::ios::binary);
777                         f->serialize(wrapper_os, protocol_version);
778                         os2<<serializeString(wrapper_os.str());
779                         count++;
780                 }
781                 writeU16(os, count);
782                 os<<serializeLongString(os2.str());
783         }
784         void deSerialize(std::istream &is)
785         {
786                 clear();
787                 int version = readU8(is);
788                 if(version != 1)
789                         throw SerializationError("unsupported NodeDefinitionManager version");
790                 u16 count = readU16(is);
791                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
792                 for(u16 n=0; n<count; n++){
793                         u16 i = readU16(is2);
794                         if(i > MAX_CONTENT){
795                                 errorstream<<"ContentFeatures::deSerialize(): "
796                                                 <<"Too large content id: "<<i<<std::endl;
797                                 continue;
798                         }
799                         /*// Do not deserialize special types
800                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
801                                 continue;*/
802                         ContentFeatures *f = &m_content_features[i];
803                         // Read it from the string wrapper
804                         std::string wrapper = deSerializeString(is2);
805                         std::istringstream wrapper_is(wrapper, std::ios::binary);
806                         f->deSerialize(wrapper_is);
807                         verbosestream<<"deserialized "<<f->name<<std::endl;
808                         if(f->name != "")
809                                 addNameIdMapping(i, f->name);
810                 }
811         }
812 private:
813         void addNameIdMapping(content_t i, std::string name)
814         {
815                 m_name_id_mapping.set(i, name);
816                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
817         }
818 private:
819         // Features indexed by id
820         ContentFeatures m_content_features[MAX_CONTENT+1];
821         // A mapping for fast converting back and forth between names and ids
822         NameIdMapping m_name_id_mapping;
823         // Like m_name_id_mapping, but only from names to ids, and includes
824         // item aliases too. Updated by updateAliases()
825         // Note: Not serialized.
826         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
827         // A mapping from groups to a list of content_ts (and their levels)
828         // that belong to it.  Necessary for a direct lookup in getIds().
829         // Note: Not serialized.
830         std::map<std::string, GroupItems> m_group_to_items;
831 };
832
833 IWritableNodeDefManager* createNodeDefManager()
834 {
835         return new CNodeDefManager();
836 }
837
838 /*
839         Serialization of old ContentFeatures formats
840 */
841
842 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
843 {
844         if(protocol_version == 13)
845         {
846                 writeU8(os, 5); // version
847                 os<<serializeString(name);
848                 writeU16(os, groups.size());
849                 for(ItemGroupList::const_iterator
850                                 i = groups.begin(); i != groups.end(); i++){
851                         os<<serializeString(i->first);
852                         writeS16(os, i->second);
853                 }
854                 writeU8(os, drawtype);
855                 writeF1000(os, visual_scale);
856                 writeU8(os, 6);
857                 for(u32 i=0; i<6; i++)
858                         tiledef[i].serialize(os, protocol_version);
859                 writeU8(os, CF_SPECIAL_COUNT);
860                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
861                         tiledef_special[i].serialize(os, protocol_version);
862                 }
863                 writeU8(os, alpha);
864                 writeU8(os, post_effect_color.getAlpha());
865                 writeU8(os, post_effect_color.getRed());
866                 writeU8(os, post_effect_color.getGreen());
867                 writeU8(os, post_effect_color.getBlue());
868                 writeU8(os, param_type);
869                 writeU8(os, param_type_2);
870                 writeU8(os, is_ground_content);
871                 writeU8(os, light_propagates);
872                 writeU8(os, sunlight_propagates);
873                 writeU8(os, walkable);
874                 writeU8(os, pointable);
875                 writeU8(os, diggable);
876                 writeU8(os, climbable);
877                 writeU8(os, buildable_to);
878                 os<<serializeString(""); // legacy: used to be metadata_name
879                 writeU8(os, liquid_type);
880                 os<<serializeString(liquid_alternative_flowing);
881                 os<<serializeString(liquid_alternative_source);
882                 writeU8(os, liquid_viscosity);
883                 writeU8(os, light_source);
884                 writeU32(os, damage_per_second);
885                 node_box.serialize(os);
886                 selection_box.serialize(os);
887                 writeU8(os, legacy_facedir_simple);
888                 writeU8(os, legacy_wallmounted);
889                 serializeSimpleSoundSpec(sound_footstep, os);
890                 serializeSimpleSoundSpec(sound_dig, os);
891                 serializeSimpleSoundSpec(sound_dug, os);
892         }
893         else
894         {
895                 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
896         }
897 }
898
899 void ContentFeatures::deSerializeOld(std::istream &is, int version)
900 {
901         if(version == 5) // In PROTOCOL_VERSION 13
902         {
903                 name = deSerializeString(is);
904                 groups.clear();
905                 u32 groups_size = readU16(is);
906                 for(u32 i=0; i<groups_size; i++){
907                         std::string name = deSerializeString(is);
908                         int value = readS16(is);
909                         groups[name] = value;
910                 }
911                 drawtype = (enum NodeDrawType)readU8(is);
912                 visual_scale = readF1000(is);
913                 if(readU8(is) != 6)
914                         throw SerializationError("unsupported tile count");
915                 for(u32 i=0; i<6; i++)
916                         tiledef[i].deSerialize(is);
917                 if(readU8(is) != CF_SPECIAL_COUNT)
918                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
919                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
920                         tiledef_special[i].deSerialize(is);
921                 alpha = readU8(is);
922                 post_effect_color.setAlpha(readU8(is));
923                 post_effect_color.setRed(readU8(is));
924                 post_effect_color.setGreen(readU8(is));
925                 post_effect_color.setBlue(readU8(is));
926                 param_type = (enum ContentParamType)readU8(is);
927                 param_type_2 = (enum ContentParamType2)readU8(is);
928                 is_ground_content = readU8(is);
929                 light_propagates = readU8(is);
930                 sunlight_propagates = readU8(is);
931                 walkable = readU8(is);
932                 pointable = readU8(is);
933                 diggable = readU8(is);
934                 climbable = readU8(is);
935                 buildable_to = readU8(is);
936                 deSerializeString(is); // legacy: used to be metadata_name
937                 liquid_type = (enum LiquidType)readU8(is);
938                 liquid_alternative_flowing = deSerializeString(is);
939                 liquid_alternative_source = deSerializeString(is);
940                 liquid_viscosity = readU8(is);
941                 light_source = readU8(is);
942                 damage_per_second = readU32(is);
943                 node_box.deSerialize(is);
944                 selection_box.deSerialize(is);
945                 legacy_facedir_simple = readU8(is);
946                 legacy_wallmounted = readU8(is);
947                 deSerializeSimpleSoundSpec(sound_footstep, is);
948                 deSerializeSimpleSoundSpec(sound_dig, is);
949                 deSerializeSimpleSoundSpec(sound_dug, is);
950         }
951         else
952         {
953                 throw SerializationError("unsupported ContentFeatures version");
954         }
955 }
956