添加地图处理

This commit is contained in:
aozhiwei 2019-07-18 16:48:07 +08:00
parent c573265b9c
commit bdb8afab0d
10 changed files with 64 additions and 51 deletions

View File

@ -264,8 +264,6 @@ const int GUN_SLOT2 = 2;
const int FRAG_SLOT = 3; const int FRAG_SLOT = 3;
const int SMOKE_SLOT = 4; const int SMOKE_SLOT = 4;
const int MAP_HEIGHT = 32*65;
const int MAP_WIDTH = 32*75;
const int MAP_CELL_WIDTH = 64 * 8; const int MAP_CELL_WIDTH = 64 * 8;
const int MAP_GRID_WIDTH = 64; const int MAP_GRID_WIDTH = 64;

View File

@ -14,35 +14,6 @@ namespace MetaData
void Map::Init() void Map::Init()
{ {
rand_space = 0;
{
std::vector<std::string> strings;
a8::Split(i->template_list(), strings, '|');
for (auto& str : strings) {
std::vector<std::string> strings2;
a8::Split(str, strings2, ':');
assert(strings2.size() == 2);
rand_space += a8::XValue(strings2[1]).GetInt();
template_list.push_back(std::make_tuple(
strings2[0],
rand_space
));
}
}
}
std::string Map::RandTemplate()
{
if (rand_space <= 0) {
return "";
}
int rnd = rand() % rand_space;
for (auto& tpl : template_list) {
if (rnd <= std::get<1>(tpl)) {
return std::get<0>(tpl);
}
}
return "";
} }
void Equip::Init() void Equip::Init()

View File

@ -20,11 +20,7 @@ namespace MetaData
{ {
const metatable::Map* i = nullptr; const metatable::Map* i = nullptr;
std::vector<std::tuple<std::string, int>> template_list;
int rand_space = 0;
void Init(); void Init();
std::string RandTemplate();
}; };
struct Attr struct Attr

View File

