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