some work-in-progress water stuff
[oweals/minetest.git] / src / voxel.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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 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.
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 General Public License for more details.
14
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.
18 */
19
20 #ifndef VOXEL_HEADER
21 #define VOXEL_HEADER
22
23 #include "common_irrlicht.h"
24 #include "mapblock.h"
25 #include <iostream>
26
27 /*
28         A fast voxel manipulator class
29
30         Not thread-safe.
31 */
32
33 /*
34         This class resembles aabbox3d<s16> a lot, but has inclusive
35         edges for saner handling of integer sizes
36 */
37 class VoxelArea
38 {
39 public:
40         // Starts as zero sized
41         VoxelArea():
42                 MinEdge(1,1,1),
43                 MaxEdge(0,0,0)
44         {
45         }
46         VoxelArea(v3s16 min_edge, v3s16 max_edge):
47                 MinEdge(min_edge),
48                 MaxEdge(max_edge)
49         {
50         }
51         VoxelArea(v3s16 p):
52                 MinEdge(p),
53                 MaxEdge(p)
54         {
55         }
56         void addArea(VoxelArea &a)
57         {
58                 if(a.MinEdge.X < MinEdge.X) MinEdge.X = a.MinEdge.X;
59                 if(a.MinEdge.Y < MinEdge.Y) MinEdge.Y = a.MinEdge.Y;
60                 if(a.MinEdge.Z < MinEdge.Z) MinEdge.Z = a.MinEdge.Z;
61                 if(a.MaxEdge.X > MaxEdge.X) MaxEdge.X = a.MaxEdge.X;
62                 if(a.MaxEdge.Y > MaxEdge.Y) MaxEdge.Y = a.MaxEdge.Y;
63                 if(a.MaxEdge.Z > MaxEdge.Z) MaxEdge.Z = a.MaxEdge.Z;
64         }
65         void addPoint(v3s16 p)
66         {
67                 if(p.X < MinEdge.X) MinEdge.X = p.X;
68                 if(p.Y < MinEdge.Y) MinEdge.Y = p.Y;
69                 if(p.Z < MinEdge.Z) MinEdge.Z = p.Z;
70                 if(p.X > MaxEdge.X) MaxEdge.X = p.X;
71                 if(p.Y > MaxEdge.Y) MaxEdge.Y = p.Y;
72                 if(p.Z > MaxEdge.Z) MaxEdge.Z = p.Z;
73         }
74         v3s16 getExtent() const
75         {
76                 return MaxEdge - MinEdge + v3s16(1,1,1);
77         }
78         s32 getVolume() const
79         {
80                 v3s16 e = getExtent();
81                 return (s32)e.X * (s32)e.Y * (s32)e.Z;
82         }
83         bool contains(VoxelArea &a) const
84         {
85                 return(
86                         a.MinEdge.X >= MinEdge.X && a.MaxEdge.X <= MaxEdge.X &&
87                         a.MinEdge.Y >= MinEdge.Y && a.MaxEdge.Y <= MaxEdge.Y &&
88                         a.MinEdge.Z >= MinEdge.Z && a.MaxEdge.Z <= MaxEdge.Z
89                 );
90         }
91         bool contains(v3s16 p) const
92         {
93                 return(
94                         p.X >= MinEdge.X && p.X <= MaxEdge.X &&
95                         p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
96                         p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z
97                 );
98         }
99         bool operator==(const VoxelArea &other) const
100         {
101                 return (MinEdge == other.MinEdge
102                                 && MaxEdge == other.MaxEdge);
103         }
104         
105         /*
106                 Translates position from virtual coordinates to array index
107         */
108         s32 index(s16 x, s16 y, s16 z) const
109         {
110                 v3s16 em = getExtent();
111                 v3s16 off = MinEdge;
112                 s32 i = (s32)(z-off.Z)*em.Y*em.X + (y-off.Y)*em.X + (x-off.X);
113                 //dstream<<" i("<<x<<","<<y<<","<<z<<")="<<i<<" ";
114                 return i;
115         }
116         s32 index(v3s16 p) const
117         {
118                 return index(p.X, p.Y, p.Z);
119         }
120
121         void print(std::ostream &o) const
122         {
123                 o<<"("<<MinEdge.X
124                  <<","<<MinEdge.Y
125                  <<","<<MinEdge.Z
126                  <<")("<<MaxEdge.X
127                  <<","<<MaxEdge.Y
128                  <<","<<MaxEdge.Z
129                  <<")";
130         }
131
132         // Edges are inclusive
133         v3s16 MinEdge;
134         v3s16 MaxEdge;
135 };
136
137 // Hasn't been copied from source (emerged)
138 #define VOXELFLAG_NOT_LOADED (1<<0)
139 // Checked as being inexistent in source
140 #define VOXELFLAG_INEXISTENT (1<<1)
141 // Algorithm-dependent
142 #define VOXELFLAG_CHECKED (1<<2)
143
144 class VoxelManipulator : public NodeContainer
145 {
146 public:
147         VoxelManipulator();
148         ~VoxelManipulator();
149         
150         /*
151                 Virtuals from NodeContainer
152         */
153         virtual u16 nodeContainerId() const
154         {
155                 return NODECONTAINER_ID_VOXELMANIPULATOR;
156         }
157         bool isValidPosition(v3s16 p)
158         {
159                 emerge(p);
160                 return !(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT);
161         }
162         // These are a bit slow and shouldn't be used internally
163         MapNode getNode(v3s16 p)
164         {
165                 emerge(p);
166
167                 if(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT)
168                 {
169                         dstream<<"ERROR: VoxelManipulator::getNode(): "
170                                         <<"p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"
171                                         <<", index="<<m_area.index(p)
172                                         <<", flags="<<(int)m_flags[m_area.index(p)]
173                                         <<" is inexistent"<<std::endl;
174                         throw InvalidPositionException
175                         ("VoxelManipulator: getNode: inexistent");
176                 }
177
178                 return m_data[m_area.index(p)];
179         }
180         void setNode(v3s16 p, MapNode &n)
181         {
182                 emerge(p);
183                 
184                 m_data[m_area.index(p)] = n;
185                 m_flags[m_area.index(p)] &= ~VOXELFLAG_INEXISTENT;
186                 m_flags[m_area.index(p)] &= ~VOXELFLAG_NOT_LOADED;
187         }
188         void setNodeNoRef(v3s16 p, MapNode n)
189         {
190                 setNode(p, n);
191         }
192
193         /*void setExists(VoxelArea a)
194         {
195                 emerge(a);
196                 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
197                 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
198                 for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
199                 {
200                         m_flags[m_area.index(x,y,z)] &= ~VOXELFLAG_INEXISTENT;
201                 }
202         }*/
203
204         /*MapNode & operator[](v3s16 p)
205         {
206                 //dstream<<"operator[] p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
207                 if(isValidPosition(p) == false)
208                         emerge(VoxelArea(p));
209                 
210                 return m_data[m_area.index(p)];
211         }*/
212
213         /*
214                 Control
215         */
216
217         void clear();
218         
219         void print(std::ostream &o);
220         
221         void addArea(VoxelArea area);
222
223         /*
224                 Algorithms
225         */
226
227         void interpolate(VoxelArea area);
228
229         void flowWater(v3s16 removed_pos);
230
231         /*
232                 Virtual functions
233         */
234         
235         /*
236                 Get the contents of the requested area from somewhere.
237                 Shall touch only nodes that have VOXELFLAG_NOT_LOADED
238                 Shall reset VOXELFLAG_NOT_LOADED
239
240                 If not found from source, add with VOXELFLAG_INEXISTENT
241         */
242         virtual void emerge(VoxelArea a)
243         {
244                 //dstream<<"emerge p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
245                 addArea(a);
246                 for(s32 z=a.MinEdge.Z; z<=a.MaxEdge.Z; z++)
247                 for(s32 y=a.MinEdge.Y; y<=a.MaxEdge.Y; y++)
248                 for(s32 x=a.MinEdge.X; x<=a.MaxEdge.X; x++)
249                 {
250                         s32 i = m_area.index(x,y,z);
251                         // Don't touch nodes that have already been loaded
252                         if(!(m_flags[i] & VOXELFLAG_NOT_LOADED))
253                                 continue;
254                         m_flags[i] = VOXELFLAG_INEXISTENT;
255                 }
256         }
257
258         /*
259                 Member variables
260         */
261
262         /*
263                 The area that is stored in m_data.
264                 addInternalBox should not be used if getExtent() == v3s16(0,0,0)
265                 MaxEdge is 1 higher than maximum allowed position
266         */
267         VoxelArea m_area;
268         /*
269                 NULL if data size is 0 (extent (0,0,0))
270                 Data is stored as [z*h*w + y*h + x]
271         */
272         MapNode *m_data;
273         /*
274                 Flags of all nodes
275         */
276         u8 *m_flags;
277 private:
278 };
279
280 class Map;
281
282 class MapVoxelManipulator : public VoxelManipulator
283 {
284 public:
285         MapVoxelManipulator(Map *map);
286
287         virtual void emerge(VoxelArea a);
288
289         void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
290
291 private:
292         Map *m_map;
293 };
294
295 #endif
296