From 46744ee90ff57d9eec91cfdc383427905e1a70eb Mon Sep 17 00:00:00 2001 From: David Cernat Date: Thu, 24 May 2018 09:38:06 +0300 Subject: [PATCH] [General] Make WorldTime script functions more consistent with others --- .../openmw-mp/Script/Functions/Worldstate.cpp | 53 ++++--------------- .../openmw-mp/Script/Functions/Worldstate.hpp | 29 ++++++---- .../worldstate/ProcessorWorldTime.hpp | 9 ++-- components/openmw-mp/Base/BaseWorldstate.hpp | 5 +- 4 files changed, 41 insertions(+), 55 deletions(-) diff --git a/apps/openmw-mp/Script/Functions/Worldstate.cpp b/apps/openmw-mp/Script/Functions/Worldstate.cpp index 56e51ef36..b59ac9d32 100644 --- a/apps/openmw-mp/Script/Functions/Worldstate.cpp +++ b/apps/openmw-mp/Script/Functions/Worldstate.cpp @@ -12,66 +12,35 @@ using namespace mwmp; BaseWorldstate writeWorldstate; -void WorldstateFunctions::SetHour(unsigned short pid, double hour) noexcept +void WorldstateFunctions::SetHour(double hour) noexcept { - Player *player; - GET_PLAYER(pid, player, ); - - writeWorldstate.guid = player->guid; - writeWorldstate.hour = hour; - writeWorldstate.day = -1; - writeWorldstate.month = -1; - writeWorldstate.timeScale = -1; - - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->setWorldstate(&writeWorldstate); - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->Send(false); } -void WorldstateFunctions::SetDay(unsigned short pid, int day) noexcept +void WorldstateFunctions::SetDay(int day) noexcept { - Player *player; - GET_PLAYER(pid, player, ); - - writeWorldstate.guid = player->guid; - - writeWorldstate.hour = -1; writeWorldstate.day = day; - writeWorldstate.month = -1; - writeWorldstate.timeScale = -1; - - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->setWorldstate(&writeWorldstate); - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->Send(false); } -void WorldstateFunctions::SetMonth(unsigned short pid, int month) noexcept +void WorldstateFunctions::SetMonth(int month) noexcept { - Player *player; - GET_PLAYER(pid, player, ); - - writeWorldstate.guid = player->guid; - - writeWorldstate.hour = -1; - writeWorldstate.day = -1; writeWorldstate.month = month; - writeWorldstate.timeScale = -1; - - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->setWorldstate(&writeWorldstate); - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->Send(false); } -void WorldstateFunctions::SetTimeScale(unsigned short pid, double timeScale) noexcept +void WorldstateFunctions::SetTimeScale(double timeScale) noexcept +{ + writeWorldstate.timeScale = timeScale; +} +void WorldstateFunctions::SendWorldTime(unsigned short pid, bool toOthers) noexcept { Player *player; GET_PLAYER(pid, player, ); writeWorldstate.guid = player->guid; - writeWorldstate.hour = -1; - writeWorldstate.day = -1; - writeWorldstate.month = -1; - writeWorldstate.timeScale = timeScale; - mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->setWorldstate(&writeWorldstate); mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->Send(false); + + if (toOthers) + mwmp::Networking::get().getWorldstatePacketController()->GetPacket(ID_WORLD_TIME)->Send(true); } diff --git a/apps/openmw-mp/Script/Functions/Worldstate.hpp b/apps/openmw-mp/Script/Functions/Worldstate.hpp index 94435be40..c3b2de7bf 100644 --- a/apps/openmw-mp/Script/Functions/Worldstate.hpp +++ b/apps/openmw-mp/Script/Functions/Worldstate.hpp @@ -7,47 +7,58 @@ {"SetHour", WorldstateFunctions::SetHour},\ {"SetDay", WorldstateFunctions::SetDay},\ {"SetMonth", WorldstateFunctions::SetMonth},\ - {"SetTimeScale", WorldstateFunctions::SetTimeScale} + {"SetTimeScale", WorldstateFunctions::SetTimeScale},\ + \ + {"SendWorldTime", WorldstateFunctions::SendWorldTime} class WorldstateFunctions { public: /** - * \brief Set the world's hour for a player and send a WorldTime packet to that player. + * \brief Set the world's hour in the worldstate stored on the server. * * \param pid The player ID. * \param hour The hour. * \return void */ - static void SetHour(unsigned short pid, double hour) noexcept; + static void SetHour(double hour) noexcept; /** - * \brief Set the world's day of the month for a player and send a WorldTime packet to that player. + * \brief Set the world's day in the worldstate stored on the server. * * \param pid The player ID. * \param day The day. * \return void */ - static void SetDay(unsigned short pid, int day) noexcept; + static void SetDay(int day) noexcept; /** - * \brief Set the world's month for a player and send a WorldTime packet to that player. + * \brief Set the world's month in the worldstate stored on the server. * * \param pid The player ID. * \param month The month. * \return void */ - static void SetMonth(unsigned short pid, int month) noexcept; + static void SetMonth(int month) noexcept; /** - * \brief Set the world's time scale for a player and send a WorldTime packet to that player. + * \brief Set the world's time scale in the worldstate stored on the server. * * \param pid The player ID. * \param timeScale The time scale. * \return void */ - static void SetTimeScale(unsigned short pid, double timeScale) noexcept; + static void SetTimeScale(double timeScale) noexcept; + + /** + * \brief Send a WorldTime packet with the current time and time scale + * to a specific player or to all players. + * + * \param pid The player ID. + * \return void + */ + static void SendWorldTime(unsigned short pid, bool toOthers = false) noexcept; }; diff --git a/apps/openmw/mwmp/processors/worldstate/ProcessorWorldTime.hpp b/apps/openmw/mwmp/processors/worldstate/ProcessorWorldTime.hpp index fd656b207..8061f0edb 100644 --- a/apps/openmw/mwmp/processors/worldstate/ProcessorWorldTime.hpp +++ b/apps/openmw/mwmp/processors/worldstate/ProcessorWorldTime.hpp @@ -23,11 +23,14 @@ namespace mwmp MWBase::World *world = MWBase::Environment::get().getWorld(); if (worldstate.hour != -1) world->setHour(worldstate.hour); - else if (worldstate.day != -1) + + if (worldstate.day != -1) world->setDay(worldstate.day); - else if (worldstate.month != -1) + + if (worldstate.month != -1) world->setMonth(worldstate.month); - else if (worldstate.timeScale != -1) + + if (worldstate.timeScale != -1) world->setTimeScale(worldstate.timeScale); } } diff --git a/components/openmw-mp/Base/BaseWorldstate.hpp b/components/openmw-mp/Base/BaseWorldstate.hpp index 04a6283ac..3c6174c76 100644 --- a/components/openmw-mp/Base/BaseWorldstate.hpp +++ b/components/openmw-mp/Base/BaseWorldstate.hpp @@ -14,7 +14,10 @@ namespace mwmp BaseWorldstate() { - + month = -1; + day = -1; + hour = -1; + timeScale = -1; } RakNet::RakNetGUID guid;