3 #include "../clib/passert.h" 4 #include "../clib/logfacility.h" 20 unsigned int y_offset )
22 return m_mapinfo_vec.at( blockidx ).cell[y_offset][x_offset];
25 signed char RawMap::rawinfo(
unsigned short x,
unsigned short y,
USTRUCT_MAPINFO* gi )
27 passert_r( is_init,
"Tried to read map information before loading the map files" );
28 passert( m_mapwidth > 0 && m_mapheight > 0 && x < m_mapwidth && y < m_mapheight );
30 unsigned int x_block = x / 8;
31 unsigned int y_block = y / 8;
32 unsigned int block = ( x_block * ( m_mapheight / 8 ) + y_block );
34 unsigned int x_offset = x & 0x7;
35 unsigned int y_offset = y & 0x7;
37 *gi = get_cell( block, x_offset, y_offset );
43 m_mapinfo_vec.push_back( block );
46 void RawMap::set_bounds(
unsigned short width,
unsigned short height )
55 if ( mapdifl_file != NULL )
58 while ( fread( &blockid,
sizeof blockid, 1, mapdifl_file ) == 1 )
60 mapdifl[blockid] = index;
69 passert_r( !is_init,
"tried to initialize RawMap twice" );
71 if ( mapfile ==
nullptr )
72 throw std::runtime_error(
"load_full_map: mapfile is not open" );
74 if ( mapdifl.size() > 0 && mapdif_file == nullptr )
75 throw std::runtime_error(
"load_full_map: mapdifl is loaded but mapdif is not" );
77 unsigned int block = 0;
80 while ( fread( &buffer,
sizeof buffer, 1, mapfile ) == 1 )
82 auto citr = mapdifl.find( block );
83 if ( citr == mapdifl.end() )
90 unsigned dif_index = ( *citr ).second;
92 if ( fseek( mapdif_file, file_offset, SEEK_SET ) != 0 )
93 throw std::runtime_error(
"rawmapinfo: fseek(mapdif_file) failure" );
95 if ( fread( &buffer,
sizeof buffer, 1, mapdif_file ) != 1 )
96 throw std::runtime_error(
"rawmapinfo: fread(mapdif_file) failure" );
107 unsigned int RawMap::load_full_map(
int uo_mapid, std::istream& ifs )
109 auto maphash = [](
int mapid,
size_t chunkidx ) {
110 char mapstring[1024];
111 snprintf( mapstring,
sizeof mapstring,
"build/map%dlegacymul/%08i.dat", mapid, (
int)chunkidx );
115 passert_r( !is_init,
"tried to initialize RawMap twice" );
123 kaitai::kstream ks( &ifs );
124 uop_t uopfile( &ks );
126 size_t totalSize = 0;
127 std::map<uint64_t, uop_t::file_t*> filemap;
140 if ( file ==
nullptr )
142 if ( file->decompressed_size() == 0 )
146 "This map is zlib compressed and we can't handle that yet." );
148 filemap[file->filehash()] = file;
149 totalSize += file->decompressed_size();
152 INFO_PRINT <<
"Warning: not all chunks read (" << filemap.size() <<
"/" 159 "Not an integer number of blocks! Check if the sizes are correct." );
160 m_mapinfo_vec.clear();
166 for (
size_t i = 0, remaining = totalSize; i < filemap.size(); i++ )
168 auto fileitr = filemap.find( maphash( uo_mapid, i ) );
169 if ( fileitr == filemap.end() )
171 ERROR_PRINT <<
"Couldn't find file hash: " << std::to_string( maphash( uo_mapid, i ) )
173 throw std::runtime_error(
"UOP map is missing a file chunk." );
176 auto file = fileitr->second;
178 passert( remaining >= file->data()->filebytes().size() );
179 memcpy( &m_mapinfo_vec[vecidx], file->data()->filebytes().data(),
180 file->data()->filebytes().size() );
182 remaining -= file->data()->filebytes().size();
186 if ( !m_mapinfo_vec.empty() )
189 return (
unsigned int)m_mapinfo_vec.size();
192 RawMap::RawMap() : m_mapwidth( 0 ), m_mapheight( 0 ) {}
#define passert_r(exp, reason)
std::vector< file_t * > * files() const
uint64_t HashLittle2(std::string s)
block_body_t * block_body()
header_t * header() const