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