598ad7fb391555eeac7f122189caf479e0fc89b4
[oweals/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 <set>
27 #include "mapnode.h"
28 #ifndef SERVER
29 #include "tile.h"
30 #endif
31 #include "materials.h" // MaterialProperties
32 class IItemDefManager;
33 class ITextureSource;
34 class IGameDef;
35
36 enum ContentParamType
37 {
38         CPT_NONE,
39         CPT_LIGHT,
40         CPT_MINERAL,
41         // Direction for chests and furnaces and such
42         CPT_FACEDIR_SIMPLE
43 };
44
45 enum LiquidType
46 {
47         LIQUID_NONE,
48         LIQUID_FLOWING,
49         LIQUID_SOURCE
50 };
51
52 enum NodeBoxType
53 {
54         NODEBOX_REGULAR, // Regular block; allows buildable_to
55         NODEBOX_FIXED, // Static separately defined box
56         NODEBOX_WALLMOUNTED, // Box for wall_mounted nodes; (top, bottom, side)
57 };
58
59 struct NodeBox
60 {
61         enum NodeBoxType type;
62         // NODEBOX_REGULAR (no parameters)
63         // NODEBOX_FIXED
64         core::aabbox3d<f32> fixed;
65         // NODEBOX_WALLMOUNTED
66         core::aabbox3d<f32> wall_top;
67         core::aabbox3d<f32> wall_bottom;
68         core::aabbox3d<f32> wall_side; // being at the -X side
69
70         NodeBox():
71                 type(NODEBOX_REGULAR),
72                 // default is rail-like
73                 fixed(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
74                 // default is sign/ladder-like
75                 wall_top(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2),
76                 wall_bottom(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
77                 wall_side(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2)
78         {}
79
80         void serialize(std::ostream &os) const;
81         void deSerialize(std::istream &is);
82 };
83
84 struct MapNode;
85 class NodeMetadata;
86
87 struct MaterialSpec
88 {
89         std::string tname;
90         bool backface_culling;
91         
92         MaterialSpec(const std::string &tname_="", bool backface_culling_=true):
93                 tname(tname_),
94                 backface_culling(backface_culling_)
95         {}
96
97         void serialize(std::ostream &os) const;
98         void deSerialize(std::istream &is);
99 };
100
101 enum NodeDrawType
102 {
103         NDT_NORMAL, // A basic solid block
104         NDT_AIRLIKE, // Nothing is drawn
105         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
106         NDT_FLOWINGLIQUID, // A very special kind of thing
107         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
108         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
109         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
110         NDT_TORCHLIKE,
111         NDT_SIGNLIKE,
112         NDT_PLANTLIKE,
113         NDT_FENCELIKE,
114         NDT_RAILLIKE,
115 };
116
117 #define CF_SPECIAL_COUNT 2
118
119 struct ContentFeatures
120 {
121         /*
122                 Cached stuff
123         */
124 #ifndef SERVER
125         // 0     1     2     3     4     5
126         // up    down  right left  back  front 
127         TileSpec tiles[6];
128         // Special material/texture
129         // - Currently used for flowing liquids
130         video::SMaterial *special_materials[CF_SPECIAL_COUNT];
131         AtlasPointer *special_aps[CF_SPECIAL_COUNT];
132         u8 solidness; // Used when choosing which face is drawn
133         u8 visual_solidness; // When solidness=0, this tells how it looks like
134         bool backface_culling;
135 #endif
136
137         /*
138                 Actual data
139         */
140
141         std::string name; // "" = undefined node
142
143         // Visual definition
144         enum NodeDrawType drawtype;
145         float visual_scale; // Misc. scale parameter
146         std::string tname_tiles[6];
147         MaterialSpec mspec_special[CF_SPECIAL_COUNT]; // Use setter methods
148         u8 alpha;
149
150         // Post effect color, drawn when the camera is inside the node.
151         video::SColor post_effect_color;
152         // Type of MapNode::param1
153         ContentParamType param_type;
154         // True for all ground-like things like stone and mud, false for eg. trees
155         bool is_ground_content;
156         bool light_propagates;
157         bool sunlight_propagates;
158         // This is used for collision detection.
159         // Also for general solidness queries.
160         bool walkable;
161         // Player can point to these
162         bool pointable;
163         // Player can dig these
164         bool diggable;
165         // Player can climb these
166         bool climbable;
167         // Player can build on these
168         bool buildable_to;
169         // If true, param2 is set to direction when placed. Used for torches.
170         // NOTE: the direction format is quite inefficient and should be changed
171         bool wall_mounted;
172         // Inventory item string as which the node appears in inventory when dug.
173         // Mineral overrides this.
174         std::string dug_item;
175         // Extra dug item and its rarity
176         std::string extra_dug_item;
177         // Usual get interval for extra dug item
178         s32 extra_dug_item_rarity;
179         // Metadata name of node (eg. "furnace")
180         std::string metadata_name;
181         // Whether the node is non-liquid, source liquid or flowing liquid
182         enum LiquidType liquid_type;
183         // If the content is liquid, this is the flowing version of the liquid.
184         std::string liquid_alternative_flowing;
185         // If the content is liquid, this is the source version of the liquid.
186         std::string liquid_alternative_source;
187         // Viscosity for fluid flow, ranging from 1 to 7, with
188         // 1 giving almost instantaneous propagation and 7 being
189         // the slowest possible
190         u8 liquid_viscosity;
191         // Amount of light the node emits
192         u8 light_source;
193         u32 damage_per_second;
194         NodeBox selection_box;
195         MaterialProperties material;
196
197         /*
198                 Methods
199         */
200         
201         ContentFeatures();
202         ~ContentFeatures();
203         void reset();
204         void serialize(std::ostream &os);
205         void deSerialize(std::istream &is);
206
207         /*
208                 Some handy methods
209         */
210         bool isLiquid() const{
211                 return (liquid_type != LIQUID_NONE);
212         }
213         bool sameLiquid(const ContentFeatures &f) const{
214                 if(!isLiquid() || !f.isLiquid()) return false;
215                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
216         }
217 };
218
219 class INodeDefManager
220 {
221 public:
222         INodeDefManager(){}
223         virtual ~INodeDefManager(){}
224         // Get node definition
225         virtual const ContentFeatures& get(content_t c) const=0;
226         virtual const ContentFeatures& get(const MapNode &n) const=0;
227         virtual bool getId(const std::string &name, content_t &result) const=0;
228         virtual content_t getId(const std::string &name) const=0;
229         virtual const ContentFeatures& get(const std::string &name) const=0;
230         
231         virtual void serialize(std::ostream &os)=0;
232 };
233
234 class IWritableNodeDefManager : public INodeDefManager
235 {
236 public:
237         IWritableNodeDefManager(){}
238         virtual ~IWritableNodeDefManager(){}
239         virtual IWritableNodeDefManager* clone()=0;
240         // Get node definition
241         virtual const ContentFeatures& get(content_t c) const=0;
242         virtual const ContentFeatures& get(const MapNode &n) const=0;
243         virtual bool getId(const std::string &name, content_t &result) const=0;
244         virtual content_t getId(const std::string &name) const=0;
245         // If not found, returns the features of CONTENT_IGNORE
246         virtual const ContentFeatures& get(const std::string &name) const=0;
247
248         // Register node definition
249         virtual void set(content_t c, const ContentFeatures &def)=0;
250         // Register node definition by name (allocate an id)
251         // If returns CONTENT_IGNORE, could not allocate id
252         virtual content_t set(const std::string &name,
253                         const ContentFeatures &def)=0;
254         // If returns CONTENT_IGNORE, could not allocate id
255         virtual content_t allocateDummy(const std::string &name)=0;
256
257         /*
258                 Update item alias mapping.
259                 Call after updating item definitions.
260         */
261         virtual void updateAliases(IItemDefManager *idef)=0;
262
263         /*
264                 Update tile textures to latest return values of TextueSource.
265                 Call after updating the texture atlas of a TextureSource.
266         */
267         virtual void updateTextures(ITextureSource *tsrc)=0;
268
269         virtual void serialize(std::ostream &os)=0;
270         virtual void deSerialize(std::istream &is)=0;
271 };
272
273 IWritableNodeDefManager* createNodeDefManager();
274
275 #endif
276