ToolDefManager serialization
[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 = tsrc->getTextureRaw(imgname);
68 }
69
70 void ContentFeatures::setInventoryTextureCube(std::string top,
71                 std::string left, std::string right, ITextureSource *tsrc)
72 {
73         if(tsrc == NULL)
74                 return;
75         
76         str_replace_char(top, '^', '&');
77         str_replace_char(left, '^', '&');
78         str_replace_char(right, '^', '&');
79
80         std::string imgname_full;
81         imgname_full += "[inventorycube{";
82         imgname_full += top;
83         imgname_full += "{";
84         imgname_full += left;
85         imgname_full += "{";
86         imgname_full += right;
87         inventory_texture = tsrc->getTextureRaw(imgname_full);
88 }
89 #endif
90
91 class CNodeDefManager: public IWritableNodeDefManager
92 {
93 public:
94         CNodeDefManager(ITextureSource *tsrc)
95         {
96 #ifndef SERVER
97                 /*
98                         Set initial material type to same in all tiles, so that the
99                         same material can be used in more stuff.
100                         This is set according to the leaves because they are the only
101                         differing material to which all materials can be changed to
102                         get this optimization.
103                 */
104                 u8 initial_material_type = MATERIAL_ALPHA_SIMPLE;
105                 /*if(new_style_leaves)
106                         initial_material_type = MATERIAL_ALPHA_SIMPLE;
107                 else
108                         initial_material_type = MATERIAL_ALPHA_NONE;*/
109                 for(u16 i=0; i<=MAX_CONTENT; i++)
110                 {
111                         ContentFeatures *f = &m_content_features[i];
112                         // Re-initialize
113                         f->reset();
114
115                         for(u16 j=0; j<6; j++)
116                                 f->tiles[j].material_type = initial_material_type;
117                 }
118 #endif
119                 /*
120                         Initially set every block to be shown as an unknown block.
121                         Don't touch CONTENT_IGNORE or CONTENT_AIR.
122                 */
123                 for(u16 i=0; i<=MAX_CONTENT; i++)
124                 {
125                         if(i == CONTENT_IGNORE || i == CONTENT_AIR)
126                                 continue;
127                         ContentFeatures *f = &m_content_features[i];
128                         f->setAllTextures(tsrc, "unknown_block.png");
129                         f->dug_item = std::string("MaterialItem2 ")+itos(i)+" 1";
130                 }
131                 // Make CONTENT_IGNORE to not block the view when occlusion culling
132                 m_content_features[CONTENT_IGNORE].solidness = 0;
133         }
134         virtual ~CNodeDefManager()
135         {
136         }
137         virtual IWritableNodeDefManager* clone()
138         {
139                 CNodeDefManager *mgr = new CNodeDefManager(NULL);
140                 for(u16 i=0; i<=MAX_CONTENT; i++)
141                 {
142                         mgr->set(i, get(i));
143                 }
144                 return mgr;
145         }
146         virtual const ContentFeatures& get(content_t c) const
147         {
148                 assert(c <= MAX_CONTENT);
149                 return m_content_features[c];
150         }
151         virtual const ContentFeatures& get(const MapNode &n) const
152         {
153                 return get(n.getContent());
154         }
155         // Writable
156         virtual void set(content_t c, const ContentFeatures &def)
157         {
158                 infostream<<"registerNode: registering content \""<<c<<"\""<<std::endl;
159                 assert(c <= MAX_CONTENT);
160                 m_content_features[c] = def;
161         }
162         virtual ContentFeatures* getModifiable(content_t c)
163         {
164                 assert(c <= MAX_CONTENT);
165                 return &m_content_features[c];
166         }
167         virtual void updateTextures(ITextureSource *tsrc)
168         {
169 #ifndef SERVER
170                 infostream<<"CNodeDefManager::updateTextures(): Updating "
171                                 <<"textures in node definitions"<<std::endl;
172                 for(u16 i=0; i<=MAX_CONTENT; i++)
173                 {
174                         ContentFeatures *f = &m_content_features[i];
175                         for(u16 j=0; j<6; j++)
176                                 tsrc->updateAP(f->tiles[j].texture);
177                         if(f->special_atlas)
178                                 tsrc->updateAP(*(f->special_atlas));
179                 }
180 #endif
181         }
182 private:
183         ContentFeatures m_content_features[MAX_CONTENT+1];
184 };
185
186 IWritableNodeDefManager* createNodeDefManager(ITextureSource *tsrc)
187 {
188         return new CNodeDefManager(tsrc);
189 }
190