3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 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"
30 #include "wieldmesh.h"
34 #include "util/serialize.h"
43 ItemDefinition::ItemDefinition()
48 ItemDefinition::ItemDefinition(const ItemDefinition &def)
54 ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
63 description = def.description;
64 inventory_image = def.inventory_image;
65 wield_image = def.wield_image;
66 wield_scale = def.wield_scale;
67 stack_max = def.stack_max;
69 liquids_pointable = def.liquids_pointable;
70 if(def.tool_capabilities)
72 tool_capabilities = new ToolCapabilities(
73 *def.tool_capabilities);
76 node_placement_prediction = def.node_placement_prediction;
77 sound_place = def.sound_place;
78 sound_place_failed = def.sound_place_failed;
83 ItemDefinition::~ItemDefinition()
88 void ItemDefinition::resetInitial()
90 // Initialize pointers to NULL so reset() does not delete undefined pointers
91 tool_capabilities = NULL;
95 void ItemDefinition::reset()
100 inventory_image = "";
102 wield_scale = v3f(1.0, 1.0, 1.0);
105 liquids_pointable = false;
106 if(tool_capabilities)
108 delete tool_capabilities;
109 tool_capabilities = NULL;
112 sound_place = SimpleSoundSpec();
113 sound_place_failed = SimpleSoundSpec();
116 node_placement_prediction = "";
119 void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
121 if(protocol_version <= 17)
122 writeU8(os, 1); // version
123 else if(protocol_version <= 20)
124 writeU8(os, 2); // version
126 writeU8(os, 3); // version
128 os<<serializeString(name);
129 os<<serializeString(description);
130 os<<serializeString(inventory_image);
131 os<<serializeString(wield_image);
132 writeV3F1000(os, wield_scale);
133 writeS16(os, stack_max);
135 writeU8(os, liquids_pointable);
136 std::string tool_capabilities_s = "";
137 if(tool_capabilities){
138 std::ostringstream tmp_os(std::ios::binary);
139 tool_capabilities->serialize(tmp_os, protocol_version);
140 tool_capabilities_s = tmp_os.str();
142 os<<serializeString(tool_capabilities_s);
143 writeU16(os, groups.size());
144 for(std::map<std::string, int>::const_iterator
145 i = groups.begin(); i != groups.end(); ++i){
146 os<<serializeString(i->first);
147 writeS16(os, i->second);
149 os<<serializeString(node_placement_prediction);
150 if(protocol_version > 17){
151 //serializeSimpleSoundSpec(sound_place, os);
152 os<<serializeString(sound_place.name);
153 writeF1000(os, sound_place.gain);
155 if (protocol_version > 20) {
156 writeF1000(os, range);
157 os << serializeString(sound_place_failed.name);
158 writeF1000(os, sound_place_failed.gain);
162 void ItemDefinition::deSerialize(std::istream &is)
168 int version = readU8(is);
169 if(version < 1 || version > 3)
170 throw SerializationError("unsupported ItemDefinition version");
171 type = (enum ItemType)readU8(is);
172 name = deSerializeString(is);
173 description = deSerializeString(is);
174 inventory_image = deSerializeString(is);
175 wield_image = deSerializeString(is);
176 wield_scale = readV3F1000(is);
177 stack_max = readS16(is);
179 liquids_pointable = readU8(is);
180 std::string tool_capabilities_s = deSerializeString(is);
181 if(!tool_capabilities_s.empty())
183 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
184 tool_capabilities = new ToolCapabilities;
185 tool_capabilities->deSerialize(tmp_is);
188 u32 groups_size = readU16(is);
189 for(u32 i=0; i<groups_size; i++){
190 std::string name = deSerializeString(is);
191 int value = readS16(is);
192 groups[name] = value;
195 // We cant be sure that node_placement_prediction is send in version 1
197 node_placement_prediction = deSerializeString(is);
198 }catch(SerializationError &e) {};
199 // Set the old default sound
200 sound_place.name = "default_place_node";
201 sound_place.gain = 0.5;
202 } else if(version >= 2) {
203 node_placement_prediction = deSerializeString(is);
204 //deserializeSimpleSoundSpec(sound_place, is);
205 sound_place.name = deSerializeString(is);
206 sound_place.gain = readF1000(is);
209 range = readF1000(is);
211 // If you add anything here, insert it primarily inside the try-catch
212 // block to not need to increase the version.
214 sound_place_failed.name = deSerializeString(is);
215 sound_place_failed.gain = readF1000(is);
216 } catch(SerializationError &e) {};
223 // SUGG: Support chains of aliases?
226 CItemDefManager::ClientCached::ClientCached() :
227 inventory_texture(NULL),
232 CItemDefManager::CItemDefManager()
235 m_main_thread = thr_get_current_thread_id();
240 /******************************************************************************/
241 CItemDefManager::~CItemDefManager()
244 const std::vector<ClientCached*> &values = m_clientcached.getValues();
245 for(std::vector<ClientCached*>::const_iterator
246 i = values.begin(); i != values.end(); ++i) {
248 ClientCached *cc = *i;
250 cc->wield_mesh->drop();
255 for (std::map<std::string, ItemDefinition*>::iterator iter =
256 m_item_definitions.begin(); iter != m_item_definitions.end();
261 m_item_definitions.clear();
264 /******************************************************************************/
265 const ItemDefinition& CItemDefManager::get(const std::string &name_) const
267 // Convert name according to possible alias
268 std::string name = getAlias(name_);
269 // Get the definition
270 std::map<std::string, ItemDefinition*>::const_iterator i;
271 i = m_item_definitions.find(name);
272 if(i == m_item_definitions.end())
273 i = m_item_definitions.find("unknown");
274 assert(i != m_item_definitions.end());
278 /******************************************************************************/
279 std::string CItemDefManager::getAlias(const std::string &name) const
281 StringMap::const_iterator it = m_aliases.find(name);
282 if (it != m_aliases.end())
288 /******************************************************************************/
289 std::set<std::string> CItemDefManager::getAll() const
291 std::set<std::string> result;
292 for(std::map<std::string, ItemDefinition *>::const_iterator
293 it = m_item_definitions.begin();
294 it != m_item_definitions.end(); ++it) {
296 result.insert(it->first);
298 for (StringMap::const_iterator
299 it = m_aliases.begin();
300 it != m_aliases.end(); ++it) {
302 result.insert(it->first);
307 /******************************************************************************/
308 bool CItemDefManager::isKnown(const std::string &name_) const
310 // Convert name according to possible alias
311 std::string name = getAlias(name_);
312 // Get the definition
313 std::map<std::string, ItemDefinition*>::const_iterator i;
315 return m_item_definitions.find(name) != m_item_definitions.end();
319 /******************************************************************************/
320 CItemDefManager::ClientCached* CItemDefManager::createClientCachedDirect(const std::string &name,
321 IGameDef *gamedef) const
323 infostream<<"Lazily creating item texture and mesh for \""
324 <<name<<"\""<<std::endl;
326 // This is not thread-safe
327 sanity_check(thr_is_current_thread(m_main_thread));
329 // Skip if already in cache
330 ClientCached *cc = NULL;
331 m_clientcached.get(name, &cc);
335 ITextureSource *tsrc = gamedef->getTextureSource();
336 INodeDefManager *nodedef = gamedef->getNodeDefManager();
337 const ItemDefinition &def = get(name);
339 // Create new ClientCached
340 cc = new ClientCached();
342 // Create an inventory texture
343 cc->inventory_texture = NULL;
344 if (def.inventory_image != "")
345 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
347 // Additional processing for nodes:
348 // - Create a wield mesh if WieldMeshSceneNode can't render
349 // the node on its own.
350 // - If inventory_texture isn't set yet, create one using
351 // render-to-texture.
352 if (def.type == ITEM_NODE) {
353 createNodeItemTexture(name, def, nodedef, cc, gamedef, tsrc);
357 m_clientcached.set(name, cc);
362 /******************************************************************************/
363 CItemDefManager::ClientCached* CItemDefManager::getClientCached(const std::string &name,
364 IGameDef *gamedef) const
366 ClientCached *cc = NULL;
367 m_clientcached.get(name, &cc);
371 if (thr_is_current_thread(m_main_thread)) {
373 return createClientCachedDirect(name, gamedef);
377 // We're gonna ask the result to be put into here
378 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
380 // Throw a request in
381 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
384 // Wait result for a second
385 GetResult<std::string, ClientCached*, u8, u8>
386 result = result_queue.pop_front(1000);
388 if (result.key == name) {
393 catch(ItemNotFoundException &e)
395 errorstream<<"Waiting for clientcached " << name << " timed out."<<std::endl;
396 return &m_dummy_clientcached;
401 /******************************************************************************/
402 // Get item inventory texture
403 video::ITexture* CItemDefManager::getInventoryTexture(const std::string &name,
404 IGameDef *gamedef) const
406 ClientCached *cc = getClientCached(name, gamedef);
410 return cc->inventory_texture;
413 /******************************************************************************/
414 // Get item wield mesh
415 scene::IMesh* CItemDefManager::getWieldMesh(const std::string &name,
416 IGameDef *gamedef) const
418 ClientCached *cc = getClientCached(name, gamedef);
422 return cc->wield_mesh;
426 /******************************************************************************/
427 void CItemDefManager::clear()
429 for (std::map<std::string, ItemDefinition*>::const_iterator
430 i = m_item_definitions.begin();
431 i != m_item_definitions.end(); ++i) {
435 m_item_definitions.clear();
438 // Add the four builtin items:
440 // "unknown" is returned whenever an undefined item
441 // is accessed (is also the unknown node)
442 // "air" is the air node
443 // "ignore" is the ignore node
445 ItemDefinition* hand_def = new ItemDefinition;
447 hand_def->wield_image = "wieldhand.png";
448 hand_def->tool_capabilities = new ToolCapabilities;
449 m_item_definitions.insert(std::make_pair("", hand_def));
451 ItemDefinition* unknown_def = new ItemDefinition;
452 unknown_def->type = ITEM_NODE;
453 unknown_def->name = "unknown";
454 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
456 ItemDefinition* air_def = new ItemDefinition;
457 air_def->type = ITEM_NODE;
458 air_def->name = "air";
459 m_item_definitions.insert(std::make_pair("air", air_def));
461 ItemDefinition* ignore_def = new ItemDefinition;
462 ignore_def->type = ITEM_NODE;
463 ignore_def->name = "ignore";
464 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
467 /******************************************************************************/
468 void CItemDefManager::registerItem(const ItemDefinition &def)
470 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
471 // Ensure that the "" item (the hand) always has ToolCapabilities
473 FATAL_ERROR_IF(!def.tool_capabilities, "Hand does not have ToolCapabilities");
475 if (m_item_definitions.count(def.name) == 0)
476 m_item_definitions[def.name] = new ItemDefinition(def);
478 *(m_item_definitions[def.name]) = def;
480 // Remove conflicting alias if it exists
481 bool alias_removed = (m_aliases.erase(def.name) != 0);
483 infostream<<"ItemDefManager: erased alias "<<def.name
484 <<" because item was defined"<<std::endl;
487 /******************************************************************************/
488 void CItemDefManager::registerAlias(const std::string &name,
489 const std::string &convert_to)
491 if (m_item_definitions.find(name) == m_item_definitions.end()) {
493 verbosestream<<"ItemDefManager: setting alias "<<name
494 <<" -> "<<convert_to<<std::endl;
495 m_aliases[name] = convert_to;
499 /******************************************************************************/
500 void CItemDefManager::serialize(std::ostream &os, u16 protocol_version)
502 writeU8(os, 0); // version
503 u16 count = m_item_definitions.size();
506 for (std::map<std::string, ItemDefinition *>::const_iterator
507 it = m_item_definitions.begin();
508 it != m_item_definitions.end(); ++it) {
509 ItemDefinition *def = it->second;
510 // Serialize ItemDefinition and write wrapped in a string
511 std::ostringstream tmp_os(std::ios::binary);
512 def->serialize(tmp_os, protocol_version);
513 os << serializeString(tmp_os.str());
516 writeU16(os, m_aliases.size());
518 for (StringMap::const_iterator
519 it = m_aliases.begin();
520 it != m_aliases.end(); ++it) {
521 os << serializeString(it->first);
522 os << serializeString(it->second);
526 /******************************************************************************/
527 void CItemDefManager::deSerialize(std::istream &is)
532 int version = readU8(is);
534 throw SerializationError("unsupported ItemDefManager version");
535 u16 count = readU16(is);
537 for (u16 i=0; i<count; i++) {
539 // Deserialize a string and grab an ItemDefinition from it
540 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
542 def.deSerialize(tmp_is);
547 u16 num_aliases = readU16(is);
548 for (u16 i=0; i<num_aliases; i++) {
550 std::string name = deSerializeString(is);
551 std::string convert_to = deSerializeString(is);
552 registerAlias(name, convert_to);
556 /******************************************************************************/
557 void CItemDefManager::processQueue(IGameDef *gamedef)
560 //NOTE this is only thread safe for ONE consumer thread!
561 while (!m_get_clientcached_queue.empty()) {
563 GetRequest<std::string, ClientCached*, u8, u8>
564 request = m_get_clientcached_queue.pop();
566 m_get_clientcached_queue.pushResult(request,
567 createClientCachedDirect(request.key, gamedef));
573 /******************************************************************************/
574 void CItemDefManager::createNodeItemTexture(const std::string& name,
575 const ItemDefinition& def, INodeDefManager* nodedef,
576 ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const
578 // Get node properties
579 content_t id = nodedef->getId(name);
580 const ContentFeatures& f = nodedef->get(id);
581 bool need_rtt_mesh = cc->inventory_texture == NULL;
582 // Keep this in sync with WieldMeshSceneNode::setItem()
583 bool need_wield_mesh = !(f.mesh_ptr[0] || f.drawtype == NDT_NORMAL
584 || f.drawtype == NDT_ALLFACES || f.drawtype == NDT_AIRLIKE);
585 scene::IMesh* node_mesh = NULL;
587 if (need_rtt_mesh || need_wield_mesh) {
590 if (f.param_type == CPT_LIGHT)
594 Make a mesh from the node
596 MeshMakeData mesh_make_data(gamedef, false);
598 if (f.param_type_2 == CPT2_WALLMOUNTED)
601 MapNode mesh_make_node(id, param1, param2);
602 mesh_make_data.fillSingleNode(&mesh_make_node);
603 MapBlockMesh mapblock_mesh(&mesh_make_data, v3s16(0, 0, 0));
604 node_mesh = mapblock_mesh.getMesh();
606 video::SColor c(255, 255, 255, 255);
607 setMeshColor(node_mesh, c);
608 // scale and translate the mesh so it's a
609 // unit cube centered on the origin
610 scaleMesh(node_mesh, v3f(1.0 / BS, 1.0 / BS, 1.0 / BS));
611 translateMesh(node_mesh, v3f(-1.0, -1.0, -1.0));
614 Draw node mesh into a render target texture
618 TextureFromMeshParams params;
619 params.mesh = node_mesh;
620 params.dim.set(64, 64);
621 params.rtt_texture_name = "INVENTORY_" + def.name + "_RTT";
622 params.delete_texture_on_shutdown = true;
623 params.camera_position.set(0, 1.0, -1.5);
624 params.camera_position.rotateXZBy(45);
625 params.camera_lookat.set(0, 0, 0);
626 // Set orthogonal projection
627 params.camera_projection_matrix.buildProjectionMatrixOrthoLH(1.65,
629 params.ambient_light.set(1.0, 0.2, 0.2, 0.2);
630 params.light_position.set(10, 100, -50);
631 params.light_color.set(1.0, 0.5, 0.5, 0.5);
632 params.light_radius = 1000;
633 cc->inventory_texture = tsrc->generateTextureFromMesh(params);
634 // render-to-target didn't work
635 if (cc->inventory_texture == NULL) {
637 cc->inventory_texture = tsrc->getTexture(f.tiledef[0].name);
641 Use the node mesh as the wield mesh
643 if (need_wield_mesh) {
645 cc->wield_mesh = node_mesh;
646 cc->wield_mesh->grab();
647 // no way reference count can be smaller than 2 in this place!
648 assert(cc->wield_mesh->getReferenceCount() >= 2);
655 /******************************************************************************/
657 IWritableItemDefManager* createItemDefManager()
659 return new CItemDefManager();