3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 Kahrl <kahrl@gmx.net>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "inventory.h"
28 #include "mapblock_mesh.h"
40 ItemDefinition::ItemDefinition()
45 ItemDefinition::ItemDefinition(const ItemDefinition &def)
51 ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
60 description = def.description;
61 inventory_image = def.inventory_image;
62 wield_image = def.wield_image;
63 wield_scale = def.wield_scale;
64 stack_max = def.stack_max;
66 liquids_pointable = def.liquids_pointable;
67 if(def.tool_capabilities)
69 tool_capabilities = new ToolCapabilities(
70 *def.tool_capabilities);
74 inventory_texture = def.inventory_texture;
77 wield_mesh = def.wield_mesh;
84 ItemDefinition::~ItemDefinition()
89 void ItemDefinition::resetInitial()
91 // Initialize pointers to NULL so reset() does not delete undefined pointers
92 tool_capabilities = NULL;
94 inventory_texture = NULL;
100 void ItemDefinition::reset()
105 inventory_image = "";
107 wield_scale = v3f(1.0, 1.0, 1.0);
110 liquids_pointable = false;
111 if(tool_capabilities)
113 delete tool_capabilities;
114 tool_capabilities = NULL;
119 inventory_texture = NULL;
128 void ItemDefinition::serialize(std::ostream &os) const
130 writeU8(os, 1); // version
132 os<<serializeString(name);
133 os<<serializeString(description);
134 os<<serializeString(inventory_image);
135 os<<serializeString(wield_image);
136 writeV3F1000(os, wield_scale);
137 writeS16(os, stack_max);
139 writeU8(os, liquids_pointable);
140 std::string tool_capabilities_s = "";
141 if(tool_capabilities){
142 std::ostringstream tmp_os(std::ios::binary);
143 tool_capabilities->serialize(tmp_os);
144 tool_capabilities_s = tmp_os.str();
146 os<<serializeString(tool_capabilities_s);
147 writeU16(os, groups.size());
148 for(std::map<std::string, int>::const_iterator
149 i = groups.begin(); i != groups.end(); i++){
150 os<<serializeString(i->first);
151 writeS16(os, i->second);
155 void ItemDefinition::deSerialize(std::istream &is)
161 int version = readU8(is);
163 throw SerializationError("unsupported ItemDefinition version");
164 type = (enum ItemType)readU8(is);
165 name = deSerializeString(is);
166 description = deSerializeString(is);
167 inventory_image = deSerializeString(is);
168 wield_image = deSerializeString(is);
169 wield_scale = readV3F1000(is);
170 stack_max = readS16(is);
172 liquids_pointable = readU8(is);
173 std::string tool_capabilities_s = deSerializeString(is);
174 if(!tool_capabilities_s.empty())
176 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
177 tool_capabilities = new ToolCapabilities;
178 tool_capabilities->deSerialize(tmp_is);
181 u32 groups_size = readU16(is);
182 for(u32 i=0; i<groups_size; i++){
183 std::string name = deSerializeString(is);
184 int value = readS16(is);
185 groups[name] = value;
193 // SUGG: Support chains of aliases?
195 class CItemDefManager: public IWritableItemDefManager
202 virtual ~CItemDefManager()
205 virtual const ItemDefinition& get(const std::string &name_) const
207 // Convert name according to possible alias
208 std::string name = getAlias(name_);
209 // Get the definition
210 std::map<std::string, ItemDefinition*>::const_iterator i;
211 i = m_item_definitions.find(name);
212 if(i == m_item_definitions.end())
213 i = m_item_definitions.find("unknown");
214 assert(i != m_item_definitions.end());
217 virtual std::string getAlias(const std::string &name) const
219 std::map<std::string, std::string>::const_iterator i;
220 i = m_aliases.find(name);
221 if(i != m_aliases.end())
225 virtual std::set<std::string> getAll() const
227 std::set<std::string> result;
228 for(std::map<std::string, ItemDefinition*>::const_iterator
229 i = m_item_definitions.begin();
230 i != m_item_definitions.end(); i++)
232 result.insert(i->first);
234 for(std::map<std::string, std::string>::const_iterator
235 i = m_aliases.begin();
236 i != m_aliases.end(); i++)
238 result.insert(i->first);
242 virtual bool isKnown(const std::string &name_) const
244 // Convert name according to possible alias
245 std::string name = getAlias(name_);
246 // Get the definition
247 std::map<std::string, ItemDefinition*>::const_iterator i;
248 return m_item_definitions.find(name) != m_item_definitions.end();
252 for(std::map<std::string, ItemDefinition*>::const_iterator
253 i = m_item_definitions.begin();
254 i != m_item_definitions.end(); i++)
258 m_item_definitions.clear();
261 // Add the four builtin items:
263 // "unknown" is returned whenever an undefined item is accessed
264 // "air" is the air node
265 // "ignore" is the ignore node
267 ItemDefinition* hand_def = new ItemDefinition;
269 hand_def->wield_image = "wieldhand.png";
270 hand_def->tool_capabilities = new ToolCapabilities;
271 m_item_definitions.insert(std::make_pair("", hand_def));
273 ItemDefinition* unknown_def = new ItemDefinition;
274 unknown_def->name = "unknown";
275 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
277 ItemDefinition* air_def = new ItemDefinition;
278 air_def->type = ITEM_NODE;
279 air_def->name = "air";
280 m_item_definitions.insert(std::make_pair("air", air_def));
282 ItemDefinition* ignore_def = new ItemDefinition;
283 ignore_def->type = ITEM_NODE;
284 ignore_def->name = "ignore";
285 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
287 virtual void registerItem(const ItemDefinition &def)
289 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
290 // Ensure that the "" item (the hand) always has ToolCapabilities
292 assert(def.tool_capabilities != NULL);
294 if(m_item_definitions.count(def.name) == 0)
295 m_item_definitions[def.name] = new ItemDefinition(def);
297 *(m_item_definitions[def.name]) = def;
299 // Remove conflicting alias if it exists
300 bool alias_removed = (m_aliases.erase(def.name) != 0);
302 infostream<<"ItemDefManager: erased alias "<<def.name
303 <<" because item was defined"<<std::endl;
305 virtual void registerAlias(const std::string &name,
306 const std::string &convert_to)
308 if(m_item_definitions.find(name) == m_item_definitions.end())
310 verbosestream<<"ItemDefManager: setting alias "<<name
311 <<" -> "<<convert_to<<std::endl;
312 m_aliases[name] = convert_to;
316 virtual void updateTexturesAndMeshes(IGameDef *gamedef)
319 infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating "
320 <<"textures and meshes in item definitions"<<std::endl;
322 ITextureSource *tsrc = gamedef->getTextureSource();
323 INodeDefManager *nodedef = gamedef->getNodeDefManager();
324 IrrlichtDevice *device = tsrc->getDevice();
325 video::IVideoDriver *driver = device->getVideoDriver();
327 for(std::map<std::string, ItemDefinition*>::iterator
328 i = m_item_definitions.begin();
329 i != m_item_definitions.end(); i++)
331 ItemDefinition *def = i->second;
333 bool need_node_mesh = false;
335 // Create an inventory texture
336 def->inventory_texture = NULL;
337 if(def->inventory_image != "")
339 def->inventory_texture = tsrc->getTextureRaw(def->inventory_image);
341 else if(def->type == ITEM_NODE)
343 need_node_mesh = true;
346 // Create a wield mesh
347 if(def->wield_mesh != NULL)
349 def->wield_mesh->drop();
350 def->wield_mesh = NULL;
352 if(def->type == ITEM_NODE && def->wield_image == "")
354 need_node_mesh = true;
356 else if(def->wield_image != "" || def->inventory_image != "")
358 // Extrude the wield image into a mesh
360 std::string imagename;
361 if(def->wield_image != "")
362 imagename = def->wield_image;
364 imagename = def->inventory_image;
366 def->wield_mesh = createExtrudedMesh(
367 tsrc->getTextureRaw(imagename),
369 def->wield_scale * v3f(40.0, 40.0, 4.0));
370 if(def->wield_mesh == NULL)
372 infostream<<"ItemDefManager: WARNING: "
373 <<"updateTexturesAndMeshes(): "
374 <<"Unable to create extruded mesh for item "
375 <<def->name<<std::endl;
384 content_t id = nodedef->getId(def->name);
385 const ContentFeatures &f = nodedef->get(id);
388 if(f.param_type == CPT_LIGHT)
392 Make a mesh from the node
394 MeshMakeData mesh_make_data(gamedef);
395 MapNode mesh_make_node(id, param1, 0);
396 mesh_make_data.fillSingleNode(&mesh_make_node);
397 MapBlockMesh mapblock_mesh(&mesh_make_data);
399 scene::IMesh *node_mesh = mapblock_mesh.getMesh();
401 setMeshColor(node_mesh, video::SColor(255, 255, 255, 255));
404 Scale and translate the mesh so it's a unit cube
405 centered on the origin
407 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
408 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
411 Draw node mesh into a render target texture
413 if(def->inventory_texture == NULL)
415 core::dimension2d<u32> dim(64,64);
416 std::string rtt_texture_name = "INVENTORY_"
417 + def->name + "_RTT";
418 v3f camera_position(0, 1.0, -1.5);
419 camera_position.rotateXZBy(45);
420 v3f camera_lookat(0, 0, 0);
421 core::CMatrix4<f32> camera_projection_matrix;
422 // Set orthogonal projection
423 camera_projection_matrix.buildProjectionMatrixOrthoLH(
426 video::SColorf ambient_light(0.2,0.2,0.2);
427 v3f light_position(10, 100, -50);
428 video::SColorf light_color(0.5,0.5,0.5);
429 f32 light_radius = 1000;
431 def->inventory_texture = generateTextureFromMesh(
432 node_mesh, device, dim, rtt_texture_name,
435 camera_projection_matrix,
441 // render-to-target didn't work
442 if(def->inventory_texture == NULL)
444 def->inventory_texture =
445 tsrc->getTextureRaw(f.tname_tiles[0]);
450 Use the node mesh as the wield mesh
452 if(def->wield_mesh == NULL)
454 // Scale to proper wield mesh proportions
455 scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
457 def->wield_mesh = node_mesh;
458 def->wield_mesh->grab();
461 // falling outside of here deletes node_mesh
466 void serialize(std::ostream &os)
468 writeU8(os, 0); // version
469 u16 count = m_item_definitions.size();
471 for(std::map<std::string, ItemDefinition*>::const_iterator
472 i = m_item_definitions.begin();
473 i != m_item_definitions.end(); i++)
475 ItemDefinition *def = i->second;
476 // Serialize ItemDefinition and write wrapped in a string
477 std::ostringstream tmp_os(std::ios::binary);
478 def->serialize(tmp_os);
479 os<<serializeString(tmp_os.str());
481 writeU16(os, m_aliases.size());
482 for(std::map<std::string, std::string>::const_iterator
483 i = m_aliases.begin(); i != m_aliases.end(); i++)
485 os<<serializeString(i->first);
486 os<<serializeString(i->second);
489 void deSerialize(std::istream &is)
494 int version = readU8(is);
496 throw SerializationError("unsupported ItemDefManager version");
497 u16 count = readU16(is);
498 for(u16 i=0; i<count; i++)
500 // Deserialize a string and grab an ItemDefinition from it
501 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
503 def.deSerialize(tmp_is);
507 u16 num_aliases = readU16(is);
508 for(u16 i=0; i<num_aliases; i++)
510 std::string name = deSerializeString(is);
511 std::string convert_to = deSerializeString(is);
512 registerAlias(name, convert_to);
517 std::map<std::string, ItemDefinition*> m_item_definitions;
519 std::map<std::string, std::string> m_aliases;
522 IWritableItemDefManager* createItemDefManager()
524 return new CItemDefManager();