Files
mir_server/Gateway/common/gameMap/game_map.cc

178 lines
3.5 KiB
C++
Raw Permalink Normal View History

2025-01-09 17:45:40 +08:00
#include <stdio.h>
#include "os_def.h"
#include "_ast.h"
#include <zlib.h>
#include "stream.h"
#include "zstream.h"
#include "game_map.h"
#include "memory/base_allocator.hpp"
#ifndef _MSC_VER
#include <arpa/inet.h>
#endif
using namespace stream;
using namespace zstream;
BaseAllocator GameMap::mapAlloc_("mapAlloc");
GameMap::GameMap()
{
width_ = 0;
height_ = 0;
moveable_index_ = NULL;
moveable_ount_ = 0;
}
GameMap::~GameMap()
{
if (moveable_index_)
mapAlloc_.FreeBuffer(moveable_index_);
}
bool GameMap::LoadFromStream(BaseStream& stream, bool newver)
{
MAPFILE_HEADER hdr;
MAPGrid* map_grid, *grid_ptr;
MAPFILE_HEADER_NEW hdr_n;
MAPGrid_NEW* map_grid_n, *grid_ptr_n;
if (!newver)
{
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ͷ
if (stream.read(&hdr, sizeof(hdr)) != sizeof(hdr))
return false;
if (hdr.version_ != MapFileVersion_Current)
return false;
}
//<2F><>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶζ<DDB6>ȡ<EFBFBD><C8A1><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>
MemoryStream ms(NULL);
if (newver)
ms.copyFrom(stream, stream.getSize());
else
ms.copyFrom(stream, hdr.data_size_);
ms.setPosition(0);
//<2F><><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><D4B1><EFBFBD>ѹ<EFBFBD><D1B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
ZDecompressionStream deStream(ms, NULL);
if (newver)
{
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ͷ
if (deStream.read(&hdr_n, sizeof(hdr_n)) != sizeof(hdr_n))
return false;
// <20><>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>մ<EFBFBD>ͷ<EFBFBD><CDB7><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
hdr_n.width_ = ntohl(hdr_n.width_);
hdr_n.height_ = ntohl(hdr_n.height_);
}
int max = 0;
if (newver)
{
size_t grid_count = sizeof(MAPGrid_NEW) * hdr_n.width_ * hdr_n.height_;
grid_ptr_n = map_grid_n = (MAPGrid_NEW*)mapAlloc_.AllocBuffer(grid_count);
if (grid_count != (const size_t)deStream.read(map_grid_n, (const int)grid_count))
return false;
max = hdr_n.width_ * hdr_n.height_;
}
else
{
size_t grid_count = sizeof(MAPGrid) * hdr.width_ * hdr.height_;
grid_ptr = map_grid = (MAPGrid*)mapAlloc_.AllocBuffer(grid_count);
if (grid_count != (const size_t)deStream.read(map_grid, (const int)grid_count))
return false;
max = hdr.width_ * hdr.height_;
}
moveable_ount_ = 0;
int* index_ptr = moveable_index_ = (int*)mapAlloc_.ReAllocBuffer(moveable_index_, max * sizeof(*index_ptr));
memset(index_ptr, -1, max * sizeof(*index_ptr)); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>memset<65><74><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
if (newver)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ɰ汾<C9B0>IJ<EFBFBD>һ<EFBFBD><D2BB>
for (int i = 0; i < max; ++i)
{
if (grid_ptr_n->flag_ != gfBlock /*&& grid_ptr_n->flag_ != gfNofly*/)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȷ<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD>ߵ<EFBFBD>,<2C><><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>߾<EFBFBD><DFBE><EFBFBD><EFBFBD><EFBFBD>
{
int x = i / hdr_n.height_;
int y = i % hdr_n.height_;
(*(index_ptr + (y * hdr_n.width_ + x))) = moveable_ount_;
moveable_ount_++;
}
//index_ptr++;
grid_ptr_n++;
}
width_ = hdr_n.width_;
height_ = hdr_n.height_;
mapAlloc_.FreeBuffer(map_grid_n);
}
else
{
for (int i = 0; i < max; ++i)
{
if ((grid_ptr->flag_ & MAPFLAG_MOVEABLE) != 0)
{
(*index_ptr) = moveable_ount_;
moveable_ount_++;
}
index_ptr++;
grid_ptr++;
}
width_ = hdr.width_;
height_ = hdr.height_;
mapAlloc_.FreeBuffer(map_grid);
}
return true;
}
bool GameMap::LoadFromFile(const char* fn, bool newver)
{
FileStream fs(fn, FileStream::faRead || FileStream::faShareRead, NULL);
return LoadFromStream(fs, newver);
}
void GameMap::initDefault(unsigned int w, unsigned int h)
{
// <20>ض<EFBFBD><D8B6>ط<EFBFBD><D8B7>Ż<EFBFBD><C5BB>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȫ<EFBFBD><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>óɿ<C3B3><C9BF><EFBFBD><EFBFBD>߶<EFBFBD>
//<2F><><EFBFBD>ɳ<EFBFBD><C9B3><EFBFBD><EFBFBD>Ҷȵ<D2B6>ͼ
int max = w * h;
moveable_ount_ = 0;
int* index_ptr = moveable_index_ = (int*)mapAlloc_.ReAllocBuffer(moveable_index_, max * sizeof(*index_ptr));
for (int i = 0; i < max; ++i)
{
(*index_ptr) = moveable_ount_;
moveable_ount_++;
index_ptr++;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD>Լ<EFBFBD><D4BC>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
width_ = w;
height_ = h;
}