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