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;
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 = "";
107 wield_scale = v3f(1.0, 1.0, 1.0);
110 liquids_pointable = false;
111 if(tool_capabilities)
113 delete tool_capabilities;
114 tool_capabilities = NULL;
117 sound_place = SimpleSoundSpec();
118 sound_place_failed = SimpleSoundSpec();
121 node_placement_prediction = "";
124 void ItemDefinition::serialize(std::ostream &os, u16 protocol_version) const
126 if(protocol_version <= 17)
127 writeU8(os, 1); // version
128 else if(protocol_version <= 20)
129 writeU8(os, 2); // version
131 writeU8(os, 3); // version
133 os<<serializeString(name);
134 os<<serializeString(description);
135 os<<serializeString(inventory_image);
136 os<<serializeString(wield_image);
137 writeV3F1000(os, wield_scale);
138 writeS16(os, stack_max);
140 writeU8(os, liquids_pointable);
141 std::string tool_capabilities_s = "";
142 if(tool_capabilities){
143 std::ostringstream tmp_os(std::ios::binary);
144 tool_capabilities->serialize(tmp_os, protocol_version);
145 tool_capabilities_s = tmp_os.str();
147 os<<serializeString(tool_capabilities_s);
148 writeU16(os, groups.size());
149 for (ItemGroupList::const_iterator
150 i = groups.begin(); i != groups.end(); ++i){
151 os << serializeString(i->first);
152 writeS16(os, i->second);
154 os<<serializeString(node_placement_prediction);
155 if(protocol_version > 17){
156 //serializeSimpleSoundSpec(sound_place, os);
157 os<<serializeString(sound_place.name);
158 writeF1000(os, sound_place.gain);
160 if (protocol_version > 20) {
161 writeF1000(os, range);
162 os << serializeString(sound_place_failed.name);
163 writeF1000(os, sound_place_failed.gain);
167 void ItemDefinition::deSerialize(std::istream &is)
173 int version = readU8(is);
174 if(version < 1 || version > 3)
175 throw SerializationError("unsupported ItemDefinition version");
176 type = (enum ItemType)readU8(is);
177 name = deSerializeString(is);
178 description = deSerializeString(is);
179 inventory_image = deSerializeString(is);
180 wield_image = deSerializeString(is);
181 wield_scale = readV3F1000(is);
182 stack_max = readS16(is);
184 liquids_pointable = readU8(is);
185 std::string tool_capabilities_s = deSerializeString(is);
186 if(!tool_capabilities_s.empty())
188 std::istringstream tmp_is(tool_capabilities_s, std::ios::binary);
189 tool_capabilities = new ToolCapabilities;
190 tool_capabilities->deSerialize(tmp_is);
193 u32 groups_size = readU16(is);
194 for(u32 i=0; i<groups_size; i++){
195 std::string name = deSerializeString(is);
196 int value = readS16(is);
197 groups[name] = value;
200 // We cant be sure that node_placement_prediction is send in version 1
202 node_placement_prediction = deSerializeString(is);
203 }catch(SerializationError &e) {};
204 // Set the old default sound
205 sound_place.name = "default_place_node";
206 sound_place.gain = 0.5;
207 } else if(version >= 2) {
208 node_placement_prediction = deSerializeString(is);
209 //deserializeSimpleSoundSpec(sound_place, is);
210 sound_place.name = deSerializeString(is);
211 sound_place.gain = readF1000(is);
214 range = readF1000(is);
216 // If you add anything here, insert it primarily inside the try-catch
217 // block to not need to increase the version.
219 sound_place_failed.name = deSerializeString(is);
220 sound_place_failed.gain = readF1000(is);
221 } catch(SerializationError &e) {};
228 // SUGG: Support chains of aliases?
230 class CItemDefManager: public IWritableItemDefManager
235 video::ITexture *inventory_texture;
236 scene::IMesh *wield_mesh;
239 inventory_texture(NULL),
250 m_main_thread = thr_get_current_thread_id();
254 virtual ~CItemDefManager()
257 const std::vector<ClientCached*> &values = m_clientcached.getValues();
258 for(std::vector<ClientCached*>::const_iterator
259 i = values.begin(); i != values.end(); ++i)
261 ClientCached *cc = *i;
263 cc->wield_mesh->drop();
268 for (std::map<std::string, ItemDefinition*>::iterator iter =
269 m_item_definitions.begin(); iter != m_item_definitions.end();
273 m_item_definitions.clear();
275 virtual const ItemDefinition& get(const std::string &name_) const
277 // Convert name according to possible alias
278 std::string name = getAlias(name_);
279 // Get the definition
280 std::map<std::string, ItemDefinition*>::const_iterator i;
281 i = m_item_definitions.find(name);
282 if(i == m_item_definitions.end())
283 i = m_item_definitions.find("unknown");
284 assert(i != m_item_definitions.end());
287 virtual std::string getAlias(const std::string &name) const
289 StringMap::const_iterator it = m_aliases.find(name);
290 if (it != m_aliases.end())
294 virtual std::set<std::string> getAll() const
296 std::set<std::string> result;
297 for(std::map<std::string, ItemDefinition *>::const_iterator
298 it = m_item_definitions.begin();
299 it != m_item_definitions.end(); ++it) {
300 result.insert(it->first);
302 for (StringMap::const_iterator
303 it = m_aliases.begin();
304 it != m_aliases.end(); ++it) {
305 result.insert(it->first);
309 virtual bool isKnown(const std::string &name_) const
311 // Convert name according to possible alias
312 std::string name = getAlias(name_);
313 // Get the definition
314 std::map<std::string, ItemDefinition*>::const_iterator i;
315 return m_item_definitions.find(name) != m_item_definitions.end();
319 ClientCached* createClientCachedDirect(const std::string &name,
320 Client *client) const
322 infostream<<"Lazily creating item texture and mesh for \""
323 <<name<<"\""<<std::endl;
325 // This is not thread-safe
326 sanity_check(thr_is_current_thread(m_main_thread));
328 // Skip if already in cache
329 ClientCached *cc = NULL;
330 m_clientcached.get(name, &cc);
334 ITextureSource *tsrc = client->getTextureSource();
335 const ItemDefinition &def = get(name);
337 // Create new ClientCached
338 cc = new ClientCached();
340 // Create an inventory texture
341 cc->inventory_texture = NULL;
342 if(def.inventory_image != "")
343 cc->inventory_texture = tsrc->getTexture(def.inventory_image);
345 ItemStack item = ItemStack();
346 item.name = def.name;
348 scene::IMesh *mesh = getItemMesh(client, item);
349 cc->wield_mesh = mesh;
352 m_clientcached.set(name, cc);
356 ClientCached* getClientCached(const std::string &name,
357 Client *client) const
359 ClientCached *cc = NULL;
360 m_clientcached.get(name, &cc);
364 if(thr_is_current_thread(m_main_thread))
366 return createClientCachedDirect(name, client);
370 // We're gonna ask the result to be put into here
371 static ResultQueue<std::string, ClientCached*, u8, u8> result_queue;
373 // Throw a request in
374 m_get_clientcached_queue.add(name, 0, 0, &result_queue);
377 // Wait result for a second
378 GetResult<std::string, ClientCached*, u8, u8>
379 result = result_queue.pop_front(1000);
381 if (result.key == name) {
386 catch(ItemNotFoundException &e)
388 errorstream<<"Waiting for clientcached " << name << " timed out."<<std::endl;
389 return &m_dummy_clientcached;
393 // Get item inventory texture
394 virtual video::ITexture* getInventoryTexture(const std::string &name,
395 Client *client) const
397 ClientCached *cc = getClientCached(name, client);
400 return cc->inventory_texture;
402 // Get item wield mesh
403 virtual scene::IMesh* getWieldMesh(const std::string &name,
404 Client *client) const
406 ClientCached *cc = getClientCached(name, client);
409 return cc->wield_mesh;
414 for(std::map<std::string, ItemDefinition*>::const_iterator
415 i = m_item_definitions.begin();
416 i != m_item_definitions.end(); ++i)
420 m_item_definitions.clear();
423 // Add the four builtin items:
425 // "unknown" is returned whenever an undefined item
426 // is accessed (is also the unknown node)
427 // "air" is the air node
428 // "ignore" is the ignore node
430 ItemDefinition* hand_def = new ItemDefinition;
432 hand_def->wield_image = "wieldhand.png";
433 hand_def->tool_capabilities = new ToolCapabilities;
434 m_item_definitions.insert(std::make_pair("", hand_def));
436 ItemDefinition* unknown_def = new ItemDefinition;
437 unknown_def->type = ITEM_NODE;
438 unknown_def->name = "unknown";
439 m_item_definitions.insert(std::make_pair("unknown", unknown_def));
441 ItemDefinition* air_def = new ItemDefinition;
442 air_def->type = ITEM_NODE;
443 air_def->name = "air";
444 m_item_definitions.insert(std::make_pair("air", air_def));
446 ItemDefinition* ignore_def = new ItemDefinition;
447 ignore_def->type = ITEM_NODE;
448 ignore_def->name = "ignore";
449 m_item_definitions.insert(std::make_pair("ignore", ignore_def));
451 virtual void registerItem(const ItemDefinition &def)
453 verbosestream<<"ItemDefManager: registering \""<<def.name<<"\""<<std::endl;
454 // Ensure that the "" item (the hand) always has ToolCapabilities
456 FATAL_ERROR_IF(!def.tool_capabilities, "Hand does not have ToolCapabilities");
458 if(m_item_definitions.count(def.name) == 0)
459 m_item_definitions[def.name] = new ItemDefinition(def);
461 *(m_item_definitions[def.name]) = def;
463 // Remove conflicting alias if it exists
464 bool alias_removed = (m_aliases.erase(def.name) != 0);
466 infostream<<"ItemDefManager: erased alias "<<def.name
467 <<" because item was defined"<<std::endl;
469 virtual void unregisterItem(const std::string &name)
471 verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
473 delete m_item_definitions[name];
474 m_item_definitions.erase(name);
476 virtual void registerAlias(const std::string &name,
477 const std::string &convert_to)
479 if (m_item_definitions.find(name) == m_item_definitions.end()) {
480 verbosestream<<"ItemDefManager: setting alias "<<name
481 <<" -> "<<convert_to<<std::endl;
482 m_aliases[name] = convert_to;
485 void serialize(std::ostream &os, u16 protocol_version)
487 writeU8(os, 0); // version
488 u16 count = m_item_definitions.size();
491 for (std::map<std::string, ItemDefinition *>::const_iterator
492 it = m_item_definitions.begin();
493 it != m_item_definitions.end(); ++it) {
494 ItemDefinition *def = it->second;
495 // Serialize ItemDefinition and write wrapped in a string
496 std::ostringstream tmp_os(std::ios::binary);
497 def->serialize(tmp_os, protocol_version);
498 os << serializeString(tmp_os.str());
501 writeU16(os, m_aliases.size());
503 for (StringMap::const_iterator
504 it = m_aliases.begin();
505 it != m_aliases.end(); ++it) {
506 os << serializeString(it->first);
507 os << serializeString(it->second);
510 void deSerialize(std::istream &is)
515 int version = readU8(is);
517 throw SerializationError("unsupported ItemDefManager version");
518 u16 count = readU16(is);
519 for(u16 i=0; i<count; i++)
521 // Deserialize a string and grab an ItemDefinition from it
522 std::istringstream tmp_is(deSerializeString(is), std::ios::binary);
524 def.deSerialize(tmp_is);
528 u16 num_aliases = readU16(is);
529 for(u16 i=0; i<num_aliases; i++)
531 std::string name = deSerializeString(is);
532 std::string convert_to = deSerializeString(is);
533 registerAlias(name, convert_to);
536 void processQueue(IGameDef *gamedef)
539 //NOTE this is only thread safe for ONE consumer thread!
540 while(!m_get_clientcached_queue.empty())
542 GetRequest<std::string, ClientCached*, u8, u8>
543 request = m_get_clientcached_queue.pop();
545 m_get_clientcached_queue.pushResult(request,
546 createClientCachedDirect(request.key, (Client *)gamedef));
552 std::map<std::string, ItemDefinition*> m_item_definitions;
556 // The id of the thread that is allowed to use irrlicht directly
557 threadid_t m_main_thread;
558 // A reference to this can be returned when nothing is found, to avoid NULLs
559 mutable ClientCached m_dummy_clientcached;
560 // Cached textures and meshes
561 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
562 // Queued clientcached fetches (to be processed by the main thread)
563 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
567 IWritableItemDefManager* createItemDefManager()
569 return new CItemDefManager();