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