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.
25 #include "inventory.h"
27 #include "client/mapblock_mesh.h"
28 #include "client/mesh.h"
29 #include "client/wieldmesh.h"
30 #include "client/tile.h"
31 #include "client/client.h"
35 #include "util/serialize.h"
36 #include "util/container.h"
37 #include "util/thread.h"
48 ItemDefinition::ItemDefinition()
53 ItemDefinition::ItemDefinition(const ItemDefinition &def)
59 ItemDefinition& ItemDefinition::operator=(const ItemDefinition &def)
68 description = def.description;
69 inventory_image = def.inventory_image;
70 inventory_overlay = def.inventory_overlay;
71 wield_image = def.wield_image;
72 wield_overlay = def.wield_overlay;
73 wield_scale = def.wield_scale;
74 stack_max = def.stack_max;
76 liquids_pointable = def.liquids_pointable;
77 if(def.tool_capabilities)
79 tool_capabilities = new ToolCapabilities(
80 *def.tool_capabilities);
83 node_placement_prediction = def.node_placement_prediction;
84 sound_place = def.sound_place;
85 sound_place_failed = def.sound_place_failed;
87 palette_image = def.palette_image;
92 ItemDefinition::~ItemDefinition()
97 void ItemDefinition::resetInitial()
99 // Initialize pointers to NULL so reset() does not delete undefined pointers
100 tool_capabilities = NULL;
104 void ItemDefinition::reset()
109 inventory_image = "";
110 inventory_overlay = "";
114 color = video::SColor(0xFFFFFFFF);
115 wield_scale = v3f(1.0, 1.0, 1.0);
118 liquids_pointable = false;
119 delete tool_capabilities;
120 tool_capabilities = NULL;
122 sound_place = SimpleSoundSpec();
123 sound_place_failed = SimpleSoundSpec();
126 node_placement_prediction = "";
129 void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
131 // protocol_version >= 37
133 writeU8(os, version);
135 os << serializeString(name);
136 os << serializeString(description);
137 os << serializeString(inventory_image);
138 os << serializeString(wield_image);
139 writeV3F32(os, wield_scale);
140 writeS16(os, stack_max);
142 writeU8(os, liquids_pointable);
144 std::string tool_capabilities_s;
145 if (tool_capabilities) {
146 std::ostringstream tmp_os(std::ios::binary);
147 tool_capabilities->serialize(tmp_os, protocol_version);
148 tool_capabilities_s = tmp_os.str();
150 os << serializeString(tool_capabilities_s);
152 writeU16(os, groups.size());
153 for (const auto &group : groups) {
154 os << serializeString(group.first);
155 writeS16(os, group.second);
158 os << serializeString(node_placement_prediction);
160 // Version from ContentFeatures::serialize to keep in sync
161 sound_place.serialize(os, CONTENTFEATURES_VERSION);
162 sound_place_failed.serialize(os, CONTENTFEATURES_VERSION);
165 os << serializeString(palette_image);
166 writeARGB8(os, color);
167 os << serializeString(inventory_overlay);
168 os << serializeString(wield_overlay);
171 void ItemDefinition::deSerialize(std::istream &is)
177 int version = readU8(is);
179 throw SerializationError("unsupported ItemDefinition version");
181 type = (enum ItemType)readU8(is);
182 name = deSerializeString(is);
183 description = deSerializeString(is);
184 inventory_image = deSerializeString(is);
185 wield_image = deSerializeString(is);
186 wield_scale = readV3F32(is);
187 stack_max = readS16(is);
189 liquids_pointable = readU8(is);
191 std::string tool_capabilities_s = deSerializeString(is);
192 if (!tool_capabilities_s.empty()) {
193 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
194 tool_capabilities = new ToolCapabilities;
195 tool_capabilities->deSerialize(tmp_is);
199 u32 groups_size = readU16(is);
200 for(u32 i=0; i<groups_size; i++){
201 std::string name = deSerializeString(is);
202 int value = readS16(is);
203 groups[name] = value;
206 node_placement_prediction = deSerializeString(is);
208 // Version from ContentFeatures::serialize to keep in sync
209 sound_place.deSerialize(is, CONTENTFEATURES_VERSION);
210 sound_place_failed.deSerialize(is, CONTENTFEATURES_VERSION);
213 palette_image = deSerializeString(is);
214 color = readARGB8(is);
215 inventory_overlay = deSerializeString(is);
216 wield_overlay = deSerializeString(is);
218 // If you add anything here, insert it primarily inside the try-catch
219 // block to not need to increase the version.
221 //} catch(SerializationError &e) {};
229 // SUGG: Support chains of aliases?
231 class CItemDefManager: public IWritableItemDefManager
236 video::ITexture *inventory_texture;
241 inventory_texture(NULL),
252 m_main_thread = std::this_thread::get_id();
256 virtual ~CItemDefManager()
259 const std::vector<ClientCached*> &values = m_clientcached.getValues();
260 for (ClientCached *cc : values) {
261 if (cc->wield_mesh.mesh)
262 cc->wield_mesh.mesh->drop();
267 for (auto &item_definition : m_item_definitions) {
268 delete item_definition.second;
270 m_item_definitions.clear();
272 virtual const ItemDefinition& get(const std::string &name_) const
274 // Convert name according to possible alias
275 std::string name = getAlias(name_);
276 // Get the definition
277 std::map<std::string, ItemDefinition*>::const_iterator i;
278 i = m_item_definitions.find(name);
279 if(i == m_item_definitions.end())
280 i = m_item_definitions.find("unknown");
281 assert(i != m_item_definitions.end());
284 virtual const std::string &getAlias(const std::string &name) const
286 StringMap::const_iterator it = m_aliases.find(name);
287 if (it != m_aliases.end())
291 virtual void getAll(std::set<std::string> &result) const
294 for (const auto &item_definition : m_item_definitions) {
295 result.insert(item_definition.first);
298 for (const auto &alias : m_aliases) {
299 result.insert(alias.first);
302 virtual bool isKnown(const std::string &name_) const
304 // Convert name according to possible alias
305 std::string name = getAlias(name_);
306 // Get the definition
307 std::map<std::string, ItemDefinition*>::const_iterator i;
308 return m_item_definitions.find(name) != m_item_definitions.end();
312 ClientCached* createClientCachedDirect(const std::string &name,
313 Client *client) const
315 infostream<<"Lazily creating item texture and mesh for \""
316 <<name<<"\""<<std::endl;
318 // This is not thread-safe
319 sanity_check(std::this_thread::get_id() == m_main_thread);
321 // Skip if already in cache
322 ClientCached *cc = NULL;
323 m_clientcached.get(name, &cc);
327 ITextureSource *tsrc = client->getTextureSource();
328 const ItemDefinition &def = get(name);
330 // Create new ClientCached
331 cc = new ClientCached();
333 // Create an inventory texture
334 cc->inventory_texture = NULL;
335 if (!def.inventory_image.empty())
336 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
338 ItemStack item = ItemStack();
339 item.name = def.name;
341 getItemMesh(client, item, &(cc->wield_mesh));
343 cc->palette = tsrc->getPalette(def.palette_image);
346 m_clientcached.set(name, cc);
350 ClientCached* getClientCached(const std::string &name,
351 Client *client) const
353 ClientCached *cc = NULL;
354 m_clientcached.get(name, &cc);
358 if (std::this_thread::get_id() == m_main_thread) {
359 return createClientCachedDirect(name, client);
362 // We're gonna ask the result to be put into here
363 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
365 // Throw a request in
366 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
369 // Wait result for a second
370 GetResult<std::string, ClientCached*, u8, u8>
371 result = result_queue.pop_front(1000);
373 if (result.key == name) {
377 } catch(ItemNotFoundException &e) {
378 errorstream << "Waiting for clientcached " << name
379 << " timed out." << std::endl;
380 return &m_dummy_clientcached;
383 // Get item inventory texture
384 virtual video::ITexture* getInventoryTexture(const std::string &name,
385 Client *client) const
387 ClientCached *cc = getClientCached(name, client);
390 return cc->inventory_texture;
392 // Get item wield mesh
393 virtual ItemMesh* getWieldMesh(const std::string &name,
394 Client *client) const
396 ClientCached *cc = getClientCached(name, client);
399 return &(cc->wield_mesh);
403 virtual Palette* getPalette(const std::string &name,
404 Client *client) const
406 ClientCached *cc = getClientCached(name, client);
412 virtual video::SColor getItemstackColor(const ItemStack &stack,
413 Client *client) const
415 // Look for direct color definition
416 const std::string &colorstring = stack.metadata.getString("color", 0);
417 video::SColor directcolor;
418 if (!colorstring.empty() && parseColorString(colorstring, directcolor, true))
420 // See if there is a palette
421 Palette *palette = getPalette(stack.name, client);
422 const std::string &index = stack.metadata.getString("palette_index", 0);
423 if (palette && !index.empty())
424 return (*palette)[mystoi(index, 0, 255)];
426 return get(stack.name).color;
431 for(std::map<std::string, ItemDefinition*>::const_iterator
432 i = m_item_definitions.begin();
433 i != m_item_definitions.end(); ++i)
437 m_item_definitions.clear();
440 // Add the four builtin items:
442 // "unknown" is returned whenever an undefined item
443 // is accessed (is also the unknown node)
444 // "air" is the air node
445 // "ignore" is the ignore node
447 ItemDefinition* hand_def = new ItemDefinition;
449 hand_def->wield_image = "wieldhand.png";
450 hand_def->tool_capabilities = new ToolCapabilities;
451 m_item_definitions.insert(std::make_pair("", hand_def));
453 ItemDefinition* unknown_def = new ItemDefinition;
454 unknown_def->type = ITEM_NODE;
455 unknown_def->name = "unknown";
456 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
458 ItemDefinition* air_def = new ItemDefinition;
459 air_def->type = ITEM_NODE;
460 air_def->name = "air";
461 m_item_definitions.insert(std::make_pair("air", air_def));
463 ItemDefinition* ignore_def = new ItemDefinition;
464 ignore_def->type = ITEM_NODE;
465 ignore_def->name = "ignore";
466 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
468 virtual void registerItem(const ItemDefinition &def)
470 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
471 // Ensure that the "" item (the hand) always has ToolCapabilities
472 if (def.name.empty())
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;
486 virtual void unregisterItem(const std::string &name)
488 verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
490 delete m_item_definitions[name];
491 m_item_definitions.erase(name);
493 virtual void registerAlias(const std::string &name,
494 const std::string &convert_to)
496 if (m_item_definitions.find(name) == m_item_definitions.end()) {
497 verbosestream<<"ItemDefManager: setting alias "<<name
498 <<" -> "<<convert_to<<std::endl;
499 m_aliases[name] = convert_to;
502 void serialize(std::ostream &os, u16 protocol_version)
504 writeU8(os, 0); // version
505 u16 count = m_item_definitions.size();
508 for (std::map<std::string, ItemDefinition *>::const_iterator
509 it = m_item_definitions.begin();
510 it != m_item_definitions.end(); ++it) {
511 ItemDefinition *def = it->second;
512 // Serialize ItemDefinition and write wrapped in a string
513 std::ostringstream tmp_os(std::ios::binary);
514 def->serialize(tmp_os, protocol_version);
515 os << serializeString(tmp_os.str());
518 writeU16(os, m_aliases.size());
520 for (StringMap::const_iterator
521 it = m_aliases.begin();
522 it != m_aliases.end(); ++it) {
523 os << serializeString(it->first);
524 os << serializeString(it->second);
527 void deSerialize(std::istream &is)
532 int version = readU8(is);
534 throw SerializationError("unsupported ItemDefManager version");
535 u16 count = readU16(is);
536 for(u16 i=0; i<count; i++)
538 // Deserialize a string and grab an ItemDefinition from it
539 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
541 def.deSerialize(tmp_is);
545 u16 num_aliases = readU16(is);
546 for(u16 i=0; i<num_aliases; i++)
548 std::string name = deSerializeString(is);
549 std::string convert_to = deSerializeString(is);
550 registerAlias(name, convert_to);
553 void processQueue(IGameDef *gamedef)
556 //NOTE this is only thread safe for ONE consumer thread!
557 while(!m_get_clientcached_queue.empty())
559 GetRequest<std::string, ClientCached*, u8, u8>
560 request = m_get_clientcached_queue.pop();
562 m_get_clientcached_queue.pushResult(request,
563 createClientCachedDirect(request.key, (Client *)gamedef));
569 std::map<std::string, ItemDefinition*> m_item_definitions;
573 // The id of the thread that is allowed to use irrlicht directly
574 std::thread::id m_main_thread;
575 // A reference to this can be returned when nothing is found, to avoid NULLs
576 mutable ClientCached m_dummy_clientcached;
577 // Cached textures and meshes
578 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
579 // Queued clientcached fetches (to be processed by the main thread)
580 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
584 IWritableItemDefManager* createItemDefManager()
586 return new CItemDefManager();