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