LCOV - code coverage report
Current view: top level - src - mapsector.cpp (source / functions) Hit Total Coverage
Test: report Lines: 42 82 51.2 %
Date: 2015-07-11 18:23:49 Functions: 12 21 57.1 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       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.
       9             : 
      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.
      14             : 
      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.
      18             : */
      19             : 
      20             : #include "mapsector.h"
      21             : #include "exceptions.h"
      22             : #include "mapblock.h"
      23             : #include "serialization.h"
      24             : 
      25         244 : MapSector::MapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
      26             :                 differs_from_disk(false),
      27             :                 m_parent(parent),
      28             :                 m_pos(pos),
      29             :                 m_gamedef(gamedef),
      30         244 :                 m_block_cache(NULL)
      31             : {
      32         244 : }
      33             : 
      34         488 : MapSector::~MapSector()
      35             : {
      36         244 :         deleteBlocks();
      37         244 : }
      38             : 
      39         244 : void MapSector::deleteBlocks()
      40             : {
      41             :         // Clear cache
      42         244 :         m_block_cache = NULL;
      43             : 
      44             :         // Delete all
      45        2985 :         for(std::map<s16, MapBlock*>::iterator i = m_blocks.begin();
      46        1990 :                 i != m_blocks.end(); ++i)
      47             :         {
      48         751 :                 delete i->second;
      49             :         }
      50             : 
      51             :         // Clear container
      52         244 :         m_blocks.clear();
      53         244 : }
      54             : 
      55     2298900 : MapBlock * MapSector::getBlockBuffered(s16 y)
      56             : {
      57             :         MapBlock *block;
      58             : 
      59     2298900 :         if(m_block_cache != NULL && y == m_block_cache_y){
      60     1885974 :                 return m_block_cache;
      61             :         }
      62             :         
      63             :         // If block doesn't exist, return NULL
      64      412926 :         std::map<s16, MapBlock*>::iterator n = m_blocks.find(y);
      65      412926 :         if(n == m_blocks.end())
      66             :         {
      67      131651 :                 block = NULL;
      68             :         }
      69             :         // If block exists, return it
      70             :         else{
      71      281275 :                 block = n->second;
      72             :         }
      73             :         
      74             :         // Cache the last result
      75      412926 :         m_block_cache_y = y;
      76      412926 :         m_block_cache = block;
      77             :         
      78      412926 :         return block;
      79             : }
      80             : 
      81     2298149 : MapBlock * MapSector::getBlockNoCreateNoEx(s16 y)
      82             : {
      83     2298149 :         return getBlockBuffered(y);
      84             : }
      85             : 
      86           0 : MapBlock * MapSector::createBlankBlockNoInsert(s16 y)
      87             : {
      88             :         assert(getBlockBuffered(y) == NULL);    // Pre-condition
      89             : 
      90           0 :         v3s16 blockpos_map(m_pos.X, y, m_pos.Y);
      91             :         
      92           0 :         MapBlock *block = new MapBlock(m_parent, blockpos_map, m_gamedef);
      93             :         
      94           0 :         return block;
      95             : }
      96             : 
      97           0 : MapBlock * MapSector::createBlankBlock(s16 y)
      98             : {
      99           0 :         MapBlock *block = createBlankBlockNoInsert(y);
     100             :         
     101           0 :         m_blocks[y] = block;
     102             : 
     103           0 :         return block;
     104             : }
     105             : 
     106         751 : void MapSector::insertBlock(MapBlock *block)
     107             : {
     108         751 :         s16 block_y = block->getPos().Y;
     109             : 
     110         751 :         MapBlock *block2 = getBlockBuffered(block_y);
     111         751 :         if(block2 != NULL){
     112           0 :                 throw AlreadyExistsException("Block already exists");
     113             :         }
     114             : 
     115         751 :         v2s16 p2d(block->getPos().X, block->getPos().Z);
     116             :         assert(p2d == m_pos);
     117             :         
     118             :         // Insert into container
     119         751 :         m_blocks[block_y] = block;
     120         751 : }
     121             : 
     122           0 : void MapSector::deleteBlock(MapBlock *block)
     123             : {
     124           0 :         s16 block_y = block->getPos().Y;
     125             : 
     126             :         // Clear from cache
     127           0 :         m_block_cache = NULL;
     128             :         
     129             :         // Remove from container
     130           0 :         m_blocks.erase(block_y);
     131             : 
     132             :         // Delete
     133           0 :         delete block;
     134           0 : }
     135             : 
     136       21931 : void MapSector::getBlocks(MapBlockVect &dest)
     137             : {
     138      261714 :         for(std::map<s16, MapBlock*>::iterator bi = m_blocks.begin();
     139      174476 :                 bi != m_blocks.end(); ++bi)
     140             :         {
     141       65307 :                 dest.push_back(bi->second);
     142             :         }
     143       21931 : }
     144             : 
     145             : /*
     146             :         ServerMapSector
     147             : */
     148             : 
     149           0 : ServerMapSector::ServerMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
     150           0 :                 MapSector(parent, pos, gamedef)
     151             : {
     152           0 : }
     153             : 
     154           0 : ServerMapSector::~ServerMapSector()
     155             : {
     156           0 : }
     157             : 
     158           0 : void ServerMapSector::serialize(std::ostream &os, u8 version)
     159             : {
     160           0 :         if(!ser_ver_supported(version))
     161           0 :                 throw VersionMismatchException("ERROR: MapSector format not supported");
     162             :         
     163             :         /*
     164             :                 [0] u8 serialization version
     165             :                 + heightmap data
     166             :         */
     167             :         
     168             :         // Server has both of these, no need to support not having them.
     169             :         //assert(m_objects != NULL);
     170             : 
     171             :         // Write version
     172           0 :         os.write((char*)&version, 1);
     173             :         
     174             :         /*
     175             :                 Add stuff here, if needed
     176             :         */
     177             : 
     178           0 : }
     179             : 
     180           0 : ServerMapSector* ServerMapSector::deSerialize(
     181             :                 std::istream &is,
     182             :                 Map *parent,
     183             :                 v2s16 p2d,
     184             :                 std::map<v2s16, MapSector*> & sectors,
     185             :                 IGameDef *gamedef
     186             :         )
     187             : {
     188             :         /*
     189             :                 [0] u8 serialization version
     190             :                 + heightmap data
     191             :         */
     192             : 
     193             :         /*
     194             :                 Read stuff
     195             :         */
     196             :         
     197             :         // Read version
     198           0 :         u8 version = SER_FMT_VER_INVALID;
     199           0 :         is.read((char*)&version, 1);
     200             :         
     201           0 :         if(!ser_ver_supported(version))
     202           0 :                 throw VersionMismatchException("ERROR: MapSector format not supported");
     203             :         
     204             :         /*
     205             :                 Add necessary reading stuff here
     206             :         */
     207             :         
     208             :         /*
     209             :                 Get or create sector
     210             :         */
     211             : 
     212           0 :         ServerMapSector *sector = NULL;
     213             : 
     214           0 :         std::map<v2s16, MapSector*>::iterator n = sectors.find(p2d);
     215             : 
     216           0 :         if(n != sectors.end())
     217             :         {
     218             :                 dstream<<"WARNING: deSerializing existent sectors not supported "
     219           0 :                                 "at the moment, because code hasn't been tested."
     220           0 :                                 <<std::endl;
     221             : 
     222           0 :                 MapSector *sector = n->second;
     223             :                 assert(sector->getId() == MAPSECTOR_SERVER);
     224           0 :                 return (ServerMapSector*)sector;
     225             :         }
     226             :         else
     227             :         {
     228           0 :                 sector = new ServerMapSector(parent, p2d, gamedef);
     229           0 :                 sectors[p2d] = sector;
     230             :         }
     231             : 
     232             :         /*
     233             :                 Set stuff in sector
     234             :         */
     235             : 
     236             :         // Nothing here
     237             : 
     238           0 :         return sector;
     239             : }
     240             : 
     241             : #ifndef SERVER
     242             : /*
     243             :         ClientMapSector
     244             : */
     245             : 
     246         244 : ClientMapSector::ClientMapSector(Map *parent, v2s16 pos, IGameDef *gamedef):
     247         244 :                 MapSector(parent, pos, gamedef)
     248             : {
     249         244 : }
     250             : 
     251         488 : ClientMapSector::~ClientMapSector()
     252             : {
     253         491 : }
     254             : 
     255             : #endif // !SERVER
     256             : 
     257             : //END

Generated by: LCOV version 1.11