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