license 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         TODO: 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()
75         {
76                 return MaxEdge - MinEdge + v3s16(1,1,1);
77         }
78         s32 getVolume()
79         {
80                 v3s16 e = getExtent();
81                 return (s32)e.X * (s32)e.Y * (s32)e.Z;
82         }
83         bool isInside(v3s16 p)
84         {
85                 return(
86                         p.X >= MinEdge.X && p.X <= MaxEdge.X &&
87                         p.Y >= MinEdge.Y && p.Y <= MaxEdge.Y &&
88                         p.Z >= MinEdge.Z && p.Z <= MaxEdge.Z
89                 );
90         }
91         bool operator==(const VoxelArea &other)
92         {
93                 return (MinEdge == other.MinEdge
94                                 && MaxEdge == other.MaxEdge);
95         }
96         
97         /*
98                 Translates position from virtual coordinates to array index
99         */
100         s32 index(s16 x, s16 y, s16 z)
101         {
102                 v3s16 em = getExtent();
103                 v3s16 off = MinEdge;
104                 s32 i = (s32)(z-off.Z)*em.Y*em.X + (y-off.Y)*em.X + (x-off.X);
105                 //dstream<<" i("<<x<<","<<y<<","<<z<<")="<<i<<" ";
106                 return i;
107         }
108         s32 index(v3s16 p)
109         {
110                 return index(p.X, p.Y, p.Z);
111         }
112
113         void print(std::ostream &o)
114         {
115                 o<<"("<<MinEdge.X
116                  <<","<<MinEdge.Y
117                  <<","<<MinEdge.Z
118                  <<")("<<MaxEdge.X
119                  <<","<<MaxEdge.Y
120                  <<","<<MaxEdge.Z
121                  <<")";
122         }
123
124         // Edges are inclusive
125         v3s16 MinEdge;
126         v3s16 MaxEdge;
127 };
128
129 class VoxelManipulator : public NodeContainer
130 {
131 public:
132         VoxelManipulator();
133         ~VoxelManipulator();
134         
135         /*
136                 Virtuals from NodeContainer
137         */
138         virtual u16 nodeContainerId() const
139         {
140                 return NODECONTAINER_ID_VOXELMANIPULATOR;
141         }
142         bool isValidPosition(v3s16 p)
143         {
144                 return m_area.isInside(p);
145         }
146         // These are a bit slow and shouldn't be used internally
147         MapNode getNode(v3s16 p)
148         {
149                 if(isValidPosition(p) == false)
150                         emerge(VoxelArea(p));
151
152                 MapNode &n = m_data[m_area.index(p)];
153
154                 //TODO: Is this the right behaviour?
155                 if(n.d == MATERIAL_IGNORE)
156                         throw InvalidPositionException
157                         ("Not returning MATERIAL_IGNORE in VoxelManipulator");
158
159                 return n;
160         }
161         void setNode(v3s16 p, MapNode & n)
162         {
163                 if(isValidPosition(p) == false)
164                         emerge(VoxelArea(p));
165                 m_data[m_area.index(p)] = n;
166         }
167         
168         MapNode & operator[](v3s16 p)
169         {
170                 //dstream<<"operator[] p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
171                 if(isValidPosition(p) == false)
172                         emerge(VoxelArea(p));
173                 return m_data[m_area.index(p)];
174         }
175
176         /*
177                 Manipulation of bigger chunks
178         */
179         
180         void print(std::ostream &o);
181         
182         void addArea(VoxelArea area);
183
184         void interpolate(VoxelArea area);
185
186         /*void blitFromNodeContainer
187                         (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c);
188         
189         void blitToNodeContainer
190                         (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c);*/
191
192         /*
193                 Virtual functions
194         */
195         
196         /*
197                 Get the contents of the requested area from somewhere.
198
199                 If not found from source, add as MATERIAL_IGNORE.
200         */
201         virtual void emerge(VoxelArea a)
202         {
203                 //dstream<<"emerge p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
204                 addArea(a);
205         }
206
207         /*
208                 Member variables
209         */
210
211         /*
212                 The area that is stored in m_data.
213                 addInternalBox should not be used if getExtent() == v3s16(0,0,0)
214                 MaxEdge is 1 higher than maximum allowed position
215         */
216         VoxelArea m_area;
217         /*
218                 NULL if data size is 0
219                 Data is stored as [z*h*w + y*h + x]
220                 Special data values:
221                         MATERIAL_IGNORE: Unspecified node
222         */
223         MapNode *m_data;
224 private:
225 };
226
227 class Map;
228
229 class MapVoxelManipulator : public VoxelManipulator
230 {
231 public:
232         MapVoxelManipulator(Map *map);
233
234         virtual void emerge(VoxelArea a);
235
236         void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);
237
238 private:
239         Map *m_map;
240 };
241
242 #endif
243