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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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"
30 #include "util/serialize.h"
38 type = NODEBOX_REGULAR;
41 // default is sign/ladder-like
42 wall_top = aabb3f(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2);
43 wall_bottom = aabb3f(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2);
44 wall_side = aabb3f(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2);
47 void NodeBox::serialize(std::ostream &os) const
49 writeU8(os, 1); // version
52 if(type == NODEBOX_FIXED)
54 writeU16(os, fixed.size());
55 for(std::vector<aabb3f>::const_iterator
57 i != fixed.end(); i++)
59 writeV3F1000(os, i->MinEdge);
60 writeV3F1000(os, i->MaxEdge);
63 else if(type == NODEBOX_WALLMOUNTED)
65 writeV3F1000(os, wall_top.MinEdge);
66 writeV3F1000(os, wall_top.MaxEdge);
67 writeV3F1000(os, wall_bottom.MinEdge);
68 writeV3F1000(os, wall_bottom.MaxEdge);
69 writeV3F1000(os, wall_side.MinEdge);
70 writeV3F1000(os, wall_side.MaxEdge);
74 void NodeBox::deSerialize(std::istream &is)
76 int version = readU8(is);
78 throw SerializationError("unsupported NodeBox version");
82 type = (enum NodeBoxType)readU8(is);
84 if(type == NODEBOX_FIXED)
86 u16 fixed_count = readU16(is);
90 box.MinEdge = readV3F1000(is);
91 box.MaxEdge = readV3F1000(is);
95 else if(type == NODEBOX_WALLMOUNTED)
97 wall_top.MinEdge = readV3F1000(is);
98 wall_top.MaxEdge = readV3F1000(is);
99 wall_bottom.MinEdge = readV3F1000(is);
100 wall_bottom.MaxEdge = readV3F1000(is);
101 wall_side.MinEdge = readV3F1000(is);
102 wall_side.MaxEdge = readV3F1000(is);
110 void TileDef::serialize(std::ostream &os) const
112 writeU8(os, 0); // version
113 os<<serializeString(name);
114 writeU8(os, animation.type);
115 writeU16(os, animation.aspect_w);
116 writeU16(os, animation.aspect_h);
117 writeF1000(os, animation.length);
120 void TileDef::deSerialize(std::istream &is)
122 int version = readU8(is);
124 throw SerializationError("unsupported TileDef version");
125 name = deSerializeString(is);
126 animation.type = (TileAnimationType)readU8(is);
127 animation.aspect_w = readU16(is);
128 animation.aspect_h = readU16(is);
129 animation.length = readF1000(is);
133 SimpleSoundSpec serialization
136 static void serializeSimpleSoundSpec(const SimpleSoundSpec &ss,
139 os<<serializeString(ss.name);
140 writeF1000(os, ss.gain);
142 static void deSerializeSimpleSoundSpec(SimpleSoundSpec &ss, std::istream &is)
144 ss.name = deSerializeString(is);
145 ss.gain = readF1000(is);
152 ContentFeatures::ContentFeatures()
157 ContentFeatures::~ContentFeatures()
161 void ContentFeatures::reset()
168 visual_solidness = 0;
169 backface_culling = true;
171 has_on_construct = false;
172 has_on_destruct = false;
173 has_after_destruct = false;
177 NOTE: Most of this is always overridden by the default values given
182 // Unknown nodes can be dug
183 groups["dig_immediate"] = 2;
184 drawtype = NDT_NORMAL;
186 for(u32 i=0; i<6; i++)
187 tiledef[i] = TileDef();
188 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
189 tiledef_special[j] = TileDef();
191 post_effect_color = video::SColor(0, 0, 0, 0);
192 param_type = CPT_NONE;
193 param_type_2 = CPT2_NONE;
194 is_ground_content = false;
195 light_propagates = false;
196 sunlight_propagates = false;
201 buildable_to = false;
202 liquid_type = LIQUID_NONE;
203 liquid_alternative_flowing = "";
204 liquid_alternative_source = "";
205 liquid_viscosity = 0;
206 liquid_renewable = true;
208 damage_per_second = 0;
209 node_box = NodeBox();
210 selection_box = NodeBox();
211 legacy_facedir_simple = false;
212 legacy_wallmounted = false;
213 sound_footstep = SimpleSoundSpec();
214 sound_dig = SimpleSoundSpec("__group");
215 sound_dug = SimpleSoundSpec();
218 void ContentFeatures::serialize(std::ostream &os)
220 writeU8(os, 5); // version
221 os<<serializeString(name);
222 writeU16(os, groups.size());
223 for(ItemGroupList::const_iterator
224 i = groups.begin(); i != groups.end(); i++){
225 os<<serializeString(i->first);
226 writeS16(os, i->second);
228 writeU8(os, drawtype);
229 writeF1000(os, visual_scale);
231 for(u32 i=0; i<6; i++)
232 tiledef[i].serialize(os);
233 writeU8(os, CF_SPECIAL_COUNT);
234 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
235 tiledef_special[i].serialize(os);
238 writeU8(os, post_effect_color.getAlpha());
239 writeU8(os, post_effect_color.getRed());
240 writeU8(os, post_effect_color.getGreen());
241 writeU8(os, post_effect_color.getBlue());
242 writeU8(os, param_type);
243 writeU8(os, param_type_2);
244 writeU8(os, is_ground_content);
245 writeU8(os, light_propagates);
246 writeU8(os, sunlight_propagates);
247 writeU8(os, walkable);
248 writeU8(os, pointable);
249 writeU8(os, diggable);
250 writeU8(os, climbable);
251 writeU8(os, buildable_to);
252 os<<serializeString(""); // legacy: used to be metadata_name
253 writeU8(os, liquid_type);
254 os<<serializeString(liquid_alternative_flowing);
255 os<<serializeString(liquid_alternative_source);
256 writeU8(os, liquid_viscosity);
257 writeU8(os, light_source);
258 writeU32(os, damage_per_second);
259 node_box.serialize(os);
260 selection_box.serialize(os);
261 writeU8(os, legacy_facedir_simple);
262 writeU8(os, legacy_wallmounted);
263 serializeSimpleSoundSpec(sound_footstep, os);
264 serializeSimpleSoundSpec(sound_dig, os);
265 serializeSimpleSoundSpec(sound_dug, os);
266 // Stuff below should be moved to correct place in a version that otherwise changes
267 // the protocol version
268 writeU8(os, liquid_renewable);
271 void ContentFeatures::deSerialize(std::istream &is)
273 int version = readU8(is);
275 throw SerializationError("unsupported ContentFeatures version");
276 name = deSerializeString(is);
278 u32 groups_size = readU16(is);
279 for(u32 i=0; i<groups_size; i++){
280 std::string name = deSerializeString(is);
281 int value = readS16(is);
282 groups[name] = value;
284 drawtype = (enum NodeDrawType)readU8(is);
285 visual_scale = readF1000(is);
287 throw SerializationError("unsupported tile count");
288 for(u32 i=0; i<6; i++)
289 tiledef[i].deSerialize(is);
290 if(readU8(is) != CF_SPECIAL_COUNT)
291 throw SerializationError("unsupported CF_SPECIAL_COUNT");
292 for(u32 i=0; i<CF_SPECIAL_COUNT; i++)
293 tiledef_special[i].deSerialize(is);
295 post_effect_color.setAlpha(readU8(is));
296 post_effect_color.setRed(readU8(is));
297 post_effect_color.setGreen(readU8(is));
298 post_effect_color.setBlue(readU8(is));
299 param_type = (enum ContentParamType)readU8(is);
300 param_type_2 = (enum ContentParamType2)readU8(is);
301 is_ground_content = readU8(is);
302 light_propagates = readU8(is);
303 sunlight_propagates = readU8(is);
304 walkable = readU8(is);
305 pointable = readU8(is);
306 diggable = readU8(is);
307 climbable = readU8(is);
308 buildable_to = readU8(is);
309 deSerializeString(is); // legacy: used to be metadata_name
310 liquid_type = (enum LiquidType)readU8(is);
311 liquid_alternative_flowing = deSerializeString(is);
312 liquid_alternative_source = deSerializeString(is);
313 liquid_viscosity = readU8(is);
314 light_source = readU8(is);
315 damage_per_second = readU32(is);
316 node_box.deSerialize(is);
317 selection_box.deSerialize(is);
318 legacy_facedir_simple = readU8(is);
319 legacy_wallmounted = readU8(is);
320 deSerializeSimpleSoundSpec(sound_footstep, is);
321 deSerializeSimpleSoundSpec(sound_dig, is);
322 deSerializeSimpleSoundSpec(sound_dug, is);
323 // If you add anything here, insert it primarily inside the try-catch
324 // block to not need to increase the version.
326 // Stuff below should be moved to correct place in a version that
327 // otherwise changes the protocol version
328 liquid_renewable = readU8(is);
329 }catch(SerializationError &e) {};
336 class CNodeDefManager: public IWritableNodeDefManager
341 m_name_id_mapping.clear();
342 m_name_id_mapping_with_aliases.clear();
344 for(u16 i=0; i<=MAX_CONTENT; i++)
346 ContentFeatures &f = m_content_features[i];
347 f.reset(); // Reset to defaults
354 f.drawtype = NDT_AIRLIKE;
355 f.param_type = CPT_LIGHT;
356 f.light_propagates = true;
357 f.sunlight_propagates = true;
361 f.buildable_to = true;
362 // Insert directly into containers
363 content_t c = CONTENT_AIR;
364 m_content_features[c] = f;
365 addNameIdMapping(c, f.name);
367 // Set CONTENT_IGNORE
371 f.drawtype = NDT_AIRLIKE;
372 f.param_type = CPT_NONE;
373 f.light_propagates = false;
374 f.sunlight_propagates = false;
378 // A way to remove accidental CONTENT_IGNOREs
379 f.buildable_to = true;
380 // Insert directly into containers
381 content_t c = CONTENT_IGNORE;
382 m_content_features[c] = f;
383 addNameIdMapping(c, f.name);
386 // CONTENT_IGNORE = not found
387 content_t getFreeId()
389 for(u32 i=0; i<=0xffff; i++){
390 const ContentFeatures &f = m_content_features[i];
394 return CONTENT_IGNORE;
400 virtual ~CNodeDefManager()
403 virtual IWritableNodeDefManager* clone()
405 CNodeDefManager *mgr = new CNodeDefManager();
406 for(u16 i=0; i<=MAX_CONTENT; i++)
412 virtual const ContentFeatures& get(content_t c) const
414 assert(c <= MAX_CONTENT);
415 return m_content_features[c];
417 virtual const ContentFeatures& get(const MapNode &n) const
419 return get(n.getContent());
421 virtual bool getId(const std::string &name, content_t &result) const
423 std::map<std::string, content_t>::const_iterator
424 i = m_name_id_mapping_with_aliases.find(name);
425 if(i == m_name_id_mapping_with_aliases.end())
430 virtual content_t getId(const std::string &name) const
432 content_t id = CONTENT_IGNORE;
436 virtual void getIds(const std::string &name, std::set<content_t> &result)
439 if(name.substr(0,6) != "group:"){
440 content_t id = CONTENT_IGNORE;
445 std::string group = name.substr(6);
446 for(u16 id=0; id<=MAX_CONTENT; id++)
448 const ContentFeatures &f = m_content_features[id];
449 if(f.name == "") // Quickly discard undefined nodes
451 if(itemgroup_get(f.groups, group) != 0)
455 virtual const ContentFeatures& get(const std::string &name) const
457 content_t id = CONTENT_IGNORE;
461 // IWritableNodeDefManager
462 virtual void set(content_t c, const ContentFeatures &def)
464 verbosestream<<"registerNode: registering content id \""<<c
465 <<"\": name=\""<<def.name<<"\""<<std::endl;
466 assert(c <= MAX_CONTENT);
467 // Don't allow redefining CONTENT_IGNORE (but allow air)
468 if(def.name == "ignore" || c == CONTENT_IGNORE){
469 infostream<<"registerNode: WARNING: Ignoring "
470 <<"CONTENT_IGNORE redefinition"<<std::endl;
473 // Check that the special contents are not redefined as different id
474 // because it would mess up everything
475 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
476 (def.name == "air" && c != CONTENT_AIR)){
477 errorstream<<"registerNode: IGNORING ERROR: "
478 <<"trying to register built-in type \""
479 <<def.name<<"\" as different id"<<std::endl;
482 m_content_features[c] = def;
484 addNameIdMapping(c, def.name);
486 virtual content_t set(const std::string &name,
487 const ContentFeatures &def)
489 assert(name == def.name);
490 u16 id = CONTENT_IGNORE;
491 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
495 if(id == CONTENT_IGNORE)
496 return CONTENT_IGNORE;
498 addNameIdMapping(id, name);
503 virtual content_t allocateDummy(const std::string &name)
510 virtual void updateAliases(IItemDefManager *idef)
512 std::set<std::string> all = idef->getAll();
513 m_name_id_mapping_with_aliases.clear();
514 for(std::set<std::string>::iterator
515 i = all.begin(); i != all.end(); i++)
517 std::string name = *i;
518 std::string convert_to = idef->getAlias(name);
520 if(m_name_id_mapping.getId(convert_to, id))
522 m_name_id_mapping_with_aliases.insert(
523 std::make_pair(name, id));
527 virtual void updateTextures(ITextureSource *tsrc)
530 infostream<<"CNodeDefManager::updateTextures(): Updating "
531 <<"textures in node definitions"<<std::endl;
533 bool new_style_water = g_settings->getBool("new_style_water");
534 bool new_style_leaves = g_settings->getBool("new_style_leaves");
535 bool opaque_water = g_settings->getBool("opaque_water");
537 for(u16 i=0; i<=MAX_CONTENT; i++)
539 ContentFeatures *f = &m_content_features[i];
541 // Figure out the actual tiles to use
543 for(u32 j=0; j<6; j++)
545 tiledef[j] = f->tiledef[j];
546 if(tiledef[j].name == "")
547 tiledef[j].name = "unknown_block.png";
559 assert(f->liquid_type == LIQUID_SOURCE);
566 f->backface_culling = false;
569 case NDT_FLOWINGLIQUID:
570 assert(f->liquid_type == LIQUID_FLOWING);
577 f->visual_solidness = 1;
581 f->visual_solidness = 1;
583 case NDT_ALLFACES_OPTIONAL:
584 if(new_style_leaves){
585 f->drawtype = NDT_ALLFACES;
587 f->visual_solidness = 1;
589 f->drawtype = NDT_NORMAL;
591 for(u32 i=0; i<6; i++){
592 tiledef[i].name += std::string("^[noalpha");
606 // Tiles (fill in f->tiles[])
607 for(u16 j=0; j<6; j++){
609 f->tiles[j].texture = tsrc->getTexture(tiledef[j].name);
611 f->tiles[j].alpha = f->alpha;
613 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
615 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
617 f->tiles[j].material_flags = 0;
618 if(f->backface_culling)
619 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
620 if(tiledef[j].animation.type == TAT_VERTICAL_FRAMES)
621 f->tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
622 // Animation parameters
623 if(f->tiles[j].material_flags &
624 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
626 // Get raw texture size to determine frame count by
628 video::ITexture *t = tsrc->getTextureRaw(tiledef[j].name);
629 v2u32 size = t->getOriginalSize();
630 int frame_height = (float)size.X /
631 (float)tiledef[j].animation.aspect_w *
632 (float)tiledef[j].animation.aspect_h;
633 int frame_count = size.Y / frame_height;
634 int frame_length_ms = 1000.0 *
635 tiledef[j].animation.length / frame_count;
636 f->tiles[j].animation_frame_count = frame_count;
637 f->tiles[j].animation_frame_length_ms = frame_length_ms;
639 // If there are no frames for an animation, switch
640 // animation off (so that having specified an animation
641 // for something but not using it in the texture pack
642 // gives no overhead)
643 if(frame_count == 1){
644 f->tiles[j].material_flags &=
645 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
649 // Special tiles (fill in f->special_tiles[])
650 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
652 f->special_tiles[j].texture =
653 tsrc->getTexture(f->tiledef_special[j].name);
655 f->special_tiles[j].alpha = f->alpha;
657 f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
659 f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
661 f->special_tiles[j].material_flags = 0;
662 if(f->tiledef_special[j].backface_culling)
663 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
664 if(f->tiledef_special[j].animation.type == TAT_VERTICAL_FRAMES)
665 f->special_tiles[j].material_flags |= MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
666 // Animation parameters
667 if(f->special_tiles[j].material_flags &
668 MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES)
670 // Get raw texture size to determine frame count by
672 video::ITexture *t = tsrc->getTextureRaw(f->tiledef_special[j].name);
673 v2u32 size = t->getOriginalSize();
674 int frame_height = (float)size.X /
675 (float)f->tiledef_special[j].animation.aspect_w *
676 (float)f->tiledef_special[j].animation.aspect_h;
677 int frame_count = size.Y / frame_height;
678 int frame_length_ms = 1000.0 *
679 f->tiledef_special[j].animation.length / frame_count;
680 f->special_tiles[j].animation_frame_count = frame_count;
681 f->special_tiles[j].animation_frame_length_ms = frame_length_ms;
683 // If there are no frames for an animation, switch
684 // animation off (so that having specified an animation
685 // for something but not using it in the texture pack
686 // gives no overhead)
687 if(frame_count == 1){
688 f->special_tiles[j].material_flags &=
689 ~MATERIAL_FLAG_ANIMATION_VERTICAL_FRAMES;
696 void serialize(std::ostream &os)
698 writeU8(os, 1); // version
700 std::ostringstream os2(std::ios::binary);
701 for(u16 i=0; i<=MAX_CONTENT; i++)
703 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
705 ContentFeatures *f = &m_content_features[i];
709 // Wrap it in a string to allow different lengths without
710 // strict version incompatibilities
711 std::ostringstream wrapper_os(std::ios::binary);
712 f->serialize(wrapper_os);
713 os2<<serializeString(wrapper_os.str());
717 os<<serializeLongString(os2.str());
719 void deSerialize(std::istream &is)
722 int version = readU8(is);
724 throw SerializationError("unsupported NodeDefinitionManager version");
725 u16 count = readU16(is);
726 std::istringstream is2(deSerializeLongString(is), std::ios::binary);
727 for(u16 n=0; n<count; n++){
728 u16 i = readU16(is2);
730 errorstream<<"ContentFeatures::deSerialize(): "
731 <<"Too large content id: "<<i<<std::endl;
734 /*// Do not deserialize special types
735 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
737 ContentFeatures *f = &m_content_features[i];
738 // Read it from the string wrapper
739 std::string wrapper = deSerializeString(is2);
740 std::istringstream wrapper_is(wrapper, std::ios::binary);
741 f->deSerialize(wrapper_is);
742 verbosestream<<"deserialized "<<f->name<<std::endl;
744 addNameIdMapping(i, f->name);
748 void addNameIdMapping(content_t i, std::string name)
750 m_name_id_mapping.set(i, name);
751 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
754 // Features indexed by id
755 ContentFeatures m_content_features[MAX_CONTENT+1];
756 // A mapping for fast converting back and forth between names and ids
757 NameIdMapping m_name_id_mapping;
758 // Like m_name_id_mapping, but only from names to ids, and includes
759 // item aliases too. Updated by updateAliases()
760 // Note: Not serialized.
761 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
764 IWritableNodeDefManager* createNodeDefManager()
766 return new CNodeDefManager();