]> git.lizzy.rs Git - minetest.git/blob - src/mapnode_contentfeatures.cpp
Move ContentFeatures to mapnode_contentfeatures.{h,cpp} and clean stuff
[minetest.git] / src / mapnode_contentfeatures.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_contentfeatures.h"
21
22 #include "main.h" // For g_settings and g_texturesource
23 #include "nodemetadata.h"
24
25 struct ContentFeatures g_content_features[MAX_CONTENT+1];
26
27 /*
28         Initialize content feature table.
29
30         Must be called before accessing the table.
31 */
32 void init_contentfeatures()
33 {
34 #ifndef SERVER
35         /*
36                 Set initial material type to same in all tiles, so that the
37                 same material can be used in more stuff.
38                 This is set according to the leaves because they are the only
39                 differing material to which all materials can be changed to
40                 get this optimization.
41         */
42         u8 initial_material_type = MATERIAL_ALPHA_SIMPLE;
43         /*if(new_style_leaves)
44                 initial_material_type = MATERIAL_ALPHA_SIMPLE;
45         else
46                 initial_material_type = MATERIAL_ALPHA_NONE;*/
47         for(u16 i=0; i<MAX_CONTENT+1; i++)
48         {
49                 ContentFeatures *f = &g_content_features[i];
50                 // Re-initialize
51                 f->reset();
52
53                 for(u16 j=0; j<6; j++)
54                         f->tiles[j].material_type = initial_material_type;
55         }
56 #endif
57
58         /*
59                 Initially set every block to be shown as an unknown block.
60                 Don't touch CONTENT_IGNORE or CONTENT_AIR.
61         */
62         for(u16 i=0; i<MAX_CONTENT+1; i++)
63         {
64                 if(i == CONTENT_IGNORE || i == CONTENT_AIR)
65                         continue;
66                 ContentFeatures *f = &g_content_features[i];
67                 f->setAllTextures("unknown_block.png");
68                 f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
69         }
70
71         // Make CONTENT_IGNORE to not block the view when occlusion culling
72         content_features(CONTENT_IGNORE).solidness = 0;
73
74 }
75
76 ContentFeatures::~ContentFeatures()
77 {
78         delete initial_metadata;
79 #ifndef SERVER
80         delete special_material;
81         delete special_atlas;
82 #endif
83 }
84
85 #ifndef SERVER
86 void ContentFeatures::setTexture(u16 i, std::string name, u8 alpha)
87 {
88         used_texturenames[name] = true;
89         
90         if(g_texturesource)
91         {
92                 tiles[i].texture = g_texturesource->getTexture(name);
93         }
94         
95         if(alpha != 255)
96         {
97                 tiles[i].alpha = alpha;
98                 tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
99         }
100
101         if(inventory_texture == NULL)
102                 setInventoryTexture(name);
103 }
104
105 void ContentFeatures::setInventoryTexture(std::string imgname)
106 {
107         if(g_texturesource == NULL)
108                 return;
109         
110         imgname += "^[forcesingle";
111         
112         inventory_texture = g_texturesource->getTextureRaw(imgname);
113 }
114
115 void ContentFeatures::setInventoryTextureCube(std::string top,
116                 std::string left, std::string right)
117 {
118         if(g_texturesource == NULL)
119                 return;
120         
121         str_replace_char(top, '^', '&');
122         str_replace_char(left, '^', '&');
123         str_replace_char(right, '^', '&');
124
125         std::string imgname_full;
126         imgname_full += "[inventorycube{";
127         imgname_full += top;
128         imgname_full += "{";
129         imgname_full += left;
130         imgname_full += "{";
131         imgname_full += right;
132         inventory_texture = g_texturesource->getTextureRaw(imgname_full);
133 }
134 #endif
135
136 ContentFeatures & content_features(content_t i)
137 {
138         return g_content_features[i];
139 }
140 ContentFeatures & content_features(MapNode &n)
141 {
142         return content_features(n.getContent());
143 }
144
145