80396f992308db095f40871d6ec5a4723286692f
[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 "util/numeric.h"
29 #include "mapnode.h"
30 #ifndef SERVER
31 #include "client/tile.h"
32 #include "shader.h"
33 #endif
34 #include "itemgroup.h"
35 #include "sound.h" // SimpleSoundSpec
36 #include "constants.h" // BS
37
38 class INodeDefManager;
39 class IItemDefManager;
40 class ITextureSource;
41 class IShaderSource;
42 class IGameDef;
43 class NodeResolver;
44
45 typedef std::list<std::pair<content_t, int> > GroupItems;
46
47 enum ContentParamType
48 {
49         CPT_NONE,
50         CPT_LIGHT,
51 };
52
53 enum ContentParamType2
54 {
55         CPT2_NONE,
56         // Need 8-bit param2
57         CPT2_FULL,
58         // Flowing liquid properties
59         CPT2_FLOWINGLIQUID,
60         // Direction for chests and furnaces and such
61         CPT2_FACEDIR,
62         // Direction for signs, torches and such
63         CPT2_WALLMOUNTED,
64         // Block level like FLOWINGLIQUID
65         CPT2_LEVELED,
66         // 2D rotation for things like plants
67         CPT2_DEGROTATE,
68         // Mesh options for plants
69         CPT2_MESHOPTIONS
70 };
71
72 enum LiquidType
73 {
74         LIQUID_NONE,
75         LIQUID_FLOWING,
76         LIQUID_SOURCE,
77 };
78
79 enum NodeBoxType
80 {
81         NODEBOX_REGULAR, // Regular block; allows buildable_to
82         NODEBOX_FIXED, // Static separately defined box(es)
83         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
84         NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
85         NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
86 };
87
88 struct NodeBox
89 {
90         enum NodeBoxType type;
91         // NODEBOX_REGULAR (no parameters)
92         // NODEBOX_FIXED
93         std::vector<aabb3f> fixed;
94         // NODEBOX_WALLMOUNTED
95         aabb3f wall_top;
96         aabb3f wall_bottom;
97         aabb3f wall_side; // being at the -X side
98         // NODEBOX_CONNECTED
99         std::vector<aabb3f> connect_top;
100         std::vector<aabb3f> connect_bottom;
101         std::vector<aabb3f> connect_front;
102         std::vector<aabb3f> connect_left;
103         std::vector<aabb3f> connect_back;
104         std::vector<aabb3f> connect_right;
105
106         NodeBox()
107         { reset(); }
108
109         void reset();
110         void serialize(std::ostream &os, u16 protocol_version) const;
111         void deSerialize(std::istream &is);
112 };
113
114 struct MapNode;
115 class NodeMetadata;
116
117 enum LeavesStyle {
118         LEAVES_FANCY,
119         LEAVES_SIMPLE,
120         LEAVES_OPAQUE,
121 };
122
123 class TextureSettings {
124 public:
125         LeavesStyle leaves_style;
126         bool opaque_water;
127         bool connected_glass;
128         bool use_normal_texture;
129         bool enable_mesh_cache;
130         bool enable_minimap;
131
132         TextureSettings() {}
133
134         void readSettings();
135 };
136
137 enum NodeDrawType
138 {
139         NDT_NORMAL, // A basic solid block
140         NDT_AIRLIKE, // Nothing is drawn
141         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
142         NDT_FLOWINGLIQUID, // A very special kind of thing
143         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
144         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
145         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
146         NDT_TORCHLIKE,
147         NDT_SIGNLIKE,
148         NDT_PLANTLIKE,
149         NDT_FENCELIKE,
150         NDT_RAILLIKE,
151         NDT_NODEBOX,
152         NDT_GLASSLIKE_FRAMED, // Glass-like, draw connected frames and all all
153                               // visible faces
154                                                   // uses 2 textures, one for frames, second for faces
155         NDT_FIRELIKE, // Draw faces slightly rotated and only on connecting nodes,
156         NDT_GLASSLIKE_FRAMED_OPTIONAL,  // enabled -> connected, disabled -> Glass-like
157                                                                         // uses 2 textures, one for frames, second for faces
158         NDT_MESH, // Uses static meshes
159 };
160
161 /*
162         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
163 */
164 enum TileAnimationType{
165         TAT_NONE=0,
166         TAT_VERTICAL_FRAMES=1,
167 };
168 struct TileDef
169 {
170         std::string name;
171         bool backface_culling; // Takes effect only in special cases
172         bool tileable_horizontal;
173         bool tileable_vertical;
174         struct{
175                 enum TileAnimationType type;
176                 int aspect_w; // width for aspect ratio
177                 int aspect_h; // height for aspect ratio
178                 float length; // seconds
179         } animation;
180
181         TileDef()
182         {
183                 name = "";
184                 backface_culling = true;
185                 tileable_horizontal = true;
186                 tileable_vertical = true;
187                 animation.type = TAT_NONE;
188                 animation.aspect_w = 1;
189                 animation.aspect_h = 1;
190                 animation.length = 1.0;
191         }
192
193         void serialize(std::ostream &os, u16 protocol_version) const;
194         void deSerialize(std::istream &is, const u8 contentfeatures_version, const NodeDrawType drawtype);
195 };
196
197 #define CF_SPECIAL_COUNT 6
198
199 struct ContentFeatures
200 {
201         /*
202                 Cached stuff
203         */
204 #ifndef SERVER
205         // 0     1     2     3     4     5
206         // up    down  right left  back  front
207         TileSpec tiles[6];
208         // Special tiles
209         // - Currently used for flowing liquids
210         TileSpec special_tiles[CF_SPECIAL_COUNT];
211         u8 solidness; // Used when choosing which face is drawn
212         u8 visual_solidness; // When solidness=0, this tells how it looks like
213         bool backface_culling;
214 #endif
215
216         // Server-side cached callback existence for fast skipping
217         bool has_on_construct;
218         bool has_on_destruct;
219         bool has_after_destruct;
220
221         /*
222                 Actual data
223         */
224
225         std::string name; // "" = undefined node
226         ItemGroupList groups; // Same as in itemdef
227
228         // Visual definition
229         enum NodeDrawType drawtype;
230         std::string mesh;
231 #ifndef SERVER
232         scene::IMesh *mesh_ptr[24];
233         video::SColor minimap_color;
234 #endif
235         float visual_scale; // Misc. scale parameter
236         TileDef tiledef[6];
237         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
238         u8 alpha;
239
240         // Post effect color, drawn when the camera is inside the node.
241         video::SColor post_effect_color;
242
243         // Type of MapNode::param1
244         ContentParamType param_type;
245         // Type of MapNode::param2
246         ContentParamType2 param_type_2;
247         // True for all ground-like things like stone and mud, false for eg. trees
248         bool is_ground_content;
249         bool light_propagates;
250         bool sunlight_propagates;
251         // This is used for collision detection.
252         // Also for general solidness queries.
253         bool walkable;
254         // Player can point to these
255         bool pointable;
256         // Player can dig these
257         bool diggable;
258         // Player can climb these
259         bool climbable;
260         // Player can build on these
261         bool buildable_to;
262         // Liquids flow into and replace node
263         bool floodable;
264         // Player cannot build to these (placement prediction disabled)
265         bool rightclickable;
266         // Flowing liquid or snow, value = default level
267         u8 leveled;
268         // Whether the node is non-liquid, source liquid or flowing liquid
269         enum LiquidType liquid_type;
270         // If the content is liquid, this is the flowing version of the liquid.
271         std::string liquid_alternative_flowing;
272         // If the content is liquid, this is the source version of the liquid.
273         std::string liquid_alternative_source;
274         // Viscosity for fluid flow, ranging from 1 to 7, with
275         // 1 giving almost instantaneous propagation and 7 being
276         // the slowest possible
277         u8 liquid_viscosity;
278         // Is liquid renewable (new liquid source will be created between 2 existing)
279         bool liquid_renewable;
280         // Number of flowing liquids surrounding source
281         u8 liquid_range;
282         u8 drowning;
283         // Amount of light the node emits
284         u8 light_source;
285         u32 damage_per_second;
286         NodeBox node_box;
287         NodeBox selection_box;
288         NodeBox collision_box;
289         // Used for waving leaves/plants
290         u8 waving;
291         // Compatibility with old maps
292         // Set to true if paramtype used to be 'facedir_simple'
293         bool legacy_facedir_simple;
294         // Set to true if wall_mounted used to be set to true
295         bool legacy_wallmounted;
296         // for NDT_CONNECTED pairing
297         u8 connect_sides;
298
299         // Sound properties
300         SimpleSoundSpec sound_footstep;
301         SimpleSoundSpec sound_dig;
302         SimpleSoundSpec sound_dug;
303
304         std::vector<std::string> connects_to;
305         std::set<content_t> connects_to_ids;
306
307         /*
308                 Methods
309         */
310
311         ContentFeatures();
312         ~ContentFeatures();
313         void reset();
314         void serialize(std::ostream &os, u16 protocol_version) const;
315         void deSerialize(std::istream &is);
316         void serializeOld(std::ostream &os, u16 protocol_version) const;
317         void deSerializeOld(std::istream &is, int version);
318
319         /*
320                 Some handy methods
321         */
322         bool isLiquid() const{
323                 return (liquid_type != LIQUID_NONE);
324         }
325         bool sameLiquid(const ContentFeatures &f) const{
326                 if(!isLiquid() || !f.isLiquid()) return false;
327                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
328         }
329
330 #ifndef SERVER
331         void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
332                 u32 shader_id, bool use_normal_texture, bool backface_culling,
333                 u8 alpha, u8 material_type);
334         void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
335                 scene::ISceneManager *smgr, scene::IMeshManipulator *meshmanip,
336                 IGameDef *gamedef, const TextureSettings &tsettings);
337 #endif
338 };
339
340 class INodeDefManager {
341 public:
342         INodeDefManager(){}
343         virtual ~INodeDefManager(){}
344         // Get node definition
345         virtual const ContentFeatures &get(content_t c) const=0;
346         virtual const ContentFeatures &get(const MapNode &n) const=0;
347         virtual bool getId(const std::string &name, content_t &result) const=0;
348         virtual content_t getId(const std::string &name) const=0;
349         // Allows "group:name" in addition to regular node names
350         // returns false if node name not found, true otherwise
351         virtual bool getIds(const std::string &name, std::set<content_t> &result)
352                         const=0;
353         virtual const ContentFeatures &get(const std::string &name) const=0;
354
355         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
356
357         virtual bool getNodeRegistrationStatus() const=0;
358
359         virtual void pendNodeResolve(NodeResolver *nr)=0;
360         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
361         virtual bool nodeboxConnects(const MapNode from, const MapNode to, u8 connect_face)=0;
362 };
363
364 class IWritableNodeDefManager : public INodeDefManager {
365 public:
366         IWritableNodeDefManager(){}
367         virtual ~IWritableNodeDefManager(){}
368         virtual IWritableNodeDefManager* clone()=0;
369         // Get node definition
370         virtual const ContentFeatures &get(content_t c) const=0;
371         virtual const ContentFeatures &get(const MapNode &n) const=0;
372         virtual bool getId(const std::string &name, content_t &result) const=0;
373         // If not found, returns CONTENT_IGNORE
374         virtual content_t getId(const std::string &name) const=0;
375         // Allows "group:name" in addition to regular node names
376         virtual bool getIds(const std::string &name, std::set<content_t> &result)
377                 const=0;
378         // If not found, returns the features of CONTENT_UNKNOWN
379         virtual const ContentFeatures &get(const std::string &name) const=0;
380
381         // Register node definition by name (allocate an id)
382         // If returns CONTENT_IGNORE, could not allocate id
383         virtual content_t set(const std::string &name,
384                         const ContentFeatures &def)=0;
385         // If returns CONTENT_IGNORE, could not allocate id
386         virtual content_t allocateDummy(const std::string &name)=0;
387         // Remove a node
388         virtual void removeNode(const std::string &name)=0;
389
390         /*
391                 Update item alias mapping.
392                 Call after updating item definitions.
393         */
394         virtual void updateAliases(IItemDefManager *idef)=0;
395
396         /*
397                 Override textures from servers with ones specified in texturepack/override.txt
398         */
399         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
400
401         /*
402                 Update tile textures to latest return values of TextueSource.
403         */
404         virtual void updateTextures(IGameDef *gamedef,
405                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
406                 void *progress_cbk_args)=0;
407
408         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
409         virtual void deSerialize(std::istream &is)=0;
410
411         virtual bool getNodeRegistrationStatus() const=0;
412         virtual void setNodeRegistrationStatus(bool completed)=0;
413
414         virtual void pendNodeResolve(NodeResolver *nr)=0;
415         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
416         virtual void runNodeResolveCallbacks()=0;
417         virtual void resetNodeResolveState()=0;
418         virtual void mapNodeboxConnections()=0;
419 };
420
421 IWritableNodeDefManager *createNodeDefManager();
422
423 class NodeResolver {
424 public:
425         NodeResolver();
426         virtual ~NodeResolver();
427         virtual void resolveNodeNames() = 0;
428
429         bool getIdFromNrBacklog(content_t *result_out,
430                 const std::string &node_alt, content_t c_fallback);
431         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
432                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
433
434         void nodeResolveInternal();
435
436         u32 m_nodenames_idx;
437         u32 m_nnlistsizes_idx;
438         std::vector<std::string> m_nodenames;
439         std::vector<size_t> m_nnlistsizes;
440         INodeDefManager *m_ndef;
441         bool m_resolve_done;
442 };
443
444 #endif