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