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"
30 void NodeBox::serialize(std::ostream &os)
32 writeU8(os, 0); // version
34 writeV3F1000(os, fixed.MinEdge);
35 writeV3F1000(os, fixed.MaxEdge);
36 writeV3F1000(os, wall_top.MinEdge);
37 writeV3F1000(os, wall_top.MaxEdge);
38 writeV3F1000(os, wall_bottom.MinEdge);
39 writeV3F1000(os, wall_bottom.MaxEdge);
40 writeV3F1000(os, wall_side.MinEdge);
41 writeV3F1000(os, wall_side.MaxEdge);
44 void NodeBox::deSerialize(std::istream &is)
46 int version = readU8(is);
48 throw SerializationError("unsupported NodeBox version");
49 type = (enum NodeBoxType)readU8(is);
50 fixed.MinEdge = readV3F1000(is);
51 fixed.MaxEdge = readV3F1000(is);
52 wall_top.MinEdge = readV3F1000(is);
53 wall_top.MaxEdge = readV3F1000(is);
54 wall_bottom.MinEdge = readV3F1000(is);
55 wall_bottom.MaxEdge = readV3F1000(is);
56 wall_side.MinEdge = readV3F1000(is);
57 wall_side.MaxEdge = readV3F1000(is);
60 void MaterialSpec::serialize(std::ostream &os)
62 os<<serializeString(tname);
63 writeU8(os, backface_culling);
66 void MaterialSpec::deSerialize(std::istream &is)
68 tname = deSerializeString(is);
69 backface_culling = readU8(is);
72 ContentFeatures::ContentFeatures()
77 ContentFeatures::~ContentFeatures()
79 delete initial_metadata;
81 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
82 delete special_materials[j];
83 delete special_aps[j];
88 void ContentFeatures::reset()
94 inventory_texture = NULL;
96 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
97 special_materials[j] = NULL;
98 special_aps[j] = NULL;
101 visual_solidness = 0;
102 backface_culling = true;
104 used_texturenames.clear();
105 modified = true; // NodeDefManager explicitly sets to false
109 drawtype = NDT_NORMAL;
111 for(u32 i=0; i<6; i++)
113 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
114 mspec_special[j] = MaterialSpec();
115 tname_inventory = "";
117 post_effect_color = video::SColor(0, 0, 0, 0);
118 param_type = CPT_NONE;
119 is_ground_content = false;
120 light_propagates = false;
121 sunlight_propagates = false;
126 buildable_to = false;
127 wall_mounted = false;
128 air_equivalent = false;
129 often_contains_mineral = false;
131 initial_metadata = NULL;
132 liquid_type = LIQUID_NONE;
133 liquid_alternative_flowing = CONTENT_IGNORE;
134 liquid_alternative_source = CONTENT_IGNORE;
135 liquid_viscosity = 0;
137 damage_per_second = 0;
138 selection_box = NodeBox();
139 material = MaterialProperties();
142 void ContentFeatures::serialize(std::ostream &os)
144 writeU8(os, 0); // version
145 writeU8(os, drawtype);
146 writeF1000(os, visual_scale);
148 for(u32 i=0; i<6; i++)
149 os<<serializeString(tname_tiles[i]);
150 os<<serializeString(tname_inventory);
151 writeU8(os, CF_SPECIAL_COUNT);
152 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
153 mspec_special[i].serialize(os);
156 writeU8(os, post_effect_color.getAlpha());
157 writeU8(os, post_effect_color.getRed());
158 writeU8(os, post_effect_color.getGreen());
159 writeU8(os, post_effect_color.getBlue());
160 writeU8(os, param_type);
161 writeU8(os, is_ground_content);
162 writeU8(os, light_propagates);
163 writeU8(os, sunlight_propagates);
164 writeU8(os, walkable);
165 writeU8(os, pointable);
166 writeU8(os, diggable);
167 writeU8(os, climbable);
168 writeU8(os, buildable_to);
169 writeU8(os, wall_mounted);
170 writeU8(os, air_equivalent);
171 writeU8(os, often_contains_mineral);
172 os<<serializeString(dug_item);
173 os<<serializeString(extra_dug_item);
174 writeS32(os, extra_dug_item_rarity);
175 if(initial_metadata){
177 initial_metadata->serialize(os);
181 writeU8(os, liquid_type);
182 writeU16(os, liquid_alternative_flowing);
183 writeU16(os, liquid_alternative_source);
184 writeU8(os, liquid_viscosity);
185 writeU8(os, light_source);
186 writeU32(os, damage_per_second);
187 selection_box.serialize(os);
188 material.serialize(os);
191 void ContentFeatures::deSerialize(std::istream &is, IGameDef *gamedef)
193 int version = readU8(is);
195 throw SerializationError("unsupported ContentFeatures version");
196 drawtype = (enum NodeDrawType)readU8(is);
197 visual_scale = readF1000(is);
199 throw SerializationError("unsupported tile count");
200 for(u32 i=0; i<6; i++)
201 tname_tiles[i] = deSerializeString(is);
202 tname_inventory = deSerializeString(is);
203 if(readU8(is) != CF_SPECIAL_COUNT)
204 throw SerializationError("unsupported CF_SPECIAL_COUNT");
205 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
206 mspec_special[i].deSerialize(is);
209 post_effect_color.setAlpha(readU8(is));
210 post_effect_color.setRed(readU8(is));
211 post_effect_color.setGreen(readU8(is));
212 post_effect_color.setBlue(readU8(is));
213 param_type = (enum ContentParamType)readU8(is);
214 is_ground_content = readU8(is);
215 light_propagates = readU8(is);
216 sunlight_propagates = readU8(is);
217 walkable = readU8(is);
218 pointable = readU8(is);
219 diggable = readU8(is);
220 climbable = readU8(is);
221 buildable_to = readU8(is);
222 wall_mounted = readU8(is);
223 air_equivalent = readU8(is);
224 often_contains_mineral = readU8(is);
225 dug_item = deSerializeString(is);
226 extra_dug_item = deSerializeString(is);
227 extra_dug_item_rarity = readS32(is);
229 initial_metadata = NodeMetadata::deSerialize(is, gamedef);
231 initial_metadata = NULL;
233 liquid_type = (enum LiquidType)readU8(is);
234 liquid_alternative_flowing = readU16(is);
235 liquid_alternative_source = readU16(is);
236 liquid_viscosity = readU8(is);
237 light_source = readU8(is);
238 damage_per_second = readU32(is);
239 selection_box.deSerialize(is);
240 material.deSerialize(is);
243 void ContentFeatures::setTexture(u16 i, std::string name)
245 used_texturenames.insert(name);
246 tname_tiles[i] = name;
247 if(tname_inventory == "")
248 tname_inventory = name;
251 void ContentFeatures::setInventoryTexture(std::string imgname)
253 tname_inventory = imgname + "^[forcesingle";
256 void ContentFeatures::setInventoryTextureCube(std::string top,
257 std::string left, std::string right)
259 str_replace_char(top, '^', '&');
260 str_replace_char(left, '^', '&');
261 str_replace_char(right, '^', '&');
263 std::string imgname_full;
264 imgname_full += "[inventorycube{";
267 imgname_full += left;
269 imgname_full += right;
270 tname_inventory = imgname_full;
273 class CNodeDefManager: public IWritableNodeDefManager
278 for(u16 i=0; i<=MAX_CONTENT; i++)
280 ContentFeatures *f = &m_content_features[i];
281 f->reset(); // Reset to defaults
282 f->modified = false; // Not changed from default
283 if(i == CONTENT_IGNORE || i == CONTENT_AIR){
284 f->drawtype = NDT_AIRLIKE;
287 f->setAllTextures("unknown_block.png");
288 //f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
291 // Make CONTENT_IGNORE to not block the view when occlusion culling
292 m_content_features[CONTENT_IGNORE].solidness = 0;
299 virtual ~CNodeDefManager()
302 virtual IWritableNodeDefManager* clone()
304 CNodeDefManager *mgr = new CNodeDefManager();
305 for(u16 i=0; i<=MAX_CONTENT; i++)
311 virtual const ContentFeatures& get(content_t c) const
313 assert(c <= MAX_CONTENT);
314 return m_content_features[c];
316 virtual const ContentFeatures& get(const MapNode &n) const
318 return get(n.getContent());
321 virtual void set(content_t c, const ContentFeatures &def)
323 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
324 assert(c <= MAX_CONTENT);
325 m_content_features[c] = def;
327 virtual ContentFeatures* getModifiable(content_t c)
329 assert(c <= MAX_CONTENT);
330 m_content_features[c].modified = true; // Assume it is modified
331 return &m_content_features[c];
333 virtual void updateTextures(ITextureSource *tsrc)
336 infostream<<"CNodeDefManager::updateTextures(): Updating "
337 <<"textures in node definitions"<<std::endl;
339 bool new_style_water = g_settings->getBool("new_style_water");
340 bool new_style_leaves = g_settings->getBool("new_style_leaves");
341 bool opaque_water = g_settings->getBool("opaque_water");
343 for(u16 i=0; i<=MAX_CONTENT; i++)
345 ContentFeatures *f = &m_content_features[i];
356 assert(f->liquid_type == LIQUID_SOURCE);
367 case NDT_FLOWINGLIQUID:
368 assert(f->liquid_type == LIQUID_FLOWING);
375 f->visual_solidness = 1;
379 f->visual_solidness = 1;
381 case NDT_ALLFACES_OPTIONAL:
382 if(new_style_leaves){
383 f->drawtype = NDT_ALLFACES;
385 f->visual_solidness = 1;
387 f->drawtype = NDT_NORMAL;
389 for(u32 i=0; i<6; i++){
390 f->tname_tiles[i] = std::string("[noalpha:")
405 if(f->tname_inventory != "")
406 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
408 f->inventory_texture = NULL;
410 for(u16 j=0; j<6; j++){
411 if(f->tname_tiles[j] == "")
413 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
414 f->tiles[j].alpha = f->alpha;
416 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
418 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
419 if(f->backface_culling)
420 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
422 f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
425 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
427 if(f->special_aps[j]){
428 delete f->special_aps[j];
429 f->special_aps[j] = NULL;
431 if(f->special_materials[j]){
432 delete f->special_materials[j];
433 f->special_materials[j] = NULL;
435 // Skip if should not exist
436 if(f->mspec_special[j].tname == "")
439 f->special_aps[j] = new AtlasPointer(
440 tsrc->getTexture(f->mspec_special[j].tname));
441 f->special_materials[j] = new video::SMaterial;
442 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
443 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
444 f->mspec_special[j].backface_culling);
445 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
446 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
447 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
449 f->special_materials[j]->MaterialType =
450 video::EMT_TRANSPARENT_VERTEX_ALPHA;
455 void serialize(std::ostream &os)
458 std::ostringstream tmp_os(std::ios::binary);
459 for(u16 i=0; i<=MAX_CONTENT; i++)
461 ContentFeatures *f = &m_content_features[i];
465 f->serialize(tmp_os);
469 os<<serializeLongString(tmp_os.str());
471 void deSerialize(std::istream &is, IGameDef *gamedef)
474 u16 count = readU16(is);
475 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
476 for(u16 n=0; n<count; n++){
477 u16 i = readU16(tmp_is);
479 errorstream<<"ContentFeatures::deSerialize(): "
480 <<"Too large content id: "<<i<<std::endl;
483 ContentFeatures *f = &m_content_features[i];
484 f->deSerialize(tmp_is, gamedef);
489 ContentFeatures m_content_features[MAX_CONTENT+1];
492 IWritableNodeDefManager* createNodeDefManager()
494 return new CNodeDefManager();