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);
82 SimpleSoundSpec serialization
85 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
88 os<<serializeString(ss.name);
89 writeF1000(os, ss.gain);
91 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
93 ss.name = deSerializeString(is);
94 ss.gain = readF1000(is);
101 ContentFeatures::ContentFeatures()
106 ContentFeatures::~ContentFeatures()
110 void ContentFeatures::reset()
117 visual_solidness = 0;
118 backface_culling = true;
123 NOTE: Most of this is always overridden by the default values given
128 // Unknown nodes can be dug
129 groups["dig_immediate"] = 2;
130 drawtype = NDT_NORMAL;
132 for(u32 i=0; i<6; i++)
134 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
135 mspec_special[j] = MaterialSpec();
137 post_effect_color = video::SColor(0, 0, 0, 0);
138 param_type = CPT_NONE;
139 param_type_2 = CPT2_NONE;
140 is_ground_content = false;
141 light_propagates = false;
142 sunlight_propagates = false;
147 buildable_to = false;
149 liquid_type = LIQUID_NONE;
150 liquid_alternative_flowing = "";
151 liquid_alternative_source = "";
152 liquid_viscosity = 0;
154 damage_per_second = 0;
155 selection_box = NodeBox();
156 legacy_facedir_simple = false;
157 legacy_wallmounted = false;
158 sound_footstep = SimpleSoundSpec();
159 sound_dug = SimpleSoundSpec();
162 void ContentFeatures::serialize(std::ostream &os)
164 writeU8(os, 2); // version
165 os<<serializeString(name);
166 writeU16(os, groups.size());
167 for(ItemGroupList::const_iterator
168 i = groups.begin(); i != groups.end(); i++){
169 os<<serializeString(i->first);
170 writeS16(os, i->second);
172 writeU8(os, drawtype);
173 writeF1000(os, visual_scale);
175 for(u32 i=0; i<6; i++)
176 os<<serializeString(tname_tiles[i]);
177 writeU8(os, CF_SPECIAL_COUNT);
178 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
179 mspec_special[i].serialize(os);
182 writeU8(os, post_effect_color.getAlpha());
183 writeU8(os, post_effect_color.getRed());
184 writeU8(os, post_effect_color.getGreen());
185 writeU8(os, post_effect_color.getBlue());
186 writeU8(os, param_type);
187 writeU8(os, param_type_2);
188 writeU8(os, is_ground_content);
189 writeU8(os, light_propagates);
190 writeU8(os, sunlight_propagates);
191 writeU8(os, walkable);
192 writeU8(os, pointable);
193 writeU8(os, diggable);
194 writeU8(os, climbable);
195 writeU8(os, buildable_to);
196 os<<serializeString(metadata_name);
197 writeU8(os, liquid_type);
198 os<<serializeString(liquid_alternative_flowing);
199 os<<serializeString(liquid_alternative_source);
200 writeU8(os, liquid_viscosity);
201 writeU8(os, light_source);
202 writeU32(os, damage_per_second);
203 selection_box.serialize(os);
204 writeU8(os, legacy_facedir_simple);
205 writeU8(os, legacy_wallmounted);
206 serializeSimpleSoundSpec(sound_footstep, os);
207 serializeSimpleSoundSpec(sound_dug, os);
210 void ContentFeatures::deSerialize(std::istream &is)
212 int version = readU8(is);
214 throw SerializationError("unsupported ContentFeatures version");
215 name = deSerializeString(is);
217 u32 groups_size = readU16(is);
218 for(u32 i=0; i<groups_size; i++){
219 std::string name = deSerializeString(is);
220 int value = readS16(is);
221 groups[name] = value;
223 drawtype = (enum NodeDrawType)readU8(is);
224 visual_scale = readF1000(is);
226 throw SerializationError("unsupported tile count");
227 for(u32 i=0; i<6; i++)
228 tname_tiles[i] = deSerializeString(is);
229 if(readU8(is) != CF_SPECIAL_COUNT)
230 throw SerializationError("unsupported CF_SPECIAL_COUNT");
231 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
232 mspec_special[i].deSerialize(is);
235 post_effect_color.setAlpha(readU8(is));
236 post_effect_color.setRed(readU8(is));
237 post_effect_color.setGreen(readU8(is));
238 post_effect_color.setBlue(readU8(is));
239 param_type = (enum ContentParamType)readU8(is);
240 param_type_2 = (enum ContentParamType2)readU8(is);
241 is_ground_content = readU8(is);
242 light_propagates = readU8(is);
243 sunlight_propagates = readU8(is);
244 walkable = readU8(is);
245 pointable = readU8(is);
246 diggable = readU8(is);
247 climbable = readU8(is);
248 buildable_to = readU8(is);
249 metadata_name = deSerializeString(is);
250 liquid_type = (enum LiquidType)readU8(is);
251 liquid_alternative_flowing = deSerializeString(is);
252 liquid_alternative_source = deSerializeString(is);
253 liquid_viscosity = readU8(is);
254 light_source = readU8(is);
255 damage_per_second = readU32(is);
256 selection_box.deSerialize(is);
257 legacy_facedir_simple = readU8(is);
258 legacy_wallmounted = readU8(is);
260 deSerializeSimpleSoundSpec(sound_footstep, is);
261 deSerializeSimpleSoundSpec(sound_dug, is);
262 }catch(SerializationError &e) {};
269 class CNodeDefManager: public IWritableNodeDefManager
274 m_name_id_mapping.clear();
275 m_name_id_mapping_with_aliases.clear();
277 for(u16 i=0; i<=MAX_CONTENT; i++)
279 ContentFeatures &f = m_content_features[i];
280 f.reset(); // Reset to defaults
287 f.drawtype = NDT_AIRLIKE;
288 f.param_type = CPT_LIGHT;
289 f.light_propagates = true;
290 f.sunlight_propagates = true;
294 f.buildable_to = true;
295 // Insert directly into containers
296 content_t c = CONTENT_AIR;
297 m_content_features[c] = f;
298 addNameIdMapping(c, f.name);
300 // Set CONTENT_IGNORE
304 f.drawtype = NDT_AIRLIKE;
305 f.param_type = CPT_NONE;
306 f.light_propagates = false;
307 f.sunlight_propagates = false;
311 // A way to remove accidental CONTENT_IGNOREs
312 f.buildable_to = true;
313 // Insert directly into containers
314 content_t c = CONTENT_IGNORE;
315 m_content_features[c] = f;
316 addNameIdMapping(c, f.name);
319 // CONTENT_IGNORE = not found
320 content_t getFreeId(bool require_full_param2)
322 // If allowed, first search in the large 4-bit-param2 pool
323 if(!require_full_param2){
324 for(u16 i=0x800; i<=0xfff; i++){
325 const ContentFeatures &f = m_content_features[i];
330 // Then search from the small 8-bit-param2 pool
331 for(u16 i=0; i<=125; i++){
332 const ContentFeatures &f = m_content_features[i];
336 return CONTENT_IGNORE;
342 virtual ~CNodeDefManager()
345 virtual IWritableNodeDefManager* clone()
347 CNodeDefManager *mgr = new CNodeDefManager();
348 for(u16 i=0; i<=MAX_CONTENT; i++)
354 virtual const ContentFeatures& get(content_t c) const
356 assert(c <= MAX_CONTENT);
357 return m_content_features[c];
359 virtual const ContentFeatures& get(const MapNode &n) const
361 return get(n.getContent());
363 virtual bool getId(const std::string &name, content_t &result) const
365 std::map<std::string, content_t>::const_iterator
366 i = m_name_id_mapping_with_aliases.find(name);
367 if(i == m_name_id_mapping_with_aliases.end())
372 virtual content_t getId(const std::string &name) const
374 content_t id = CONTENT_IGNORE;
378 virtual const ContentFeatures& get(const std::string &name) const
380 content_t id = CONTENT_IGNORE;
384 // IWritableNodeDefManager
385 virtual void set(content_t c, const ContentFeatures &def)
387 verbosestream<<"registerNode: registering content id \""<<c
388 <<"\": name=\""<<def.name<<"\""<<std::endl;
389 assert(c <= MAX_CONTENT);
390 // Don't allow redefining CONTENT_IGNORE (but allow air)
391 if(def.name == "ignore" || c == CONTENT_IGNORE){
392 infostream<<"registerNode: WARNING: Ignoring "
393 <<"CONTENT_IGNORE redefinition"<<std::endl;
396 // Check that the special contents are not redefined as different id
397 // because it would mess up everything
398 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
399 (def.name == "air" && c != CONTENT_AIR)){
400 errorstream<<"registerNode: IGNORING ERROR: "
401 <<"trying to register built-in type \""
402 <<def.name<<"\" as different id"<<std::endl;
405 m_content_features[c] = def;
407 addNameIdMapping(c, def.name);
409 virtual content_t set(const std::string &name,
410 const ContentFeatures &def)
412 assert(name == def.name);
413 u16 id = CONTENT_IGNORE;
414 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
416 // Determine if full param2 is required
417 bool require_full_param2 = (
418 def.param_type_2 == CPT2_FULL
420 def.param_type_2 == CPT2_FLOWINGLIQUID
422 def.legacy_wallmounted
425 id = getFreeId(require_full_param2);
426 if(id == CONTENT_IGNORE)
427 return CONTENT_IGNORE;
429 addNameIdMapping(id, name);
434 virtual content_t allocateDummy(const std::string &name)
441 virtual void updateAliases(IItemDefManager *idef)
443 std::set<std::string> all = idef->getAll();
444 m_name_id_mapping_with_aliases.clear();
445 for(std::set<std::string>::iterator
446 i = all.begin(); i != all.end(); i++)
448 std::string name = *i;
449 std::string convert_to = idef->getAlias(name);
451 if(m_name_id_mapping.getId(convert_to, id))
453 m_name_id_mapping_with_aliases.insert(
454 std::make_pair(name, id));
458 virtual void updateTextures(ITextureSource *tsrc)
461 infostream<<"CNodeDefManager::updateTextures(): Updating "
462 <<"textures in node definitions"<<std::endl;
464 bool new_style_water = g_settings->getBool("new_style_water");
465 bool new_style_leaves = g_settings->getBool("new_style_leaves");
466 bool opaque_water = g_settings->getBool("opaque_water");
468 for(u16 i=0; i<=MAX_CONTENT; i++)
470 ContentFeatures *f = &m_content_features[i];
472 std::string tname_tiles[6];
473 for(u32 j=0; j<6; j++)
475 tname_tiles[j] = f->tname_tiles[j];
476 if(tname_tiles[j] == "")
477 tname_tiles[j] = "unknown_block.png";
489 assert(f->liquid_type == LIQUID_SOURCE);
498 f->backface_culling = false;
501 case NDT_FLOWINGLIQUID:
502 assert(f->liquid_type == LIQUID_FLOWING);
509 f->visual_solidness = 1;
513 f->visual_solidness = 1;
515 case NDT_ALLFACES_OPTIONAL:
516 if(new_style_leaves){
517 f->drawtype = NDT_ALLFACES;
519 f->visual_solidness = 1;
521 f->drawtype = NDT_NORMAL;
523 for(u32 i=0; i<6; i++){
524 tname_tiles[i] += std::string("^[noalpha");
538 for(u16 j=0; j<6; j++){
539 f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]);
540 f->tiles[j].alpha = f->alpha;
542 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
544 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
545 f->tiles[j].material_flags = 0;
546 if(f->backface_culling)
547 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
550 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
551 f->special_tiles[j].texture = tsrc->getTexture(f->mspec_special[j].tname);
552 f->special_tiles[j].alpha = f->alpha;
554 f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
556 f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
557 f->special_tiles[j].material_flags = 0;
558 if(f->mspec_special[j].backface_culling)
559 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
564 void serialize(std::ostream &os)
567 std::ostringstream tmp_os(std::ios::binary);
568 for(u16 i=0; i<=MAX_CONTENT; i++)
570 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
572 ContentFeatures *f = &m_content_features[i];
576 f->serialize(tmp_os);
580 os<<serializeLongString(tmp_os.str());
582 void deSerialize(std::istream &is)
585 u16 count = readU16(is);
586 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
587 for(u16 n=0; n<count; n++){
588 u16 i = readU16(tmp_is);
590 errorstream<<"ContentFeatures::deSerialize(): "
591 <<"Too large content id: "<<i<<std::endl;
594 /*// Do not deserialize special types
595 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
597 ContentFeatures *f = &m_content_features[i];
598 f->deSerialize(tmp_is);
600 addNameIdMapping(i, f->name);
604 void addNameIdMapping(content_t i, std::string name)
606 m_name_id_mapping.set(i, name);
607 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
610 // Features indexed by id
611 ContentFeatures m_content_features[MAX_CONTENT+1];
612 // A mapping for fast converting back and forth between names and ids
613 NameIdMapping m_name_id_mapping;
614 // Like m_name_id_mapping, but only from names to ids, and includes
615 // item aliases too. Updated by updateAliases()
616 // Note: Not serialized.
617 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
620 IWritableNodeDefManager* createNodeDefManager()
622 return new CNodeDefManager();