]> git.lizzy.rs Git - minetest.git/blob - src/itemdef.h
Revert "Add support for using arbitrary meshes as items"
[minetest.git] / src / itemdef.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Kahrl <kahrl@gmx.net>
5
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.
10
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.
15
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.
19 */
20
21 #ifndef ITEMDEF_HEADER
22 #define ITEMDEF_HEADER
23
24 #include "irrlichttypes_extrabloated.h"
25 #include <string>
26 #include <iostream>
27 #include <set>
28 #include <map>
29 #include "itemgroup.h"
30 #include "sound.h"
31 #include "util/container.h"
32 #include "util/thread.h"
33
34 #ifndef SERVER
35 #include "client/tile.h"
36 #endif
37
38 class IGameDef;
39 class INodeDefManager;
40 struct ToolCapabilities;
41
42 /*
43         Base item definition
44 */
45
46 enum ItemType
47 {
48         ITEM_NONE,
49         ITEM_NODE,
50         ITEM_CRAFT,
51         ITEM_TOOL
52 };
53
54 struct ItemDefinition
55 {
56         /*
57                 Basic item properties
58         */
59         ItemType type;
60         std::string name; // "" = hand
61         std::string description; // Shown in tooltip.
62
63         /*
64                 Visual properties
65         */
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
68         v3f wield_scale;
69
70         /*
71                 Item stack and interaction properties
72         */
73         s16 stack_max;
74         bool usable;
75         bool liquids_pointable;
76         // May be NULL. If non-NULL, deleted by destructor
77         ToolCapabilities *tool_capabilities;
78         ItemGroupList groups;
79         SimpleSoundSpec sound_place;
80         SimpleSoundSpec sound_place_failed;
81         f32 range;
82
83         // Client shall immediately place this node when player places the item.
84         // Server will update the precise end result a moment later.
85         // "" = no prediction
86         std::string node_placement_prediction;
87
88         /*
89                 Some helpful methods
90         */
91         ItemDefinition();
92         ItemDefinition(const ItemDefinition &def);
93         ItemDefinition& operator=(const ItemDefinition &def);
94         ~ItemDefinition();
95         void reset();
96         void serialize(std::ostream &os, u16 protocol_version) const;
97         void deSerialize(std::istream &is);
98 private:
99         void resetInitial();
100 };
101
102 class IItemDefManager
103 {
104 public:
105         IItemDefManager(){}
106         virtual ~IItemDefManager(){}
107
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;
116 #ifndef SERVER
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;
123 #endif
124
125         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
126 };
127
128 class IWritableItemDefManager : public IItemDefManager
129 {
130 public:
131         IWritableItemDefManager(){}
132         virtual ~IWritableItemDefManager(){}
133
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;
142 #ifndef SERVER
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;
149 #endif
150
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;
161
162         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
163         virtual void deSerialize(std::istream &is)=0;
164
165         // Do stuff asked by threads that can only be done in the main thread
166         virtual void processQueue(IGameDef *gamedef)=0;
167 };
168
169
170 class CItemDefManager: public IWritableItemDefManager
171 {
172 public:
173         CItemDefManager();
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;
179
180 #ifndef SERVER
181         // Get item inventory texture
182         virtual video::ITexture* getInventoryTexture(const std::string &name,
183                         IGameDef *gamedef) const;
184
185         // Get item wield mesh
186         virtual scene::IMesh* getWieldMesh(const std::string &name,
187                         IGameDef *gamedef) const;
188 #endif
189         void clear();
190
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);
196
197         void processQueue(IGameDef *gamedef);
198
199 private:
200
201 #ifndef SERVER
202         struct ClientCached
203         {
204                 video::ITexture *inventory_texture;
205                 scene::IMesh *wield_mesh;
206
207                 ClientCached();
208         };
209
210         void createNodeItemTexture(const std::string& name,
211                         const ItemDefinition& def, INodeDefManager* nodedef,
212                         ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
213
214         ClientCached* createClientCachedDirect(const std::string &name,
215                         IGameDef *gamedef) const;
216
217         ClientCached* getClientCached(const std::string &name,
218                         IGameDef *gamedef) const;
219
220         // The id of the thread that is allowed to use irrlicht directly
221         threadid_t m_main_thread;
222
223         // A reference to this can be returned when nothing is found, to avoid NULLs
224         mutable ClientCached m_dummy_clientcached;
225
226         // Cached textures and meshes
227         mutable MutexedMap<std::string, ClientCached*> m_clientcached;
228
229         // Queued clientcached fetches (to be processed by the main thread)
230         mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
231 #endif
232
233         // Key is name
234         std::map<std::string, ItemDefinition*> m_item_definitions;
235
236         // Aliases
237         std::map<std::string, std::string> m_aliases;
238 };
239
240 IWritableItemDefManager* createItemDefManager();
241
242 #endif