Update inventory texture too
[oweals/minetest.git] / src / nodedef.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 "nodedef.h"
21
22 #include "main.h" // For g_settings
23 #include "nodemetadata.h"
24 #ifndef SERVER
25 #include "tile.h"
26 #endif
27 #include "log.h"
28
29 ContentFeatures::~ContentFeatures()
30 {
31         delete initial_metadata;
32 #ifndef SERVER
33         delete special_material;
34         delete special_atlas;
35 #endif
36 }
37
38 #ifndef SERVER
39 void ContentFeatures::setTexture(ITextureSource *tsrc,
40                 u16 i, std::string name, u8 alpha)
41 {
42         used_texturenames.insert(name);
43         
44         if(tsrc)
45         {
46                 tiles[i].texture = tsrc->getTexture(name);
47         }
48         
49         if(alpha != 255)
50         {
51                 tiles[i].alpha = alpha;
52                 tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
53         }
54
55         if(inventory_texture == NULL)
56                 setInventoryTexture(name, tsrc);
57 }
58
59 void ContentFeatures::setInventoryTexture(std::string imgname,
60                 ITextureSource *tsrc)
61 {
62         if(tsrc == NULL)
63                 return;
64         
65         imgname += "^[forcesingle";
66         
67         inventory_texture_name = imgname;
68         inventory_texture = tsrc->getTextureRaw(imgname);
69 }
70
71 void ContentFeatures::setInventoryTextureCube(std::string top,
72                 std::string left, std::string right, ITextureSource *tsrc)
73 {
74         if(tsrc == NULL)
75                 return;
76         
77         str_replace_char(top, '^', '&');
78         str_replace_char(left, '^', '&');
79         str_replace_char(right, '^', '&');
80
81         std::string imgname_full;
82         imgname_full += "[inventorycube{";
83         imgname_full += top;
84         imgname_full += "{";
85         imgname_full += left;
86         imgname_full += "{";
87         imgname_full += right;
88         inventory_texture_name = imgname_full;
89         inventory_texture = tsrc->getTextureRaw(imgname_full);
90 }
91 #endif
92
93 class CNodeDefManager: public IWritableNodeDefManager
94 {
95 public:
96         CNodeDefManager(ITextureSource *tsrc)
97         {
98 #ifndef SERVER
99                 /*
100                         Set initial material type to same in all tiles, so that the
101                         same material can be used in more stuff.
102                         This is set according to the leaves because they are the only
103                         differing material to which all materials can be changed to
104                         get this optimization.
105                 */
106                 u8 initial_material_type = MATERIAL_ALPHA_SIMPLE;
107                 /*if(new_style_leaves)
108                         initial_material_type = MATERIAL_ALPHA_SIMPLE;
109                 else
110                         initial_material_type = MATERIAL_ALPHA_NONE;*/
111                 for(u16 i=0; i<=MAX_CONTENT; i++)
112                 {
113                         ContentFeatures *f = &m_content_features[i];
114                         // Re-initialize
115                         f->reset();
116
117                         for(u16 j=0; j<6; j++)
118                                 f->tiles[j].material_type = initial_material_type;
119                 }
120 #endif
121                 /*
122                         Initially set every block to be shown as an unknown block.
123                         Don't touch CONTENT_IGNORE or CONTENT_AIR.
124                 */
125                 for(u16 i=0; i<=MAX_CONTENT; i++)
126                 {
127                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
128                                 continue;
129                         ContentFeatures *f = &m_content_features[i];
130                         f->setAllTextures(tsrc, "unknown_block.png");
131                         f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
132                 }
133                 // Make CONTENT_IGNORE to not block the view when occlusion culling
134                 m_content_features[CONTENT_IGNORE].solidness = 0;
135         }
136         virtual ~CNodeDefManager()
137         {
138         }
139         virtual IWritableNodeDefManager* clone()
140         {
141                 CNodeDefManager *mgr = new CNodeDefManager(NULL);
142                 for(u16 i=0; i<=MAX_CONTENT; i++)
143                 {
144                         mgr->set(i, get(i));
145                 }
146                 return mgr;
147         }
148         virtual const ContentFeatures& get(content_t c) const
149         {
150                 assert(c <= MAX_CONTENT);
151                 return m_content_features[c];
152         }
153         virtual const ContentFeatures& get(const MapNode &n) const
154         {
155                 return get(n.getContent());
156         }
157         // Writable
158         virtual void set(content_t c, const ContentFeatures &def)
159         {
160                 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
161                 assert(c <= MAX_CONTENT);
162                 m_content_features[c] = def;
163         }
164         virtual ContentFeatures* getModifiable(content_t c)
165         {
166                 assert(c <= MAX_CONTENT);
167                 return &m_content_features[c];
168         }
169         virtual void updateTextures(ITextureSource *tsrc)
170         {
171 #ifndef SERVER
172                 infostream<<"CNodeDefManager::updateTextures(): Updating "
173                                 <<"textures in node definitions"<<std::endl;
174                 for(u16 i=0; i<=MAX_CONTENT; i++)
175                 {
176                         ContentFeatures *f = &m_content_features[i];
177                         for(u16 j=0; j<6; j++)
178                                 tsrc->updateAP(f->tiles[j].texture);
179                         if(f->special_atlas){
180                                 tsrc->updateAP(*(f->special_atlas));
181                                 if(f->special_material)
182                                         f->special_material->setTexture(0, f->special_atlas->atlas);
183                                 if(f->special_material2)
184                                         f->special_material2->setTexture(0, f->special_atlas->atlas);
185                         }
186                         if(f->inventory_texture_name != ""){
187                                 f->inventory_texture =
188                                         tsrc->getTextureRaw(f->inventory_texture_name);
189                         }
190                 }
191 #endif
192         }
193 private:
194         ContentFeatures m_content_features[MAX_CONTENT+1];
195 };
196
197 IWritableNodeDefManager* createNodeDefManager(ITextureSource *tsrc)
198 {
199         return new CNodeDefManager(tsrc);
200 }
201