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
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 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
107 special_materials[j] = NULL;
108 special_aps[j] = NULL;
111 visual_solidness = 0;
112 backface_culling = true;
117 NOTE: Most of this is always overridden by the default values given
122 drawtype = NDT_NORMAL;
124 for(u32 i=0; i<6; i++)
126 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
127 mspec_special[j] = MaterialSpec();
129 post_effect_color = video::SColor(0, 0, 0, 0);
130 param_type = CPT_NONE;
131 param_type_2 = CPT2_NONE;
132 is_ground_content = false;
133 light_propagates = false;
134 sunlight_propagates = false;
139 buildable_to = false;
141 liquid_type = LIQUID_NONE;
142 liquid_alternative_flowing = "";
143 liquid_alternative_source = "";
144 liquid_viscosity = 0;
146 damage_per_second = 0;
147 selection_box = NodeBox();
148 legacy_facedir_simple = false;
149 legacy_wallmounted = false;
152 void ContentFeatures::serialize(std::ostream &os)
154 writeU8(os, 2); // version
155 os<<serializeString(name);
156 writeU16(os, groups.size());
157 for(std::map<std::string, int>::const_iterator
158 i = groups.begin(); i != groups.end(); i++){
159 os<<serializeString(i->first);
160 writeS16(os, i->second);
162 writeU8(os, drawtype);
163 writeF1000(os, visual_scale);
165 for(u32 i=0; i<6; i++)
166 os<<serializeString(tname_tiles[i]);
167 writeU8(os, CF_SPECIAL_COUNT);
168 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
169 mspec_special[i].serialize(os);
172 writeU8(os, post_effect_color.getAlpha());
173 writeU8(os, post_effect_color.getRed());
174 writeU8(os, post_effect_color.getGreen());
175 writeU8(os, post_effect_color.getBlue());
176 writeU8(os, param_type);
177 writeU8(os, param_type_2);
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 os<<serializeString(metadata_name);
187 writeU8(os, liquid_type);
188 os<<serializeString(liquid_alternative_flowing);
189 os<<serializeString(liquid_alternative_source);
190 writeU8(os, liquid_viscosity);
191 writeU8(os, light_source);
192 writeU32(os, damage_per_second);
193 selection_box.serialize(os);
194 writeU8(os, legacy_facedir_simple);
195 writeU8(os, legacy_wallmounted);
198 void ContentFeatures::deSerialize(std::istream &is)
200 int version = readU8(is);
202 throw SerializationError("unsupported ContentFeatures version");
203 name = deSerializeString(is);
205 u32 groups_size = readU16(is);
206 for(u32 i=0; i<groups_size; i++){
207 std::string name = deSerializeString(is);
208 int value = readS16(is);
209 groups[name] = value;
211 drawtype = (enum NodeDrawType)readU8(is);
212 visual_scale = readF1000(is);
214 throw SerializationError("unsupported tile count");
215 for(u32 i=0; i<6; i++)
216 tname_tiles[i] = deSerializeString(is);
217 if(readU8(is) != CF_SPECIAL_COUNT)
218 throw SerializationError("unsupported CF_SPECIAL_COUNT");
219 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
220 mspec_special[i].deSerialize(is);
223 post_effect_color.setAlpha(readU8(is));
224 post_effect_color.setRed(readU8(is));
225 post_effect_color.setGreen(readU8(is));
226 post_effect_color.setBlue(readU8(is));
227 param_type = (enum ContentParamType)readU8(is);
228 param_type_2 = (enum ContentParamType2)readU8(is);
229 is_ground_content = readU8(is);
230 light_propagates = readU8(is);
231 sunlight_propagates = readU8(is);
232 walkable = readU8(is);
233 pointable = readU8(is);
234 diggable = readU8(is);
235 climbable = readU8(is);
236 buildable_to = readU8(is);
237 metadata_name = deSerializeString(is);
238 liquid_type = (enum LiquidType)readU8(is);
239 liquid_alternative_flowing = deSerializeString(is);
240 liquid_alternative_source = deSerializeString(is);
241 liquid_viscosity = readU8(is);
242 light_source = readU8(is);
243 damage_per_second = readU32(is);
244 selection_box.deSerialize(is);
245 legacy_facedir_simple = readU8(is);
246 legacy_wallmounted = readU8(is);
253 class CNodeDefManager: public IWritableNodeDefManager
258 m_name_id_mapping.clear();
259 m_name_id_mapping_with_aliases.clear();
261 for(u16 i=0; i<=MAX_CONTENT; i++)
263 ContentFeatures &f = m_content_features[i];
264 f.reset(); // Reset to defaults
271 f.drawtype = NDT_AIRLIKE;
272 f.param_type = CPT_LIGHT;
273 f.light_propagates = true;
274 f.sunlight_propagates = true;
278 f.buildable_to = true;
279 // Insert directly into containers
280 content_t c = CONTENT_AIR;
281 m_content_features[c] = f;
282 addNameIdMapping(c, f.name);
284 // Set CONTENT_IGNORE
288 f.drawtype = NDT_AIRLIKE;
289 f.param_type = CPT_NONE;
290 f.light_propagates = false;
291 f.sunlight_propagates = false;
295 // A way to remove accidental CONTENT_IGNOREs
296 f.buildable_to = true;
297 // Insert directly into containers
298 content_t c = CONTENT_IGNORE;
299 m_content_features[c] = f;
300 addNameIdMapping(c, f.name);
303 // CONTENT_IGNORE = not found
304 content_t getFreeId(bool require_full_param2)
306 // If allowed, first search in the large 4-bit-param2 pool
307 if(!require_full_param2){
308 for(u16 i=0x800; i<=0xfff; i++){
309 const ContentFeatures &f = m_content_features[i];
314 // Then search from the small 8-bit-param2 pool
315 for(u16 i=0; i<=125; i++){
316 const ContentFeatures &f = m_content_features[i];
320 return CONTENT_IGNORE;
326 virtual ~CNodeDefManager()
329 virtual IWritableNodeDefManager* clone()
331 CNodeDefManager *mgr = new CNodeDefManager();
332 for(u16 i=0; i<=MAX_CONTENT; i++)
338 virtual const ContentFeatures& get(content_t c) const
340 assert(c <= MAX_CONTENT);
341 return m_content_features[c];
343 virtual const ContentFeatures& get(const MapNode &n) const
345 return get(n.getContent());
347 virtual bool getId(const std::string &name, content_t &result) const
349 std::map<std::string, content_t>::const_iterator
350 i = m_name_id_mapping_with_aliases.find(name);
351 if(i == m_name_id_mapping_with_aliases.end())
356 virtual content_t getId(const std::string &name) const
358 content_t id = CONTENT_IGNORE;
362 virtual const ContentFeatures& get(const std::string &name) const
364 content_t id = CONTENT_IGNORE;
368 // IWritableNodeDefManager
369 virtual void set(content_t c, const ContentFeatures &def)
371 infostream<<"registerNode: registering content id \""<<c
372 <<"\": name=\""<<def.name<<"\""<<std::endl;
373 assert(c <= MAX_CONTENT);
374 // Don't allow redefining CONTENT_IGNORE (but allow air)
375 if(def.name == "ignore" || c == CONTENT_IGNORE){
376 infostream<<"registerNode: WARNING: Ignoring "
377 <<"CONTENT_IGNORE redefinition"<<std::endl;
380 // Check that the special contents are not redefined as different id
381 // because it would mess up everything
382 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
383 (def.name == "air" && c != CONTENT_AIR)){
384 errorstream<<"registerNode: IGNORING ERROR: "
385 <<"trying to register built-in type \""
386 <<def.name<<"\" as different id"<<std::endl;
389 m_content_features[c] = def;
391 addNameIdMapping(c, def.name);
393 virtual content_t set(const std::string &name,
394 const ContentFeatures &def)
396 assert(name == def.name);
397 u16 id = CONTENT_IGNORE;
398 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
400 // Determine if full param2 is required
401 bool require_full_param2 = (
402 def.param_type_2 == CPT2_FULL
404 def.param_type_2 == CPT2_FLOWINGLIQUID
406 def.legacy_wallmounted
409 id = getFreeId(require_full_param2);
410 if(id == CONTENT_IGNORE)
411 return CONTENT_IGNORE;
413 addNameIdMapping(id, name);
418 virtual content_t allocateDummy(const std::string &name)
425 virtual void updateAliases(IItemDefManager *idef)
427 std::set<std::string> all = idef->getAll();
428 m_name_id_mapping_with_aliases.clear();
429 for(std::set<std::string>::iterator
430 i = all.begin(); i != all.end(); i++)
432 std::string name = *i;
433 std::string convert_to = idef->getAlias(name);
435 if(m_name_id_mapping.getId(convert_to, id))
437 m_name_id_mapping_with_aliases.insert(
438 std::make_pair(name, id));
442 virtual void updateTextures(ITextureSource *tsrc)
445 infostream<<"CNodeDefManager::updateTextures(): Updating "
446 <<"textures in node definitions"<<std::endl;
448 bool new_style_water = g_settings->getBool("new_style_water");
449 bool new_style_leaves = g_settings->getBool("new_style_leaves");
450 bool opaque_water = g_settings->getBool("opaque_water");
452 for(u16 i=0; i<=MAX_CONTENT; i++)
454 ContentFeatures *f = &m_content_features[i];
456 std::string tname_tiles[6];
457 for(u32 j=0; j<6; j++)
459 tname_tiles[j] = f->tname_tiles[j];
460 if(tname_tiles[j] == "")
461 tname_tiles[j] = "unknown_block.png";
473 assert(f->liquid_type == LIQUID_SOURCE);
482 f->backface_culling = false;
485 case NDT_FLOWINGLIQUID:
486 assert(f->liquid_type == LIQUID_FLOWING);
493 f->visual_solidness = 1;
497 f->visual_solidness = 1;
499 case NDT_ALLFACES_OPTIONAL:
500 if(new_style_leaves){
501 f->drawtype = NDT_ALLFACES;
503 f->visual_solidness = 1;
505 f->drawtype = NDT_NORMAL;
507 for(u32 i=0; i<6; i++){
508 tname_tiles[i] += std::string("^[noalpha");
522 for(u16 j=0; j<6; j++){
523 f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]);
524 f->tiles[j].alpha = f->alpha;
526 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
528 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
529 if(f->backface_culling)
530 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
532 f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
535 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
537 if(f->special_aps[j]){
538 delete f->special_aps[j];
539 f->special_aps[j] = NULL;
541 if(f->special_materials[j]){
542 delete f->special_materials[j];
543 f->special_materials[j] = NULL;
545 // Skip if should not exist
546 if(f->mspec_special[j].tname == "")
549 f->special_aps[j] = new AtlasPointer(
550 tsrc->getTexture(f->mspec_special[j].tname));
551 f->special_materials[j] = new video::SMaterial;
552 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
553 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
554 f->mspec_special[j].backface_culling);
555 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
556 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
557 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
559 f->special_materials[j]->MaterialType =
560 video::EMT_TRANSPARENT_VERTEX_ALPHA;
565 void serialize(std::ostream &os)
568 std::ostringstream tmp_os(std::ios::binary);
569 for(u16 i=0; i<=MAX_CONTENT; i++)
571 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
573 ContentFeatures *f = &m_content_features[i];
577 f->serialize(tmp_os);
581 os<<serializeLongString(tmp_os.str());
583 void deSerialize(std::istream &is)
586 u16 count = readU16(is);
587 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
588 for(u16 n=0; n<count; n++){
589 u16 i = readU16(tmp_is);
591 errorstream<<"ContentFeatures::deSerialize(): "
592 <<"Too large content id: "<<i<<std::endl;
595 /*// Do not deserialize special types
596 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
598 ContentFeatures *f = &m_content_features[i];
599 f->deSerialize(tmp_is);
601 addNameIdMapping(i, f->name);
605 void addNameIdMapping(content_t i, std::string name)
607 m_name_id_mapping.set(i, name);
608 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
611 // Features indexed by id
612 ContentFeatures m_content_features[MAX_CONTENT+1];
613 // A mapping for fast converting back and forth between names and ids
614 NameIdMapping m_name_id_mapping;
615 // Like m_name_id_mapping, but only from names to ids, and includes
616 // item aliases too. Updated by updateAliases()
617 // Note: Not serialized.
618 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
621 IWritableNodeDefManager* createNodeDefManager()
623 return new CNodeDefManager();