]> git.lizzy.rs Git - dragonfireclient.git/blob - src/itemdef.h
15734ff44450c8dab517edfdd8c23fd9827094d1
[dragonfireclient.git] / src / itemdef.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2011 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 General Public License as published by
8 the Free Software Foundation; either version 2 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 General Public License for more details.
15
16 You should have received a copy of the GNU 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 "common_irrlicht.h"
25 #include <string>
26 #include <iostream>
27 #include <set>
28 #include <map>
29 class IGameDef;
30 struct ToolCapabilities;
31
32 /*
33         Some helpers
34 */
35
36 static inline int itemgroup_get(const std::map<std::string, int> &groups,
37                 const std::string &name)
38 {
39         std::map<std::string, int>::const_iterator i = groups.find(name);
40         if(i == groups.end())
41                 return 0;
42         return i->second;
43 }
44
45 /*
46         Base item definition
47 */
48
49 enum ItemType
50 {
51         ITEM_NONE,
52         ITEM_NODE,
53         ITEM_CRAFT,
54         ITEM_TOOL,
55 };
56
57 struct ItemDefinition
58 {
59         /*
60                 Basic item properties
61         */
62         ItemType type;
63         std::string name; // "" = hand
64         std::string description; // Shown in tooltip.
65
66         /*
67                 Visual properties
68         */
69         std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
70         std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
71         v3f wield_scale;
72
73         /*
74                 Item stack and interaction properties
75         */
76         s16 stack_max;
77         bool usable;
78         bool liquids_pointable;
79         // May be NULL. If non-NULL, deleted by destructor
80         ToolCapabilities *tool_capabilities;
81         std::map<std::string, int> groups;
82
83         /*
84                 Cached stuff
85         */
86 #ifndef SERVER
87         video::ITexture *inventory_texture;
88         scene::IMesh *wield_mesh;
89 #endif
90
91         /*
92                 Some helpful methods
93         */
94         ItemDefinition();
95         ItemDefinition(const ItemDefinition &def);
96         ItemDefinition& operator=(const ItemDefinition &def);
97         ~ItemDefinition();
98         void reset();
99         void serialize(std::ostream &os) const;
100         void deSerialize(std::istream &is);
101 private:
102         void resetInitial();
103 };
104
105 class IItemDefManager
106 {
107 public:
108         IItemDefManager(){}
109         virtual ~IItemDefManager(){}
110
111         // Get item definition
112         virtual const ItemDefinition& get(const std::string &name) const=0;
113         // Get alias definition
114         virtual std::string getAlias(const std::string &name) const=0;
115         // Get set of all defined item names and aliases
116         virtual std::set<std::string> getAll() const=0;
117         // Check if item is known
118         virtual bool isKnown(const std::string &name) const=0;
119
120         virtual void serialize(std::ostream &os)=0;
121 };
122
123 class IWritableItemDefManager : public IItemDefManager
124 {
125 public:
126         IWritableItemDefManager(){}
127         virtual ~IWritableItemDefManager(){}
128
129         // Get item definition
130         virtual const ItemDefinition& get(const std::string &name) const=0;
131         // Get alias definition
132         virtual std::string getAlias(const std::string &name) const=0;
133         // Get set of all defined item names and aliases
134         virtual std::set<std::string> getAll() const=0;
135         // Check if item is known
136         virtual bool isKnown(const std::string &name) const=0;
137
138         // Remove all registered item and node definitions and aliases
139         // Then re-add the builtin item definitions
140         virtual void clear()=0;
141         // Register item definition
142         virtual void registerItem(const ItemDefinition &def)=0;
143         // Set an alias so that items named <name> will load as <convert_to>.
144         // Alias is not set if <name> has already been defined.
145         // Alias will be removed if <name> is defined at a later point of time.
146         virtual void registerAlias(const std::string &name,
147                         const std::string &convert_to)=0;
148
149         /*
150                 Update inventory textures and wield meshes to latest
151                 return values of ITextureSource and INodeDefManager.
152                 Call after updating the texture atlas of a texture source.
153         */
154         virtual void updateTexturesAndMeshes(IGameDef *gamedef)=0;
155
156         virtual void serialize(std::ostream &os)=0;
157         virtual void deSerialize(std::istream &is)=0;
158 };
159
160 IWritableItemDefManager* createItemDefManager();
161
162 #endif