This commit is contained in:
aozhiwei 2022-11-28 19:23:15 +08:00
parent 2bcac4b1ad
commit 2c6b2bd4a1
3 changed files with 87 additions and 1 deletions

View File

@ -16,6 +16,23 @@
const int MAP_GRID_WIDTH = 64;
static const int NAVMESHSET_MAGIC = 'M'<<24 | 'S'<<16 | 'E'<<8 | 'T'; //'MSET';
static const int NAVMESHSET_VERSION = 1;
struct NavMeshSetHeader
{
int magic;
int version;
int numTiles;
dtNavMeshParams params;
};
struct NavMeshTileHeader
{
dtTileRef tileRef;
int dataSize;
};
void MapInstance::Init()
{
current_map_block_uniid_ = MAP_BLOCK_START_ID;
@ -61,6 +78,69 @@ void MapInstance::Init()
}
{
navmesh_ = dtAllocNavMesh();
FILE *fp = fopen((MetaMgr::Instance()->GetResDir() + "map3.bin").c_str(), "rb");
if(fp){
//fseek(fp, 0, SEEK_END);
//int file_size = ftell(fp);
int file_size = 1;
if(file_size){
NavMeshSetHeader header;
size_t readLen = fread(&header, sizeof(NavMeshSetHeader), 1, fp);
if (readLen != 1) {
fclose(fp);
abort();
}
if (header.magic != NAVMESHSET_MAGIC) {
fclose(fp);
abort();
}
if (header.version != NAVMESHSET_VERSION) {
fclose(fp);
abort();
}
dtStatus status = navmesh_->init(&header.params);
if (dtStatusFailed(status)) {
fclose(fp);
abort();
}
{
// Read tiles.
for (int i = 0; i < header.numTiles; ++i) {
NavMeshTileHeader tileHeader;
readLen = fread(&tileHeader, sizeof(tileHeader), 1, fp);
if (readLen != 1) {
fclose(fp);
abort();
}
if (!tileHeader.tileRef || !tileHeader.dataSize) {
abort();
break;
}
unsigned char* data = (unsigned char*)dtAlloc(tileHeader.dataSize,
DT_ALLOC_PERM);
if (!data) {
abort();
break;
}
memset(data, 0, tileHeader.dataSize);
readLen = fread(data, tileHeader.dataSize, 1, fp);
if (readLen != 1) {
dtFree(data);
fclose(fp);
abort();
}
navmesh_->addTile(data,
tileHeader.dataSize,
DT_TILE_FREE_DATA,
tileHeader.tileRef,
0);
}
}
}
fclose(fp);
}
}
}

View File

@ -1146,7 +1146,7 @@ private:
}
private:
public:
std::string res_path;
};
@ -1541,3 +1541,8 @@ MetaData::RankRoom* MetaMgr::GetRoomRank(int id )
{
return nullptr;
}
std::string MetaMgr::GetResDir()
{
return loader_->res_path;
}

View File

@ -72,6 +72,7 @@ class MetaMgr : public a8::Singleton<MetaMgr>
std::string GetText(const std::string& textid, const std::string& def_text="");
bool HasText(const std::string& textid);
std::vector<std::tuple<int, std::string>>* GetTextElements(const std::string& textid);
std::string GetResDir();
void CheckMapSpawnPoint();