]> git.lizzy.rs Git - minetest.git/blob - src/itemdef.h
Make collisionMoveSimple time overflow message written to log/show up at max once...
[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         std::string meshname;    // name of internal mesh (or meshfile to use TBD)
70         std::string meshtexture; // meshtexture
71
72         /*
73                 Item stack and interaction properties
74         */
75         s16 stack_max;
76         bool usable;
77         bool liquids_pointable;
78         // May be NULL. If non-NULL, deleted by destructor
79         ToolCapabilities *tool_capabilities;
80         ItemGroupList groups;
81         SimpleSoundSpec sound_place;
82         SimpleSoundSpec sound_place_failed;
83         f32 range;
84
85         // Client shall immediately place this node when player places the item.
86         // Server will update the precise end result a moment later.
87         // "" = no prediction
88         std::string node_placement_prediction;
89
90         /*
91                 Some helpful methods
92         */
93         ItemDefinition();
94         ItemDefinition(const ItemDefinition &def);
95         ItemDefinition& operator=(const ItemDefinition &def);
96         ~ItemDefinition();
97         void reset();
98         void serialize(std::ostream &os, u16 protocol_version) const;
99         void deSerialize(std::istream &is);
100 private:
101         void resetInitial();
102 };
103
104 class IItemDefManager
105 {
106 public:
107         IItemDefManager(){}
108         virtual ~IItemDefManager(){}
109
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;
118 #ifndef SERVER
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;
125 #endif
126
127         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
128 };
129
130 class IWritableItemDefManager : public IItemDefManager
131 {
132 public:
133         IWritableItemDefManager(){}
134         virtual ~IWritableItemDefManager(){}
135
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;
144 #ifndef SERVER
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;
151 #endif
152
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;
163
164         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
165         virtual void deSerialize(std::istream &is)=0;
166
167         // Do stuff asked by threads that can only be done in the main thread
168         virtual void processQueue(IGameDef *gamedef)=0;
169 };
170
171
172 class CItemDefManager: public IWritableItemDefManager
173 {
174 public:
175         CItemDefManager();
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;
181
182 #ifndef SERVER
183         // Get item inventory texture
184         virtual video::ITexture* getInventoryTexture(const std::string &name,
185                         IGameDef *gamedef) const;
186
187         // Get item wield mesh
188         virtual scene::IMesh* getWieldMesh(const std::string &name,
189                         IGameDef *gamedef) const;
190 #endif
191         void clear();
192
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);
198
199         void processQueue(IGameDef *gamedef);
200
201 private:
202
203 #ifndef SERVER
204         struct ClientCached
205         {
206                 video::ITexture *inventory_texture;
207                 scene::IMesh *wield_mesh;
208
209                 ClientCached();
210         };
211
212         void createNodeItemTexture(const std::string& name,
213                         const ItemDefinition& def, INodeDefManager* nodedef,
214                         ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
215
216         void createMeshItemTexture(const std::string& name,
217                         const ItemDefinition& def, INodeDefManager* nodedef,
218                         ClientCached* cc, IGameDef* gamedef, ITextureSource* tsrc) const;
219
220         void renderMeshToTexture(const ItemDefinition& def, scene::IMesh* mesh,
221                         ClientCached* cc, ITextureSource* tsrc) const;
222
223         ClientCached* createClientCachedDirect(const std::string &name,
224                         IGameDef *gamedef) const;
225
226         ClientCached* getClientCached(const std::string &name,
227                         IGameDef *gamedef) const;
228
229         // The id of the thread that is allowed to use irrlicht directly
230         threadid_t m_main_thread;
231
232         // A reference to this can be returned when nothing is found, to avoid NULLs
233         mutable ClientCached m_dummy_clientcached;
234
235         // Cached textures and meshes
236         mutable MutexedMap<std::string, ClientCached*> m_clientcached;
237
238         // Queued clientcached fetches (to be processed by the main thread)
239         mutable RequestQueue<std::string, ClientCached*, u8, u8> m_get_clientcached_queue;
240 #endif
241
242         // Key is name
243         std::map<std::string, ItemDefinition*> m_item_definitions;
244
245         // Aliases
246         std::map<std::string, std::string> m_aliases;
247 };
248
249 IWritableItemDefManager* createItemDefManager();
250
251 #endif