Fix sound direction and add experimental:soundblock alias sb in minimal for testing
[oweals/minetest.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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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
31 /*
32         NodeBox
33 */
34
35 void NodeBox::serialize(std::ostream &os) const
36 {
37         writeU8(os, 0); // version
38         writeU8(os, type);
39         writeV3F1000(os, fixed.MinEdge);
40         writeV3F1000(os, fixed.MaxEdge);
41         writeV3F1000(os, wall_top.MinEdge);
42         writeV3F1000(os, wall_top.MaxEdge);
43         writeV3F1000(os, wall_bottom.MinEdge);
44         writeV3F1000(os, wall_bottom.MaxEdge);
45         writeV3F1000(os, wall_side.MinEdge);
46         writeV3F1000(os, wall_side.MaxEdge);
47 }
48
49 void NodeBox::deSerialize(std::istream &is)
50 {
51         int version = readU8(is);
52         if(version != 0)
53                 throw SerializationError("unsupported NodeBox version");
54         type = (enum NodeBoxType)readU8(is);
55         fixed.MinEdge = readV3F1000(is);
56         fixed.MaxEdge = readV3F1000(is);
57         wall_top.MinEdge = readV3F1000(is);
58         wall_top.MaxEdge = readV3F1000(is);
59         wall_bottom.MinEdge = readV3F1000(is);
60         wall_bottom.MaxEdge = readV3F1000(is);
61         wall_side.MinEdge = readV3F1000(is);
62         wall_side.MaxEdge = readV3F1000(is);
63 }
64
65 /*
66         MaterialSpec
67 */
68
69 void MaterialSpec::serialize(std::ostream &os) const
70 {
71         os<<serializeString(tname);
72         writeU8(os, backface_culling);
73 }
74
75 void MaterialSpec::deSerialize(std::istream &is)
76 {
77         tname = deSerializeString(is);
78         backface_culling = readU8(is);
79 }
80
81 /*
82         SimpleSoundSpec serialization
83 */
84
85 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
86                 std::ostream &os)
87 {
88         os<<serializeString(ss.name);
89         writeF1000(os, ss.gain);
90 }
91 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
92 {
93         ss.name = deSerializeString(is);
94         ss.gain = readF1000(is);
95 }
96
97 /*
98         ContentFeatures
99 */
100
101 ContentFeatures::ContentFeatures()
102 {
103         reset();
104 }
105
106 ContentFeatures::~ContentFeatures()
107 {
108 }
109
110 void ContentFeatures::reset()
111 {
112         /*
113                 Cached stuff
114         */
115 #ifndef SERVER
116         solidness = 2;
117         visual_solidness = 0;
118         backface_culling = true;
119 #endif
120         /*
121                 Actual data
122                 
123                 NOTE: Most of this is always overridden by the default values given
124                       in builtin.lua
125         */
126         name = "";
127         groups.clear();
128         // Unknown nodes can be dug
129         groups["dig_immediate"] = 2;
130         drawtype = NDT_NORMAL;
131         visual_scale = 1.0;
132         for(u32 i=0; i<6; i++)
133                 tname_tiles[i] = "";
134         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
135                 mspec_special[j] = MaterialSpec();
136         alpha = 255;
137         post_effect_color = video::SColor(0, 0, 0, 0);
138         param_type = CPT_NONE;
139         param_type_2 = CPT2_NONE;
140         is_ground_content = false;
141         light_propagates = false;
142         sunlight_propagates = false;
143         walkable = true;
144         pointable = true;
145         diggable = true;
146         climbable = false;
147         buildable_to = false;
148         metadata_name = "";
149         liquid_type = LIQUID_NONE;
150         liquid_alternative_flowing = "";
151         liquid_alternative_source = "";
152         liquid_viscosity = 0;
153         light_source = 0;
154         damage_per_second = 0;
155         selection_box = NodeBox();
156         legacy_facedir_simple = false;
157         legacy_wallmounted = false;
158         sound_footstep = SimpleSoundSpec();
159         sound_dig = SimpleSoundSpec("__group");
160         sound_dug = SimpleSoundSpec();
161 }
162
163 void ContentFeatures::serialize(std::ostream &os)
164 {
165         writeU8(os, 3); // version
166         os<<serializeString(name);
167         writeU16(os, groups.size());
168         for(ItemGroupList::const_iterator
169                         i = groups.begin(); i != groups.end(); i++){
170                 os<<serializeString(i->first);
171                 writeS16(os, i->second);
172         }
173         writeU8(os, drawtype);
174         writeF1000(os, visual_scale);
175         writeU8(os, 6);
176         for(u32 i=0; i<6; i++)
177                 os<<serializeString(tname_tiles[i]);
178         writeU8(os, CF_SPECIAL_COUNT);
179         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
180                 mspec_special[i].serialize(os);
181         }
182         writeU8(os, alpha);
183         writeU8(os, post_effect_color.getAlpha());
184         writeU8(os, post_effect_color.getRed());
185         writeU8(os, post_effect_color.getGreen());
186         writeU8(os, post_effect_color.getBlue());
187         writeU8(os, param_type);
188         writeU8(os, param_type_2);
189         writeU8(os, is_ground_content);
190         writeU8(os, light_propagates);
191         writeU8(os, sunlight_propagates);
192         writeU8(os, walkable);
193         writeU8(os, pointable);
194         writeU8(os, diggable);
195         writeU8(os, climbable);
196         writeU8(os, buildable_to);
197         os<<serializeString(metadata_name);
198         writeU8(os, liquid_type);
199         os<<serializeString(liquid_alternative_flowing);
200         os<<serializeString(liquid_alternative_source);
201         writeU8(os, liquid_viscosity);
202         writeU8(os, light_source);
203         writeU32(os, damage_per_second);
204         selection_box.serialize(os);
205         writeU8(os, legacy_facedir_simple);
206         writeU8(os, legacy_wallmounted);
207         serializeSimpleSoundSpec(sound_footstep, os);
208         serializeSimpleSoundSpec(sound_dig, os);
209         serializeSimpleSoundSpec(sound_dug, os);
210 }
211
212 void ContentFeatures::deSerialize(std::istream &is)
213 {
214         int version = readU8(is);
215         if(version != 3)
216                 throw SerializationError("unsupported ContentFeatures version");
217         name = deSerializeString(is);
218         groups.clear();
219         u32 groups_size = readU16(is);
220         for(u32 i=0; i<groups_size; i++){
221                 std::string name = deSerializeString(is);
222                 int value = readS16(is);
223                 groups[name] = value;
224         }
225         drawtype = (enum NodeDrawType)readU8(is);
226         visual_scale = readF1000(is);
227         if(readU8(is) != 6)
228                 throw SerializationError("unsupported tile count");
229         for(u32 i=0; i<6; i++)
230                 tname_tiles[i] = deSerializeString(is);
231         if(readU8(is) != CF_SPECIAL_COUNT)
232                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
233         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
234                 mspec_special[i].deSerialize(is);
235         }
236         alpha = readU8(is);
237         post_effect_color.setAlpha(readU8(is));
238         post_effect_color.setRed(readU8(is));
239         post_effect_color.setGreen(readU8(is));
240         post_effect_color.setBlue(readU8(is));
241         param_type = (enum ContentParamType)readU8(is);
242         param_type_2 = (enum ContentParamType2)readU8(is);
243         is_ground_content = readU8(is);
244         light_propagates = readU8(is);
245         sunlight_propagates = readU8(is);
246         walkable = readU8(is);
247         pointable = readU8(is);
248         diggable = readU8(is);
249         climbable = readU8(is);
250         buildable_to = readU8(is);
251         metadata_name = deSerializeString(is);
252         liquid_type = (enum LiquidType)readU8(is);
253         liquid_alternative_flowing = deSerializeString(is);
254         liquid_alternative_source = deSerializeString(is);
255         liquid_viscosity = readU8(is);
256         light_source = readU8(is);
257         damage_per_second = readU32(is);
258         selection_box.deSerialize(is);
259         legacy_facedir_simple = readU8(is);
260         legacy_wallmounted = readU8(is);
261         // If you add anything here, insert it primarily inside the try-catch
262         // block to not need to increase the version.
263         try{
264                 deSerializeSimpleSoundSpec(sound_footstep, is);
265                 deSerializeSimpleSoundSpec(sound_dig, is);
266                 deSerializeSimpleSoundSpec(sound_dug, is);
267         }catch(SerializationError &e) {};
268 }
269
270 /*
271         CNodeDefManager
272 */
273
274 class CNodeDefManager: public IWritableNodeDefManager
275 {
276 public:
277         void clear()
278         {
279                 m_name_id_mapping.clear();
280                 m_name_id_mapping_with_aliases.clear();
281
282                 for(u16 i=0; i<=MAX_CONTENT; i++)
283                 {
284                         ContentFeatures &f = m_content_features[i];
285                         f.reset(); // Reset to defaults
286                 }
287                 
288                 // Set CONTENT_AIR
289                 {
290                         ContentFeatures f;
291                         f.name = "air";
292                         f.drawtype = NDT_AIRLIKE;
293                         f.param_type = CPT_LIGHT;
294                         f.light_propagates = true;
295                         f.sunlight_propagates = true;
296                         f.walkable = false;
297                         f.pointable = false;
298                         f.diggable = false;
299                         f.buildable_to = true;
300                         // Insert directly into containers
301                         content_t c = CONTENT_AIR;
302                         m_content_features[c] = f;
303                         addNameIdMapping(c, f.name);
304                 }
305                 // Set CONTENT_IGNORE
306                 {
307                         ContentFeatures f;
308                         f.name = "ignore";
309                         f.drawtype = NDT_AIRLIKE;
310                         f.param_type = CPT_NONE;
311                         f.light_propagates = false;
312                         f.sunlight_propagates = false;
313                         f.walkable = false;
314                         f.pointable = false;
315                         f.diggable = false;
316                         // A way to remove accidental CONTENT_IGNOREs
317                         f.buildable_to = true;
318                         // Insert directly into containers
319                         content_t c = CONTENT_IGNORE;
320                         m_content_features[c] = f;
321                         addNameIdMapping(c, f.name);
322                 }
323         }
324         // CONTENT_IGNORE = not found
325         content_t getFreeId(bool require_full_param2)
326         {
327                 // If allowed, first search in the large 4-bit-param2 pool
328                 if(!require_full_param2){
329                         for(u16 i=0x800; i<=0xfff; i++){
330                                 const ContentFeatures &f = m_content_features[i];
331                                 if(f.name == "")
332                                         return i;
333                         }
334                 }
335                 // Then search from the small 8-bit-param2 pool
336                 for(u16 i=0; i<=125; i++){
337                         const ContentFeatures &f = m_content_features[i];
338                         if(f.name == "")
339                                 return i;
340                 }
341                 return CONTENT_IGNORE;
342         }
343         CNodeDefManager()
344         {
345                 clear();
346         }
347         virtual ~CNodeDefManager()
348         {
349         }
350         virtual IWritableNodeDefManager* clone()
351         {
352                 CNodeDefManager *mgr = new CNodeDefManager();
353                 for(u16 i=0; i<=MAX_CONTENT; i++)
354                 {
355                         mgr->set(i, get(i));
356                 }
357                 return mgr;
358         }
359         virtual const ContentFeatures& get(content_t c) const
360         {
361                 assert(c <= MAX_CONTENT);
362                 return m_content_features[c];
363         }
364         virtual const ContentFeatures& get(const MapNode &n) const
365         {
366                 return get(n.getContent());
367         }
368         virtual bool getId(const std::string &name, content_t &result) const
369         {
370                 std::map<std::string, content_t>::const_iterator
371                         i = m_name_id_mapping_with_aliases.find(name);
372                 if(i == m_name_id_mapping_with_aliases.end())
373                         return false;
374                 result = i->second;
375                 return true;
376         }
377         virtual content_t getId(const std::string &name) const
378         {
379                 content_t id = CONTENT_IGNORE;
380                 getId(name, id);
381                 return id;
382         }
383         virtual void getIds(const std::string &name, std::set<content_t> &result)
384                         const
385         {
386                 if(name.substr(0,6) != "group:"){
387                         content_t id = CONTENT_IGNORE;
388                         if(getId(name, id))
389                                 result.insert(id);
390                         return;
391                 }
392                 std::string group = name.substr(6);
393                 for(u16 id=0; id<=MAX_CONTENT; id++)
394                 {
395                         const ContentFeatures &f = m_content_features[id];
396                         if(f.name == "") // Quickly discard undefined nodes
397                                 continue;
398                         if(itemgroup_get(f.groups, group) != 0)
399                                 result.insert(id);
400                 }
401         }
402         virtual const ContentFeatures& get(const std::string &name) const
403         {
404                 content_t id = CONTENT_IGNORE;
405                 getId(name, id);
406                 return get(id);
407         }
408         // IWritableNodeDefManager
409         virtual void set(content_t c, const ContentFeatures &def)
410         {
411                 verbosestream<<"registerNode: registering content id \""<<c
412                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
413                 assert(c <= MAX_CONTENT);
414                 // Don't allow redefining CONTENT_IGNORE (but allow air)
415                 if(def.name == "ignore" || c == CONTENT_IGNORE){
416                         infostream<<"registerNode: WARNING: Ignoring "
417                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
418                         return;
419                 }
420                 // Check that the special contents are not redefined as different id
421                 // because it would mess up everything
422                 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
423                         (def.name == "air" && c != CONTENT_AIR)){
424                         errorstream<<"registerNode: IGNORING ERROR: "
425                                         <<"trying to register built-in type \""
426                                         <<def.name<<"\" as different id"<<std::endl;
427                         return;
428                 }
429                 m_content_features[c] = def;
430                 if(def.name != "")
431                         addNameIdMapping(c, def.name);
432         }
433         virtual content_t set(const std::string &name,
434                         const ContentFeatures &def)
435         {
436                 assert(name == def.name);
437                 u16 id = CONTENT_IGNORE;
438                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
439                 if(!found){
440                         // Determine if full param2 is required
441                         bool require_full_param2 = (
442                                 def.param_type_2 == CPT2_FULL
443                                 ||
444                                 def.param_type_2 == CPT2_FLOWINGLIQUID
445                                 ||
446                                 def.legacy_wallmounted
447                         );
448                         // Get some id
449                         id = getFreeId(require_full_param2);
450                         if(id == CONTENT_IGNORE)
451                                 return CONTENT_IGNORE;
452                         if(name != "")
453                                 addNameIdMapping(id, name);
454                 }
455                 set(id, def);
456                 return id;
457         }
458         virtual content_t allocateDummy(const std::string &name)
459         {
460                 assert(name != "");
461                 ContentFeatures f;
462                 f.name = name;
463                 return set(name, f);
464         }
465         virtual void updateAliases(IItemDefManager *idef)
466         {
467                 std::set<std::string> all = idef->getAll();
468                 m_name_id_mapping_with_aliases.clear();
469                 for(std::set<std::string>::iterator
470                                 i = all.begin(); i != all.end(); i++)
471                 {
472                         std::string name = *i;
473                         std::string convert_to = idef->getAlias(name);
474                         content_t id;
475                         if(m_name_id_mapping.getId(convert_to, id))
476                         {
477                                 m_name_id_mapping_with_aliases.insert(
478                                                 std::make_pair(name, id));
479                         }
480                 }
481         }
482         virtual void updateTextures(ITextureSource *tsrc)
483         {
484 #ifndef SERVER
485                 infostream<<"CNodeDefManager::updateTextures(): Updating "
486                                 <<"textures in node definitions"<<std::endl;
487
488                 bool new_style_water = g_settings->getBool("new_style_water");
489                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
490                 bool opaque_water = g_settings->getBool("opaque_water");
491                 
492                 for(u16 i=0; i<=MAX_CONTENT; i++)
493                 {
494                         ContentFeatures *f = &m_content_features[i];
495
496                         std::string tname_tiles[6];
497                         for(u32 j=0; j<6; j++)
498                         {
499                                 tname_tiles[j] = f->tname_tiles[j];
500                                 if(tname_tiles[j] == "")
501                                         tname_tiles[j] = "unknown_block.png";
502                         }
503
504                         switch(f->drawtype){
505                         default:
506                         case NDT_NORMAL:
507                                 f->solidness = 2;
508                                 break;
509                         case NDT_AIRLIKE:
510                                 f->solidness = 0;
511                                 break;
512                         case NDT_LIQUID:
513                                 assert(f->liquid_type == LIQUID_SOURCE);
514                                 if(opaque_water)
515                                         f->alpha = 255;
516                                 if(new_style_water){
517                                         f->solidness = 0;
518                                 } else {
519                                         f->solidness = 1;
520                                         if(f->alpha == 255)
521                                                 f->solidness = 2;
522                                         f->backface_culling = false;
523                                 }
524                                 break;
525                         case NDT_FLOWINGLIQUID:
526                                 assert(f->liquid_type == LIQUID_FLOWING);
527                                 f->solidness = 0;
528                                 if(opaque_water)
529                                         f->alpha = 255;
530                                 break;
531                         case NDT_GLASSLIKE:
532                                 f->solidness = 0;
533                                 f->visual_solidness = 1;
534                                 break;
535                         case NDT_ALLFACES:
536                                 f->solidness = 0;
537                                 f->visual_solidness = 1;
538                                 break;
539                         case NDT_ALLFACES_OPTIONAL:
540                                 if(new_style_leaves){
541                                         f->drawtype = NDT_ALLFACES;
542                                         f->solidness = 0;
543                                         f->visual_solidness = 1;
544                                 } else {
545                                         f->drawtype = NDT_NORMAL;
546                                         f->solidness = 2;
547                                         for(u32 i=0; i<6; i++){
548                                                 tname_tiles[i] += std::string("^[noalpha");
549                                         }
550                                 }
551                                 break;
552                         case NDT_TORCHLIKE:
553                         case NDT_SIGNLIKE:
554                         case NDT_PLANTLIKE:
555                         case NDT_FENCELIKE:
556                         case NDT_RAILLIKE:
557                                 f->solidness = 0;
558                                 break;
559                         }
560
561                         // Tile textures
562                         for(u16 j=0; j<6; j++){
563                                 f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]);
564                                 f->tiles[j].alpha = f->alpha;
565                                 if(f->alpha == 255)
566                                         f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
567                                 else
568                                         f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
569                                 f->tiles[j].material_flags = 0;
570                                 if(f->backface_culling)
571                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
572                         }
573                         // Special tiles
574                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
575                                 f->special_tiles[j].texture = tsrc->getTexture(f->mspec_special[j].tname);
576                                 f->special_tiles[j].alpha = f->alpha;
577                                 if(f->alpha == 255)
578                                         f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
579                                 else
580                                         f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
581                                 f->special_tiles[j].material_flags = 0;
582                                 if(f->mspec_special[j].backface_culling)
583                                         f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
584                         }
585                 }
586 #endif
587         }
588         void serialize(std::ostream &os)
589         {
590                 writeU8(os, 1); // version
591                 u16 count = 0;
592                 std::ostringstream os2(std::ios::binary);
593                 for(u16 i=0; i<=MAX_CONTENT; i++)
594                 {
595                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
596                                 continue;
597                         ContentFeatures *f = &m_content_features[i];
598                         if(f->name == "")
599                                 continue;
600                         writeU16(os2, i);
601                         // Wrap it in a string to allow different lengths without
602                         // strict version incompatibilities
603                         std::ostringstream wrapper_os(std::ios::binary);
604                         f->serialize(wrapper_os);
605                         os2<<serializeString(wrapper_os.str());
606                         count++;
607                 }
608                 writeU16(os, count);
609                 os<<serializeLongString(os2.str());
610         }
611         void deSerialize(std::istream &is)
612         {
613                 clear();
614                 int version = readU8(is);
615                 if(version != 1)
616                         throw SerializationError("unsupported NodeDefinitionManager version");
617                 u16 count = readU16(is);
618                 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
619                 for(u16 n=0; n<count; n++){
620                         u16 i = readU16(is2);
621                         if(i > MAX_CONTENT){
622                                 errorstream<<"ContentFeatures::deSerialize(): "
623                                                 <<"Too large content id: "<<i<<std::endl;
624                                 continue;
625                         }
626                         /*// Do not deserialize special types
627                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
628                                 continue;*/
629                         ContentFeatures *f = &m_content_features[i];
630                         // Read it from the string wrapper
631                         std::string wrapper = deSerializeString(is2);
632                         std::istringstream wrapper_is(wrapper, std::ios::binary);
633                         f->deSerialize(wrapper_is);
634                         if(f->name != "")
635                                 addNameIdMapping(i, f->name);
636                 }
637         }
638 private:
639         void addNameIdMapping(content_t i, std::string name)
640         {
641                 m_name_id_mapping.set(i, name);
642                 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
643         }
644 private:
645         // Features indexed by id
646         ContentFeatures m_content_features[MAX_CONTENT+1];
647         // A mapping for fast converting back and forth between names and ids
648         NameIdMapping m_name_id_mapping;
649         // Like m_name_id_mapping, but only from names to ids, and includes
650         // item aliases too. Updated by updateAliases()
651         // Note: Not serialized.
652         std::map<std::string, content_t> m_name_id_mapping_with_aliases;
653 };
654
655 IWritableNodeDefManager* createNodeDefManager()
656 {
657         return new CNodeDefManager();
658 }
659