]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
Add hardware node coloring. Includes:
[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 /*
170         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
171 */
172
173 struct TileDef
174 {
175         std::string name;
176         bool backface_culling; // Takes effect only in special cases
177         bool tileable_horizontal;
178         bool tileable_vertical;
179         //! If true, the tile has its own color.
180         bool has_color;
181         //! The color of the tile.
182         video::SColor color;
183
184         struct TileAnimationParams animation;
185
186         TileDef()
187         {
188                 name = "";
189                 backface_culling = true;
190                 tileable_horizontal = true;
191                 tileable_vertical = true;
192                 has_color = false;
193                 color = video::SColor(0xFFFFFFFF);
194                 animation.type = TAT_NONE;
195         }
196
197         void serialize(std::ostream &os, u16 protocol_version) const;
198         void deSerialize(std::istream &is, const u8 contentfeatures_version, const NodeDrawType drawtype);
199 };
200
201 #define CF_SPECIAL_COUNT 6
202
203 struct ContentFeatures
204 {
205         /*
206                 Cached stuff
207          */
208 #ifndef SERVER
209         // 0     1     2     3     4     5
210         // up    down  right left  back  front
211         TileSpec tiles[6];
212         // Special tiles
213         // - Currently used for flowing liquids
214         TileSpec special_tiles[CF_SPECIAL_COUNT];
215         u8 solidness; // Used when choosing which face is drawn
216         u8 visual_solidness; // When solidness=0, this tells how it looks like
217         bool backface_culling;
218 #endif
219
220         // Server-side cached callback existence for fast skipping
221         bool has_on_construct;
222         bool has_on_destruct;
223         bool has_after_destruct;
224
225         /*
226                 Actual data
227          */
228
229         // --- GENERAL PROPERTIES ---
230
231         std::string name; // "" = undefined node
232         ItemGroupList groups; // Same as in itemdef
233         // Type of MapNode::param1
234         ContentParamType param_type;
235         // Type of MapNode::param2
236         ContentParamType2 param_type_2;
237
238         // --- VISUAL PROPERTIES ---
239
240         enum NodeDrawType drawtype;
241         std::string mesh;
242 #ifndef SERVER
243         scene::IMesh *mesh_ptr[24];
244         video::SColor minimap_color;
245 #endif
246         float visual_scale; // Misc. scale parameter
247         TileDef tiledef[6];
248         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
249         // If 255, the node is opaque.
250         // Otherwise it uses texture alpha.
251         u8 alpha;
252         // The color of the node.
253         video::SColor color;
254         std::string palette_name;
255         std::vector<video::SColor> *palette;
256         // Used for waving leaves/plants
257         u8 waving;
258         // for NDT_CONNECTED pairing
259         u8 connect_sides;
260         std::vector<std::string> connects_to;
261         std::set<content_t> connects_to_ids;
262         // Post effect color, drawn when the camera is inside the node.
263         video::SColor post_effect_color;
264         // Flowing liquid or snow, value = default level
265         u8 leveled;
266
267         // --- LIGHTING-RELATED ---
268
269         bool light_propagates;
270         bool sunlight_propagates;
271         // Amount of light the node emits
272         u8 light_source;
273
274         // --- MAP GENERATION ---
275
276         // True for all ground-like things like stone and mud, false for eg. trees
277         bool is_ground_content;
278
279         // --- INTERACTION PROPERTIES ---
280
281         // This is used for collision detection.
282         // Also for general solidness queries.
283         bool walkable;
284         // Player can point to these
285         bool pointable;
286         // Player can dig these
287         bool diggable;
288         // Player can climb these
289         bool climbable;
290         // Player can build on these
291         bool buildable_to;
292         // Player cannot build to these (placement prediction disabled)
293         bool rightclickable;
294         u32 damage_per_second;
295
296         // --- LIQUID PROPERTIES ---
297
298         // Whether the node is non-liquid, source liquid or flowing liquid
299         enum LiquidType liquid_type;
300         // If the content is liquid, this is the flowing version of the liquid.
301         std::string liquid_alternative_flowing;
302         // If the content is liquid, this is the source version of the liquid.
303         std::string liquid_alternative_source;
304         // Viscosity for fluid flow, ranging from 1 to 7, with
305         // 1 giving almost instantaneous propagation and 7 being
306         // the slowest possible
307         u8 liquid_viscosity;
308         // Is liquid renewable (new liquid source will be created between 2 existing)
309         bool liquid_renewable;
310         // Number of flowing liquids surrounding source
311         u8 liquid_range;
312         u8 drowning;
313         // Liquids flow into and replace node
314         bool floodable;
315
316         // --- NODEBOXES ---
317
318         NodeBox node_box;
319         NodeBox selection_box;
320         NodeBox collision_box;
321
322         // --- SOUND PROPERTIES ---
323
324         SimpleSoundSpec sound_footstep;
325         SimpleSoundSpec sound_dig;
326         SimpleSoundSpec sound_dug;
327
328         // --- LEGACY ---
329
330         // Compatibility with old maps
331         // Set to true if paramtype used to be 'facedir_simple'
332         bool legacy_facedir_simple;
333         // Set to true if wall_mounted used to be set to true
334         bool legacy_wallmounted;
335
336         /*
337                 Methods
338         */
339
340         ContentFeatures();
341         ~ContentFeatures();
342         void reset();
343         void serialize(std::ostream &os, u16 protocol_version) const;
344         void deSerialize(std::istream &is);
345         void serializeOld(std::ostream &os, u16 protocol_version) const;
346         void deSerializeOld(std::istream &is, int version);
347         /*!
348          * Since vertex alpha is no lnger supported, this method
349          * adds instructions to the texture names to blend alpha there.
350          *
351          * tiledef, tiledef_special and alpha must be initialized
352          * before calling this.
353          */
354         void correctAlpha();
355
356         /*
357                 Some handy methods
358         */
359         bool isLiquid() const{
360                 return (liquid_type != LIQUID_NONE);
361         }
362         bool sameLiquid(const ContentFeatures &f) const{
363                 if(!isLiquid() || !f.isLiquid()) return false;
364                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
365         }
366
367 #ifndef SERVER
368         void fillTileAttribs(ITextureSource *tsrc, TileSpec *tile, TileDef *tiledef,
369                 u32 shader_id, bool use_normal_texture, bool backface_culling,
370                 u8 material_type);
371         void updateTextures(ITextureSource *tsrc, IShaderSource *shdsrc,
372                 scene::IMeshManipulator *meshmanip, Client *client, const TextureSettings &tsettings);
373 #endif
374 };
375
376 class INodeDefManager {
377 public:
378         INodeDefManager(){}
379         virtual ~INodeDefManager(){}
380         // Get node definition
381         virtual const ContentFeatures &get(content_t c) const=0;
382         virtual const ContentFeatures &get(const MapNode &n) const=0;
383         virtual bool getId(const std::string &name, content_t &result) const=0;
384         virtual content_t getId(const std::string &name) const=0;
385         // Allows "group:name" in addition to regular node names
386         // returns false if node name not found, true otherwise
387         virtual bool getIds(const std::string &name, std::set<content_t> &result)
388                         const=0;
389         virtual const ContentFeatures &get(const std::string &name) const=0;
390
391         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
392
393         virtual bool getNodeRegistrationStatus() const=0;
394
395         virtual void pendNodeResolve(NodeResolver *nr)=0;
396         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
397         virtual bool nodeboxConnects(const MapNode from, const MapNode to, u8 connect_face)=0;
398         /*!
399          * Returns the smallest box in node coordinates that
400          * contains all nodes' selection boxes.
401          */
402         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
403 };
404
405 class IWritableNodeDefManager : public INodeDefManager {
406 public:
407         IWritableNodeDefManager(){}
408         virtual ~IWritableNodeDefManager(){}
409         virtual IWritableNodeDefManager* clone()=0;
410         // Get node definition
411         virtual const ContentFeatures &get(content_t c) const=0;
412         virtual const ContentFeatures &get(const MapNode &n) const=0;
413         virtual bool getId(const std::string &name, content_t &result) const=0;
414         // If not found, returns CONTENT_IGNORE
415         virtual content_t getId(const std::string &name) const=0;
416         // Allows "group:name" in addition to regular node names
417         virtual bool getIds(const std::string &name, std::set<content_t> &result)
418                 const=0;
419         // If not found, returns the features of CONTENT_UNKNOWN
420         virtual const ContentFeatures &get(const std::string &name) const=0;
421
422         // Register node definition by name (allocate an id)
423         // If returns CONTENT_IGNORE, could not allocate id
424         virtual content_t set(const std::string &name,
425                         const ContentFeatures &def)=0;
426         // If returns CONTENT_IGNORE, could not allocate id
427         virtual content_t allocateDummy(const std::string &name)=0;
428         // Remove a node
429         virtual void removeNode(const std::string &name)=0;
430
431         /*
432                 Update item alias mapping.
433                 Call after updating item definitions.
434         */
435         virtual void updateAliases(IItemDefManager *idef)=0;
436
437         /*
438                 Override textures from servers with ones specified in texturepack/override.txt
439         */
440         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
441
442         /*
443                 Update tile textures to latest return values of TextueSource.
444         */
445         virtual void updateTextures(IGameDef *gamedef,
446                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
447                 void *progress_cbk_args)=0;
448
449         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
450         virtual void deSerialize(std::istream &is)=0;
451
452         virtual bool getNodeRegistrationStatus() const=0;
453         virtual void setNodeRegistrationStatus(bool completed)=0;
454
455         virtual void pendNodeResolve(NodeResolver *nr)=0;
456         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
457         virtual void runNodeResolveCallbacks()=0;
458         virtual void resetNodeResolveState()=0;
459         virtual void mapNodeboxConnections()=0;
460         virtual core::aabbox3d<s16> getSelectionBoxIntUnion() const=0;
461 };
462
463 IWritableNodeDefManager *createNodeDefManager();
464
465 class NodeResolver {
466 public:
467         NodeResolver();
468         virtual ~NodeResolver();
469         virtual void resolveNodeNames() = 0;
470
471         bool getIdFromNrBacklog(content_t *result_out,
472                 const std::string &node_alt, content_t c_fallback);
473         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
474                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
475
476         void nodeResolveInternal();
477
478         u32 m_nodenames_idx;
479         u32 m_nnlistsizes_idx;
480         std::vector<std::string> m_nodenames;
481         std::vector<size_t> m_nnlistsizes;
482         INodeDefManager *m_ndef;
483         bool m_resolve_done;
484 };
485
486 #endif