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 TODO: 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;
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 isInside(v3s16 p)
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
91 bool operator==(const VoxelArea &other)
93 return (MinEdge == other.MinEdge
94 && MaxEdge == other.MaxEdge);
98 Translates position from virtual coordinates to array index
100 s32 index(s16 x, s16 y, s16 z)
102 v3s16 em = getExtent();
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<<" ";
110 return index(p.X, p.Y, p.Z);
113 void print(std::ostream &o)
124 // Edges are inclusive
129 class VoxelManipulator : public NodeContainer
136 Virtuals from NodeContainer
138 virtual u16 nodeContainerId() const
140 return NODECONTAINER_ID_VOXELMANIPULATOR;
142 bool isValidPosition(v3s16 p)
144 return m_area.isInside(p);
146 // These are a bit slow and shouldn't be used internally
147 MapNode getNode(v3s16 p)
149 if(isValidPosition(p) == false)
150 emerge(VoxelArea(p));
152 MapNode &n = m_data[m_area.index(p)];
154 //TODO: Is this the right behaviour?
155 if(n.d == MATERIAL_IGNORE)
156 throw InvalidPositionException
157 ("Not returning MATERIAL_IGNORE in VoxelManipulator");
161 void setNode(v3s16 p, MapNode & n)
163 if(isValidPosition(p) == false)
164 emerge(VoxelArea(p));
165 m_data[m_area.index(p)] = n;
168 MapNode & operator[](v3s16 p)
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)];
177 Manipulation of bigger chunks
180 void print(std::ostream &o);
182 void addArea(VoxelArea area);
184 void interpolate(VoxelArea area);
186 /*void blitFromNodeContainer
187 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c);
189 void blitToNodeContainer
190 (v3s16 p_from, v3s16 p_to, v3s16 size, NodeContainer *c);*/
197 Get the contents of the requested area from somewhere.
199 If not found from source, add as MATERIAL_IGNORE.
201 virtual void emerge(VoxelArea a)
203 //dstream<<"emerge p=("<<p.X<<","<<p.Y<<","<<p.Z<<")"<<std::endl;
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
218 NULL if data size is 0
219 Data is stored as [z*h*w + y*h + x]
221 MATERIAL_IGNORE: Unspecified node
229 class MapVoxelManipulator : public VoxelManipulator
232 MapVoxelManipulator(Map *map);
234 virtual void emerge(VoxelArea a);
236 void blitBack(core::map<v3s16, MapBlock*> & modified_blocks);