From 585c24cee879ab1a2f437601c9f4ceed6119874c Mon Sep 17 00:00:00 2001 From: Koncord Date: Sat, 9 Dec 2017 11:41:20 +0800 Subject: [PATCH] [Server] Optimize CellController --- apps/openmw-mp/Actors.cpp | 6 +++--- apps/openmw-mp/Cell.cpp | 22 +++++++++------------- apps/openmw-mp/Cell.hpp | 2 +- apps/openmw-mp/CellController.cpp | 14 +++++++------- apps/openmw-mp/CellController.hpp | 6 +++--- apps/openmw-mp/Player.cpp | 6 +++--- 6 files changed, 26 insertions(+), 30 deletions(-) diff --git a/apps/openmw-mp/Actors.cpp b/apps/openmw-mp/Actors.cpp index 911633405..b98930dcc 100644 --- a/apps/openmw-mp/Actors.cpp +++ b/apps/openmw-mp/Actors.cpp @@ -175,7 +175,7 @@ void ActorController::sendActors(std::shared_ptr player, std::vector player, std::vectorsetActorList(&actorList); packet->Send(actorList.guid); if (sendToAll) - CellController::get().getCell(&actorList.cell)->sendToLoaded(packet, &actorList); + CellController::get().getCell(actorList.cell)->sendToLoaded(packet, &actorList); } void ActorController::requestList(std::shared_ptr player, const ESM::Cell &cell) @@ -266,7 +266,7 @@ void ActorController::requestList(std::shared_ptr player, const ESM::Cel std::vector> ActorController::getActors(std::shared_ptr player, const ESM::Cell &cell) { - Cell *serverCell = CellController::get().getCell(&player->cell); + Cell *serverCell = CellController::get().getCell(player->cell); std::vector> actorList; diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index f3292f09f..9781369fe 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -57,7 +57,7 @@ void Cell::addPlayer(Player *player) void Cell::removePlayer(Player *player) { - for (Iterator it = begin(); it != end(); it++) + for (auto it = begin(); it != end(); it++) { if (*it == player) { @@ -82,14 +82,12 @@ void Cell::removePlayer(Player *player) void Cell::readActorList(unsigned char packetID, const mwmp::BaseActorList *newActorList) { - for (unsigned int i = 0; i < newActorList->count; i++) + for (auto &newActor : newActorList->baseActors) { - auto &newActor = newActorList->baseActors.at(i); - mwmp::BaseActor *cellActor; if (containsActor(newActor->refNumIndex, newActor->mpNum)) { - cellActor = getActor(newActor->refNumIndex, newActor->mpNum); + mwmp::BaseActor *cellActor = getActor(newActor->refNumIndex, newActor->mpNum); switch (packetID) { @@ -135,7 +133,7 @@ mwmp::BaseActor *Cell::getActor(int refNumIndex, int mpNum) return nullptr; } -void Cell::removeActors(const mwmp::BaseActorList *newActorList) +void Cell::removeActors(const mwmp::BaseActorList &newActorList) { for (auto it = cellActorList.baseActors.begin(); it != cellActorList.baseActors.end();) { @@ -144,10 +142,8 @@ void Cell::removeActors(const mwmp::BaseActorList *newActorList) bool foundActor = false; - for (unsigned int i = 0; i < newActorList->count; i++) + for (const auto &newActor : newActorList.baseActors) { - auto &newActor = newActorList->baseActors.at(i); - if (newActor->refNumIndex == refNumIndex && newActor->mpNum == mpNum) { it = cellActorList.baseActors.erase(it); @@ -190,7 +186,7 @@ void Cell::sendToLoaded(mwmp::ActorPacket *actorPacket, mwmp::BaseActorList *bas std::list plList; - for (auto pl : players) + for (auto &pl : players) { if (pl != nullptr && !pl->npc.mName.empty()) plList.push_back(pl); @@ -199,7 +195,7 @@ void Cell::sendToLoaded(mwmp::ActorPacket *actorPacket, mwmp::BaseActorList *bas plList.sort(); plList.unique(); - for (auto pl : plList) + for (auto &pl : plList) { if (pl->guid == baseActorList->guid) continue; @@ -217,7 +213,7 @@ void Cell::sendToLoaded(mwmp::WorldPacket *worldPacket, mwmp::BaseEvent *baseEve std::list plList; - for (auto pl : players) + for (auto &pl : players) { if (pl != nullptr && !pl->npc.mName.empty()) plList.push_back(pl); @@ -226,7 +222,7 @@ void Cell::sendToLoaded(mwmp::WorldPacket *worldPacket, mwmp::BaseEvent *baseEve plList.sort(); plList.unique(); - for (auto pl : plList) + for (auto &pl : plList) { if (pl->guid == baseEvent->guid) continue; diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 4ad8eb428..f3554b40b 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -33,7 +33,7 @@ public: void readActorList(unsigned char packetID, const mwmp::BaseActorList *newActorList); bool containsActor(int refNumIndex, int mpNum); mwmp::BaseActor *getActor(int refNumIndex, int mpNum); - void removeActors(const mwmp::BaseActorList *newActorList); + void removeActors(const mwmp::BaseActorList &newActorList); RakNet::RakNetGUID *getAuthority(); void setAuthority(const RakNet::RakNetGUID& guid); diff --git a/apps/openmw-mp/CellController.cpp b/apps/openmw-mp/CellController.cpp index 1062e1e8e..d62b07e1c 100644 --- a/apps/openmw-mp/CellController.cpp +++ b/apps/openmw-mp/CellController.cpp @@ -21,12 +21,12 @@ CellController &CellController::get() return cellCtrl; } -Cell *CellController::getCell(ESM::Cell *esmCell) +Cell *CellController::getCell(const ESM::Cell &esmCell) { - if (esmCell->isExterior()) - return getCellByXY(esmCell->mData.mX, esmCell->mData.mY); + if (esmCell.isExterior()) + return getCellByXY(esmCell.mData.mX, esmCell.mData.mY); else - return getCellByName(esmCell->mName); + return getCellByName(esmCell.mName); } @@ -46,7 +46,7 @@ Cell *CellController::getCellByXY(int x, int y) return *it; } -Cell *CellController::getCellByName(std::string cellName) +Cell *CellController::getCellByName(const std::string &cellName) { auto it = find_if(cells.begin(), cells.end(), [cellName](const Cell *c) { @@ -62,7 +62,7 @@ Cell *CellController::getCellByName(std::string cellName) return *it; } -Cell *CellController::addCell(ESM::Cell cellData) +Cell *CellController::addCell(const ESM::Cell &cellData) { LOG_APPEND(Log::LOG_INFO, "- Loaded cells: %d", cells.size()); auto it = find_if(cells.begin(), cells.end(), [cellData](const Cell *c) { @@ -132,7 +132,7 @@ void CellController::deletePlayer(Player *player) { LOG_APPEND(Log::LOG_INFO, "- Iterating through Cells from Player %s", player->npc.mName.c_str()); - for (auto it = player->getCells()->begin(); player->getCells()->size() != 0; ++it) + for (auto it = player->getCells()->begin(); !player->getCells()->empty(); ++it) removePlayer(*it, player); } diff --git a/apps/openmw-mp/CellController.hpp b/apps/openmw-mp/CellController.hpp index faa9af402..b7d60c7ab 100644 --- a/apps/openmw-mp/CellController.hpp +++ b/apps/openmw-mp/CellController.hpp @@ -28,15 +28,15 @@ public: typedef std::deque TContainer; typedef TContainer::iterator TIter; - Cell * addCell(ESM::Cell cell); + Cell *addCell(const ESM::Cell &cellData); void removeCell(Cell *); void removePlayer(Cell *cell, Player *player); void deletePlayer(Player *player); - Cell *getCell(ESM::Cell *esmCell); + Cell *getCell(const ESM::Cell &esmCell); Cell *getCellByXY(int x, int y); - Cell *getCellByName(std::string cellName); + Cell *getCellByName(const std::string &cellName); void update(Player *player); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 9ee2314aa..a3082c3d6 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -299,8 +299,8 @@ void Player::sendToLoaded(mwmp::PlayerPacket *myPacket) { std::list plList; - for (auto cell : cells) - for (auto pl : *cell) + for (auto &cell : cells) + for (auto &pl : *cell) plList.push_back(pl); plList.sort(); @@ -796,7 +796,7 @@ void Player::setAuthority() writeActorList.cell = cell; writeActorList.guid = guid; - Cell *serverCell = CellController::get().getCell(&cell); + Cell *serverCell = CellController::get().getCell(cell); if (serverCell != nullptr) { serverCell->setAuthority(guid);