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