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.
20 #include "mapsector.h"
21 #include "jmutexautolock.h"
23 #include "exceptions.h"
25 MapSector::MapSector(NodeContainer *parent, v2s16 pos):
26 differs_from_disk(true),
32 assert(m_mutex.IsInitialized());
35 MapSector::~MapSector()
40 void MapSector::deleteBlocks()
42 JMutexAutoLock lock(m_mutex);
48 core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
49 for(; i.atEnd() == false; i++)
51 delete i.getNode()->getValue();
58 MapBlock * MapSector::getBlockBuffered(s16 y)
62 if(m_block_cache != NULL && y == m_block_cache_y){
66 // If block doesn't exist, return NULL
67 core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
72 // If block exists, return it
74 block = n->getValue();
77 // Cache the last result
79 m_block_cache = block;
84 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
86 JMutexAutoLock lock(m_mutex);
88 return getBlockBuffered(y);
91 MapBlock * MapSector::getBlockNoCreate(s16 y)
93 MapBlock *block = getBlockNoCreateNoEx(y);
96 throw InvalidPositionException();
101 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
103 // There should not be a block at this position
104 if(getBlockBuffered(y) != NULL)
105 throw AlreadyExistsException("Block already exists");
107 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
109 MapBlock *block = new MapBlock(m_parent, blockpos_map);
114 MapBlock * MapSector::createBlankBlock(s16 y)
116 JMutexAutoLock lock(m_mutex);
118 MapBlock *block = createBlankBlockNoInsert(y);
120 m_blocks.insert(y, block);
125 void MapSector::insertBlock(MapBlock *block)
127 s16 block_y = block->getPos().Y;
130 JMutexAutoLock lock(m_mutex);
132 MapBlock *block2 = getBlockBuffered(block_y);
134 throw AlreadyExistsException("Block already exists");
137 v2s16 p2d(block->getPos().X, block->getPos().Z);
138 assert(p2d == m_pos);
140 // Insert into container
141 m_blocks.insert(block_y, block);
145 void MapSector::removeBlock(MapBlock *block)
147 s16 block_y = block->getPos().Y;
149 JMutexAutoLock lock(m_mutex);
152 m_block_cache = NULL;
154 // Remove from container
155 m_blocks.remove(block_y);
158 void MapSector::getBlocks(core::list<MapBlock*> &dest)
160 JMutexAutoLock lock(m_mutex);
162 core::list<MapBlock*> ref_list;
164 core::map<s16, MapBlock*>::Iterator bi;
166 bi = m_blocks.getIterator();
167 for(; bi.atEnd() == false; bi++)
169 MapBlock *b = bi.getNode()->getValue();
178 ServerMapSector::ServerMapSector(NodeContainer *parent, v2s16 pos):
179 MapSector(parent, pos)
183 ServerMapSector::~ServerMapSector()
187 f32 ServerMapSector::getGroundHeight(v2s16 p, bool generate)
189 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
192 void ServerMapSector::setGroundHeight(v2s16 p, f32 y, bool generate)
196 void ServerMapSector::serialize(std::ostream &os, u8 version)
198 if(!ser_ver_supported(version))
199 throw VersionMismatchException("ERROR: MapSector format not supported");
202 [0] u8 serialization version
206 // Server has both of these, no need to support not having them.
207 //assert(m_objects != NULL);
210 os.write((char*)&version, 1);
213 Add stuff here, if needed
218 ServerMapSector* ServerMapSector::deSerialize(
220 NodeContainer *parent,
222 core::map<v2s16, MapSector*> & sectors
226 [0] u8 serialization version
235 u8 version = SER_FMT_VER_INVALID;
236 is.read((char*)&version, 1);
238 if(!ser_ver_supported(version))
239 throw VersionMismatchException("ERROR: MapSector format not supported");
242 Add necessary reading stuff here
249 ServerMapSector *sector = NULL;
251 core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
255 dstream<<"WARNING: deSerializing existent sectors not supported "
256 "at the moment, because code hasn't been tested."
259 MapSector *sector = n->getValue();
260 assert(sector->getId() == MAPSECTOR_SERVER);
261 return (ServerMapSector*)sector;
265 sector = new ServerMapSector(parent, p2d);
266 sectors.insert(p2d, sector);
283 ClientMapSector::ClientMapSector(NodeContainer *parent, v2s16 pos):
284 MapSector(parent, pos)
288 ClientMapSector::~ClientMapSector()
292 void ClientMapSector::deSerialize(std::istream &is)
295 [0] u8 serialization version
302 In which corners are in these positions
310 u8 version = SER_FMT_VER_INVALID;
311 is.read((char*)&version, 1);
313 if(!ser_ver_supported(version))
314 throw VersionMismatchException("ERROR: MapSector format not supported");
318 // Dummy read corners
319 is.read((char*)buf, 2);
320 is.read((char*)buf, 2);
321 is.read((char*)buf, 2);
322 is.read((char*)buf, 2);