Switch the license to be LGPLv2/later, with small parts still remaining as GPLv2...
[oweals/minetest.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 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 "common_irrlicht.h"
25 #include <string>
26 #include <iostream>
27 #include <set>
28 #include "itemgroup.h"
29 class IGameDef;
30 struct ToolCapabilities;
31
32 /*
33         Base item definition
34 */
35
36 enum ItemType
37 {
38         ITEM_NONE,
39         ITEM_NODE,
40         ITEM_CRAFT,
41         ITEM_TOOL,
42 };
43
44 struct ItemDefinition
45 {
46         /*
47                 Basic item properties
48         */
49         ItemType type;
50         std::string name; // "" = hand
51         std::string description; // Shown in tooltip.
52
53         /*
54                 Visual properties
55         */
56         std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
57         std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
58         v3f wield_scale;
59
60         /*
61                 Item stack and interaction properties
62         */
63         s16 stack_max;
64         bool usable;
65         bool liquids_pointable;
66         // May be NULL. If non-NULL, deleted by destructor
67         ToolCapabilities *tool_capabilities;
68         ItemGroupList groups;
69
70         /*
71                 Cached stuff
72         */
73 #ifndef SERVER
74         video::ITexture *inventory_texture;
75         scene::IMesh *wield_mesh;
76 #endif
77
78         /*
79                 Some helpful methods
80         */
81         ItemDefinition();
82         ItemDefinition(const ItemDefinition &def);
83         ItemDefinition& operator=(const ItemDefinition &def);
84         ~ItemDefinition();
85         void reset();
86         void serialize(std::ostream &os) const;
87         void deSerialize(std::istream &is);
88 private:
89         void resetInitial();
90 };
91
92 class IItemDefManager
93 {
94 public:
95         IItemDefManager(){}
96         virtual ~IItemDefManager(){}
97
98         // Get item definition
99         virtual const ItemDefinition& get(const std::string &name) const=0;
100         // Get alias definition
101         virtual std::string getAlias(const std::string &name) const=0;
102         // Get set of all defined item names and aliases
103         virtual std::set<std::string> getAll() const=0;
104         // Check if item is known
105         virtual bool isKnown(const std::string &name) const=0;
106
107         virtual void serialize(std::ostream &os)=0;
108 };
109
110 class IWritableItemDefManager : public IItemDefManager
111 {
112 public:
113         IWritableItemDefManager(){}
114         virtual ~IWritableItemDefManager(){}
115
116         // Get item definition
117         virtual const ItemDefinition& get(const std::string &name) const=0;
118         // Get alias definition
119         virtual std::string getAlias(const std::string &name) const=0;
120         // Get set of all defined item names and aliases
121         virtual std::set<std::string> getAll() const=0;
122         // Check if item is known
123         virtual bool isKnown(const std::string &name) const=0;
124
125         // Remove all registered item and node definitions and aliases
126         // Then re-add the builtin item definitions
127         virtual void clear()=0;
128         // Register item definition
129         virtual void registerItem(const ItemDefinition &def)=0;
130         // Set an alias so that items named <name> will load as <convert_to>.
131         // Alias is not set if <name> has already been defined.
132         // Alias will be removed if <name> is defined at a later point of time.
133         virtual void registerAlias(const std::string &name,
134                         const std::string &convert_to)=0;
135
136         /*
137                 Update inventory textures and wield meshes to latest
138                 return values of ITextureSource and INodeDefManager.
139                 Call after updating the texture atlas of a texture source.
140         */
141         virtual void updateTexturesAndMeshes(IGameDef *gamedef)=0;
142
143         virtual void serialize(std::ostream &os)=0;
144         virtual void deSerialize(std::istream &is)=0;
145 };
146
147 IWritableItemDefManager* createItemDefManager();
148
149 #endif