youhua gridcell
This commit is contained in:
parent
680e25e15b
commit
ae1ce9b415
128
server/gameserver/gridcell.cc
Normal file
128
server/gameserver/gridcell.cc
Normal file
@ -0,0 +1,128 @@
|
||||
#include "precompile.h"
|
||||
|
||||
#include "gridcell.h"
|
||||
#include "bullet.h"
|
||||
#include "entity.h"
|
||||
#include "human.h"
|
||||
#include "room.h"
|
||||
|
||||
GridCell::GridCell()
|
||||
{
|
||||
human_list_.reserve(MAX_ROOM_IDX);
|
||||
entity_list_.reserve(MAX_ROOM_IDX);
|
||||
bullet_list_.reserve(MAX_ROOM_IDX);
|
||||
for (int i = 0; i < MAX_ROOM_IDX; ++i) {
|
||||
human_list_.push_back(std::set<Human*>());
|
||||
entity_list_.push_back(std::set<Entity*>());
|
||||
bullet_list_.push_back(std::set<Bullet*>());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void GridCell::ClearRoomData(Room* room)
|
||||
{
|
||||
human_list_[room->GetRoomIdx()].clear();
|
||||
entity_list_[room->GetRoomIdx()].clear();
|
||||
bullet_list_[room->GetRoomIdx()].clear();
|
||||
}
|
||||
|
||||
void GridCell::AddHuman(Human* hum)
|
||||
{
|
||||
human_list_[hum->room->GetRoomIdx()].insert(hum);
|
||||
}
|
||||
|
||||
void GridCell::RemoveHuman(Human* hum)
|
||||
{
|
||||
human_list_[hum->room->GetRoomIdx()].erase(hum);
|
||||
}
|
||||
|
||||
bool GridCell::ExistsHuman(Human* hum)
|
||||
{
|
||||
return human_list_[hum->room->GetRoomIdx()].find(hum) != human_list_[hum->room->GetRoomIdx()].end();
|
||||
}
|
||||
|
||||
void GridCell::TouchHumanList(std::function<void (Human*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop)
|
||||
{
|
||||
for (Human* hum : human_list_[room_idx]) {
|
||||
func(hum, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GridCell::TouchHumanListEx(std::function<void (Human*, bool&)> func,
|
||||
int room_idx,
|
||||
bool& stop)
|
||||
{
|
||||
TouchHumanList(func, room_idx, stop);
|
||||
}
|
||||
|
||||
void GridCell::AddBullet(Bullet* bullet)
|
||||
{
|
||||
bullet_list_[bullet->room->GetRoomIdx()].insert(bullet);
|
||||
}
|
||||
|
||||
void GridCell::RemoveBullet(Bullet* bullet)
|
||||
{
|
||||
bullet_list_[bullet->room->GetRoomIdx()].erase(bullet);
|
||||
}
|
||||
|
||||
void GridCell::AddPermanentEntity(Entity* entity)
|
||||
{
|
||||
entity_list_[0].insert(entity);
|
||||
}
|
||||
|
||||
void GridCell::AddRoomEntity(Room* room, Entity* entity)
|
||||
{
|
||||
entity_list_[room->GetRoomIdx()].insert(entity);
|
||||
}
|
||||
|
||||
void GridCell::RemoveRoomEntity(Room* room, Entity* entity)
|
||||
{
|
||||
entity_list_[room->GetRoomIdx()].erase(entity);
|
||||
}
|
||||
|
||||
bool GridCell::ExistsEntity(Room* room, Entity* entity)
|
||||
{
|
||||
if (entity_list_[0].find(entity) != entity_list_[0].end()) {
|
||||
return true;
|
||||
}
|
||||
return entity_list_[room->GetRoomIdx()].find(entity) !=
|
||||
entity_list_[room->GetRoomIdx()].end();
|
||||
}
|
||||
|
||||
void GridCell::TouchLayer0EntityList(std::function<void (Entity*, bool&)>& func,
|
||||
bool& stop)
|
||||
{
|
||||
for (Entity* entity : entity_list_[0]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GridCell::TouchLayer1EntityList(std::function<void (Entity*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop)
|
||||
{
|
||||
for (Entity* entity : entity_list_[room_idx]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GridCell::TouchAllLayerEntityList(std::function<void (Entity*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop)
|
||||
{
|
||||
TouchLayer0EntityList(func, stop);
|
||||
if (!stop) {
|
||||
TouchLayer1EntityList(func, room_idx, stop);
|
||||
}
|
||||
}
|
43
server/gameserver/gridcell.h
Normal file
43
server/gameserver/gridcell.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
class Entity;
|
||||
class Human;
|
||||
class Bullet;
|
||||
class Room;
|
||||
|
||||
class GridCell
|
||||
{
|
||||
public:
|
||||
|
||||
GridCell();
|
||||
void ClearRoomData(Room* room);
|
||||
void AddHuman(Human* hum);
|
||||
void RemoveHuman(Human* hum);
|
||||
bool ExistsHuman(Human* hum);
|
||||
void TouchHumanList(std::function<void (Human*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop);
|
||||
void TouchHumanListEx(std::function<void (Human*, bool&)> func,
|
||||
int room_idx,
|
||||
bool& stop);
|
||||
|
||||
void AddBullet(Bullet* bullet);
|
||||
void RemoveBullet(Bullet* bullet);
|
||||
void AddPermanentEntity(Entity* entity);
|
||||
void AddRoomEntity(Room* room, Entity* entity);
|
||||
void RemoveRoomEntity(Room* room, Entity* entity);
|
||||
bool ExistsEntity(Room* room, Entity* Entity);
|
||||
void TouchLayer0EntityList(std::function<void (Entity*, bool&)>& func,
|
||||
bool& stop);
|
||||
void TouchLayer1EntityList(std::function<void (Entity*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop);
|
||||
void TouchAllLayerEntityList(std::function<void (Entity*, bool&)>& func,
|
||||
int room_idx,
|
||||
bool& stop);
|
||||
|
||||
private:
|
||||
std::vector<std::set<Human*>> human_list_;
|
||||
std::vector<std::set<Entity*>> entity_list_;
|
||||
std::vector<std::set<Bullet*>> bullet_list_;
|
||||
};
|
@ -4,18 +4,7 @@
|
||||
#include "human.h"
|
||||
#include "bullet.h"
|
||||
#include "room.h"
|
||||
|
||||
GridCell::GridCell()
|
||||
{
|
||||
human_list.reserve(MAX_ROOM_IDX);
|
||||
entity_list.reserve(MAX_ROOM_IDX);
|
||||
bullet_list.reserve(MAX_ROOM_IDX);
|
||||
for (int i = 0; i < MAX_ROOM_IDX; ++i) {
|
||||
human_list.push_back(std::set<Human*>());
|
||||
entity_list.push_back(std::set<Entity*>());
|
||||
bullet_list.push_back(std::set<Bullet*>());
|
||||
}
|
||||
}
|
||||
#include "gridcell.h"
|
||||
|
||||
/*
|
||||
1 2 3
|
||||
@ -107,9 +96,7 @@ void GridService::ClearRoomData(Room* room)
|
||||
}
|
||||
for (int i = 0; i < (max_grid_id_ + 1); ++i) {
|
||||
GridCell& cell = cells_[i];
|
||||
cell.human_list[room->GetRoomIdx()].clear();
|
||||
cell.entity_list[room->GetRoomIdx()].clear();
|
||||
cell.bullet_list[room->GetRoomIdx()].clear();
|
||||
cell.ClearRoomData(room);
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,7 +111,7 @@ void GridService::AddHuman(Human* hum)
|
||||
if (hum->GetGridId() == 0 || hum->GetGridId() > max_grid_id_) {
|
||||
abort();
|
||||
}
|
||||
cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].insert(hum);
|
||||
cells_[hum->GetGridId()].AddHuman(hum);
|
||||
GetAllCells(hum->room, hum->GetGridId(), hum->GetGridList());
|
||||
}
|
||||
|
||||
@ -149,8 +136,8 @@ void GridService::MoveHuman(Human* hum)
|
||||
hum->GetGridList(),
|
||||
inc_grid_list,
|
||||
dec_grid_list);
|
||||
cells_[hum->GetGridId()].human_list[hum->room->GetRoomIdx()].erase(hum);
|
||||
cells_[new_grid_id].human_list[hum->room->GetRoomIdx()].insert(hum);
|
||||
cells_[hum->GetGridId()].RemoveHuman(hum);
|
||||
cells_[new_grid_id].AddHuman(hum);
|
||||
hum->SetGridId(new_grid_id);
|
||||
hum->OnGridListChange(old_grid_list, inc_grid_list, dec_grid_list);
|
||||
}
|
||||
@ -167,7 +154,7 @@ void GridService::AddBullet(Bullet* bullet)
|
||||
if (bullet->GetGridId() == 0 || bullet->GetGridId() > max_grid_id_) {
|
||||
abort();
|
||||
}
|
||||
cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].insert(bullet);
|
||||
cells_[bullet->GetGridId()].AddBullet(bullet);
|
||||
GetAllCells(bullet->room, bullet->GetGridId(), bullet->GetGridList());
|
||||
}
|
||||
|
||||
@ -192,8 +179,8 @@ void GridService::MoveBullet(Bullet* bullet)
|
||||
bullet->GetGridList(),
|
||||
inc_grid_list,
|
||||
dec_grid_list);
|
||||
cells_[bullet->GetGridId()].bullet_list[bullet->room->GetRoomIdx()].erase(bullet);
|
||||
cells_[new_grid_id].bullet_list[bullet->room->GetRoomIdx()].insert(bullet);
|
||||
cells_[bullet->GetGridId()].RemoveBullet(bullet);
|
||||
cells_[new_grid_id].AddBullet(bullet);
|
||||
bullet->SetGridId(new_grid_id);
|
||||
}
|
||||
}
|
||||
@ -201,7 +188,7 @@ void GridService::MoveBullet(Bullet* bullet)
|
||||
void GridService::DelBullet(Bullet* bullet)
|
||||
{
|
||||
GridCell& cell = cells_[bullet->GetGridId()];
|
||||
cell.bullet_list[bullet->room->GetRoomIdx()].erase(bullet);
|
||||
cell.RemoveBullet(bullet);
|
||||
}
|
||||
|
||||
void GridService::AddRoomEntity(Room* room, Entity* entity)
|
||||
@ -216,13 +203,13 @@ void GridService::AddRoomEntity(Room* room, Entity* entity)
|
||||
if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) {
|
||||
abort();
|
||||
}
|
||||
cells_[entity->GetGridId()].entity_list[room->GetRoomIdx()].insert(entity);
|
||||
cells_[entity->GetGridId()].AddRoomEntity(room, entity);
|
||||
}
|
||||
|
||||
void GridService::DelRoomEntity(Room* room, Entity* entity)
|
||||
{
|
||||
GridCell& cell = cells_[entity->GetGridId()];
|
||||
cell.entity_list[room->GetRoomIdx()].erase(entity);
|
||||
cell.RemoveRoomEntity(room, entity);
|
||||
}
|
||||
|
||||
void GridService::AddPermanentEntity(Entity* entity)
|
||||
@ -237,14 +224,13 @@ void GridService::AddPermanentEntity(Entity* entity)
|
||||
if (entity->GetGridId() == 0 || entity->GetGridId() > max_grid_id_) {
|
||||
abort();
|
||||
}
|
||||
cells_[entity->GetGridId()].entity_list[0].insert(entity);
|
||||
cells_[entity->GetGridId()].AddPermanentEntity(entity);
|
||||
}
|
||||
|
||||
bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
|
||||
{
|
||||
for (auto& cell : grid_list) {
|
||||
if (cell->human_list[hum->room->GetRoomIdx()].find(hum) !=
|
||||
cell->human_list[hum->room->GetRoomIdx()].end()) {
|
||||
if (cell->ExistsHuman(hum)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -254,11 +240,7 @@ bool GridService::HumanInGridList(Human* hum, std::set<GridCell*>& grid_list)
|
||||
bool GridService::EntityInGridList(Room* room, Entity* entity, std::set<GridCell*>& grid_list)
|
||||
{
|
||||
for (auto& cell : grid_list) {
|
||||
if (cell->entity_list[0].find(entity) != cell->entity_list[0].end()) {
|
||||
return true;
|
||||
}
|
||||
if (cell->entity_list[room->GetRoomIdx()].find(entity) !=
|
||||
cell->entity_list[room->GetRoomIdx()].end()) {
|
||||
if (cell->ExistsEntity(room, entity)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -344,11 +326,9 @@ void GridService::TouchLayer0EntityList(std::set<GridCell*>& grid_list,
|
||||
{
|
||||
bool stop = false;
|
||||
for (auto& cell : grid_list) {
|
||||
for (Entity* entity : cell->entity_list[0]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
cell->TouchLayer0EntityList(func, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -359,11 +339,9 @@ void GridService::TouchLayer1EntityList(int room_idx,
|
||||
{
|
||||
bool stop = false;
|
||||
for (auto& cell : grid_list) {
|
||||
for (Entity* entity : cell->entity_list[room_idx]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
cell->TouchLayer1EntityList(func, room_idx, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -374,17 +352,9 @@ void GridService::TouchAllLayerEntityList(int room_idx,
|
||||
{
|
||||
bool stop = false;
|
||||
for (auto& cell : grid_list) {
|
||||
for (Entity* entity : cell->entity_list[0]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (Entity* entity : cell->entity_list[room_idx]) {
|
||||
func(entity, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
cell->TouchAllLayerEntityList(func, room_idx, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -395,11 +365,9 @@ void GridService::TouchAllLayerHumanList(int room_idx,
|
||||
{
|
||||
bool stop = false;
|
||||
for (auto& cell : grid_list) {
|
||||
for (Human* hum : cell->human_list[room_idx]) {
|
||||
func(hum, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
cell->TouchHumanList(func, room_idx, stop);
|
||||
if (stop) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -407,10 +375,16 @@ void GridService::TouchAllLayerHumanList(int room_idx,
|
||||
void GridService::DeatchHuman(Human* target)
|
||||
{
|
||||
for (auto& cell : target->GetGridList()) {
|
||||
for (Human* hum : cell->human_list[target->room->GetRoomIdx()]) {
|
||||
hum->AddOutObjects(target);
|
||||
}
|
||||
cell->human_list[target->room->GetRoomIdx()].erase(target);
|
||||
bool stop = false;
|
||||
cell->TouchHumanListEx
|
||||
(
|
||||
[target] (Human* hum, bool& stop)
|
||||
{
|
||||
hum->AddOutObjects(target);
|
||||
},
|
||||
target->room->GetRoomIdx(),
|
||||
stop);
|
||||
cell->RemoveHuman(target);
|
||||
}
|
||||
target->SetGridId(0);
|
||||
target->GetGridList().clear();
|
||||
|
@ -1,21 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
class Entity;
|
||||
class Human;
|
||||
class Bullet;
|
||||
|
||||
struct GridCell
|
||||
{
|
||||
std::vector<std::set<Human*>> human_list;
|
||||
std::vector<std::set<Entity*>> entity_list;
|
||||
std::vector<std::set<Bullet*>> bullet_list;
|
||||
|
||||
GridCell();
|
||||
};
|
||||
|
||||
class Human;
|
||||
class Entity;
|
||||
class Room;
|
||||
class Bullet;
|
||||
class GridCell;
|
||||
class GridService
|
||||
{
|
||||
public:
|
||||
|
Loading…
x
Reference in New Issue
Block a user