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 "jmutexautolock.h"
25 #include "exceptions.h"
28 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
29 differs_from_disk(false),
37 MapSector::~MapSector()
42 void MapSector::deleteBlocks()
48 for(std::map<s16, MapBlock*>::iterator i = m_blocks.begin();
49 i != m_blocks.end(); ++i)
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 std::map<s16, MapBlock*>::iterator n = m_blocks.find(y);
68 if(n == m_blocks.end())
72 // If block exists, return it
77 // Cache the last result
79 m_block_cache = block;
84 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
86 return getBlockBuffered(y);
89 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
91 assert(getBlockBuffered(y) == NULL);
93 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
95 MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
100 MapBlock * MapSector::createBlankBlock(s16 y)
102 MapBlock *block = createBlankBlockNoInsert(y);
109 void MapSector::insertBlock(MapBlock *block)
111 s16 block_y = block->getPos().Y;
113 MapBlock *block2 = getBlockBuffered(block_y);
115 throw AlreadyExistsException("Block already exists");
118 v2s16 p2d(block->getPos().X, block->getPos().Z);
119 assert(p2d == m_pos);
121 // Insert into container
122 m_blocks[block_y] = block;
125 void MapSector::deleteBlock(MapBlock *block)
127 s16 block_y = block->getPos().Y;
130 m_block_cache = NULL;
132 // Remove from container
133 m_blocks.erase(block_y);
139 void MapSector::getBlocks(std::list<MapBlock*> &dest)
141 for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
142 bi != m_blocks.end(); ++bi)
144 dest.push_back(bi->second);
152 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
153 MapSector(parent, pos, gamedef)
157 ServerMapSector::~ServerMapSector()
161 void ServerMapSector::serialize(std::ostream &os, u8 version)
163 if(!ser_ver_supported(version))
164 throw VersionMismatchException("ERROR: MapSector format not supported");
167 [0] u8 serialization version
171 // Server has both of these, no need to support not having them.
172 //assert(m_objects != NULL);
175 os.write((char*)&version, 1);
178 Add stuff here, if needed
183 ServerMapSector* ServerMapSector::deSerialize(
187 std::map<v2s16, MapSector*> & sectors,
192 [0] u8 serialization version
201 u8 version = SER_FMT_VER_INVALID;
202 is.read((char*)&version, 1);
204 if(!ser_ver_supported(version))
205 throw VersionMismatchException("ERROR: MapSector format not supported");
208 Add necessary reading stuff here
215 ServerMapSector *sector = NULL;
217 std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
219 if(n != sectors.end())
221 dstream<<"WARNING: deSerializing existent sectors not supported "
222 "at the moment, because code hasn't been tested."
225 MapSector *sector = n->second;
226 assert(sector->getId() == MAPSECTOR_SERVER);
227 return (ServerMapSector*)sector;
231 sector = new ServerMapSector(parent, p2d, gamedef);
232 sectors[p2d] = sector;
249 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
250 MapSector(parent, pos, gamedef)
254 ClientMapSector::~ClientMapSector()