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