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 "main.h" // g_settings
35 #include "util/serialize.h"
36 #include "util/container.h"
37 #include "util/thread.h"
44 ItemDefinition::ItemDefinition()
49 ItemDefinition::ItemDefinition(const ItemDefinition &def)
55 ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
64 description = def.description;
65 inventory_image = def.inventory_image;
66 wield_image = def.wield_image;
67 wield_scale = def.wield_scale;
68 stack_max = def.stack_max;
70 liquids_pointable = def.liquids_pointable;
71 if(def.tool_capabilities)
73 tool_capabilities = new ToolCapabilities(
74 *def.tool_capabilities);
77 node_placement_prediction = def.node_placement_prediction;
81 ItemDefinition::~ItemDefinition()
86 void ItemDefinition::resetInitial()
88 // Initialize pointers to NULL so reset() does not delete undefined pointers
89 tool_capabilities = NULL;
93 void ItemDefinition::reset()
100 wield_scale = v3f(1.0, 1.0, 1.0);
103 rightclickable = false;
104 liquids_pointable = false;
105 if(tool_capabilities)
107 delete tool_capabilities;
108 tool_capabilities = NULL;
112 node_placement_prediction = "";
115 void ItemDefinition::serialize(std::ostream &os) const
117 writeU8(os, 1); // version
119 os<<serializeString(name);
120 os<<serializeString(description);
121 os<<serializeString(inventory_image);
122 os<<serializeString(wield_image);
123 writeV3F1000(os, wield_scale);
124 writeS16(os, stack_max);
126 writeU8(os, liquids_pointable);
127 std::string tool_capabilities_s = "";
128 if(tool_capabilities){
129 std::ostringstream tmp_os(std::ios::binary);
130 tool_capabilities->serialize(tmp_os);
131 tool_capabilities_s = tmp_os.str();
133 os<<serializeString(tool_capabilities_s);
134 writeU16(os, groups.size());
135 for(std::map<std::string, int>::const_iterator
136 i = groups.begin(); i != groups.end(); i++){
137 os<<serializeString(i->first);
138 writeS16(os, i->second);
140 os<<serializeString(node_placement_prediction);
143 void ItemDefinition::deSerialize(std::istream &is)
149 int version = readU8(is);
151 throw SerializationError("unsupported ItemDefinition version");
152 type = (enum ItemType)readU8(is);
153 name = deSerializeString(is);
154 description = deSerializeString(is);
155 inventory_image = deSerializeString(is);
156 wield_image = deSerializeString(is);
157 wield_scale = readV3F1000(is);
158 stack_max = readS16(is);
160 liquids_pointable = readU8(is);
161 std::string tool_capabilities_s = deSerializeString(is);
162 if(!tool_capabilities_s.empty())
164 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
165 tool_capabilities = new ToolCapabilities;
166 tool_capabilities->deSerialize(tmp_is);
169 u32 groups_size = readU16(is);
170 for(u32 i=0; i<groups_size; i++){
171 std::string name = deSerializeString(is);
172 int value = readS16(is);
173 groups[name] = value;
175 // If you add anything here, insert it primarily inside the try-catch
176 // block to not need to increase the version.
178 node_placement_prediction = deSerializeString(is);
179 }catch(SerializationError &e) {};
186 // SUGG: Support chains of aliases?
188 class CItemDefManager: public IWritableItemDefManager
193 video::ITexture *inventory_texture;
194 scene::IMesh *wield_mesh;
197 inventory_texture(NULL),
207 m_main_thread = get_current_thread_id();
212 virtual ~CItemDefManager()
215 const core::list<ClientCached*> &values = m_clientcached.getValues();
216 for(core::list<ClientCached*>::ConstIterator
217 i = values.begin(); i != values.end(); ++i)
219 ClientCached *cc = *i;
220 cc->wield_mesh->drop();
224 virtual const ItemDefinition& get(const std::string &name_) const
226 // Convert name according to possible alias
227 std::string name = getAlias(name_);
228 // Get the definition
229 std::map<std::string, ItemDefinition*>::const_iterator i;
230 i = m_item_definitions.find(name);
231 if(i == m_item_definitions.end())
232 i = m_item_definitions.find("unknown");
233 assert(i != m_item_definitions.end());
236 virtual std::string getAlias(const std::string &name) const
238 std::map<std::string, std::string>::const_iterator i;
239 i = m_aliases.find(name);
240 if(i != m_aliases.end())
244 virtual std::set<std::string> getAll() const
246 std::set<std::string> result;
247 for(std::map<std::string, ItemDefinition*>::const_iterator
248 i = m_item_definitions.begin();
249 i != m_item_definitions.end(); i++)
251 result.insert(i->first);
253 for(std::map<std::string, std::string>::const_iterator
254 i = m_aliases.begin();
255 i != m_aliases.end(); i++)
257 result.insert(i->first);
261 virtual bool isKnown(const std::string &name_) const
263 // Convert name according to possible alias
264 std::string name = getAlias(name_);
265 // Get the definition
266 std::map<std::string, ItemDefinition*>::const_iterator i;
267 return m_item_definitions.find(name) != m_item_definitions.end();
270 ClientCached* createClientCachedDirect(const std::string &name,
271 IGameDef *gamedef) const
273 infostream<<"Lazily creating item texture and mesh for \""
274 <<name<<"\""<<std::endl;
276 // This is not thread-safe
277 assert(get_current_thread_id() == m_main_thread);
279 // Skip if already in cache
280 ClientCached *cc = NULL;
281 m_clientcached.get(name, &cc);
285 ITextureSource *tsrc = gamedef->getTextureSource();
286 INodeDefManager *nodedef = gamedef->getNodeDefManager();
287 IrrlichtDevice *device = tsrc->getDevice();
288 video::IVideoDriver *driver = device->getVideoDriver();
289 const ItemDefinition *def = &get(name);
291 // Create new ClientCached
292 cc = new ClientCached();
294 bool need_node_mesh = false;
296 // Create an inventory texture
297 cc->inventory_texture = NULL;
298 if(def->inventory_image != "")
300 cc->inventory_texture = tsrc->getTextureRaw(def->inventory_image);
302 else if(def->type == ITEM_NODE)
304 need_node_mesh = true;
307 // Create a wield mesh
308 if(cc->wield_mesh != NULL)
310 cc->wield_mesh->drop();
311 cc->wield_mesh = NULL;
313 if(def->type == ITEM_NODE && def->wield_image == "")
315 need_node_mesh = true;
317 else if(def->wield_image != "" || def->inventory_image != "")
319 // Extrude the wield image into a mesh
321 std::string imagename;
322 if(def->wield_image != "")
323 imagename = def->wield_image;
325 imagename = def->inventory_image;
327 cc->wield_mesh = createExtrudedMesh(
328 tsrc->getTextureRaw(imagename),
330 def->wield_scale * v3f(40.0, 40.0, 4.0));
331 if(cc->wield_mesh == NULL)
333 infostream<<"ItemDefManager: WARNING: "
334 <<"updateTexturesAndMeshes(): "
335 <<"Unable to create extruded mesh for item "
336 <<def->name<<std::endl;
345 content_t id = nodedef->getId(def->name);
346 const ContentFeatures &f = nodedef->get(id);
349 if(f.param_type == CPT_LIGHT)
353 Make a mesh from the node
355 MeshMakeData mesh_make_data(gamedef);
356 MapNode mesh_make_node(id, param1, 0);
357 mesh_make_data.fillSingleNode(&mesh_make_node);
358 MapBlockMesh mapblock_mesh(&mesh_make_data);
360 scene::IMesh *node_mesh = mapblock_mesh.getMesh();
362 video::SColor c(255, 255, 255, 255);
363 if(g_settings->getS32("enable_shaders") != 0)
364 c = MapBlock_LightColor(255, 0xffff, decode_light(f.light_source));
365 setMeshColor(node_mesh, c);
368 Scale and translate the mesh so it's a unit cube
369 centered on the origin
371 scaleMesh(node_mesh, v3f(1.0/BS, 1.0/BS, 1.0/BS));
372 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
375 Draw node mesh into a render target texture
377 if(cc->inventory_texture == NULL)
379 core::dimension2d<u32> dim(64,64);
380 std::string rtt_texture_name = "INVENTORY_"
381 + def->name + "_RTT";
382 v3f camera_position(0, 1.0, -1.5);
383 camera_position.rotateXZBy(45);
384 v3f camera_lookat(0, 0, 0);
385 core::CMatrix4<f32> camera_projection_matrix;
386 // Set orthogonal projection
387 camera_projection_matrix.buildProjectionMatrixOrthoLH(
390 video::SColorf ambient_light(0.2,0.2,0.2);
391 v3f light_position(10, 100, -50);
392 video::SColorf light_color(0.5,0.5,0.5);
393 f32 light_radius = 1000;
395 cc->inventory_texture = generateTextureFromMesh(
396 node_mesh, device, dim, rtt_texture_name,
399 camera_projection_matrix,
405 // render-to-target didn't work
406 if(cc->inventory_texture == NULL)
408 cc->inventory_texture =
409 tsrc->getTextureRaw(f.tiledef[0].name);
414 Use the node mesh as the wield mesh
416 if(cc->wield_mesh == NULL)
418 // Scale to proper wield mesh proportions
419 scaleMesh(node_mesh, v3f(30.0, 30.0, 30.0)
421 cc->wield_mesh = node_mesh;
422 cc->wield_mesh->grab();
425 // falling outside of here deletes node_mesh
429 m_clientcached.set(name, cc);
433 ClientCached* getClientCached(const std::string &name,
434 IGameDef *gamedef) const
436 ClientCached *cc = NULL;
437 m_clientcached.get(name, &cc);
441 if(get_current_thread_id() == m_main_thread)
443 return createClientCachedDirect(name, gamedef);
447 // We're gonna ask the result to be put into here
448 ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
449 // Throw a request in
450 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
452 // Wait result for a second
453 GetResult<std::string, ClientCached*, u8, u8>
454 result = result_queue.pop_front(1000);
455 // Check that at least something worked OK
456 assert(result.key == name);
460 catch(ItemNotFoundException &e)
462 errorstream<<"Waiting for clientcached timed out."<<std::endl;
463 return &m_dummy_clientcached;
467 // Get item inventory texture
468 virtual video::ITexture* getInventoryTexture(const std::string &name,
469 IGameDef *gamedef) const
471 ClientCached *cc = getClientCached(name, gamedef);
474 return cc->inventory_texture;
476 // Get item wield mesh
477 virtual scene::IMesh* getWieldMesh(const std::string &name,
478 IGameDef *gamedef) const
480 ClientCached *cc = getClientCached(name, gamedef);
483 return cc->wield_mesh;
488 for(std::map<std::string, ItemDefinition*>::const_iterator
489 i = m_item_definitions.begin();
490 i != m_item_definitions.end(); i++)
494 m_item_definitions.clear();
497 // Add the four builtin items:
499 // "unknown" is returned whenever an undefined item is accessed
500 // "air" is the air node
501 // "ignore" is the ignore node
503 ItemDefinition* hand_def = new ItemDefinition;
505 hand_def->wield_image = "wieldhand.png";
506 hand_def->tool_capabilities = new ToolCapabilities;
507 m_item_definitions.insert(std::make_pair("", hand_def));
509 ItemDefinition* unknown_def = new ItemDefinition;
510 unknown_def->name = "unknown";
511 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
513 ItemDefinition* air_def = new ItemDefinition;
514 air_def->type = ITEM_NODE;
515 air_def->name = "air";
516 m_item_definitions.insert(std::make_pair("air", air_def));
518 ItemDefinition* ignore_def = new ItemDefinition;
519 ignore_def->type = ITEM_NODE;
520 ignore_def->name = "ignore";
521 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
523 virtual void registerItem(const ItemDefinition &def)
525 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
526 // Ensure that the "" item (the hand) always has ToolCapabilities
528 assert(def.tool_capabilities != NULL);
530 if(m_item_definitions.count(def.name) == 0)
531 m_item_definitions[def.name] = new ItemDefinition(def);
533 *(m_item_definitions[def.name]) = def;
535 // Remove conflicting alias if it exists
536 bool alias_removed = (m_aliases.erase(def.name) != 0);
538 infostream<<"ItemDefManager: erased alias "<<def.name
539 <<" because item was defined"<<std::endl;
541 virtual void registerAlias(const std::string &name,
542 const std::string &convert_to)
544 if(m_item_definitions.find(name) == m_item_definitions.end())
546 verbosestream<<"ItemDefManager: setting alias "<<name
547 <<" -> "<<convert_to<<std::endl;
548 m_aliases[name] = convert_to;
551 void serialize(std::ostream &os)
553 writeU8(os, 0); // version
554 u16 count = m_item_definitions.size();
556 for(std::map<std::string, ItemDefinition*>::const_iterator
557 i = m_item_definitions.begin();
558 i != m_item_definitions.end(); i++)
560 ItemDefinition *def = i->second;
561 // Serialize ItemDefinition and write wrapped in a string
562 std::ostringstream tmp_os(std::ios::binary);
563 def->serialize(tmp_os);
564 os<<serializeString(tmp_os.str());
566 writeU16(os, m_aliases.size());
567 for(std::map<std::string, std::string>::const_iterator
568 i = m_aliases.begin(); i != m_aliases.end(); i++)
570 os<<serializeString(i->first);
571 os<<serializeString(i->second);
574 void deSerialize(std::istream &is)
579 int version = readU8(is);
581 throw SerializationError("unsupported ItemDefManager version");
582 u16 count = readU16(is);
583 for(u16 i=0; i<count; i++)
585 // Deserialize a string and grab an ItemDefinition from it
586 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
588 def.deSerialize(tmp_is);
592 u16 num_aliases = readU16(is);
593 for(u16 i=0; i<num_aliases; i++)
595 std::string name = deSerializeString(is);
596 std::string convert_to = deSerializeString(is);
597 registerAlias(name, convert_to);
600 void processQueue(IGameDef *gamedef)
603 while(m_get_clientcached_queue.size() > 0)
605 GetRequest<std::string, ClientCached*, u8, u8>
606 request = m_get_clientcached_queue.pop();
607 GetResult<std::string, ClientCached*, u8, u8>
609 result.key = request.key;
610 result.callers = request.callers;
611 result.item = createClientCachedDirect(request.key, gamedef);
612 request.dest->push_back(result);
618 std::map<std::string, ItemDefinition*> m_item_definitions;
620 std::map<std::string, std::string> m_aliases;
622 // The id of the thread that is allowed to use irrlicht directly
623 threadid_t m_main_thread;
624 // A reference to this can be returned when nothing is found, to avoid NULLs
625 mutable ClientCached m_dummy_clientcached;
626 // Cached textures and meshes
627 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
628 // Queued clientcached fetches (to be processed by the main thread)
629 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
633 IWritableItemDefManager* createItemDefManager()
635 return new CItemDefManager();