3 Copyright (C) 2010 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 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.
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.
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.
23 #include "common_irrlicht.h"
28 A fast voxel manipulator class
34 This class resembles aabbox3d<s16> a lot, but has inclusive
35 edges for saner handling of integer sizes
40 // Starts as zero sized
46 VoxelArea(v3s16 min_edge, v3s16 max_edge):
56 void addArea(VoxelArea &a)
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;
65 void addPoint(v3s16 p)
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;
74 v3s16 getExtent() const
76 return MaxEdge - MinEdge + v3s16(1,1,1);
80 v3s16 e = getExtent();
81 return (s32)e.X * (s32)e.Y * (s32)e.Z;
83 bool contains(VoxelArea &a) const
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
91 bool contains(v3s16 p) const
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
99 bool operator==(const VoxelArea &other) const
101 return (MinEdge == other.MinEdge
102 && MaxEdge == other.MaxEdge);
106 Translates position from virtual coordinates to array index
108 s32 index(s16 x, s16 y, s16 z) const
110 v3s16 em = getExtent();
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<<" ";
116 s32 index(v3s16 p) const
118 return index(p.X, p.Y, p.Z);
121 void print(std::ostream &o) const
132 // Edges are inclusive
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)
144 class VoxelManipulator : public NodeContainer
151 Virtuals from NodeContainer
153 virtual u16 nodeContainerId() const
155 return NODECONTAINER_ID_VOXELMANIPULATOR;
157 bool isValidPosition(v3s16 p)
160 return !(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT);
162 // These are a bit slow and shouldn't be used internally
163 MapNode getNode(v3s16 p)
167 if(m_flags[m_area.index(p)] & VOXELFLAG_INEXISTENT)
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");
178 return m_data[m_area.index(p)];
180 void setNode(v3s16 p, MapNode &n)
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;
188 void setNodeNoRef(v3s16 p, MapNode n)
193 /*void setExists(VoxelArea 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++)
200 m_flags[m_area.index(x,y,z)] &= ~VOXELFLAG_INEXISTENT;
204 /*MapNode & operator[](v3s16 p)
206 //dstream<<"operator[] p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
207 if(isValidPosition(p) == false)
208 emerge(VoxelArea(p));
210 return m_data[m_area.index(p)];
219 void print(std::ostream &o);
221 void addArea(VoxelArea area);
227 void interpolate(VoxelArea area);
229 void flowWater(v3s16 removed_pos);
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
240 If not found from source, add with VOXELFLAG_INEXISTENT
242 virtual void emerge(VoxelArea a)
244 //dstream<<"emerge p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
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++)
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))
254 m_flags[i] = VOXELFLAG_INEXISTENT;
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
269 NULL if data size is 0 (extent (0,0,0))
270 Data is stored as [z*h*w + y*h + x]
282 class MapVoxelManipulator : public VoxelManipulator
285 MapVoxelManipulator(Map *map);
287 virtual void emerge(VoxelArea a);
289 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);