diff --git a/server/gameserver/gridservice.cc b/server/gameserver/gridservice.cc index b935319..0c6dd67 100644 --- a/server/gameserver/gridservice.cc +++ b/server/gameserver/gridservice.cc @@ -139,19 +139,38 @@ void GridService::MoveHuman(Human* hum) } } -void GridService::AddEntity(Entity* entity, std::set& inc_grid_list) +void GridService::AddEntity(Entity* entity) { - + int x = (int)entity->pos.x + cell_width_; + int y = (int)entity->pos.y + cell_width_; + 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_) { + abort(); + } + cells_[entity->grid_id].entity_list.insert(entity); } bool GridService::HumanInGridList(Human* hum, std::set& grid_list) { - + for (auto& cell : grid_list) { + if (cell->human_list.find(hum) != cell->human_list.end()) { + return true; + } + } + return false; } bool GridService::EntityInGridList(Entity* entity, std::set& grid_list) { - + for (auto& cell : grid_list) { + if (cell->entity_list.find(entity) != cell->entity_list.end()) { + return true; + } + } + return false; } void GridService::GetGridList(int grid_id, int offset, diff --git a/server/gameserver/gridservice.h b/server/gameserver/gridservice.h index df7306f..a3c4033 100644 --- a/server/gameserver/gridservice.h +++ b/server/gameserver/gridservice.h @@ -37,7 +37,7 @@ class GridService void AddHuman(Human* hum); void MoveHuman(Human* hum); - void AddEntity(Entity* entity, std::set& inc_grid_list); + void AddEntity(Entity* entity); bool HumanInGridList(Human* hum, std::set& grid_list); bool EntityInGridList(Entity* entity, std::set& grid_list); diff --git a/server/gameserver/room.cc b/server/gameserver/room.cc index 18a1244..5721ad5 100644 --- a/server/gameserver/room.cc +++ b/server/gameserver/room.cc @@ -403,10 +403,7 @@ void Room::CreateBuilding(int thing_id, float building_x, float building_y) building->pos = Vector2D(building_x, building_y); building->Initialize(); uniid_hash_[building->entity_uniid] = building; - { - std::set inc_grid_list; - grid_service.AddEntity(building, inc_grid_list); - } + grid_service.AddEntity(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]; @@ -468,10 +465,7 @@ void Room::CreateLoot(int equip_id, Vector2D pos, int count) entity->count = count; entity->Initialize(); uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); - } + grid_service.AddEntity(entity); } } @@ -953,10 +947,7 @@ Obstacle* Room::InternalCreateObstacle(int id, float x, float y, on_precreate(entity); } uniid_hash_[entity->entity_uniid] = entity; - { - std::set inc_grid_list; - grid_service.AddEntity(entity, inc_grid_list); - } + grid_service.AddEntity(entity); return entity; } return nullptr;