4e43369b414d071984ecb8ff2cb5f0f0e30101da
[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 "nodemetadata.h"
24 #ifndef SERVER
25 #include "tile.h"
26 #endif
27 #include "log.h"
28 #include "settings.h"
29
30 /*
31         NodeBox
32 */
33
34 void NodeBox::serialize(std::ostream &os) const
35 {
36         writeU8(os, 0); // version
37         writeU8(os, type);
38         writeV3F1000(os, fixed.MinEdge);
39         writeV3F1000(os, fixed.MaxEdge);
40         writeV3F1000(os, wall_top.MinEdge);
41         writeV3F1000(os, wall_top.MaxEdge);
42         writeV3F1000(os, wall_bottom.MinEdge);
43         writeV3F1000(os, wall_bottom.MaxEdge);
44         writeV3F1000(os, wall_side.MinEdge);
45         writeV3F1000(os, wall_side.MaxEdge);
46 }
47
48 void NodeBox::deSerialize(std::istream &is)
49 {
50         int version = readU8(is);
51         if(version != 0)
52                 throw SerializationError("unsupported NodeBox version");
53         type = (enum NodeBoxType)readU8(is);
54         fixed.MinEdge = readV3F1000(is);
55         fixed.MaxEdge = readV3F1000(is);
56         wall_top.MinEdge = readV3F1000(is);
57         wall_top.MaxEdge = readV3F1000(is);
58         wall_bottom.MinEdge = readV3F1000(is);
59         wall_bottom.MaxEdge = readV3F1000(is);
60         wall_side.MinEdge = readV3F1000(is);
61         wall_side.MaxEdge = readV3F1000(is);
62 }
63
64 /*
65         MaterialSpec
66 */
67
68 void MaterialSpec::serialize(std::ostream &os) const
69 {
70         os<<serializeString(tname);
71         writeU8(os, backface_culling);
72 }
73
74 void MaterialSpec::deSerialize(std::istream &is)
75 {
76         tname = deSerializeString(is);
77         backface_culling = readU8(is);
78 }
79
80 /*
81         ContentFeatures
82 */
83
84 ContentFeatures::ContentFeatures()
85 {
86         reset();
87 }
88
89 ContentFeatures::~ContentFeatures()
90 {
91         delete initial_metadata;
92 #ifndef SERVER
93         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
94                 delete special_materials[j];
95                 delete special_aps[j];
96         }
97 #endif
98 }
99
100 void ContentFeatures::reset()
101 {
102         /*
103                 Cached stuff
104         */
105 #ifndef SERVER
106         inventory_texture = NULL;
107         
108         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
109                 special_materials[j] = NULL;
110                 special_aps[j] = NULL;
111         }
112         solidness = 2;
113         visual_solidness = 0;
114         backface_culling = true;
115 #endif
116         used_texturenames.clear();
117         modified = true; // NodeDefManager explicitly sets to false
118         /*
119                 Actual data
120         */
121         drawtype = NDT_NORMAL;
122         visual_scale = 1.0;
123         for(u32 i=0; i<6; i++)
124                 tname_tiles[i] = "";
125         for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
126                 mspec_special[j] = MaterialSpec();
127         tname_inventory = "";
128         alpha = 255;
129         post_effect_color = video::SColor(0, 0, 0, 0);
130         param_type = CPT_NONE;
131         is_ground_content = false;
132         light_propagates = false;
133         sunlight_propagates = false;
134         walkable = true;
135         pointable = true;
136         diggable = true;
137         climbable = false;
138         buildable_to = false;
139         wall_mounted = false;
140         air_equivalent = false;
141         often_contains_mineral = false;
142         dug_item = "";
143         initial_metadata = NULL;
144         liquid_type = LIQUID_NONE;
145         liquid_alternative_flowing = CONTENT_IGNORE;
146         liquid_alternative_source = CONTENT_IGNORE;
147         liquid_viscosity = 0;
148         light_source = 0;
149         damage_per_second = 0;
150         selection_box = NodeBox();
151         material = MaterialProperties();
152 }
153
154 void ContentFeatures::serialize(std::ostream &os)
155 {
156         writeU8(os, 0); // version
157         writeU8(os, drawtype);
158         writeF1000(os, visual_scale);
159         writeU8(os, 6);
160         for(u32 i=0; i<6; i++)
161                 os<<serializeString(tname_tiles[i]);
162         os<<serializeString(tname_inventory);
163         writeU8(os, CF_SPECIAL_COUNT);
164         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
165                 mspec_special[i].serialize(os);
166         }
167         writeU8(os, alpha);
168         writeU8(os, post_effect_color.getAlpha());
169         writeU8(os, post_effect_color.getRed());
170         writeU8(os, post_effect_color.getGreen());
171         writeU8(os, post_effect_color.getBlue());
172         writeU8(os, param_type);
173         writeU8(os, is_ground_content);
174         writeU8(os, light_propagates);
175         writeU8(os, sunlight_propagates);
176         writeU8(os, walkable);
177         writeU8(os, pointable);
178         writeU8(os, diggable);
179         writeU8(os, climbable);
180         writeU8(os, buildable_to);
181         writeU8(os, wall_mounted);
182         writeU8(os, air_equivalent);
183         writeU8(os, often_contains_mineral);
184         os<<serializeString(dug_item);
185         os<<serializeString(extra_dug_item);
186         writeS32(os, extra_dug_item_rarity);
187         if(initial_metadata){
188                 writeU8(os, true);
189                 initial_metadata->serialize(os);
190         } else {
191                 writeU8(os, false);
192         }
193         writeU8(os, liquid_type);
194         writeU16(os, liquid_alternative_flowing);
195         writeU16(os, liquid_alternative_source);
196         writeU8(os, liquid_viscosity);
197         writeU8(os, light_source);
198         writeU32(os, damage_per_second);
199         selection_box.serialize(os);
200         material.serialize(os);
201 }
202
203 void ContentFeatures::deSerialize(std::istream &is, IGameDef *gamedef)
204 {
205         int version = readU8(is);
206         if(version != 0)
207                 throw SerializationError("unsupported ContentFeatures version");
208         drawtype = (enum NodeDrawType)readU8(is);
209         visual_scale = readF1000(is);
210         if(readU8(is) != 6)
211                 throw SerializationError("unsupported tile count");
212         for(u32 i=0; i<6; i++)
213                 tname_tiles[i] = deSerializeString(is);
214         tname_inventory = deSerializeString(is);
215         if(readU8(is) != CF_SPECIAL_COUNT)
216                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
217         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
218                 mspec_special[i].deSerialize(is);
219         }
220         alpha = readU8(is);
221         post_effect_color.setAlpha(readU8(is));
222         post_effect_color.setRed(readU8(is));
223         post_effect_color.setGreen(readU8(is));
224         post_effect_color.setBlue(readU8(is));
225         param_type = (enum ContentParamType)readU8(is);
226         is_ground_content = readU8(is);
227         light_propagates = readU8(is);
228         sunlight_propagates = readU8(is);
229         walkable = readU8(is);
230         pointable = readU8(is);
231         diggable = readU8(is);
232         climbable = readU8(is);
233         buildable_to = readU8(is);
234         wall_mounted = readU8(is);
235         air_equivalent = readU8(is);
236         often_contains_mineral = readU8(is);
237         dug_item = deSerializeString(is);
238         extra_dug_item = deSerializeString(is);
239         extra_dug_item_rarity = readS32(is);
240         if(readU8(is)){
241                 initial_metadata = NodeMetadata::deSerialize(is, gamedef);
242         } else {
243                 initial_metadata = NULL;
244         }
245         liquid_type = (enum LiquidType)readU8(is);
246         liquid_alternative_flowing = readU16(is);
247         liquid_alternative_source = readU16(is);
248         liquid_viscosity = readU8(is);
249         light_source = readU8(is);
250         damage_per_second = readU32(is);
251         selection_box.deSerialize(is);
252         material.deSerialize(is);
253 }
254
255 void ContentFeatures::setTexture(u16 i, std::string name)
256 {
257         used_texturenames.insert(name);
258         tname_tiles[i] = name;
259         if(tname_inventory == "")
260                 tname_inventory = name;
261 }
262
263 void ContentFeatures::setAllTextures(std::string name)
264 {
265         for(u16 i=0; i<6; i++)
266                 setTexture(i, name);
267         // Force inventory texture too
268         setInventoryTexture(name);
269 }
270
271 void ContentFeatures::setSpecialMaterial(u16 i, const MaterialSpec &mspec)
272 {
273         assert(i < CF_SPECIAL_COUNT);
274         mspec_special[i] = mspec;
275 }
276
277 void ContentFeatures::setInventoryTexture(std::string imgname)
278 {
279         tname_inventory = imgname + "^[forcesingle";
280 }
281
282 void ContentFeatures::setInventoryTextureCube(std::string top,
283                 std::string left, std::string right)
284 {
285         str_replace_char(top, '^', '&');
286         str_replace_char(left, '^', '&');
287         str_replace_char(right, '^', '&');
288
289         std::string imgname_full;
290         imgname_full += "[inventorycube{";
291         imgname_full += top;
292         imgname_full += "{";
293         imgname_full += left;
294         imgname_full += "{";
295         imgname_full += right;
296         tname_inventory = imgname_full;
297 }
298
299 /*
300         CNodeDefManager
301 */
302
303 class CNodeDefManager: public IWritableNodeDefManager
304 {
305 public:
306         void clear()
307         {
308                 for(u16 i=0; i<=MAX_CONTENT; i++)
309                 {
310                         ContentFeatures *f = &m_content_features[i];
311                         f->reset(); // Reset to defaults
312                         f->modified = false; // Not changed from default
313                         if(i == CONTENT_IGNORE || i == CONTENT_AIR){
314                                 f->drawtype = NDT_AIRLIKE;
315                                 continue;
316                         }
317                         f->setAllTextures("unknown_block.png");
318                         //f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
319                 }
320 #ifndef SERVER
321                 // Make CONTENT_IGNORE to not block the view when occlusion culling
322                 m_content_features[CONTENT_IGNORE].solidness = 0;
323 #endif
324         }
325         CNodeDefManager()
326         {
327                 clear();
328         }
329         virtual ~CNodeDefManager()
330         {
331         }
332         virtual IWritableNodeDefManager* clone()
333         {
334                 CNodeDefManager *mgr = new CNodeDefManager();
335                 for(u16 i=0; i<=MAX_CONTENT; i++)
336                 {
337                         mgr->set(i, get(i));
338                 }
339                 return mgr;
340         }
341         virtual const ContentFeatures& get(content_t c) const
342         {
343                 assert(c <= MAX_CONTENT);
344                 return m_content_features[c];
345         }
346         virtual const ContentFeatures& get(const MapNode &n) const
347         {
348                 return get(n.getContent());
349         }
350         // Writable
351         virtual void set(content_t c, const ContentFeatures &def)
352         {
353                 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
354                 assert(c <= MAX_CONTENT);
355                 m_content_features[c] = def;
356         }
357         virtual ContentFeatures* getModifiable(content_t c)
358         {
359                 assert(c <= MAX_CONTENT);
360                 m_content_features[c].modified = true; // Assume it is modified
361                 return &m_content_features[c];
362         }
363         virtual void updateTextures(ITextureSource *tsrc)
364         {
365 #ifndef SERVER
366                 infostream<<"CNodeDefManager::updateTextures(): Updating "
367                                 <<"textures in node definitions"<<std::endl;
368
369                 bool new_style_water = g_settings->getBool("new_style_water");
370                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
371                 bool opaque_water = g_settings->getBool("opaque_water");
372                 
373                 for(u16 i=0; i<=MAX_CONTENT; i++)
374                 {
375                         ContentFeatures *f = &m_content_features[i];
376
377                         switch(f->drawtype){
378                         default:
379                         case NDT_NORMAL:
380                                 f->solidness = 2;
381                                 break;
382                         case NDT_AIRLIKE:
383                                 f->solidness = 0;
384                                 break;
385                         case NDT_LIQUID:
386                                 assert(f->liquid_type == LIQUID_SOURCE);
387                                 if(opaque_water)
388                                         f->alpha = 255;
389                                 if(new_style_water){
390                                         f->solidness = 0;
391                                 } else {
392                                         f->solidness = 1;
393                                         if(f->alpha == 255)
394                                                 f->solidness = 2;
395                                 }
396                                 break;
397                         case NDT_FLOWINGLIQUID:
398                                 assert(f->liquid_type == LIQUID_FLOWING);
399                                 f->solidness = 0;
400                                 if(opaque_water)
401                                         f->alpha = 255;
402                                 break;
403                         case NDT_GLASSLIKE:
404                                 f->solidness = 0;
405                                 f->visual_solidness = 1;
406                                 break;
407                         case NDT_ALLFACES:
408                                 f->solidness = 0;
409                                 f->visual_solidness = 1;
410                                 break;
411                         case NDT_ALLFACES_OPTIONAL:
412                                 if(new_style_leaves){
413                                         f->drawtype = NDT_ALLFACES;
414                                         f->solidness = 0;
415                                         f->visual_solidness = 1;
416                                 } else {
417                                         f->drawtype = NDT_NORMAL;
418                                         f->solidness = 1;
419                                         for(u32 i=0; i<6; i++){
420                                                 f->tname_tiles[i] = std::string("[noalpha:")
421                                                                 + f->tname_tiles[i];
422                                         }
423                                 }
424                                 break;
425                         case NDT_TORCHLIKE:
426                         case NDT_SIGNLIKE:
427                         case NDT_PLANTLIKE:
428                         case NDT_FENCELIKE:
429                         case NDT_RAILLIKE:
430                                 f->solidness = 0;
431                                 break;
432                         }
433
434                         // Inventory texture
435                         if(f->tname_inventory != "")
436                                 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
437                         else
438                                 f->inventory_texture = NULL;
439                         // Tile textures
440                         for(u16 j=0; j<6; j++){
441                                 if(f->tname_tiles[j] == "")
442                                         continue;
443                                 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
444                                 f->tiles[j].alpha = f->alpha;
445                                 if(f->alpha == 255)
446                                         f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
447                                 else
448                                         f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
449                                 if(f->backface_culling)
450                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
451                                 else
452                                         f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
453                         }
454                         // Special textures
455                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
456                                 // Remove all stuff
457                                 if(f->special_aps[j]){
458                                         delete f->special_aps[j];
459                                         f->special_aps[j] = NULL;
460                                 }
461                                 if(f->special_materials[j]){
462                                         delete f->special_materials[j];
463                                         f->special_materials[j] = NULL;
464                                 }
465                                 // Skip if should not exist
466                                 if(f->mspec_special[j].tname == "")
467                                         continue;
468                                 // Create all stuff
469                                 f->special_aps[j] = new AtlasPointer(
470                                                 tsrc->getTexture(f->mspec_special[j].tname));
471                                 f->special_materials[j] = new video::SMaterial;
472                                 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
473                                 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
474                                                 f->mspec_special[j].backface_culling);
475                                 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
476                                 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
477                                 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
478                                 if(f->alpha != 255)
479                                         f->special_materials[j]->MaterialType =
480                                                         video::EMT_TRANSPARENT_VERTEX_ALPHA;
481                         }
482                 }
483 #endif
484         }
485         void serialize(std::ostream &os)
486         {
487                 u16 count = 0;
488                 std::ostringstream tmp_os(std::ios::binary);
489                 for(u16 i=0; i<=MAX_CONTENT; i++)
490                 {
491                         ContentFeatures *f = &m_content_features[i];
492                         if(!f->modified)
493                                 continue;
494                         writeU16(tmp_os, i);
495                         f->serialize(tmp_os);
496                         count++;
497                 }
498                 writeU16(os, count);
499                 os<<serializeLongString(tmp_os.str());
500         }
501         void deSerialize(std::istream &is, IGameDef *gamedef)
502         {
503                 clear();
504                 u16 count = readU16(is);
505                 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
506                 for(u16 n=0; n<count; n++){
507                         u16 i = readU16(tmp_is);
508                         if(i > MAX_CONTENT){
509                                 errorstream<<"ContentFeatures::deSerialize(): "
510                                                 <<"Too large content id: "<<i<<std::endl;
511                                 continue;
512                         }
513                         ContentFeatures *f = &m_content_features[i];
514                         f->deSerialize(tmp_is, gamedef);
515                         f->modified = true;
516                 }
517         }
518 private:
519         ContentFeatures m_content_features[MAX_CONTENT+1];
520 };
521
522 IWritableNodeDefManager* createNodeDefManager()
523 {
524         return new CNodeDefManager();
525 }
526