]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodedef.cpp
ca889890781128a34da81eb0dc0a8a05dc159bc9
[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_block.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 = 0;
662                         if(is_liquid){
663                                 if(f->alpha == 255)
664                                         material_type = TILE_MATERIAL_LIQUID_OPAQUE;
665                                 else
666                                         material_type = TILE_MATERIAL_LIQUID_TRANSPARENT;
667                         } else{
668                                 material_type = TILE_MATERIAL_BASIC;
669                         }
670
671                         // Tiles (fill in f->tiles[])
672                         for(u16 j=0; j<6; j++){
673                                 // Texture
674                                 f->tiles[j].texture = tsrc->getTexture(tiledef[j].name);
675                                 // Alpha
676                                 f->tiles[j].alpha = f->alpha;
677                                 // Material type
678                                 f->tiles[j].material_type = material_type;
679                                 // Material flags
680                                 f->tiles[j].material_flags = 0;
681                                 if(f->backface_culling)
682                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
683                                 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
684                                         f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
685                                 // Animation parameters
686                                 if(f->tiles[j].material_flags &
687                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
688                                 {
689                                         // Get raw texture size to determine frame count by
690                                         // aspect ratio
691                                         video::ITexture *t = tsrc->getTextureRaw(tiledef[j].name);
692                                         v2u32 size = t->getOriginalSize();
693                                         int frame_height = (float)size.X /
694                                                         (float)tiledef[j].animation.aspect_w *
695                                                         (float)tiledef[j].animation.aspect_h;
696                                         int frame_count = size.Y / frame_height;
697                                         int frame_length_ms = 1000.0 *
698                                                         tiledef[j].animation.length / frame_count;
699                                         f->tiles[j].animation_frame_count = frame_count;
700                                         f->tiles[j].animation_frame_length_ms = frame_length_ms;
701
702                                         // If there are no frames for an animation, switch
703                                         // animation off (so that having specified an animation
704                                         // for something but not using it in the texture pack
705                                         // gives no overhead)
706                                         if(frame_count == 1){
707                                                 f->tiles[j].material_flags &=
708                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
709                                         }
710                                 }
711                         }
712                         // Special tiles (fill in f->special_tiles[])
713                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
714                                 // Texture
715                                 f->special_tiles[j].texture =
716                                                 tsrc->getTexture(f->tiledef_special[j].name);
717                                 // Alpha
718                                 f->special_tiles[j].alpha = f->alpha;
719                                 // Material type
720                                 f->special_tiles[j].material_type = material_type;
721                                 // Material flags
722                                 f->special_tiles[j].material_flags = 0;
723                                 if(f->tiledef_special[j].backface_culling)
724                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
725                                 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
726                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
727                                 // Animation parameters
728                                 if(f->special_tiles[j].material_flags &
729                                                 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
730                                 {
731                                         // Get raw texture size to determine frame count by
732                                         // aspect ratio
733                                         video::ITexture *t = tsrc->getTextureRaw(f->tiledef_special[j].name);
734                                         v2u32 size = t->getOriginalSize();
735                                         int frame_height = (float)size.X /
736                                                         (float)f->tiledef_special[j].animation.aspect_w *
737                                                         (float)f->tiledef_special[j].animation.aspect_h;
738                                         int frame_count = size.Y / frame_height;
739                                         int frame_length_ms = 1000.0 *
740                                                         f->tiledef_special[j].animation.length / frame_count;
741                                         f->special_tiles[j].animation_frame_count = frame_count;
742                                         f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
743
744                                         // If there are no frames for an animation, switch
745                                         // animation off (so that having specified an animation
746                                         // for something but not using it in the texture pack
747                                         // gives no overhead)
748                                         if(frame_count == 1){
749                                                 f->special_tiles[j].material_flags &=
750                                                                 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
751                                         }
752                                 }
753                         }
754                 }
755 #endif
756         }
757         void serialize(std::ostream &os, u16 protocol_version)
758         {
759                 writeU8(os, 1); // version
760                 u16 count = 0;
761                 std::ostringstream os2(std::ios::binary);
762                 for(u16 i=0; i<=MAX_CONTENT; i++)
763                 {
764                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
765                                 continue;
766                         ContentFeatures *f = &m_content_features[i];
767                         if(f->name == "")
768                                 continue;
769                         writeU16(os2, i);
770                         // Wrap it in a string to allow different lengths without
771                         // strict version incompatibilities
772                         std::ostringstream wrapper_os(std::ios::binary);
773                         f->serialize(wrapper_os, protocol_version);
774                         os2<<serializeString(wrapper_os.str());
775                         count++;
776                 }
777                 writeU16(os, count);
778                 os<<serializeLongString(os2.str());
779         }
780         void deSerialize(std::istream &is)
781         {
782                 clear();
783                 int version = readU8(is);
784                 if(version != 1)
785                         throw SerializationError("unsupported NodeDefinitionManager version");
786                 u16 count = readU16(is);
787                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
788                 for(u16 n=0; n<count; n++){
789                         u16 i = readU16(is2);
790                         if(i > MAX_CONTENT){
791                                 errorstream<<"ContentFeatures::deSerialize(): "
792                                                 <<"Too large content id: "<<i<<std::endl;
793                                 continue;
794                         }
795                         /*// Do not deserialize special types
796                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
797                                 continue;*/
798                         ContentFeatures *f = &m_content_features[i];
799                         // Read it from the string wrapper
800                         std::string wrapper = deSerializeString(is2);
801                         std::istringstream wrapper_is(wrapper, std::ios::binary);
802                         f->deSerialize(wrapper_is);
803                         verbosestream<<"deserialized "<<f->name<<std::endl;
804                         if(f->name != "")
805                                 addNameIdMapping(i, f->name);
806                 }
807         }
808 private:
809         void addNameIdMapping(content_t i, std::string name)
810         {
811                 m_name_id_mapping.set(i, name);
812                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
813         }
814 private:
815         // Features indexed by id
816         ContentFeatures m_content_features[MAX_CONTENT+1];
817         // A mapping for fast converting back and forth between names and ids
818         NameIdMapping m_name_id_mapping;
819         // Like m_name_id_mapping, but only from names to ids, and includes
820         // item aliases too. Updated by updateAliases()
821         // Note: Not serialized.
822         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
823         // A mapping from groups to a list of content_ts (and their levels)
824         // that belong to it.  Necessary for a direct lookup in getIds().
825         // Note: Not serialized.
826         std::map<std::string, GroupItems> m_group_to_items;
827 };
828
829 IWritableNodeDefManager* createNodeDefManager()
830 {
831         return new CNodeDefManager();
832 }
833
834 /*
835         Serialization of old ContentFeatures formats
836 */
837
838 void ContentFeatures::serializeOld(std::ostream &os, u16 protocol_version)
839 {
840         if(protocol_version == 13)
841         {
842                 writeU8(os, 5); // version
843                 os<<serializeString(name);
844                 writeU16(os, groups.size());
845                 for(ItemGroupList::const_iterator
846                                 i = groups.begin(); i != groups.end(); i++){
847                         os<<serializeString(i->first);
848                         writeS16(os, i->second);
849                 }
850                 writeU8(os, drawtype);
851                 writeF1000(os, visual_scale);
852                 writeU8(os, 6);
853                 for(u32 i=0; i<6; i++)
854                         tiledef[i].serialize(os, protocol_version);
855                 writeU8(os, CF_SPECIAL_COUNT);
856                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
857                         tiledef_special[i].serialize(os, protocol_version);
858                 }
859                 writeU8(os, alpha);
860                 writeU8(os, post_effect_color.getAlpha());
861                 writeU8(os, post_effect_color.getRed());
862                 writeU8(os, post_effect_color.getGreen());
863                 writeU8(os, post_effect_color.getBlue());
864                 writeU8(os, param_type);
865                 writeU8(os, param_type_2);
866                 writeU8(os, is_ground_content);
867                 writeU8(os, light_propagates);
868                 writeU8(os, sunlight_propagates);
869                 writeU8(os, walkable);
870                 writeU8(os, pointable);
871                 writeU8(os, diggable);
872                 writeU8(os, climbable);
873                 writeU8(os, buildable_to);
874                 os<<serializeString(""); // legacy: used to be metadata_name
875                 writeU8(os, liquid_type);
876                 os<<serializeString(liquid_alternative_flowing);
877                 os<<serializeString(liquid_alternative_source);
878                 writeU8(os, liquid_viscosity);
879                 writeU8(os, light_source);
880                 writeU32(os, damage_per_second);
881                 node_box.serialize(os);
882                 selection_box.serialize(os);
883                 writeU8(os, legacy_facedir_simple);
884                 writeU8(os, legacy_wallmounted);
885                 serializeSimpleSoundSpec(sound_footstep, os);
886                 serializeSimpleSoundSpec(sound_dig, os);
887                 serializeSimpleSoundSpec(sound_dug, os);
888         }
889         else
890         {
891                 throw SerializationError("ContentFeatures::serialize(): Unsupported version requested");
892         }
893 }
894
895 void ContentFeatures::deSerializeOld(std::istream &is, int version)
896 {
897         if(version == 5) // In PROTOCOL_VERSION 13
898         {
899                 name = deSerializeString(is);
900                 groups.clear();
901                 u32 groups_size = readU16(is);
902                 for(u32 i=0; i<groups_size; i++){
903                         std::string name = deSerializeString(is);
904                         int value = readS16(is);
905                         groups[name] = value;
906                 }
907                 drawtype = (enum NodeDrawType)readU8(is);
908                 visual_scale = readF1000(is);
909                 if(readU8(is) != 6)
910                         throw SerializationError("unsupported tile count");
911                 for(u32 i=0; i<6; i++)
912                         tiledef[i].deSerialize(is);
913                 if(readU8(is) != CF_SPECIAL_COUNT)
914                         throw SerializationError("unsupported CF_SPECIAL_COUNT");
915                 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
916                         tiledef_special[i].deSerialize(is);
917                 alpha = readU8(is);
918                 post_effect_color.setAlpha(readU8(is));
919                 post_effect_color.setRed(readU8(is));
920                 post_effect_color.setGreen(readU8(is));
921                 post_effect_color.setBlue(readU8(is));
922                 param_type = (enum ContentParamType)readU8(is);
923                 param_type_2 = (enum ContentParamType2)readU8(is);
924                 is_ground_content = readU8(is);
925                 light_propagates = readU8(is);
926                 sunlight_propagates = readU8(is);
927                 walkable = readU8(is);
928                 pointable = readU8(is);
929                 diggable = readU8(is);
930                 climbable = readU8(is);
931                 buildable_to = readU8(is);
932                 deSerializeString(is); // legacy: used to be metadata_name
933                 liquid_type = (enum LiquidType)readU8(is);
934                 liquid_alternative_flowing = deSerializeString(is);
935                 liquid_alternative_source = deSerializeString(is);
936                 liquid_viscosity = readU8(is);
937                 light_source = readU8(is);
938                 damage_per_second = readU32(is);
939                 node_box.deSerialize(is);
940                 selection_box.deSerialize(is);
941                 legacy_facedir_simple = readU8(is);
942                 legacy_wallmounted = readU8(is);
943                 deSerializeSimpleSoundSpec(sound_footstep, is);
944                 deSerializeSimpleSoundSpec(sound_dig, is);
945                 deSerializeSimpleSoundSpec(sound_dug, is);
946         }
947         else
948         {
949                 throw SerializationError("unsupported ContentFeatures version");
950         }
951 }
952