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, INodeDefManager *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 void VoxelManipulator::unspreadLight(enum LightBank bank, v3s16 p, u8 oldlight,
319 std::set<v3s16> & light_sources, INodeDefManager *nodemgr)
321 VoxelArea voxel_area(p - v3s16(1,1,1), p + v3s16(1,1,1));
324 // Loop through 6 neighbors
325 for (const v3s16 &dir : g_6dirs) {
326 // Get the position of the neighbor node
327 v3s16 n2pos = p + dir;
329 u32 n2i = m_area.index(n2pos);
331 if(m_flags[n2i] & VOXELFLAG_NO_DATA)
334 MapNode &n2 = m_data[n2i];
337 If the neighbor is dimmer than what was specified
338 as oldlight (the light of the previous node)
340 u8 light2 = n2.getLight(bank, nodemgr);
341 if(light2 < oldlight)
344 And the neighbor is transparent and it has some light
346 if(nodemgr->get(n2).light_propagates && light2 != 0)
349 Set light to 0 and add to queue
352 n2.setLight(bank, 0, nodemgr);
354 unspreadLight(bank, n2pos, light2, light_sources, nodemgr);
357 Remove from light_sources if it is there
358 NOTE: This doesn't happen nearly at all
360 /*if(light_sources.find(n2pos))
362 std::cout<<"Removed from light_sources"<<std::endl;
363 light_sources.remove(n2pos);
368 light_sources.insert(n2pos);
373 void VoxelManipulator::spreadLight(enum LightBank bank, v3s16 p,
374 INodeDefManager *nodemgr)
376 VoxelArea voxel_area(p - v3s16(1,1,1), p + v3s16(1,1,1));
379 u32 i = m_area.index(p);
381 if(m_flags[i] & VOXELFLAG_NO_DATA)
384 MapNode &n = m_data[i];
386 u8 oldlight = n.getLight(bank, nodemgr);
387 u8 newlight = diminish_light(oldlight);
389 // Loop through 6 neighbors
390 for (const auto &dir : g_6dirs) {
391 // Get the position of the neighbor node
392 v3s16 n2pos = p + dir;
394 u32 n2i = m_area.index(n2pos);
396 if(m_flags[n2i] & VOXELFLAG_NO_DATA)
399 MapNode &n2 = m_data[n2i];
401 u8 light2 = n2.getLight(bank, nodemgr);
404 If the neighbor is brighter than the current node,
405 add to list (it will light up this node on its turn)
407 if(light2 > undiminish_light(oldlight))
409 spreadLight(bank, n2pos, nodemgr);
412 If the neighbor is dimmer than how much light this node
413 would spread on it, add to list
415 if(light2 < newlight)
417 if(nodemgr->get(n2).light_propagates)
419 n2.setLight(bank, newlight, nodemgr);
420 spreadLight(bank, n2pos, nodemgr);
427 const MapNode VoxelManipulator::ContentIgnoreNode = MapNode(CONTENT_IGNORE);
430 Lights neighbors of from_nodes, collects all them and then
433 void VoxelManipulator::spreadLight(enum LightBank bank,
434 std::set<v3s16> & from_nodes, INodeDefManager *nodemgr)
436 if(from_nodes.empty())
439 std::set<v3s16> lighted_nodes;
441 for (const v3s16 &pos : from_nodes) {
442 VoxelArea voxel_area(pos - v3s16(1,1,1), pos + v3s16(1,1,1));
445 u32 i = m_area.index(pos);
447 if(m_flags[i] & VOXELFLAG_NO_DATA)
450 MapNode &n = m_data[i];
452 u8 oldlight = n.getLight(bank, nodemgr);
453 u8 newlight = diminish_light(oldlight);
455 // Loop through 6 neighbors
456 for (const v3s16 &dir : g_6dirs) {
457 // Get the position of the neighbor node
458 v3s16 n2pos = pos + dir;
462 u32 n2i = m_area.index(n2pos);
464 if(m_flags[n2i] & VOXELFLAG_NO_DATA)
467 MapNode &n2 = m_data[n2i];
469 u8 light2 = n2.getLight(bank, nodemgr);
472 If the neighbor is brighter than the current node,
473 add to list (it will light up this node on its turn)
475 if(light2 > undiminish_light(oldlight))
477 lighted_nodes.insert(n2pos);
480 If the neighbor is dimmer than how much light this node
481 would spread on it, add to list
483 if(light2 < newlight)
485 if(nodemgr->get(n2).light_propagates)
487 n2.setLight(bank, newlight, nodemgr);
488 lighted_nodes.insert(n2pos);
492 catch(InvalidPositionException &e)
499 /*dstream<<"spreadLight(): Changed block "
500 <<blockchangecount<<" times"
501 <<" for "<<from_nodes.size()<<" nodes"
504 if(!lighted_nodes.empty())
505 spreadLight(bank, lighted_nodes, nodemgr);