From 6dde0ca9c92155f164a711318350e9a0f0f96a58 Mon Sep 17 00:00:00 2001 From: David Cernat Date: Fri, 7 Jul 2017 09:25:48 +0300 Subject: [PATCH] [Server] Rework script functons by removing unnecessary vector buffers Add new initialization script functions to allow clearing all player changes vectors in a manner consistent with Actor and World functions Add toOthers boolean to all methods that send packets with player changes vectors --- apps/openmw-mp/Player.hpp | 10 ---- apps/openmw-mp/Script/Functions/Books.cpp | 17 ++++--- apps/openmw-mp/Script/Functions/Books.hpp | 6 ++- apps/openmw-mp/Script/Functions/Cells.cpp | 17 ++++--- apps/openmw-mp/Script/Functions/Cells.hpp | 7 ++- apps/openmw-mp/Script/Functions/Dialogue.cpp | 34 +++++++++----- apps/openmw-mp/Script/Functions/Dialogue.hpp | 10 +++- apps/openmw-mp/Script/Functions/Factions.cpp | 49 ++++++++++++-------- apps/openmw-mp/Script/Functions/Factions.hpp | 26 +++++++---- apps/openmw-mp/Script/Functions/Items.cpp | 33 ++++++------- apps/openmw-mp/Script/Functions/Items.hpp | 38 ++++++++------- apps/openmw-mp/Script/Functions/Quests.cpp | 19 +++++--- apps/openmw-mp/Script/Functions/Quests.hpp | 5 +- apps/openmw-mp/Script/Functions/Spells.cpp | 33 ++++++------- apps/openmw-mp/Script/Functions/Spells.hpp | 20 ++++---- 15 files changed, 186 insertions(+), 138 deletions(-) diff --git a/apps/openmw-mp/Player.hpp b/apps/openmw-mp/Player.hpp index 2b06ef42e..8dfd23c0f 100644 --- a/apps/openmw-mp/Player.hpp +++ b/apps/openmw-mp/Player.hpp @@ -70,16 +70,6 @@ public: void forEachLoaded(std::function func); -public: - mwmp::InventoryChanges inventoryChangesBuffer; - mwmp::SpellbookChanges spellbookChangesBuffer; - mwmp::JournalChanges journalChangesBuffer; - mwmp::FactionChanges factionChangesBuffer; - mwmp::TopicChanges topicChangesBuffer; - mwmp::KillChanges killChangesBuffer; - mwmp::BookChanges bookChangesBuffer; - mwmp::MapChanges mapChangesBuffer; - private: CellController::TContainer cells; bool handshakeState; diff --git a/apps/openmw-mp/Script/Functions/Books.cpp b/apps/openmw-mp/Script/Functions/Books.cpp index 2d0f7adcc..3b6b905ae 100644 --- a/apps/openmw-mp/Script/Functions/Books.cpp +++ b/apps/openmw-mp/Script/Functions/Books.cpp @@ -5,6 +5,14 @@ using namespace mwmp; +void BookFunctions::InitializeBookChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + return player->bookChanges.books.clear(); +} + unsigned int BookFunctions::GetBookChangesSize(unsigned short pid) noexcept { Player *player; @@ -21,7 +29,7 @@ void BookFunctions::AddBook(unsigned short pid, const char* bookId) noexcept mwmp::Book book; book.bookId = bookId; - player->bookChangesBuffer.books.push_back(book); + player->bookChanges.books.push_back(book); } const char *BookFunctions::GetBookId(unsigned short pid, unsigned int i) noexcept @@ -35,14 +43,11 @@ const char *BookFunctions::GetBookId(unsigned short pid, unsigned int i) noexcep return player->bookChanges.books.at(i).bookId.c_str(); } -void BookFunctions::SendBookChanges(unsigned short pid) noexcept +void BookFunctions::SendBookChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->bookChanges, player->bookChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK)->Send(false); - player->bookChanges = std::move(player->bookChangesBuffer); - player->bookChangesBuffer.books.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_BOOK)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Books.hpp b/apps/openmw-mp/Script/Functions/Books.hpp index a12903af5..ca9831cb4 100644 --- a/apps/openmw-mp/Script/Functions/Books.hpp +++ b/apps/openmw-mp/Script/Functions/Books.hpp @@ -2,6 +2,8 @@ #define OPENMW_BOOKAPI_HPP #define BOOKAPI \ + {"InitializeBookChanges", BookFunctions::InitializeBookChanges},\ + \ {"GetBookChangesSize", BookFunctions::GetBookChangesSize},\ \ {"AddBook", BookFunctions::AddBook},\ @@ -14,13 +16,15 @@ class BookFunctions { public: + static void InitializeBookChanges(unsigned short pid) noexcept; + static unsigned int GetBookChangesSize(unsigned short pid) noexcept; static void AddBook(unsigned short pid, const char* bookId) noexcept; static const char *GetBookId(unsigned short pid, unsigned int i) noexcept; - static void SendBookChanges(unsigned short pid) noexcept; + static void SendBookChanges(unsigned short pid, bool toOthers = false) noexcept; private: }; diff --git a/apps/openmw-mp/Script/Functions/Cells.cpp b/apps/openmw-mp/Script/Functions/Cells.cpp index db2685708..b856bc96b 100644 --- a/apps/openmw-mp/Script/Functions/Cells.cpp +++ b/apps/openmw-mp/Script/Functions/Cells.cpp @@ -10,6 +10,14 @@ using namespace std; static std::string tempCellDescription; +void CellFunctions::InitializeMapChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->mapChanges.cellsExplored.clear(); +} + unsigned int CellFunctions::GetCellStateChangesSize(unsigned short pid) noexcept { Player *player; @@ -119,7 +127,7 @@ void CellFunctions::AddCellExplored(unsigned short pid, const char* cellDescript GET_PLAYER(pid, player, ); ESM::Cell cellExplored = Utils::getCellFromDescription(cellDescription); - player->mapChangesBuffer.cellsExplored.push_back(cellExplored); + player->mapChanges.cellsExplored.push_back(cellExplored); } void CellFunctions::SendCell(unsigned short pid) noexcept @@ -131,14 +139,11 @@ void CellFunctions::SendCell(unsigned short pid) noexcept mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_CELL_CHANGE)->Send(false); } -void CellFunctions::SendMapChanges(unsigned short pid) noexcept +void CellFunctions::SendMapChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->mapChanges, player->mapChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_MAP)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_MAP)->Send(false); - player->mapChanges = std::move(player->mapChangesBuffer); - player->mapChangesBuffer.cellsExplored.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_MAP)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Cells.hpp b/apps/openmw-mp/Script/Functions/Cells.hpp index e918dd262..14ea21466 100644 --- a/apps/openmw-mp/Script/Functions/Cells.hpp +++ b/apps/openmw-mp/Script/Functions/Cells.hpp @@ -4,6 +4,8 @@ #include "../Types.hpp" #define CELLAPI \ + {"InitializeMapChanges", CellFunctions::InitializeMapChanges},\ + \ {"GetCellStateChangesSize", CellFunctions::GetCellStateChangesSize},\ \ {"GetCellStateType", CellFunctions::GetCellStateType},\ @@ -28,6 +30,9 @@ class CellFunctions { public: + + static void InitializeMapChanges(unsigned short pid) noexcept; + static unsigned int GetCellStateChangesSize(unsigned short pid) noexcept; static unsigned int GetCellStateType(unsigned short pid, unsigned int i) noexcept; @@ -46,7 +51,7 @@ public: static void AddCellExplored(unsigned short pid, const char* cellDescription) noexcept; static void SendCell(unsigned short pid) noexcept; - static void SendMapChanges(unsigned short pid) noexcept; + static void SendMapChanges(unsigned short pid, bool toOthers = false) noexcept; }; #endif //OPENMW_CELLAPI_HPP diff --git a/apps/openmw-mp/Script/Functions/Dialogue.cpp b/apps/openmw-mp/Script/Functions/Dialogue.cpp index e7a5c3a96..7b808c347 100644 --- a/apps/openmw-mp/Script/Functions/Dialogue.cpp +++ b/apps/openmw-mp/Script/Functions/Dialogue.cpp @@ -5,6 +5,22 @@ using namespace mwmp; +void DialogueFunctions::InitializeTopicChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->topicChanges.topics.clear(); +} + +void DialogueFunctions::InitializeKillChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->killChanges.kills.clear(); +} + unsigned int DialogueFunctions::GetTopicChangesSize(unsigned short pid) noexcept { Player *player; @@ -29,7 +45,7 @@ void DialogueFunctions::AddTopic(unsigned short pid, const char* topicId) noexce mwmp::Topic topic; topic.topicId = topicId; - player->topicChangesBuffer.topics.push_back(topic); + player->topicChanges.topics.push_back(topic); } void DialogueFunctions::AddKill(unsigned short pid, const char* refId, int number) noexcept @@ -41,7 +57,7 @@ void DialogueFunctions::AddKill(unsigned short pid, const char* refId, int numbe kill.refId = refId; kill.number = number; - player->killChangesBuffer.kills.push_back(kill); + player->killChanges.kills.push_back(kill); } const char *DialogueFunctions::GetTopicId(unsigned short pid, unsigned int i) noexcept @@ -74,26 +90,20 @@ int DialogueFunctions::GetKillNumber(unsigned short pid, unsigned int i) noexcep return player->killChanges.kills.at(i).number; } -void DialogueFunctions::SendTopicChanges(unsigned short pid) noexcept +void DialogueFunctions::SendTopicChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->topicChanges, player->topicChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC)->Send(false); - player->topicChanges = std::move(player->topicChangesBuffer); - player->topicChangesBuffer.topics.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_TOPIC)->Send(toOthers); } -void DialogueFunctions::SendKillChanges(unsigned short pid) noexcept +void DialogueFunctions::SendKillChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->killChanges, player->killChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_KILL_COUNT)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_KILL_COUNT)->Send(false); - player->killChanges = std::move(player->killChangesBuffer); - player->killChangesBuffer.kills.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_KILL_COUNT)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Dialogue.hpp b/apps/openmw-mp/Script/Functions/Dialogue.hpp index e1773cc5a..2a93af05f 100644 --- a/apps/openmw-mp/Script/Functions/Dialogue.hpp +++ b/apps/openmw-mp/Script/Functions/Dialogue.hpp @@ -2,6 +2,9 @@ #define OPENMW_DIALOGUEAPI_HPP #define DIALOGUEAPI \ + {"InitializeTopicChanges", DialogueFunctions::InitializeTopicChanges},\ + {"InitializeKillChanges", DialogueFunctions::InitializeKillChanges},\ + \ {"GetTopicChangesSize", DialogueFunctions::GetTopicChangesSize},\ {"GetKillChangesSize", DialogueFunctions::GetKillChangesSize},\ \ @@ -19,6 +22,9 @@ class DialogueFunctions { public: + static void InitializeTopicChanges(unsigned short pid) noexcept; + static void InitializeKillChanges(unsigned short pid) noexcept; + static unsigned int GetTopicChangesSize(unsigned short pid) noexcept; static unsigned int GetKillChangesSize(unsigned short pid) noexcept; @@ -29,8 +35,8 @@ public: static const char *GetKillRefId(unsigned short pid, unsigned int i) noexcept; static int GetKillNumber(unsigned short pid, unsigned int i) noexcept; - static void SendTopicChanges(unsigned short pid) noexcept; - static void SendKillChanges(unsigned short pid) noexcept; + static void SendTopicChanges(unsigned short pid, bool toOthers = false) noexcept; + static void SendKillChanges(unsigned short pid, bool toOthers = false) noexcept; private: }; diff --git a/apps/openmw-mp/Script/Functions/Factions.cpp b/apps/openmw-mp/Script/Functions/Factions.cpp index a15b53d5c..d5dbc1ed5 100644 --- a/apps/openmw-mp/Script/Functions/Factions.cpp +++ b/apps/openmw-mp/Script/Functions/Factions.cpp @@ -6,33 +6,28 @@ using namespace mwmp; -unsigned int FactionFunctions::GetFactionChangesSize(unsigned short pid) noexcept +void FactionFunctions::InitializeFactionChanges(unsigned short pid) noexcept { Player *player; - GET_PLAYER(pid, player, 0); + GET_PLAYER(pid, player, ); - return player->factionChanges.count; + player->factionChanges.factions.clear(); } -unsigned char FactionFunctions::GetFactionChangesAction(unsigned short pid) noexcept +unsigned int FactionFunctions::GetFactionChangesSize(unsigned short pid) noexcept { Player *player; GET_PLAYER(pid, player, 0); - return player->factionChanges.action; + return player->factionChanges.count; } -void FactionFunctions::AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept +unsigned char FactionFunctions::GetFactionChangesAction(unsigned short pid) noexcept { Player *player; - GET_PLAYER(pid, player, ); - - mwmp::Faction faction; - faction.factionId = factionId; - faction.rank = rank; - faction.isExpelled = isExpelled; + GET_PLAYER(pid, player, 0); - player->factionChangesBuffer.factions.push_back(faction); + return player->factionChanges.action; } const char *FactionFunctions::GetFactionId(unsigned short pid, unsigned int i) noexcept @@ -62,16 +57,32 @@ bool FactionFunctions::GetFactionExpelledState(unsigned short pid, unsigned int return player->factionChanges.factions.at(i).isExpelled; } -void FactionFunctions::SendFactionChanges(unsigned short pid) noexcept +void FactionFunctions::SetFactionChangesAction(unsigned short pid, unsigned char action) noexcept { Player *player; GET_PLAYER(pid, player, ); - player->factionChangesBuffer.action = mwmp::FactionChanges::BOTH; + player->factionChanges.action = action; +} + +void FactionFunctions::AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + mwmp::Faction faction; + faction.factionId = factionId; + faction.rank = rank; + faction.isExpelled = isExpelled; + + player->factionChanges.factions.push_back(faction); +} + +void FactionFunctions::SendFactionChanges(unsigned short pid, bool toOthers) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); - std::swap(player->factionChanges, player->factionChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION)->Send(false); - player->factionChanges = std::move(player->factionChangesBuffer); - player->factionChangesBuffer.factions.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_FACTION)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Factions.hpp b/apps/openmw-mp/Script/Functions/Factions.hpp index d7d633254..8b957ce29 100644 --- a/apps/openmw-mp/Script/Functions/Factions.hpp +++ b/apps/openmw-mp/Script/Functions/Factions.hpp @@ -2,31 +2,37 @@ #define OPENMW_FACTIONAPI_HPP #define FACTIONAPI \ - {"GetFactionChangesSize", FactionFunctions::GetFactionChangesSize},\ - {"GetFactionChangesAction", FactionFunctions::GetFactionChangesAction},\ + {"InitializeFactionChanges", FactionFunctions::InitializeFactionChanges},\ \ - {"AddFaction", FactionFunctions::AddFaction},\ + {"GetFactionChangesSize", FactionFunctions::GetFactionChangesSize},\ + {"GetFactionChangesAction", FactionFunctions::GetFactionChangesAction},\ \ - {"GetFactionId", FactionFunctions::GetFactionId},\ - {"GetFactionRank", FactionFunctions::GetFactionRank},\ - {"GetFactionExpelledState", FactionFunctions::GetFactionExpelledState},\ + {"GetFactionId", FactionFunctions::GetFactionId},\ + {"GetFactionRank", FactionFunctions::GetFactionRank},\ + {"GetFactionExpelledState", FactionFunctions::GetFactionExpelledState},\ \ - {"SendFactionChanges", FactionFunctions::SendFactionChanges} + {"SetFactionChangesAction", FactionFunctions::SetFactionChangesAction},\ + {"AddFaction", FactionFunctions::AddFaction},\ + \ + {"SendFactionChanges", FactionFunctions::SendFactionChanges} class FactionFunctions { public: + static void InitializeFactionChanges(unsigned short pid) noexcept; + static unsigned int GetFactionChangesSize(unsigned short pid) noexcept; static unsigned char GetFactionChangesAction(unsigned short pid) noexcept; - static void AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept; - static const char *GetFactionId(unsigned short pid, unsigned int i) noexcept; static int GetFactionRank(unsigned short pid, unsigned int i) noexcept; static bool GetFactionExpelledState(unsigned short pid, unsigned int i) noexcept; - static void SendFactionChanges(unsigned short pid) noexcept; + static void SetFactionChangesAction(unsigned short pid, unsigned char action) noexcept; + static void AddFaction(unsigned short pid, const char* factionId, unsigned int rank, bool isExpelled) noexcept; + + static void SendFactionChanges(unsigned short pid, bool toOthers = false) noexcept; private: }; diff --git a/apps/openmw-mp/Script/Functions/Items.cpp b/apps/openmw-mp/Script/Functions/Items.cpp index 281b2bcb7..0626a1c34 100644 --- a/apps/openmw-mp/Script/Functions/Items.cpp +++ b/apps/openmw-mp/Script/Functions/Items.cpp @@ -11,6 +11,15 @@ using namespace mwmp; +void ItemFunctions::InitializeInventoryChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->inventoryChanges.items.clear(); + player->inventoryChanges.action = InventoryChanges::SET; +} + int ItemFunctions::GetEquipmentSize() noexcept { return MWWorld::InventoryStore::Slots; @@ -52,8 +61,8 @@ void ItemFunctions::AddItem(unsigned short pid, const char* refId, unsigned int item.count = count; item.charge = charge; - player->inventoryChangesBuffer.items.push_back(item); - player->inventoryChangesBuffer.action = InventoryChanges::ADD; + player->inventoryChanges.items.push_back(item); + player->inventoryChanges.action = InventoryChanges::ADD; } void ItemFunctions::RemoveItem(unsigned short pid, const char* refId, unsigned short count) noexcept @@ -65,17 +74,8 @@ void ItemFunctions::RemoveItem(unsigned short pid, const char* refId, unsigned s item.refId = refId; item.count = count; - player->inventoryChangesBuffer.items.push_back(item); - player->inventoryChangesBuffer.action = InventoryChanges::REMOVE; -} - -void ItemFunctions::ClearInventory(unsigned short pid) noexcept -{ - Player *player; - GET_PLAYER(pid, player, ); - - player->inventoryChangesBuffer.items.clear(); - player->inventoryChangesBuffer.action = InventoryChanges::SET; + player->inventoryChanges.items.push_back(item); + player->inventoryChanges.action = InventoryChanges::REMOVE; } bool ItemFunctions::HasItemEquipped(unsigned short pid, const char* refId) @@ -150,14 +150,11 @@ void ItemFunctions::SendEquipment(unsigned short pid) noexcept mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_EQUIPMENT)->Send(true); } -void ItemFunctions::SendInventoryChanges(unsigned short pid) noexcept +void ItemFunctions::SendInventoryChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->inventoryChanges, player->inventoryChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_INVENTORY)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_INVENTORY)->Send(false); - player->inventoryChanges = std::move(player->inventoryChangesBuffer); - player->inventoryChangesBuffer.items.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_INVENTORY)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Items.hpp b/apps/openmw-mp/Script/Functions/Items.hpp index e70c22a09..6bc87a981 100644 --- a/apps/openmw-mp/Script/Functions/Items.hpp +++ b/apps/openmw-mp/Script/Functions/Items.hpp @@ -6,33 +6,36 @@ #define OPENMW_ITEMAPI_HPP #define ITEMAPI \ - {"GetEquipmentSize", ItemFunctions::GetEquipmentSize},\ - {"GetInventoryChangesSize", ItemFunctions::GetInventoryChangesSize},\ + {"InitializeInventoryChanges", ItemFunctions::InitializeInventoryChanges},\ \ - {"EquipItem", ItemFunctions::EquipItem},\ - {"UnequipItem", ItemFunctions::UnequipItem},\ + {"GetEquipmentSize", ItemFunctions::GetEquipmentSize},\ + {"GetInventoryChangesSize", ItemFunctions::GetInventoryChangesSize},\ \ - {"AddItem", ItemFunctions::AddItem},\ - {"RemoveItem", ItemFunctions::RemoveItem},\ - {"ClearInventory", ItemFunctions::ClearInventory},\ + {"EquipItem", ItemFunctions::EquipItem},\ + {"UnequipItem", ItemFunctions::UnequipItem},\ \ - {"HasItemEquipped", ItemFunctions::HasItemEquipped},\ + {"AddItem", ItemFunctions::AddItem},\ + {"RemoveItem", ItemFunctions::RemoveItem},\ \ - {"GetEquipmentItemRefId", ItemFunctions::GetEquipmentItemRefId},\ - {"GetEquipmentItemCount", ItemFunctions::GetEquipmentItemCount},\ - {"GetEquipmentItemCharge", ItemFunctions::GetEquipmentItemCharge},\ + {"HasItemEquipped", ItemFunctions::HasItemEquipped},\ \ - {"GetInventoryItemRefId", ItemFunctions::GetInventoryItemRefId},\ - {"GetInventoryItemCount", ItemFunctions::GetInventoryItemCount},\ - {"GetInventoryItemCharge", ItemFunctions::GetInventoryItemCharge},\ + {"GetEquipmentItemRefId", ItemFunctions::GetEquipmentItemRefId},\ + {"GetEquipmentItemCount", ItemFunctions::GetEquipmentItemCount},\ + {"GetEquipmentItemCharge", ItemFunctions::GetEquipmentItemCharge},\ \ - {"SendEquipment", ItemFunctions::SendEquipment},\ - {"SendInventoryChanges", ItemFunctions::SendInventoryChanges} + {"GetInventoryItemRefId", ItemFunctions::GetInventoryItemRefId},\ + {"GetInventoryItemCount", ItemFunctions::GetInventoryItemCount},\ + {"GetInventoryItemCharge", ItemFunctions::GetInventoryItemCharge},\ + \ + {"SendEquipment", ItemFunctions::SendEquipment},\ + {"SendInventoryChanges", ItemFunctions::SendInventoryChanges} class ItemFunctions { public: + static void InitializeInventoryChanges(unsigned short pid) noexcept; + static int GetEquipmentSize() noexcept; static unsigned int GetInventoryChangesSize(unsigned short pid) noexcept; @@ -41,7 +44,6 @@ public: static void AddItem(unsigned short pid, const char* refId, unsigned int count, int charge) noexcept; static void RemoveItem(unsigned short pid, const char* refId, unsigned short count) noexcept; - static void ClearInventory(unsigned short pid) noexcept; static bool HasItemEquipped(unsigned short pid, const char* refId); @@ -54,7 +56,7 @@ public: static int GetInventoryItemCharge(unsigned short pid, unsigned int i) noexcept; static void SendEquipment(unsigned short pid) noexcept; - static void SendInventoryChanges(unsigned short pid) noexcept; + static void SendInventoryChanges(unsigned short pid, bool toOthers = false) noexcept; private: }; diff --git a/apps/openmw-mp/Script/Functions/Quests.cpp b/apps/openmw-mp/Script/Functions/Quests.cpp index 4658b2e1b..a6897c37c 100644 --- a/apps/openmw-mp/Script/Functions/Quests.cpp +++ b/apps/openmw-mp/Script/Functions/Quests.cpp @@ -6,6 +6,14 @@ using namespace mwmp; +void QuestFunctions::InitializeJournalChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->journalChanges.journalItems.clear(); +} + unsigned int QuestFunctions::GetJournalChangesSize(unsigned short pid) noexcept { Player *player; @@ -25,7 +33,7 @@ void QuestFunctions::AddJournalEntry(unsigned short pid, const char* quest, unsi journalItem.index = index; journalItem.actorRefId = actorRefId; - player->journalChangesBuffer.journalItems.push_back(journalItem); + player->journalChanges.journalItems.push_back(journalItem); } void QuestFunctions::AddJournalIndex(unsigned short pid, const char* quest, unsigned int index) noexcept @@ -38,7 +46,7 @@ void QuestFunctions::AddJournalIndex(unsigned short pid, const char* quest, unsi journalItem.quest = quest; journalItem.index = index; - player->journalChangesBuffer.journalItems.push_back(journalItem); + player->journalChanges.journalItems.push_back(journalItem); } const char *QuestFunctions::GetJournalItemQuest(unsigned short pid, unsigned int i) noexcept @@ -76,14 +84,11 @@ const char *QuestFunctions::GetJournalItemActorRefId(unsigned short pid, unsigne return player->journalChanges.journalItems.at(i).actorRefId.c_str(); } -void QuestFunctions::SendJournalChanges(unsigned short pid) noexcept +void QuestFunctions::SendJournalChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->journalChanges, player->journalChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_JOURNAL)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_JOURNAL)->Send(false); - player->journalChanges = std::move(player->journalChangesBuffer); - player->journalChangesBuffer.journalItems.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_JOURNAL)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Quests.hpp b/apps/openmw-mp/Script/Functions/Quests.hpp index 0ae3daac5..2011f036a 100644 --- a/apps/openmw-mp/Script/Functions/Quests.hpp +++ b/apps/openmw-mp/Script/Functions/Quests.hpp @@ -2,6 +2,8 @@ #define OPENMW_QUESTAPI_HPP #define QUESTAPI \ + {"InitializeJournalChanges", QuestFunctions::InitializeJournalChanges},\ + \ {"GetJournalChangesSize", QuestFunctions::GetJournalChangesSize},\ \ {"AddJournalEntry", QuestFunctions::AddJournalEntry},\ @@ -17,6 +19,7 @@ class QuestFunctions { public: + static void InitializeJournalChanges(unsigned short pid) noexcept; static unsigned int GetJournalChangesSize(unsigned short pid) noexcept; @@ -28,7 +31,7 @@ public: static int GetJournalItemType(unsigned short pid, unsigned int i) noexcept; static const char *GetJournalItemActorRefId(unsigned short pid, unsigned int i) noexcept; - static void SendJournalChanges(unsigned short pid) noexcept; + static void SendJournalChanges(unsigned short pid, bool toOthers = false) noexcept; private: }; diff --git a/apps/openmw-mp/Script/Functions/Spells.cpp b/apps/openmw-mp/Script/Functions/Spells.cpp index ae93cc95f..248a44195 100644 --- a/apps/openmw-mp/Script/Functions/Spells.cpp +++ b/apps/openmw-mp/Script/Functions/Spells.cpp @@ -6,6 +6,15 @@ using namespace mwmp; +void SpellFunctions::InitializeSpellbookChanges(unsigned short pid) noexcept +{ + Player *player; + GET_PLAYER(pid, player, ); + + player->spellbookChanges.spells.clear(); + player->spellbookChanges.action = SpellbookChanges::SET; +} + unsigned int SpellFunctions::GetSpellbookChangesSize(unsigned short pid) noexcept { Player *player; @@ -30,8 +39,8 @@ void SpellFunctions::AddSpell(unsigned short pid, const char* spellId) noexcept ESM::Spell spell; spell.mId = spellId; - player->spellbookChangesBuffer.spells.push_back(spell); - player->spellbookChangesBuffer.action = SpellbookChanges::ADD; + player->spellbookChanges.spells.push_back(spell); + player->spellbookChanges.action = SpellbookChanges::ADD; } void SpellFunctions::RemoveSpell(unsigned short pid, const char* spellId) noexcept @@ -42,17 +51,8 @@ void SpellFunctions::RemoveSpell(unsigned short pid, const char* spellId) noexce ESM::Spell spell; spell.mId = spellId; - player->spellbookChangesBuffer.spells.push_back(spell); - player->spellbookChangesBuffer.action = SpellbookChanges::REMOVE; -} - -void SpellFunctions::ClearSpellbook(unsigned short pid) noexcept -{ - Player *player; - GET_PLAYER(pid, player, ); - - player->spellbookChangesBuffer.spells.clear(); - player->spellbookChangesBuffer.action = SpellbookChanges::SET; + player->spellbookChanges.spells.push_back(spell); + player->spellbookChanges.action = SpellbookChanges::REMOVE; } const char *SpellFunctions::GetSpellId(unsigned short pid, unsigned int i) noexcept @@ -66,14 +66,11 @@ const char *SpellFunctions::GetSpellId(unsigned short pid, unsigned int i) noexc return player->spellbookChanges.spells.at(i).mId.c_str(); } -void SpellFunctions::SendSpellbookChanges(unsigned short pid) noexcept +void SpellFunctions::SendSpellbookChanges(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); - std::swap(player->spellbookChanges, player->spellbookChangesBuffer); mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK)->setPlayer(player); - mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK)->Send(false); - player->spellbookChanges = std::move(player->spellbookChangesBuffer); - player->spellbookChangesBuffer.spells.clear(); + mwmp::Networking::get().getPlayerPacketController()->GetPacket(ID_PLAYER_SPELLBOOK)->Send(toOthers); } diff --git a/apps/openmw-mp/Script/Functions/Spells.hpp b/apps/openmw-mp/Script/Functions/Spells.hpp index 82553c28a..259fb6d26 100644 --- a/apps/openmw-mp/Script/Functions/Spells.hpp +++ b/apps/openmw-mp/Script/Functions/Spells.hpp @@ -2,31 +2,33 @@ #define OPENMW_SPELLAPI_HPP #define SPELLAPI \ - {"GetSpellbookChangesSize", SpellFunctions::GetSpellbookChangesSize},\ - {"GetSpellbookAction", SpellFunctions::GetSpellbookAction},\ + {"InitializeSpellbookChanges", SpellFunctions::InitializeSpellbookChanges},\ \ - {"AddSpell", SpellFunctions::AddSpell},\ - {"RemoveSpell", SpellFunctions::RemoveSpell},\ - {"ClearSpellbook", SpellFunctions::ClearSpellbook},\ + {"GetSpellbookChangesSize", SpellFunctions::GetSpellbookChangesSize},\ + {"GetSpellbookAction", SpellFunctions::GetSpellbookAction},\ \ - {"GetSpellId", SpellFunctions::GetSpellId},\ + {"AddSpell", SpellFunctions::AddSpell},\ + {"RemoveSpell", SpellFunctions::RemoveSpell},\ \ - {"SendSpellbookChanges", SpellFunctions::SendSpellbookChanges} + {"GetSpellId", SpellFunctions::GetSpellId},\ + \ + {"SendSpellbookChanges", SpellFunctions::SendSpellbookChanges} class SpellFunctions { public: + static void InitializeSpellbookChanges(unsigned short pid) noexcept; + static unsigned int GetSpellbookChangesSize(unsigned short pid) noexcept; static unsigned int GetSpellbookAction(unsigned short pid) noexcept; static void AddSpell(unsigned short pid, const char* spellId) noexcept; static void RemoveSpell(unsigned short pid, const char* spellId) noexcept; - static void ClearSpellbook(unsigned short pid) noexcept; static const char *GetSpellId(unsigned short pid, unsigned int i) noexcept; - static void SendSpellbookChanges(unsigned short pid) noexcept; + static void SendSpellbookChanges(unsigned short pid, bool toOthers = false) noexcept; private: };