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"
25 MapSector::MapSector(NodeContainer *parent, v2s16 pos):
26 differs_from_disk(true),
33 assert(m_mutex.IsInitialized());
36 MapSector::~MapSector()
41 void MapSector::deleteBlocks()
43 JMutexAutoLock lock(m_mutex);
49 core::map<s16, MapBlock*>::Iterator i = m_blocks.getIterator();
50 for(; i.atEnd() == false; i++)
52 delete i.getNode()->getValue();
59 MapBlock * MapSector::getBlockBuffered(s16 y)
63 if(m_block_cache != NULL && y == m_block_cache_y){
67 // If block doesn't exist, return NULL
68 core::map<s16, MapBlock*>::Node *n = m_blocks.find(y);
73 // If block exists, return it
75 block = n->getValue();
78 // Cache the last result
80 m_block_cache = block;
85 MapBlock * MapSector::getBlockNoCreate(s16 y)
87 JMutexAutoLock lock(m_mutex);
89 MapBlock *block = getBlockBuffered(y);
92 throw InvalidPositionException();
97 MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
99 // There should not be a block at this position
100 if(getBlockBuffered(y) != NULL)
101 throw AlreadyExistsException("Block already exists");
103 v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
105 MapBlock *block = new MapBlock(m_parent, blockpos_map);
110 MapBlock * MapSector::createBlankBlock(s16 y)
112 JMutexAutoLock lock(m_mutex);
114 MapBlock *block = createBlankBlockNoInsert(y);
116 m_blocks.insert(y, block);
121 void MapSector::insertBlock(MapBlock *block)
123 s16 block_y = block->getPos().Y;
126 JMutexAutoLock lock(m_mutex);
128 MapBlock *block2 = getBlockBuffered(block_y);
130 throw AlreadyExistsException("Block already exists");
133 v2s16 p2d(block->getPos().X, block->getPos().Z);
134 assert(p2d == m_pos);
136 // Insert into container
137 m_blocks.insert(block_y, block);
141 void MapSector::removeBlock(MapBlock *block)
143 s16 block_y = block->getPos().Y;
145 JMutexAutoLock lock(m_mutex);
148 m_block_cache = NULL;
150 // Remove from container
151 m_blocks.remove(block_y);
154 void MapSector::getBlocks(core::list<MapBlock*> &dest)
156 JMutexAutoLock lock(m_mutex);
158 core::list<MapBlock*> ref_list;
160 core::map<s16, MapBlock*>::Iterator bi;
162 bi = m_blocks.getIterator();
163 for(; bi.atEnd() == false; bi++)
165 MapBlock *b = bi.getNode()->getValue();
174 ServerMapSector::ServerMapSector(NodeContainer *parent, v2s16 pos, u16 hm_split):
175 MapSector(parent, pos),
176 m_hm_split(hm_split),
179 // hm_split has to be 1 or 2^x
180 assert(hm_split == 0 || hm_split == 1 || (hm_split & (hm_split-1)) == 0);
181 assert(hm_split * hm_split <= MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT);
183 for(u16 i=0; i<hm_split*hm_split; i++)
184 m_heightmaps[i] = NULL;
187 ServerMapSector::~ServerMapSector()
189 u16 hm_count = m_hm_split * m_hm_split;
192 for(u16 i=0; i<hm_count; i++)
195 delete m_heightmaps[i];
202 void ServerMapSector::setHeightmap(v2s16 hm_p, FixedHeightmap *hm)
204 assert(isInArea(hm_p, m_hm_split));
206 s16 i = hm_p.Y * m_hm_split + hm_p.X;
208 // Don't allow setting already set heightmaps as of now
209 assert(m_heightmaps[i] == NULL);
211 /*std::cout<<"MapSector::setHeightmap for sector "
212 <<"("<<m_pos.X<<","<<m_pos.Y<<"): "
213 <<"Setting heightmap "
214 <<"("<<hm_p.X<<","<<hm_p.Y<<")"
216 <<" to pointer "<<(long long)hm
219 m_heightmaps[i] = hm;
221 differs_from_disk = true;
224 FixedHeightmap * ServerMapSector::getHeightmap(v2s16 hm_p)
226 assert(isInArea(hm_p, m_hm_split));
228 s16 i = hm_p.Y * m_hm_split + hm_p.X;
230 return m_heightmaps[i];
233 f32 ServerMapSector::getGroundHeight(v2s16 p, bool generate)
238 /*std::cout<<"Sector has no heightmap"
239 <<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
240 <<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
242 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
245 // Side length of heightmap
246 s16 hm_d = MAP_BLOCKSIZE / m_hm_split;
248 // Position of selected heightmap
249 v2s16 hm_p = getContainerPos(p, hm_d);
250 if(isInArea(hm_p, m_hm_split) == false)
252 /*std::cout<<"Sector has no heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
253 <<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
254 <<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
256 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
259 // Selected heightmap
260 FixedHeightmap *hm = m_heightmaps[hm_p.Y * m_hm_split + hm_p.X];
264 /*std::cout<<"Sector heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
266 <<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
267 <<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
269 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
272 // Position in selected heighmap
273 v2s16 p_in_hm = p - hm_p * hm_d;
274 if(isInArea(p_in_hm, hm_d+1) == false)
276 /*std::cout<<"Position ("<<p_in_hm.X<<","<<p_in_hm.Y<<")"
277 " not in sector heightmap area"
278 <<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
279 <<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
281 return GROUNDHEIGHT_NOTFOUND_SETVALUE;
284 f32 h = hm->getGroundHeight(p_in_hm);
286 /*if(h < GROUNDHEIGHT_VALID_MINVALUE)
288 std::cout<<"Sector heightmap ("<<hm_p.X<<","<<hm_p.Y<<")"
289 " returned invalid value"
290 <<" while trying to get height at ("<<p.X<<","<<p.Y<<")"
291 <<" which is ("<<p_in_hm.X<<","<<p_in_hm.Y<<") in heightmap"
292 <<" for sector ("<<m_pos.X<<","<<m_pos.Y<<")"
299 void ServerMapSector::setGroundHeight(v2s16 p, f32 y, bool generate)
303 This causes glitches because the sector cannot be actually
304 modified according to heightmap changes.
306 This is useful when generating continued sub-heightmaps
314 // Side length of heightmap
315 s16 hm_d = MAP_BLOCKSIZE / m_hm_split;
317 // Position of selected heightmap
318 v2s16 hm_p = getContainerPos(p, hm_d);
319 if(isInArea(hm_p, m_hm_split) == false)
322 // Selected heightmap
323 FixedHeightmap *hm = m_heightmaps[hm_p.Y * m_hm_split + hm_p.X];
328 // Position in selected heighmap
329 v2s16 p_in_hm = p - hm_p * hm_d;
330 if(isInArea(p_in_hm, hm_d) == false)
333 hm->setGroundHeight(p_in_hm, y);
335 differs_from_disk = true;
338 void ServerMapSector::serialize(std::ostream &os, u8 version)
340 if(!ser_ver_supported(version))
341 throw VersionMismatchException("ERROR: MapSector format not supported");
344 [0] u8 serialization version
348 // Server has both of these, no need to support not having them.
349 assert(m_objects != NULL);
352 os.write((char*)&version, 1);
355 Serialize heightmap(s)
358 // Version with single heightmap
362 FixedHeightmap::serializedLength(version, MAP_BLOCKSIZE);
364 SharedBuffer<u8> data(heightmap_size);
365 m_heightmaps[0]->serialize(*data, version);
367 os.write((const char*)*data, heightmap_size);
376 if(m_objects->size() > 65535)
377 object_count = 65535;
379 object_count = m_objects->size();
382 writeU16(b, object_count);
383 os.write((char*)b, 2);
385 core::map<v3s16, u8>::Iterator i;
386 i = m_objects->getIterator();
387 for(; i.atEnd() == false; i++)
389 v3s16 p = i.getNode()->getKey();
390 u8 d = i.getNode()->getValue();
392 writeV3S16(&b[0], p);
394 os.write((char*)b, 7);
398 // Version with multiple heightmaps
404 throw SerializationError("Sector has too many heightmaps");
406 // Write heightmap split ratio
407 writeU8(buf, m_hm_split);
408 os.write((char*)buf, 1);
410 // If there are heightmaps, write them
413 u16 hm_d = MAP_BLOCKSIZE / m_hm_split;
415 u32 hm_size = FixedHeightmap::serializedLength(version, hm_d);
416 SharedBuffer<u8> data(hm_size);
418 u16 hm_count = m_hm_split * m_hm_split;
421 for(u16 i=0; i<hm_count; i++)
423 m_heightmaps[i]->serialize(*data, version);
424 os.write((const char*)*data, hm_size);
433 if(m_objects->size() > 65535)
434 object_count = 65535;
436 object_count = m_objects->size();
439 writeU16(b, object_count);
440 os.write((char*)b, 2);
442 core::map<v3s16, u8>::Iterator i;
443 i = m_objects->getIterator();
444 for(; i.atEnd() == false; i++)
446 v3s16 p = i.getNode()->getKey();
447 u8 d = i.getNode()->getValue();
449 writeV3S16(&b[0], p);
451 os.write((char*)b, 7);
456 ServerMapSector* ServerMapSector::deSerialize(
458 NodeContainer *parent,
460 Heightmap *master_hm,
461 core::map<v2s16, MapSector*> & sectors
465 [0] u8 serialization version
474 u8 version = SER_FMT_VER_INVALID;
475 is.read((char*)&version, 1);
477 if(!ser_ver_supported(version))
478 throw VersionMismatchException("ERROR: MapSector format not supported");
484 FixedHeightmap *hms[MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT];
487 // Version with a single heightmap
493 FixedHeightmap::serializedLength(version, MAP_BLOCKSIZE);
495 SharedBuffer<u8> data(hm_size);
496 is.read((char*)*data, hm_size);
498 hms[0] = new FixedHeightmap(master_hm, p2d, MAP_BLOCKSIZE);
499 hms[0]->deSerialize(*data, version);
501 // Version with multiple heightmaps
507 is.read((char*)buf, 1);
508 hm_split = readU8(buf);
510 // If there are heightmaps, read them
513 u16 hm_count = hm_split * hm_split;
515 if(hm_count > MAPSECTOR_FIXEDHEIGHTMAPS_MAXCOUNT)
516 throw SerializationError("Sector has too many heightmaps");
518 u16 hm_d = MAP_BLOCKSIZE / hm_split;
520 u32 hm_size = FixedHeightmap::serializedLength(version, hm_d);
523 for(s16 y=0; y<hm_split; y++)
524 for(s16 x=0; x<hm_split; x++)
526 SharedBuffer<u8> data(hm_size);
527 is.read((char*)*data, hm_size);
529 hms[i] = new FixedHeightmap(master_hm, p2d+v2s16(x,y), hm_d);
530 hms[i]->deSerialize(*data, version);
540 core::map<v3s16, u8> *objects = new core::map<v3s16, u8>;
545 is.read((char*)b, 2);
546 u16 object_count = readU16(b);
548 for(u16 i=0; i<object_count; i++)
551 is.read((char*)b, 7);
552 v3s16 p = readV3S16(&b[0]);
554 objects->insert(p, d);
562 ServerMapSector *sector = NULL;
564 core::map<v2s16, MapSector*>::Node *n = sectors.find(p2d);
568 dstream<<"deSerializing existent sectors not supported "
569 "at the moment, because code hasn't been tested."
572 // NOTE: At least hm_split mismatch would have to be checked
574 //sector = n->getValue();
578 sector = new ServerMapSector(parent, p2d, hm_split);
579 sectors.insert(p2d, sector);
588 sector->m_hm_split = hm_split;
590 u16 hm_count = hm_split * hm_split;
592 for(u16 i=0; i<hm_count; i++)
594 // Set (or change) heightmap
595 FixedHeightmap *oldhm = sector->m_heightmaps[i];
596 sector->m_heightmaps[i] = hms[i];
601 // Set (or change) objects
602 core::map<v3s16, u8> *oldfo = sector->m_objects;
603 sector->m_objects = objects;
614 ClientMapSector::ClientMapSector(NodeContainer *parent, v2s16 pos):
615 MapSector(parent, pos)
619 ClientMapSector::~ClientMapSector()
623 void ClientMapSector::deSerialize(std::istream &is)
626 [0] u8 serialization version
633 In which corners are in these positions
641 u8 version = SER_FMT_VER_INVALID;
642 is.read((char*)&version, 1);
644 if(!ser_ver_supported(version))
645 throw VersionMismatchException("ERROR: MapSector format not supported");
647 throw VersionMismatchException("ERROR: MapSector format not supported");
652 is.read((char*)buf, 2);
653 s16 c0 = readU16(buf);
654 is.read((char*)buf, 2);
655 s16 c1 = readU16(buf);
656 is.read((char*)buf, 2);
657 s16 c2 = readU16(buf);
658 is.read((char*)buf, 2);
659 s16 c3 = readU16(buf);