From 234510a87f52972f4e8eb4e6a68df82b6cd85510 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sat, 29 Apr 2017 23:05:12 +0300 Subject: [PATCH] [Server] Split off Cell and CellController into two different files --- apps/openmw-mp/CMakeLists.txt | 1 + apps/openmw-mp/Cell.cpp | 195 +++--------------------------- apps/openmw-mp/Cell.hpp | 39 +----- apps/openmw-mp/CellController.cpp | 170 ++++++++++++++++++++++++++ apps/openmw-mp/CellController.hpp | 47 +++++++ apps/openmw-mp/Networking.cpp | 1 + apps/openmw-mp/Player.hpp | 1 + 7 files changed, 238 insertions(+), 216 deletions(-) create mode 100644 apps/openmw-mp/CellController.cpp create mode 100644 apps/openmw-mp/CellController.hpp diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index 174b85137..e7de55dcc 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -70,6 +70,7 @@ set(SERVER Networking.cpp MasterClient.cpp Cell.cpp + CellController.cpp Utils.cpp Script/Script.cpp Script/ScriptFunction.cpp Script/ScriptFunctions.cpp diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 1adab8d22..7e5698841 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -9,6 +9,21 @@ using namespace std; +Cell::Cell(ESM::Cell cell) : cell(cell) +{ + +} + +Cell::Iterator Cell::begin() const +{ + return players.begin(); +} + +Cell::Iterator Cell::end() const +{ + return players.end(); +} + void Cell::addPlayer(Player *player) { auto it = find(player->cells.begin(), player->cells.end(), this); @@ -103,183 +118,3 @@ std::string Cell::getDescription() const { return cell.getDescription(); } - -CellController::CellController() -{ - -} - -CellController::~CellController() -{ - for (auto cell : cells) - delete cell; -} - -CellController *CellController::sThis = nullptr; - -void CellController::create() -{ - assert(!sThis); - sThis = new CellController; -} - -void CellController::destroy() -{ - assert(sThis); - delete sThis; - sThis = nullptr; -} - -CellController *CellController::get() -{ - assert(sThis); - return sThis; -} - -Cell *CellController::getCell(ESM::Cell *esmCell) -{ - if (esmCell->isExterior()) - return getCellByXY(esmCell->mData.mX, esmCell->mData.mY); - else - return getCellByName(esmCell->mName); -} - - -Cell *CellController::getCellByXY(int x, int y) -{ - auto it = find_if(cells.begin(), cells.end(), [x, y](const Cell *c) - { - return c->cell.mData.mX == x && c->cell.mData.mY == y; - }); - - if (it == cells.end()) - { - LOG_APPEND(Log::LOG_INFO, "- Attempt to get Cell at %i, %i failed!", x, y); - return nullptr; - } - - return *it; -} - -Cell *CellController::getCellByName(std::string cellName) -{ - auto it = find_if(cells.begin(), cells.end(), [cellName](const Cell *c) - { - return c->cell.mName == cellName; - }); - - if (it == cells.end()) - { - LOG_APPEND(Log::LOG_INFO, "- Attempt to get Cell at %s failed!", cellName.c_str()); - return nullptr; - } - - return *it; -} - -Cell *CellController::addCell(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) { - // Currently we cannot compare because plugin lists can be loaded in different order - //return c->cell.sRecordId == cellData.sRecordId; - return c->cell.isExterior() ? (c->cell.mData.mX == cellData.mData.mX && c->cell.mData.mY == cellData.mData.mY) : - (c->cell.mName == cellData.mName); - }); - - Cell *cell; - if (it == cells.end()) - { - LOG_APPEND(Log::LOG_INFO, "- Adding %s to CellController", cellData.getDescription().c_str()); - - cell = new Cell(cellData); - cells.push_back(cell); - } - else - { - LOG_APPEND(Log::LOG_INFO, "- Found %s in CellController", cellData.getDescription().c_str()); - cell = *it; - } - - return cell; - - -} - -void CellController::removeCell(Cell *cell) -{ - if (cell == nullptr) - return; - - for (auto it = cells.begin(); it != cells.end();) - { - if (*it != nullptr && *it == cell) - { - LOG_APPEND(Log::LOG_INFO, "- Removing %s from CellController", cell->getDescription().c_str()); - - delete *it; - it = cells.erase(it); - } - else - ++it; - } -} - -void CellController::removePlayer(Cell *cell, Player *player) -{ - cell->removePlayer(player); - - if (cell->players.empty()) - { - LOG_APPEND(Log::LOG_INFO, "- Deleting empty cell from memory: %s", cell->getDescription().c_str()); - auto it = find(cells.begin(), cells.end(), cell); - delete *it; - cells.erase(it); - } -} - -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) - removePlayer(*it, player); -} - -void CellController::update(Player *player) -{ - for (auto cell : player->cellStateChanges.cellStates) - { - if (cell.type == mwmp::CellState::LOAD) - { - Cell *c = addCell(cell.cell); - c->addPlayer(player); - } - else - { - Cell *c; - if (!cell.cell.isExterior()) - c = getCellByName(cell.cell.mName); - else - c = getCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); - - if (c != nullptr) - removePlayer(c, player); - } - } -} - -Cell::Cell(ESM::Cell cell): cell(cell) -{ - -} - -Cell::Iterator Cell::begin() const -{ - return players.begin(); -} - -Cell::Iterator Cell::end() const -{ - return players.end(); -} diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index a8e1f1ef7..648142907 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -2,8 +2,8 @@ // Created by koncord on 18.02.17. // -#ifndef OPENMW_CELL_HPP -#define OPENMW_CELL_HPP +#ifndef OPENMW_SERVERCELL_HPP +#define OPENMW_SERVERCELL_HPP #include #include @@ -15,39 +15,6 @@ class Player; class Cell; - -class CellController -{ -private: - CellController(); - ~CellController(); - - CellController(CellController&); // not used -public: - static void create(); - static void destroy(); - static CellController *get(); -public: - typedef std::deque TContainer; - typedef TContainer::iterator TIter; - - Cell * addCell(ESM::Cell cell); - void removeCell(Cell *); - - void removePlayer(Cell *cell, Player *player); - void deletePlayer(Player *player); - - Cell *getCell(ESM::Cell *esmCell); - Cell *getCellByXY(int x, int y); - Cell *getCellByName(std::string cellName); - - void update(Player *player); - -private: - static CellController *sThis; - TContainer cells; -}; - class Cell { friend class CellController; @@ -75,4 +42,4 @@ private: }; -#endif //OPENMW_CELL_HPP +#endif //OPENMW_SERVERCELL_HPP diff --git a/apps/openmw-mp/CellController.cpp b/apps/openmw-mp/CellController.cpp new file mode 100644 index 000000000..8df799157 --- /dev/null +++ b/apps/openmw-mp/CellController.cpp @@ -0,0 +1,170 @@ +#include "CellController.hpp" + +#include +#include "Cell.hpp" +#include "Player.hpp" + +using namespace std; + +CellController::CellController() +{ + +} + +CellController::~CellController() +{ + for (auto cell : cells) + delete cell; +} + +CellController *CellController::sThis = nullptr; + +void CellController::create() +{ + assert(!sThis); + sThis = new CellController; +} + +void CellController::destroy() +{ + assert(sThis); + delete sThis; + sThis = nullptr; +} + +CellController *CellController::get() +{ + assert(sThis); + return sThis; +} + +Cell *CellController::getCell(ESM::Cell *esmCell) +{ + if (esmCell->isExterior()) + return getCellByXY(esmCell->mData.mX, esmCell->mData.mY); + else + return getCellByName(esmCell->mName); +} + + +Cell *CellController::getCellByXY(int x, int y) +{ + auto it = find_if(cells.begin(), cells.end(), [x, y](const Cell *c) + { + return c->cell.mData.mX == x && c->cell.mData.mY == y; + }); + + if (it == cells.end()) + { + LOG_APPEND(Log::LOG_INFO, "- Attempt to get Cell at %i, %i failed!", x, y); + return nullptr; + } + + return *it; +} + +Cell *CellController::getCellByName(std::string cellName) +{ + auto it = find_if(cells.begin(), cells.end(), [cellName](const Cell *c) + { + return c->cell.mName == cellName; + }); + + if (it == cells.end()) + { + LOG_APPEND(Log::LOG_INFO, "- Attempt to get Cell at %s failed!", cellName.c_str()); + return nullptr; + } + + return *it; +} + +Cell *CellController::addCell(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) { + // Currently we cannot compare because plugin lists can be loaded in different order + //return c->cell.sRecordId == cellData.sRecordId; + return c->cell.isExterior() ? (c->cell.mData.mX == cellData.mData.mX && c->cell.mData.mY == cellData.mData.mY) : + (c->cell.mName == cellData.mName); + }); + + Cell *cell; + if (it == cells.end()) + { + LOG_APPEND(Log::LOG_INFO, "- Adding %s to CellController", cellData.getDescription().c_str()); + + cell = new Cell(cellData); + cells.push_back(cell); + } + else + { + LOG_APPEND(Log::LOG_INFO, "- Found %s in CellController", cellData.getDescription().c_str()); + cell = *it; + } + + return cell; +} + +void CellController::removeCell(Cell *cell) +{ + if (cell == nullptr) + return; + + for (auto it = cells.begin(); it != cells.end();) + { + if (*it != nullptr && *it == cell) + { + LOG_APPEND(Log::LOG_INFO, "- Removing %s from CellController", cell->getDescription().c_str()); + + delete *it; + it = cells.erase(it); + } + else + ++it; + } +} + +void CellController::removePlayer(Cell *cell, Player *player) +{ + cell->removePlayer(player); + + if (cell->players.empty()) + { + LOG_APPEND(Log::LOG_INFO, "- Deleting empty cell from memory: %s", cell->getDescription().c_str()); + auto it = find(cells.begin(), cells.end(), cell); + delete *it; + cells.erase(it); + } +} + +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) + removePlayer(*it, player); +} + +void CellController::update(Player *player) +{ + for (auto cell : player->cellStateChanges.cellStates) + { + if (cell.type == mwmp::CellState::LOAD) + { + Cell *c = addCell(cell.cell); + c->addPlayer(player); + } + else + { + Cell *c; + if (!cell.cell.isExterior()) + c = getCellByName(cell.cell.mName); + else + c = getCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); + + if (c != nullptr) + removePlayer(c, player); + } + } +} diff --git a/apps/openmw-mp/CellController.hpp b/apps/openmw-mp/CellController.hpp new file mode 100644 index 000000000..b71064156 --- /dev/null +++ b/apps/openmw-mp/CellController.hpp @@ -0,0 +1,47 @@ +#ifndef OPENMW_SERVERCELLCONTROLLER_HPP +#define OPENMW_SERVERCELLCONTROLLER_HPP + +#include +#include +#include +#include +#include +#include + +class Player; +class Cell; + + +class CellController +{ +private: + CellController(); + ~CellController(); + + CellController(CellController&); // not used +public: + static void create(); + static void destroy(); + static CellController *get(); +public: + typedef std::deque TContainer; + typedef TContainer::iterator TIter; + + Cell * addCell(ESM::Cell cell); + void removeCell(Cell *); + + void removePlayer(Cell *cell, Player *player); + void deletePlayer(Player *player); + + Cell *getCell(ESM::Cell *esmCell); + Cell *getCellByXY(int x, int y); + Cell *getCellByName(std::string cellName); + + void update(Player *player); + +private: + static CellController *sThis; + TContainer cells; +}; + +#endif //OPENMW_SERVERCELLCONTROLLER_HPP diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 5db9508e0..4fc971396 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -17,6 +17,7 @@ #include "Networking.hpp" #include "MasterClient.hpp" #include "Cell.hpp" +#include "CellController.hpp" #include "PlayerProcessor.hpp" #include "ActorProcessor.hpp" #include "WorldProcessor.hpp" diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 208abc99b..8d6d219b5 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -19,6 +19,7 @@ #include #include #include "Cell.hpp" +#include "CellController.hpp" struct Player; typedef std::map TPlayers;