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