merged the content type extension and delta
[oweals/minetest.git] / src / content_mapnode.cpp
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 // For g_settings
21 #include "main.h"
22
23 #include "content_mapnode.h"
24 #include "mapnode.h"
25 #include "content_nodemeta.h"
26
27 // TODO: Get rid of these and set up some attributes like toughness,
28 //       fluffyness, and a funciton to calculate time and durability loss
29 //       (and sound? and whatever else) from them
30 void setStoneLikeDiggingProperties(DiggingPropertiesList &list, float toughness);
31 void setDirtLikeDiggingProperties(DiggingPropertiesList &list, float toughness);
32 void setWoodLikeDiggingProperties(DiggingPropertiesList &list, float toughness);
33
34 content_t trans_table_19[][2] = {
35         {CONTENT_GRASS, 1},
36         {CONTENT_TREE, 4},
37         {CONTENT_LEAVES, 5},
38         {CONTENT_GRASS_FOOTSTEPS, 6},
39         {CONTENT_MESE, 7},
40         {CONTENT_MUD, 8},
41         {CONTENT_CLOUD, 10},
42         {CONTENT_COALSTONE, 11},
43         {CONTENT_WOOD, 12},
44         {CONTENT_SAND, 13},
45         {CONTENT_COBBLE, 18},
46         {CONTENT_STEEL, 19},
47         {CONTENT_GLASS, 20},
48         {CONTENT_MOSSYCOBBLE, 22},
49         {CONTENT_GRAVEL, 23},
50         {CONTENT_SANDSTONE, 24},
51         {CONTENT_CACTUS, 25},
52         {CONTENT_BRICK, 26},
53         {CONTENT_CLAY, 27},
54         {CONTENT_PAPYRUS, 28},
55         {CONTENT_BOOKSHELF, 29},
56 };
57
58 MapNode mapnode_translate_from_internal(MapNode n_from, u8 version)
59 {
60         MapNode result = n_from;
61         if(version <= 19)
62         {
63                 content_t c_from = n_from.getContent();
64                 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
65                 {
66                         if(trans_table_19[i][0] == c_from)
67                         {
68                                 result.setContent(trans_table_19[i][1]);
69                                 break;
70                         }
71                 }
72         }
73         return result;
74 }
75 MapNode mapnode_translate_to_internal(MapNode n_from, u8 version)
76 {
77         MapNode result = n_from;
78         if(version <= 19)
79         {
80                 content_t c_from = n_from.getContent();
81                 for(u32 i=0; i<sizeof(trans_table_19)/sizeof(trans_table_19[0]); i++)
82                 {
83                         if(trans_table_19[i][1] == c_from)
84                         {
85                                 result.setContent(trans_table_19[i][0]);
86                                 break;
87                         }
88                 }
89         }
90         return result;
91 }
92
93 void content_mapnode_init()
94 {
95         // Read some settings
96         bool new_style_water = g_settings.getBool("new_style_water");
97         bool new_style_leaves = g_settings.getBool("new_style_leaves");
98         bool invisible_stone = g_settings.getBool("invisible_stone");
99
100         content_t i;
101         ContentFeatures *f = NULL;
102
103         i = CONTENT_STONE;
104         f = &content_features(i);
105         f->setAllTextures("stone.png");
106         f->setInventoryTextureCube("stone.png", "stone.png", "stone.png");
107         f->param_type = CPT_MINERAL;
108         f->is_ground_content = true;
109         f->dug_item = std::string("MaterialItem ")+itos(CONTENT_COBBLE)+" 1";
110         setStoneLikeDiggingProperties(f->digging_properties, 1.0);
111         if(invisible_stone)
112                 f->solidness = 0; // For debugging, hides regular stone
113         
114         i = CONTENT_GRASS;
115         f = &content_features(i);
116         f->setAllTextures("mud.png^grass_side.png");
117         f->setTexture(0, "grass.png");
118         f->setTexture(1, "mud.png");
119         f->param_type = CPT_MINERAL;
120         f->is_ground_content = true;
121         f->dug_item = std::string("MaterialItem ")+itos(CONTENT_MUD)+" 1";
122         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
123         
124         i = CONTENT_GRASS_FOOTSTEPS;
125         f = &content_features(i);
126         f->setAllTextures("mud.png^grass_side.png");
127         f->setTexture(0, "grass_footsteps.png");
128         f->setTexture(1, "mud.png");
129         f->param_type = CPT_MINERAL;
130         f->is_ground_content = true;
131         f->dug_item = std::string("MaterialItem ")+itos(CONTENT_MUD)+" 1";
132         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
133         
134         i = CONTENT_MUD;
135         f = &content_features(i);
136         f->setAllTextures("mud.png");
137         f->setInventoryTextureCube("mud.png", "mud.png", "mud.png");
138         f->param_type = CPT_MINERAL;
139         f->is_ground_content = true;
140         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
141         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
142         
143         i = CONTENT_SAND;
144         f = &content_features(i);
145         f->setAllTextures("sand.png");
146         f->setInventoryTextureCube("sand.png", "sand.png", "sand.png");
147         f->param_type = CPT_MINERAL;
148         f->is_ground_content = true;
149         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
150         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
151         
152         i = CONTENT_GRAVEL;
153         f = &content_features(i);
154         f->setAllTextures("gravel.png");
155         f->setInventoryTextureCube("gravel.png", "gravel.png", "gravel.png");
156         f->param_type = CPT_MINERAL;
157         f->is_ground_content = true;
158         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
159         setDirtLikeDiggingProperties(f->digging_properties, 1.75);
160         
161         i = CONTENT_SANDSTONE;
162         f = &content_features(i);
163         f->setAllTextures("sandstone.png");
164         f->setInventoryTextureCube("sandstone.png", "sandstone.png", "sandstone.png");
165         f->param_type = CPT_MINERAL;
166         f->is_ground_content = true;
167         f->dug_item = std::string("MaterialItem ")+itos(CONTENT_SAND)+" 1";
168         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
169
170         i = CONTENT_CLAY;
171         f = &content_features(i);
172         f->setAllTextures("clay.png");
173         f->setInventoryTextureCube("clay.png", "clay.png", "clay.png");
174         f->param_type = CPT_MINERAL;
175         f->is_ground_content = true;
176         f->dug_item = std::string("CraftItem lump_of_clay 4");
177         setDirtLikeDiggingProperties(f->digging_properties, 1.0);
178
179         i = CONTENT_BRICK;
180         f = &content_features(i);
181         f->setAllTextures("brick.png");
182         f->setInventoryTextureCube("brick.png", "brick.png", "brick.png");
183         f->param_type = CPT_MINERAL;
184         f->is_ground_content = true;
185         f->dug_item = std::string("CraftItem clay_brick 4");
186         setStoneLikeDiggingProperties(f->digging_properties, 1.0);
187
188         i = CONTENT_TREE;
189         f = &content_features(i);
190         f->setAllTextures("tree.png");
191         f->setTexture(0, "tree_top.png");
192         f->setTexture(1, "tree_top.png");
193         f->param_type = CPT_MINERAL;
194         f->is_ground_content = true;
195         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
196         setWoodLikeDiggingProperties(f->digging_properties, 1.0);
197         
198         i = CONTENT_LEAVES;
199         f = &content_features(i);
200         f->light_propagates = true;
201         //f->param_type = CPT_MINERAL;
202         f->param_type = CPT_LIGHT;
203         f->is_ground_content = true;
204         if(new_style_leaves)
205         {
206                 f->solidness = 0; // drawn separately, makes no faces
207                 f->setInventoryTextureCube("leaves.png", "leaves.png", "leaves.png");
208         }
209         else
210         {
211                 f->setAllTextures("[noalpha:leaves.png");
212         }
213         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
214         setWoodLikeDiggingProperties(f->digging_properties, 0.15);
215
216         i = CONTENT_CACTUS;
217         f = &content_features(i);
218         f->setAllTextures("cactus_side.png");
219         f->setTexture(0, "cactus_top.png");
220         f->setTexture(1, "cactus_top.png");
221         f->setInventoryTextureCube("cactus_top.png", "cactus_side.png", "cactus_side.png");
222         f->param_type = CPT_MINERAL;
223         f->is_ground_content = true;
224         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
225         setWoodLikeDiggingProperties(f->digging_properties, 0.75);
226
227         i = CONTENT_PAPYRUS;
228         f = &content_features(i);
229         f->setInventoryTexture("papyrus.png");
230         f->light_propagates = true;
231         f->param_type = CPT_LIGHT;
232         f->is_ground_content = true;
233         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
234         f->solidness = 0; // drawn separately, makes no faces
235         f->walkable = false;
236         setWoodLikeDiggingProperties(f->digging_properties, 0.25);
237
238         i = CONTENT_BOOKSHELF;
239         f = &content_features(i);
240         f->setAllTextures("bookshelf.png");
241         f->setTexture(0, "wood.png");
242         f->setTexture(1, "wood.png");
243         // FIXME: setInventoryTextureCube() only cares for the first texture
244         f->setInventoryTextureCube("bookshelf.png", "bookshelf.png", "bookshelf.png");
245         //f->setInventoryTextureCube("wood.png", "bookshelf.png", "bookshelf.png");
246         f->param_type = CPT_MINERAL;
247         f->is_ground_content = true;
248         setWoodLikeDiggingProperties(f->digging_properties, 0.75);
249
250         i = CONTENT_GLASS;
251         f = &content_features(i);
252         f->light_propagates = true;
253         f->param_type = CPT_LIGHT;
254         f->is_ground_content = true;
255         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
256         f->solidness = 0; // drawn separately, makes no faces
257         f->setInventoryTextureCube("glass.png", "glass.png", "glass.png");
258         setWoodLikeDiggingProperties(f->digging_properties, 0.15);
259
260         i = CONTENT_FENCE;
261         f = &content_features(i);
262         f->light_propagates = true;
263         f->param_type = CPT_LIGHT;
264         f->is_ground_content = true;
265         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
266         f->solidness = 0; // drawn separately, makes no faces
267         f->air_equivalent = true; // grass grows underneath
268         f->setInventoryTexture("item_fence.png");
269         setWoodLikeDiggingProperties(f->digging_properties, 0.75);
270
271         i = CONTENT_RAIL;
272         f = &content_features(i);
273         f->setInventoryTexture("rail.png");
274         f->light_propagates = true;
275         f->param_type = CPT_LIGHT;
276         f->is_ground_content = true;
277         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
278         f->solidness = 0; // drawn separately, makes no faces
279         f->air_equivalent = true; // grass grows underneath
280         f->walkable = false;
281         setDirtLikeDiggingProperties(f->digging_properties, 0.75);
282
283         // Deprecated
284         i = CONTENT_COALSTONE;
285         f = &content_features(i);
286         f->setAllTextures("stone.png^mineral_coal.png");
287         f->is_ground_content = true;
288         setStoneLikeDiggingProperties(f->digging_properties, 1.5);
289         
290         i = CONTENT_WOOD;
291         f = &content_features(i);
292         f->setAllTextures("wood.png");
293         f->setInventoryTextureCube("wood.png", "wood.png", "wood.png");
294         f->is_ground_content = true;
295         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
296         setWoodLikeDiggingProperties(f->digging_properties, 0.75);
297         
298         i = CONTENT_MESE;
299         f = &content_features(i);
300         f->setAllTextures("mese.png");
301         f->setInventoryTextureCube("mese.png", "mese.png", "mese.png");
302         f->is_ground_content = true;
303         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
304         setStoneLikeDiggingProperties(f->digging_properties, 0.5);
305         
306         i = CONTENT_CLOUD;
307         f = &content_features(i);
308         f->setAllTextures("cloud.png");
309         f->setInventoryTextureCube("cloud.png", "cloud.png", "cloud.png");
310         f->is_ground_content = true;
311         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
312         
313         i = CONTENT_AIR;
314         f = &content_features(i);
315         f->param_type = CPT_LIGHT;
316         f->light_propagates = true;
317         f->sunlight_propagates = true;
318         f->solidness = 0;
319         f->walkable = false;
320         f->pointable = false;
321         f->diggable = false;
322         f->buildable_to = true;
323         f->air_equivalent = true;
324         
325         i = CONTENT_WATER;
326         f = &content_features(i);
327         f->setInventoryTextureCube("water.png", "water.png", "water.png");
328         f->param_type = CPT_LIGHT;
329         f->light_propagates = true;
330         f->solidness = 0; // Drawn separately, makes no faces
331         f->walkable = false;
332         f->pointable = false;
333         f->diggable = false;
334         f->buildable_to = true;
335         f->liquid_type = LIQUID_FLOWING;
336         f->liquid_alternative_flowing = CONTENT_WATER;
337         f->liquid_alternative_source = CONTENT_WATERSOURCE;
338         
339         i = CONTENT_WATERSOURCE;
340         f = &content_features(i);
341         //f->setInventoryTexture("water.png");
342         f->setInventoryTextureCube("water.png", "water.png", "water.png");
343         if(new_style_water)
344         {
345                 f->solidness = 0; // drawn separately, makes no faces
346         }
347         else // old style
348         {
349                 f->solidness = 1;
350
351                 TileSpec t;
352                 if(g_texturesource)
353                         t.texture = g_texturesource->getTexture("water.png");
354                 
355                 t.alpha = WATER_ALPHA;
356                 t.material_type = MATERIAL_ALPHA_VERTEX;
357                 t.material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
358                 f->setAllTiles(t);
359         }
360         f->param_type = CPT_LIGHT;
361         f->light_propagates = true;
362         f->walkable = false;
363         f->pointable = false;
364         f->diggable = false;
365         f->buildable_to = true;
366         f->liquid_type = LIQUID_SOURCE;
367         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
368         f->liquid_alternative_flowing = CONTENT_WATER;
369         f->liquid_alternative_source = CONTENT_WATERSOURCE;
370         
371         i = CONTENT_TORCH;
372         f = &content_features(i);
373         f->setInventoryTexture("torch_on_floor.png");
374         f->param_type = CPT_LIGHT;
375         f->light_propagates = true;
376         f->sunlight_propagates = true;
377         f->solidness = 0; // drawn separately, makes no faces
378         f->walkable = false;
379         f->wall_mounted = true;
380         f->air_equivalent = true;
381         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
382         f->light_source = LIGHT_MAX-1;
383         f->digging_properties.set("", DiggingProperties(true, 0.0, 0));
384         
385         i = CONTENT_SIGN_WALL;
386         f = &content_features(i);
387         f->setInventoryTexture("sign_wall.png");
388         f->param_type = CPT_LIGHT;
389         f->light_propagates = true;
390         f->sunlight_propagates = true;
391         f->solidness = 0; // drawn separately, makes no faces
392         f->walkable = false;
393         f->wall_mounted = true;
394         f->air_equivalent = true;
395         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
396         if(f->initial_metadata == NULL)
397                 f->initial_metadata = new SignNodeMetadata("Some sign");
398         f->digging_properties.set("", DiggingProperties(true, 0.5, 0));
399         
400         i = CONTENT_CHEST;
401         f = &content_features(i);
402         f->param_type = CPT_FACEDIR_SIMPLE;
403         f->setAllTextures("chest_side.png");
404         f->setTexture(0, "chest_top.png");
405         f->setTexture(1, "chest_top.png");
406         f->setTexture(5, "chest_front.png"); // Z-
407         f->setInventoryTexture("chest_top.png");
408         //f->setInventoryTextureCube("chest_top.png", "chest_side.png", "chest_side.png");
409         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
410         if(f->initial_metadata == NULL)
411                 f->initial_metadata = new ChestNodeMetadata();
412         setWoodLikeDiggingProperties(f->digging_properties, 1.0);
413         
414         i = CONTENT_FURNACE;
415         f = &content_features(i);
416         f->param_type = CPT_FACEDIR_SIMPLE;
417         f->setAllTextures("furnace_side.png");
418         f->setTexture(5, "furnace_front.png"); // Z-
419         f->setInventoryTexture("furnace_front.png");
420         //f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
421         f->dug_item = std::string("MaterialItem ")+itos(CONTENT_COBBLE)+" 6";
422         if(f->initial_metadata == NULL)
423                 f->initial_metadata = new FurnaceNodeMetadata();
424         setStoneLikeDiggingProperties(f->digging_properties, 3.0);
425         
426         i = CONTENT_COBBLE;
427         f = &content_features(i);
428         f->setAllTextures("cobble.png");
429         f->setInventoryTextureCube("cobble.png", "cobble.png", "cobble.png");
430         f->param_type = CPT_NONE;
431         f->is_ground_content = true;
432         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
433         setStoneLikeDiggingProperties(f->digging_properties, 0.9);
434
435         i = CONTENT_MOSSYCOBBLE;
436         f = &content_features(i);
437         f->setAllTextures("mossycobble.png");
438         f->setInventoryTextureCube("mossycobble.png", "mossycobble.png", "mossycobble.png");
439         f->param_type = CPT_NONE;
440         f->is_ground_content = true;
441         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
442         setStoneLikeDiggingProperties(f->digging_properties, 0.8);
443         
444         i = CONTENT_STEEL;
445         f = &content_features(i);
446         f->setAllTextures("steel_block.png");
447         f->setInventoryTextureCube("steel_block.png", "steel_block.png",
448                         "steel_block.png");
449         f->param_type = CPT_NONE;
450         f->is_ground_content = true;
451         f->dug_item = std::string("MaterialItem ")+itos(i)+" 1";
452         setStoneLikeDiggingProperties(f->digging_properties, 5.0);
453         
454         // NOTE: Remember to add frequently used stuff to the texture atlas in tile.cpp
455         
456
457         /*
458                 Add MesePick to everything
459         */
460         for(u16 i=0; i<256; i++)
461         {
462                 content_features(i).digging_properties.set("MesePick",
463                                 DiggingProperties(true, 0.0, 65535./1337));
464         }
465
466 }
467
468 void setStoneLikeDiggingProperties(DiggingPropertiesList &list, float toughness)
469 {
470         list.set("",
471                         DiggingProperties(true, 15.0*toughness, 0));
472         
473         list.set("WPick",
474                         DiggingProperties(true, 1.3*toughness, 65535./30.*toughness));
475         list.set("STPick",
476                         DiggingProperties(true, 0.75*toughness, 65535./100.*toughness));
477         list.set("SteelPick",
478                         DiggingProperties(true, 0.50*toughness, 65535./333.*toughness));
479
480         /*list.set("MesePick",
481                         DiggingProperties(true, 0.0*toughness, 65535./20.*toughness));*/
482 }
483
484 void setDirtLikeDiggingProperties(DiggingPropertiesList &list, float toughness)
485 {
486         list.set("",
487                         DiggingProperties(true, 0.75*toughness, 0));
488         
489         list.set("WShovel",
490                         DiggingProperties(true, 0.4*toughness, 65535./50.*toughness));
491         list.set("STShovel",
492                         DiggingProperties(true, 0.2*toughness, 65535./150.*toughness));
493         list.set("SteelShovel",
494                         DiggingProperties(true, 0.15*toughness, 65535./400.*toughness));
495 }
496
497 void setWoodLikeDiggingProperties(DiggingPropertiesList &list, float toughness)
498 {
499         list.set("",
500                         DiggingProperties(true, 3.0*toughness, 0));
501         
502         list.set("WAxe",
503                         DiggingProperties(true, 1.5*toughness, 65535./30.*toughness));
504         list.set("STAxe",
505                         DiggingProperties(true, 0.75*toughness, 65535./100.*toughness));
506         list.set("SteelAxe",
507                         DiggingProperties(true, 0.5*toughness, 65535./333.*toughness));
508 }
509
510