polserver/pol-core/plib/filemapserver.cpp

57 lines
1.3 KiB
C++
Raw Permalink Normal View History

/** @file
*
* @par History
*/
2016-02-11 22:54:43 +01:00
#include "filemapserver.h"
#include <string>
2016-02-11 22:54:43 +01:00
#include "../clib/passert.h"
namespace Pol
{
namespace Plib
{
FileMapServer::FileMapServer( const RealmDescriptor& descriptor )
: MapServer( descriptor ), _mapfile(), _cur_mapblock_index( -1L )
{
std::string filename = _descriptor.path( "base.dat" );
_mapfile.Open( filename, std::ios::in );
_mapfile.Read( _cur_mapblock );
_cur_mapblock_index = 0;
}
MAPCELL FileMapServer::GetMapCell( unsigned short x, unsigned short y ) const
{
passert( x < _descriptor.width && y < _descriptor.height );
unsigned short xblock = x >> MAPBLOCK_SHIFT;
unsigned short xcell = x & MAPBLOCK_CELLMASK;
unsigned short yblock = y >> MAPBLOCK_SHIFT;
unsigned short ycell = y & MAPBLOCK_CELLMASK;
int block_index = yblock * ( _descriptor.width >> MAPBLOCK_SHIFT ) + xblock;
if ( block_index != _cur_mapblock_index )
{
// read the existing block in
std::fstream::pos_type offset = block_index * sizeof( _cur_mapblock );
_mapfile.Seek( offset );
_mapfile.Read( _cur_mapblock );
_cur_mapblock_index = block_index;
2016-02-11 22:54:43 +01:00
}
return _cur_mapblock.cell[xcell][ycell];
}
size_t FileMapServer::sizeEstimate() const
{
return sizeof( *this ) + MapServer::sizeEstimate() + _mapfile.sizeEstimate();
}
}
}