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 Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser 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"
33 #include "util/serialize.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);
73 node_placement_prediction = def.node_placement_prediction;
75 inventory_texture = def.inventory_texture;
78 wield_mesh = def.wield_mesh;
85 ItemDefinition::~ItemDefinition()
90 void ItemDefinition::resetInitial()
92 // Initialize pointers to NULL so reset() does not delete undefined pointers
93 tool_capabilities = NULL;
95 inventory_texture = NULL;
101 void ItemDefinition::reset()
106 inventory_image = "";
108 wield_scale = v3f(1.0, 1.0, 1.0);
111 liquids_pointable = false;
112 if(tool_capabilities)
114 delete tool_capabilities;
115 tool_capabilities = NULL;
119 node_placement_prediction = "";
122 inventory_texture = NULL;
131 void ItemDefinition::serialize(std::ostream &os) const
133 writeU8(os, 1); // version
135 os<<serializeString(name);
136 os<<serializeString(description);
137 os<<serializeString(inventory_image);
138 os<<serializeString(wield_image);
139 writeV3F1000(os, wield_scale);
140 writeS16(os, stack_max);
142 writeU8(os, liquids_pointable);
143 std::string tool_capabilities_s = "";
144 if(tool_capabilities){
145 std::ostringstream tmp_os(std::ios::binary);
146 tool_capabilities->serialize(tmp_os);
147 tool_capabilities_s = tmp_os.str();
149 os<<serializeString(tool_capabilities_s);
150 writeU16(os, groups.size());
151 for(std::map<std::string, int>::const_iterator
152 i = groups.begin(); i != groups.end(); i++){
153 os<<serializeString(i->first);
154 writeS16(os, i->second);
156 os<<serializeString(node_placement_prediction);
159 void ItemDefinition::deSerialize(std::istream &is)
165 int version = readU8(is);
167 throw SerializationError("unsupported ItemDefinition version");
168 type = (enum ItemType)readU8(is);
169 name = deSerializeString(is);
170 description = deSerializeString(is);
171 inventory_image = deSerializeString(is);
172 wield_image = deSerializeString(is);
173 wield_scale = readV3F1000(is);
174 stack_max = readS16(is);
176 liquids_pointable = readU8(is);
177 std::string tool_capabilities_s = deSerializeString(is);
178 if(!tool_capabilities_s.empty())
180 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
181 tool_capabilities = new ToolCapabilities;
182 tool_capabilities->deSerialize(tmp_is);
185 u32 groups_size = readU16(is);
186 for(u32 i=0; i<groups_size; i++){
187 std::string name = deSerializeString(is);
188 int value = readS16(is);
189 groups[name] = value;
191 // If you add anything here, insert it primarily inside the try-catch
192 // block to not need to increase the version.
194 node_placement_prediction = deSerializeString(is);
195 }catch(SerializationError &e) {};
202 // SUGG: Support chains of aliases?
204 class CItemDefManager: public IWritableItemDefManager
211 virtual ~CItemDefManager()
214 virtual const ItemDefinition& get(const std::string &name_) const
216 // Convert name according to possible alias
217 std::string name = getAlias(name_);
218 // Get the definition
219 std::map<std::string, ItemDefinition*>::const_iterator i;
220 i = m_item_definitions.find(name);
221 if(i == m_item_definitions.end())
222 i = m_item_definitions.find("unknown");
223 assert(i != m_item_definitions.end());
226 virtual std::string getAlias(const std::string &name) const
228 std::map<std::string, std::string>::const_iterator i;
229 i = m_aliases.find(name);
230 if(i != m_aliases.end())
234 virtual std::set<std::string> getAll() const
236 std::set<std::string> result;
237 for(std::map<std::string, ItemDefinition*>::const_iterator
238 i = m_item_definitions.begin();
239 i != m_item_definitions.end(); i++)
241 result.insert(i->first);
243 for(std::map<std::string, std::string>::const_iterator
244 i = m_aliases.begin();
245 i != m_aliases.end(); i++)
247 result.insert(i->first);
251 virtual bool isKnown(const std::string &name_) const
253 // Convert name according to possible alias
254 std::string name = getAlias(name_);
255 // Get the definition
256 std::map<std::string, ItemDefinition*>::const_iterator i;
257 return m_item_definitions.find(name) != m_item_definitions.end();
261 for(std::map<std::string, ItemDefinition*>::const_iterator
262 i = m_item_definitions.begin();
263 i != m_item_definitions.end(); i++)
267 m_item_definitions.clear();
270 // Add the four builtin items:
272 // "unknown" is returned whenever an undefined item is accessed
273 // "air" is the air node
274 // "ignore" is the ignore node
276 ItemDefinition* hand_def = new ItemDefinition;
278 hand_def->wield_image = "wieldhand.png";
279 hand_def->tool_capabilities = new ToolCapabilities;
280 m_item_definitions.insert(std::make_pair("", hand_def));
282 ItemDefinition* unknown_def = new ItemDefinition;
283 unknown_def->name = "unknown";
284 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
286 ItemDefinition* air_def = new ItemDefinition;
287 air_def->type = ITEM_NODE;
288 air_def->name = "air";
289 m_item_definitions.insert(std::make_pair("air", air_def));
291 ItemDefinition* ignore_def = new ItemDefinition;
292 ignore_def->type = ITEM_NODE;
293 ignore_def->name = "ignore";
294 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
296 virtual void registerItem(const ItemDefinition &def)
298 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
299 // Ensure that the "" item (the hand) always has ToolCapabilities
301 assert(def.tool_capabilities != NULL);
303 if(m_item_definitions.count(def.name) == 0)
304 m_item_definitions[def.name] = new ItemDefinition(def);
306 *(m_item_definitions[def.name]) = def;
308 // Remove conflicting alias if it exists
309 bool alias_removed = (m_aliases.erase(def.name) != 0);
311 infostream<<"ItemDefManager: erased alias "<<def.name
312 <<" because item was defined"<<std::endl;
314 virtual void registerAlias(const std::string &name,
315 const std::string &convert_to)
317 if(m_item_definitions.find(name) == m_item_definitions.end())
319 verbosestream<<"ItemDefManager: setting alias "<<name
320 <<" -> "<<convert_to<<std::endl;
321 m_aliases[name] = convert_to;
325 virtual void updateTexturesAndMeshes(IGameDef *gamedef)
328 infostream<<"ItemDefManager::updateTexturesAndMeshes(): Updating "
329 <<"textures and meshes in item definitions"<<std::endl;
331 ITextureSource *tsrc = gamedef->getTextureSource();
332 INodeDefManager *nodedef = gamedef->getNodeDefManager();
333 IrrlichtDevice *device = tsrc->getDevice();
334 video::IVideoDriver *driver = device->getVideoDriver();
336 for(std::map<std::string, ItemDefinition*>::iterator
337 i = m_item_definitions.begin();
338 i != m_item_definitions.end(); i++)
340 ItemDefinition *def = i->second;
342 bool need_node_mesh = false;
344 // Create an inventory texture
345 def->inventory_texture = NULL;
346 if(def->inventory_image != "")
348 def->inventory_texture = tsrc->getTextureRaw(def->inventory_image);
350 else if(def->type == ITEM_NODE)
352 need_node_mesh = true;
355 // Create a wield mesh
356 if(def->wield_mesh != NULL)
358 def->wield_mesh->drop();
359 def->wield_mesh = NULL;
361 if(def->type == ITEM_NODE && def->wield_image == "")
363 need_node_mesh = true;
365 else if(def->wield_image != "" || def->inventory_image != "")
367 // Extrude the wield image into a mesh
369 std::string imagename;
370 if(def->wield_image != "")
371 imagename = def->wield_image;
373 imagename = def->inventory_image;
375 def->wield_mesh = createExtrudedMesh(
376 tsrc->getTextureRaw(imagename),
378 def->wield_scale * v3f(40.0, 40.0, 4.0));
379 if(def->wield_mesh == NULL)
381 infostream<<"ItemDefManager: WARNING: "
382 <<"updateTexturesAndMeshes(): "
383 <<"Unable to create extruded mesh for item "
384 <<def->name<<std::endl;
393 content_t id = nodedef->getId(def->name);
394 const ContentFeatures &f = nodedef->get(id);
397 if(f.param_type == CPT_LIGHT)
401 Make a mesh from the node
403 MeshMakeData mesh_make_data(gamedef);
404 MapNode mesh_make_node(id, param1, 0);
405 mesh_make_data.fillSingleNode(&mesh_make_node);
406 MapBlockMesh mapblock_mesh(&mesh_make_data);
408 scene::IMesh *node_mesh = mapblock_mesh.getMesh();
410 setMeshColor(node_mesh, video::SColor(255, 255, 255, 255));
413 Scale and translate the mesh so it's a unit cube
414 centered on the origin
416 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
417 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
420 Draw node mesh into a render target texture
422 if(def->inventory_texture == NULL)
424 core::dimension2d<u32> dim(64,64);
425 std::string rtt_texture_name = "INVENTORY_"
426 + def->name + "_RTT";
427 v3f camera_position(0, 1.0, -1.5);
428 camera_position.rotateXZBy(45);
429 v3f camera_lookat(0, 0, 0);
430 core::CMatrix4<f32> camera_projection_matrix;
431 // Set orthogonal projection
432 camera_projection_matrix.buildProjectionMatrixOrthoLH(
435 video::SColorf ambient_light(0.2,0.2,0.2);
436 v3f light_position(10, 100, -50);
437 video::SColorf light_color(0.5,0.5,0.5);
438 f32 light_radius = 1000;
440 def->inventory_texture = generateTextureFromMesh(
441 node_mesh, device, dim, rtt_texture_name,
444 camera_projection_matrix,
450 // render-to-target didn't work
451 if(def->inventory_texture == NULL)
453 def->inventory_texture =
454 tsrc->getTextureRaw(f.tiledef[0].name);
459 Use the node mesh as the wield mesh
461 if(def->wield_mesh == NULL)
463 // Scale to proper wield mesh proportions
464 scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
466 def->wield_mesh = node_mesh;
467 def->wield_mesh->grab();
470 // falling outside of here deletes node_mesh
475 void serialize(std::ostream &os)
477 writeU8(os, 0); // version
478 u16 count = m_item_definitions.size();
480 for(std::map<std::string, ItemDefinition*>::const_iterator
481 i = m_item_definitions.begin();
482 i != m_item_definitions.end(); i++)
484 ItemDefinition *def = i->second;
485 // Serialize ItemDefinition and write wrapped in a string
486 std::ostringstream tmp_os(std::ios::binary);
487 def->serialize(tmp_os);
488 os<<serializeString(tmp_os.str());
490 writeU16(os, m_aliases.size());
491 for(std::map<std::string, std::string>::const_iterator
492 i = m_aliases.begin(); i != m_aliases.end(); i++)
494 os<<serializeString(i->first);
495 os<<serializeString(i->second);
498 void deSerialize(std::istream &is)
503 int version = readU8(is);
505 throw SerializationError("unsupported ItemDefManager version");
506 u16 count = readU16(is);
507 for(u16 i=0; i<count; i++)
509 // Deserialize a string and grab an ItemDefinition from it
510 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
512 def.deSerialize(tmp_is);
516 u16 num_aliases = readU16(is);
517 for(u16 i=0; i<num_aliases; i++)
519 std::string name = deSerializeString(is);
520 std::string convert_to = deSerializeString(is);
521 registerAlias(name, convert_to);
526 std::map<std::string, ItemDefinition*> m_item_definitions;
528 std::map<std::string, std::string> m_aliases;
531 IWritableItemDefManager* createItemDefManager()
533 return new CItemDefManager();