]> git.lizzy.rs Git - minetest.git/blob - src/nodedef.cpp
6fa410cc2027ac48aecfb277b1b0d96a564225ee
[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         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
34                 delete special_materials[j];
35                 delete special_aps[j];
36         }
37 #endif
38 }
39
40 #if 0
41 void ContentFeatures::setTexture(ITextureSource *tsrc,
42                 u16 i, std::string name, u8 alpha)
43 {
44         used_texturenames.insert(name);
45         
46         if(tsrc)
47         {
48                 tiles[i].texture = tsrc->getTexture(name);
49         }
50         
51         if(alpha != 255)
52         {
53                 tiles[i].alpha = alpha;
54                 tiles[i].material_type = MATERIAL_ALPHA_VERTEX;
55         }
56
57         if(inventory_texture == NULL)
58                 setInventoryTexture(name, tsrc);
59 }
60
61 void ContentFeatures::setInventoryTexture(std::string imgname,
62                 ITextureSource *tsrc)
63 {
64         if(tsrc == NULL)
65                 return;
66         
67         imgname += "^[forcesingle";
68         
69         inventory_texture = tsrc->getTextureRaw(imgname);
70 }
71
72 void ContentFeatures::setInventoryTextureCube(std::string top,
73                 std::string left, std::string right, ITextureSource *tsrc)
74 {
75         if(tsrc == NULL)
76                 return;
77         
78         str_replace_char(top, '^', '&');
79         str_replace_char(left, '^', '&');
80         str_replace_char(right, '^', '&');
81
82         std::string imgname_full;
83         imgname_full += "[inventorycube{";
84         imgname_full += top;
85         imgname_full += "{";
86         imgname_full += left;
87         imgname_full += "{";
88         imgname_full += right;
89         inventory_texture = tsrc->getTextureRaw(imgname_full);
90 }
91 #endif
92
93 void ContentFeatures::setTexture(u16 i, std::string name)
94 {
95         used_texturenames.insert(name);
96         tname_tiles[i] = name;
97         if(tname_inventory == "")
98                 tname_inventory = name;
99 }
100
101 void ContentFeatures::setInventoryTexture(std::string imgname)
102 {
103         tname_inventory = imgname + "^[forcesingle";
104 }
105
106 void ContentFeatures::setInventoryTextureCube(std::string top,
107                 std::string left, std::string right)
108 {
109         str_replace_char(top, '^', '&');
110         str_replace_char(left, '^', '&');
111         str_replace_char(right, '^', '&');
112
113         std::string imgname_full;
114         imgname_full += "[inventorycube{";
115         imgname_full += top;
116         imgname_full += "{";
117         imgname_full += left;
118         imgname_full += "{";
119         imgname_full += right;
120         tname_inventory = imgname_full;
121 }
122
123 class CNodeDefManager: public IWritableNodeDefManager
124 {
125 public:
126         CNodeDefManager()
127         {
128                 for(u16 i=0; i<=MAX_CONTENT; i++)
129                 {
130                         ContentFeatures *f = &m_content_features[i];
131                         // Reset to defaults
132                         f->reset();
133                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
134                                 continue;
135                         f->setAllTextures("unknown_block.png");
136                         f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
137                 }
138                 // Make CONTENT_IGNORE to not block the view when occlusion culling
139                 m_content_features[CONTENT_IGNORE].solidness = 0;
140         }
141         virtual ~CNodeDefManager()
142         {
143         }
144         virtual IWritableNodeDefManager* clone()
145         {
146                 CNodeDefManager *mgr = new CNodeDefManager();
147                 for(u16 i=0; i<=MAX_CONTENT; i++)
148                 {
149                         mgr->set(i, get(i));
150                 }
151                 return mgr;
152         }
153         virtual const ContentFeatures& get(content_t c) const
154         {
155                 assert(c <= MAX_CONTENT);
156                 return m_content_features[c];
157         }
158         virtual const ContentFeatures& get(const MapNode &n) const
159         {
160                 return get(n.getContent());
161         }
162         // Writable
163         virtual void set(content_t c, const ContentFeatures &def)
164         {
165                 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
166                 assert(c <= MAX_CONTENT);
167                 m_content_features[c] = def;
168         }
169         virtual ContentFeatures* getModifiable(content_t c)
170         {
171                 assert(c <= MAX_CONTENT);
172                 return &m_content_features[c];
173         }
174         virtual void updateTextures(ITextureSource *tsrc)
175         {
176 #ifndef SERVER
177                 infostream<<"CNodeDefManager::updateTextures(): Updating "
178                                 <<"textures in node definitions"<<std::endl;
179                 for(u16 i=0; i<=MAX_CONTENT; i++)
180                 {
181                         infostream<<"Updating content "<<i<<std::endl;
182                         ContentFeatures *f = &m_content_features[i];
183                         // Inventory texture
184                         if(f->tname_inventory != "")
185                                 f->inventory_texture = tsrc->getTextureRaw(f->tname_inventory);
186                         else
187                                 f->inventory_texture = NULL;
188                         // Tile textures
189                         for(u16 j=0; j<6; j++){
190                                 if(f->tname_tiles[j] == "")
191                                         continue;
192                                 f->tiles[j].texture = tsrc->getTexture(f->tname_tiles[j]);
193                                 f->tiles[j].alpha = f->alpha;
194                                 if(f->alpha == 255)
195                                         f->tiles[j].material_type = MATERIAL_ALPHA_SIMPLE;
196                                 else
197                                         f->tiles[j].material_type = MATERIAL_ALPHA_VERTEX;
198                                 if(f->backface_culling)
199                                         f->tiles[j].material_flags |= MATERIAL_FLAG_BACKFACE_CULLING;
200                                 else
201                                         f->tiles[j].material_flags &= ~MATERIAL_FLAG_BACKFACE_CULLING;
202                         }
203                         // Special textures
204                         for(u16 j=0; j<CF_SPECIAL_COUNT; j++){
205                                 // Remove all stuff
206                                 if(f->special_aps[j]){
207                                         delete f->special_aps[j];
208                                         f->special_aps[j] = NULL;
209                                 }
210                                 if(f->special_materials[j]){
211                                         delete f->special_materials[j];
212                                         f->special_materials[j] = NULL;
213                                 }
214                                 // Skip if should not exist
215                                 if(f->mspec_special[j].tname == "")
216                                         continue;
217                                 // Create all stuff
218                                 f->special_aps[j] = new AtlasPointer(
219                                                 tsrc->getTexture(f->mspec_special[j].tname));
220                                 f->special_materials[j] = new video::SMaterial;
221                                 f->special_materials[j]->setFlag(video::EMF_LIGHTING, false);
222                                 f->special_materials[j]->setFlag(video::EMF_BACK_FACE_CULLING,
223                                                 f->mspec_special[j].backface_culling);
224                                 f->special_materials[j]->setFlag(video::EMF_BILINEAR_FILTER, false);
225                                 f->special_materials[j]->setFlag(video::EMF_FOG_ENABLE, true);
226                                 f->special_materials[j]->setTexture(0, f->special_aps[j]->atlas);
227                         }
228                 }
229 #endif
230         }
231 private:
232         ContentFeatures m_content_features[MAX_CONTENT+1];
233 };
234
235 IWritableNodeDefManager* createNodeDefManager()
236 {
237         return new CNodeDefManager();
238 }
239