license stuff
[oweals/minetest.git] / src / voxel.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 "voxel.h"
21 #include "map.h"
22
23 VoxelManipulator::VoxelManipulator():
24         m_data(NULL)
25 {
26 }
27
28 VoxelManipulator::~VoxelManipulator()
29 {
30         if(m_data)
31                 delete[] m_data;
32 }
33
34 void VoxelManipulator::addArea(VoxelArea area)
35 {
36         if(area.getExtent() == v3s16(0,0,0))
37                 return;
38         
39         // Calculate new area
40         VoxelArea new_area;
41         if(m_area.getExtent() == v3s16(0,0,0))
42         {
43                 new_area = area;
44         }
45         else
46         {
47                 new_area = m_area;
48                 new_area.addArea(area);
49         }
50
51         if(new_area == m_area)
52                 return;
53
54         s32 new_size = new_area.getVolume();
55
56         /*dstream<<"adding area ";
57         area.print(dstream);
58         dstream<<", old area ";
59         m_area.print(dstream);
60         dstream<<", new area ";
61         new_area.print(dstream);
62         dstream<<", new_size="<<new_size;
63         dstream<<std::endl;*/
64
65         // Allocate and clear new data
66         MapNode *new_data;
67         new_data = new MapNode[new_size];
68         for(s32 i=0; i<new_size; i++)
69         {
70                 new_data[i].d = MATERIAL_IGNORE;
71         }
72         
73         // Copy old data
74         
75         for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
76         for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
77         for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
78         {
79                 new_data[new_area.index(z,y,x)] = m_data[m_area.index(x,y,z)];
80         }
81
82         // Replace member
83         m_area = new_area;
84         MapNode *old_data = m_data;
85         m_data = new_data;
86         delete[] old_data;
87 }
88
89 void VoxelManipulator::print(std::ostream &o)
90 {
91         v3s16 em = m_area.getExtent();
92         v3s16 of = m_area.MinEdge;
93         o<<"size: "<<em.X<<"x"<<em.Y<<"x"<<em.Z
94          <<" offset: ("<<of.X<<","<<of.Y<<","<<of.Z<<")"<<std::endl;
95         
96         for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
97         {
98                 if(em.X >= 3 && em.Y >= 3)
99                 {
100                         if(y==m_area.MinEdge.Y+0) o<<"y x-> ";
101                         if(y==m_area.MinEdge.Y+1) o<<"|     ";
102                         if(y==m_area.MinEdge.Y+2) o<<"V     ";
103                 }
104
105                 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
106                 {
107                         for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
108                         {
109                                 u8 m = m_data[m_area.index(x,y,z)].d;
110                                 char c = 'X';
111                                 if(m == MATERIAL_IGNORE)
112                                         c = 'I';
113                                 else if(m <= 9)
114                                         c = m + '0';
115                                 o<<c;
116                         }
117                         o<<' ';
118                 }
119                 o<<std::endl;
120         }
121 }
122
123 void VoxelManipulator::interpolate(VoxelArea area)
124 {
125         VoxelArea emerge_area = area;
126         emerge_area.MinEdge -= v3s16(1,1,1);
127         emerge_area.MaxEdge += v3s16(1,1,1);
128         emerge(emerge_area);
129
130         SharedBuffer<u8> buf(area.getVolume());
131
132         for(s32 z=area.MinEdge.Z; z<=area.MaxEdge.Z; z++)
133         for(s32 y=area.MinEdge.Y; y<=area.MaxEdge.Y; y++)
134         for(s32 x=area.MinEdge.X; x<=area.MaxEdge.X; x++)
135         {
136                 v3s16 p(x,y,z);
137
138                 v3s16 dirs[] = {
139                         v3s16(1,1,0),
140                         v3s16(1,0,1),
141                         v3s16(1,-1,0),
142                         v3s16(1,0,-1),
143                         v3s16(-1,1,0),
144                         v3s16(-1,0,1),
145                         v3s16(-1,-1,0),
146                         v3s16(-1,0,-1),
147                 };
148                 //const v3s16 *dirs = g_26dirs;
149                 
150                 s16 total = 0;
151                 s16 airness = 0;
152                 u8 m = MATERIAL_IGNORE;
153
154                 for(s16 i=0; i<8; i++)
155                 //for(s16 i=0; i<26; i++)
156                 {
157                         v3s16 p2 = p + dirs[i];
158
159                         MapNode &n = m_data[m_area.index(p2)];
160                         if(n.d == MATERIAL_IGNORE)
161                                 continue;
162
163                         airness += (n.d == MATERIAL_AIR) ? 1 : -1;
164                         total++;
165
166                         if(m == MATERIAL_IGNORE && n.d != MATERIAL_AIR)
167                                 m = n.d;
168                 }
169
170                 // 1 if air, 0 if not
171                 buf[area.index(p)] = airness > -total/2 ? MATERIAL_AIR : m;
172                 //buf[area.index(p)] = airness > -total ? MATERIAL_AIR : m;
173                 //buf[area.index(p)] = airness >= -7 ? MATERIAL_AIR : m;
174         }
175
176         for(s32 z=area.MinEdge.Z; z<=area.MaxEdge.Z; z++)
177         for(s32 y=area.MinEdge.Y; y<=area.MaxEdge.Y; y++)
178         for(s32 x=area.MinEdge.X; x<=area.MaxEdge.X; x++)
179         {
180                 v3s16 p(x,y,z);
181                 m_data[m_area.index(p)].d = buf[area.index(p)];
182         }
183 }
184
185 #if 0
186 void VoxelManipulator::blitFromNodeContainer
187                 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c)
188 {
189         VoxelArea a_to(p_to, p_to+size-v3s16(1,1,1));
190         addArea(a_to);
191         for(s16 z=0; z<size.Z; z++)
192         for(s16 y=0; y<size.Y; y++)
193         for(s16 x=0; x<size.X; x++)
194         {
195                 v3s16 p(x,y,z);
196                 try{
197                         MapNode n = c->getNode(p_from + p);
198                         m_data[m_area.index(p_to + p)] = n;
199                 }
200                 catch(InvalidPositionException &e)
201                 {
202                 }
203                 
204                 /*v3s16 p(x,y,z);
205                 MapNode n(MATERIAL_IGNORE);
206                 try{
207                         n = c->getNode(p_from + p);
208                 }
209                 catch(InvalidPositionException &e)
210                 {
211                 }
212                 m_data[m_area.index(p_to + p)] = n;*/
213         }
214 }
215
216 void VoxelManipulator::blitToNodeContainer
217                 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c)
218 {
219         for(s16 z=0; z<size.Z; z++)
220         for(s16 y=0; y<size.Y; y++)
221         for(s16 x=0; x<size.X; x++)
222         {
223                 v3s16 p(x,y,z);
224                 try{
225                         MapNode &n = m_data[m_area.index(p_from + p)];
226                         if(n.d == MATERIAL_IGNORE)
227                                 continue;
228                         c->setNode(p_to + p, n);
229                 }
230                 catch(InvalidPositionException &e)
231                 {
232                 }
233         }
234 }
235 #endif
236
237 /*
238         MapVoxelManipulator
239 */
240
241 MapVoxelManipulator::MapVoxelManipulator(Map *map)
242 {
243         m_map = map;
244 }
245
246 void MapVoxelManipulator::emerge(VoxelArea a)
247 {
248         v3s16 size = a.getExtent();
249
250         addArea(a);
251
252         for(s16 z=0; z<size.Z; z++)
253         for(s16 y=0; y<size.Y; y++)
254         for(s16 x=0; x<size.X; x++)
255         {
256                 v3s16 p(x,y,z);
257                 try{
258                         MapNode n = m_map->getNode(a.MinEdge + p);
259                         m_data[m_area.index(a.MinEdge + p)] = n;
260                 }
261                 catch(InvalidPositionException &e)
262                 {
263                 }
264         }
265 }
266
267 void MapVoxelManipulator::blitBack
268                 (core::map<v3s16, MapBlock*> & modified_blocks)
269 {
270         /*
271                 Initialize block cache
272         */
273         v3s16 blockpos_last;
274         MapBlock *block = NULL;
275         bool block_checked_in_modified = false;
276
277         for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
278         for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
279         for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
280         {
281                 v3s16 p(x,y,z);
282
283                 MapNode &n = m_data[m_area.index(p)];
284                 if(n.d == MATERIAL_IGNORE)
285                         continue;
286                         
287                 v3s16 blockpos = getNodeBlockPos(p);
288                 
289                 try
290                 {
291                         // Get block
292                         if(block == NULL || blockpos != blockpos_last){
293                                 block = m_map->getBlockNoCreate(blockpos);
294                                 blockpos_last = blockpos;
295                                 block_checked_in_modified = false;
296                         }
297                         
298                         // Calculate relative position in block
299                         v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
300
301                         // Don't continue if nothing has changed here
302                         if(block->getNode(relpos) == n)
303                                 continue;
304
305                         //m_map->setNode(m_area.MinEdge + p, n);
306                         block->setNode(relpos, n);
307                         
308                         /*
309                                 Make sure block is in modified_blocks
310                         */
311                         if(block_checked_in_modified == false)
312                         {
313                                 modified_blocks[blockpos] = block;
314                                 block_checked_in_modified = true;
315                         }
316                 }
317                 catch(InvalidPositionException &e)
318                 {
319                 }
320         }
321 }
322
323 //END