@ -61,6 +61,7 @@ public:
std::map<std::string, std::list<metatable::MapTplThingJson>> maptpl_meta_hash; std::map<std::string, std::list<metatable::MapTplThingJson>> maptpl_meta_hash;
std::map<std::string, std::vector<MetaData::MapTplThing>> maptpl_hash; std::map<std::string, std::vector<MetaData::MapTplThing>> maptpl_hash;
std::map<std::string, std::vector<MetaData::MapTplThing>> born_point_hash; std::map<std::string, std::vector<MetaData::MapTplThing>> born_point_hash;
std::map<std::string, std::tuple<int, int>> maptpl_size_hash;
std::map<int, MetaData::Dress*> dress_hash; std::map<int, MetaData::Dress*> dress_hash;
std::map<int, MetaData::Tank*> tank_hash; std::map<int, MetaData::Tank*> tank_hash;
std::map<int, MetaData::Driver*> driver_hash; std::map<int, MetaData::Driver*> driver_hash;
@ -147,32 +148,41 @@ private:
gamemap_hash[item.i->map_id()] = &item; gamemap_hash[item.i->map_id()] = &item;
#if 1 #if 1
{ {
for (auto& tuple : item.template_list) { {
auto itr = maptpl_meta_hash.find(std::get<0>(tuple)); auto itr = maptpl_meta_hash.find(meta.template_name());
if (itr == maptpl_meta_hash.end()) { if (itr == maptpl_meta_hash.end()) {
maptpl_meta_hash[std::get<0>(tuple)] = std::list<metatable::MapTplThingJson>(); maptpl_meta_hash[meta.template_name()] = std::list<metatable::MapTplThingJson>();
itr = maptpl_meta_hash.find(std::get<0>(tuple)); itr = maptpl_meta_hash.find(meta.template_name());
} else { } else {
itr->second.clear(); itr->second.clear();
} }
f8::ReadJsonMetaFile(res_path + std::get<0>(tuple) + ".json", itr->second); f8::ReadJsonMetaFile(res_path + meta.template_name() + ".json", itr->second);
} }
for (auto& pair : maptpl_meta_hash) { for (auto& pair : maptpl_meta_hash) {
std::vector<MetaData::MapTplThing> things; std::vector<MetaData::MapTplThing> things;
std::vector<MetaData::MapTplThing> born_point; std::vector<MetaData::MapTplThing> born_point;
int map_width = 0;
int map_height = 0;
for (auto& itr : pair.second) { for (auto& itr : pair.second) {
if (itr.is_born_point()) { if (itr.is_born_point()) {
auto& thing = a8::FastAppend(born_point); auto& thing = a8::FastAppend(born_point);
thing.i = &itr; thing.i = &itr;
thing.Init(); thing.Init();
} else if (itr.is_map_info()) {
map_width = itr.map_width();
map_height = itr.map_height();
} else { } else {
auto& thing = a8::FastAppend(things); auto& thing = a8::FastAppend(things);
thing.i = &itr; thing.i = &itr;
thing.Init(); thing.Init();
} }
} }
if (map_width < MAP_GRID_WIDTH || map_height < MAP_GRID_WIDTH) {
abort();
}
maptpl_hash[pair.first] = things; maptpl_hash[pair.first] = things;
born_point_hash[pair.first] = born_point; born_point_hash[pair.first] = born_point;
maptpl_size_hash[pair.first] = std::make_tuple(map_width, map_height);
if (born_point.size() != ROOM_MAX_PLAYER_NUM) { if (born_point.size() != ROOM_MAX_PLAYER_NUM) {
abort(); abort();
} }
@ -359,6 +369,15 @@ MetaData::Map* MetaMgr::GetMap(int map_id)
return itr != loader_->gamemap_hash.end() ? itr->second : nullptr; return itr != loader_->gamemap_hash.end() ? itr->second : nullptr;
} }
MetaData::Map* MetaMgr::RandMap()
{
std::vector<MetaData::Map*> map_list;
for (auto& pair : loader_->gamemap_hash) {
map_list.push_back(pair.second);
}
return !map_list.empty() ? map_list[rand() % map_list.size()] : nullptr;
}
MetaData::MapThing* MetaMgr::GetMapThing(int mapthing_id) MetaData::MapThing* MetaMgr::GetMapThing(int mapthing_id)
{ {
auto itr = loader_->mapthing_hash.find(mapthing_id); auto itr = loader_->mapthing_hash.find(mapthing_id);
@ -413,6 +432,18 @@ std::vector<MetaData::MapTplThing>* MetaMgr::GetMapBornPoints(const std::string&
return itr != loader_->born_point_hash.end() ? &itr->second : nullptr; return itr != loader_->born_point_hash.end() ? &itr->second : nullptr;
} }
bool MetaMgr::GetMapTplSize(const std::string& map_name, int& width, int& height)
{
auto itr = loader_->maptpl_size_hash.find(map_name);
if (itr != loader_->maptpl_size_hash.end()) {
width = std::get<0>(itr->second);
height = std::get<1>(itr->second);
return true;
} else {
return false;
}
}
MetaData::Skill* MetaMgr::GetSkill(int skill_id) MetaData::Skill* MetaMgr::GetSkill(int skill_id)
{ {
auto itr = loader_->skill_hash.find(skill_id); auto itr = loader_->skill_hash.find(skill_id);
@ -477,3 +508,4 @@ MetaData::Robot* MetaMgr::GetRobot(int robot_id)
auto itr = loader_->robot_hash.find(robot_id); auto itr = loader_->robot_hash.find(robot_id);
return itr != loader_->robot_hash.end() ? itr->second : nullptr; return itr != loader_->robot_hash.end() ? itr->second : nullptr;
} }

View File

@ -20,6 +20,7 @@ class MetaMgr : public a8::Singleton<MetaMgr>
double GetSysParamAsFloat(const std::string& param_name, double def_val = 0.0f); double GetSysParamAsFloat(const std::string& param_name, double def_val = 0.0f);
std::string GetSysParamAsString(const std::string& param_name, const char* def_val = ""); std::string GetSysParamAsString(const std::string& param_name, const char* def_val = "");
MetaData::Map* GetMap(int map_id); MetaData::Map* GetMap(int map_id);
MetaData::Map* RandMap();
MetaData::MapThing* GetMapThing(int mapthing_id); MetaData::MapThing* GetMapThing(int mapthing_id);
MetaData::Player* GetPlayer(int id); MetaData::Player* GetPlayer(int id);
MetaData::Equip* GetEquip(int id); MetaData::Equip* GetEquip(int id);
@ -29,6 +30,7 @@ class MetaMgr : public a8::Singleton<MetaMgr>
MetaData::SafeArea* GetSafeArea(int area_id); MetaData::SafeArea* GetSafeArea(int area_id);
std::vector<MetaData::MapTplThing>* GetMapTplThing(const std::string& map_name); std::vector<MetaData::MapTplThing>* GetMapTplThing(const std::string& map_name);
std::vector<MetaData::MapTplThing>* GetMapBornPoints(const std::string& map_name); std::vector<MetaData::MapTplThing>* GetMapBornPoints(const std::string& map_name);
bool GetMapTplSize(const std::string& map_name, int& width, int& height);
MetaData::Skill* GetSkill(int skill_id); MetaData::Skill* GetSkill(int skill_id);
MetaData::Buff* GetBuff(int buff_id); MetaData::Buff* GetBuff(int buff_id);
MetaData::Attr* GetAttrById(int attr_id); MetaData::Attr* GetAttrById(int attr_id);

View File

@ -39,8 +39,8 @@ void Room::Init()
xtimer.Init(RoomXGetTickCount, this, 100, 100); xtimer.Init(RoomXGetTickCount, this, 100, 100);
xtimer_attacher.xtimer = &xtimer; xtimer_attacher.xtimer = &xtimer;
frame_event.room = this; frame_event.room = this;
grid_service.Init(MAP_WIDTH, MAP_HEIGHT, MAP_CELL_WIDTH * 8); grid_service.Init(map_width, map_height, MAP_CELL_WIDTH * 8);
map_service.Init(MAP_WIDTH / MAP_GRID_WIDTH, MAP_HEIGHT / MAP_GRID_WIDTH, MAP_GRID_WIDTH); map_service.Init(map_width / MAP_GRID_WIDTH, map_height / MAP_GRID_WIDTH, MAP_GRID_WIDTH);
CreateThings(); CreateThings();
if (App::Instance()->HasFlag(1)) { if (App::Instance()->HasFlag(1)) {
@ -356,7 +356,6 @@ void Room::ScatterDrop(a8::Vec2 center, int drop_id)
void Room::CreateThings() void Room::CreateThings()
{ {
map_tpl_name = map_meta->RandTemplate();
std::vector<MetaData::MapTplThing>* things = MetaMgr::Instance()->GetMapTplThing(map_tpl_name); std::vector<MetaData::MapTplThing>* things = MetaMgr::Instance()->GetMapTplThing(map_tpl_name);
if (things) { if (things) {
for (auto& thing_tpl : *things) { for (auto& thing_tpl : *things) {
@ -530,14 +529,14 @@ void Room::CreateLoot(int equip_id, a8::Vec2 pos, int count, int equip_lv)
entity->pos = pos; entity->pos = pos;
#if 1 #if 1
{ {
if (entity->pos.x >= MAP_WIDTH) { if (entity->pos.x >= map_width) {
entity->pos.x = MAP_WIDTH - 1; entity->pos.x = map_width - 1;
} }
if (entity->pos.x < 1.0f) { if (entity->pos.x < 1.0f) {
entity->pos.x = 1.0f; entity->pos.x = 1.0f;
} }
if (entity->pos.y >= MAP_HEIGHT) { if (entity->pos.y >= map_height) {
entity->pos.y = MAP_HEIGHT - 1; entity->pos.y = map_height - 1;
} }
if (entity->pos.y < 1.0f) { if (entity->pos.y < 1.0f) {
entity->pos.y = 1.0f; entity->pos.y = 1.0f;
@ -661,14 +660,14 @@ bool Room::OverBorder(const a8::Vec2 pos, float radius)
return true; return true;
} }
int right_x = pos.x + radius; int right_x = pos.x + radius;
if (right_x > MAP_WIDTH) { if (right_x > map_width) {
return true; return true;
} }
} }
//检查y轴 //检查y轴
{ {
int up_y = pos.y + radius; int up_y = pos.y + radius;
if (up_y > MAP_HEIGHT) { if (up_y > map_height) {
return true; return true;
} }
int down_y = pos.y - radius; int down_y = pos.y - radius;

View File

@ -31,6 +31,8 @@ public:
long long room_uuid = 0; long long room_uuid = 0;
MetaData::Map* map_meta = nullptr; MetaData::Map* map_meta = nullptr;
std::string map_tpl_name; std::string map_tpl_name;
int map_width = 0;
int map_height = 0;
FrameEvent frame_event; FrameEvent frame_event;
FrameMaker frame_maker; FrameMaker frame_maker;
long long frame_no = 0; long long frame_no = 0;

View File

@ -53,7 +53,13 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
if (GetRoomByUuid(room->room_uuid)) { if (GetRoomByUuid(room->room_uuid)) {
abort(); abort();
} }
room->map_meta = MetaMgr::Instance()->GetMap(1001); if (App::Instance()->HasFlag(5)) {
room->map_meta = MetaMgr::Instance()->GetMap(1);
} else {
room->map_meta = MetaMgr::Instance()->RandMap();
}
room->map_tpl_name = room->map_meta->i->template_name();
assert(MetaMgr::Instance()->GetMapTplSize(room->map_tpl_name, room->map_width, room->map_height));
room->Init(); room->Init();
inactive_room_hash_[room->room_uuid] = room; inactive_room_hash_[room->room_uuid] = room;
room_hash_[room->room_uuid] = room; room_hash_[room->room_uuid] = room;
@ -72,6 +78,8 @@ void RoomMgr::_CMJoin(f8::MsgHdr& hdr, const cs::CMJoin& msg)
{ {
cs::SMMapInfo notifymsg; cs::SMMapInfo notifymsg;
notifymsg.set_map_id(room->map_meta->i->map_id()); notifymsg.set_map_id(room->map_meta->i->map_id());
notifymsg.set_map_width(room->map_width);
notifymsg.set_map_height(room->map_height);
GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg); GGListener::Instance()->SendToClient(hdr.socket_handle, hdr.seqid, notifymsg);
} }
} }

View File

@ -690,6 +690,8 @@ message SMJoinedNotify
message SMMapInfo message SMMapInfo
{ {
optional int32 map_id = 1; //id optional int32 map_id = 1; //id
optional int32 map_width = 2; //
optional int32 map_height = 3; //
repeated MFMapObject objects = 6; // repeated MFMapObject objects = 6; //
} }

View File

@ -22,7 +22,7 @@ message Attr
message Map message Map
{ {
required int32 map_id = 1; //id required int32 map_id = 1; //id
required string template_list = 2; // optional string template_name = 2; //
required string map_name = 3; // required string map_name = 3; //
} }
@ -277,4 +277,7 @@ message MapTplThingJson
required float y = 6; required float y = 6;
required int32 is_born_point = 7; required int32 is_born_point = 7;
required string born_angle = 8; required string born_angle = 8;
optional int32 is_map_info = 9;
optional int32 map_width = 10;
optional int32 map_height = 11;
} }