3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
22 #include "main.h" // For g_settings
23 #include "nodemetadata.h"
34 void NodeBox::serialize(std::ostream &os) const
36 writeU8(os, 0); // version
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);
48 void NodeBox::deSerialize(std::istream &is)
50 int version = readU8(is);
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);
68 void MaterialSpec::serialize(std::ostream &os) const
70 os<<serializeString(tname);
71 writeU8(os, backface_culling);
74 void MaterialSpec::deSerialize(std::istream &is)
76 tname = deSerializeString(is);
77 backface_culling = readU8(is);
84 ContentFeatures::ContentFeatures()
89 ContentFeatures::~ContentFeatures()
91 delete initial_metadata;
93 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
94 delete special_materials[j];
95 delete special_aps[j];
100 void ContentFeatures::reset()
106 inventory_texture = NULL;
108 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
109 special_materials[j] = NULL;
110 special_aps[j] = NULL;
113 visual_solidness = 0;
114 backface_culling = true;
116 used_texturenames.clear();
117 modified = true; // NodeDefManager explicitly sets to false
121 drawtype = NDT_NORMAL;
123 for(u32 i=0; i<6; i++)
125 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
126 mspec_special[j] = MaterialSpec();
127 tname_inventory = "";
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;
138 buildable_to = false;
139 wall_mounted = false;
140 air_equivalent = false;
141 often_contains_mineral = false;
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;
151 damage_per_second = 0;
152 selection_box = NodeBox();
153 material = MaterialProperties();
154 cookresult_item = "";
157 void ContentFeatures::serialize(std::ostream &os)
159 writeU8(os, 0); // version
160 writeU8(os, drawtype);
161 writeF1000(os, visual_scale);
163 for(u32 i=0; i<6; i++)
164 os<<serializeString(tname_tiles[i]);
165 os<<serializeString(tname_inventory);
166 writeU8(os, CF_SPECIAL_COUNT);
167 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
168 mspec_special[i].serialize(os);
171 writeU8(os, post_effect_color.getAlpha());
172 writeU8(os, post_effect_color.getRed());
173 writeU8(os, post_effect_color.getGreen());
174 writeU8(os, post_effect_color.getBlue());
175 writeU8(os, param_type);
176 writeU8(os, is_ground_content);
177 writeU8(os, light_propagates);
178 writeU8(os, sunlight_propagates);
179 writeU8(os, walkable);
180 writeU8(os, pointable);
181 writeU8(os, diggable);
182 writeU8(os, climbable);
183 writeU8(os, buildable_to);
184 writeU8(os, wall_mounted);
185 writeU8(os, air_equivalent);
186 writeU8(os, often_contains_mineral);
187 os<<serializeString(dug_item);
188 os<<serializeString(extra_dug_item);
189 writeS32(os, extra_dug_item_rarity);
190 if(initial_metadata){
192 initial_metadata->serialize(os);
196 writeU8(os, liquid_type);
197 writeU16(os, liquid_alternative_flowing);
198 writeU16(os, liquid_alternative_source);
199 writeU8(os, liquid_viscosity);
200 writeU8(os, light_source);
201 writeU32(os, damage_per_second);
202 selection_box.serialize(os);
203 material.serialize(os);
204 os<<serializeString(cookresult_item);
207 void ContentFeatures::deSerialize(std::istream &is, IGameDef *gamedef)
209 int version = readU8(is);
211 throw SerializationError("unsupported ContentFeatures version");
212 drawtype = (enum NodeDrawType)readU8(is);
213 visual_scale = readF1000(is);
215 throw SerializationError("unsupported tile count");
216 for(u32 i=0; i<6; i++)
217 tname_tiles[i] = deSerializeString(is);
218 tname_inventory = deSerializeString(is);
219 if(readU8(is) != CF_SPECIAL_COUNT)
220 throw SerializationError("unsupported CF_SPECIAL_COUNT");
221 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
222 mspec_special[i].deSerialize(is);
225 post_effect_color.setAlpha(readU8(is));
226 post_effect_color.setRed(readU8(is));
227 post_effect_color.setGreen(readU8(is));
228 post_effect_color.setBlue(readU8(is));
229 param_type = (enum ContentParamType)readU8(is);
230 is_ground_content = readU8(is);
231 light_propagates = readU8(is);
232 sunlight_propagates = readU8(is);
233 walkable = readU8(is);
234 pointable = readU8(is);
235 diggable = readU8(is);
236 climbable = readU8(is);
237 buildable_to = readU8(is);
238 wall_mounted = readU8(is);
239 air_equivalent = readU8(is);
240 often_contains_mineral = readU8(is);
241 dug_item = deSerializeString(is);
242 extra_dug_item = deSerializeString(is);
243 extra_dug_item_rarity = readS32(is);
245 initial_metadata = NodeMetadata::deSerialize(is, gamedef);
247 initial_metadata = NULL;
249 liquid_type = (enum LiquidType)readU8(is);
250 liquid_alternative_flowing = readU16(is);
251 liquid_alternative_source = readU16(is);
252 liquid_viscosity = readU8(is);
253 light_source = readU8(is);
254 damage_per_second = readU32(is);
255 selection_box.deSerialize(is);
256 material.deSerialize(is);
257 cookresult_item = deSerializeString(is);
260 void ContentFeatures::setTexture(u16 i, std::string name)
262 used_texturenames.insert(name);
263 tname_tiles[i] = name;
264 if(tname_inventory == "")
265 tname_inventory = name;
268 void ContentFeatures::setAllTextures(std::string name)
270 for(u16 i=0; i<6; i++)
272 // Force inventory texture too
273 setInventoryTexture(name);
276 void ContentFeatures::setSpecialMaterial(u16 i, const MaterialSpec &mspec)
278 assert(i < CF_SPECIAL_COUNT);
279 mspec_special[i] = mspec;
282 void ContentFeatures::setInventoryTexture(std::string imgname)
284 tname_inventory = imgname + "^[forcesingle";
287 void ContentFeatures::setInventoryTextureCube(std::string top,
288 std::string left, std::string right)
290 str_replace_char(top, '^', '&');
291 str_replace_char(left, '^', '&');
292 str_replace_char(right, '^', '&');
294 std::string imgname_full;
295 imgname_full += "[inventorycube{";
298 imgname_full += left;
300 imgname_full += right;
301 tname_inventory = imgname_full;
308 class CNodeDefManager: public IWritableNodeDefManager
313 for(u16 i=0; i<=MAX_CONTENT; i++)
315 ContentFeatures *f = &m_content_features[i];
316 f->reset(); // Reset to defaults
317 f->modified = false; // Not changed from default
318 if(i == CONTENT_IGNORE || i == CONTENT_AIR){
319 f->drawtype = NDT_AIRLIKE;
322 f->setAllTextures("unknown_block.png");
323 //f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
326 // Make CONTENT_IGNORE to not block the view when occlusion culling
327 m_content_features[CONTENT_IGNORE].solidness = 0;
334 virtual ~CNodeDefManager()
337 virtual IWritableNodeDefManager* clone()
339 CNodeDefManager *mgr = new CNodeDefManager();
340 for(u16 i=0; i<=MAX_CONTENT; i++)
346 virtual const ContentFeatures& get(content_t c) const
348 assert(c <= MAX_CONTENT);
349 return m_content_features[c];
351 virtual const ContentFeatures& get(const MapNode &n) const
353 return get(n.getContent());
356 virtual void set(content_t c, const ContentFeatures &def)
358 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
359 assert(c <= MAX_CONTENT);
360 m_content_features[c] = def;
362 virtual ContentFeatures* getModifiable(content_t c)
364 assert(c <= MAX_CONTENT);
365 m_content_features[c].modified = true; // Assume it is modified
366 return &m_content_features[c];
368 virtual void updateTextures(ITextureSource *tsrc)
371 infostream<<"CNodeDefManager::updateTextures(): Updating "
372 <<"textures in node definitions"<<std::endl;
374 bool new_style_water = g_settings->getBool("new_style_water");
375 bool new_style_leaves = g_settings->getBool("new_style_leaves");
376 bool opaque_water = g_settings->getBool("opaque_water");
378 for(u16 i=0; i<=MAX_CONTENT; i++)
380 ContentFeatures *f = &m_content_features[i];
391 assert(f->liquid_type == LIQUID_SOURCE);
402 case NDT_FLOWINGLIQUID:
403 assert(f->liquid_type == LIQUID_FLOWING);
410 f->visual_solidness = 1;
414 f->visual_solidness = 1;
416 case NDT_ALLFACES_OPTIONAL:
417 if(new_style_leaves){
418 f->drawtype = NDT_ALLFACES;
420 f->visual_solidness = 1;
422 f->drawtype = NDT_NORMAL;
424 for(u32 i=0; i<6; i++){
425 f->tname_tiles[i] = std::string("[noalpha:")
440 if(f->tname_inventory != "")
441 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
443 f->inventory_texture = NULL;
445 for(u16 j=0; j<6; j++){
446 if(f->tname_tiles[j] == "")
448 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
449 f->tiles[j].alpha = f->alpha;
451 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
453 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
454 if(f->backface_culling)
455 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
457 f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
460 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
462 if(f->special_aps[j]){
463 delete f->special_aps[j];
464 f->special_aps[j] = NULL;
466 if(f->special_materials[j]){
467 delete f->special_materials[j];
468 f->special_materials[j] = NULL;
470 // Skip if should not exist
471 if(f->mspec_special[j].tname == "")
474 f->special_aps[j] = new AtlasPointer(
475 tsrc->getTexture(f->mspec_special[j].tname));
476 f->special_materials[j] = new video::SMaterial;
477 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
478 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
479 f->mspec_special[j].backface_culling);
480 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
481 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
482 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
484 f->special_materials[j]->MaterialType =
485 video::EMT_TRANSPARENT_VERTEX_ALPHA;
490 void serialize(std::ostream &os)
493 std::ostringstream tmp_os(std::ios::binary);
494 for(u16 i=0; i<=MAX_CONTENT; i++)
496 ContentFeatures *f = &m_content_features[i];
500 f->serialize(tmp_os);
504 os<<serializeLongString(tmp_os.str());
506 void deSerialize(std::istream &is, IGameDef *gamedef)
509 u16 count = readU16(is);
510 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
511 for(u16 n=0; n<count; n++){
512 u16 i = readU16(tmp_is);
514 errorstream<<"ContentFeatures::deSerialize(): "
515 <<"Too large content id: "<<i<<std::endl;
518 ContentFeatures *f = &m_content_features[i];
519 f->deSerialize(tmp_is, gamedef);
524 ContentFeatures m_content_features[MAX_CONTENT+1];
527 IWritableNodeDefManager* createNodeDefManager()
529 return new CNodeDefManager();