Pol  Revision:4b29d2b
filemapserver.cpp
Go to the documentation of this file.
1 
7 #include "filemapserver.h"
8 
9 #include <string>
10 
11 #include "../clib/passert.h"
12 
13 namespace Pol
14 {
15 namespace Plib
16 {
18  : MapServer( descriptor ), _mapfile(), _cur_mapblock_index( -1L )
19 {
20  std::string filename = _descriptor.path( "base.dat" );
21 
22  _mapfile.Open( filename, std::ios::in );
23 
26 }
27 
28 MAPCELL FileMapServer::GetMapCell( unsigned short x, unsigned short y ) const
29 {
31 
32  unsigned short xblock = x >> MAPBLOCK_SHIFT;
33  unsigned short xcell = x & MAPBLOCK_CELLMASK;
34  unsigned short yblock = y >> MAPBLOCK_SHIFT;
35  unsigned short ycell = y & MAPBLOCK_CELLMASK;
36 
37  int block_index = yblock * ( _descriptor.width >> MAPBLOCK_SHIFT ) + xblock;
38  if ( block_index != _cur_mapblock_index )
39  {
40  // read the existing block in
41  std::fstream::pos_type offset = block_index * sizeof( _cur_mapblock );
42  _mapfile.Seek( offset );
44 
45  _cur_mapblock_index = block_index;
46  }
47 
48  return _cur_mapblock.cell[xcell][ycell];
49 }
50 
52 {
53  return sizeof( *this ) + MapServer::sizeEstimate() + _mapfile.sizeEstimate();
54 }
55 }
56 }
size_t sizeEstimate() const
Definition: binaryfile.cpp:101
const unsigned MAPBLOCK_CELLMASK
Definition: mapblock.h:20
void Open(const std::string &filename, std::ios::openmode mode)
Definition: binaryfile.cpp:30
virtual size_t sizeEstimate() const
Definition: mapserver.cpp:167
MAPCELL cell[MAPBLOCK_CHUNK][MAPBLOCK_CHUNK]
Definition: mapblock.h:24
FileMapServer(const RealmDescriptor &descriptor)
virtual size_t sizeEstimate() const POL_OVERRIDE
#define passert(exp)
Definition: passert.h:62
const RealmDescriptor _descriptor
Definition: mapserver.h:37
void Seek(std::fstream::pos_type abs_offset)
Definition: binaryfile.cpp:50
std::string path(const std::string &filename) const
Clib::BinaryFile _mapfile
Definition: filemapserver.h:37
void Read(T &val)
Definition: binaryfile.h:29
virtual MAPCELL GetMapCell(unsigned short x, unsigned short y) const POL_OVERRIDE
const unsigned MAPBLOCK_SHIFT
Definition: mapblock.h:19
Definition: berror.cpp:12