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