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