remove_detached_inventory: Fix segfault during mod load
[oweals/minetest.git] / src / voxel.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "voxel.h"
21 #include "map.h"
22 #include "gettime.h"
23 #include "nodedef.h"
24 #include "util/directiontables.h"
25 #include "util/timetaker.h"
26 #include <cstring>  // memcpy, memset
27
28 /*
29         Debug stuff
30 */
31 u64 addarea_time = 0;
32 u64 emerge_time = 0;
33 u64 emerge_load_time = 0;
34 u64 clearflag_time = 0;
35
36 VoxelManipulator::~VoxelManipulator()
37 {
38         clear();
39 }
40
41 void VoxelManipulator::clear()
42 {
43         // Reset area to volume=0
44         m_area = VoxelArea();
45         delete[] m_data;
46         m_data = nullptr;
47         delete[] m_flags;
48         m_flags = nullptr;
49 }
50
51 void VoxelManipulator::print(std::ostream &o, const NodeDefManager *ndef,
52         VoxelPrintMode mode)
53 {
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;
58
59         for(s32 y=m_area.MaxEdge.Y; y>=m_area.MinEdge.Y; y--)
60         {
61                 if(em.X >= 3 && em.Y >= 3)
62                 {
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-> ";
66                         else                           o<<"      ";
67                 }
68
69                 for(s32 z=m_area.MinEdge.Z; z<=m_area.MaxEdge.Z; z++)
70                 {
71                         for(s32 x=m_area.MinEdge.X; x<=m_area.MaxEdge.X; x++)
72                         {
73                                 u8 f = m_flags[m_area.index(x,y,z)];
74                                 char c;
75                                 if(f & VOXELFLAG_NO_DATA)
76                                         c = 'N';
77                                 else
78                                 {
79                                         c = 'X';
80                                         MapNode n = m_data[m_area.index(x,y,z)];
81                                         content_t m = n.getContent();
82                                         u8 pr = n.param2;
83                                         if(mode == VOXELPRINT_MATERIAL)
84                                         {
85                                                 if(m <= 9)
86                                                         c = m + '0';
87                                         }
88                                         else if(mode == VOXELPRINT_WATERPRESSURE)
89                                         {
90                                                 if(ndef->get(m).isLiquid())
91                                                 {
92                                                         c = 'w';
93                                                         if(pr <= 9)
94                                                                 c = pr + '0';
95                                                 }
96                                                 else if(m == CONTENT_AIR)
97                                                 {
98                                                         c = ' ';
99                                                 }
100                                                 else
101                                                 {
102                                                         c = '#';
103                                                 }
104                                         }
105                                         else if(mode == VOXELPRINT_LIGHT_DAY)
106                                         {
107                                                 if(ndef->get(m).light_source != 0)
108                                                         c = 'S';
109                                                 else if(!ndef->get(m).light_propagates)
110                                                         c = 'X';
111                                                 else
112                                                 {
113                                                         u8 light = n.getLight(LIGHTBANK_DAY, ndef);
114                                                         if(light < 10)
115                                                                 c = '0' + light;
116                                                         else
117                                                                 c = 'a' + (light-10);
118                                                 }
119                                         }
120                                 }
121                                 o<<c;
122                         }
123                         o<<' ';
124                 }
125                 o<<std::endl;
126         }
127 }
128
129 void VoxelManipulator::addArea(const VoxelArea &area)
130 {
131         // Cancel if requested area has zero volume
132         if (area.hasEmptyExtent())
133                 return;
134
135         // Cancel if m_area already contains the requested area
136         if(m_area.contains(area))
137                 return;
138
139         TimeTaker timer("addArea", &addarea_time);
140
141         // Calculate new area
142         VoxelArea new_area;
143         // New area is the requested area if m_area has zero volume
144         if(m_area.hasEmptyExtent())
145         {
146                 new_area = area;
147         }
148         // Else add requested area to m_area
149         else
150         {
151                 new_area = m_area;
152                 new_area.addArea(area);
153         }
154
155         s32 new_size = new_area.getVolume();
156
157         /*dstream<<"adding area ";
158         area.print(dstream);
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;*/
165
166         // Allocate new data and clear flags
167         MapNode *new_data = new MapNode[new_size];
168         assert(new_data);
169         u8 *new_flags = new u8[new_size];
170         assert(new_flags);
171         memset(new_flags, VOXELFLAG_NO_DATA, new_size);
172
173         // Copy old data
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++)
177         {
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);
180
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));
185         }
186
187         // Replace area, data and flags
188
189         m_area = new_area;
190
191         MapNode *old_data = m_data;
192         u8 *old_flags = m_flags;
193
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;*/
196
197         m_data = new_data;
198         m_flags = new_flags;
199
200         delete[] old_data;
201         delete[] old_flags;
202
203         //dstream<<"addArea done"<<std::endl;
204 }
205
206 void VoxelManipulator::copyFrom(MapNode *src, const VoxelArea& src_area,
207                 v3s16 from_pos, v3s16 to_pos, const v3s16 &size)
208 {
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
214          * performance.
215          *
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.
223          *
224          * src       <-------------------->|'''''' dest mod ''''''''
225          * dest      <--------------------------------------------->
226          *
227          * dest_mod (it's essentially a modulus) is added to the destination index
228          * after every full iteration of the y span.
229          *
230          * This method falls under the category "linear array and incrementing
231          * index".
232          */
233
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;
239
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);
242
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);
247                         i_src += src_step;
248                         i_local += dest_step;
249                 }
250                 i_local += dest_mod;
251         }
252 }
253
254 void VoxelManipulator::copyTo(MapNode *dst, const VoxelArea& dst_area,
255                 v3s16 dst_pos, v3s16 from_pos, const v3s16 &size)
256 {
257         for(s16 z=0; z<size.Z; z++)
258         for(s16 y=0; y<size.Y; y++)
259         {
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];
265                         i_dst++;
266                         i_local++;
267                 }
268         }
269 }
270
271 /*
272         Algorithms
273         -----------------------------------------------------
274 */
275
276 void VoxelManipulator::clearFlag(u8 flags)
277 {
278         // 0-1ms on moderate area
279         TimeTaker timer("clearFlag", &clearflag_time);
280
281         //v3s16 s = m_area.getExtent();
282
283         /*dstream<<"clearFlag clearing area of size "
284                         <<""<<s.X<<"x"<<s.Y<<"x"<<s.Z<<""
285                         <<std::endl;*/
286
287         //s32 count = 0;
288
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++)
292         {
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)
296                         count++;
297         }*/
298
299         s32 volume = m_area.getVolume();
300         for(s32 i=0; i<volume; i++)
301         {
302                 m_flags[i] &= ~flags;
303         }
304
305         /*s32 volume = m_area.getVolume();
306         for(s32 i=0; i<volume; i++)
307         {
308                 u8 f = m_flags[i];
309                 m_flags[i] &= ~flags;
310                 if(m_flags[i] != f)
311                         count++;
312         }
313
314         dstream<<"clearFlag changed "<<count<<" flags out of "
315                         <<volume<<" nodes"<<std::endl;*/
316 }
317
318 const MapNode VoxelManipulator::ContentIgnoreNode = MapNode(CONTENT_IGNORE);
319
320 //END