From 7ffe66be8e842c458369821a67088d04dd207494 Mon Sep 17 00:00:00 2001 From: Robbe Bryssinck Date: Tue, 10 May 2022 22:57:15 +0200 Subject: [PATCH] feat: server side pvp setting --- Code/client/Games/Skyrim/Actor.cpp | 6 ++++++ Code/client/Games/Skyrim/Magic/MagicTarget.cpp | 2 +- Code/client/Services/Generic/PlayerService.cpp | 3 ++- Code/client/Services/Generic/TransportService.cpp | 3 +++ Code/client/World.h | 5 +++++ Code/encoding/Structs/ServerSettings.cpp | 6 ++++-- Code/encoding/Structs/ServerSettings.h | 3 ++- Code/server/GameServer.cpp | 4 +++- 8 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Code/client/Games/Skyrim/Actor.cpp b/Code/client/Games/Skyrim/Actor.cpp index 3ba1d4c66..6ddbd14cb 100644 --- a/Code/client/Games/Skyrim/Actor.cpp +++ b/Code/client/Games/Skyrim/Actor.cpp @@ -579,6 +579,12 @@ bool TP_MAKE_THISCALL(HookDamageActor, Actor, float aDamage, Actor* apHitter) const auto* pExHittee = apThis->GetExtension(); if (pExHittee->IsLocalPlayer()) { + if (!World::Get().GetServerSettings().PvpEnabled) + { + if (apHitter && apHitter->GetExtension()->IsRemotePlayer()) + return false; + } + World::Get().GetRunner().Trigger(HealthChangeEvent(apThis->formID, -realDamage)); return ThisCall(RealDamageActor, apThis, aDamage, apHitter); } diff --git a/Code/client/Games/Skyrim/Magic/MagicTarget.cpp b/Code/client/Games/Skyrim/Magic/MagicTarget.cpp index df217dded..9c818b1d5 100644 --- a/Code/client/Games/Skyrim/Magic/MagicTarget.cpp +++ b/Code/client/Games/Skyrim/Magic/MagicTarget.cpp @@ -60,7 +60,7 @@ bool TP_MAKE_THISCALL(HookAddTarget, MagicTarget, MagicTarget::AddTargetData& ar if (!pCasterExtension->IsLocalPlayer()) return false; - if (!arData.pEffectItem->IsHealingEffect()) + if (!arData.pEffectItem->IsHealingEffect() && !World::Get().GetServerSettings().PvpEnabled) return false; bool result = ThisCall(RealAddTarget, apThis, arData); diff --git a/Code/client/Services/Generic/PlayerService.cpp b/Code/client/Services/Generic/PlayerService.cpp index 2b699d9a9..d56218a21 100644 --- a/Code/client/Services/Generic/PlayerService.cpp +++ b/Code/client/Services/Generic/PlayerService.cpp @@ -42,6 +42,7 @@ void PlayerService::OnDisconnected(const DisconnectedEvent& acEvent) noexcept PlayerCharacter::Get()->SetDifficulty(m_previousDifficulty); m_serverDifficulty = m_previousDifficulty = 6; + // Restore to the default value (150) float* greetDistance = Settings::GetGreetDistance(); *greetDistance = 150.f; } @@ -52,7 +53,7 @@ void PlayerService::OnServerSettingsReceived(const ServerSettings& acSettings) n PlayerCharacter::Get()->SetDifficulty(acSettings.Difficulty); m_serverDifficulty = acSettings.Difficulty; - if (!acSettings.EnableGreetings) + if (!acSettings.GreetingsEnabled) { float* greetDistance = Settings::GetGreetDistance(); *greetDistance = 0.f; diff --git a/Code/client/Services/Generic/TransportService.cpp b/Code/client/Services/Generic/TransportService.cpp index 24b703844..e0434db1a 100644 --- a/Code/client/Services/Generic/TransportService.cpp +++ b/Code/client/Services/Generic/TransportService.cpp @@ -153,6 +153,9 @@ void TransportService::HandleAuthenticationResponse(const AuthenticationResponse case AuthenticationResponse::ResponseType::kAccepted: { m_connected = true; + + m_world.SetServerSettings(acMessage.Settings); + m_dispatcher.trigger(acMessage.UserMods); m_dispatcher.trigger(acMessage.Settings); m_dispatcher.trigger(ConnectedEvent()); diff --git a/Code/client/World.h b/Code/client/World.h index 4d7c2d67a..1da2b447f 100644 --- a/Code/client/World.h +++ b/Code/client/World.h @@ -8,6 +8,7 @@ #include +#include struct World : entt::registry { @@ -32,6 +33,9 @@ struct World : entt::registry return m_dispatcher; } + const ServerSettings& GetServerSettings() const noexcept { return m_serverSettings; } + void SetServerSettings(ServerSettings aServerSettings) noexcept { m_serverSettings = aServerSettings; } + [[nodiscard]] uint64_t GetTick() const noexcept; static void Create() noexcept; @@ -43,6 +47,7 @@ struct World : entt::registry RunnerService m_runner; TransportService m_transport; ModSystem m_modSystem; + ServerSettings m_serverSettings{}; std::chrono::high_resolution_clock::time_point m_lastFrameTime; }; diff --git a/Code/encoding/Structs/ServerSettings.cpp b/Code/encoding/Structs/ServerSettings.cpp index 9c761d82d..a88d7f2c1 100644 --- a/Code/encoding/Structs/ServerSettings.cpp +++ b/Code/encoding/Structs/ServerSettings.cpp @@ -16,12 +16,14 @@ bool ServerSettings::operator!=(const ServerSettings& acRhs) const noexcept void ServerSettings::Serialize(TiltedPhoques::Buffer::Writer& aWriter) const noexcept { Serialization::WriteVarInt(aWriter, Difficulty); - Serialization::WriteBool(aWriter, EnableGreetings); + Serialization::WriteBool(aWriter, GreetingsEnabled); + Serialization::WriteBool(aWriter, PvpEnabled); } void ServerSettings::Deserialize(TiltedPhoques::Buffer::Reader& aReader) noexcept { Difficulty = Serialization::ReadVarInt(aReader) & 0xFFFFFFFF; - EnableGreetings = Serialization::ReadBool(aReader); + GreetingsEnabled = Serialization::ReadBool(aReader); + PvpEnabled = Serialization::ReadBool(aReader); } diff --git a/Code/encoding/Structs/ServerSettings.h b/Code/encoding/Structs/ServerSettings.h index e89c55e39..c980910ad 100644 --- a/Code/encoding/Structs/ServerSettings.h +++ b/Code/encoding/Structs/ServerSettings.h @@ -13,5 +13,6 @@ struct ServerSettings void Deserialize(TiltedPhoques::Buffer::Reader& aReader) noexcept; uint32_t Difficulty{}; - bool EnableGreetings{}; + bool GreetingsEnabled{}; + bool PvpEnabled{}; }; diff --git a/Code/server/GameServer.cpp b/Code/server/GameServer.cpp index 9a584a7b1..e0186d586 100644 --- a/Code/server/GameServer.cpp +++ b/Code/server/GameServer.cpp @@ -42,6 +42,7 @@ Console::Setting bEnableMoPo{"ModPolicy:bEnabled", "Bypass the mod policy restri Console::SettingsFlags::kHidden | Console::SettingsFlags::kLocked}; Console::Setting uDifficulty{"Gameplay:uDifficulty", "In game difficulty (0 to 5)", 4u}; Console::Setting bEnableGreetings{"Gameplay:bEnableGreetings", "Enables NPC greetings (disabled by default since they can be spammy with dialogue sync)", false}; +Console::Setting bEnablePvp{"Gameplay:bEnablePvp", "Enables pvp", true}; // -- Commands -- Console::Command TogglePremium("TogglePremium", "Toggle the premium mode", [](Console::ArgStack& aStack) { bPremiumTickrate = aStack.Pop(); }); @@ -565,7 +566,8 @@ void GameServer::HandleAuthenticationRequest(const ConnectionId_t aConnectionId, acRequest->UserMods.ModList.size(), modList.c_str()); serverResponse.Settings.Difficulty = uDifficulty.value_as(); - serverResponse.Settings.EnableGreetings = bEnableGreetings; + serverResponse.Settings.GreetingsEnabled = bEnableGreetings; + serverResponse.Settings.PvpEnabled = bEnablePvp; serverResponse.Type = AuthenticationResponse::ResponseType::kAccepted; Send(aConnectionId, serverResponse);