210e466d2dfadbc2f14e654443c2fe48734f2fb8
[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         extra_dug_item = "";
144         extra_dug_item_rarity = 2;
145         initial_metadata = NULL;
146         liquid_type = LIQUID_NONE;
147         liquid_alternative_flowing = CONTENT_IGNORE;
148         liquid_alternative_source = CONTENT_IGNORE;
149         liquid_viscosity = 0;
150         light_source = 0;
151         damage_per_second = 0;
152         selection_box = NodeBox();
153         material = MaterialProperties();
154         cookresult_item = ""; // Cannot be cooked
155         furnace_cooktime = 3.0;
156         furnace_burntime = -1.0; // Cannot be burned
157 }
158
159 void ContentFeatures::serialize(std::ostream &os)
160 {
161         writeU8(os, 0); // version
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         os<<serializeString(tname_inventory);
168         writeU8(os, CF_SPECIAL_COUNT);
169         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
170                 mspec_special[i].serialize(os);
171         }
172         writeU8(os, alpha);
173         writeU8(os, post_effect_color.getAlpha());
174         writeU8(os, post_effect_color.getRed());
175         writeU8(os, post_effect_color.getGreen());
176         writeU8(os, post_effect_color.getBlue());
177         writeU8(os, param_type);
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         writeU8(os, wall_mounted);
187         writeU8(os, air_equivalent);
188         writeU8(os, often_contains_mineral);
189         os<<serializeString(dug_item);
190         os<<serializeString(extra_dug_item);
191         writeS32(os, extra_dug_item_rarity);
192         if(initial_metadata){
193                 writeU8(os, true);
194                 initial_metadata->serialize(os);
195         } else {
196                 writeU8(os, false);
197         }
198         writeU8(os, liquid_type);
199         writeU16(os, liquid_alternative_flowing);
200         writeU16(os, liquid_alternative_source);
201         writeU8(os, liquid_viscosity);
202         writeU8(os, light_source);
203         writeU32(os, damage_per_second);
204         selection_box.serialize(os);
205         material.serialize(os);
206         os<<serializeString(cookresult_item);
207         writeF1000(os, furnace_cooktime);
208         writeF1000(os, furnace_burntime);
209 }
210
211 void ContentFeatures::deSerialize(std::istream &is, IGameDef *gamedef)
212 {
213         int version = readU8(is);
214         if(version != 0)
215                 throw SerializationError("unsupported ContentFeatures version");
216         drawtype = (enum NodeDrawType)readU8(is);
217         visual_scale = readF1000(is);
218         if(readU8(is) != 6)
219                 throw SerializationError("unsupported tile count");
220         for(u32 i=0; i<6; i++)
221                 tname_tiles[i] = deSerializeString(is);
222         tname_inventory = deSerializeString(is);
223         if(readU8(is) != CF_SPECIAL_COUNT)
224                 throw SerializationError("unsupported CF_SPECIAL_COUNT");
225         for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
226                 mspec_special[i].deSerialize(is);
227         }
228         alpha = readU8(is);
229         post_effect_color.setAlpha(readU8(is));
230         post_effect_color.setRed(readU8(is));
231         post_effect_color.setGreen(readU8(is));
232         post_effect_color.setBlue(readU8(is));
233         param_type = (enum ContentParamType)readU8(is);
234         is_ground_content = readU8(is);
235         light_propagates = readU8(is);
236         sunlight_propagates = readU8(is);
237         walkable = readU8(is);
238         pointable = readU8(is);
239         diggable = readU8(is);
240         climbable = readU8(is);
241         buildable_to = readU8(is);
242         wall_mounted = readU8(is);
243         air_equivalent = readU8(is);
244         often_contains_mineral = readU8(is);
245         dug_item = deSerializeString(is);
246         extra_dug_item = deSerializeString(is);
247         extra_dug_item_rarity = readS32(is);
248         if(readU8(is)){
249                 initial_metadata = NodeMetadata::deSerialize(is, gamedef);
250         } else {
251                 initial_metadata = NULL;
252         }
253         liquid_type = (enum LiquidType)readU8(is);
254         liquid_alternative_flowing = readU16(is);
255         liquid_alternative_source = readU16(is);
256         liquid_viscosity = readU8(is);
257         light_source = readU8(is);
258         damage_per_second = readU32(is);
259         selection_box.deSerialize(is);
260         material.deSerialize(is);
261         cookresult_item = deSerializeString(is);
262         furnace_cooktime = readF1000(is);
263         furnace_burntime = readF1000(is);
264 }
265
266 void ContentFeatures::setTexture(u16 i, std::string name)
267 {
268         used_texturenames.insert(name);
269         tname_tiles[i] = name;
270         if(tname_inventory == "")
271                 tname_inventory = name;
272 }
273
274 void ContentFeatures::setAllTextures(std::string name)
275 {
276         for(u16 i=0; i<6; i++)
277                 setTexture(i, name);
278         // Force inventory texture too
279         setInventoryTexture(name);
280 }
281
282 void ContentFeatures::setSpecialMaterial(u16 i, const MaterialSpec &mspec)
283 {
284         assert(i < CF_SPECIAL_COUNT);
285         mspec_special[i] = mspec;
286 }
287
288 void ContentFeatures::setInventoryTexture(std::string imgname)
289 {
290         tname_inventory = imgname + "^[forcesingle";
291 }
292
293 void ContentFeatures::setInventoryTextureCube(std::string top,
294                 std::string left, std::string right)
295 {
296         str_replace_char(top, '^', '&');
297         str_replace_char(left, '^', '&');
298         str_replace_char(right, '^', '&');
299
300         std::string imgname_full;
301         imgname_full += "[inventorycube{";
302         imgname_full += top;
303         imgname_full += "{";
304         imgname_full += left;
305         imgname_full += "{";
306         imgname_full += right;
307         tname_inventory = imgname_full;
308 }
309
310 /*
311         CNodeDefManager
312 */
313
314 class CNodeDefManager: public IWritableNodeDefManager
315 {
316 public:
317         void clear()
318         {
319                 for(u16 i=0; i<=MAX_CONTENT; i++)
320                 {
321                         ContentFeatures *f = &m_content_features[i];
322                         f->reset(); // Reset to defaults
323                         f->modified = false; // Not changed from default
324                         if(i == CONTENT_IGNORE || i == CONTENT_AIR){
325                                 f->drawtype = NDT_AIRLIKE;
326                                 continue;
327                         }
328                         f->setAllTextures("unknown_block.png");
329                         //f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
330                 }
331 #ifndef SERVER
332                 // Make CONTENT_IGNORE to not block the view when occlusion culling
333                 m_content_features[CONTENT_IGNORE].solidness = 0;
334 #endif
335         }
336         CNodeDefManager()
337         {
338                 clear();
339         }
340         virtual ~CNodeDefManager()
341         {
342         }
343         virtual IWritableNodeDefManager* clone()
344         {
345                 CNodeDefManager *mgr = new CNodeDefManager();
346                 for(u16 i=0; i<=MAX_CONTENT; i++)
347                 {
348                         mgr->set(i, get(i));
349                 }
350                 return mgr;
351         }
352         virtual const ContentFeatures& get(content_t c) const
353         {
354                 assert(c <= MAX_CONTENT);
355                 return m_content_features[c];
356         }
357         virtual const ContentFeatures& get(const MapNode &n) const
358         {
359                 return get(n.getContent());
360         }
361         // Writable
362         virtual void set(content_t c, const ContentFeatures &def)
363         {
364                 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
365                 assert(c <= MAX_CONTENT);
366                 m_content_features[c] = def;
367         }
368         virtual ContentFeatures* getModifiable(content_t c)
369         {
370                 assert(c <= MAX_CONTENT);
371                 m_content_features[c].modified = true; // Assume it is modified
372                 return &m_content_features[c];
373         }
374         virtual void updateTextures(ITextureSource *tsrc)
375         {
376 #ifndef SERVER
377                 infostream<<"CNodeDefManager::updateTextures(): Updating "
378                                 <<"textures in node definitions"<<std::endl;
379
380                 bool new_style_water = g_settings->getBool("new_style_water");
381                 bool new_style_leaves = g_settings->getBool("new_style_leaves");
382                 bool opaque_water = g_settings->getBool("opaque_water");
383                 
384                 for(u16 i=0; i<=MAX_CONTENT; i++)
385                 {
386                         ContentFeatures *f = &m_content_features[i];
387
388                         switch(f->drawtype){
389                         default:
390                         case NDT_NORMAL:
391                                 f->solidness = 2;
392                                 break;
393                         case NDT_AIRLIKE:
394                                 f->solidness = 0;
395                                 break;
396                         case NDT_LIQUID:
397                                 assert(f->liquid_type == LIQUID_SOURCE);
398                                 if(opaque_water)
399                                         f->alpha = 255;
400                                 if(new_style_water){
401                                         f->solidness = 0;
402                                 } else {
403                                         f->solidness = 1;
404                                         if(f->alpha == 255)
405                                                 f->solidness = 2;
406                                 }
407                                 break;
408                         case NDT_FLOWINGLIQUID:
409                                 assert(f->liquid_type == LIQUID_FLOWING);
410                                 f->solidness = 0;
411                                 if(opaque_water)
412                                         f->alpha = 255;
413                                 break;
414                         case NDT_GLASSLIKE:
415                                 f->solidness = 0;
416                                 f->visual_solidness = 1;
417                                 break;
418                         case NDT_ALLFACES:
419                                 f->solidness = 0;
420                                 f->visual_solidness = 1;
421                                 break;
422                         case NDT_ALLFACES_OPTIONAL:
423                                 if(new_style_leaves){
424                                         f->drawtype = NDT_ALLFACES;
425                                         f->solidness = 0;
426                                         f->visual_solidness = 1;
427                                 } else {
428                                         f->drawtype = NDT_NORMAL;
429                                         f->solidness = 1;
430                                         for(u32 i=0; i<6; i++){
431                                                 f->tname_tiles[i] = std::string("[noalpha:")
432                                                                 + f->tname_tiles[i];
433                                         }
434                                 }
435                                 break;
436                         case NDT_TORCHLIKE:
437                         case NDT_SIGNLIKE:
438                         case NDT_PLANTLIKE:
439                         case NDT_FENCELIKE:
440                         case NDT_RAILLIKE:
441                                 f->solidness = 0;
442                                 break;
443                         }
444
445                         // Inventory texture
446                         if(f->tname_inventory != "")
447                                 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
448                         else
449                                 f->inventory_texture = NULL;
450                         // Tile textures
451                         for(u16 j=0; j<6; j++){
452                                 if(f->tname_tiles[j] == "")
453                                         continue;
454                                 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
455                                 f->tiles[j].alpha = f->alpha;
456                                 if(f->alpha == 255)
457                                         f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
458                                 else
459                                         f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
460                                 if(f->backface_culling)
461                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
462                                 else
463                                         f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
464                         }
465                         // Special textures
466                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
467                                 // Remove all stuff
468                                 if(f->special_aps[j]){
469                                         delete f->special_aps[j];
470                                         f->special_aps[j] = NULL;
471                                 }
472                                 if(f->special_materials[j]){
473                                         delete f->special_materials[j];
474                                         f->special_materials[j] = NULL;
475                                 }
476                                 // Skip if should not exist
477                                 if(f->mspec_special[j].tname == "")
478                                         continue;
479                                 // Create all stuff
480                                 f->special_aps[j] = new AtlasPointer(
481                                                 tsrc->getTexture(f->mspec_special[j].tname));
482                                 f->special_materials[j] = new video::SMaterial;
483                                 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
484                                 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
485                                                 f->mspec_special[j].backface_culling);
486                                 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
487                                 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
488                                 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
489                                 if(f->alpha != 255)
490                                         f->special_materials[j]->MaterialType =
491                                                         video::EMT_TRANSPARENT_VERTEX_ALPHA;
492                         }
493                 }
494 #endif
495         }
496         void serialize(std::ostream &os)
497         {
498                 u16 count = 0;
499                 std::ostringstream tmp_os(std::ios::binary);
500                 for(u16 i=0; i<=MAX_CONTENT; i++)
501                 {
502                         ContentFeatures *f = &m_content_features[i];
503                         if(!f->modified)
504                                 continue;
505                         writeU16(tmp_os, i);
506                         f->serialize(tmp_os);
507                         count++;
508                 }
509                 writeU16(os, count);
510                 os<<serializeLongString(tmp_os.str());
511         }
512         void deSerialize(std::istream &is, IGameDef *gamedef)
513         {
514                 clear();
515                 u16 count = readU16(is);
516                 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
517                 for(u16 n=0; n<count; n++){
518                         u16 i = readU16(tmp_is);
519                         if(i > MAX_CONTENT){
520                                 errorstream<<"ContentFeatures::deSerialize(): "
521                                                 <<"Too large content id: "<<i<<std::endl;
522                                 continue;
523                         }
524                         ContentFeatures *f = &m_content_features[i];
525                         f->deSerialize(tmp_is, gamedef);
526                         f->modified = true;
527                 }
528         }
529 private:
530         ContentFeatures m_content_features[MAX_CONTENT+1];
531 };
532
533 IWritableNodeDefManager* createNodeDefManager()
534 {
535         return new CNodeDefManager();
536 }
537