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