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():
29 VoxelManipulator::~VoxelManipulator()
38 void VoxelManipulator::clear()
40 // Reset area to volume=0
50 void VoxelManipulator::print(std::ostream &o)
52 v3s16 em = m_area.getExtent();
53 v3s16 of = m_area.MinEdge;
54 o<<"size: "<<em.X<<"x"<<em.Y<<"x"<<em.Z
55 <<" offset: ("<<of.X<<","<<of.Y<<","<<of.Z<<")"<<std::endl;
57 for(s32 y=m_area.MaxEdge.Y; y>=m_area.MinEdge.Y; y--)
59 if(em.X >= 3 && em.Y >= 3)
61 if (y==m_area.MinEdge.Y+2) o<<"^ ";
62 else if(y==m_area.MinEdge.Y+1) o<<"| ";
63 else if(y==m_area.MinEdge.Y+0) o<<"y x-> ";
67 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
69 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
71 u8 f = m_flags[m_area.index(x,y,z)];
73 if(f & VOXELFLAG_NOT_LOADED)
75 else if(f & VOXELFLAG_INEXISTENT)
80 u8 m = m_data[m_area.index(x,y,z)].d;
92 void VoxelManipulator::addArea(VoxelArea area)
94 // Cancel if requested area has zero volume
95 if(area.getExtent() == v3s16(0,0,0))
98 // Cancel if m_area already contains the requested area
99 if(m_area.contains(area))
102 // Calculate new area
104 // New area is the requested area if m_area has zero volume
105 if(m_area.getExtent() == v3s16(0,0,0))
109 // Else add requested area to m_area
113 new_area.addArea(area);
116 s32 new_size = new_area.getVolume();
118 /*dstream<<"adding area ";
120 dstream<<", old area ";
121 m_area.print(dstream);
122 dstream<<", new area ";
123 new_area.print(dstream);
124 dstream<<", new_size="<<new_size;
125 dstream<<std::endl;*/
127 // Allocate and clear new data
128 MapNode *new_data = new MapNode[new_size];
129 u8 *new_flags = new u8[new_size];
130 for(s32 i=0; i<new_size; i++)
132 new_flags[i] = VOXELFLAG_NOT_LOADED;
137 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
138 for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
139 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
141 // If loaded, copy data and flags
142 if((m_flags[m_area.index(x,y,z)] & VOXELFLAG_NOT_LOADED) == false)
144 new_data[new_area.index(x,y,z)] = m_data[m_area.index(x,y,z)];
145 new_flags[new_area.index(x,y,z)] = m_flags[m_area.index(x,y,z)];
149 // Replace area, data and flags
153 MapNode *old_data = m_data;
154 u8 *old_flags = m_flags;
156 /*dstream<<"old_data="<<(int)old_data<<", new_data="<<(int)new_data
157 <<", old_flags="<<(int)m_flags<<", new_flags="<<(int)new_flags<<std::endl;*/
168 void VoxelManipulator::interpolate(VoxelArea area)
170 VoxelArea emerge_area = area;
171 emerge_area.MinEdge -= v3s16(1,1,1);
172 emerge_area.MaxEdge += v3s16(1,1,1);
175 SharedBuffer<u8> buf(area.getVolume());
177 for(s32 z=area.MinEdge.Z; z<=area.MaxEdge.Z; z++)
178 for(s32 y=area.MinEdge.Y; y<=area.MaxEdge.Y; y++)
179 for(s32 x=area.MinEdge.X; x<=area.MaxEdge.X; x++)
193 //const v3s16 *dirs = g_26dirs;
197 u8 m = MATERIAL_IGNORE;
199 for(s16 i=0; i<8; i++)
200 //for(s16 i=0; i<26; i++)
202 v3s16 p2 = p + dirs[i];
204 u8 f = m_flags[m_area.index(p2)];
205 assert(!(f & VOXELFLAG_NOT_LOADED));
206 if(f & VOXELFLAG_INEXISTENT)
209 MapNode &n = m_data[m_area.index(p2)];
211 airness += (n.d == MATERIAL_AIR) ? 1 : -1;
214 if(m == MATERIAL_IGNORE && n.d != MATERIAL_AIR)
218 // 1 if air, 0 if not
219 buf[area.index(p)] = airness > -total/2 ? MATERIAL_AIR : m;
220 //buf[area.index(p)] = airness > -total ? MATERIAL_AIR : m;
221 //buf[area.index(p)] = airness >= -7 ? MATERIAL_AIR : m;
224 for(s32 z=area.MinEdge.Z; z<=area.MaxEdge.Z; z++)
225 for(s32 y=area.MinEdge.Y; y<=area.MaxEdge.Y; y++)
226 for(s32 x=area.MinEdge.X; x<=area.MaxEdge.X; x++)
229 m_data[m_area.index(p)].d = buf[area.index(p)];
233 void VoxelManipulator::flowWater(v3s16 removed_pos)
237 v3s16(-1,0,0), // left
238 v3s16(1,0,0), // right
239 v3s16(0,0,-1), // front
240 v3s16(0,0,1), // back
241 v3s16(0,-1,0), // bottom
246 // Load neighboring nodes
247 // TODO: A bigger area would be better
248 emerge(VoxelArea(removed_pos - v3s16(1,1,1), removed_pos + v3s16(1,1,1)));
253 p = removed_pos + dirs[i];
254 u8 f = m_flags[m_area.index(p)];
255 // Inexistent or checked nodes can't move
256 if(f & (VOXELFLAG_INEXISTENT | VOXELFLAG_CHECKED))
258 MapNode &n = m_data[m_area.index(p)];
259 // Only liquid nodes can move
260 if(material_liquid(n.d) == false)
262 // If block is at top, select it always
267 // If block is at bottom, select it if it has enough pressure
274 // Else block is at some side. Select it if it has enough pressure
281 // If there is nothing to move, return
285 // Switch nodes at p and removed_pos
286 MapNode n = m_data[m_area.index(p)];
287 u8 f = m_flags[m_area.index(p)];
288 m_data[m_area.index(p)] = m_data[m_area.index(removed_pos)];
289 m_flags[m_area.index(p)] = m_flags[m_area.index(removed_pos)];
290 m_data[m_area.index(removed_pos)] = n;
291 m_flags[m_area.index(removed_pos)] = f;
294 m_flags[m_area.index(p)] |= VOXELFLAG_CHECKED;
299 // Flow water to the newly created empty position
302 // Try flowing water to empty positions around removed_pos.
303 // They are checked in reverse order compared to the previous loop.
306 p = removed_pos + dirs[i];
307 u8 f = m_flags[m_area.index(p)];
308 // Water can't move to inexistent nodes
309 if(f & VOXELFLAG_INEXISTENT)
311 MapNode &n = m_data[m_area.index(p)];
312 // Water can only move to air
313 if(n.d != MATERIAL_AIR)
323 MapVoxelManipulator::MapVoxelManipulator(Map *map)
328 void MapVoxelManipulator::emerge(VoxelArea a)
330 v3s16 size = a.getExtent();
334 for(s16 z=0; z<size.Z; z++)
335 for(s16 y=0; y<size.Y; y++)
336 for(s16 x=0; x<size.X; x++)
339 s32 i = m_area.index(a.MinEdge + p);
340 // Don't touch nodes that have already been loaded
341 if(!(m_flags[i] & VOXELFLAG_NOT_LOADED))
344 MapNode n = m_map->getNode(a.MinEdge + p);
348 catch(InvalidPositionException &e)
350 m_flags[i] = VOXELFLAG_INEXISTENT;
355 void MapVoxelManipulator::blitBack
356 (core::map<v3s16, MapBlock*> & modified_blocks)
359 Initialize block cache
362 MapBlock *block = NULL;
363 bool block_checked_in_modified = false;
365 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
366 for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
367 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
371 u8 f = m_flags[m_area.index(p)];
372 if(f & (VOXELFLAG_NOT_LOADED|VOXELFLAG_INEXISTENT))
375 MapNode &n = m_data[m_area.index(p)];
377 v3s16 blockpos = getNodeBlockPos(p);
382 if(block == NULL || blockpos != blockpos_last){
383 block = m_map->getBlockNoCreate(blockpos);
384 blockpos_last = blockpos;
385 block_checked_in_modified = false;
388 // Calculate relative position in block
389 v3s16 relpos = p - blockpos * MAP_BLOCKSIZE;
391 // Don't continue if nothing has changed here
392 if(block->getNode(relpos) == n)
395 //m_map->setNode(m_area.MinEdge + p, n);
396 block->setNode(relpos, n);
399 Make sure block is in modified_blocks
401 if(block_checked_in_modified == false)
403 modified_blocks[blockpos] = block;
404 block_checked_in_modified = true;
407 catch(InvalidPositionException &e)