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()
94 void ContentFeatures::reset()
101 visual_solidness = 0;
102 backface_culling = true;
107 NOTE: Most of this is always overridden by the default values given
112 // Unknown nodes can be dug
113 groups["dig_immediate"] = 2;
114 drawtype = NDT_NORMAL;
116 for(u32 i=0; i<6; i++)
118 for(u16 j=0; j<CF_SPECIAL_COUNT; j++)
119 mspec_special[j] = MaterialSpec();
121 post_effect_color = video::SColor(0, 0, 0, 0);
122 param_type = CPT_NONE;
123 param_type_2 = CPT2_NONE;
124 is_ground_content = false;
125 light_propagates = false;
126 sunlight_propagates = false;
131 buildable_to = false;
133 liquid_type = LIQUID_NONE;
134 liquid_alternative_flowing = "";
135 liquid_alternative_source = "";
136 liquid_viscosity = 0;
138 damage_per_second = 0;
139 selection_box = NodeBox();
140 legacy_facedir_simple = false;
141 legacy_wallmounted = false;
144 void ContentFeatures::serialize(std::ostream &os)
146 writeU8(os, 2); // version
147 os<<serializeString(name);
148 writeU16(os, groups.size());
149 for(ItemGroupList::const_iterator
150 i = groups.begin(); i != groups.end(); i++){
151 os<<serializeString(i->first);
152 writeS16(os, i->second);
154 writeU8(os, drawtype);
155 writeF1000(os, visual_scale);
157 for(u32 i=0; i<6; i++)
158 os<<serializeString(tname_tiles[i]);
159 writeU8(os, CF_SPECIAL_COUNT);
160 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
161 mspec_special[i].serialize(os);
164 writeU8(os, post_effect_color.getAlpha());
165 writeU8(os, post_effect_color.getRed());
166 writeU8(os, post_effect_color.getGreen());
167 writeU8(os, post_effect_color.getBlue());
168 writeU8(os, param_type);
169 writeU8(os, param_type_2);
170 writeU8(os, is_ground_content);
171 writeU8(os, light_propagates);
172 writeU8(os, sunlight_propagates);
173 writeU8(os, walkable);
174 writeU8(os, pointable);
175 writeU8(os, diggable);
176 writeU8(os, climbable);
177 writeU8(os, buildable_to);
178 os<<serializeString(metadata_name);
179 writeU8(os, liquid_type);
180 os<<serializeString(liquid_alternative_flowing);
181 os<<serializeString(liquid_alternative_source);
182 writeU8(os, liquid_viscosity);
183 writeU8(os, light_source);
184 writeU32(os, damage_per_second);
185 selection_box.serialize(os);
186 writeU8(os, legacy_facedir_simple);
187 writeU8(os, legacy_wallmounted);
190 void ContentFeatures::deSerialize(std::istream &is)
192 int version = readU8(is);
194 throw SerializationError("unsupported ContentFeatures version");
195 name = deSerializeString(is);
197 u32 groups_size = readU16(is);
198 for(u32 i=0; i<groups_size; i++){
199 std::string name = deSerializeString(is);
200 int value = readS16(is);
201 groups[name] = value;
203 drawtype = (enum NodeDrawType)readU8(is);
204 visual_scale = readF1000(is);
206 throw SerializationError("unsupported tile count");
207 for(u32 i=0; i<6; i++)
208 tname_tiles[i] = deSerializeString(is);
209 if(readU8(is) != CF_SPECIAL_COUNT)
210 throw SerializationError("unsupported CF_SPECIAL_COUNT");
211 for(u32 i=0; i<CF_SPECIAL_COUNT; i++){
212 mspec_special[i].deSerialize(is);
215 post_effect_color.setAlpha(readU8(is));
216 post_effect_color.setRed(readU8(is));
217 post_effect_color.setGreen(readU8(is));
218 post_effect_color.setBlue(readU8(is));
219 param_type = (enum ContentParamType)readU8(is);
220 param_type_2 = (enum ContentParamType2)readU8(is);
221 is_ground_content = readU8(is);
222 light_propagates = readU8(is);
223 sunlight_propagates = readU8(is);
224 walkable = readU8(is);
225 pointable = readU8(is);
226 diggable = readU8(is);
227 climbable = readU8(is);
228 buildable_to = readU8(is);
229 metadata_name = deSerializeString(is);
230 liquid_type = (enum LiquidType)readU8(is);
231 liquid_alternative_flowing = deSerializeString(is);
232 liquid_alternative_source = deSerializeString(is);
233 liquid_viscosity = readU8(is);
234 light_source = readU8(is);
235 damage_per_second = readU32(is);
236 selection_box.deSerialize(is);
237 legacy_facedir_simple = readU8(is);
238 legacy_wallmounted = readU8(is);
245 class CNodeDefManager: public IWritableNodeDefManager
250 m_name_id_mapping.clear();
251 m_name_id_mapping_with_aliases.clear();
253 for(u16 i=0; i<=MAX_CONTENT; i++)
255 ContentFeatures &f = m_content_features[i];
256 f.reset(); // Reset to defaults
263 f.drawtype = NDT_AIRLIKE;
264 f.param_type = CPT_LIGHT;
265 f.light_propagates = true;
266 f.sunlight_propagates = true;
270 f.buildable_to = true;
271 // Insert directly into containers
272 content_t c = CONTENT_AIR;
273 m_content_features[c] = f;
274 addNameIdMapping(c, f.name);
276 // Set CONTENT_IGNORE
280 f.drawtype = NDT_AIRLIKE;
281 f.param_type = CPT_NONE;
282 f.light_propagates = false;
283 f.sunlight_propagates = false;
287 // A way to remove accidental CONTENT_IGNOREs
288 f.buildable_to = true;
289 // Insert directly into containers
290 content_t c = CONTENT_IGNORE;
291 m_content_features[c] = f;
292 addNameIdMapping(c, f.name);
295 // CONTENT_IGNORE = not found
296 content_t getFreeId(bool require_full_param2)
298 // If allowed, first search in the large 4-bit-param2 pool
299 if(!require_full_param2){
300 for(u16 i=0x800; i<=0xfff; i++){
301 const ContentFeatures &f = m_content_features[i];
306 // Then search from the small 8-bit-param2 pool
307 for(u16 i=0; i<=125; i++){
308 const ContentFeatures &f = m_content_features[i];
312 return CONTENT_IGNORE;
318 virtual ~CNodeDefManager()
321 virtual IWritableNodeDefManager* clone()
323 CNodeDefManager *mgr = new CNodeDefManager();
324 for(u16 i=0; i<=MAX_CONTENT; i++)
330 virtual const ContentFeatures& get(content_t c) const
332 assert(c <= MAX_CONTENT);
333 return m_content_features[c];
335 virtual const ContentFeatures& get(const MapNode &n) const
337 return get(n.getContent());
339 virtual bool getId(const std::string &name, content_t &result) const
341 std::map<std::string, content_t>::const_iterator
342 i = m_name_id_mapping_with_aliases.find(name);
343 if(i == m_name_id_mapping_with_aliases.end())
348 virtual content_t getId(const std::string &name) const
350 content_t id = CONTENT_IGNORE;
354 virtual const ContentFeatures& get(const std::string &name) const
356 content_t id = CONTENT_IGNORE;
360 // IWritableNodeDefManager
361 virtual void set(content_t c, const ContentFeatures &def)
363 verbosestream<<"registerNode: registering content id \""<<c
364 <<"\": name=\""<<def.name<<"\""<<std::endl;
365 assert(c <= MAX_CONTENT);
366 // Don't allow redefining CONTENT_IGNORE (but allow air)
367 if(def.name == "ignore" || c == CONTENT_IGNORE){
368 infostream<<"registerNode: WARNING: Ignoring "
369 <<"CONTENT_IGNORE redefinition"<<std::endl;
372 // Check that the special contents are not redefined as different id
373 // because it would mess up everything
374 if((def.name == "ignore" && c != CONTENT_IGNORE) ||
375 (def.name == "air" && c != CONTENT_AIR)){
376 errorstream<<"registerNode: IGNORING ERROR: "
377 <<"trying to register built-in type \""
378 <<def.name<<"\" as different id"<<std::endl;
381 m_content_features[c] = def;
383 addNameIdMapping(c, def.name);
385 virtual content_t set(const std::string &name,
386 const ContentFeatures &def)
388 assert(name == def.name);
389 u16 id = CONTENT_IGNORE;
390 bool found = m_name_id_mapping.getId(name, id); // ignore aliases
392 // Determine if full param2 is required
393 bool require_full_param2 = (
394 def.param_type_2 == CPT2_FULL
396 def.param_type_2 == CPT2_FLOWINGLIQUID
398 def.legacy_wallmounted
401 id = getFreeId(require_full_param2);
402 if(id == CONTENT_IGNORE)
403 return CONTENT_IGNORE;
405 addNameIdMapping(id, name);
410 virtual content_t allocateDummy(const std::string &name)
417 virtual void updateAliases(IItemDefManager *idef)
419 std::set<std::string> all = idef->getAll();
420 m_name_id_mapping_with_aliases.clear();
421 for(std::set<std::string>::iterator
422 i = all.begin(); i != all.end(); i++)
424 std::string name = *i;
425 std::string convert_to = idef->getAlias(name);
427 if(m_name_id_mapping.getId(convert_to, id))
429 m_name_id_mapping_with_aliases.insert(
430 std::make_pair(name, id));
434 virtual void updateTextures(ITextureSource *tsrc)
437 infostream<<"CNodeDefManager::updateTextures(): Updating "
438 <<"textures in node definitions"<<std::endl;
440 bool new_style_water = g_settings->getBool("new_style_water");
441 bool new_style_leaves = g_settings->getBool("new_style_leaves");
442 bool opaque_water = g_settings->getBool("opaque_water");
444 for(u16 i=0; i<=MAX_CONTENT; i++)
446 ContentFeatures *f = &m_content_features[i];
448 std::string tname_tiles[6];
449 for(u32 j=0; j<6; j++)
451 tname_tiles[j] = f->tname_tiles[j];
452 if(tname_tiles[j] == "")
453 tname_tiles[j] = "unknown_block.png";
465 assert(f->liquid_type == LIQUID_SOURCE);
474 f->backface_culling = false;
477 case NDT_FLOWINGLIQUID:
478 assert(f->liquid_type == LIQUID_FLOWING);
485 f->visual_solidness = 1;
489 f->visual_solidness = 1;
491 case NDT_ALLFACES_OPTIONAL:
492 if(new_style_leaves){
493 f->drawtype = NDT_ALLFACES;
495 f->visual_solidness = 1;
497 f->drawtype = NDT_NORMAL;
499 for(u32 i=0; i<6; i++){
500 tname_tiles[i] += std::string("^[noalpha");
514 for(u16 j=0; j<6; j++){
515 f->tiles[j].texture = tsrc->getTexture(tname_tiles[j]);
516 f->tiles[j].alpha = f->alpha;
518 f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
520 f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
521 f->tiles[j].material_flags = 0;
522 if(f->backface_culling)
523 f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
526 for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
527 f->special_tiles[j].texture = tsrc->getTexture(f->mspec_special[j].tname);
528 f->special_tiles[j].alpha = f->alpha;
530 f->special_tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
532 f->special_tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
533 f->special_tiles[j].material_flags = 0;
534 if(f->mspec_special[j].backface_culling)
535 f->special_tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
540 void serialize(std::ostream &os)
543 std::ostringstream tmp_os(std::ios::binary);
544 for(u16 i=0; i<=MAX_CONTENT; i++)
546 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
548 ContentFeatures *f = &m_content_features[i];
552 f->serialize(tmp_os);
556 os<<serializeLongString(tmp_os.str());
558 void deSerialize(std::istream &is)
561 u16 count = readU16(is);
562 std::istringstream tmp_is(deSerializeLongString(is), std::ios::binary);
563 for(u16 n=0; n<count; n++){
564 u16 i = readU16(tmp_is);
566 errorstream<<"ContentFeatures::deSerialize(): "
567 <<"Too large content id: "<<i<<std::endl;
570 /*// Do not deserialize special types
571 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
573 ContentFeatures *f = &m_content_features[i];
574 f->deSerialize(tmp_is);
576 addNameIdMapping(i, f->name);
580 void addNameIdMapping(content_t i, std::string name)
582 m_name_id_mapping.set(i, name);
583 m_name_id_mapping_with_aliases.insert(std::make_pair(name, i));
586 // Features indexed by id
587 ContentFeatures m_content_features[MAX_CONTENT+1];
588 // A mapping for fast converting back and forth between names and ids
589 NameIdMapping m_name_id_mapping;
590 // Like m_name_id_mapping, but only from names to ids, and includes
591 // item aliases too. Updated by updateAliases()
592 // Note: Not serialized.
593 std::map<std::string, content_t> m_name_id_mapping_with_aliases;
596 IWritableNodeDefManager* createNodeDefManager()
598 return new CNodeDefManager();