3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
23 VoxelManipulator::VoxelManipulator():
28 VoxelManipulator::~VoxelManipulator()
34 void VoxelManipulator::addArea(VoxelArea area)
36 if(area.getExtent() == v3s16(0,0,0))
41 if(m_area.getExtent() == v3s16(0,0,0))
48 new_area.addArea(area);
51 if(new_area == m_area)
54 s32 new_size = new_area.getVolume();
56 /*dstream<<"adding area ";
58 dstream<<", old area ";
59 m_area.print(dstream);
60 dstream<<", new area ";
61 new_area.print(dstream);
62 dstream<<", new_size="<<new_size;
65 // Allocate and clear new data
67 new_data = new MapNode[new_size];
68 for(s32 i=0; i<new_size; i++)
70 new_data[i].d = MATERIAL_IGNORE;
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++)
79 new_data[new_area.index(z,y,x)] = m_data[m_area.index(x,y,z)];
84 MapNode *old_data = m_data;
89 void VoxelManipulator::print(std::ostream &o)
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;
96 for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
98 if(em.X >= 3 && em.Y >= 3)
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 ";
105 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
107 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
109 u8 m = m_data[m_area.index(x,y,z)].d;
111 if(m == MATERIAL_IGNORE)
123 void VoxelManipulator::interpolate(VoxelArea area)
125 VoxelArea emerge_area = area;
126 emerge_area.MinEdge -= v3s16(1,1,1);
127 emerge_area.MaxEdge += v3s16(1,1,1);
130 SharedBuffer<u8> buf(area.getVolume());
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++)
148 //const v3s16 *dirs = g_26dirs;
152 u8 m = MATERIAL_IGNORE;
154 for(s16 i=0; i<8; i++)
155 //for(s16 i=0; i<26; i++)
157 v3s16 p2 = p + dirs[i];
159 MapNode &n = m_data[m_area.index(p2)];
160 if(n.d == MATERIAL_IGNORE)
163 airness += (n.d == MATERIAL_AIR) ? 1 : -1;
166 if(m == MATERIAL_IGNORE && n.d != MATERIAL_AIR)
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;
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++)
181 m_data[m_area.index(p)].d = buf[area.index(p)];
186 void VoxelManipulator::blitFromNodeContainer
187 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c)
189 VoxelArea a_to(p_to, p_to+size-v3s16(1,1,1));
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++)
197 MapNode n = c->getNode(p_from + p);
198 m_data[m_area.index(p_to + p)] = n;
200 catch(InvalidPositionException &e)
205 MapNode n(MATERIAL_IGNORE);
207 n = c->getNode(p_from + p);
209 catch(InvalidPositionException &e)
212 m_data[m_area.index(p_to + p)] = n;*/
216 void VoxelManipulator::blitToNodeContainer
217 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c)
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++)
225 MapNode &n = m_data[m_area.index(p_from + p)];
226 if(n.d == MATERIAL_IGNORE)
228 c->setNode(p_to + p, n);
230 catch(InvalidPositionException &e)
241 MapVoxelManipulator::MapVoxelManipulator(Map *map)
246 void MapVoxelManipulator::emerge(VoxelArea a)
248 v3s16 size = a.getExtent();
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++)
258 MapNode n = m_map->getNode(a.MinEdge + p);
259 m_data[m_area.index(a.MinEdge + p)] = n;
261 catch(InvalidPositionException &e)
267 void MapVoxelManipulator::blitBack
268 (core::map<v3s16, MapBlock*> & modified_blocks)
271 Initialize block cache
274 MapBlock *block = NULL;
275 bool block_checked_in_modified = false;
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++)
283 MapNode &n = m_data[m_area.index(p)];
284 if(n.d == MATERIAL_IGNORE)
287 v3s16 blockpos = getNodeBlockPos(p);
292 if(block == NULL || blockpos != blockpos_last){
293 block = m_map->getBlockNoCreate(blockpos);
294 blockpos_last = blockpos;
295 block_checked_in_modified = false;
298 // Calculate relative position in block
299 v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
301 // Don't continue if nothing has changed here
302 if(block->getNode(relpos) == n)
305 //m_map->setNode(m_area.MinEdge + p, n);
306 block->setNode(relpos, n);
309 Make sure block is in modified_blocks
311 if(block_checked_in_modified == false)
313 modified_blocks[blockpos] = block;
314 block_checked_in_modified = true;
317 catch(InvalidPositionException &e)