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