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