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"
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 inventory_overlay = def.inventory_overlay;
67 wield_image = def.wield_image;
68 wield_overlay = def.wield_overlay;
69 wield_scale = def.wield_scale;
70 stack_max = def.stack_max;
72 liquids_pointable = def.liquids_pointable;
73 if(def.tool_capabilities)
75 tool_capabilities = new ToolCapabilities(
76 *def.tool_capabilities);
79 node_placement_prediction = def.node_placement_prediction;
80 sound_place = def.sound_place;
81 sound_place_failed = def.sound_place_failed;
83 palette_image = def.palette_image;
88 ItemDefinition::~ItemDefinition()
93 void ItemDefinition::resetInitial()
95 // Initialize pointers to NULL so reset() does not delete undefined pointers
96 tool_capabilities = NULL;
100 void ItemDefinition::reset()
105 inventory_image = "";
106 inventory_overlay = "";
110 color = video::SColor(0xFFFFFFFF);
111 wield_scale = v3f(1.0, 1.0, 1.0);
114 liquids_pointable = false;
115 delete tool_capabilities;
116 tool_capabilities = NULL;
118 sound_place = SimpleSoundSpec();
119 sound_place_failed = SimpleSoundSpec();
122 node_placement_prediction = "";
125 void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
127 // protocol_version >= 37
129 writeU8(os, version);
131 os << serializeString(name);
132 os << serializeString(description);
133 os << serializeString(inventory_image);
134 os << serializeString(wield_image);
135 writeV3F32(os, wield_scale);
136 writeS16(os, stack_max);
138 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, protocol_version);
144 tool_capabilities_s = tmp_os.str();
146 os << serializeString(tool_capabilities_s);
148 writeU16(os, groups.size());
149 for (const auto &group : groups) {
150 os << serializeString(group.first);
151 writeS16(os, group.second);
154 os << serializeString(node_placement_prediction);
156 // Version from ContentFeatures::serialize to keep in sync
157 sound_place.serialize(os, CONTENTFEATURES_VERSION);
158 sound_place_failed.serialize(os, CONTENTFEATURES_VERSION);
161 os << serializeString(palette_image);
162 writeARGB8(os, color);
163 os << serializeString(inventory_overlay);
164 os << serializeString(wield_overlay);
167 void ItemDefinition::deSerialize(std::istream &is)
173 int version = readU8(is);
175 throw SerializationError("unsupported ItemDefinition version");
177 type = (enum ItemType)readU8(is);
178 name = deSerializeString(is);
179 description = deSerializeString(is);
180 inventory_image = deSerializeString(is);
181 wield_image = deSerializeString(is);
182 wield_scale = readV3F32(is);
183 stack_max = readS16(is);
185 liquids_pointable = readU8(is);
187 std::string tool_capabilities_s = deSerializeString(is);
188 if (!tool_capabilities_s.empty()) {
189 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
190 tool_capabilities = new ToolCapabilities;
191 tool_capabilities->deSerialize(tmp_is);
195 u32 groups_size = readU16(is);
196 for(u32 i=0; i<groups_size; i++){
197 std::string name = deSerializeString(is);
198 int value = readS16(is);
199 groups[name] = value;
202 node_placement_prediction = deSerializeString(is);
204 // Version from ContentFeatures::serialize to keep in sync
205 sound_place.deSerialize(is, CONTENTFEATURES_VERSION);
206 sound_place_failed.deSerialize(is, CONTENTFEATURES_VERSION);
209 palette_image = deSerializeString(is);
210 color = readARGB8(is);
211 inventory_overlay = deSerializeString(is);
212 wield_overlay = deSerializeString(is);
214 // If you add anything here, insert it primarily inside the try-catch
215 // block to not need to increase the version.
217 //} catch(SerializationError &e) {};
225 // SUGG: Support chains of aliases?
227 class CItemDefManager: public IWritableItemDefManager
232 video::ITexture *inventory_texture;
237 inventory_texture(NULL),
248 m_main_thread = std::this_thread::get_id();
252 virtual ~CItemDefManager()
255 const std::vector<ClientCached*> &values = m_clientcached.getValues();
256 for (ClientCached *cc : values) {
257 if (cc->wield_mesh.mesh)
258 cc->wield_mesh.mesh->drop();
263 for (auto &item_definition : m_item_definitions) {
264 delete item_definition.second;
266 m_item_definitions.clear();
268 virtual const ItemDefinition& get(const std::string &name_) const
270 // Convert name according to possible alias
271 std::string name = getAlias(name_);
272 // Get the definition
273 std::map<std::string, ItemDefinition*>::const_iterator i;
274 i = m_item_definitions.find(name);
275 if(i == m_item_definitions.end())
276 i = m_item_definitions.find("unknown");
277 assert(i != m_item_definitions.end());
280 virtual const std::string &getAlias(const std::string &name) const
282 StringMap::const_iterator it = m_aliases.find(name);
283 if (it != m_aliases.end())
287 virtual void getAll(std::set<std::string> &result) const
290 for (const auto &item_definition : m_item_definitions) {
291 result.insert(item_definition.first);
294 for (const auto &alias : m_aliases) {
295 result.insert(alias.first);
298 virtual bool isKnown(const std::string &name_) const
300 // Convert name according to possible alias
301 std::string name = getAlias(name_);
302 // Get the definition
303 std::map<std::string, ItemDefinition*>::const_iterator i;
304 return m_item_definitions.find(name) != m_item_definitions.end();
308 ClientCached* createClientCachedDirect(const std::string &name,
309 Client *client) const
311 infostream<<"Lazily creating item texture and mesh for \""
312 <<name<<"\""<<std::endl;
314 // This is not thread-safe
315 sanity_check(std::this_thread::get_id() == m_main_thread);
317 // Skip if already in cache
318 ClientCached *cc = NULL;
319 m_clientcached.get(name, &cc);
323 ITextureSource *tsrc = client->getTextureSource();
324 const ItemDefinition &def = get(name);
326 // Create new ClientCached
327 cc = new ClientCached();
329 // Create an inventory texture
330 cc->inventory_texture = NULL;
331 if (!def.inventory_image.empty())
332 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
334 ItemStack item = ItemStack();
335 item.name = def.name;
337 getItemMesh(client, item, &(cc->wield_mesh));
339 cc->palette = tsrc->getPalette(def.palette_image);
342 m_clientcached.set(name, cc);
346 ClientCached* getClientCached(const std::string &name,
347 Client *client) const
349 ClientCached *cc = NULL;
350 m_clientcached.get(name, &cc);
354 if (std::this_thread::get_id() == m_main_thread) {
355 return createClientCachedDirect(name, client);
358 // We're gonna ask the result to be put into here
359 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
361 // Throw a request in
362 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
365 // Wait result for a second
366 GetResult<std::string, ClientCached*, u8, u8>
367 result = result_queue.pop_front(1000);
369 if (result.key == name) {
373 } catch(ItemNotFoundException &e) {
374 errorstream << "Waiting for clientcached " << name
375 << " timed out." << std::endl;
376 return &m_dummy_clientcached;
379 // Get item inventory texture
380 virtual video::ITexture* getInventoryTexture(const std::string &name,
381 Client *client) const
383 ClientCached *cc = getClientCached(name, client);
386 return cc->inventory_texture;
388 // Get item wield mesh
389 virtual ItemMesh* getWieldMesh(const std::string &name,
390 Client *client) const
392 ClientCached *cc = getClientCached(name, client);
395 return &(cc->wield_mesh);
399 virtual Palette* getPalette(const std::string &name,
400 Client *client) const
402 ClientCached *cc = getClientCached(name, client);
408 virtual video::SColor getItemstackColor(const ItemStack &stack,
409 Client *client) const
411 // Look for direct color definition
412 const std::string &colorstring = stack.metadata.getString("color", 0);
413 video::SColor directcolor;
414 if (!colorstring.empty() && parseColorString(colorstring, directcolor, true))
416 // See if there is a palette
417 Palette *palette = getPalette(stack.name, client);
418 const std::string &index = stack.metadata.getString("palette_index", 0);
419 if (palette && !index.empty())
420 return (*palette)[mystoi(index, 0, 255)];
422 return get(stack.name).color;
427 for(std::map<std::string, ItemDefinition*>::const_iterator
428 i = m_item_definitions.begin();
429 i != m_item_definitions.end(); ++i)
433 m_item_definitions.clear();
436 // Add the four builtin items:
438 // "unknown" is returned whenever an undefined item
439 // is accessed (is also the unknown node)
440 // "air" is the air node
441 // "ignore" is the ignore node
443 ItemDefinition* hand_def = new ItemDefinition;
445 hand_def->wield_image = "wieldhand.png";
446 hand_def->tool_capabilities = new ToolCapabilities;
447 m_item_definitions.insert(std::make_pair("", hand_def));
449 ItemDefinition* unknown_def = new ItemDefinition;
450 unknown_def->type = ITEM_NODE;
451 unknown_def->name = "unknown";
452 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
454 ItemDefinition* air_def = new ItemDefinition;
455 air_def->type = ITEM_NODE;
456 air_def->name = "air";
457 m_item_definitions.insert(std::make_pair("air", air_def));
459 ItemDefinition* ignore_def = new ItemDefinition;
460 ignore_def->type = ITEM_NODE;
461 ignore_def->name = "ignore";
462 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
464 virtual void registerItem(const ItemDefinition &def)
466 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
467 // Ensure that the "" item (the hand) always has ToolCapabilities
468 if (def.name.empty())
469 FATAL_ERROR_IF(!def.tool_capabilities, "Hand does not have ToolCapabilities");
471 if(m_item_definitions.count(def.name) == 0)
472 m_item_definitions[def.name] = new ItemDefinition(def);
474 *(m_item_definitions[def.name]) = def;
476 // Remove conflicting alias if it exists
477 bool alias_removed = (m_aliases.erase(def.name) != 0);
479 infostream<<"ItemDefManager: erased alias "<<def.name
480 <<" because item was defined"<<std::endl;
482 virtual void unregisterItem(const std::string &name)
484 verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
486 delete m_item_definitions[name];
487 m_item_definitions.erase(name);
489 virtual void registerAlias(const std::string &name,
490 const std::string &convert_to)
492 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;
498 void serialize(std::ostream &os, u16 protocol_version)
500 writeU8(os, 0); // version
501 u16 count = m_item_definitions.size();
504 for (std::map<std::string, ItemDefinition *>::const_iterator
505 it = m_item_definitions.begin();
506 it != m_item_definitions.end(); ++it) {
507 ItemDefinition *def = it->second;
508 // Serialize ItemDefinition and write wrapped in a string
509 std::ostringstream tmp_os(std::ios::binary);
510 def->serialize(tmp_os, protocol_version);
511 os << serializeString(tmp_os.str());
514 writeU16(os, m_aliases.size());
516 for (StringMap::const_iterator
517 it = m_aliases.begin();
518 it != m_aliases.end(); ++it) {
519 os << serializeString(it->first);
520 os << serializeString(it->second);
523 void deSerialize(std::istream &is)
528 int version = readU8(is);
530 throw SerializationError("unsupported ItemDefManager version");
531 u16 count = readU16(is);
532 for(u16 i=0; i<count; i++)
534 // Deserialize a string and grab an ItemDefinition from it
535 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
537 def.deSerialize(tmp_is);
541 u16 num_aliases = readU16(is);
542 for(u16 i=0; i<num_aliases; i++)
544 std::string name = deSerializeString(is);
545 std::string convert_to = deSerializeString(is);
546 registerAlias(name, convert_to);
549 void processQueue(IGameDef *gamedef)
552 //NOTE this is only thread safe for ONE consumer thread!
553 while(!m_get_clientcached_queue.empty())
555 GetRequest<std::string, ClientCached*, u8, u8>
556 request = m_get_clientcached_queue.pop();
558 m_get_clientcached_queue.pushResult(request,
559 createClientCachedDirect(request.key, (Client *)gamedef));
565 std::map<std::string, ItemDefinition*> m_item_definitions;
569 // The id of the thread that is allowed to use irrlicht directly
570 std::thread::id m_main_thread;
571 // A reference to this can be returned when nothing is found, to avoid NULLs
572 mutable ClientCached m_dummy_clientcached;
573 // Cached textures and meshes
574 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
575 // Queued clientcached fetches (to be processed by the main thread)
576 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
580 IWritableItemDefManager* createItemDefManager()
582 return new CItemDefManager();