]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.h
Add texture overriding
[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 #endif
34 #include "itemgroup.h"
35 #include "sound.h" // SimpleSoundSpec
36 #include "constants.h" // BS
37
38 class INodeDefManager;
39 class IItemDefManager;
40 class ITextureSource;
41 class IShaderSource;
42 class IGameDef;
43 class NodeResolver;
44
45 typedef std::list<std::pair<content_t, int> > GroupItems;
46
47 enum ContentParamType
48 {
49         CPT_NONE,
50         CPT_LIGHT,
51 };
52
53 enum ContentParamType2
54 {
55         CPT2_NONE,
56         // Need 8-bit param2
57         CPT2_FULL,
58         // Flowing liquid properties
59         CPT2_FLOWINGLIQUID,
60         // Direction for chests and furnaces and such
61         CPT2_FACEDIR,
62         // Direction for signs, torches and such
63         CPT2_WALLMOUNTED,
64         // Block level like FLOWINGLIQUID
65         CPT2_LEVELED,
66 };
67
68 enum LiquidType
69 {
70         LIQUID_NONE,
71         LIQUID_FLOWING,
72         LIQUID_SOURCE,
73 };
74
75 enum NodeBoxType
76 {
77         NODEBOX_REGULAR, // Regular block; allows buildable_to
78         NODEBOX_FIXED, // Static separately defined box(es)
79         NODEBOX_WALLMOUNTED, // Box for wall mounted nodes; (top, bottom, side)
80         NODEBOX_LEVELED, // Same as fixed, but with dynamic height from param2. for snow, ...
81 };
82
83 struct NodeBox
84 {
85         enum NodeBoxType type;
86         // NODEBOX_REGULAR (no parameters)
87         // NODEBOX_FIXED
88         std::vector<aabb3f> fixed;
89         // NODEBOX_WALLMOUNTED
90         aabb3f wall_top;
91         aabb3f wall_bottom;
92         aabb3f wall_side; // being at the -X side
93
94         NodeBox()
95         { reset(); }
96
97         void reset();
98         void serialize(std::ostream &os, u16 protocol_version) const;
99         void deSerialize(std::istream &is);
100 };
101
102 struct MapNode;
103 class NodeMetadata;
104
105 /*
106         Stand-alone definition of a TileSpec (basically a server-side TileSpec)
107 */
108 enum TileAnimationType{
109         TAT_NONE=0,
110         TAT_VERTICAL_FRAMES=1,
111 };
112 struct TileDef
113 {
114         std::string name;
115         bool backface_culling; // Takes effect only in special cases
116         struct{
117                 enum TileAnimationType type;
118                 int aspect_w; // width for aspect ratio
119                 int aspect_h; // height for aspect ratio
120                 float length; // seconds
121         } animation;
122
123         TileDef()
124         {
125                 name = "";
126                 backface_culling = true;
127                 animation.type = TAT_NONE;
128                 animation.aspect_w = 1;
129                 animation.aspect_h = 1;
130                 animation.length = 1.0;
131         }
132
133         void serialize(std::ostream &os, u16 protocol_version) const;
134         void deSerialize(std::istream &is);
135 };
136
137 enum NodeDrawType
138 {
139         NDT_NORMAL, // A basic solid block
140         NDT_AIRLIKE, // Nothing is drawn
141         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
142         NDT_FLOWINGLIQUID, // A very special kind of thing
143         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
144         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
145         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
146         NDT_TORCHLIKE,
147         NDT_SIGNLIKE,
148         NDT_PLANTLIKE,
149         NDT_FENCELIKE,
150         NDT_RAILLIKE,
151         NDT_NODEBOX,
152         NDT_GLASSLIKE_FRAMED, // Glass-like, draw connected frames and all all
153                               // visible faces
154                                                   // uses 2 textures, one for frames, second for faces
155         NDT_FIRELIKE, // Draw faces slightly rotated and only on connecting nodes,
156         NDT_GLASSLIKE_FRAMED_OPTIONAL,  // enabled -> connected, disabled -> Glass-like
157                                                                         // uses 2 textures, one for frames, second for faces
158         NDT_MESH, // Uses static meshes
159 };
160
161 #define CF_SPECIAL_COUNT 6
162
163 struct ContentFeatures
164 {
165         /*
166                 Cached stuff
167         */
168 #ifndef SERVER
169         // 0     1     2     3     4     5
170         // up    down  right left  back  front
171         TileSpec tiles[6];
172         // Special tiles
173         // - Currently used for flowing liquids
174         TileSpec special_tiles[CF_SPECIAL_COUNT];
175         u8 solidness; // Used when choosing which face is drawn
176         u8 visual_solidness; // When solidness=0, this tells how it looks like
177         bool backface_culling;
178 #endif
179
180         // Server-side cached callback existence for fast skipping
181         bool has_on_construct;
182         bool has_on_destruct;
183         bool has_after_destruct;
184
185         /*
186                 Actual data
187         */
188
189         std::string name; // "" = undefined node
190         ItemGroupList groups; // Same as in itemdef
191
192         // Visual definition
193         enum NodeDrawType drawtype;
194         std::string mesh;
195 #ifndef SERVER
196         scene::IMesh *mesh_ptr[24];
197 #endif
198         float visual_scale; // Misc. scale parameter
199         TileDef tiledef[6];
200         TileDef tiledef_special[CF_SPECIAL_COUNT]; // eg. flowing liquid
201         u8 alpha;
202
203         // Post effect color, drawn when the camera is inside the node.
204         video::SColor post_effect_color;
205         // Type of MapNode::param1
206         ContentParamType param_type;
207         // Type of MapNode::param2
208         ContentParamType2 param_type_2;
209         // True for all ground-like things like stone and mud, false for eg. trees
210         bool is_ground_content;
211         bool light_propagates;
212         bool sunlight_propagates;
213         // This is used for collision detection.
214         // Also for general solidness queries.
215         bool walkable;
216         // Player can point to these
217         bool pointable;
218         // Player can dig these
219         bool diggable;
220         // Player can climb these
221         bool climbable;
222         // Player can build on these
223         bool buildable_to;
224         // Player cannot build to these (placement prediction disabled)
225         bool rightclickable;
226         // Flowing liquid or snow, value = default level
227         u8 leveled;
228         // Whether the node is non-liquid, source liquid or flowing liquid
229         enum LiquidType liquid_type;
230         // If the content is liquid, this is the flowing version of the liquid.
231         std::string liquid_alternative_flowing;
232         // If the content is liquid, this is the source version of the liquid.
233         std::string liquid_alternative_source;
234         // Viscosity for fluid flow, ranging from 1 to 7, with
235         // 1 giving almost instantaneous propagation and 7 being
236         // the slowest possible
237         u8 liquid_viscosity;
238         // Is liquid renewable (new liquid source will be created between 2 existing)
239         bool liquid_renewable;
240         // Number of flowing liquids surrounding source
241         u8 liquid_range;
242         u8 drowning;
243         // Amount of light the node emits
244         u8 light_source;
245         u32 damage_per_second;
246         NodeBox node_box;
247         NodeBox selection_box;
248         NodeBox collision_box;
249         // Used for waving leaves/plants
250         u8 waving;
251         // Compatibility with old maps
252         // Set to true if paramtype used to be 'facedir_simple'
253         bool legacy_facedir_simple;
254         // Set to true if wall_mounted used to be set to true
255         bool legacy_wallmounted;
256
257         // Sound properties
258         SimpleSoundSpec sound_footstep;
259         SimpleSoundSpec sound_dig;
260         SimpleSoundSpec sound_dug;
261
262         /*
263                 Methods
264         */
265
266         ContentFeatures();
267         ~ContentFeatures();
268         void reset();
269         void serialize(std::ostream &os, u16 protocol_version) const;
270         void deSerialize(std::istream &is);
271         void serializeOld(std::ostream &os, u16 protocol_version) const;
272         void deSerializeOld(std::istream &is, int version);
273
274         /*
275                 Some handy methods
276         */
277         bool isLiquid() const{
278                 return (liquid_type != LIQUID_NONE);
279         }
280         bool sameLiquid(const ContentFeatures &f) const{
281                 if(!isLiquid() || !f.isLiquid()) return false;
282                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
283         }
284 };
285
286 class INodeDefManager {
287 public:
288         INodeDefManager(){}
289         virtual ~INodeDefManager(){}
290         // Get node definition
291         virtual const ContentFeatures &get(content_t c) const=0;
292         virtual const ContentFeatures &get(const MapNode &n) const=0;
293         virtual bool getId(const std::string &name, content_t &result) const=0;
294         virtual content_t getId(const std::string &name) const=0;
295         // Allows "group:name" in addition to regular node names
296         virtual void getIds(const std::string &name, std::set<content_t> &result)
297                         const=0;
298         virtual const ContentFeatures &get(const std::string &name) const=0;
299
300         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
301
302         virtual bool getNodeRegistrationStatus() const=0;
303
304         virtual void pendNodeResolve(NodeResolver *nr)=0;
305         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
306 };
307
308 class IWritableNodeDefManager : public INodeDefManager {
309 public:
310         IWritableNodeDefManager(){}
311         virtual ~IWritableNodeDefManager(){}
312         virtual IWritableNodeDefManager* clone()=0;
313         // Get node definition
314         virtual const ContentFeatures &get(content_t c) const=0;
315         virtual const ContentFeatures &get(const MapNode &n) const=0;
316         virtual bool getId(const std::string &name, content_t &result) const=0;
317         // If not found, returns CONTENT_IGNORE
318         virtual content_t getId(const std::string &name) const=0;
319         // Allows "group:name" in addition to regular node names
320         virtual void getIds(const std::string &name, std::set<content_t> &result)
321                 const=0;
322         // If not found, returns the features of CONTENT_UNKNOWN
323         virtual const ContentFeatures &get(const std::string &name) const=0;
324
325         // Register node definition by name (allocate an id)
326         // If returns CONTENT_IGNORE, could not allocate id
327         virtual content_t set(const std::string &name,
328                         const ContentFeatures &def)=0;
329         // If returns CONTENT_IGNORE, could not allocate id
330         virtual content_t allocateDummy(const std::string &name)=0;
331
332         /*
333                 Update item alias mapping.
334                 Call after updating item definitions.
335         */
336         virtual void updateAliases(IItemDefManager *idef)=0;
337
338         /*
339                 Override textures from servers with ones specified in texturepack/override.txt
340         */
341         virtual void applyTextureOverrides(const std::string &override_filepath)=0;
342
343         /*
344                 Update tile textures to latest return values of TextueSource.
345         */
346         virtual void updateTextures(IGameDef *gamedef,
347                 void (*progress_cbk)(void *progress_args, u32 progress, u32 max_progress),
348                 void *progress_cbk_args)=0;
349
350         virtual void serialize(std::ostream &os, u16 protocol_version) const=0;
351         virtual void deSerialize(std::istream &is)=0;
352
353         virtual bool getNodeRegistrationStatus() const=0;
354         virtual void setNodeRegistrationStatus(bool completed)=0;
355
356         virtual void pendNodeResolve(NodeResolver *nr)=0;
357         virtual bool cancelNodeResolveCallback(NodeResolver *nr)=0;
358         virtual void runNodeResolveCallbacks()=0;
359         virtual void resetNodeResolveState()=0;
360 };
361
362 IWritableNodeDefManager *createNodeDefManager();
363
364 class NodeResolver {
365 public:
366         NodeResolver();
367         virtual ~NodeResolver();
368         virtual void resolveNodeNames() = 0;
369
370         bool getIdFromNrBacklog(content_t *result_out,
371                 const std::string &node_alt, content_t c_fallback);
372         bool getIdsFromNrBacklog(std::vector<content_t> *result_out,
373                 bool all_required=false, content_t c_fallback=CONTENT_IGNORE);
374
375         void nodeResolveInternal();
376
377         u32 m_nodenames_idx;
378         u32 m_nnlistsizes_idx;
379         std::vector<std::string> m_nodenames;
380         std::vector<size_t> m_nnlistsizes;
381         INodeDefManager *m_ndef;
382         bool m_resolve_done;
383 };
384
385 #endif