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(std::map<s16, MapBlock*>::iterator i = m_blocks.begin();
46 i != m_blocks.end(); ++i)
55 MapBlock * MapSector::getBlockBuffered(s16 y)
59 if(m_block_cache != NULL && y == m_block_cache_y){
63 // If block doesn't exist, return NULL
64 std::map<s16, MapBlock*>::iterator n = m_blocks.find(y);
65 if(n == m_blocks.end())
69 // If block exists, return it
74 // Cache the last result
76 m_block_cache = block;
81 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
83 return getBlockBuffered(y);
86 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
88 assert(getBlockBuffered(y) == NULL); // Pre-condition
90 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
92 MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
97 MapBlock * MapSector::createBlankBlock(s16 y)
99 MapBlock *block = createBlankBlockNoInsert(y);
106 void MapSector::insertBlock(MapBlock *block)
108 s16 block_y = block->getPos().Y;
110 MapBlock *block2 = getBlockBuffered(block_y);
112 throw AlreadyExistsException("Block already exists");
115 v2s16 p2d(block->getPos().X, block->getPos().Z);
116 assert(p2d == m_pos);
118 // Insert into container
119 m_blocks[block_y] = block;
122 void MapSector::deleteBlock(MapBlock *block)
124 s16 block_y = block->getPos().Y;
127 m_block_cache = NULL;
129 // Remove from container
130 m_blocks.erase(block_y);
136 void MapSector::getBlocks(MapBlockVect &dest)
138 for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
139 bi != m_blocks.end(); ++bi)
141 dest.push_back(bi->second);
145 bool MapSector::empty()
147 return m_blocks.empty();
154 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
155 MapSector(parent, pos, gamedef)
159 ServerMapSector::~ServerMapSector()
163 void ServerMapSector::serialize(std::ostream &os, u8 version)
165 if(!ser_ver_supported(version))
166 throw VersionMismatchException("ERROR: MapSector format not supported");
169 [0] u8 serialization version
173 // Server has both of these, no need to support not having them.
174 //assert(m_objects != NULL);
177 os.write((char*)&version, 1);
180 Add stuff here, if needed
185 ServerMapSector* ServerMapSector::deSerialize(
189 std::map<v2s16, MapSector*> & sectors,
194 [0] u8 serialization version
203 u8 version = SER_FMT_VER_INVALID;
204 is.read((char*)&version, 1);
206 if(!ser_ver_supported(version))
207 throw VersionMismatchException("ERROR: MapSector format not supported");
210 Add necessary reading stuff here
217 ServerMapSector *sector = NULL;
219 std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
221 if(n != sectors.end())
223 warningstream<<"deSerializing existent sectors not supported "
224 "at the moment, because code hasn't been tested."
227 MapSector *sector = n->second;
228 assert(sector->getId() == MAPSECTOR_SERVER);
229 return (ServerMapSector*)sector;
233 sector = new ServerMapSector(parent, p2d, gamedef);
234 sectors[p2d] = sector;
251 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
252 MapSector(parent, pos, gamedef)
256 ClientMapSector::~ClientMapSector()