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