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 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 core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
49 for(; i.atEnd() == false; i++)
51 delete i.getNode()->getValue();
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 core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
72 // If block exists, return it
74 block = n->getValue();
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);
104 m_blocks.insert(y, block);
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.insert(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.remove(block_y);
139 void MapSector::getBlocks(core::list<MapBlock*> &dest)
141 core::list<MapBlock*> ref_list;
143 core::map<s16, MapBlock*>::Iterator bi;
145 bi = m_blocks.getIterator();
146 for(; bi.atEnd() == false; bi++)
148 MapBlock *b = bi.getNode()->getValue();
157 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
158 MapSector(parent, pos, gamedef)
162 ServerMapSector::~ServerMapSector()
166 void ServerMapSector::serialize(std::ostream &os, u8 version)
168 if(!ser_ver_supported(version))
169 throw VersionMismatchException("ERROR: MapSector format not supported");
172 [0] u8 serialization version
176 // Server has both of these, no need to support not having them.
177 //assert(m_objects != NULL);
180 os.write((char*)&version, 1);
183 Add stuff here, if needed
188 ServerMapSector* ServerMapSector::deSerialize(
192 core::map<v2s16, MapSector*> & sectors,
197 [0] u8 serialization version
206 u8 version = SER_FMT_VER_INVALID;
207 is.read((char*)&version, 1);
209 if(!ser_ver_supported(version))
210 throw VersionMismatchException("ERROR: MapSector format not supported");
213 Add necessary reading stuff here
220 ServerMapSector *sector = NULL;
222 core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
226 dstream<<"WARNING: deSerializing existent sectors not supported "
227 "at the moment, because code hasn't been tested."
230 MapSector *sector = n->getValue();
231 assert(sector->getId() == MAPSECTOR_SERVER);
232 return (ServerMapSector*)sector;
236 sector = new ServerMapSector(parent, p2d, gamedef);
237 sectors.insert(p2d, sector);
254 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
255 MapSector(parent, pos, gamedef)
259 ClientMapSector::~ClientMapSector()