Move ContentFeatures to mapnode_contentfeatures.{h,cpp} and clean stuff
[oweals/minetest.git] / src / mapnode_contentfeatures.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 MAPNODE_CONTENTFEATURES_HEADER
21 #define MAPNODE_CONTENTFEATURES_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include "mapnode.h"
26 #ifndef SERVER
27 #include "tile.h"
28 #endif
29
30 /*
31         Content feature list
32         
33         Used for determining properties of MapNodes by content type without
34         storing such properties in the nodes itself.
35 */
36
37 /*
38         Initialize content feature table.
39
40         Must be called before accessing the table.
41 */
42 void init_contentfeatures();
43
44 enum ContentParamType
45 {
46         CPT_NONE,
47         CPT_LIGHT,
48         CPT_MINERAL,
49         // Direction for chests and furnaces and such
50         CPT_FACEDIR_SIMPLE
51 };
52
53 enum LiquidType
54 {
55         LIQUID_NONE,
56         LIQUID_FLOWING,
57         LIQUID_SOURCE
58 };
59
60 struct MapNode;
61 class NodeMetadata;
62
63 struct ContentFeatures
64 {
65 #ifndef SERVER
66         /*
67                 0: up
68                 1: down
69                 2: right
70                 3: left
71                 4: back
72                 5: front
73         */
74         TileSpec tiles[6];
75         
76         video::ITexture *inventory_texture;
77
78         // Used currently for flowing liquids
79         u8 vertex_alpha;
80         // Post effect color, drawn when the camera is inside the node.
81         video::SColor post_effect_color;
82         // Special irrlicht material, used sometimes
83         video::SMaterial *special_material;
84         video::SMaterial *special_material2;
85         AtlasPointer *special_atlas;
86 #endif
87
88         // List of all block textures that have been used (value is dummy)
89         // Exists on server too for cleaner code in content_mapnode.cpp
90         core::map<std::string, bool> used_texturenames;
91         
92         // Type of MapNode::param1
93         ContentParamType param_type;
94         // True for all ground-like things like stone and mud, false for eg. trees
95         bool is_ground_content;
96         bool light_propagates;
97         bool sunlight_propagates;
98         u8 solidness; // Used when choosing which face is drawn
99         u8 visual_solidness; // When solidness=0, this tells how it looks like
100         // This is used for collision detection.
101         // Also for general solidness queries.
102         bool walkable;
103         // Player can point to these
104         bool pointable;
105         // Player can dig these
106         bool diggable;
107         // Player can climb these
108         bool climbable;
109         // Player can build on these
110         bool buildable_to;
111         // Whether the node has no liquid, source liquid or flowing liquid
112         enum LiquidType liquid_type;
113         // If true, param2 is set to direction when placed. Used for torches.
114         // NOTE: the direction format is quite inefficient and should be changed
115         bool wall_mounted;
116         // If true, node is equivalent to air. Torches are, air is. Water is not.
117         // Is used for example to check whether a mud block can have grass on.
118         bool air_equivalent;
119         // Whether this content type often contains mineral.
120         // Used for texture atlas creation.
121         // Currently only enabled for CONTENT_STONE.
122         bool often_contains_mineral;
123         
124         // Inventory item string as which the node appears in inventory when dug.
125         // Mineral overrides this.
126         std::string dug_item;
127
128         // Extra dug item and its rarity
129         std::string extra_dug_item;
130         s32 extra_dug_item_rarity;
131
132         // Initial metadata is cloned from this
133         NodeMetadata *initial_metadata;
134         
135         // If the content is liquid, this is the flowing version of the liquid.
136         // If content is liquid, this is the same content.
137         content_t liquid_alternative_flowing;
138         // If the content is liquid, this is the source version of the liquid.
139         content_t liquid_alternative_source;
140         // Viscosity for fluid flow, ranging from 1 to 7, with
141         // 1 giving almost instantaneous propagation and 7 being
142         // the slowest possible
143         u8 liquid_viscosity;
144         
145         // Amount of light the node emits
146         u8 light_source;
147         
148         // Digging properties for different tools
149         DiggingPropertiesList digging_properties;
150
151         u32 damage_per_second;
152         
153         // NOTE: Move relevant properties to here from elsewhere
154
155         void reset()
156         {
157 #ifndef SERVER
158                 inventory_texture = NULL;
159                 
160                 vertex_alpha = 255;
161                 post_effect_color = video::SColor(0, 0, 0, 0);
162                 special_material = NULL;
163                 special_material2 = NULL;
164                 special_atlas = NULL;
165 #endif
166                 param_type = CPT_NONE;
167                 is_ground_content = false;
168                 light_propagates = false;
169                 sunlight_propagates = false;
170                 solidness = 2;
171                 visual_solidness = 0;
172                 walkable = true;
173                 pointable = true;
174                 diggable = true;
175                 climbable = false;
176                 buildable_to = false;
177                 liquid_type = LIQUID_NONE;
178                 wall_mounted = false;
179                 air_equivalent = false;
180                 often_contains_mineral = false;
181                 dug_item = "";
182                 initial_metadata = NULL;
183                 liquid_alternative_flowing = CONTENT_IGNORE;
184                 liquid_alternative_source = CONTENT_IGNORE;
185                 liquid_viscosity = 0;
186                 light_source = 0;
187                 digging_properties.clear();
188                 damage_per_second = 0;
189         }
190
191         ContentFeatures()
192         {
193                 reset();
194         }
195
196         ~ContentFeatures();
197         
198         /*
199                 Quickhands for simple materials
200         */
201         
202 #ifdef SERVER
203         void setTexture(u16 i, std::string name, u8 alpha=255)
204         {}
205         void setAllTextures(std::string name, u8 alpha=255)
206         {}
207 #else
208         void setTexture(u16 i, std::string name, u8 alpha=255);
209
210         void setAllTextures(std::string name, u8 alpha=255)
211         {
212                 for(u16 i=0; i<6; i++)
213                 {
214                         setTexture(i, name, alpha);
215                 }
216                 // Force inventory texture too
217                 setInventoryTexture(name);
218         }
219 #endif
220
221 #ifndef SERVER
222         void setTile(u16 i, const TileSpec &tile)
223         {
224                 tiles[i] = tile;
225         }
226         void setAllTiles(const TileSpec &tile)
227         {
228                 for(u16 i=0; i<6; i++)
229                 {
230                         setTile(i, tile);
231                 }
232         }
233 #endif
234
235 #ifdef SERVER
236         void setInventoryTexture(std::string imgname)
237         {}
238         void setInventoryTextureCube(std::string top,
239                         std::string left, std::string right)
240         {}
241 #else
242         void setInventoryTexture(std::string imgname);
243         
244         void setInventoryTextureCube(std::string top,
245                         std::string left, std::string right);
246 #endif
247 };
248
249 /*
250         Call this to access the ContentFeature list
251 */
252 ContentFeatures & content_features(content_t i);
253 ContentFeatures & content_features(MapNode &n);
254
255 /*
256         Here is a bunch of DEPRECATED functions.
257 */
258
259 /*
260         If true, the material allows light propagation and brightness is stored
261         in param.
262         NOTE: Don't use, use "content_features(m).whatever" instead
263 */
264 inline bool light_propagates_content(content_t m)
265 {
266         return content_features(m).light_propagates;
267 }
268 /*
269         If true, the material allows lossless sunlight propagation.
270         NOTE: It doesn't seem to go through torches regardlessly of this
271         NOTE: Don't use, use "content_features(m).whatever" instead
272 */
273 inline bool sunlight_propagates_content(content_t m)
274 {
275         return content_features(m).sunlight_propagates;
276 }
277 /*
278         On a node-node surface, the material of the node with higher solidness
279         is used for drawing.
280         0: Invisible
281         1: Transparent
282         2: Opaque
283         NOTE: Don't use, use "content_features(m).whatever" instead
284 */
285 inline u8 content_solidness(content_t m)
286 {
287         return content_features(m).solidness;
288 }
289 // Objects collide with walkable contents
290 // NOTE: Don't use, use "content_features(m).whatever" instead
291 inline bool content_walkable(content_t m)
292 {
293         return content_features(m).walkable;
294 }
295 // NOTE: Don't use, use "content_features(m).whatever" instead
296 inline bool content_liquid(content_t m)
297 {
298         return content_features(m).liquid_type != LIQUID_NONE;
299 }
300 // NOTE: Don't use, use "content_features(m).whatever" instead
301 inline bool content_flowing_liquid(content_t m)
302 {
303         return content_features(m).liquid_type == LIQUID_FLOWING;
304 }
305 // NOTE: Don't use, use "content_features(m).whatever" instead
306 inline bool content_liquid_source(content_t m)
307 {
308         return content_features(m).liquid_type == LIQUID_SOURCE;
309 }
310 // CONTENT_WATER || CONTENT_WATERSOURCE -> CONTENT_WATER
311 // CONTENT_LAVA || CONTENT_LAVASOURCE -> CONTENT_LAVA
312 // NOTE: Don't use, use "content_features(m).whatever" instead
313 inline content_t make_liquid_flowing(content_t m)
314 {
315         u8 c = content_features(m).liquid_alternative_flowing;
316         assert(c != CONTENT_IGNORE);
317         return c;
318 }
319 // Pointable contents can be pointed to in the map
320 // NOTE: Don't use, use "content_features(m).whatever" instead
321 inline bool content_pointable(content_t m)
322 {
323         return content_features(m).pointable;
324 }
325 // NOTE: Don't use, use "content_features(m).whatever" instead
326 inline bool content_diggable(content_t m)
327 {
328         return content_features(m).diggable;
329 }
330 // NOTE: Don't use, use "content_features(m).whatever" instead
331 inline bool content_buildable_to(content_t m)
332 {
333         return content_features(m).buildable_to;
334 }
335
336
337 #endif
338