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