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
69 std::string meshname; // name of internal mesh (or meshfile to use TBD)
70 std::string meshtexture; // meshtexture
73 Item stack and interaction properties
77 bool liquids_pointable;
78 // May be NULL. If non-NULL, deleted by destructor
79 ToolCapabilities *tool_capabilities;
81 SimpleSoundSpec sound_place;
82 SimpleSoundSpec sound_place_failed;
85 // Client shall immediately place this node when player places the item.
86 // Server will update the precise end result a moment later.
88 std::string node_placement_prediction;
94 ItemDefinition(const ItemDefinition &def);
95 ItemDefinition& operator=(const ItemDefinition &def);
98 void serialize(std::ostream &os, u16 protocol_version) const;
99 void deSerialize(std::istream &is);
104 class IItemDefManager
108 virtual ~IItemDefManager(){}
110 // Get item definition
111 virtual const ItemDefinition& get(const std::string &name) const=0;
112 // Get alias definition
113 virtual std::string getAlias(const std::string &name) const=0;
114 // Get set of all defined item names and aliases
115 virtual std::set<std::string> getAll() const=0;
116 // Check if item is known
117 virtual bool isKnown(const std::string &name) const=0;
119 // Get item inventory texture
120 virtual video::ITexture* getInventoryTexture(const std::string &name,
121 IGameDef *gamedef) const=0;
122 // Get item wield mesh
123 virtual scene::IMesh* getWieldMesh(const std::string &name,
124 IGameDef *gamedef) const=0;
127 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
130 class IWritableItemDefManager : public IItemDefManager
133 IWritableItemDefManager(){}
134 virtual ~IWritableItemDefManager(){}
136 // Get item definition
137 virtual const ItemDefinition& get(const std::string &name) const=0;
138 // Get alias definition
139 virtual std::string getAlias(const std::string &name) const=0;
140 // Get set of all defined item names and aliases
141 virtual std::set<std::string> getAll() const=0;
142 // Check if item is known
143 virtual bool isKnown(const std::string &name) const=0;
145 // Get item inventory texture
146 virtual video::ITexture* getInventoryTexture(const std::string &name,
147 IGameDef *gamedef) const=0;
148 // Get item wield mesh
149 virtual scene::IMesh* getWieldMesh(const std::string &name,
150 IGameDef *gamedef) const=0;
153 // Remove all registered item and node definitions and aliases
154 // Then re-add the builtin item definitions
155 virtual void clear()=0;
156 // Register item definition
157 virtual void registerItem(const ItemDefinition &def)=0;
158 // Set an alias so that items named <name> will load as <convert_to>.
159 // Alias is not set if <name> has already been defined.
160 // Alias will be removed if <name> is defined at a later point of time.
161 virtual void registerAlias(const std::string &name,
162 const std::string &convert_to)=0;
164 virtual void serialize(std::ostream &os, u16 protocol_version)=0;
165 virtual void deSerialize(std::istream &is)=0;
167 // Do stuff asked by threads that can only be done in the main thread
168 virtual void processQueue(IGameDef *gamedef)=0;
172 class CItemDefManager: public IWritableItemDefManager
176 virtual ~CItemDefManager();
177 virtual const ItemDefinition& get(const std::string &name_) const;
178 virtual std::string getAlias(const std::string &name) const;
179 virtual std::set<std::string> getAll() const;
180 virtual bool isKnown(const std::string &name_) const;
183 // Get item inventory texture
184 virtual video::ITexture* getInventoryTexture(const std::string &name,
185 IGameDef *gamedef) const;
187 // Get item wield mesh
188 virtual scene::IMesh* getWieldMesh(const std::string &name,
189 IGameDef *gamedef) const;
193 virtual void registerItem(const ItemDefinition &def);
194 virtual void registerAlias(const std::string &name,
195 const std::string &convert_to);
196 void serialize(std::ostream &os, u16 protocol_version);
197 void deSerialize(std::istream &is);
199 void processQueue(IGameDef *gamedef);
206 video::ITexture *inventory_texture;
207 scene::IMesh *wield_mesh;
212 void createNodeItemTexture(const std::string& name,
213 const ItemDefinition& def, INodeDefManager* nodedef,
214 ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
216 void createMeshItemTexture(const std::string& name,
217 const ItemDefinition& def, INodeDefManager* nodedef,
218 ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
220 void renderMeshToTexture(const ItemDefinition& def, scene::IMesh* mesh,
221 ClientCached* cc, ITextureSource* tsrc) const;
223 ClientCached* createClientCachedDirect(const std::string &name,
224 IGameDef *gamedef) const;
226 ClientCached* getClientCached(const std::string &name,
227 IGameDef *gamedef) const;
229 // The id of the thread that is allowed to use irrlicht directly
230 threadid_t m_main_thread;
232 // A reference to this can be returned when nothing is found, to avoid NULLs
233 mutable ClientCached m_dummy_clientcached;
235 // Cached textures and meshes
236 mutable MutexedMap<std::string, ClientCached*> m_clientcached;
238 // Queued clientcached fetches (to be processed by the main thread)
239 mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
243 std::map<std::string, ItemDefinition*> m_item_definitions;
246 std::map<std::string, std::string> m_aliases;
249 IWritableItemDefManager* createItemDefManager();