Update Copyright Years
[oweals/minetest.git] / src / nodedef.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser 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 "irrlichttypes_bloated.h"
24 #include <string>
25 #include <iostream>
26 #include <map>
27 #include "mapnode.h"
28 #ifndef SERVER
29 #include "tile.h"
30 #endif
31 #include "itemgroup.h"
32 #include "sound.h" // SimpleSoundSpec
33 #include "constants.h" // BS
34
35 class IItemDefManager;
36 class ITextureSource;
37 class IGameDef;
38
39 enum ContentParamType
40 {
41         CPT_NONE,
42         CPT_LIGHT,
43 };
44
45 enum ContentParamType2
46 {
47         CPT2_NONE,
48         // Need 8-bit param2
49         CPT2_FULL,
50         // Flowing liquid properties
51         CPT2_FLOWINGLIQUID,
52         // Direction for chests and furnaces and such
53         CPT2_FACEDIR,
54         // Direction for signs, torches and such
55         CPT2_WALLMOUNTED,
56 };
57
58 enum LiquidType
59 {
60         LIQUID_NONE,
61         LIQUID_FLOWING,
62         LIQUID_SOURCE
63 };
64
65 enum NodeBoxType
66 {
67         NODEBOX_REGULAR, // Regular block; allows buildable_to
68         NODEBOX_FIXED, // Static separately defined box(es)
69         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
70 };
71
72 struct NodeBox
73 {
74         enum NodeBoxType type;
75         // NODEBOX_REGULAR (no parameters)
76         // NODEBOX_FIXED
77         std::vector<aabb3f> fixed;
78         // NODEBOX_WALLMOUNTED
79         aabb3f wall_top;
80         aabb3f wall_bottom;
81         aabb3f wall_side; // being at the -X side
82
83         NodeBox()
84         { reset(); }
85
86         void reset();
87         void serialize(std::ostream &os) const;
88         void deSerialize(std::istream &is);
89 };
90
91 struct MapNode;
92 class NodeMetadata;
93
94 /*
95         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
96 */
97 enum TileAnimationType{
98         TAT_NONE=0,
99         TAT_VERTICAL_FRAMES=1,
100 };
101 struct TileDef
102 {
103         std::string name;
104         bool backface_culling; // Takes effect only in special cases
105         struct{
106                 enum TileAnimationType type;
107                 int aspect_w; // width for aspect ratio
108                 int aspect_h; // height for aspect ratio
109                 float length; // seconds
110         } animation;
111
112         TileDef()
113         {
114                 name = "";
115                 backface_culling = true;
116                 animation.type = TAT_NONE;
117                 animation.aspect_w = 1;
118                 animation.aspect_h = 1;
119                 animation.length = 1.0;
120         }
121
122         void serialize(std::ostream &os) const;
123         void deSerialize(std::istream &is);
124 };
125
126 enum NodeDrawType
127 {
128         NDT_NORMAL, // A basic solid block
129         NDT_AIRLIKE, // Nothing is drawn
130         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
131         NDT_FLOWINGLIQUID, // A very special kind of thing
132         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
133         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
134         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
135         NDT_TORCHLIKE,
136         NDT_SIGNLIKE,
137         NDT_PLANTLIKE,
138         NDT_FENCELIKE,
139         NDT_RAILLIKE,
140         NDT_NODEBOX,
141 };
142
143 #define CF_SPECIAL_COUNT 2
144
145 struct ContentFeatures
146 {
147         /*
148                 Cached stuff
149         */
150 #ifndef SERVER
151         // 0     1     2     3     4     5
152         // up    down  right left  back  front 
153         TileSpec tiles[6];
154         // Special tiles
155         // - Currently used for flowing liquids
156         TileSpec special_tiles[CF_SPECIAL_COUNT];
157         u8 solidness; // Used when choosing which face is drawn
158         u8 visual_solidness; // When solidness=0, this tells how it looks like
159         bool backface_culling;
160 #endif
161
162         // Server-side cached callback existence for fast skipping
163         bool has_on_construct;
164         bool has_on_destruct;
165         bool has_after_destruct;
166
167         /*
168                 Actual data
169         */
170
171         std::string name; // "" = undefined node
172         ItemGroupList groups; // Same as in itemdef
173
174         // Visual definition
175         enum NodeDrawType drawtype;
176         float visual_scale; // Misc. scale parameter
177         TileDef tiledef[6];
178         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
179         u8 alpha;
180
181         // Post effect color, drawn when the camera is inside the node.
182         video::SColor post_effect_color;
183         // Type of MapNode::param1
184         ContentParamType param_type;
185         // Type of MapNode::param2
186         ContentParamType2 param_type_2;
187         // True for all ground-like things like stone and mud, false for eg. trees
188         bool is_ground_content;
189         bool light_propagates;
190         bool sunlight_propagates;
191         // This is used for collision detection.
192         // Also for general solidness queries.
193         bool walkable;
194         // Player can point to these
195         bool pointable;
196         // Player can dig these
197         bool diggable;
198         // Player can climb these
199         bool climbable;
200         // Player can build on these
201         bool buildable_to;
202         // Player cannot build to these (placement prediction disabled)
203         bool rightclickable;
204         // Whether the node is non-liquid, source liquid or flowing liquid
205         enum LiquidType liquid_type;
206         // If the content is liquid, this is the flowing version of the liquid.
207         std::string liquid_alternative_flowing;
208         // If the content is liquid, this is the source version of the liquid.
209         std::string liquid_alternative_source;
210         // Viscosity for fluid flow, ranging from 1 to 7, with
211         // 1 giving almost instantaneous propagation and 7 being
212         // the slowest possible
213         u8 liquid_viscosity;
214         // Is liquid renewable (new liquid source will be created between 2 existing)
215         bool liquid_renewable;
216         // Amount of light the node emits
217         u8 light_source;
218         u32 damage_per_second;
219         NodeBox node_box;
220         NodeBox selection_box;
221         // Compatibility with old maps
222         // Set to true if paramtype used to be 'facedir_simple'
223         bool legacy_facedir_simple;
224         // Set to true if wall_mounted used to be set to true
225         bool legacy_wallmounted;
226
227         // Sound properties
228         SimpleSoundSpec sound_footstep;
229         SimpleSoundSpec sound_dig;
230         SimpleSoundSpec sound_dug;
231
232         /*
233                 Methods
234         */
235         
236         ContentFeatures();
237         ~ContentFeatures();
238         void reset();
239         void serialize(std::ostream &os, u16 protocol_version);
240         void deSerialize(std::istream &is);
241         void serializeOld(std::ostream &os, u16 protocol_version);
242         void deSerializeOld(std::istream &is, int version);
243
244         /*
245                 Some handy methods
246         */
247         bool isLiquid() const{
248                 return (liquid_type != LIQUID_NONE);
249         }
250         bool sameLiquid(const ContentFeatures &f) const{
251                 if(!isLiquid() || !f.isLiquid()) return false;
252                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
253         }
254 };
255
256 class INodeDefManager
257 {
258 public:
259         INodeDefManager(){}
260         virtual ~INodeDefManager(){}
261         // Get node definition
262         virtual const ContentFeatures& get(content_t c) const=0;
263         virtual const ContentFeatures& get(const MapNode &n) const=0;
264         virtual bool getId(const std::string &name, content_t &result) const=0;
265         virtual content_t getId(const std::string &name) const=0;
266         // Allows "group:name" in addition to regular node names
267         virtual void getIds(const std::string &name, std::set<content_t> &result)
268                         const=0;
269         virtual const ContentFeatures& get(const std::string &name) const=0;
270         
271         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
272 };
273
274 class IWritableNodeDefManager : public INodeDefManager
275 {
276 public:
277         IWritableNodeDefManager(){}
278         virtual ~IWritableNodeDefManager(){}
279         virtual IWritableNodeDefManager* clone()=0;
280         // Get node definition
281         virtual const ContentFeatures& get(content_t c) const=0;
282         virtual const ContentFeatures& get(const MapNode &n) const=0;
283         virtual bool getId(const std::string &name, content_t &result) const=0;
284         virtual content_t getId(const std::string &name) const=0;
285         // Allows "group:name" in addition to regular node names
286         virtual void getIds(const std::string &name, std::set<content_t> &result)
287                         const=0;
288         // If not found, returns the features of CONTENT_IGNORE
289         virtual const ContentFeatures& get(const std::string &name) const=0;
290
291         // Register node definition
292         virtual void set(content_t c, const ContentFeatures &def)=0;
293         // Register node definition by name (allocate an id)
294         // If returns CONTENT_IGNORE, could not allocate id
295         virtual content_t set(const std::string &name,
296                         const ContentFeatures &def)=0;
297         // If returns CONTENT_IGNORE, could not allocate id
298         virtual content_t allocateDummy(const std::string &name)=0;
299
300         /*
301                 Update item alias mapping.
302                 Call after updating item definitions.
303         */
304         virtual void updateAliases(IItemDefManager *idef)=0;
305
306         /*
307                 Update tile textures to latest return values of TextueSource.
308                 Call after updating the texture atlas of a TextureSource.
309         */
310         virtual void updateTextures(ITextureSource *tsrc)=0;
311
312         virtual void serialize(std::ostream &os, u16 protocol_version)=0;
313         virtual void deSerialize(std::istream &is)=0;
314 };
315
316 IWritableNodeDefManager* createNodeDefManager();
317
318 #endif
319