]> git.lizzy.rs Git - dragonfireclient.git/blob - src/itemdef.h
Fix some reference counters (memleak) (#8981)
[dragonfireclient.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 #pragma once
22
23 #include "irrlichttypes_extrabloated.h"
24 #include <string>
25 #include <iostream>
26 #include <set>
27 #include "itemgroup.h"
28 #include "sound.h"
29 class IGameDef;
30 class Client;
31 struct ToolCapabilities;
32 #ifndef SERVER
33 #include "client/tile.h"
34 struct ItemMesh;
35 struct ItemStack;
36 #endif
37
38 /*
39         Base item definition
40 */
41
42 enum ItemType
43 {
44         ITEM_NONE,
45         ITEM_NODE,
46         ITEM_CRAFT,
47         ITEM_TOOL,
48 };
49
50 struct ItemDefinition
51 {
52         /*
53                 Basic item properties
54         */
55         ItemType type;
56         std::string name; // "" = hand
57         std::string description; // Shown in tooltip.
58
59         /*
60                 Visual properties
61         */
62         std::string inventory_image; // Optional for nodes, mandatory for tools/craftitems
63         std::string inventory_overlay; // Overlay of inventory_image.
64         std::string wield_image; // If empty, inventory_image or mesh (only nodes) is used
65         std::string wield_overlay; // Overlay of wield_image.
66         std::string palette_image; // If specified, the item will be colorized based on this
67         video::SColor color; // The fallback color of the node.
68         v3f wield_scale;
69
70         /*
71                 Item stack and interaction properties
72         */
73         u16 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() = default;
106
107         virtual ~IItemDefManager() = default;
108
109         // Get item definition
110         virtual const ItemDefinition& get(const std::string &name) const=0;
111         // Get alias definition
112         virtual const std::string &getAlias(const std::string &name) const=0;
113         // Get set of all defined item names and aliases
114         virtual void getAll(std::set<std::string> &result) const=0;
115         // Check if item is known
116         virtual bool isKnown(const std::string &name) const=0;
117 #ifndef SERVER
118         // Get item inventory texture
119         virtual video::ITexture* getInventoryTexture(const std::string &name,
120                         Client *client) const=0;
121         // Get item wield mesh
122         virtual ItemMesh* getWieldMesh(const std::string &name,
123                 Client *client) const=0;
124         // Get item palette
125         virtual Palette* getPalette(const std::string &name,
126                 Client *client) const = 0;
127         // Returns the base color of an item stack: the color of all
128         // tiles that do not define their own color.
129         virtual video::SColor getItemstackColor(const ItemStack &stack,
130                 Client *client) const = 0;
131 #endif
132
133         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
134 };
135
136 class IWritableItemDefManager : public IItemDefManager
137 {
138 public:
139         IWritableItemDefManager() = default;
140
141         virtual ~IWritableItemDefManager() = default;
142
143         // Get item definition
144         virtual const ItemDefinition& get(const std::string &name) const=0;
145         // Get alias definition
146         virtual const std::string &getAlias(const std::string &name) const=0;
147         // Get set of all defined item names and aliases
148         virtual void getAll(std::set<std::string> &result) const=0;
149         // Check if item is known
150         virtual bool isKnown(const std::string &name) const=0;
151 #ifndef SERVER
152         // Get item inventory texture
153         virtual video::ITexture* getInventoryTexture(const std::string &name,
154                         Client *client) const=0;
155         // Get item wield mesh
156         virtual ItemMesh* getWieldMesh(const std::string &name,
157                 Client *client) const=0;
158 #endif
159
160         // Remove all registered item and node definitions and aliases
161         // Then re-add the builtin item definitions
162         virtual void clear()=0;
163         // Register item definition
164         virtual void registerItem(const ItemDefinition &def)=0;
165         virtual void unregisterItem(const std::string &name)=0;
166         // Set an alias so that items named <name> will load as <convert_to>.
167         // Alias is not set if <name> has already been defined.
168         // Alias will be removed if <name> is defined at a later point of time.
169         virtual void registerAlias(const std::string &name,
170                         const std::string &convert_to)=0;
171
172         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
173         virtual void deSerialize(std::istream &is)=0;
174
175         // Do stuff asked by threads that can only be done in the main thread
176         virtual void processQueue(IGameDef *gamedef)=0;
177 };
178
179 IWritableItemDefManager* createItemDefManager();