LocalPlayer::accelerateHorizontal: cleanups
[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 #include <IMeshManipulator.h>
34 class Client;
35 #endif
36 #include "itemgroup.h"
37 #include "sound.h" // SimpleSoundSpec
38 #include "constants.h" // BS
39 #include "tileanimation.h"
40
41 class INodeDefManager;
42 class IItemDefManager;
43 class ITextureSource;
44 class IShaderSource;
45 class IGameDef;
46 class NodeResolver;
47
48 typedef std::list<std::pair<content_t, int> > GroupItems;
49
50 enum ContentParamType
51 {
52         CPT_NONE,
53         CPT_LIGHT,
54 };
55
56 enum ContentParamType2
57 {
58         CPT2_NONE,
59         // Need 8-bit param2
60         CPT2_FULL,
61         // Flowing liquid properties
62         CPT2_FLOWINGLIQUID,
63         // Direction for chests and furnaces and such
64         CPT2_FACEDIR,
65         // Direction for signs, torches and such
66         CPT2_WALLMOUNTED,
67         // Block level like FLOWINGLIQUID
68         CPT2_LEVELED,
69         // 2D rotation for things like plants
70         CPT2_DEGROTATE,
71         // Mesh options for plants
72         CPT2_MESHOPTIONS,
73         // Index for palette
74         CPT2_COLOR,
75         // 3 bits of palette index, then facedir
76         CPT2_COLORED_FACEDIR,
77         // 5 bits of palette index, then wallmounted
78         CPT2_COLORED_WALLMOUNTED,
79         // Glasslike framed drawtype internal liquid level, param2 values 0 to 63
80         CPT2_GLASSLIKE_LIQUID_LEVEL,
81 };
82
83 enum LiquidType
84 {
85         LIQUID_NONE,
86         LIQUID_FLOWING,
87         LIQUID_SOURCE,
88 };
89
90 enum NodeBoxType
91 {
92         NODEBOX_REGULAR, // Regular block; allows buildable_to
93         NODEBOX_FIXED, // Static separately defined box(es)
94         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
95         NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
96         NODEBOX_CONNECTED, // optionally draws nodeboxes if a neighbor node attaches
97 };
98
99 struct NodeBox
100 {
101         enum NodeBoxType type;
102         // NODEBOX_REGULAR (no parameters)
103         // NODEBOX_FIXED
104         std::vector<aabb3f> fixed;
105         // NODEBOX_WALLMOUNTED
106         aabb3f wall_top;
107         aabb3f wall_bottom;
108         aabb3f wall_side; // being at the -X side
109         // NODEBOX_CONNECTED
110         std::vector<aabb3f> connect_top;
111         std::vector<aabb3f> connect_bottom;
112         std::vector<aabb3f> connect_front;
113         std::vector<aabb3f> connect_left;
114         std::vector<aabb3f> connect_back;
115         std::vector<aabb3f> connect_right;
116
117         NodeBox()
118         { reset(); }
119
120         void reset();
121         void serialize(std::ostream &os, u16 protocol_version) const;
122         void deSerialize(std::istream &is);
123 };
124
125 struct MapNode;
126 class NodeMetadata;
127
128 enum LeavesStyle {
129         LEAVES_FANCY,
130         LEAVES_SIMPLE,
131         LEAVES_OPAQUE,
132 };
133
134 class TextureSettings {
135 public:
136         LeavesStyle leaves_style;
137         bool opaque_water;
138         bool connected_glass;
139         bool use_normal_texture;
140         bool enable_mesh_cache;
141         bool enable_minimap;
142
143         TextureSettings() {}
144
145         void readSettings();
146 };
147
148 enum NodeDrawType
149 {
150         // A basic solid block
151         NDT_NORMAL,
152         // Nothing is drawn
153         NDT_AIRLIKE,
154         // Do not draw face towards same kind of flowing/source liquid
155         NDT_LIQUID,
156         // A very special kind of thing
157         NDT_FLOWINGLIQUID,
158         // Glass-like, don't draw faces towards other glass
159         NDT_GLASSLIKE,
160         // Leaves-like, draw all faces no matter what
161         NDT_ALLFACES,
162         // Enabled -> ndt_allfaces, disabled -> ndt_normal
163         NDT_ALLFACES_OPTIONAL,
164         // Single plane perpendicular to a surface
165         NDT_TORCHLIKE,
166         // Single plane parallel to a surface
167         NDT_SIGNLIKE,
168         // 2 vertical planes in a 'X' shape diagonal to XZ axes.
169         // paramtype2 = "meshoptions" allows various forms, sizes and
170         // vertical and horizontal random offsets.
171         NDT_PLANTLIKE,
172         // Fenceposts that connect to neighbouring fenceposts with horizontal bars
173         NDT_FENCELIKE,
174         // Selects appropriate junction texture to connect like rails to
175         // neighbouring raillikes.
176         NDT_RAILLIKE,
177         // Custom Lua-definable structure of multiple cuboids
178         NDT_NODEBOX,
179         // Glass-like, draw connected frames and all visible faces.
180         // param2 > 0 defines 64 levels of internal liquid
181         // Uses 3 textures, one for frames, second for faces,
182         // optional third is a 'special tile' for the liquid.
183         NDT_GLASSLIKE_FRAMED,
184         // Draw faces slightly rotated and only on neighbouring nodes
185         NDT_FIRELIKE,
186         // Enabled -> ndt_glasslike_framed, disabled -> ndt_glasslike
187         NDT_GLASSLIKE_FRAMED_OPTIONAL,
188         // Uses static meshes
189         NDT_MESH,
190         // Combined plantlike-on-solid
191         NDT_PLANTLIKE_ROOTED,
192 };
193
194 // Mesh options for NDT_PLANTLIKE with CPT2_MESHOPTIONS
195 static const u8 MO_MASK_STYLE          = 0x07;
196 static const u8 MO_BIT_RANDOM_OFFSET   = 0x08;
197 static const u8 MO_BIT_SCALE_SQRT2     = 0x10;
198 static const u8 MO_BIT_RANDOM_OFFSET_Y = 0x20;
199 enum PlantlikeStyle {
200         PLANT_STYLE_CROSS,
201         PLANT_STYLE_CROSS2,
202         PLANT_STYLE_STAR,
203         PLANT_STYLE_HASH,
204         PLANT_STYLE_HASH2,
205 };
206
207 /*
208         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
209 */
210
211 struct TileDef
212 {
213         std::string name = "";
214         bool backface_culling = true; // Takes effect only in special cases
215         bool tileable_horizontal = true;
216         bool tileable_vertical = true;
217         //! If true, the tile has its own color.
218         bool has_color = false;
219         //! The color of the tile.
220         video::SColor color = video::SColor(0xFFFFFFFF);
221
222         struct TileAnimationParams animation;
223
224         TileDef()
225         {
226                 animation.type = TAT_NONE;
227         }
228
229         void serialize(std::ostream &os, u16 protocol_version) const;
230         void deSerialize(std::istream &is, const u8 contentfeatures_version, const NodeDrawType drawtype);
231 };
232
233 #define CF_SPECIAL_COUNT 6
234
235 struct ContentFeatures
236 {
237         /*
238                 Cached stuff
239          */
240 #ifndef SERVER
241         // 0     1     2     3     4     5
242         // up    down  right left  back  front
243         TileSpec tiles[6];
244         // Special tiles
245         // - Currently used for flowing liquids
246         TileSpec special_tiles[CF_SPECIAL_COUNT];
247         u8 solidness; // Used when choosing which face is drawn
248         u8 visual_solidness; // When solidness=0, this tells how it looks like
249         bool backface_culling;
250 #endif
251
252         // Server-side cached callback existence for fast skipping
253         bool has_on_construct;
254         bool has_on_destruct;
255         bool has_after_destruct;
256
257         /*
258                 Actual data
259          */
260
261         // --- GENERAL PROPERTIES ---
262
263         std::string name; // "" = undefined node
264         ItemGroupList groups; // Same as in itemdef
265         // Type of MapNode::param1
266         ContentParamType param_type;
267         // Type of MapNode::param2
268         ContentParamType2 param_type_2;
269
270         // --- VISUAL PROPERTIES ---
271
272         enum NodeDrawType drawtype;
273         std::string mesh;
274 #ifndef SERVER
275         scene::IMesh *mesh_ptr[24];
276         video::SColor minimap_color;
277 #endif
278         float visual_scale; // Misc. scale parameter
279         TileDef tiledef[6];
280         // These will be drawn over the base tiles.
281         TileDef tiledef_overlay[6];
282         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
283         // If 255, the node is opaque.
284         // Otherwise it uses texture alpha.
285         u8 alpha;
286         // The color of the node.
287         video::SColor color;
288         std::string palette_name;
289         std::vector<video::SColor> *palette;
290         // Used for waving leaves/plants
291         u8 waving;
292         // for NDT_CONNECTED pairing
293         u8 connect_sides;
294         std::vector<std::string> connects_to;
295         std::set<content_t> connects_to_ids;
296         // Post effect color, drawn when the camera is inside the node.
297         video::SColor post_effect_color;
298         // Flowing liquid or snow, value = default level
299         u8 leveled;
300
301         // --- LIGHTING-RELATED ---
302
303         bool light_propagates;
304         bool sunlight_propagates;
305         // Amount of light the node emits
306         u8 light_source;
307
308         // --- MAP GENERATION ---
309
310         // True for all ground-like things like stone and mud, false for eg. trees
311         bool is_ground_content;
312
313         // --- INTERACTION PROPERTIES ---
314
315         // This is used for collision detection.
316         // Also for general solidness queries.
317         bool walkable;
318         // Player can point to these
319         bool pointable;
320         // Player can dig these
321         bool diggable;
322         // Player can climb these
323         bool climbable;
324         // Player can build on these
325         bool buildable_to;
326         // Player cannot build to these (placement prediction disabled)
327         bool rightclickable;
328         u32 damage_per_second;
329
330         // --- LIQUID PROPERTIES ---
331
332         // Whether the node is non-liquid, source liquid or flowing liquid
333         enum LiquidType liquid_type;
334         // If the content is liquid, this is the flowing version of the liquid.
335         std::string liquid_alternative_flowing;
336         // If the content is liquid, this is the source version of the liquid.
337         std::string liquid_alternative_source;
338         // Viscosity for fluid flow, ranging from 1 to 7, with
339         // 1 giving almost instantaneous propagation and 7 being
340         // the slowest possible
341         u8 liquid_viscosity;
342         // Is liquid renewable (new liquid source will be created between 2 existing)
343         bool liquid_renewable;
344         // Number of flowing liquids surrounding source
345         u8 liquid_range;
346         u8 drowning;
347         // Liquids flow into and replace node
348         bool floodable;
349
350         // --- NODEBOXES ---
351
352         NodeBox node_box;
353         NodeBox selection_box;
354         NodeBox collision_box;
355
356         // --- SOUND PROPERTIES ---
357
358         SimpleSoundSpec sound_footstep;
359         SimpleSoundSpec sound_dig;
360         SimpleSoundSpec sound_dug;
361
362         // --- LEGACY ---
363
364         // Compatibility with old maps
365         // Set to true if paramtype used to be 'facedir_simple'
366         bool legacy_facedir_simple;
367         // Set to true if wall_mounted used to be set to true
368         bool legacy_wallmounted;
369
370         /*
371                 Methods
372         */
373
374         ContentFeatures();
375         ~ContentFeatures();
376         void reset();
377         void serialize(std::ostream &os, u16 protocol_version) const;
378         void deSerialize(std::istream &is);
379         void serializeOld(std::ostream &os, u16 protocol_version) const;
380         void deSerializeOld(std::istream &is, int version);
381         /*!
382          * Since vertex alpha is no longer supported, this method
383          * adds opacity directly to the texture pixels.
384          *
385          * \param tiles array of the tile definitions.
386          * \param length length of tiles
387          */
388         void correctAlpha(TileDef *tiles, int length);
389
390         /*
391                 Some handy methods
392         */
393         bool isLiquid() const{
394                 return (liquid_type != LIQUID_NONE);
395         }
396         bool sameLiquid(const ContentFeatures &f) const{
397                 if(!isLiquid() || !f.isLiquid()) return false;
398                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
399         }
400
401         int getGroup(const std::string &group) const
402         {
403                 return itemgroup_get(groups, group);
404         }
405
406 #ifndef SERVER
407         void fillTileAttribs(ITextureSource *tsrc, TileLayer *tile, TileDef *tiledef,
408                 u32 shader_id, bool use_normal_texture, bool backface_culling,
409                 u8 material_type);
410         void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
411                 scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
412 #endif
413 };
414
415 class INodeDefManager {
416 public:
417         INodeDefManager(){}
418         virtual ~INodeDefManager(){}
419         // Get node definition
420         virtual const ContentFeatures &get(content_t c) const=0;
421         virtual const ContentFeatures &get(const MapNode &n) const=0;
422         virtual bool getId(const std::string &name, content_t &result) const=0;
423         virtual content_t getId(const std::string &name) const=0;
424         // Allows "group:name" in addition to regular node names
425         // returns false if node name not found, true otherwise
426         virtual bool getIds(const std::string &name, std::set<content_t> &result)
427                         const=0;
428         virtual const ContentFeatures &get(const std::string &name) const=0;
429
430         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
431
432         virtual void pendNodeResolve(NodeResolver *nr)=0;
433         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
434         virtual bool nodeboxConnects(const MapNode from, const MapNode to, u8 connect_face)=0;
435         /*!
436          * Returns the smallest box in node coordinates that
437          * contains all nodes' selection boxes.
438          */
439         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
440 };
441
442 class IWritableNodeDefManager : public INodeDefManager {
443 public:
444         IWritableNodeDefManager(){}
445         virtual ~IWritableNodeDefManager(){}
446         virtual IWritableNodeDefManager* clone()=0;
447         // Get node definition
448         virtual const ContentFeatures &get(content_t c) const=0;
449         virtual const ContentFeatures &get(const MapNode &n) const=0;
450         virtual bool getId(const std::string &name, content_t &result) const=0;
451         // If not found, returns CONTENT_IGNORE
452         virtual content_t getId(const std::string &name) const=0;
453         // Allows "group:name" in addition to regular node names
454         virtual bool getIds(const std::string &name, std::set<content_t> &result)
455                 const=0;
456         // If not found, returns the features of CONTENT_UNKNOWN
457         virtual const ContentFeatures &get(const std::string &name) const=0;
458
459         // Register node definition by name (allocate an id)
460         // If returns CONTENT_IGNORE, could not allocate id
461         virtual content_t set(const std::string &name,
462                         const ContentFeatures &def)=0;
463         // If returns CONTENT_IGNORE, could not allocate id
464         virtual content_t allocateDummy(const std::string &name)=0;
465         // Remove a node
466         virtual void removeNode(const std::string &name)=0;
467
468         /*
469                 Update item alias mapping.
470                 Call after updating item definitions.
471         */
472         virtual void updateAliases(IItemDefManager *idef)=0;
473
474         /*
475                 Override textures from servers with ones specified in texturepack/override.txt
476         */
477         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
478
479         /*
480                 Update tile textures to latest return values of TextueSource.
481         */
482         virtual void updateTextures(IGameDef *gamedef,
483                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
484                 void *progress_cbk_args)=0;
485
486         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
487         virtual void deSerialize(std::istream &is)=0;
488
489         virtual void setNodeRegistrationStatus(bool completed)=0;
490
491         virtual void pendNodeResolve(NodeResolver *nr)=0;
492         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
493         virtual void runNodeResolveCallbacks()=0;
494         virtual void resetNodeResolveState()=0;
495         virtual void mapNodeboxConnections()=0;
496         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
497 };
498
499 IWritableNodeDefManager *createNodeDefManager();
500
501 class NodeResolver {
502 public:
503         NodeResolver();
504         virtual ~NodeResolver();
505         virtual void resolveNodeNames() = 0;
506
507         bool getIdFromNrBacklog(content_t *result_out,
508                 const std::string &node_alt, content_t c_fallback);
509         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
510                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
511
512         void nodeResolveInternal();
513
514         u32 m_nodenames_idx = 0;
515         u32 m_nnlistsizes_idx = 0;
516         std::vector<std::string> m_nodenames;
517         std::vector<size_t> m_nnlistsizes;
518         INodeDefManager *m_ndef = nullptr;
519         bool m_resolve_done = false;
520 };
521
522 #endif