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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
15 You should have received a copy of the GNU 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"
23 #include "exceptions.h"
26 MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
27 differs_from_disk(false),
35 MapSector::~MapSector()
40 void MapSector::deleteBlocks()
46 core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
47 for(; i.atEnd() == false; i++)
49 delete i.getNode()->getValue();
56 MapBlock * MapSector::getBlockBuffered(s16 y)
60 if(m_block_cache != NULL && y == m_block_cache_y){
64 // If block doesn't exist, return NULL
65 core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
70 // If block exists, return it
72 block = n->getValue();
75 // Cache the last result
77 m_block_cache = block;
82 MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
84 return getBlockBuffered(y);
87 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
89 assert(getBlockBuffered(y) == NULL);
91 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
93 MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
98 MapBlock * MapSector::createBlankBlock(s16 y)
100 MapBlock *block = createBlankBlockNoInsert(y);
102 m_blocks.insert(y, block);
107 void MapSector::insertBlock(MapBlock *block)
109 s16 block_y = block->getPos().Y;
111 MapBlock *block2 = getBlockBuffered(block_y);
113 throw AlreadyExistsException("Block already exists");
116 v2s16 p2d(block->getPos().X, block->getPos().Z);
117 assert(p2d == m_pos);
119 // Insert into container
120 m_blocks.insert(block_y, block);
123 void MapSector::deleteBlock(MapBlock *block)
125 s16 block_y = block->getPos().Y;
128 m_block_cache = NULL;
130 // Remove from container
131 m_blocks.remove(block_y);
137 void MapSector::getBlocks(core::list<MapBlock*> &dest)
139 core::list<MapBlock*> ref_list;
141 core::map<s16, MapBlock*>::Iterator bi;
143 bi = m_blocks.getIterator();
144 for(; bi.atEnd() == false; bi++)
146 MapBlock *b = bi.getNode()->getValue();
155 ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
156 MapSector(parent, pos, gamedef)
160 ServerMapSector::~ServerMapSector()
164 void ServerMapSector::serialize(std::ostream &os, u8 version)
166 if(!ser_ver_supported(version))
167 throw VersionMismatchException("ERROR: MapSector format not supported");
170 [0] u8 serialization version
174 // Server has both of these, no need to support not having them.
175 //assert(m_objects != NULL);
178 os.write((char*)&version, 1);
181 Add stuff here, if needed
186 ServerMapSector* ServerMapSector::deSerialize(
190 core::map<v2s16, MapSector*> & sectors,
195 [0] u8 serialization version
204 u8 version = SER_FMT_VER_INVALID;
205 is.read((char*)&version, 1);
207 if(!ser_ver_supported(version))
208 throw VersionMismatchException("ERROR: MapSector format not supported");
211 Add necessary reading stuff here
218 ServerMapSector *sector = NULL;
220 core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
224 dstream<<"WARNING: deSerializing existent sectors not supported "
225 "at the moment, because code hasn't been tested."
228 MapSector *sector = n->getValue();
229 assert(sector->getId() == MAPSECTOR_SERVER);
230 return (ServerMapSector*)sector;
234 sector = new ServerMapSector(parent, p2d, gamedef);
235 sectors.insert(p2d, sector);
252 ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
253 MapSector(parent, pos, gamedef)
257 ClientMapSector::~ClientMapSector()