From 8887a267c7f0f5204dc5f42cd5640146e6f5ab9b Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 13:26:42 +0800 Subject: [PATCH 01/22] [Server] Add Cell and CellController --- apps/openmw-mp/CMakeLists.txt | 1 + apps/openmw-mp/Cell.cpp | 169 ++++++++++++++++++++++++++++++++++ apps/openmw-mp/Cell.hpp | 64 +++++++++++++ apps/openmw-mp/Player.cpp | 5 + apps/openmw-mp/Player.hpp | 5 + 5 files changed, 244 insertions(+) create mode 100644 apps/openmw-mp/Cell.cpp create mode 100644 apps/openmw-mp/Cell.hpp diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index 11863815f..061b57fac 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -69,6 +69,7 @@ set(SERVER Networking.cpp Utils.cpp MasterClient.cpp + Cell.cpp Script/Script.cpp Script/ScriptFunction.cpp Script/ScriptFunctions.cpp diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp new file mode 100644 index 000000000..67d5ee922 --- /dev/null +++ b/apps/openmw-mp/Cell.cpp @@ -0,0 +1,169 @@ +// +// Created by koncord on 18.02.17. +// + +#include "Cell.hpp" + +#include +#include "Player.hpp" + +using namespace std; + +void Cell::AddPlayer(Player *player) +{ + auto it = find(player->cells.begin(), player->cells.end(), this); + if(it == player->cells.end()) + player->cells.push_back(this); + players.push_back(player); +} + +void Cell::RemovePlayer(Player *player) +{ + for(Iterator it = players.begin(); it != players.end(); it++) + { + if(*it == player) + { + auto it2 = find(player->cells.begin(), player->cells.end(), this); + if(it2 != player->cells.end()) + player->cells.erase(it2); + players.erase(it); + return; + } + + } + +} + +Cell::TPlayers Cell::getPlayers() +{ + return players; +} + +CellController::CellController() +{ + +} + +CellController::~CellController() +{ + +} + +CellController *CellController::sThis = nullptr; + +void CellController::Create() +{ + sThis = new CellController; +} + +void CellController::Destroy() +{ + assert(sThis); + delete sThis; + sThis = nullptr; +} + +CellController *CellController::Get() +{ + return sThis; +} + +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()) + return nullptr; + return *it; +} + +Cell *CellController::GetCellByID(std::string cellid) +{ + auto it = find_if(cells.begin(), cells.end(), [cellid](const Cell *c) { + return c->cell.mName == cellid; + }); + if(it == cells.end()) + return nullptr; + return *it; +} + +Cell *CellController::AddCell(ESM::Cell cellData) +{ + + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Loaded cells: %d", cells.size()); + auto it = find_if(cells.begin(), cells.end(), [cellData](const Cell *c) { + //return c->cell.sRecordId == cellData.sRecordId; // Currently we cannot compare because plugin lists can be loaded in different order + return c->cell.mData.mX == cellData.mData.mX && c->cell.mData.mY == cellData.mData.mY && + c->cell.mCellId.mWorldspace == cellData.mCellId.mWorldspace; + }); + Cell *cell; + if(it == cells.end()) + { + cell = new Cell(cellData); + cells.push_back(cell); + } + else + 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) + { + delete *it; + it = cells.erase(it); + } + else + ++it; + } +} + +void CellController::RemovePlayer(Cell *cell, Player *player) +{ + + cell->RemovePlayer(player); + + if(cell->players.empty()) + { + auto it = find(cells.begin(), cells.end(), cell); + delete *it; + cells.erase(it); + } +} + +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 + { + LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Unload cell: %d %d %s", cell.cell.mData.mX, cell.cell.mData.mY, cell.cell.mName.c_str()); + Cell *c; + if(!cell.cell.isExterior()) + c = GetCellByID(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) +{ + +} diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp new file mode 100644 index 000000000..d121f9c3a --- /dev/null +++ b/apps/openmw-mp/Cell.hpp @@ -0,0 +1,64 @@ +// +// Created by koncord on 18.02.17. +// + +#ifndef OPENMW_CELL_HPP +#define OPENMW_CELL_HPP + +#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); + + Cell *GetCellByXY(int x, int y); + Cell *GetCellByID(std::string cellid); + + void update(Player *player); + +private: + static CellController *sThis; + TContainer cells; +}; + +class Cell +{ + friend class CellController; +public: + Cell(ESM::Cell cell); + typedef std::deque TPlayers; + typedef TPlayers::iterator Iterator; + + void AddPlayer(Player *player); + void RemovePlayer(Player *player); + + TPlayers getPlayers(); +private: + TPlayers players; + ESM::Cell cell; +}; + + +#endif //OPENMW_CELL_HPP diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 666ed5282..5c2325ae0 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -135,3 +135,8 @@ std::chrono::steady_clock::time_point Player::getLastAttackerTime() { return lastAttackerTime; } + +CellController::TContainer Player::GetCells() +{ + return cells; +} diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 3fdc0b2e3..3045c4b4b 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -17,6 +17,7 @@ #include #include +#include "Cell.hpp" struct Player; typedef std::map TPlayers; @@ -38,6 +39,7 @@ private: class Player : public mwmp::BasePlayer { + friend class Cell; unsigned short id; public: @@ -67,12 +69,15 @@ public: virtual ~Player(); + CellController::TContainer GetCells(); + public: mwmp::InventoryChanges inventoryChangesBuffer; mwmp::SpellbookChanges spellbookChangesBuffer; mwmp::JournalChanges journalChangesBuffer; private: + CellController::TContainer cells; bool handshakeState; int loadState; unsigned short lastAttacker; From 08f78e21c1f5bcd3eba4f78589714954edcf2bca Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 13:27:39 +0800 Subject: [PATCH 02/22] [Server] Use CellController in Networking --- apps/openmw-mp/Networking.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 0d3948df5..9eeceaee4 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -15,6 +15,7 @@ #include "Networking.hpp" #include "MasterClient.hpp" +#include "Cell.hpp" using namespace mwmp; using namespace std; @@ -29,6 +30,8 @@ Networking::Networking(RakNet::RakPeerInterface *peer) this->peer = peer; players = Players::getPlayers(); + CellController::Create(); + playerController = new PlayerPacketController(peer); worldController = new WorldPacketController(peer); @@ -46,6 +49,8 @@ Networking::~Networking() { Script::Call(false); + CellController::Destroy(); + sThis = 0; delete playerController; LOG_QUIT(); @@ -180,6 +185,8 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); + CellController::Get()->update(player); + Script::Call(player->getId()); break; From bbc062de62a985fb5c912ee3ebe1b60b23cc706b Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 16:07:44 +0800 Subject: [PATCH 03/22] [Server] Use lowCamelCase in Cell --- apps/openmw-mp/Cell.cpp | 32 ++++++++++++++++---------------- apps/openmw-mp/Cell.hpp | 20 ++++++++++---------- apps/openmw-mp/Networking.cpp | 6 +++--- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 67d5ee922..bcba54298 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -9,7 +9,7 @@ using namespace std; -void Cell::AddPlayer(Player *player) +void Cell::addPlayer(Player *player) { auto it = find(player->cells.begin(), player->cells.end(), this); if(it == player->cells.end()) @@ -17,7 +17,7 @@ void Cell::AddPlayer(Player *player) players.push_back(player); } -void Cell::RemovePlayer(Player *player) +void Cell::removePlayer(Player *player) { for(Iterator it = players.begin(); it != players.end(); it++) { @@ -51,24 +51,24 @@ CellController::~CellController() CellController *CellController::sThis = nullptr; -void CellController::Create() +void CellController::create() { sThis = new CellController; } -void CellController::Destroy() +void CellController::destroy() { assert(sThis); delete sThis; sThis = nullptr; } -CellController *CellController::Get() +CellController *CellController::get() { return sThis; } -Cell *CellController::GetCellByXY(int x, int y) +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; @@ -78,7 +78,7 @@ Cell *CellController::GetCellByXY(int x, int y) return *it; } -Cell *CellController::GetCellByID(std::string cellid) +Cell *CellController::getCellByID(std::string cellid) { auto it = find_if(cells.begin(), cells.end(), [cellid](const Cell *c) { return c->cell.mName == cellid; @@ -88,7 +88,7 @@ Cell *CellController::GetCellByID(std::string cellid) return *it; } -Cell *CellController::AddCell(ESM::Cell cellData) +Cell *CellController::addCell(ESM::Cell cellData) { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Loaded cells: %d", cells.size()); @@ -110,7 +110,7 @@ Cell *CellController::AddCell(ESM::Cell cellData) } -void CellController::RemoveCell(Cell *cell) +void CellController::removeCell(Cell *cell) { if(cell == nullptr) return; @@ -126,10 +126,10 @@ void CellController::RemoveCell(Cell *cell) } } -void CellController::RemovePlayer(Cell *cell, Player *player) +void CellController::removePlayer(Cell *cell, Player *player) { - cell->RemovePlayer(player); + cell->removePlayer(player); if(cell->players.empty()) { @@ -145,20 +145,20 @@ void CellController::update(Player *player) { if(cell.type == mwmp::CellState::LOAD) { - Cell *c = AddCell(cell.cell); - c->AddPlayer(player); + Cell *c = addCell(cell.cell); + c->addPlayer(player); } else { LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Unload cell: %d %d %s", cell.cell.mData.mX, cell.cell.mData.mY, cell.cell.mName.c_str()); Cell *c; if(!cell.cell.isExterior()) - c = GetCellByID(cell.cell.mName); + c = getCellByID(cell.cell.mName); else - c = GetCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); + c = getCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); if(c != nullptr) - RemovePlayer(c, player); + removePlayer(c, player); } } } diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index d121f9c3a..a38d42228 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -21,20 +21,20 @@ private: CellController(CellController&); // not used public: - static void Create(); - static void Destroy(); - static CellController *Get(); + 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 *); + Cell * addCell(ESM::Cell cell); + void removeCell(Cell *); - void RemovePlayer(Cell *cell, Player *player); + void removePlayer(Cell *cell, Player *player); - Cell *GetCellByXY(int x, int y); - Cell *GetCellByID(std::string cellid); + Cell *getCellByXY(int x, int y); + Cell *getCellByID(std::string cellid); void update(Player *player); @@ -51,8 +51,8 @@ public: typedef std::deque TPlayers; typedef TPlayers::iterator Iterator; - void AddPlayer(Player *player); - void RemovePlayer(Player *player); + void addPlayer(Player *player); + void removePlayer(Player *player); TPlayers getPlayers(); private: diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 9eeceaee4..b4f8e1f7d 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -30,7 +30,7 @@ Networking::Networking(RakNet::RakPeerInterface *peer) this->peer = peer; players = Players::getPlayers(); - CellController::Create(); + CellController::create(); playerController = new PlayerPacketController(peer); worldController = new WorldPacketController(peer); @@ -49,7 +49,7 @@ Networking::~Networking() { Script::Call(false); - CellController::Destroy(); + CellController::destroy(); sThis = 0; delete playerController; @@ -185,7 +185,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); - CellController::Get()->update(player); + CellController::get()->update(player); Script::Call(player->getId()); From 846ceacc9002da0a92c4a909174046a97b7ee4a2 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 16:21:40 +0800 Subject: [PATCH 04/22] [Server] Add begin & end iterators to Cell class --- apps/openmw-mp/Cell.cpp | 12 +++++++++++- apps/openmw-mp/Cell.hpp | 3 +++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index bcba54298..642c40e35 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -19,7 +19,7 @@ void Cell::addPlayer(Player *player) void Cell::removePlayer(Player *player) { - for(Iterator it = players.begin(); it != players.end(); it++) + for(Iterator it = begin(); it != end(); it++) { if(*it == player) { @@ -167,3 +167,13 @@ Cell::Cell(ESM::Cell cell): cell(cell) { } + +Cell::Iterator Cell::begin() +{ + return players.begin(); +} + +Cell::Iterator Cell::end() +{ + return players.end(); +} diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index a38d42228..54ff89e22 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -50,6 +50,9 @@ public: Cell(ESM::Cell cell); typedef std::deque TPlayers; typedef TPlayers::iterator Iterator; + + Iterator begin(); + Iterator end(); void addPlayer(Player *player); void removePlayer(Player *player); From b7600150716133a5cfd1f06e2f32e1a542010506 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 16:38:47 +0800 Subject: [PATCH 05/22] [Server] Send some packets only to nearest players --- apps/openmw-mp/Networking.cpp | 31 +++++++++++++++++++++++-------- apps/openmw-mp/Player.cpp | 16 +++++++++++++++- apps/openmw-mp/Player.hpp | 4 +++- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index b4f8e1f7d..d559f8f82 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -150,7 +150,10 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) if (!player->creatureStats.mDead) { myPacket->Read(player); - myPacket->Send(player, true); //send to other clients + //myPacket->Send(player, true); //send to other clients + + player->sendToNearest(myPacket); + } break; @@ -197,7 +200,9 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) if (!player->creatureStats.mDead) { myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); + + player->sendToNearest(myPacket); Script::Call(player->getId()); } @@ -210,7 +215,8 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) if (!player->creatureStats.mDead) { myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); + player->sendToNearest(myPacket); Script::Call(player->getId()); } @@ -223,7 +229,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) if (!player->creatureStats.mDead) { myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); Script::Call(player->getId()); } @@ -235,7 +241,9 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) DEBUG_PRINTF("ID_PLAYER_EQUIPMENT\n"); myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); + + player->sendToNearest(myPacket); Script::Call(player->getId()); @@ -298,7 +306,8 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) } } - myPacket->Send(player, true); + //myPacket->Send(player, true); + player->sendToNearest(myPacket); playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->RequestData(player->attack.target); } break; @@ -308,7 +317,10 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) { DEBUG_PRINTF("ID_PLAYER_DYNAMICSTATS\n"); myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); + + player->sendToNearest(myPacket); + break; } @@ -359,7 +371,10 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) { DEBUG_PRINTF("ID_PLAYER_DRAWSTATE\n"); myPacket->Read(player); - myPacket->Send(player, true); + //myPacket->Send(player, true); + + player->sendToNearest(myPacket); + break; } diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 5c2325ae0..17302d90c 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -136,7 +136,21 @@ std::chrono::steady_clock::time_point Player::getLastAttackerTime() return lastAttackerTime; } -CellController::TContainer Player::GetCells() +CellController::TContainer Player::getCells() { return cells; } + +void Player::sendToNearest(mwmp::PlayerPacket *myPacket) +{ + for(auto cell : getCells()) + { + for(auto pl : *cell) + { + if(pl == this) + continue; + + myPacket->Send(this, pl->guid); + } + } +} diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 3045c4b4b..b3f9b4346 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -17,6 +17,7 @@ #include #include +#include #include "Cell.hpp" struct Player; @@ -69,7 +70,8 @@ public: virtual ~Player(); - CellController::TContainer GetCells(); + CellController::TContainer getCells(); + void sendToNearest(mwmp::PlayerPacket *myPacket); public: mwmp::InventoryChanges inventoryChangesBuffer; From 033472d939b89f1737d6327788e31174ad940218 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 17:42:39 +0800 Subject: [PATCH 06/22] [Server] Add doForNearest function --- apps/openmw-mp/Networking.cpp | 11 +++++++++++ apps/openmw-mp/Player.cpp | 14 ++++++++++++++ apps/openmw-mp/Player.hpp | 2 ++ 3 files changed, 27 insertions(+) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index d559f8f82..c00163510 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -170,6 +170,17 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) LOG_APPEND(Log::LOG_INFO, "- Moved to %s", player->cell.getDescription().c_str()); + player->doForNearest([this](Player *pl, Player *other){ + const RakNet::RakNetGUID &guid = pl->guid; + playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(other, guid); + playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(other, guid); + playerController->GetPacket(ID_PLAYER_SKILL)->Send(other, guid); + //playerController->GetPacket(ID_PLAYER_POS)->Send(pl, guid); + playerController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(other, guid); + playerController->GetPacket(ID_PLAYER_ATTACK)->Send(other, guid); + playerController->GetPacket(ID_PLAYER_DRAWSTATE)->Send(other, guid); + }); + myPacket->Send(player, true); //send to other clients Script::Call(player->getId()); } diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 17302d90c..d69c34548 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -154,3 +154,17 @@ void Player::sendToNearest(mwmp::PlayerPacket *myPacket) } } } + +void Player::doForNearest(std::function func) +{ + for(auto cell : getCells()) + { + for(auto pl : *cell) + { + if(pl == this) + continue; + + func(this, pl); + } + } +} diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index b3f9b4346..ebfb084be 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -73,6 +73,8 @@ public: CellController::TContainer getCells(); void sendToNearest(mwmp::PlayerPacket *myPacket); + void doForNearest(std::function func); + public: mwmp::InventoryChanges inventoryChangesBuffer; mwmp::SpellbookChanges spellbookChangesBuffer; From cce49e355abf67224a9b69656c55cc8706b92be3 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 18:44:52 +0800 Subject: [PATCH 07/22] [Server] Some fixes to sendToNearest & doForNearest --- apps/openmw-mp/Player.cpp | 40 +++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index d69c34548..19b2fc8a6 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -143,28 +143,36 @@ CellController::TContainer Player::getCells() void Player::sendToNearest(mwmp::PlayerPacket *myPacket) { - for(auto cell : getCells()) - { - for(auto pl : *cell) - { - if(pl == this) - continue; + std::list plList; - myPacket->Send(this, pl->guid); - } + for(auto cell : getCells()) + for (auto pl : *cell) + plList.push_back(pl); + + plList.sort(); + plList.unique(); + + for(auto pl : plList) + { + if(pl == this) continue; + myPacket->Send(this, pl->guid); } } void Player::doForNearest(std::function func) { - for(auto cell : getCells()) - { - for(auto pl : *cell) - { - if(pl == this) - continue; + std::list plList; - func(this, pl); - } + for(auto cell : getCells()) + for (auto pl : *cell) + plList.push_back(pl); + + plList.sort(); + plList.unique(); + + for(auto pl : plList) + { + if(pl == this) continue; + func(this, pl); } } From 931ecd5acb4d59d86ddfad06f3ee5e1878dec6cf Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 19:29:14 +0800 Subject: [PATCH 08/22] [Server] Imrpove debug info in CellController --- apps/openmw-mp/Cell.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 642c40e35..f5c1dc167 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -91,7 +91,7 @@ Cell *CellController::getCellByID(std::string cellid) Cell *CellController::addCell(ESM::Cell cellData) { - LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Loaded cells: %d", cells.size()); + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Loaded cells: %d", cells.size()); auto it = find_if(cells.begin(), cells.end(), [cellData](const Cell *c) { //return c->cell.sRecordId == cellData.sRecordId; // Currently we cannot compare because plugin lists can be loaded in different order return c->cell.mData.mX == cellData.mData.mX && c->cell.mData.mY == cellData.mData.mY && @@ -133,6 +133,7 @@ void CellController::removePlayer(Cell *cell, Player *player) if(cell->players.empty()) { + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Deleting empty cell from memory: %s", player->npc.mName, player->getId(), cell->cell.getDescription()); auto it = find(cells.begin(), cells.end(), cell); delete *it; cells.erase(it); @@ -150,7 +151,7 @@ void CellController::update(Player *player) } else { - LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Unload cell: %d %d %s", cell.cell.mData.mX, cell.cell.mData.mY, cell.cell.mName.c_str()); + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player %s (%d) unloaded cell: %s", player->npc.mName, player->getId(), cell.cell.getDescription()); Cell *c; if(!cell.cell.isExterior()) c = getCellByID(cell.cell.mName); From ca6883139524b0e44cb63beee90c307d41c67445 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 20:37:26 +0800 Subject: [PATCH 09/22] [Server] Rename sendToNearest to sendToLoaded --- apps/openmw-mp/Networking.cpp | 14 +++++++------- apps/openmw-mp/Player.cpp | 2 +- apps/openmw-mp/Player.hpp | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index c00163510..a71d4bde8 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -152,7 +152,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); //myPacket->Send(player, true); //send to other clients - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); } @@ -213,7 +213,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); Script::Call(player->getId()); } @@ -227,7 +227,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) { myPacket->Read(player); //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); Script::Call(player->getId()); } @@ -254,7 +254,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); Script::Call(player->getId()); @@ -318,7 +318,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) } //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->RequestData(player->attack.target); } break; @@ -330,7 +330,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); break; } @@ -384,7 +384,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) myPacket->Read(player); //myPacket->Send(player, true); - player->sendToNearest(myPacket); + player->sendToLoaded(myPacket); break; } diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 19b2fc8a6..8a63aa1ee 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -141,7 +141,7 @@ CellController::TContainer Player::getCells() return cells; } -void Player::sendToNearest(mwmp::PlayerPacket *myPacket) +void Player::sendToLoaded(mwmp::PlayerPacket *myPacket) { std::list plList; diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index ebfb084be..41df9c780 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -71,7 +71,7 @@ public: virtual ~Player(); CellController::TContainer getCells(); - void sendToNearest(mwmp::PlayerPacket *myPacket); + void sendToLoaded(mwmp::PlayerPacket *myPacket); void doForNearest(std::function func); From 465d0fe4b478b9022a2642163c4d7947e8a197a9 Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 20:38:17 +0800 Subject: [PATCH 10/22] [Server] Fix log messages in CellController --- apps/openmw-mp/Cell.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index f5c1dc167..6d27ab5f9 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -133,7 +133,7 @@ void CellController::removePlayer(Cell *cell, Player *player) if(cell->players.empty()) { - LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Deleting empty cell from memory: %s", player->npc.mName, player->getId(), cell->cell.getDescription()); + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Deleting empty cell from memory: %s", player->npc.mName, player->getId(), cell->cell.getDescription().c_str()); auto it = find(cells.begin(), cells.end(), cell); delete *it; cells.erase(it); @@ -151,7 +151,7 @@ void CellController::update(Player *player) } else { - LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player %s (%d) unloaded cell: %s", player->npc.mName, player->getId(), cell.cell.getDescription()); + LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player %s (%d) unloaded cell: %s", player->npc.mName, player->getId(), cell.cell.getDescription().c_str()); Cell *c; if(!cell.cell.isExterior()) c = getCellByID(cell.cell.mName); From 3aae782a51ed181919d5749db850cec26a454e4a Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 20:41:50 +0800 Subject: [PATCH 11/22] [Server] Send info about loader to others --- apps/openmw-mp/Networking.cpp | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index a71d4bde8..77f880ffe 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -171,14 +171,21 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) player->cell.getDescription().c_str()); player->doForNearest([this](Player *pl, Player *other){ - const RakNet::RakNetGUID &guid = pl->guid; - playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(other, guid); - playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(other, guid); - playerController->GetPacket(ID_PLAYER_SKILL)->Send(other, guid); - //playerController->GetPacket(ID_PLAYER_POS)->Send(pl, guid); - playerController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(other, guid); - playerController->GetPacket(ID_PLAYER_ATTACK)->Send(other, guid); - playerController->GetPacket(ID_PLAYER_DRAWSTATE)->Send(other, guid); + playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(other, pl->guid); + playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(other, pl->guid); + playerController->GetPacket(ID_PLAYER_SKILL)->Send(other, pl->guid); + playerController->GetPacket(ID_PLAYER_POS)->Send(pl, pl->guid); + playerController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(other, pl->guid); + playerController->GetPacket(ID_PLAYER_ATTACK)->Send(other, pl->guid); + playerController->GetPacket(ID_PLAYER_DRAWSTATE)->Send(other, pl->guid); + + playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_SKILL)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_POS)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_ATTACK)->Send(pl, other->guid); + playerController->GetPacket(ID_PLAYER_DRAWSTATE)->Send(pl, other->guid); }); myPacket->Send(player, true); //send to other clients From f2ce46ffed31014b1b014a419eb0b19f8830188c Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 21:32:25 +0800 Subject: [PATCH 12/22] [Server] Rename doForNearest to forEachLoaded --- apps/openmw-mp/Networking.cpp | 2 +- apps/openmw-mp/Player.cpp | 2 +- apps/openmw-mp/Player.hpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 77f880ffe..0d78468f3 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -170,7 +170,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) LOG_APPEND(Log::LOG_INFO, "- Moved to %s", player->cell.getDescription().c_str()); - player->doForNearest([this](Player *pl, Player *other){ + player->forEachLoaded([this](Player *pl, Player *other) { playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_SKILL)->Send(other, pl->guid); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 8a63aa1ee..33edfd4f5 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -159,7 +159,7 @@ void Player::sendToLoaded(mwmp::PlayerPacket *myPacket) } } -void Player::doForNearest(std::function func) +void Player::forEachLoaded(std::function func) { std::list plList; diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 41df9c780..f9fdbb878 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -73,7 +73,7 @@ public: CellController::TContainer getCells(); void sendToLoaded(mwmp::PlayerPacket *myPacket); - void doForNearest(std::function func); + void forEachLoaded(std::function func); public: mwmp::InventoryChanges inventoryChangesBuffer; From 67099e437a951746d6cf0492d2a018ddecd312ed Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 22:27:00 +0800 Subject: [PATCH 13/22] [Server] Remove player from Cells when disconnected --- apps/openmw-mp/Cell.cpp | 14 ++++++++++++++ apps/openmw-mp/Cell.hpp | 1 + apps/openmw-mp/Player.cpp | 2 ++ 3 files changed, 17 insertions(+) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 6d27ab5f9..7341504e7 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -140,6 +140,20 @@ void CellController::removePlayer(Cell *cell, Player *player) } } +void CellController::deletePlayer(Player *player) +{ + for_each (cells.begin(), cells.end(), [&player](Cell *cell) { + for (auto it = cell->begin(); it != cell->end(); ++it) + { + if (*it == player) + { + cell->players.erase(it); + break; + } + } + }); +} + void CellController::update(Player *player) { for(auto cell : player->cellStateChanges.cellStates) diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 54ff89e22..27d6ddba1 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -32,6 +32,7 @@ public: void removeCell(Cell *); void removePlayer(Cell *cell, Player *player); + void deletePlayer(Player *player); Cell *getCellByXY(int x, int y); Cell *getCellByID(std::string cellid); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 33edfd4f5..22a1cd994 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -15,6 +15,8 @@ void Players::deletePlayer(RakNet::RakNetGUID guid) if (players[guid] != 0) { + CellController::get()->deletePlayer(players[guid]); + LOG_APPEND(Log::LOG_INFO, "- Emptying slot %i", players[guid]->getId()); From fd36ec7613d19c78fe4c598f1667c74317b291ad Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 19 Feb 2017 22:43:57 +0800 Subject: [PATCH 14/22] [Server] Iterate cells where player was loaded instead all --- apps/openmw-mp/Cell.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 7341504e7..9b78228c7 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -142,7 +142,7 @@ void CellController::removePlayer(Cell *cell, Player *player) void CellController::deletePlayer(Player *player) { - for_each (cells.begin(), cells.end(), [&player](Cell *cell) { + for_each (player->getCells().begin(), player->getCells().end(), [&player](Cell *cell) { for (auto it = cell->begin(); it != cell->end(); ++it) { if (*it == player) From 887b436ee7f6d949e34ba94d99507c4322b0d5fb Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 16:53:15 +0200 Subject: [PATCH 15/22] [Server] Make style for new methods consistent with rest of project --- apps/openmw-mp/Cell.cpp | 28 ++++++++++++++-------------- apps/openmw-mp/MasterClient.cpp | 6 +++--- apps/openmw-mp/Networking.cpp | 2 +- apps/openmw-mp/Player.cpp | 14 +++++++------- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 9b78228c7..1726b744a 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -12,19 +12,19 @@ using namespace std; void Cell::addPlayer(Player *player) { auto it = find(player->cells.begin(), player->cells.end(), this); - if(it == player->cells.end()) + if (it == player->cells.end()) player->cells.push_back(this); players.push_back(player); } void Cell::removePlayer(Player *player) { - for(Iterator it = begin(); it != end(); it++) + for (Iterator it = begin(); it != end(); it++) { - if(*it == player) + if (*it == player) { auto it2 = find(player->cells.begin(), player->cells.end(), this); - if(it2 != player->cells.end()) + if (it2 != player->cells.end()) player->cells.erase(it2); players.erase(it); return; @@ -73,7 +73,7 @@ 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()) + if (it == cells.end()) return nullptr; return *it; } @@ -83,7 +83,7 @@ Cell *CellController::getCellByID(std::string cellid) auto it = find_if(cells.begin(), cells.end(), [cellid](const Cell *c) { return c->cell.mName == cellid; }); - if(it == cells.end()) + if (it == cells.end()) return nullptr; return *it; } @@ -98,7 +98,7 @@ Cell *CellController::addCell(ESM::Cell cellData) c->cell.mCellId.mWorldspace == cellData.mCellId.mWorldspace; }); Cell *cell; - if(it == cells.end()) + if (it == cells.end()) { cell = new Cell(cellData); cells.push_back(cell); @@ -112,11 +112,11 @@ Cell *CellController::addCell(ESM::Cell cellData) void CellController::removeCell(Cell *cell) { - if(cell == nullptr) + if (cell == nullptr) return; for (auto it = cells.begin(); it != cells.end();) { - if(*it != nullptr && *it == cell) + if (*it != nullptr && *it == cell) { delete *it; it = cells.erase(it); @@ -131,7 +131,7 @@ void CellController::removePlayer(Cell *cell, Player *player) cell->removePlayer(player); - if(cell->players.empty()) + if (cell->players.empty()) { LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Deleting empty cell from memory: %s", player->npc.mName, player->getId(), cell->cell.getDescription().c_str()); auto it = find(cells.begin(), cells.end(), cell); @@ -156,9 +156,9 @@ void CellController::deletePlayer(Player *player) void CellController::update(Player *player) { - for(auto cell : player->cellStateChanges.cellStates) + for (auto cell : player->cellStateChanges.cellStates) { - if(cell.type == mwmp::CellState::LOAD) + if (cell.type == mwmp::CellState::LOAD) { Cell *c = addCell(cell.cell); c->addPlayer(player); @@ -167,12 +167,12 @@ void CellController::update(Player *player) { LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player %s (%d) unloaded cell: %s", player->npc.mName, player->getId(), cell.cell.getDescription().c_str()); Cell *c; - if(!cell.cell.isExterior()) + if (!cell.cell.isExterior()) c = getCellByID(cell.cell.mName); else c = getCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); - if(c != nullptr) + if (c != nullptr) removePlayer(c, player); } } diff --git a/apps/openmw-mp/MasterClient.cpp b/apps/openmw-mp/MasterClient.cpp index bdd915123..dcb787e69 100644 --- a/apps/openmw-mp/MasterClient.cpp +++ b/apps/openmw-mp/MasterClient.cpp @@ -152,7 +152,7 @@ void MasterClient::Update() { LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Update rate is too low, and the master server has deleted information about" " the server. Trying low rate..."); - if((timeout - step_rate) >= step_rate) + if ((timeout - step_rate) >= step_rate) SetUpdateRate(timeout - step_rate); update = false; } @@ -177,10 +177,10 @@ void MasterClient::Start() void MasterClient::Stop() { - if(!sRun) + if (!sRun) return; sRun = false; - if(thrQuery.joinable()) + if (thrQuery.joinable()) thrQuery.join(); } diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 0d78468f3..9c9db8a14 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -857,7 +857,7 @@ int Networking::mainLoop() RakNet::BitStream bs; bs.Write((unsigned char) ID_MASTER_QUERY); bs.Write(Players::getPlayers()->size()); - for(auto player : *Players::getPlayers()) + for (auto player : *Players::getPlayers()) bs.Write(RakNet::RakString(player.second->npc.mName.c_str())); bs.Write(0); // plugins peer->Send(&bs, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->systemAddress, false); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 22a1cd994..6f9b5ee3e 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -54,7 +54,7 @@ void Players::newPlayer(RakNet::RakNetGUID guid) Player *Players::getPlayer(RakNet::RakNetGUID guid) { - if(players.count(guid) == 0) + if (players.count(guid) == 0) return nullptr; return players[guid]; } @@ -147,16 +147,16 @@ void Player::sendToLoaded(mwmp::PlayerPacket *myPacket) { std::list plList; - for(auto cell : getCells()) + for (auto cell : getCells()) for (auto pl : *cell) plList.push_back(pl); plList.sort(); plList.unique(); - for(auto pl : plList) + for (auto pl : plList) { - if(pl == this) continue; + if (pl == this) continue; myPacket->Send(this, pl->guid); } } @@ -165,16 +165,16 @@ void Player::forEachLoaded(std::function func) { std::list plList; - for(auto cell : getCells()) + for (auto cell : getCells()) for (auto pl : *cell) plList.push_back(pl); plList.sort(); plList.unique(); - for(auto pl : plList) + for (auto pl : plList) { - if(pl == this) continue; + if (pl == this) continue; func(this, pl); } } From 275dfaf05fa33674f113f0b016f4e203c7759f21 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 17:27:38 +0200 Subject: [PATCH 16/22] [Server] Add Cell::getDescription() for debugging purposes --- apps/openmw-mp/Cell.cpp | 9 +++++---- apps/openmw-mp/Cell.hpp | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index 1726b744a..c2e7d94fe 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -29,9 +29,7 @@ void Cell::removePlayer(Player *player) players.erase(it); return; } - } - } Cell::TPlayers Cell::getPlayers() @@ -39,6 +37,11 @@ Cell::TPlayers Cell::getPlayers() return players; } +std::string Cell::getDescription() const +{ + return cell.getDescription(); +} + CellController::CellController() { @@ -90,7 +93,6 @@ Cell *CellController::getCellByID(std::string cellid) Cell *CellController::addCell(ESM::Cell cellData) { - LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Loaded cells: %d", cells.size()); auto it = find_if(cells.begin(), cells.end(), [cellData](const Cell *c) { //return c->cell.sRecordId == cellData.sRecordId; // Currently we cannot compare because plugin lists can be loaded in different order @@ -128,7 +130,6 @@ void CellController::removeCell(Cell *cell) void CellController::removePlayer(Cell *cell, Player *player) { - cell->removePlayer(player); if (cell->players.empty()) diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 27d6ddba1..758a9fa33 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -59,6 +59,8 @@ public: void removePlayer(Player *player); TPlayers getPlayers(); + + std::string getDescription() const; private: TPlayers players; ESM::Cell cell; From ee1ba1728e2b887521fc677be6c3a77a7115d0e3 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 18:37:20 +0200 Subject: [PATCH 17/22] [Server] Rename getCellByID() into getCellByName() to avoid confusion --- apps/openmw-mp/Cell.cpp | 8 ++++---- apps/openmw-mp/Cell.hpp | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index c2e7d94fe..a0a2f8340 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -81,10 +81,10 @@ Cell *CellController::getCellByXY(int x, int y) return *it; } -Cell *CellController::getCellByID(std::string cellid) +Cell *CellController::getCellByName(std::string cellName) { - auto it = find_if(cells.begin(), cells.end(), [cellid](const Cell *c) { - return c->cell.mName == cellid; + auto it = find_if(cells.begin(), cells.end(), [cellName](const Cell *c) { + return c->cell.mName == cellName; }); if (it == cells.end()) return nullptr; @@ -169,7 +169,7 @@ void CellController::update(Player *player) LOG_MESSAGE_SIMPLE(Log::LOG_VERBOSE, "Player %s (%d) unloaded cell: %s", player->npc.mName, player->getId(), cell.cell.getDescription().c_str()); Cell *c; if (!cell.cell.isExterior()) - c = getCellByID(cell.cell.mName); + c = getCellByName(cell.cell.mName); else c = getCellByXY(cell.cell.getGridX(), cell.cell.getGridY()); diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 758a9fa33..175351016 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -35,7 +35,7 @@ public: void deletePlayer(Player *player); Cell *getCellByXY(int x, int y); - Cell *getCellByID(std::string cellid); + Cell *getCellByName(std::string cellName); void update(Player *player); From eb0744aa77f66c98625108e41f164965f7897c4a Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 19:39:53 +0200 Subject: [PATCH 18/22] [Server] Add CellController:getCell() for getting Cell through ESM::Cell --- apps/openmw-mp/Cell.cpp | 9 +++++++++ apps/openmw-mp/Cell.hpp | 2 ++ 2 files changed, 11 insertions(+) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index a0a2f8340..c907d90d2 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -71,6 +71,15 @@ CellController *CellController::get() 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) { diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 175351016..162becebe 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -34,6 +34,7 @@ public: 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); @@ -61,6 +62,7 @@ public: TPlayers getPlayers(); std::string getDescription() const; + private: TPlayers players; ESM::Cell cell; From 791089d34293e2a05ff3e9a3136c7ca3c4a8b83b Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 19:42:16 +0200 Subject: [PATCH 19/22] [Server] Add Cell::sendToLoaded() for sending events to loaded players --- apps/openmw-mp/Cell.cpp | 17 +++++++++++++++++ apps/openmw-mp/Cell.hpp | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index c907d90d2..c4a1c570d 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -37,6 +37,23 @@ Cell::TPlayers Cell::getPlayers() return players; } +void Cell::sendToLoaded(mwmp::WorldPacket *worldPacket, mwmp::BaseEvent *baseEvent) +{ + std::list plList; + + for (auto pl :getPlayers()) + plList.push_back(pl); + + plList.sort(); + plList.unique(); + + for (auto pl : plList) + { + if (pl->guid == baseEvent->guid) continue; + worldPacket->Send(baseEvent, pl->guid); + } +} + std::string Cell::getDescription() const { return cell.getDescription(); diff --git a/apps/openmw-mp/Cell.hpp b/apps/openmw-mp/Cell.hpp index 162becebe..938aa0a55 100644 --- a/apps/openmw-mp/Cell.hpp +++ b/apps/openmw-mp/Cell.hpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include class Player; class Cell; @@ -60,9 +62,11 @@ public: void removePlayer(Player *player); TPlayers getPlayers(); + void sendToLoaded(mwmp::WorldPacket *worldPacket, mwmp::BaseEvent *baseEvent); std::string getDescription() const; + private: TPlayers players; ESM::Cell cell; From 24251cafd05a45e7c26a7cb129a22c008b069abb Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 20:20:44 +0200 Subject: [PATCH 20/22] [Server] Send large ID_CONTAINER packets on a need-to-know basis --- apps/openmw-mp/Cell.cpp | 2 +- apps/openmw-mp/Networking.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/openmw-mp/Cell.cpp b/apps/openmw-mp/Cell.cpp index c4a1c570d..fb28d9ef4 100644 --- a/apps/openmw-mp/Cell.cpp +++ b/apps/openmw-mp/Cell.cpp @@ -50,7 +50,7 @@ void Cell::sendToLoaded(mwmp::WorldPacket *worldPacket, mwmp::BaseEvent *baseEve for (auto pl : plList) { if (pl->guid == baseEvent->guid) continue; - worldPacket->Send(baseEvent, pl->guid); + worldPacket->Send(baseEvent, pl->guid); } } diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 9c9db8a14..21fab4769 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -598,7 +598,14 @@ void Networking::processWorldPacket(RakNet::Packet *packet) player->npc.mName.c_str()); myPacket->Read(baseEvent); - myPacket->Send(baseEvent, true); + + // Until we have a timestamp-based system, send packets pertaining to more + // than one container (i.e. replies to server requests for container contents) + // only to players who have the container's cell loaded + if (baseEvent->action == BaseEvent::SET && baseEvent->objectChanges.count > 1) + CellController::get()->getCell(&baseEvent->cell)->sendToLoaded(myPacket, baseEvent); + else + myPacket->Send(baseEvent, true); Script::Call( player->getId(), From ed91f20cd31c5f784c393a120de8ed1b00ced562 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 23:11:26 +0200 Subject: [PATCH 21/22] [General] Print action used for ID_CONTAINER when receiving one --- apps/openmw-mp/Networking.cpp | 3 +++ apps/openmw/mwmp/Networking.cpp | 1 + 2 files changed, 4 insertions(+) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 21fab4769..74ad23478 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -189,6 +189,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) }); myPacket->Send(player, true); //send to other clients + Script::Call(player->getId()); } else @@ -599,6 +600,8 @@ void Networking::processWorldPacket(RakNet::Packet *packet) myPacket->Read(baseEvent); + LOG_APPEND(Log::LOG_WARN, "- action: %i", baseEvent->action); + // Until we have a timestamp-based system, send packets pertaining to more // than one container (i.e. replies to server requests for container contents) // only to players who have the container's cell loaded diff --git a/apps/openmw/mwmp/Networking.cpp b/apps/openmw/mwmp/Networking.cpp index b592f9fc3..9fdeb8565 100644 --- a/apps/openmw/mwmp/Networking.cpp +++ b/apps/openmw/mwmp/Networking.cpp @@ -747,6 +747,7 @@ void Networking::processWorldPacket(RakNet::Packet *packet) if (!ptrCellStore) return; LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "Received ID_CONTAINER"); + LOG_APPEND(Log::LOG_WARN, "- action: %i", event->action); // If we've received a request for information, comply with it if (event->action == mwmp::BaseEvent::REQUEST) From 3e031faa96b3571f02208955f89f7f41dbf6aba6 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Sun, 19 Feb 2017 23:30:43 +0200 Subject: [PATCH 22/22] [Server] Exchange position information correctly between players --- apps/openmw-mp/Networking.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 74ad23478..352756f53 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -174,7 +174,7 @@ void Networking::processPlayerPacket(RakNet::Packet *packet) playerController->GetPacket(ID_PLAYER_DYNAMICSTATS)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_ATTRIBUTE)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_SKILL)->Send(other, pl->guid); - playerController->GetPacket(ID_PLAYER_POS)->Send(pl, pl->guid); + playerController->GetPacket(ID_PLAYER_POS)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_EQUIPMENT)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_ATTACK)->Send(other, pl->guid); playerController->GetPacket(ID_PLAYER_DRAWSTATE)->Send(other, pl->guid);