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