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();
161 void ContentFeatures::serialize(std::ostream &os)
163 writeU8(os, 2); // version
164 os<<serializeString(name);
165 writeU16(os, groups.size());
166 for(ItemGroupList::const_iterator
167 i = groups.begin(); i != groups.end(); i++){
168 os<<serializeString(i->first);
169 writeS16(os, i->second);
171 writeU8(os, drawtype);
172 writeF1000(os, visual_scale);
174 for(u32 i=0; i<6; i++)
175 os<<serializeString(tname_tiles[i]);
176 writeU8(os, CF_SPECIAL_COUNT);
177 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
178 mspec_special[i].serialize(os);
181 writeU8(os, post_effect_color.getAlpha());
182 writeU8(os, post_effect_color.getRed());
183 writeU8(os, post_effect_color.getGreen());
184 writeU8(os, post_effect_color.getBlue());
185 writeU8(os, param_type);
186 writeU8(os, param_type_2);
187 writeU8(os, is_ground_content);
188 writeU8(os, light_propagates);
189 writeU8(os, sunlight_propagates);
190 writeU8(os, walkable);
191 writeU8(os, pointable);
192 writeU8(os, diggable);
193 writeU8(os, climbable);
194 writeU8(os, buildable_to);
195 os<<serializeString(metadata_name);
196 writeU8(os, liquid_type);
197 os<<serializeString(liquid_alternative_flowing);
198 os<<serializeString(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 writeU8(os, legacy_facedir_simple);
204 writeU8(os, legacy_wallmounted);
205 serializeSimpleSoundSpec(sound_footstep, os);
208 void ContentFeatures::deSerialize(std::istream &is)
210 int version = readU8(is);
212 throw SerializationError("unsupported ContentFeatures version");
213 name = deSerializeString(is);
215 u32 groups_size = readU16(is);
216 for(u32 i=0; i<groups_size; i++){
217 std::string name = deSerializeString(is);
218 int value = readS16(is);
219 groups[name] = value;
221 drawtype = (enum NodeDrawType)readU8(is);
222 visual_scale = readF1000(is);
224 throw SerializationError("unsupported tile count");
225 for(u32 i=0; i<6; i++)
226 tname_tiles[i] = deSerializeString(is);
227 if(readU8(is) != CF_SPECIAL_COUNT)
228 throw SerializationError("unsupported CF_SPECIAL_COUNT");
229 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
230 mspec_special[i].deSerialize(is);
233 post_effect_color.setAlpha(readU8(is));
234 post_effect_color.setRed(readU8(is));
235 post_effect_color.setGreen(readU8(is));
236 post_effect_color.setBlue(readU8(is));
237 param_type = (enum ContentParamType)readU8(is);
238 param_type_2 = (enum ContentParamType2)readU8(is);
239 is_ground_content = readU8(is);
240 light_propagates = readU8(is);
241 sunlight_propagates = readU8(is);
242 walkable = readU8(is);
243 pointable = readU8(is);
244 diggable = readU8(is);
245 climbable = readU8(is);
246 buildable_to = readU8(is);
247 metadata_name = deSerializeString(is);
248 liquid_type = (enum LiquidType)readU8(is);
249 liquid_alternative_flowing = deSerializeString(is);
250 liquid_alternative_source = deSerializeString(is);
251 liquid_viscosity = readU8(is);
252 light_source = readU8(is);
253 damage_per_second = readU32(is);
254 selection_box.deSerialize(is);
255 legacy_facedir_simple = readU8(is);
256 legacy_wallmounted = readU8(is);
258 deSerializeSimpleSoundSpec(sound_footstep, is);
259 }catch(SerializationError &e) {};
266 class CNodeDefManager: public IWritableNodeDefManager
271 m_name_id_mapping.clear();
272 m_name_id_mapping_with_aliases.clear();
274 for(u16 i=0; i<=MAX_CONTENT; i++)
276 ContentFeatures &f = m_content_features[i];
277 f.reset(); // Reset to defaults
284 f.drawtype = NDT_AIRLIKE;
285 f.param_type = CPT_LIGHT;
286 f.light_propagates = true;
287 f.sunlight_propagates = true;
291 f.buildable_to = true;
292 // Insert directly into containers
293 content_t c = CONTENT_AIR;
294 m_content_features[c] = f;
295 addNameIdMapping(c, f.name);
297 // Set CONTENT_IGNORE
301 f.drawtype = NDT_AIRLIKE;
302 f.param_type = CPT_NONE;
303 f.light_propagates = false;
304 f.sunlight_propagates = false;
308 // A way to remove accidental CONTENT_IGNOREs
309 f.buildable_to = true;
310 // Insert directly into containers
311 content_t c = CONTENT_IGNORE;
312 m_content_features[c] = f;
313 addNameIdMapping(c, f.name);
316 // CONTENT_IGNORE = not found
317 content_t getFreeId(bool require_full_param2)
319 // If allowed, first search in the large 4-bit-param2 pool
320 if(!require_full_param2){
321 for(u16 i=0x800; i<=0xfff; i++){
322 const ContentFeatures &f = m_content_features[i];
327 // Then search from the small 8-bit-param2 pool
328 for(u16 i=0; i<=125; i++){
329 const ContentFeatures &f = m_content_features[i];
333 return CONTENT_IGNORE;
339 virtual ~CNodeDefManager()
342 virtual IWritableNodeDefManager* clone()
344 CNodeDefManager *mgr = new CNodeDefManager();
345 for(u16 i=0; i<=MAX_CONTENT; i++)
351 virtual const ContentFeatures& get(content_t c) const
353 assert(c <= MAX_CONTENT);
354 return m_content_features[c];
356 virtual const ContentFeatures& get(const MapNode &n) const
358 return get(n.getContent());
360 virtual bool getId(const std::string &name, content_t &result) const
362 std::map<std::string, content_t>::const_iterator
363 i = m_name_id_mapping_with_aliases.find(name);
364 if(i == m_name_id_mapping_with_aliases.end())
369 virtual content_t getId(const std::string &name) const
371 content_t id = CONTENT_IGNORE;
375 virtual const ContentFeatures& get(const std::string &name) const
377 content_t id = CONTENT_IGNORE;
381 // IWritableNodeDefManager
382 virtual void set(content_t c, const ContentFeatures &def)
384 verbosestream<<"registerNode: registering content id \""<<c
385 <<"\": name=\""<<def.name<<"\""<<std::endl;
386 assert(c <= MAX_CONTENT);
387 // Don't allow redefining CONTENT_IGNORE (but allow air)
388 if(def.name == "ignore" || c == CONTENT_IGNORE){
389 infostream<<"registerNode: WARNING: Ignoring "
390 <<"CONTENT_IGNORE redefinition"<<std::endl;
393 // Check that the special contents are not redefined as different id
394 // because it would mess up everything
395 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
396 (def.name == "air" && c != CONTENT_AIR)){
397 errorstream<<"registerNode: IGNORING ERROR: "
398 <<"trying to register built-in type \""
399 <<def.name<<"\" as different id"<<std::endl;
402 m_content_features[c] = def;
404 addNameIdMapping(c, def.name);
406 virtual content_t set(const std::string &name,
407 const ContentFeatures &def)
409 assert(name == def.name);
410 u16 id = CONTENT_IGNORE;
411 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
413 // Determine if full param2 is required
414 bool require_full_param2 = (
415 def.param_type_2 == CPT2_FULL
417 def.param_type_2 == CPT2_FLOWINGLIQUID
419 def.legacy_wallmounted
422 id = getFreeId(require_full_param2);
423 if(id == CONTENT_IGNORE)
424 return CONTENT_IGNORE;
426 addNameIdMapping(id, name);
431 virtual content_t allocateDummy(const std::string &name)
438 virtual void updateAliases(IItemDefManager *idef)
440 std::set<std::string> all = idef->getAll();
441 m_name_id_mapping_with_aliases.clear();
442 for(std::set<std::string>::iterator
443 i = all.begin(); i != all.end(); i++)
445 std::string name = *i;
446 std::string convert_to = idef->getAlias(name);
448 if(m_name_id_mapping.getId(convert_to, id))
450 m_name_id_mapping_with_aliases.insert(
451 std::make_pair(name, id));
455 virtual void updateTextures(ITextureSource *tsrc)
458 infostream<<"CNodeDefManager::updateTextures(): Updating "
459 <<"textures in node definitions"<<std::endl;
461 bool new_style_water = g_settings->getBool("new_style_water");
462 bool new_style_leaves = g_settings->getBool("new_style_leaves");
463 bool opaque_water = g_settings->getBool("opaque_water");
465 for(u16 i=0; i<=MAX_CONTENT; i++)
467 ContentFeatures *f = &m_content_features[i];
469 std::string tname_tiles[6];
470 for(u32 j=0; j<6; j++)
472 tname_tiles[j] = f->tname_tiles[j];
473 if(tname_tiles[j] == "")
474 tname_tiles[j] = "unknown_block.png";
486 assert(f->liquid_type == LIQUID_SOURCE);
495 f->backface_culling = false;
498 case NDT_FLOWINGLIQUID:
499 assert(f->liquid_type == LIQUID_FLOWING);
506 f->visual_solidness = 1;
510 f->visual_solidness = 1;
512 case NDT_ALLFACES_OPTIONAL:
513 if(new_style_leaves){
514 f->drawtype = NDT_ALLFACES;
516 f->visual_solidness = 1;
518 f->drawtype = NDT_NORMAL;
520 for(u32 i=0; i<6; i++){
521 tname_tiles[i] += std::string("^[noalpha");
535 for(u16 j=0; j<6; j++){
536 f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]);
537 f->tiles[j].alpha = f->alpha;
539 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
541 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
542 f->tiles[j].material_flags = 0;
543 if(f->backface_culling)
544 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
547 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
548 f->special_tiles[j].texture = tsrc->getTexture(f->mspec_special[j].tname);
549 f->special_tiles[j].alpha = f->alpha;
551 f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
553 f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
554 f->special_tiles[j].material_flags = 0;
555 if(f->mspec_special[j].backface_culling)
556 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
561 void serialize(std::ostream &os)
564 std::ostringstream tmp_os(std::ios::binary);
565 for(u16 i=0; i<=MAX_CONTENT; i++)
567 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
569 ContentFeatures *f = &m_content_features[i];
573 f->serialize(tmp_os);
577 os<<serializeLongString(tmp_os.str());
579 void deSerialize(std::istream &is)
582 u16 count = readU16(is);
583 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
584 for(u16 n=0; n<count; n++){
585 u16 i = readU16(tmp_is);
587 errorstream<<"ContentFeatures::deSerialize(): "
588 <<"Too large content id: "<<i<<std::endl;
591 /*// Do not deserialize special types
592 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
594 ContentFeatures *f = &m_content_features[i];
595 f->deSerialize(tmp_is);
597 addNameIdMapping(i, f->name);
601 void addNameIdMapping(content_t i, std::string name)
603 m_name_id_mapping.set(i, name);
604 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
607 // Features indexed by id
608 ContentFeatures m_content_features[MAX_CONTENT+1];
609 // A mapping for fast converting back and forth between names and ids
610 NameIdMapping m_name_id_mapping;
611 // Like m_name_id_mapping, but only from names to ids, and includes
612 // item aliases too. Updated by updateAliases()
613 // Note: Not serialized.
614 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
617 IWritableNodeDefManager* createNodeDefManager()
619 return new CNodeDefManager();