Add node definitions in Lua and do not use content_mapnode_init anymore (except in...
[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 <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         // Whether this content type often contains mineral.
178         // Used for texture atlas creation.
179         // Currently only enabled for CONTENT_STONE.
180         bool often_contains_mineral;
181         // Inventory item string as which the node appears in inventory when dug.
182         // Mineral overrides this.
183         std::string dug_item;
184         // Extra dug item and its rarity
185         std::string extra_dug_item;
186         // Usual get interval for extra dug item
187         s32 extra_dug_item_rarity;
188         // Metadata name of node (eg. "furnace")
189         std::string metadata_name;
190         // Whether the node is non-liquid, source liquid or flowing liquid
191         enum LiquidType liquid_type;
192         // If the content is liquid, this is the flowing version of the liquid.
193         std::string liquid_alternative_flowing;
194         // If the content is liquid, this is the source version of the liquid.
195         std::string liquid_alternative_source;
196         // Viscosity for fluid flow, ranging from 1 to 7, with
197         // 1 giving almost instantaneous propagation and 7 being
198         // the slowest possible
199         u8 liquid_viscosity;
200         // Amount of light the node emits
201         u8 light_source;
202         u32 damage_per_second;
203         NodeBox selection_box;
204         MaterialProperties material;
205         std::string cookresult_item;
206         float furnace_cooktime;
207         float furnace_burntime;
208
209         /*
210                 Methods
211         */
212         
213         ContentFeatures();
214         ~ContentFeatures();
215         void reset();
216         void serialize(std::ostream &os);
217         void deSerialize(std::istream &is, IGameDef *gamedef);
218
219         /*
220                 Texture setters.
221                 
222         */
223         
224         // Texture setters. They also add stuff to used_texturenames.
225         void setTexture(u16 i, std::string name);
226         void setAllTextures(std::string name);
227         void setSpecialMaterial(u16 i, const MaterialSpec &mspec);
228
229         void setInventoryTexture(std::string imgname);
230         void setInventoryTextureCube(std::string top,
231                         std::string left, std::string right);
232         
233         /*
234                 Some handy methods
235         */
236         bool isLiquid() const{
237                 return (liquid_type != LIQUID_NONE);
238         }
239         bool sameLiquid(const ContentFeatures &f) const{
240                 if(!isLiquid() || !f.isLiquid()) return false;
241                 return (liquid_alternative_flowing == f.liquid_alternative_flowing);
242         }
243 };
244
245 class INodeDefManager
246 {
247 public:
248         INodeDefManager(){}
249         virtual ~INodeDefManager(){}
250         // Get node definition
251         virtual const ContentFeatures& get(content_t c) const=0;
252         virtual const ContentFeatures& get(const MapNode &n) const=0;
253         virtual bool getId(const std::string &name, content_t &result) const=0;
254         virtual content_t getId(const std::string &name) const=0;
255         virtual const ContentFeatures& get(const std::string &name) const=0;
256         
257         virtual void serialize(std::ostream &os)=0;
258 };
259
260 class IWritableNodeDefManager : public INodeDefManager
261 {
262 public:
263         IWritableNodeDefManager(){}
264         virtual ~IWritableNodeDefManager(){}
265         virtual IWritableNodeDefManager* clone()=0;
266         // Get node definition
267         virtual const ContentFeatures& get(content_t c) const=0;
268         virtual const ContentFeatures& get(const MapNode &n) const=0;
269         virtual bool getId(const std::string &name, content_t &result) const=0;
270         virtual content_t getId(const std::string &name) const=0;
271         // If not found, returns the features of CONTENT_IGNORE
272         virtual const ContentFeatures& get(const std::string &name) const=0;
273                 
274         // Register node definition
275         virtual void set(content_t c, const ContentFeatures &def)=0;
276         // Register node definition by name (allocate an id)
277         // If returns CONTENT_IGNORE, could not allocate id
278         virtual content_t set(const std::string &name,
279                         const ContentFeatures &def)=0;
280         // If returns CONTENT_IGNORE, could not allocate id
281         virtual content_t allocateDummy(const std::string &name)=0;
282
283         /*
284                 Update tile textures to latest return values of TextueSource.
285                 Call after updating the texture atlas of a TextureSource.
286         */
287         virtual void updateTextures(ITextureSource *tsrc)=0;
288
289         virtual void serialize(std::ostream &os)=0;
290         virtual void deSerialize(std::istream &is, IGameDef *gamedef)=0;
291 };
292
293 IWritableNodeDefManager* createNodeDefManager();
294
295 #endif
296