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"
29 #include "nameidmapping.h"
35 void NodeBox::serialize(std::ostream &os) const
37 writeU8(os, 0); // version
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);
49 void NodeBox::deSerialize(std::istream &is)
51 int version = readU8(is);
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);
69 void MaterialSpec::serialize(std::ostream &os) const
71 os<<serializeString(tname);
72 writeU8(os, backface_culling);
75 void MaterialSpec::deSerialize(std::istream &is)
77 tname = deSerializeString(is);
78 backface_culling = readU8(is);
85 ContentFeatures::ContentFeatures()
90 ContentFeatures::~ContentFeatures()
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();
120 NOTE: Most of this is always overridden by the default values given
124 drawtype = NDT_NORMAL;
126 for(u32 i=0; i<6; i++)
128 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
129 mspec_special[j] = MaterialSpec();
130 tname_inventory = "";
132 post_effect_color = video::SColor(0, 0, 0, 0);
133 param_type = CPT_NONE;
134 is_ground_content = false;
135 light_propagates = false;
136 sunlight_propagates = false;
141 buildable_to = false;
142 wall_mounted = false;
143 often_contains_mineral = false;
146 extra_dug_item_rarity = 2;
148 liquid_type = LIQUID_NONE;
149 liquid_alternative_flowing = "";
150 liquid_alternative_source = "";
151 liquid_viscosity = 0;
153 damage_per_second = 0;
154 selection_box = NodeBox();
155 material = MaterialProperties();
156 cookresult_item = ""; // Cannot be cooked
157 furnace_cooktime = 3.0;
158 furnace_burntime = -1.0; // Cannot be burned
161 void ContentFeatures::serialize(std::ostream &os)
163 writeU8(os, 0); // version
164 os<<serializeString(name);
165 writeU8(os, drawtype);
166 writeF1000(os, visual_scale);
168 for(u32 i=0; i<6; i++)
169 os<<serializeString(tname_tiles[i]);
170 os<<serializeString(tname_inventory);
171 writeU8(os, CF_SPECIAL_COUNT);
172 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
173 mspec_special[i].serialize(os);
176 writeU8(os, post_effect_color.getAlpha());
177 writeU8(os, post_effect_color.getRed());
178 writeU8(os, post_effect_color.getGreen());
179 writeU8(os, post_effect_color.getBlue());
180 writeU8(os, param_type);
181 writeU8(os, is_ground_content);
182 writeU8(os, light_propagates);
183 writeU8(os, sunlight_propagates);
184 writeU8(os, walkable);
185 writeU8(os, pointable);
186 writeU8(os, diggable);
187 writeU8(os, climbable);
188 writeU8(os, buildable_to);
189 writeU8(os, wall_mounted);
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 os<<serializeString(metadata_name);
195 writeU8(os, liquid_type);
196 os<<serializeString(liquid_alternative_flowing);
197 os<<serializeString(liquid_alternative_source);
198 writeU8(os, liquid_viscosity);
199 writeU8(os, light_source);
200 writeU32(os, damage_per_second);
201 selection_box.serialize(os);
202 material.serialize(os);
203 os<<serializeString(cookresult_item);
204 writeF1000(os, furnace_cooktime);
205 writeF1000(os, furnace_burntime);
208 void ContentFeatures::deSerialize(std::istream &is, IGameDef *gamedef)
210 int version = readU8(is);
212 throw SerializationError("unsupported ContentFeatures version");
213 name = deSerializeString(is);
214 drawtype = (enum NodeDrawType)readU8(is);
215 visual_scale = readF1000(is);
217 throw SerializationError("unsupported tile count");
218 for(u32 i=0; i<6; i++)
219 tname_tiles[i] = deSerializeString(is);
220 tname_inventory = deSerializeString(is);
221 if(readU8(is) != CF_SPECIAL_COUNT)
222 throw SerializationError("unsupported CF_SPECIAL_COUNT");
223 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
224 mspec_special[i].deSerialize(is);
227 post_effect_color.setAlpha(readU8(is));
228 post_effect_color.setRed(readU8(is));
229 post_effect_color.setGreen(readU8(is));
230 post_effect_color.setBlue(readU8(is));
231 param_type = (enum ContentParamType)readU8(is);
232 is_ground_content = readU8(is);
233 light_propagates = readU8(is);
234 sunlight_propagates = readU8(is);
235 walkable = readU8(is);
236 pointable = readU8(is);
237 diggable = readU8(is);
238 climbable = readU8(is);
239 buildable_to = readU8(is);
240 wall_mounted = readU8(is);
241 often_contains_mineral = readU8(is);
242 dug_item = deSerializeString(is);
243 extra_dug_item = deSerializeString(is);
244 extra_dug_item_rarity = readS32(is);
245 metadata_name = deSerializeString(is);
246 liquid_type = (enum LiquidType)readU8(is);
247 liquid_alternative_flowing = deSerializeString(is);
248 liquid_alternative_source = deSerializeString(is);
249 liquid_viscosity = readU8(is);
250 light_source = readU8(is);
251 damage_per_second = readU32(is);
252 selection_box.deSerialize(is);
253 material.deSerialize(is);
254 cookresult_item = deSerializeString(is);
255 furnace_cooktime = readF1000(is);
256 furnace_burntime = readF1000(is);
259 void ContentFeatures::setTexture(u16 i, std::string name)
261 used_texturenames.insert(name);
262 tname_tiles[i] = name;
263 if(tname_inventory == "")
264 tname_inventory = name;
267 void ContentFeatures::setAllTextures(std::string name)
269 for(u16 i=0; i<6; i++)
271 // Force inventory texture too
272 setInventoryTexture(name);
275 void ContentFeatures::setSpecialMaterial(u16 i, const MaterialSpec &mspec)
277 assert(i < CF_SPECIAL_COUNT);
278 mspec_special[i] = mspec;
281 void ContentFeatures::setInventoryTexture(std::string imgname)
283 tname_inventory = imgname + "^[forcesingle";
286 void ContentFeatures::setInventoryTextureCube(std::string top,
287 std::string left, std::string right)
289 str_replace_char(top, '^', '&');
290 str_replace_char(left, '^', '&');
291 str_replace_char(right, '^', '&');
293 std::string imgname_full;
294 imgname_full += "[inventorycube{";
297 imgname_full += left;
299 imgname_full += right;
300 tname_inventory = imgname_full;
307 class CNodeDefManager: public IWritableNodeDefManager
312 m_name_id_mapping.clear();
313 for(u16 i=0; i<=MAX_CONTENT; i++)
315 ContentFeatures &f = m_content_features[i];
316 f.reset(); // Reset to defaults
317 f.setAllTextures("unknown_block.png");
324 f.drawtype = NDT_AIRLIKE;
325 f.param_type = CPT_LIGHT;
326 f.light_propagates = true;
327 f.sunlight_propagates = true;
331 f.buildable_to = true;
332 // Insert directly into containers
333 content_t c = CONTENT_AIR;
334 m_content_features[c] = f;
335 m_name_id_mapping.set(c, f.name);
337 // Set CONTENT_IGNORE
341 f.drawtype = NDT_AIRLIKE;
342 f.param_type = CPT_NONE;
343 f.light_propagates = false;
344 f.sunlight_propagates = false;
348 // A way to remove accidental CONTENT_IGNOREs
349 f.buildable_to = true;
350 // Insert directly into containers
351 content_t c = CONTENT_IGNORE;
352 m_content_features[c] = f;
353 m_name_id_mapping.set(c, f.name);
356 // CONTENT_IGNORE = not found
357 content_t getFreeId(bool require_full_param2)
359 // If allowed, first search in the large 4-byte-param2 pool
360 if(!require_full_param2){
361 for(u16 i=0x800; i<=0xfff; i++){
362 const ContentFeatures &f = m_content_features[i];
367 // Then search from the small 8-byte-param2 pool
368 for(u16 i=0; i<=125; i++){
369 const ContentFeatures &f = m_content_features[i];
373 return CONTENT_IGNORE;
379 virtual ~CNodeDefManager()
382 virtual IWritableNodeDefManager* clone()
384 CNodeDefManager *mgr = new CNodeDefManager();
385 for(u16 i=0; i<=MAX_CONTENT; i++)
391 virtual const ContentFeatures& get(content_t c) const
393 assert(c <= MAX_CONTENT);
394 return m_content_features[c];
396 virtual const ContentFeatures& get(const MapNode &n) const
398 return get(n.getContent());
400 virtual bool getId(const std::string &name, content_t &result) const
402 return m_name_id_mapping.getId(name, result);
404 virtual content_t getId(const std::string &name) const
406 content_t id = CONTENT_IGNORE;
410 virtual const ContentFeatures& get(const std::string &name) const
412 content_t id = CONTENT_IGNORE;
416 // IWritableNodeDefManager
417 virtual void set(content_t c, const ContentFeatures &def)
419 infostream<<"registerNode: registering content id \""<<c
420 <<"\": name=\""<<def.name<<"\""<<std::endl;
421 assert(c <= MAX_CONTENT);
422 // Don't allow redefining CONTENT_IGNORE (but allow air)
423 if(def.name == "ignore" || c == CONTENT_IGNORE){
424 infostream<<"registerNode: WARNING: Ignoring "
425 <<"CONTENT_IGNORE redefinition"<<std::endl;
428 // Check that the special contents are not redefined as different id
429 // because it would mess up everything
430 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
431 (def.name == "air" && c != CONTENT_AIR)){
432 errorstream<<"registerNode: IGNORING ERROR: "
433 <<"trying to register built-in type \""
434 <<def.name<<"\" as different id"<<std::endl;
437 m_content_features[c] = def;
439 m_name_id_mapping.set(c, def.name);
441 virtual content_t set(const std::string &name,
442 const ContentFeatures &def)
444 assert(name == def.name);
445 u16 id = CONTENT_IGNORE;
446 bool found = m_name_id_mapping.getId(name, id);
448 // Determine if full param2 is required
449 bool require_full_param2 = (
450 def.liquid_type == LIQUID_FLOWING
452 def.drawtype == NDT_FLOWINGLIQUID
454 def.drawtype == NDT_TORCHLIKE
456 def.drawtype == NDT_SIGNLIKE
459 id = getFreeId(require_full_param2);
460 if(id == CONTENT_IGNORE)
461 return CONTENT_IGNORE;
463 m_name_id_mapping.set(id, name);
468 virtual content_t allocateDummy(const std::string &name)
473 f.setAllTextures("unknown_block.png");
476 virtual void updateTextures(ITextureSource *tsrc)
479 infostream<<"CNodeDefManager::updateTextures(): Updating "
480 <<"textures in node definitions"<<std::endl;
482 bool new_style_water = g_settings->getBool("new_style_water");
483 bool new_style_leaves = g_settings->getBool("new_style_leaves");
484 bool opaque_water = g_settings->getBool("opaque_water");
486 for(u16 i=0; i<=MAX_CONTENT; i++)
488 ContentFeatures *f = &m_content_features[i];
499 assert(f->liquid_type == LIQUID_SOURCE);
508 f->backface_culling = false;
511 case NDT_FLOWINGLIQUID:
512 assert(f->liquid_type == LIQUID_FLOWING);
519 f->visual_solidness = 1;
523 f->visual_solidness = 1;
525 case NDT_ALLFACES_OPTIONAL:
526 if(new_style_leaves){
527 f->drawtype = NDT_ALLFACES;
529 f->visual_solidness = 1;
531 f->drawtype = NDT_NORMAL;
533 for(u32 i=0; i<6; i++){
534 f->tname_tiles[i] = f->tname_tiles[i]
535 + std::string("^[noalpha");
549 if(f->tname_inventory != "")
550 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
552 f->inventory_texture = NULL;
554 for(u16 j=0; j<6; j++){
555 if(f->tname_tiles[j] == "")
557 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
558 f->tiles[j].alpha = f->alpha;
560 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
562 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
563 if(f->backface_culling)
564 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
566 f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
569 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
571 if(f->special_aps[j]){
572 delete f->special_aps[j];
573 f->special_aps[j] = NULL;
575 if(f->special_materials[j]){
576 delete f->special_materials[j];
577 f->special_materials[j] = NULL;
579 // Skip if should not exist
580 if(f->mspec_special[j].tname == "")
583 f->special_aps[j] = new AtlasPointer(
584 tsrc->getTexture(f->mspec_special[j].tname));
585 f->special_materials[j] = new video::SMaterial;
586 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
587 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
588 f->mspec_special[j].backface_culling);
589 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
590 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
591 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
593 f->special_materials[j]->MaterialType =
594 video::EMT_TRANSPARENT_VERTEX_ALPHA;
599 void serialize(std::ostream &os)
602 std::ostringstream tmp_os(std::ios::binary);
603 for(u16 i=0; i<=MAX_CONTENT; i++)
605 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
607 ContentFeatures *f = &m_content_features[i];
611 f->serialize(tmp_os);
615 os<<serializeLongString(tmp_os.str());
617 void deSerialize(std::istream &is, IGameDef *gamedef)
620 u16 count = readU16(is);
621 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
622 for(u16 n=0; n<count; n++){
623 u16 i = readU16(tmp_is);
625 errorstream<<"ContentFeatures::deSerialize(): "
626 <<"Too large content id: "<<i<<std::endl;
629 /*// Do not deserialize special types
630 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
632 ContentFeatures *f = &m_content_features[i];
633 f->deSerialize(tmp_is, gamedef);
635 m_name_id_mapping.set(i, f->name);
639 ContentFeatures m_content_features[MAX_CONTENT+1];
640 NameIdMapping m_name_id_mapping;
643 IWritableNodeDefManager* createNodeDefManager()
645 return new CNodeDefManager();