]> git.lizzy.rs Git - dragonfireclient.git/blob - src/nodedef.h
Completely generalized mesh generation; ContentFeatures serialization
[dragonfireclient.git] / src / nodedef.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
24 #include <string>
25 #include <iostream>
26 #include <set>
27 #include "mapnode.h"
28 #ifndef SERVER
29 #include "tile.h"
30 #endif
31 #include "materials.h" // MaterialProperties
32 class ITextureSource;
33 class IGameDef;
34
35 /*
36         TODO: Rename to nodedef.h
37 */
38
39 #if 0
40
41 /*
42         Content feature list
43         
44         Used for determining properties of MapNodes by content type without
45         storing such properties in the nodes itself.
46 */
47
48 /*
49         Initialize content feature table.
50
51         Must be called before accessing the table.
52 */
53 void init_contentfeatures(ITextureSource *tsrc);
54
55 #endif
56
57 enum ContentParamType
58 {
59         CPT_NONE,
60         CPT_LIGHT,
61         CPT_MINERAL,
62         // Direction for chests and furnaces and such
63         CPT_FACEDIR_SIMPLE
64 };
65
66 enum LiquidType
67 {
68         LIQUID_NONE,
69         LIQUID_FLOWING,
70         LIQUID_SOURCE
71 };
72
73 enum NodeBoxType
74 {
75         NODEBOX_REGULAR, // Regular block; allows buildable_to
76         NODEBOX_FIXED, // Static separately defined box
77         NODEBOX_WALLMOUNTED, // Box for wall_mounted nodes; (top, bottom, side)
78 };
79
80 struct NodeBox
81 {
82         enum NodeBoxType type;
83         // NODEBOX_REGULAR (no parameters)
84         // NODEBOX_FIXED
85         core::aabbox3d<f32> fixed;
86         // NODEBOX_WALLMOUNTED
87         core::aabbox3d<f32> wall_top;
88         core::aabbox3d<f32> wall_bottom;
89         core::aabbox3d<f32> wall_side; // being at the -X side
90
91         NodeBox():
92                 type(NODEBOX_REGULAR),
93                 // default is rail-like
94                 fixed(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
95                 // default is sign/ladder-like
96                 wall_top(-BS/2, BS/2-BS/16., -BS/2, BS/2, BS/2, BS/2),
97                 wall_bottom(-BS/2, -BS/2, -BS/2, BS/2, -BS/2+BS/16., BS/2),
98                 wall_side(-BS/2, -BS/2, -BS/2, -BS/2+BS/16., BS/2, BS/2)
99         {}
100
101         void serialize(std::ostream &os);
102         void deSerialize(std::istream &is);
103 };
104
105 struct MapNode;
106 class NodeMetadata;
107
108 struct MaterialSpec
109 {
110         std::string tname;
111         bool backface_culling;
112         
113         MaterialSpec(const std::string &tname_="", bool backface_culling_=true):
114                 tname(tname_),
115                 backface_culling(backface_culling_)
116         {}
117
118         void serialize(std::ostream &os);
119         void deSerialize(std::istream &is);
120 };
121
122 enum NodeDrawType
123 {
124         NDT_NORMAL, // A basic solid block
125         NDT_AIRLIKE, // Nothing is drawn
126         NDT_LIQUID, // Do not draw face towards same kind of flowing/source liquid
127         NDT_FLOWINGLIQUID, // A very special kind of thing
128         NDT_GLASSLIKE, // Glass-like, don't draw faces towards other glass
129         NDT_ALLFACES, // Leaves-like, draw all faces no matter what
130         NDT_ALLFACES_OPTIONAL, // Fancy -> allfaces, fast -> normal
131         NDT_TORCHLIKE,
132         NDT_SIGNLIKE,
133         NDT_PLANTLIKE,
134         NDT_FENCELIKE,
135         NDT_RAILLIKE,
136 };
137
138 #define CF_SPECIAL_COUNT 2
139
140 struct ContentFeatures
141 {
142         /*
143                 Cached stuff
144         */
145 #ifndef SERVER
146         // 0     1     2     3     4     5
147         // up    down  right left  back  front 
148         TileSpec tiles[6];
149         video::ITexture *inventory_texture;
150         // Special material/texture
151         // - Currently used for flowing liquids
152         video::SMaterial *special_materials[CF_SPECIAL_COUNT];
153         AtlasPointer *special_aps[CF_SPECIAL_COUNT];
154         u8 solidness; // Used when choosing which face is drawn
155         u8 visual_solidness; // When solidness=0, this tells how it looks like
156         bool backface_culling;
157 #endif
158         
159         // List of all block textures that have been used (value is dummy)
160         // Used for texture atlas making.
161         // Exists on server too for cleaner code in content_mapnode.cpp.
162         std::set<std::string> used_texturenames;
163         
164         // True if this actually contains non-default data
165         bool modified;
166
167         /*
168                 Actual data
169         */
170
171         // Visual definition
172         enum NodeDrawType drawtype;
173         float visual_scale; // Misc. scale parameter
174         std::string tname_tiles[6];
175         std::string tname_inventory;
176         MaterialSpec mspec_special[CF_SPECIAL_COUNT];
177         u8 alpha;
178
179         // Post effect color, drawn when the camera is inside the node.
180         video::SColor post_effect_color;
181         // Type of MapNode::param1
182         ContentParamType param_type;
183         // True for all ground-like things like stone and mud, false for eg. trees
184         bool is_ground_content;
185         bool light_propagates;
186         bool sunlight_propagates;
187         // This is used for collision detection.
188         // Also for general solidness queries.
189         bool walkable;
190         // Player can point to these
191         bool pointable;
192         // Player can dig these
193         bool diggable;
194         // Player can climb these
195         bool climbable;
196         // Player can build on these
197         bool buildable_to;
198         // If true, param2 is set to direction when placed. Used for torches.
199         // NOTE: the direction format is quite inefficient and should be changed
200         bool wall_mounted;
201         // If true, node is equivalent to air. Torches are, air is. Water is not.
202         // Is used for example to check whether a mud block can have grass on.
203         bool air_equivalent;
204         // Whether this content type often contains mineral.
205         // Used for texture atlas creation.
206         // Currently only enabled for CONTENT_STONE.
207         bool often_contains_mineral;
208         // Inventory item string as which the node appears in inventory when dug.
209         // Mineral overrides this.
210         std::string dug_item;
211         // Extra dug item and its rarity
212         std::string extra_dug_item;
213         s32 extra_dug_item_rarity;
214         // Initial metadata is cloned from this
215         NodeMetadata *initial_metadata;
216         // Whether the node is non-liquid, source liquid or flowing liquid
217         enum LiquidType liquid_type;
218         // If the content is liquid, this is the flowing version of the liquid.
219         content_t liquid_alternative_flowing;
220         // If the content is liquid, this is the source version of the liquid.
221         content_t liquid_alternative_source;
222         // Viscosity for fluid flow, ranging from 1 to 7, with
223         // 1 giving almost instantaneous propagation and 7 being
224         // the slowest possible
225         u8 liquid_viscosity;
226         // Amount of light the node emits
227         u8 light_source;
228         u32 damage_per_second;
229         NodeBox selection_box;
230         MaterialProperties material;
231
232         /*
233                 Methods
234         */
235         
236         ContentFeatures();
237         ~ContentFeatures();
238         void reset();
239         void serialize(std::ostream &os);
240         void deSerialize(std::istream &is, IGameDef *gamedef);
241
242         /*
243                 Quickhands for simple materials
244         */
245         
246         void setTexture(u16 i, std::string name);
247
248         void setAllTextures(std::string name, u8 alpha=255)
249         {
250                 for(u16 i=0; i<6; i++)
251                         setTexture(i, name);
252                 alpha = alpha;
253                 // Force inventory texture too
254                 setInventoryTexture(name);
255         }
256
257         void setInventoryTexture(std::string imgname);
258         void setInventoryTextureCube(std::string top,
259                         std::string left, std::string right);
260
261         /*
262                 Some handy methods
263         */
264         bool isLiquid() const{
265                 return (liquid_type != LIQUID_NONE);
266         }
267         bool sameLiquid(const ContentFeatures &f) const{
268                 if(!isLiquid() || !f.isLiquid()) return false;
269                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
270         }
271 };
272
273 class INodeDefManager
274 {
275 public:
276         INodeDefManager(){}
277         virtual ~INodeDefManager(){}
278         // Get node definition
279         virtual const ContentFeatures& get(content_t c) const=0;
280         virtual const ContentFeatures& get(const MapNode &n) const=0;
281 };
282
283 class IWritableNodeDefManager : public INodeDefManager
284 {
285 public:
286         IWritableNodeDefManager(){}
287         virtual ~IWritableNodeDefManager(){}
288         virtual IWritableNodeDefManager* clone()=0;
289         // Get node definition
290         virtual const ContentFeatures& get(content_t c) const=0;
291         virtual const ContentFeatures& get(const MapNode &n) const=0;
292                 
293         // Register node definition
294         virtual void set(content_t c, const ContentFeatures &def)=0;
295         virtual ContentFeatures* getModifiable(content_t c)=0;
296
297         /*
298                 Update tile textures to latest return values of TextueSource.
299                 Call after updating the texture atlas of a TextureSource.
300         */
301         virtual void updateTextures(ITextureSource *tsrc)=0;
302
303         virtual void serialize(std::ostream &os)=0;
304         virtual void deSerialize(std::istream &is, IGameDef *gamedef)=0;
305 };
306
307 IWritableNodeDefManager* createNodeDefManager();
308
309 #endif
310