itemgroup.h and ItemGroupList typedef
[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         groups.clear();
122         drawtype = NDT_NORMAL;
123         visual_scale = 1.0;
124         for(u32 i=0; i<6; i++)
125                 tname_tiles[i] = "";
126         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
127                 mspec_special[j] = MaterialSpec();
128         alpha = 255;
129         post_effect_color = video::SColor(0, 0, 0, 0);
130         param_type = CPT_NONE;
131         param_type_2 = CPT2_NONE;
132         is_ground_content = false;
133         light_propagates = false;
134         sunlight_propagates = false;
135         walkable = true;
136         pointable = true;
137         diggable = true;
138         climbable = false;
139         buildable_to = false;
140         metadata_name = "";
141         liquid_type = LIQUID_NONE;
142         liquid_alternative_flowing = "";
143         liquid_alternative_source = "";
144         liquid_viscosity = 0;
145         light_source = 0;
146         damage_per_second = 0;
147         selection_box = NodeBox();
148         legacy_facedir_simple = false;
149         legacy_wallmounted = false;
150 }
151
152 void ContentFeatures::serialize(std::ostream &os)
153 {
154         writeU8(os, 2); // version
155         os<<serializeString(name);
156         writeU16(os, groups.size());
157         for(ItemGroupList::const_iterator
158                         i = groups.begin(); i != groups.end(); i++){
159                 os<<serializeString(i->first);
160                 writeS16(os, i->second);
161         }
162         writeU8(os, drawtype);
163         writeF1000(os, visual_scale);
164         writeU8(os, 6);
165         for(u32 i=0; i<6; i++)
166                 os<<serializeString(tname_tiles[i]);
167         writeU8(os, CF_SPECIAL_COUNT);
168         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
169                 mspec_special[i].serialize(os);
170         }
171         writeU8(os, alpha);
172         writeU8(os, post_effect_color.getAlpha());
173         writeU8(os, post_effect_color.getRed());
174         writeU8(os, post_effect_color.getGreen());
175         writeU8(os, post_effect_color.getBlue());
176         writeU8(os, param_type);
177         writeU8(os, param_type_2);
178         writeU8(os, is_ground_content);
179         writeU8(os, light_propagates);
180         writeU8(os, sunlight_propagates);
181         writeU8(os, walkable);
182         writeU8(os, pointable);
183         writeU8(os, diggable);
184         writeU8(os, climbable);
185         writeU8(os, buildable_to);
186         os<<serializeString(metadata_name);
187         writeU8(os, liquid_type);
188         os<<serializeString(liquid_alternative_flowing);
189         os<<serializeString(liquid_alternative_source);
190         writeU8(os, liquid_viscosity);
191         writeU8(os, light_source);
192         writeU32(os, damage_per_second);
193         selection_box.serialize(os);
194         writeU8(os, legacy_facedir_simple);
195         writeU8(os, legacy_wallmounted);
196 }
197
198 void ContentFeatures::deSerialize(std::istream &is)
199 {
200         int version = readU8(is);
201         if(version != 2)
202                 throw SerializationError("unsupported ContentFeatures version");
203         name = deSerializeString(is);
204         groups.clear();
205         u32 groups_size = readU16(is);
206         for(u32 i=0; i<groups_size; i++){
207                 std::string name = deSerializeString(is);
208                 int value = readS16(is);
209                 groups[name] = value;
210         }
211         drawtype = (enum NodeDrawType)readU8(is);
212         visual_scale = readF1000(is);
213         if(readU8(is) != 6)
214                 throw SerializationError("unsupported tile count");
215         for(u32 i=0; i<6; i++)
216                 tname_tiles[i] = deSerializeString(is);
217         if(readU8(is) != CF_SPECIAL_COUNT)
218                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
219         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
220                 mspec_special[i].deSerialize(is);
221         }
222         alpha = readU8(is);
223         post_effect_color.setAlpha(readU8(is));
224         post_effect_color.setRed(readU8(is));
225         post_effect_color.setGreen(readU8(is));
226         post_effect_color.setBlue(readU8(is));
227         param_type = (enum ContentParamType)readU8(is);
228         param_type_2 = (enum ContentParamType2)readU8(is);
229         is_ground_content = readU8(is);
230         light_propagates = readU8(is);
231         sunlight_propagates = readU8(is);
232         walkable = readU8(is);
233         pointable = readU8(is);
234         diggable = readU8(is);
235         climbable = readU8(is);
236         buildable_to = readU8(is);
237         metadata_name = deSerializeString(is);
238         liquid_type = (enum LiquidType)readU8(is);
239         liquid_alternative_flowing = deSerializeString(is);
240         liquid_alternative_source = deSerializeString(is);
241         liquid_viscosity = readU8(is);
242         light_source = readU8(is);
243         damage_per_second = readU32(is);
244         selection_box.deSerialize(is);
245         legacy_facedir_simple = readU8(is);
246         legacy_wallmounted = readU8(is);
247 }
248
249 /*
250         CNodeDefManager
251 */
252
253 class CNodeDefManager: public IWritableNodeDefManager
254 {
255 public:
256         void clear()
257         {
258                 m_name_id_mapping.clear();
259                 m_name_id_mapping_with_aliases.clear();
260
261                 for(u16 i=0; i<=MAX_CONTENT; i++)
262                 {
263                         ContentFeatures &f = m_content_features[i];
264                         f.reset(); // Reset to defaults
265                 }
266                 
267                 // Set CONTENT_AIR
268                 {
269                         ContentFeatures f;
270                         f.name = "air";
271                         f.drawtype = NDT_AIRLIKE;
272                         f.param_type = CPT_LIGHT;
273                         f.light_propagates = true;
274                         f.sunlight_propagates = true;
275                         f.walkable = false;
276                         f.pointable = false;
277                         f.diggable = false;
278                         f.buildable_to = true;
279                         // Insert directly into containers
280                         content_t c = CONTENT_AIR;
281                         m_content_features[c] = f;
282                         addNameIdMapping(c, f.name);
283                 }
284                 // Set CONTENT_IGNORE
285                 {
286                         ContentFeatures f;
287                         f.name = "ignore";
288                         f.drawtype = NDT_AIRLIKE;
289                         f.param_type = CPT_NONE;
290                         f.light_propagates = false;
291                         f.sunlight_propagates = false;
292                         f.walkable = false;
293                         f.pointable = false;
294                         f.diggable = false;
295                         // A way to remove accidental CONTENT_IGNOREs
296                         f.buildable_to = true;
297                         // Insert directly into containers
298                         content_t c = CONTENT_IGNORE;
299                         m_content_features[c] = f;
300                         addNameIdMapping(c, f.name);
301                 }
302         }
303         // CONTENT_IGNORE = not found
304         content_t getFreeId(bool require_full_param2)
305         {
306                 // If allowed, first search in the large 4-bit-param2 pool
307                 if(!require_full_param2){
308                         for(u16 i=0x800; i<=0xfff; i++){
309                                 const ContentFeatures &f = m_content_features[i];
310                                 if(f.name == "")
311                                         return i;
312                         }
313                 }
314                 // Then search from the small 8-bit-param2 pool
315                 for(u16 i=0; i<=125; i++){
316                         const ContentFeatures &f = m_content_features[i];
317                         if(f.name == "")
318                                 return i;
319                 }
320                 return CONTENT_IGNORE;
321         }
322         CNodeDefManager()
323         {
324                 clear();
325         }
326         virtual ~CNodeDefManager()
327         {
328         }
329         virtual IWritableNodeDefManager* clone()
330         {
331                 CNodeDefManager *mgr = new CNodeDefManager();
332                 for(u16 i=0; i<=MAX_CONTENT; i++)
333                 {
334                         mgr->set(i, get(i));
335                 }
336                 return mgr;
337         }
338         virtual const ContentFeatures& get(content_t c) const
339         {
340                 assert(c <= MAX_CONTENT);
341                 return m_content_features[c];
342         }
343         virtual const ContentFeatures& get(const MapNode &n) const
344         {
345                 return get(n.getContent());
346         }
347         virtual bool getId(const std::string &name, content_t &result) const
348         {
349                 std::map<std::string, content_t>::const_iterator
350                         i = m_name_id_mapping_with_aliases.find(name);
351                 if(i == m_name_id_mapping_with_aliases.end())
352                         return false;
353                 result = i->second;
354                 return true;
355         }
356         virtual content_t getId(const std::string &name) const
357         {
358                 content_t id = CONTENT_IGNORE;
359                 getId(name, id);
360                 return id;
361         }
362         virtual const ContentFeatures& get(const std::string &name) const
363         {
364                 content_t id = CONTENT_IGNORE;
365                 getId(name, id);
366                 return get(id);
367         }
368         // IWritableNodeDefManager
369         virtual void set(content_t c, const ContentFeatures &def)
370         {
371                 infostream<<"registerNode: registering content id \""<<c
372                                 <<"\": name=\""<<def.name<<"\""<<std::endl;
373                 assert(c <= MAX_CONTENT);
374                 // Don't allow redefining CONTENT_IGNORE (but allow air)
375                 if(def.name == "ignore" || c == CONTENT_IGNORE){
376                         infostream<<"registerNode: WARNING: Ignoring "
377                                         <<"CONTENT_IGNORE redefinition"<<std::endl;
378                         return;
379                 }
380                 // Check that the special contents are not redefined as different id
381                 // because it would mess up everything
382                 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
383                         (def.name == "air" && c != CONTENT_AIR)){
384                         errorstream<<"registerNode: IGNORING ERROR: "
385                                         <<"trying to register built-in type \""
386                                         <<def.name<<"\" as different id"<<std::endl;
387                         return;
388                 }
389                 m_content_features[c] = def;
390                 if(def.name != "")
391                         addNameIdMapping(c, def.name);
392         }
393         virtual content_t set(const std::string &name,
394                         const ContentFeatures &def)
395         {
396                 assert(name == def.name);
397                 u16 id = CONTENT_IGNORE;
398                 bool found = m_name_id_mapping.getId(name, id);  // ignore aliases
399                 if(!found){
400                         // Determine if full param2 is required
401                         bool require_full_param2 = (
402                                 def.param_type_2 == CPT2_FULL
403                                 ||
404                                 def.param_type_2 == CPT2_FLOWINGLIQUID
405                                 ||
406                                 def.legacy_wallmounted
407                         );
408                         // Get some id
409                         id = getFreeId(require_full_param2);
410                         if(id == CONTENT_IGNORE)
411                                 return CONTENT_IGNORE;
412                         if(name != "")
413                                 addNameIdMapping(id, name);
414                 }
415                 set(id, def);
416                 return id;
417         }
418         virtual content_t allocateDummy(const std::string &name)
419         {
420                 assert(name != "");
421                 ContentFeatures f;
422                 f.name = name;
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