From 8f5d31cb03b2b883627d3d4453a0600a5371cc09 Mon Sep 17 00:00:00 2001 From: Koncord Date: Tue, 2 Jan 2018 11:57:32 +0800 Subject: [PATCH] [Server] Iterate only through updated players on each frame --- apps/openmw-mp/BaseMgr.cpp | 26 +++++++++++++++++++++ apps/openmw-mp/BaseMgr.hpp | 21 +++++++++++++++++ apps/openmw-mp/Books.cpp | 20 ++++------------ apps/openmw-mp/Books.hpp | 12 +++------- apps/openmw-mp/CMakeLists.txt | 1 + apps/openmw-mp/Cells.cpp | 12 ++++++++-- apps/openmw-mp/Cells.hpp | 2 ++ apps/openmw-mp/CharClass.cpp | 27 +++++++-------------- apps/openmw-mp/CharClass.hpp | 10 ++++---- apps/openmw-mp/Dialogue.cpp | 12 ++++------ apps/openmw-mp/Dialogue.hpp | 7 +++--- apps/openmw-mp/Factions.cpp | 16 +++++-------- apps/openmw-mp/Factions.hpp | 8 +++---- apps/openmw-mp/GUI.cpp | 44 +++++++++++------------------------ apps/openmw-mp/GUI.hpp | 21 ++++++----------- apps/openmw-mp/Inventory.cpp | 28 ++++++++++++++++++++++ apps/openmw-mp/NetActor.cpp | 22 +++++++++++++++++- apps/openmw-mp/NetActor.hpp | 6 +++++ apps/openmw-mp/Networking.cpp | 8 +------ apps/openmw-mp/Player.cpp | 10 ++++++++ apps/openmw-mp/Player.hpp | 3 +++ apps/openmw-mp/Players.cpp | 17 ++++++++++++++ apps/openmw-mp/Players.hpp | 6 +++++ apps/openmw-mp/Quests.cpp | 13 ++++------- apps/openmw-mp/Quests.hpp | 7 +++--- apps/openmw-mp/Settings.cpp | 22 ++++++------------ apps/openmw-mp/Settings.hpp | 10 ++++---- apps/openmw-mp/Spells.cpp | 21 +++++------------ apps/openmw-mp/Spells.hpp | 10 +++----- apps/openmw-mp/Weather.cpp | 21 +++++++---------- apps/openmw-mp/Weather.hpp | 8 +++---- apps/openmw-mp/Window.cpp | 12 +++++----- apps/openmw-mp/Window.hpp | 9 ++++--- 33 files changed, 260 insertions(+), 212 deletions(-) create mode 100644 apps/openmw-mp/BaseMgr.cpp create mode 100644 apps/openmw-mp/BaseMgr.hpp diff --git a/apps/openmw-mp/BaseMgr.cpp b/apps/openmw-mp/BaseMgr.cpp new file mode 100644 index 000000000..484991ff4 --- /dev/null +++ b/apps/openmw-mp/BaseMgr.cpp @@ -0,0 +1,26 @@ +// +// Created by koncord on 02.01.18. +// + +#include "BaseMgr.hpp" +#include "Player.hpp" + +BaseMgr::BaseMgr(Player *player) : player(player), changed(false) +{ + +} + +void BaseMgr::update() +{ + if (!changed) + return; + changed = false; + + processUpdate(); +} + +void BaseMgr::setChanged() +{ + changed = true; + player->addToUpdateQueue(); +} diff --git a/apps/openmw-mp/BaseMgr.hpp b/apps/openmw-mp/BaseMgr.hpp new file mode 100644 index 000000000..2c830eb81 --- /dev/null +++ b/apps/openmw-mp/BaseMgr.hpp @@ -0,0 +1,21 @@ +// +// Created by koncord on 02.01.18. +// + +#pragma once + +class Player; + +class BaseMgr +{ +public: + explicit BaseMgr(Player *player); + void update(); +protected: + bool isChanged() const { return changed; }; + void setChanged(); + virtual void processUpdate() = 0; + Player *player; +private: + bool changed; +}; diff --git a/apps/openmw-mp/Books.cpp b/apps/openmw-mp/Books.cpp index 0aa5e1e9c..9f788a016 100644 --- a/apps/openmw-mp/Books.cpp +++ b/apps/openmw-mp/Books.cpp @@ -20,22 +20,16 @@ void Books::Init(LuaState &lua) ); } -Books::Books(Player *player) : player(player), changed(false) +Books::Books(Player *player) : BaseMgr(player) { - -} - -Books::~Books() -{ - } void Books::addBook(const std::string &bookId) { - if (!changed) + if (!isChanged()) reset(); player->bookChanges.books.push_back({bookId}); - changed = true; + setChanged(); } std::string Books::getBookId(unsigned i) const @@ -56,14 +50,10 @@ void Books::reset() player->bookChanges.books.clear(); } -void Books::update() +void Books::processUpdate() { - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK); packet->setPlayer(player); packet->Send(/*toOthers*/ false); -} +} \ No newline at end of file diff --git a/apps/openmw-mp/Books.hpp b/apps/openmw-mp/Books.hpp index ba351fdd6..da2e54851 100644 --- a/apps/openmw-mp/Books.hpp +++ b/apps/openmw-mp/Books.hpp @@ -5,28 +5,22 @@ #pragma once #include +#include "BaseMgr.hpp" class LuaState; class Player; -class Books +class Books final: public BaseMgr { public: static void Init(LuaState &lua); public: - explicit Books(Player *player); - ~Books(); - void addBook(const std::string &bookId); std::string getBookId(unsigned i) const; unsigned getChanges() const; void reset(); - void update(); private: - Player *player; - bool changed; + void processUpdate() final; }; - - diff --git a/apps/openmw-mp/CMakeLists.txt b/apps/openmw-mp/CMakeLists.txt index a16c9b18a..520d38c01 100644 --- a/apps/openmw-mp/CMakeLists.txt +++ b/apps/openmw-mp/CMakeLists.txt @@ -51,6 +51,7 @@ set(SERVER stacktrace.cpp Window.cpp Weather.cpp + BaseMgr.cpp Script/CommandController.cpp Script/EventController.cpp Script/LuaState.cpp Script/luaUtils.cpp ) diff --git a/apps/openmw-mp/Cells.cpp b/apps/openmw-mp/Cells.cpp index 9b295990a..fdb9a8f2f 100644 --- a/apps/openmw-mp/Cells.cpp +++ b/apps/openmw-mp/Cells.cpp @@ -5,6 +5,7 @@ #include #include "Script/LuaState.hpp" +#include "Player.hpp" #include "Networking.hpp" #include "Cells.hpp" @@ -45,13 +46,20 @@ std::string Cells::getDescription() const return netActor->getNetCreature()->cell.getDescription(); } +inline void Cells::setChanged() +{ + if (!changedCell && netActor->isPlayer()) + netActor->toPlayer()->addToUpdateQueue(); + changedCell = true; +} + void Cells::setDescription(const std::string &cellDescription) { /*LOG_MESSAGE_SIMPLE(Log::LOG_INFO, "Script is moving %s from %s to %s", netActor->getNetCreature()->npc.mName.c_str(), netActor->getNetCreature()->cell.getDescription().c_str(), cellDescription.c_str());*/ netActor->getNetCreature()->cell = Utils::getCellFromDescription(cellDescription); - changedCell = true; + setChanged(); } std::tuple Cells::getExterior() const @@ -70,7 +78,7 @@ void Cells::setExterior(int x, int y) netActor->getNetCreature()->cell.mData.mX = x; netActor->getNetCreature()->cell.mData.mY = y; - changedCell = true; + setChanged(); } bool Cells::isExterior() const diff --git a/apps/openmw-mp/Cells.hpp b/apps/openmw-mp/Cells.hpp index 97fa167fa..f7f3d9697 100644 --- a/apps/openmw-mp/Cells.hpp +++ b/apps/openmw-mp/Cells.hpp @@ -35,9 +35,11 @@ public: bool isChangedCell() const; void resetChangedCell(); + private: NetActor *netActor; bool changedCell; + void setChanged(); }; diff --git a/apps/openmw-mp/CharClass.cpp b/apps/openmw-mp/CharClass.cpp index 739468cc3..64551ee9a 100644 --- a/apps/openmw-mp/CharClass.cpp +++ b/apps/openmw-mp/CharClass.cpp @@ -35,14 +35,8 @@ void CharClass::Init(LuaState &lua) ); } -CharClass::CharClass(Player *player) : player(player), changed(false) +CharClass::CharClass(Player *player) : BaseMgr(player) { - printf("CharClass::CharClass()\n"); -} - -CharClass::~CharClass() -{ - printf("CharClass::~CharClass()\n"); } string CharClass::getDefault() const @@ -53,7 +47,7 @@ string CharClass::getDefault() const void CharClass::setDefault(const string &className) { player->charClass.mId = className; - changed = true; + setChanged(); printf("CharClass::setDefault()\n"); } @@ -65,7 +59,7 @@ bool CharClass::isCustom() const void CharClass::setName(const string &className) { player->charClass.mName = className; - changed = true; + setChanged(); } string CharClass::getName() const @@ -81,7 +75,7 @@ std::string CharClass::getDescription() const void CharClass::setDescription(const string &desc) { player->charClass.mDescription = desc; - changed = true; + setChanged(); } std::tuple CharClass::getMajorAttributes() const @@ -95,7 +89,7 @@ void CharClass::setMajorAttributes(int first, int second) auto &data = player->charClass.mData; data.mAttribute[0] = first; data.mAttribute[1] = second; - changed = true; + setChanged(); } int CharClass::getSpecialization() const @@ -107,7 +101,7 @@ void CharClass::setSpecialization(int spec) { auto &data = player->charClass.mData; data.mSpecialization = spec; - changed = true; + setChanged(); } std::tuple CharClass::getMinorSkills() const @@ -124,7 +118,7 @@ void CharClass::setMinorSkills(int first, int second, int third, int fourth, int data.mSkills[2][0] = third; data.mSkills[3][0] = fourth; data.mSkills[4][0] = fifth; - changed = true; + setChanged(); } std::tuple CharClass::getMajorSkills() const @@ -141,14 +135,11 @@ void CharClass::setMajorSkills(int first, int second, int third, int fourth, int data.mSkills[2][1] = third; data.mSkills[3][1] = fourth; data.mSkills[4][1] = fifth; - changed = true; + setChanged(); } -void CharClass::update() +void CharClass::processUpdate() { - if (!changed) - return; - changed = false; printf("CharClass::update()\n"); auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_CHARCLASS); packet->setPlayer(player); diff --git a/apps/openmw-mp/CharClass.hpp b/apps/openmw-mp/CharClass.hpp index eed7af065..7fe5b8d2a 100644 --- a/apps/openmw-mp/CharClass.hpp +++ b/apps/openmw-mp/CharClass.hpp @@ -6,19 +6,18 @@ #include #include +#include "BaseMgr.hpp" class LuaState; class Player; -class CharClass +class CharClass final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit CharClass(Player *player); - ~CharClass(); - void update(); std::string getDefault() const; void setDefault(const std::string &className); @@ -41,10 +40,9 @@ public: std::tuple getMajorSkills() const; void setMajorSkills(int fisrt, int second, int third, int fourth, int fifth); + private: - // not controlled pointer - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/Dialogue.cpp b/apps/openmw-mp/Dialogue.cpp index 064137fd0..f549e7a46 100644 --- a/apps/openmw-mp/Dialogue.cpp +++ b/apps/openmw-mp/Dialogue.cpp @@ -21,7 +21,7 @@ void Dialogue::Init(LuaState &lua) -Dialogue::Dialogue(Player *player) : player(player), changed(false) +Dialogue::Dialogue(Player *player) : BaseMgr(player) { } @@ -32,12 +32,8 @@ void Dialogue::reset() player->topicChanges.topics.clear(); } -void Dialogue::update() +void Dialogue::processUpdate() { - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC); packet->setPlayer(player); @@ -46,9 +42,9 @@ void Dialogue::update() void Dialogue::addTopic(const std::string &topicId) { - if (!changed) + if (!isChanged()) reset(); - changed = true; + setChanged(); player->topicChanges.topics.push_back({topicId}); } diff --git a/apps/openmw-mp/Dialogue.hpp b/apps/openmw-mp/Dialogue.hpp index f2f054c52..6d05f4a4c 100644 --- a/apps/openmw-mp/Dialogue.hpp +++ b/apps/openmw-mp/Dialogue.hpp @@ -5,11 +5,12 @@ #pragma once #include +#include "BaseMgr.hpp" class LuaState; class Player; -class Dialogue +class Dialogue final: public BaseMgr { public: static void Init(LuaState &lua); @@ -24,10 +25,8 @@ public: void playSpeech(const std::string &sound); void reset(); - void update(); private: - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/Factions.cpp b/apps/openmw-mp/Factions.cpp index 8c1a7339d..3761429a0 100644 --- a/apps/openmw-mp/Factions.cpp +++ b/apps/openmw-mp/Factions.cpp @@ -22,7 +22,7 @@ void Factions::Init(LuaState &lua) ); } -Factions::Factions(Player *player): player(player), changed(false) +Factions::Factions(Player *player): BaseMgr(player) { } @@ -32,12 +32,8 @@ Factions::~Factions() } -void Factions::update() +void Factions::processUpdate() { - if (!changed) - return; - changed = false; - auto packet =mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION); packet->setPlayer(player); packet->Send(/*toOthers*/ false); @@ -52,13 +48,13 @@ int Factions::getFactionChangesAction() const void Factions::setFactionChangesAction(int action) { player->factionChanges.action = action; - changed = true; + setChanged(); } void Factions::addFaction(Faction faction) { player->factionChanges.factions.push_back(faction.faction); - changed = true; + setChanged(); } @@ -70,13 +66,13 @@ Faction Factions::getFaction(int id) const void Factions::setFaction(int id, Faction faction) { player->factionChanges.factions.at(id) = faction.faction; - changed = true; + setChanged(); } void Factions::clear() { player->factionChanges.factions.clear(); - changed = true; + setChanged(); } size_t Factions::size() const diff --git a/apps/openmw-mp/Factions.hpp b/apps/openmw-mp/Factions.hpp index 2a79b4185..28c43a53c 100644 --- a/apps/openmw-mp/Factions.hpp +++ b/apps/openmw-mp/Factions.hpp @@ -6,6 +6,7 @@ #include #include +#include "BaseMgr.hpp" class LuaState; class Player; @@ -33,7 +34,7 @@ public: mwmp::Faction faction; }; -class Factions +class Factions final: public BaseMgr { public: static void Init(LuaState &lua); @@ -41,7 +42,6 @@ public: explicit Factions(Player *player); ~Factions(); - void update(); int getFactionChangesAction() const; void setFactionChangesAction(int action); @@ -53,7 +53,5 @@ public: void clear(); private: - mwmp::Faction tempFaction; - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/GUI.cpp b/apps/openmw-mp/GUI.cpp index c439fb1b6..bfb8316c4 100644 --- a/apps/openmw-mp/GUI.cpp +++ b/apps/openmw-mp/GUI.cpp @@ -23,22 +23,13 @@ void GUI::Init(LuaState &lua) Window::Init(lua); } -GUI::GUI(Player *player): player(player), changed(false) +GUI::GUI(Player *player): BaseMgr(player) { } -GUI::~GUI() +void GUI::processUpdate() { - -} - -void GUI::update() -{ - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GUI_MESSAGEBOX); packet->setPlayer(player); packet->Send(false); @@ -50,7 +41,7 @@ void GUI::messageBox(int id, const char *label) player->guiMessageBox.label = label; player->guiMessageBox.type = Player::GUIMessageBox::MessageBox; - changed = true; + setChanged(); } void GUI::customMessageBox(int id, const char *label, const char *buttons) @@ -60,7 +51,7 @@ void GUI::customMessageBox(int id, const char *label, const char *buttons) player->guiMessageBox.buttons = buttons; player->guiMessageBox.type = Player::GUIMessageBox::CustomMessageBox; - changed = true; + setChanged(); } void GUI::inputDialog(int id, const char *label) @@ -69,7 +60,7 @@ void GUI::inputDialog(int id, const char *label) player->guiMessageBox.label = label; player->guiMessageBox.type = Player::GUIMessageBox::InputDialog; - changed = true; + setChanged(); } void GUI::passwordDialog(int id, const char *label, const char *note) @@ -79,7 +70,7 @@ void GUI::passwordDialog(int id, const char *label, const char *note) player->guiMessageBox.note = note; player->guiMessageBox.type = Player::GUIMessageBox::PasswordDialog; - changed = true; + setChanged(); } void GUI::listBox(int id, const char *label, const char *items) @@ -89,7 +80,7 @@ void GUI::listBox(int id, const char *label, const char *items) player->guiMessageBox.data = items; player->guiMessageBox.type = Player::GUIMessageBox::ListBox; - changed = true; + setChanged(); } void GUI::setMapVisibility(unsigned short targetPID, unsigned short affectedPID, unsigned short state) @@ -155,32 +146,23 @@ void QuickKeys::Init(LuaState &lua) ); } -QuickKeys::QuickKeys(Player *player) : player(player), changed(false) +QuickKeys::QuickKeys(Player *player) : BaseMgr(player) { } -QuickKeys::~QuickKeys() +void QuickKeys::processUpdate() { - -} - -void QuickKeys::update() -{ - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_QUICKKEYS); packet->setPlayer(player); packet->Send(false); clear(); } -void QuickKeys::addQuickKey(QuickKey quickKey) +void QuickKeys::addQuickKey(const QuickKey &quickKey) { player->quickKeyChanges.quickKeys.push_back(quickKey.quickKey); - changed = true; + setChanged(); } @@ -192,13 +174,13 @@ QuickKey QuickKeys::getQuickKey(int id) const void QuickKeys::setQuickKey(int id, const QuickKey &quickKey) { player->quickKeyChanges.quickKeys.at(id) = quickKey.quickKey; - changed = true; + setChanged(); } void QuickKeys::clear() { player->quickKeyChanges.quickKeys.clear(); - changed = true; + setChanged(); } size_t QuickKeys::size() const diff --git a/apps/openmw-mp/GUI.hpp b/apps/openmw-mp/GUI.hpp index 87ae35b9b..36eb61709 100644 --- a/apps/openmw-mp/GUI.hpp +++ b/apps/openmw-mp/GUI.hpp @@ -5,19 +5,17 @@ #pragma once #include "Window.hpp" +#include "BaseMgr.hpp" class LuaState; class Player; -class GUI +class GUI final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit GUI(Player *player); - ~GUI(); - - void update(); void messageBox(int id, const char *label); @@ -40,8 +38,7 @@ public: unsigned int getChanges() const; private: - Player *player; - bool changed; + void processUpdate() final; std::unordered_map> windows; int lastWindowId; }; @@ -57,8 +54,8 @@ public: int getSlot() const; void setSlot(unsigned short slot); - int getType() const; - void setType(int slot); + mwmp::QuickKey::Type getType() const; + void setType(mwmp::QuickKey::Type slot); std::string getItemId() const; void setItemId(const std::string &itemId); @@ -66,15 +63,12 @@ public: mwmp::QuickKey quickKey; }; -class QuickKeys +class QuickKeys final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit QuickKeys(Player *player); - ~QuickKeys(); - - void update(); void addQuickKey(const QuickKey &quickKey); QuickKey getQuickKey(int id) const; @@ -83,6 +77,5 @@ public: void clear(); private: - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/Inventory.cpp b/apps/openmw-mp/Inventory.cpp index c69f44436..8e5e91886 100644 --- a/apps/openmw-mp/Inventory.cpp +++ b/apps/openmw-mp/Inventory.cpp @@ -12,6 +12,7 @@ #include "Inventory.hpp" #include "NetActor.hpp" +#include using namespace std; @@ -45,6 +46,26 @@ Inventory::~Inventory() void Inventory::update() { printf("Inventory::update()"); + /*if(isEquipmentChanged()) + { + if (netActor->isPlayer()) + { + auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT); + packet->setPlayer(dynamic_cast(netActor)); + packet->Send(false); + packet->Send(true); + } + else + { + auto packet = mwmp::Networking::get().getActorPacketController()->GetPacket(ID_ACTOR_EQUIPMENT); + packet->setActorList(&actorList); + packet->Send(actorList.guid); + + if (sendToAll) + serverCell->sendToLoaded(packet, &actorList); + } + resetEquipmentFlag(); + }*/ /*if (equipmentChanged) { auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT); @@ -86,6 +107,9 @@ void Inventory::equipItem(unsigned short slot, const std::string& refId, unsigne if (!Utils::vectorContains(&netActor->getNetCreature()->equipmentIndexChanges, slot)) netActor->getNetCreature()->equipmentIndexChanges.push_back(slot); + if(!equipmentChanged && netActor->isPlayer()) + netActor->toPlayer()->addToUpdateQueue(); + equipmentChanged = true; } @@ -110,6 +134,8 @@ void Inventory::addItem(const std::string &refId, unsigned int count, int charge netActor->getNetCreature()->inventoryChanges.items.push_back(item); netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::ADD; + if (inventoryChanged == 0 && netActor->isPlayer()) + netActor->toPlayer()->addToUpdateQueue(); inventoryChanged = netActor->getNetCreature()->inventoryChanges.action; } @@ -126,6 +152,8 @@ void Inventory::removeItem(const std::string &refId, unsigned short count) netActor->getNetCreature()->inventoryChanges.items.push_back(item); netActor->getNetCreature()->inventoryChanges.action = mwmp::InventoryChanges::REMOVE; + if (inventoryChanged == 0 && netActor->isPlayer()) + netActor->toPlayer()->addToUpdateQueue(); inventoryChanged = netActor->getNetCreature()->inventoryChanges.action; } diff --git a/apps/openmw-mp/NetActor.cpp b/apps/openmw-mp/NetActor.cpp index 1b73f60db..2d6c2c434 100644 --- a/apps/openmw-mp/NetActor.cpp +++ b/apps/openmw-mp/NetActor.cpp @@ -9,10 +9,11 @@ #include "Networking.hpp" #include "NetActor.hpp" +#include "Player.hpp" using namespace std; -NetActor::NetActor() : inventory(this), cellAPI(this) +NetActor::NetActor() : inventory(this), cellAPI(this), isActorPlayer(false) { } @@ -37,6 +38,9 @@ void NetActor::setPosition(float x, float y, float z) netCreature->position.pos[0] = x; netCreature->position.pos[1] = y; netCreature->position.pos[2] = z; + + if(!positionChanged && isPlayer()) + toPlayer()->addToUpdateQueue(); positionChanged = true; } @@ -49,6 +53,9 @@ void NetActor::setRotation(float x, float z) { netCreature->position.rot[0] = x; netCreature->position.rot[2] = z; + + if (!positionChanged && isPlayer()) + toPlayer()->addToUpdateQueue(); positionChanged = true; } @@ -65,6 +72,8 @@ void NetActor::setHealth(float base, float current) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 0)) netCreature->statsDynamicIndexChanges.push_back(0); + if (!statsChanged && isPlayer()) + toPlayer()->addToUpdateQueue(); statsChanged = true; } @@ -81,6 +90,8 @@ void NetActor::setMagicka(float base, float current) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 1)) netCreature->statsDynamicIndexChanges.push_back(1); + if (!statsChanged && isPlayer()) + toPlayer()->addToUpdateQueue(); statsChanged = true; } @@ -97,6 +108,8 @@ void NetActor::setFatigue(float base, float current) if (!Utils::vectorContains(&netCreature->statsDynamicIndexChanges, 2)) netCreature->statsDynamicIndexChanges.push_back(2); + if (!statsChanged && isPlayer()) + toPlayer()->addToUpdateQueue(); statsChanged = true; } @@ -109,3 +122,10 @@ Cells &NetActor::getCell() { return cellAPI; } + +Player *NetActor::toPlayer() +{ + if (isPlayer()) + return dynamic_cast(this); + return nullptr; +} diff --git a/apps/openmw-mp/NetActor.hpp b/apps/openmw-mp/NetActor.hpp index 4b3afedd3..734d384e2 100644 --- a/apps/openmw-mp/NetActor.hpp +++ b/apps/openmw-mp/NetActor.hpp @@ -20,6 +20,7 @@ class NetActor { public: NetActor(); + virtual ~NetActor() = default; void resetUpdateFlags(); @@ -63,13 +64,18 @@ public: Cells &getCell(); mwmp::BaseNetCreature *getNetCreature() { return netCreature; } + + bool isPlayer() const { return isActorPlayer; } + Player *toPlayer(); protected: bool baseInfoChanged, levelChanged, statsChanged, positionChanged, attributesChanged, skillsChanged; + mwmp::BasePlayer *basePlayer; mwmp::BaseNetCreature *netCreature; Inventory inventory; Cells cellAPI; + bool isActorPlayer; }; diff --git a/apps/openmw-mp/Networking.cpp b/apps/openmw-mp/Networking.cpp index 7b192e6ce..bb53ef3fc 100644 --- a/apps/openmw-mp/Networking.cpp +++ b/apps/openmw-mp/Networking.cpp @@ -474,13 +474,7 @@ int Networking::mainLoop() } timerCtrl.tick(); if (updated) - { - // fixme: not good to call for_each every frame. Maybe for_each_Updated will be better - Players::for_each([](shared_ptr pl) { - pl->update(); - }); - } - this_thread::sleep_for (chrono::milliseconds(1)); + Players::processUpdated(); } timerCtrl.terminate(); diff --git a/apps/openmw-mp/Player.cpp b/apps/openmw-mp/Player.cpp index 9ba8b3579..bee49eb61 100644 --- a/apps/openmw-mp/Player.cpp +++ b/apps/openmw-mp/Player.cpp @@ -123,6 +123,7 @@ Player::Player(RakNet::RakNetGUID guid) : BasePlayer(guid), NetActor(), changedM markedForDeletion = false; storedData = mwmp::Networking::get().getState().getState()->create_table(); customData = mwmp::Networking::get().getState().getState()->create_table(); + isActorPlayer = true; } Player::~Player() @@ -131,6 +132,14 @@ Player::~Player() CellController::get().deletePlayer(this); } +void Player::addToUpdateQueue() +{ + if (inUpdateQueue) + return; + inUpdateQueue = true; + Players::addToQueue(this); +} + void Player::update() { auto plPCtrl = mwmp::Networking::get().getPlayerPacketController(); @@ -236,6 +245,7 @@ void Player::update() weatherMgr.update(); resetUpdateFlags(); + inUpdateQueue = false; } unsigned short Player::getId() diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 6a8d8e835..cdb9879b8 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -191,6 +191,8 @@ public: bool isMarkedForDeleteion() const; + void addToUpdateQueue(); + private: CellController::TContainer cells; int handshakeCounter; @@ -209,6 +211,7 @@ private: sol::table storedData; sol::table customData; bool markedForDeletion; + bool inUpdateQueue; }; #endif //OPENMW_PLAYER_HPP diff --git a/apps/openmw-mp/Players.cpp b/apps/openmw-mp/Players.cpp index f00254ef1..84df34a65 100644 --- a/apps/openmw-mp/Players.cpp +++ b/apps/openmw-mp/Players.cpp @@ -7,6 +7,7 @@ using namespace std; Players::Store Players::store; +std::queue Players::updateQueue; void Players::Init(LuaState &lua) { @@ -124,3 +125,19 @@ size_t Players::size() { return store.size(); } + +void Players::processUpdated() +{ + while (!updateQueue.empty()) + { + Player *player = updateQueue.front(); + updateQueue.pop(); + player->update(); + } +} + + +void Players::addToQueue(Player *player) +{ + updateQueue.push(player); +} \ No newline at end of file diff --git a/apps/openmw-mp/Players.hpp b/apps/openmw-mp/Players.hpp index bcfe9603f..9c9dabda5 100644 --- a/apps/openmw-mp/Players.hpp +++ b/apps/openmw-mp/Players.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include "Player.hpp" class LuaState; @@ -18,6 +19,7 @@ class LuaState; class Players { + friend class Player; public: static void Init(LuaState &lua); public: @@ -47,7 +49,11 @@ public: static void for_each(std::function)> func); + static void processUpdated(); private: + static void addToQueue(Player *player); + static Store store; + static std::queue updateQueue; }; diff --git a/apps/openmw-mp/Quests.cpp b/apps/openmw-mp/Quests.cpp index ec3d8883a..f7a3e079a 100644 --- a/apps/openmw-mp/Quests.cpp +++ b/apps/openmw-mp/Quests.cpp @@ -85,17 +85,12 @@ void Quests::Init(LuaState &lua) ); } -Quests::Quests(Player *player) : player(player), changedKills(false), changedJournal(false) +Quests::Quests(Player *player) : BaseMgr(player), changedKills(false), changedJournal(false) { } -Quests::~Quests() -{ - -} - -void Quests::update() +void Quests::processUpdate() { if (changedJournal) { @@ -130,13 +125,14 @@ void Quests::addJournalItem(JournalItem item) { player->journalChanges.journalItems.push_back(item.item); changedJournal = true; - + setChanged(); } void Quests::setJournalItem(unsigned int id, JournalItem item) { player->journalChanges.journalItems.at(id) = item.item; changedJournal = true; + setChanged(); } JournalItem Quests::getJournalItem(unsigned int id) @@ -148,6 +144,7 @@ void Quests::addKill(const std::string &refId, int number) { player->killChanges.kills.push_back({refId, number}); changedKills = true; + setChanged(); } std::tuple Quests::getKill(unsigned int i) const diff --git a/apps/openmw-mp/Quests.hpp b/apps/openmw-mp/Quests.hpp index c71237664..d6eb67276 100644 --- a/apps/openmw-mp/Quests.hpp +++ b/apps/openmw-mp/Quests.hpp @@ -6,6 +6,7 @@ #include #include +#include "BaseMgr.hpp" class LuaState; class Player; @@ -34,15 +35,13 @@ public: mwmp::JournalItem item; }; -class Quests +class Quests final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit Quests(Player *player); - ~Quests(); - void update(); size_t getJournalChangesSize() const; size_t getKillChangesSize() const; @@ -55,7 +54,7 @@ public: std::tuple getKill(unsigned int i) const; private: - Player *player; + void processUpdate() final; bool changedKills, changedJournal; }; diff --git a/apps/openmw-mp/Settings.cpp b/apps/openmw-mp/Settings.cpp index 4d297ebac..0718d5d63 100644 --- a/apps/openmw-mp/Settings.cpp +++ b/apps/openmw-mp/Settings.cpp @@ -21,51 +21,43 @@ void GameSettings::Init(LuaState &lua) ); } -GameSettings::GameSettings(Player *player) : player(player), changed(false) +GameSettings::GameSettings(Player *player) : BaseMgr(player) { } -GameSettings::~GameSettings() -{ -} - void GameSettings::setConsoleAllowed(bool state) { player->consoleAllowed = state; - changed = true; + setChanged(); } void GameSettings::setDifficulty(int difficulty) { player->difficulty = difficulty; - changed = true; + setChanged(); } void GameSettings::setBedRestAllowed(bool state) { player->bedRestAllowed = state; - changed = true; + setChanged(); } void GameSettings::setWildernessRestAllowed(bool state) { player->wildernessRestAllowed = state; - changed = true; + setChanged(); } void GameSettings::setWaitAllowed(bool state) { player->waitAllowed = state; - changed = true; + setChanged(); } -void GameSettings::update() +void GameSettings::processUpdate() { - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_SETTINGS); packet->setPlayer(player); packet->Send(false); diff --git a/apps/openmw-mp/Settings.hpp b/apps/openmw-mp/Settings.hpp index 85b0d67e5..99dd2e9fd 100644 --- a/apps/openmw-mp/Settings.hpp +++ b/apps/openmw-mp/Settings.hpp @@ -4,18 +4,19 @@ #pragma once +#include "BaseMgr.hpp" + class LuaState; class Player; -class GameSettings +class GameSettings final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit GameSettings(Player *player); - ~GameSettings(); void setConsoleAllowed(bool state); @@ -27,8 +28,7 @@ public: void setWaitAllowed(bool state); - void update(); + private: - Player *player; - bool changed; + void processUpdate() final; }; \ No newline at end of file diff --git a/apps/openmw-mp/Spells.cpp b/apps/openmw-mp/Spells.cpp index f8c663956..9ef963f29 100644 --- a/apps/openmw-mp/Spells.cpp +++ b/apps/openmw-mp/Spells.cpp @@ -217,22 +217,13 @@ void Spells::Init(LuaState &lua) ); } -Spells::Spells(Player *player) : player(player), changed(false) +Spells::Spells(Player *player) : BaseMgr(player) { } -Spells::~Spells() +void Spells::processUpdate() { - -} - -void Spells::update() -{ - if (!changed) - return; - changed = false; - auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK); packet->setPlayer(player); packet->Send(false); @@ -240,18 +231,18 @@ void Spells::update() void Spells::addCustomSpell(Spell spell) { - if (!changed) + if (!isChanged()) clear(); - changed = true; + setChanged(); player->spellbookChanges.spells.push_back(spell.spell); } void Spells::addDefaultSpell(const std::string &spell) { - if (!changed) + if (!isChanged()) clear(); - changed = true; + setChanged(); ESM::Spell s; s.mId = spell; diff --git a/apps/openmw-mp/Spells.hpp b/apps/openmw-mp/Spells.hpp index c82d1541e..316127b22 100644 --- a/apps/openmw-mp/Spells.hpp +++ b/apps/openmw-mp/Spells.hpp @@ -5,6 +5,7 @@ #pragma once #include +#include "BaseMgr.hpp" class LuaState; class Player; @@ -72,15 +73,12 @@ public: ESM::Spell spell; }; -class Spells +class Spells final: public BaseMgr { public: static void Init(LuaState &lua); public: explicit Spells(Player *player); - ~Spells(); - - void update(); void addCustomSpell(Spell spell); Spell getCustomSpell(unsigned int i); @@ -90,9 +88,7 @@ public: private: void clear(); - - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/Weather.cpp b/apps/openmw-mp/Weather.cpp index df9832f51..728ec5d54 100644 --- a/apps/openmw-mp/Weather.cpp +++ b/apps/openmw-mp/Weather.cpp @@ -25,25 +25,22 @@ void WeatherMgr::Init(LuaState &lua) ); } -WeatherMgr::WeatherMgr(Player *player) : player(player), changed(false) +WeatherMgr::WeatherMgr(Player *player) : BaseMgr(player) { } void WeatherMgr::setWeather(int weather) { - changed = true; + setChanged(); player->weather.nextWeather = weather; player->weather.transitionFactor = 0.0f; player->weather.updateTime = 0.0f; } -void WeatherMgr::update() +void WeatherMgr::processUpdate() { - if (!changed) - return; - changed = false; auto packet = mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_GAME_WEATHER); @@ -55,7 +52,7 @@ void WeatherMgr::update() void WeatherMgr::setCurrent(int weather) { player->weather.currentWeather = weather; - changed = true; + setChanged(); } int WeatherMgr::getCurrent() const @@ -66,7 +63,7 @@ int WeatherMgr::getCurrent() const void WeatherMgr::setNext(int weather) { player->weather.nextWeather = weather; - changed = true; + setChanged(); } int WeatherMgr::getNext() const @@ -77,7 +74,7 @@ int WeatherMgr::getNext() const void WeatherMgr::setTransition(float time) { player->weather.transitionFactor = time; - changed = true; + setChanged(); } float WeatherMgr::getTransition() const @@ -88,7 +85,7 @@ float WeatherMgr::getTransition() const void WeatherMgr::setUpdate(float time) { player->weather.updateTime = time; - changed = true; + setChanged(); } float WeatherMgr::getUpdate() const @@ -104,8 +101,8 @@ void WeatherMgr::requestWeather() void WeatherMgr::copy(const WeatherMgr &other) { - if(other.player == player) + if (other.player == player) return; player->weather = other.player->weather; - changed = true; + setChanged(); } diff --git a/apps/openmw-mp/Weather.hpp b/apps/openmw-mp/Weather.hpp index 57434dcd1..7f7133a27 100644 --- a/apps/openmw-mp/Weather.hpp +++ b/apps/openmw-mp/Weather.hpp @@ -6,10 +6,11 @@ #include #include +#include "BaseMgr.hpp" class Player; -class WeatherMgr +class WeatherMgr final: public BaseMgr { public: static void Init(LuaState &lua); @@ -17,8 +18,6 @@ public: explicit WeatherMgr(Player *player); ~WeatherMgr() = default; - void update(); - void setWeather(int weather); void setCurrent(int weather); @@ -38,8 +37,7 @@ public: void copy(const WeatherMgr &other); private: - Player *player; - bool changed; + void processUpdate() final; }; diff --git a/apps/openmw-mp/Window.cpp b/apps/openmw-mp/Window.cpp index 1048d306c..506b85863 100644 --- a/apps/openmw-mp/Window.cpp +++ b/apps/openmw-mp/Window.cpp @@ -26,16 +26,11 @@ void Window::Init(LuaState &lua) ); } -Window::Window(Player *player, int id) : player(player) +Window::Window(Player *player, int id) : BaseMgr(player) { guiWindow.id = id; } -Window::~Window() -{ - -} - void Window::show() { printf("Trying to show window: %d\n", guiWindow.id); @@ -213,3 +208,8 @@ void Window::addSlider(short x, short y, short w, short h, const std::string &id widget.disabled = false; guiWindow.widgets.push_back(widget); } + +void Window::processUpdate() +{ + // Currently not used +} diff --git a/apps/openmw-mp/Window.hpp b/apps/openmw-mp/Window.hpp index c655088c1..5e2c86f70 100644 --- a/apps/openmw-mp/Window.hpp +++ b/apps/openmw-mp/Window.hpp @@ -6,16 +6,16 @@ #include #include +#include "BaseMgr.hpp" class Player; -class Window +class Window final: public BaseMgr { public: static void Init(LuaState &lua); public: Window(Player *player, int id); - ~Window(); void setSize(short x, short y); @@ -34,10 +34,9 @@ public: void addPassiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional active); void addActiveListBox(short x, short y, short w, short h,const std::string &id, sol::table data, sol::optional active); void addSlider(short x, short y, short w, short h,const std::string &id, sol::optional active); -private: - Player *player; - bool changed; +private: + void processUpdate() final; sol::function callback; mwmp::BasePlayer::GUIWindow guiWindow; };