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 "mapblock_mesh.h"
29 #include "wieldmesh.h"
30 #include "client/tile.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 >= 36
133 writeU8(os, 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, protocol_version);
147 tool_capabilities_s = tmp_os.str();
149 os << serializeString(tool_capabilities_s);
150 writeU16(os, groups.size());
151 for (const auto &group : groups) {
152 os << serializeString(group.first);
153 writeS16(os, group.second);
155 os << serializeString(node_placement_prediction);
156 os << serializeString(sound_place.name);
157 writeF1000(os, sound_place.gain);
158 writeF1000(os, range);
159 os << serializeString(sound_place_failed.name);
160 writeF1000(os, sound_place_failed.gain);
161 os << serializeString(palette_image);
162 writeARGB8(os, color);
164 writeF1000(os, sound_place.pitch);
165 writeF1000(os, sound_place_failed.pitch);
166 os << serializeString(inventory_overlay);
167 os << serializeString(wield_overlay);
170 void ItemDefinition::deSerialize(std::istream &is)
176 int version = readU8(is);
178 throw SerializationError("unsupported ItemDefinition version");
180 type = (enum ItemType)readU8(is);
181 name = deSerializeString(is);
182 description = deSerializeString(is);
183 inventory_image = deSerializeString(is);
184 wield_image = deSerializeString(is);
185 wield_scale = readV3F1000(is);
186 stack_max = readS16(is);
188 liquids_pointable = readU8(is);
189 std::string tool_capabilities_s = deSerializeString(is);
190 if(!tool_capabilities_s.empty())
192 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
193 tool_capabilities = new ToolCapabilities;
194 tool_capabilities->deSerialize(tmp_is);
197 u32 groups_size = readU16(is);
198 for(u32 i=0; i<groups_size; i++){
199 std::string name = deSerializeString(is);
200 int value = readS16(is);
201 groups[name] = value;
204 node_placement_prediction = deSerializeString(is);
205 //deserializeSimpleSoundSpec(sound_place, is);
206 sound_place.name = deSerializeString(is);
207 sound_place.gain = readF1000(is);
208 range = readF1000(is);
210 sound_place_failed.name = deSerializeString(is);
211 sound_place_failed.gain = readF1000(is);
212 palette_image = deSerializeString(is);
213 color = readARGB8(is);
215 sound_place.pitch = readF1000(is);
216 sound_place_failed.pitch = readF1000(is);
217 inventory_overlay = deSerializeString(is);
218 wield_overlay = deSerializeString(is);
220 // If you add anything here, insert it primarily inside the try-catch
221 // block to not need to increase the version.
223 //} catch(SerializationError &e) {};
230 // SUGG: Support chains of aliases?
232 class CItemDefManager: public IWritableItemDefManager
237 video::ITexture *inventory_texture;
242 inventory_texture(NULL),
253 m_main_thread = std::this_thread::get_id();
257 virtual ~CItemDefManager()
260 const std::vector<ClientCached*> &values = m_clientcached.getValues();
261 for (ClientCached *cc : values) {
262 if (cc->wield_mesh.mesh)
263 cc->wield_mesh.mesh->drop();
268 for (auto &item_definition : m_item_definitions) {
269 delete item_definition.second;
271 m_item_definitions.clear();
273 virtual const ItemDefinition& get(const std::string &name_) const
275 // Convert name according to possible alias
276 std::string name = getAlias(name_);
277 // Get the definition
278 std::map<std::string, ItemDefinition*>::const_iterator i;
279 i = m_item_definitions.find(name);
280 if(i == m_item_definitions.end())
281 i = m_item_definitions.find("unknown");
282 assert(i != m_item_definitions.end());
285 virtual const std::string &getAlias(const std::string &name) const
287 StringMap::const_iterator it = m_aliases.find(name);
288 if (it != m_aliases.end())
292 virtual void getAll(std::set<std::string> &result) const
295 for (const auto &item_definition : m_item_definitions) {
296 result.insert(item_definition.first);
299 for (const auto &alias : m_aliases) {
300 result.insert(alias.first);
303 virtual bool isKnown(const std::string &name_) const
305 // Convert name according to possible alias
306 std::string name = getAlias(name_);
307 // Get the definition
308 std::map<std::string, ItemDefinition*>::const_iterator i;
309 return m_item_definitions.find(name) != m_item_definitions.end();
313 ClientCached* createClientCachedDirect(const std::string &name,
314 Client *client) const
316 infostream<<"Lazily creating item texture and mesh for \""
317 <<name<<"\""<<std::endl;
319 // This is not thread-safe
320 sanity_check(std::this_thread::get_id() == m_main_thread);
322 // Skip if already in cache
323 ClientCached *cc = NULL;
324 m_clientcached.get(name, &cc);
328 ITextureSource *tsrc = client->getTextureSource();
329 const ItemDefinition &def = get(name);
331 // Create new ClientCached
332 cc = new ClientCached();
334 // Create an inventory texture
335 cc->inventory_texture = NULL;
336 if (!def.inventory_image.empty())
337 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
339 ItemStack item = ItemStack();
340 item.name = def.name;
342 getItemMesh(client, item, &(cc->wield_mesh));
344 cc->palette = tsrc->getPalette(def.palette_image);
347 m_clientcached.set(name, cc);
351 ClientCached* getClientCached(const std::string &name,
352 Client *client) const
354 ClientCached *cc = NULL;
355 m_clientcached.get(name, &cc);
359 if (std::this_thread::get_id() == m_main_thread) {
360 return createClientCachedDirect(name, client);
363 // We're gonna ask the result to be put into here
364 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
366 // Throw a request in
367 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
370 // Wait result for a second
371 GetResult<std::string, ClientCached*, u8, u8>
372 result = result_queue.pop_front(1000);
374 if (result.key == name) {
378 } catch(ItemNotFoundException &e) {
379 errorstream << "Waiting for clientcached " << name
380 << " timed out." << std::endl;
381 return &m_dummy_clientcached;
384 // Get item inventory texture
385 virtual video::ITexture* getInventoryTexture(const std::string &name,
386 Client *client) const
388 ClientCached *cc = getClientCached(name, client);
391 return cc->inventory_texture;
393 // Get item wield mesh
394 virtual ItemMesh* getWieldMesh(const std::string &name,
395 Client *client) const
397 ClientCached *cc = getClientCached(name, client);
400 return &(cc->wield_mesh);
404 virtual Palette* getPalette(const std::string &name,
405 Client *client) const
407 ClientCached *cc = getClientCached(name, client);
413 virtual video::SColor getItemstackColor(const ItemStack &stack,
414 Client *client) const
416 // Look for direct color definition
417 const std::string &colorstring = stack.metadata.getString("color", 0);
418 video::SColor directcolor;
419 if (!colorstring.empty() && parseColorString(colorstring, directcolor, true))
421 // See if there is a palette
422 Palette *palette = getPalette(stack.name, client);
423 const std::string &index = stack.metadata.getString("palette_index", 0);
424 if (palette && !index.empty())
425 return (*palette)[mystoi(index, 0, 255)];
427 return get(stack.name).color;
432 for(std::map<std::string, ItemDefinition*>::const_iterator
433 i = m_item_definitions.begin();
434 i != m_item_definitions.end(); ++i)
438 m_item_definitions.clear();
441 // Add the four builtin items:
443 // "unknown" is returned whenever an undefined item
444 // is accessed (is also the unknown node)
445 // "air" is the air node
446 // "ignore" is the ignore node
448 ItemDefinition* hand_def = new ItemDefinition;
450 hand_def->wield_image = "wieldhand.png";
451 hand_def->tool_capabilities = new ToolCapabilities;
452 m_item_definitions.insert(std::make_pair("", hand_def));
454 ItemDefinition* unknown_def = new ItemDefinition;
455 unknown_def->type = ITEM_NODE;
456 unknown_def->name = "unknown";
457 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
459 ItemDefinition* air_def = new ItemDefinition;
460 air_def->type = ITEM_NODE;
461 air_def->name = "air";
462 m_item_definitions.insert(std::make_pair("air", air_def));
464 ItemDefinition* ignore_def = new ItemDefinition;
465 ignore_def->type = ITEM_NODE;
466 ignore_def->name = "ignore";
467 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
469 virtual void registerItem(const ItemDefinition &def)
471 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
472 // Ensure that the "" item (the hand) always has ToolCapabilities
473 if (def.name.empty())
474 FATAL_ERROR_IF(!def.tool_capabilities, "Hand does not have ToolCapabilities");
476 if(m_item_definitions.count(def.name) == 0)
477 m_item_definitions[def.name] = new ItemDefinition(def);
479 *(m_item_definitions[def.name]) = def;
481 // Remove conflicting alias if it exists
482 bool alias_removed = (m_aliases.erase(def.name) != 0);
484 infostream<<"ItemDefManager: erased alias "<<def.name
485 <<" because item was defined"<<std::endl;
487 virtual void unregisterItem(const std::string &name)
489 verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
491 delete m_item_definitions[name];
492 m_item_definitions.erase(name);
494 virtual void registerAlias(const std::string &name,
495 const std::string &convert_to)
497 if (m_item_definitions.find(name) == m_item_definitions.end()) {
498 verbosestream<<"ItemDefManager: setting alias "<<name
499 <<" -> "<<convert_to<<std::endl;
500 m_aliases[name] = convert_to;
503 void serialize(std::ostream &os, u16 protocol_version)
505 writeU8(os, 0); // version
506 u16 count = m_item_definitions.size();
509 for (std::map<std::string, ItemDefinition *>::const_iterator
510 it = m_item_definitions.begin();
511 it != m_item_definitions.end(); ++it) {
512 ItemDefinition *def = it->second;
513 // Serialize ItemDefinition and write wrapped in a string
514 std::ostringstream tmp_os(std::ios::binary);
515 def->serialize(tmp_os, protocol_version);
516 os << serializeString(tmp_os.str());
519 writeU16(os, m_aliases.size());
521 for (StringMap::const_iterator
522 it = m_aliases.begin();
523 it != m_aliases.end(); ++it) {
524 os << serializeString(it->first);
525 os << serializeString(it->second);
528 void deSerialize(std::istream &is)
533 int version = readU8(is);
535 throw SerializationError("unsupported ItemDefManager version");
536 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);
546 u16 num_aliases = readU16(is);
547 for(u16 i=0; i<num_aliases; i++)
549 std::string name = deSerializeString(is);
550 std::string convert_to = deSerializeString(is);
551 registerAlias(name, convert_to);
554 void processQueue(IGameDef *gamedef)
557 //NOTE this is only thread safe for ONE consumer thread!
558 while(!m_get_clientcached_queue.empty())
560 GetRequest<std::string, ClientCached*, u8, u8>
561 request = m_get_clientcached_queue.pop();
563 m_get_clientcached_queue.pushResult(request,
564 createClientCachedDirect(request.key, (Client *)gamedef));
570 std::map<std::string, ItemDefinition*> m_item_definitions;
574 // The id of the thread that is allowed to use irrlicht directly
575 std::thread::id m_main_thread;
576 // A reference to this can be returned when nothing is found, to avoid NULLs
577 mutable ClientCached m_dummy_clientcached;
578 // Cached textures and meshes
579 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
580 // Queued clientcached fetches (to be processed by the main thread)
581 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
585 IWritableItemDefManager* createItemDefManager()
587 return new CItemDefManager();