bug-fixin'
[oweals/minetest.git] / src / mapnode.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 #include "mapnode.h"
21 #include "tile.h"
22 #include "porting.h"
23 #include <string>
24 #include "mineral.h"
25 // For g_settings
26 #include "main.h"
27
28 ContentFeatures::~ContentFeatures()
29 {
30         if(translate_to)
31                 delete translate_to;
32 }
33
34 struct ContentFeatures g_content_features[256];
35
36 ContentFeatures & content_features(u8 i)
37 {
38         return g_content_features[i];
39 }
40
41 void init_mapnode(IIrrlichtWrapper *irrlicht)
42 {
43         bool new_style_water = g_settings.getBool("new_style_water");
44         bool new_style_leaves = g_settings.getBool("new_style_leaves");
45         
46         u8 i;
47         ContentFeatures *f = NULL;
48
49         i = CONTENT_STONE;
50         f = &g_content_features[i];
51         f->setAllTextures(irrlicht->getTextureId("stone.png"));
52         f->param_type = CPT_MINERAL;
53         f->is_ground_content = true;
54         
55         i = CONTENT_GRASS;
56         f = &g_content_features[i];
57         f->setAllTextures(TextureSpec(irrlicht->getTextureId("mud.png"),
58                         irrlicht->getTextureId("grass_side.png")));
59         f->setTexture(0, irrlicht->getTextureId("grass.png"));
60         f->setTexture(1, irrlicht->getTextureId("mud.png"));
61         f->setInventoryTexture(irrlicht->getTextureId("grass.png"));
62         f->param_type = CPT_MINERAL;
63         f->is_ground_content = true;
64         
65         i = CONTENT_GRASS_FOOTSTEPS;
66         f = &g_content_features[i];
67         f->setAllTextures(TextureSpec(irrlicht->getTextureId("mud.png"),
68                         irrlicht->getTextureId("grass_side.png")));
69         f->setTexture(0, irrlicht->getTextureId("grass_footsteps.png"));
70         f->setTexture(1, irrlicht->getTextureId("mud.png"));
71         f->setInventoryTexture(irrlicht->getTextureId("grass_footsteps.png"));
72         f->param_type = CPT_MINERAL;
73         f->is_ground_content = true;
74         
75         i = CONTENT_MUD;
76         f = &g_content_features[i];
77         f->setAllTextures(irrlicht->getTextureId("mud.png"));
78         f->param_type = CPT_MINERAL;
79         f->is_ground_content = true;
80         
81         i = CONTENT_SAND;
82         f = &g_content_features[i];
83         f->setAllTextures(irrlicht->getTextureId("sand.png"));
84         f->param_type = CPT_MINERAL;
85         f->is_ground_content = true;
86         
87         i = CONTENT_TREE;
88         f = &g_content_features[i];
89         f->setAllTextures(irrlicht->getTextureId("tree.png"));
90         f->setTexture(0, irrlicht->getTextureId("tree_top.png"));
91         f->setTexture(1, irrlicht->getTextureId("tree_top.png"));
92         f->setInventoryTexture(irrlicht->getTextureId("tree_top.png"));
93         f->param_type = CPT_MINERAL;
94         f->is_ground_content = true;
95         
96         i = CONTENT_LEAVES;
97         f = &g_content_features[i];
98         f->light_propagates = true;
99         //f->param_type = CPT_MINERAL;
100         f->param_type = CPT_LIGHT;
101         f->is_ground_content = true;
102         if(new_style_leaves)
103         {
104                 f->solidness = 0; // drawn separately, makes no faces
105         }
106         else
107         {
108                 f->setAllTextures(irrlicht->getTextureId("leaves.png"));
109         }
110         /*{
111                 TileSpec t;
112                 t.spec = TextureSpec(irrlicht->getTextureId("leaves.png"));
113                 //t.material_type = MATERIAL_ALPHA_SIMPLE;
114                 //t.material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
115                 f->setAllTiles(t);
116         }*/
117         
118         i = CONTENT_COALSTONE;
119         f = &g_content_features[i];
120         //f->translate_to = new MapNode(CONTENT_STONE, MINERAL_COAL);
121         f->setAllTextures(TextureSpec(irrlicht->getTextureId("coal.png"),
122                         irrlicht->getTextureId("mineral_coal.png")));
123         f->is_ground_content = true;
124         
125         i = CONTENT_WOOD;
126         f = &g_content_features[i];
127         f->setAllTextures(irrlicht->getTextureId("wood.png"));
128         f->is_ground_content = true;
129         
130         i = CONTENT_MESE;
131         f = &g_content_features[i];
132         f->setAllTextures(irrlicht->getTextureId("mese.png"));
133         f->is_ground_content = true;
134         
135         i = CONTENT_CLOUD;
136         f = &g_content_features[i];
137         f->setAllTextures(irrlicht->getTextureId("cloud.png"));
138         f->is_ground_content = true;
139         
140         i = CONTENT_AIR;
141         f = &g_content_features[i];
142         f->param_type = CPT_LIGHT;
143         f->light_propagates = true;
144         f->sunlight_propagates = true;
145         f->solidness = 0;
146         f->walkable = false;
147         f->pointable = false;
148         f->diggable = false;
149         f->buildable_to = true;
150         
151         i = CONTENT_WATER;
152         f = &g_content_features[i];
153         f->setInventoryTexture(irrlicht->getTextureId("water.png"));
154         f->param_type = CPT_LIGHT;
155         f->light_propagates = true;
156         f->solidness = 0; // Drawn separately, makes no faces
157         f->walkable = false;
158         f->pointable = false;
159         f->diggable = false;
160         f->buildable_to = true;
161         f->liquid_type = LIQUID_FLOWING;
162         
163         i = CONTENT_WATERSOURCE;
164         f = &g_content_features[i];
165         f->setInventoryTexture(irrlicht->getTextureId("water.png"));
166         if(new_style_water)
167         {
168                 f->solidness = 0; // drawn separately, makes no faces
169         }
170         else // old style
171         {
172                 f->setAllTextures(irrlicht->getTextureId("water.png"), WATER_ALPHA);
173                 TileSpec t;
174                 t.spec = TextureSpec(irrlicht->getTextureId("water.png"));
175                 t.alpha = WATER_ALPHA;
176                 t.material_type = MATERIAL_ALPHA_VERTEX;
177                 t.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
178                 f->setAllTiles(t);
179                 f->solidness = 1;
180         }
181         f->param_type = CPT_LIGHT;
182         f->light_propagates = true;
183         f->walkable = false;
184         f->pointable = false;
185         f->diggable = false;
186         f->buildable_to = true;
187         f->liquid_type = LIQUID_SOURCE;
188         
189         i = CONTENT_TORCH;
190         f = &g_content_features[i];
191         f->setInventoryTexture(irrlicht->getTextureId("torch_on_floor.png"));
192         f->param_type = CPT_LIGHT;
193         f->light_propagates = true;
194         f->solidness = 0; // drawn separately, makes no faces
195         f->walkable = false;
196         f->wall_mounted = true;
197         
198 }
199
200 TileSpec MapNode::getTile(v3s16 dir)
201 {
202         TileSpec spec;
203         
204         s32 dir_i = -1;
205         
206         if(dir == v3s16(0,0,0))
207                 dir_i = -1;
208         else if(dir == v3s16(0,1,0))
209                 dir_i = 0;
210         else if(dir == v3s16(0,-1,0))
211                 dir_i = 1;
212         else if(dir == v3s16(1,0,0))
213                 dir_i = 2;
214         else if(dir == v3s16(-1,0,0))
215                 dir_i = 3;
216         else if(dir == v3s16(0,0,1))
217                 dir_i = 4;
218         else if(dir == v3s16(0,0,-1))
219                 dir_i = 5;
220         
221         if(dir_i == -1)
222                 // Non-directional
223                 spec = content_features(d).tiles[0];
224         else 
225                 spec = content_features(d).tiles[dir_i];
226         
227         if(content_features(d).param_type == CPT_MINERAL)
228         {
229                 u8 mineral = param & 0x1f;
230                 // Add mineral block texture
231                 textureid_t tid = mineral_block_texture(mineral);
232                 if(tid != 0)
233                         spec.spec.addTid(tid);
234         }
235
236         return spec;
237 }
238
239 u8 MapNode::getMineral()
240 {
241         if(content_features(d).param_type == CPT_MINERAL)
242         {
243                 return param & 0x1f;
244         }
245
246         return MINERAL_NONE;
247 }
248
249 // Pointers to c_str()s g_content_features[i].inventory_image_path
250 //const char * g_content_inventory_texture_paths[USEFUL_CONTENT_COUNT] = {0};
251
252 void init_content_inventory_texture_paths()
253 {
254         dstream<<"DEPRECATED "<<__FUNCTION_NAME<<std::endl;
255         /*for(u16 i=0; i<USEFUL_CONTENT_COUNT; i++)
256         {
257                 g_content_inventory_texture_paths[i] =
258                                 g_content_features[i].inventory_image_path.c_str();
259         }*/
260 }
261