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