3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
24 #include "util/directiontables.h"
25 #include "util/timetaker.h"
26 #include <cstring> // memcpy, memset
33 u64 emerge_load_time = 0;
34 u64 clearflag_time = 0;
36 VoxelManipulator::~VoxelManipulator()
41 void VoxelManipulator::clear()
43 // Reset area to volume=0
51 void VoxelManipulator::print(std::ostream &o, const NodeDefManager *ndef,
54 const v3s16 &em = m_area.getExtent();
55 v3s16 of = m_area.MinEdge;
56 o<<"size: "<<em.X<<"x"<<em.Y<<"x"<<em.Z
57 <<" offset: ("<<of.X<<","<<of.Y<<","<<of.Z<<")"<<std::endl;
59 for(s32 y=m_area.MaxEdge.Y; y>=m_area.MinEdge.Y; y--)
61 if(em.X >= 3 && em.Y >= 3)
63 if (y==m_area.MinEdge.Y+2) o<<"^ ";
64 else if(y==m_area.MinEdge.Y+1) o<<"| ";
65 else if(y==m_area.MinEdge.Y+0) o<<"y x-> ";
69 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
71 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
73 u8 f = m_flags[m_area.index(x,y,z)];
75 if(f & VOXELFLAG_NO_DATA)
80 MapNode n = m_data[m_area.index(x,y,z)];
81 content_t m = n.getContent();
83 if(mode == VOXELPRINT_MATERIAL)
88 else if(mode == VOXELPRINT_WATERPRESSURE)
90 if(ndef->get(m).isLiquid())
96 else if(m == CONTENT_AIR)
105 else if(mode == VOXELPRINT_LIGHT_DAY)
107 if(ndef->get(m).light_source != 0)
109 else if(!ndef->get(m).light_propagates)
113 u8 light = n.getLight(LIGHTBANK_DAY, ndef);
117 c = 'a' + (light-10);
129 void VoxelManipulator::addArea(const VoxelArea &area)
131 // Cancel if requested area has zero volume
132 if (area.hasEmptyExtent())
135 // Cancel if m_area already contains the requested area
136 if(m_area.contains(area))
139 TimeTaker timer("addArea", &addarea_time);
141 // Calculate new area
143 // New area is the requested area if m_area has zero volume
144 if(m_area.hasEmptyExtent())
148 // Else add requested area to m_area
152 new_area.addArea(area);
155 s32 new_size = new_area.getVolume();
157 /*dstream<<"adding area ";
159 dstream<<", old area ";
160 m_area.print(dstream);
161 dstream<<", new area ";
162 new_area.print(dstream);
163 dstream<<", new_size="<<new_size;
164 dstream<<std::endl;*/
166 // Allocate new data and clear flags
167 MapNode *new_data = new MapNode[new_size];
169 u8 *new_flags = new u8[new_size];
171 memset(new_flags, VOXELFLAG_NO_DATA, new_size);
174 s32 old_x_width = m_area.MaxEdge.X - m_area.MinEdge.X + 1;
175 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
176 for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
178 unsigned int old_index = m_area.index(m_area.MinEdge.X,y,z);
179 unsigned int new_index = new_area.index(m_area.MinEdge.X,y,z);
181 memcpy(&new_data[new_index], &m_data[old_index],
182 old_x_width * sizeof(MapNode));
183 memcpy(&new_flags[new_index], &m_flags[old_index],
184 old_x_width * sizeof(u8));
187 // Replace area, data and flags
191 MapNode *old_data = m_data;
192 u8 *old_flags = m_flags;
194 /*dstream<<"old_data="<<(int)old_data<<", new_data="<<(int)new_data
195 <<", old_flags="<<(int)m_flags<<", new_flags="<<(int)new_flags<<std::endl;*/
203 //dstream<<"addArea done"<<std::endl;
206 void VoxelManipulator::copyFrom(MapNode *src, const VoxelArea& src_area,
207 v3s16 from_pos, v3s16 to_pos, const v3s16 &size)
209 /* The reason for this optimised code is that we're a member function
210 * and the data type/layout of m_data is know to us: it's stored as
211 * [z*h*w + y*h + x]. Therefore we can take the calls to m_area index
212 * (which performs the preceding mapping/indexing of m_data) out of the
213 * inner loop and calculate the next index as we're iterating to gain
216 * src_step and dest_step is the amount required to be added to our index
217 * every time y increments. Because the destination area may be larger
218 * than the source area we need one additional variable (otherwise we could
219 * just continue adding dest_step as is done for the source data): dest_mod.
220 * dest_mod is the difference in size between a "row" in the source data
221 * and a "row" in the destination data (I am using the term row loosely
222 * and for illustrative purposes). E.g.
224 * src <-------------------->|'''''' dest mod ''''''''
225 * dest <--------------------------------------------->
227 * dest_mod (it's essentially a modulus) is added to the destination index
228 * after every full iteration of the y span.
230 * This method falls under the category "linear array and incrementing
234 s32 src_step = src_area.getExtent().X;
235 s32 dest_step = m_area.getExtent().X;
236 s32 dest_mod = m_area.index(to_pos.X, to_pos.Y, to_pos.Z + 1)
237 - m_area.index(to_pos.X, to_pos.Y, to_pos.Z)
238 - dest_step * size.Y;
240 s32 i_src = src_area.index(from_pos.X, from_pos.Y, from_pos.Z);
241 s32 i_local = m_area.index(to_pos.X, to_pos.Y, to_pos.Z);
243 for (s16 z = 0; z < size.Z; z++) {
244 for (s16 y = 0; y < size.Y; y++) {
245 memcpy(&m_data[i_local], &src[i_src], size.X * sizeof(*m_data));
246 memset(&m_flags[i_local], 0, size.X);
248 i_local += dest_step;
254 void VoxelManipulator::copyTo(MapNode *dst, const VoxelArea& dst_area,
255 v3s16 dst_pos, v3s16 from_pos, const v3s16 &size)
257 for(s16 z=0; z<size.Z; z++)
258 for(s16 y=0; y<size.Y; y++)
260 s32 i_dst = dst_area.index(dst_pos.X, dst_pos.Y+y, dst_pos.Z+z);
261 s32 i_local = m_area.index(from_pos.X, from_pos.Y+y, from_pos.Z+z);
262 for (s16 x = 0; x < size.X; x++) {
263 if (m_data[i_local].getContent() != CONTENT_IGNORE)
264 dst[i_dst] = m_data[i_local];
273 -----------------------------------------------------
276 void VoxelManipulator::clearFlag(u8 flags)
278 // 0-1ms on moderate area
279 TimeTaker timer("clearFlag", &clearflag_time);
281 //v3s16 s = m_area.getExtent();
283 /*dstream<<"clearFlag clearing area of size "
284 <<""<<s.X<<"x"<<s.Y<<"x"<<s.Z<<""
289 /*for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
290 for(s32 y=m_area.MinEdge.Y; y<=m_area.MaxEdge.Y; y++)
291 for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
293 u8 f = m_flags[m_area.index(x,y,z)];
294 m_flags[m_area.index(x,y,z)] &= ~flags;
295 if(m_flags[m_area.index(x,y,z)] != f)
299 s32 volume = m_area.getVolume();
300 for(s32 i=0; i<volume; i++)
302 m_flags[i] &= ~flags;
305 /*s32 volume = m_area.getVolume();
306 for(s32 i=0; i<volume; i++)
309 m_flags[i] &= ~flags;
314 dstream<<"clearFlag changed "<<count<<" flags out of "
315 <<volume<<" nodes"<<std::endl;*/
318 const MapNode VoxelManipulator::ContentIgnoreNode = MapNode(CONTENT_IGNORE);