From 680e25e15b2b26c6a78e1097bab064ecd6b99ad3 Mon Sep 17 00:00:00 2001 From: aozhiwei Date: Sat, 30 May 2020 20:50:55 +0800 Subject: [PATCH] yuhua grid_id --- server/gameserver/entity.cc | 4 +-- server/gameserver/entity.h | 6 ++-- server/gameserver/gridservice.cc | 50 +++++++++++++++++--------------- server/gameserver/human.cc | 4 +-- server/gameserver/room.cc | 4 +-- 5 files changed, 35 insertions(+), 33 deletions(-) diff --git a/server/gameserver/entity.cc b/server/gameserver/entity.cc index 6ec6b6f..83fb170 100644 --- a/server/gameserver/entity.cc +++ b/server/gameserver/entity.cc @@ -93,7 +93,7 @@ void Entity::ClearColliders() void Entity::BroadcastFullState(Room* room) { std::set tmp_grids; - room->grid_service->GetAllCells(room, grid_id, tmp_grids); + room->grid_service->GetAllCells(room, grid_id_, tmp_grids); room->grid_service->TouchAllLayerHumanList ( room->GetRoomIdx(), @@ -107,7 +107,7 @@ void Entity::BroadcastFullState(Room* room) void Entity::BroadcastDeleteState(Room* room) { std::set tmp_grids; - room->grid_service->GetAllCells(room, grid_id, tmp_grids); + room->grid_service->GetAllCells(room, grid_id_, tmp_grids); room->grid_service->TouchAllLayerHumanList ( room->GetRoomIdx(), diff --git a/server/gameserver/entity.h b/server/gameserver/entity.h index efc8282..088de4b 100644 --- a/server/gameserver/entity.h +++ b/server/gameserver/entity.h @@ -14,8 +14,6 @@ class Entity { public: - int grid_id = 0; - Entity(); virtual ~Entity(); virtual void Initialize(); @@ -32,6 +30,7 @@ class Entity EntitySubType_e GetEntitySubType() { return entity_subtype_; } virtual bool IsEntityType(EntityType_e type) { return type == entity_type_;} virtual bool IsEntitySubType(EntitySubType_e subtype) { return subtype == entity_subtype_;} + int GetGridId() const { return grid_id_; } bool TestCollision(Room* room, Entity* b); bool TestCollision(Room* room, ColliderComponent* b); bool TestCollisionEx(Room* room, const a8::Vec2& aabb_pos, AabbCollider& aabb_box); @@ -47,6 +46,7 @@ class Entity private: void ClearColliders(); + void SetGridId(int grid_id) { grid_id_ = grid_id; } protected: std::list colliders_; @@ -57,6 +57,8 @@ private: EntitySubType_e entity_subtype_ = EST_None; a8::Vec2 pos_; + int grid_id_ = 0; friend class EntityFactory; + friend class GridService; }; diff --git a/server/gameserver/gridservice.cc b/server/gameserver/gridservice.cc index 1908a28..2e09109 100644 --- a/server/gameserver/gridservice.cc +++ b/server/gameserver/gridservice.cc @@ -120,12 +120,12 @@ void GridService::AddHuman(Human* hum) if (BroderOverFlow(x, y)) { abort(); } - hum->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_; - if (hum->grid_id == 0 || hum->grid_id > max_grid_id_) { + hum->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_); + if (hum->GetGridId() == 0 || hum->GetGridId() > max_grid_id_) { abort(); } - cells_[hum->grid_id].human_list[hum->room->GetRoomIdx()].insert(hum); - GetAllCells(hum->room, hum->grid_id, hum->GetGridList()); + cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].insert(hum); + GetAllCells(hum->room, hum->GetGridId(), hum->GetGridList()); } void GridService::MoveHuman(Human* hum) @@ -139,19 +139,19 @@ void GridService::MoveHuman(Human* hum) if (new_grid_id == 0 || new_grid_id > max_grid_id_) { abort(); } - if (new_grid_id != hum->grid_id) { + if (new_grid_id != hum->GetGridId()) { std::set inc_grid_list; std::set dec_grid_list; std::set old_grid_list = hum->GetGridList(); ComputeDiff(hum->room, - hum->grid_id, + hum->GetGridId(), new_grid_id, hum->GetGridList(), inc_grid_list, dec_grid_list); - cells_[hum->grid_id].human_list[hum->room->GetRoomIdx()].erase(hum); + cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].erase(hum); cells_[new_grid_id].human_list[hum->room->GetRoomIdx()].insert(hum); - hum->grid_id = new_grid_id; + hum->SetGridId(new_grid_id); hum->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list); } } @@ -163,12 +163,12 @@ void GridService::AddBullet(Bullet* bullet) if (BroderOverFlow(x, y)) { abort(); } - bullet->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_; - if (bullet->grid_id == 0 || bullet->grid_id > max_grid_id_) { + bullet->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_); + if (bullet->GetGridId() == 0 || bullet->GetGridId() > max_grid_id_) { abort(); } - cells_[bullet->grid_id].bullet_list[bullet->room->GetRoomIdx()].insert(bullet); - GetAllCells(bullet->room, bullet->grid_id, bullet->GetGridList()); + cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].insert(bullet); + GetAllCells(bullet->room, bullet->GetGridId(), bullet->GetGridList()); } void GridService::MoveBullet(Bullet* bullet) @@ -182,25 +182,25 @@ void GridService::MoveBullet(Bullet* bullet) if (new_grid_id == 0 || new_grid_id > max_grid_id_) { abort(); } - if (new_grid_id != bullet->grid_id) { + if (new_grid_id != bullet->GetGridId()) { std::set inc_grid_list; std::set dec_grid_list; std::set old_grid_list = bullet->GetGridList(); ComputeDiff(bullet->room, - bullet->grid_id, + bullet->GetGridId(), new_grid_id, bullet->GetGridList(), inc_grid_list, dec_grid_list); - cells_[bullet->grid_id].bullet_list[bullet->room->GetRoomIdx()].erase(bullet); + cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].erase(bullet); cells_[new_grid_id].bullet_list[bullet->room->GetRoomIdx()].insert(bullet); - bullet->grid_id = new_grid_id; + bullet->SetGridId(new_grid_id); } } void GridService::DelBullet(Bullet* bullet) { - GridCell& cell = cells_[bullet->grid_id]; + GridCell& cell = cells_[bullet->GetGridId()]; cell.bullet_list[bullet->room->GetRoomIdx()].erase(bullet); } @@ -212,16 +212,16 @@ void GridService::AddRoomEntity(Room* room, Entity* entity) if (BroderOverFlow(x, y)) { abort(); } - entity->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_; - if (entity->grid_id == 0 || entity->grid_id > max_grid_id_) { + entity->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_); + if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) { abort(); } - cells_[entity->grid_id].entity_list[room->GetRoomIdx()].insert(entity); + cells_[entity->GetGridId()].entity_list[room->GetRoomIdx()].insert(entity); } void GridService::DelRoomEntity(Room* room, Entity* entity) { - GridCell& cell = cells_[entity->grid_id]; + GridCell& cell = cells_[entity->GetGridId()]; cell.entity_list[room->GetRoomIdx()].erase(entity); } @@ -233,11 +233,11 @@ void GridService::AddPermanentEntity(Entity* entity) if (BroderOverFlow(x, y)) { abort(); } - entity->grid_id = x/cell_width_ + (y/cell_width_) * cell_count_per_row_; - if (entity->grid_id == 0 || entity->grid_id > max_grid_id_) { + entity->SetGridId(x/cell_width_ + (y/cell_width_) * cell_count_per_row_); + if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) { abort(); } - cells_[entity->grid_id].entity_list[0].insert(entity); + cells_[entity->GetGridId()].entity_list[0].insert(entity); } bool GridService::HumanInGridList(Human* hum, std::set& grid_list) @@ -412,4 +412,6 @@ void GridService::DeatchHuman(Human* target) } cell->human_list[target->room->GetRoomIdx()].erase(target); } + target->SetGridId(0); + target->GetGridList().clear(); } diff --git a/server/gameserver/human.cc b/server/gameserver/human.cc index 2c37b14..8bfc227 100644 --- a/server/gameserver/human.cc +++ b/server/gameserver/human.cc @@ -1357,7 +1357,7 @@ void Human::FillMFGasData(cs::MFGasData* gas_data) bool Human::CanSee(const Human* hum) const { - return room->grid_service->InView(grid_id, hum->grid_id); + return room->grid_service->InView(GetGridId(), hum->GetGridId()); } void Human::RecalcVolume() @@ -3268,7 +3268,5 @@ void Human::RemoveFromScene() hum->RemovePartObjects(target); return true; }); - grid_id = 0; - GetGridList().clear(); last_collision_door_ = nullptr; } diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index b8ff0ab..4cee5e0 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -2048,7 +2048,7 @@ void Room::ShuaGridRound(Human* target) a8::HasBitFlag(hum->status, HS_Disable) && !hum->real_dead && hum->team_uuid.empty() && - grid_service->InView(target->grid_id, hum->GetPos().x, hum->GetPos().y) + grid_service->InView(target->GetGridId(), hum->GetPos().x, hum->GetPos().y) ) { EnableHuman(hum); xtimer.AddDeadLineTimerAndAttach @@ -2074,7 +2074,7 @@ void Room::ShuaGridRound(Human* target) } } #ifdef DEBUG - a8::UdpLog::Instance()->Debug("OnHumanGrid %d %d", {target->grid_id, count}); + a8::UdpLog::Instance()->Debug("OnHumanGrid %d %d", {target->GetGridId(), count}); #endif #ifdef DEBUG CheckPartObjects();