game2006/server/gameserver/gridcell.cc
aozhiwei 2b94011b01 1
2024-01-30 16:58:09 +08:00

167 lines
4.2 KiB
C++

#include "precompile.h"
#include "gridcell.h"
#include "entity.h"
#include "human.h"
#include "room.h"
#include "car.h"
#include "hero.h"
#include "creature.h"
#include "global.h"
#include "perf.h"
GridCell::GridCell()
{
entitys_.reserve(MAX_ROOM_IDX);
creatures_.reserve(MAX_ROOM_IDX);
obstacles_.reserve(MAX_ROOM_IDX);
for (int i = 0; i < MAX_ROOM_IDX; ++i) {
entitys_.push_back(std::set<Entity*>());
creatures_.push_back(std::set<Creature*>());
obstacles_.push_back(std::set<Obstacle*>());
}
}
void GridCell::ClearRoomData(Room* room)
{
entitys_[room->GetRoomIdx()].clear();
creatures_[room->GetRoomIdx()].clear();
obstacles_[room->GetRoomIdx()].clear();
}
void GridCell::TraverseHumanList(std::function<void (Human*, bool&)> func,
int room_idx,
bool& stop)
{
++Perf::Instance()->traversing_cell_creature_count;
for (Creature* c : creatures_[room_idx]) {
if (!c->IsHuman()) {
continue;
}
if (c->IsOb()) {
continue;
}
Human* hum = (Human*)c;
func(hum, stop);
if (stop) {
--Perf::Instance()->traversing_cell_creature_count;
return;
}
}
--Perf::Instance()->traversing_cell_creature_count;
}
void GridCell::TraverseCreatures(std::function<void (Creature*, bool&)>& func,
int room_idx,
bool& stop)
{
++Perf::Instance()->traversing_cell_creature_count;
for (Creature* c : creatures_[room_idx]) {
if (c->IsOb()) {
continue;
}
func(c, stop);
if (stop) {
--Perf::Instance()->traversing_cell_creature_count;
return;
}
}
--Perf::Instance()->traversing_cell_creature_count;
}
void GridCell::TraverseObstacles(std::function<void (Obstacle*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Obstacle* ob : obstacles_[room_idx]) {
func(ob, stop);
if (stop) {
return;
}
}
}
void GridCell::AddRoomEntity(Room* room, Entity* entity)
{
entitys_[room->GetRoomIdx()].insert(entity);
if (entity->IsEntityType(ET_Obstacle)) {
obstacles_[room->GetRoomIdx()].insert((Obstacle*)entity);
}
}
void GridCell::RemoveRoomEntity(Room* room, Entity* entity)
{
entitys_[room->GetRoomIdx()].erase(entity);
if (entity->IsEntityType(ET_Obstacle)) {
obstacles_[room->GetRoomIdx()].erase((Obstacle*)entity);
}
}
bool GridCell::EntityExists(Room* room, Entity* entity)
{
if (entitys_[0].find(entity) != entitys_[0].end()) {
return true;
}
return entitys_[room->GetRoomIdx()].find(entity) !=
entitys_[room->GetRoomIdx()].end();
}
void GridCell::TraverseLayer0EntityList(std::function<void (Entity*, bool&)>& func,
bool& stop)
{
for (Entity* entity : entitys_[0]) {
func(entity, stop);
if (stop) {
return;
}
}
}
void GridCell::TraverseLayer1EntityList(std::function<void (Entity*, bool&)>& func,
int room_idx,
bool& stop)
{
for (Entity* entity : entitys_[room_idx]) {
func(entity, stop);
if (stop) {
return;
}
}
}
void GridCell::TraverseAllLayerEntityList(std::function<void (Entity*, bool&)>& func,
int room_idx,
bool& stop)
{
TraverseLayer0EntityList(func, stop);
if (!stop) {
TraverseLayer1EntityList(func, room_idx, stop);
}
}
void GridCell::AddCreature(Creature* c)
{
#ifdef MYDEBUG
if (Perf::Instance()->traversing_cell_creature_count > 0) {
A8_ABORT();
}
#endif
creatures_[c->room->GetRoomIdx()].insert(c);
}
void GridCell::RemoveCreature(Creature* c)
{
#ifdef MYDEBUG
if (Perf::Instance()->traversing_cell_creature_count > 0) {
A8_ABORT();
}
#endif
creatures_[c->room->GetRoomIdx()].erase(c);
}
bool GridCell::CreatureExists(Creature* c)
{
return creatures_[c->room->GetRoomIdx()].find(c) != creatures_[c->room->GetRoomIdx()].end();
}