diff --git a/apps/openmw-mp/Script/Functions/Worldstate.cpp b/apps/openmw-mp/Script/Functions/Worldstate.cpp index 5ce39cea8..164e3af45 100644 --- a/apps/openmw-mp/Script/Functions/Worldstate.cpp +++ b/apps/openmw-mp/Script/Functions/Worldstate.cpp @@ -158,6 +158,11 @@ void WorldstateFunctions::AddSynchronizedClientScriptId(const char *scriptId) no writeWorldstate.synchronizedClientScriptIds.push_back(scriptId); } +void WorldstateFunctions::AddSynchronizedClientGlobalId(const char *globalId) noexcept +{ + writeWorldstate.synchronizedClientGlobalIds.push_back(globalId); +} + void WorldstateFunctions::AddEnforcedCollisionRefId(const char *refId) noexcept { writeWorldstate.enforcedCollisionRefIds.push_back(refId); @@ -168,6 +173,11 @@ void WorldstateFunctions::ClearSynchronizedClientScriptIds() noexcept writeWorldstate.synchronizedClientScriptIds.clear(); } +void WorldstateFunctions::ClearSynchronizedClientGlobalIds() noexcept +{ + writeWorldstate.synchronizedClientGlobalIds.clear(); +} + void WorldstateFunctions::ClearEnforcedCollisionRefIds() noexcept { writeWorldstate.enforcedCollisionRefIds.clear(); diff --git a/apps/openmw-mp/Script/Functions/Worldstate.hpp b/apps/openmw-mp/Script/Functions/Worldstate.hpp index 6406b53b6..d1c551c8c 100644 --- a/apps/openmw-mp/Script/Functions/Worldstate.hpp +++ b/apps/openmw-mp/Script/Functions/Worldstate.hpp @@ -44,9 +44,11 @@ {"UseActorCollisionForPlacedObjects", WorldstateFunctions::UseActorCollisionForPlacedObjects},\ \ {"AddSynchronizedClientScriptId", WorldstateFunctions::AddSynchronizedClientScriptId},\ + {"AddSynchronizedClientGlobalId", WorldstateFunctions::AddSynchronizedClientGlobalId},\ {"AddEnforcedCollisionRefId", WorldstateFunctions::AddEnforcedCollisionRefId},\ \ {"ClearSynchronizedClientScriptIds", WorldstateFunctions::ClearSynchronizedClientScriptIds},\ + {"ClearSynchronizedClientGlobalIds", WorldstateFunctions::ClearSynchronizedClientGlobalIds},\ {"ClearEnforcedCollisionRefIds", WorldstateFunctions::ClearEnforcedCollisionRefIds},\ \ {"SaveMapTileImageFile", WorldstateFunctions::SaveMapTileImageFile},\ @@ -298,14 +300,23 @@ public: static void UseActorCollisionForPlacedObjects(bool useActorCollision) noexcept; /** - * \brief Add an ID to the list of script IDs whose variables should all be synchronized - * across players. + * \brief Add an ID to the list of script IDs whose variable changes should be sent to the + * the server by clients. * * \param scriptId The ID. * \return void */ static void AddSynchronizedClientScriptId(const char* scriptId) noexcept; + /** + * \brief Add an ID to the list of global IDs whose value changes should be sent to the + * server by clients. + * + * \param globalId The ID. + * \return void + */ + static void AddSynchronizedClientGlobalId(const char* globalId) noexcept; + /** * \brief Add a refId to the list of refIds for which collision should be enforced * irrespective of other settings. @@ -316,13 +327,21 @@ public: static void AddEnforcedCollisionRefId(const char* refId) noexcept; /** - * \brief Clear the list of script IDs whose variables should all be synchronized - * across players. + * \brief Clear the list of script IDs whose variable changes should be sent to the + * the server by clients. * * \return void */ static void ClearSynchronizedClientScriptIds() noexcept; + /** + * \brief Clear the list of global IDs whose value changes should be sent to the + * the server by clients. + * + * \return void + */ + static void ClearSynchronizedClientGlobalIds() noexcept; + /** * \brief Clear the list of refIds for which collision should be enforced irrespective * of other settings. diff --git a/apps/openmw/mwmp/Main.cpp b/apps/openmw/mwmp/Main.cpp index 8c8bb94fa..83359d92f 100644 --- a/apps/openmw/mwmp/Main.cpp +++ b/apps/openmw/mwmp/Main.cpp @@ -232,11 +232,21 @@ CellController *Main::getCellController() const return mCellController; } -bool Main::isValidPacketScript(std::string script) +bool Main::isValidPacketScript(std::string scriptId) { mwmp::BaseWorldstate *worldstate = get().getNetworking()->getWorldstate(); - if (Utils::vectorContains(worldstate->synchronizedClientScriptIds, script)) + if (Utils::vectorContains(worldstate->synchronizedClientScriptIds, scriptId)) + return true; + + return false; +} + +bool Main::isValidPacketGlobal(std::string globalId) +{ + mwmp::BaseWorldstate *worldstate = get().getNetworking()->getWorldstate(); + + if (Utils::vectorContains(worldstate->synchronizedClientGlobalIds, globalId)) return true; return false; diff --git a/apps/openmw/mwmp/Main.hpp b/apps/openmw/mwmp/Main.hpp index 18522d149..3111a1665 100644 --- a/apps/openmw/mwmp/Main.hpp +++ b/apps/openmw/mwmp/Main.hpp @@ -26,7 +26,9 @@ namespace mwmp static const Main &get(); static void frame(float dt); - static bool isValidPacketScript(std::string script); + static bool isValidPacketScript(std::string scriptId); + static bool isValidPacketGlobal(std::string globalId); + static std::string getResDir(); Networking *getNetworking() const; diff --git a/apps/openmw/mwmp/processors/worldstate/ProcessorClientScriptSettings.hpp b/apps/openmw/mwmp/processors/worldstate/ProcessorClientScriptSettings.hpp index 2db5a8b12..368f4a442 100644 --- a/apps/openmw/mwmp/processors/worldstate/ProcessorClientScriptSettings.hpp +++ b/apps/openmw/mwmp/processors/worldstate/ProcessorClientScriptSettings.hpp @@ -18,7 +18,21 @@ namespace mwmp virtual void Do(WorldstatePacket &packet, Worldstate &worldstate) { - // Placeholder + LOG_MESSAGE_SIMPLE(TimedLog::LOG_INFO, "Received ID_CLIENT_SCRIPT_SETTINGS making us send packets for the following globals:"); + std::string debugMessage = ""; + + for (const auto &globalId : worldstate.synchronizedClientGlobalIds) + { + if (TimedLog::GetLevel() <= TimedLog::LOG_INFO) + { + if (!debugMessage.empty()) + debugMessage += ", "; + + debugMessage += globalId; + } + } + + LOG_APPEND(TimedLog::LOG_INFO, "- %s", debugMessage.c_str()); } }; } diff --git a/apps/openmw/mwscript/interpretercontext.cpp b/apps/openmw/mwscript/interpretercontext.cpp index 8edb44fab..8f347ed3a 100644 --- a/apps/openmw/mwscript/interpretercontext.cpp +++ b/apps/openmw/mwscript/interpretercontext.cpp @@ -323,10 +323,11 @@ namespace MWScript /* Start of tes3mp addition - Send an ID_SCRIPT_GLOBAL_SHORT packet every time a global short changes its value - in a script approved for packet sending + Send an ID_SCRIPT_GLOBAL_SHORT packet when a global short changes its value as long as + it is being set in a script that has been approved for packet sending or the global itself + has been set to always be synchronized */ - if (sendPackets) + if (sendPackets || mwmp::Main::isValidPacketGlobal(name)) { mwmp::ObjectList *objectList = mwmp::Main::get().getNetworking()->getObjectList(); objectList->reset(); diff --git a/components/openmw-mp/Base/BaseWorldstate.hpp b/components/openmw-mp/Base/BaseWorldstate.hpp index 8d42680eb..6a2bd3a0e 100644 --- a/components/openmw-mp/Base/BaseWorldstate.hpp +++ b/components/openmw-mp/Base/BaseWorldstate.hpp @@ -308,6 +308,7 @@ namespace mwmp mwmp::Time time; std::vector synchronizedClientScriptIds; + std::vector synchronizedClientGlobalIds; bool hasPlayerCollision; bool hasActorCollision; diff --git a/components/openmw-mp/Packets/Worldstate/PacketClientScriptSettings.cpp b/components/openmw-mp/Packets/Worldstate/PacketClientScriptSettings.cpp index 1a494e9cb..673023d86 100644 --- a/components/openmw-mp/Packets/Worldstate/PacketClientScriptSettings.cpp +++ b/components/openmw-mp/Packets/Worldstate/PacketClientScriptSettings.cpp @@ -30,4 +30,22 @@ void PacketClientScriptSettings::Packet(RakNet::BitStream *bs, bool send) { RW(clientScriptId, send); } + + uint32_t clientGlobalsCount; + + if (send) + clientGlobalsCount = static_cast(worldstate->synchronizedClientGlobalIds.size()); + + RW(clientGlobalsCount, send); + + if (!send) + { + worldstate->synchronizedClientGlobalIds.clear(); + worldstate->synchronizedClientGlobalIds.resize(clientGlobalsCount); + } + + for (auto &&clientGlobalId : worldstate->synchronizedClientGlobalIds) + { + RW(clientGlobalId, send); + } }