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