3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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 "exceptions.h"
23 #include "serialization.h"
25 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
26 differs_from_disk(false),
34 MapSector::~MapSector()
39 void MapSector::deleteBlocks()
45 for (UNORDERED_MAP<s16, MapBlock*>::iterator i = m_blocks.begin();
46 i != m_blocks.end(); ++i) {
54 MapBlock * MapSector::getBlockBuffered(s16 y)
58 if (m_block_cache != NULL && y == m_block_cache_y) {
62 // If block doesn't exist, return NULL
63 UNORDERED_MAP<s16, MapBlock*>::iterator n = m_blocks.find(y);
64 block = (n != m_blocks.end() ? n->second : NULL);
66 // Cache the last result
68 m_block_cache = block;
73 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
75 return getBlockBuffered(y);
78 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
80 assert(getBlockBuffered(y) == NULL); // Pre-condition
82 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
84 MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
89 MapBlock * MapSector::createBlankBlock(s16 y)
91 MapBlock *block = createBlankBlockNoInsert(y);
98 void MapSector::insertBlock(MapBlock *block)
100 s16 block_y = block->getPos().Y;
102 MapBlock *block2 = getBlockBuffered(block_y);
104 throw AlreadyExistsException("Block already exists");
107 v2s16 p2d(block->getPos().X, block->getPos().Z);
108 assert(p2d == m_pos);
110 // Insert into container
111 m_blocks[block_y] = block;
114 void MapSector::deleteBlock(MapBlock *block)
116 s16 block_y = block->getPos().Y;
119 m_block_cache = NULL;
121 // Remove from container
122 m_blocks.erase(block_y);
128 void MapSector::getBlocks(MapBlockVect &dest)
130 for (UNORDERED_MAP<s16, MapBlock*>::iterator bi = m_blocks.begin();
131 bi != m_blocks.end(); ++bi) {
132 dest.push_back(bi->second);
140 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
141 MapSector(parent, pos, gamedef)
145 ServerMapSector::~ServerMapSector()
149 void ServerMapSector::serialize(std::ostream &os, u8 version)
151 if(!ser_ver_supported(version))
152 throw VersionMismatchException("ERROR: MapSector format not supported");
155 [0] u8 serialization version
159 // Server has both of these, no need to support not having them.
160 //assert(m_objects != NULL);
163 os.write((char*)&version, 1);
166 Add stuff here, if needed
171 ServerMapSector* ServerMapSector::deSerialize(
175 std::map<v2s16, MapSector*> & sectors,
180 [0] u8 serialization version
189 u8 version = SER_FMT_VER_INVALID;
190 is.read((char*)&version, 1);
192 if(!ser_ver_supported(version))
193 throw VersionMismatchException("ERROR: MapSector format not supported");
196 Add necessary reading stuff here
203 ServerMapSector *sector = NULL;
205 std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
207 if(n != sectors.end())
209 warningstream<<"deSerializing existent sectors not supported "
210 "at the moment, because code hasn't been tested."
213 MapSector *sector = n->second;
214 assert(sector->getId() == MAPSECTOR_SERVER);
215 return (ServerMapSector*)sector;
219 sector = new ServerMapSector(parent, p2d, gamedef);
220 sectors[p2d] = sector;
237 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
238 MapSector(parent, pos, gamedef)
242 ClientMapSector::~ClientMapSector()