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