add mapmgr
This commit is contained in:
parent
1b294da213
commit
64a0507fd1
@ -20,6 +20,7 @@
|
||||
#include "roommgr.h"
|
||||
#include "player.h"
|
||||
#include "playermgr.h"
|
||||
#include "mapmgr.h"
|
||||
|
||||
#include "ss_msgid.pb.h"
|
||||
#include "ss_proto.pb.h"
|
||||
@ -157,6 +158,7 @@ bool App::Init(int argc, char* argv[])
|
||||
MetaMgr::Instance()->Init();
|
||||
uuid.SetMachineId((node_id - 1) * MAX_NODE_ID + instance_id);
|
||||
RoomMgr::Instance()->Init();
|
||||
MapMgr::Instance()->Init();
|
||||
PlayerMgr::Instance()->Init();
|
||||
GGListener::Instance()->Init();
|
||||
|
||||
@ -192,6 +194,7 @@ void App::UnInit()
|
||||
{
|
||||
GGListener::Instance()->UnInit();
|
||||
PlayerMgr::Instance()->UnInit();
|
||||
MapMgr::Instance()->UnInit();
|
||||
RoomMgr::Instance()->UnInit();
|
||||
MetaMgr::Instance()->UnInit();
|
||||
JsonDataMgr::Instance()->UnInit();
|
||||
|
@ -30,7 +30,6 @@ static void _GMOpsReload(f8::JsonHttpRequest* request)
|
||||
request->resp_xobj->SetVal("errcode", 0);
|
||||
request->resp_xobj->SetVal("errmsg", "");
|
||||
JsonDataMgr::Instance()->Reload();
|
||||
RoomMgr::Instance()->InstallReportStateTimer();
|
||||
a8::UdpLog::Instance()->Info("reload config files", {});
|
||||
}
|
||||
|
||||
|
200
server/gameserver/mapmgr.cc
Normal file
200
server/gameserver/mapmgr.cc
Normal file
@ -0,0 +1,200 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include "mapservice.h"
|
||||
#include "gridservice.h"
|
||||
#include "building.h"
|
||||
#include "obstacle.h"
|
||||
#include "loot.h"
|
||||
#include "mapmgr.h"
|
||||
#include "metamgr.h"
|
||||
#include "room.h"
|
||||
|
||||
const int MAP_GRID_WIDTH = 64;
|
||||
|
||||
void MapMgr::Init()
|
||||
{
|
||||
map_meta_ = MetaMgr::Instance()->GetMap(2001);
|
||||
if (!map_meta_) {
|
||||
abort();
|
||||
}
|
||||
map_service_ = new MapService();
|
||||
grid_service_ = new GridService();
|
||||
grid_service_->Init(map_meta_->i->map_width(),
|
||||
map_meta_->i->map_height(),
|
||||
MetaMgr::Instance()->map_cell_width);
|
||||
map_service_->Init(map_meta_->i->map_width() / MAP_GRID_WIDTH,
|
||||
map_meta_->i->map_height() / MAP_GRID_WIDTH,
|
||||
MAP_GRID_WIDTH);
|
||||
CreateThings();
|
||||
a8::XPrintf("current_uniid:%d loots:%d spawn_points:%d\n",
|
||||
{
|
||||
current_uniid_,
|
||||
loots_.size(),
|
||||
spawn_points_.size()
|
||||
});
|
||||
if (current_uniid_ >= FIXED_OBJECT_MAXID) {
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void MapMgr::UnInit()
|
||||
{
|
||||
map_service_->UnInit();
|
||||
grid_service_->UnInit();
|
||||
A8_SAFE_DELETE(map_service_);
|
||||
A8_SAFE_DELETE(grid_service_);
|
||||
}
|
||||
|
||||
void MapMgr::AttachRoom(Room* room)
|
||||
{
|
||||
room->grid_service = grid_service_;
|
||||
room->map_service = map_service_;
|
||||
room->spawn_points = &spawn_points_;
|
||||
room->newbie_born_point_meta = newbie_born_point_;
|
||||
room->loots = &loots_;
|
||||
room->buildings = &buildings_;
|
||||
room->map_meta = map_meta_;
|
||||
}
|
||||
|
||||
void MapMgr::CreateThings()
|
||||
{
|
||||
std::string map_tpl_name = map_meta_->RandTemplate();
|
||||
std::map<std::string, MetaData::MapTplThing*> spawn_points_hash;
|
||||
std::vector<MetaData::MapTplThing>* things = MetaMgr::Instance()->GetMapTplThing(map_tpl_name);
|
||||
if (things) {
|
||||
for (auto& thing_tpl : *things) {
|
||||
switch (thing_tpl.i->_object_type()) {
|
||||
case kMOT_Object:
|
||||
{
|
||||
if (thing_tpl.i->weight() >= rand() % 10000) {
|
||||
CreateMapObject(thing_tpl);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kMOT_SpawnPoint:
|
||||
{
|
||||
if (spawn_points_hash.find(thing_tpl.i->name()) !=
|
||||
spawn_points_hash.end()) {
|
||||
abort();
|
||||
}
|
||||
spawn_points_.push_back(&thing_tpl);
|
||||
spawn_points_hash[thing_tpl.i->name()] = &thing_tpl;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (spawn_points_hash.find(MetaMgr::Instance()->newbie_born_point) !=
|
||||
spawn_points_hash.end()) {
|
||||
newbie_born_point_ = spawn_points_hash[MetaMgr::Instance()->newbie_born_point];
|
||||
}
|
||||
}
|
||||
|
||||
void MapMgr::CreateMapObject(MetaData::MapTplThing& thing_tpl)
|
||||
{
|
||||
int thing_id = thing_tpl.RandThing();
|
||||
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id);
|
||||
if (thing_meta) {
|
||||
if (thing_meta->i->is_house()) {
|
||||
CreateBuilding(thing_id, thing_tpl.i->x(), thing_tpl.i->y());
|
||||
} else {
|
||||
InternalCreateObstacle(thing_id, thing_tpl.i->x(), thing_tpl.i->y(),
|
||||
[] (Obstacle* entity)
|
||||
{
|
||||
});
|
||||
|
||||
}
|
||||
} else {
|
||||
loots_.push_back(&thing_tpl);
|
||||
}
|
||||
}
|
||||
|
||||
void MapMgr::CreateBuilding(int thing_id, float building_x, float building_y)
|
||||
{
|
||||
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id);
|
||||
if (!thing_meta) {
|
||||
return;
|
||||
}
|
||||
MetaData::Building* building_meta = MetaMgr::Instance()->GetBuilding(thing_meta->i->house_id());
|
||||
if (!building_meta) {
|
||||
return;
|
||||
}
|
||||
Building* building = new Building();
|
||||
building->meta = building_meta;
|
||||
building->is_permanent = true;
|
||||
building->permanent_map_service = map_service_;
|
||||
building->building_id = thing_id;
|
||||
building->entity_uniid = AllocUniid();
|
||||
building->SetPos(a8::Vec2(building_x, building_y));
|
||||
building->Initialize();
|
||||
uniid_hash_[building->entity_uniid] = building;
|
||||
grid_service_->AddPermanentEntity(building);
|
||||
for (size_t door_idx = 0; door_idx < building_meta->doors.size(); ++door_idx) {
|
||||
if (door_idx >= 0 && door_idx < building->meta->doors.size()) {
|
||||
MetaData::Building::Door* door_meta = &building->meta->doors[door_idx];
|
||||
float x = building->GetX() + door_meta->state0->x() - building->meta->i->tilewidth() / 2.0;
|
||||
float y = building->GetY() + door_meta->state0->y() - building->meta->i->tileheight() / 2.0;
|
||||
InternalCreateObstacle(DOOR_THING_ID, x, y,
|
||||
[building, door_idx] (Obstacle* entity)
|
||||
{
|
||||
entity->SetDoorInfo(building, door_idx);
|
||||
});
|
||||
}
|
||||
}
|
||||
for (auto& obj : building_meta->i->lootobj()) {
|
||||
float x = building->GetX() + obj.x() - building->meta->i->tilewidth() / 2.0;
|
||||
float y = building->GetY() + obj.y() - building->meta->i->tileheight() / 2.0;
|
||||
if (obj._rand_space() > 0) {
|
||||
int rnd = rand () % obj._rand_space();
|
||||
for (auto& pair : obj._things()) {
|
||||
if (rnd <= pair.value()) {
|
||||
InternalCreateObstacle(pair.key(), x, y,
|
||||
[building] (Obstacle* entity)
|
||||
{
|
||||
entity->SetBuilding(building);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
buildings_.push_back(building);
|
||||
}
|
||||
|
||||
Obstacle* MapMgr::InternalCreateObstacle(int id, float x, float y,
|
||||
std::function<void (Obstacle*)> on_precreate)
|
||||
{
|
||||
MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id);
|
||||
if (thing) {
|
||||
Obstacle* entity = new Obstacle();
|
||||
entity->meta = thing;
|
||||
entity->is_permanent = true;
|
||||
entity->permanent_map_service = map_service_;
|
||||
entity->entity_uniid = AllocUniid();
|
||||
entity->SetPos(a8::Vec2(x, y));
|
||||
entity->Initialize();
|
||||
if (on_precreate) {
|
||||
on_precreate(entity);
|
||||
}
|
||||
uniid_hash_[entity->entity_uniid] = entity;
|
||||
grid_service_->AddPermanentEntity(entity);
|
||||
return entity;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Entity* MapMgr::GetEntityByUniId(int uniid)
|
||||
{
|
||||
auto itr = uniid_hash_.find(uniid);
|
||||
return itr != uniid_hash_.end() ? itr->second : nullptr;
|
||||
}
|
||||
|
||||
int MapMgr::AllocUniid()
|
||||
{
|
||||
while (GetEntityByUniId(++current_uniid_) || current_uniid_ == 0) {
|
||||
}
|
||||
return current_uniid_;
|
||||
}
|
46
server/gameserver/mapmgr.h
Normal file
46
server/gameserver/mapmgr.h
Normal file
@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
struct Map;
|
||||
struct MapTplThing;
|
||||
}
|
||||
|
||||
class Entity;
|
||||
class Obstacle;
|
||||
class Building;
|
||||
class MapService;
|
||||
class GridService;
|
||||
class Room;
|
||||
class MapMgr : public a8::Singleton<MapMgr>
|
||||
{
|
||||
private:
|
||||
MapMgr() {};
|
||||
friend class a8::Singleton<MapMgr>;
|
||||
|
||||
public:
|
||||
void Init();
|
||||
void UnInit();
|
||||
|
||||
void AttachRoom(Room* room);
|
||||
|
||||
private:
|
||||
void CreateThings();
|
||||
void CreateMapObject(MetaData::MapTplThing& thing_tpl);
|
||||
void CreateBuilding(int thing_id, float building_x, float building_y);
|
||||
Obstacle* InternalCreateObstacle(int id, float x, float y,
|
||||
std::function<void (Obstacle*)> on_precreate);
|
||||
Entity* GetEntityByUniId(int uniid);
|
||||
int AllocUniid();
|
||||
|
||||
private:
|
||||
int current_uniid_ = 0;
|
||||
std::map<int, Entity*> uniid_hash_;
|
||||
MapService* map_service_ = nullptr;
|
||||
GridService* grid_service_ = nullptr;
|
||||
MetaData::Map* map_meta_ = nullptr;
|
||||
std::vector<MetaData::MapTplThing*> spawn_points_;
|
||||
MetaData::MapTplThing* newbie_born_point_ = nullptr;
|
||||
std::vector<MetaData::MapTplThing*> loots_;
|
||||
std::vector<Building*> buildings_;
|
||||
};
|
@ -13,17 +13,11 @@
|
||||
#include "metamgr.h"
|
||||
#include "jsondatamgr.h"
|
||||
#include "playermgr.h"
|
||||
#include "mapservice.h"
|
||||
#include "gridservice.h"
|
||||
#include "building.h"
|
||||
#include "obstacle.h"
|
||||
#include "loot.h"
|
||||
#include "mapmgr.h"
|
||||
|
||||
#include "framework/cpp/httpclientpool.h"
|
||||
#include "framework/cpp/utils.h"
|
||||
|
||||
const int MAP_GRID_WIDTH = 64;
|
||||
|
||||
const int ROOM_NUM_DOWN_LIMIT = 15;
|
||||
const int ROOM_NUM_UP_LIMIT = 40;
|
||||
const int HUM_NUM_DOWN_LIMIT = 1000;
|
||||
@ -51,28 +45,6 @@ static RoomType_e GetHumanRoomType(const cs::CMJoin& msg)
|
||||
|
||||
void RoomMgr::Init()
|
||||
{
|
||||
map_meta_ = MetaMgr::Instance()->GetMap(2001);
|
||||
if (!map_meta_) {
|
||||
abort();
|
||||
}
|
||||
map_service_ = new MapService();
|
||||
grid_service_ = new GridService();
|
||||
grid_service_->Init(map_meta_->i->map_width(),
|
||||
map_meta_->i->map_height(),
|
||||
MetaMgr::Instance()->map_cell_width);
|
||||
map_service_->Init(map_meta_->i->map_width() / MAP_GRID_WIDTH,
|
||||
map_meta_->i->map_height() / MAP_GRID_WIDTH,
|
||||
MAP_GRID_WIDTH);
|
||||
CreateThings();
|
||||
a8::XPrintf("current_uniid:%d loots:%d spawn_points:%d\n",
|
||||
{
|
||||
current_uniid_,
|
||||
loots_.size(),
|
||||
spawn_points_.size()
|
||||
});
|
||||
if (current_uniid_ >= FIXED_OBJECT_MAXID) {
|
||||
abort();
|
||||
}
|
||||
InstallReportStateTimer();
|
||||
}
|
||||
|
||||
@ -86,10 +58,6 @@ void RoomMgr::UnInit()
|
||||
pair.second->UnInit();
|
||||
delete pair.second;
|
||||
}
|
||||
map_service_->UnInit();
|
||||
grid_service_->UnInit();
|
||||
A8_SAFE_DELETE(map_service_);
|
||||
A8_SAFE_DELETE(grid_service_);
|
||||
}
|
||||
|
||||
void RoomMgr::Update(int delta_time)
|
||||
@ -190,18 +158,19 @@ Room* RoomMgr::GetRoomByIdx(int room_idx)
|
||||
|
||||
void RoomMgr::AddOverRoom(long long room_uuid)
|
||||
{
|
||||
auto callback = [] (const a8::XParams& param)
|
||||
{
|
||||
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
|
||||
if (room) {
|
||||
if ((room->pending_request <= 0) ||
|
||||
(a8::XGetTickCount() - room->game_over_tick > 1000 * 8)) {
|
||||
RoomMgr::Instance()->room_hash_.erase(room->room_uuid);
|
||||
RoomMgr::Instance()->over_room_hash_[room->room_uuid] = room;
|
||||
RoomMgr::Instance()->FreeOverRoom(param.sender);
|
||||
}
|
||||
}
|
||||
};
|
||||
auto callback =
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
Room* room = RoomMgr::Instance()->GetRoomByUuid(param.sender);
|
||||
if (room) {
|
||||
if ((room->pending_request <= 0) ||
|
||||
(a8::XGetTickCount() - room->game_over_tick > 1000 * 8)) {
|
||||
RoomMgr::Instance()->room_hash_.erase(room->room_uuid);
|
||||
RoomMgr::Instance()->over_room_hash_[room->room_uuid] = room;
|
||||
RoomMgr::Instance()->FreeOverRoom(param.sender);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
inactive_room_hash_.erase(room_uuid);
|
||||
Room* room = GetRoomByUuid(room_uuid);
|
||||
@ -221,40 +190,44 @@ void RoomMgr::ActiveRoom(long long room_uuid)
|
||||
|
||||
void RoomMgr::ReportServerState(int instance_id, const std::string& host, int port)
|
||||
{
|
||||
auto on_ok = [] (a8::XParams& param, a8::XObject& data)
|
||||
auto on_ok =
|
||||
[] (a8::XParams& param, a8::XObject& data)
|
||||
{
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach
|
||||
(1000,
|
||||
a8::XParams()
|
||||
.SetSender(param.sender)
|
||||
.SetParam1(param.param1)
|
||||
.SetParam2(param.param2),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000,
|
||||
a8::XParams()
|
||||
.SetSender(param.sender)
|
||||
.SetParam1(param.param1)
|
||||
.SetParam2(param.param2),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&RoomMgr::Instance()->reportstate_timer_attacher_.timer_list_);
|
||||
};
|
||||
auto on_error = [] (a8::XParams& param, const std::string& response)
|
||||
{
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000,
|
||||
a8::XParams()
|
||||
.SetSender(param.sender)
|
||||
.SetParam1(param.param1)
|
||||
.SetParam2(param.param2),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&RoomMgr::Instance()->reportstate_timer_attacher_.timer_list_);
|
||||
};
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&RoomMgr::Instance()->reportstate_timer_attacher_.timer_list_);
|
||||
};
|
||||
auto on_error =
|
||||
[] (a8::XParams& param, const std::string& response)
|
||||
{
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach
|
||||
(1000,
|
||||
a8::XParams()
|
||||
.SetSender(param.sender)
|
||||
.SetParam1(param.param1)
|
||||
.SetParam2(param.param2),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&RoomMgr::Instance()->reportstate_timer_attacher_.timer_list_);
|
||||
};
|
||||
std::string url = a8::Format("http://%s:%d/webapp/index.php?c=GS&a=report&",
|
||||
{
|
||||
host,
|
||||
@ -303,20 +276,21 @@ void RoomMgr::InstallReportStateTimer()
|
||||
std::string remote_ip = master_svr_conf->At("ip")->AsXValue();
|
||||
int remote_port = master_svr_conf->At("listen_port")->AsXValue();
|
||||
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach(1000 + (i + 1),
|
||||
a8::XParams()
|
||||
.SetSender(instance_id)
|
||||
.SetParam1(remote_ip)
|
||||
.SetParam2(remote_port),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&reportstate_timer_attacher_.timer_list_);
|
||||
a8::Timer::Instance()->AddDeadLineTimerAndAttach
|
||||
(1000 + (i + 1),
|
||||
a8::XParams()
|
||||
.SetSender(instance_id)
|
||||
.SetParam1(remote_ip)
|
||||
.SetParam2(remote_port),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
RoomMgr::Instance()->ReportServerState(
|
||||
param.sender,
|
||||
param.param1,
|
||||
param.param2
|
||||
);
|
||||
},
|
||||
&reportstate_timer_attacher_.timer_list_);
|
||||
}
|
||||
}
|
||||
|
||||
@ -329,152 +303,6 @@ bool RoomMgr::IsLimitJoin()
|
||||
);
|
||||
}
|
||||
|
||||
void RoomMgr::CreateThings()
|
||||
{
|
||||
std::string map_tpl_name = map_meta_->RandTemplate();
|
||||
std::map<std::string, MetaData::MapTplThing*> spawn_points_hash;
|
||||
std::vector<MetaData::MapTplThing>* things = MetaMgr::Instance()->GetMapTplThing(map_tpl_name);
|
||||
if (things) {
|
||||
for (auto& thing_tpl : *things) {
|
||||
switch (thing_tpl.i->_object_type()) {
|
||||
case kMOT_Object:
|
||||
{
|
||||
if (thing_tpl.i->weight() >= rand() % 10000) {
|
||||
CreateMapObject(thing_tpl);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kMOT_SpawnPoint:
|
||||
{
|
||||
if (spawn_points_hash.find(thing_tpl.i->name()) !=
|
||||
spawn_points_hash.end()) {
|
||||
abort();
|
||||
}
|
||||
spawn_points_.push_back(&thing_tpl);
|
||||
spawn_points_hash[thing_tpl.i->name()] = &thing_tpl;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
abort();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (spawn_points_hash.find(MetaMgr::Instance()->newbie_born_point) !=
|
||||
spawn_points_hash.end()) {
|
||||
newbie_born_point_ = spawn_points_hash[MetaMgr::Instance()->newbie_born_point];
|
||||
}
|
||||
}
|
||||
|
||||
void RoomMgr::CreateMapObject(MetaData::MapTplThing& thing_tpl)
|
||||
{
|
||||
int thing_id = thing_tpl.RandThing();
|
||||
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id);
|
||||
if (thing_meta) {
|
||||
if (thing_meta->i->is_house()) {
|
||||
CreateBuilding(thing_id, thing_tpl.i->x(), thing_tpl.i->y());
|
||||
} else {
|
||||
InternalCreateObstacle(thing_id, thing_tpl.i->x(), thing_tpl.i->y(),
|
||||
[] (Obstacle* entity)
|
||||
{
|
||||
});
|
||||
|
||||
}
|
||||
} else {
|
||||
loots_.push_back(&thing_tpl);
|
||||
}
|
||||
}
|
||||
|
||||
void RoomMgr::CreateBuilding(int thing_id, float building_x, float building_y)
|
||||
{
|
||||
MetaData::MapThing* thing_meta = MetaMgr::Instance()->GetMapThing(thing_id);
|
||||
if (!thing_meta) {
|
||||
return;
|
||||
}
|
||||
MetaData::Building* building_meta = MetaMgr::Instance()->GetBuilding(thing_meta->i->house_id());
|
||||
if (!building_meta) {
|
||||
return;
|
||||
}
|
||||
Building* building = new Building();
|
||||
#if 0
|
||||
building->room = this;
|
||||
#endif
|
||||
building->meta = building_meta;
|
||||
building->is_permanent = true;
|
||||
building->permanent_map_service = map_service_;
|
||||
building->building_id = thing_id;
|
||||
building->entity_uniid = AllocUniid();
|
||||
building->SetPos(a8::Vec2(building_x, building_y));
|
||||
building->Initialize();
|
||||
uniid_hash_[building->entity_uniid] = building;
|
||||
grid_service_->AddPermanentEntity(building);
|
||||
for (size_t door_idx = 0; door_idx < building_meta->doors.size(); ++door_idx) {
|
||||
if (door_idx >= 0 && door_idx < building->meta->doors.size()) {
|
||||
MetaData::Building::Door* door_meta = &building->meta->doors[door_idx];
|
||||
float x = building->GetX() + door_meta->state0->x() - building->meta->i->tilewidth() / 2.0;
|
||||
float y = building->GetY() + door_meta->state0->y() - building->meta->i->tileheight() / 2.0;
|
||||
InternalCreateObstacle(DOOR_THING_ID, x, y,
|
||||
[building, door_idx] (Obstacle* entity)
|
||||
{
|
||||
entity->SetDoorInfo(building, door_idx);
|
||||
});
|
||||
}
|
||||
}
|
||||
for (auto& obj : building_meta->i->lootobj()) {
|
||||
float x = building->GetX() + obj.x() - building->meta->i->tilewidth() / 2.0;
|
||||
float y = building->GetY() + obj.y() - building->meta->i->tileheight() / 2.0;
|
||||
if (obj._rand_space() > 0) {
|
||||
int rnd = rand () % obj._rand_space();
|
||||
for (auto& pair : obj._things()) {
|
||||
if (rnd <= pair.value()) {
|
||||
InternalCreateObstacle(pair.key(), x, y,
|
||||
[building] (Obstacle* entity)
|
||||
{
|
||||
entity->SetBuilding(building);
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
buildings_.push_back(building);
|
||||
}
|
||||
|
||||
Obstacle* RoomMgr::InternalCreateObstacle(int id, float x, float y,
|
||||
std::function<void (Obstacle*)> on_precreate)
|
||||
{
|
||||
MetaData::MapThing* thing = MetaMgr::Instance()->GetMapThing(id);
|
||||
if (thing) {
|
||||
Obstacle* entity = new Obstacle();
|
||||
entity->meta = thing;
|
||||
entity->is_permanent = true;
|
||||
entity->permanent_map_service = map_service_;
|
||||
entity->entity_uniid = AllocUniid();
|
||||
entity->SetPos(a8::Vec2(x, y));
|
||||
entity->Initialize();
|
||||
if (on_precreate) {
|
||||
on_precreate(entity);
|
||||
}
|
||||
uniid_hash_[entity->entity_uniid] = entity;
|
||||
grid_service_->AddPermanentEntity(entity);
|
||||
return entity;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Entity* RoomMgr::GetEntityByUniId(int uniid)
|
||||
{
|
||||
auto itr = uniid_hash_.find(uniid);
|
||||
return itr != uniid_hash_.end() ? itr->second : nullptr;
|
||||
}
|
||||
|
||||
int RoomMgr::AllocUniid()
|
||||
{
|
||||
while (GetEntityByUniId(++current_uniid_) || current_uniid_ == 0) {
|
||||
}
|
||||
return current_uniid_;
|
||||
}
|
||||
|
||||
int RoomMgr::AllocRoomIdx()
|
||||
{
|
||||
do {
|
||||
@ -498,19 +326,13 @@ Room* RoomMgr::CreateRoom(RoomType_e room_type)
|
||||
room->room_type = room_type;
|
||||
room->room_uuid = App::Instance()->NewUuid();
|
||||
room->room_idx = AllocRoomIdx();
|
||||
room->grid_service = grid_service_;
|
||||
room->map_service = map_service_;
|
||||
room->spawn_points = &spawn_points_;
|
||||
room->newbie_born_point_meta = newbie_born_point_;
|
||||
room->loots = &loots_;
|
||||
room->buildings = &buildings_;
|
||||
MapMgr::Instance()->AttachRoom(room);
|
||||
if (GetRoomByUuid(room->room_uuid)) {
|
||||
abort();
|
||||
}
|
||||
if (GetRoomByIdx(room->room_idx)) {
|
||||
abort();
|
||||
}
|
||||
room->map_meta = map_meta_;
|
||||
room->Init();
|
||||
inactive_room_hash_[room->room_uuid] = room;
|
||||
room_hash_[room->room_uuid] = room;
|
||||
@ -526,23 +348,24 @@ void RoomMgr::JoinErrorHandle(const cs::CMJoin& msg, int error_code, int socket_
|
||||
GGListener::Instance()->SendToClient(socket_handle, 0, notifymsg);
|
||||
}
|
||||
{
|
||||
a8::Timer::Instance()->
|
||||
AddDeadLineTimer(1000 * 2,
|
||||
a8::XParams()
|
||||
.SetSender(socket_handle),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
GGListener::Instance()->ForceCloseChildSocket(param.sender);
|
||||
});
|
||||
a8::Timer::Instance()->AddDeadLineTimer
|
||||
(1000 * 2,
|
||||
a8::XParams()
|
||||
.SetSender(socket_handle),
|
||||
[] (const a8::XParams& param)
|
||||
{
|
||||
GGListener::Instance()->ForceCloseChildSocket(param.sender);
|
||||
});
|
||||
}
|
||||
a8::UdpLog::Instance()->Warning("join error errcode:%d accountid:%s max_mainloop_rundelay:%d "
|
||||
"room_num:%d player_num:%d online_num:%d",
|
||||
{
|
||||
error_code,
|
||||
msg.account_id(),
|
||||
App::Instance()->perf.max_run_delay_time,
|
||||
RoomMgr::Instance()->RoomNum(),
|
||||
App::Instance()->perf.entity_num[ET_Player],
|
||||
PlayerMgr::Instance()->OnlineNum(),
|
||||
});
|
||||
a8::UdpLog::Instance()->Warning
|
||||
("join error errcode:%d accountid:%s max_mainloop_rundelay:%d "
|
||||
"room_num:%d player_num:%d online_num:%d",
|
||||
{
|
||||
error_code,
|
||||
msg.account_id(),
|
||||
App::Instance()->perf.max_run_delay_time,
|
||||
RoomMgr::Instance()->RoomNum(),
|
||||
App::Instance()->perf.entity_num[ET_Player],
|
||||
PlayerMgr::Instance()->OnlineNum(),
|
||||
});
|
||||
}
|
||||
|
@ -7,23 +7,7 @@ namespace cs
|
||||
class CMJoin;
|
||||
}
|
||||
|
||||
namespace MetaData
|
||||
{
|
||||
struct Map;
|
||||
struct MapTplThing;
|
||||
}
|
||||
|
||||
namespace metatable
|
||||
{
|
||||
class DropObjJson;
|
||||
}
|
||||
|
||||
class Room;
|
||||
class Entity;
|
||||
class Obstacle;
|
||||
class Building;
|
||||
class MapService;
|
||||
class GridService;
|
||||
class RoomMgr : public a8::Singleton<RoomMgr>
|
||||
{
|
||||
public:
|
||||
@ -43,42 +27,26 @@ class RoomMgr : public a8::Singleton<RoomMgr>
|
||||
int RoomNum();
|
||||
int OverRoomNum();
|
||||
Room* GetRoomByUuid(long long uuid);
|
||||
Room* GetRoomByIdx(int room_idx);
|
||||
void AddOverRoom(long long room_uuid);
|
||||
void InstallReportStateTimer();
|
||||
|
||||
private:
|
||||
void InstallReportStateTimer();
|
||||
Room* GetRoomByIdx(int room_idx);
|
||||
Room* GetJoinableRoom(const cs::CMJoin& msg, const RoomType_e self_room_type);
|
||||
void ReportServerState(int instance_id, const std::string& host, int port);
|
||||
void FreeOverRoom(long long room_uuid);
|
||||
bool IsLimitJoin();
|
||||
|
||||
void CreateThings();
|
||||
void CreateMapObject(MetaData::MapTplThing& thing_tpl);
|
||||
void CreateBuilding(int thing_id, float building_x, float building_y);
|
||||
Obstacle* InternalCreateObstacle(int id, float x, float y,
|
||||
std::function<void (Obstacle*)> on_precreate);
|
||||
Entity* GetEntityByUniId(int uniid);
|
||||
int AllocUniid();
|
||||
int AllocRoomIdx();
|
||||
Room* CreateRoom(RoomType_e room_type);
|
||||
void JoinErrorHandle(const cs::CMJoin& msg, int error_code, int socket_handle);
|
||||
|
||||
private:
|
||||
int current_room_idx_ = 0;
|
||||
|
||||
std::map<long long, Room*> inactive_room_hash_;
|
||||
std::map<long long, Room*> room_hash_;
|
||||
std::map<int, Room*> room_idx_hash_;
|
||||
std::map<long long, Room*> over_room_hash_;
|
||||
a8::TimerAttacher reportstate_timer_attacher_;
|
||||
|
||||
int current_room_idx_ = 0;
|
||||
int current_uniid_ = 0;
|
||||
std::map<int, Entity*> uniid_hash_;
|
||||
MapService* map_service_ = nullptr;
|
||||
GridService* grid_service_ = nullptr;
|
||||
MetaData::Map* map_meta_ = nullptr;
|
||||
std::vector<MetaData::MapTplThing*> spawn_points_;
|
||||
MetaData::MapTplThing* newbie_born_point_ = nullptr;
|
||||
std::vector<MetaData::MapTplThing*> loots_;
|
||||
std::vector<Building*> buildings_;
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user