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):
32 MapSector::~MapSector()
37 void MapSector::deleteBlocks()
40 m_block_cache = nullptr;
43 for (std::unordered_map<s16, MapBlock*>::iterator i = m_blocks.begin();
44 i != m_blocks.end(); ++i) {
52 MapBlock * MapSector::getBlockBuffered(s16 y)
56 if (m_block_cache && y == m_block_cache_y) {
60 // If block doesn't exist, return NULL
61 std::unordered_map<s16, MapBlock*>::const_iterator n = m_blocks.find(y);
62 block = (n != m_blocks.end() ? n->second : nullptr);
64 // Cache the last result
66 m_block_cache = block;
71 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
73 return getBlockBuffered(y);
76 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
78 assert(getBlockBuffered(y) == NULL); // Pre-condition
80 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
82 MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
87 MapBlock * MapSector::createBlankBlock(s16 y)
89 MapBlock *block = createBlankBlockNoInsert(y);
96 void MapSector::insertBlock(MapBlock *block)
98 s16 block_y = block->getPos().Y;
100 MapBlock *block2 = getBlockBuffered(block_y);
102 throw AlreadyExistsException("Block already exists");
105 v2s16 p2d(block->getPos().X, block->getPos().Z);
106 assert(p2d == m_pos);
108 // Insert into container
109 m_blocks[block_y] = block;
112 void MapSector::deleteBlock(MapBlock *block)
114 s16 block_y = block->getPos().Y;
117 m_block_cache = nullptr;
119 // Remove from container
120 m_blocks.erase(block_y);
126 void MapSector::getBlocks(MapBlockVect &dest)
128 for (std::unordered_map<s16, MapBlock*>::iterator bi = m_blocks.begin();
129 bi != m_blocks.end(); ++bi) {
130 dest.push_back(bi->second);
138 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
139 MapSector(parent, pos, gamedef)
143 ServerMapSector::~ServerMapSector()
147 void ServerMapSector::serialize(std::ostream &os, u8 version)
149 if(!ser_ver_supported(version))
150 throw VersionMismatchException("ERROR: MapSector format not supported");
153 [0] u8 serialization version
157 // Server has both of these, no need to support not having them.
158 //assert(m_objects != NULL);
161 os.write((char*)&version, 1);
164 Add stuff here, if needed
169 ServerMapSector* ServerMapSector::deSerialize(
173 std::map<v2s16, MapSector*> & sectors,
178 [0] u8 serialization version
187 u8 version = SER_FMT_VER_INVALID;
188 is.read((char*)&version, 1);
190 if(!ser_ver_supported(version))
191 throw VersionMismatchException("ERROR: MapSector format not supported");
194 Add necessary reading stuff here
201 ServerMapSector *sector = NULL;
203 std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
205 if(n != sectors.end())
207 warningstream<<"deSerializing existent sectors not supported "
208 "at the moment, because code hasn't been tested."
211 MapSector *sector = n->second;
212 assert(sector->getId() == MAPSECTOR_SERVER);
213 return (ServerMapSector*)sector;
217 sector = new ServerMapSector(parent, p2d, gamedef);
218 sectors[p2d] = sector;
235 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
236 MapSector(parent, pos, gamedef)
240 ClientMapSector::~ClientMapSector()