Skip to content

Commit

Permalink
feat: server side pvp setting
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 10, 2022
1 parent 9c4d951 commit 7ffe66b
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Code/client/Games/Skyrim/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion Code/client/Games/Skyrim/Magic/MagicTarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion Code/client/Services/Generic/PlayerService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions Code/client/Services/Generic/TransportService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 5 additions & 0 deletions Code/client/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <Systems/ModSystem.h>

#include <Structs/ServerSettings.h>

struct World : entt::registry
{
Expand All @@ -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;
Expand All @@ -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;
};
6 changes: 4 additions & 2 deletions Code/encoding/Structs/ServerSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

3 changes: 2 additions & 1 deletion Code/encoding/Structs/ServerSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ struct ServerSettings
void Deserialize(TiltedPhoques::Buffer::Reader& aReader) noexcept;

uint32_t Difficulty{};
bool EnableGreetings{};
bool GreetingsEnabled{};
bool PvpEnabled{};
};
4 changes: 3 additions & 1 deletion Code/server/GameServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> TogglePremium("TogglePremium", "Toggle the premium mode",
[](Console::ArgStack& aStack) { bPremiumTickrate = aStack.Pop<bool>(); });
Expand Down Expand Up @@ -565,7 +566,8 @@ void GameServer::HandleAuthenticationRequest(const ConnectionId_t aConnectionId,
acRequest->UserMods.ModList.size(), modList.c_str());

serverResponse.Settings.Difficulty = uDifficulty.value_as<uint8_t>();
serverResponse.Settings.EnableGreetings = bEnableGreetings;
serverResponse.Settings.GreetingsEnabled = bEnableGreetings;
serverResponse.Settings.PvpEnabled = bEnablePvp;

serverResponse.Type = AuthenticationResponse::ResponseType::kAccepted;
Send(aConnectionId, serverResponse);
Expand Down

0 comments on commit 7ffe66b

Please sign in to comment.