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.
21 (c) 2010 Perttu Ahola <celeron55@gmail.com>
24 #ifndef MAPSECTOR_HEADER
25 #define MAPSECTOR_HEADER
28 #include "common_irrlicht.h"
30 //#include "heightmap.h"
31 #include "exceptions.h"
34 This is an Y-wise stack of MapBlocks.
37 #define MAPSECTOR_SERVER 0
38 #define MAPSECTOR_CLIENT 1
40 class MapSector: public NodeContainer
44 MapSector(NodeContainer *parent, v2s16 pos);
47 virtual u16 nodeContainerId() const
49 return NODECONTAINER_ID_MAPSECTOR;
52 virtual u32 getId() const = 0;
61 MapBlock * getBlockNoCreateNoEx(s16 y);
62 MapBlock * getBlockNoCreate(s16 y);
63 MapBlock * createBlankBlockNoInsert(s16 y);
64 MapBlock * createBlankBlock(s16 y);
65 //MapBlock * getBlock(s16 y, bool generate=true);
67 void insertBlock(MapBlock *block);
69 // This is used to remove a dummy from the sector while generating it.
70 // Block is only removed from internal container, not deleted.
71 void removeBlock(MapBlock *block);
74 This might not be a thread-safe depending on the day.
75 See the implementation.
77 void getBlocks(core::list<MapBlock*> &dest);
80 If all nodes in area can be accessed, returns true and
81 adds all blocks in area to blocks.
83 If all nodes in area cannot be accessed, returns false.
85 The implementation of this is quite slow
87 if blocks==NULL; it is not accessed at all.
89 bool isValidArea(v3s16 p_min_nodes, v3s16 p_max_nodes,
90 core::map<s16, MapBlock*> *blocks)
92 core::map<s16, MapBlock*> bs;
94 v3s16 p_min = getNodeBlockPos(p_min_nodes);
95 v3s16 p_max = getNodeBlockPos(p_max_nodes);
96 if(p_min.X != 0 || p_min.Z != 0
97 || p_max.X != 0 || p_max.Z != 0)
100 for(s16 y=p_min.Y; y<=p_max.Y; y++)
103 MapBlock *block = getBlockNoCreate(y);
109 catch(InvalidPositionException &e)
117 for(core::map<s16, MapBlock*>::Iterator i=bs.getIterator();
118 i.atEnd()==false; i++)
120 MapBlock *block = i.getNode()->getValue();
121 s16 y = i.getNode()->getKey();
122 blocks->insert(y, block);
128 void getBlocksInArea(v3s16 p_min_nodes, v3s16 p_max_nodes,
129 core::map<v3s16, MapBlock*> &blocks)
131 v3s16 p_min = getNodeBlockPos(p_min_nodes);
132 v3s16 p_max = getNodeBlockPos(p_max_nodes);
134 for(s16 y=p_min.Y; y<=p_max.Y; y++)
137 MapBlock *block = getBlockNoCreate(y);
138 blocks.insert(block->getPos(), block);
140 catch(InvalidPositionException &e)
146 // virtual from NodeContainer
147 bool isValidPosition(v3s16 p)
149 v3s16 blockpos = getNodeBlockPos(p);
151 if(blockpos.X != 0 || blockpos.Z != 0)
156 blockref = getBlockNoCreate(blockpos.Y);
158 catch(InvalidPositionException &e)
166 // virtual from NodeContainer
167 MapNode getNode(v3s16 p)
169 v3s16 blockpos = getNodeBlockPos(p);
170 if(blockpos.X != 0 || blockpos.Z != 0)
171 throw InvalidPositionException
172 ("MapSector only allows Y");
174 MapBlock * blockref = getBlockNoCreate(blockpos.Y);
175 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
177 return blockref->getNode(relpos);
179 // virtual from NodeContainer
180 void setNode(v3s16 p, MapNode & n)
182 v3s16 blockpos = getNodeBlockPos(p);
183 if(blockpos.X != 0 || blockpos.Z != 0)
184 throw InvalidPositionException
185 ("MapSector only allows Y");
187 MapBlock * blockref = getBlockNoCreate(blockpos.Y);
188 v3s16 relpos = p - blockpos*MAP_BLOCKSIZE;
189 blockref->setNode(relpos, n);
193 virtual f32 getGroundHeight(v2s16 p, bool generate=false)
195 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
197 virtual void setGroundHeight(v2s16 p, f32 y, bool generate=false)
201 // When true, sector metadata is changed from the one on disk
202 // (sector metadata = all but blocks)
203 // Basically, this should be changed to true in every setter method
204 bool differs_from_disk;
206 // Counts seconds from last usage.
207 // Sector can be deleted from memory after some time of inactivity.
208 // NOTE: It has to be made very sure no other thread is accessing
209 // the sector and it doesn't remain in any cache when
215 // The pile of MapBlocks
216 core::map<s16, MapBlock*> m_blocks;
217 //JMutex m_blocks_mutex; // For public access functions
219 NodeContainer *m_parent;
220 // Position on parent (in MapBlock widths)
223 // Be sure to set this to NULL when the cached block is deleted
224 MapBlock *m_block_cache;
227 // This is used for protecting m_blocks
233 MapBlock *getBlockBuffered(s16 y);
237 class ServerMapSector : public MapSector
240 ServerMapSector(NodeContainer *parent, v2s16 pos);
245 return MAPSECTOR_SERVER;
249 f32 getGroundHeight(v2s16 p, bool generate=false);
250 void setGroundHeight(v2s16 p, f32 y, bool generate=false);
253 These functions handle metadata.
254 They do not handle blocks.
256 void serialize(std::ostream &os, u8 version);
258 static ServerMapSector* deSerialize(
260 NodeContainer *parent,
262 core::map<v2s16, MapSector*> & sectors
269 class ClientMapSector : public MapSector
272 ClientMapSector(NodeContainer *parent, v2s16 pos);
277 return MAPSECTOR_CLIENT;
280 void deSerialize(std::istream &is);
282 /*s16 getCorner(u16 i)
288 // The ground height of the corners is stored in here