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),
33 assert(m_mutex.IsInitialized());
36 MapSector::~MapSector()
41 void MapSector::deleteBlocks()
43 JMutexAutoLock lock(m_mutex);
49 core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
50 for(; i.atEnd() == false; i++)
52 delete i.getNode()->getValue();
59 MapBlock * MapSector::getBlockBuffered(s16 y)
63 if(m_block_cache != NULL && y == m_block_cache_y){
67 // If block doesn't exist, return NULL
68 core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
73 // If block exists, return it
75 block = n->getValue();
78 // Cache the last result
80 m_block_cache = block;
85 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
87 JMutexAutoLock lock(m_mutex);
89 return getBlockBuffered(y);
92 MapBlock * MapSector::getBlockNoCreate(s16 y)
94 MapBlock *block = getBlockNoCreateNoEx(y);
97 throw InvalidPositionException();
102 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
104 // There should not be a block at this position
105 if(getBlockBuffered(y) != NULL)
106 throw AlreadyExistsException("Block already exists");
108 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
110 MapBlock *block = new MapBlock(m_parent, blockpos_map);
115 MapBlock * MapSector::createBlankBlock(s16 y)
117 JMutexAutoLock lock(m_mutex);
119 MapBlock *block = createBlankBlockNoInsert(y);
121 m_blocks.insert(y, block);
126 void MapSector::insertBlock(MapBlock *block)
128 s16 block_y = block->getPos().Y;
131 JMutexAutoLock lock(m_mutex);
133 MapBlock *block2 = getBlockBuffered(block_y);
135 throw AlreadyExistsException("Block already exists");
138 v2s16 p2d(block->getPos().X, block->getPos().Z);
139 assert(p2d == m_pos);
141 // Insert into container
142 m_blocks.insert(block_y, block);
146 void MapSector::removeBlock(MapBlock *block)
148 s16 block_y = block->getPos().Y;
150 JMutexAutoLock lock(m_mutex);
153 m_block_cache = NULL;
155 // Remove from container
156 m_blocks.remove(block_y);
159 void MapSector::getBlocks(core::list<MapBlock*> &dest)
161 JMutexAutoLock lock(m_mutex);
163 core::list<MapBlock*> ref_list;
165 core::map<s16, MapBlock*>::Iterator bi;
167 bi = m_blocks.getIterator();
168 for(; bi.atEnd() == false; bi++)
170 MapBlock *b = bi.getNode()->getValue();
179 ServerMapSector::ServerMapSector(NodeContainer *parent, v2s16 pos):
180 MapSector(parent, pos)
184 ServerMapSector::~ServerMapSector()
188 f32 ServerMapSector::getGroundHeight(v2s16 p, bool generate)
190 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
193 void ServerMapSector::setGroundHeight(v2s16 p, f32 y, bool generate)
197 void ServerMapSector::serialize(std::ostream &os, u8 version)
199 if(!ser_ver_supported(version))
200 throw VersionMismatchException("ERROR: MapSector format not supported");
203 [0] u8 serialization version
207 // Server has both of these, no need to support not having them.
208 //assert(m_objects != NULL);
211 os.write((char*)&version, 1);
214 Add stuff here, if needed
219 ServerMapSector* ServerMapSector::deSerialize(
221 NodeContainer *parent,
223 core::map<v2s16, MapSector*> & sectors
227 [0] u8 serialization version
236 u8 version = SER_FMT_VER_INVALID;
237 is.read((char*)&version, 1);
239 if(!ser_ver_supported(version))
240 throw VersionMismatchException("ERROR: MapSector format not supported");
243 Add necessary reading stuff here
250 ServerMapSector *sector = NULL;
252 core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
256 dstream<<"WARNING: deSerializing existent sectors not supported "
257 "at the moment, because code hasn't been tested."
260 MapSector *sector = n->getValue();
261 assert(sector->getId() == MAPSECTOR_SERVER);
262 return (ServerMapSector*)sector;
266 sector = new ServerMapSector(parent, p2d);
267 sectors.insert(p2d, sector);
284 ClientMapSector::ClientMapSector(NodeContainer *parent, v2s16 pos):
285 MapSector(parent, pos)
289 ClientMapSector::~ClientMapSector()
293 void ClientMapSector::deSerialize(std::istream &is)
296 [0] u8 serialization version
303 In which corners are in these positions
311 u8 version = SER_FMT_VER_INVALID;
312 is.read((char*)&version, 1);
314 if(!ser_ver_supported(version))
315 throw VersionMismatchException("ERROR: MapSector format not supported");
319 // Dummy read corners
320 is.read((char*)buf, 2);
321 is.read((char*)buf, 2);
322 is.read((char*)buf, 2);
323 is.read((char*)buf, 2);