]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
5196e02339f94ec4f9be8a07cfeced0d682bcf48
[minetest.git] / src / nodedef.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef NODEDEF_HEADER
21 #define NODEDEF_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include <iostream>
26 #include <map>
27 #include "mapnode.h"
28 #ifndef SERVER
29 #include "tile.h"
30 #endif
31 class IItemDefManager;
32 class ITextureSource;
33 class IGameDef;
34
35 enum ContentParamType
36 {
37         CPT_NONE,
38         CPT_LIGHT,
39 };
40
41 enum ContentParamType2
42 {
43         CPT2_NONE,
44         // Need 8-bit param2
45         CPT2_FULL,
46         // Flowing liquid properties
47         CPT2_FLOWINGLIQUID,
48         // Direction for chests and furnaces and such
49         CPT2_FACEDIR,
50         // Direction for signs, torches and such
51         CPT2_WALLMOUNTED,
52 };
53
54 enum LiquidType
55 {
56         LIQUID_NONE,
57         LIQUID_FLOWING,
58         LIQUID_SOURCE
59 };
60
61 enum NodeBoxType
62 {
63         NODEBOX_REGULAR, // Regular block; allows buildable_to
64         NODEBOX_FIXED, // Static separately defined box
65         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
66 };
67
68 struct NodeBox
69 {
70         enum NodeBoxType type;
71         // NODEBOX_REGULAR (no parameters)
72         // NODEBOX_FIXED
73         core::aabbox3d<f32> fixed;
74         // NODEBOX_WALLMOUNTED
75         core::aabbox3d<f32> wall_top;
76         core::aabbox3d<f32> wall_bottom;
77         core::aabbox3d<f32> wall_side; // being at the -X side
78
79         NodeBox():
80                 type(NODEBOX_REGULAR),
81                 // default is rail-like
82                 fixed(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
83                 // default is sign/ladder-like
84                 wall_top(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2),
85                 wall_bottom(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
86                 wall_side(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2)
87         {}
88
89         void serialize(std::ostream &os) const;
90         void deSerialize(std::istream &is);
91 };
92
93 struct MapNode;
94 class NodeMetadata;
95
96 struct MaterialSpec
97 {
98         std::string tname;
99         bool backface_culling;
100         
101         MaterialSpec(const std::string &tname_="", bool backface_culling_=true):
102                 tname(tname_),
103                 backface_culling(backface_culling_)
104         {}
105
106         void serialize(std::ostream &os) const;
107         void deSerialize(std::istream &is);
108 };
109
110 enum NodeDrawType
111 {
112         NDT_NORMAL, // A basic solid block
113         NDT_AIRLIKE, // Nothing is drawn
114         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
115         NDT_FLOWINGLIQUID, // A very special kind of thing
116         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
117         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
118         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
119         NDT_TORCHLIKE,
120         NDT_SIGNLIKE,
121         NDT_PLANTLIKE,
122         NDT_FENCELIKE,
123         NDT_RAILLIKE,
124 };
125
126 #define CF_SPECIAL_COUNT 2
127
128 struct ContentFeatures
129 {
130         /*
131                 Cached stuff
132         */
133 #ifndef SERVER
134         // 0     1     2     3     4     5
135         // up    down  right left  back  front 
136         TileSpec tiles[6];
137         // Special material/texture
138         // - Currently used for flowing liquids
139         video::SMaterial *special_materials[CF_SPECIAL_COUNT];
140         AtlasPointer *special_aps[CF_SPECIAL_COUNT];
141         u8 solidness; // Used when choosing which face is drawn
142         u8 visual_solidness; // When solidness=0, this tells how it looks like
143         bool backface_culling;
144 #endif
145
146         /*
147                 Actual data
148         */
149
150         std::string name; // "" = undefined node
151         std::map<std::string, int> groups; // Same as in itemdef
152
153         // Visual definition
154         enum NodeDrawType drawtype;
155         float visual_scale; // Misc. scale parameter
156         std::string tname_tiles[6];
157         MaterialSpec mspec_special[CF_SPECIAL_COUNT]; // Use setter methods
158         u8 alpha;
159
160         // Post effect color, drawn when the camera is inside the node.
161         video::SColor post_effect_color;
162         // Type of MapNode::param1
163         ContentParamType param_type;
164         // Type of MapNode::param2
165         ContentParamType2 param_type_2;
166         // True for all ground-like things like stone and mud, false for eg. trees
167         bool is_ground_content;
168         bool light_propagates;
169         bool sunlight_propagates;
170         // This is used for collision detection.
171         // Also for general solidness queries.
172         bool walkable;
173         // Player can point to these
174         bool pointable;
175         // Player can dig these
176         bool diggable;
177         // Player can climb these
178         bool climbable;
179         // Player can build on these
180         bool buildable_to;
181         // Metadata name of node (eg. "furnace")
182         std::string metadata_name;
183         // Whether the node is non-liquid, source liquid or flowing liquid
184         enum LiquidType liquid_type;
185         // If the content is liquid, this is the flowing version of the liquid.
186         std::string liquid_alternative_flowing;
187         // If the content is liquid, this is the source version of the liquid.
188         std::string liquid_alternative_source;
189         // Viscosity for fluid flow, ranging from 1 to 7, with
190         // 1 giving almost instantaneous propagation and 7 being
191         // the slowest possible
192         u8 liquid_viscosity;
193         // Amount of light the node emits
194         u8 light_source;
195         u32 damage_per_second;
196         NodeBox selection_box;
197         // Compatibility with old maps
198         // Set to true if paramtype used to be 'facedir_simple'
199         bool legacy_facedir_simple;
200         // Set to true if wall_mounted used to be set to true
201         bool legacy_wallmounted;
202
203         /*
204                 Methods
205         */
206         
207         ContentFeatures();
208         ~ContentFeatures();
209         void reset();
210         void serialize(std::ostream &os);
211         void deSerialize(std::istream &is);
212
213         /*
214                 Some handy methods
215         */
216         bool isLiquid() const{
217                 return (liquid_type != LIQUID_NONE);
218         }
219         bool sameLiquid(const ContentFeatures &f) const{
220                 if(!isLiquid() || !f.isLiquid()) return false;
221                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
222         }
223 };
224
225 class INodeDefManager
226 {
227 public:
228         INodeDefManager(){}
229         virtual ~INodeDefManager(){}
230         // Get node definition
231         virtual const ContentFeatures& get(content_t c) const=0;
232         virtual const ContentFeatures& get(const MapNode &n) const=0;
233         virtual bool getId(const std::string &name, content_t &result) const=0;
234         virtual content_t getId(const std::string &name) const=0;
235         virtual const ContentFeatures& get(const std::string &name) const=0;
236         
237         virtual void serialize(std::ostream &os)=0;
238 };
239
240 class IWritableNodeDefManager : public INodeDefManager
241 {
242 public:
243         IWritableNodeDefManager(){}
244         virtual ~IWritableNodeDefManager(){}
245         virtual IWritableNodeDefManager* clone()=0;
246         // Get node definition
247         virtual const ContentFeatures& get(content_t c) const=0;
248         virtual const ContentFeatures& get(const MapNode &n) const=0;
249         virtual bool getId(const std::string &name, content_t &result) const=0;
250         virtual content_t getId(const std::string &name) const=0;
251         // If not found, returns the features of CONTENT_IGNORE
252         virtual const ContentFeatures& get(const std::string &name) const=0;
253
254         // Register node definition
255         virtual void set(content_t c, const ContentFeatures &def)=0;
256         // Register node definition by name (allocate an id)
257         // If returns CONTENT_IGNORE, could not allocate id
258         virtual content_t set(const std::string &name,
259                         const ContentFeatures &def)=0;
260         // If returns CONTENT_IGNORE, could not allocate id
261         virtual content_t allocateDummy(const std::string &name)=0;
262
263         /*
264                 Update item alias mapping.
265                 Call after updating item definitions.
266         */
267         virtual void updateAliases(IItemDefManager *idef)=0;
268
269         /*
270                 Update tile textures to latest return values of TextueSource.
271                 Call after updating the texture atlas of a TextureSource.
272         */
273         virtual void updateTextures(ITextureSource *tsrc)=0;
274
275         virtual void serialize(std::ostream &os)=0;
276         virtual void deSerialize(std::istream &is)=0;
277 };
278
279 IWritableNodeDefManager* createNodeDefManager();
280
281 #endif
282