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.
21 #ifndef ITEMDEF_HEADER
22 #define ITEMDEF_HEADER
24 #include "irrlichttypes_extrabloated.h"
29 #include "itemgroup.h"
31 #include "util/container.h"
32 #include "util/thread.h"
35 #include "client/tile.h"
39 class INodeDefManager;
40 struct ToolCapabilities;
60 std::string name; // "" = hand
61 std::string description; // Shown in tooltip.
66 std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
67 std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
71 Item stack and interaction properties
75 bool liquids_pointable;
76 // May be NULL. If non-NULL, deleted by destructor
77 ToolCapabilities *tool_capabilities;
79 SimpleSoundSpec sound_place;
80 SimpleSoundSpec sound_place_failed;
83 // Client shall immediately place this node when player places the item.
84 // Server will update the precise end result a moment later.
86 std::string node_placement_prediction;
92 ItemDefinition(const ItemDefinition &def);
93 ItemDefinition& operator=(const ItemDefinition &def);
96 void serialize(std::ostream &os, u16 protocol_version) const;
97 void deSerialize(std::istream &is);
102 class IItemDefManager
106 virtual ~IItemDefManager(){}
108 // Get item definition
109 virtual const ItemDefinition& get(const std::string &name) const=0;
110 // Get alias definition
111 virtual std::string getAlias(const std::string &name) const=0;
112 // Get set of all defined item names and aliases
113 virtual std::set<std::string> getAll() const=0;
114 // Check if item is known
115 virtual bool isKnown(const std::string &name) const=0;
117 // Get item inventory texture
118 virtual video::ITexture* getInventoryTexture(const std::string &name,
119 IGameDef *gamedef) const=0;
120 // Get item wield mesh
121 virtual scene::IMesh* getWieldMesh(const std::string &name,
122 IGameDef *gamedef) const=0;
125 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
128 class IWritableItemDefManager : public IItemDefManager
131 IWritableItemDefManager(){}
132 virtual ~IWritableItemDefManager(){}
134 // Get item definition
135 virtual const ItemDefinition& get(const std::string &name) const=0;
136 // Get alias definition
137 virtual std::string getAlias(const std::string &name) const=0;
138 // Get set of all defined item names and aliases
139 virtual std::set<std::string> getAll() const=0;
140 // Check if item is known
141 virtual bool isKnown(const std::string &name) const=0;
143 // Get item inventory texture
144 virtual video::ITexture* getInventoryTexture(const std::string &name,
145 IGameDef *gamedef) const=0;
146 // Get item wield mesh
147 virtual scene::IMesh* getWieldMesh(const std::string &name,
148 IGameDef *gamedef) const=0;
151 // Remove all registered item and node definitions and aliases
152 // Then re-add the builtin item definitions
153 virtual void clear()=0;
154 // Register item definition
155 virtual void registerItem(const ItemDefinition &def)=0;
156 // Set an alias so that items named <name> will load as <convert_to>.
157 // Alias is not set if <name> has already been defined.
158 // Alias will be removed if <name> is defined at a later point of time.
159 virtual void registerAlias(const std::string &name,
160 const std::string &convert_to)=0;
162 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
163 virtual void deSerialize(std::istream &is)=0;
165 // Do stuff asked by threads that can only be done in the main thread
166 virtual void processQueue(IGameDef *gamedef)=0;
170 class CItemDefManager: public IWritableItemDefManager
174 virtual ~CItemDefManager();
175 virtual const ItemDefinition& get(const std::string &name_) const;
176 virtual std::string getAlias(const std::string &name) const;
177 virtual std::set<std::string> getAll() const;
178 virtual bool isKnown(const std::string &name_) const;
181 // Get item inventory texture
182 virtual video::ITexture* getInventoryTexture(const std::string &name,
183 IGameDef *gamedef) const;
185 // Get item wield mesh
186 virtual scene::IMesh* getWieldMesh(const std::string &name,
187 IGameDef *gamedef) const;
191 virtual void registerItem(const ItemDefinition &def);
192 virtual void registerAlias(const std::string &name,
193 const std::string &convert_to);
194 void serialize(std::ostream &os, u16 protocol_version);
195 void deSerialize(std::istream &is);
197 void processQueue(IGameDef *gamedef);
204 video::ITexture *inventory_texture;
205 scene::IMesh *wield_mesh;
210 void createNodeItemTexture(const std::string& name,
211 const ItemDefinition& def, INodeDefManager* nodedef,
212 ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
214 ClientCached* createClientCachedDirect(const std::string &name,
215 IGameDef *gamedef) const;
217 ClientCached* getClientCached(const std::string &name,
218 IGameDef *gamedef) const;
220 // The id of the thread that is allowed to use irrlicht directly
221 threadid_t m_main_thread;
223 // A reference to this can be returned when nothing is found, to avoid NULLs
224 mutable ClientCached m_dummy_clientcached;
226 // Cached textures and meshes
227 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
229 // Queued clientcached fetches (to be processed by the main thread)
230 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
234 std::map<std::string, ItemDefinition*> m_item_definitions;
237 std::map<std::string, std::string> m_aliases;
240 IWritableItemDefManager* createItemDefManager();