#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" GridCell::GridCell() { entitys_.reserve(MAX_ROOM_IDX); creatures_.reserve(MAX_ROOM_IDX); for (int i = 0; i < MAX_ROOM_IDX; ++i) { entitys_.push_back(std::set()); creatures_.push_back(std::set()); } } void GridCell::ClearRoomData(Room* room) { entitys_[room->GetRoomIdx()].clear(); creatures_[room->GetRoomIdx()].clear(); } void GridCell::TraverseHumanList(std::function func, int room_idx, bool& stop) { ++Global::Instance()->traversing_cell_creature_count; for (Creature* c : creatures_[room_idx]) { if (!c->IsHuman()) { continue; } Human* hum = (Human*)c; func(hum, stop); if (stop) { --Global::Instance()->traversing_cell_creature_count; return; } } --Global::Instance()->traversing_cell_creature_count; } void GridCell::TraverseCreatures(std::function& func, int room_idx, bool& stop) { ++Global::Instance()->traversing_cell_creature_count; for (Creature* c : creatures_[room_idx]) { func(c, stop); if (stop) { --Global::Instance()->traversing_cell_creature_count; return; } } --Global::Instance()->traversing_cell_creature_count; } void GridCell::AddPermanentEntity(Entity* entity) { entitys_[0].insert(entity); } void GridCell::AddRoomEntity(Room* room, Entity* entity) { entitys_[room->GetRoomIdx()].insert(entity); } void GridCell::RemoveRoomEntity(Room* room, Entity* entity) { entitys_[room->GetRoomIdx()].erase(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& func, bool& stop) { for (Entity* entity : entitys_[0]) { func(entity, stop); if (stop) { return; } } } void GridCell::TraverseLayer1EntityList(std::function& func, int room_idx, bool& stop) { for (Entity* entity : entitys_[room_idx]) { func(entity, stop); if (stop) { return; } } } void GridCell::TraverseAllLayerEntityList(std::function& func, int room_idx, bool& stop) { TraverseLayer0EntityList(func, stop); if (!stop) { TraverseLayer1EntityList(func, room_idx, stop); } } void GridCell::AddCreature(Creature* c) { #ifdef DEBUG if (Global::Instance()->traversing_cell_creature_count > 0) { A8_ABORT(); } #endif creatures_[c->room->GetRoomIdx()].insert(c); } void GridCell::RemoveCreature(Creature* c) { #ifdef DEBUG if (Global::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(); }