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 wield_image = def.wield_image;
71 wield_scale = def.wield_scale;
72 stack_max = def.stack_max;
74 liquids_pointable = def.liquids_pointable;
75 if(def.tool_capabilities)
77 tool_capabilities = new ToolCapabilities(
78 *def.tool_capabilities);
81 node_placement_prediction = def.node_placement_prediction;
82 sound_place = def.sound_place;
83 sound_place_failed = def.sound_place_failed;
85 palette_image = def.palette_image;
90 ItemDefinition::~ItemDefinition()
95 void ItemDefinition::resetInitial()
97 // Initialize pointers to NULL so reset() does not delete undefined pointers
98 tool_capabilities = NULL;
102 void ItemDefinition::reset()
107 inventory_image = "";
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
128 writeU8(os, 3); // version (proto > 20)
130 os << serializeString(name);
131 os << serializeString(description);
132 os << serializeString(inventory_image);
133 os << serializeString(wield_image);
134 writeV3F1000(os, wield_scale);
135 writeS16(os, stack_max);
137 writeU8(os, liquids_pointable);
138 std::string tool_capabilities_s = "";
139 if(tool_capabilities){
140 std::ostringstream tmp_os(std::ios::binary);
141 tool_capabilities->serialize(tmp_os, protocol_version);
142 tool_capabilities_s = tmp_os.str();
144 os << serializeString(tool_capabilities_s);
145 writeU16(os, groups.size());
146 for (ItemGroupList::const_iterator
147 i = groups.begin(); i != groups.end(); ++i){
148 os << serializeString(i->first);
149 writeS16(os, i->second);
151 os << serializeString(node_placement_prediction);
152 os << serializeString(sound_place.name);
153 writeF1000(os, sound_place.gain);
154 writeF1000(os, range);
155 os << serializeString(sound_place_failed.name);
156 writeF1000(os, sound_place_failed.gain);
157 os << serializeString(palette_image);
158 writeU32(os, color.color);
159 writeF1000(os, sound_place.pitch);
160 writeF1000(os, sound_place_failed.pitch);
163 void ItemDefinition::deSerialize(std::istream &is)
169 int version = readU8(is);
170 if(version < 1 || version > 3)
171 throw SerializationError("unsupported ItemDefinition version");
172 type = (enum ItemType)readU8(is);
173 name = deSerializeString(is);
174 description = deSerializeString(is);
175 inventory_image = deSerializeString(is);
176 wield_image = deSerializeString(is);
177 wield_scale = readV3F1000(is);
178 stack_max = readS16(is);
180 liquids_pointable = readU8(is);
181 std::string tool_capabilities_s = deSerializeString(is);
182 if(!tool_capabilities_s.empty())
184 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
185 tool_capabilities = new ToolCapabilities;
186 tool_capabilities->deSerialize(tmp_is);
189 u32 groups_size = readU16(is);
190 for(u32 i=0; i<groups_size; i++){
191 std::string name = deSerializeString(is);
192 int value = readS16(is);
193 groups[name] = value;
196 // We cant be sure that node_placement_prediction is send in version 1
198 node_placement_prediction = deSerializeString(is);
199 }catch(SerializationError &e) {};
200 // Set the old default sound
201 sound_place.name = "default_place_node";
202 sound_place.gain = 0.5;
203 } else if(version >= 2) {
204 node_placement_prediction = deSerializeString(is);
205 //deserializeSimpleSoundSpec(sound_place, is);
206 sound_place.name = deSerializeString(is);
207 sound_place.gain = readF1000(is);
210 range = readF1000(is);
212 // If you add anything here, insert it primarily inside the try-catch
213 // block to not need to increase the version.
215 sound_place_failed.name = deSerializeString(is);
216 sound_place_failed.gain = readF1000(is);
217 palette_image = deSerializeString(is);
218 color.set(readU32(is));
219 sound_place.pitch = readF1000(is);
220 sound_place_failed.pitch = readF1000(is);
221 } catch(SerializationError &e) {};
228 // SUGG: Support chains of aliases?
230 class CItemDefManager: public IWritableItemDefManager
235 video::ITexture *inventory_texture;
240 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(std::vector<ClientCached*>::const_iterator
261 i = values.begin(); i != values.end(); ++i)
263 ClientCached *cc = *i;
264 if (cc->wield_mesh.mesh)
265 cc->wield_mesh.mesh->drop();
270 for (std::map<std::string, ItemDefinition*>::iterator iter =
271 m_item_definitions.begin(); iter != m_item_definitions.end();
275 m_item_definitions.clear();
277 virtual const ItemDefinition& get(const std::string &name_) const
279 // Convert name according to possible alias
280 std::string name = getAlias(name_);
281 // Get the definition
282 std::map<std::string, ItemDefinition*>::const_iterator i;
283 i = m_item_definitions.find(name);
284 if(i == m_item_definitions.end())
285 i = m_item_definitions.find("unknown");
286 assert(i != m_item_definitions.end());
289 virtual const std::string &getAlias(const std::string &name) const
291 StringMap::const_iterator it = m_aliases.find(name);
292 if (it != m_aliases.end())
296 virtual void getAll(std::set<std::string> &result) const
299 for(std::map<std::string, ItemDefinition *>::const_iterator
300 it = m_item_definitions.begin();
301 it != m_item_definitions.end(); ++it) {
302 result.insert(it->first);
304 for (StringMap::const_iterator
305 it = m_aliases.begin();
306 it != m_aliases.end(); ++it) {
307 result.insert(it->first);
310 virtual bool isKnown(const std::string &name_) const
312 // Convert name according to possible alias
313 std::string name = getAlias(name_);
314 // Get the definition
315 std::map<std::string, ItemDefinition*>::const_iterator i;
316 return m_item_definitions.find(name) != m_item_definitions.end();
320 ClientCached* createClientCachedDirect(const std::string &name,
321 Client *client) const
323 infostream<<"Lazily creating item texture and mesh for \""
324 <<name<<"\""<<std::endl;
326 // This is not thread-safe
327 sanity_check(std::this_thread::get_id() == m_main_thread);
329 // Skip if already in cache
330 ClientCached *cc = NULL;
331 m_clientcached.get(name, &cc);
335 ITextureSource *tsrc = client->getTextureSource();
336 const ItemDefinition &def = get(name);
338 // Create new ClientCached
339 cc = new ClientCached();
341 // Create an inventory texture
342 cc->inventory_texture = NULL;
343 if(def.inventory_image != "")
344 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
346 ItemStack item = ItemStack();
347 item.name = def.name;
349 getItemMesh(client, item, &(cc->wield_mesh));
351 cc->palette = tsrc->getPalette(def.palette_image);
354 m_clientcached.set(name, cc);
358 ClientCached* getClientCached(const std::string &name,
359 Client *client) const
361 ClientCached *cc = NULL;
362 m_clientcached.get(name, &cc);
366 if (std::this_thread::get_id() == m_main_thread) {
367 return createClientCachedDirect(name, client);
369 // We're gonna ask the result to be put into here
370 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
372 // Throw a request in
373 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
376 // Wait result for a second
377 GetResult<std::string, ClientCached*, u8, u8>
378 result = result_queue.pop_front(1000);
380 if (result.key == name) {
385 catch(ItemNotFoundException &e)
387 errorstream<<"Waiting for clientcached " << name << " timed out."<<std::endl;
388 return &m_dummy_clientcached;
392 // Get item inventory texture
393 virtual video::ITexture* getInventoryTexture(const std::string &name,
394 Client *client) const
396 ClientCached *cc = getClientCached(name, client);
399 return cc->inventory_texture;
401 // Get item wield mesh
402 virtual ItemMesh* getWieldMesh(const std::string &name,
403 Client *client) const
405 ClientCached *cc = getClientCached(name, client);
408 return &(cc->wield_mesh);
412 virtual Palette* getPalette(const std::string &name,
413 Client *client) const
415 ClientCached *cc = getClientCached(name, client);
421 virtual video::SColor getItemstackColor(const ItemStack &stack,
422 Client *client) const
424 // Look for direct color definition
425 const std::string &colorstring = stack.metadata.getString("color", 0);
426 video::SColor directcolor;
427 if ((colorstring != "")
428 && parseColorString(colorstring, directcolor, true))
430 // See if there is a palette
431 Palette *palette = getPalette(stack.name, client);
432 const std::string &index = stack.metadata.getString("palette_index", 0);
433 if ((palette != NULL) && (index != ""))
434 return (*palette)[mystoi(index, 0, 255)];
436 return get(stack.name).color;
441 for(std::map<std::string, ItemDefinition*>::const_iterator
442 i = m_item_definitions.begin();
443 i != m_item_definitions.end(); ++i)
447 m_item_definitions.clear();
450 // Add the four builtin items:
452 // "unknown" is returned whenever an undefined item
453 // is accessed (is also the unknown node)
454 // "air" is the air node
455 // "ignore" is the ignore node
457 ItemDefinition* hand_def = new ItemDefinition;
459 hand_def->wield_image = "wieldhand.png";
460 hand_def->tool_capabilities = new ToolCapabilities;
461 m_item_definitions.insert(std::make_pair("", hand_def));
463 ItemDefinition* unknown_def = new ItemDefinition;
464 unknown_def->type = ITEM_NODE;
465 unknown_def->name = "unknown";
466 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
468 ItemDefinition* air_def = new ItemDefinition;
469 air_def->type = ITEM_NODE;
470 air_def->name = "air";
471 m_item_definitions.insert(std::make_pair("air", air_def));
473 ItemDefinition* ignore_def = new ItemDefinition;
474 ignore_def->type = ITEM_NODE;
475 ignore_def->name = "ignore";
476 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
478 virtual void registerItem(const ItemDefinition &def)
480 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
481 // Ensure that the "" item (the hand) always has ToolCapabilities
483 FATAL_ERROR_IF(!def.tool_capabilities, "Hand does not have ToolCapabilities");
485 if(m_item_definitions.count(def.name) == 0)
486 m_item_definitions[def.name] = new ItemDefinition(def);
488 *(m_item_definitions[def.name]) = def;
490 // Remove conflicting alias if it exists
491 bool alias_removed = (m_aliases.erase(def.name) != 0);
493 infostream<<"ItemDefManager: erased alias "<<def.name
494 <<" because item was defined"<<std::endl;
496 virtual void unregisterItem(const std::string &name)
498 verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
500 delete m_item_definitions[name];
501 m_item_definitions.erase(name);
503 virtual void registerAlias(const std::string &name,
504 const std::string &convert_to)
506 if (m_item_definitions.find(name) == m_item_definitions.end()) {
507 verbosestream<<"ItemDefManager: setting alias "<<name
508 <<" -> "<<convert_to<<std::endl;
509 m_aliases[name] = convert_to;
512 void serialize(std::ostream &os, u16 protocol_version)
514 writeU8(os, 0); // version
515 u16 count = m_item_definitions.size();
518 for (std::map<std::string, ItemDefinition *>::const_iterator
519 it = m_item_definitions.begin();
520 it != m_item_definitions.end(); ++it) {
521 ItemDefinition *def = it->second;
522 // Serialize ItemDefinition and write wrapped in a string
523 std::ostringstream tmp_os(std::ios::binary);
524 def->serialize(tmp_os, protocol_version);
525 os << serializeString(tmp_os.str());
528 writeU16(os, m_aliases.size());
530 for (StringMap::const_iterator
531 it = m_aliases.begin();
532 it != m_aliases.end(); ++it) {
533 os << serializeString(it->first);
534 os << serializeString(it->second);
537 void deSerialize(std::istream &is)
542 int version = readU8(is);
544 throw SerializationError("unsupported ItemDefManager version");
545 u16 count = readU16(is);
546 for(u16 i=0; i<count; i++)
548 // Deserialize a string and grab an ItemDefinition from it
549 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
551 def.deSerialize(tmp_is);
555 u16 num_aliases = readU16(is);
556 for(u16 i=0; i<num_aliases; i++)
558 std::string name = deSerializeString(is);
559 std::string convert_to = deSerializeString(is);
560 registerAlias(name, convert_to);
563 void processQueue(IGameDef *gamedef)
566 //NOTE this is only thread safe for ONE consumer thread!
567 while(!m_get_clientcached_queue.empty())
569 GetRequest<std::string, ClientCached*, u8, u8>
570 request = m_get_clientcached_queue.pop();
572 m_get_clientcached_queue.pushResult(request,
573 createClientCachedDirect(request.key, (Client *)gamedef));
579 std::map<std::string, ItemDefinition*> m_item_definitions;
583 // The id of the thread that is allowed to use irrlicht directly
584 std::thread::id m_main_thread;
585 // A reference to this can be returned when nothing is found, to avoid NULLs
586 mutable ClientCached m_dummy_clientcached;
587 // Cached textures and meshes
588 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
589 // Queued clientcached fetches (to be processed by the main thread)
590 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
594 IWritableItemDefManager* createItemDefManager()
596 return new CItemDefManager();